]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/wait-forked-but-failed-child.rs
Imported Upstream version 1.0.0~beta
[rustc.git] / src / test / run-pass / wait-forked-but-failed-child.rs
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.
4 //
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.
10
11 // pretty-expanded FIXME #23616
12
13 #![feature(libc, old_io)]
14
15 extern crate libc;
16
17 use std::old_io::process::Command;
18
19 use libc::funcs::posix88::unistd;
20
21 // The output from "ps -A -o pid,ppid,args" should look like this:
22 // PID PPID COMMAND
23 // 1 0 /sbin/init
24 // 2 0 [kthreadd]
25 // ...
26 // 6076 9064 /bin/zsh
27 // ...
28 // 7164 6076 ./spawn-failure
29 // 7165 7164 [spawn-failure] <defunct>
30 // 7166 7164 [spawn-failure] <defunct>
31 // ...
32 // 7197 7164 [spawn-failure] <defunct>
33 // 7198 7164 ps -A -o pid,ppid,command
34 // ...
35
36 #[cfg(unix)]
37 fn find_zombies() {
38 let my_pid = unsafe { unistd::getpid() };
39
40 // http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
41 let ps_cmd_output = Command::new("ps").args(&["-A", "-o", "pid,ppid,args"]).output().unwrap();
42 let ps_output = String::from_utf8_lossy(&ps_cmd_output.output);
43
44 for (line_no, line) in ps_output.split('\n').enumerate() {
45 if 0 < line_no && 0 < line.len() &&
46 my_pid == line.split(' ').filter(|w| 0 < w.len()).nth(1)
47 .expect("1st column should be PPID")
48 .parse().ok()
49 .expect("PPID string into integer") &&
50 line.contains("defunct") {
51 panic!("Zombie child {}", line);
52 }
53 }
54 }
55
56 #[cfg(windows)]
57 fn find_zombies() { }
58
59 fn main() {
60 let too_long = format!("/NoSuchCommand{:0300}", 0u8);
61
62 let _failures = (0..100).map(|_| {
63 let cmd = Command::new(&too_long);
64 let failed = cmd.spawn();
65 assert!(failed.is_err(), "Make sure the command fails to spawn(): {:?}", cmd);
66 failed
67 }).collect::<Vec<_>>();
68
69 find_zombies();
70 // then _failures goes out of scope
71 }