#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #define fail(msg, args...) \ do { \ fprintf(stderr, "FAIL:%i: " msg "\n", __LINE__, ## args); \ exit(1); \ } while (0) static void child_exit(int sig) { int status; printf("failure, child exited with %i: %s\n", sig, strsignal(sig)); printf("wait() = %i\n", wait(&status)); printf("status = %i\n", status); printf("\tWIFEXITED = %i\n", WIFEXITED(status)); printf("\tWEXITSTATUS = %i\n", WEXITSTATUS(status)); printf("\tWIFSIGNALED = %i\n", WIFSIGNALED(status)); printf("\tWTERMSIG = %i (%s)\n", WTERMSIG(status), strsignal(WTERMSIG(status))); exit(1); } int main(int argc, char *argv[]) { long pret; pid_t pid; /* child process ... shouldnt be executed, but just in case ... */ if (argc > 1 && !strcmp(argv[1], "child")) fail("kernel should have halted me..."); pid = vfork(); if (pid == -1) fail("vfork() didnt work: %m"); else if (!pid) { /* do the child stuff here */ errno = 0; pret = ptrace(PTRACE_TRACEME, 0, NULL, NULL); if (pret && errno) fail("ptrace(PTRACE_TRACEME) = %li: %m", pret); int eret = execlp(argv[0], argv[0], "child", NULL); fail("execlp() = %i", eret); } /* do the parent stuff here */ signal(SIGCHLD, child_exit); errno = 0; pret = ptrace(PTRACE_PEEKUSER, pid, NULL, NULL); if (pret && errno) fail("ptrace(PTRACE_PEEKUSER, %i) = %li: %m", pid, pret); puts("SUCCESS! :D"); return 0; }