| 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 |
#ifdef SI_MESGQ |
| 27 |
{ SI_MESGQ, -1, 5 }, |
| 28 |
#endif |
| 29 |
{ CLD_EXITED, SIGCHLD, 6 }, |
| 30 |
{ CLD_KILLED, SIGCHLD, 7 }, |
| 31 |
{ CLD_DUMPED, SIGCHLD, 8 }, |
| 32 |
{ CLD_TRAPPED, SIGCHLD, 9 }, |
| 33 |
{ CLD_STOPPED, SIGCHLD, 10 }, |
| 34 |
{ CLD_CONTINUED, SIGCHLD, 11 }, |
| 35 |
}; |
| 36 |
|
| 37 |
uint8_t sighook_signal_cause(const siginfo_t *info) { |
| 38 |
const size_t const_len = sizeof consts / sizeof *consts; |
| 39 |
size_t i; |
| 40 |
for (i = 0; i < const_len; i ++) { |
| 41 |
if ( |
| 42 |
consts[i].native == info->si_code && |
| 43 |
(consts[i].signal == -1 || consts[i].signal == info->si_signo) |
| 44 |
) { |
| 45 |
return consts[i].translated; |
| 46 |
} |
| 47 |
} |
| 48 |
return 0; // The "Unknown" variant |
| 49 |
} |
| 50 |
|
| 51 |
pid_t sighook_signal_pid(const siginfo_t *info) { |
| 52 |
return info->si_pid; |
| 53 |
} |
| 54 |
|
| 55 |
uid_t sighook_signal_uid(const siginfo_t *info) { |
| 56 |
return info->si_uid; |
| 57 |
} |