2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2012-08-14 14:11:15 -05:00
|
|
|
// NB: transitionary, de-mode-ing.
|
2012-10-03 16:52:09 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
2012-08-14 14:11:15 -05:00
|
|
|
#[forbid(deprecated_pattern)];
|
2013-01-26 01:02:27 -06:00
|
|
|
#[allow(structural_records)];
|
2012-08-14 14:11:15 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
//! Process spawning
|
2012-12-23 16:41:37 -06:00
|
|
|
use io;
|
2012-09-04 13:12:17 -05:00
|
|
|
use io::ReaderUtil;
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc;
|
|
|
|
use libc::{pid_t, c_void, c_int};
|
2013-01-22 14:38:08 -06:00
|
|
|
use pipes::{stream, SharedChan};
|
2012-12-23 16:41:37 -06:00
|
|
|
use option::{Some, None};
|
|
|
|
use os;
|
2013-01-08 21:37:25 -06:00
|
|
|
use prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ptr;
|
|
|
|
use run;
|
|
|
|
use str;
|
|
|
|
use task;
|
|
|
|
use vec;
|
2011-03-11 06:30:18 -06:00
|
|
|
|
2011-11-16 22:49:38 -06:00
|
|
|
#[abi = "cdecl"]
|
2012-07-03 18:11:00 -05:00
|
|
|
extern mod rustrt {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe fn rust_run_program(argv: **libc::c_char, envp: *c_void,
|
|
|
|
dir: *libc::c_char,
|
|
|
|
in_fd: c_int, out_fd: c_int, err_fd: c_int)
|
|
|
|
-> pid_t;
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// A value representing a child process
|
2012-09-26 18:01:40 -05:00
|
|
|
pub trait Program {
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns the process id of the program
|
2011-11-15 00:29:40 -06:00
|
|
|
fn get_id() -> pid_t;
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns an io::writer that can be used to write to stdin
|
2012-08-14 15:38:35 -05:00
|
|
|
fn input() -> io::Writer;
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns an io::reader that can be used to read from stdout
|
2012-08-14 15:38:35 -05:00
|
|
|
fn output() -> io::Reader;
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns an io::reader that can be used to read from stderr
|
2012-08-14 15:38:35 -05:00
|
|
|
fn err() -> io::Reader;
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Closes the handle to the child processes standard input
|
2011-10-26 18:24:31 -05:00
|
|
|
fn close_input();
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Waits for the child process to terminate. Closes the handle
|
|
|
|
* to stdin if necessary.
|
|
|
|
*/
|
2011-10-26 18:24:31 -05:00
|
|
|
fn finish() -> int;
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Closes open handles
|
2011-10-26 18:24:31 -05:00
|
|
|
fn destroy();
|
2012-01-11 06:27:46 -06:00
|
|
|
}
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Run a program, providing stdin, stdout and stderr handles
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * prog - The path to an executable
|
|
|
|
* * args - Vector of arguments to pass to the child process
|
|
|
|
* * env - optional env-modification for child
|
|
|
|
* * dir - optional dir to run child in (default current dir)
|
|
|
|
* * in_fd - A file descriptor for the child to use as std input
|
|
|
|
* * out_fd - A file descriptor for the child to use as std output
|
|
|
|
* * err_fd - A file descriptor for the child to use as std error
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The process id of the spawned process
|
|
|
|
*/
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn spawn_process(prog: &str, args: &[~str],
|
2012-08-20 14:23:37 -05:00
|
|
|
env: &Option<~[(~str,~str)]>,
|
|
|
|
dir: &Option<~str>,
|
2012-03-12 22:04:27 -05:00
|
|
|
in_fd: c_int, out_fd: c_int, err_fd: c_int)
|
2013-01-10 23:23:07 -06:00
|
|
|
-> pid_t {
|
|
|
|
unsafe {
|
|
|
|
do with_argv(prog, args) |argv| {
|
|
|
|
do with_envp(env) |envp| {
|
|
|
|
do with_dirp(dir) |dirp| {
|
|
|
|
rustrt::rust_run_program(argv, envp, dirp,
|
|
|
|
in_fd, out_fd, err_fd)
|
|
|
|
}
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 14:11:15 -05:00
|
|
|
fn with_argv<T>(prog: &str, args: &[~str],
|
2012-06-24 22:18:18 -05:00
|
|
|
cb: fn(**libc::c_char) -> T) -> T {
|
2012-06-30 18:19:07 -05:00
|
|
|
let mut argptrs = str::as_c_str(prog, |b| ~[b]);
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut tmps = ~[];
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(args) |arg| {
|
2012-09-18 23:41:37 -05:00
|
|
|
let t = @copy *arg;
|
2012-09-26 19:33:34 -05:00
|
|
|
tmps.push(t);
|
|
|
|
argptrs.push_all(str::as_c_str(*t, |b| ~[b]));
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
2012-09-26 19:33:34 -05:00
|
|
|
argptrs.push(ptr::null());
|
2012-09-13 13:46:10 -05:00
|
|
|
vec::as_imm_buf(argptrs, |buf, _len| cb(buf))
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-08-20 14:23:37 -05:00
|
|
|
fn with_envp<T>(env: &Option<~[(~str,~str)]>,
|
2012-06-24 22:18:18 -05:00
|
|
|
cb: fn(*c_void) -> T) -> T {
|
2012-02-07 20:55:02 -06:00
|
|
|
// On posixy systems we can pass a char** for envp, which is
|
|
|
|
// a null-terminated array of "k=v\n" strings.
|
2012-08-14 14:11:15 -05:00
|
|
|
match *env {
|
2012-09-28 15:00:07 -05:00
|
|
|
Some(ref es) if !vec::is_empty(*es) => {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut tmps = ~[];
|
|
|
|
let mut ptrs = ~[];
|
2012-02-07 20:55:02 -06:00
|
|
|
|
2012-09-28 15:00:07 -05:00
|
|
|
for vec::each(*es) |e| {
|
2012-09-18 23:41:37 -05:00
|
|
|
let (k,v) = copy *e;
|
2012-08-22 19:24:52 -05:00
|
|
|
let t = @(fmt!("%s=%s", k, v));
|
2012-09-26 19:33:34 -05:00
|
|
|
tmps.push(t);
|
|
|
|
ptrs.push_all(str::as_c_str(*t, |b| ~[b]));
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
2012-09-26 19:33:34 -05:00
|
|
|
ptrs.push(ptr::null());
|
2012-09-13 13:46:10 -05:00
|
|
|
vec::as_imm_buf(ptrs, |p, _len|
|
2012-09-18 19:34:08 -05:00
|
|
|
unsafe { cb(::cast::reinterpret_cast(&p)) }
|
2012-06-30 18:19:07 -05:00
|
|
|
)
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => cb(ptr::null())
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-08-20 14:23:37 -05:00
|
|
|
fn with_envp<T>(env: &Option<~[(~str,~str)]>,
|
2012-06-24 22:18:18 -05:00
|
|
|
cb: fn(*c_void) -> T) -> T {
|
2012-02-07 20:55:02 -06:00
|
|
|
// On win32 we pass an "environment block" which is not a char**, but
|
|
|
|
// rather a concatenation of null-terminated k=v\0 sequences, with a final
|
|
|
|
// \0 to terminate.
|
2012-06-24 22:18:18 -05:00
|
|
|
unsafe {
|
2012-08-14 17:58:21 -05:00
|
|
|
match *env {
|
2012-12-07 23:52:39 -06:00
|
|
|
Some(ref es) if !vec::is_empty(*es) => {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut blk : ~[u8] = ~[];
|
2012-12-07 23:52:39 -06:00
|
|
|
for vec::each(*es) |e| {
|
2012-12-14 16:26:57 -06:00
|
|
|
let (k,v) = copy *e;
|
2012-08-22 19:24:52 -05:00
|
|
|
let t = fmt!("%s=%s", k, v);
|
2012-09-19 10:25:49 -05:00
|
|
|
let mut v : ~[u8] = ::cast::reinterpret_cast(&t);
|
2012-06-24 22:18:18 -05:00
|
|
|
blk += v;
|
2012-09-19 10:25:49 -05:00
|
|
|
::cast::forget(v);
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
blk += ~[0_u8];
|
2012-09-19 10:25:49 -05:00
|
|
|
vec::as_imm_buf(blk, |p, _len| cb(::cast::reinterpret_cast(&p)))
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => cb(ptr::null())
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
fn with_dirp<T>(d: &Option<~str>,
|
2012-06-24 22:18:18 -05:00
|
|
|
cb: fn(*libc::c_char) -> T) -> T {
|
2012-08-14 14:11:15 -05:00
|
|
|
match *d {
|
2012-09-28 15:00:07 -05:00
|
|
|
Some(ref dir) => str::as_c_str(*dir, cb),
|
2012-08-20 14:23:37 -05:00
|
|
|
None => cb(ptr::null())
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
2011-07-18 23:07:33 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Spawns a process and waits for it to terminate
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * prog - The path to an executable
|
|
|
|
* * args - Vector of arguments to pass to the child process
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* The process id
|
|
|
|
*/
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn run_program(prog: &str, args: &[~str]) -> int {
|
2012-08-20 14:23:37 -05:00
|
|
|
let pid = spawn_process(prog, args, &None, &None,
|
2012-03-20 14:38:57 -05:00
|
|
|
0i32, 0i32, 0i32);
|
2013-01-31 19:51:01 -06:00
|
|
|
if pid == -1 as pid_t { die!(); }
|
2012-08-01 19:30:05 -05:00
|
|
|
return waitpid(pid);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Spawns a process and returns a program
|
|
|
|
*
|
|
|
|
* The returned value is a boxed class containing a <program> object that can
|
|
|
|
* be used for sending and receiving data over the standard file descriptors.
|
|
|
|
* The class will ensure that file descriptors are closed properly.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * prog - The path to an executable
|
|
|
|
* * args - Vector of arguments to pass to the child process
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* A class with a <program> field
|
|
|
|
*/
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn start_program(prog: &str, args: &[~str]) -> Program {
|
2013-01-22 10:11:57 -06:00
|
|
|
let pipe_input = os::pipe();
|
|
|
|
let pipe_output = os::pipe();
|
|
|
|
let pipe_err = os::pipe();
|
|
|
|
let pid =
|
|
|
|
spawn_process(prog, args, &None, &None,
|
|
|
|
pipe_input.in, pipe_output.out,
|
|
|
|
pipe_err.out);
|
2013-01-10 23:23:07 -06:00
|
|
|
|
2013-01-22 10:11:57 -06:00
|
|
|
unsafe {
|
2013-01-31 19:51:01 -06:00
|
|
|
if pid == -1 as pid_t { die!(); }
|
2013-01-22 10:11:57 -06:00
|
|
|
libc::close(pipe_input.in);
|
|
|
|
libc::close(pipe_output.out);
|
|
|
|
libc::close(pipe_err.out);
|
|
|
|
}
|
2011-07-18 23:07:33 -05:00
|
|
|
|
2013-01-22 10:11:57 -06:00
|
|
|
struct ProgRepr {
|
|
|
|
pid: pid_t,
|
|
|
|
mut in_fd: c_int,
|
|
|
|
out_file: *libc::FILE,
|
|
|
|
err_file: *libc::FILE,
|
|
|
|
mut finished: bool,
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
|
2013-01-22 10:11:57 -06:00
|
|
|
fn close_repr_input(r: &ProgRepr) {
|
|
|
|
let invalid_fd = -1i32;
|
|
|
|
if r.in_fd != invalid_fd {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-01-22 10:11:57 -06:00
|
|
|
libc::close(r.in_fd);
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2013-01-22 10:11:57 -06:00
|
|
|
r.in_fd = invalid_fd;
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2013-01-22 10:11:57 -06:00
|
|
|
}
|
|
|
|
fn finish_repr(r: &ProgRepr) -> int {
|
|
|
|
if r.finished { return 0; }
|
|
|
|
r.finished = true;
|
|
|
|
close_repr_input(r);
|
|
|
|
return waitpid(r.pid);
|
|
|
|
}
|
|
|
|
fn destroy_repr(r: &ProgRepr) {
|
|
|
|
unsafe {
|
|
|
|
finish_repr(r);
|
|
|
|
libc::fclose(r.out_file);
|
|
|
|
libc::fclose(r.err_file);
|
2012-01-11 06:27:46 -06:00
|
|
|
}
|
2013-01-22 10:11:57 -06:00
|
|
|
}
|
|
|
|
struct ProgRes {
|
|
|
|
r: ProgRepr,
|
|
|
|
drop { destroy_repr(&self.r); }
|
|
|
|
}
|
2012-01-11 06:27:46 -06:00
|
|
|
|
2013-01-22 10:11:57 -06:00
|
|
|
fn ProgRes(r: ProgRepr) -> ProgRes {
|
|
|
|
ProgRes {
|
|
|
|
r: move r
|
2012-09-04 17:23:28 -05:00
|
|
|
}
|
2013-01-22 10:11:57 -06:00
|
|
|
}
|
2012-09-04 17:23:28 -05:00
|
|
|
|
2013-01-22 10:11:57 -06:00
|
|
|
impl ProgRes: Program {
|
|
|
|
fn get_id() -> pid_t { return self.r.pid; }
|
|
|
|
fn input() -> io::Writer {
|
|
|
|
io::fd_writer(self.r.in_fd, false)
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2013-01-22 10:11:57 -06:00
|
|
|
fn output() -> io::Reader {
|
|
|
|
io::FILE_reader(self.r.out_file, false)
|
|
|
|
}
|
|
|
|
fn err() -> io::Reader {
|
|
|
|
io::FILE_reader(self.r.err_file, false)
|
|
|
|
}
|
|
|
|
fn close_input() { close_repr_input(&self.r); }
|
|
|
|
fn finish() -> int { finish_repr(&self.r) }
|
|
|
|
fn destroy() { destroy_repr(&self.r); }
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2013-01-22 10:11:57 -06:00
|
|
|
let repr = ProgRepr {
|
|
|
|
pid: pid,
|
|
|
|
in_fd: pipe_input.out,
|
|
|
|
out_file: os::fdopen(pipe_output.in),
|
|
|
|
err_file: os::fdopen(pipe_err.in),
|
|
|
|
finished: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
ProgRes(repr) as Program
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn read_all(rd: io::Reader) -> ~str {
|
2012-10-18 08:11:03 -05:00
|
|
|
let buf = io::with_bytes_writer(|wr| {
|
|
|
|
let mut bytes = [mut 0, ..4096];
|
|
|
|
while !rd.eof() {
|
|
|
|
let nread = rd.read(bytes, bytes.len());
|
|
|
|
wr.write(bytes.view(0, nread));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
str::from_bytes(buf)
|
2011-07-28 23:15:56 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Spawns a process, waits for it to exit, and returns the exit code, and
|
|
|
|
* contents of stdout and stderr.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * prog - The path to an executable
|
|
|
|
* * args - Vector of arguments to pass to the child process
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* A record, {status: int, out: str, err: str} containing the exit code,
|
|
|
|
* the contents of stdout and the contents of stderr.
|
|
|
|
*/
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn program_output(prog: &str, args: &[~str]) ->
|
2012-07-14 00:57:48 -05:00
|
|
|
{status: int, out: ~str, err: ~str} {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
let pipe_in = os::pipe();
|
|
|
|
let pipe_out = os::pipe();
|
|
|
|
let pipe_err = os::pipe();
|
|
|
|
let pid = spawn_process(prog, args, &None, &None,
|
|
|
|
pipe_in.in, pipe_out.out, pipe_err.out);
|
|
|
|
|
|
|
|
os::close(pipe_in.in);
|
|
|
|
os::close(pipe_out.out);
|
|
|
|
os::close(pipe_err.out);
|
|
|
|
if pid == -1i32 {
|
|
|
|
os::close(pipe_in.out);
|
|
|
|
os::close(pipe_out.in);
|
|
|
|
os::close(pipe_err.in);
|
2013-01-31 19:51:01 -06:00
|
|
|
die!();
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-06-09 00:03:26 -05:00
|
|
|
|
|
|
|
os::close(pipe_in.out);
|
|
|
|
|
2013-01-10 23:23:07 -06:00
|
|
|
// Spawn two entire schedulers to read both stdout and sterr
|
|
|
|
// in parallel so we don't deadlock while blocking on one
|
|
|
|
// or the other. FIXME (#2625): Surely there's a much more
|
|
|
|
// clever way to do this.
|
2013-01-22 14:38:08 -06:00
|
|
|
let (p, ch) = stream();
|
|
|
|
let ch = SharedChan(ch);
|
|
|
|
let ch_clone = ch.clone();
|
2013-01-10 23:23:07 -06:00
|
|
|
do task::spawn_sched(task::SingleThreaded) {
|
|
|
|
let errput = readclose(pipe_err.in);
|
2013-01-22 14:38:08 -06:00
|
|
|
ch.send((2, move errput));
|
2013-01-10 23:23:07 -06:00
|
|
|
};
|
|
|
|
do task::spawn_sched(task::SingleThreaded) {
|
|
|
|
let output = readclose(pipe_out.in);
|
2013-01-22 14:38:08 -06:00
|
|
|
ch_clone.send((1, move output));
|
2012-06-09 00:03:26 -05:00
|
|
|
};
|
2013-01-10 23:23:07 -06:00
|
|
|
let status = run::waitpid(pid);
|
|
|
|
let mut errs = ~"";
|
|
|
|
let mut outs = ~"";
|
|
|
|
let mut count = 2;
|
|
|
|
while count > 0 {
|
2013-01-22 14:38:08 -06:00
|
|
|
let stream = p.recv();
|
2013-01-10 23:23:07 -06:00
|
|
|
match stream {
|
|
|
|
(1, copy s) => {
|
|
|
|
outs = move s;
|
|
|
|
}
|
|
|
|
(2, copy s) => {
|
|
|
|
errs = move s;
|
|
|
|
}
|
|
|
|
(n, _) => {
|
2013-01-31 19:51:01 -06:00
|
|
|
die!(fmt!("program_output received an unexpected file \
|
2013-01-10 23:23:07 -06:00
|
|
|
number: %u", n));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
count -= 1;
|
|
|
|
};
|
|
|
|
return {status: status, out: move outs, err: move errs};
|
|
|
|
}
|
2012-06-09 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
pub fn writeclose(fd: c_int, s: ~str) {
|
2012-09-07 20:08:21 -05:00
|
|
|
use io::WriterUtil;
|
2012-06-09 00:03:26 -05:00
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
error!("writeclose %d, %s", fd as int, s);
|
2012-06-09 00:03:26 -05:00
|
|
|
let writer = io::fd_writer(fd, false);
|
|
|
|
writer.write_str(s);
|
|
|
|
|
|
|
|
os::close(fd);
|
|
|
|
}
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
pub fn readclose(fd: c_int) -> ~str {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
let file = os::fdopen(fd);
|
|
|
|
let reader = io::FILE_reader(file, false);
|
|
|
|
let buf = io::with_bytes_writer(|writer| {
|
|
|
|
let mut bytes = [mut 0, ..4096];
|
|
|
|
while !reader.eof() {
|
|
|
|
let nread = reader.read(bytes, bytes.len());
|
|
|
|
writer.write(bytes.view(0, nread));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
os::fclose(file);
|
|
|
|
str::from_bytes(buf)
|
|
|
|
}
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-09-08 14:03:07 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Waits for a process to exit and returns the exit code
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn waitpid(pid: pid_t) -> int {
|
2012-08-01 19:30:05 -05:00
|
|
|
return waitpid_os(pid);
|
2011-09-08 14:03:07 -05:00
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2011-11-15 18:49:25 -06:00
|
|
|
fn waitpid_os(pid: pid_t) -> int {
|
|
|
|
os::waitpid(pid) as int
|
2011-09-08 14:03:07 -05:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2011-11-15 00:29:40 -06:00
|
|
|
fn waitpid_os(pid: pid_t) -> int {
|
2011-10-26 18:24:31 -05:00
|
|
|
#[cfg(target_os = "linux")]
|
2012-11-29 18:21:49 -06:00
|
|
|
#[cfg(target_os = "android")]
|
2011-11-15 00:29:40 -06:00
|
|
|
fn WIFEXITED(status: i32) -> bool {
|
|
|
|
(status & 0xffi32) == 0i32
|
2011-10-26 18:24:31 -05:00
|
|
|
}
|
2011-09-08 14:03:07 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
#[cfg(target_os = "macos")]
|
2011-12-30 02:18:55 -06:00
|
|
|
#[cfg(target_os = "freebsd")]
|
2011-11-15 00:29:40 -06:00
|
|
|
fn WIFEXITED(status: i32) -> bool {
|
|
|
|
(status & 0x7fi32) == 0i32
|
2011-10-26 18:24:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
2012-11-29 18:21:49 -06:00
|
|
|
#[cfg(target_os = "android")]
|
2011-11-15 00:29:40 -06:00
|
|
|
fn WEXITSTATUS(status: i32) -> i32 {
|
2011-11-15 16:27:55 -06:00
|
|
|
(status >> 8i32) & 0xffi32
|
2011-10-26 18:24:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
2011-12-30 02:18:55 -06:00
|
|
|
#[cfg(target_os = "freebsd")]
|
2011-11-15 00:29:40 -06:00
|
|
|
fn WEXITSTATUS(status: i32) -> i32 {
|
|
|
|
status >> 8i32
|
2011-10-26 18:24:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let status = os::waitpid(pid);
|
2012-08-01 19:30:05 -05:00
|
|
|
return if WIFEXITED(status) {
|
2011-11-15 00:29:40 -06:00
|
|
|
WEXITSTATUS(status) as int
|
2011-10-26 18:24:31 -05:00
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
|
|
|
}
|
2011-09-08 14:03:07 -05:00
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-01-08 21:37:25 -06:00
|
|
|
use debug;
|
2012-09-07 20:08:21 -05:00
|
|
|
use io::WriterUtil;
|
2013-01-08 21:37:25 -06:00
|
|
|
use option::{None, Some};
|
2012-12-27 19:53:04 -06:00
|
|
|
use os;
|
2013-01-08 21:37:25 -06:00
|
|
|
use run::{readclose, writeclose};
|
2012-12-27 19:53:04 -06:00
|
|
|
use run;
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
// Regression test for memory leaks
|
2012-06-14 21:13:17 -05:00
|
|
|
#[ignore(cfg(windows))] // FIXME (#2626)
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn test_leaks() {
|
2012-08-14 14:11:15 -05:00
|
|
|
run::run_program("echo", []);
|
|
|
|
run::start_program("echo", []);
|
|
|
|
run::program_output("echo", []);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-28 15:00:07 -05:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn test_pipes() {
|
2012-01-17 21:05:07 -06:00
|
|
|
let pipe_in = os::pipe();
|
|
|
|
let pipe_out = os::pipe();
|
|
|
|
let pipe_err = os::pipe();
|
|
|
|
|
|
|
|
let pid =
|
|
|
|
run::spawn_process(
|
2012-08-20 14:23:37 -05:00
|
|
|
"cat", [], &None, &None,
|
2012-02-07 20:55:02 -06:00
|
|
|
pipe_in.in, pipe_out.out, pipe_err.out);
|
2012-01-17 21:05:07 -06:00
|
|
|
os::close(pipe_in.in);
|
|
|
|
os::close(pipe_out.out);
|
|
|
|
os::close(pipe_err.out);
|
|
|
|
|
2013-01-31 19:51:01 -06:00
|
|
|
if pid == -1i32 { die!(); }
|
2012-07-14 00:57:48 -05:00
|
|
|
let expected = ~"test";
|
2012-09-28 15:00:07 -05:00
|
|
|
writeclose(pipe_in.out, copy expected);
|
2012-01-17 21:05:07 -06:00
|
|
|
let actual = readclose(pipe_out.in);
|
|
|
|
readclose(pipe_err.in);
|
|
|
|
os::waitpid(pid);
|
|
|
|
|
2012-12-05 13:40:47 -06:00
|
|
|
log(debug, copy expected);
|
|
|
|
log(debug, copy actual);
|
2012-01-17 21:05:07 -06:00
|
|
|
assert (expected == actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn waitpid() {
|
2012-08-14 14:11:15 -05:00
|
|
|
let pid = run::spawn_process("false", [],
|
2012-08-20 14:23:37 -05:00
|
|
|
&None, &None,
|
2012-02-07 20:55:02 -06:00
|
|
|
0i32, 0i32, 0i32);
|
2012-01-17 21:05:07 -06:00
|
|
|
let status = run::waitpid(pid);
|
|
|
|
assert status == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-11 06:30:18 -06:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|