use std::sync::atomic::{AtomicBool, Ordering};
use libc::{SIGINT, SIGTERM, c_int, sigaction, sigset_t};
pub(super) static QUIT_FLAG: AtomicBool = AtomicBool::new(false);
extern "C" fn handle_term(_sig: c_int) {
QUIT_FLAG.store(true, Ordering::Relaxed);
}
pub(super) fn install() -> std::io::Result<()> {
let mut sa: sigaction = unsafe { core::mem::zeroed() };
unsafe {
libc::sigemptyset(&mut sa.sa_mask);
}
sa.sa_handler = handle_term as *const () as usize;
sa.sa_flags = 0;
let r = unsafe { sigaction(SIGTERM, &sa, core::ptr::null_mut()) };
if r < 0 {
return Err(std::io::Error::last_os_error());
}
let r = unsafe { sigaction(SIGINT, &sa, core::ptr::null_mut()) };
if r < 0 {
return Err(std::io::Error::last_os_error());
}
Ok(())
}