Hard
A minimal ptrace() loop and an external SIGTERM
A tiny tracer forks, has the child call PTRACE_TRACEME and exec a server, then runs the loop below. The server keeps the default disposition for SIGTERM. While it runs, an operator runs kill -TERM <server pid> from another shell. What happens?
tracer.cpp
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
execl("./server", "server", nullptr);
_exit(127);
}
for (;;) {
int status;
if (waitpid(pid, &status, 0) == -1) break;
if (WIFEXITED(status) || WIFSIGNALED(status)) break;
ptrace(PTRACE_CONT, pid, nullptr, nullptr); // data argument = 0
}
}48 of 53 in Operating Systems