1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Bindings for executing child processes
14 #![allow(non_upper_case_globals)]
16 pub use self::StdioContainer::*;
17 pub use self::ProcessExit::*;
21 use collections::HashMap;
25 use io::pipe::{PipeStream, PipePair};
26 use io::{IoResult, IoError};
30 use path::BytesContainer;
31 use sync::mpsc::{channel, Receiver};
32 use sys::fs::FileDesc;
33 use sys::process::Process as ProcessImp;
37 #[cfg(windows)] use hash;
38 #[cfg(windows)] use str;
40 /// Signal a process to exit, without forcibly killing it. Corresponds to
41 /// SIGTERM on unix platforms.
42 #[cfg(windows)] pub const PleaseExitSignal: int = 15;
43 /// Signal a process to exit immediately, forcibly killing it. Corresponds to
44 /// SIGKILL on unix platforms.
45 #[cfg(windows)] pub const MustDieSignal: int = 9;
46 /// Signal a process to exit, without forcibly killing it. Corresponds to
47 /// SIGTERM on unix platforms.
48 #[cfg(not(windows))] pub const PleaseExitSignal: int = libc::SIGTERM as int;
49 /// Signal a process to exit immediately, forcibly killing it. Corresponds to
50 /// SIGKILL on unix platforms.
51 #[cfg(not(windows))] pub const MustDieSignal: int = libc::SIGKILL as int;
53 /// Representation of a running or exited child process.
55 /// This structure is used to represent and manage child processes. A child
56 /// process is created via the `Command` struct, which configures the spawning
57 /// process and can itself be constructed using a builder-style interface.
62 /// use std::io::Command;
64 /// let mut child = match Command::new("/bin/cat").arg("file.txt").spawn() {
65 /// Ok(child) => child,
66 /// Err(e) => panic!("failed to execute child: {}", e),
69 /// let contents = child.stdout.as_mut().unwrap().read_to_end();
70 /// assert!(child.wait().unwrap().success());
76 /// None until wait() is called.
77 exit_code: Option<ProcessExit>,
79 /// Manually delivered signal
80 exit_signal: Option<int>,
82 /// Deadline after which wait() will return
85 /// Handle to the child's stdin, if the `stdin` field of this process's
86 /// `ProcessConfig` was `CreatePipe`. By default, this handle is `Some`.
87 pub stdin: Option<PipeStream>,
89 /// Handle to the child's stdout, if the `stdout` field of this process's
90 /// `ProcessConfig` was `CreatePipe`. By default, this handle is `Some`.
91 pub stdout: Option<PipeStream>,
93 /// Handle to the child's stderr, if the `stderr` field of this process's
94 /// `ProcessConfig` was `CreatePipe`. By default, this handle is `Some`.
95 pub stderr: Option<PipeStream>,
98 /// A representation of environment variable name
99 /// It compares case-insensitive on Windows and case-sensitive everywhere else.
101 #[derive(Hash, PartialEq, Eq, Clone, Show)]
102 struct EnvKey(CString);
106 #[derive(Eq, Clone, Show)]
107 struct EnvKey(CString);
110 impl<H: hash::Writer + hash::Hasher> hash::Hash<H> for EnvKey {
111 fn hash(&self, state: &mut H) {
112 let &EnvKey(ref x) = self;
113 match str::from_utf8(x.as_bytes()) {
114 Ok(s) => for ch in s.chars() {
115 (ch as u8 as char).to_lowercase().hash(state);
117 Err(..) => x.hash(state)
123 impl PartialEq for EnvKey {
124 fn eq(&self, other: &EnvKey) -> bool {
125 let &EnvKey(ref x) = self;
126 let &EnvKey(ref y) = other;
127 match (str::from_utf8(x.as_bytes()), str::from_utf8(y.as_bytes())) {
128 (Ok(xs), Ok(ys)) => {
129 if xs.len() != ys.len() {
132 for (xch, ych) in xs.chars().zip(ys.chars()) {
133 if xch.to_lowercase() != ych.to_lowercase() {
140 // If either is not a valid utf8 string, just compare them byte-wise
146 impl BytesContainer for EnvKey {
147 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
148 let &EnvKey(ref k) = self;
149 k.container_as_bytes()
153 /// A HashMap representation of environment variables.
154 pub type EnvMap = HashMap<EnvKey, CString>;
156 /// The `Command` type acts as a process builder, providing fine-grained control
157 /// over how a new process should be spawned. A default configuration can be
158 /// generated using `Command::new(program)`, where `program` gives a path to the
159 /// program to be executed. Additional builder methods allow the configuration
160 /// to be changed (for example, by adding arguments) prior to spawning:
163 /// use std::io::Command;
165 /// let mut process = match Command::new("sh").arg("-c").arg("echo hello").spawn() {
167 /// Err(e) => panic!("failed to execute process: {}", e),
170 /// let output = process.stdout.as_mut().unwrap().read_to_end();
174 // The internal data for the builder. Documented by the builder
175 // methods below, and serialized into rt::rtio::ProcessConfig.
179 cwd: Option<CString>,
180 stdin: StdioContainer,
181 stdout: StdioContainer,
182 stderr: StdioContainer,
188 // FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
189 // we cannot usefully take BytesContainer arguments by reference (without forcing an
190 // additional & around &str). So we are instead temporarily adding an instance
191 // for &Path, so that we can take BytesContainer as owned. When DST lands, the &Path
192 // instance should be removed, and arguments bound by BytesContainer should be passed by
193 // reference. (Here: {new, arg, args, env}.)
196 /// Constructs a new `Command` for launching the program at
197 /// path `program`, with the following default configuration:
199 /// * No arguments to the program
200 /// * Inherit the current process's environment
201 /// * Inherit the current process's working directory
202 /// * A readable pipe for stdin (file descriptor 0)
203 /// * A writeable pipe for stdout and stderr (file descriptors 1 and 2)
205 /// Builder methods are provided to change these defaults and
206 /// otherwise configure the process.
207 pub fn new<T: BytesContainer>(program: T) -> Command {
209 program: CString::from_slice(program.container_as_bytes()),
213 stdin: CreatePipe(true, false),
214 stdout: CreatePipe(false, true),
215 stderr: CreatePipe(false, true),
222 /// Add an argument to pass to the program.
223 pub fn arg<'a, T: BytesContainer>(&'a mut self, arg: T) -> &'a mut Command {
224 self.args.push(CString::from_slice(arg.container_as_bytes()));
228 /// Add multiple arguments to pass to the program.
229 pub fn args<'a, T: BytesContainer>(&'a mut self, args: &[T]) -> &'a mut Command {
230 self.args.extend(args.iter().map(|arg| {
231 CString::from_slice(arg.container_as_bytes())
235 // Get a mutable borrow of the environment variable map for this `Command`.
236 fn get_env_map<'a>(&'a mut self) -> &'a mut EnvMap {
238 Some(ref mut map) => map,
240 // if the env is currently just inheriting from the parent's,
241 // materialize the parent's env into a hashtable.
242 self.env = Some(os::env_as_bytes().into_iter().map(|(k, v)| {
243 (EnvKey(CString::from_slice(k.as_slice())),
244 CString::from_slice(v.as_slice()))
246 self.env.as_mut().unwrap()
251 /// Inserts or updates an environment variable mapping.
253 /// Note that environment variable names are case-insensitive (but case-preserving) on Windows,
254 /// and case-sensitive on all other platforms.
255 pub fn env<'a, T, U>(&'a mut self, key: T, val: U)
257 where T: BytesContainer, U: BytesContainer {
258 let key = EnvKey(CString::from_slice(key.container_as_bytes()));
259 let val = CString::from_slice(val.container_as_bytes());
260 self.get_env_map().insert(key, val);
264 /// Removes an environment variable mapping.
265 pub fn env_remove<'a, T>(&'a mut self, key: T) -> &'a mut Command
266 where T: BytesContainer {
267 let key = EnvKey(CString::from_slice(key.container_as_bytes()));
268 self.get_env_map().remove(&key);
272 /// Sets the entire environment map for the child process.
274 /// If the given slice contains multiple instances of an environment
275 /// variable, the *rightmost* instance will determine the value.
276 pub fn env_set_all<'a, T, U>(&'a mut self, env: &[(T,U)])
278 where T: BytesContainer, U: BytesContainer {
279 self.env = Some(env.iter().map(|&(ref k, ref v)| {
280 (EnvKey(CString::from_slice(k.container_as_bytes())),
281 CString::from_slice(v.container_as_bytes()))
286 /// Set the working directory for the child process.
287 pub fn cwd<'a>(&'a mut self, dir: &Path) -> &'a mut Command {
288 self.cwd = Some(CString::from_slice(dir.as_vec()));
292 /// Configuration for the child process's stdin handle (file descriptor 0).
293 /// Defaults to `CreatePipe(true, false)` so the input can be written to.
294 pub fn stdin<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command {
299 /// Configuration for the child process's stdout handle (file descriptor 1).
300 /// Defaults to `CreatePipe(false, true)` so the output can be collected.
301 pub fn stdout<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command {
306 /// Configuration for the child process's stderr handle (file descriptor 2).
307 /// Defaults to `CreatePipe(false, true)` so the output can be collected.
308 pub fn stderr<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command {
313 /// Sets the child process's user id. This translates to a `setuid` call in
314 /// the child process. Setting this value on windows will cause the spawn to
315 /// fail. Failure in the `setuid` call on unix will also cause the spawn to
317 pub fn uid<'a>(&'a mut self, id: uint) -> &'a mut Command {
322 /// Similar to `uid`, but sets the group id of the child process. This has
323 /// the same semantics as the `uid` field.
324 pub fn gid<'a>(&'a mut self, id: uint) -> &'a mut Command {
329 /// Sets the child process to be spawned in a detached state. On unix, this
330 /// means that the child is the leader of a new process group.
331 pub fn detached<'a>(&'a mut self) -> &'a mut Command {
336 /// Executes the command as a child process, which is returned.
337 pub fn spawn(&self) -> IoResult<Process> {
338 let (their_stdin, our_stdin) = try!(setup_io(self.stdin));
339 let (their_stdout, our_stdout) = try!(setup_io(self.stdout));
340 let (their_stderr, our_stderr) = try!(setup_io(self.stderr));
342 match ProcessImp::spawn(self, their_stdin, their_stdout, their_stderr) {
344 Ok(handle) => Ok(Process {
357 /// Executes the command as a child process, waiting for it to finish and
358 /// collecting all of its output.
363 /// use std::io::Command;
365 /// let output = match Command::new("cat").arg("foot.txt").output() {
366 /// Ok(output) => output,
367 /// Err(e) => panic!("failed to execute process: {}", e),
370 /// println!("status: {}", output.status);
371 /// println!("stdout: {}", String::from_utf8_lossy(output.output.as_slice()));
372 /// println!("stderr: {}", String::from_utf8_lossy(output.error.as_slice()));
374 pub fn output(&self) -> IoResult<ProcessOutput> {
375 self.spawn().and_then(|p| p.wait_with_output())
378 /// Executes a command as a child process, waiting for it to finish and
379 /// collecting its exit status.
384 /// use std::io::Command;
386 /// let status = match Command::new("ls").status() {
387 /// Ok(status) => status,
388 /// Err(e) => panic!("failed to execute process: {}", e),
391 /// println!("process exited with: {}", status);
393 pub fn status(&self) -> IoResult<ProcessExit> {
394 self.spawn().and_then(|mut p| p.wait())
398 impl fmt::String for Command {
399 /// Format the program and arguments of a Command for display. Any
400 /// non-utf8 data is lossily converted using the utf8 replacement
402 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
403 try!(write!(f, "{}", String::from_utf8_lossy(self.program.as_bytes())));
404 for arg in self.args.iter() {
405 try!(write!(f, " '{}'", String::from_utf8_lossy(arg.as_bytes())));
411 fn setup_io(io: StdioContainer) -> IoResult<(Option<PipeStream>, Option<PipeStream>)> {
420 theirs = Some(PipeStream::from_filedesc(FileDesc::new(fd, false)));
423 CreatePipe(readable, _writable) => {
424 let PipePair { reader, writer } = try!(PipeStream::pair());
426 theirs = Some(reader);
429 theirs = Some(writer);
437 // Allow the sys module to get access to the Command state
438 impl sys::process::ProcessConfig<EnvKey, CString> for Command {
439 fn program(&self) -> &CString {
442 fn args(&self) -> &[CString] {
445 fn env(&self) -> Option<&EnvMap> {
448 fn cwd(&self) -> Option<&CString> {
451 fn uid(&self) -> Option<uint> {
454 fn gid(&self) -> Option<uint> {
457 fn detach(&self) -> bool {
463 /// The output of a finished process.
464 #[derive(PartialEq, Eq, Clone)]
465 pub struct ProcessOutput {
466 /// The status (exit code) of the process.
467 pub status: ProcessExit,
468 /// The data that the process wrote to stdout.
470 /// The data that the process wrote to stderr.
474 /// Describes what to do with a standard io stream for a child process.
475 #[derive(Clone, Copy)]
476 pub enum StdioContainer {
477 /// This stream will be ignored. This is the equivalent of attaching the
478 /// stream to `/dev/null`
481 /// The specified file descriptor is inherited for the stream which it is
482 /// specified for. Ownership of the file descriptor is *not* taken, so the
483 /// caller must clean it up.
484 InheritFd(libc::c_int),
486 /// Creates a pipe for the specified file descriptor which will be created
487 /// when the process is spawned.
489 /// The first boolean argument is whether the pipe is readable, and the
490 /// second is whether it is writable. These properties are from the view of
491 /// the *child* process, not the parent process.
492 CreatePipe(bool /* readable */, bool /* writable */),
495 /// Describes the result of a process after it has terminated.
496 /// Note that Windows have no signals, so the result is usually ExitStatus.
497 #[derive(PartialEq, Eq, Clone, Copy)]
498 pub enum ProcessExit {
499 /// Normal termination with an exit status.
502 /// Termination by signal, with the signal number.
506 impl fmt::Show for ProcessExit {
507 /// Format a ProcessExit enum, to nicely present the information.
508 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
509 fmt::String::fmt(self, f)
514 impl fmt::String for ProcessExit {
515 /// Format a ProcessExit enum, to nicely present the information.
516 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
518 ExitStatus(code) => write!(f, "exit code: {}", code),
519 ExitSignal(code) => write!(f, "signal: {}", code),
525 /// Was termination successful? Signal termination not considered a success,
526 /// and success is defined as a zero exit status.
527 pub fn success(&self) -> bool {
528 return self.matches_exit_status(0);
531 /// Checks whether this ProcessExit matches the given exit status.
532 /// Termination by signal will never match an exit code.
533 pub fn matches_exit_status(&self, wanted: int) -> bool {
534 *self == ExitStatus(wanted)
539 /// Sends `signal` to another process in the system identified by `id`.
541 /// Note that windows doesn't quite have the same model as unix, so some
542 /// unix signals are mapped to windows signals. Notably, unix termination
543 /// signals (SIGTERM/SIGKILL/SIGINT) are translated to `TerminateProcess`.
545 /// Additionally, a signal number of 0 can check for existence of the target
546 /// process. Note, though, that on some platforms signals will continue to
547 /// be successfully delivered if the child has exited, but not yet been
549 pub fn kill(id: libc::pid_t, signal: int) -> IoResult<()> {
550 unsafe { ProcessImp::killpid(id, signal) }
553 /// Returns the process id of this child process
554 pub fn id(&self) -> libc::pid_t { self.handle.id() }
556 /// Sends the specified signal to the child process, returning whether the
557 /// signal could be delivered or not.
559 /// Note that signal 0 is interpreted as a poll to check whether the child
560 /// process is still alive or not. If an error is returned, then the child
561 /// process has exited.
563 /// On some unix platforms signals will continue to be received after a
564 /// child has exited but not yet been reaped. In order to report the status
565 /// of signal delivery correctly, unix implementations may invoke
566 /// `waitpid()` with `WNOHANG` in order to reap the child as necessary.
570 /// If the signal delivery fails, the corresponding error is returned.
571 pub fn signal(&mut self, signal: int) -> IoResult<()> {
572 #[cfg(unix)] fn collect_status(p: &mut Process) {
573 // On Linux (and possibly other unices), a process that has exited will
574 // continue to accept signals because it is "defunct". The delivery of
575 // signals will only fail once the child has been reaped. For this
576 // reason, if the process hasn't exited yet, then we attempt to collect
577 // their status with WNOHANG.
578 if p.exit_code.is_none() {
579 match p.handle.try_wait() {
580 Some(code) => { p.exit_code = Some(code); }
585 #[cfg(windows)] fn collect_status(_p: &mut Process) {}
587 collect_status(self);
589 // if the process has finished, and therefore had waitpid called,
590 // and we kill it, then on unix we might ending up killing a
591 // newer process that happens to have the same (re-used) id
592 if self.exit_code.is_some() {
594 kind: io::InvalidInput,
595 desc: "invalid argument: can't kill an exited process",
600 // A successfully delivered signal that isn't 0 (just a poll for being
601 // alive) is recorded for windows (see wait())
602 match unsafe { self.handle.kill(signal) } {
603 Ok(()) if signal == 0 => Ok(()),
604 Ok(()) => { self.exit_signal = Some(signal); Ok(()) }
610 /// Sends a signal to this child requesting that it exits. This is
611 /// equivalent to sending a SIGTERM on unix platforms.
612 pub fn signal_exit(&mut self) -> IoResult<()> {
613 self.signal(PleaseExitSignal)
616 /// Sends a signal to this child forcing it to exit. This is equivalent to
617 /// sending a SIGKILL on unix platforms.
618 pub fn signal_kill(&mut self) -> IoResult<()> {
619 self.signal(MustDieSignal)
622 /// Wait for the child to exit completely, returning the status that it
623 /// exited with. This function will continue to have the same return value
624 /// after it has been called at least once.
626 /// The stdin handle to the child process will be closed before waiting.
630 /// This function can fail if a timeout was previously specified via
631 /// `set_timeout` and the timeout expires before the child exits.
632 pub fn wait(&mut self) -> IoResult<ProcessExit> {
633 drop(self.stdin.take());
634 match self.exit_code {
635 Some(code) => Ok(code),
637 let code = try!(self.handle.wait(self.deadline));
638 // On windows, waitpid will never return a signal. If a signal
639 // was successfully delivered to the process, however, we can
640 // consider it as having died via a signal.
641 let code = match self.exit_signal {
643 Some(signal) if cfg!(windows) => ExitSignal(signal),
646 self.exit_code = Some(code);
652 /// Sets a timeout, in milliseconds, for future calls to wait().
654 /// The argument specified is a relative distance into the future, in
655 /// milliseconds, after which any call to wait() will return immediately
656 /// with a timeout error, and all future calls to wait() will not block.
658 /// A value of `None` will clear any previous timeout, and a value of `Some`
659 /// will override any previously set timeout.
664 /// # #![allow(unstable)]
665 /// use std::io::{Command, IoResult};
666 /// use std::io::process::ProcessExit;
668 /// fn run_gracefully(prog: &str) -> IoResult<ProcessExit> {
669 /// let mut p = try!(Command::new("long-running-process").spawn());
671 /// // give the process 10 seconds to finish completely
672 /// p.set_timeout(Some(10_000));
674 /// Ok(status) => return Ok(status),
678 /// // Attempt to exit gracefully, but don't wait for it too long
679 /// try!(p.signal_exit());
680 /// p.set_timeout(Some(1_000));
682 /// Ok(status) => return Ok(status),
686 /// // Well, we did our best, forcefully kill the process
687 /// try!(p.signal_kill());
688 /// p.set_timeout(None);
692 #[unstable = "the type of the timeout is likely to change"]
693 pub fn set_timeout(&mut self, timeout_ms: Option<u64>) {
694 self.deadline = timeout_ms.map(|i| i + sys::timer::now()).unwrap_or(0);
697 /// Simultaneously wait for the child to exit and collect all remaining
698 /// output on the stdout/stderr handles, returning a `ProcessOutput`
701 /// The stdin handle to the child is closed before waiting.
705 /// This function can fail for any of the same reasons that `wait()` can
707 pub fn wait_with_output(mut self) -> IoResult<ProcessOutput> {
708 drop(self.stdin.take());
709 fn read(stream: Option<io::PipeStream>) -> Receiver<IoResult<Vec<u8>>> {
710 let (tx, rx) = channel();
713 Thread::spawn(move |:| {
714 let mut stream = stream;
715 tx.send(stream.read_to_end()).unwrap();
718 None => tx.send(Ok(Vec::new())).unwrap()
722 let stdout = read(self.stdout.take());
723 let stderr = read(self.stderr.take());
725 let status = try!(self.wait());
729 output: stdout.recv().unwrap().unwrap_or(Vec::new()),
730 error: stderr.recv().unwrap().unwrap_or(Vec::new()),
734 /// Forgets this process, allowing it to outlive the parent
736 /// This function will forcefully prevent calling `wait()` on the child
737 /// process in the destructor, allowing the child to outlive the
738 /// parent. Note that this operation can easily lead to leaking the
739 /// resources of the child process, so care must be taken when
740 /// invoking this method.
741 pub fn forget(mut self) {
746 impl Drop for Process {
748 if self.forget { return }
750 // Close all I/O before exiting to ensure that the child doesn't wait
751 // forever to print some text or something similar.
752 drop(self.stdin.take());
753 drop(self.stdout.take());
754 drop(self.stderr.take());
756 self.set_timeout(None);
757 let _ = self.wait().unwrap();
763 use io::{Truncate, Write, TimedOut, timer, process, FileNotFound};
764 use prelude::v1::{Ok, Err, range, drop, Some, None, Vec};
765 use prelude::v1::{Path, String, Reader, Writer, Clone};
766 use prelude::v1::{SliceExt, Str, StrExt, AsSlice, ToString, GenericPath};
767 use io::fs::PathExtensions;
769 use rt::running_on_valgrind;
771 use super::{CreatePipe};
772 use super::{InheritFd, Process, PleaseExitSignal, Command, ProcessOutput};
773 use sync::mpsc::channel;
777 // FIXME(#10380) these tests should not all be ignored on android.
779 #[cfg(not(target_os="android"))]
782 let p = Command::new("true").spawn();
784 let mut p = p.unwrap();
785 assert!(p.wait().unwrap().success());
788 #[cfg(not(target_os="android"))]
791 match Command::new("if-this-is-a-binary-then-the-world-has-ended").spawn() {
797 #[cfg(not(target_os="android"))]
799 fn exit_reported_right() {
800 let p = Command::new("false").spawn();
802 let mut p = p.unwrap();
803 assert!(p.wait().unwrap().matches_exit_status(1));
804 drop(p.wait().clone());
807 #[cfg(all(unix, not(target_os="android")))]
809 fn signal_reported_right() {
810 let p = Command::new("/bin/sh").arg("-c").arg("kill -1 $$").spawn();
812 let mut p = p.unwrap();
813 match p.wait().unwrap() {
814 process::ExitSignal(1) => {},
815 result => panic!("not terminated by signal 1 (instead, {})", result),
819 pub fn read_all(input: &mut Reader) -> String {
820 input.read_to_string().unwrap()
823 pub fn run_output(cmd: Command) -> String {
826 let mut p = p.unwrap();
827 assert!(p.stdout.is_some());
828 let ret = read_all(p.stdout.as_mut().unwrap() as &mut Reader);
829 assert!(p.wait().unwrap().success());
833 #[cfg(not(target_os="android"))]
836 let mut cmd = Command::new("echo");
837 cmd.arg("foobar").stdout(CreatePipe(false, true));
838 assert_eq!(run_output(cmd), "foobar\n");
841 #[cfg(all(unix, not(target_os="android")))]
844 let mut cmd = Command::new("/bin/sh");
845 cmd.arg("-c").arg("pwd")
846 .cwd(&Path::new("/"))
847 .stdout(CreatePipe(false, true));
848 assert_eq!(run_output(cmd), "/\n");
851 #[cfg(all(unix, not(target_os="android")))]
854 let mut p = Command::new("/bin/sh")
855 .arg("-c").arg("read line; echo $line")
856 .stdin(CreatePipe(true, false))
857 .stdout(CreatePipe(false, true))
859 p.stdin.as_mut().unwrap().write("foobar".as_bytes()).unwrap();
860 drop(p.stdin.take());
861 let out = read_all(p.stdout.as_mut().unwrap() as &mut Reader);
862 assert!(p.wait().unwrap().success());
863 assert_eq!(out, "foobar\n");
866 #[cfg(not(target_os="android"))]
869 let mut p = Command::new("true").detached().spawn().unwrap();
870 assert!(p.wait().unwrap().success());
875 fn uid_fails_on_windows() {
876 assert!(Command::new("test").uid(10).spawn().is_err());
879 #[cfg(all(unix, not(target_os="android")))]
883 let mut p = Command::new("/bin/sh")
884 .arg("-c").arg("true")
885 .uid(unsafe { libc::getuid() as uint })
886 .gid(unsafe { libc::getgid() as uint })
888 assert!(p.wait().unwrap().success());
891 #[cfg(all(unix, not(target_os="android")))]
893 fn uid_to_root_fails() {
896 // if we're already root, this isn't a valid test. Most of the bots run
897 // as non-root though (android is an exception).
898 if unsafe { libc::getuid() == 0 } { return }
899 assert!(Command::new("/bin/ls").uid(0).gid(0).spawn().is_err());
902 #[cfg(not(target_os="android"))]
904 fn test_process_status() {
905 let mut status = Command::new("false").status().unwrap();
906 assert!(status.matches_exit_status(1));
908 status = Command::new("true").status().unwrap();
909 assert!(status.success());
913 fn test_process_output_fail_to_start() {
914 match Command::new("/no-binary-by-this-name-should-exist").output() {
915 Err(e) => assert_eq!(e.kind, FileNotFound),
920 #[cfg(not(target_os="android"))]
922 fn test_process_output_output() {
923 let ProcessOutput {status, output, error}
924 = Command::new("echo").arg("hello").output().unwrap();
925 let output_str = str::from_utf8(output.as_slice()).unwrap();
927 assert!(status.success());
928 assert_eq!(output_str.trim().to_string(), "hello");
930 if !running_on_valgrind() {
931 assert_eq!(error, Vec::new());
935 #[cfg(not(target_os="android"))]
937 fn test_process_output_error() {
938 let ProcessOutput {status, output, error}
939 = Command::new("mkdir").arg(".").output().unwrap();
941 assert!(status.matches_exit_status(1));
942 assert_eq!(output, Vec::new());
943 assert!(!error.is_empty());
946 #[cfg(not(target_os="android"))]
948 fn test_finish_once() {
949 let mut prog = Command::new("false").spawn().unwrap();
950 assert!(prog.wait().unwrap().matches_exit_status(1));
953 #[cfg(not(target_os="android"))]
955 fn test_finish_twice() {
956 let mut prog = Command::new("false").spawn().unwrap();
957 assert!(prog.wait().unwrap().matches_exit_status(1));
958 assert!(prog.wait().unwrap().matches_exit_status(1));
961 #[cfg(not(target_os="android"))]
963 fn test_wait_with_output_once() {
964 let prog = Command::new("echo").arg("hello").spawn().unwrap();
965 let ProcessOutput {status, output, error} = prog.wait_with_output().unwrap();
966 let output_str = str::from_utf8(output.as_slice()).unwrap();
968 assert!(status.success());
969 assert_eq!(output_str.trim().to_string(), "hello");
971 if !running_on_valgrind() {
972 assert_eq!(error, Vec::new());
976 #[cfg(all(unix, not(target_os="android")))]
977 pub fn pwd_cmd() -> Command {
980 #[cfg(target_os="android")]
981 pub fn pwd_cmd() -> Command {
982 let mut cmd = Command::new("/system/bin/sh");
983 cmd.arg("-c").arg("pwd");
988 pub fn pwd_cmd() -> Command {
989 let mut cmd = Command::new("cmd");
990 cmd.arg("/c").arg("cd");
995 fn test_keep_current_working_dir() {
997 let prog = pwd_cmd().spawn().unwrap();
999 let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap();
1000 let parent_dir = os::getcwd().unwrap();
1001 let child_dir = Path::new(output.trim());
1003 let parent_stat = parent_dir.stat().unwrap();
1004 let child_stat = child_dir.stat().unwrap();
1006 assert_eq!(parent_stat.unstable.device, child_stat.unstable.device);
1007 assert_eq!(parent_stat.unstable.inode, child_stat.unstable.inode);
1011 fn test_change_working_directory() {
1013 // test changing to the parent of os::getcwd() because we know
1014 // the path exists (and os::getcwd() is not expected to be root)
1015 let parent_dir = os::getcwd().unwrap().dir_path();
1016 let prog = pwd_cmd().cwd(&parent_dir).spawn().unwrap();
1018 let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap();
1019 let child_dir = Path::new(output.trim());
1021 let parent_stat = parent_dir.stat().unwrap();
1022 let child_stat = child_dir.stat().unwrap();
1024 assert_eq!(parent_stat.unstable.device, child_stat.unstable.device);
1025 assert_eq!(parent_stat.unstable.inode, child_stat.unstable.inode);
1028 #[cfg(all(unix, not(target_os="android")))]
1029 pub fn env_cmd() -> Command {
1032 #[cfg(target_os="android")]
1033 pub fn env_cmd() -> Command {
1034 let mut cmd = Command::new("/system/bin/sh");
1035 cmd.arg("-c").arg("set");
1040 pub fn env_cmd() -> Command {
1041 let mut cmd = Command::new("cmd");
1042 cmd.arg("/c").arg("set");
1046 #[cfg(not(target_os="android"))]
1048 fn test_inherit_env() {
1050 if running_on_valgrind() { return; }
1052 let prog = env_cmd().spawn().unwrap();
1053 let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap();
1056 for &(ref k, ref v) in r.iter() {
1057 // don't check windows magical empty-named variables
1058 assert!(k.is_empty() ||
1059 output.contains(format!("{}={}", *k, *v).as_slice()),
1060 "output doesn't contain `{}={}`\n{}",
1064 #[cfg(target_os="android")]
1066 fn test_inherit_env() {
1068 if running_on_valgrind() { return; }
1070 let mut prog = env_cmd().spawn().unwrap();
1071 let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap();
1074 for &(ref k, ref v) in r.iter() {
1075 // don't check android RANDOM variables
1076 if *k != "RANDOM".to_string() {
1077 assert!(output.contains(format!("{}={}",
1080 output.contains(format!("{}=\'{}\'",
1088 fn test_override_env() {
1090 let mut new_env = vec![("RUN_TEST_NEW_ENV", "123")];
1092 // In some build environments (such as chrooted Nix builds), `env` can
1093 // only be found in the explicitly-provided PATH env variable, not in
1094 // default places such as /bin or /usr/bin. So we need to pass through
1095 // PATH to our sub-process.
1096 let path_val: String;
1097 match os::getenv("PATH") {
1101 new_env.push(("PATH", path_val.as_slice()))
1105 let prog = env_cmd().env_set_all(new_env.as_slice()).spawn().unwrap();
1106 let result = prog.wait_with_output().unwrap();
1107 let output = String::from_utf8_lossy(result.output.as_slice()).to_string();
1109 assert!(output.contains("RUN_TEST_NEW_ENV=123"),
1110 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
1114 fn test_add_to_env() {
1115 let prog = env_cmd().env("RUN_TEST_NEW_ENV", "123").spawn().unwrap();
1116 let result = prog.wait_with_output().unwrap();
1117 let output = String::from_utf8_lossy(result.output.as_slice()).to_string();
1119 assert!(output.contains("RUN_TEST_NEW_ENV=123"),
1120 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
1124 pub fn sleeper() -> Process {
1125 Command::new("sleep").arg("1000").spawn().unwrap()
1128 pub fn sleeper() -> Process {
1129 // There's a `timeout` command on windows, but it doesn't like having
1130 // its output piped, so instead just ping ourselves a few times with
1131 // gaps in between so we're sure this process is alive for awhile
1132 Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn().unwrap()
1137 let mut p = sleeper();
1138 Process::kill(p.id(), PleaseExitSignal).unwrap();
1139 assert!(!p.wait().unwrap().success());
1144 let mut p = sleeper();
1145 assert!(Process::kill(p.id(), 0).is_ok());
1146 p.signal_kill().unwrap();
1147 assert!(!p.wait().unwrap().success());
1152 let mut p = sleeper();
1153 p.signal_kill().unwrap();
1154 for _ in range(0i, 20) {
1155 if p.signal(0).is_err() {
1156 assert!(!p.wait().unwrap().success());
1159 timer::sleep(Duration::milliseconds(100));
1161 panic!("never saw the child go away");
1166 let mut p = sleeper();
1167 p.set_timeout(Some(10));
1168 assert_eq!(p.wait().err().unwrap().kind, TimedOut);
1169 assert_eq!(p.wait().err().unwrap().kind, TimedOut);
1170 p.signal_kill().unwrap();
1171 p.set_timeout(None);
1172 assert!(p.wait().is_ok());
1176 fn wait_timeout2() {
1177 let (tx, rx) = channel();
1178 let tx2 = tx.clone();
1179 let _t = Thread::spawn(move|| {
1180 let mut p = sleeper();
1181 p.set_timeout(Some(10));
1182 assert_eq!(p.wait().err().unwrap().kind, TimedOut);
1183 p.signal_kill().unwrap();
1184 tx.send(()).unwrap();
1186 let _t = Thread::spawn(move|| {
1187 let mut p = sleeper();
1188 p.set_timeout(Some(10));
1189 assert_eq!(p.wait().err().unwrap().kind, TimedOut);
1190 p.signal_kill().unwrap();
1191 tx2.send(()).unwrap();
1202 assert!(Process::kill(id, 0).is_ok());
1203 assert!(Process::kill(id, PleaseExitSignal).is_ok());
1207 fn dont_close_fd_on_command_spawn() {
1210 let path = if cfg!(windows) {
1213 Path::new("/dev/null")
1216 let fdes = match fs::open(&path, Truncate, Write) {
1218 Err(_) => panic!("failed to open file descriptor"),
1221 let mut cmd = pwd_cmd();
1222 let _ = cmd.stdout(InheritFd(fdes.fd()));
1223 assert!(cmd.status().unwrap().success());
1224 assert!(fdes.write("extra write\n".as_bytes()).is_ok());
1229 fn env_map_keys_ci() {
1232 let mut cmd = Command::new("");
1233 cmd.env("path", "foo");
1234 cmd.env("Path", "bar");
1235 let env = &cmd.env.unwrap();
1236 let val = env.get(&EnvKey(CString::from_slice(b"PATH")));
1237 assert!(val.unwrap() == &CString::from_slice(b"bar"));