PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ptracetest2.c

http://github.com/Eelis/geordi
C | 40 lines | 30 code | 8 blank | 2 comment | 4 complexity | 5ee060b9285ab161b14feb9ba56efd6e MD5 | raw file
 1
 2#include <assert.h>
 3#include <sys/ptrace.h>
 4#include <linux/ptrace.h>
 5#include <sys/types.h>
 6#include <sys/stat.h>
 7#include <sys/wait.h>
 8#include <unistd.h>
 9#include <syscall.h>
10#include <sys/reg.h>
11#include <stdio.h>
12#include <stdlib.h>
13
14#ifdef __x86_64__
15#define SYSCALL_OFF (ORIG_RAX * 8)
16#else
17#define SYSCALL_OFF (ORIG_EAX * 4)
18#endif
19
20void checked (char const * const s, int const r) { if (r == -1) { perror(s); abort(); } }
21
22int main()
23{
24  pid_t const child = fork();
25
26  checked("fork", child);
27
28  if(child == 0)
29  {
30    checked("ptrace", ptrace(PTRACE_TRACEME, 0, NULL, NULL));
31    sleep(2);
32    checked("execl", execl("/usr/bin/whoami", "whoami", NULL));
33  }
34
35  checked("ptrace", ptrace(PTRACE_SETOPTIONS, child, NULL, PTRACE_O_TRACESYSGOOD));
36    // Will fail saying: ptrace: No such process
37    // Conclusion: disregarding sleep-solutions, this means the parent cannot reliably set PTRACE_O_TRACESYSGOOD before the first wait().
38
39  return 0;
40}