8 Replies - 482 Views - Last Post: 30 November 2017 - 06:22 PM Rate Topic: -----

#1 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

setpgid returns error when trying to set parent to child id.

Posted 29 November 2017 - 02:26 PM

I'm trying to set the parent process to a dummy child's group id, so I can change the session id of the parent. I'm trying to change the id so, I can switch which terminal the code runs in, much like this post goes into. Though I get an error code from trying to do this.

The code in question:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>

void sig_exit(int signum)
{
    _Exit(0);
}

void sig_wait(int signum)
{

}

pid_t change_process_group()
{
    pid_t child_pid;
    if ((child_pid = fork()) < 0)
    {
        perror("fork failed while attaching to term");
        exit(1);
    }
    if (child_pid == 0)
    {
//        pid_t parent = getppid();
        setpgid(0, getpid());

        signal(SIGUSR1, sig_exit); // wait till parent tells child to exit

//        sleep(5);
//        kill(parent, SIGUSR2);
        pause();
        printf("Shouldn't reach this\n");
    }

//    sleep(5);
//    signal(SIGUSR2, sig_wait);
//    pause();
    int parent_pid = getpid();
    int code = setpgid(getpgid(child_pid), parent_pid); // need child process group
    printf("%s\n", strerror(errno));
	pid_t sid = setsid();
    
    printf("Printing to this console\n");

    return  child_pid;
}

main()
{
   pid_t child = change_process_group();

   kill(child, SIGUSR1);
}



The commented out lines were from me thinking the child's group id wasn't being changed before trying to set the parent's group id, but I still get an EPROM from that. Both processes have the same session id. Is there any function calls I'm missing, or security settings needing to be addressed that would allow me to change the group id of the parent to the child?

This post has been edited by zerophase: 29 November 2017 - 06:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: setpgid returns error when trying to set parent to child id.

#2 COKEDUDE  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 268
  • Joined: 01-February 10

Re: setpgid returns error when trying to set parent to child id.

Posted 29 November 2017 - 04:41 PM

Whats the error code you get? What are all the different #includes we need to test your code?
Was This Post Helpful? 0
  • +
  • -

#3 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Re: setpgid returns error when trying to set parent to child id.

Posted 29 November 2017 - 06:23 PM

The error is operation not permitted.
Was This Post Helpful? 0
  • +
  • -

#4 COKEDUDE  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 268
  • Joined: 01-February 10

Re: setpgid returns error when trying to set parent to child id.

Posted 30 November 2017 - 12:55 AM

What are all the different #includes we need to test your code?

When I run your code I get Interrupted system call. I did also uncomment your comments.
Was This Post Helpful? 0
  • +
  • -

#5 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Re: setpgid returns error when trying to set parent to child id.

Posted 30 November 2017 - 10:20 AM

I'm pretty sure I added all of the required includes to the post.

This post has been edited by Skydiver: 30 November 2017 - 06:23 PM
Reason for edit:: No need to quote the post above yours. Just use the big Reply button.

Was This Post Helpful? 0
  • +
  • -

#6 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Re: setpgid returns error when trying to set parent to child id.

Posted 30 November 2017 - 03:34 PM

I have an updated version that works as expected, except for when gdb is attached for debugging purposes. Really unsure how I should address having the debugger attached. I'm using CLion as the ide, but pretty sure anything that uses gdb would reproduce the problem.

Entire code sample:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/ptrace.h>

void sig_exit(int signum)
{
    _Exit(0);
}

pid_t change_process_group()
{
    pid_t child_pid;
    int child_ready_pipe[2];
    if (pipe(child_ready_pipe) < 0)
    {
        perror("pipe");
        exit(1);
    }
    if ((child_pid = fork()) < 0)
    {
        perror("fork failed while attaching to term");
        exit(1);
    }
    if (child_pid == 0)
    {
        close(child_ready_pipe[0]);
        sleep(1); // mimic slow start of the child
        if (setpgid(0, 0))
            perror("child setpgid to create group");
        close(child_ready_pipe[1]);

        signal(SIGUSR1, sig_exit); // wait till parent tells child to exit
        pause();
        printf("Shouldn't reach this\n");
    }

    close(child_ready_pipe[1]);
    int parent_pid = getpid();
    char ignored;
    read(child_ready_pipe[0], &ignored, 1);
    close(child_ready_pipe[0]);
    if (setpgid(parent_pid, child_pid) < 0) // need child process group
        perror("parent setpgid");
    if (setsid() < 0)
        perror("parent setsid");

    return  child_pid;
}

main()
{ 
    pid_t child = change_process_group();
    //... Other code unrelated.
    
    kill(child, SIGUSR1);
}



The error message happen at parent setpgid and parent setsid.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6118
  • View blog
  • Posts: 21,072
  • Joined: 05-May 12

Re: setpgid returns error when trying to set parent to child id.

Posted 30 November 2017 - 04:11 PM

Typically when you get inconsistent behavior when running in a debugger vs. not, it's a sign of using an uninitialized variable (or the using memory out of bounds which is of course essentially uninitialized data).
Was This Post Helpful? 0
  • +
  • -

#8 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Re: setpgid returns error when trying to set parent to child id.

Posted 30 November 2017 - 05:43 PM

I think it has something to do with the session leader. Going to be looking into that for the next day or so.

This post has been edited by Skydiver: 30 November 2017 - 06:23 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6118
  • View blog
  • Posts: 21,072
  • Joined: 05-May 12

Re: setpgid returns error when trying to set parent to child id.

Posted 30 November 2017 - 06:22 PM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1