我想知道是否有启动进程挂起的方法?
类似于 Windows 中的 CreateProcess + CREATE_SUSPENDED:
CreateProcessA(
NULL,
CmdLine,
NULL,
NULL,
FALSE,
CREATE_SUSPENDED,
NULL,
"C:\\Windows\\System32\\",
&si,
&pi);
ptrace 似乎只支持 PTRACE_ATTACH,没有办法启动一个进程并直接挂起它,有什么想法吗?
编辑
我需要能够像这样捕捉过程,
int main()
{
exit(0);
}
因此 shell 方法将不起作用,因为进程退出速度非常快。
最佳答案
一种可能的方法是将流程详细信息作为流程、fork 和 child 中的输入,然后再执行流程,向自己发送停止信号。
示例代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
main()
{
int pid;
//Note: Replace argv_list with corresponding binary and arguments
//Can also get these values as command line argument from main(argc, argv)
char *argv_list[] = {"/usr/bin/ls","-lrt", "/", NULL};
if((pid=fork())==0)
{
printf("Child Executing\n");
//Send STOP signal to self to suspend
kill(getpid(), SIGSTOP);
//Sleep for stop signal handler to suspend current process
sleep(1);
//This line should start to execute only after CONT signal is sent.
execv(argv_list[0],argv_list);
}
else
{
printf("Child PID:%d, Parent continuing\n", pid);
}
printf("\n***Main - Exiting\n");
exit(0);
}
关于CreateProcess + CREATE_SUSPENDED 标志在 Linux 中是否等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54509868/