2011-10-26 18:24:31 -05:00
|
|
|
/*
|
|
|
|
Module: run
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
Process spawning
|
|
|
|
*/
|
2011-09-01 19:27:58 -05:00
|
|
|
import str::sbuf;
|
2011-11-15 00:29:40 -06:00
|
|
|
import ctypes::{fd_t, pid_t};
|
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 {
|
2011-11-15 00:29:40 -06:00
|
|
|
fn rust_run_program(argv: *sbuf, in_fd: fd_t, out_fd: fd_t, err_fd: fd_t) ->
|
|
|
|
pid_t;
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/* Section: Types */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Resource: program_res
|
|
|
|
|
|
|
|
A resource that manages the destruction of a <program> object
|
|
|
|
|
|
|
|
program_res ensures that the destroy method is called on a
|
|
|
|
program object in order to close open file descriptors.
|
|
|
|
*/
|
|
|
|
resource program_res(p: program) { p.destroy(); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
Obj: program
|
|
|
|
|
|
|
|
An object representing a child process
|
|
|
|
*/
|
|
|
|
type program = obj {
|
|
|
|
/*
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Section: Operations */
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn arg_vec(prog: str, args: [@str]) -> [sbuf] {
|
2011-09-02 17:34:58 -05:00
|
|
|
let argptrs = str::as_buf(prog, {|buf| [buf] });
|
|
|
|
for arg in args { argptrs += str::as_buf(*arg, {|buf| [buf] }); }
|
2011-10-28 16:25:40 -05:00
|
|
|
argptrs += [ptr::null()];
|
2011-03-11 06:30:18 -06:00
|
|
|
ret argptrs;
|
|
|
|
}
|
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
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
|
|
|
|
*/
|
2011-11-15 00:29:40 -06:00
|
|
|
fn spawn_process(prog: str, args: [str], in_fd: fd_t, out_fd: fd_t, err_fd: fd_t)
|
|
|
|
-> pid_t unsafe {
|
2011-08-31 19:34:50 -05:00
|
|
|
// Note: we have to hold on to these vector references while we hold a
|
|
|
|
// pointer to their buffers
|
|
|
|
let prog = prog;
|
2011-09-12 04:27:30 -05:00
|
|
|
let args = vec::map({|arg| @arg }, args);
|
2011-07-27 07:19:39 -05:00
|
|
|
let argv = arg_vec(prog, args);
|
2011-08-19 17:16:48 -05:00
|
|
|
let pid =
|
2011-09-02 17:34:58 -05:00
|
|
|
rustrt::rust_run_program(vec::unsafe::to_ptr(argv), in_fd, out_fd,
|
|
|
|
err_fd);
|
2011-07-18 23:07:33 -05:00
|
|
|
ret pid;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2011-09-12 04:27:30 -05:00
|
|
|
fn run_program(prog: str, args: [str]) -> int {
|
2011-11-15 00:29:40 -06:00
|
|
|
ret waitpid(spawn_process(prog, args, 0i32, 0i32, 0i32));
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/*
|
|
|
|
Function: start_program
|
2011-03-11 06:30:18 -06:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
Spawns a process and returns a boxed <program_res>
|
|
|
|
|
|
|
|
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-07-29 06:31:44 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
A boxed resource of <program>
|
|
|
|
*/
|
2011-09-12 04:27:30 -05:00
|
|
|
fn start_program(prog: str, args: [str]) -> @program_res {
|
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 =
|
|
|
|
spawn_process(prog, args, 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; }
|
2011-07-26 07:06:02 -05:00
|
|
|
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);
|
2011-11-15 00:29:40 -06:00
|
|
|
obj new_program(pid: pid_t,
|
|
|
|
mutable in_fd: fd_t,
|
2011-07-27 07:19:39 -05:00
|
|
|
out_file: os::libc::FILE,
|
2011-07-28 23:15:56 -05:00
|
|
|
err_file: os::libc::FILE,
|
2011-07-27 07:19:39 -05:00
|
|
|
mutable finished: bool) {
|
2011-11-15 00:29:40 -06:00
|
|
|
fn get_id() -> pid_t { ret pid; }
|
2011-08-11 21:14:38 -05:00
|
|
|
fn input() -> io::writer {
|
2011-08-19 17:16:48 -05:00
|
|
|
ret io::new_writer(io::fd_buf_writer(in_fd, option::none));
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-08-11 21:14:38 -05:00
|
|
|
fn output() -> io::reader {
|
2011-08-19 17:16:48 -05:00
|
|
|
ret io::new_reader(io::FILE_buf_reader(out_file, option::none));
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-08-11 21:14:38 -05:00
|
|
|
fn err() -> io::reader {
|
2011-08-19 17:16:48 -05:00
|
|
|
ret io::new_reader(io::FILE_buf_reader(err_file, option::none));
|
2011-07-28 23:15:56 -05:00
|
|
|
}
|
2011-07-19 16:00:15 -05:00
|
|
|
fn close_input() {
|
2011-11-15 00:29:40 -06:00
|
|
|
let invalid_fd = -1i32;
|
2011-07-27 07:19:39 -05:00
|
|
|
if in_fd != invalid_fd {
|
2011-07-19 16:00:15 -05:00
|
|
|
os::libc::close(in_fd);
|
|
|
|
in_fd = invalid_fd;
|
|
|
|
}
|
|
|
|
}
|
2011-04-19 15:35:49 -05:00
|
|
|
fn finish() -> int {
|
2011-07-27 07:19:39 -05:00
|
|
|
if finished { ret 0; }
|
2011-03-11 06:30:18 -06:00
|
|
|
finished = true;
|
2011-07-19 16:00:15 -05:00
|
|
|
self.close_input();
|
2011-09-08 14:03:07 -05:00
|
|
|
ret waitpid(pid);
|
2011-07-27 09:52:49 -05:00
|
|
|
}
|
2011-07-29 06:31:44 -05:00
|
|
|
fn destroy() {
|
|
|
|
self.finish();
|
2011-07-27 09:52:49 -05:00
|
|
|
os::libc::fclose(out_file);
|
2011-07-28 23:15:56 -05:00
|
|
|
os::libc::fclose(err_file);
|
2011-07-27 09:52:49 -05:00
|
|
|
}
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
ret @program_res(new_program(pid, pipe_input.out,
|
2011-07-29 06:31:44 -05:00
|
|
|
os::fd_FILE(pipe_output.in),
|
2011-08-19 17:16:48 -05:00
|
|
|
os::fd_FILE(pipe_err.in), false));
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
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() {
|
2011-08-11 20:49:36 -05:00
|
|
|
let bytes = rd.read_bytes(4096u);
|
2011-09-01 19:27:58 -05:00
|
|
|
buf += str::unsafe_from_bytes(bytes);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
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.
|
|
|
|
*/
|
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} {
|
2011-07-28 23:15:56 -05:00
|
|
|
let pr = start_program(prog, args);
|
|
|
|
pr.close_input();
|
2011-10-20 05:32:43 -05:00
|
|
|
let out = read_all(pr.output());
|
|
|
|
let err = read_all(pr.err());
|
|
|
|
ret {status: pr.finish(), out: out, err: err};
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-09-08 14:03:07 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/*
|
|
|
|
Function: waitpid
|
2011-09-08 14:03:07 -05:00
|
|
|
|
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-09-08 14:03:07 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
#[cfg(target_os = "win32")]
|
|
|
|
fn waitpid_os(pid: int) -> int {
|
|
|
|
os::waitpid(pid)
|
2011-09-08 14:03:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "macos")]
|
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-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-10-26 18:24:31 -05:00
|
|
|
(status >> 8) & 0xff
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
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
|
|
|
}
|
|
|
|
|
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
|
2011-05-16 20:21:22 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-03-11 06:30:18 -06:00
|
|
|
// End:
|