Implemented a ProcessExit enum and helper methods to std::rt::io::process for getting process termination status, or the signal that terminated a process.

A test has been added to rtio-processes.rs to ensure signal termination is picked up correctly.
This commit is contained in:
Matthew Iselin 2013-11-12 11:37:14 +10:00
parent 88e383ef1e
commit f698decf37
15 changed files with 202 additions and 100 deletions

View file

@ -24,6 +24,7 @@
// See #9341
use std::rt::io;
use std::rt::io::process;
use std::rt::io::process::{Process, ProcessConfig, CreatePipe, Ignored};
use std::str;
@ -42,7 +43,7 @@ fn smoke() {
let p = Process::new(args);
assert!(p.is_some());
let mut p = p.unwrap();
assert_eq!(p.wait(), 0);
assert!(p.wait().success());
}
#[test]
@ -78,7 +79,27 @@ fn exit_reported_right() {
let p = Process::new(args);
assert!(p.is_some());
let mut p = p.unwrap();
assert_eq!(p.wait(), 1);
assert!(p.wait().matches_exit_status(1));
}
#[test]
#[cfg(unix, not(target_os="android"))]
fn signal_reported_right() {
let io = ~[];
let args = ProcessConfig {
program: "/bin/sh",
args: [~"-c", ~"kill -1 $$"],
env: None,
cwd: None,
io: io,
};
let p = Process::new(args);
assert!(p.is_some());
let mut p = p.unwrap();
match p.wait() {
process::ExitSignal(1) => {},
result => fail!("not terminated by signal 1 (instead, {})", result),
}
}
fn read_all(input: &mut Reader) -> ~str {
@ -100,7 +121,7 @@ fn run_output(args: ProcessConfig) -> ~str {
assert!(p.io[0].is_none());
assert!(p.io[1].is_some());
let ret = read_all(p.io[1].get_mut_ref() as &mut Reader);
assert_eq!(p.wait(), 0);
assert!(p.wait().success());
return ret;
}
@ -152,6 +173,6 @@ fn stdin_works() {
p.io[0].get_mut_ref().write("foobar".as_bytes());
p.io[0] = None; // close stdin;
let out = read_all(p.io[1].get_mut_ref() as &mut Reader);
assert_eq!(p.wait(), 0);
assert!(p.wait().success());
assert_eq!(out, ~"foobar\n");
}

View file

@ -0,0 +1,29 @@
// copyright 2013 the rust project developers. see the copyright
// file at the top-level directory of this distribution and at
// http://rust-lang.org/copyright.
//
// licensed under the apache license, version 2.0 <license-apache or
// http://www.apache.org/licenses/license-2.0> or the mit license
// <license-mit or http://opensource.org/licenses/mit>, at your
// option. this file may not be copied, modified, or distributed
// except according to those terms.
// xfail-fast
use std::{os, run};
use std::rt::io::process;
fn main() {
let args = os::args();
if args.len() >= 2 && args[1] == ~"signal" {
// Raise a segfault.
unsafe { *(0 as *mut int) = 0; }
} else {
let status = run::process_status(args[0], [~"signal"]);
match status {
process::ExitSignal(_) => {},
_ => fail!("invalid termination (was not signalled): {:?}", status)
}
}
}