Created
June 24, 2020 11:04
-
-
Save gmambro/cd8fbab7529f3c7e96df90eeb71741c8 to your computer and use it in GitHub Desktop.
Rust sichld handle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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