2014-12-04 12:05:57 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-03-05 20:33:58 -06:00
|
|
|
#![feature(libc, old_io)]
|
|
|
|
|
2014-12-04 12:05:57 -06:00
|
|
|
extern crate libc;
|
|
|
|
|
2015-01-22 18:31:00 -06:00
|
|
|
use std::old_io::process::Command;
|
2014-12-04 12:05:57 -06:00
|
|
|
|
|
|
|
use libc::funcs::posix88::unistd;
|
|
|
|
|
2014-12-06 17:05:13 -06:00
|
|
|
// The output from "ps -A -o pid,ppid,args" should look like this:
|
|
|
|
// PID PPID COMMAND
|
|
|
|
// 1 0 /sbin/init
|
2014-12-04 12:05:57 -06:00
|
|
|
// 2 0 [kthreadd]
|
|
|
|
// ...
|
2014-12-06 17:05:13 -06:00
|
|
|
// 6076 9064 /bin/zsh
|
|
|
|
// ...
|
|
|
|
// 7164 6076 ./spawn-failure
|
|
|
|
// 7165 7164 [spawn-failure] <defunct>
|
|
|
|
// 7166 7164 [spawn-failure] <defunct>
|
2014-12-04 12:05:57 -06:00
|
|
|
// ...
|
2014-12-06 17:05:13 -06:00
|
|
|
// 7197 7164 [spawn-failure] <defunct>
|
|
|
|
// 7198 7164 ps -A -o pid,ppid,command
|
2014-12-04 12:05:57 -06:00
|
|
|
// ...
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn find_zombies() {
|
2014-12-06 17:05:13 -06:00
|
|
|
let my_pid = unsafe { unistd::getpid() };
|
2014-12-04 12:05:57 -06:00
|
|
|
|
2014-12-06 17:05:13 -06:00
|
|
|
// http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
|
|
|
|
let ps_cmd_output = Command::new("ps").args(&["-A", "-o", "pid,ppid,args"]).output().unwrap();
|
2015-02-01 20:53:25 -06:00
|
|
|
let ps_output = String::from_utf8_lossy(&ps_cmd_output.output);
|
2014-12-04 12:05:57 -06:00
|
|
|
|
2014-12-06 17:05:13 -06:00
|
|
|
for (line_no, line) in ps_output.split('\n').enumerate() {
|
|
|
|
if 0 < line_no && 0 < line.len() &&
|
2015-01-02 01:53:35 -06:00
|
|
|
my_pid == line.split(' ').filter(|w| 0 < w.len()).nth(1)
|
|
|
|
.expect("1st column should be PPID")
|
2015-01-28 00:52:32 -06:00
|
|
|
.parse().ok()
|
2015-01-02 01:53:35 -06:00
|
|
|
.expect("PPID string into integer") &&
|
2014-12-06 17:05:13 -06:00
|
|
|
line.contains("defunct") {
|
|
|
|
panic!("Zombie child {}", line);
|
2014-12-04 12:05:57 -06:00
|
|
|
}
|
2014-12-06 17:05:13 -06:00
|
|
|
}
|
2014-12-04 12:05:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn find_zombies() { }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let too_long = format!("/NoSuchCommand{:0300}", 0u8);
|
|
|
|
|
2015-01-26 14:44:22 -06:00
|
|
|
let _failures = (0..100).map(|_| {
|
2015-02-01 20:53:25 -06:00
|
|
|
let cmd = Command::new(&too_long);
|
2014-12-06 17:05:13 -06:00
|
|
|
let failed = cmd.spawn();
|
2015-01-20 17:45:07 -06:00
|
|
|
assert!(failed.is_err(), "Make sure the command fails to spawn(): {:?}", cmd);
|
2014-12-06 17:05:13 -06:00
|
|
|
failed
|
2015-01-02 01:53:35 -06:00
|
|
|
}).collect::<Vec<_>>();
|
2014-12-04 12:05:57 -06:00
|
|
|
|
|
|
|
find_zombies();
|
2014-12-06 17:05:13 -06:00
|
|
|
// then _failures goes out of scope
|
2014-12-04 12:05:57 -06:00
|
|
|
}
|