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