Ptrace/wait on a non child

2.1k views Asked by At
int Enable ( int pid) 
{
int status;
#if 1 
    {
    printf ( "child pid = %d \n", pid );
long ret = ptrace (PTRACE_ATTACH, pid, NULL, NULL);


        do {
            int w = waitpid(-1, &status, 0);
            if (w == -1) {
                perror("waitpid error :");
                exit(EXIT_FAILURE);
            }

            if (WIFEXITED(status)) {
                printf("exited, status=%d\n", WEXITSTATUS(status));
            } else if (WIFSIGNALED(status)) {
                printf("killed by signal %d\n", WTERMSIG(status));
            } else if (WIFSTOPPED(status)) {
                printf("stopped by signal %d\n", WSTOPSIG(status));
            } else if (WIFCONTINUED(status)) {
                printf("continued\n");
            }
        } while (!WIFEXITED(status) && !WIFSIGNALED(status));

        exit(EXIT_SUCCESS);
    }
#endif
//  while ((result = wait(&status)) != -1 && result != pid){ printf (" this is not my child go back \n"); };
}
int main(int arg, char*argv[])
{

    Enable(atoi(argv[1]));
    sleep(125);
}

-- I ran a daemon with pid 6841 and tried to wait on it after ptrace-attach

./ptrace 6841
child pid = 6841 
waitpid error :: No child processes

In short I want to be able to wait on a non-child process - any other program welcome.

2

There are 2 answers

0
Armali On

oops. not working if Iam not root :)

This is documented behavior; see ptrace() - Unix, Linux System Call e. g.

non-root processes cannot trace processes that they cannot send signals to

1
demonshir On
if(ret == 0)
{
    //child process
}
else
{
    //parent process
}