gpt4 book ai didi

c - 跟踪/等待非子进程

转载 作者:行者123 更新时间:2023-11-30 18:13:56 28 4
gpt4 key购买 nike

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

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

最佳答案

oops. not working if Iam not root :)

这是有记录的行为;请参阅ptrace() - Unix, Linux System Call e.克。

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

关于c - 跟踪/等待非子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20365656/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com