| 1 | use std::fmt; |
| 2 | |
| 3 | use crate::system::interface::ProcessId; |
| 4 | |
| 5 | use super::SignalNumber; |
| 6 | |
| 7 | /// Information related to the arrival of a signal. |
| 8 | #[repr(transparent)] |
| 9 | pub(crate) struct SignalInfo { |
| 10 | info: libc::siginfo_t, |
| 11 | } |
| 12 | |
| 13 | impl SignalInfo { |
| 14 | pub(super) const SIZE: usize = std::mem::size_of::<Self>(); |
| 15 | |
| 16 | /// Returns whether the signal was sent by the user or not. |
| 17 | fn is_user_signaled(&self) -> bool { |
| 18 | // This matches the definition of the SI_FROMUSER macro. |
| 19 | self.info.si_code <= 0 |
| 20 | } |
| 21 | |
| 22 | /// Gets the PID that sent the signal. |
| 23 | pub(crate) fn signaler_pid(&self) -> Option<ProcessId> { |
| 24 | if self.is_user_signaled() { |
| 25 | // SAFETY: si_pid is always initialized if the signal is user signaled. |
| 26 | let id = unsafe { self.info.si_pid() }; |
| 27 | Some(ProcessId::new(id)) |
| 28 | } else { |
| 29 | None |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /// Gets the signal number. |
| 34 | pub(crate) fn signal(&self) -> SignalNumber { |
| 35 | self.info.si_signo |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl fmt::Display for SignalInfo { |
| 40 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 41 | write!( |
| 42 | f, |
| 43 | "{} {} from ", |
| 44 | if self.is_user_signaled() { |
| 45 | " user signaled" |
| 46 | } else { |
| 47 | "" |
| 48 | }, |
| 49 | self.signal(), |
| 50 | )?; |
| 51 | if let Some(pid) = self.signaler_pid() { |
| 52 | write!(f, "{pid}") |
| 53 | } else { |
| 54 | write!(f, "<none>") |
| 55 | } |
| 56 | } |
| 57 | } |