Line data Source code
1 : use signal_hook::iterator::Signals;
2 :
3 : pub use signal_hook::consts::{signal::*, TERM_SIGNALS};
4 :
5 : pub enum Signal {
6 : Quit,
7 : Interrupt,
8 : Terminate,
9 : }
10 :
11 : impl Signal {
12 0 : pub fn name(&self) -> &'static str {
13 0 : match self {
14 0 : Signal::Quit => "SIGQUIT",
15 0 : Signal::Interrupt => "SIGINT",
16 0 : Signal::Terminate => "SIGTERM",
17 : }
18 0 : }
19 : }
20 :
21 : pub struct ShutdownSignals;
22 :
23 : impl ShutdownSignals {
24 0 : pub fn handle(mut handler: impl FnMut(Signal) -> anyhow::Result<()>) -> anyhow::Result<()> {
25 0 : for raw_signal in Signals::new(TERM_SIGNALS)?.into_iter() {
26 0 : let signal = match raw_signal {
27 0 : SIGINT => Signal::Interrupt,
28 0 : SIGTERM => Signal::Terminate,
29 0 : SIGQUIT => Signal::Quit,
30 0 : other => panic!("unknown signal: {}", other),
31 : };
32 :
33 0 : handler(signal)?;
34 : }
35 :
36 0 : Ok(())
37 0 : }
38 : }
|