Debian Codegraph @

src/system/signal/set.rs

raw symbols SCIP
1 use crate::{cutils::cerr, system::make_zeroed_sigaction};
2
3 use super::{SignalNumber, handler::SignalHandlerBehavior};
4
5 use std::ffi::c_int;
6 use std::io;
7 use std::mem::MaybeUninit;
8 use std::sync::atomic::{AtomicBool, Ordering};
9
10 #[repr(transparent)]
11 pub(super) struct SignalAction {
12 raw: libc::sigaction,
13 }
14
15 impl SignalAction {
16 pub(super) fn new(behavior: SignalHandlerBehavior) -> io::Result<Self> {
17 // This guarantees that functions won't be interrupted by this signal as long as the
18 // handler is alive.
19 let mut sa_flags = libc::SA_RESTART;
20
21 // We only need a full `sa_mask` if we are going to stream the signal information as we
22 // don't want to be interrupted by any signals while executing `send_siginfo`.
23 let (sa_sigaction, sa_mask) = match behavior {
24 SignalHandlerBehavior::Default => (libc::SIG_DFL, SignalSet::empty()?),
25 SignalHandlerBehavior::Ignore => (libc::SIG_IGN, SignalSet::empty()?),
26 SignalHandlerBehavior::Stream => {
27 // Specify that we want to pass a signal-catching function in `sa_sigaction`.
28 sa_flags |= libc::SA_SIGINFO;
29 (
30 super::stream::send_siginfo as *const () as libc::sighandler_t,
31 SignalSet::full()?,
32 )
33 }
34 SignalHandlerBehavior::StorePending => (
35 store_pending as *const () as libc::sighandler_t,
36 SignalSet::full()?,
37 ),
38 };
39
40 let mut raw: libc::sigaction = make_zeroed_sigaction();
41 raw.sa_sigaction = sa_sigaction;
42 raw.sa_mask = sa_mask.raw;
43 raw.sa_flags = sa_flags;
44
45 Ok(Self { raw })
46 }
47
48 pub(super) fn register(&self, signal: SignalNumber) -> io::Result<Self> {
49 let mut original_action = MaybeUninit::<Self>::zeroed();
50
51 // SAFETY: `sigaction` expects a valid pointer, which we provide; the typecast is valid
52 // since SignalAction is a repr(transparent) newtype struct.
53 cerr(unsafe { libc::sigaction(signal, &self.raw, original_action.as_mut_ptr().cast()) })?;
54
55 // SAFETY: `sigaction` will have properly initialized `original_action`.
56 Ok(unsafe { original_action.assume_init() })
57 }
58 }
59
60 static PENDING_SIGNALS: [AtomicBool; 65] = [const { AtomicBool::new(false) }; 65];
61
62 extern "C" fn store_pending(signal: SignalNumber) {
63 /* ignore any weird signals */
64 if let Some(flag) = PENDING_SIGNALS.get(signal as usize) {
65 flag.store(true, Ordering::SeqCst);
66 }
67 }
68
69 pub(crate) fn take_first_pending() -> Option<SignalNumber> {
70 for (signal, val) in PENDING_SIGNALS.iter().enumerate() {
71 if val.swap(false, Ordering::SeqCst) {
72 return Some(SignalNumber::try_from(signal).unwrap());
73 }
74 }
75 None
76 }
77
78 // A signal set that can be used to mask signals.
79 #[repr(transparent)]
80 pub(crate) struct SignalSet {
81 raw: libc::sigset_t,
82 }
83
84 impl SignalSet {
85 /// Create an empty set.
86 pub(crate) fn empty() -> io::Result<Self> {
87 let mut set = MaybeUninit::<Self>::zeroed();
88
89 // SAFETY: same as above
90 cerr(unsafe { libc::sigemptyset(set.as_mut_ptr().cast()) })?;
91
92 // SAFETY: `sigemptyset` will have initialized `set`
93 Ok(unsafe { set.assume_init() })
94 }
95
96 /// Create a set containing all the signals.
97 pub(crate) fn full() -> io::Result<Self> {
98 let mut set = MaybeUninit::<Self>::zeroed();
99
100 // SAFETY: same as above
101 cerr(unsafe { libc::sigfillset(set.as_mut_ptr().cast()) })?;
102
103 // SAFETY: `sigfillset` will have initialized `set`
104 Ok(unsafe { set.assume_init() })
105 }
106
107 /// Add a signal to this set
108 pub(crate) fn add(&mut self, sig: SignalNumber) -> io::Result<()> {
109 // SAFETY: we pass a valid mutable pointer to `sigaddset`
110 cerr(unsafe { libc::sigaddset(&mut self.raw, sig) })?;
111
112 Ok(())
113 }
114
115 fn sigprocmask(&self, how: c_int) -> io::Result<Self> {
116 let mut original_set = MaybeUninit::<Self>::zeroed();
117
118 // SAFETY: same as above
119 cerr(unsafe { libc::sigprocmask(how, &self.raw, original_set.as_mut_ptr().cast()) })?;
120
121 // SAFETY: `sigprocmask` will have initialized `set`
122 Ok(unsafe { original_set.assume_init() })
123 }
124
125 /// Block all the signals in this set and return the previous set of blocked signals.
126 ///
127 /// After calling this function successfully, the set of blocked signals will be the union of
128 /// the previous set of blocked signals and this set.
129 pub(crate) fn block(&self) -> io::Result<Self> {
130 self.sigprocmask(libc::SIG_BLOCK)
131 }
132
133 /// Unblock all the signals in this set and return the previous set of blocked signals.
134 ///
135 /// After calling this function successfully, the set of blocked signals will be the previous
136 /// set of blocked signals without this set.
137 pub(crate) fn unblock(&self) -> io::Result<Self> {
138 self.sigprocmask(libc::SIG_UNBLOCK)
139 }
140
141 /// Block only the signals that are in this set and return the previous set of blocked signals.
142 ///
143 /// After calling this function successfully, the set of blocked signals will be the exactly
144 /// this set.
145 pub(crate) fn set_mask(&self) -> io::Result<Self> {
146 self.sigprocmask(libc::SIG_SETMASK)
147 }
148 }