#include #include #include #include #include #include #include static void sighup(int sig) { } int main(int argc, char **argv) { struct sigaction act; pid_t pid; int status; pid = fork(); if (pid < 0) err(1, "fork"); if (pid == 0) { act.sa_handler = sighup; act.sa_flags = 0; sigemptyset(&act.sa_mask); if (sigaction(SIGHUP, &act, NULL) != 0) err(1, "sigaction"); while (1) { sleep(1); } } else { sleep(1); /* give the child a chance to call sigaction */ if (kill(pid, SIGSTOP) != 0) err(1, "kill(SIGSTOP)"); printf("waiting for child to stop...\n"); if (waitpid(pid, &status, WUNTRACED) != pid) err(1, "waitpid"); if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) errx(1, "unexpected status %d after SIGSTOP", status); if (kill(pid, SIGHUP) != 0) err(1, "kill(SIGHUP)"); if (ptrace(PT_ATTACH, pid, NULL, 0) != 0) err(1, "ptrace(PT_ATTACH)"); if (waitpid(pid, &status, WUNTRACED) != pid) err(1, "waitpid"); if (!WIFSTOPPED(status)) errx(1, "unexpected status %d after PT_ATTACH", status); printf("stopping signal is %d\n", WSTOPSIG(status)); if (ptrace(PT_DETACH, pid, NULL, 0) != 0) err(1, "ptrace(PT_DETACH)"); /* if ptrace works as expected, we'll block here */ printf("waiting on child...\n"); if (waitpid(pid, &status, WUNTRACED) != pid) err(1, "waitpid"); if (!WIFSTOPPED(status)) errx(1, "unexpected status %d after PT_DETACH", status); printf("child is stopped after detach (sig %d)\n", WSTOPSIG(status)); } return (0); }