2013-03-31 15:30:33 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06: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 07:58:00 -05:00
|
|
|
//! Process spawning.
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
use cast;
|
2013-07-02 14:47:32 -05:00
|
|
|
use clone::Clone;
|
2013-05-24 21:35:29 -05:00
|
|
|
use comm::{stream, SharedChan, GenericChan, GenericPort};
|
2012-12-23 16:41:37 -06:00
|
|
|
use io;
|
2013-06-25 19:02:03 -05:00
|
|
|
use iterator::IteratorUtil;
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc::{pid_t, c_void, c_int};
|
2013-05-24 21:35:29 -05:00
|
|
|
use libc;
|
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 task;
|
2013-07-03 01:34:17 -05:00
|
|
|
use vec::ImmutableVector;
|
2011-03-11 06:30:18 -06:00
|
|
|
|
2013-05-12 07:58:00 -05: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 15:06:26 -05:00
|
|
|
priv pid: pid_t,
|
2013-05-12 07:58:00 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A handle to the process - on unix this will always be NULL, but on
|
|
|
|
* windows it will be a HANDLE to the process, which will prevent the
|
|
|
|
* pid being re-used until the handle is closed.
|
|
|
|
*/
|
2013-04-24 15:06:26 -05:00
|
|
|
priv handle: *(),
|
2013-05-12 07:58:00 -05:00
|
|
|
|
|
|
|
/// Some(fd), or None when stdin is being redirected from a fd not created by Process::new.
|
|
|
|
priv input: Option<c_int>,
|
|
|
|
|
2013-05-14 16:47:30 -05:00
|
|
|
/// Some(file), or None when stdout is being redirected to a fd not created by Process::new.
|
2013-05-12 07:58:00 -05:00
|
|
|
priv output: Option<*libc::FILE>,
|
|
|
|
|
2013-05-14 16:47:30 -05:00
|
|
|
/// Some(file), or None when stderr is being redirected to a fd not created by Process::new.
|
2013-05-12 07:58:00 -05:00
|
|
|
priv error: Option<*libc::FILE>,
|
|
|
|
|
|
|
|
/// None until finish() is called.
|
|
|
|
priv exit_code: Option<int>,
|
2013-04-24 15:06:26 -05:00
|
|
|
}
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
/// Options that can be given when starting a Process.
|
|
|
|
pub struct ProcessOptions<'self> {
|
2013-04-24 15:06:26 -05:00
|
|
|
|
2013-05-12 07:58:00 -05: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.
|
|
|
|
*/
|
|
|
|
env: Option<&'self [(~str, ~str)]>,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this is None then a new pipe will be created for the new progam's
|
|
|
|
* output and Process.output() will provide a Reader to read from this pipe.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
out_fd: Option<c_int>,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
err_fd: Option<c_int>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <'self> ProcessOptions<'self> {
|
|
|
|
/// Return a ProcessOptions that has None in every field.
|
|
|
|
pub fn new<'a>() -> ProcessOptions<'a> {
|
|
|
|
ProcessOptions {
|
|
|
|
env: None,
|
|
|
|
dir: None,
|
|
|
|
in_fd: None,
|
|
|
|
out_fd: None,
|
|
|
|
err_fd: None,
|
|
|
|
}
|
2013-04-24 15:06:26 -05:00
|
|
|
}
|
2013-04-23 09:16:48 -05:00
|
|
|
}
|
|
|
|
|
2013-05-12 07:58:00 -05: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 17:17:22 -05:00
|
|
|
impl Process {
|
2013-05-12 07:58:00 -05: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-05-31 17:17:22 -05:00
|
|
|
pub fn new(prog: &str, args: &[~str], options: ProcessOptions)
|
|
|
|
-> Process {
|
2013-05-12 07:58:00 -05:00
|
|
|
let (in_pipe, in_fd) = match options.in_fd {
|
|
|
|
None => {
|
|
|
|
let pipe = os::pipe();
|
|
|
|
(Some(pipe), pipe.in)
|
|
|
|
},
|
|
|
|
Some(fd) => (None, fd)
|
|
|
|
};
|
|
|
|
let (out_pipe, out_fd) = match options.out_fd {
|
|
|
|
None => {
|
|
|
|
let pipe = os::pipe();
|
|
|
|
(Some(pipe), pipe.out)
|
|
|
|
},
|
|
|
|
Some(fd) => (None, fd)
|
|
|
|
};
|
|
|
|
let (err_pipe, err_fd) = match options.err_fd {
|
|
|
|
None => {
|
|
|
|
let pipe = os::pipe();
|
|
|
|
(Some(pipe), pipe.out)
|
|
|
|
},
|
|
|
|
Some(fd) => (None, fd)
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = spawn_process_os(prog, args, options.env, options.dir,
|
|
|
|
in_fd, out_fd, err_fd);
|
|
|
|
|
|
|
|
unsafe {
|
2013-06-10 16:50:12 -05:00
|
|
|
for in_pipe.iter().advance |pipe| { libc::close(pipe.in); }
|
|
|
|
for out_pipe.iter().advance |pipe| { libc::close(pipe.out); }
|
|
|
|
for err_pipe.iter().advance |pipe| { libc::close(pipe.out); }
|
2013-05-12 07:58:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Process {
|
|
|
|
pid: res.pid,
|
|
|
|
handle: res.handle,
|
|
|
|
input: in_pipe.map(|pipe| pipe.out),
|
|
|
|
output: out_pipe.map(|pipe| os::fdopen(pipe.in)),
|
|
|
|
error: err_pipe.map(|pipe| os::fdopen(pipe.in)),
|
|
|
|
exit_code: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the unique id of the process
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_id(&self) -> pid_t { self.pid }
|
2013-05-12 07:58:00 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
fn input_fd(&mut self) -> c_int {
|
2013-05-12 07:58:00 -05:00
|
|
|
match self.input {
|
|
|
|
Some(fd) => fd,
|
|
|
|
None => fail!("This Process's stdin was redirected to an \
|
|
|
|
existing file descriptor.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
fn output_file(&mut self) -> *libc::FILE {
|
2013-05-12 07:58:00 -05:00
|
|
|
match self.output {
|
|
|
|
Some(file) => file,
|
|
|
|
None => fail!("This Process's stdout was redirected to an \
|
|
|
|
existing file descriptor.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
fn error_file(&mut self) -> *libc::FILE {
|
2013-05-12 07:58:00 -05:00
|
|
|
match self.error {
|
|
|
|
Some(file) => file,
|
|
|
|
None => fail!("This Process's stderr was redirected to an \
|
|
|
|
existing file descriptor.")
|
|
|
|
}
|
|
|
|
}
|
2013-04-24 15:06:26 -05:00
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
/**
|
|
|
|
* Returns whether this process is reading its stdin from an existing file
|
|
|
|
* descriptor rather than a pipe that was created specifically for this
|
|
|
|
* process.
|
|
|
|
*
|
|
|
|
* If this method returns true then self.input() will fail.
|
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn input_redirected(&self) -> bool {
|
2013-05-12 07:58:00 -05:00
|
|
|
self.input.is_none()
|
|
|
|
}
|
2013-04-24 15:06:26 -05:00
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
/**
|
|
|
|
* Returns whether this process is writing its stdout to an existing file
|
|
|
|
* descriptor rather than a pipe that was created specifically for this
|
|
|
|
* process.
|
|
|
|
*
|
|
|
|
* If this method returns true then self.output() will fail.
|
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn output_redirected(&self) -> bool {
|
2013-05-12 07:58:00 -05:00
|
|
|
self.output.is_none()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this process is writing its stderr to an existing file
|
|
|
|
* descriptor rather than a pipe that was created specifically for this
|
|
|
|
* process.
|
|
|
|
*
|
|
|
|
* If this method returns true then self.error() will fail.
|
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn error_redirected(&self) -> bool {
|
2013-05-12 07:58:00 -05:00
|
|
|
self.error.is_none()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an io::Writer that can be used to write to this Process's stdin.
|
|
|
|
*
|
|
|
|
* Fails if this Process's stdin was redirected to an existing file descriptor.
|
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn input(&mut self) -> @io::Writer {
|
2013-05-12 07:58:00 -05:00
|
|
|
// FIXME: the Writer can still be used after self is destroyed: #2625
|
2013-05-14 16:47:30 -05:00
|
|
|
io::fd_writer(self.input_fd(), false)
|
2013-04-24 15:06:26 -05:00
|
|
|
}
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
/**
|
|
|
|
* Returns an io::Reader that can be used to read from this Process's stdout.
|
|
|
|
*
|
|
|
|
* Fails if this Process's stdout was redirected to an existing file descriptor.
|
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn output(&mut self) -> @io::Reader {
|
2013-05-12 07:58:00 -05:00
|
|
|
// FIXME: the Reader can still be used after self is destroyed: #2625
|
2013-05-14 16:47:30 -05:00
|
|
|
io::FILE_reader(self.output_file(), false)
|
2013-04-24 15:06:26 -05:00
|
|
|
}
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
/**
|
|
|
|
* Returns an io::Reader that can be used to read from this Process's stderr.
|
|
|
|
*
|
|
|
|
* Fails if this Process's stderr was redirected to an existing file descriptor.
|
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn error(&mut self) -> @io::Reader {
|
2013-05-12 07:58:00 -05:00
|
|
|
// FIXME: the Reader can still be used after self is destroyed: #2625
|
2013-05-14 16:47:30 -05:00
|
|
|
io::FILE_reader(self.error_file(), false)
|
2013-04-24 15:06:26 -05:00
|
|
|
}
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
/**
|
|
|
|
* Closes the handle to the child process's stdin.
|
|
|
|
*
|
|
|
|
* If this process is reading its stdin from an existing file descriptor, then this
|
|
|
|
* method does nothing.
|
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn close_input(&mut self) {
|
2013-05-12 07:58:00 -05:00
|
|
|
match self.input {
|
|
|
|
Some(-1) | None => (),
|
|
|
|
Some(fd) => {
|
|
|
|
unsafe {
|
|
|
|
libc::close(fd);
|
|
|
|
}
|
|
|
|
self.input = Some(-1);
|
2013-04-23 09:16:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
fn close_outputs(&mut self) {
|
2013-05-12 07:58:00 -05:00
|
|
|
fclose_and_null(&mut self.output);
|
|
|
|
fclose_and_null(&mut self.error);
|
|
|
|
|
|
|
|
fn fclose_and_null(f_opt: &mut Option<*libc::FILE>) {
|
|
|
|
match *f_opt {
|
|
|
|
Some(f) if !f.is_null() => {
|
|
|
|
unsafe {
|
|
|
|
libc::fclose(f);
|
|
|
|
*f_opt = Some(0 as *libc::FILE);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
2013-04-23 09:16:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 15:06:26 -05:00
|
|
|
/**
|
2013-05-12 07:58:00 -05:00
|
|
|
* Closes the handle to stdin, waits for the child process to terminate,
|
|
|
|
* and returns the exit code.
|
|
|
|
*
|
|
|
|
* If the child has already been finished then the exit code is returned.
|
2013-04-24 15:06:26 -05:00
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn finish(&mut self) -> int {
|
2013-06-10 16:50:12 -05:00
|
|
|
for self.exit_code.iter().advance |&code| {
|
2013-05-12 07:58:00 -05:00
|
|
|
return code;
|
|
|
|
}
|
2013-04-23 09:16:48 -05:00
|
|
|
self.close_input();
|
2013-05-12 07:58:00 -05:00
|
|
|
let code = waitpid(self.pid);
|
|
|
|
self.exit_code = Some(code);
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* If the child has already been finished then the exit code and any remaining
|
|
|
|
* unread output of stdout and stderr will be returned.
|
|
|
|
*
|
|
|
|
* This method will fail if the child process's stdout or stderr streams were
|
|
|
|
* redirected to existing file descriptors.
|
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn finish_with_output(&mut self) -> ProcessOutput {
|
2013-05-14 16:47:30 -05:00
|
|
|
let output_file = self.output_file();
|
|
|
|
let error_file = self.error_file();
|
2013-05-12 07:58:00 -05: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.
|
|
|
|
let (p, ch) = stream();
|
|
|
|
let ch = SharedChan::new(ch);
|
|
|
|
let ch_clone = ch.clone();
|
|
|
|
do task::spawn_sched(task::SingleThreaded) {
|
|
|
|
let errput = io::FILE_reader(error_file, false);
|
|
|
|
ch.send((2, errput.read_whole_stream()));
|
|
|
|
}
|
|
|
|
do task::spawn_sched(task::SingleThreaded) {
|
|
|
|
let output = io::FILE_reader(output_file, false);
|
|
|
|
ch_clone.send((1, output.read_whole_stream()));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-23 09:16:48 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
fn destroy_internal(&mut self, force: bool) {
|
2013-05-12 07:58:00 -05:00
|
|
|
// if the process has finished, and therefore had waitpid called,
|
|
|
|
// and we kill it, then on unix we might ending up killing a
|
|
|
|
// newer process that happens to have the same (re-used) id
|
|
|
|
if self.exit_code.is_none() {
|
|
|
|
killpid(self.pid, force);
|
|
|
|
self.finish();
|
|
|
|
}
|
2013-04-23 09:16:48 -05:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn killpid(pid: pid_t, _force: bool) {
|
|
|
|
unsafe {
|
|
|
|
libc::funcs::extra::kernel32::TerminateProcess(
|
|
|
|
cast::transmute(pid), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn killpid(pid: pid_t, force: bool) {
|
|
|
|
let signal = if force {
|
|
|
|
libc::consts::os::posix88::SIGKILL
|
|
|
|
} else {
|
|
|
|
libc::consts::os::posix88::SIGTERM
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
libc::funcs::posix88::signal::kill(pid, signal as c_int);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2013-04-06 14:49:52 -05:00
|
|
|
/**
|
2013-05-12 07:58:00 -05:00
|
|
|
* Terminates the process, giving it a chance to clean itself up if
|
2013-04-11 15:51:39 -05: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 14:49:52 -05:00
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn destroy(&mut self) { self.destroy_internal(false); }
|
2013-04-11 15:51:39 -05:00
|
|
|
|
|
|
|
/**
|
2013-05-12 07:58:00 -05:00
|
|
|
* Terminates the process as soon as possible without giving it a
|
2013-04-11 15:51:39 -05:00
|
|
|
* chance to clean itself up.
|
|
|
|
*
|
|
|
|
* On Posix OSs SIGKILL will be sent to the process. On Win32
|
|
|
|
* TerminateProcess(..) will be called.
|
|
|
|
*/
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn force_destroy(&mut self) { self.destroy_internal(true); }
|
2012-01-11 06:27:46 -06:00
|
|
|
}
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
impl Drop for Process {
|
2013-06-20 20:06:13 -05:00
|
|
|
fn drop(&self) {
|
2013-06-10 17:18:04 -05:00
|
|
|
// FIXME(#4330) Need self by value to get mutability.
|
2013-05-12 07:58:00 -05:00
|
|
|
let mut_self: &mut Process = unsafe { cast::transmute(self) };
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
mut_self.finish();
|
|
|
|
mut_self.close_outputs();
|
|
|
|
free_handle(self.handle);
|
|
|
|
}
|
2013-04-22 14:05:07 -05:00
|
|
|
}
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
struct SpawnProcessResult {
|
2013-04-30 17:00:07 -05:00
|
|
|
pid: pid_t,
|
|
|
|
handle: *(),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn spawn_process_os(prog: &str, args: &[~str],
|
|
|
|
env: Option<&[(~str, ~str)]>,
|
|
|
|
dir: Option<&Path>,
|
|
|
|
in_fd: c_int, out_fd: c_int, err_fd: c_int) -> SpawnProcessResult {
|
2013-04-30 17:00:07 -05:00
|
|
|
|
|
|
|
use libc::types::os::arch::extra::{DWORD, HANDLE, STARTUPINFO};
|
|
|
|
use libc::consts::os::extra::{
|
|
|
|
TRUE, FALSE,
|
|
|
|
STARTF_USESTDHANDLES,
|
|
|
|
INVALID_HANDLE_VALUE,
|
|
|
|
DUPLICATE_SAME_ACCESS
|
|
|
|
};
|
|
|
|
use libc::funcs::extra::kernel32::{
|
|
|
|
GetCurrentProcess,
|
|
|
|
DuplicateHandle,
|
|
|
|
CloseHandle,
|
|
|
|
CreateProcessA
|
|
|
|
};
|
|
|
|
use libc::funcs::extra::msvcrt::get_osfhandle;
|
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use sys;
|
|
|
|
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-04-30 17:00:07 -05:00
|
|
|
|
|
|
|
let mut si = zeroed_startupinfo();
|
|
|
|
si.cb = sys::size_of::<STARTUPINFO>() as DWORD;
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
|
|
|
|
|
|
|
let cur_proc = GetCurrentProcess();
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
let orig_std_in = get_osfhandle(in_fd) as HANDLE;
|
2013-04-30 17:00:07 -05:00
|
|
|
if orig_std_in == INVALID_HANDLE_VALUE as HANDLE {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in get_osfhandle: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
if DuplicateHandle(cur_proc, orig_std_in, cur_proc, &mut si.hStdInput,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in DuplicateHandle: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
let orig_std_out = get_osfhandle(out_fd) as HANDLE;
|
2013-04-30 17:00:07 -05:00
|
|
|
if orig_std_out == INVALID_HANDLE_VALUE as HANDLE {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in get_osfhandle: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
if DuplicateHandle(cur_proc, orig_std_out, cur_proc, &mut si.hStdOutput,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in DuplicateHandle: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
let orig_std_err = get_osfhandle(err_fd) as HANDLE;
|
|
|
|
if orig_std_err == INVALID_HANDLE_VALUE as HANDLE {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in get_osfhandle: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
if DuplicateHandle(cur_proc, orig_std_err, cur_proc, &mut si.hStdError,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in DuplicateHandle: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let cmd = make_command_line(prog, args);
|
|
|
|
let mut pi = zeroed_process_information();
|
|
|
|
let mut create_err = None;
|
|
|
|
|
|
|
|
do with_envp(env) |envp| {
|
|
|
|
do with_dirp(dir) |dirp| {
|
2013-07-22 23:41:46 -05:00
|
|
|
do cmd.as_c_str |cmdp| {
|
2013-04-30 17:00:07 -05:00
|
|
|
let created = CreateProcessA(ptr::null(), cast::transmute(cmdp),
|
|
|
|
ptr::mut_null(), ptr::mut_null(), TRUE,
|
|
|
|
0, envp, dirp, &mut si, &mut pi);
|
|
|
|
if created == FALSE {
|
|
|
|
create_err = Some(os::last_os_error());
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
}
|
2013-04-30 17:00:07 -05:00
|
|
|
|
|
|
|
CloseHandle(si.hStdInput);
|
|
|
|
CloseHandle(si.hStdOutput);
|
|
|
|
CloseHandle(si.hStdError);
|
|
|
|
|
2013-06-11 13:54:05 -05:00
|
|
|
for create_err.iter().advance |msg| {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in CreateProcess: %s", *msg);
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// We close the thread handle because we don't care about keeping the thread id valid,
|
|
|
|
// and we aren't keeping the thread handle around to be able to close it later. We don't
|
|
|
|
// close the process handle however because we want the process id to stay valid at least
|
|
|
|
// until the calling code closes the process handle.
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
SpawnProcessResult {
|
2013-04-30 17:00:07 -05:00
|
|
|
pid: pi.dwProcessId as pid_t,
|
|
|
|
handle: pi.hProcess as *()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn zeroed_startupinfo() -> libc::types::os::arch::extra::STARTUPINFO {
|
|
|
|
libc::types::os::arch::extra::STARTUPINFO {
|
|
|
|
cb: 0,
|
|
|
|
lpReserved: ptr::mut_null(),
|
|
|
|
lpDesktop: ptr::mut_null(),
|
|
|
|
lpTitle: ptr::mut_null(),
|
|
|
|
dwX: 0,
|
|
|
|
dwY: 0,
|
|
|
|
dwXSize: 0,
|
|
|
|
dwYSize: 0,
|
|
|
|
dwXCountChars: 0,
|
|
|
|
dwYCountCharts: 0,
|
|
|
|
dwFillAttribute: 0,
|
|
|
|
dwFlags: 0,
|
|
|
|
wShowWindow: 0,
|
|
|
|
cbReserved2: 0,
|
|
|
|
lpReserved2: ptr::mut_null(),
|
|
|
|
hStdInput: ptr::mut_null(),
|
|
|
|
hStdOutput: ptr::mut_null(),
|
|
|
|
hStdError: ptr::mut_null()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn zeroed_process_information() -> libc::types::os::arch::extra::PROCESS_INFORMATION {
|
|
|
|
libc::types::os::arch::extra::PROCESS_INFORMATION {
|
|
|
|
hProcess: ptr::mut_null(),
|
|
|
|
hThread: ptr::mut_null(),
|
|
|
|
dwProcessId: 0,
|
|
|
|
dwThreadId: 0
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-30 17:00:07 -05:00
|
|
|
// FIXME: this is only pub so it can be tested (see issue #4536)
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn make_command_line(prog: &str, args: &[~str]) -> ~str {
|
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use uint;
|
|
|
|
|
2013-04-30 17:00:07 -05:00
|
|
|
let mut cmd = ~"";
|
|
|
|
append_arg(&mut cmd, prog);
|
2013-06-21 07:29:53 -05:00
|
|
|
for args.iter().advance |arg| {
|
2013-04-30 17:00:07 -05:00
|
|
|
cmd.push_char(' ');
|
|
|
|
append_arg(&mut cmd, *arg);
|
|
|
|
}
|
|
|
|
return cmd;
|
|
|
|
|
|
|
|
fn append_arg(cmd: &mut ~str, arg: &str) {
|
2013-07-04 21:13:26 -05:00
|
|
|
let quote = arg.iter().any(|c| c == ' ' || c == '\t');
|
2013-04-30 17:00:07 -05:00
|
|
|
if quote {
|
|
|
|
cmd.push_char('"');
|
|
|
|
}
|
|
|
|
for uint::range(0, arg.len()) |i| {
|
|
|
|
append_char_at(cmd, arg, i);
|
|
|
|
}
|
|
|
|
if quote {
|
|
|
|
cmd.push_char('"');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn append_char_at(cmd: &mut ~str, arg: &str, i: uint) {
|
|
|
|
match arg[i] as char {
|
|
|
|
'"' => {
|
|
|
|
// Escape quotes.
|
|
|
|
cmd.push_str("\\\"");
|
|
|
|
}
|
|
|
|
'\\' => {
|
|
|
|
if backslash_run_ends_in_quote(arg, i) {
|
|
|
|
// Double all backslashes that are in runs before quotes.
|
|
|
|
cmd.push_str("\\\\");
|
|
|
|
} else {
|
|
|
|
// Pass other backslashes through unescaped.
|
|
|
|
cmd.push_char('\\');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c => {
|
|
|
|
cmd.push_char(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn backslash_run_ends_in_quote(s: &str, mut i: uint) -> bool {
|
|
|
|
while i < s.len() && s[i] as char == '\\' {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
return i < s.len() && s[i] as char == '"';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn spawn_process_os(prog: &str, args: &[~str],
|
|
|
|
env: Option<&[(~str, ~str)]>,
|
|
|
|
dir: Option<&Path>,
|
|
|
|
in_fd: c_int, out_fd: c_int, err_fd: c_int) -> SpawnProcessResult {
|
2013-04-30 17:00:07 -05:00
|
|
|
|
|
|
|
use libc::funcs::posix88::unistd::{fork, dup2, close, chdir, execvp};
|
|
|
|
use libc::funcs::bsd44::getdtablesize;
|
2013-06-25 19:02:03 -05:00
|
|
|
use int;
|
2013-04-30 17:00:07 -05:00
|
|
|
|
|
|
|
mod rustrt {
|
|
|
|
use libc::c_void;
|
|
|
|
|
|
|
|
#[abi = "cdecl"]
|
2013-07-18 21:08:57 -05:00
|
|
|
extern {
|
|
|
|
pub unsafe fn rust_unset_sigprocmask();
|
|
|
|
pub unsafe fn rust_set_environ(envp: *c_void);
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-01 16:20:26 -05:00
|
|
|
|
2013-04-30 17:00:07 -05:00
|
|
|
unsafe {
|
|
|
|
|
|
|
|
let pid = fork();
|
|
|
|
if pid < 0 {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in fork: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
} else if pid > 0 {
|
2013-05-12 07:58:00 -05:00
|
|
|
return SpawnProcessResult {pid: pid, handle: ptr::null()};
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rustrt::rust_unset_sigprocmask();
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
if dup2(in_fd, 0) == -1 {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in dup2(in_fd, 0): %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
if dup2(out_fd, 1) == -1 {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in dup2(out_fd, 1): %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
if dup2(err_fd, 2) == -1 {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in dup3(err_fd, 2): %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
// close all other fds
|
2013-07-01 05:30:14 -05:00
|
|
|
for int::range_rev(getdtablesize() as int, 3) |fd| {
|
2013-04-30 17:00:07 -05:00
|
|
|
close(fd as c_int);
|
|
|
|
}
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
do with_dirp(dir) |dirp| {
|
|
|
|
if !dirp.is_null() && chdir(dirp) == -1 {
|
|
|
|
fail!("failure in chdir: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do with_envp(env) |envp| {
|
|
|
|
if !envp.is_null() {
|
|
|
|
rustrt::rust_set_environ(envp);
|
|
|
|
}
|
|
|
|
do with_argv(prog, args) |argv| {
|
|
|
|
execvp(*argv, argv);
|
|
|
|
// execvp only returns if an error occurred
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in execvp: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
2012-08-14 14:11:15 -05:00
|
|
|
fn with_argv<T>(prog: &str, args: &[~str],
|
2013-03-07 16:38:38 -06:00
|
|
|
cb: &fn(**libc::c_char) -> T) -> T {
|
2013-07-22 23:41:46 -05:00
|
|
|
let mut argptrs = ~[prog.as_c_str(|b| b)];
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut tmps = ~[];
|
2013-06-21 07:29:53 -05:00
|
|
|
for args.iter().advance |arg| {
|
2013-07-02 14:47:32 -05:00
|
|
|
let t = @(*arg).clone();
|
2012-09-26 19:33:34 -05:00
|
|
|
tmps.push(t);
|
2013-07-22 23:41:46 -05:00
|
|
|
argptrs.push(t.as_c_str(|b| b));
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
2012-09-26 19:33:34 -05:00
|
|
|
argptrs.push(ptr::null());
|
2013-07-03 01:34:17 -05:00
|
|
|
argptrs.as_imm_buf(|buf, _len| cb(buf))
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn with_envp<T>(env: Option<&[(~str, ~str)]>, 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.
|
2013-05-12 07:58:00 -05:00
|
|
|
match env {
|
|
|
|
Some(es) => {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut tmps = ~[];
|
|
|
|
let mut ptrs = ~[];
|
2012-02-07 20:55:02 -06:00
|
|
|
|
2013-06-20 14:14:30 -05:00
|
|
|
for es.iter().advance |pair| {
|
|
|
|
// Use of match here is just to workaround limitations
|
|
|
|
// in the stage0 irrefutable pattern impl.
|
|
|
|
match pair {
|
|
|
|
&(ref k, ref v) => {
|
|
|
|
let kv = @fmt!("%s=%s", *k, *v);
|
|
|
|
tmps.push(kv);
|
2013-07-22 23:41:46 -05:00
|
|
|
ptrs.push(kv.as_c_str(|b| b));
|
2013-06-20 14:14:30 -05:00
|
|
|
}
|
|
|
|
}
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
|
2012-09-26 19:33:34 -05:00
|
|
|
ptrs.push(ptr::null());
|
2013-07-03 01:34:17 -05:00
|
|
|
ptrs.as_imm_buf(|p, _len|
|
2013-04-20 09:27:16 -05:00
|
|
|
unsafe { cb(::cast::transmute(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)]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn with_envp<T>(env: Option<&[(~str, ~str)]>, cb: &fn(*mut 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.
|
2013-05-12 07:58:00 -05:00
|
|
|
match env {
|
|
|
|
Some(es) => {
|
|
|
|
let mut blk = ~[];
|
2013-07-01 13:45:24 -05:00
|
|
|
for es.iter().advance |pair| {
|
|
|
|
let kv = fmt!("%s=%s", pair.first(), pair.second());
|
2013-07-24 14:33:49 -05:00
|
|
|
blk.push_all(kv.to_bytes_with_null());
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
blk.push(0);
|
2013-07-03 01:34:17 -05:00
|
|
|
blk.as_imm_buf(|p, _len|
|
2013-05-12 07:58:00 -05:00
|
|
|
unsafe { cb(::cast::transmute(p)) }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => cb(ptr::mut_null())
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
fn with_dirp<T>(d: Option<&Path>,
|
2013-03-07 16:38:38 -06:00
|
|
|
cb: &fn(*libc::c_char) -> T) -> T {
|
2013-05-12 07:58:00 -05:00
|
|
|
match d {
|
2013-07-22 23:41:46 -05:00
|
|
|
Some(dir) => dir.to_str().as_c_str(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
|
|
|
}
|
|
|
|
|
2013-04-22 14:05:07 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
priv fn free_handle(handle: *()) {
|
|
|
|
unsafe {
|
|
|
|
libc::funcs::extra::kernel32::CloseHandle(cast::transmute(handle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
priv fn free_handle(_handle: *()) {
|
|
|
|
// unix has no process handle object, just a pid
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2013-05-12 07:58:00 -05:00
|
|
|
* Spawns a process and waits for it to terminate. The process will
|
|
|
|
* inherit the current stdin/stdout/stderr file descriptors.
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * prog - The path to an executable
|
|
|
|
* * args - Vector of arguments to pass to the child process
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
2013-03-25 13:12:14 -05:00
|
|
|
* The process's exit code
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2013-05-12 07:58:00 -05: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)
|
2012-10-18 08:11:03 -05:00
|
|
|
});
|
2013-05-12 07:58:00 -05:00
|
|
|
prog.finish()
|
2011-07-28 23:15:56 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2013-05-12 07:58:00 -05:00
|
|
|
* Spawns a process, records all its output, and waits for it to terminate.
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * prog - The path to an executable
|
|
|
|
* * args - Vector of arguments to pass to the child process
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
2013-05-12 07:58:00 -05:00
|
|
|
* The process's stdout/stderr output and exit code.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2013-05-12 07:58:00 -05:00
|
|
|
pub fn process_output(prog: &str, args: &[~str]) -> ProcessOutput {
|
|
|
|
let mut prog = Process::new(prog, args, ProcessOptions::new());
|
|
|
|
prog.finish_with_output()
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-09-08 14:03:07 -05:00
|
|
|
|
2013-04-22 15:58:19 -05:00
|
|
|
/**
|
|
|
|
* Waits for a process to exit and returns the exit code, failing
|
|
|
|
* if there is no process with the specified id.
|
2013-05-12 07:58:00 -05:00
|
|
|
*
|
|
|
|
* Note that this is private to avoid race conditions on unix where if
|
|
|
|
* a user calls waitpid(some_process.get_id()) then some_process.finish()
|
2013-05-14 16:47:30 -05:00
|
|
|
* and some_process.destroy() and some_process.finalize() will then either
|
2013-05-12 07:58:00 -05:00
|
|
|
* operate on a none-existant process or, even worse, on a newer process
|
|
|
|
* with the same id.
|
2013-04-22 15:58:19 -05:00
|
|
|
*/
|
2013-05-12 07:58:00 -05:00
|
|
|
priv 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 {
|
2013-04-30 17:00:07 -05:00
|
|
|
|
|
|
|
use libc::types::os::arch::extra::DWORD;
|
|
|
|
use libc::consts::os::extra::{
|
|
|
|
SYNCHRONIZE,
|
|
|
|
PROCESS_QUERY_INFORMATION,
|
|
|
|
FALSE,
|
|
|
|
STILL_ACTIVE,
|
|
|
|
INFINITE,
|
|
|
|
WAIT_FAILED
|
|
|
|
};
|
|
|
|
use libc::funcs::extra::kernel32::{
|
|
|
|
OpenProcess,
|
|
|
|
GetExitCodeProcess,
|
|
|
|
CloseHandle,
|
|
|
|
WaitForSingleObject
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
|
|
|
let proc = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, pid as DWORD);
|
|
|
|
if proc.is_null() {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in OpenProcess: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let mut status = 0;
|
|
|
|
if GetExitCodeProcess(proc, &mut status) == FALSE {
|
|
|
|
CloseHandle(proc);
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in GetExitCodeProcess: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
if status != STILL_ACTIVE {
|
|
|
|
CloseHandle(proc);
|
|
|
|
return status as int;
|
|
|
|
}
|
|
|
|
if WaitForSingleObject(proc, INFINITE) == WAIT_FAILED {
|
|
|
|
CloseHandle(proc);
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in WaitForSingleObject: %s", os::last_os_error());
|
2013-04-30 17:00:07 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-22 15:58:19 -05:00
|
|
|
}
|
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 {
|
2013-04-22 15:58:19 -05:00
|
|
|
|
|
|
|
use libc::funcs::posix01::wait::*;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-04-22 15:58:19 -05:00
|
|
|
let mut status = 0 as c_int;
|
|
|
|
if unsafe { waitpid(pid, &mut status, 0) } == -1 {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("failure in waitpid: %s", os::last_os_error());
|
2013-04-22 15:58:19 -05:00
|
|
|
}
|
|
|
|
|
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-05-12 07:58:00 -05:00
|
|
|
use io;
|
2013-06-18 21:52:05 -05:00
|
|
|
use libc::{c_int, uintptr_t};
|
2013-05-12 07:58:00 -05:00
|
|
|
use option::{Option, None, Some};
|
2012-12-27 19:53:04 -06:00
|
|
|
use os;
|
2013-05-12 07:58:00 -05:00
|
|
|
use path::Path;
|
2012-12-27 19:53:04 -06:00
|
|
|
use run;
|
2013-05-12 07:58:00 -05:00
|
|
|
use str;
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2013-04-30 17:00:07 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn test_make_command_line() {
|
|
|
|
assert_eq!(
|
|
|
|
run::make_command_line("prog", [~"aaa", ~"bbb", ~"ccc"]),
|
|
|
|
~"prog aaa bbb ccc"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
run::make_command_line("C:\\Program Files\\blah\\blah.exe", [~"aaa"]),
|
|
|
|
~"\"C:\\Program Files\\blah\\blah.exe\" aaa"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
run::make_command_line("C:\\Program Files\\test", [~"aa\"bb"]),
|
|
|
|
~"\"C:\\Program Files\\test\" aa\\\"bb"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
run::make_command_line("echo", [~"a b c"]),
|
|
|
|
~"echo \"a b c\""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-04-16 15:23:44 -05:00
|
|
|
#[test]
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn test_process_status() {
|
|
|
|
assert_eq!(run::process_status("false", []), 1);
|
|
|
|
assert_eq!(run::process_status("true", []), 0);
|
|
|
|
}
|
2013-06-28 05:02:39 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
fn test_process_status() {
|
|
|
|
assert_eq!(run::process_status("/system/bin/sh", [~"-c",~"false"]), 1);
|
|
|
|
assert_eq!(run::process_status("/system/bin/sh", [~"-c",~"true"]), 0);
|
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn test_process_output_output() {
|
|
|
|
|
|
|
|
let run::ProcessOutput {status, output, error}
|
|
|
|
= run::process_output("echo", [~"hello"]);
|
|
|
|
let output_str = str::from_bytes(output);
|
|
|
|
|
|
|
|
assert_eq!(status, 0);
|
|
|
|
assert_eq!(output_str.trim().to_owned(), ~"hello");
|
2013-06-18 21:52:05 -05:00
|
|
|
// FIXME #7224
|
|
|
|
if !running_on_valgrind() {
|
|
|
|
assert_eq!(error, ~[]);
|
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
}
|
2013-06-28 05:02:39 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
fn test_process_output_output() {
|
|
|
|
|
|
|
|
let run::ProcessOutput {status, output, error}
|
|
|
|
= run::process_output("/system/bin/sh", [~"-c",~"echo hello"]);
|
|
|
|
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 07:58:00 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn test_process_output_error() {
|
|
|
|
|
|
|
|
let run::ProcessOutput {status, output, error}
|
|
|
|
= run::process_output("mkdir", [~"."]);
|
|
|
|
|
|
|
|
assert_eq!(status, 1);
|
|
|
|
assert_eq!(output, ~[]);
|
|
|
|
assert!(!error.is_empty());
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2013-06-28 05:02:39 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
fn test_process_output_error() {
|
|
|
|
|
|
|
|
let run::ProcessOutput {status, output, error}
|
|
|
|
= run::process_output("/system/bin/mkdir", [~"."]);
|
|
|
|
|
|
|
|
assert_eq!(status, 255);
|
|
|
|
assert_eq!(output, ~[]);
|
|
|
|
assert!(!error.is_empty());
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_pipes() {
|
2013-05-12 07:58:00 -05:00
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
let pipe_in = os::pipe();
|
|
|
|
let pipe_out = os::pipe();
|
|
|
|
let pipe_err = os::pipe();
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
let mut proc = run::Process::new("cat", [], run::ProcessOptions {
|
|
|
|
dir: None,
|
|
|
|
env: None,
|
|
|
|
in_fd: Some(pipe_in.in),
|
|
|
|
out_fd: Some(pipe_out.out),
|
|
|
|
err_fd: Some(pipe_err.out)
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(proc.input_redirected());
|
|
|
|
assert!(proc.output_redirected());
|
|
|
|
assert!(proc.error_redirected());
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
os::close(pipe_in.in);
|
|
|
|
os::close(pipe_out.out);
|
|
|
|
os::close(pipe_err.out);
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
let expected = ~"test";
|
2013-05-12 07:58:00 -05:00
|
|
|
writeclose(pipe_in.out, expected);
|
2012-01-17 21:05:07 -06:00
|
|
|
let actual = readclose(pipe_out.in);
|
|
|
|
readclose(pipe_err.in);
|
2013-05-12 07:58:00 -05:00
|
|
|
proc.finish();
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(expected, actual);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-05-12 07:58:00 -05:00
|
|
|
fn writeclose(fd: c_int, s: &str) {
|
|
|
|
let writer = io::fd_writer(fd, false);
|
|
|
|
writer.write_str(s);
|
|
|
|
os::close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn readclose(fd: c_int) -> ~str {
|
|
|
|
unsafe {
|
|
|
|
let file = os::fdopen(fd);
|
|
|
|
let reader = io::FILE_reader(file, false);
|
|
|
|
let buf = reader.read_whole_stream();
|
|
|
|
os::fclose(file);
|
|
|
|
str::from_bytes(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn test_finish_once() {
|
|
|
|
let mut prog = run::Process::new("false", [], run::ProcessOptions::new());
|
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
}
|
2013-06-28 05:02:39 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
fn test_finish_once() {
|
|
|
|
let mut prog = run::Process::new("/system/bin/sh", [~"-c",~"false"],
|
|
|
|
run::ProcessOptions::new());
|
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn test_finish_twice() {
|
|
|
|
let mut prog = run::Process::new("false", [], run::ProcessOptions::new());
|
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
}
|
2013-06-28 05:02:39 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
fn test_finish_twice() {
|
|
|
|
let mut prog = run::Process::new("/system/bin/sh", [~"-c",~"false"],
|
|
|
|
run::ProcessOptions::new());
|
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
assert_eq!(prog.finish(), 1);
|
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn test_finish_with_output_once() {
|
|
|
|
|
|
|
|
let mut prog = run::Process::new("echo", [~"hello"], run::ProcessOptions::new());
|
|
|
|
let run::ProcessOutput {status, output, error}
|
|
|
|
= 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 21:52:05 -05:00
|
|
|
// FIXME #7224
|
|
|
|
if !running_on_valgrind() {
|
|
|
|
assert_eq!(error, ~[]);
|
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
}
|
2013-06-28 05:02:39 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
fn test_finish_with_output_once() {
|
|
|
|
|
|
|
|
let mut prog = run::Process::new("/system/bin/sh", [~"-c",~"echo hello"],
|
|
|
|
run::ProcessOptions::new());
|
|
|
|
let run::ProcessOutput {status, output, error}
|
|
|
|
= prog.finish_with_output();
|
|
|
|
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 07:58:00 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn test_finish_with_output_twice() {
|
|
|
|
|
|
|
|
let mut prog = run::Process::new("echo", [~"hello"], run::ProcessOptions::new());
|
|
|
|
let run::ProcessOutput {status, output, error}
|
|
|
|
= 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 21:52:05 -05:00
|
|
|
// FIXME #7224
|
|
|
|
if !running_on_valgrind() {
|
|
|
|
assert_eq!(error, ~[]);
|
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
|
|
|
|
let run::ProcessOutput {status, output, error}
|
|
|
|
= prog.finish_with_output();
|
|
|
|
|
|
|
|
assert_eq!(status, 0);
|
|
|
|
assert_eq!(output, ~[]);
|
2013-06-18 21:52:05 -05:00
|
|
|
// FIXME #7224
|
|
|
|
if !running_on_valgrind() {
|
|
|
|
assert_eq!(error, ~[]);
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2013-06-28 05:02:39 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_os="android")]
|
|
|
|
fn test_finish_with_output_twice() {
|
|
|
|
|
|
|
|
let mut prog = run::Process::new("/system/bin/sh", [~"-c",~"echo hello"],
|
|
|
|
run::ProcessOptions::new());
|
|
|
|
let run::ProcessOutput {status, output, error}
|
|
|
|
= prog.finish_with_output();
|
|
|
|
|
|
|
|
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, ~[]);
|
|
|
|
}
|
|
|
|
|
|
|
|
let run::ProcessOutput {status, output, error}
|
|
|
|
= prog.finish_with_output();
|
|
|
|
|
|
|
|
assert_eq!(status, 0);
|
|
|
|
assert_eq!(output, ~[]);
|
|
|
|
// FIXME #7224
|
|
|
|
if !running_on_valgrind() {
|
|
|
|
assert_eq!(error, ~[]);
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2013-04-22 14:05:07 -05:00
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(not(windows),not(target_os="android"))]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn test_finish_with_output_redirected() {
|
|
|
|
let mut prog = run::Process::new("echo", [~"hello"], run::ProcessOptions {
|
|
|
|
env: None,
|
|
|
|
dir: None,
|
|
|
|
in_fd: Some(0),
|
|
|
|
out_fd: Some(1),
|
|
|
|
err_fd: Some(2)
|
|
|
|
});
|
|
|
|
// this should fail because it is not valid to read the output when it was redirected
|
|
|
|
prog.finish_with_output();
|
|
|
|
}
|
2013-06-28 05:02:39 -05:00
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
#[cfg(not(windows),target_os="android")]
|
|
|
|
fn test_finish_with_output_redirected() {
|
|
|
|
let mut prog = run::Process::new("/system/bin/sh", [~"-c",~"echo hello"],
|
|
|
|
run::ProcessOptions {
|
|
|
|
env: None,
|
|
|
|
dir: None,
|
|
|
|
in_fd: Some(0),
|
|
|
|
out_fd: Some(1),
|
|
|
|
err_fd: Some(2)
|
|
|
|
});
|
|
|
|
// this should fail because it is not valid to read the output when it was redirected
|
|
|
|
prog.finish_with_output();
|
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(unix,not(target_os="android"))]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn run_pwd(dir: Option<&Path>) -> run::Process {
|
|
|
|
run::Process::new("pwd", [], run::ProcessOptions {
|
|
|
|
dir: dir,
|
|
|
|
.. run::ProcessOptions::new()
|
|
|
|
})
|
|
|
|
}
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(unix,target_os="android")]
|
|
|
|
fn run_pwd(dir: Option<&Path>) -> run::Process {
|
|
|
|
run::Process::new("/system/bin/sh", [~"-c",~"pwd"], run::ProcessOptions {
|
|
|
|
dir: dir,
|
|
|
|
.. run::ProcessOptions::new()
|
|
|
|
})
|
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn run_pwd(dir: Option<&Path>) -> run::Process {
|
|
|
|
run::Process::new("cmd", [~"/c", ~"cd"], run::ProcessOptions {
|
|
|
|
dir: dir,
|
|
|
|
.. run::ProcessOptions::new()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-06-01 01:11:29 -05: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 07:58:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_change_working_directory() {
|
2013-05-14 14:10:14 -05: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-06-01 01:11:29 -05:00
|
|
|
let parent_dir = os::getcwd().dir_path().normalize();
|
|
|
|
let mut prog = run_pwd(Some(&parent_dir));
|
2013-05-12 07:58:00 -05:00
|
|
|
|
|
|
|
let output = str::from_bytes(prog.finish_with_output().output);
|
|
|
|
let child_dir = Path(output.trim()).normalize();
|
|
|
|
|
2013-06-01 01:11:29 -05: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 07:58:00 -05:00
|
|
|
}
|
|
|
|
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(unix,not(target_os="android"))]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn run_env(env: Option<&[(~str, ~str)]>) -> run::Process {
|
|
|
|
run::Process::new("env", [], run::ProcessOptions {
|
|
|
|
env: env,
|
|
|
|
.. run::ProcessOptions::new()
|
|
|
|
})
|
|
|
|
}
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(unix,target_os="android")]
|
|
|
|
fn run_env(env: Option<&[(~str, ~str)]>) -> run::Process {
|
|
|
|
run::Process::new("/system/bin/sh", [~"-c",~"set"], run::ProcessOptions {
|
|
|
|
env: env,
|
|
|
|
.. run::ProcessOptions::new()
|
|
|
|
})
|
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn run_env(env: Option<&[(~str, ~str)]>) -> run::Process {
|
|
|
|
run::Process::new("cmd", [~"/c", ~"set"], run::ProcessOptions {
|
|
|
|
env: env,
|
|
|
|
.. run::ProcessOptions::new()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-06-28 05:02:39 -05:00
|
|
|
#[cfg(not(target_os="android"))]
|
2013-05-12 07:58:00 -05:00
|
|
|
fn test_inherit_env() {
|
2013-06-20 05:43:21 -05:00
|
|
|
if running_on_valgrind() { return; }
|
2013-05-12 07:58:00 -05:00
|
|
|
|
|
|
|
let mut prog = run_env(None);
|
|
|
|
let output = str::from_bytes(prog.finish_with_output().output);
|
|
|
|
|
2013-06-21 07:29:53 -05:00
|
|
|
let r = os::env();
|
2013-06-20 14:14:30 -05:00
|
|
|
for r.iter().advance |&(ref k, ref v)| {
|
2013-05-12 07:58:00 -05:00
|
|
|
// don't check windows magical empty-named variables
|
2013-06-20 14:14:30 -05:00
|
|
|
assert!(k.is_empty() || output.contains(fmt!("%s=%s", *k, *v)));
|
2013-05-12 07:58:00 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-28 05:02:39 -05: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();
|
|
|
|
for r.iter().advance |&(k, v)| {
|
|
|
|
// don't check android RANDOM variables
|
|
|
|
if k != ~"RANDOM" {
|
|
|
|
assert!(output.contains(fmt!("%s=%s", k, v)) ||
|
|
|
|
output.contains(fmt!("%s=\'%s\'", k, v)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-12 07:58:00 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_to_env() {
|
|
|
|
|
|
|
|
let mut new_env = os::env();
|
|
|
|
new_env.push((~"RUN_TEST_NEW_ENV", ~"123"));
|
|
|
|
|
|
|
|
let mut prog = run_env(Some(new_env.slice(0, new_env.len())));
|
|
|
|
let output = str::from_bytes(prog.finish_with_output().output);
|
|
|
|
|
|
|
|
assert!(output.contains("RUN_TEST_NEW_ENV=123"));
|
2013-04-22 14:05:07 -05:00
|
|
|
}
|
2013-06-18 21:52:05 -05:00
|
|
|
|
|
|
|
fn running_on_valgrind() -> bool {
|
|
|
|
unsafe { rust_running_on_valgrind() != 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
extern {
|
|
|
|
fn rust_running_on_valgrind() -> uintptr_t;
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|