2013-03-31 21:30:33 +01:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// 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.
|
|
|
|
|
|
2013-05-12 13:58:00 +01:00
|
|
|
//! Process spawning.
|
|
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
use cast;
|
2013-08-15 14:18:13 -07:00
|
|
|
use cell::Cell;
|
2013-05-24 19:35:29 -07:00
|
|
|
use comm::{stream, SharedChan, GenericChan, GenericPort};
|
2013-08-15 14:18:13 -07:00
|
|
|
#[cfg(not(windows))]
|
2013-05-24 19:35:29 -07:00
|
|
|
use libc;
|
2013-08-15 14:18:13 -07:00
|
|
|
use libc::{pid_t, c_int};
|
2013-01-08 19:37:25 -08:00
|
|
|
use prelude::*;
|
2012-12-23 17:41:37 -05:00
|
|
|
use task;
|
2013-07-03 16:34:17 +10:00
|
|
|
use vec::ImmutableVector;
|
2011-03-11 13:30:18 +01:00
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
use rt::io;
|
|
|
|
|
use rt::local::Local;
|
|
|
|
|
use rt::rtio::{IoFactoryObject, RtioProcessObject, RtioProcess, IoFactory};
|
|
|
|
|
use rt::uv::process;
|
|
|
|
|
|
2013-05-12 13:58:00 +01:00
|
|
|
/**
|
|
|
|
|
* A value representing a child process.
|
|
|
|
|
*
|
|
|
|
|
* The lifetime of this value is linked to the lifetime of the actual
|
|
|
|
|
* process - the Process destructor calls self.finish() which waits
|
|
|
|
|
* for the process to terminate.
|
|
|
|
|
*/
|
|
|
|
|
pub struct Process {
|
|
|
|
|
/// The unique id of the process (this should never be negative).
|
2013-04-24 21:06:26 +01:00
|
|
|
priv pid: pid_t,
|
2013-05-12 13:58:00 +01:00
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
/// The internal handle to the underlying libuv process.
|
|
|
|
|
priv handle: ~RtioProcessObject,
|
2013-05-12 13:58:00 +01:00
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
/// Some(fd), or None when stdin is being redirected from a fd not created
|
|
|
|
|
/// by Process::new.
|
|
|
|
|
priv input: Option<~io::Writer>,
|
2013-05-12 13:58:00 +01:00
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
/// Some(file), or None when stdout is being redirected to a fd not created
|
|
|
|
|
/// by Process::new.
|
|
|
|
|
priv output: Option<~io::Reader>,
|
2013-05-12 13:58:00 +01:00
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
/// Some(file), or None when stderr is being redirected to a fd not created
|
|
|
|
|
/// by Process::new.
|
|
|
|
|
priv error: Option<~io::Reader>,
|
2013-04-24 21:06:26 +01:00
|
|
|
}
|
|
|
|
|
|
2013-05-12 13:58:00 +01:00
|
|
|
/// Options that can be given when starting a Process.
|
|
|
|
|
pub struct ProcessOptions<'self> {
|
2013-04-24 21:06:26 +01:00
|
|
|
|
2013-05-12 13:58:00 +01:00
|
|
|
/**
|
|
|
|
|
* If this is None then the new process will have the same initial
|
|
|
|
|
* environment as the parent process.
|
|
|
|
|
*
|
|
|
|
|
* If this is Some(vec-of-names-and-values) then the new process will
|
|
|
|
|
* have an environment containing the given named values only.
|
|
|
|
|
*/
|
2013-08-13 15:56:17 -07:00
|
|
|
env: Option<~[(~str, ~str)]>,
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If this is None then the new process will use the same initial working
|
|
|
|
|
* directory as the parent process.
|
|
|
|
|
*
|
|
|
|
|
* If this is Some(path) then the new process will use the given path
|
|
|
|
|
* for its initial working directory.
|
|
|
|
|
*/
|
|
|
|
|
dir: Option<&'self Path>,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If this is None then a new pipe will be created for the new process's
|
|
|
|
|
* input and Process.input() will provide a Writer to write to this pipe.
|
|
|
|
|
*
|
|
|
|
|
* If this is Some(file-descriptor) then the new process will read its input
|
|
|
|
|
* from the given file descriptor, Process.input_redirected() will return
|
|
|
|
|
* true, and Process.input() will fail.
|
|
|
|
|
*/
|
|
|
|
|
in_fd: Option<c_int>,
|
|
|
|
|
|
|
|
|
|
/**
|
2013-08-16 15:41:28 +10:00
|
|
|
* If this is None then a new pipe will be created for the new program's
|
2013-05-12 13:58:00 +01:00
|
|
|
* output and Process.output() will provide a Reader to read from this pipe.
|
|
|
|
|
*
|
2013-08-15 14:18:13 -07:00
|
|
|
* If this is Some(file-descriptor) then the new process will write its
|
|
|
|
|
* output to the given file descriptor, Process.output_redirected() will
|
|
|
|
|
* return true, and Process.output() will fail.
|
2013-05-12 13:58:00 +01:00
|
|
|
*/
|
|
|
|
|
out_fd: Option<c_int>,
|
|
|
|
|
|
|
|
|
|
/**
|
2013-08-15 14:18:13 -07:00
|
|
|
* If this is None then a new pipe will be created for the new progam's
|
|
|
|
|
* error stream and Process.error() will provide a Reader to read from this
|
|
|
|
|
* pipe.
|
2013-05-12 13:58:00 +01:00
|
|
|
*
|
2013-08-15 14:18:13 -07:00
|
|
|
* If this is Some(file-descriptor) then the new process will write its
|
|
|
|
|
* error output to the given file descriptor, Process.error_redirected()
|
|
|
|
|
* will return true, and and Process.error() will fail.
|
2013-05-12 13:58:00 +01:00
|
|
|
*/
|
|
|
|
|
err_fd: Option<c_int>,
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
impl<'self> ProcessOptions<'self> {
|
2013-05-12 13:58:00 +01:00
|
|
|
/// Return a ProcessOptions that has None in every field.
|
2013-08-15 14:18:13 -07:00
|
|
|
pub fn new() -> ProcessOptions {
|
2013-05-12 13:58:00 +01:00
|
|
|
ProcessOptions {
|
|
|
|
|
env: None,
|
|
|
|
|
dir: None,
|
|
|
|
|
in_fd: None,
|
|
|
|
|
out_fd: None,
|
|
|
|
|
err_fd: None,
|
|
|
|
|
}
|
2013-04-24 21:06:26 +01:00
|
|
|
}
|
2013-04-24 00:16:48 +10:00
|
|
|
}
|
|
|
|
|
|
2013-05-12 13:58:00 +01:00
|
|
|
/// The output of a finished process.
|
|
|
|
|
pub struct ProcessOutput {
|
|
|
|
|
/// The status (exit code) of the process.
|
|
|
|
|
status: int,
|
|
|
|
|
|
|
|
|
|
/// The data that the process wrote to stdout.
|
|
|
|
|
output: ~[u8],
|
|
|
|
|
|
|
|
|
|
/// The data that the process wrote to stderr.
|
|
|
|
|
error: ~[u8],
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
impl Process {
|
2013-05-12 13:58:00 +01:00
|
|
|
/**
|
|
|
|
|
* Spawns a new Process.
|
|
|
|
|
*
|
|
|
|
|
* # Arguments
|
|
|
|
|
*
|
|
|
|
|
* * prog - The path to an executable.
|
|
|
|
|
* * args - Vector of arguments to pass to the child process.
|
|
|
|
|
* * options - Options to configure the environment of the process,
|
|
|
|
|
* the working directory and the standard IO streams.
|
|
|
|
|
*/
|
2013-08-14 21:41:40 -04:00
|
|
|
pub fn new(prog: &str, args: &[~str],
|
2013-08-15 14:18:13 -07:00
|
|
|
options: ProcessOptions) -> Option<Process> {
|
|
|
|
|
// First, translate all the stdio options into their libuv equivalents
|
|
|
|
|
let (uv_stdin, stdin) = match options.in_fd {
|
|
|
|
|
Some(fd) => (process::InheritFd(fd), None),
|
2013-05-12 13:58:00 +01:00
|
|
|
None => {
|
2013-08-15 14:18:13 -07:00
|
|
|
let p = io::pipe::PipeStream::new().expect("need stdin pipe");
|
|
|
|
|
(process::CreatePipe(p.uv_pipe(), true, false),
|
|
|
|
|
Some(~p as ~io::Writer))
|
|
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
};
|
2013-08-15 14:18:13 -07:00
|
|
|
let (uv_stdout, stdout) = match options.out_fd {
|
|
|
|
|
Some(fd) => (process::InheritFd(fd), None),
|
2013-05-12 13:58:00 +01:00
|
|
|
None => {
|
2013-08-15 14:18:13 -07:00
|
|
|
let p = io::pipe::PipeStream::new().expect("need stdout pipe");
|
|
|
|
|
(process::CreatePipe(p.uv_pipe(), false, true),
|
|
|
|
|
Some(~p as ~io::Reader))
|
|
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
};
|
2013-08-15 14:18:13 -07:00
|
|
|
let (uv_stderr, stderr) = match options.err_fd {
|
|
|
|
|
Some(fd) => (process::InheritFd(fd), None),
|
2013-05-12 13:58:00 +01:00
|
|
|
None => {
|
2013-08-15 14:18:13 -07:00
|
|
|
let p = io::pipe::PipeStream::new().expect("need stderr pipe");
|
|
|
|
|
(process::CreatePipe(p.uv_pipe(), false, true),
|
|
|
|
|
Some(~p as ~io::Reader))
|
|
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
};
|
|
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
// Next, massage our options into the libuv options
|
|
|
|
|
let dir = options.dir.map(|d| d.to_str());
|
|
|
|
|
let dir = dir.map(|d| d.as_slice());
|
|
|
|
|
let config = process::Config {
|
|
|
|
|
program: prog,
|
|
|
|
|
args: args,
|
|
|
|
|
env: options.env.map(|e| e.as_slice()),
|
|
|
|
|
cwd: dir,
|
|
|
|
|
io: [uv_stdin, uv_stdout, uv_stderr],
|
|
|
|
|
};
|
2013-05-12 13:58:00 +01:00
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
// Finally, actually spawn the process
|
2013-05-12 13:58:00 +01:00
|
|
|
unsafe {
|
2013-08-15 14:18:13 -07:00
|
|
|
let io: *mut IoFactoryObject = Local::unsafe_borrow();
|
|
|
|
|
match (*io).spawn(&config) {
|
|
|
|
|
Ok(handle) => {
|
|
|
|
|
Some(Process {
|
|
|
|
|
pid: handle.id(),
|
|
|
|
|
handle: handle,
|
|
|
|
|
input: stdin,
|
|
|
|
|
output: stdout,
|
|
|
|
|
error: stderr,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
Err(*) => { None }
|
|
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the unique id of the process
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn get_id(&self) -> pid_t { self.pid }
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
/**
|
2013-08-15 14:18:13 -07:00
|
|
|
* Returns a rt::io::Writer that can be used to write to this Process's
|
|
|
|
|
* stdin.
|
2013-05-12 13:58:00 +01:00
|
|
|
*
|
2013-08-15 14:18:13 -07:00
|
|
|
* Fails if this Process's stdin was redirected to an existing file
|
|
|
|
|
* descriptor.
|
2013-05-12 13:58:00 +01:00
|
|
|
*/
|
2013-08-15 14:18:13 -07:00
|
|
|
pub fn input<'a>(&'a mut self) -> &'a mut io::Writer {
|
|
|
|
|
let ret: &mut io::Writer = *self.input.get_mut_ref();
|
|
|
|
|
return ret;
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
2013-04-24 21:06:26 +01:00
|
|
|
|
2013-05-12 13:58:00 +01:00
|
|
|
/**
|
2013-08-15 14:18:13 -07:00
|
|
|
* Returns a rt::io::Reader that can be used to read from this Process's
|
|
|
|
|
* stdout.
|
2013-05-12 13:58:00 +01:00
|
|
|
*
|
2013-08-15 14:18:13 -07:00
|
|
|
* Fails if this Process's stdout was redirected to an existing file
|
|
|
|
|
* descriptor.
|
2013-05-12 13:58:00 +01:00
|
|
|
*/
|
2013-08-15 14:18:13 -07:00
|
|
|
pub fn output<'a>(&'a mut self) -> &'a mut io::Reader {
|
|
|
|
|
let ret: &mut io::Reader = *self.output.get_mut_ref();
|
|
|
|
|
return ret;
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-08-15 14:18:13 -07:00
|
|
|
* Returns a rt::io::Reader that can be used to read from this Process's
|
|
|
|
|
* stderr.
|
2013-05-12 13:58:00 +01:00
|
|
|
*
|
2013-08-15 14:18:13 -07:00
|
|
|
* Fails if this Process's stderr was redirected to an existing file
|
|
|
|
|
* descriptor.
|
2013-05-12 13:58:00 +01:00
|
|
|
*/
|
2013-08-15 14:18:13 -07:00
|
|
|
pub fn error<'a>(&'a mut self) -> &'a mut io::Reader {
|
|
|
|
|
let ret: &mut io::Reader = *self.error.get_mut_ref();
|
|
|
|
|
return ret;
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-08-15 14:18:13 -07:00
|
|
|
* Closes the handle to stdin, waits for the child process to terminate, and
|
|
|
|
|
* returns the exit code.
|
2013-05-12 13:58:00 +01:00
|
|
|
*
|
2013-08-15 14:18:13 -07:00
|
|
|
* If the child has already been finished then the exit code is returned.
|
2013-05-12 13:58:00 +01:00
|
|
|
*/
|
2013-08-15 14:18:13 -07:00
|
|
|
pub fn finish(&mut self) -> int {
|
|
|
|
|
// We're not going to be giving any more input, so close the input by
|
|
|
|
|
// destroying it. Also, if the output is desired, then
|
|
|
|
|
// finish_with_output is called so we discard all the outputs here. Note
|
|
|
|
|
// that the process may not terminate if we don't destroy stdio because
|
|
|
|
|
// it'll be waiting in a write which we'll just never read.
|
|
|
|
|
self.input.take();
|
|
|
|
|
self.output.take();
|
|
|
|
|
self.error.take();
|
2013-04-24 21:06:26 +01:00
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
self.handle.wait()
|
2013-04-24 21:06:26 +01:00
|
|
|
}
|
|
|
|
|
|
2013-05-12 13:58:00 +01:00
|
|
|
/**
|
2013-08-15 14:18:13 -07:00
|
|
|
* Closes the handle to stdin, waits for the child process to terminate,
|
|
|
|
|
* and reads and returns all remaining output of stdout and stderr, along
|
|
|
|
|
* with the exit code.
|
2013-05-12 13:58:00 +01:00
|
|
|
*
|
2013-08-15 14:18:13 -07:00
|
|
|
* If the child has already been finished then the exit code and any
|
|
|
|
|
* remaining unread output of stdout and stderr will be returned.
|
2013-05-12 13:58:00 +01:00
|
|
|
*
|
2013-08-15 14:18:13 -07:00
|
|
|
* This method will fail if the child process's stdout or stderr streams
|
|
|
|
|
* were redirected to existing file descriptors, or if this method has
|
|
|
|
|
* already been called.
|
2013-05-12 13:58:00 +01:00
|
|
|
*/
|
2013-08-15 14:18:13 -07:00
|
|
|
pub fn finish_with_output(&mut self) -> ProcessOutput {
|
|
|
|
|
// This should probably be a helper method in rt::io
|
|
|
|
|
fn read_everything(input: &mut io::Reader) -> ~[u8] {
|
|
|
|
|
let mut result = ~[];
|
|
|
|
|
let mut buf = [0u8, ..1024];
|
|
|
|
|
loop {
|
|
|
|
|
match input.read(buf) {
|
|
|
|
|
Some(i) => { result = result + buf.slice_to(i) }
|
|
|
|
|
None => break
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
2013-04-24 00:16:48 +10:00
|
|
|
}
|
2013-08-15 14:18:13 -07:00
|
|
|
return result;
|
2013-04-24 00:16:48 +10:00
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
let (p, ch) = stream();
|
|
|
|
|
let ch = SharedChan::new(ch);
|
|
|
|
|
let ch_clone = ch.clone();
|
2013-08-15 14:18:13 -07:00
|
|
|
|
|
|
|
|
let stderr = Cell::new(self.error.take().unwrap());
|
|
|
|
|
do task::spawn {
|
|
|
|
|
let output = read_everything(stderr.take());
|
|
|
|
|
ch.send((2, output));
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
2013-08-15 14:18:13 -07:00
|
|
|
let stdout = Cell::new(self.output.take().unwrap());
|
|
|
|
|
do task::spawn {
|
|
|
|
|
let output = read_everything(stdout.take());
|
|
|
|
|
ch_clone.send((1, output));
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let status = self.finish();
|
|
|
|
|
|
|
|
|
|
let (errs, outs) = match (p.recv(), p.recv()) {
|
|
|
|
|
((1, o), (2, e)) => (e, o),
|
|
|
|
|
((2, e), (1, o)) => (e, o),
|
|
|
|
|
((x, _), (y, _)) => {
|
|
|
|
|
fail!("unexpected file numbers: %u, %u", x, y);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ProcessOutput {status: status,
|
|
|
|
|
output: outs,
|
|
|
|
|
error: errs};
|
2013-04-24 00:16:48 +10:00
|
|
|
}
|
|
|
|
|
|
2013-04-06 20:49:52 +01:00
|
|
|
/**
|
2013-05-12 13:58:00 +01:00
|
|
|
* Terminates the process, giving it a chance to clean itself up if
|
2013-04-11 21:51:39 +01:00
|
|
|
* this is supported by the operating system.
|
|
|
|
|
*
|
|
|
|
|
* On Posix OSs SIGTERM will be sent to the process. On Win32
|
|
|
|
|
* TerminateProcess(..) will be called.
|
2013-04-06 20:49:52 +01:00
|
|
|
*/
|
2013-08-15 14:18:13 -07:00
|
|
|
pub fn destroy(&mut self) {
|
|
|
|
|
#[cfg(windows)] fn sigterm() -> int { 15 }
|
|
|
|
|
#[cfg(not(windows))] fn sigterm() -> int { libc::SIGTERM as int }
|
|
|
|
|
self.handle.kill(sigterm());
|
|
|
|
|
self.finish();
|
|
|
|
|
}
|
2013-04-11 21:51:39 +01:00
|
|
|
|
|
|
|
|
/**
|
2013-05-12 13:58:00 +01:00
|
|
|
* Terminates the process as soon as possible without giving it a
|
2013-04-11 21:51:39 +01:00
|
|
|
* chance to clean itself up.
|
|
|
|
|
*
|
|
|
|
|
* On Posix OSs SIGKILL will be sent to the process. On Win32
|
|
|
|
|
* TerminateProcess(..) will be called.
|
|
|
|
|
*/
|
2013-08-15 14:18:13 -07:00
|
|
|
pub fn force_destroy(&mut self) {
|
|
|
|
|
#[cfg(windows)] fn sigkill() -> int { 9 }
|
|
|
|
|
#[cfg(not(windows))] fn sigkill() -> int { libc::SIGKILL as int }
|
|
|
|
|
self.handle.kill(sigkill());
|
|
|
|
|
self.finish();
|
|
|
|
|
}
|
2012-01-11 13:27:46 +01:00
|
|
|
}
|
2011-10-26 16:24:31 -07:00
|
|
|
|
2013-05-12 13:58:00 +01:00
|
|
|
impl Drop for Process {
|
2013-06-20 21:06:13 -04:00
|
|
|
fn drop(&self) {
|
2013-06-10 18:18:04 -04:00
|
|
|
// FIXME(#4330) Need self by value to get mutability.
|
2013-05-12 13:58:00 +01:00
|
|
|
let mut_self: &mut Process = unsafe { cast::transmute(self) };
|
|
|
|
|
mut_self.finish();
|
2013-04-22 20:05:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/**
|
2013-05-12 13:58:00 +01:00
|
|
|
* Spawns a process and waits for it to terminate. The process will
|
|
|
|
|
* inherit the current stdin/stdout/stderr file descriptors.
|
2012-07-04 22:53:12 +01:00
|
|
|
*
|
|
|
|
|
* # Arguments
|
|
|
|
|
*
|
|
|
|
|
* * prog - The path to an executable
|
|
|
|
|
* * args - Vector of arguments to pass to the child process
|
|
|
|
|
*
|
|
|
|
|
* # Return value
|
|
|
|
|
*
|
2013-03-25 11:12:14 -07:00
|
|
|
* The process's exit code
|
2012-07-04 22:53:12 +01:00
|
|
|
*/
|
2013-05-12 13:58:00 +01:00
|
|
|
pub fn process_status(prog: &str, args: &[~str]) -> int {
|
|
|
|
|
let mut prog = Process::new(prog, args, ProcessOptions {
|
|
|
|
|
env: None,
|
|
|
|
|
dir: None,
|
|
|
|
|
in_fd: Some(0),
|
|
|
|
|
out_fd: Some(1),
|
|
|
|
|
err_fd: Some(2)
|
2013-08-15 14:18:13 -07:00
|
|
|
}).unwrap();
|
2013-05-12 13:58:00 +01:00
|
|
|
prog.finish()
|
2011-07-28 21:15:56 -07:00
|
|
|
}
|
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/**
|
2013-05-12 13:58:00 +01:00
|
|
|
* Spawns a process, records all its output, and waits for it to terminate.
|
2012-07-04 22:53:12 +01:00
|
|
|
*
|
|
|
|
|
* # Arguments
|
|
|
|
|
*
|
|
|
|
|
* * prog - The path to an executable
|
|
|
|
|
* * args - Vector of arguments to pass to the child process
|
|
|
|
|
*
|
|
|
|
|
* # Return value
|
|
|
|
|
*
|
2013-05-12 13:58:00 +01:00
|
|
|
* The process's stdout/stderr output and exit code.
|
2012-07-04 22:53:12 +01:00
|
|
|
*/
|
2013-05-12 13:58:00 +01:00
|
|
|
pub fn process_output(prog: &str, args: &[~str]) -> ProcessOutput {
|
2013-08-15 14:18:13 -07:00
|
|
|
let mut prog = Process::new(prog, args, ProcessOptions::new()).unwrap();
|
2013-05-12 13:58:00 +01:00
|
|
|
prog.finish_with_output()
|
2011-03-11 13:30:18 +01:00
|
|
|
}
|
2011-09-08 12:03:07 -07:00
|
|
|
|
2012-01-17 19:05:07 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2012-12-27 17:53:04 -08:00
|
|
|
use os;
|
2013-05-12 13:58:00 +01:00
|
|
|
use path::Path;
|
2013-08-15 14:18:13 -07:00
|
|
|
use prelude::*;
|
2013-05-12 13:58:00 +01:00
|
|
|
use str;
|
2013-08-15 14:18:13 -07:00
|
|
|
use super::*;
|
2013-08-09 16:30:44 -07:00
|
|
|
use unstable::running_on_valgrind;
|
2012-01-17 19:05:07 -08:00
|
|
|
|
2013-04-16 21:23:44 +01:00
|
|
|
#[test]
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 13:58:00 +01:00
|
|
|
fn test_process_status() {
|
2013-08-15 14:18:13 -07:00
|
|
|
assert_eq!(process_status("false", []), 1);
|
|
|
|
|
assert_eq!(process_status("true", []), 0);
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
2013-06-28 19:02:39 +09:00
|
|
|
#[test]
|
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
|
fn test_process_status() {
|
2013-08-15 14:18:13 -07:00
|
|
|
assert_eq!(process_status("/system/bin/sh", [~"-c",~"false"]), 1);
|
|
|
|
|
assert_eq!(process_status("/system/bin/sh", [~"-c",~"true"]), 0);
|
2013-06-28 19:02:39 +09:00
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
#[test]
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 13:58:00 +01:00
|
|
|
fn test_process_output_output() {
|
|
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
let ProcessOutput {status, output, error}
|
|
|
|
|
= process_output("echo", [~"hello"]);
|
2013-05-12 13:58:00 +01:00
|
|
|
let output_str = str::from_bytes(output);
|
|
|
|
|
|
|
|
|
|
assert_eq!(status, 0);
|
|
|
|
|
assert_eq!(output_str.trim().to_owned(), ~"hello");
|
2013-06-18 19:52:05 -07:00
|
|
|
// FIXME #7224
|
|
|
|
|
if !running_on_valgrind() {
|
|
|
|
|
assert_eq!(error, ~[]);
|
|
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
2013-06-28 19:02:39 +09:00
|
|
|
#[test]
|
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
|
fn test_process_output_output() {
|
|
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
let ProcessOutput {status, output, error}
|
|
|
|
|
= process_output("/system/bin/sh", [~"-c",~"echo hello"]);
|
2013-06-28 19:02:39 +09:00
|
|
|
let output_str = str::from_bytes(output);
|
|
|
|
|
|
|
|
|
|
assert_eq!(status, 0);
|
|
|
|
|
assert_eq!(output_str.trim().to_owned(), ~"hello");
|
|
|
|
|
// FIXME #7224
|
|
|
|
|
if !running_on_valgrind() {
|
|
|
|
|
assert_eq!(error, ~[]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
#[test]
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 13:58:00 +01:00
|
|
|
fn test_process_output_error() {
|
|
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
let ProcessOutput {status, output, error}
|
|
|
|
|
= process_output("mkdir", [~"."]);
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
assert_eq!(status, 1);
|
|
|
|
|
assert_eq!(output, ~[]);
|
|
|
|
|
assert!(!error.is_empty());
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
2013-06-28 19:02:39 +09:00
|
|
|
#[test]
|
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
|
fn test_process_output_error() {
|
|
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
let ProcessOutput {status, output, error}
|
|
|
|
|
= process_output("/system/bin/mkdir", [~"."]);
|
2013-06-28 19:02:39 +09:00
|
|
|
|
|
|
|
|
assert_eq!(status, 255);
|
|
|
|
|
assert_eq!(output, ~[]);
|
|
|
|
|
assert!(!error.is_empty());
|
|
|
|
|
}
|
2012-01-17 19:05:07 -08:00
|
|
|
|
|
|
|
|
#[test]
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 13:58:00 +01:00
|
|
|
fn test_finish_once() {
|
2013-08-15 14:18:13 -07:00
|
|
|
let mut prog = Process::new("false", [], ProcessOptions::new()).unwrap();
|
2013-05-12 13:58:00 +01:00
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
|
}
|
2013-06-28 19:02:39 +09:00
|
|
|
#[test]
|
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
|
fn test_finish_once() {
|
2013-08-15 14:18:13 -07:00
|
|
|
let mut prog = Process::new("/system/bin/sh", [~"-c",~"false"],
|
|
|
|
|
ProcessOptions::new()).unwrap();
|
2013-06-28 19:02:39 +09:00
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
#[test]
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 13:58:00 +01:00
|
|
|
fn test_finish_twice() {
|
2013-08-15 14:18:13 -07:00
|
|
|
let mut prog = Process::new("false", [], ProcessOptions::new()).unwrap();
|
2013-05-12 13:58:00 +01:00
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
|
}
|
2013-06-28 19:02:39 +09:00
|
|
|
#[test]
|
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
|
fn test_finish_twice() {
|
2013-08-15 14:18:13 -07:00
|
|
|
let mut prog = Process::new("/system/bin/sh", [~"-c",~"false"],
|
|
|
|
|
ProcessOptions::new()).unwrap();
|
2013-06-28 19:02:39 +09:00
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
#[test]
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 13:58:00 +01:00
|
|
|
fn test_finish_with_output_once() {
|
|
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
let prog = Process::new("echo", [~"hello"], ProcessOptions::new());
|
|
|
|
|
let mut prog = prog.unwrap();
|
|
|
|
|
let ProcessOutput {status, output, error}
|
2013-05-12 13:58:00 +01:00
|
|
|
= prog.finish_with_output();
|
|
|
|
|
let output_str = str::from_bytes(output);
|
|
|
|
|
|
|
|
|
|
assert_eq!(status, 0);
|
|
|
|
|
assert_eq!(output_str.trim().to_owned(), ~"hello");
|
2013-06-18 19:52:05 -07:00
|
|
|
// FIXME #7224
|
|
|
|
|
if !running_on_valgrind() {
|
|
|
|
|
assert_eq!(error, ~[]);
|
|
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
2013-06-28 19:02:39 +09:00
|
|
|
#[test]
|
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
|
fn test_finish_with_output_once() {
|
|
|
|
|
|
2013-08-15 14:18:13 -07:00
|
|
|
let mut prog = Process::new("/system/bin/sh", [~"-c",~"echo hello"],
|
|
|
|
|
ProcessOptions::new()).unwrap();
|
|
|
|
|
let ProcessOutput {status, output, error}
|
2013-05-12 13:58:00 +01:00
|
|
|
= prog.finish_with_output();
|
|
|
|
|
let output_str = str::from_bytes(output);
|
|
|
|
|
|
|
|
|
|
assert_eq!(status, 0);
|
|
|
|
|
assert_eq!(output_str.trim().to_owned(), ~"hello");
|
2013-06-18 19:52:05 -07:00
|
|
|
// FIXME #7224
|
|
|
|
|
if !running_on_valgrind() {
|
|
|
|
|
assert_eq!(error, ~[]);
|
|
|
|
|
}
|
2013-06-28 19:02:39 +09:00
|
|
|
}
|
2012-01-17 19:05:07 -08:00
|
|
|
|
2013-04-22 20:05:07 +01:00
|
|
|
#[test]
|
|
|
|
|
#[should_fail]
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(not(windows),not(target_os="android"))]
|
2013-05-12 13:58:00 +01:00
|
|
|
fn test_finish_with_output_redirected() {
|
2013-08-15 14:18:13 -07:00
|
|
|
let mut prog = Process::new("echo", [~"hello"], ProcessOptions {
|
2013-05-12 13:58:00 +01:00
|
|
|
env: None,
|
|
|
|
|
dir: None,
|
|
|
|
|
in_fd: Some(0),
|
|
|
|
|
out_fd: Some(1),
|
|
|
|
|
err_fd: Some(2)
|
2013-08-15 14:18:13 -07:00
|
|
|
}).unwrap();
|
|
|
|
|
// this should fail because it is not valid to read the output when it
|
|
|
|
|
// was redirected
|
2013-05-12 13:58:00 +01:00
|
|
|
prog.finish_with_output();
|
|
|
|
|
}
|
2013-06-28 19:02:39 +09:00
|
|
|
#[test]
|
|
|
|
|
#[should_fail]
|
|
|
|
|
#[cfg(not(windows),target_os="android")]
|
|
|
|
|
fn test_finish_with_output_redirected() {
|
2013-08-15 14:18:13 -07:00
|
|
|
let mut prog = Process::new("/system/bin/sh", [~"-c",~"echo hello"],
|
|
|
|
|
ProcessOptions {
|
2013-06-28 19:02:39 +09:00
|
|
|
env: None,
|
|
|
|
|
dir: None,
|
|
|
|
|
in_fd: Some(0),
|
|
|
|
|
out_fd: Some(1),
|
|
|
|
|
err_fd: Some(2)
|
2013-08-15 14:18:13 -07:00
|
|
|
}).unwrap();
|
|
|
|
|
// this should fail because it is not valid to read the output when it
|
|
|
|
|
// was redirected
|
2013-06-28 19:02:39 +09:00
|
|
|
prog.finish_with_output();
|
|
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(unix,not(target_os="android"))]
|
2013-08-15 14:18:13 -07:00
|
|
|
fn run_pwd(dir: Option<&Path>) -> Process {
|
|
|
|
|
Process::new("pwd", [], ProcessOptions {
|
2013-05-12 13:58:00 +01:00
|
|
|
dir: dir,
|
2013-08-15 14:18:13 -07:00
|
|
|
.. ProcessOptions::new()
|
|
|
|
|
}).unwrap()
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(unix,target_os="android")]
|
2013-08-15 14:18:13 -07:00
|
|
|
fn run_pwd(dir: Option<&Path>) -> Process {
|
|
|
|
|
Process::new("/system/bin/sh", [~"-c",~"pwd"], ProcessOptions {
|
2013-06-28 19:02:39 +09:00
|
|
|
dir: dir,
|
2013-08-15 14:18:13 -07:00
|
|
|
.. ProcessOptions::new()
|
|
|
|
|
}).unwrap()
|
2013-06-28 19:02:39 +09:00
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2013-08-15 14:18:13 -07:00
|
|
|
fn run_pwd(dir: Option<&Path>) -> Process {
|
|
|
|
|
Process::new("cmd", [~"/c", ~"cd"], ProcessOptions {
|
2013-05-12 13:58:00 +01:00
|
|
|
dir: dir,
|
2013-08-15 14:18:13 -07:00
|
|
|
.. ProcessOptions::new()
|
|
|
|
|
}).unwrap()
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_keep_current_working_dir() {
|
|
|
|
|
let mut prog = run_pwd(None);
|
|
|
|
|
|
|
|
|
|
let output = str::from_bytes(prog.finish_with_output().output);
|
|
|
|
|
let parent_dir = os::getcwd().normalize();
|
|
|
|
|
let child_dir = Path(output.trim()).normalize();
|
|
|
|
|
|
2013-05-31 23:11:29 -07:00
|
|
|
let parent_stat = parent_dir.stat().unwrap();
|
|
|
|
|
let child_stat = child_dir.stat().unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(parent_stat.st_dev, child_stat.st_dev);
|
|
|
|
|
assert_eq!(parent_stat.st_ino, child_stat.st_ino);
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_change_working_directory() {
|
2013-05-14 20:10:14 +01:00
|
|
|
// test changing to the parent of os::getcwd() because we know
|
|
|
|
|
// the path exists (and os::getcwd() is not expected to be root)
|
2013-05-31 23:11:29 -07:00
|
|
|
let parent_dir = os::getcwd().dir_path().normalize();
|
|
|
|
|
let mut prog = run_pwd(Some(&parent_dir));
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
let output = str::from_bytes(prog.finish_with_output().output);
|
|
|
|
|
let child_dir = Path(output.trim()).normalize();
|
|
|
|
|
|
2013-05-31 23:11:29 -07:00
|
|
|
let parent_stat = parent_dir.stat().unwrap();
|
|
|
|
|
let child_stat = child_dir.stat().unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(parent_stat.st_dev, child_stat.st_dev);
|
|
|
|
|
assert_eq!(parent_stat.st_ino, child_stat.st_ino);
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(unix,not(target_os="android"))]
|
2013-08-15 14:18:13 -07:00
|
|
|
fn run_env(env: Option<~[(~str, ~str)]>) -> Process {
|
|
|
|
|
Process::new("env", [], ProcessOptions {
|
2013-05-12 13:58:00 +01:00
|
|
|
env: env,
|
2013-08-15 14:18:13 -07:00
|
|
|
.. ProcessOptions::new()
|
|
|
|
|
}).unwrap()
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(unix,target_os="android")]
|
2013-08-15 14:18:13 -07:00
|
|
|
fn run_env(env: Option<~[(~str, ~str)]>) -> Process {
|
|
|
|
|
Process::new("/system/bin/sh", [~"-c",~"set"], ProcessOptions {
|
2013-06-28 19:02:39 +09:00
|
|
|
env: env,
|
2013-08-15 14:18:13 -07:00
|
|
|
.. ProcessOptions::new()
|
|
|
|
|
}).unwrap()
|
2013-06-28 19:02:39 +09:00
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2013-08-15 14:18:13 -07:00
|
|
|
fn run_env(env: Option<~[(~str, ~str)]>) -> Process {
|
|
|
|
|
Process::new("cmd", [~"/c", ~"set"], ProcessOptions {
|
2013-05-12 13:58:00 +01:00
|
|
|
env: env,
|
2013-08-15 14:18:13 -07:00
|
|
|
.. ProcessOptions::new()
|
|
|
|
|
}).unwrap()
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-06-28 19:02:39 +09:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 13:58:00 +01:00
|
|
|
fn test_inherit_env() {
|
2013-06-20 22:43:21 +12:00
|
|
|
if running_on_valgrind() { return; }
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
let mut prog = run_env(None);
|
|
|
|
|
let output = str::from_bytes(prog.finish_with_output().output);
|
|
|
|
|
|
2013-06-21 08:29:53 -04:00
|
|
|
let r = os::env();
|
2013-08-03 12:45:23 -04:00
|
|
|
for &(ref k, ref v) in r.iter() {
|
2013-05-12 13:58:00 +01:00
|
|
|
// don't check windows magical empty-named variables
|
2013-06-20 15:14:30 -04:00
|
|
|
assert!(k.is_empty() || output.contains(fmt!("%s=%s", *k, *v)));
|
2013-05-12 13:58:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
2013-06-28 19:02:39 +09:00
|
|
|
#[test]
|
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
|
fn test_inherit_env() {
|
|
|
|
|
if running_on_valgrind() { return; }
|
|
|
|
|
|
|
|
|
|
let mut prog = run_env(None);
|
|
|
|
|
let output = str::from_bytes(prog.finish_with_output().output);
|
|
|
|
|
|
|
|
|
|
let r = os::env();
|
2013-07-18 17:17:47 +09:00
|
|
|
for &(ref k, ref v) in r.iter() {
|
2013-06-28 19:02:39 +09:00
|
|
|
// don't check android RANDOM variables
|
2013-07-18 17:17:47 +09:00
|
|
|
if *k != ~"RANDOM" {
|
|
|
|
|
assert!(output.contains(fmt!("%s=%s", *k, *v)) ||
|
|
|
|
|
output.contains(fmt!("%s=\'%s\'", *k, *v)));
|
2013-06-28 19:02:39 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-12 13:58:00 +01:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_add_to_env() {
|
|
|
|
|
let mut new_env = os::env();
|
|
|
|
|
new_env.push((~"RUN_TEST_NEW_ENV", ~"123"));
|
|
|
|
|
|
2013-08-13 15:56:17 -07:00
|
|
|
let mut prog = run_env(Some(new_env));
|
2013-05-12 13:58:00 +01:00
|
|
|
let output = str::from_bytes(prog.finish_with_output().output);
|
|
|
|
|
|
|
|
|
|
assert!(output.contains("RUN_TEST_NEW_ENV=123"));
|
2013-04-22 20:05:07 +01:00
|
|
|
}
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|