1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 use std::io::process::Command;
16 use std::iter::IteratorExt;
18 use libc::funcs::posix88::unistd;
21 // "ps -A -o pid,sid,command" with GNU ps should output something like this:
27 // 12562 9237 ./spawn-failure
28 // 12563 9237 [spawn-failure] <defunct>
29 // 12564 9237 [spawn-failure] <defunct>
31 // 12592 9237 [spawn-failure] <defunct>
32 // 12593 9237 ps -A -o pid,sid,command
33 // 12884 12884 /bin/zsh
34 // 12922 12922 /bin/zsh
39 // http://man.freebsd.org/ps(1)
40 // http://man7.org/linux/man-pages/man1/ps.1.html
41 #[cfg(not(target_os = "macos"))]
42 const FIELDS: &'static str = "pid,sid,command";
44 // https://developer.apple.com/library/mac/documentation/Darwin/
45 // Reference/ManPages/man1/ps.1.html
46 #[cfg(target_os = "macos")]
47 const FIELDS: &'static str = "pid,sess,command";
49 let my_sid = unsafe { unistd::getsid(0) };
51 let ps_cmd_output = Command::new("ps").args(&["-A", "-o", FIELDS]).output().unwrap();
52 let ps_output = String::from_utf8_lossy(ps_cmd_output.output.as_slice());
54 let found = ps_output.split('\n').enumerate().any(|(line_no, line)|
55 0 < line_no && 0 < line.len() &&
56 my_sid == from_str(line.split(' ').filter(|w| 0 < w.len()).nth(1)
57 .expect("1st column should be Session ID")
58 ).expect("Session ID string into integer") &&
59 line.contains("defunct") && {
60 println!("Zombie child {}", line);
65 assert!( ! found, "Found at least one zombie child");
72 let too_long = format!("/NoSuchCommand{:0300}", 0u8);
74 for _ in range(0u32, 100) {
75 let invalid = Command::new(too_long.as_slice()).spawn();
76 assert!(invalid.is_err());