1 //! Unix-specific extensions to primitives in the `std::process` module.
3 #![stable(feature = "rust1", since = "1.0.0")]
7 use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
10 use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
12 /// Unix-specific extensions to the [`process::Command`] builder.
13 #[stable(feature = "rust1", since = "1.0.0")]
14 pub trait CommandExt {
15 /// Sets the child process's user ID. This translates to a
16 /// `setuid` call in the child process. Failure in the `setuid`
17 /// call will cause the spawn to fail.
18 #[stable(feature = "rust1", since = "1.0.0")]
21 #[cfg(not(target_os = "vxworks"))] id: u32,
22 #[cfg(target_os = "vxworks")] id: u16,
23 ) -> &mut process::Command;
25 /// Similar to `uid`, but sets the group ID of the child process. This has
26 /// the same semantics as the `uid` field.
27 #[stable(feature = "rust1", since = "1.0.0")]
30 #[cfg(not(target_os = "vxworks"))] id: u32,
31 #[cfg(target_os = "vxworks")] id: u16,
32 ) -> &mut process::Command;
34 /// Schedules a closure to be run just before the `exec` function is
37 /// The closure is allowed to return an I/O error whose OS error code will
38 /// be communicated back to the parent and returned as an error from when
39 /// the spawn was requested.
41 /// Multiple closures can be registered and they will be called in order of
42 /// their registration. If a closure returns `Err` then no further closures
43 /// will be called and the spawn operation will immediately return with a
46 /// # Notes and Safety
48 /// This closure will be run in the context of the child process after a
49 /// `fork`. This primarily means that any modifications made to memory on
50 /// behalf of this closure will **not** be visible to the parent process.
51 /// This is often a very constrained environment where normal operations
52 /// like `malloc` or acquiring a mutex are not guaranteed to work (due to
53 /// other threads perhaps still running when the `fork` was run).
55 /// This also means that all resources such as file descriptors and
56 /// memory-mapped regions got duplicated. It is your responsibility to make
57 /// sure that the closure does not violate library invariants by making
58 /// invalid use of these duplicates.
60 /// When this closure is run, aspects such as the stdio file descriptors and
61 /// working directory have successfully been changed, so output to these
62 /// locations may not appear where intended.
63 #[stable(feature = "process_pre_exec", since = "1.34.0")]
64 unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
66 F: FnMut() -> io::Result<()> + Send + Sync + 'static;
68 /// Schedules a closure to be run just before the `exec` function is
71 /// This method is stable and usable, but it should be unsafe. To fix
72 /// that, it got deprecated in favor of the unsafe [`pre_exec`].
74 /// [`pre_exec`]: CommandExt::pre_exec
75 #[stable(feature = "process_exec", since = "1.15.0")]
76 #[rustc_deprecated(since = "1.37.0", reason = "should be unsafe, use `pre_exec` instead")]
77 fn before_exec<F>(&mut self, f: F) -> &mut process::Command
79 F: FnMut() -> io::Result<()> + Send + Sync + 'static,
81 unsafe { self.pre_exec(f) }
84 /// Performs all the required setup by this `Command`, followed by calling
85 /// the `execvp` syscall.
87 /// On success this function will not return, and otherwise it will return
88 /// an error indicating why the exec (or another part of the setup of the
89 /// `Command`) failed.
91 /// `exec` not returning has the same implications as calling
92 /// [`process::exit`] – no destructors on the current stack or any other
93 /// thread’s stack will be run. Therefore, it is recommended to only call
94 /// `exec` at a point where it is fine to not run any destructors. Note,
95 /// that the `execvp` syscall independently guarantees that all memory is
96 /// freed and all file descriptors with the `CLOEXEC` option (set by default
97 /// on all file descriptors opened by the standard library) are closed.
99 /// This function, unlike `spawn`, will **not** `fork` the process to create
100 /// a new child. Like spawn, however, the default behavior for the stdio
101 /// descriptors will be to inherited from the current process.
105 /// The process may be in a "broken state" if this function returns in
106 /// error. For example the working directory, environment variables, signal
107 /// handling settings, various user/group information, or aspects of stdio
108 /// file descriptors may have changed. If a "transactional spawn" is
109 /// required to gracefully handle errors it is recommended to use the
110 /// cross-platform `spawn` instead.
111 #[stable(feature = "process_exec2", since = "1.9.0")]
112 fn exec(&mut self) -> io::Error;
114 /// Set executable argument
116 /// Set the first process argument, `argv[0]`, to something other than the
117 /// default executable path.
118 #[stable(feature = "process_set_argv0", since = "1.45.0")]
119 fn arg0<S>(&mut self, arg: S) -> &mut process::Command
124 #[stable(feature = "rust1", since = "1.0.0")]
125 impl CommandExt for process::Command {
128 #[cfg(not(target_os = "vxworks"))] id: u32,
129 #[cfg(target_os = "vxworks")] id: u16,
130 ) -> &mut process::Command {
131 self.as_inner_mut().uid(id);
137 #[cfg(not(target_os = "vxworks"))] id: u32,
138 #[cfg(target_os = "vxworks")] id: u16,
139 ) -> &mut process::Command {
140 self.as_inner_mut().gid(id);
144 unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
146 F: FnMut() -> io::Result<()> + Send + Sync + 'static,
148 self.as_inner_mut().pre_exec(Box::new(f));
152 fn exec(&mut self) -> io::Error {
153 self.as_inner_mut().exec(sys::process::Stdio::Inherit)
156 fn arg0<S>(&mut self, arg: S) -> &mut process::Command
160 self.as_inner_mut().set_arg_0(arg.as_ref());
165 /// Unix-specific extensions to [`process::ExitStatus`].
166 #[stable(feature = "rust1", since = "1.0.0")]
167 pub trait ExitStatusExt {
168 /// Creates a new `ExitStatus` from the raw underlying `i32` return value of
170 #[stable(feature = "exit_status_from", since = "1.12.0")]
171 fn from_raw(raw: i32) -> Self;
173 /// If the process was terminated by a signal, returns that signal.
175 /// Ie, if `WIFSIGNALED`, this returns `WTERMSIG`.
176 #[stable(feature = "rust1", since = "1.0.0")]
177 fn signal(&self) -> Option<i32>;
179 /// Returns the underlying raw `wait` status.
180 #[unstable(feature = "unix_process_wait_more", issue = "none")]
181 fn into_raw(self) -> i32;
184 #[stable(feature = "rust1", since = "1.0.0")]
185 impl ExitStatusExt for process::ExitStatus {
186 fn from_raw(raw: i32) -> Self {
187 process::ExitStatus::from_inner(From::from(raw))
190 fn signal(&self) -> Option<i32> {
191 self.as_inner().signal()
194 fn into_raw(self) -> i32 {
195 self.as_inner().into_raw().into()
199 #[stable(feature = "process_extensions", since = "1.2.0")]
200 impl FromRawFd for process::Stdio {
201 unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio {
202 let fd = sys::fd::FileDesc::new(fd);
203 let io = sys::process::Stdio::Fd(fd);
204 process::Stdio::from_inner(io)
208 #[stable(feature = "process_extensions", since = "1.2.0")]
209 impl AsRawFd for process::ChildStdin {
210 fn as_raw_fd(&self) -> RawFd {
211 self.as_inner().fd().raw()
215 #[stable(feature = "process_extensions", since = "1.2.0")]
216 impl AsRawFd for process::ChildStdout {
217 fn as_raw_fd(&self) -> RawFd {
218 self.as_inner().fd().raw()
222 #[stable(feature = "process_extensions", since = "1.2.0")]
223 impl AsRawFd for process::ChildStderr {
224 fn as_raw_fd(&self) -> RawFd {
225 self.as_inner().fd().raw()
229 #[stable(feature = "into_raw_os", since = "1.4.0")]
230 impl IntoRawFd for process::ChildStdin {
231 fn into_raw_fd(self) -> RawFd {
232 self.into_inner().into_fd().into_raw()
236 #[stable(feature = "into_raw_os", since = "1.4.0")]
237 impl IntoRawFd for process::ChildStdout {
238 fn into_raw_fd(self) -> RawFd {
239 self.into_inner().into_fd().into_raw()
243 #[stable(feature = "into_raw_os", since = "1.4.0")]
244 impl IntoRawFd for process::ChildStderr {
245 fn into_raw_fd(self) -> RawFd {
246 self.into_inner().into_fd().into_raw()
250 /// Returns the OS-assigned process identifier associated with this process's parent.
251 #[stable(feature = "unix_ppid", since = "1.27.0")]
252 pub fn parent_id() -> u32 {
253 crate::sys::os::getppid()