Skip to content

libstd/sys/unix/process.rs: reap a zombie who didn't get through to exec(2) #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/libstd/io/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,8 @@ mod tests {
use time::Duration;
use str;
use rt::running_on_valgrind;
use iter::IteratorExt;
use libc::funcs::posix88::unistd;

// FIXME(#10380) these tests should not all be ignored on android.

Expand Down Expand Up @@ -1213,4 +1215,39 @@ mod tests {
let val = env.get(&EnvKey("PATH".to_c_str()));
assert!(val.unwrap() == &"bar".to_c_str());
}

#[test]
#[cfg(unix)]
fn test_exec_check_failure() {
#[cfg(not(target_os = "macos"))]
const FIELDS: &'static str = "pid,sid,command";
#[cfg(target_os = "macos")]
const FIELDS: &'static str = "pid,sess,command";

let too_long = format!("/nosuchcmd{:0300}", 0u8);

for _ in range(0u32, 100) {
let invalid = process::Command::new(too_long.as_slice()).spawn();
assert!(invalid.is_err());
}

let my_sid = unsafe { unistd::getsid(0) };

let mut ps_cmd = process::Command::new("ps").args(&["-A", "-o", FIELDS]).spawn().unwrap();
let ps_output = ps_cmd.stdout.as_mut().expect("output from ps").read_to_string().unwrap();

let n_my_zombies = ps_output.split('\n').enumerate().filter_map(|(line_no, line)| {
if 0 < line_no {
line.split(' ').filter(|w| 0 < w.len()).enumerate().fold(None, |acc, (word_idx, w)| {
if 0 < word_idx { acc.or(Some(w)) } else { None }
}).and_then(|sid_s| {
if my_sid == from_str(sid_s).expect("Session ID string") &&
line.contains("defunct")
{ Some(()) } else { None }
})
} else { None }
}).count();

assert!(n_my_zombies < 90);
}
}
23 changes: 18 additions & 5 deletions src/libstd/sys/unix/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use self::Req::*;

use libc::{mod, pid_t, c_void, c_int};
use c_str::CString;
use io::{mod, IoResult, IoError};
use io::{mod, IoResult, IoError, EndOfFile};
use mem;
use os;
use ptr;
Expand All @@ -20,6 +20,7 @@ use io::process::{ProcessExit, ExitStatus, ExitSignal};
use collections;
use path::BytesContainer;
use hash::Hash;
use core::i32;

use sys::{mod, retry, c, wouldblock, set_nonblocking, ms_to_timeval};
use sys::fs::FileDesc;
Expand Down Expand Up @@ -106,18 +107,28 @@ impl Process {
if pid < 0 {
return Err(super::last_error())
} else if pid > 0 {
let p = Process{ pid: pid };
drop(output);
let mut bytes = [0, ..4];
let mut bytes = [0, ..8];
return match input.read(&mut bytes) {
Ok(4) => {
Ok(8) => {
assert_eq!(b"fail", bytes.slice_from(4));
let errno = (bytes[0] as i32 << 24) |
(bytes[1] as i32 << 16) |
(bytes[2] as i32 << 8) |
(bytes[3] as i32 << 0);
assert!(p.wait(0).is_ok());
Err(super::decode_error(errno))
}
Err(..) => Ok(Process { pid: pid }),
Ok(..) => panic!("short read on the cloexec pipe"),
Err(ref e) if e.kind == EndOfFile => Ok(p),
Err(e) => {
assert!(p.wait(0).is_ok());
panic!("the cloexec pipe failed: {}", e)
},
Ok(..) => { // pipe I/O up to PIPE_BUF bytes should be atomic
assert!(p.wait(0).is_ok());
panic!("short read on the cloexec pipe")
}
};
}

Expand Down Expand Up @@ -155,11 +166,13 @@ impl Process {

fn fail(output: &mut FileDesc) -> ! {
let errno = sys::os::errno();
assert!(0 <= errno && errno <= i32::MAX as int);
let bytes = [
(errno >> 24) as u8,
(errno >> 16) as u8,
(errno >> 8) as u8,
(errno >> 0) as u8,
b'f', b'a', b'i', b'l'
];
assert!(output.write(&bytes).is_ok());
unsafe { libc::_exit(1) }
Expand Down