| 1 |
//! The [`KernelSigSet`] type. |
| 2 |
|
| 3 |
#![allow(unsafe_code)] |
| 4 |
#![allow(non_camel_case_types)] |
| 5 |
|
| 6 |
use crate::backend::c; |
| 7 |
use crate::signal::Signal; |
| 8 |
use core::fmt; |
| 9 |
use linux_raw_sys::general::{kernel_sigset_t, _NSIG}; |
| 10 |
|
| 11 |
/// `kernel_sigset_t`—A set of signal numbers, as used by some syscalls. |
| 12 |
/// |
| 13 |
/// This is similar to `libc::sigset_t`, but with only enough space for the |
| 14 |
/// signals currently known to be used by the kernel. libc implementations |
| 15 |
/// reserve extra space so that if Linux defines new signals in the future |
| 16 |
/// they can add support without breaking their dynamic linking ABI. Rustix |
| 17 |
/// doesn't support a dynamic linking ABI, so if we need to increase the |
| 18 |
/// size of `KernelSigSet` in the future, we can do so. |
| 19 |
/// |
| 20 |
/// It's also the case that the last time Linux changed the size of its |
| 21 |
/// `kernel_sigset_t` was when it added support for POSIX.1b signals in 1999. |
| 22 |
/// |
| 23 |
/// `KernelSigSet` is guaranteed to have a subset of the layout of |
| 24 |
/// `libc::sigset_t`. |
| 25 |
/// |
| 26 |
/// libc implementations typically also reserve some signal values for internal |
| 27 |
/// use. In a process that contains a libc, some unsafe functions invoke |
| 28 |
/// undefined behavior if passed a `KernelSigSet` that contains one of the |
| 29 |
/// signals that the libc reserves. |
| 30 |
#[repr(transparent)] |
| 31 |
#[derive(Clone)] |
| 32 |
pub struct KernelSigSet(kernel_sigset_t); |
| 33 |
|
| 34 |
impl KernelSigSet { |
| 35 |
/// Create a new empty `KernelSigSet`. |
| 36 |
pub const fn empty() -> Self { |
| 37 |
const fn zeros<const N: usize>() -> [c::c_ulong; N] { |
| 38 |
[0; N] |
| 39 |
} |
| 40 |
Self(kernel_sigset_t { sig: zeros() }) |
| 41 |
} |
| 42 |
|
| 43 |
/// Create a new `KernelSigSet` with all signals set. |
| 44 |
/// |
| 45 |
/// This includes signals which are typically reserved for libc. |
| 46 |
pub const fn all() -> Self { |
| 47 |
const fn ones<const N: usize>() -> [c::c_ulong; N] { |
| 48 |
[!0; N] |
| 49 |
} |
| 50 |
Self(kernel_sigset_t { sig: ones() }) |
| 51 |
} |
| 52 |
|
| 53 |
/// Remove all signals. |
| 54 |
pub fn clear(&mut self) { |
| 55 |
*self = Self(kernel_sigset_t { |
| 56 |
sig: Default::default(), |
| 57 |
}); |
| 58 |
} |
| 59 |
|
| 60 |
/// Insert a signal. |
| 61 |
pub fn insert(&mut self, sig: Signal) { |
| 62 |
let sigs_per_elt = core::mem::size_of_val(&self.0.sig[0]) * 8; |
| 63 |
|
| 64 |
let raw = (sig.as_raw().wrapping_sub(1)) as usize; |
| 65 |
self.0.sig[raw / sigs_per_elt] |= 1 << (raw % sigs_per_elt); |
| 66 |
} |
| 67 |
|
| 68 |
/// Insert all signals. |
| 69 |
pub fn insert_all(&mut self) { |
| 70 |
self.0.sig.fill(!0); |
| 71 |
} |
| 72 |
|
| 73 |
/// Remove a signal. |
| 74 |
pub fn remove(&mut self, sig: Signal) { |
| 75 |
let sigs_per_elt = core::mem::size_of_val(&self.0.sig[0]) * 8; |
| 76 |
|
| 77 |
let raw = (sig.as_raw().wrapping_sub(1)) as usize; |
| 78 |
self.0.sig[raw / sigs_per_elt] &= !(1 << (raw % sigs_per_elt)); |
| 79 |
} |
| 80 |
|
| 81 |
/// Test whether a given signal is present. |
| 82 |
pub fn contains(&self, sig: Signal) -> bool { |
| 83 |
let sigs_per_elt = core::mem::size_of_val(&self.0.sig[0]) * 8; |
| 84 |
|
| 85 |
let raw = (sig.as_raw().wrapping_sub(1)) as usize; |
| 86 |
(self.0.sig[raw / sigs_per_elt] & (1 << (raw % sigs_per_elt))) != 0 |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl Default for KernelSigSet { |
| 91 |
#[inline] |
| 92 |
fn default() -> Self { |
| 93 |
Self::empty() |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
impl fmt::Debug for KernelSigSet { |
| 98 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 99 |
let mut d = f.debug_set(); |
| 100 |
|
| 101 |
// Surprisingly, `_NSIG` is inclusive. |
| 102 |
for i in 1..=_NSIG { |
| 103 |
// SAFETY: This value is non-zero, in range, and only used for |
| 104 |
// debug output. |
| 105 |
let sig = unsafe { Signal::from_raw_unchecked(i as _) }; |
| 106 |
|
| 107 |
if self.contains(sig) { |
| 108 |
d.entry(&sig); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
d.finish() |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#[cfg(test)] |
| 117 |
mod tests { |
| 118 |
use super::*; |
| 119 |
#[cfg(linux_raw)] |
| 120 |
use crate::runtime::{KERNEL_SIGRTMAX, KERNEL_SIGRTMIN}; |
| 121 |
use core::mem::{align_of, size_of}; |
| 122 |
|
| 123 |
#[test] |
| 124 |
fn test_assumptions() { |
| 125 |
#[cfg(linux_raw)] |
| 126 |
assert!(KERNEL_SIGRTMAX as usize - 1 < size_of::<KernelSigSet>() * 8); |
| 127 |
} |
| 128 |
|
| 129 |
#[test] |
| 130 |
fn test_layouts() { |
| 131 |
assert!(size_of::<KernelSigSet>() <= size_of::<libc::sigset_t>()); |
| 132 |
assert!(align_of::<KernelSigSet>() <= align_of::<libc::sigset_t>()); |
| 133 |
} |
| 134 |
|
| 135 |
/// A bunch of signals for testing. |
| 136 |
fn sigs() -> Vec<Signal> { |
| 137 |
#[allow(unused_mut)] |
| 138 |
let mut sigs = vec![ |
| 139 |
Signal::HUP, |
| 140 |
Signal::INT, |
| 141 |
Signal::QUIT, |
| 142 |
Signal::ILL, |
| 143 |
Signal::TRAP, |
| 144 |
Signal::ABORT, |
| 145 |
Signal::BUS, |
| 146 |
Signal::FPE, |
| 147 |
Signal::KILL, |
| 148 |
Signal::USR1, |
| 149 |
Signal::SEGV, |
| 150 |
Signal::USR2, |
| 151 |
Signal::PIPE, |
| 152 |
Signal::ALARM, |
| 153 |
Signal::TERM, |
| 154 |
Signal::CHILD, |
| 155 |
Signal::CONT, |
| 156 |
Signal::STOP, |
| 157 |
Signal::TSTP, |
| 158 |
Signal::TTIN, |
| 159 |
Signal::TTOU, |
| 160 |
Signal::URG, |
| 161 |
Signal::XCPU, |
| 162 |
Signal::XFSZ, |
| 163 |
Signal::VTALARM, |
| 164 |
Signal::PROF, |
| 165 |
Signal::WINCH, |
| 166 |
Signal::SYS, |
| 167 |
unsafe { Signal::from_raw_unchecked(libc::SIGRTMIN()) }, |
| 168 |
unsafe { Signal::from_raw_unchecked(libc::SIGRTMIN() + 7) }, |
| 169 |
unsafe { Signal::from_raw_unchecked(libc::SIGRTMAX()) }, |
| 170 |
]; |
| 171 |
|
| 172 |
#[cfg(linux_raw)] |
| 173 |
{ |
| 174 |
sigs.push(unsafe { Signal::from_raw_unchecked(KERNEL_SIGRTMIN) }); |
| 175 |
sigs.push(unsafe { Signal::from_raw_unchecked(KERNEL_SIGRTMIN + 7) }); |
| 176 |
sigs.push(unsafe { Signal::from_raw_unchecked(KERNEL_SIGRTMAX) }); |
| 177 |
} |
| 178 |
|
| 179 |
sigs |
| 180 |
} |
| 181 |
|
| 182 |
/// A bunch of non-reserved signals for testing. |
| 183 |
fn libc_sigs() -> [Signal; 31] { |
| 184 |
[ |
| 185 |
Signal::HUP, |
| 186 |
Signal::INT, |
| 187 |
Signal::QUIT, |
| 188 |
Signal::ILL, |
| 189 |
Signal::TRAP, |
| 190 |
Signal::ABORT, |
| 191 |
Signal::BUS, |
| 192 |
Signal::FPE, |
| 193 |
Signal::KILL, |
| 194 |
Signal::USR1, |
| 195 |
Signal::SEGV, |
| 196 |
Signal::USR2, |
| 197 |
Signal::PIPE, |
| 198 |
Signal::ALARM, |
| 199 |
Signal::TERM, |
| 200 |
Signal::CHILD, |
| 201 |
Signal::CONT, |
| 202 |
Signal::STOP, |
| 203 |
Signal::TSTP, |
| 204 |
Signal::TTIN, |
| 205 |
Signal::TTOU, |
| 206 |
Signal::URG, |
| 207 |
Signal::XCPU, |
| 208 |
Signal::XFSZ, |
| 209 |
Signal::VTALARM, |
| 210 |
Signal::PROF, |
| 211 |
Signal::WINCH, |
| 212 |
Signal::SYS, |
| 213 |
unsafe { Signal::from_raw_unchecked(libc::SIGRTMIN()) }, |
| 214 |
unsafe { Signal::from_raw_unchecked(libc::SIGRTMIN() + 7) }, |
| 215 |
unsafe { Signal::from_raw_unchecked(libc::SIGRTMAX()) }, |
| 216 |
] |
| 217 |
} |
| 218 |
|
| 219 |
#[test] |
| 220 |
fn test_ops_plain() { |
| 221 |
for sig in sigs() { |
| 222 |
let mut set = KernelSigSet::empty(); |
| 223 |
for sig in sigs() { |
| 224 |
assert!(!set.contains(sig)); |
| 225 |
} |
| 226 |
|
| 227 |
set.insert(sig); |
| 228 |
assert!(set.contains(sig)); |
| 229 |
for sig in sigs().iter().filter(|s| **s != sig) { |
| 230 |
assert!(!set.contains(*sig)); |
| 231 |
} |
| 232 |
|
| 233 |
set.remove(sig); |
| 234 |
for sig in sigs() { |
| 235 |
assert!(!set.contains(sig)); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn test_clear() { |
| 242 |
let mut set = KernelSigSet::empty(); |
| 243 |
for sig in sigs() { |
| 244 |
set.insert(sig); |
| 245 |
} |
| 246 |
|
| 247 |
set.clear(); |
| 248 |
|
| 249 |
for sig in sigs() { |
| 250 |
assert!(!set.contains(sig)); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
// io_uring libraries assume that libc's `sigset_t` matches the layout |
| 255 |
// of the Linux kernel's `kernel_sigset_t`. Test that rustix's layout |
| 256 |
// matches as well. |
| 257 |
#[test] |
| 258 |
fn test_libc_layout_compatibility() { |
| 259 |
use crate::utils::as_ptr; |
| 260 |
|
| 261 |
let mut lc = unsafe { core::mem::zeroed::<libc::sigset_t>() }; |
| 262 |
let mut ru = KernelSigSet::empty(); |
| 263 |
let r = unsafe { libc::sigemptyset(&mut lc) }; |
| 264 |
|
| 265 |
assert_eq!(r, 0); |
| 266 |
assert_eq!( |
| 267 |
unsafe { |
| 268 |
libc::memcmp( |
| 269 |
as_ptr(&lc).cast(), |
| 270 |
as_ptr(&ru).cast(), |
| 271 |
core::mem::size_of::<KernelSigSet>(), |
| 272 |
) |
| 273 |
}, |
| 274 |
0 |
| 275 |
); |
| 276 |
|
| 277 |
for sig in libc_sigs() { |
| 278 |
ru.insert(sig); |
| 279 |
assert_ne!( |
| 280 |
unsafe { |
| 281 |
libc::memcmp( |
| 282 |
as_ptr(&lc).cast(), |
| 283 |
as_ptr(&ru).cast(), |
| 284 |
core::mem::size_of::<KernelSigSet>(), |
| 285 |
) |
| 286 |
}, |
| 287 |
0 |
| 288 |
); |
| 289 |
let r = unsafe { libc::sigaddset(&mut lc, sig.as_raw()) }; |
| 290 |
assert_eq!(r, 0); |
| 291 |
assert_eq!( |
| 292 |
unsafe { |
| 293 |
libc::memcmp( |
| 294 |
as_ptr(&lc).cast(), |
| 295 |
as_ptr(&ru).cast(), |
| 296 |
core::mem::size_of::<KernelSigSet>(), |
| 297 |
) |
| 298 |
}, |
| 299 |
0 |
| 300 |
); |
| 301 |
ru.remove(sig); |
| 302 |
assert_ne!( |
| 303 |
unsafe { |
| 304 |
libc::memcmp( |
| 305 |
as_ptr(&lc).cast(), |
| 306 |
as_ptr(&ru).cast(), |
| 307 |
core::mem::size_of::<KernelSigSet>(), |
| 308 |
) |
| 309 |
}, |
| 310 |
0 |
| 311 |
); |
| 312 |
let r = unsafe { libc::sigdelset(&mut lc, sig.as_raw()) }; |
| 313 |
assert_eq!(r, 0); |
| 314 |
assert_eq!( |
| 315 |
unsafe { |
| 316 |
libc::memcmp( |
| 317 |
as_ptr(&lc).cast(), |
| 318 |
as_ptr(&ru).cast(), |
| 319 |
core::mem::size_of::<KernelSigSet>(), |
| 320 |
) |
| 321 |
}, |
| 322 |
0 |
| 323 |
); |
| 324 |
} |
| 325 |
} |
| 326 |
} |