1 //! Unix-specific extensions to primitives in the `std::process` module.
3 #![stable(feature = "rust1", since = "1.0.0")]
9 use crate::sys::vxworks::ext::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")]
19 fn uid(&mut self, id: u16) -> &mut process::Command;
21 /// Similar to `uid`, but sets the group ID of the child process. This has
22 /// the same semantics as the `uid` field.
23 #[stable(feature = "rust1", since = "1.0.0")]
24 fn gid(&mut self, id: u16) -> &mut process::Command;
26 /// Schedules a closure to be run just before the `exec` function is
29 /// The closure is allowed to return an I/O error whose OS error code will
30 /// be communicated back to the parent and returned as an error from when
31 /// the spawn was requested.
33 /// Multiple closures can be registered and they will be called in order of
34 /// their registration. If a closure returns `Err` then no further closures
35 /// will be called and the spawn operation will immediately return with a
38 /// # Notes and Safety
40 /// This closure will be run in the context of the child process after a
41 /// `fork`. This primarily means that any modifications made to memory on
42 /// behalf of this closure will **not** be visible to the parent process.
43 /// This is often a very constrained environment where normal operations
44 /// like `malloc` or acquiring a mutex are not guaranteed to work (due to
45 /// other threads perhaps still running when the `fork` was run).
47 /// This also means that all resources such as file descriptors and
48 /// memory-mapped regions got duplicated. It is your responsibility to make
49 /// sure that the closure does not violate library invariants by making
50 /// invalid use of these duplicates.
52 /// When this closure is run, aspects such as the stdio file descriptors and
53 /// working directory have successfully been changed, so output to these
54 /// locations may not appear where intended.
55 #[stable(feature = "process_pre_exec", since = "1.34.0")]
56 unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
58 F: FnMut() -> io::Result<()> + Send + Sync + 'static;
60 /// Schedules a closure to be run just before the `exec` function is
63 /// This method is stable and usable, but it should be unsafe. To fix
64 /// that, it got deprecated in favor of the unsafe [`pre_exec`].
66 /// [`pre_exec`]: CommandExt::pre_exec
67 #[stable(feature = "process_exec", since = "1.15.0")]
68 #[rustc_deprecated(since = "1.37.0", reason = "should be unsafe, use `pre_exec` instead")]
69 fn before_exec<F>(&mut self, f: F) -> &mut process::Command
71 F: FnMut() -> io::Result<()> + Send + Sync + 'static,
73 unsafe { self.pre_exec(f) }
76 /// Performs all the required setup by this `Command`, followed by calling
77 /// the `execvp` syscall.
79 /// On success this function will not return, and otherwise it will return
80 /// an error indicating why the exec (or another part of the setup of the
81 /// `Command`) failed.
83 /// `exec` not returning has the same implications as calling
84 /// [`process::exit`] – no destructors on the current stack or any other
85 /// thread’s stack will be run. Therefore, it is recommended to only call
86 /// `exec` at a point where it is fine to not run any destructors. Note,
87 /// that the `execvp` syscall independently guarantees that all memory is
88 /// freed and all file descriptors with the `CLOEXEC` option (set by default
89 /// on all file descriptors opened by the standard library) are closed.
91 /// This function, unlike `spawn`, will **not** `fork` the process to create
92 /// a new child. Like spawn, however, the default behavior for the stdio
93 /// descriptors will be to inherited from the current process.
98 /// The process may be in a "broken state" if this function returns in
99 /// error. For example the working directory, environment variables, signal
100 /// handling settings, various user/group information, or aspects of stdio
101 /// file descriptors may have changed. If a "transactional spawn" is
102 /// required to gracefully handle errors it is recommended to use the
103 /// cross-platform `spawn` instead.
104 #[stable(feature = "process_exec2", since = "1.9.0")]
105 fn exec(&mut self) -> io::Error;
107 /// Set executable argument
109 /// Set the first process argument, `argv[0]`, to something other than the
110 /// default executable path.
111 #[stable(feature = "process_set_argv0", since = "1.45.0")]
112 fn arg0<S>(&mut self, arg: S) -> &mut process::Command
117 #[stable(feature = "rust1", since = "1.0.0")]
118 impl CommandExt for process::Command {
119 fn uid(&mut self, id: u16) -> &mut process::Command {
120 self.as_inner_mut().uid(id);
124 fn gid(&mut self, id: u16) -> &mut process::Command {
125 self.as_inner_mut().gid(id);
129 unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
131 F: FnMut() -> io::Result<()> + Send + Sync + 'static,
133 self.as_inner_mut().pre_exec(Box::new(f));
137 fn exec(&mut self) -> io::Error {
138 self.as_inner_mut().exec(sys::process::Stdio::Inherit)
141 fn arg0<S>(&mut self, arg: S) -> &mut process::Command
145 self.as_inner_mut().set_arg_0(arg.as_ref());
150 /// Unix-specific extensions to [`process::ExitStatus`].
151 #[stable(feature = "rust1", since = "1.0.0")]
152 pub trait ExitStatusExt {
153 /// Creates a new `ExitStatus` from the raw underlying `i32` return value of
155 #[stable(feature = "exit_status_from", since = "1.12.0")]
156 fn from_raw(raw: i32) -> Self;
158 /// If the process was terminated by a signal, returns that signal.
159 #[stable(feature = "rust1", since = "1.0.0")]
160 fn signal(&self) -> Option<i32>;
163 #[stable(feature = "rust1", since = "1.0.0")]
164 impl ExitStatusExt for process::ExitStatus {
165 fn from_raw(raw: i32) -> Self {
166 process::ExitStatus::from_inner(From::from(raw))
169 fn signal(&self) -> Option<i32> {
170 self.as_inner().signal()
174 #[stable(feature = "process_extensions", since = "1.2.0")]
175 impl FromRawFd for process::Stdio {
176 unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio {
177 let fd = sys::fd::FileDesc::new(fd);
178 let io = sys::process::Stdio::Fd(fd);
179 process::Stdio::from_inner(io)
183 #[stable(feature = "process_extensions", since = "1.2.0")]
184 impl AsRawFd for process::ChildStdin {
185 fn as_raw_fd(&self) -> RawFd {
186 self.as_inner().fd().raw()
190 #[stable(feature = "process_extensions", since = "1.2.0")]
191 impl AsRawFd for process::ChildStdout {
192 fn as_raw_fd(&self) -> RawFd {
193 self.as_inner().fd().raw()
197 #[stable(feature = "process_extensions", since = "1.2.0")]
198 impl AsRawFd for process::ChildStderr {
199 fn as_raw_fd(&self) -> RawFd {
200 self.as_inner().fd().raw()
204 #[stable(feature = "into_raw_os", since = "1.4.0")]
205 impl IntoRawFd for process::ChildStdin {
206 fn into_raw_fd(self) -> RawFd {
207 self.into_inner().into_fd().into_raw()
211 #[stable(feature = "into_raw_os", since = "1.4.0")]
212 impl IntoRawFd for process::ChildStdout {
213 fn into_raw_fd(self) -> RawFd {
214 self.into_inner().into_fd().into_raw()
218 #[stable(feature = "into_raw_os", since = "1.4.0")]
219 impl IntoRawFd for process::ChildStderr {
220 fn into_raw_fd(self) -> RawFd {
221 self.into_inner().into_fd().into_raw()
225 /// Returns the OS-assigned process identifier associated with this process's parent.
226 #[stable(feature = "unix_ppid", since = "1.27.0")]
227 pub fn parent_id() -> u32 {
228 crate::sys::os::getppid()