diff options
author | Matias Linares <matiaslina@openmailbox.org> | 2015-11-15 21:21:23 -0300 |
---|---|---|
committer | Matias Linares <matiaslina@openmailbox.org> | 2015-11-15 21:21:23 -0300 |
commit | 1c3e06f29ad59c084c00725bf7041c2af126b22d (patch) | |
tree | d7e983109ca171e2174216a9df44034adcc96b6c | |
parent | 12af9d3ec0d55c258d10e73e8fe6a99f3c9f05ff (diff) | |
download | dotwm-1c3e06f29ad59c084c00725bf7041c2af126b22d.tar.gz |
Clean of zombie process.
The spawn of process within the exec_cmd function now are handled by a new
function (collect_zombies).
For reference, this code was taken from the _zombie_ crate.
-rw-r--r-- | Cargo.lock | 11 | ||||
-rw-r--r-- | src/command.rs | 40 | ||||
-rw-r--r-- | src/dotwm.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 3 |
4 files changed, 25 insertions, 33 deletions
@@ -2,8 +2,8 @@ name = "dotwm" version = "0.1.0" dependencies = [ - "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "x11 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "x11 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -12,13 +12,18 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "libc" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "pkg-config" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "x11" -version = "2.2.0" +version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", 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 {} + }; +} diff --git a/src/dotwm.rs b/src/dotwm.rs index e94b17f..d528899 100644 --- a/src/dotwm.rs +++ b/src/dotwm.rs @@ -29,10 +29,12 @@ unsafe extern "C" fn error_handler(d: *mut Display, evptr: *mut XErrorEvent) -> } pub struct DotWM { + /// Reference to the X display. pub display: *mut Display, + /// References to all the windows. + pub window_list: Vec<XWindow>, // Map with the keys as (key, mod) cw_idx: usize, - pub window_list: Vec<XWindow>, } /// DotWM state. diff --git a/src/main.rs b/src/main.rs index 02df6fd..4a0c545 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ pub mod dotwm; pub mod event; use dotwm::DotWM; -use command::exec_cmd; +use command::{exec_cmd,collect_zombies}; use event::{Event,next_event}; use safe_x11::grab_key; @@ -157,5 +157,6 @@ fn main() { }, _ => (), } + collect_zombies(); } } |