【问题标题】:Ptrace/wait on a non childPtrace/等待非孩子
【发布时间】:2013-12-04 02:23:08
【问题描述】:
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

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

【问题讨论】:

  • 你不检查ptrace是否返回错误。
  • 这表明ptrace() 失败了。检查errno 找出原因。
  • 工作。 :) 我有些权限问题谢谢。
  • 哎呀。如果我不是 root 则无法工作:)

标签: c ptrace waitpid


【解决方案1】:

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

这是记录在案的行为;见ptrace() - Unix, Linux System Call e。 g.

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

发出信号

【讨论】:

    【解决方案2】:
    if(ret == 0)
    {
        //child process
    }
    else
    {
        //parent process
    }
    

    【讨论】:

    • 我想你错过了“简而言之,我希望能够等待非子进程 - 欢迎任何其他程序。”
    猜你喜欢
    • 2023-03-19
    • 2016-11-30
    • 2021-04-28
    • 2012-09-10
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    相关资源
    最近更新 更多