use core::mem::MaybeUninit;
use core::ptr;
fn stderr(msg: &[u8]) {
unsafe {
libc::write(libc::STDERR_FILENO, msg.as_ptr().cast(), msg.len());
}
}
pub struct SignalGuard(libc::sigset_t);
impl SignalGuard {
pub fn new() -> Self {
let mut new_set = MaybeUninit::uninit();
let mut old_set = MaybeUninit::uninit();
unsafe {
if libc::sigfillset(new_set.as_mut_ptr()) != 0 {
stderr(b"[new] sigfillset() failed\n");
libc::abort();
}
if libc::pthread_sigmask(
libc::SIG_SETMASK,
new_set.as_ptr(),
old_set.as_mut_ptr(),
) != 0
{
stderr(b"[new] pthread_sigmask() failed\n");
libc::abort();
}
}
Self(unsafe { old_set.assume_init() })
}
}
impl Drop for SignalGuard {
fn drop(&mut self) {
unsafe {
if libc::pthread_sigmask(
libc::SIG_SETMASK,
&self.0 as _,
ptr::null_mut(),
) != 0
{
stderr(b"[drop] pthread_sigmask() failed\n");
libc::abort();
}
}
}
}