Debian Codegraph @

src/system/signal/state.rs

raw symbols SCIP
1 use std::io;
2
3 use crate::cutils::cerr;
4 use crate::log::dev_debug;
5 use crate::system::make_zeroed_sigaction;
6 use crate::system::signal::signal_name;
7
8 use super::{SignalNumber, consts::*};
9
10 struct State {
11 sa: libc::sigaction,
12 restore: bool,
13 }
14
15 pub(crate) struct SignalsState {
16 sa_handlers: [State; Self::SAVED_SIGNALS.len()],
17 }
18
19 impl SignalsState {
20 ///SIGKILL SIGSTOP are not included since forbidden
21 const SAVED_SIGNALS: [SignalNumber; 14] = [
22 SIGINT, SIGQUIT, SIGTSTP, SIGTERM, SIGHUP, SIGALRM, SIGPIPE, SIGUSR1, SIGUSR2, SIGCHLD,
23 SIGCONT, SIGWINCH, SIGTTIN, SIGTTOU,
24 ];
25
26 pub(crate) fn save() -> io::Result<Self> {
27 let mut state_array = std::array::from_fn(|_| State {
28 sa: make_zeroed_sigaction(),
29 restore: false,
30 });
31
32 for (idx, &signal) in Self::SAVED_SIGNALS.iter().enumerate() {
33 let state = &mut state_array[idx];
34
35 // safety: `signal` is a constant value and a valid signal
36 // second parameter can be null
37 // `state.sa` is a valid already initialized `sigaction`
38 cerr(unsafe { libc::sigaction(signal, std::ptr::null(), &mut state.sa) })?;
39 }
40
41 Ok(Self {
42 sa_handlers: state_array,
43 })
44 }
45
46 pub(super) fn updated(&mut self, signal: SignalNumber) -> io::Result<()> {
47 match Self::SAVED_SIGNALS
48 .iter()
49 .enumerate()
50 .find(|&(_, &s)| s == signal)
51 {
52 None => Err(io::Error::new(io::ErrorKind::NotFound, "invalid signal")),
53 Some((idx, _)) => {
54 self.sa_handlers[idx].restore = true;
55 Ok(())
56 }
57 }
58 }
59
60 pub(crate) fn restore(&mut self) -> io::Result<()> {
61 for (idx, state) in self.sa_handlers.iter_mut().enumerate() {
62 if state.restore {
63 let signal = Self::SAVED_SIGNALS[idx];
64 dev_debug!("restoring updated signal: {}", signal_name(signal));
65 // safety: `signal` is a constant value and a valid signal
66 // `sa` is a valid already initialized sigaction
67 // Third parameter can be NULL
68 cerr(unsafe { libc::sigaction(signal, &state.sa, std::ptr::null_mut()) })?;
69 state.restore = false;
70 }
71 }
72 Ok(())
73 }
74 }