use std::io;
#[repr(C)]
struct SigActionLibc {
sa_handler: *const (),
sa_mask: [u8; 128],
sa_flags: i32,
_pad: i32,
sa_restorer: *const (),
}
const _: () = assert!(core::mem::size_of::<SigActionLibc>() == 152);
const _: () = assert!(core::mem::offset_of!(SigActionLibc, sa_handler) == 0);
const _: () = assert!(core::mem::offset_of!(SigActionLibc, sa_mask) == 8);
const _: () = assert!(core::mem::offset_of!(SigActionLibc, sa_flags) == 136);
const _: () = assert!(core::mem::offset_of!(SigActionLibc, sa_restorer) == 144);
extern "C" {
fn sigaction(signum: i32, act: *const SigActionLibc, oldact: *mut SigActionLibc) -> i32;
}
const SA_RESTART_LIBC: i32 = 0x1000_0000u32 as i32;
pub(super) unsafe fn install(handler: extern "C" fn(i32)) -> io::Result<()> {
const SIGINT: i32 = 2;
const SIGTERM: i32 = 15;
let mut act = std::mem::MaybeUninit::<SigActionLibc>::zeroed();
unsafe {
(*act.as_mut_ptr()).sa_handler = handler as *const ();
(*act.as_mut_ptr()).sa_flags = SA_RESTART_LIBC;
}
let act = unsafe { act.assume_init() };
for sig in [SIGINT, SIGTERM] {
let rc = unsafe { sigaction(sig, &act, std::ptr::null_mut()) };
if rc == -1 {
return Err(io::Error::last_os_error());
}
}
Ok(())
}