| 1 | module os |
| 2 | |
| 3 | #include <sys/ptrace.h> |
| 4 | |
| 5 | fn C.ptrace(i32, u32, voidptr, i32) i32 |
| 6 | |
| 7 | // debugger_present returns a bool indicating if the process is being debugged. |
| 8 | pub fn debugger_present() bool { |
| 9 | $if openbsd { |
| 10 | // check if a child process could trace its parent process, |
| 11 | // if not a debugger must be present |
| 12 | pid := fork() |
| 13 | if pid == 0 { |
| 14 | ppid := getppid() |
| 15 | /* |
| 16 | * On OpenBSD, impossible to trace a parent process from its child. |
| 17 | * Possible errors: |
| 18 | * - EBUSY: process already traced |
| 19 | * - EPERM: process is not a child of tracer (sysctl kern.global_ptrace=0) |
| 20 | * - EINVAL: process is an ancestor of the current process and not init |
| 21 | * (sysctl kern.global_ptrace=1) |
| 22 | */ |
| 23 | if C.ptrace(C.PT_ATTACH, ppid, unsafe { nil }, 0) < 0 { |
| 24 | if C.errno == C.EBUSY { |
| 25 | // external debugger must be present |
| 26 | exit(1) |
| 27 | } else { |
| 28 | // no external debugger |
| 29 | exit(0) |
| 30 | } |
| 31 | } |
| 32 | } else { |
| 33 | mut status := 0 |
| 34 | // wait until the child process dies |
| 35 | C.waitpid(pid, &status, 0) |
| 36 | // check the exit code of the child process check |
| 37 | if posix_wait_status_exit_code(status) == 0 { |
| 38 | return false |
| 39 | } else { |
| 40 | return true |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | return false |
| 45 | } |
| 46 | |