|
| 1 | +//! Low-level internals of [`signal-hook`](https://docs.rs/signal-hook). |
| 2 | +//! |
| 3 | +//! This crate contains some internal APIs, split off to a separate crate for technical reasons. Do |
| 4 | +//! not use directly. There are no stability guarantees, no documentation and you should use |
| 5 | +//! `signal-hook` directly. |
| 6 | +
|
| 7 | +#[doc(hidden)] |
| 8 | +pub mod internal { |
| 9 | + use libc::{pid_t, siginfo_t, uid_t}; |
| 10 | + |
| 11 | + // Careful: make sure the signature and the constants match the C source |
| 12 | + extern "C" { |
| 13 | + fn sighook_signal_cause(info: &siginfo_t) -> Cause; |
| 14 | + fn sighook_signal_pid(info: &siginfo_t) -> pid_t; |
| 15 | + fn sighook_signal_uid(info: &siginfo_t) -> uid_t; |
| 16 | + } |
| 17 | + |
| 18 | + // Warning: must be in sync with the C code |
| 19 | + #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 20 | + #[non_exhaustive] |
| 21 | + #[repr(u8)] |
| 22 | + pub enum Cause { |
| 23 | + Unknown = 0, |
| 24 | + Kernel = 1, |
| 25 | + User = 2, |
| 26 | + TKill = 3, |
| 27 | + Queue = 4, |
| 28 | + MesgQ = 5, |
| 29 | + Exited = 6, |
| 30 | + Killed = 7, |
| 31 | + Dumped = 8, |
| 32 | + Trapped = 9, |
| 33 | + Stopped = 10, |
| 34 | + Continued = 11, |
| 35 | + } |
| 36 | + |
| 37 | + impl Cause { |
| 38 | + // The MacOs doesn't use the SI_* constants and leaves si_code at 0. But it doesn't use an |
| 39 | + // union, it has a good-behaved struct with fields and therefore we *can* read the values, |
| 40 | + // even though they'd contain nonsense (zeroes). We wipe that out later. |
| 41 | + #[cfg(target_os = "macos")] |
| 42 | + fn has_process(self) -> bool { |
| 43 | + true |
| 44 | + } |
| 45 | + |
| 46 | + #[cfg(not(target_os = "macos"))] |
| 47 | + fn has_process(self) -> bool { |
| 48 | + use Cause::*; |
| 49 | + match self { |
| 50 | + Unknown | Kernel => false, |
| 51 | + User | TKill | Queue | MesgQ | Exited | Killed | Dumped | Trapped | Stopped |
| 52 | + | Continued => true, |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 58 | + #[non_exhaustive] |
| 59 | + pub struct Process { |
| 60 | + pub pid: pid_t, |
| 61 | + pub uid: uid_t, |
| 62 | + } |
| 63 | + |
| 64 | + impl Process { |
| 65 | + /** |
| 66 | + * Extract the process information. |
| 67 | + * |
| 68 | + * # Safety |
| 69 | + * |
| 70 | + * The `info` must have a `si_code` corresponding to some situation that has the `si_pid` |
| 71 | + * and `si_uid` filled in. |
| 72 | + */ |
| 73 | + unsafe fn extract(info: &siginfo_t) -> Self { |
| 74 | + Self { |
| 75 | + pid: sighook_signal_pid(info), |
| 76 | + uid: sighook_signal_uid(info), |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + pub const fn to_u64(self) -> u64 { |
| 81 | + let pid = self.pid as u32; // With overflow for negative ones |
| 82 | + let uid = self.uid as u32; |
| 83 | + ((pid as u64) << 32) | (uid as u64) |
| 84 | + } |
| 85 | + |
| 86 | + pub const fn from_u64(encoded: u64) -> Self { |
| 87 | + let pid = ((encoded >> 32) as u32) as _; |
| 88 | + let uid = (encoded as u32) as _; |
| 89 | + Self { pid, uid } |
| 90 | + } |
| 91 | + |
| 92 | + pub const EMPTY: Self = Self { pid: -1, uid: 0 }; |
| 93 | + |
| 94 | + pub const NO_PROCESS: Self = Self { pid: -1, uid: 1 }; |
| 95 | + } |
| 96 | + |
| 97 | + #[derive(Clone, Debug, Eq, PartialEq)] |
| 98 | + #[non_exhaustive] |
| 99 | + pub struct SigInfo { |
| 100 | + pub cause: Cause, |
| 101 | + pub process: Option<Process>, |
| 102 | + } |
| 103 | + |
| 104 | + impl SigInfo { |
| 105 | + // Note: shall be async-signal-safe |
| 106 | + pub fn extract(info: &siginfo_t) -> Self { |
| 107 | + let cause = unsafe { sighook_signal_cause(info) }; |
| 108 | + let process = if cause.has_process() { |
| 109 | + let process = unsafe { Process::extract(info) }; |
| 110 | + // On macos we don't have the si_code to go by, but we can go by the values being |
| 111 | + // empty there. |
| 112 | + if cfg!(target_os = "macos") && process.pid == 0 && process.uid == 0 { |
| 113 | + None |
| 114 | + } else { |
| 115 | + Some(process) |
| 116 | + } |
| 117 | + } else { |
| 118 | + None |
| 119 | + }; |
| 120 | + Self { cause, process } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments