2013-09-16 15:28:56 -07:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! Bindings for executing child processes
|
|
|
|
|
2014-05-05 16:58:42 -07:00
|
|
|
#![allow(experimental)]
|
2014-10-27 15:37:07 -07:00
|
|
|
#![allow(non_upper_case_globals)]
|
2014-05-05 16:58:42 -07:00
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::StdioContainer::*;
|
|
|
|
pub use self::ProcessExit::*;
|
|
|
|
|
2013-09-16 15:28:56 -07:00
|
|
|
use prelude::*;
|
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
use fmt;
|
2014-07-02 13:50:45 -07:00
|
|
|
use os;
|
2014-06-03 20:09:39 -07:00
|
|
|
use io::{IoResult, IoError};
|
2014-02-18 12:04:51 -08:00
|
|
|
use io;
|
|
|
|
use libc;
|
2014-05-05 14:33:55 -07:00
|
|
|
use c_str::CString;
|
2014-07-02 13:50:45 -07:00
|
|
|
use collections::HashMap;
|
2014-09-17 23:40:27 -07:00
|
|
|
use hash::Hash;
|
|
|
|
#[cfg(windows)]
|
|
|
|
use std::hash::sip::SipState;
|
2014-10-09 16:27:28 -07:00
|
|
|
use io::pipe::{PipeStream, PipePair};
|
|
|
|
use path::BytesContainer;
|
|
|
|
|
|
|
|
use sys;
|
|
|
|
use sys::fs::FileDesc;
|
|
|
|
use sys::process::Process as ProcessImp;
|
2013-09-16 15:28:56 -07:00
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
/// Signal a process to exit, without forcibly killing it. Corresponds to
|
|
|
|
/// SIGTERM on unix platforms.
|
2014-10-06 16:29:47 -07:00
|
|
|
#[cfg(windows)] pub const PleaseExitSignal: int = 15;
|
2014-02-18 12:04:51 -08:00
|
|
|
/// Signal a process to exit immediately, forcibly killing it. Corresponds to
|
|
|
|
/// SIGKILL on unix platforms.
|
2014-10-06 16:29:47 -07:00
|
|
|
#[cfg(windows)] pub const MustDieSignal: int = 9;
|
2014-02-18 12:04:51 -08:00
|
|
|
/// Signal a process to exit, without forcibly killing it. Corresponds to
|
|
|
|
/// SIGTERM on unix platforms.
|
2014-10-06 16:29:47 -07:00
|
|
|
#[cfg(not(windows))] pub const PleaseExitSignal: int = libc::SIGTERM as int;
|
2014-02-18 12:04:51 -08:00
|
|
|
/// Signal a process to exit immediately, forcibly killing it. Corresponds to
|
|
|
|
/// SIGKILL on unix platforms.
|
2014-10-06 16:29:47 -07:00
|
|
|
#[cfg(not(windows))] pub const MustDieSignal: int = libc::SIGKILL as int;
|
2013-10-06 13:22:18 -07:00
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
/// Representation of a running or exited child process.
|
|
|
|
///
|
2014-05-05 14:33:55 -07:00
|
|
|
/// This structure is used to represent and manage child processes. A child
|
|
|
|
/// process is created via the `Command` struct, which configures the spawning
|
|
|
|
/// process and can itself be constructed using a builder-style interface.
|
2014-02-18 12:04:51 -08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```should_fail
|
2014-05-05 14:33:55 -07:00
|
|
|
/// use std::io::Command;
|
2014-02-18 12:04:51 -08:00
|
|
|
///
|
2014-05-05 14:33:55 -07:00
|
|
|
/// let mut child = match Command::new("/bin/cat").arg("file.txt").spawn() {
|
2014-02-18 12:04:51 -08:00
|
|
|
/// Ok(child) => child,
|
2014-10-09 15:17:22 -04:00
|
|
|
/// Err(e) => panic!("failed to execute child: {}", e),
|
2014-02-18 12:04:51 -08:00
|
|
|
/// };
|
|
|
|
///
|
2014-08-18 17:52:38 -07:00
|
|
|
/// let contents = child.stdout.as_mut().unwrap().read_to_end();
|
2014-05-05 16:58:42 -07:00
|
|
|
/// assert!(child.wait().unwrap().success());
|
2014-02-18 12:04:51 -08:00
|
|
|
/// ```
|
2013-09-16 15:28:56 -07:00
|
|
|
pub struct Process {
|
2014-10-09 16:27:28 -07:00
|
|
|
handle: ProcessImp,
|
2014-05-30 17:18:12 -07:00
|
|
|
forget: bool,
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-10-09 16:27:28 -07:00
|
|
|
/// None until wait() is called.
|
|
|
|
exit_code: Option<ProcessExit>,
|
|
|
|
|
|
|
|
/// Manually delivered signal
|
|
|
|
exit_signal: Option<int>,
|
|
|
|
|
|
|
|
/// Deadline after which wait() will return
|
|
|
|
deadline: u64,
|
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
/// Handle to the child's stdin, if the `stdin` field of this process's
|
|
|
|
/// `ProcessConfig` was `CreatePipe`. By default, this handle is `Some`.
|
2014-10-09 16:27:28 -07:00
|
|
|
pub stdin: Option<PipeStream>,
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
/// Handle to the child's stdout, if the `stdout` field of this process's
|
|
|
|
/// `ProcessConfig` was `CreatePipe`. By default, this handle is `Some`.
|
2014-10-09 16:27:28 -07:00
|
|
|
pub stdout: Option<PipeStream>,
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
/// Handle to the child's stderr, if the `stderr` field of this process's
|
|
|
|
/// `ProcessConfig` was `CreatePipe`. By default, this handle is `Some`.
|
2014-10-09 16:27:28 -07:00
|
|
|
pub stderr: Option<PipeStream>,
|
2013-09-16 15:28:56 -07:00
|
|
|
}
|
|
|
|
|
2014-09-14 01:33:21 -07:00
|
|
|
/// A representation of environment variable name
|
|
|
|
/// It compares case-insensitive on Windows and case-sensitive everywhere else.
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
#[deriving(PartialEq, Eq, Hash, Clone, Show)]
|
|
|
|
struct EnvKey(CString);
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(windows)]
|
|
|
|
#[deriving(Eq, Clone, Show)]
|
|
|
|
struct EnvKey(CString);
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl Hash for EnvKey {
|
|
|
|
fn hash(&self, state: &mut SipState) {
|
|
|
|
let &EnvKey(ref x) = self;
|
|
|
|
match x.as_str() {
|
|
|
|
Some(s) => for ch in s.chars() {
|
|
|
|
(ch as u8 as char).to_lowercase().hash(state);
|
|
|
|
},
|
|
|
|
None => x.hash(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl PartialEq for EnvKey {
|
|
|
|
fn eq(&self, other: &EnvKey) -> bool {
|
|
|
|
let &EnvKey(ref x) = self;
|
|
|
|
let &EnvKey(ref y) = other;
|
|
|
|
match (x.as_str(), y.as_str()) {
|
|
|
|
(Some(xs), Some(ys)) => {
|
|
|
|
if xs.len() != ys.len() {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
for (xch, ych) in xs.chars().zip(ys.chars()) {
|
|
|
|
if xch.to_lowercase() != ych.to_lowercase() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// If either is not a valid utf8 string, just compare them byte-wise
|
|
|
|
_ => return x.eq(y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 16:27:28 -07:00
|
|
|
impl BytesContainer for EnvKey {
|
|
|
|
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
|
|
|
|
let &EnvKey(ref k) = self;
|
|
|
|
k.container_as_bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:50:45 -07:00
|
|
|
/// A HashMap representation of environment variables.
|
2014-09-14 01:33:21 -07:00
|
|
|
pub type EnvMap = HashMap<EnvKey, CString>;
|
2014-07-02 13:50:45 -07:00
|
|
|
|
2014-05-05 14:33:55 -07:00
|
|
|
/// The `Command` type acts as a process builder, providing fine-grained control
|
|
|
|
/// over how a new process should be spawned. A default configuration can be
|
|
|
|
/// generated using `Command::new(program)`, where `program` gives a path to the
|
|
|
|
/// program to be executed. Additional builder methods allow the configuration
|
|
|
|
/// to be changed (for example, by adding arguments) prior to spawning:
|
2014-02-18 12:04:51 -08:00
|
|
|
///
|
|
|
|
/// ```
|
2014-05-05 14:33:55 -07:00
|
|
|
/// use std::io::Command;
|
2014-02-18 12:04:51 -08:00
|
|
|
///
|
2014-05-05 14:33:55 -07:00
|
|
|
/// let mut process = match Command::new("sh").arg("-c").arg("echo hello").spawn() {
|
|
|
|
/// Ok(p) => p,
|
2014-10-09 15:17:22 -04:00
|
|
|
/// Err(e) => panic!("failed to execute process: {}", e),
|
2014-02-18 12:04:51 -08:00
|
|
|
/// };
|
2014-05-05 14:33:55 -07:00
|
|
|
///
|
2014-08-18 17:52:38 -07:00
|
|
|
/// let output = process.stdout.as_mut().unwrap().read_to_end();
|
2014-02-18 12:04:51 -08:00
|
|
|
/// ```
|
2014-07-09 20:00:44 -05:00
|
|
|
#[deriving(Clone)]
|
2014-05-05 14:33:55 -07:00
|
|
|
pub struct Command {
|
|
|
|
// The internal data for the builder. Documented by the builder
|
|
|
|
// methods below, and serialized into rt::rtio::ProcessConfig.
|
|
|
|
program: CString,
|
|
|
|
args: Vec<CString>,
|
2014-07-02 13:50:45 -07:00
|
|
|
env: Option<EnvMap>,
|
2014-05-05 14:33:55 -07:00
|
|
|
cwd: Option<CString>,
|
|
|
|
stdin: StdioContainer,
|
|
|
|
stdout: StdioContainer,
|
|
|
|
stderr: StdioContainer,
|
|
|
|
uid: Option<uint>,
|
|
|
|
gid: Option<uint>,
|
|
|
|
detach: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
|
|
|
|
// we cannot usefully take ToCStr arguments by reference (without forcing an
|
|
|
|
// additional & around &str). So we are instead temporarily adding an instance
|
|
|
|
// for &Path, so that we can take ToCStr as owned. When DST lands, the &Path
|
|
|
|
// instance should be removed, and arguments bound by ToCStr should be passed by
|
|
|
|
// reference. (Here: {new, arg, args, env}.)
|
|
|
|
|
|
|
|
impl Command {
|
|
|
|
/// Constructs a new `Command` for launching the program at
|
|
|
|
/// path `program`, with the following default configuration:
|
|
|
|
///
|
|
|
|
/// * No arguments to the program
|
|
|
|
/// * Inherit the current process's environment
|
|
|
|
/// * Inherit the current process's working directory
|
|
|
|
/// * A readable pipe for stdin (file descriptor 0)
|
2014-05-22 22:50:31 +10:00
|
|
|
/// * A writeable pipe for stdout and stderr (file descriptors 1 and 2)
|
2014-05-05 14:33:55 -07:00
|
|
|
///
|
|
|
|
/// Builder methods are provided to change these defaults and
|
|
|
|
/// otherwise configure the process.
|
|
|
|
pub fn new<T:ToCStr>(program: T) -> Command {
|
|
|
|
Command {
|
|
|
|
program: program.to_c_str(),
|
|
|
|
args: Vec::new(),
|
|
|
|
env: None,
|
|
|
|
cwd: None,
|
|
|
|
stdin: CreatePipe(true, false),
|
|
|
|
stdout: CreatePipe(false, true),
|
|
|
|
stderr: CreatePipe(false, true),
|
|
|
|
uid: None,
|
|
|
|
gid: None,
|
|
|
|
detach: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add an argument to pass to the program.
|
2014-07-02 13:50:45 -07:00
|
|
|
pub fn arg<'a, T: ToCStr>(&'a mut self, arg: T) -> &'a mut Command {
|
2014-05-05 14:33:55 -07:00
|
|
|
self.args.push(arg.to_c_str());
|
|
|
|
self
|
|
|
|
}
|
2013-09-16 15:28:56 -07:00
|
|
|
|
2014-05-05 14:33:55 -07:00
|
|
|
/// Add multiple arguments to pass to the program.
|
2014-07-02 13:50:45 -07:00
|
|
|
pub fn args<'a, T: ToCStr>(&'a mut self, args: &[T]) -> &'a mut Command {
|
2014-05-05 14:33:55 -07:00
|
|
|
self.args.extend(args.iter().map(|arg| arg.to_c_str()));;
|
|
|
|
self
|
|
|
|
}
|
2014-07-02 13:50:45 -07:00
|
|
|
// Get a mutable borrow of the environment variable map for this `Command`.
|
2014-09-14 01:33:21 -07:00
|
|
|
fn get_env_map<'a>(&'a mut self) -> &'a mut EnvMap {
|
2014-07-02 13:50:45 -07:00
|
|
|
match self.env {
|
|
|
|
Some(ref mut map) => map,
|
|
|
|
None => {
|
|
|
|
// if the env is currently just inheriting from the parent's,
|
|
|
|
// materialize the parent's env into a hashtable.
|
2014-09-14 20:27:36 -07:00
|
|
|
self.env = Some(os::env_as_bytes().into_iter()
|
2014-11-27 14:43:55 -05:00
|
|
|
.map(|(k, v)| (EnvKey(k.to_c_str()),
|
|
|
|
v.to_c_str()))
|
2014-07-02 13:50:45 -07:00
|
|
|
.collect());
|
|
|
|
self.env.as_mut().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 15:28:56 -07:00
|
|
|
|
2014-07-02 13:50:45 -07:00
|
|
|
/// Inserts or updates an environment variable mapping.
|
2014-09-14 01:33:21 -07:00
|
|
|
///
|
|
|
|
/// Note that environment variable names are case-insensitive (but case-preserving) on Windows,
|
|
|
|
/// and case-sensitive on all other platforms.
|
2014-07-02 13:50:45 -07:00
|
|
|
pub fn env<'a, T: ToCStr, U: ToCStr>(&'a mut self, key: T, val: U)
|
|
|
|
-> &'a mut Command {
|
2014-09-14 01:33:21 -07:00
|
|
|
self.get_env_map().insert(EnvKey(key.to_c_str()), val.to_c_str());
|
2014-07-02 13:50:45 -07:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes an environment variable mapping.
|
|
|
|
pub fn env_remove<'a, T: ToCStr>(&'a mut self, key: T) -> &'a mut Command {
|
2014-09-14 01:33:21 -07:00
|
|
|
self.get_env_map().remove(&EnvKey(key.to_c_str()));
|
2014-07-02 13:50:45 -07:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the entire environment map for the child process.
|
|
|
|
///
|
|
|
|
/// If the given slice contains multiple instances of an environment
|
|
|
|
/// variable, the *rightmost* instance will determine the value.
|
|
|
|
pub fn env_set_all<'a, T: ToCStr, U: ToCStr>(&'a mut self, env: &[(T,U)])
|
|
|
|
-> &'a mut Command {
|
2014-09-14 01:33:21 -07:00
|
|
|
self.env = Some(env.iter().map(|&(ref k, ref v)| (EnvKey(k.to_c_str()), v.to_c_str()))
|
2014-07-02 13:50:45 -07:00
|
|
|
.collect());
|
2014-05-05 14:33:55 -07:00
|
|
|
self
|
|
|
|
}
|
2013-09-16 15:28:56 -07:00
|
|
|
|
2014-05-05 14:33:55 -07:00
|
|
|
/// Set the working directory for the child process.
|
|
|
|
pub fn cwd<'a>(&'a mut self, dir: &Path) -> &'a mut Command {
|
|
|
|
self.cwd = Some(dir.to_c_str());
|
|
|
|
self
|
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
/// Configuration for the child process's stdin handle (file descriptor 0).
|
2014-05-05 14:33:55 -07:00
|
|
|
/// Defaults to `CreatePipe(true, false)` so the input can be written to.
|
|
|
|
pub fn stdin<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command {
|
|
|
|
self.stdin = cfg;
|
|
|
|
self
|
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
/// Configuration for the child process's stdout handle (file descriptor 1).
|
2014-05-05 14:33:55 -07:00
|
|
|
/// Defaults to `CreatePipe(false, true)` so the output can be collected.
|
|
|
|
pub fn stdout<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command {
|
|
|
|
self.stdout = cfg;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configuration for the child process's stderr handle (file descriptor 2).
|
|
|
|
/// Defaults to `CreatePipe(false, true)` so the output can be collected.
|
|
|
|
pub fn stderr<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command {
|
|
|
|
self.stderr = cfg;
|
|
|
|
self
|
|
|
|
}
|
2014-02-06 22:38:05 -08:00
|
|
|
|
|
|
|
/// Sets the child process's user id. This translates to a `setuid` call in
|
|
|
|
/// the child process. Setting this value on windows will cause the spawn to
|
|
|
|
/// fail. Failure in the `setuid` call on unix will also cause the spawn to
|
|
|
|
/// fail.
|
2014-05-05 14:33:55 -07:00
|
|
|
pub fn uid<'a>(&'a mut self, id: uint) -> &'a mut Command {
|
|
|
|
self.uid = Some(id);
|
|
|
|
self
|
|
|
|
}
|
2014-02-06 22:38:05 -08:00
|
|
|
|
|
|
|
/// Similar to `uid`, but sets the group id of the child process. This has
|
|
|
|
/// the same semantics as the `uid` field.
|
2014-05-05 14:33:55 -07:00
|
|
|
pub fn gid<'a>(&'a mut self, id: uint) -> &'a mut Command {
|
|
|
|
self.gid = Some(id);
|
|
|
|
self
|
|
|
|
}
|
2014-02-06 22:38:05 -08:00
|
|
|
|
2014-05-05 14:33:55 -07:00
|
|
|
/// Sets the child process to be spawned in a detached state. On unix, this
|
2014-02-06 22:38:05 -08:00
|
|
|
/// means that the child is the leader of a new process group.
|
2014-05-05 14:33:55 -07:00
|
|
|
pub fn detached<'a>(&'a mut self) -> &'a mut Command {
|
|
|
|
self.detach = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes the command as a child process, which is returned.
|
|
|
|
pub fn spawn(&self) -> IoResult<Process> {
|
2014-10-09 16:27:28 -07:00
|
|
|
let (their_stdin, our_stdin) = try!(setup_io(self.stdin));
|
|
|
|
let (their_stdout, our_stdout) = try!(setup_io(self.stdout));
|
|
|
|
let (their_stderr, our_stderr) = try!(setup_io(self.stderr));
|
|
|
|
|
|
|
|
match ProcessImp::spawn(self, their_stdin, their_stdout, their_stderr) {
|
|
|
|
Err(e) => Err(e),
|
|
|
|
Ok(handle) => Ok(Process {
|
|
|
|
handle: handle,
|
|
|
|
forget: false,
|
|
|
|
exit_code: None,
|
|
|
|
exit_signal: None,
|
|
|
|
deadline: 0,
|
|
|
|
stdin: our_stdin,
|
|
|
|
stdout: our_stdout,
|
|
|
|
stderr: our_stderr,
|
2014-05-05 14:33:55 -07:00
|
|
|
})
|
2014-10-09 16:27:28 -07:00
|
|
|
}
|
2014-05-05 14:33:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes the command as a child process, waiting for it to finish and
|
|
|
|
/// collecting all of its output.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::Command;
|
|
|
|
///
|
|
|
|
/// let output = match Command::new("cat").arg("foot.txt").output() {
|
|
|
|
/// Ok(output) => output,
|
2014-10-09 15:17:22 -04:00
|
|
|
/// Err(e) => panic!("failed to execute process: {}", e),
|
2014-05-05 14:33:55 -07:00
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// println!("status: {}", output.status);
|
2014-07-10 18:21:16 +02:00
|
|
|
/// println!("stdout: {}", String::from_utf8_lossy(output.output.as_slice()));
|
|
|
|
/// println!("stderr: {}", String::from_utf8_lossy(output.error.as_slice()));
|
2014-05-05 14:33:55 -07:00
|
|
|
/// ```
|
|
|
|
pub fn output(&self) -> IoResult<ProcessOutput> {
|
|
|
|
self.spawn().and_then(|p| p.wait_with_output())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes a command as a child process, waiting for it to finish and
|
|
|
|
/// collecting its exit status.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::Command;
|
|
|
|
///
|
|
|
|
/// let status = match Command::new("ls").status() {
|
|
|
|
/// Ok(status) => status,
|
2014-10-09 15:17:22 -04:00
|
|
|
/// Err(e) => panic!("failed to execute process: {}", e),
|
2014-05-05 14:33:55 -07:00
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// println!("process exited with: {}", status);
|
|
|
|
/// ```
|
|
|
|
pub fn status(&self) -> IoResult<ProcessExit> {
|
|
|
|
self.spawn().and_then(|mut p| p.wait())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Show for Command {
|
|
|
|
/// Format the program and arguments of a Command for display. Any
|
|
|
|
/// non-utf8 data is lossily converted using the utf8 replacement
|
|
|
|
/// character.
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-07-10 18:21:16 +02:00
|
|
|
try!(write!(f, "{}", String::from_utf8_lossy(self.program.as_bytes_no_nul())));
|
2014-05-05 14:33:55 -07:00
|
|
|
for arg in self.args.iter() {
|
2014-07-10 18:21:16 +02:00
|
|
|
try!(write!(f, " '{}'", String::from_utf8_lossy(arg.as_bytes_no_nul())));
|
2014-05-05 14:33:55 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2013-09-16 15:28:56 -07:00
|
|
|
}
|
|
|
|
|
2014-10-09 16:27:28 -07:00
|
|
|
fn setup_io(io: StdioContainer) -> IoResult<(Option<PipeStream>, Option<PipeStream>)> {
|
|
|
|
let ours;
|
|
|
|
let theirs;
|
|
|
|
match io {
|
|
|
|
Ignored => {
|
|
|
|
theirs = None;
|
|
|
|
ours = None;
|
|
|
|
}
|
|
|
|
InheritFd(fd) => {
|
|
|
|
theirs = Some(PipeStream::from_filedesc(FileDesc::new(fd, false)));
|
|
|
|
ours = None;
|
|
|
|
}
|
|
|
|
CreatePipe(readable, _writable) => {
|
|
|
|
let PipePair { reader, writer } = try!(PipeStream::pair());
|
|
|
|
if readable {
|
|
|
|
theirs = Some(reader);
|
|
|
|
ours = Some(writer);
|
|
|
|
} else {
|
|
|
|
theirs = Some(writer);
|
|
|
|
ours = Some(reader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok((theirs, ours))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow the sys module to get access to the Command state
|
|
|
|
impl sys::process::ProcessConfig<EnvKey, CString> for Command {
|
|
|
|
fn program(&self) -> &CString {
|
|
|
|
&self.program
|
|
|
|
}
|
|
|
|
fn args(&self) -> &[CString] {
|
|
|
|
self.args.as_slice()
|
|
|
|
}
|
|
|
|
fn env(&self) -> Option<&EnvMap> {
|
|
|
|
self.env.as_ref()
|
|
|
|
}
|
|
|
|
fn cwd(&self) -> Option<&CString> {
|
|
|
|
self.cwd.as_ref()
|
|
|
|
}
|
|
|
|
fn uid(&self) -> Option<uint> {
|
|
|
|
self.uid.clone()
|
|
|
|
}
|
|
|
|
fn gid(&self) -> Option<uint> {
|
|
|
|
self.gid.clone()
|
|
|
|
}
|
|
|
|
fn detach(&self) -> bool {
|
|
|
|
self.detach
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
/// The output of a finished process.
|
2014-05-31 10:43:52 -07:00
|
|
|
#[deriving(PartialEq, Eq, Clone)]
|
2014-02-18 12:04:51 -08:00
|
|
|
pub struct ProcessOutput {
|
|
|
|
/// The status (exit code) of the process.
|
2014-03-27 15:09:47 -07:00
|
|
|
pub status: ProcessExit,
|
2014-02-18 12:04:51 -08:00
|
|
|
/// The data that the process wrote to stdout.
|
2014-03-26 09:24:16 -07:00
|
|
|
pub output: Vec<u8>,
|
2014-02-18 12:04:51 -08:00
|
|
|
/// The data that the process wrote to stderr.
|
2014-03-26 09:24:16 -07:00
|
|
|
pub error: Vec<u8>,
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
|
2013-09-16 15:28:56 -07:00
|
|
|
/// Describes what to do with a standard io stream for a child process.
|
2014-07-09 20:00:44 -05:00
|
|
|
#[deriving(Clone)]
|
2013-09-16 15:28:56 -07:00
|
|
|
pub enum StdioContainer {
|
|
|
|
/// This stream will be ignored. This is the equivalent of attaching the
|
|
|
|
/// stream to `/dev/null`
|
|
|
|
Ignored,
|
|
|
|
|
|
|
|
/// The specified file descriptor is inherited for the stream which it is
|
2014-08-10 14:10:34 -07:00
|
|
|
/// specified for. Ownership of the file descriptor is *not* taken, so the
|
|
|
|
/// caller must clean it up.
|
2013-09-16 15:28:56 -07:00
|
|
|
InheritFd(libc::c_int),
|
|
|
|
|
2013-10-16 11:39:51 -07:00
|
|
|
/// Creates a pipe for the specified file descriptor which will be created
|
|
|
|
/// when the process is spawned.
|
2013-09-16 15:28:56 -07:00
|
|
|
///
|
|
|
|
/// The first boolean argument is whether the pipe is readable, and the
|
|
|
|
/// second is whether it is writable. These properties are from the view of
|
|
|
|
/// the *child* process, not the parent process.
|
2013-10-16 11:39:51 -07:00
|
|
|
CreatePipe(bool /* readable */, bool /* writable */),
|
2013-09-16 15:28:56 -07:00
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
impl Copy for StdioContainer {}
|
|
|
|
|
2013-11-12 11:37:14 +10:00
|
|
|
/// Describes the result of a process after it has terminated.
|
2013-11-14 02:58:19 +09:00
|
|
|
/// Note that Windows have no signals, so the result is usually ExitStatus.
|
2014-05-31 10:43:52 -07:00
|
|
|
#[deriving(PartialEq, Eq, Clone)]
|
2013-11-12 11:37:14 +10:00
|
|
|
pub enum ProcessExit {
|
|
|
|
/// Normal termination with an exit status.
|
|
|
|
ExitStatus(int),
|
|
|
|
|
|
|
|
/// Termination by signal, with the signal number.
|
|
|
|
ExitSignal(int),
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
impl Copy for ProcessExit {}
|
|
|
|
|
2014-01-30 00:46:37 +11:00
|
|
|
impl fmt::Show for ProcessExit {
|
2013-11-12 11:37:14 +10:00
|
|
|
/// Format a ProcessExit enum, to nicely present the information.
|
2014-02-05 23:55:13 +11:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2014-05-10 14:05:06 -07:00
|
|
|
ExitStatus(code) => write!(f, "exit code: {}", code),
|
|
|
|
ExitSignal(code) => write!(f, "signal: {}", code),
|
2013-11-12 11:37:14 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcessExit {
|
|
|
|
/// Was termination successful? Signal termination not considered a success,
|
|
|
|
/// and success is defined as a zero exit status.
|
|
|
|
pub fn success(&self) -> bool {
|
|
|
|
return self.matches_exit_status(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks whether this ProcessExit matches the given exit status.
|
|
|
|
/// Termination by signal will never match an exit code.
|
|
|
|
pub fn matches_exit_status(&self, wanted: int) -> bool {
|
|
|
|
*self == ExitStatus(wanted)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 15:28:56 -07:00
|
|
|
impl Process {
|
2014-02-18 12:04:51 -08:00
|
|
|
/// Sends `signal` to another process in the system identified by `id`.
|
|
|
|
///
|
|
|
|
/// Note that windows doesn't quite have the same model as unix, so some
|
|
|
|
/// unix signals are mapped to windows signals. Notably, unix termination
|
|
|
|
/// signals (SIGTERM/SIGKILL/SIGINT) are translated to `TerminateProcess`.
|
|
|
|
///
|
|
|
|
/// Additionally, a signal number of 0 can check for existence of the target
|
2014-03-25 08:44:40 -07:00
|
|
|
/// process. Note, though, that on some platforms signals will continue to
|
|
|
|
/// be successfully delivered if the child has exited, but not yet been
|
|
|
|
/// reaped.
|
2014-02-18 12:04:51 -08:00
|
|
|
pub fn kill(id: libc::pid_t, signal: int) -> IoResult<()> {
|
2014-10-09 16:27:28 -07:00
|
|
|
unsafe { ProcessImp::killpid(id, signal) }
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
|
2013-09-16 15:28:56 -07:00
|
|
|
/// Returns the process id of this child process
|
|
|
|
pub fn id(&self) -> libc::pid_t { self.handle.id() }
|
|
|
|
|
|
|
|
/// Sends the specified signal to the child process, returning whether the
|
|
|
|
/// signal could be delivered or not.
|
|
|
|
///
|
2014-03-25 08:44:40 -07:00
|
|
|
/// Note that signal 0 is interpreted as a poll to check whether the child
|
|
|
|
/// process is still alive or not. If an error is returned, then the child
|
|
|
|
/// process has exited.
|
|
|
|
///
|
|
|
|
/// On some unix platforms signals will continue to be received after a
|
|
|
|
/// child has exited but not yet been reaped. In order to report the status
|
|
|
|
/// of signal delivery correctly, unix implementations may invoke
|
|
|
|
/// `waitpid()` with `WNOHANG` in order to reap the child as necessary.
|
|
|
|
///
|
|
|
|
/// # Errors
|
2013-09-16 15:28:56 -07:00
|
|
|
///
|
2014-01-30 16:55:20 -08:00
|
|
|
/// If the signal delivery fails, the corresponding error is returned.
|
2014-01-29 16:33:57 -08:00
|
|
|
pub fn signal(&mut self, signal: int) -> IoResult<()> {
|
2014-10-09 16:27:28 -07:00
|
|
|
#[cfg(unix)] fn collect_status(p: &mut Process) {
|
|
|
|
// On Linux (and possibly other unices), a process that has exited will
|
|
|
|
// continue to accept signals because it is "defunct". The delivery of
|
|
|
|
// signals will only fail once the child has been reaped. For this
|
|
|
|
// reason, if the process hasn't exited yet, then we attempt to collect
|
|
|
|
// their status with WNOHANG.
|
|
|
|
if p.exit_code.is_none() {
|
|
|
|
match p.handle.try_wait() {
|
|
|
|
Some(code) => { p.exit_code = Some(code); }
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(windows)] fn collect_status(_p: &mut Process) {}
|
|
|
|
|
|
|
|
collect_status(self);
|
|
|
|
|
|
|
|
// 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_some() {
|
|
|
|
return Err(IoError {
|
|
|
|
kind: io::InvalidInput,
|
|
|
|
desc: "invalid argument: can't kill an exited process",
|
|
|
|
detail: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// A successfully delivered signal that isn't 0 (just a poll for being
|
|
|
|
// alive) is recorded for windows (see wait())
|
|
|
|
match unsafe { self.handle.kill(signal) } {
|
|
|
|
Ok(()) if signal == 0 => Ok(()),
|
|
|
|
Ok(()) => { self.exit_signal = Some(signal); Ok(()) }
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
|
2013-09-16 15:28:56 -07:00
|
|
|
}
|
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
/// Sends a signal to this child requesting that it exits. This is
|
|
|
|
/// equivalent to sending a SIGTERM on unix platforms.
|
|
|
|
pub fn signal_exit(&mut self) -> IoResult<()> {
|
|
|
|
self.signal(PleaseExitSignal)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sends a signal to this child forcing it to exit. This is equivalent to
|
|
|
|
/// sending a SIGKILL on unix platforms.
|
|
|
|
pub fn signal_kill(&mut self) -> IoResult<()> {
|
|
|
|
self.signal(MustDieSignal)
|
|
|
|
}
|
|
|
|
|
2013-09-16 15:28:56 -07:00
|
|
|
/// Wait for the child to exit completely, returning the status that it
|
|
|
|
/// exited with. This function will continue to have the same return value
|
|
|
|
/// after it has been called at least once.
|
2014-02-18 12:04:51 -08:00
|
|
|
///
|
|
|
|
/// The stdin handle to the child process will be closed before waiting.
|
2014-05-05 16:58:42 -07:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function can fail if a timeout was previously specified via
|
|
|
|
/// `set_timeout` and the timeout expires before the child exits.
|
|
|
|
pub fn wait(&mut self) -> IoResult<ProcessExit> {
|
2014-02-18 12:04:51 -08:00
|
|
|
drop(self.stdin.take());
|
2014-10-09 16:27:28 -07:00
|
|
|
match self.exit_code {
|
|
|
|
Some(code) => Ok(code),
|
|
|
|
None => {
|
|
|
|
let code = try!(self.handle.wait(self.deadline));
|
|
|
|
// On windows, waitpid will never return a signal. If a signal
|
|
|
|
// was successfully delivered to the process, however, we can
|
|
|
|
// consider it as having died via a signal.
|
|
|
|
let code = match self.exit_signal {
|
|
|
|
None => code,
|
|
|
|
Some(signal) if cfg!(windows) => ExitSignal(signal),
|
|
|
|
Some(..) => code,
|
|
|
|
};
|
|
|
|
self.exit_code = Some(code);
|
|
|
|
Ok(code)
|
|
|
|
}
|
2014-06-03 20:09:39 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
|
2014-05-05 16:58:42 -07:00
|
|
|
/// Sets a timeout, in milliseconds, for future calls to wait().
|
|
|
|
///
|
|
|
|
/// The argument specified is a relative distance into the future, in
|
|
|
|
/// milliseconds, after which any call to wait() will return immediately
|
|
|
|
/// with a timeout error, and all future calls to wait() will not block.
|
|
|
|
///
|
|
|
|
/// A value of `None` will clear any previous timeout, and a value of `Some`
|
|
|
|
/// will override any previously set timeout.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # #![allow(experimental)]
|
2014-06-27 05:22:26 +09:00
|
|
|
/// use std::io::{Command, IoResult};
|
|
|
|
/// use std::io::process::ProcessExit;
|
2014-05-05 16:58:42 -07:00
|
|
|
///
|
|
|
|
/// fn run_gracefully(prog: &str) -> IoResult<ProcessExit> {
|
2014-05-05 14:33:55 -07:00
|
|
|
/// let mut p = try!(Command::new("long-running-process").spawn());
|
2014-05-05 16:58:42 -07:00
|
|
|
///
|
|
|
|
/// // give the process 10 seconds to finish completely
|
|
|
|
/// p.set_timeout(Some(10_000));
|
|
|
|
/// match p.wait() {
|
|
|
|
/// Ok(status) => return Ok(status),
|
|
|
|
/// Err(..) => {}
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Attempt to exit gracefully, but don't wait for it too long
|
|
|
|
/// try!(p.signal_exit());
|
|
|
|
/// p.set_timeout(Some(1_000));
|
|
|
|
/// match p.wait() {
|
|
|
|
/// Ok(status) => return Ok(status),
|
|
|
|
/// Err(..) => {}
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Well, we did our best, forcefully kill the process
|
|
|
|
/// try!(p.signal_kill());
|
|
|
|
/// p.set_timeout(None);
|
|
|
|
/// p.wait()
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[experimental = "the type of the timeout is likely to change"]
|
|
|
|
pub fn set_timeout(&mut self, timeout_ms: Option<u64>) {
|
2014-10-09 16:27:28 -07:00
|
|
|
self.deadline = timeout_ms.map(|i| i + sys::timer::now()).unwrap_or(0);
|
2014-05-05 16:58:42 -07:00
|
|
|
}
|
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
/// Simultaneously wait for the child to exit and collect all remaining
|
|
|
|
/// output on the stdout/stderr handles, returning a `ProcessOutput`
|
|
|
|
/// instance.
|
|
|
|
///
|
|
|
|
/// The stdin handle to the child is closed before waiting.
|
2014-05-05 16:58:42 -07:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function can fail for any of the same reasons that `wait()` can
|
|
|
|
/// fail.
|
|
|
|
pub fn wait_with_output(mut self) -> IoResult<ProcessOutput> {
|
2014-02-18 12:04:51 -08:00
|
|
|
drop(self.stdin.take());
|
2014-03-26 09:24:16 -07:00
|
|
|
fn read(stream: Option<io::PipeStream>) -> Receiver<IoResult<Vec<u8>>> {
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
2014-02-18 12:04:51 -08:00
|
|
|
match stream {
|
2014-11-26 08:12:18 -05:00
|
|
|
Some(stream) => spawn(move |:| {
|
2014-02-18 12:04:51 -08:00
|
|
|
let mut stream = stream;
|
2014-03-09 14:58:32 -07:00
|
|
|
tx.send(stream.read_to_end())
|
2014-02-18 12:04:51 -08:00
|
|
|
}),
|
2014-03-26 09:24:16 -07:00
|
|
|
None => tx.send(Ok(Vec::new()))
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
2014-03-09 14:58:32 -07:00
|
|
|
rx
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
let stdout = read(self.stdout.take());
|
|
|
|
let stderr = read(self.stderr.take());
|
|
|
|
|
2014-05-05 16:58:42 -07:00
|
|
|
let status = try!(self.wait());
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-05-05 16:58:42 -07:00
|
|
|
Ok(ProcessOutput {
|
|
|
|
status: status,
|
|
|
|
output: stdout.recv().ok().unwrap_or(Vec::new()),
|
|
|
|
error: stderr.recv().ok().unwrap_or(Vec::new()),
|
|
|
|
})
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
2014-05-30 17:18:12 -07:00
|
|
|
|
|
|
|
/// Forgets this process, allowing it to outlive the parent
|
|
|
|
///
|
|
|
|
/// This function will forcefully prevent calling `wait()` on the child
|
|
|
|
/// process in the destructor, allowing the child to outlive the
|
|
|
|
/// parent. Note that this operation can easily lead to leaking the
|
|
|
|
/// resources of the child process, so care must be taken when
|
|
|
|
/// invoking this method.
|
|
|
|
pub fn forget(mut self) {
|
|
|
|
self.forget = true;
|
|
|
|
}
|
2013-09-16 15:28:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Process {
|
|
|
|
fn drop(&mut self) {
|
2014-05-30 17:18:12 -07:00
|
|
|
if self.forget { return }
|
|
|
|
|
2013-09-16 15:28:56 -07:00
|
|
|
// Close all I/O before exiting to ensure that the child doesn't wait
|
|
|
|
// forever to print some text or something similar.
|
2014-02-18 12:04:51 -08:00
|
|
|
drop(self.stdin.take());
|
|
|
|
drop(self.stdout.take());
|
|
|
|
drop(self.stderr.take());
|
2013-09-16 15:28:56 -07:00
|
|
|
|
2014-05-05 16:58:42 -07:00
|
|
|
self.set_timeout(None);
|
|
|
|
let _ = self.wait().unwrap();
|
2013-09-16 15:28:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-26 18:28:24 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2014-09-30 21:42:55 -07:00
|
|
|
#![allow(unused_imports)]
|
|
|
|
|
|
|
|
use super::*;
|
2013-12-26 18:28:24 -08:00
|
|
|
use prelude::*;
|
2014-09-30 21:42:55 -07:00
|
|
|
use io::timer::*;
|
|
|
|
use io::*;
|
|
|
|
use io::fs::PathExtensions;
|
|
|
|
use time::Duration;
|
|
|
|
use str;
|
|
|
|
use rt::running_on_valgrind;
|
2013-12-26 18:28:24 -08:00
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
// FIXME(#10380) these tests should not all be ignored on android.
|
|
|
|
|
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn smoke() {
|
2014-05-05 14:33:55 -07:00
|
|
|
let p = Command::new("true").spawn();
|
2014-01-30 14:10:53 -08:00
|
|
|
assert!(p.is_ok());
|
2013-12-26 18:28:24 -08:00
|
|
|
let mut p = p.unwrap();
|
2014-05-05 16:58:42 -07:00
|
|
|
assert!(p.wait().unwrap().success());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2013-12-26 18:28:24 -08:00
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn smoke_failure() {
|
2014-05-05 14:33:55 -07:00
|
|
|
match Command::new("if-this-is-a-binary-then-the-world-has-ended").spawn() {
|
2014-10-09 15:17:22 -04:00
|
|
|
Ok(..) => panic!(),
|
2013-12-26 18:28:24 -08:00
|
|
|
Err(..) => {}
|
|
|
|
}
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2013-12-26 18:28:24 -08:00
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn exit_reported_right() {
|
2014-05-05 14:33:55 -07:00
|
|
|
let p = Command::new("false").spawn();
|
2014-01-30 14:10:53 -08:00
|
|
|
assert!(p.is_ok());
|
2013-12-26 18:28:24 -08:00
|
|
|
let mut p = p.unwrap();
|
2014-05-05 16:58:42 -07:00
|
|
|
assert!(p.wait().unwrap().matches_exit_status(1));
|
2014-03-20 18:59:50 -07:00
|
|
|
drop(p.wait().clone());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2013-12-26 18:28:24 -08:00
|
|
|
|
2014-09-28 22:31:50 -07:00
|
|
|
#[cfg(all(unix, not(target_os="android")))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn signal_reported_right() {
|
2014-05-05 14:33:55 -07:00
|
|
|
let p = Command::new("/bin/sh").arg("-c").arg("kill -1 $$").spawn();
|
2014-01-30 14:10:53 -08:00
|
|
|
assert!(p.is_ok());
|
2013-12-26 18:28:24 -08:00
|
|
|
let mut p = p.unwrap();
|
2014-05-05 16:58:42 -07:00
|
|
|
match p.wait().unwrap() {
|
2013-12-26 18:28:24 -08:00
|
|
|
process::ExitSignal(1) => {},
|
2014-10-09 15:17:22 -04:00
|
|
|
result => panic!("not terminated by signal 1 (instead, {})", result),
|
2013-12-26 18:28:24 -08:00
|
|
|
}
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2013-12-26 18:28:24 -08:00
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn read_all(input: &mut Reader) -> String {
|
2014-06-21 03:39:03 -07:00
|
|
|
input.read_to_string().unwrap()
|
2013-12-26 18:28:24 -08:00
|
|
|
}
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn run_output(cmd: Command) -> String {
|
2014-05-05 14:33:55 -07:00
|
|
|
let p = cmd.spawn();
|
2014-01-30 14:10:53 -08:00
|
|
|
assert!(p.is_ok());
|
2013-12-26 18:28:24 -08:00
|
|
|
let mut p = p.unwrap();
|
2014-02-18 12:04:51 -08:00
|
|
|
assert!(p.stdout.is_some());
|
2014-10-05 18:11:17 +08:00
|
|
|
let ret = read_all(p.stdout.as_mut().unwrap() as &mut Reader);
|
2014-05-05 16:58:42 -07:00
|
|
|
assert!(p.wait().unwrap().success());
|
2013-12-26 18:28:24 -08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn stdout_works() {
|
2014-05-05 14:33:55 -07:00
|
|
|
let mut cmd = Command::new("echo");
|
|
|
|
cmd.arg("foobar").stdout(CreatePipe(false, true));
|
2014-11-27 19:45:47 -05:00
|
|
|
assert_eq!(run_output(cmd), "foobar\n");
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2013-12-26 18:28:24 -08:00
|
|
|
|
2014-09-28 22:31:50 -07:00
|
|
|
#[cfg(all(unix, not(target_os="android")))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn set_cwd_works() {
|
2014-05-05 14:33:55 -07:00
|
|
|
let mut cmd = Command::new("/bin/sh");
|
|
|
|
cmd.arg("-c").arg("pwd")
|
|
|
|
.cwd(&Path::new("/"))
|
|
|
|
.stdout(CreatePipe(false, true));
|
2014-11-27 19:45:47 -05:00
|
|
|
assert_eq!(run_output(cmd), "/\n");
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2013-12-26 18:28:24 -08:00
|
|
|
|
2014-09-28 22:31:50 -07:00
|
|
|
#[cfg(all(unix, not(target_os="android")))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn stdin_works() {
|
2014-05-05 14:33:55 -07:00
|
|
|
let mut p = Command::new("/bin/sh")
|
|
|
|
.arg("-c").arg("read line; echo $line")
|
|
|
|
.stdin(CreatePipe(true, false))
|
|
|
|
.stdout(CreatePipe(false, true))
|
|
|
|
.spawn().unwrap();
|
2014-10-05 18:11:17 +08:00
|
|
|
p.stdin.as_mut().unwrap().write("foobar".as_bytes()).unwrap();
|
2014-02-18 12:04:51 -08:00
|
|
|
drop(p.stdin.take());
|
2014-10-05 18:11:17 +08:00
|
|
|
let out = read_all(p.stdout.as_mut().unwrap() as &mut Reader);
|
2014-05-05 16:58:42 -07:00
|
|
|
assert!(p.wait().unwrap().success());
|
2014-11-27 19:45:47 -05:00
|
|
|
assert_eq!(out, "foobar\n");
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2013-12-26 18:28:24 -08:00
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn detach_works() {
|
2014-05-05 14:33:55 -07:00
|
|
|
let mut p = Command::new("true").detached().spawn().unwrap();
|
2014-05-05 16:58:42 -07:00
|
|
|
assert!(p.wait().unwrap().success());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-06 22:38:05 -08:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn uid_fails_on_windows() {
|
2014-05-05 14:33:55 -07:00
|
|
|
assert!(Command::new("test").uid(10).spawn().is_err());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-06 22:38:05 -08:00
|
|
|
|
2014-09-28 22:31:50 -07:00
|
|
|
#[cfg(all(unix, not(target_os="android")))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn uid_works() {
|
2014-02-06 22:38:05 -08:00
|
|
|
use libc;
|
2014-05-05 14:33:55 -07:00
|
|
|
let mut p = Command::new("/bin/sh")
|
|
|
|
.arg("-c").arg("true")
|
|
|
|
.uid(unsafe { libc::getuid() as uint })
|
|
|
|
.gid(unsafe { libc::getgid() as uint })
|
|
|
|
.spawn().unwrap();
|
2014-05-05 16:58:42 -07:00
|
|
|
assert!(p.wait().unwrap().success());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-06 22:38:05 -08:00
|
|
|
|
2014-09-28 22:31:50 -07:00
|
|
|
#[cfg(all(unix, not(target_os="android")))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn uid_to_root_fails() {
|
2014-02-06 22:38:05 -08:00
|
|
|
use libc;
|
|
|
|
|
|
|
|
// if we're already root, this isn't a valid test. Most of the bots run
|
|
|
|
// as non-root though (android is an exception).
|
|
|
|
if unsafe { libc::getuid() == 0 } { return }
|
2014-05-05 14:33:55 -07:00
|
|
|
assert!(Command::new("/bin/ls").uid(0).gid(0).spawn().is_err());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_status() {
|
2014-05-05 14:33:55 -07:00
|
|
|
let mut status = Command::new("false").status().unwrap();
|
2014-02-18 12:04:51 -08:00
|
|
|
assert!(status.matches_exit_status(1));
|
|
|
|
|
2014-05-05 14:33:55 -07:00
|
|
|
status = Command::new("true").status().unwrap();
|
2014-02-18 12:04:51 -08:00
|
|
|
assert!(status.success());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_output_fail_to_start() {
|
2014-05-05 14:33:55 -07:00
|
|
|
match Command::new("/no-binary-by-this-name-should-exist").output() {
|
2014-02-18 12:04:51 -08:00
|
|
|
Err(e) => assert_eq!(e.kind, FileNotFound),
|
2014-10-09 15:17:22 -04:00
|
|
|
Ok(..) => panic!()
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_output_output() {
|
2014-02-18 12:04:51 -08:00
|
|
|
let ProcessOutput {status, output, error}
|
2014-05-05 14:33:55 -07:00
|
|
|
= Command::new("echo").arg("hello").output().unwrap();
|
2014-03-26 09:24:16 -07:00
|
|
|
let output_str = str::from_utf8(output.as_slice()).unwrap();
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
assert!(status.success());
|
2014-11-27 19:45:47 -05:00
|
|
|
assert_eq!(output_str.trim().to_string(), "hello");
|
2014-02-18 12:04:51 -08:00
|
|
|
// FIXME #7224
|
|
|
|
if !running_on_valgrind() {
|
2014-03-26 09:24:16 -07:00
|
|
|
assert_eq!(error, Vec::new());
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_process_output_error() {
|
2014-02-18 12:04:51 -08:00
|
|
|
let ProcessOutput {status, output, error}
|
2014-05-05 14:33:55 -07:00
|
|
|
= Command::new("mkdir").arg(".").output().unwrap();
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
assert!(status.matches_exit_status(1));
|
2014-03-26 09:24:16 -07:00
|
|
|
assert_eq!(output, Vec::new());
|
2014-02-18 12:04:51 -08:00
|
|
|
assert!(!error.is_empty());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_finish_once() {
|
2014-05-05 14:33:55 -07:00
|
|
|
let mut prog = Command::new("false").spawn().unwrap();
|
2014-05-05 16:58:42 -07:00
|
|
|
assert!(prog.wait().unwrap().matches_exit_status(1));
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_finish_twice() {
|
2014-05-05 14:33:55 -07:00
|
|
|
let mut prog = Command::new("false").spawn().unwrap();
|
2014-05-05 16:58:42 -07:00
|
|
|
assert!(prog.wait().unwrap().matches_exit_status(1));
|
|
|
|
assert!(prog.wait().unwrap().matches_exit_status(1));
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_wait_with_output_once() {
|
2014-05-05 14:33:55 -07:00
|
|
|
let prog = Command::new("echo").arg("hello").spawn().unwrap();
|
2014-05-05 16:58:42 -07:00
|
|
|
let ProcessOutput {status, output, error} = prog.wait_with_output().unwrap();
|
2014-03-26 09:24:16 -07:00
|
|
|
let output_str = str::from_utf8(output.as_slice()).unwrap();
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
assert!(status.success());
|
2014-11-27 19:45:47 -05:00
|
|
|
assert_eq!(output_str.trim().to_string(), "hello");
|
2014-02-18 12:04:51 -08:00
|
|
|
// FIXME #7224
|
|
|
|
if !running_on_valgrind() {
|
2014-03-26 09:24:16 -07:00
|
|
|
assert_eq!(error, Vec::new());
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-09-28 22:31:50 -07:00
|
|
|
#[cfg(all(unix, not(target_os="android")))]
|
2014-05-05 14:33:55 -07:00
|
|
|
pub fn pwd_cmd() -> Command {
|
|
|
|
Command::new("pwd")
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
#[cfg(target_os="android")]
|
2014-05-05 14:33:55 -07:00
|
|
|
pub fn pwd_cmd() -> Command {
|
|
|
|
let mut cmd = Command::new("/system/bin/sh");
|
|
|
|
cmd.arg("-c").arg("pwd");
|
|
|
|
cmd
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2014-05-05 14:33:55 -07:00
|
|
|
pub fn pwd_cmd() -> Command {
|
|
|
|
let mut cmd = Command::new("cmd");
|
|
|
|
cmd.arg("/c").arg("cd");
|
|
|
|
cmd
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_keep_current_working_dir() {
|
2014-02-18 12:04:51 -08:00
|
|
|
use os;
|
2014-05-05 14:33:55 -07:00
|
|
|
let prog = pwd_cmd().spawn().unwrap();
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-06-30 16:41:30 +02:00
|
|
|
let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap();
|
2014-11-11 14:38:20 +09:00
|
|
|
let parent_dir = os::getcwd().unwrap();
|
2014-11-27 14:43:55 -05:00
|
|
|
let child_dir = Path::new(output.trim());
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
let parent_stat = parent_dir.stat().unwrap();
|
|
|
|
let child_stat = child_dir.stat().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(parent_stat.unstable.device, child_stat.unstable.device);
|
|
|
|
assert_eq!(parent_stat.unstable.inode, child_stat.unstable.inode);
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_change_working_directory() {
|
2014-02-18 12:04:51 -08:00
|
|
|
use os;
|
|
|
|
// test changing to the parent of os::getcwd() because we know
|
|
|
|
// the path exists (and os::getcwd() is not expected to be root)
|
2014-11-11 14:38:20 +09:00
|
|
|
let parent_dir = os::getcwd().unwrap().dir_path();
|
2014-05-05 14:33:55 -07:00
|
|
|
let prog = pwd_cmd().cwd(&parent_dir).spawn().unwrap();
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-06-30 16:41:30 +02:00
|
|
|
let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap();
|
2014-11-27 14:43:55 -05:00
|
|
|
let child_dir = Path::new(output.trim());
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
let parent_stat = parent_dir.stat().unwrap();
|
|
|
|
let child_stat = child_dir.stat().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(parent_stat.unstable.device, child_stat.unstable.device);
|
|
|
|
assert_eq!(parent_stat.unstable.inode, child_stat.unstable.inode);
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-09-28 22:31:50 -07:00
|
|
|
#[cfg(all(unix, not(target_os="android")))]
|
2014-05-05 14:33:55 -07:00
|
|
|
pub fn env_cmd() -> Command {
|
|
|
|
Command::new("env")
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
#[cfg(target_os="android")]
|
2014-05-05 14:33:55 -07:00
|
|
|
pub fn env_cmd() -> Command {
|
|
|
|
let mut cmd = Command::new("/system/bin/sh");
|
|
|
|
cmd.arg("-c").arg("set");
|
|
|
|
cmd
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2014-05-05 14:33:55 -07:00
|
|
|
pub fn env_cmd() -> Command {
|
|
|
|
let mut cmd = Command::new("cmd");
|
|
|
|
cmd.arg("/c").arg("set");
|
|
|
|
cmd
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os="android"))]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_inherit_env() {
|
2014-02-18 12:04:51 -08:00
|
|
|
use os;
|
|
|
|
if running_on_valgrind() { return; }
|
|
|
|
|
2014-05-05 14:33:55 -07:00
|
|
|
let prog = env_cmd().spawn().unwrap();
|
2014-06-30 16:41:30 +02:00
|
|
|
let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap();
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
let r = os::env();
|
|
|
|
for &(ref k, ref v) in r.iter() {
|
|
|
|
// don't check windows magical empty-named variables
|
2014-05-16 10:45:16 -07:00
|
|
|
assert!(k.is_empty() ||
|
2014-11-27 14:43:55 -05:00
|
|
|
output.contains(format!("{}={}", *k, *v).as_slice()),
|
2014-10-02 13:14:14 -07:00
|
|
|
"output doesn't contain `{}={}`\n{}",
|
|
|
|
k, v, output);
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
#[cfg(target_os="android")]
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_inherit_env() {
|
2014-02-18 12:04:51 -08:00
|
|
|
use os;
|
|
|
|
if running_on_valgrind() { return; }
|
|
|
|
|
2014-05-05 14:33:55 -07:00
|
|
|
let mut prog = env_cmd().spawn().unwrap();
|
2014-06-30 16:41:30 +02:00
|
|
|
let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap();
|
2014-02-18 12:04:51 -08:00
|
|
|
|
|
|
|
let r = os::env();
|
|
|
|
for &(ref k, ref v) in r.iter() {
|
|
|
|
// don't check android RANDOM variables
|
2014-05-25 03:17:19 -07:00
|
|
|
if *k != "RANDOM".to_string() {
|
2014-12-07 08:49:17 -05:00
|
|
|
assert!(output.contains(format!("{}={}",
|
2014-05-16 10:45:16 -07:00
|
|
|
*k,
|
|
|
|
*v).as_slice()) ||
|
2014-12-07 08:49:17 -05:00
|
|
|
output.contains(format!("{}=\'{}\'",
|
2014-05-16 10:45:16 -07:00
|
|
|
*k,
|
|
|
|
*v).as_slice()));
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
}
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_override_env() {
|
2014-09-30 15:57:06 +02:00
|
|
|
use os;
|
|
|
|
let mut new_env = vec![("RUN_TEST_NEW_ENV", "123")];
|
|
|
|
|
|
|
|
// In some build environments (such as chrooted Nix builds), `env` can
|
|
|
|
// only be found in the explicitly-provided PATH env variable, not in
|
|
|
|
// default places such as /bin or /usr/bin. So we need to pass through
|
|
|
|
// PATH to our sub-process.
|
|
|
|
let path_val: String;
|
|
|
|
match os::getenv("PATH") {
|
|
|
|
None => {}
|
|
|
|
Some(val) => {
|
|
|
|
path_val = val;
|
|
|
|
new_env.push(("PATH", path_val.as_slice()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:50:45 -07:00
|
|
|
let prog = env_cmd().env_set_all(new_env.as_slice()).spawn().unwrap();
|
2014-05-05 16:58:42 -07:00
|
|
|
let result = prog.wait_with_output().unwrap();
|
2014-07-10 18:21:16 +02:00
|
|
|
let output = String::from_utf8_lossy(result.output.as_slice()).into_string();
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-11-27 14:43:55 -05:00
|
|
|
assert!(output.contains("RUN_TEST_NEW_ENV=123"),
|
2014-02-18 12:04:51 -08:00
|
|
|
"didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_add_to_env() {
|
2014-07-02 13:50:45 -07:00
|
|
|
let prog = env_cmd().env("RUN_TEST_NEW_ENV", "123").spawn().unwrap();
|
|
|
|
let result = prog.wait_with_output().unwrap();
|
2014-10-05 18:11:17 +08:00
|
|
|
let output = String::from_utf8_lossy(result.output.as_slice()).into_string();
|
2014-07-02 13:50:45 -07:00
|
|
|
|
2014-11-27 14:43:55 -05:00
|
|
|
assert!(output.contains("RUN_TEST_NEW_ENV=123"),
|
2014-07-02 13:50:45 -07:00
|
|
|
"didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-07-02 13:50:45 -07:00
|
|
|
|
2014-02-18 12:04:51 -08:00
|
|
|
#[cfg(unix)]
|
|
|
|
pub fn sleeper() -> Process {
|
2014-05-05 14:33:55 -07:00
|
|
|
Command::new("sleep").arg("1000").spawn().unwrap()
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn sleeper() -> Process {
|
2014-02-24 10:59:11 -08:00
|
|
|
// There's a `timeout` command on windows, but it doesn't like having
|
|
|
|
// its output piped, so instead just ping ourselves a few times with
|
2014-06-08 13:22:49 -04:00
|
|
|
// gaps in between so we're sure this process is alive for awhile
|
2014-05-05 14:33:55 -07:00
|
|
|
Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn().unwrap()
|
2014-02-18 12:04:51 -08:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_kill() {
|
2014-02-18 12:04:51 -08:00
|
|
|
let mut p = sleeper();
|
|
|
|
Process::kill(p.id(), PleaseExitSignal).unwrap();
|
2014-05-05 16:58:42 -07:00
|
|
|
assert!(!p.wait().unwrap().success());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-02-18 12:04:51 -08:00
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_exists() {
|
2014-02-18 12:04:51 -08:00
|
|
|
let mut p = sleeper();
|
|
|
|
assert!(Process::kill(p.id(), 0).is_ok());
|
|
|
|
p.signal_kill().unwrap();
|
2014-05-05 16:58:42 -07:00
|
|
|
assert!(!p.wait().unwrap().success());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-03-25 08:44:40 -07:00
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn test_zero() {
|
2014-03-25 08:44:40 -07:00
|
|
|
let mut p = sleeper();
|
|
|
|
p.signal_kill().unwrap();
|
2014-04-21 17:58:52 -04:00
|
|
|
for _ in range(0i, 20) {
|
2014-03-25 08:44:40 -07:00
|
|
|
if p.signal(0).is_err() {
|
2014-05-05 16:58:42 -07:00
|
|
|
assert!(!p.wait().unwrap().success());
|
2014-03-25 08:44:40 -07:00
|
|
|
return
|
|
|
|
}
|
2014-07-30 20:07:41 -07:00
|
|
|
timer::sleep(Duration::milliseconds(100));
|
2014-03-25 08:44:40 -07:00
|
|
|
}
|
2014-10-09 15:17:22 -04:00
|
|
|
panic!("never saw the child go away");
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-05-05 16:58:42 -07:00
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn wait_timeout() {
|
2014-05-05 16:58:42 -07:00
|
|
|
let mut p = sleeper();
|
|
|
|
p.set_timeout(Some(10));
|
|
|
|
assert_eq!(p.wait().err().unwrap().kind, TimedOut);
|
|
|
|
assert_eq!(p.wait().err().unwrap().kind, TimedOut);
|
|
|
|
p.signal_kill().unwrap();
|
|
|
|
p.set_timeout(None);
|
|
|
|
assert!(p.wait().is_ok());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-05-05 16:58:42 -07:00
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn wait_timeout2() {
|
2014-05-05 16:58:42 -07:00
|
|
|
let (tx, rx) = channel();
|
|
|
|
let tx2 = tx.clone();
|
2014-11-26 08:12:18 -05:00
|
|
|
spawn(move|| {
|
2014-05-05 16:58:42 -07:00
|
|
|
let mut p = sleeper();
|
|
|
|
p.set_timeout(Some(10));
|
|
|
|
assert_eq!(p.wait().err().unwrap().kind, TimedOut);
|
|
|
|
p.signal_kill().unwrap();
|
|
|
|
tx.send(());
|
|
|
|
});
|
2014-11-26 08:12:18 -05:00
|
|
|
spawn(move|| {
|
2014-05-05 16:58:42 -07:00
|
|
|
let mut p = sleeper();
|
|
|
|
p.set_timeout(Some(10));
|
|
|
|
assert_eq!(p.wait().err().unwrap().kind, TimedOut);
|
|
|
|
p.signal_kill().unwrap();
|
|
|
|
tx2.send(());
|
|
|
|
});
|
|
|
|
rx.recv();
|
|
|
|
rx.recv();
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-05-30 17:18:12 -07:00
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn forget() {
|
2014-05-30 17:18:12 -07:00
|
|
|
let p = sleeper();
|
|
|
|
let id = p.id();
|
|
|
|
p.forget();
|
|
|
|
assert!(Process::kill(id, 0).is_ok());
|
|
|
|
assert!(Process::kill(id, PleaseExitSignal).is_ok());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-08-10 14:10:34 -07:00
|
|
|
|
2014-09-30 21:42:55 -07:00
|
|
|
#[test]
|
|
|
|
fn dont_close_fd_on_command_spawn() {
|
2014-10-09 16:27:28 -07:00
|
|
|
use sys::fs;
|
2014-08-10 14:10:34 -07:00
|
|
|
|
|
|
|
let path = if cfg!(windows) {
|
|
|
|
Path::new("NUL")
|
|
|
|
} else {
|
|
|
|
Path::new("/dev/null")
|
|
|
|
};
|
|
|
|
|
2014-11-16 10:37:31 +01:00
|
|
|
let fdes = match fs::open(&path, Truncate, Write) {
|
2014-08-10 14:10:34 -07:00
|
|
|
Ok(f) => f,
|
2014-10-09 15:17:22 -04:00
|
|
|
Err(_) => panic!("failed to open file descriptor"),
|
2014-08-10 14:10:34 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut cmd = pwd_cmd();
|
|
|
|
let _ = cmd.stdout(InheritFd(fdes.fd()));
|
|
|
|
assert!(cmd.status().unwrap().success());
|
2014-10-09 16:27:28 -07:00
|
|
|
assert!(fdes.write("extra write\n".as_bytes()).is_ok());
|
2014-09-30 21:42:55 -07:00
|
|
|
}
|
2014-09-14 01:33:21 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn env_map_keys_ci() {
|
|
|
|
use super::EnvKey;
|
|
|
|
let mut cmd = Command::new("");
|
|
|
|
cmd.env("path", "foo");
|
|
|
|
cmd.env("Path", "bar");
|
|
|
|
let env = &cmd.env.unwrap();
|
2014-11-06 12:25:16 -05:00
|
|
|
let val = env.get(&EnvKey("PATH".to_c_str()));
|
2014-09-14 01:33:21 -07:00
|
|
|
assert!(val.unwrap() == &"bar".to_c_str());
|
|
|
|
}
|
2013-12-26 18:28:24 -08:00
|
|
|
}
|