linux_unsafe/
sigset.rs

1#![allow(non_camel_case_types)]
2
3/// A set of signals for use with signal blocking functions.
4#[derive(Clone, Copy, Debug)]
5#[repr(C)]
6pub struct sigset_t {
7    // For now we only support 32-bit and 64-bit architectures, and there are
8    // only 32 signals defined, so it's safe to assume that they all fit in
9    // one element. We'll need to be more clever about this if we ever support
10    // an architecture where sigset_t has a different layout.
11    sig: [crate::ulong; 1],
12}
13
14impl sigset_t {
15    const ELEMS: usize = 1;
16    const ELEM_BITS: usize = core::mem::size_of::<crate::ulong>() * 8;
17    const FILLED: crate::ulong = !0;
18
19    #[inline]
20    pub const fn new_empty() -> Self {
21        Self {
22            sig: [0; Self::ELEMS],
23        }
24    }
25
26    #[inline]
27    pub const fn new_filled() -> Self {
28        Self {
29            sig: [Self::FILLED; Self::ELEMS],
30        }
31    }
32
33    #[inline]
34    pub fn sigemptyset(&mut self) {
35        for elem in &mut self.sig {
36            *elem = 0;
37        }
38    }
39
40    #[inline]
41    pub fn sigfillset(&mut self) {
42        for elem in &mut self.sig {
43            *elem = Self::FILLED;
44        }
45    }
46
47    #[inline]
48    pub fn sigaddset(&mut self, signum: crate::int) -> crate::result::Result<()> {
49        let (elem, bit) = Self::sigpos(signum)?;
50        self.sig[elem] |= (1 << bit) as crate::ulong;
51        Ok(())
52    }
53
54    #[inline]
55    pub fn sigdelset(&mut self, signum: crate::int) -> crate::result::Result<()> {
56        let (elem, bit) = Self::sigpos(signum)?;
57        self.sig[elem] &= !(1 << bit) as crate::ulong;
58        Ok(())
59    }
60
61    #[inline]
62    pub fn sigismember(&mut self, signum: crate::int) -> crate::result::Result<bool> {
63        let (elem, bit) = Self::sigpos(signum)?;
64        Ok((self.sig[elem] & (1 << bit)) != 0)
65    }
66
67    pub fn as_ptr(&self) -> *const Self {
68        self as *const Self
69    }
70
71    pub fn as_mut_ptr(&mut self) -> *mut Self {
72        self as *mut Self
73    }
74
75    fn sigpos(signum: crate::int) -> crate::result::Result<(usize, usize)> {
76        let total_bit = (signum - 1) as usize;
77        let elem = total_bit / Self::ELEM_BITS;
78        if elem >= Self::ELEMS {
79            return Err(crate::result::Error::new(22 /* EINVAL */));
80        }
81        let bit = total_bit % Self::ELEM_BITS;
82        Ok((elem, bit))
83    }
84}