| 1 | //! Utilities to handle signals. |
| 2 | mod handler; |
| 3 | mod info; |
| 4 | mod set; |
| 5 | mod state; |
| 6 | mod stream; |
| 7 | |
| 8 | pub(crate) use handler::{SignalHandler, SignalHandlerBehavior}; |
| 9 | pub(crate) use set::{SignalSet, take_first_pending}; |
| 10 | pub(crate) use state::SignalsState; |
| 11 | pub(crate) use stream::{SignalStream, register_handlers}; |
| 12 | |
| 13 | use std::borrow::Cow; |
| 14 | use std::convert::Infallible; |
| 15 | use std::ffi::c_int; |
| 16 | |
| 17 | use crate::system::signal::set::SignalAction; |
| 18 | |
| 19 | pub(crate) type SignalNumber = c_int; |
| 20 | |
| 21 | macro_rules! define_consts { |
| 22 | ($($signal:ident,)*) => { |
| 23 | pub(crate) mod consts { |
| 24 | pub(crate) use libc::{$($signal,)*}; |
| 25 | } |
| 26 | |
| 27 | pub(crate) fn signal_name(signal: SignalNumber) -> Cow<'static, str> { |
| 28 | match signal { |
| 29 | $(consts::$signal => stringify!($signal).into(),)* |
| 30 | _ => format!("unknown signal ({signal})").into(), |
| 31 | } |
| 32 | } |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | define_consts! { |
| 37 | SIGINT, |
| 38 | SIGQUIT, |
| 39 | SIGTSTP, |
| 40 | SIGTERM, |
| 41 | SIGHUP, |
| 42 | SIGALRM, |
| 43 | SIGPIPE, |
| 44 | SIGUSR1, |
| 45 | SIGUSR2, |
| 46 | SIGCHLD, |
| 47 | SIGCONT, |
| 48 | SIGWINCH, |
| 49 | SIGTTIN, |
| 50 | SIGTTOU, |
| 51 | SIGKILL, |
| 52 | SIGSTOP, |
| 53 | } |
| 54 | |
| 55 | pub(crate) fn exit_with_signal(signal: i32) -> Result<Infallible, crate::common::Error> { |
| 56 | // Try to restore signal handler to the default to make the kill below actually kill the process |
| 57 | // rather than run a signal handler. |
| 58 | let _ = (|| SignalAction::new(SignalHandlerBehavior::Default)?.register(signal))(); |
| 59 | |
| 60 | crate::system::kill(crate::system::Process::process_id(), signal)?; |
| 61 | unreachable!(); |
| 62 | } |