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