1 // Copyright 2015 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 //! A module for working with processes.
15 //! Basic usage where we try to execute the `cat` shell command:
18 //! use std::process::Command;
20 //! let mut child = Command::new("/bin/cat")
23 //! .expect("failed to execute child");
25 //! let ecode = child.wait()
26 //! .expect("failed to wait on child");
28 //! assert!(ecode.success());
31 //! Calling a command with input and reading its output:
34 //! use std::process::{Command, Stdio};
35 //! use std::io::Write;
37 //! let mut child = Command::new("/bin/cat")
38 //! .stdin(Stdio::piped())
39 //! .stdout(Stdio::piped())
41 //! .expect("failed to execute child");
44 //! // limited borrow of stdin
45 //! let stdin = child.stdin.as_mut().expect("failed to get stdin");
46 //! stdin.write_all(b"test").expect("failed to write to stdin");
49 //! let output = child
50 //! .wait_with_output()
51 //! .expect("failed to wait on child");
53 //! assert_eq!(b"test", output.stdout.as_slice());
56 #![stable(feature = "process", since = "1.0.0")]
63 use io::{self, Initializer};
66 use sys::pipe::{read2, AnonPipe};
67 use sys::process as imp;
68 use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
70 /// Representation of a running or exited child process.
72 /// This structure is used to represent and manage child processes. A child
73 /// process is created via the [`Command`] struct, which configures the
74 /// spawning process and can itself be constructed using a builder-style
77 /// There is no implementation of [`Drop`] for child processes,
78 /// so if you do not ensure the `Child` has exited then it will continue to
79 /// run, even after the `Child` handle to the child process has gone out of
82 /// Calling [`wait`](#method.wait) (or other functions that wrap around it) will make
83 /// the parent process wait until the child has actually exited before
89 /// use std::process::Command;
91 /// let mut child = Command::new("/bin/cat")
94 /// .expect("failed to execute child");
96 /// let ecode = child.wait()
97 /// .expect("failed to wait on child");
99 /// assert!(ecode.success());
102 /// [`Command`]: struct.Command.html
103 /// [`Drop`]: ../../core/ops/trait.Drop.html
104 /// [`wait`]: #method.wait
105 #[stable(feature = "process", since = "1.0.0")]
107 handle: imp::Process,
109 /// The handle for writing to the child's stdin, if it has been captured
110 #[stable(feature = "process", since = "1.0.0")]
111 pub stdin: Option<ChildStdin>,
113 /// The handle for reading from the child's stdout, if it has been captured
114 #[stable(feature = "process", since = "1.0.0")]
115 pub stdout: Option<ChildStdout>,
117 /// The handle for reading from the child's stderr, if it has been captured
118 #[stable(feature = "process", since = "1.0.0")]
119 pub stderr: Option<ChildStderr>,
122 impl AsInner<imp::Process> for Child {
123 fn as_inner(&self) -> &imp::Process { &self.handle }
126 impl FromInner<(imp::Process, imp::StdioPipes)> for Child {
127 fn from_inner((handle, io): (imp::Process, imp::StdioPipes)) -> Child {
130 stdin: io.stdin.map(ChildStdin::from_inner),
131 stdout: io.stdout.map(ChildStdout::from_inner),
132 stderr: io.stderr.map(ChildStderr::from_inner),
137 impl IntoInner<imp::Process> for Child {
138 fn into_inner(self) -> imp::Process { self.handle }
141 #[stable(feature = "std_debug", since = "1.16.0")]
142 impl fmt::Debug for Child {
143 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
144 f.debug_struct("Child")
145 .field("stdin", &self.stdin)
146 .field("stdout", &self.stdout)
147 .field("stderr", &self.stderr)
152 /// A handle to a child process's stdin.
154 /// This struct is used in the [`stdin`] field on [`Child`].
156 /// [`Child`]: struct.Child.html
157 /// [`stdin`]: struct.Child.html#structfield.stdin
158 #[stable(feature = "process", since = "1.0.0")]
159 pub struct ChildStdin {
163 #[stable(feature = "process", since = "1.0.0")]
164 impl Write for ChildStdin {
165 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
166 self.inner.write(buf)
169 fn flush(&mut self) -> io::Result<()> {
174 impl AsInner<AnonPipe> for ChildStdin {
175 fn as_inner(&self) -> &AnonPipe { &self.inner }
178 impl IntoInner<AnonPipe> for ChildStdin {
179 fn into_inner(self) -> AnonPipe { self.inner }
182 impl FromInner<AnonPipe> for ChildStdin {
183 fn from_inner(pipe: AnonPipe) -> ChildStdin {
184 ChildStdin { inner: pipe }
188 #[stable(feature = "std_debug", since = "1.16.0")]
189 impl fmt::Debug for ChildStdin {
190 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
191 f.pad("ChildStdin { .. }")
195 /// A handle to a child process's stdout.
197 /// This struct is used in the [`stdout`] field on [`Child`].
199 /// [`Child`]: struct.Child.html
200 /// [`stdout`]: struct.Child.html#structfield.stdout
201 #[stable(feature = "process", since = "1.0.0")]
202 pub struct ChildStdout {
206 #[stable(feature = "process", since = "1.0.0")]
207 impl Read for ChildStdout {
208 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
212 unsafe fn initializer(&self) -> Initializer {
217 impl AsInner<AnonPipe> for ChildStdout {
218 fn as_inner(&self) -> &AnonPipe { &self.inner }
221 impl IntoInner<AnonPipe> for ChildStdout {
222 fn into_inner(self) -> AnonPipe { self.inner }
225 impl FromInner<AnonPipe> for ChildStdout {
226 fn from_inner(pipe: AnonPipe) -> ChildStdout {
227 ChildStdout { inner: pipe }
231 #[stable(feature = "std_debug", since = "1.16.0")]
232 impl fmt::Debug for ChildStdout {
233 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234 f.pad("ChildStdout { .. }")
238 /// A handle to a child process's stderr.
240 /// This struct is used in the [`stderr`] field on [`Child`].
242 /// [`Child`]: struct.Child.html
243 /// [`stderr`]: struct.Child.html#structfield.stderr
244 #[stable(feature = "process", since = "1.0.0")]
245 pub struct ChildStderr {
249 #[stable(feature = "process", since = "1.0.0")]
250 impl Read for ChildStderr {
251 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
255 unsafe fn initializer(&self) -> Initializer {
260 impl AsInner<AnonPipe> for ChildStderr {
261 fn as_inner(&self) -> &AnonPipe { &self.inner }
264 impl IntoInner<AnonPipe> for ChildStderr {
265 fn into_inner(self) -> AnonPipe { self.inner }
268 impl FromInner<AnonPipe> for ChildStderr {
269 fn from_inner(pipe: AnonPipe) -> ChildStderr {
270 ChildStderr { inner: pipe }
274 #[stable(feature = "std_debug", since = "1.16.0")]
275 impl fmt::Debug for ChildStderr {
276 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
277 f.pad("ChildStderr { .. }")
281 /// A process builder, providing fine-grained control
282 /// over how a new process should be spawned.
284 /// A default configuration can be
285 /// generated using `Command::new(program)`, where `program` gives a path to the
286 /// program to be executed. Additional builder methods allow the configuration
287 /// to be changed (for example, by adding arguments) prior to spawning:
290 /// use std::process::Command;
292 /// let output = if cfg!(target_os = "windows") {
293 /// Command::new("cmd")
294 /// .args(&["/C", "echo hello"])
296 /// .expect("failed to execute process")
298 /// Command::new("sh")
300 /// .arg("echo hello")
302 /// .expect("failed to execute process")
305 /// let hello = output.stdout;
307 #[stable(feature = "process", since = "1.0.0")]
313 /// Constructs a new `Command` for launching the program at
314 /// path `program`, with the following default configuration:
316 /// * No arguments to the program
317 /// * Inherit the current process's environment
318 /// * Inherit the current process's working directory
319 /// * Inherit stdin/stdout/stderr for `spawn` or `status`, but create pipes for `output`
321 /// Builder methods are provided to change these defaults and
322 /// otherwise configure the process.
324 /// If `program` is not an absolute path, the `PATH` will be searched in
325 /// an OS-defined way.
327 /// The search path to be used may be controlled by setting the
328 /// `PATH` environment variable on the Command,
329 /// but this has some implementation limitations on Windows
330 /// (see https://github.com/rust-lang/rust/issues/37519).
337 /// use std::process::Command;
339 /// Command::new("sh")
341 /// .expect("sh command failed to start");
343 #[stable(feature = "process", since = "1.0.0")]
344 pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
345 Command { inner: imp::Command::new(program.as_ref()) }
348 /// Add an argument to pass to the program.
350 /// Only one argument can be passed per use. So instead of:
353 /// # std::process::Command::new("sh")
354 /// .arg("-C /path/to/repo")
361 /// # std::process::Command::new("sh")
363 /// .arg("/path/to/repo")
367 /// To pass multiple arguments see [`args`].
369 /// [`args`]: #method.args
376 /// use std::process::Command;
378 /// Command::new("ls")
382 /// .expect("ls command failed to start");
384 #[stable(feature = "process", since = "1.0.0")]
385 pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
386 self.inner.arg(arg.as_ref());
390 /// Add multiple arguments to pass to the program.
392 /// To pass a single argument see [`arg`].
394 /// [`arg`]: #method.arg
401 /// use std::process::Command;
403 /// Command::new("ls")
404 /// .args(&["-l", "-a"])
406 /// .expect("ls command failed to start");
408 #[stable(feature = "process", since = "1.0.0")]
409 pub fn args<I, S>(&mut self, args: I) -> &mut Command
410 where I: IntoIterator<Item=S>, S: AsRef<OsStr>
413 self.arg(arg.as_ref());
418 /// Inserts or updates an environment variable mapping.
420 /// Note that environment variable names are case-insensitive (but case-preserving) on Windows,
421 /// and case-sensitive on all other platforms.
428 /// use std::process::Command;
430 /// Command::new("ls")
431 /// .env("PATH", "/bin")
433 /// .expect("ls command failed to start");
435 #[stable(feature = "process", since = "1.0.0")]
436 pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
437 where K: AsRef<OsStr>, V: AsRef<OsStr>
439 self.inner.env(key.as_ref(), val.as_ref());
443 /// Add or update multiple environment variable mappings.
450 /// use std::process::{Command, Stdio};
452 /// use std::collections::HashMap;
454 /// let filtered_env : HashMap<String, String> =
455 /// env::vars().filter(|&(ref k, _)|
456 /// k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH"
459 /// Command::new("printenv")
460 /// .stdin(Stdio::null())
461 /// .stdout(Stdio::inherit())
463 /// .envs(&filtered_env)
465 /// .expect("printenv failed to start");
467 #[stable(feature = "command_envs", since = "1.19.0")]
468 pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
469 where I: IntoIterator<Item=(K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr>
471 for (ref key, ref val) in vars {
472 self.inner.env(key.as_ref(), val.as_ref());
477 /// Removes an environment variable mapping.
484 /// use std::process::Command;
486 /// Command::new("ls")
487 /// .env_remove("PATH")
489 /// .expect("ls command failed to start");
491 #[stable(feature = "process", since = "1.0.0")]
492 pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
493 self.inner.env_remove(key.as_ref());
497 /// Clears the entire environment map for the child process.
504 /// use std::process::Command;
506 /// Command::new("ls")
509 /// .expect("ls command failed to start");
511 #[stable(feature = "process", since = "1.0.0")]
512 pub fn env_clear(&mut self) -> &mut Command {
513 self.inner.env_clear();
517 /// Sets the working directory for the child process.
524 /// use std::process::Command;
526 /// Command::new("ls")
527 /// .current_dir("/bin")
529 /// .expect("ls command failed to start");
531 #[stable(feature = "process", since = "1.0.0")]
532 pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command {
533 self.inner.cwd(dir.as_ref().as_ref());
537 /// Configuration for the child process's stdin handle (file descriptor 0).
544 /// use std::process::{Command, Stdio};
546 /// Command::new("ls")
547 /// .stdin(Stdio::null())
549 /// .expect("ls command failed to start");
551 #[stable(feature = "process", since = "1.0.0")]
552 pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
553 self.inner.stdin(cfg.into().0);
557 /// Configuration for the child process's stdout handle (file descriptor 1).
564 /// use std::process::{Command, Stdio};
566 /// Command::new("ls")
567 /// .stdout(Stdio::null())
569 /// .expect("ls command failed to start");
571 #[stable(feature = "process", since = "1.0.0")]
572 pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
573 self.inner.stdout(cfg.into().0);
577 /// Configuration for the child process's stderr handle (file descriptor 2).
584 /// use std::process::{Command, Stdio};
586 /// Command::new("ls")
587 /// .stderr(Stdio::null())
589 /// .expect("ls command failed to start");
591 #[stable(feature = "process", since = "1.0.0")]
592 pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
593 self.inner.stderr(cfg.into().0);
597 /// Executes the command as a child process, returning a handle to it.
599 /// By default, stdin, stdout and stderr are inherited from the parent.
606 /// use std::process::Command;
608 /// Command::new("ls")
610 /// .expect("ls command failed to start");
612 #[stable(feature = "process", since = "1.0.0")]
613 pub fn spawn(&mut self) -> io::Result<Child> {
614 self.inner.spawn(imp::Stdio::Inherit, true).map(Child::from_inner)
617 /// Executes the command as a child process, waiting for it to finish and
618 /// collecting all of its output.
620 /// By default, stdin, stdout and stderr are captured (and used to
621 /// provide the resulting output).
626 /// use std::process::Command;
627 /// let output = Command::new("/bin/cat")
630 /// .expect("failed to execute process");
632 /// println!("status: {}", output.status);
633 /// println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
634 /// println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
636 /// assert!(output.status.success());
638 #[stable(feature = "process", since = "1.0.0")]
639 pub fn output(&mut self) -> io::Result<Output> {
640 self.inner.spawn(imp::Stdio::MakePipe, false).map(Child::from_inner)
641 .and_then(|p| p.wait_with_output())
644 /// Executes a command as a child process, waiting for it to finish and
645 /// collecting its exit status.
647 /// By default, stdin, stdout and stderr are inherited from the parent.
652 /// use std::process::Command;
654 /// let status = Command::new("/bin/cat")
657 /// .expect("failed to execute process");
659 /// println!("process exited with: {}", status);
661 /// assert!(status.success());
663 #[stable(feature = "process", since = "1.0.0")]
664 pub fn status(&mut self) -> io::Result<ExitStatus> {
665 self.inner.spawn(imp::Stdio::Inherit, true).map(Child::from_inner)
666 .and_then(|mut p| p.wait())
670 #[stable(feature = "rust1", since = "1.0.0")]
671 impl fmt::Debug for Command {
672 /// Format the program and arguments of a Command for display. Any
673 /// non-utf8 data is lossily converted using the utf8 replacement
675 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
680 impl AsInner<imp::Command> for Command {
681 fn as_inner(&self) -> &imp::Command { &self.inner }
684 impl AsInnerMut<imp::Command> for Command {
685 fn as_inner_mut(&mut self) -> &mut imp::Command { &mut self.inner }
688 /// The output of a finished process.
689 #[derive(PartialEq, Eq, Clone)]
690 #[stable(feature = "process", since = "1.0.0")]
692 /// The status (exit code) of the process.
693 #[stable(feature = "process", since = "1.0.0")]
694 pub status: ExitStatus,
695 /// The data that the process wrote to stdout.
696 #[stable(feature = "process", since = "1.0.0")]
698 /// The data that the process wrote to stderr.
699 #[stable(feature = "process", since = "1.0.0")]
703 // If either stderr or stdout are valid utf8 strings it prints the valid
704 // strings, otherwise it prints the byte sequence instead
705 #[stable(feature = "process_output_debug", since = "1.7.0")]
706 impl fmt::Debug for Output {
707 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
709 let stdout_utf8 = str::from_utf8(&self.stdout);
710 let stdout_debug: &fmt::Debug = match stdout_utf8 {
712 Err(_) => &self.stdout
715 let stderr_utf8 = str::from_utf8(&self.stderr);
716 let stderr_debug: &fmt::Debug = match stderr_utf8 {
718 Err(_) => &self.stderr
721 fmt.debug_struct("Output")
722 .field("status", &self.status)
723 .field("stdout", stdout_debug)
724 .field("stderr", stderr_debug)
729 /// Describes what to do with a standard I/O stream for a child process.
730 #[stable(feature = "process", since = "1.0.0")]
731 pub struct Stdio(imp::Stdio);
734 /// A new pipe should be arranged to connect the parent and child processes.
735 #[stable(feature = "process", since = "1.0.0")]
736 pub fn piped() -> Stdio { Stdio(imp::Stdio::MakePipe) }
738 /// The child inherits from the corresponding parent descriptor.
739 #[stable(feature = "process", since = "1.0.0")]
740 pub fn inherit() -> Stdio { Stdio(imp::Stdio::Inherit) }
742 /// This stream will be ignored. This is the equivalent of attaching the
743 /// stream to `/dev/null`
744 #[stable(feature = "process", since = "1.0.0")]
745 pub fn null() -> Stdio { Stdio(imp::Stdio::Null) }
748 impl FromInner<imp::Stdio> for Stdio {
749 fn from_inner(inner: imp::Stdio) -> Stdio {
754 #[stable(feature = "std_debug", since = "1.16.0")]
755 impl fmt::Debug for Stdio {
756 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
757 f.pad("Stdio { .. }")
761 #[stable(feature = "stdio_from", since = "1.20.0")]
762 impl From<ChildStdin> for Stdio {
763 fn from(child: ChildStdin) -> Stdio {
764 Stdio::from_inner(child.into_inner().into())
768 #[stable(feature = "stdio_from", since = "1.20.0")]
769 impl From<ChildStdout> for Stdio {
770 fn from(child: ChildStdout) -> Stdio {
771 Stdio::from_inner(child.into_inner().into())
775 #[stable(feature = "stdio_from", since = "1.20.0")]
776 impl From<ChildStderr> for Stdio {
777 fn from(child: ChildStderr) -> Stdio {
778 Stdio::from_inner(child.into_inner().into())
782 #[stable(feature = "stdio_from", since = "1.20.0")]
783 impl From<fs::File> for Stdio {
784 fn from(file: fs::File) -> Stdio {
785 Stdio::from_inner(file.into_inner().into())
789 /// Describes the result of a process after it has terminated.
791 /// This `struct` is used to represent the exit status of a child process.
792 /// Child processes are created via the [`Command`] struct and their exit
793 /// status is exposed through the [`status`] method.
795 /// [`Command`]: struct.Command.html
796 /// [`status`]: struct.Command.html#method.status
797 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
798 #[stable(feature = "process", since = "1.0.0")]
799 pub struct ExitStatus(imp::ExitStatus);
802 /// Was termination successful? Signal termination not considered a success,
803 /// and success is defined as a zero exit status.
808 /// use std::process::Command;
810 /// let status = Command::new("mkdir")
813 /// .expect("failed to execute mkdir");
815 /// if status.success() {
816 /// println!("'projects/' directory created");
818 /// println!("failed to create 'projects/' directory");
821 #[stable(feature = "process", since = "1.0.0")]
822 pub fn success(&self) -> bool {
826 /// Returns the exit code of the process, if any.
828 /// On Unix, this will return `None` if the process was terminated
829 /// by a signal; `std::os::unix` provides an extension trait for
830 /// extracting the signal and other details from the `ExitStatus`.
835 /// use std::process::Command;
837 /// let status = Command::new("mkdir")
840 /// .expect("failed to execute mkdir");
842 /// match status.code() {
843 /// Some(code) => println!("Exited with status code: {}", code),
844 /// None => println!("Process terminated by signal")
847 #[stable(feature = "process", since = "1.0.0")]
848 pub fn code(&self) -> Option<i32> {
853 impl AsInner<imp::ExitStatus> for ExitStatus {
854 fn as_inner(&self) -> &imp::ExitStatus { &self.0 }
857 impl FromInner<imp::ExitStatus> for ExitStatus {
858 fn from_inner(s: imp::ExitStatus) -> ExitStatus {
863 #[stable(feature = "process", since = "1.0.0")]
864 impl fmt::Display for ExitStatus {
865 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
871 /// Forces the child to exit. This is equivalent to sending a
872 /// SIGKILL on unix platforms.
879 /// use std::process::Command;
881 /// let mut command = Command::new("yes");
882 /// if let Ok(mut child) = command.spawn() {
883 /// child.kill().expect("command wasn't running");
885 /// println!("yes command didn't start");
888 #[stable(feature = "process", since = "1.0.0")]
889 pub fn kill(&mut self) -> io::Result<()> {
893 /// Returns the OS-assigned process identifier associated with this child.
900 /// use std::process::Command;
902 /// let mut command = Command::new("ls");
903 /// if let Ok(child) = command.spawn() {
904 /// println!("Child's id is {}", child.id());
906 /// println!("ls command didn't start");
909 #[stable(feature = "process_id", since = "1.3.0")]
910 pub fn id(&self) -> u32 {
914 /// Waits for the child to exit completely, returning the status that it
915 /// exited with. This function will continue to have the same return value
916 /// after it has been called at least once.
918 /// The stdin handle to the child process, if any, will be closed
919 /// before waiting. This helps avoid deadlock: it ensures that the
920 /// child does not block waiting for input from the parent, while
921 /// the parent waits for the child to exit.
928 /// use std::process::Command;
930 /// let mut command = Command::new("ls");
931 /// if let Ok(mut child) = command.spawn() {
932 /// child.wait().expect("command wasn't running");
933 /// println!("Child has finished its execution!");
935 /// println!("ls command didn't start");
938 #[stable(feature = "process", since = "1.0.0")]
939 pub fn wait(&mut self) -> io::Result<ExitStatus> {
940 drop(self.stdin.take());
941 self.handle.wait().map(ExitStatus)
944 /// Attempts to collect the exit status of the child if it has already
947 /// This function will not block the calling thread and will only advisorily
948 /// check to see if the child process has exited or not. If the child has
949 /// exited then on Unix the process id is reaped. This function is
950 /// guaranteed to repeatedly return a successful exit status so long as the
951 /// child has already exited.
953 /// If the child has exited, then `Ok(Some(status))` is returned. If the
954 /// exit status is not available at this time then `Ok(None)` is returned.
955 /// If an error occurs, then that error is returned.
957 /// Note that unlike `wait`, this function will not attempt to drop stdin.
964 /// use std::process::Command;
966 /// let mut child = Command::new("ls").spawn().unwrap();
968 /// match child.try_wait() {
969 /// Ok(Some(status)) => println!("exited with: {}", status),
971 /// println!("status not ready yet, let's really wait");
972 /// let res = child.wait();
973 /// println!("result: {:?}", res);
975 /// Err(e) => println!("error attempting to wait: {}", e),
978 #[stable(feature = "process_try_wait", since = "1.18.0")]
979 pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
980 Ok(self.handle.try_wait()?.map(ExitStatus))
983 /// Simultaneously waits for the child to exit and collect all remaining
984 /// output on the stdout/stderr handles, returning an `Output`
987 /// The stdin handle to the child process, if any, will be closed
988 /// before waiting. This helps avoid deadlock: it ensures that the
989 /// child does not block waiting for input from the parent, while
990 /// the parent waits for the child to exit.
992 /// By default, stdin, stdout and stderr are inherited from the parent.
993 /// In order to capture the output into this `Result<Output>` it is
994 /// necessary to create new pipes between parent and child. Use
995 /// `stdout(Stdio::piped())` or `stderr(Stdio::piped())`, respectively.
1000 /// use std::process::{Command, Stdio};
1002 /// let child = Command::new("/bin/cat")
1003 /// .arg("file.txt")
1004 /// .stdout(Stdio::piped())
1006 /// .expect("failed to execute child");
1008 /// let output = child
1009 /// .wait_with_output()
1010 /// .expect("failed to wait on child");
1012 /// assert!(output.status.success());
1015 #[stable(feature = "process", since = "1.0.0")]
1016 pub fn wait_with_output(mut self) -> io::Result<Output> {
1017 drop(self.stdin.take());
1019 let (mut stdout, mut stderr) = (Vec::new(), Vec::new());
1020 match (self.stdout.take(), self.stderr.take()) {
1022 (Some(mut out), None) => {
1023 let res = out.read_to_end(&mut stdout);
1026 (None, Some(mut err)) => {
1027 let res = err.read_to_end(&mut stderr);
1030 (Some(out), Some(err)) => {
1031 let res = read2(out.inner, &mut stdout, err.inner, &mut stderr);
1036 let status = self.wait()?;
1045 /// Terminates the current process with the specified exit code.
1047 /// This function will never return and will immediately terminate the current
1048 /// process. The exit code is passed through to the underlying OS and will be
1049 /// available for consumption by another process.
1051 /// Note that because this function never returns, and that it terminates the
1052 /// process, no destructors on the current stack or any other thread's stack
1053 /// will be run. If a clean shutdown is needed it is recommended to only call
1054 /// this function at a known point where there are no more destructors left
1057 /// ## Platform-specific behavior
1059 /// **Unix**: On Unix-like platforms, it is unlikely that all 32 bits of `exit`
1060 /// will be visible to a parent process inspecting the exit code. On most
1061 /// Unix-like platforms, only the eight least-significant bits are considered.
1065 /// Due to this function’s behavior regarding destructors, a conventional way
1066 /// to use the function is to extract the actual computation to another
1067 /// function and compute the exit code from its return value:
1070 /// use std::io::{self, Write};
1072 /// fn run_app() -> Result<(), ()> {
1073 /// // Application logic here
1078 /// ::std::process::exit(match run_app() {
1081 /// writeln!(io::stderr(), "error: {:?}", err).unwrap();
1088 /// Due to [platform-specific behavior], the exit code for this example will be
1089 /// `0` on Linux, but `256` on Windows:
1092 /// use std::process;
1094 /// process::exit(0x0100);
1097 /// [platform-specific behavior]: #platform-specific-behavior
1098 #[stable(feature = "rust1", since = "1.0.0")]
1099 pub fn exit(code: i32) -> ! {
1100 ::sys_common::cleanup();
1101 ::sys::os::exit(code)
1104 /// Terminates the process in an abnormal fashion.
1106 /// The function will never return and will immediately terminate the current
1107 /// process in a platform specific "abnormal" manner.
1109 /// Note that because this function never returns, and that it terminates the
1110 /// process, no destructors on the current stack or any other thread's stack
1111 /// will be run. If a clean shutdown is needed it is recommended to only call
1112 /// this function at a known point where there are no more destructors left
1118 /// use std::process;
1121 /// println!("aborting");
1123 /// process::abort();
1125 /// // execution never gets here
1129 /// The [`abort`] function terminates the process, so the destructor will not
1130 /// get run on the example below:
1133 /// use std::process;
1137 /// impl Drop for HasDrop {
1138 /// fn drop(&mut self) {
1139 /// println!("This will never be printed!");
1144 /// let _x = HasDrop;
1145 /// process::abort();
1146 /// // the destructor implemented for HasDrop will never get run
1149 #[stable(feature = "process_abort", since = "1.17.0")]
1150 pub fn abort() -> ! {
1151 unsafe { ::sys::abort_internal() };
1154 #[cfg(all(test, not(target_os = "emscripten")))]
1160 use super::{Command, Output, Stdio};
1162 // FIXME(#10380) these tests should not all be ignored on android.
1165 #[cfg_attr(target_os = "android", ignore)]
1167 let p = if cfg!(target_os = "windows") {
1168 Command::new("cmd").args(&["/C", "exit 0"]).spawn()
1170 Command::new("true").spawn()
1173 let mut p = p.unwrap();
1174 assert!(p.wait().unwrap().success());
1178 #[cfg_attr(target_os = "android", ignore)]
1179 fn smoke_failure() {
1180 match Command::new("if-this-is-a-binary-then-the-world-has-ended").spawn() {
1187 #[cfg_attr(target_os = "android", ignore)]
1188 fn exit_reported_right() {
1189 let p = if cfg!(target_os = "windows") {
1190 Command::new("cmd").args(&["/C", "exit 1"]).spawn()
1192 Command::new("false").spawn()
1195 let mut p = p.unwrap();
1196 assert!(p.wait().unwrap().code() == Some(1));
1202 #[cfg_attr(target_os = "android", ignore)]
1203 fn signal_reported_right() {
1204 use os::unix::process::ExitStatusExt;
1206 let mut p = Command::new("/bin/sh")
1207 .arg("-c").arg("read a")
1208 .stdin(Stdio::piped())
1211 match p.wait().unwrap().signal() {
1213 result => panic!("not terminated by signal 9 (instead, {:?})",
1218 pub fn run_output(mut cmd: Command) -> String {
1219 let p = cmd.spawn();
1221 let mut p = p.unwrap();
1222 assert!(p.stdout.is_some());
1223 let mut ret = String::new();
1224 p.stdout.as_mut().unwrap().read_to_string(&mut ret).unwrap();
1225 assert!(p.wait().unwrap().success());
1230 #[cfg_attr(target_os = "android", ignore)]
1232 if cfg!(target_os = "windows") {
1233 let mut cmd = Command::new("cmd");
1234 cmd.args(&["/C", "echo foobar"]).stdout(Stdio::piped());
1235 assert_eq!(run_output(cmd), "foobar\r\n");
1237 let mut cmd = Command::new("echo");
1238 cmd.arg("foobar").stdout(Stdio::piped());
1239 assert_eq!(run_output(cmd), "foobar\n");
1244 #[cfg_attr(any(windows, target_os = "android"), ignore)]
1245 fn set_current_dir_works() {
1246 let mut cmd = Command::new("/bin/sh");
1247 cmd.arg("-c").arg("pwd")
1249 .stdout(Stdio::piped());
1250 assert_eq!(run_output(cmd), "/\n");
1254 #[cfg_attr(any(windows, target_os = "android"), ignore)]
1256 let mut p = Command::new("/bin/sh")
1257 .arg("-c").arg("read line; echo $line")
1258 .stdin(Stdio::piped())
1259 .stdout(Stdio::piped())
1261 p.stdin.as_mut().unwrap().write("foobar".as_bytes()).unwrap();
1262 drop(p.stdin.take());
1263 let mut out = String::new();
1264 p.stdout.as_mut().unwrap().read_to_string(&mut out).unwrap();
1265 assert!(p.wait().unwrap().success());
1266 assert_eq!(out, "foobar\n");
1271 #[cfg_attr(target_os = "android", ignore)]
1274 use os::unix::prelude::*;
1276 let mut p = Command::new("/bin/sh")
1277 .arg("-c").arg("true")
1278 .uid(unsafe { libc::getuid() })
1279 .gid(unsafe { libc::getgid() })
1281 assert!(p.wait().unwrap().success());
1285 #[cfg_attr(target_os = "android", ignore)]
1287 fn uid_to_root_fails() {
1288 use os::unix::prelude::*;
1291 // if we're already root, this isn't a valid test. Most of the bots run
1292 // as non-root though (android is an exception).
1293 if unsafe { libc::getuid() == 0 } { return }
1294 assert!(Command::new("/bin/ls").uid(0).gid(0).spawn().is_err());
1298 #[cfg_attr(target_os = "android", ignore)]
1299 fn test_process_status() {
1300 let mut status = if cfg!(target_os = "windows") {
1301 Command::new("cmd").args(&["/C", "exit 1"]).status().unwrap()
1303 Command::new("false").status().unwrap()
1305 assert!(status.code() == Some(1));
1307 status = if cfg!(target_os = "windows") {
1308 Command::new("cmd").args(&["/C", "exit 0"]).status().unwrap()
1310 Command::new("true").status().unwrap()
1312 assert!(status.success());
1316 fn test_process_output_fail_to_start() {
1317 match Command::new("/no-binary-by-this-name-should-exist").output() {
1318 Err(e) => assert_eq!(e.kind(), ErrorKind::NotFound),
1324 #[cfg_attr(target_os = "android", ignore)]
1325 fn test_process_output_output() {
1326 let Output {status, stdout, stderr}
1327 = if cfg!(target_os = "windows") {
1328 Command::new("cmd").args(&["/C", "echo hello"]).output().unwrap()
1330 Command::new("echo").arg("hello").output().unwrap()
1332 let output_str = str::from_utf8(&stdout).unwrap();
1334 assert!(status.success());
1335 assert_eq!(output_str.trim().to_string(), "hello");
1336 assert_eq!(stderr, Vec::new());
1340 #[cfg_attr(target_os = "android", ignore)]
1341 fn test_process_output_error() {
1342 let Output {status, stdout, stderr}
1343 = if cfg!(target_os = "windows") {
1344 Command::new("cmd").args(&["/C", "mkdir ."]).output().unwrap()
1346 Command::new("mkdir").arg(".").output().unwrap()
1349 assert!(status.code() == Some(1));
1350 assert_eq!(stdout, Vec::new());
1351 assert!(!stderr.is_empty());
1355 #[cfg_attr(target_os = "android", ignore)]
1356 fn test_finish_once() {
1357 let mut prog = if cfg!(target_os = "windows") {
1358 Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
1360 Command::new("false").spawn().unwrap()
1362 assert!(prog.wait().unwrap().code() == Some(1));
1366 #[cfg_attr(target_os = "android", ignore)]
1367 fn test_finish_twice() {
1368 let mut prog = if cfg!(target_os = "windows") {
1369 Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
1371 Command::new("false").spawn().unwrap()
1373 assert!(prog.wait().unwrap().code() == Some(1));
1374 assert!(prog.wait().unwrap().code() == Some(1));
1378 #[cfg_attr(target_os = "android", ignore)]
1379 fn test_wait_with_output_once() {
1380 let prog = if cfg!(target_os = "windows") {
1381 Command::new("cmd").args(&["/C", "echo hello"]).stdout(Stdio::piped()).spawn().unwrap()
1383 Command::new("echo").arg("hello").stdout(Stdio::piped()).spawn().unwrap()
1386 let Output {status, stdout, stderr} = prog.wait_with_output().unwrap();
1387 let output_str = str::from_utf8(&stdout).unwrap();
1389 assert!(status.success());
1390 assert_eq!(output_str.trim().to_string(), "hello");
1391 assert_eq!(stderr, Vec::new());
1394 #[cfg(all(unix, not(target_os="android")))]
1395 pub fn env_cmd() -> Command {
1398 #[cfg(target_os="android")]
1399 pub fn env_cmd() -> Command {
1400 let mut cmd = Command::new("/system/bin/sh");
1401 cmd.arg("-c").arg("set");
1406 pub fn env_cmd() -> Command {
1407 let mut cmd = Command::new("cmd");
1408 cmd.arg("/c").arg("set");
1413 fn test_inherit_env() {
1416 let result = env_cmd().output().unwrap();
1417 let output = String::from_utf8(result.stdout).unwrap();
1419 for (ref k, ref v) in env::vars() {
1420 // don't check android RANDOM variables
1421 if cfg!(target_os = "android") && *k == "RANDOM" {
1425 // Windows has hidden environment variables whose names start with
1426 // equals signs (`=`). Those do not show up in the output of the
1428 assert!((cfg!(windows) && k.starts_with("=")) ||
1429 k.starts_with("DYLD") ||
1430 output.contains(&format!("{}={}", *k, *v)) ||
1431 output.contains(&format!("{}='{}'", *k, *v)),
1432 "output doesn't contain `{}={}`\n{}",
1438 fn test_override_env() {
1441 // In some build environments (such as chrooted Nix builds), `env` can
1442 // only be found in the explicitly-provided PATH env variable, not in
1443 // default places such as /bin or /usr/bin. So we need to pass through
1444 // PATH to our sub-process.
1445 let mut cmd = env_cmd();
1446 cmd.env_clear().env("RUN_TEST_NEW_ENV", "123");
1447 if let Some(p) = env::var_os("PATH") {
1448 cmd.env("PATH", &p);
1450 let result = cmd.output().unwrap();
1451 let output = String::from_utf8_lossy(&result.stdout).to_string();
1453 assert!(output.contains("RUN_TEST_NEW_ENV=123"),
1454 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
1458 fn test_add_to_env() {
1459 let result = env_cmd().env("RUN_TEST_NEW_ENV", "123").output().unwrap();
1460 let output = String::from_utf8_lossy(&result.stdout).to_string();
1462 assert!(output.contains("RUN_TEST_NEW_ENV=123"),
1463 "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
1466 // Regression tests for #30858.
1468 fn test_interior_nul_in_progname_is_error() {
1469 match Command::new("has-some-\0\0s-inside").spawn() {
1470 Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
1476 fn test_interior_nul_in_arg_is_error() {
1477 match Command::new("echo").arg("has-some-\0\0s-inside").spawn() {
1478 Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
1484 fn test_interior_nul_in_args_is_error() {
1485 match Command::new("echo").args(&["has-some-\0\0s-inside"]).spawn() {
1486 Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
1492 fn test_interior_nul_in_current_dir_is_error() {
1493 match Command::new("echo").current_dir("has-some-\0\0s-inside").spawn() {
1494 Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
1499 // Regression tests for #30862.
1501 fn test_interior_nul_in_env_key_is_error() {
1502 match env_cmd().env("has-some-\0\0s-inside", "value").spawn() {
1503 Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
1509 fn test_interior_nul_in_env_value_is_error() {
1510 match env_cmd().env("key", "has-some-\0\0s-inside").spawn() {
1511 Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
1516 /// Test that process creation flags work by debugging a process.
1517 /// Other creation flags make it hard or impossible to detect
1518 /// behavioral changes in the process.
1521 fn test_creation_flags() {
1522 use os::windows::process::CommandExt;
1523 use sys::c::{BOOL, DWORD, INFINITE};
1525 struct DEBUG_EVENT {
1526 pub event_code: DWORD,
1527 pub process_id: DWORD,
1528 pub thread_id: DWORD,
1529 // This is a union in the real struct, but we don't
1530 // need this data for the purposes of this test.
1531 pub _junk: [u8; 164],
1535 fn WaitForDebugEvent(lpDebugEvent: *mut DEBUG_EVENT, dwMilliseconds: DWORD) -> BOOL;
1536 fn ContinueDebugEvent(dwProcessId: DWORD, dwThreadId: DWORD,
1537 dwContinueStatus: DWORD) -> BOOL;
1540 const DEBUG_PROCESS: DWORD = 1;
1541 const EXIT_PROCESS_DEBUG_EVENT: DWORD = 5;
1542 const DBG_EXCEPTION_NOT_HANDLED: DWORD = 0x80010001;
1544 let mut child = Command::new("cmd")
1545 .creation_flags(DEBUG_PROCESS)
1546 .stdin(Stdio::piped()).spawn().unwrap();
1547 child.stdin.take().unwrap().write_all(b"exit\r\n").unwrap();
1549 let mut event = DEBUG_EVENT {
1556 if unsafe { WaitForDebugEvent(&mut event as *mut DEBUG_EVENT, INFINITE) } == 0 {
1557 panic!("WaitForDebugEvent failed!");
1561 if event.event_code == EXIT_PROCESS_DEBUG_EVENT {
1565 if unsafe { ContinueDebugEvent(event.process_id,
1567 DBG_EXCEPTION_NOT_HANDLED) } == 0 {
1568 panic!("ContinueDebugEvent failed!");
1571 assert!(events > 0);