1 /*
2  * Low-level extraction code to overcome rust's libc not having the best access
3  * to siginfo_t details.
4  */
5 #include <stdbool.h>
6 #include <signal.h>
7 #include <stdint.h>
8 
9 struct Const {
10     int native;
11     // The signal this applies to, or -1 if it applies to anything.
12     int signal;
13     uint8_t translated;
14 };
15 
16 // Warning: must be in sync with the rust source code
17 struct Const consts[] = {
18 #ifdef SI_KERNEL
19     { SI_KERNEL, -1, 1 },
20 #endif
21     { SI_USER, -1, 2 },
22 #ifdef SI_TKILL
23     { SI_TKILL, -1, 3 },
24 #endif
25     { SI_QUEUE, -1, 4 },
26     { SI_MESGQ, -1, 5 },
27     { CLD_EXITED, SIGCHLD, 6 },
28     { CLD_KILLED, SIGCHLD, 7 },
29     { CLD_DUMPED, SIGCHLD, 8 },
30     { CLD_TRAPPED, SIGCHLD, 9 },
31     { CLD_STOPPED, SIGCHLD, 10 },
32     { CLD_CONTINUED, SIGCHLD, 11 },
33 };
34 
sighook_signal_cause(const siginfo_t * info)35 uint8_t sighook_signal_cause(const siginfo_t *info) {
36     const size_t const_len = sizeof consts / sizeof *consts;
37     size_t i;
38     for (i = 0; i < const_len; i ++) {
39         if (
40             consts[i].native == info->si_code &&
41             (consts[i].signal == -1 || consts[i].signal == info->si_signo)
42         ) {
43             return consts[i].translated;
44         }
45     }
46     return 0; // The "Unknown" variant
47 }
48 
sighook_signal_pid(const siginfo_t * info)49 pid_t sighook_signal_pid(const siginfo_t *info) {
50     return info->si_pid;
51 }
52 
sighook_signal_uid(const siginfo_t * info)53 uid_t sighook_signal_uid(const siginfo_t *info) {
54     return info->si_uid;
55 }
56