Trait CommandExt
trait CommandExt: Sealed
Unix-specific extensions to the process::Command builder.
This trait is sealed: it cannot be implemented outside the standard library. This is so that future additional methods are not breaking changes.
Required Methods
fn uid(self: &mut Self, id: u32) -> &mut process::CommandSets the child process's user ID. This translates to a
setuidcall in the child process. Failure in thesetuidcall will cause the spawn to fail.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.fn gid(self: &mut Self, id: u32) -> &mut process::CommandSimilar to
uid, but sets the group ID of the child process. This has the same semantics as theuidfield.fn groups(self: &mut Self, groups: &[u32]) -> &mut process::CommandSets the supplementary group IDs for the calling process. Translates to a
setgroupscall in the child process.unsafe fn pre_exec<F>(self: &mut Self, f: F) -> &mut process::Command where F: FnMut() -> io::Result<()> + Send + Sync + 'staticSchedules a closure to be run just before the
execfunction 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
Errthen no further closures will be called and the spawn operation will immediately return with a failure.Notes and Safety
This closure will be run in the context of the child process after a
fork. This primarily means that any modifications made to memory on behalf of this closure will not be visible to the parent process. This is often a very constrained environment where normal operations likemalloc, accessing environment variables throughstd::envor acquiring a mutex are not guaranteed to work (due to other threads perhaps still running when theforkwas run).Note that the list of allocating functions includes
Error::newandError::other. To signal a non-trivial error, prefer [panic!].For further details refer to the POSIX fork() specification and the equivalent documentation for any targeted platform, especially the requirements around async-signal-safety.
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 invalid use of these duplicates.
Panicking in the closure is safe only if all the format arguments for the panic message can be safely formatted; this is because although
Commandcallsstd::panic::always_abortbefore calling the pre_exec hook, panic will still try to format the panic message.When this closure is run, aspects such as the stdio file descriptors and working directory have successfully been changed, so output to these locations might not appear where intended.
fn exec(self: &mut Self) -> io::ErrorPerforms all the required setup by this
Command, followed by calling theexecvpsyscall.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.execnot returning has the same implications as callingprocess::exit– no destructors on the current stack or any other thread’s stack will be run. Therefore, it is recommended to only callexecat a point where it is fine to not run any destructors. Note, that theexecvpsyscall independently guarantees that all memory is freed and all file descriptors with theCLOEXECoption (set by default on all file descriptors opened by the standard library) are closed.This function, unlike
spawn, will notforkthe process to create a new child. Like spawn, however, the default behavior for the stdio descriptors will be to inherit them 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
spawninstead.fn arg0<S>(self: &mut Self, arg: S) -> &mut process::Command where S: AsRef<OsStr>Set executable argument
Set the first process argument,
argv[0], to something other than the default executable path.fn process_group(self: &mut Self, pgroup: i32) -> &mut process::CommandSets the process group ID (PGID) of the child process. Equivalent to a
setpgidcall 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
sleepsubprocess 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.
A process group ID of 0 will use the process ID as the PGID.
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>>(())fn chroot<P: AsRef<Path>>(self: &mut Self, dir: P) -> &mut process::CommandSet the root of the child process. This calls
chrootin the child process before executing the command.This happens before changing to the directory specified with
process::Command::current_dir, and that directory will be relative to the new root.If no directory has been specified with
process::Command::current_dir, this will set the directory to/, to avoid leaving the current directory outside the chroot. (This is an intentional difference from the underlyingchrootsystem call.)fn setsid(self: &mut Self, setsid: bool) -> &mut process::Command
Provided Methods
unsafe fn before_exec<F>(self: &mut Self, f: F) -> &mut process::Command where F: FnMut() -> io::Result<()> + Send + Sync + 'staticSchedules a closure to be run just before the
execfunction is invoked.before_execused 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 unsafepre_exec. Meanwhile, Rust gained the ability to make an existing safe method fully unsafe in a new edition, which is howbefore_execbecameunsafe. It still also remains deprecated;pre_execshould be used instead.
Implementors
impl CommandExt for process::Command