rust/src/libstd/run_program.rs

446 lines
11 KiB
Rust
Raw Normal View History

2011-10-26 18:24:31 -05:00
/*
Module: run
2011-10-26 18:24:31 -05:00
Process spawning
*/
2012-01-11 06:27:46 -06:00
import option::{some, none};
import str::sbuf;
import ctypes::{fd_t, pid_t, void};
2011-07-18 23:07:33 -05:00
export program;
export run_program;
export start_program;
export program_output;
export spawn_process;
export waitpid;
2011-07-18 23:07:33 -05:00
#[abi = "cdecl"]
native mod rustrt {
fn rust_run_program(argv: *sbuf, envp: *void, dir: sbuf,
in_fd: fd_t, out_fd: fd_t, err_fd: fd_t)
-> pid_t;
}
2011-10-26 18:24:31 -05:00
/* Section: Types */
/*
2012-01-11 06:27:46 -06:00
Iface: program
2011-10-26 18:24:31 -05:00
2012-01-11 06:27:46 -06:00
A value representing a child process
2011-10-26 18:24:31 -05:00
*/
2012-01-11 06:27:46 -06:00
iface program {
2011-10-26 18:24:31 -05:00
/*
Method: get_id
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
/*
Method: input
Returns an io::writer that can be used to write to stdin
*/
fn input() -> io::writer;
/*
Method: output
Returns an io::reader that can be used to read from stdout
*/
fn output() -> io::reader;
/*
Method: err
Returns an io::reader that can be used to read from stderr
*/
fn err() -> io::reader;
/*
Method: close_input
Closes the handle to the child processes standard input
*/
fn close_input();
/*
Method: finish
Waits for the child process to terminate. Closes the handle
to stdin if necessary.
*/
fn finish() -> int;
/*
Method: destroy
Closes open handles
*/
fn destroy();
2012-01-11 06:27:46 -06:00
}
2011-10-26 18:24:31 -05:00
/* Section: Operations */
/*
Function: spawn_process
Run a program, providing stdin, stdout and stderr handles
Parameters:
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)
2011-10-26 18:24:31 -05:00
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
Returns:
The process id of the spawned process
*/
fn spawn_process(prog: str, args: [str],
env: option<[(str,str)]>,
dir: option<str>,
in_fd: fd_t, out_fd: fd_t, err_fd: fd_t)
2011-11-15 00:29:40 -06:00
-> pid_t unsafe {
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],
cb: fn(*sbuf) -> T) -> T unsafe {
let argptrs = str::as_buf(prog) {|b| [b] };
let tmps = [];
for arg in args {
let t = @arg;
tmps += [t];
argptrs += str::as_buf(*t) {|b| [b] };
}
argptrs += [ptr::null()];
vec::as_buf(argptrs, cb)
}
#[cfg(target_os = "macos")]
#[cfg(target_os = "linux")]
#[cfg(target_os = "freebsd")]
fn with_envp<T>(env: option<[(str,str)]>,
cb: fn(*void) -> T) -> T unsafe {
// On posixy systems we can pass a char** for envp, which is
// a null-terminated array of "k=v\n" strings.
alt env {
some (es) {
let tmps = [];
let ptrs = [];
for (k,v) in es {
let t = @(#fmt("%s=%s", k, v));
vec::push(tmps, t);
ptrs += str::as_buf(*t) {|b| [b]};
}
ptrs += [ptr::null()];
vec::as_buf(ptrs) { |p| cb(::unsafe::reinterpret_cast(p)) }
}
none {
cb(ptr::null())
}
}
}
#[cfg(target_os = "win32")]
fn with_envp<T>(env: option<[(str,str)]>,
cb: fn(*void) -> T) -> T unsafe {
// 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 {
some (es) {
let blk : [u8] = [];
for (k,v) in es {
let t = #fmt("%s=%s", k, v);
let v : [u8] = ::unsafe::reinterpret_cast(t);
blk += v;
::unsafe::leak(v);
}
blk += [0_u8];
vec::as_buf(blk) {|p| cb(::unsafe::reinterpret_cast(p)) }
}
none {
cb(ptr::null())
}
}
}
fn with_dirp<T>(d: option<str>,
cb: fn(sbuf) -> T) -> T unsafe {
alt d {
some(dir) { str::as_buf(dir, cb) }
none { cb(ptr::null()) }
}
2011-07-18 23:07:33 -05:00
}
2011-10-26 18:24:31 -05:00
/*
Function: run_program
Spawns a process and waits for it to terminate
Parameters:
prog - The path to an executable
args - Vector of arguments to pass to the child process
Returns:
The process id
*/
fn run_program(prog: str, args: [str]) -> int {
ret waitpid(spawn_process(prog, args, none, none,
0i32, 0i32, 0i32));
}
2011-10-26 18:24:31 -05:00
/*
Function: start_program
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.
Parameters:
prog - The path to an executable
args - Vector of arguments to pass to the child process
Returns:
2011-10-26 18:24:31 -05:00
A boxed resource of <program>
*/
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();
let pid =
spawn_process(prog, args, none, none,
pipe_input.in, pipe_output.out,
pipe_err.out);
2011-07-18 23:07:33 -05:00
2011-11-15 00:29:40 -06:00
if pid == -1i32 { fail; }
os::libc::close(pipe_input.in);
os::libc::close(pipe_output.out);
2011-07-28 23:15:56 -05:00
os::libc::close(pipe_err.out);
2012-01-11 06:27:46 -06:00
type prog_repr = {pid: pid_t,
mutable in_fd: fd_t,
2012-02-01 04:45:23 -06:00
out_file: os::FILE,
err_file: os::FILE,
2012-01-11 06:27:46 -06:00
mutable finished: bool};
fn close_repr_input(r: prog_repr) {
let invalid_fd = -1i32;
if r.in_fd != invalid_fd {
os::libc::close(r.in_fd);
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);
os::libc::fclose(r.out_file);
os::libc::fclose(r.err_file);
}
resource prog_res(r: prog_repr) { destroy_repr(r); }
impl of program for prog_res {
fn get_id() -> pid_t { ret self.pid; }
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); }
}
2012-01-11 06:27:46 -06:00
let repr = {pid: pid,
mutable in_fd: pipe_input.out,
out_file: os::fd_FILE(pipe_output.in),
err_file: os::fd_FILE(pipe_err.in),
mutable finished: false};
ret prog_res(repr) as program;
}
fn read_all(rd: io::reader) -> str {
2011-09-02 17:34:58 -05:00
let buf = "";
2011-07-28 23:15:56 -05:00
while !rd.eof() {
let bytes = rd.read_bytes(4096u);
buf += str::from_bytes(bytes);
}
2011-07-28 23:15:56 -05:00
ret buf;
}
2011-10-26 18:24:31 -05:00
/*
Function: program_output
Spawns a process, waits for it to exit, and returns the exit code, and
contents of stdout and stderr.
Parameters:
prog - The path to an executable
args - Vector of arguments to pass to the child process
Returns:
A record, {status: int, out: str, err: str} containing the exit code,
the contents of stdout and the contents of stderr.
*/
fn program_output(prog: str, args: [str]) ->
2011-09-02 17:34:58 -05:00
{status: int, out: str, err: str} {
2011-07-28 23:15:56 -05:00
let pr = start_program(prog, args);
pr.close_input();
let out = read_all(pr.output());
let err = read_all(pr.err());
ret {status: pr.finish(), out: out, err: err};
}
2011-10-26 18:24:31 -05:00
/*
Function: waitpid
2011-10-26 18:24:31 -05:00
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-10-26 18:24:31 -05:00
#[cfg(target_os = "win32")]
fn waitpid_os(pid: pid_t) -> int {
os::waitpid(pid) as int
}
#[cfg(target_os = "linux")]
#[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 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-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 {
(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
};
}
}
2012-01-17 21:05:07 -06:00
#[cfg(test)]
mod tests {
import io::writer_util;
import ctypes::fd_t;
// Regression test for memory leaks
#[ignore(cfg(target_os = "win32"))] // FIXME
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(
"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);
fn writeclose(fd: fd_t, s: str) {
#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: fd_t) -> str {
// Copied from run::program_output
let file = os::fd_FILE(fd);
let reader = io::FILE_reader(file, false);
let buf = "";
while !reader.eof() {
let bytes = reader.read_bytes(4096u);
buf += str::from_bytes(bytes);
2012-01-17 21:05:07 -06:00
}
os::fclose(file);
ret buf;
}
}
#[test]
fn waitpid() {
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;
}
}
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: