2021-09-17 23:23:21 +05:30
|
|
|
|
//! Unix-specific extensions to primitives in the [`std::process`] module.
|
|
|
|
|
|
//!
|
|
|
|
|
|
//! [`std::process`]: crate::process
|
2015-04-15 23:21:13 -07:00
|
|
|
|
|
|
|
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
|
|
2023-01-10 10:44:05 +01:00
|
|
|
|
use cfg_if::cfg_if;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
|
2019-11-17 22:56:13 -08:00
|
|
|
|
use crate::ffi::OsStr;
|
2021-08-19 12:24:25 -07:00
|
|
|
|
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
|
2025-02-27 22:00:15 +00:00
|
|
|
|
use crate::path::Path;
|
2021-02-10 21:30:30 +00:00
|
|
|
|
use crate::sealed::Sealed;
|
2019-02-11 04:23:21 +09:00
|
|
|
|
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
|
2023-01-10 10:44:05 +01:00
|
|
|
|
use crate::{io, process, sys};
|
2022-06-07 19:57:15 -07:00
|
|
|
|
|
2023-01-10 10:44:05 +01:00
|
|
|
|
cfg_if! {
|
2023-06-04 17:44:43 +03:00
|
|
|
|
if #[cfg(any(target_os = "vxworks", target_os = "espidf", target_os = "horizon", target_os = "vita"))] {
|
2023-01-10 10:44:05 +01:00
|
|
|
|
type UserId = u16;
|
|
|
|
|
|
type GroupId = u16;
|
|
|
|
|
|
} else if #[cfg(target_os = "nto")] {
|
|
|
|
|
|
// Both IDs are signed, see `sys/target_nto.h` of the QNX Neutrino SDP.
|
|
|
|
|
|
// Only positive values should be used, see e.g.
|
|
|
|
|
|
// https://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.lib_ref/topic/s/setuid.html
|
|
|
|
|
|
type UserId = i32;
|
|
|
|
|
|
type GroupId = i32;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
type UserId = u32;
|
|
|
|
|
|
type GroupId = u32;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-06-07 19:57:15 -07:00
|
|
|
|
|
2018-04-09 17:44:28 -07: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-15 23:21:13 -07: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-15 23:21:13 -07:00
|
|
|
|
/// `setuid` call in the child process. Failure in the `setuid`
|
|
|
|
|
|
/// call will cause the spawn to fail.
|
2022-04-12 07:46:07 -07:00
|
|
|
|
///
|
|
|
|
|
|
/// # Notes
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This will also trigger a call to `setgroups(0, NULL)` in the child
|
|
|
|
|
|
/// process if no groups have been specified.
|
|
|
|
|
|
/// This removes supplementary groups that might have given the child
|
|
|
|
|
|
/// unwanted permissions.
|
2015-04-15 23:21:13 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-06-07 19:57:15 -07:00
|
|
|
|
fn uid(&mut self, id: UserId) -> &mut process::Command;
|
2015-04-15 23:21:13 -07: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-15 23:21:13 -07:00
|
|
|
|
/// the same semantics as the `uid` field.
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-06-07 19:57:15 -07:00
|
|
|
|
fn gid(&mut self, id: GroupId) -> &mut process::Command;
|
2015-06-21 16:43:19 +02:00
|
|
|
|
|
2020-06-15 21:39:34 -07:00
|
|
|
|
/// Sets the supplementary group IDs for the calling process. Translates to
|
|
|
|
|
|
/// a `setgroups` call in the child process.
|
2021-11-09 17:28:56 -08:00
|
|
|
|
#[unstable(feature = "setgroups", issue = "90747")]
|
2022-06-07 19:57:15 -07:00
|
|
|
|
fn groups(&mut self, groups: &[GroupId]) -> &mut process::Command;
|
2020-06-15 21:39:34 -07:00
|
|
|
|
|
2016-02-03 16:55:59 -08: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 19:57:06 +01:00
|
|
|
|
/// # Notes and Safety
|
2016-02-03 16:55:59 -08:00
|
|
|
|
///
|
|
|
|
|
|
/// This closure will be run in the context of the child process after a
|
2017-08-11 20:34:14 +02:00
|
|
|
|
/// `fork`. This primarily means that any modifications made to memory on
|
2016-02-03 16:55:59 -08:00
|
|
|
|
/// behalf of this closure will **not** be visible to the parent process.
|
|
|
|
|
|
/// This is often a very constrained environment where normal operations
|
2021-03-09 21:42:38 +01:00
|
|
|
|
/// like `malloc`, accessing environment variables through [`std::env`]
|
|
|
|
|
|
/// or acquiring a mutex are not guaranteed to work (due to
|
2016-02-03 16:55:59 -08:00
|
|
|
|
/// other threads perhaps still running when the `fork` was run).
|
|
|
|
|
|
///
|
2021-03-09 21:42:38 +01:00
|
|
|
|
/// For further details refer to the [POSIX fork() specification]
|
|
|
|
|
|
/// and the equivalent documentation for any targeted
|
|
|
|
|
|
/// platform, especially the requirements around *async-signal-safety*.
|
|
|
|
|
|
///
|
2019-02-01 19:53:32 +01: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 11:17:59 +01:00
|
|
|
|
/// invalid use of these duplicates.
|
2019-02-01 19:53:32 +01:00
|
|
|
|
///
|
2021-02-07 13:41:49 +00:00
|
|
|
|
/// Panicking in the closure is safe only if all the format arguments for the
|
|
|
|
|
|
/// panic message can be safely formatted; this is because although
|
|
|
|
|
|
/// `Command` calls [`std::panic::always_abort`](crate::panic::always_abort)
|
|
|
|
|
|
/// before calling the pre_exec hook, panic will still try to format the
|
|
|
|
|
|
/// panic message.
|
|
|
|
|
|
///
|
2016-02-03 16:55:59 -08:00
|
|
|
|
/// When this closure is run, aspects such as the stdio file descriptors and
|
|
|
|
|
|
/// working directory have successfully been changed, so output to these
|
2021-07-23 19:14:28 -04:00
|
|
|
|
/// locations might not appear where intended.
|
2021-03-09 21:42:38 +01:00
|
|
|
|
///
|
|
|
|
|
|
/// [POSIX fork() specification]:
|
|
|
|
|
|
/// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html
|
|
|
|
|
|
/// [`std::env`]: mod@crate::env
|
2019-02-01 19:53:32 +01:00
|
|
|
|
#[stable(feature = "process_pre_exec", since = "1.34.0")]
|
|
|
|
|
|
unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
|
|
|
|
|
|
where
|
|
|
|
|
|
F: FnMut() -> io::Result<()> + Send + Sync + 'static;
|
|
|
|
|
|
|
|
|
|
|
|
/// Schedules a closure to be run just before the `exec` function is
|
|
|
|
|
|
/// invoked.
|
|
|
|
|
|
///
|
2024-06-04 11:50:18 +02:00
|
|
|
|
/// `before_exec` used to be a safe method, but it needs to be unsafe since the closure may only
|
|
|
|
|
|
/// perform operations that are *async-signal-safe*. Hence it got deprecated in favor of the
|
|
|
|
|
|
/// unsafe [`pre_exec`]. Meanwhile, Rust gained the ability to make an existing safe method
|
|
|
|
|
|
/// fully unsafe in a new edition, which is how `before_exec` became `unsafe`. It still also
|
|
|
|
|
|
/// remains deprecated; `pre_exec` should be used instead.
|
2019-02-01 19:53:32 +01:00
|
|
|
|
///
|
2020-08-18 22:11:20 +05:30
|
|
|
|
/// [`pre_exec`]: CommandExt::pre_exec
|
2016-12-14 12:22:15 -08:00
|
|
|
|
#[stable(feature = "process_exec", since = "1.15.0")]
|
2022-04-07 21:20:32 -04:00
|
|
|
|
#[deprecated(since = "1.37.0", note = "should be unsafe, use `pre_exec` instead")]
|
2024-09-05 17:23:40 +01:00
|
|
|
|
#[rustc_deprecated_safe_2024(audit_that = "the closure is async-signal-safe")]
|
2024-06-04 11:50:18 +02:00
|
|
|
|
unsafe fn before_exec<F>(&mut self, f: F) -> &mut process::Command
|
2019-02-01 19:53:32 +01:00
|
|
|
|
where
|
|
|
|
|
|
F: FnMut() -> io::Result<()> + Send + Sync + 'static,
|
|
|
|
|
|
{
|
|
|
|
|
|
unsafe { self.pre_exec(f) }
|
|
|
|
|
|
}
|
2016-02-04 11:16:32 -08: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 21:42:07 +02: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 11:16:32 -08:00
|
|
|
|
/// This function, unlike `spawn`, will **not** `fork` the process to create
|
|
|
|
|
|
/// a new child. Like spawn, however, the default behavior for the stdio
|
2024-11-04 20:42:21 +02:00
|
|
|
|
/// descriptors will be to inherit them from the current process.
|
2016-02-04 11:16:32 -08:00
|
|
|
|
///
|
|
|
|
|
|
/// # 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 10:42:53 -07:00
|
|
|
|
#[stable(feature = "process_exec2", since = "1.9.0")]
|
2024-10-17 05:46:11 -07:00
|
|
|
|
#[must_use]
|
2016-02-04 11:16:32 -08:00
|
|
|
|
fn exec(&mut self) -> io::Error;
|
2019-11-17 22:56:13 -08:00
|
|
|
|
|
|
|
|
|
|
/// Set executable argument
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Set the first process argument, `argv[0]`, to something other than the
|
|
|
|
|
|
/// default executable path.
|
2020-05-11 17:19:25 -07:00
|
|
|
|
#[stable(feature = "process_set_argv0", since = "1.45.0")]
|
2019-11-17 22:56:13 -08:00
|
|
|
|
fn arg0<S>(&mut self, arg: S) -> &mut process::Command
|
|
|
|
|
|
where
|
|
|
|
|
|
S: AsRef<OsStr>;
|
2022-02-03 09:40:45 +00:00
|
|
|
|
|
2022-07-16 14:45:16 -07:00
|
|
|
|
/// Sets the process group ID (PGID) of the child process. Equivalent to a
|
2022-06-16 00:12:03 +02:00
|
|
|
|
/// `setpgid` call in the child process, but may be more efficient.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Process groups determine which processes receive signals.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Pressing Ctrl-C in a terminal will send SIGINT to all processes in
|
|
|
|
|
|
/// the current foreground process group. By spawning the `sleep`
|
|
|
|
|
|
/// subprocess in a new process group, it will not receive SIGINT from the
|
|
|
|
|
|
/// terminal.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The parent process could install a signal handler and manage the
|
|
|
|
|
|
/// subprocess on its own terms.
|
|
|
|
|
|
///
|
2022-07-16 14:45:16 -07:00
|
|
|
|
/// A process group ID of 0 will use the process ID as the PGID.
|
|
|
|
|
|
///
|
2022-06-16 00:12:03 +02:00
|
|
|
|
/// ```no_run
|
|
|
|
|
|
/// use std::process::Command;
|
|
|
|
|
|
/// use std::os::unix::process::CommandExt;
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Command::new("sleep")
|
|
|
|
|
|
/// .arg("10")
|
|
|
|
|
|
/// .process_group(0)
|
|
|
|
|
|
/// .spawn()?
|
|
|
|
|
|
/// .wait()?;
|
|
|
|
|
|
/// #
|
|
|
|
|
|
/// # Ok::<_, Box<dyn std::error::Error>>(())
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
#[stable(feature = "process_set_process_group", since = "1.64.0")]
|
2022-02-03 09:40:45 +00:00
|
|
|
|
fn process_group(&mut self, pgroup: i32) -> &mut process::Command;
|
2025-02-27 22:00:15 +00:00
|
|
|
|
|
|
|
|
|
|
/// Set the root of the child process. This calls `chroot` in the child process before executing
|
|
|
|
|
|
/// the command.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This happens before changing to the directory specified with `Command::current_dir`, and
|
|
|
|
|
|
/// that directory will be relative to the new root. If no directory has been specified with
|
|
|
|
|
|
/// `Command::current_dir`, this will set the directory to `/`, to avoid leaving the current
|
|
|
|
|
|
/// directory outside the chroot.
|
|
|
|
|
|
#[unstable(feature = "process_chroot", issue = "none")]
|
|
|
|
|
|
fn chroot<P: AsRef<Path>>(&mut self, dir: P) -> &mut process::Command;
|
2015-04-15 23:21:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
|
impl CommandExt for process::Command {
|
2022-06-07 19:57:15 -07:00
|
|
|
|
fn uid(&mut self, id: UserId) -> &mut process::Command {
|
2016-01-15 15:29:45 -05:00
|
|
|
|
self.as_inner_mut().uid(id);
|
2015-04-15 23:21:13 -07:00
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-07 19:57:15 -07:00
|
|
|
|
fn gid(&mut self, id: GroupId) -> &mut process::Command {
|
2016-01-15 15:29:45 -05:00
|
|
|
|
self.as_inner_mut().gid(id);
|
2015-04-15 23:21:13 -07:00
|
|
|
|
self
|
|
|
|
|
|
}
|
2015-06-21 16:43:19 +02:00
|
|
|
|
|
2022-06-07 19:57:15 -07:00
|
|
|
|
fn groups(&mut self, groups: &[GroupId]) -> &mut process::Command {
|
2020-06-15 21:39:34 -07:00
|
|
|
|
self.as_inner_mut().groups(groups);
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-01 19:53:32 +01:00
|
|
|
|
unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
|
2016-02-03 16:55:59 -08:00
|
|
|
|
where
|
|
|
|
|
|
F: FnMut() -> io::Result<()> + Send + Sync + 'static,
|
|
|
|
|
|
{
|
2019-02-01 19:53:32 +01:00
|
|
|
|
self.as_inner_mut().pre_exec(Box::new(f));
|
2016-02-03 16:55:59 -08:00
|
|
|
|
self
|
|
|
|
|
|
}
|
2016-02-04 11:16:32 -08:00
|
|
|
|
|
|
|
|
|
|
fn exec(&mut self) -> io::Error {
|
2021-02-23 20:19:15 -08:00
|
|
|
|
// NOTE: This may *not* be safe to call after `libc::fork`, because it
|
|
|
|
|
|
// may allocate. That may be worth fixing at some point in the future.
|
2016-02-04 11:16:32 -08:00
|
|
|
|
self.as_inner_mut().exec(sys::process::Stdio::Inherit)
|
|
|
|
|
|
}
|
2019-11-17 22:56:13 -08:00
|
|
|
|
|
|
|
|
|
|
fn arg0<S>(&mut self, arg: S) -> &mut process::Command
|
|
|
|
|
|
where
|
|
|
|
|
|
S: AsRef<OsStr>,
|
|
|
|
|
|
{
|
|
|
|
|
|
self.as_inner_mut().set_arg_0(arg.as_ref());
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
2022-02-03 09:40:45 +00:00
|
|
|
|
|
|
|
|
|
|
fn process_group(&mut self, pgroup: i32) -> &mut process::Command {
|
|
|
|
|
|
self.as_inner_mut().pgroup(pgroup);
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
2025-02-27 22:00:15 +00:00
|
|
|
|
|
|
|
|
|
|
fn chroot<P: AsRef<Path>>(&mut self, dir: P) -> &mut process::Command {
|
|
|
|
|
|
self.as_inner_mut().chroot(dir.as_ref());
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
2015-04-15 23:21:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-24 14:52:16 +00:00
|
|
|
|
/// Unix-specific extensions to [`process::ExitStatus`] and
|
|
|
|
|
|
/// [`ExitStatusError`](process::ExitStatusError).
|
2021-01-04 17:11:41 +00:00
|
|
|
|
///
|
2021-05-11 18:09:03 +01:00
|
|
|
|
/// On Unix, `ExitStatus` **does not necessarily represent an exit status**, as
|
2021-11-08 17:28:02 +00:00
|
|
|
|
/// passed to the `_exit` system call or returned by
|
2021-05-11 18:09:03 +01:00
|
|
|
|
/// [`ExitStatus::code()`](crate::process::ExitStatus::code). It represents **any wait status**
|
|
|
|
|
|
/// as returned by one of the `wait` family of system
|
2021-02-24 14:52:16 +00:00
|
|
|
|
/// calls.
|
2021-02-22 15:26:19 +00:00
|
|
|
|
///
|
2021-02-24 14:52:16 +00:00
|
|
|
|
/// A Unix wait status (a Rust `ExitStatus`) can represent a Unix exit status, but can also
|
|
|
|
|
|
/// represent other kinds of process event.
|
2021-02-22 15:26:19 +00:00
|
|
|
|
///
|
2021-01-10 18:11:00 -08: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-15 23:21:13 -07:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2021-02-10 21:30:30 +00:00
|
|
|
|
pub trait ExitStatusExt: Sealed {
|
2021-02-24 14:52:16 +00:00
|
|
|
|
/// Creates a new `ExitStatus` or `ExitStatusError` from the raw underlying integer status
|
|
|
|
|
|
/// value from `wait`
|
2021-02-22 15:26:19 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// The value should be a **wait status, not an exit status**.
|
2021-02-24 14:52:16 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// # Panics
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Panics on an attempt to make an `ExitStatusError` from a wait status of `0`.
|
|
|
|
|
|
///
|
2021-06-25 13:18:56 -07:00
|
|
|
|
/// Making an `ExitStatus` always succeeds and never panics.
|
2016-08-11 14:08:24 -07:00
|
|
|
|
#[stable(feature = "exit_status_from", since = "1.12.0")]
|
2016-04-26 15:23:46 -07:00
|
|
|
|
fn from_raw(raw: i32) -> Self;
|
|
|
|
|
|
|
2015-04-15 23:21:13 -07: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-15 23:21:13 -07: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-10-22 10:47:34 -07:00
|
|
|
|
#[stable(feature = "unix_process_wait_more", since = "1.58.0")]
|
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
|
2021-03-25 10:48:27 +00:00
|
|
|
|
/// a `wait` system call which was passed `WUNTRACED`, and was then converted into an `ExitStatus`.
|
2021-10-22 10:47:34 -07:00
|
|
|
|
#[stable(feature = "unix_process_wait_more", since = "1.58.0")]
|
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
|
2021-03-25 10:48:27 +00:00
|
|
|
|
/// which was passed `WCONTINUED`, and was then converted into an `ExitStatus`.
|
2021-10-22 10:47:34 -07:00
|
|
|
|
#[stable(feature = "unix_process_wait_more", since = "1.58.0")]
|
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-02-22 15:26:19 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// The returned integer is a **wait status, not an exit status**.
|
2021-10-22 10:47:34 -07:00
|
|
|
|
#[stable(feature = "unix_process_wait_more", since = "1.58.0")]
|
2020-12-12 21:41:55 +00:00
|
|
|
|
fn into_raw(self) -> i32;
|
2015-04-15 23:21:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
|
impl ExitStatusExt for process::ExitStatus {
|
2016-04-26 15:23:46 -07:00
|
|
|
|
fn from_raw(raw: i32) -> Self {
|
|
|
|
|
|
process::ExitStatus::from_inner(From::from(raw))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-15 23:21:13 -07:00
|
|
|
|
fn signal(&self) -> Option<i32> {
|
2015-10-29 13:45:56 -07:00
|
|
|
|
self.as_inner().signal()
|
2015-04-15 23:21:13 -07: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-15 23:21:13 -07:00
|
|
|
|
}
|
2015-05-12 11:03:49 -07:00
|
|
|
|
|
2021-02-24 14:52:16 +00:00
|
|
|
|
#[unstable(feature = "exit_status_error", issue = "84908")]
|
|
|
|
|
|
impl ExitStatusExt for process::ExitStatusError {
|
|
|
|
|
|
fn from_raw(raw: i32) -> Self {
|
|
|
|
|
|
process::ExitStatus::from_raw(raw)
|
|
|
|
|
|
.exit_ok()
|
|
|
|
|
|
.expect_err("<ExitStatusError as ExitStatusExt>::from_raw(0) but zero is not an error")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn signal(&self) -> Option<i32> {
|
|
|
|
|
|
self.into_status().signal()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn core_dumped(&self) -> bool {
|
|
|
|
|
|
self.into_status().core_dumped()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn stopped_signal(&self) -> Option<i32> {
|
|
|
|
|
|
self.into_status().stopped_signal()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn continued(&self) -> bool {
|
|
|
|
|
|
self.into_status().continued()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn into_raw(self) -> i32 {
|
|
|
|
|
|
self.into_status().into_raw()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-09 16:41:14 -07:00
|
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 11:03:49 -07:00
|
|
|
|
impl FromRawFd for process::Stdio {
|
2021-04-25 07:39:09 +01:00
|
|
|
|
#[inline]
|
2015-05-12 11:03:49 -07:00
|
|
|
|
unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio {
|
2021-06-30 21:44:30 -07:00
|
|
|
|
let fd = sys::fd::FileDesc::from_raw_fd(fd);
|
2016-02-04 11:10:37 -08:00
|
|
|
|
let io = sys::process::Stdio::Fd(fd);
|
|
|
|
|
|
process::Stdio::from_inner(io)
|
2015-05-12 11:03:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-08 15:38:08 -08:00
|
|
|
|
#[stable(feature = "io_safety", since = "1.63.0")]
|
2021-08-17 16:28:00 -07:00
|
|
|
|
impl From<OwnedFd> for process::Stdio {
|
2024-02-04 12:14:53 -05:00
|
|
|
|
/// Takes ownership of a file descriptor and returns a [`Stdio`](process::Stdio)
|
|
|
|
|
|
/// that can attach a stream to it.
|
2021-08-17 16:28:00 -07:00
|
|
|
|
#[inline]
|
|
|
|
|
|
fn from(fd: OwnedFd) -> process::Stdio {
|
|
|
|
|
|
let fd = sys::fd::FileDesc::from_inner(fd);
|
|
|
|
|
|
let io = sys::process::Stdio::Fd(fd);
|
|
|
|
|
|
process::Stdio::from_inner(io)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-09 16:41:14 -07:00
|
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 11:03:49 -07:00
|
|
|
|
impl AsRawFd for process::ChildStdin {
|
2021-04-25 07:39:09 +01:00
|
|
|
|
#[inline]
|
2015-05-12 11:03:49 -07:00
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
2021-06-30 21:44:30 -07:00
|
|
|
|
self.as_inner().as_raw_fd()
|
2015-05-12 11:03:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-09 16:41:14 -07:00
|
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 11:03:49 -07:00
|
|
|
|
impl AsRawFd for process::ChildStdout {
|
2021-04-25 07:39:09 +01:00
|
|
|
|
#[inline]
|
2015-05-12 11:03:49 -07:00
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
2021-06-30 21:44:30 -07:00
|
|
|
|
self.as_inner().as_raw_fd()
|
2015-05-12 11:03:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-09 16:41:14 -07:00
|
|
|
|
#[stable(feature = "process_extensions", since = "1.2.0")]
|
2015-05-12 11:03:49 -07:00
|
|
|
|
impl AsRawFd for process::ChildStderr {
|
2021-04-25 07:39:09 +01:00
|
|
|
|
#[inline]
|
2015-05-12 11:03:49 -07:00
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
2021-06-30 21:44:30 -07:00
|
|
|
|
self.as_inner().as_raw_fd()
|
2015-05-12 11:03:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-07-15 23:31:24 -07:00
|
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-15 23:31:24 -07:00
|
|
|
|
impl IntoRawFd for process::ChildStdin {
|
2021-04-25 07:39:09 +01:00
|
|
|
|
#[inline]
|
2015-07-15 23:31:24 -07:00
|
|
|
|
fn into_raw_fd(self) -> RawFd {
|
2021-06-30 21:44:30 -07:00
|
|
|
|
self.into_inner().into_inner().into_raw_fd()
|
2015-07-15 23:31:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-15 23:31:24 -07:00
|
|
|
|
impl IntoRawFd for process::ChildStdout {
|
2021-04-25 07:39:09 +01:00
|
|
|
|
#[inline]
|
2015-07-15 23:31:24 -07:00
|
|
|
|
fn into_raw_fd(self) -> RawFd {
|
2021-06-30 21:44:30 -07:00
|
|
|
|
self.into_inner().into_inner().into_raw_fd()
|
2015-07-15 23:31:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-15 23:31:24 -07:00
|
|
|
|
impl IntoRawFd for process::ChildStderr {
|
2021-04-25 07:39:09 +01:00
|
|
|
|
#[inline]
|
2015-07-15 23:31:24 -07:00
|
|
|
|
fn into_raw_fd(self) -> RawFd {
|
2021-06-30 21:44:30 -07:00
|
|
|
|
self.into_inner().into_inner().into_raw_fd()
|
2015-07-15 23:31:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-11-18 21:09:18 -08:00
|
|
|
|
|
2022-03-08 15:38:08 -08:00
|
|
|
|
#[stable(feature = "io_safety", since = "1.63.0")]
|
2021-08-19 12:24:25 -07:00
|
|
|
|
impl AsFd for crate::process::ChildStdin {
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
|
|
|
|
|
self.as_inner().as_fd()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-08 15:38:08 -08:00
|
|
|
|
#[stable(feature = "io_safety", since = "1.63.0")]
|
2021-08-19 12:24:25 -07:00
|
|
|
|
impl From<crate::process::ChildStdin> for OwnedFd {
|
2024-02-04 12:14:53 -05:00
|
|
|
|
/// Takes ownership of a [`ChildStdin`](crate::process::ChildStdin)'s file descriptor.
|
2021-08-19 12:24:25 -07:00
|
|
|
|
#[inline]
|
|
|
|
|
|
fn from(child_stdin: crate::process::ChildStdin) -> OwnedFd {
|
|
|
|
|
|
child_stdin.into_inner().into_inner().into_inner()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-15 12:26:30 +01:00
|
|
|
|
/// Creates a `ChildStdin` from the provided `OwnedFd`.
|
2023-03-09 10:46:38 +01:00
|
|
|
|
///
|
|
|
|
|
|
/// The provided file descriptor must point to a pipe
|
|
|
|
|
|
/// with the `CLOEXEC` flag set.
|
2023-10-02 19:12:46 -04:00
|
|
|
|
#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
|
2022-07-06 15:29:17 +02:00
|
|
|
|
impl From<OwnedFd> for process::ChildStdin {
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
fn from(fd: OwnedFd) -> process::ChildStdin {
|
|
|
|
|
|
let fd = sys::fd::FileDesc::from_inner(fd);
|
|
|
|
|
|
let pipe = sys::pipe::AnonPipe::from_inner(fd);
|
|
|
|
|
|
process::ChildStdin::from_inner(pipe)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-08 15:38:08 -08:00
|
|
|
|
#[stable(feature = "io_safety", since = "1.63.0")]
|
2021-08-19 12:24:25 -07:00
|
|
|
|
impl AsFd for crate::process::ChildStdout {
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
|
|
|
|
|
self.as_inner().as_fd()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-08 15:38:08 -08:00
|
|
|
|
#[stable(feature = "io_safety", since = "1.63.0")]
|
2021-08-19 12:24:25 -07:00
|
|
|
|
impl From<crate::process::ChildStdout> for OwnedFd {
|
2024-02-04 12:14:53 -05:00
|
|
|
|
/// Takes ownership of a [`ChildStdout`](crate::process::ChildStdout)'s file descriptor.
|
2021-08-19 12:24:25 -07:00
|
|
|
|
#[inline]
|
|
|
|
|
|
fn from(child_stdout: crate::process::ChildStdout) -> OwnedFd {
|
|
|
|
|
|
child_stdout.into_inner().into_inner().into_inner()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-15 12:26:30 +01:00
|
|
|
|
/// Creates a `ChildStdout` from the provided `OwnedFd`.
|
2023-03-09 10:46:38 +01:00
|
|
|
|
///
|
|
|
|
|
|
/// The provided file descriptor must point to a pipe
|
|
|
|
|
|
/// with the `CLOEXEC` flag set.
|
2023-10-02 19:12:46 -04:00
|
|
|
|
#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
|
2022-07-06 15:29:17 +02:00
|
|
|
|
impl From<OwnedFd> for process::ChildStdout {
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
fn from(fd: OwnedFd) -> process::ChildStdout {
|
|
|
|
|
|
let fd = sys::fd::FileDesc::from_inner(fd);
|
|
|
|
|
|
let pipe = sys::pipe::AnonPipe::from_inner(fd);
|
|
|
|
|
|
process::ChildStdout::from_inner(pipe)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-08 15:38:08 -08:00
|
|
|
|
#[stable(feature = "io_safety", since = "1.63.0")]
|
2021-08-19 12:24:25 -07:00
|
|
|
|
impl AsFd for crate::process::ChildStderr {
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
|
|
|
|
|
self.as_inner().as_fd()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-08 15:38:08 -08:00
|
|
|
|
#[stable(feature = "io_safety", since = "1.63.0")]
|
2021-08-19 12:24:25 -07:00
|
|
|
|
impl From<crate::process::ChildStderr> for OwnedFd {
|
2024-02-04 12:14:53 -05:00
|
|
|
|
/// Takes ownership of a [`ChildStderr`](crate::process::ChildStderr)'s file descriptor.
|
2021-08-19 12:24:25 -07:00
|
|
|
|
#[inline]
|
|
|
|
|
|
fn from(child_stderr: crate::process::ChildStderr) -> OwnedFd {
|
|
|
|
|
|
child_stderr.into_inner().into_inner().into_inner()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-15 12:26:30 +01:00
|
|
|
|
/// Creates a `ChildStderr` from the provided `OwnedFd`.
|
2023-03-09 10:46:38 +01:00
|
|
|
|
///
|
|
|
|
|
|
/// The provided file descriptor must point to a pipe
|
|
|
|
|
|
/// with the `CLOEXEC` flag set.
|
2023-10-02 19:12:46 -04:00
|
|
|
|
#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
|
2022-07-06 15:29:17 +02:00
|
|
|
|
impl From<OwnedFd> for process::ChildStderr {
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
fn from(fd: OwnedFd) -> process::ChildStderr {
|
|
|
|
|
|
let fd = sys::fd::FileDesc::from_inner(fd);
|
|
|
|
|
|
let pipe = sys::pipe::AnonPipe::from_inner(fd);
|
|
|
|
|
|
process::ChildStderr::from_inner(pipe)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-18 21:09:18 -08:00
|
|
|
|
/// Returns the OS-assigned process identifier associated with this process's parent.
|
2021-10-30 23:37:32 -04:00
|
|
|
|
#[must_use]
|
2018-04-03 19:47:37 -06:00
|
|
|
|
#[stable(feature = "unix_ppid", since = "1.27.0")]
|
2017-11-18 21:09:18 -08:00
|
|
|
|
pub fn parent_id() -> u32 {
|
2019-02-11 04:23:21 +09:00
|
|
|
|
crate::sys::os::getppid()
|
2017-11-18 21:09:18 -08:00
|
|
|
|
}
|