]>
Commit | Line | Data |
---|---|---|
2c00a5a8 | 1 | // ignore-cloudabi no processes |
abe05a73 | 2 | // ignore-emscripten no processes |
48663c56 | 3 | // ignore-sgx no processes |
c34b1796 | 4 | |
0731742a | 5 | #![feature(rustc_private)] |
c34b1796 | 6 | |
1a4d82fc JJ |
7 | extern crate libc; |
8 | ||
9346a6ac | 9 | use std::process::Command; |
1a4d82fc | 10 | |
1a4d82fc JJ |
11 | // The output from "ps -A -o pid,ppid,args" should look like this: |
12 | // PID PPID COMMAND | |
13 | // 1 0 /sbin/init | |
14 | // 2 0 [kthreadd] | |
15 | // ... | |
16 | // 6076 9064 /bin/zsh | |
17 | // ... | |
18 | // 7164 6076 ./spawn-failure | |
19 | // 7165 7164 [spawn-failure] <defunct> | |
20 | // 7166 7164 [spawn-failure] <defunct> | |
21 | // ... | |
22 | // 7197 7164 [spawn-failure] <defunct> | |
23 | // 7198 7164 ps -A -o pid,ppid,command | |
24 | // ... | |
25 | ||
26 | #[cfg(unix)] | |
27 | fn find_zombies() { | |
92a42be0 | 28 | let my_pid = unsafe { libc::getpid() }; |
1a4d82fc JJ |
29 | |
30 | // http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html | |
31 | let ps_cmd_output = Command::new("ps").args(&["-A", "-o", "pid,ppid,args"]).output().unwrap(); | |
9346a6ac | 32 | let ps_output = String::from_utf8_lossy(&ps_cmd_output.stdout); |
1a4d82fc JJ |
33 | |
34 | for (line_no, line) in ps_output.split('\n').enumerate() { | |
35 | if 0 < line_no && 0 < line.len() && | |
36 | my_pid == line.split(' ').filter(|w| 0 < w.len()).nth(1) | |
37 | .expect("1st column should be PPID") | |
85aaf69f | 38 | .parse().ok() |
1a4d82fc JJ |
39 | .expect("PPID string into integer") && |
40 | line.contains("defunct") { | |
41 | panic!("Zombie child {}", line); | |
42 | } | |
43 | } | |
44 | } | |
45 | ||
46 | #[cfg(windows)] | |
47 | fn find_zombies() { } | |
48 | ||
49 | fn main() { | |
50 | let too_long = format!("/NoSuchCommand{:0300}", 0u8); | |
51 | ||
85aaf69f | 52 | let _failures = (0..100).map(|_| { |
9346a6ac | 53 | let mut cmd = Command::new(&too_long); |
1a4d82fc | 54 | let failed = cmd.spawn(); |
85aaf69f | 55 | assert!(failed.is_err(), "Make sure the command fails to spawn(): {:?}", cmd); |
1a4d82fc JJ |
56 | failed |
57 | }).collect::<Vec<_>>(); | |
58 | ||
59 | find_zombies(); | |
60 | // then _failures goes out of scope | |
61 | } |