2012-03-07 20:17:30 -06:00
|
|
|
#[doc ="Process spawning"];
|
2012-01-11 06:27:46 -06:00
|
|
|
import option::{some, none};
|
2012-03-12 22:04:27 -05:00
|
|
|
import libc::{pid_t, c_void, c_int};
|
2011-03-11 06:30:18 -06:00
|
|
|
|
2011-07-18 23:07:33 -05:00
|
|
|
export program;
|
|
|
|
export run_program;
|
|
|
|
export start_program;
|
|
|
|
export program_output;
|
2011-07-25 23:25:32 -05:00
|
|
|
export spawn_process;
|
2011-09-08 14:03:07 -05:00
|
|
|
export waitpid;
|
2011-07-18 23:07:33 -05:00
|
|
|
|
2011-11-16 22:49:38 -06:00
|
|
|
#[abi = "cdecl"]
|
|
|
|
native mod rustrt {
|
2012-03-14 17:10:34 -05:00
|
|
|
fn rust_run_program(argv: **libc::c_char, envp: *c_void,
|
|
|
|
dir: *libc::c_char,
|
2012-03-12 22:04:27 -05:00
|
|
|
in_fd: c_int, out_fd: c_int, err_fd: c_int)
|
2012-02-07 20:55:02 -06:00
|
|
|
-> pid_t;
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc ="A value representing a child process"]
|
2012-01-11 06:27:46 -06:00
|
|
|
iface program {
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc ="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-03-07 20:17:30 -06:00
|
|
|
#[doc ="Returns an io::writer that can be used to write to stdin"]
|
2011-10-26 18:24:31 -05:00
|
|
|
fn input() -> io::writer;
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc ="Returns an io::reader that can be used to read from stdout"]
|
2011-10-26 18:24:31 -05:00
|
|
|
fn output() -> io::reader;
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc ="Returns an io::reader that can be used to read from stderr"]
|
2011-10-26 18:24:31 -05:00
|
|
|
fn err() -> io::reader;
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "Closes the handle to the child processes standard input"]
|
2011-10-26 18:24:31 -05:00
|
|
|
fn close_input();
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
2011-10-26 18:24:31 -05:00
|
|
|
Waits for the child process to terminate. Closes the handle
|
|
|
|
to stdin if necessary.
|
2012-03-07 20:17:30 -06:00
|
|
|
"]
|
2011-10-26 18:24:31 -05:00
|
|
|
fn finish() -> int;
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc ="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-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
2011-10-26 18:24:31 -05:00
|
|
|
Run a program, providing stdin, stdout and stderr handles
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
# Arguments
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
* 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
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
# Return value
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
The process id of the spawned process
|
2012-03-07 20:17:30 -06:00
|
|
|
"]
|
2012-02-07 20:55:02 -06:00
|
|
|
fn spawn_process(prog: str, args: [str],
|
|
|
|
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)
|
2011-11-15 00:29:40 -06:00
|
|
|
-> pid_t unsafe {
|
2012-02-07 20:55:02 -06:00
|
|
|
with_argv(prog, args) {|argv|
|
|
|
|
with_envp(env) { |envp|
|
|
|
|
with_dirp(dir) { |dirp|
|
|
|
|
rustrt::rust_run_program(argv, envp, dirp,
|
|
|
|
in_fd, out_fd, err_fd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_argv<T>(prog: str, args: [str],
|
2012-03-14 17:10:34 -05:00
|
|
|
cb: fn(**libc::c_char) -> T) -> T unsafe {
|
|
|
|
let mut argptrs = str::as_c_str(prog) {|b| [b] };
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut tmps = [];
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(args) {|arg|
|
2012-02-07 20:55:02 -06:00
|
|
|
let t = @arg;
|
|
|
|
tmps += [t];
|
2012-03-14 17:10:34 -05:00
|
|
|
argptrs += str::as_c_str(*t) {|b| [b] };
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
argptrs += [ptr::null()];
|
|
|
|
vec::as_buf(argptrs, cb)
|
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-02-07 20:55:02 -06:00
|
|
|
fn with_envp<T>(env: option<[(str,str)]>,
|
2012-03-12 22:04:27 -05:00
|
|
|
cb: fn(*c_void) -> T) -> T unsafe {
|
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.
|
|
|
|
alt env {
|
2012-04-13 11:19:07 -05:00
|
|
|
some(es) if !vec::is_empty(es) {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut tmps = [];
|
|
|
|
let mut ptrs = [];
|
2012-02-07 20:55:02 -06:00
|
|
|
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(es) {|e|
|
|
|
|
let (k,v) = e;
|
2012-02-07 20:55:02 -06:00
|
|
|
let t = @(#fmt("%s=%s", k, v));
|
|
|
|
vec::push(tmps, t);
|
2012-03-14 17:10:34 -05:00
|
|
|
ptrs += str::as_c_str(*t) {|b| [b]};
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
ptrs += [ptr::null()];
|
|
|
|
vec::as_buf(ptrs) { |p| cb(::unsafe::reinterpret_cast(p)) }
|
|
|
|
}
|
2012-04-13 11:19:07 -05:00
|
|
|
_ {
|
2012-02-07 20:55:02 -06:00
|
|
|
cb(ptr::null())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-02-07 20:55:02 -06:00
|
|
|
fn with_envp<T>(env: option<[(str,str)]>,
|
2012-03-12 22:04:27 -05:00
|
|
|
cb: fn(*c_void) -> T) -> T unsafe {
|
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.
|
|
|
|
alt env {
|
2012-04-13 11:19:07 -05:00
|
|
|
some(es) if !vec::is_empty(es) {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut blk : [u8] = [];
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(es) {|e|
|
|
|
|
let (k,v) = e;
|
2012-02-07 20:55:02 -06:00
|
|
|
let t = #fmt("%s=%s", k, v);
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut v : [u8] = ::unsafe::reinterpret_cast(t);
|
2012-02-07 20:55:02 -06:00
|
|
|
blk += v;
|
2012-03-20 17:12:30 -05:00
|
|
|
::unsafe::forget(v);
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
blk += [0_u8];
|
|
|
|
vec::as_buf(blk) {|p| cb(::unsafe::reinterpret_cast(p)) }
|
|
|
|
}
|
2012-04-13 11:19:07 -05:00
|
|
|
_ {
|
2012-02-07 20:55:02 -06:00
|
|
|
cb(ptr::null())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_dirp<T>(d: option<str>,
|
2012-03-14 17:10:34 -05:00
|
|
|
cb: fn(*libc::c_char) -> T) -> T unsafe {
|
2012-02-07 20:55:02 -06:00
|
|
|
alt d {
|
2012-03-14 17:10:34 -05:00
|
|
|
some(dir) { str::as_c_str(dir, cb) }
|
2012-02-07 20:55:02 -06:00
|
|
|
none { cb(ptr::null()) }
|
|
|
|
}
|
2011-07-18 23:07:33 -05:00
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc ="
|
2011-10-26 18:24:31 -05:00
|
|
|
Spawns a process and waits for it to terminate
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
# Arguments
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
* prog - The path to an executable
|
|
|
|
* args - Vector of arguments to pass to the child process
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
# Return value
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
The process id
|
2012-03-07 20:17:30 -06:00
|
|
|
"]
|
2011-09-12 04:27:30 -05:00
|
|
|
fn run_program(prog: str, args: [str]) -> int {
|
2012-03-20 14:38:57 -05:00
|
|
|
let pid = spawn_process(prog, args, none, none,
|
|
|
|
0i32, 0i32, 0i32);
|
|
|
|
if pid == -1 as pid_t { fail; }
|
|
|
|
ret waitpid(pid);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc ="
|
2012-01-11 06:27:46 -06:00
|
|
|
Spawns a process and returns a program
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
The returned value is a boxed resource containing a <program> object that can
|
|
|
|
be used for sending and recieving data over the standard file descriptors.
|
|
|
|
The resource will ensure that file descriptors are closed properly.
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
# Arguments
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
* prog - The path to an executable
|
|
|
|
* args - Vector of arguments to pass to the child process
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
# Return value
|
2011-07-29 06:31:44 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
A boxed resource of <program>
|
2012-03-07 20:17:30 -06:00
|
|
|
"]
|
2012-01-11 06:27:46 -06:00
|
|
|
fn start_program(prog: str, args: [str]) -> program {
|
2011-07-27 07:19:39 -05:00
|
|
|
let pipe_input = os::pipe();
|
|
|
|
let pipe_output = os::pipe();
|
2011-07-28 23:15:56 -05:00
|
|
|
let pipe_err = os::pipe();
|
2011-08-19 17:16:48 -05:00
|
|
|
let pid =
|
2012-02-07 20:55:02 -06:00
|
|
|
spawn_process(prog, args, none, none,
|
|
|
|
pipe_input.in, pipe_output.out,
|
2011-08-19 17:16:48 -05:00
|
|
|
pipe_err.out);
|
2011-07-18 23:07:33 -05:00
|
|
|
|
2012-03-20 14:38:57 -05:00
|
|
|
if pid == -1 as pid_t { fail; }
|
2012-03-12 22:04:27 -05:00
|
|
|
libc::close(pipe_input.in);
|
|
|
|
libc::close(pipe_output.out);
|
|
|
|
libc::close(pipe_err.out);
|
2012-01-11 06:27:46 -06:00
|
|
|
|
|
|
|
type prog_repr = {pid: pid_t,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut in_fd: c_int,
|
2012-03-12 22:04:27 -05:00
|
|
|
out_file: *libc::FILE,
|
|
|
|
err_file: *libc::FILE,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut finished: bool};
|
2012-01-11 06:27:46 -06:00
|
|
|
|
|
|
|
fn close_repr_input(r: prog_repr) {
|
|
|
|
let invalid_fd = -1i32;
|
|
|
|
if r.in_fd != invalid_fd {
|
2012-03-12 22:04:27 -05:00
|
|
|
libc::close(r.in_fd);
|
2012-01-11 06:27:46 -06:00
|
|
|
r.in_fd = invalid_fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn finish_repr(r: prog_repr) -> int {
|
|
|
|
if r.finished { ret 0; }
|
|
|
|
r.finished = true;
|
|
|
|
close_repr_input(r);
|
|
|
|
ret waitpid(r.pid);
|
|
|
|
}
|
|
|
|
fn destroy_repr(r: prog_repr) {
|
|
|
|
finish_repr(r);
|
2012-03-12 22:04:27 -05:00
|
|
|
libc::fclose(r.out_file);
|
|
|
|
libc::fclose(r.err_file);
|
2012-01-11 06:27:46 -06:00
|
|
|
}
|
|
|
|
resource prog_res(r: prog_repr) { destroy_repr(r); }
|
|
|
|
|
|
|
|
impl of program for prog_res {
|
|
|
|
fn get_id() -> pid_t { ret self.pid; }
|
2012-01-11 08:15:54 -06:00
|
|
|
fn input() -> io::writer { io::fd_writer(self.in_fd, false) }
|
|
|
|
fn output() -> io::reader { io::FILE_reader(self.out_file, false) }
|
|
|
|
fn err() -> io::reader { io::FILE_reader(self.err_file, false) }
|
2012-01-11 06:27:46 -06:00
|
|
|
fn close_input() { close_repr_input(*self); }
|
|
|
|
fn finish() -> int { finish_repr(*self) }
|
|
|
|
fn destroy() { destroy_repr(*self); }
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2012-01-11 06:27:46 -06:00
|
|
|
let repr = {pid: pid,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut in_fd: pipe_input.out,
|
2012-03-12 22:04:27 -05:00
|
|
|
out_file: os::fdopen(pipe_output.in),
|
|
|
|
err_file: os::fdopen(pipe_err.in),
|
2012-03-26 20:35:18 -05:00
|
|
|
mut finished: false};
|
2012-01-11 06:27:46 -06:00
|
|
|
ret prog_res(repr) as program;
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn read_all(rd: io::reader) -> str {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut buf = "";
|
2011-07-28 23:15:56 -05:00
|
|
|
while !rd.eof() {
|
2011-08-11 20:49:36 -05:00
|
|
|
let bytes = rd.read_bytes(4096u);
|
2012-01-25 02:53:17 -06:00
|
|
|
buf += str::from_bytes(bytes);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-07-28 23:15:56 -05:00
|
|
|
ret buf;
|
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc ="
|
2011-10-26 18:24:31 -05:00
|
|
|
Spawns a process, waits for it to exit, and returns the exit code, and
|
|
|
|
contents of stdout and stderr.
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
# Arguments
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
* prog - The path to an executable
|
|
|
|
* args - Vector of arguments to pass to the child process
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
# Return value
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
A record, {status: int, out: str, err: str} containing the exit code,
|
|
|
|
the contents of stdout and the contents of stderr.
|
2012-03-07 20:17:30 -06:00
|
|
|
"]
|
2011-09-12 04:27:30 -05:00
|
|
|
fn program_output(prog: str, args: [str]) ->
|
2011-09-02 17:34:58 -05:00
|
|
|
{status: int, out: str, err: str} {
|
2012-06-09 00:03:26 -05:00
|
|
|
|
|
|
|
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);
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
os::close(pipe_in.out);
|
|
|
|
|
|
|
|
// Spawn two entire schedulers to read both stdout and sterr
|
|
|
|
// in parallel so we don't deadlock while blocking on one
|
2012-06-21 18:44:10 -05:00
|
|
|
// or the other. FIXME (#2625): Surely there's a much more
|
|
|
|
// clever way to do this.
|
2012-06-09 00:03:26 -05:00
|
|
|
let p = comm::port();
|
|
|
|
let ch = comm::chan(p);
|
|
|
|
task::spawn_sched(task::single_threaded) {||
|
|
|
|
let errput = readclose(pipe_err.in);
|
|
|
|
comm::send(ch, (2, errput));
|
|
|
|
};
|
|
|
|
task::spawn_sched(task::single_threaded) {||
|
|
|
|
let output = readclose(pipe_out.in);
|
|
|
|
comm::send(ch, (1, output));
|
|
|
|
};
|
|
|
|
let status = run::waitpid(pid);
|
|
|
|
let mut errs = "";
|
|
|
|
let mut outs = "";
|
|
|
|
let mut count = 2;
|
|
|
|
while count > 0 {
|
|
|
|
let stream = comm::recv(p);
|
|
|
|
alt check stream {
|
|
|
|
(1, s) {
|
|
|
|
outs = s;
|
|
|
|
}
|
|
|
|
(2, s) {
|
|
|
|
errs = s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
count -= 1;
|
|
|
|
};
|
|
|
|
ret {status: status, out: outs, err: errs};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn writeclose(fd: c_int, s: str) {
|
|
|
|
import io::writer_util;
|
|
|
|
|
|
|
|
#error("writeclose %d, %s", fd as int, s);
|
|
|
|
let writer = io::fd_writer(fd, false);
|
|
|
|
writer.write_str(s);
|
|
|
|
|
|
|
|
os::close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn readclose(fd: c_int) -> str {
|
|
|
|
let file = os::fdopen(fd);
|
|
|
|
let reader = io::FILE_reader(file, false);
|
|
|
|
let mut buf = "";
|
|
|
|
while !reader.eof() {
|
|
|
|
let bytes = reader.read_bytes(4096u);
|
|
|
|
buf += str::from_bytes(bytes);
|
|
|
|
}
|
|
|
|
os::fclose(file);
|
|
|
|
ret buf;
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-09-08 14:03:07 -05:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc ="Waits for a process to exit and returns the exit code"]
|
2011-11-15 00:29:40 -06:00
|
|
|
fn waitpid(pid: pid_t) -> int {
|
2011-10-26 18:24:31 -05:00
|
|
|
ret 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")]
|
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")]
|
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);
|
|
|
|
ret 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 {
|
|
|
|
|
|
|
|
import io::writer_util;
|
|
|
|
|
|
|
|
// Regression test for memory leaks
|
2012-06-14 21:13:17 -05:00
|
|
|
#[ignore(cfg(windows))] // FIXME (#2626)
|
2012-01-17 21:05:07 -06:00
|
|
|
fn test_leaks() {
|
|
|
|
run::run_program("echo", []);
|
|
|
|
run::start_program("echo", []);
|
|
|
|
run::program_output("echo", []);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pipes() {
|
|
|
|
let pipe_in = os::pipe();
|
|
|
|
let pipe_out = os::pipe();
|
|
|
|
let pipe_err = os::pipe();
|
|
|
|
|
|
|
|
let pid =
|
|
|
|
run::spawn_process(
|
2012-02-07 20:55:02 -06:00
|
|
|
"cat", [], none, none,
|
|
|
|
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);
|
|
|
|
|
|
|
|
if pid == -1i32 { fail; }
|
|
|
|
let expected = "test";
|
|
|
|
writeclose(pipe_in.out, expected);
|
|
|
|
let actual = readclose(pipe_out.in);
|
|
|
|
readclose(pipe_err.in);
|
|
|
|
os::waitpid(pid);
|
|
|
|
|
|
|
|
log(debug, expected);
|
|
|
|
log(debug, actual);
|
|
|
|
assert (expected == actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn waitpid() {
|
2012-02-07 20:55:02 -06:00
|
|
|
let pid = run::spawn_process("false", [],
|
|
|
|
none, none,
|
|
|
|
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:
|