分页: 1 / 1

[已解决]linux里有没有使进程创建后马上挂起的函数

发表于 : 2023-08-10 10:25
wxf
好像没有是吧,fork exec, 甚至clone都没有这样的功能啊。

windows里,CreateProcess函数执行时,可以传递一个参数 CREATE_SUSPENDED,使进程创建后不执行,然后父进程可以对这个进程做一些设置,比如注入一个dll,然后调用ResumeThread(lpProcessInformation->hThread); 恢复进程的执行。

linux里没有这样的机制吗?

Re: linux里有没有使进程创建后马上挂起的函数

发表于 : 2023-08-10 13:44
astolia

代码: 全选

pid_t pid = fork();
if (pid == 0) {
    ptrace(PTRACE_TRACEME, 0, NULL, 0);
    execlp("ls", "ls", NULL);
} else {
    int status;
    waitpid(pid, &status, 0);
    if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
        printf("child %d suspended, press Enter to continue\n", pid);
        getchar();
        ptrace(PTRACE_CONT, pid, NULL, 0);
    }
}

Re: linux里有没有使进程创建后马上挂起的函数

发表于 : 2023-08-10 14:35
wxf
astolia 写了: 2023-08-10 13:44

代码: 全选

pid_t pid = fork();
if (pid == 0) {
    ptrace(PTRACE_TRACEME, 0, NULL, 0);
    execlp("ls", "ls", NULL);
} else {
    int status;
    waitpid(pid, &status, 0);
    if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
        printf("child %d suspended, press Enter to continue\n", pid);
        getchar();
        ptrace(PTRACE_CONT, pid, NULL, 0);
    }
}
get到知识了!谢谢大佬