2016-09-29 22:10:42 +00:00
|
|
|
|
//! A module for working with processes.
|
|
|
|
|
//!
|
2017-10-16 02:45:07 +00:00
|
|
|
|
//! This module is mostly concerned with spawning and interacting with child
|
|
|
|
|
//! processes, but it also provides [`abort`] and [`exit`] for terminating the
|
|
|
|
|
//! current process.
|
2016-09-29 22:10:42 +00:00
|
|
|
|
//!
|
2017-10-16 02:45:07 +00:00
|
|
|
|
//! # Spawning a process
|
2016-09-29 22:10:42 +00:00
|
|
|
|
//!
|
2017-10-16 02:45:07 +00:00
|
|
|
|
//! The [`Command`] struct is used to configure and spawn processes:
|
2016-09-29 22:10:42 +00:00
|
|
|
|
//!
|
2019-03-14 04:53:44 +00:00
|
|
|
|
//! ```no_run
|
2016-09-29 22:10:42 +00:00
|
|
|
|
//! use std::process::Command;
|
|
|
|
|
//!
|
2017-10-16 02:45:07 +00:00
|
|
|
|
//! let output = Command::new("echo")
|
|
|
|
|
//! .arg("Hello world")
|
|
|
|
|
//! .output()
|
|
|
|
|
//! .expect("Failed to execute command");
|
2016-09-29 22:10:42 +00:00
|
|
|
|
//!
|
2017-10-16 02:45:07 +00:00
|
|
|
|
//! assert_eq!(b"Hello world\n", output.stdout.as_slice());
|
2016-09-29 22:10:42 +00:00
|
|
|
|
//! ```
|
2017-02-27 06:01:47 +00:00
|
|
|
|
//!
|
2017-10-16 02:45:07 +00:00
|
|
|
|
//! Several methods on [`Command`], such as [`spawn`] or [`output`], can be used
|
|
|
|
|
//! to spawn a process. In particular, [`output`] spawns the child process and
|
|
|
|
|
//! waits until the process terminates, while [`spawn`] will return a [`Child`]
|
|
|
|
|
//! that represents the spawned child process.
|
|
|
|
|
//!
|
|
|
|
|
//! # Handling I/O
|
|
|
|
|
//!
|
2017-10-18 00:49:02 +00:00
|
|
|
|
//! The [`stdout`], [`stdin`], and [`stderr`] of a child process can be
|
|
|
|
|
//! configured by passing an [`Stdio`] to the corresponding method on
|
|
|
|
|
//! [`Command`]. Once spawned, they can be accessed from the [`Child`]. For
|
|
|
|
|
//! example, piping output from one command into another command can be done
|
|
|
|
|
//! like so:
|
2017-10-14 06:38:55 +00:00
|
|
|
|
//!
|
2017-10-29 03:24:49 +00:00
|
|
|
|
//! ```no_run
|
2017-10-14 06:38:55 +00:00
|
|
|
|
//! use std::process::{Command, Stdio};
|
2016-09-29 22:10:42 +00:00
|
|
|
|
//!
|
2017-10-14 06:38:55 +00:00
|
|
|
|
//! // stdout must be configured with `Stdio::piped` in order to use
|
|
|
|
|
//! // `echo_child.stdout`
|
|
|
|
|
//! let echo_child = Command::new("echo")
|
|
|
|
|
//! .arg("Oh no, a tpyo!")
|
|
|
|
|
//! .stdout(Stdio::piped())
|
|
|
|
|
//! .spawn()
|
|
|
|
|
//! .expect("Failed to start echo process");
|
|
|
|
|
//!
|
|
|
|
|
//! // Note that `echo_child` is moved here, but we won't be needing
|
|
|
|
|
//! // `echo_child` anymore
|
|
|
|
|
//! let echo_out = echo_child.stdout.expect("Failed to open echo stdout");
|
|
|
|
|
//!
|
|
|
|
|
//! let mut sed_child = Command::new("sed")
|
|
|
|
|
//! .arg("s/tpyo/typo/")
|
|
|
|
|
//! .stdin(Stdio::from(echo_out))
|
|
|
|
|
//! .stdout(Stdio::piped())
|
|
|
|
|
//! .spawn()
|
|
|
|
|
//! .expect("Failed to start sed process");
|
2016-09-29 22:10:42 +00:00
|
|
|
|
//!
|
2017-10-14 06:38:55 +00:00
|
|
|
|
//! let output = sed_child.wait_with_output().expect("Failed to wait on sed");
|
|
|
|
|
//! assert_eq!(b"Oh no, a typo!\n", output.stdout.as_slice());
|
2016-09-29 22:10:42 +00:00
|
|
|
|
//! ```
|
2017-02-27 06:01:47 +00:00
|
|
|
|
//!
|
2018-01-04 21:01:57 +00:00
|
|
|
|
//! Note that [`ChildStderr`] and [`ChildStdout`] implement [`Read`] and
|
|
|
|
|
//! [`ChildStdin`] implements [`Write`]:
|
2017-02-27 06:01:47 +00:00
|
|
|
|
//!
|
|
|
|
|
//! ```no_run
|
|
|
|
|
//! use std::process::{Command, Stdio};
|
|
|
|
|
//! use std::io::Write;
|
|
|
|
|
//!
|
|
|
|
|
//! let mut child = Command::new("/bin/cat")
|
|
|
|
|
//! .stdin(Stdio::piped())
|
|
|
|
|
//! .stdout(Stdio::piped())
|
|
|
|
|
//! .spawn()
|
|
|
|
|
//! .expect("failed to execute child");
|
|
|
|
|
//!
|
|
|
|
|
//! {
|
|
|
|
|
//! // limited borrow of stdin
|
|
|
|
|
//! let stdin = child.stdin.as_mut().expect("failed to get stdin");
|
|
|
|
|
//! stdin.write_all(b"test").expect("failed to write to stdin");
|
|
|
|
|
//! }
|
|
|
|
|
//!
|
|
|
|
|
//! let output = child
|
|
|
|
|
//! .wait_with_output()
|
|
|
|
|
//! .expect("failed to wait on child");
|
|
|
|
|
//!
|
|
|
|
|
//! assert_eq!(b"test", output.stdout.as_slice());
|
|
|
|
|
//! ```
|
2017-10-14 06:38:55 +00:00
|
|
|
|
//!
|
2017-10-16 02:45:07 +00:00
|
|
|
|
//! [`abort`]: fn.abort.html
|
|
|
|
|
//! [`exit`]: fn.exit.html
|
|
|
|
|
//!
|
2017-10-14 06:38:55 +00:00
|
|
|
|
//! [`Command`]: struct.Command.html
|
2017-10-16 02:45:07 +00:00
|
|
|
|
//! [`spawn`]: struct.Command.html#method.spawn
|
|
|
|
|
//! [`output`]: struct.Command.html#method.output
|
|
|
|
|
//!
|
2017-10-14 06:38:55 +00:00
|
|
|
|
//! [`Child`]: struct.Child.html
|
2017-10-18 00:49:02 +00:00
|
|
|
|
//! [`ChildStdin`]: struct.ChildStdin.html
|
|
|
|
|
//! [`ChildStdout`]: struct.ChildStdout.html
|
|
|
|
|
//! [`ChildStderr`]: struct.ChildStderr.html
|
2017-10-16 02:45:07 +00:00
|
|
|
|
//! [`Stdio`]: struct.Stdio.html
|
2017-10-18 00:49:02 +00:00
|
|
|
|
//!
|
|
|
|
|
//! [`stdout`]: struct.Command.html#method.stdout
|
|
|
|
|
//! [`stdin`]: struct.Command.html#method.stdin
|
|
|
|
|
//! [`stderr`]: struct.Command.html#method.stderr
|
|
|
|
|
//!
|
|
|
|
|
//! [`Write`]: ../io/trait.Write.html
|
|
|
|
|
//! [`Read`]: ../io/trait.Read.html
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#![stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::io::prelude::*;
|
|
|
|
|
|
|
|
|
|
use crate::ffi::OsStr;
|
|
|
|
|
use crate::fmt;
|
|
|
|
|
use crate::fs;
|
2019-04-27 15:34:08 +00:00
|
|
|
|
use crate::io::{self, Initializer, IoSlice, IoSliceMut};
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::path::Path;
|
|
|
|
|
use crate::str;
|
|
|
|
|
use crate::sys::pipe::{read2, AnonPipe};
|
|
|
|
|
use crate::sys::process as imp;
|
|
|
|
|
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
|
|
|
|
/// Representation of a running or exited child process.
|
|
|
|
|
///
|
|
|
|
|
/// This structure is used to represent and manage child processes. A child
|
2016-07-09 15:47:12 +00:00
|
|
|
|
/// process is created via the [`Command`] struct, which configures the
|
|
|
|
|
/// spawning process and can itself be constructed using a builder-style
|
|
|
|
|
/// interface.
|
2015-02-06 17:42:57 +00:00
|
|
|
|
///
|
2017-03-25 03:29:23 +00:00
|
|
|
|
/// There is no implementation of [`Drop`] for child processes,
|
|
|
|
|
/// so if you do not ensure the `Child` has exited then it will continue to
|
|
|
|
|
/// run, even after the `Child` handle to the child process has gone out of
|
|
|
|
|
/// scope.
|
|
|
|
|
///
|
2017-04-19 18:00:50 +00:00
|
|
|
|
/// Calling [`wait`](#method.wait) (or other functions that wrap around it) will make
|
2017-03-25 03:29:23 +00:00
|
|
|
|
/// the parent process wait until the child has actually exited before
|
|
|
|
|
/// continuing.
|
|
|
|
|
///
|
2019-05-01 13:57:02 +00:00
|
|
|
|
/// # Warning
|
|
|
|
|
///
|
|
|
|
|
/// On some system, calling [`wait`] or similar is necessary for the OS to
|
|
|
|
|
/// release resources. A process that terminated but has not been waited on is
|
|
|
|
|
/// still around as a "zombie". Leaving too many zombies around may exhaust
|
|
|
|
|
/// global resources (for example process IDs).
|
|
|
|
|
///
|
|
|
|
|
/// The standard library does *not* automatically wait on child processes (not
|
|
|
|
|
/// even if the `Child` is dropped), it is up to the application developer to do
|
|
|
|
|
/// so. As a consequence, dropping `Child` handles without waiting on them first
|
|
|
|
|
/// is not recommended in long-running applications.
|
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-02-06 17:42:57 +00:00
|
|
|
|
///
|
2015-03-26 20:30:33 +00:00
|
|
|
|
/// ```should_panic
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
2015-04-29 21:00:10 +00:00
|
|
|
|
/// let mut child = Command::new("/bin/cat")
|
2015-04-29 22:21:23 +00:00
|
|
|
|
/// .arg("file.txt")
|
|
|
|
|
/// .spawn()
|
2016-03-21 13:17:17 +00:00
|
|
|
|
/// .expect("failed to execute child");
|
2015-04-29 21:00:10 +00:00
|
|
|
|
///
|
|
|
|
|
/// let ecode = child.wait()
|
2016-03-21 13:17:17 +00:00
|
|
|
|
/// .expect("failed to wait on child");
|
2015-04-29 21:00:10 +00:00
|
|
|
|
///
|
|
|
|
|
/// assert!(ecode.success());
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// ```
|
2016-01-31 19:50:22 +00:00
|
|
|
|
///
|
2016-07-09 15:47:12 +00:00
|
|
|
|
/// [`Command`]: struct.Command.html
|
|
|
|
|
/// [`Drop`]: ../../core/ops/trait.Drop.html
|
|
|
|
|
/// [`wait`]: #method.wait
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub struct Child {
|
2015-05-12 18:03:49 +00:00
|
|
|
|
handle: imp::Process,
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
2017-09-16 03:02:50 +00:00
|
|
|
|
/// The handle for writing to the child's standard input (stdin), if it has
|
|
|
|
|
/// been captured.
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub stdin: Option<ChildStdin>,
|
|
|
|
|
|
2017-09-16 03:02:50 +00:00
|
|
|
|
/// The handle for reading from the child's standard output (stdout), if it
|
|
|
|
|
/// has been captured.
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub stdout: Option<ChildStdout>,
|
|
|
|
|
|
2017-09-16 03:02:50 +00:00
|
|
|
|
/// The handle for reading from the child's standard error (stderr), if it
|
|
|
|
|
/// has been captured.
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub stderr: Option<ChildStderr>,
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 18:03:49 +00:00
|
|
|
|
impl AsInner<imp::Process> for Child {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn as_inner(&self) -> &imp::Process {
|
|
|
|
|
&self.handle
|
|
|
|
|
}
|
2015-05-12 18:03:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 19:10:37 +00:00
|
|
|
|
impl FromInner<(imp::Process, imp::StdioPipes)> for Child {
|
|
|
|
|
fn from_inner((handle, io): (imp::Process, imp::StdioPipes)) -> Child {
|
|
|
|
|
Child {
|
2017-08-07 05:54:09 +00:00
|
|
|
|
handle,
|
2016-02-04 19:10:37 +00:00
|
|
|
|
stdin: io.stdin.map(ChildStdin::from_inner),
|
|
|
|
|
stdout: io.stdout.map(ChildStdout::from_inner),
|
|
|
|
|
stderr: io.stderr.map(ChildStderr::from_inner),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-16 06:31:24 +00:00
|
|
|
|
impl IntoInner<imp::Process> for Child {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn into_inner(self) -> imp::Process {
|
|
|
|
|
self.handle
|
|
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for Child {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
|
f.debug_struct("Child")
|
|
|
|
|
.field("stdin", &self.stdin)
|
|
|
|
|
.field("stdout", &self.stdout)
|
|
|
|
|
.field("stderr", &self.stderr)
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 03:02:50 +00:00
|
|
|
|
/// A handle to a child process's standard input (stdin).
|
2017-05-03 05:13:18 +00:00
|
|
|
|
///
|
|
|
|
|
/// This struct is used in the [`stdin`] field on [`Child`].
|
2016-07-09 15:47:12 +00:00
|
|
|
|
///
|
2017-09-16 02:57:12 +00:00
|
|
|
|
/// When an instance of `ChildStdin` is [dropped], the `ChildStdin`'s underlying
|
2017-09-22 01:11:11 +00:00
|
|
|
|
/// file handle will be closed. If the child process was blocked on input prior
|
|
|
|
|
/// to being dropped, it will become unblocked after dropping.
|
2017-09-16 02:57:12 +00:00
|
|
|
|
///
|
2016-07-09 15:47:12 +00:00
|
|
|
|
/// [`Child`]: struct.Child.html
|
|
|
|
|
/// [`stdin`]: struct.Child.html#structfield.stdin
|
2017-09-16 02:57:12 +00:00
|
|
|
|
/// [dropped]: ../ops/trait.Drop.html
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub struct ChildStdin {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
inner: AnonPipe,
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
impl Write for ChildStdin {
|
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
|
self.inner.write(buf)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
|
self.inner.write_vectored(bufs)
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 01:02:52 +00:00
|
|
|
|
fn is_write_vectored(&self) -> bool {
|
|
|
|
|
self.inner.is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 18:03:49 +00:00
|
|
|
|
impl AsInner<AnonPipe> for ChildStdin {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn as_inner(&self) -> &AnonPipe {
|
|
|
|
|
&self.inner
|
|
|
|
|
}
|
2015-05-12 18:03:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-16 06:31:24 +00:00
|
|
|
|
impl IntoInner<AnonPipe> for ChildStdin {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn into_inner(self) -> AnonPipe {
|
|
|
|
|
self.inner
|
|
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 19:10:37 +00:00
|
|
|
|
impl FromInner<AnonPipe> for ChildStdin {
|
|
|
|
|
fn from_inner(pipe: AnonPipe) -> ChildStdin {
|
|
|
|
|
ChildStdin { inner: pipe }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for ChildStdin {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
|
f.pad("ChildStdin { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 03:02:50 +00:00
|
|
|
|
/// A handle to a child process's standard output (stdout).
|
2017-05-03 05:13:18 +00:00
|
|
|
|
///
|
|
|
|
|
/// This struct is used in the [`stdout`] field on [`Child`].
|
2016-07-09 15:47:12 +00:00
|
|
|
|
///
|
2017-09-16 02:57:12 +00:00
|
|
|
|
/// When an instance of `ChildStdout` is [dropped], the `ChildStdout`'s
|
|
|
|
|
/// underlying file handle will be closed.
|
|
|
|
|
///
|
2016-07-09 15:47:12 +00:00
|
|
|
|
/// [`Child`]: struct.Child.html
|
|
|
|
|
/// [`stdout`]: struct.Child.html#structfield.stdout
|
2017-09-16 02:57:12 +00:00
|
|
|
|
/// [dropped]: ../ops/trait.Drop.html
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub struct ChildStdout {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
inner: AnonPipe,
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
impl Read for ChildStdout {
|
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
|
self.inner.read(buf)
|
|
|
|
|
}
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
|
self.inner.read_vectored(bufs)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
|
fn is_read_vectored(&self) -> bool {
|
|
|
|
|
self.inner.is_read_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 01:29:18 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
|
Initializer::nop()
|
2016-02-12 08:17:24 +00:00
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 18:03:49 +00:00
|
|
|
|
impl AsInner<AnonPipe> for ChildStdout {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn as_inner(&self) -> &AnonPipe {
|
|
|
|
|
&self.inner
|
|
|
|
|
}
|
2015-05-12 18:03:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-16 06:31:24 +00:00
|
|
|
|
impl IntoInner<AnonPipe> for ChildStdout {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn into_inner(self) -> AnonPipe {
|
|
|
|
|
self.inner
|
|
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 19:10:37 +00:00
|
|
|
|
impl FromInner<AnonPipe> for ChildStdout {
|
|
|
|
|
fn from_inner(pipe: AnonPipe) -> ChildStdout {
|
|
|
|
|
ChildStdout { inner: pipe }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for ChildStdout {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
|
f.pad("ChildStdout { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 20:23:52 +00:00
|
|
|
|
/// A handle to a child process's stderr.
|
|
|
|
|
///
|
|
|
|
|
/// This struct is used in the [`stderr`] field on [`Child`].
|
2016-07-09 15:47:12 +00:00
|
|
|
|
///
|
2017-09-16 02:57:12 +00:00
|
|
|
|
/// When an instance of `ChildStderr` is [dropped], the `ChildStderr`'s
|
|
|
|
|
/// underlying file handle will be closed.
|
|
|
|
|
///
|
2016-07-09 15:47:12 +00:00
|
|
|
|
/// [`Child`]: struct.Child.html
|
|
|
|
|
/// [`stderr`]: struct.Child.html#structfield.stderr
|
2017-09-16 02:57:12 +00:00
|
|
|
|
/// [dropped]: ../ops/trait.Drop.html
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub struct ChildStderr {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
inner: AnonPipe,
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
impl Read for ChildStderr {
|
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
|
self.inner.read(buf)
|
|
|
|
|
}
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
std: Add `{read,write}_vectored` for more types
This commit implements the `{read,write}_vectored` methods on more types
in the standard library, namely:
* `std::fs::File`
* `std::process::ChildStd{in,out,err}`
* `std::io::Std{in,out,err}`
* `std::io::Std{in,out,err}Lock`
* `std::io::Std{in,out,err}Raw`
Where supported the OS implementations hook up to native support,
otherwise it falls back to the already-defaulted implementation.
2019-04-10 19:51:25 +00:00
|
|
|
|
self.inner.read_vectored(bufs)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
|
fn is_read_vectored(&self) -> bool {
|
|
|
|
|
self.inner.is_read_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 01:29:18 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
|
Initializer::nop()
|
2016-02-12 08:17:24 +00:00
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 18:03:49 +00:00
|
|
|
|
impl AsInner<AnonPipe> for ChildStderr {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn as_inner(&self) -> &AnonPipe {
|
|
|
|
|
&self.inner
|
|
|
|
|
}
|
2015-05-12 18:03:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-16 06:31:24 +00:00
|
|
|
|
impl IntoInner<AnonPipe> for ChildStderr {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn into_inner(self) -> AnonPipe {
|
|
|
|
|
self.inner
|
|
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 19:10:37 +00:00
|
|
|
|
impl FromInner<AnonPipe> for ChildStderr {
|
|
|
|
|
fn from_inner(pipe: AnonPipe) -> ChildStderr {
|
|
|
|
|
ChildStderr { inner: pipe }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for ChildStderr {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
|
f.pad("ChildStderr { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-09 21:23:08 +00:00
|
|
|
|
/// A process builder, providing fine-grained control
|
|
|
|
|
/// over how a new process should be spawned.
|
|
|
|
|
///
|
|
|
|
|
/// A default configuration can be
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// 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:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
2017-01-04 09:09:16 +00:00
|
|
|
|
/// let output = if cfg!(target_os = "windows") {
|
2017-01-03 12:20:30 +00:00
|
|
|
|
/// Command::new("cmd")
|
|
|
|
|
/// .args(&["/C", "echo hello"])
|
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("failed to execute process")
|
|
|
|
|
/// } else {
|
|
|
|
|
/// Command::new("sh")
|
|
|
|
|
/// .arg("-c")
|
|
|
|
|
/// .arg("echo hello")
|
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("failed to execute process")
|
|
|
|
|
/// };
|
2016-03-21 13:17:17 +00:00
|
|
|
|
///
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// let hello = output.stdout;
|
|
|
|
|
/// ```
|
2018-08-11 10:13:29 +00:00
|
|
|
|
///
|
|
|
|
|
/// `Command` can be reused to spawn multiple processes. The builder methods
|
|
|
|
|
/// change the command without needing to immediately spawn the process.
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// let mut echo_hello = Command::new("sh");
|
|
|
|
|
/// echo_hello.arg("-c")
|
2018-08-11 20:02:49 +00:00
|
|
|
|
/// .arg("echo hello");
|
2018-08-11 10:13:29 +00:00
|
|
|
|
/// let hello_1 = echo_hello.output().expect("failed to execute process");
|
|
|
|
|
/// let hello_2 = echo_hello.output().expect("failed to execute process");
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// Similarly, you can call builder methods after spawning a process and then
|
|
|
|
|
/// spawn a new process with the modified settings.
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// let mut list_dir = Command::new("ls");
|
|
|
|
|
///
|
|
|
|
|
/// // Execute `ls` in the current directory of the program.
|
|
|
|
|
/// list_dir.status().expect("process failed to execute");
|
|
|
|
|
///
|
2019-09-13 09:36:35 +00:00
|
|
|
|
/// println!();
|
2018-08-11 10:13:29 +00:00
|
|
|
|
///
|
|
|
|
|
/// // Change `ls` to execute in the root directory.
|
|
|
|
|
/// list_dir.current_dir("/");
|
|
|
|
|
///
|
|
|
|
|
/// // And then execute `ls` again but in the root directory.
|
|
|
|
|
/// list_dir.status().expect("process failed to execute");
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub struct Command {
|
2015-05-12 18:03:49 +00:00
|
|
|
|
inner: imp::Command,
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2015-04-29 21:00:10 +00:00
|
|
|
|
/// * Inherit stdin/stdout/stderr for `spawn` or `status`, but create pipes for `output`
|
2015-02-06 17:42:57 +00:00
|
|
|
|
///
|
|
|
|
|
/// Builder methods are provided to change these defaults and
|
|
|
|
|
/// otherwise configure the process.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
2016-11-26 17:15:33 +00:00
|
|
|
|
/// If `program` is not an absolute path, the `PATH` will be searched in
|
|
|
|
|
/// an OS-defined way.
|
|
|
|
|
///
|
|
|
|
|
/// The search path to be used may be controlled by setting the
|
|
|
|
|
/// `PATH` environment variable on the Command,
|
|
|
|
|
/// but this has some implementation limitations on Windows
|
2019-02-09 22:16:58 +00:00
|
|
|
|
/// (see issue #37519).
|
2016-11-26 17:15:33 +00:00
|
|
|
|
///
|
2016-04-28 10:42:42 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("sh")
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("sh command failed to start");
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-03-30 18:00:05 +00:00
|
|
|
|
pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
|
2016-02-04 19:10:37 +00:00
|
|
|
|
Command { inner: imp::Command::new(program.as_ref()) }
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 22:16:58 +00:00
|
|
|
|
/// Adds an argument to pass to the program.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
2017-03-08 04:04:59 +00:00
|
|
|
|
/// Only one argument can be passed per use. So instead of:
|
|
|
|
|
///
|
2017-06-20 07:15:16 +00:00
|
|
|
|
/// ```no_run
|
|
|
|
|
/// # std::process::Command::new("sh")
|
2017-03-08 04:04:59 +00:00
|
|
|
|
/// .arg("-C /path/to/repo")
|
2017-06-20 07:15:16 +00:00
|
|
|
|
/// # ;
|
2017-03-08 04:04:59 +00:00
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// usage would be:
|
|
|
|
|
///
|
2017-06-20 07:15:16 +00:00
|
|
|
|
/// ```no_run
|
|
|
|
|
/// # std::process::Command::new("sh")
|
2017-03-08 04:04:59 +00:00
|
|
|
|
/// .arg("-C")
|
|
|
|
|
/// .arg("/path/to/repo")
|
2017-06-20 07:15:16 +00:00
|
|
|
|
/// # ;
|
2017-03-08 04:04:59 +00:00
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// To pass multiple arguments see [`args`].
|
|
|
|
|
///
|
|
|
|
|
/// [`args`]: #method.args
|
|
|
|
|
///
|
2016-04-28 10:42:42 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("ls")
|
|
|
|
|
/// .arg("-l")
|
|
|
|
|
/// .arg("-a")
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("ls command failed to start");
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-03-30 18:00:05 +00:00
|
|
|
|
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
|
|
|
|
|
self.inner.arg(arg.as_ref());
|
2015-02-06 17:42:57 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 22:16:58 +00:00
|
|
|
|
/// Adds multiple arguments to pass to the program.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
2017-03-08 04:04:59 +00:00
|
|
|
|
/// To pass a single argument see [`arg`].
|
|
|
|
|
///
|
|
|
|
|
/// [`arg`]: #method.arg
|
|
|
|
|
///
|
2016-04-28 10:42:42 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("ls")
|
|
|
|
|
/// .args(&["-l", "-a"])
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("ls command failed to start");
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2017-01-21 16:01:11 +00:00
|
|
|
|
pub fn args<I, S>(&mut self, args: I) -> &mut Command
|
2019-11-27 18:29:00 +00:00
|
|
|
|
where
|
|
|
|
|
I: IntoIterator<Item = S>,
|
|
|
|
|
S: AsRef<OsStr>,
|
2017-01-21 16:01:11 +00:00
|
|
|
|
{
|
2015-10-31 18:09:43 +00:00
|
|
|
|
for arg in args {
|
|
|
|
|
self.arg(arg.as_ref());
|
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Inserts or updates an environment variable mapping.
|
|
|
|
|
///
|
|
|
|
|
/// Note that environment variable names are case-insensitive (but case-preserving) on Windows,
|
|
|
|
|
/// and case-sensitive on all other platforms.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("ls")
|
|
|
|
|
/// .env("PATH", "/bin")
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("ls command failed to start");
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-03-12 19:59:53 +00:00
|
|
|
|
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
|
2019-11-27 18:29:00 +00:00
|
|
|
|
where
|
|
|
|
|
K: AsRef<OsStr>,
|
|
|
|
|
V: AsRef<OsStr>,
|
2015-02-06 17:42:57 +00:00
|
|
|
|
{
|
2017-12-17 15:21:47 +00:00
|
|
|
|
self.inner.env_mut().set(key.as_ref(), val.as_ref());
|
2015-02-06 17:42:57 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 22:16:58 +00:00
|
|
|
|
/// Adds or updates multiple environment variable mappings.
|
2017-01-05 20:51:45 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
2017-03-24 08:42:21 +00:00
|
|
|
|
///
|
2017-01-05 20:51:45 +00:00
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
/// use std::env;
|
2017-01-21 16:01:11 +00:00
|
|
|
|
/// use std::collections::HashMap;
|
2017-01-05 20:51:45 +00:00
|
|
|
|
///
|
2017-01-21 16:01:11 +00:00
|
|
|
|
/// let filtered_env : HashMap<String, String> =
|
2017-01-05 20:51:45 +00:00
|
|
|
|
/// env::vars().filter(|&(ref k, _)|
|
|
|
|
|
/// k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH"
|
|
|
|
|
/// ).collect();
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("printenv")
|
|
|
|
|
/// .stdin(Stdio::null())
|
|
|
|
|
/// .stdout(Stdio::inherit())
|
|
|
|
|
/// .env_clear()
|
|
|
|
|
/// .envs(&filtered_env)
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("printenv failed to start");
|
|
|
|
|
/// ```
|
2017-06-08 15:31:25 +00:00
|
|
|
|
#[stable(feature = "command_envs", since = "1.19.0")]
|
2017-01-21 16:01:11 +00:00
|
|
|
|
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
|
2019-11-27 18:29:00 +00:00
|
|
|
|
where
|
|
|
|
|
I: IntoIterator<Item = (K, V)>,
|
|
|
|
|
K: AsRef<OsStr>,
|
|
|
|
|
V: AsRef<OsStr>,
|
2017-01-05 20:51:45 +00:00
|
|
|
|
{
|
2017-01-21 16:01:11 +00:00
|
|
|
|
for (ref key, ref val) in vars {
|
2017-12-17 15:21:47 +00:00
|
|
|
|
self.inner.env_mut().set(key.as_ref(), val.as_ref());
|
2017-01-05 20:51:45 +00:00
|
|
|
|
}
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// Removes an environment variable mapping.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("ls")
|
|
|
|
|
/// .env_remove("PATH")
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("ls command failed to start");
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-03-30 18:00:05 +00:00
|
|
|
|
pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
|
2017-12-17 15:21:47 +00:00
|
|
|
|
self.inner.env_mut().remove(key.as_ref());
|
2015-02-06 17:42:57 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Clears the entire environment map for the child process.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("ls")
|
|
|
|
|
/// .env_clear()
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("ls command failed to start");
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub fn env_clear(&mut self) -> &mut Command {
|
2017-12-17 15:21:47 +00:00
|
|
|
|
self.inner.env_mut().clear();
|
2015-02-06 17:42:57 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
|
/// Sets the working directory for the child process.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
2018-08-15 18:07:44 +00:00
|
|
|
|
/// # Platform-specific behavior
|
|
|
|
|
///
|
2018-11-27 02:59:49 +00:00
|
|
|
|
/// If the program path is relative (e.g., `"./script.sh"`), it's ambiguous
|
2018-08-15 18:07:44 +00:00
|
|
|
|
/// whether it should be interpreted relative to the parent's working
|
|
|
|
|
/// directory or relative to `current_dir`. The behavior in this case is
|
|
|
|
|
/// platform specific and unstable, and it's recommended to use
|
|
|
|
|
/// [`canonicalize`] to get an absolute program path instead.
|
|
|
|
|
///
|
2016-04-28 10:42:42 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("ls")
|
|
|
|
|
/// .current_dir("/bin")
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("ls command failed to start");
|
|
|
|
|
/// ```
|
2018-08-15 18:07:44 +00:00
|
|
|
|
///
|
|
|
|
|
/// [`canonicalize`]: ../fs/fn.canonicalize.html
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2016-02-04 02:09:35 +00:00
|
|
|
|
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command {
|
2015-03-30 18:00:05 +00:00
|
|
|
|
self.inner.cwd(dir.as_ref().as_ref());
|
2015-02-06 17:42:57 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 01:01:51 +00:00
|
|
|
|
/// Configuration for the child process's standard input (stdin) handle.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
2017-10-09 18:20:07 +00:00
|
|
|
|
/// Defaults to [`inherit`] when used with `spawn` or `status`, and
|
|
|
|
|
/// defaults to [`piped`] when used with `output`.
|
|
|
|
|
///
|
|
|
|
|
/// [`inherit`]: struct.Stdio.html#method.inherit
|
|
|
|
|
/// [`piped`]: struct.Stdio.html#method.piped
|
|
|
|
|
///
|
2016-04-28 10:42:42 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("ls")
|
|
|
|
|
/// .stdin(Stdio::null())
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("ls command failed to start");
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2017-06-06 22:42:55 +00:00
|
|
|
|
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
|
|
|
|
|
self.inner.stdin(cfg.into().0);
|
2015-02-06 17:42:57 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 01:01:51 +00:00
|
|
|
|
/// Configuration for the child process's standard output (stdout) handle.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
2017-10-09 18:20:07 +00:00
|
|
|
|
/// Defaults to [`inherit`] when used with `spawn` or `status`, and
|
|
|
|
|
/// defaults to [`piped`] when used with `output`.
|
|
|
|
|
///
|
|
|
|
|
/// [`inherit`]: struct.Stdio.html#method.inherit
|
|
|
|
|
/// [`piped`]: struct.Stdio.html#method.piped
|
|
|
|
|
///
|
2016-04-28 10:42:42 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("ls")
|
|
|
|
|
/// .stdout(Stdio::null())
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("ls command failed to start");
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2017-06-06 22:42:55 +00:00
|
|
|
|
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
|
|
|
|
|
self.inner.stdout(cfg.into().0);
|
2015-02-06 17:42:57 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 01:01:51 +00:00
|
|
|
|
/// Configuration for the child process's standard error (stderr) handle.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
2017-10-09 18:20:07 +00:00
|
|
|
|
/// Defaults to [`inherit`] when used with `spawn` or `status`, and
|
|
|
|
|
/// defaults to [`piped`] when used with `output`.
|
|
|
|
|
///
|
|
|
|
|
/// [`inherit`]: struct.Stdio.html#method.inherit
|
|
|
|
|
/// [`piped`]: struct.Stdio.html#method.piped
|
|
|
|
|
///
|
2016-04-28 10:42:42 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("ls")
|
|
|
|
|
/// .stderr(Stdio::null())
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("ls command failed to start");
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2017-06-06 22:42:55 +00:00
|
|
|
|
pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
|
|
|
|
|
self.inner.stderr(cfg.into().0);
|
2015-02-06 17:42:57 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Executes the command as a child process, returning a handle to it.
|
|
|
|
|
///
|
2015-06-06 00:27:04 +00:00
|
|
|
|
/// By default, stdin, stdout and stderr are inherited from the parent.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// Command::new("ls")
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("ls command failed to start");
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub fn spawn(&mut self) -> io::Result<Child> {
|
2016-02-12 18:28:03 +00:00
|
|
|
|
self.inner.spawn(imp::Stdio::Inherit, true).map(Child::from_inner)
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Executes the command as a child process, waiting for it to finish and
|
|
|
|
|
/// collecting all of its output.
|
|
|
|
|
///
|
2017-11-23 01:47:31 +00:00
|
|
|
|
/// By default, stdout and stderr are captured (and used to provide the
|
|
|
|
|
/// resulting output). Stdin is not inherited from the parent and any
|
|
|
|
|
/// attempt by the child process to read from the stdin stream will result
|
|
|
|
|
/// in the stream immediately closing.
|
2015-02-06 17:42:57 +00:00
|
|
|
|
///
|
2015-03-08 16:07:58 +00:00
|
|
|
|
/// # Examples
|
2015-02-06 17:42:57 +00:00
|
|
|
|
///
|
2016-03-21 13:17:17 +00:00
|
|
|
|
/// ```should_panic
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// use std::process::Command;
|
2018-11-10 17:16:04 +00:00
|
|
|
|
/// use std::io::{self, Write};
|
2016-04-28 10:42:42 +00:00
|
|
|
|
/// let output = Command::new("/bin/cat")
|
|
|
|
|
/// .arg("file.txt")
|
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("failed to execute process");
|
2015-02-06 17:42:57 +00:00
|
|
|
|
///
|
|
|
|
|
/// println!("status: {}", output.status);
|
2018-11-10 17:16:04 +00:00
|
|
|
|
/// io::stdout().write_all(&output.stdout).unwrap();
|
|
|
|
|
/// io::stderr().write_all(&output.stderr).unwrap();
|
2016-03-22 14:19:24 +00:00
|
|
|
|
///
|
|
|
|
|
/// assert!(output.status.success());
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub fn output(&mut self) -> io::Result<Output> {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
self.inner
|
|
|
|
|
.spawn(imp::Stdio::MakePipe, false)
|
|
|
|
|
.map(Child::from_inner)
|
2016-02-04 19:10:37 +00:00
|
|
|
|
.and_then(|p| p.wait_with_output())
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Executes a command as a child process, waiting for it to finish and
|
|
|
|
|
/// collecting its exit status.
|
|
|
|
|
///
|
2015-06-06 00:32:34 +00:00
|
|
|
|
/// By default, stdin, stdout and stderr are inherited from the parent.
|
2015-02-06 17:42:57 +00:00
|
|
|
|
///
|
2015-03-12 01:11:40 +00:00
|
|
|
|
/// # Examples
|
2015-02-06 17:42:57 +00:00
|
|
|
|
///
|
2016-03-21 13:17:17 +00:00
|
|
|
|
/// ```should_panic
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
2016-04-28 10:42:42 +00:00
|
|
|
|
/// let status = Command::new("/bin/cat")
|
|
|
|
|
/// .arg("file.txt")
|
|
|
|
|
/// .status()
|
|
|
|
|
/// .expect("failed to execute process");
|
2015-02-06 17:42:57 +00:00
|
|
|
|
///
|
|
|
|
|
/// println!("process exited with: {}", status);
|
2016-03-22 14:19:24 +00:00
|
|
|
|
///
|
|
|
|
|
/// assert!(status.success());
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub fn status(&mut self) -> io::Result<ExitStatus> {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
self.inner
|
|
|
|
|
.spawn(imp::Stdio::Inherit, true)
|
|
|
|
|
.map(Child::from_inner)
|
|
|
|
|
.and_then(|mut p| p.wait())
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl fmt::Debug for Command {
|
|
|
|
|
/// Format the program and arguments of a Command for display. Any
|
|
|
|
|
/// non-utf8 data is lossily converted using the utf8 replacement
|
|
|
|
|
/// character.
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-01-15 20:29:45 +00:00
|
|
|
|
self.inner.fmt(f)
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 18:03:49 +00:00
|
|
|
|
impl AsInner<imp::Command> for Command {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn as_inner(&self) -> &imp::Command {
|
|
|
|
|
&self.inner
|
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 18:03:49 +00:00
|
|
|
|
impl AsInnerMut<imp::Command> for Command {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn as_inner_mut(&mut self) -> &mut imp::Command {
|
|
|
|
|
&mut self.inner
|
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The output of a finished process.
|
2017-10-13 17:18:09 +00:00
|
|
|
|
///
|
|
|
|
|
/// This is returned in a Result by either the [`output`] method of a
|
|
|
|
|
/// [`Command`], or the [`wait_with_output`] method of a [`Child`]
|
|
|
|
|
/// process.
|
2017-10-08 21:11:20 +00:00
|
|
|
|
///
|
|
|
|
|
/// [`Command`]: struct.Command.html
|
|
|
|
|
/// [`Child`]: struct.Child.html
|
|
|
|
|
/// [`output`]: struct.Command.html#method.output
|
|
|
|
|
/// [`wait_with_output`]: struct.Child.html#method.wait_with_output
|
2015-02-06 17:42:57 +00:00
|
|
|
|
#[derive(PartialEq, Eq, Clone)]
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub struct Output {
|
|
|
|
|
/// The status (exit code) of the process.
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub status: ExitStatus,
|
|
|
|
|
/// The data that the process wrote to stdout.
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub stdout: Vec<u8>,
|
|
|
|
|
/// The data that the process wrote to stderr.
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub stderr: Vec<u8>,
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-16 20:28:54 +00:00
|
|
|
|
// If either stderr or stdout are valid utf8 strings it prints the valid
|
|
|
|
|
// strings, otherwise it prints the byte sequence instead
|
|
|
|
|
#[stable(feature = "process_output_debug", since = "1.7.0")]
|
|
|
|
|
impl fmt::Debug for Output {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-12-16 20:28:54 +00:00
|
|
|
|
let stdout_utf8 = str::from_utf8(&self.stdout);
|
2018-07-10 18:35:36 +00:00
|
|
|
|
let stdout_debug: &dyn fmt::Debug = match stdout_utf8 {
|
2015-12-16 20:28:54 +00:00
|
|
|
|
Ok(ref str) => str,
|
2019-11-27 18:29:00 +00:00
|
|
|
|
Err(_) => &self.stdout,
|
2015-12-16 20:28:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let stderr_utf8 = str::from_utf8(&self.stderr);
|
2018-07-10 18:35:36 +00:00
|
|
|
|
let stderr_debug: &dyn fmt::Debug = match stderr_utf8 {
|
2015-12-16 20:28:54 +00:00
|
|
|
|
Ok(ref str) => str,
|
2019-11-27 18:29:00 +00:00
|
|
|
|
Err(_) => &self.stderr,
|
2015-12-16 20:28:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fmt.debug_struct("Output")
|
|
|
|
|
.field("status", &self.status)
|
|
|
|
|
.field("stdout", stdout_debug)
|
|
|
|
|
.field("stderr", stderr_debug)
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 16:12:14 +00:00
|
|
|
|
/// Describes what to do with a standard I/O stream for a child process when
|
|
|
|
|
/// passed to the [`stdin`], [`stdout`], and [`stderr`] methods of [`Command`].
|
|
|
|
|
///
|
2017-10-08 18:09:16 +00:00
|
|
|
|
/// [`stdin`]: struct.Command.html#method.stdin
|
|
|
|
|
/// [`stdout`]: struct.Command.html#method.stdout
|
|
|
|
|
/// [`stderr`]: struct.Command.html#method.stderr
|
|
|
|
|
/// [`Command`]: struct.Command.html
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2016-02-04 19:10:37 +00:00
|
|
|
|
pub struct Stdio(imp::Stdio);
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
|
|
|
|
impl Stdio {
|
2015-02-28 00:23:21 +00:00
|
|
|
|
/// A new pipe should be arranged to connect the parent and child processes.
|
2017-10-08 16:12:14 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// With stdout:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
///
|
|
|
|
|
/// let output = Command::new("echo")
|
|
|
|
|
/// .arg("Hello, world!")
|
|
|
|
|
/// .stdout(Stdio::piped())
|
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("Failed to execute command");
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "Hello, world!\n");
|
|
|
|
|
/// // Nothing echoed to console
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// With stdin:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::io::Write;
|
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
///
|
|
|
|
|
/// let mut child = Command::new("rev")
|
|
|
|
|
/// .stdin(Stdio::piped())
|
|
|
|
|
/// .stdout(Stdio::piped())
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("Failed to spawn child process");
|
2017-10-08 18:09:16 +00:00
|
|
|
|
///
|
2017-10-08 16:12:14 +00:00
|
|
|
|
/// {
|
2019-09-19 11:36:10 +00:00
|
|
|
|
/// let stdin = child.stdin.as_mut().expect("Failed to open stdin");
|
2017-10-08 16:12:14 +00:00
|
|
|
|
/// stdin.write_all("Hello, world!".as_bytes()).expect("Failed to write to stdin");
|
|
|
|
|
/// }
|
2017-10-08 18:09:16 +00:00
|
|
|
|
///
|
2017-10-08 16:12:14 +00:00
|
|
|
|
/// let output = child.wait_with_output().expect("Failed to read stdout");
|
2019-09-08 21:03:09 +00:00
|
|
|
|
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "!dlrow ,olleH");
|
2017-10-08 16:12:14 +00:00
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub fn piped() -> Stdio {
|
|
|
|
|
Stdio(imp::Stdio::MakePipe)
|
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
|
|
|
|
/// The child inherits from the corresponding parent descriptor.
|
2017-10-08 16:12:14 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// With stdout:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
///
|
|
|
|
|
/// let output = Command::new("echo")
|
|
|
|
|
/// .arg("Hello, world!")
|
|
|
|
|
/// .stdout(Stdio::inherit())
|
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("Failed to execute command");
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "");
|
|
|
|
|
/// // "Hello, world!" echoed to console
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// With stdin:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::{Command, Stdio};
|
2018-11-10 17:16:04 +00:00
|
|
|
|
/// use std::io::{self, Write};
|
2017-10-08 18:09:16 +00:00
|
|
|
|
///
|
2017-10-08 16:12:14 +00:00
|
|
|
|
/// let output = Command::new("rev")
|
|
|
|
|
/// .stdin(Stdio::inherit())
|
|
|
|
|
/// .stdout(Stdio::piped())
|
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("Failed to execute command");
|
2017-10-08 18:09:16 +00:00
|
|
|
|
///
|
2018-11-10 17:16:04 +00:00
|
|
|
|
/// print!("You piped in the reverse of: ");
|
|
|
|
|
/// io::stdout().write_all(&output.stdout).unwrap();
|
2017-10-08 16:12:14 +00:00
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub fn inherit() -> Stdio {
|
|
|
|
|
Stdio(imp::Stdio::Inherit)
|
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
|
|
|
|
/// This stream will be ignored. This is the equivalent of attaching the
|
|
|
|
|
/// stream to `/dev/null`
|
2017-10-08 16:12:14 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// With stdout:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
///
|
|
|
|
|
/// let output = Command::new("echo")
|
|
|
|
|
/// .arg("Hello, world!")
|
|
|
|
|
/// .stdout(Stdio::null())
|
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("Failed to execute command");
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "");
|
|
|
|
|
/// // Nothing echoed to console
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// With stdin:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::{Command, Stdio};
|
2017-10-08 18:09:16 +00:00
|
|
|
|
///
|
2017-10-08 16:12:14 +00:00
|
|
|
|
/// let output = Command::new("rev")
|
|
|
|
|
/// .stdin(Stdio::null())
|
|
|
|
|
/// .stdout(Stdio::piped())
|
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("Failed to execute command");
|
2017-10-08 18:09:16 +00:00
|
|
|
|
///
|
2017-10-08 16:12:14 +00:00
|
|
|
|
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "");
|
|
|
|
|
/// // Ignores any piped-in input
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
pub fn null() -> Stdio {
|
|
|
|
|
Stdio(imp::Stdio::Null)
|
|
|
|
|
}
|
2015-05-12 18:03:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 19:10:37 +00:00
|
|
|
|
impl FromInner<imp::Stdio> for Stdio {
|
|
|
|
|
fn from_inner(inner: imp::Stdio) -> Stdio {
|
|
|
|
|
Stdio(inner)
|
2015-05-12 18:03:49 +00:00
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
|
impl fmt::Debug for Stdio {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-11-25 18:21:49 +00:00
|
|
|
|
f.pad("Stdio { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 22:42:55 +00:00
|
|
|
|
#[stable(feature = "stdio_from", since = "1.20.0")]
|
|
|
|
|
impl From<ChildStdin> for Stdio {
|
2018-10-19 10:00:45 +00:00
|
|
|
|
/// Converts a `ChildStdin` into a `Stdio`
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// `ChildStdin` will be converted to `Stdio` using `Stdio::from` under the hood.
|
|
|
|
|
///
|
2019-02-26 03:49:49 +00:00
|
|
|
|
/// ```rust,no_run
|
2018-10-19 10:00:45 +00:00
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
///
|
|
|
|
|
/// let reverse = Command::new("rev")
|
|
|
|
|
/// .stdin(Stdio::piped())
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("failed reverse command");
|
|
|
|
|
///
|
|
|
|
|
/// let _echo = Command::new("echo")
|
|
|
|
|
/// .arg("Hello, world!")
|
|
|
|
|
/// .stdout(reverse.stdin.unwrap()) // Converted into a Stdio here
|
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("failed echo command");
|
|
|
|
|
///
|
|
|
|
|
/// // "!dlrow ,olleH" echoed to console
|
|
|
|
|
/// ```
|
2017-06-06 22:42:55 +00:00
|
|
|
|
fn from(child: ChildStdin) -> Stdio {
|
|
|
|
|
Stdio::from_inner(child.into_inner().into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "stdio_from", since = "1.20.0")]
|
|
|
|
|
impl From<ChildStdout> for Stdio {
|
2018-10-19 10:00:45 +00:00
|
|
|
|
/// Converts a `ChildStdout` into a `Stdio`
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// `ChildStdout` will be converted to `Stdio` using `Stdio::from` under the hood.
|
|
|
|
|
///
|
2019-02-26 03:49:49 +00:00
|
|
|
|
/// ```rust,no_run
|
2018-10-19 10:00:45 +00:00
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
///
|
|
|
|
|
/// let hello = Command::new("echo")
|
|
|
|
|
/// .arg("Hello, world!")
|
|
|
|
|
/// .stdout(Stdio::piped())
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("failed echo command");
|
|
|
|
|
///
|
|
|
|
|
/// let reverse = Command::new("rev")
|
|
|
|
|
/// .stdin(hello.stdout.unwrap()) // Converted into a Stdio here
|
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("failed reverse command");
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(reverse.stdout, b"!dlrow ,olleH\n");
|
|
|
|
|
/// ```
|
2017-06-06 22:42:55 +00:00
|
|
|
|
fn from(child: ChildStdout) -> Stdio {
|
|
|
|
|
Stdio::from_inner(child.into_inner().into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "stdio_from", since = "1.20.0")]
|
|
|
|
|
impl From<ChildStderr> for Stdio {
|
2018-10-19 10:00:45 +00:00
|
|
|
|
/// Converts a `ChildStderr` into a `Stdio`
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```rust,no_run
|
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
///
|
|
|
|
|
/// let reverse = Command::new("rev")
|
|
|
|
|
/// .arg("non_existing_file.txt")
|
|
|
|
|
/// .stderr(Stdio::piped())
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("failed reverse command");
|
|
|
|
|
///
|
|
|
|
|
/// let cat = Command::new("cat")
|
|
|
|
|
/// .arg("-")
|
|
|
|
|
/// .stdin(reverse.stderr.unwrap()) // Converted into a Stdio here
|
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("failed echo command");
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(
|
|
|
|
|
/// String::from_utf8_lossy(&cat.stdout),
|
|
|
|
|
/// "rev: cannot open non_existing_file.txt: No such file or directory\n"
|
|
|
|
|
/// );
|
|
|
|
|
/// ```
|
2017-06-06 22:42:55 +00:00
|
|
|
|
fn from(child: ChildStderr) -> Stdio {
|
|
|
|
|
Stdio::from_inner(child.into_inner().into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "stdio_from", since = "1.20.0")]
|
|
|
|
|
impl From<fs::File> for Stdio {
|
2018-10-19 10:00:45 +00:00
|
|
|
|
/// Converts a `File` into a `Stdio`
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// `File` will be converted to `Stdio` using `Stdio::from` under the hood.
|
|
|
|
|
///
|
|
|
|
|
/// ```rust,no_run
|
|
|
|
|
/// use std::fs::File;
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// // With the `foo.txt` file containing `Hello, world!"
|
|
|
|
|
/// let file = File::open("foo.txt").unwrap();
|
|
|
|
|
///
|
|
|
|
|
/// let reverse = Command::new("rev")
|
2018-11-12 18:05:20 +00:00
|
|
|
|
/// .stdin(file) // Implicit File conversion into a Stdio
|
2018-10-19 10:00:45 +00:00
|
|
|
|
/// .output()
|
|
|
|
|
/// .expect("failed reverse command");
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(reverse.stdout, b"!dlrow ,olleH");
|
|
|
|
|
/// ```
|
2017-06-06 22:42:55 +00:00
|
|
|
|
fn from(file: fs::File) -> Stdio {
|
|
|
|
|
Stdio::from_inner(file.into_inner().into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// Describes the result of a process after it has terminated.
|
2017-05-11 15:49:16 +00:00
|
|
|
|
///
|
|
|
|
|
/// This `struct` is used to represent the exit status of a child process.
|
|
|
|
|
/// Child processes are created via the [`Command`] struct and their exit
|
2019-06-05 07:58:39 +00:00
|
|
|
|
/// status is exposed through the [`status`] method, or the [`wait`] method
|
2019-06-05 06:41:37 +00:00
|
|
|
|
/// of a [`Child`] process.
|
2017-05-11 15:49:16 +00:00
|
|
|
|
///
|
|
|
|
|
/// [`Command`]: struct.Command.html
|
2019-06-05 06:41:37 +00:00
|
|
|
|
/// [`Child`]: struct.Child.html
|
2017-05-11 15:49:16 +00:00
|
|
|
|
/// [`status`]: struct.Command.html#method.status
|
2019-06-05 06:41:37 +00:00
|
|
|
|
/// [`wait`]: struct.Child.html#method.wait
|
2015-02-06 17:42:57 +00:00
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-05-12 18:03:49 +00:00
|
|
|
|
pub struct ExitStatus(imp::ExitStatus);
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
|
|
|
|
impl ExitStatus {
|
2017-07-21 06:44:53 +00:00
|
|
|
|
/// Was termination successful? Signal termination is not considered a
|
|
|
|
|
/// success, and success is defined as a zero exit status.
|
2016-07-12 23:52:44 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```rust,no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// let status = Command::new("mkdir")
|
|
|
|
|
/// .arg("projects")
|
|
|
|
|
/// .status()
|
|
|
|
|
/// .expect("failed to execute mkdir");
|
|
|
|
|
///
|
|
|
|
|
/// if status.success() {
|
|
|
|
|
/// println!("'projects/' directory created");
|
|
|
|
|
/// } else {
|
|
|
|
|
/// println!("failed to create 'projects/' directory");
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub fn success(&self) -> bool {
|
|
|
|
|
self.0.success()
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
|
/// Returns the exit code of the process, if any.
|
2015-02-06 17:42:57 +00:00
|
|
|
|
///
|
|
|
|
|
/// On Unix, this will return `None` if the process was terminated
|
|
|
|
|
/// by a signal; `std::os::unix` provides an extension trait for
|
|
|
|
|
/// extracting the signal and other details from the `ExitStatus`.
|
2017-05-11 15:49:16 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// let status = Command::new("mkdir")
|
|
|
|
|
/// .arg("projects")
|
|
|
|
|
/// .status()
|
|
|
|
|
/// .expect("failed to execute mkdir");
|
|
|
|
|
///
|
|
|
|
|
/// match status.code() {
|
|
|
|
|
/// Some(code) => println!("Exited with status code: {}", code),
|
|
|
|
|
/// None => println!("Process terminated by signal")
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub fn code(&self) -> Option<i32> {
|
|
|
|
|
self.0.code()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 18:03:49 +00:00
|
|
|
|
impl AsInner<imp::ExitStatus> for ExitStatus {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn as_inner(&self) -> &imp::ExitStatus {
|
|
|
|
|
&self.0
|
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 22:23:46 +00:00
|
|
|
|
impl FromInner<imp::ExitStatus> for ExitStatus {
|
|
|
|
|
fn from_inner(s: imp::ExitStatus) -> ExitStatus {
|
|
|
|
|
ExitStatus(s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
impl fmt::Display for ExitStatus {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-02-06 17:42:57 +00:00
|
|
|
|
self.0.fmt(f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 18:31:17 +00:00
|
|
|
|
/// This type represents the status code a process can return to its
|
|
|
|
|
/// parent under normal termination.
|
|
|
|
|
///
|
|
|
|
|
/// Numeric values used in this type don't have portable meanings, and
|
|
|
|
|
/// different platforms may mask different amounts of them.
|
|
|
|
|
///
|
|
|
|
|
/// For the platform's canonical successful and unsuccessful codes, see
|
|
|
|
|
/// the [`SUCCESS`] and [`FAILURE`] associated items.
|
|
|
|
|
///
|
2018-03-01 07:34:20 +00:00
|
|
|
|
/// [`SUCCESS`]: #associatedconstant.SUCCESS
|
|
|
|
|
/// [`FAILURE`]: #associatedconstant.FAILURE
|
2018-02-27 18:31:17 +00:00
|
|
|
|
///
|
|
|
|
|
/// **Warning**: While various forms of this were discussed in [RFC #1937],
|
|
|
|
|
/// it was ultimately cut from that RFC, and thus this type is more subject
|
|
|
|
|
/// to change even than the usual unstable item churn.
|
|
|
|
|
///
|
|
|
|
|
/// [RFC #1937]: https://github.com/rust-lang/rfcs/pull/1937
|
2018-02-24 08:31:33 +00:00
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
2018-03-04 02:29:30 +00:00
|
|
|
|
#[unstable(feature = "process_exitcode_placeholder", issue = "48711")]
|
|
|
|
|
pub struct ExitCode(imp::ExitCode);
|
2018-02-24 08:31:33 +00:00
|
|
|
|
|
2018-03-04 02:29:30 +00:00
|
|
|
|
#[unstable(feature = "process_exitcode_placeholder", issue = "48711")]
|
2018-02-27 18:31:17 +00:00
|
|
|
|
impl ExitCode {
|
|
|
|
|
/// The canonical ExitCode for successful termination on this platform.
|
|
|
|
|
///
|
|
|
|
|
/// Note that a `()`-returning `main` implicitly results in a successful
|
|
|
|
|
/// termination, so there's no need to return this from `main` unless
|
|
|
|
|
/// you're also returning other possible codes.
|
2018-03-04 02:29:30 +00:00
|
|
|
|
#[unstable(feature = "process_exitcode_placeholder", issue = "48711")]
|
|
|
|
|
pub const SUCCESS: ExitCode = ExitCode(imp::ExitCode::SUCCESS);
|
2018-02-27 18:31:17 +00:00
|
|
|
|
|
|
|
|
|
/// The canonical ExitCode for unsuccessful termination on this platform.
|
|
|
|
|
///
|
|
|
|
|
/// If you're only returning this and `SUCCESS` from `main`, consider
|
|
|
|
|
/// instead returning `Err(_)` and `Ok(())` respectively, which will
|
|
|
|
|
/// return the same codes (but will also `eprintln!` the error).
|
2018-03-04 02:29:30 +00:00
|
|
|
|
#[unstable(feature = "process_exitcode_placeholder", issue = "48711")]
|
|
|
|
|
pub const FAILURE: ExitCode = ExitCode(imp::ExitCode::FAILURE);
|
2018-02-27 18:31:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-06 17:42:57 +00:00
|
|
|
|
impl Child {
|
2018-04-17 07:00:48 +00:00
|
|
|
|
/// Forces the child process to exit. If the child has already exited, an [`InvalidInput`]
|
2018-03-29 17:10:40 +00:00
|
|
|
|
/// error is returned.
|
|
|
|
|
///
|
|
|
|
|
/// The mapping to [`ErrorKind`]s is not part of the compatibility contract of the function,
|
|
|
|
|
/// especially the [`Other`] kind might change to more specific kinds in the future.
|
2018-03-28 17:54:34 +00:00
|
|
|
|
///
|
|
|
|
|
/// This is equivalent to sending a SIGKILL on Unix platforms.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// let mut command = Command::new("yes");
|
|
|
|
|
/// if let Ok(mut child) = command.spawn() {
|
|
|
|
|
/// child.kill().expect("command wasn't running");
|
|
|
|
|
/// } else {
|
|
|
|
|
/// println!("yes command didn't start");
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2018-03-28 22:05:51 +00:00
|
|
|
|
///
|
2018-03-29 17:10:40 +00:00
|
|
|
|
/// [`ErrorKind`]: ../io/enum.ErrorKind.html
|
2018-03-28 22:05:51 +00:00
|
|
|
|
/// [`InvalidInput`]: ../io/enum.ErrorKind.html#variant.InvalidInput
|
2018-04-08 15:20:15 +00:00
|
|
|
|
/// [`Other`]: ../io/enum.ErrorKind.html#variant.Other
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub fn kill(&mut self) -> io::Result<()> {
|
2016-02-04 02:09:35 +00:00
|
|
|
|
self.handle.kill()
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-16 16:44:05 +00:00
|
|
|
|
/// Returns the OS-assigned process identifier associated with this child.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// let mut command = Command::new("ls");
|
|
|
|
|
/// if let Ok(child) = command.spawn() {
|
2019-02-09 22:16:58 +00:00
|
|
|
|
/// println!("Child's ID is {}", child.id());
|
2016-04-28 10:42:42 +00:00
|
|
|
|
/// } else {
|
|
|
|
|
/// println!("ls command didn't start");
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2015-07-28 22:44:30 +00:00
|
|
|
|
#[stable(feature = "process_id", since = "1.3.0")]
|
2015-04-16 16:44:05 +00:00
|
|
|
|
pub fn id(&self) -> u32 {
|
|
|
|
|
self.handle.id()
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
|
/// Waits for the child to exit completely, returning the status that it
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// exited with. This function will continue to have the same return value
|
|
|
|
|
/// after it has been called at least once.
|
|
|
|
|
///
|
|
|
|
|
/// The stdin handle to the child process, if any, will be closed
|
|
|
|
|
/// before waiting. This helps avoid deadlock: it ensures that the
|
|
|
|
|
/// child does not block waiting for input from the parent, while
|
|
|
|
|
/// the parent waits for the child to exit.
|
2016-04-28 10:42:42 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// let mut command = Command::new("ls");
|
|
|
|
|
/// if let Ok(mut child) = command.spawn() {
|
|
|
|
|
/// child.wait().expect("command wasn't running");
|
|
|
|
|
/// println!("Child has finished its execution!");
|
|
|
|
|
/// } else {
|
|
|
|
|
/// println!("ls command didn't start");
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub fn wait(&mut self) -> io::Result<ExitStatus> {
|
|
|
|
|
drop(self.stdin.take());
|
2016-02-04 02:09:35 +00:00
|
|
|
|
self.handle.wait().map(ExitStatus)
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 06:47:09 +00:00
|
|
|
|
/// Attempts to collect the exit status of the child if it has already
|
|
|
|
|
/// exited.
|
|
|
|
|
///
|
2018-11-12 18:05:20 +00:00
|
|
|
|
/// This function will not block the calling thread and will only
|
2017-01-06 06:47:09 +00:00
|
|
|
|
/// check to see if the child process has exited or not. If the child has
|
2019-02-09 22:16:58 +00:00
|
|
|
|
/// exited then on Unix the process ID is reaped. This function is
|
2017-01-06 06:47:09 +00:00
|
|
|
|
/// guaranteed to repeatedly return a successful exit status so long as the
|
|
|
|
|
/// child has already exited.
|
|
|
|
|
///
|
2017-02-03 22:39:41 +00:00
|
|
|
|
/// If the child has exited, then `Ok(Some(status))` is returned. If the
|
|
|
|
|
/// exit status is not available at this time then `Ok(None)` is returned.
|
|
|
|
|
/// If an error occurs, then that error is returned.
|
2017-01-06 06:47:09 +00:00
|
|
|
|
///
|
|
|
|
|
/// Note that unlike `wait`, this function will not attempt to drop stdin.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
///
|
|
|
|
|
/// let mut child = Command::new("ls").spawn().unwrap();
|
|
|
|
|
///
|
|
|
|
|
/// match child.try_wait() {
|
2017-02-03 22:39:41 +00:00
|
|
|
|
/// Ok(Some(status)) => println!("exited with: {}", status),
|
|
|
|
|
/// Ok(None) => {
|
2017-01-06 06:47:09 +00:00
|
|
|
|
/// println!("status not ready yet, let's really wait");
|
|
|
|
|
/// let res = child.wait();
|
|
|
|
|
/// println!("result: {:?}", res);
|
|
|
|
|
/// }
|
|
|
|
|
/// Err(e) => println!("error attempting to wait: {}", e),
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2017-05-11 04:17:24 +00:00
|
|
|
|
#[stable(feature = "process_try_wait", since = "1.18.0")]
|
2017-02-03 22:39:41 +00:00
|
|
|
|
pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
|
|
|
|
|
Ok(self.handle.try_wait()?.map(ExitStatus))
|
2017-01-06 06:47:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
|
/// Simultaneously waits for the child to exit and collect all remaining
|
2015-12-02 04:12:01 +00:00
|
|
|
|
/// output on the stdout/stderr handles, returning an `Output`
|
2015-02-06 17:42:57 +00:00
|
|
|
|
/// instance.
|
|
|
|
|
///
|
|
|
|
|
/// The stdin handle to the child process, if any, will be closed
|
|
|
|
|
/// before waiting. This helps avoid deadlock: it ensures that the
|
|
|
|
|
/// child does not block waiting for input from the parent, while
|
|
|
|
|
/// the parent waits for the child to exit.
|
2016-03-18 15:11:37 +00:00
|
|
|
|
///
|
|
|
|
|
/// By default, stdin, stdout and stderr are inherited from the parent.
|
|
|
|
|
/// In order to capture the output into this `Result<Output>` it is
|
|
|
|
|
/// necessary to create new pipes between parent and child. Use
|
2016-03-19 20:07:47 +00:00
|
|
|
|
/// `stdout(Stdio::piped())` or `stderr(Stdio::piped())`, respectively.
|
2016-03-18 15:11:37 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2016-03-19 09:41:13 +00:00
|
|
|
|
/// ```should_panic
|
2016-03-18 15:11:37 +00:00
|
|
|
|
/// use std::process::{Command, Stdio};
|
|
|
|
|
///
|
2016-06-16 21:20:22 +00:00
|
|
|
|
/// let child = Command::new("/bin/cat")
|
|
|
|
|
/// .arg("file.txt")
|
|
|
|
|
/// .stdout(Stdio::piped())
|
|
|
|
|
/// .spawn()
|
|
|
|
|
/// .expect("failed to execute child");
|
2016-03-18 15:11:37 +00:00
|
|
|
|
///
|
2016-06-16 21:20:22 +00:00
|
|
|
|
/// let output = child
|
|
|
|
|
/// .wait_with_output()
|
|
|
|
|
/// .expect("failed to wait on child");
|
2016-03-18 15:11:37 +00:00
|
|
|
|
///
|
2016-06-16 21:20:22 +00:00
|
|
|
|
/// assert!(output.status.success());
|
2016-03-18 15:11:37 +00:00
|
|
|
|
/// ```
|
|
|
|
|
///
|
2015-02-28 00:23:21 +00:00
|
|
|
|
#[stable(feature = "process", since = "1.0.0")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub fn wait_with_output(mut self) -> io::Result<Output> {
|
|
|
|
|
drop(self.stdin.take());
|
2016-02-12 18:29:25 +00:00
|
|
|
|
|
|
|
|
|
let (mut stdout, mut stderr) = (Vec::new(), Vec::new());
|
|
|
|
|
match (self.stdout.take(), self.stderr.take()) {
|
|
|
|
|
(None, None) => {}
|
|
|
|
|
(Some(mut out), None) => {
|
|
|
|
|
let res = out.read_to_end(&mut stdout);
|
|
|
|
|
res.unwrap();
|
|
|
|
|
}
|
|
|
|
|
(None, Some(mut err)) => {
|
|
|
|
|
let res = err.read_to_end(&mut stderr);
|
|
|
|
|
res.unwrap();
|
|
|
|
|
}
|
|
|
|
|
(Some(out), Some(err)) => {
|
|
|
|
|
let res = read2(out.inner, &mut stdout, err.inner, &mut stderr);
|
|
|
|
|
res.unwrap();
|
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
|
let status = self.wait()?;
|
2019-11-27 18:29:00 +00:00
|
|
|
|
Ok(Output { status, stdout, stderr })
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 21:41:59 +00:00
|
|
|
|
/// Terminates the current process with the specified exit code.
|
|
|
|
|
///
|
|
|
|
|
/// This function will never return and will immediately terminate the current
|
|
|
|
|
/// process. The exit code is passed through to the underlying OS and will be
|
|
|
|
|
/// available for consumption by another process.
|
|
|
|
|
///
|
|
|
|
|
/// Note that because this function never returns, and that it terminates the
|
|
|
|
|
/// process, no destructors on the current stack or any other thread's stack
|
|
|
|
|
/// will be run. If a clean shutdown is needed it is recommended to only call
|
|
|
|
|
/// this function at a known point where there are no more destructors left
|
|
|
|
|
/// to run.
|
2016-12-03 23:46:28 +00:00
|
|
|
|
///
|
2016-12-15 21:07:37 +00:00
|
|
|
|
/// ## Platform-specific behavior
|
|
|
|
|
///
|
|
|
|
|
/// **Unix**: On Unix-like platforms, it is unlikely that all 32 bits of `exit`
|
|
|
|
|
/// will be visible to a parent process inspecting the exit code. On most
|
|
|
|
|
/// Unix-like platforms, only the eight least-significant bits are considered.
|
|
|
|
|
///
|
2016-12-03 23:46:28 +00:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2016-12-21 19:42:07 +00:00
|
|
|
|
/// Due to this function’s behavior regarding destructors, a conventional way
|
|
|
|
|
/// to use the function is to extract the actual computation to another
|
|
|
|
|
/// function and compute the exit code from its return value:
|
|
|
|
|
///
|
2016-12-03 23:46:28 +00:00
|
|
|
|
/// ```
|
2016-12-21 19:42:07 +00:00
|
|
|
|
/// fn run_app() -> Result<(), ()> {
|
|
|
|
|
/// // Application logic here
|
|
|
|
|
/// Ok(())
|
|
|
|
|
/// }
|
2016-12-03 23:46:28 +00:00
|
|
|
|
///
|
2016-12-21 19:42:07 +00:00
|
|
|
|
/// fn main() {
|
2019-10-20 18:13:47 +00:00
|
|
|
|
/// std::process::exit(match run_app() {
|
|
|
|
|
/// Ok(_) => 0,
|
|
|
|
|
/// Err(err) => {
|
|
|
|
|
/// eprintln!("error: {:?}", err);
|
|
|
|
|
/// 1
|
|
|
|
|
/// }
|
2016-12-21 19:42:07 +00:00
|
|
|
|
/// });
|
|
|
|
|
/// }
|
2016-12-03 23:46:28 +00:00
|
|
|
|
/// ```
|
2016-12-15 21:07:37 +00:00
|
|
|
|
///
|
|
|
|
|
/// Due to [platform-specific behavior], the exit code for this example will be
|
|
|
|
|
/// `0` on Linux, but `256` on Windows:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process;
|
|
|
|
|
///
|
2017-05-02 00:38:59 +00:00
|
|
|
|
/// process::exit(0x0100);
|
2016-12-15 21:07:37 +00:00
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// [platform-specific behavior]: #platform-specific-behavior
|
2015-03-31 21:41:59 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
pub fn exit(code: i32) -> ! {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
crate::sys_common::cleanup();
|
|
|
|
|
crate::sys::os::exit(code)
|
2015-03-31 21:41:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-17 17:31:14 +00:00
|
|
|
|
/// Terminates the process in an abnormal fashion.
|
|
|
|
|
///
|
|
|
|
|
/// The function will never return and will immediately terminate the current
|
|
|
|
|
/// process in a platform specific "abnormal" manner.
|
|
|
|
|
///
|
|
|
|
|
/// Note that because this function never returns, and that it terminates the
|
|
|
|
|
/// process, no destructors on the current stack or any other thread's stack
|
2017-09-27 21:47:21 +00:00
|
|
|
|
/// will be run.
|
|
|
|
|
///
|
|
|
|
|
/// This is in contrast to the default behaviour of [`panic!`] which unwinds
|
|
|
|
|
/// the current thread's stack and calls all destructors.
|
|
|
|
|
/// When `panic="abort"` is set, either as an argument to `rustc` or in a
|
2017-10-02 18:59:50 +00:00
|
|
|
|
/// crate's Cargo.toml, [`panic!`] and `abort` are similar. However,
|
|
|
|
|
/// [`panic!`] will still call the [panic hook] while `abort` will not.
|
2017-09-27 21:47:21 +00:00
|
|
|
|
///
|
|
|
|
|
/// If a clean shutdown is needed it is recommended to only call
|
2016-11-17 17:31:14 +00:00
|
|
|
|
/// this function at a known point where there are no more destructors left
|
|
|
|
|
/// to run.
|
2017-03-29 13:59:22 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
2017-03-29 14:19:23 +00:00
|
|
|
|
///
|
2017-03-30 07:55:44 +00:00
|
|
|
|
/// ```no_run
|
2017-03-29 13:59:22 +00:00
|
|
|
|
/// use std::process;
|
2017-03-29 14:30:39 +00:00
|
|
|
|
///
|
2017-03-29 13:59:22 +00:00
|
|
|
|
/// fn main() {
|
|
|
|
|
/// println!("aborting");
|
2017-03-29 14:30:39 +00:00
|
|
|
|
///
|
2017-03-29 13:59:22 +00:00
|
|
|
|
/// process::abort();
|
2017-03-29 14:30:39 +00:00
|
|
|
|
///
|
2017-03-29 13:59:22 +00:00
|
|
|
|
/// // execution never gets here
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2017-04-05 18:41:43 +00:00
|
|
|
|
///
|
2017-09-27 20:13:07 +00:00
|
|
|
|
/// The `abort` function terminates the process, so the destructor will not
|
2017-04-06 08:17:32 +00:00
|
|
|
|
/// get run on the example below:
|
2017-04-05 18:41:43 +00:00
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::process;
|
|
|
|
|
///
|
|
|
|
|
/// struct HasDrop;
|
|
|
|
|
///
|
|
|
|
|
/// impl Drop for HasDrop {
|
|
|
|
|
/// fn drop(&mut self) {
|
|
|
|
|
/// println!("This will never be printed!");
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
|
|
|
|
/// let _x = HasDrop;
|
|
|
|
|
/// process::abort();
|
|
|
|
|
/// // the destructor implemented for HasDrop will never get run
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2017-09-27 21:47:21 +00:00
|
|
|
|
///
|
|
|
|
|
/// [`panic!`]: ../../std/macro.panic.html
|
2017-10-02 18:59:50 +00:00
|
|
|
|
/// [panic hook]: ../../std/panic/fn.set_hook.html
|
2017-03-15 03:52:20 +00:00
|
|
|
|
#[stable(feature = "process_abort", since = "1.17.0")]
|
2016-11-17 17:31:14 +00:00
|
|
|
|
pub fn abort() -> ! {
|
2020-05-17 17:37:44 +00:00
|
|
|
|
crate::sys::abort_internal();
|
2016-11-17 17:31:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-06 05:49:36 +00:00
|
|
|
|
/// Returns the OS-assigned process identifier associated with this process.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
2017-10-08 02:59:58 +00:00
|
|
|
|
/// use std::process;
|
2017-10-06 05:49:36 +00:00
|
|
|
|
///
|
2017-10-08 02:59:58 +00:00
|
|
|
|
/// println!("My pid is {}", process::id());
|
2017-10-06 05:49:36 +00:00
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
///
|
2018-04-03 01:34:06 +00:00
|
|
|
|
#[stable(feature = "getpid", since = "1.26.0")]
|
2017-10-08 02:59:58 +00:00
|
|
|
|
pub fn id() -> u32 {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
crate::sys::os::getpid()
|
2017-10-06 05:49:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-22 22:36:55 +00:00
|
|
|
|
/// A trait for implementing arbitrary return types in the `main` function.
|
|
|
|
|
///
|
2019-09-05 16:15:28 +00:00
|
|
|
|
/// The C-main function only supports to return integers as return type.
|
2018-02-22 22:36:55 +00:00
|
|
|
|
/// So, every type implementing the `Termination` trait has to be converted
|
|
|
|
|
/// to an integer.
|
|
|
|
|
///
|
|
|
|
|
/// The default implementations are returning `libc::EXIT_SUCCESS` to indicate
|
|
|
|
|
/// a successful execution. In case of a failure, `libc::EXIT_FAILURE` is returned.
|
|
|
|
|
#[cfg_attr(not(test), lang = "termination")]
|
|
|
|
|
#[unstable(feature = "termination_trait_lib", issue = "43301")]
|
2018-03-22 04:28:48 +00:00
|
|
|
|
#[rustc_on_unimplemented(
|
2019-11-27 18:29:00 +00:00
|
|
|
|
message = "`main` has invalid return type `{Self}`",
|
|
|
|
|
label = "`main` can only return types that implement `{Termination}`"
|
|
|
|
|
)]
|
2018-02-22 22:36:55 +00:00
|
|
|
|
pub trait Termination {
|
|
|
|
|
/// Is called to get the representation of the value as status code.
|
|
|
|
|
/// This status code is returned to the operating system.
|
|
|
|
|
fn report(self) -> i32;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[unstable(feature = "termination_trait_lib", issue = "43301")]
|
|
|
|
|
impl Termination for () {
|
2018-04-05 18:07:19 +00:00
|
|
|
|
#[inline]
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn report(self) -> i32 {
|
|
|
|
|
ExitCode::SUCCESS.report()
|
|
|
|
|
}
|
2018-02-22 22:36:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[unstable(feature = "termination_trait_lib", issue = "43301")]
|
2018-02-24 08:31:33 +00:00
|
|
|
|
impl<E: fmt::Debug> Termination for Result<(), E> {
|
2018-02-22 22:36:55 +00:00
|
|
|
|
fn report(self) -> i32 {
|
|
|
|
|
match self {
|
2018-02-27 18:31:17 +00:00
|
|
|
|
Ok(()) => ().report(),
|
|
|
|
|
Err(err) => Err::<!, _>(err).report(),
|
2018-02-22 22:36:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[unstable(feature = "termination_trait_lib", issue = "43301")]
|
|
|
|
|
impl Termination for ! {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn report(self) -> i32 {
|
|
|
|
|
self
|
|
|
|
|
}
|
2018-02-22 22:36:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[unstable(feature = "termination_trait_lib", issue = "43301")]
|
2018-02-24 08:31:33 +00:00
|
|
|
|
impl<E: fmt::Debug> Termination for Result<!, E> {
|
2018-02-22 22:36:55 +00:00
|
|
|
|
fn report(self) -> i32 {
|
2018-02-24 08:31:33 +00:00
|
|
|
|
let Err(err) = self;
|
|
|
|
|
eprintln!("Error: {:?}", err);
|
2018-02-27 18:31:17 +00:00
|
|
|
|
ExitCode::FAILURE.report()
|
2018-02-22 22:36:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[unstable(feature = "termination_trait_lib", issue = "43301")]
|
2018-02-24 08:31:33 +00:00
|
|
|
|
impl Termination for ExitCode {
|
2018-04-05 18:07:19 +00:00
|
|
|
|
#[inline]
|
2018-02-22 22:36:55 +00:00
|
|
|
|
fn report(self) -> i32 {
|
2018-03-04 02:29:30 +00:00
|
|
|
|
self.0.as_i32()
|
2018-02-22 22:36:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-12 17:58:30 +00:00
|
|
|
|
#[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten", target_env = "sgx"))))]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
mod tests {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::io::prelude::*;
|
2015-03-30 18:00:05 +00:00
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
|
use super::{Command, Output, Stdio};
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::io::ErrorKind;
|
|
|
|
|
use crate::str;
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
|
|
|
|
// FIXME(#10380) these tests should not all be ignored on android.
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn smoke() {
|
2017-01-04 08:57:19 +00:00
|
|
|
|
let p = if cfg!(target_os = "windows") {
|
2017-01-03 12:20:30 +00:00
|
|
|
|
Command::new("cmd").args(&["/C", "exit 0"]).spawn()
|
|
|
|
|
} else {
|
|
|
|
|
Command::new("true").spawn()
|
|
|
|
|
};
|
2015-02-06 17:42:57 +00:00
|
|
|
|
assert!(p.is_ok());
|
|
|
|
|
let mut p = p.unwrap();
|
|
|
|
|
assert!(p.wait().unwrap().success());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2016-01-23 07:49:57 +00:00
|
|
|
|
#[cfg_attr(target_os = "android", ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn smoke_failure() {
|
|
|
|
|
match Command::new("if-this-is-a-binary-then-the-world-has-ended").spawn() {
|
|
|
|
|
Ok(..) => panic!(),
|
|
|
|
|
Err(..) => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn exit_reported_right() {
|
2017-01-04 08:57:19 +00:00
|
|
|
|
let p = if cfg!(target_os = "windows") {
|
2017-01-03 12:20:30 +00:00
|
|
|
|
Command::new("cmd").args(&["/C", "exit 1"]).spawn()
|
|
|
|
|
} else {
|
|
|
|
|
Command::new("false").spawn()
|
|
|
|
|
};
|
2015-02-06 17:42:57 +00:00
|
|
|
|
assert!(p.is_ok());
|
|
|
|
|
let mut p = p.unwrap();
|
|
|
|
|
assert!(p.wait().unwrap().code() == Some(1));
|
2015-03-31 23:20:09 +00:00
|
|
|
|
drop(p.wait());
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-19 04:36:28 +00:00
|
|
|
|
#[test]
|
2016-01-23 07:49:57 +00:00
|
|
|
|
#[cfg(unix)]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn signal_reported_right() {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::os::unix::process::ExitStatusExt;
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
|
let mut p =
|
|
|
|
|
Command::new("/bin/sh").arg("-c").arg("read a").stdin(Stdio::piped()).spawn().unwrap();
|
2015-09-18 17:19:23 +00:00
|
|
|
|
p.kill().unwrap();
|
2015-02-06 17:42:57 +00:00
|
|
|
|
match p.wait().unwrap().signal() {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
Some(9) => {}
|
|
|
|
|
result => panic!("not terminated by signal 9 (instead, {:?})", result),
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run_output(mut cmd: Command) -> String {
|
|
|
|
|
let p = cmd.spawn();
|
|
|
|
|
assert!(p.is_ok());
|
|
|
|
|
let mut p = p.unwrap();
|
|
|
|
|
assert!(p.stdout.is_some());
|
|
|
|
|
let mut ret = String::new();
|
|
|
|
|
p.stdout.as_mut().unwrap().read_to_string(&mut ret).unwrap();
|
|
|
|
|
assert!(p.wait().unwrap().success());
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn stdout_works() {
|
2017-01-04 08:57:19 +00:00
|
|
|
|
if cfg!(target_os = "windows") {
|
2017-01-03 12:20:30 +00:00
|
|
|
|
let mut cmd = Command::new("cmd");
|
|
|
|
|
cmd.args(&["/C", "echo foobar"]).stdout(Stdio::piped());
|
|
|
|
|
assert_eq!(run_output(cmd), "foobar\r\n");
|
|
|
|
|
} else {
|
|
|
|
|
let mut cmd = Command::new("echo");
|
|
|
|
|
cmd.arg("foobar").stdout(Stdio::piped());
|
|
|
|
|
assert_eq!(run_output(cmd), "foobar\n");
|
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(windows, target_os = "android", target_os = "vxworks"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn set_current_dir_works() {
|
|
|
|
|
let mut cmd = Command::new("/bin/sh");
|
2019-11-27 18:29:00 +00:00
|
|
|
|
cmd.arg("-c").arg("pwd").current_dir("/").stdout(Stdio::piped());
|
2015-02-06 17:42:57 +00:00
|
|
|
|
assert_eq!(run_output(cmd), "/\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(windows, target_os = "android", target_os = "vxworks"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn stdin_works() {
|
|
|
|
|
let mut p = Command::new("/bin/sh")
|
2019-11-27 18:29:00 +00:00
|
|
|
|
.arg("-c")
|
|
|
|
|
.arg("read line; echo $line")
|
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
|
.spawn()
|
|
|
|
|
.unwrap();
|
2015-02-06 17:42:57 +00:00
|
|
|
|
p.stdin.as_mut().unwrap().write("foobar".as_bytes()).unwrap();
|
|
|
|
|
drop(p.stdin.take());
|
|
|
|
|
let mut out = String::new();
|
|
|
|
|
p.stdout.as_mut().unwrap().read_to_string(&mut out).unwrap();
|
|
|
|
|
assert!(p.wait().unwrap().success());
|
|
|
|
|
assert_eq!(out, "foobar\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn test_process_status() {
|
2017-01-04 08:57:19 +00:00
|
|
|
|
let mut status = if cfg!(target_os = "windows") {
|
2017-01-03 12:20:30 +00:00
|
|
|
|
Command::new("cmd").args(&["/C", "exit 1"]).status().unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
Command::new("false").status().unwrap()
|
|
|
|
|
};
|
2015-02-06 17:42:57 +00:00
|
|
|
|
assert!(status.code() == Some(1));
|
|
|
|
|
|
2017-01-04 08:57:19 +00:00
|
|
|
|
status = if cfg!(target_os = "windows") {
|
2017-01-03 12:20:30 +00:00
|
|
|
|
Command::new("cmd").args(&["/C", "exit 0"]).status().unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
Command::new("true").status().unwrap()
|
|
|
|
|
};
|
2015-02-06 17:42:57 +00:00
|
|
|
|
assert!(status.success());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_process_output_fail_to_start() {
|
|
|
|
|
match Command::new("/no-binary-by-this-name-should-exist").output() {
|
2015-03-17 01:08:57 +00:00
|
|
|
|
Err(e) => assert_eq!(e.kind(), ErrorKind::NotFound),
|
2019-11-27 18:29:00 +00:00
|
|
|
|
Ok(..) => panic!(),
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn test_process_output_output() {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
let Output { status, stdout, stderr } = if cfg!(target_os = "windows") {
|
|
|
|
|
Command::new("cmd").args(&["/C", "echo hello"]).output().unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
Command::new("echo").arg("hello").output().unwrap()
|
|
|
|
|
};
|
2015-03-30 18:00:05 +00:00
|
|
|
|
let output_str = str::from_utf8(&stdout).unwrap();
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
|
|
|
|
assert!(status.success());
|
|
|
|
|
assert_eq!(output_str.trim().to_string(), "hello");
|
2015-07-27 23:10:59 +00:00
|
|
|
|
assert_eq!(stderr, Vec::new());
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn test_process_output_error() {
|
2019-11-27 18:29:00 +00:00
|
|
|
|
let Output { status, stdout, stderr } = if cfg!(target_os = "windows") {
|
|
|
|
|
Command::new("cmd").args(&["/C", "mkdir ."]).output().unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
Command::new("mkdir").arg("./").output().unwrap()
|
|
|
|
|
};
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
|
|
|
|
assert!(status.code() == Some(1));
|
|
|
|
|
assert_eq!(stdout, Vec::new());
|
|
|
|
|
assert!(!stderr.is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn test_finish_once() {
|
2017-01-04 08:57:19 +00:00
|
|
|
|
let mut prog = if cfg!(target_os = "windows") {
|
2017-01-03 12:20:30 +00:00
|
|
|
|
Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
Command::new("false").spawn().unwrap()
|
|
|
|
|
};
|
2015-02-06 17:42:57 +00:00
|
|
|
|
assert!(prog.wait().unwrap().code() == Some(1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn test_finish_twice() {
|
2017-01-04 08:57:19 +00:00
|
|
|
|
let mut prog = if cfg!(target_os = "windows") {
|
2017-01-03 12:20:30 +00:00
|
|
|
|
Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
Command::new("false").spawn().unwrap()
|
|
|
|
|
};
|
2015-02-06 17:42:57 +00:00
|
|
|
|
assert!(prog.wait().unwrap().code() == Some(1));
|
|
|
|
|
assert!(prog.wait().unwrap().code() == Some(1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(any(target_os = "vxworks", target_os = "android"), ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn test_wait_with_output_once() {
|
2017-01-04 08:57:19 +00:00
|
|
|
|
let prog = if cfg!(target_os = "windows") {
|
2017-01-03 12:20:30 +00:00
|
|
|
|
Command::new("cmd").args(&["/C", "echo hello"]).stdout(Stdio::piped()).spawn().unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
Command::new("echo").arg("hello").stdout(Stdio::piped()).spawn().unwrap()
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
|
let Output { status, stdout, stderr } = prog.wait_with_output().unwrap();
|
2015-03-30 18:00:05 +00:00
|
|
|
|
let output_str = str::from_utf8(&stdout).unwrap();
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
|
|
|
|
assert!(status.success());
|
|
|
|
|
assert_eq!(output_str.trim().to_string(), "hello");
|
2015-07-27 23:10:59 +00:00
|
|
|
|
assert_eq!(stderr, Vec::new());
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
|
#[cfg(all(unix, not(target_os = "android")))]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub fn env_cmd() -> Command {
|
|
|
|
|
Command::new("env")
|
|
|
|
|
}
|
2019-11-27 18:29:00 +00:00
|
|
|
|
#[cfg(target_os = "android")]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
pub fn env_cmd() -> Command {
|
|
|
|
|
let mut cmd = Command::new("/system/bin/sh");
|
|
|
|
|
cmd.arg("-c").arg("set");
|
|
|
|
|
cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
pub fn env_cmd() -> Command {
|
|
|
|
|
let mut cmd = Command::new("cmd");
|
|
|
|
|
cmd.arg("/c").arg("set");
|
|
|
|
|
cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(target_os = "vxworks", ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn test_override_env() {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::env;
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
|
|
|
|
// 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 mut cmd = env_cmd();
|
|
|
|
|
cmd.env_clear().env("RUN_TEST_NEW_ENV", "123");
|
|
|
|
|
if let Some(p) = env::var_os("PATH") {
|
|
|
|
|
cmd.env("PATH", &p);
|
|
|
|
|
}
|
|
|
|
|
let result = cmd.output().unwrap();
|
2015-03-30 18:00:05 +00:00
|
|
|
|
let output = String::from_utf8_lossy(&result.stdout).to_string();
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
|
assert!(
|
|
|
|
|
output.contains("RUN_TEST_NEW_ENV=123"),
|
|
|
|
|
"didn't find RUN_TEST_NEW_ENV inside of:\n\n{}",
|
|
|
|
|
output
|
|
|
|
|
);
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(target_os = "vxworks", ignore)]
|
2015-02-06 17:42:57 +00:00
|
|
|
|
fn test_add_to_env() {
|
|
|
|
|
let result = env_cmd().env("RUN_TEST_NEW_ENV", "123").output().unwrap();
|
2015-03-30 18:00:05 +00:00
|
|
|
|
let output = String::from_utf8_lossy(&result.stdout).to_string();
|
2015-02-06 17:42:57 +00:00
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
|
assert!(
|
|
|
|
|
output.contains("RUN_TEST_NEW_ENV=123"),
|
|
|
|
|
"didn't find RUN_TEST_NEW_ENV inside of:\n\n{}",
|
|
|
|
|
output
|
|
|
|
|
);
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|
2016-01-15 20:29:45 +00:00
|
|
|
|
|
2017-12-17 15:21:47 +00:00
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(target_os = "vxworks", ignore)]
|
2017-12-17 15:21:47 +00:00
|
|
|
|
fn test_capture_env_at_spawn() {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::env;
|
2017-12-17 15:21:47 +00:00
|
|
|
|
|
|
|
|
|
let mut cmd = env_cmd();
|
|
|
|
|
cmd.env("RUN_TEST_NEW_ENV1", "123");
|
|
|
|
|
|
|
|
|
|
// This variable will not be present if the environment has already
|
|
|
|
|
// been captured above.
|
|
|
|
|
env::set_var("RUN_TEST_NEW_ENV2", "456");
|
|
|
|
|
let result = cmd.output().unwrap();
|
|
|
|
|
env::remove_var("RUN_TEST_NEW_ENV2");
|
|
|
|
|
|
|
|
|
|
let output = String::from_utf8_lossy(&result.stdout).to_string();
|
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
|
assert!(
|
|
|
|
|
output.contains("RUN_TEST_NEW_ENV1=123"),
|
|
|
|
|
"didn't find RUN_TEST_NEW_ENV1 inside of:\n\n{}",
|
|
|
|
|
output
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
output.contains("RUN_TEST_NEW_ENV2=456"),
|
|
|
|
|
"didn't find RUN_TEST_NEW_ENV2 inside of:\n\n{}",
|
|
|
|
|
output
|
|
|
|
|
);
|
2017-12-17 15:21:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 20:29:45 +00:00
|
|
|
|
// Regression tests for #30858.
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_interior_nul_in_progname_is_error() {
|
|
|
|
|
match Command::new("has-some-\0\0s-inside").spawn() {
|
|
|
|
|
Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
|
|
|
|
|
Ok(_) => panic!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_interior_nul_in_arg_is_error() {
|
|
|
|
|
match Command::new("echo").arg("has-some-\0\0s-inside").spawn() {
|
|
|
|
|
Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
|
|
|
|
|
Ok(_) => panic!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_interior_nul_in_args_is_error() {
|
|
|
|
|
match Command::new("echo").args(&["has-some-\0\0s-inside"]).spawn() {
|
|
|
|
|
Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
|
|
|
|
|
Ok(_) => panic!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_interior_nul_in_current_dir_is_error() {
|
|
|
|
|
match Command::new("echo").current_dir("has-some-\0\0s-inside").spawn() {
|
|
|
|
|
Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
|
|
|
|
|
Ok(_) => panic!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Regression tests for #30862.
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(target_os = "vxworks", ignore)]
|
2016-01-15 20:29:45 +00:00
|
|
|
|
fn test_interior_nul_in_env_key_is_error() {
|
|
|
|
|
match env_cmd().env("has-some-\0\0s-inside", "value").spawn() {
|
|
|
|
|
Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
|
|
|
|
|
Ok(_) => panic!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-30 09:29:16 +00:00
|
|
|
|
#[cfg_attr(target_os = "vxworks", ignore)]
|
2016-01-15 20:29:45 +00:00
|
|
|
|
fn test_interior_nul_in_env_value_is_error() {
|
|
|
|
|
match env_cmd().env("key", "has-some-\0\0s-inside").spawn() {
|
|
|
|
|
Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
|
|
|
|
|
Ok(_) => panic!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-01 00:44:07 +00:00
|
|
|
|
|
2019-02-09 22:16:58 +00:00
|
|
|
|
/// Tests that process creation flags work by debugging a process.
|
2016-12-01 00:44:07 +00:00
|
|
|
|
/// Other creation flags make it hard or impossible to detect
|
|
|
|
|
/// behavioral changes in the process.
|
|
|
|
|
#[test]
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn test_creation_flags() {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::os::windows::process::CommandExt;
|
|
|
|
|
use crate::sys::c::{BOOL, DWORD, INFINITE};
|
2016-12-01 00:44:07 +00:00
|
|
|
|
#[repr(C, packed)]
|
|
|
|
|
struct DEBUG_EVENT {
|
|
|
|
|
pub event_code: DWORD,
|
|
|
|
|
pub process_id: DWORD,
|
|
|
|
|
pub thread_id: DWORD,
|
|
|
|
|
// This is a union in the real struct, but we don't
|
|
|
|
|
// need this data for the purposes of this test.
|
|
|
|
|
pub _junk: [u8; 164],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "system" {
|
|
|
|
|
fn WaitForDebugEvent(lpDebugEvent: *mut DEBUG_EVENT, dwMilliseconds: DWORD) -> BOOL;
|
2019-11-27 18:29:00 +00:00
|
|
|
|
fn ContinueDebugEvent(
|
|
|
|
|
dwProcessId: DWORD,
|
|
|
|
|
dwThreadId: DWORD,
|
|
|
|
|
dwContinueStatus: DWORD,
|
|
|
|
|
) -> BOOL;
|
2016-12-01 00:44:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const DEBUG_PROCESS: DWORD = 1;
|
|
|
|
|
const EXIT_PROCESS_DEBUG_EVENT: DWORD = 5;
|
|
|
|
|
const DBG_EXCEPTION_NOT_HANDLED: DWORD = 0x80010001;
|
|
|
|
|
|
|
|
|
|
let mut child = Command::new("cmd")
|
2016-12-01 02:31:47 +00:00
|
|
|
|
.creation_flags(DEBUG_PROCESS)
|
2019-11-27 18:29:00 +00:00
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
|
.spawn()
|
|
|
|
|
.unwrap();
|
2016-12-01 00:44:07 +00:00
|
|
|
|
child.stdin.take().unwrap().write_all(b"exit\r\n").unwrap();
|
|
|
|
|
let mut events = 0;
|
2019-11-27 18:29:00 +00:00
|
|
|
|
let mut event = DEBUG_EVENT { event_code: 0, process_id: 0, thread_id: 0, _junk: [0; 164] };
|
2016-12-01 00:44:07 +00:00
|
|
|
|
loop {
|
|
|
|
|
if unsafe { WaitForDebugEvent(&mut event as *mut DEBUG_EVENT, INFINITE) } == 0 {
|
|
|
|
|
panic!("WaitForDebugEvent failed!");
|
|
|
|
|
}
|
|
|
|
|
events += 1;
|
|
|
|
|
|
|
|
|
|
if event.event_code == EXIT_PROCESS_DEBUG_EVENT {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
|
if unsafe {
|
|
|
|
|
ContinueDebugEvent(event.process_id, event.thread_id, DBG_EXCEPTION_NOT_HANDLED)
|
|
|
|
|
} == 0
|
|
|
|
|
{
|
2016-12-01 00:44:07 +00:00
|
|
|
|
panic!("ContinueDebugEvent failed!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert!(events > 0);
|
|
|
|
|
}
|
2018-01-26 15:33:58 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
2020-05-22 16:30:26 +00:00
|
|
|
|
fn test_command_implements_send_sync() {
|
|
|
|
|
fn take_send_sync_type<T: Send + Sync>(_: T) {}
|
|
|
|
|
take_send_sync_type(Command::new(""))
|
2018-01-26 15:33:58 +00:00
|
|
|
|
}
|
2015-02-06 17:42:57 +00:00
|
|
|
|
}
|