2015-04-16 06:21:13 +00:00
|
|
|
|
//! Unix-specific extensions to primitives in the `std::process` module.
|
|
|
|
|
|
|
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
|
2019-11-18 06:56:13 +00:00
|
|
|
|
use crate::ffi::OsStr;
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::io;
|
2019-11-27 18:28:39 +00:00
|
|
|
|
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::process;
|
2021-02-10 21:30:30 +00:00
|
|
|
|
use crate::sealed::Sealed;
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::sys;
|
2019-11-27 18:28:39 +00:00
|
|
|
|
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
|
2015-04-16 06:21:13 +00:00
|
|
|
|
|
2018-04-10 00:44:28 +00:00
|
|
|
|
/// Unix-specific extensions to the [`process::Command`] builder.
|
2021-02-10 21:30:30 +00:00
|
|
|
|
///
|
|
|
|
|
/// This trait is sealed: it cannot be implemented outside the standard library.
|
|
|
|
|
/// This is so that future additional methods are not breaking changes.
|
2015-04-16 06:21:13 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2021-02-10 21:30:30 +00:00
|
|
|
|
pub trait CommandExt: Sealed {
|
2019-02-09 22:16:58 +00:00
|
|
|
|
/// Sets the child process's user ID. This translates to a
|
2015-04-16 06:21:13 +00:00
|
|
|
|
/// `setuid` call in the child process. Failure in the `setuid`
|
|
|
|
|
/// call will cause the spawn to fail.
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-10-07 18:38:25 +00:00
|
|
|
|
fn uid(
|
|
|
|
|
&mut self,
|
|
|
|
|
#[cfg(not(target_os = "vxworks"))] id: u32,
|
|
|
|
|
#[cfg(target_os = "vxworks")] id: u16,
|
|
|
|
|
) -> &mut process::Command;
|
2015-04-16 06:21:13 +00:00
|
|
|
|
|
2019-02-09 22:16:58 +00:00
|
|
|
|
/// Similar to `uid`, but sets the group ID of the child process. This has
|
2015-04-16 06:21:13 +00:00
|
|
|
|
/// the same semantics as the `uid` field.
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2020-10-07 18:38:25 +00:00
|
|
|
|
fn gid(
|
|
|
|
|
&mut self,
|
|
|
|
|
#[cfg(not(target_os = "vxworks"))] id: u32,
|
|
|
|
|
#[cfg(target_os = "vxworks")] id: u16,
|
|
|
|
|
) -> &mut process::Command;
|
2015-06-21 14:43:19 +00:00
|
|
|
|
|
2020-06-16 04:39:34 +00:00
|
|
|
|
/// Sets the supplementary group IDs for the calling process. Translates to
|
|
|
|
|
/// a `setgroups` call in the child process.
|
|
|
|
|
#[unstable(feature = "setgroups", issue = "38527", reason = "")]
|
|
|
|
|
fn groups(
|
|
|
|
|
&mut self,
|
|
|
|
|
#[cfg(not(target_os = "vxworks"))] groups: &[u32],
|
|
|
|
|
#[cfg(target_os = "vxworks")] groups: &[u16],
|
|
|
|
|
) -> &mut process::Command;
|
|
|
|
|
|
2016-02-04 00:55:59 +00:00
|
|
|
|
/// Schedules a closure to be run just before the `exec` function is
|
|
|
|
|
/// invoked.
|
|
|
|
|
///
|
|
|
|
|
/// The closure is allowed to return an I/O error whose OS error code will
|
|
|
|
|
/// be communicated back to the parent and returned as an error from when
|
|
|
|
|
/// the spawn was requested.
|
|
|
|
|
///
|
|
|
|
|
/// Multiple closures can be registered and they will be called in order of
|
|
|
|
|
/// their registration. If a closure returns `Err` then no further closures
|
|
|
|
|
/// will be called and the spawn operation will immediately return with a
|
|
|
|
|
/// failure.
|
|
|
|
|
///
|
2019-02-01 18:57:06 +00:00
|
|
|
|
/// # Notes and Safety
|
2016-02-04 00:55:59 +00:00
|
|
|
|
///
|
|
|
|
|
/// This closure will be run in the context of the child process after a
|
2017-08-11 18:34:14 +00:00
|
|
|
|
/// `fork`. This primarily means that any modifications made to memory on
|
2016-02-04 00:55:59 +00:00
|
|
|
|
/// behalf of this closure will **not** be visible to the parent process.
|
|
|
|
|
/// This is often a very constrained environment where normal operations
|
|
|
|
|
/// like `malloc` or acquiring a mutex are not guaranteed to work (due to
|
|
|
|
|
/// other threads perhaps still running when the `fork` was run).
|
|
|
|
|
///
|
2019-02-01 18:53:32 +00:00
|
|
|
|
/// This also means that all resources such as file descriptors and
|
|
|
|
|
/// memory-mapped regions got duplicated. It is your responsibility to make
|
|
|
|
|
/// sure that the closure does not violate library invariants by making
|
2019-02-03 10:17:59 +00:00
|
|
|
|
/// invalid use of these duplicates.
|
2019-02-01 18:53:32 +00:00
|
|
|
|
///
|
2016-02-04 00:55:59 +00:00
|
|
|
|
/// When this closure is run, aspects such as the stdio file descriptors and
|
|
|
|
|
/// working directory have successfully been changed, so output to these
|
|
|
|
|
/// locations may not appear where intended.
|
2019-02-01 18:53:32 +00:00
|
|
|
|
#[stable(feature = "process_pre_exec", since = "1.34.0")]
|
|
|
|
|
unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
|
2019-11-27 18:28:39 +00:00
|
|
|
|
where
|
|
|
|
|
F: FnMut() -> io::Result<()> + Send + Sync + 'static;
|
2019-02-01 18:53:32 +00:00
|
|
|
|
|
|
|
|
|
/// Schedules a closure to be run just before the `exec` function is
|
|
|
|
|
/// invoked.
|
|
|
|
|
///
|
2019-02-02 10:05:43 +00:00
|
|
|
|
/// This method is stable and usable, but it should be unsafe. To fix
|
|
|
|
|
/// that, it got deprecated in favor of the unsafe [`pre_exec`].
|
2019-02-01 18:53:32 +00:00
|
|
|
|
///
|
2020-08-18 16:41:20 +00:00
|
|
|
|
/// [`pre_exec`]: CommandExt::pre_exec
|
2016-12-14 20:22:15 +00:00
|
|
|
|
#[stable(feature = "process_exec", since = "1.15.0")]
|
2019-02-02 10:00:55 +00:00
|
|
|
|
#[rustc_deprecated(since = "1.37.0", reason = "should be unsafe, use `pre_exec` instead")]
|
2016-02-04 00:55:59 +00:00
|
|
|
|
fn before_exec<F>(&mut self, f: F) -> &mut process::Command
|
2019-11-27 18:28:39 +00:00
|
|
|
|
where
|
|
|
|
|
F: FnMut() -> io::Result<()> + Send + Sync + 'static,
|
2019-02-01 18:53:32 +00:00
|
|
|
|
{
|
|
|
|
|
unsafe { self.pre_exec(f) }
|
|
|
|
|
}
|
2016-02-04 19:16:32 +00:00
|
|
|
|
|
|
|
|
|
/// Performs all the required setup by this `Command`, followed by calling
|
|
|
|
|
/// the `execvp` syscall.
|
|
|
|
|
///
|
|
|
|
|
/// On success this function will not return, and otherwise it will return
|
|
|
|
|
/// an error indicating why the exec (or another part of the setup of the
|
|
|
|
|
/// `Command`) failed.
|
|
|
|
|
///
|
2016-12-21 19:42:07 +00:00
|
|
|
|
/// `exec` not returning has the same implications as calling
|
|
|
|
|
/// [`process::exit`] – no destructors on the current stack or any other
|
|
|
|
|
/// thread’s stack will be run. Therefore, it is recommended to only call
|
|
|
|
|
/// `exec` at a point where it is fine to not run any destructors. Note,
|
|
|
|
|
/// that the `execvp` syscall independently guarantees that all memory is
|
|
|
|
|
/// freed and all file descriptors with the `CLOEXEC` option (set by default
|
|
|
|
|
/// on all file descriptors opened by the standard library) are closed.
|
|
|
|
|
///
|
2016-02-04 19:16:32 +00:00
|
|
|
|
/// This function, unlike `spawn`, will **not** `fork` the process to create
|
|
|
|
|
/// a new child. Like spawn, however, the default behavior for the stdio
|
|
|
|
|
/// descriptors will be to inherited from the current process.
|
|
|
|
|
///
|
|
|
|
|
/// # Notes
|
|
|
|
|
///
|
|
|
|
|
/// The process may be in a "broken state" if this function returns in
|
|
|
|
|
/// error. For example the working directory, environment variables, signal
|
|
|
|
|
/// handling settings, various user/group information, or aspects of stdio
|
|
|
|
|
/// file descriptors may have changed. If a "transactional spawn" is
|
|
|
|
|
/// required to gracefully handle errors it is recommended to use the
|
|
|
|
|
/// cross-platform `spawn` instead.
|
2016-04-07 17:42:53 +00:00
|
|
|
|
#[stable(feature = "process_exec2", since = "1.9.0")]
|
2016-02-04 19:16:32 +00:00
|
|
|
|
fn exec(&mut self) -> io::Error;
|
2019-11-18 06:56:13 +00:00
|
|
|
|
|
|
|
|
|
/// Set executable argument
|
|
|
|
|
///
|
|
|
|
|
/// Set the first process argument, `argv[0]`, to something other than the
|
|
|
|
|
/// default executable path.
|
2020-05-12 00:19:25 +00:00
|
|
|
|
#[stable(feature = "process_set_argv0", since = "1.45.0")]
|
2019-11-18 06:56:13 +00:00
|
|
|
|
fn arg0<S>(&mut self, arg: S) -> &mut process::Command
|
2019-11-27 18:28:39 +00:00
|
|
|
|
where
|
|
|
|
|
S: AsRef<OsStr>;
|
2015-04-16 06:21:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl CommandExt for process::Command {
|
2020-10-07 18:38:25 +00:00
|
|
|
|
fn uid(
|
|
|
|
|
&mut self,
|
|
|
|
|
#[cfg(not(target_os = "vxworks"))] id: u32,
|
|
|
|
|
#[cfg(target_os = "vxworks")] id: u16,
|
|
|
|
|
) -> &mut process::Command {
|
2016-01-15 20:29:45 +00:00
|
|
|
|
self.as_inner_mut().uid(id);
|
2015-04-16 06:21:13 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 18:38:25 +00:00
|
|
|
|
fn gid(
|
|
|
|
|
&mut self,
|
|
|
|
|
#[cfg(not(target_os = "vxworks"))] id: u32,
|
|
|
|
|
#[cfg(target_os = "vxworks")] id: u16,
|
|
|
|
|
) -> &mut process::Command {
|
2016-01-15 20:29:45 +00:00
|
|
|
|
self.as_inner_mut().gid(id);
|
2015-04-16 06:21:13 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
2015-06-21 14:43:19 +00:00
|
|
|
|
|
2020-06-16 04:39:34 +00:00
|
|
|
|
fn groups(
|
|
|
|
|
&mut self,
|
|
|
|
|
#[cfg(not(target_os = "vxworks"))] groups: &[u32],
|
|
|
|
|
#[cfg(target_os = "vxworks")] groups: &[u16],
|
|
|
|
|
) -> &mut process::Command {
|
|
|
|
|
self.as_inner_mut().groups(groups);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 18:53:32 +00:00
|
|
|
|
unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
|
2019-11-27 18:28:39 +00:00
|
|
|
|
where
|
|
|
|
|
F: FnMut() -> io::Result<()> + Send + Sync + 'static,
|
2016-02-04 00:55:59 +00:00
|
|
|
|
{
|
2019-02-01 18:53:32 +00:00
|
|
|
|
self.as_inner_mut().pre_exec(Box::new(f));
|
2016-02-04 00:55:59 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
2016-02-04 19:16:32 +00:00
|
|
|
|
|
|
|
|
|
fn exec(&mut self) -> io::Error {
|
|
|
|
|
self.as_inner_mut().exec(sys::process::Stdio::Inherit)
|
|
|
|
|
}
|
2019-11-18 06:56:13 +00:00
|
|
|
|
|
|
|
|
|
fn arg0<S>(&mut self, arg: S) -> &mut process::Command
|
2019-11-27 18:28:39 +00:00
|
|
|
|
where
|
|
|
|
|
S: AsRef<OsStr>,
|
2019-11-18 06:56:13 +00:00
|
|
|
|
{
|
|
|
|
|
self.as_inner_mut().set_arg_0(arg.as_ref());
|
|
|
|
|
self
|
|
|
|
|
}
|
2015-04-16 06:21:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-10 00:44:28 +00:00
|
|
|
|
/// Unix-specific extensions to [`process::ExitStatus`].
|
2021-01-04 17:11:41 +00:00
|
|
|
|
///
|
2021-01-11 02:11:00 +00:00
|
|
|
|
/// This trait is sealed: it cannot be implemented outside the standard library.
|
2021-01-04 17:11:41 +00:00
|
|
|
|
/// This is so that future additional methods are not breaking changes.
|
2015-04-16 06:21:13 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2021-02-10 21:30:30 +00:00
|
|
|
|
pub trait ExitStatusExt: Sealed {
|
2016-04-26 22:23:46 +00:00
|
|
|
|
/// Creates a new `ExitStatus` from the raw underlying `i32` return value of
|
|
|
|
|
/// a process.
|
2016-08-11 21:08:24 +00:00
|
|
|
|
#[stable(feature = "exit_status_from", since = "1.12.0")]
|
2016-04-26 22:23:46 +00:00
|
|
|
|
fn from_raw(raw: i32) -> Self;
|
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
|
/// If the process was terminated by a signal, returns that signal.
|
2020-12-12 21:37:34 +00:00
|
|
|
|
///
|
2020-12-13 11:03:37 +00:00
|
|
|
|
/// In other words, if `WIFSIGNALED`, this returns `WTERMSIG`.
|
2015-04-16 06:21:13 +00:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
fn signal(&self) -> Option<i32>;
|
2020-12-12 21:41:55 +00:00
|
|
|
|
|
2020-12-12 21:44:13 +00:00
|
|
|
|
/// If the process was terminated by a signal, says whether it dumped core.
|
2021-01-04 17:37:34 +00:00
|
|
|
|
#[unstable(feature = "unix_process_wait_more", issue = "80695")]
|
2020-12-12 21:44:13 +00:00
|
|
|
|
fn core_dumped(&self) -> bool;
|
|
|
|
|
|
2020-12-12 21:47:47 +00:00
|
|
|
|
/// If the process was stopped by a signal, returns that signal.
|
|
|
|
|
///
|
2020-12-13 11:03:17 +00:00
|
|
|
|
/// In other words, if `WIFSTOPPED`, this returns `WSTOPSIG`. This is only possible if the status came from
|
2020-12-12 21:47:47 +00:00
|
|
|
|
/// a `wait` system call which was passed `WUNTRACED`, was then converted into an `ExitStatus`.
|
2021-01-04 17:37:34 +00:00
|
|
|
|
#[unstable(feature = "unix_process_wait_more", issue = "80695")]
|
2020-12-12 21:47:47 +00:00
|
|
|
|
fn stopped_signal(&self) -> Option<i32>;
|
|
|
|
|
|
2020-12-12 21:52:17 +00:00
|
|
|
|
/// Whether the process was continued from a stopped status.
|
|
|
|
|
///
|
|
|
|
|
/// Ie, `WIFCONTINUED`. This is only possible if the status came from a `wait` system call
|
|
|
|
|
/// which was passed `WCONTINUED`, was then converted into an `ExitStatus`.
|
2021-01-04 17:37:34 +00:00
|
|
|
|
#[unstable(feature = "unix_process_wait_more", issue = "80695")]
|
2020-12-12 21:52:17 +00:00
|
|
|
|
fn continued(&self) -> bool;
|
|
|
|
|
|
2020-12-12 21:41:55 +00:00
|
|
|
|
/// Returns the underlying raw `wait` status.
|
2021-01-04 17:37:34 +00:00
|
|
|
|
#[unstable(feature = "unix_process_wait_more", issue = "80695")]
|
2020-12-12 21:41:55 +00:00
|
|
|
|
fn into_raw(self) -> i32;
|
2015-04-16 06:21:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl ExitStatusExt for process::ExitStatus {
|
2016-04-26 22:23:46 +00:00
|
|
|
|
fn from_raw(raw: i32) -> Self {
|
|
|
|
|
process::ExitStatus::from_inner(From::from(raw))
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-16 06:21:13 +00:00
|
|
|
|
fn signal(&self) -> Option<i32> {
|
2015-10-29 20:45:56 +00:00
|
|
|
|
self.as_inner().signal()
|
2015-04-16 06:21:13 +00:00
|
|
|
|
}
|
2020-12-12 21:41:55 +00:00
|
|
|
|
|
2020-12-12 21:44:13 +00:00
|
|
|
|
fn core_dumped(&self) -> bool {
|
|
|
|
|
self.as_inner().core_dumped()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 21:47:47 +00:00
|
|
|
|
fn stopped_signal(&self) -> Option<i32> {
|
|
|
|
|
self.as_inner().stopped_signal()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 21:52:17 +00:00
|
|
|
|
fn continued(&self) -> bool {
|
|
|
|
|
self.as_inner().continued()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 21:41:55 +00:00
|
|
|
|
fn into_raw(self) -> i32 {
|
|
|
|
|
self.as_inner().into_raw().into()
|
|
|
|
|
}
|
2015-04-16 06:21:13 +00:00
|
|
|
|
}
|
2015-05-12 18:03:49 +00:00
|
|
|
|
|
2015-06-09 23:41:14 +00:00
|
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 18:03:49 +00:00
|
|
|
|
impl FromRawFd for process::Stdio {
|
|
|
|
|
unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio {
|
2016-02-04 19:10:37 +00:00
|
|
|
|
let fd = sys::fd::FileDesc::new(fd);
|
|
|
|
|
let io = sys::process::Stdio::Fd(fd);
|
|
|
|
|
process::Stdio::from_inner(io)
|
2015-05-12 18:03:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-09 23:41:14 +00:00
|
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 18:03:49 +00:00
|
|
|
|
impl AsRawFd for process::ChildStdin {
|
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
|
self.as_inner().fd().raw()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-09 23:41:14 +00:00
|
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 18:03:49 +00:00
|
|
|
|
impl AsRawFd for process::ChildStdout {
|
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
|
self.as_inner().fd().raw()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-09 23:41:14 +00:00
|
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 18:03:49 +00:00
|
|
|
|
impl AsRawFd for process::ChildStderr {
|
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
|
self.as_inner().fd().raw()
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
|
|
2016-09-28 10:28:42 +00:00
|
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-16 06:31:24 +00:00
|
|
|
|
impl IntoRawFd for process::ChildStdin {
|
|
|
|
|
fn into_raw_fd(self) -> RawFd {
|
|
|
|
|
self.into_inner().into_fd().into_raw()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 10:28:42 +00:00
|
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-16 06:31:24 +00:00
|
|
|
|
impl IntoRawFd for process::ChildStdout {
|
|
|
|
|
fn into_raw_fd(self) -> RawFd {
|
|
|
|
|
self.into_inner().into_fd().into_raw()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 10:28:42 +00:00
|
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-16 06:31:24 +00:00
|
|
|
|
impl IntoRawFd for process::ChildStderr {
|
|
|
|
|
fn into_raw_fd(self) -> RawFd {
|
|
|
|
|
self.into_inner().into_fd().into_raw()
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-19 05:09:18 +00:00
|
|
|
|
|
|
|
|
|
/// Returns the OS-assigned process identifier associated with this process's parent.
|
2018-04-04 01:47:37 +00:00
|
|
|
|
#[stable(feature = "unix_ppid", since = "1.27.0")]
|
2017-11-19 05:09:18 +00:00
|
|
|
|
pub fn parent_id() -> u32 {
|
2019-02-10 19:23:21 +00:00
|
|
|
|
crate::sys::os::getppid()
|
2017-11-19 05:09:18 +00:00
|
|
|
|
}
|