summaryrefslogtreecommitdiff
path: root/src/command.rs
blob: b728776b15cc5e5a04acfff067dd5d722993dab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// See LICENSE file for copyright and license details.

//! Command execution module.
//!
use std::ffi::OsStr;
use std::process::{Command,Child};
use std::io::Result;

use libc::c_int;
use libc::pid_t;
use std::ptr;

const WNOHANG: c_int = 0x00000001;

extern {
    /// wait for a child process to stop or terminate.
    pub fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t;
}

/// 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 {}
    };
}