]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/wait-forked-but-failed-child.rs
Add a doctest for the std::string::as_string method.
[rust.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 // ignore-test
12
13 extern crate libc;
14
15 use std::io::process::Command;
16 use std::iter::IteratorExt;
17
18 use libc::funcs::posix88::unistd;
19
20
21 // "ps -A -o pid,sid,command" with GNU ps should output something like this:
22 //   PID   SID COMMAND
23 //     1     1 /sbin/init
24 //     2     0 [kthreadd]
25 //     3     0 [ksoftirqd/0]
26 // ...
27 // 12562  9237 ./spawn-failure
28 // 12563  9237 [spawn-failure] <defunct>
29 // 12564  9237 [spawn-failure] <defunct>
30 // ...
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
35 // ...
36
37 #[cfg(unix)]
38 fn find_zombies() {
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";
43
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";
48
49     let my_sid = unsafe { unistd::getsid(0) };
50
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());
53
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);
61             true
62         }
63     );
64
65     assert!( ! found, "Found at least one zombie child");
66 }
67
68 #[cfg(windows)]
69 fn find_zombies() { }
70
71 fn main() {
72     let too_long = format!("/NoSuchCommand{:0300}", 0u8);
73
74     for _ in range(0u32, 100) {
75         let invalid = Command::new(too_long.as_slice()).spawn();
76         assert!(invalid.is_err());
77     }
78
79     find_zombies();
80 }