diff options
Diffstat (limited to 'src/command.rs')
-rw-r--r-- | src/command.rs | 40 |
1 files changed, 12 insertions, 28 deletions
diff --git a/src/command.rs b/src/command.rs index fe42b22..09259af 100644 --- a/src/command.rs +++ b/src/command.rs @@ -2,10 +2,11 @@ //! use std::ffi::OsStr; use std::process::{Command,Child}; -use std::io::{Result, Error, ErrorKind}; +use std::io::Result; use libc::c_int; use libc::pid_t; +use std::ptr; const WNOHANG: c_int = 0x00000001; @@ -14,34 +15,17 @@ extern { pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t; } -/// Extension to the Child struct that allows to check the status without block the -/// current thread. -pub trait ChildExt { - fn wait_nohang(&mut self) -> Result<bool>; -} - -impl ChildExt for Child { - /// Checks if the calling child is a zombie and try to kill it, returning - /// if it was killed or not. - fn wait_nohang(&mut self) -> Result<bool> { - let mut stat = 0i32; - let pid = unsafe { waitpid(self.id() as i32, &mut stat, WNOHANG) }; - if pid < 0 { - Err(Error::new(ErrorKind::NotFound, format!("pid not found {}", pid))) - } else { - if pid > 0 { - Ok(self.wait() - .map(|x| { - x.success() - }).unwrap_or(false)) - } else { - Ok(false) - } - } - } -} - /// Executes the given command. pub fn exec_cmd<S: AsRef<OsStr>>(program: S, args: &[S]) -> Result<Child> { Command::new(program).args(args).spawn() } + +/// Collect all the zombies. There's no need to add another dependency for this +/// since it's trivial to implement. +/// +/// See https://github.com/Kintaro/zombie for reference. +pub fn collect_zombies() { + unsafe { + while waitpid(-1, ptr::null_mut(), WNOHANG) > 0 {} + }; +} |