SystemCall儿童父母执行问题(fork Execve)Linux
我是SystemCall的新手。我在孩子内使用execve,因此父母应像正常情况一样运行,而不是由execve打开。问题是孩子在执行后执行并停止整个事情。我的目标是计算Execve执行的BASH命令(参数)中执行的克隆数量。
我读了那个男人,我仍然有些困惑。 我只能使用ptrace,fork,等待/等待。
争论:
/bin/bash -c "echo 'first test' | wc -c"
int main(int argc, char *argv[]) {
pid_t child_pid = fork();
int status;
int counter = 0;
wait(&status);
if (child_pid == -1) {
exit(1);
} else {
while(status != child_pid){
if (child_pid == 0) {
ptrace(PTRACE_TRACEME, child_pid, 0, 0);
raise(SIGSTOP);
execve(argv[1], &argv[1], NULL);
ptrace(PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACECLONE);
ptrace(PTRACE_CONT, child_pid, 0L, 0L);
if(status>>8 == (SIGTRAP | (PTRACE_EVENT_CLONE<<8)))
counter++;
}
}
}
printf("# of clone executions: %d", counter);
return 0;
}
Im new to systemcall. Im using execve inside a child so the parent should run as normal and not be ovewritten by execve. The problem is that the child executes and stops the whole thing after execve. My goal here is to count the number of clone executed in the bash command(argument) executed by execve.
Ive read the man, Im still a bit confused.
I can only use ptrace, fork, wait/waitpid.
argument:
/bin/bash -c "echo 'first test' | wc -c"
int main(int argc, char *argv[]) {
pid_t child_pid = fork();
int status;
int counter = 0;
wait(&status);
if (child_pid == -1) {
exit(1);
} else {
while(status != child_pid){
if (child_pid == 0) {
ptrace(PTRACE_TRACEME, child_pid, 0, 0);
raise(SIGSTOP);
execve(argv[1], &argv[1], NULL);
ptrace(PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACECLONE);
ptrace(PTRACE_CONT, child_pid, 0L, 0L);
if(status>>8 == (SIGTRAP | (PTRACE_EVENT_CLONE<<8)))
counter++;
}
}
}
printf("# of clone executions: %d", counter);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
execve
将覆盖子进程,因此execve
之后的任何指令都不会执行,除非呼叫execve
失败。取而代之的是,您应该运行ptrace
跟踪execve
在父进程编辑中出现:这是一个评论的解决方案,用于计算clone syscalls的数量:
您可以交叉检查它反对strace
strace 2&gt;&amp; 1 your_command | grep克隆| WC -L
execve
will overwrite the child process, so any instruction afterexecve
will not be executed, unless the call toexecve
fails. You should instead run theptrace
tracing that appears after theexecve
in the parent processEdit: here is a commented solution to count the number of clone syscalls:
You can cross-check it against strace
strace 2>&1 your_command | grep clone | wc -l