0
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);
}

-- 我用 pid 6841 运行了一个守护进程,并试图在 ptrace-attach 之后等待它

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

简而言之,我希望能够等待非子进程 - 欢迎任何其他程序。

4

2 回答 2

0

哎呀。如果我不是 root 则无法工作:)

这是记录在案的行为;请参阅ptrace() - Unix、Linux 系统调用,例如

非根进程无法跟踪它们无法向其发送信号的进程

于 2017-01-10T13:18:04.543 回答
-1
if(ret == 0)
{
    //child process
}
else
{
    //parent process
}
于 2016-03-03T09:02:42.080 回答