diff --git a/src/sigchld.rs b/src/sigchld.rs index 0dcb1d27f..55355b04d 100644 --- a/src/sigchld.rs +++ b/src/sigchld.rs @@ -10,6 +10,8 @@ use std::mem; use std::os::unix::io::RawFd; use std::ptr; +#[cfg(not(target_os = "linux"))] +static mut WRITE_END: RawFd = -1; pub struct ChildWaiter { fd: RawFd, @@ -23,7 +25,38 @@ impl Drop for ChildWaiter { } } +#[cfg(not(target_os = "linux"))] +extern "C" fn chld_handler(_signo: libc::c_int, _: *const u8, _: *const u8) { + unsafe { + libc::write(WRITE_END, "x".as_ptr() as *const _, 1); + } +} + impl ChildWaiter { + #[cfg(not(target_os = "linux"))] + pub fn new() -> Result { + unsafe { + let mut pipe: [RawFd; 2] = [-1, -1]; + let res = libc::pipe(pipe.as_mut_ptr()); + if res == -1 { + bail!("pipe failed: {:?}", io::Error::last_os_error()); + } + + WRITE_END = pipe[1]; + + let mut sa: libc::sigaction = mem::zeroed(); + sa.sa_sigaction = chld_handler as usize; + sa.sa_flags = (libc::SA_RESTART | libc::SA_NOCLDSTOP) as _; + let res = libc::sigaction(libc::SIGCHLD, &sa, ptr::null_mut()); + if res == -1 { + bail!("sigaction SIGCHLD failed: {:?}", io::Error::last_os_error()); + } + + Ok(Self { fd: pipe[0] }) + } + } + + #[cfg(target_os = "linux")] pub fn new() -> Result { unsafe { let mut mask: libc::sigset_t = mem::zeroed();