use std::io;
use std::os::fd::{OwnedFd, RawFd};
use std::sync::atomic::{AtomicI32, Ordering};
use rustix::io::Errno;
pub(super) const FAULTS: [libc::c_int; 7] = [
libc::SIGSEGV,
libc::SIGBUS,
libc::SIGILL,
libc::SIGFPE,
libc::SIGABRT,
libc::SIGTRAP,
libc::SIGSYS,
];
pub(super) const TERMINATIONS: [libc::c_int; 4] = [libc::SIGHUP, libc::SIGINT, libc::SIGQUIT, libc::SIGTERM];
const STOPS: [libc::c_int; 3] = [libc::SIGTSTP, libc::SIGTTIN, libc::SIGTTOU];
const NOT_TERMINATING: [libc::c_int; 4] = [libc::SIGCHLD, libc::SIGURG, libc::SIGWINCH, libc::SIGCONT];
pub(super) struct Inherited {
pub(super) max: libc::c_int,
ignored: Vec<bool>,
handleable: Vec<bool>,
}
impl Inherited {
pub(super) fn read() -> Inherited {
let max = libc::SIGRTMAX();
let mut ignored = vec![false; max as usize + 1];
let mut handleable = vec![false; max as usize + 1];
for signal in 1..=max {
let mut current: libc::sigaction = unsafe { std::mem::zeroed() };
if unsafe { libc::sigaction(signal, std::ptr::null(), &mut current) } == 0 {
ignored[signal as usize] = current.sa_sigaction == libc::SIG_IGN;
handleable[signal as usize] = true;
} else {
debug_assert_eq!(
io::Error::last_os_error().raw_os_error(),
Some(libc::EINVAL),
"sigaction({signal}) query"
);
}
}
Inherited {
max,
ignored,
handleable,
}
}
pub(super) fn was_ignored(&self, signal: libc::c_int) -> bool {
self.ignored.get(signal as usize).copied().unwrap_or(false)
}
pub(super) fn ignored_ptr(&self) -> *const bool {
self.ignored.as_ptr()
}
pub(super) fn handleable_ptr(&self) -> *const bool {
self.handleable.as_ptr()
}
fn is_handleable(&self, signal: libc::c_int) -> bool {
self.handleable.get(signal as usize).copied().unwrap_or(false)
}
}
static WAKE: AtomicI32 = AtomicI32::new(-1);
extern "C" fn on_terminating_signal(signal: libc::c_int) {
let fd = WAKE.load(Ordering::Relaxed);
if fd >= 0 {
unsafe {
let saved = *libc::__errno_location();
let byte = signal as u8;
libc::write(fd, (&byte as *const u8).cast(), 1);
*libc::__errno_location() = saved;
}
}
}
extern "C" fn on_stop_signal(_: libc::c_int) {}
pub(super) struct Wake {
pub(super) rx: OwnedFd,
pub(super) tx: OwnedFd,
}
impl Wake {
pub(super) fn new() -> io::Result<Wake> {
let (rx, tx) = std::io::pipe()?;
let (rx, tx): (OwnedFd, OwnedFd) = (rx.into(), tx.into());
for end in [&rx, &tx] {
let flags = rustix::fs::fcntl_getfl(end)?;
rustix::fs::fcntl_setfl(end, flags | rustix::fs::OFlags::NONBLOCK)?;
}
Ok(Wake { rx, tx })
}
}
pub(super) fn install(inherited: &Inherited, wake: RawFd) {
WAKE.store(wake, Ordering::Relaxed);
for signal in 1..=inherited.max {
let skip = signal == libc::SIGKILL
|| signal == libc::SIGSTOP
|| !inherited.is_handleable(signal)
|| FAULTS.contains(&signal)
|| NOT_TERMINATING.contains(&signal)
|| (inherited.was_ignored(signal) && signal != libc::SIGPIPE);
if skip {
continue;
}
let handler: extern "C" fn(libc::c_int) = if STOPS.contains(&signal) {
on_stop_signal
} else {
on_terminating_signal
};
let mut action: libc::sigaction = unsafe { std::mem::zeroed() };
action.sa_sigaction = handler as *const () as usize;
action.sa_flags = libc::SA_RESTART;
let filled = unsafe { libc::sigfillset(&mut action.sa_mask) };
debug_assert_eq!(filled, 0, "sigfillset");
let set = unsafe { libc::sigaction(signal, &action, std::ptr::null_mut()) };
debug_assert_eq!(set, 0, "sigaction({signal}): {}", Errno::from_raw_os_error(errno()));
}
}
pub(super) fn reset_sigchld() {
let mut action: libc::sigaction = unsafe { std::mem::zeroed() };
action.sa_sigaction = libc::SIG_DFL;
let set = unsafe { libc::sigaction(libc::SIGCHLD, &action, std::ptr::null_mut()) };
debug_assert_eq!(set, 0, "sigaction(SIGCHLD): {}", Errno::from_raw_os_error(errno()));
}
pub(super) fn block_all() -> libc::sigset_t {
unsafe {
let mut all: libc::sigset_t = std::mem::zeroed();
let mut old: libc::sigset_t = std::mem::zeroed();
let filled = libc::sigfillset(&mut all);
debug_assert_eq!(filled, 0, "sigfillset");
let blocked = libc::pthread_sigmask(libc::SIG_BLOCK, &all, &mut old);
debug_assert_eq!(blocked, 0, "pthread_sigmask(SIG_BLOCK)");
old
}
}
pub(super) fn restore(mask: &libc::sigset_t) {
let restored = unsafe { libc::pthread_sigmask(libc::SIG_SETMASK, mask, std::ptr::null_mut()) };
debug_assert_eq!(restored, 0, "pthread_sigmask(SIG_SETMASK)");
}
fn errno() -> i32 {
io::Error::last_os_error().raw_os_error().unwrap_or(0)
}