Skip to content

Instantly share code, notes, and snippets.

@gmambro
Created June 24, 2020 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmambro/cd8fbab7529f3c7e96df90eeb71741c8 to your computer and use it in GitHub Desktop.
Save gmambro/cd8fbab7529f3c7e96df90eeb71741c8 to your computer and use it in GitHub Desktop.
Rust sichld handle
#[cfg(unix)]
use nix::{
sys::{
signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal},
wait::{waitpid, WaitPidFlag, WaitStatus},
},
unistd::Pid,
};
// Note: Signal handler should be written with great care. E.g if you
// ever want to print from this function you need to use libc write
#[cfg(unix)]
extern "C" fn handle_sigchld(_sig: libc::c_int) {
loop {
match waitpid(Pid::from_raw(-1), Some(WaitPidFlag::WNOHANG)) {
Ok(WaitStatus::StillAlive) => break,
_ => (),
}
}
}
fn main() {
#[cfg(unix)]
{
let sig_action = SigAction::new(
SigHandler::Handler(handle_sigchld),
SaFlags::empty(),
SigSet::empty(),
);
if let Err(err) = unsafe { sigaction(Signal::SIGCHLD, &sig_action) } {
panic!("sigaction() failed: {}", err);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment