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.
14 /// [`process::Command`]: ../../../../std/process/struct.Command.html
15 #[stable(feature = "rust1", since = "1.0.0")]
16 pub trait CommandExt {
17 /// Sets the child process's user ID. This translates to a
18 /// `setuid` call in the child process. Failure in the `setuid`
19 /// call will cause the spawn to fail.
20 #[stable(feature = "rust1", since = "1.0.0")]
21 fn uid(&mut self, id: u16) -> &mut process::Command;
23 /// Similar to `uid`, but sets the group ID of the child process. This has
24 /// the same semantics as the `uid` field.
25 #[stable(feature = "rust1", since = "1.0.0")]
26 fn gid(&mut self, id: u16) -> &mut process::Command;
28 /// Schedules a closure to be run just before the `exec` function is
31 /// The closure is allowed to return an I/O error whose OS error code will
32 /// be communicated back to the parent and returned as an error from when
33 /// the spawn was requested.
35 /// Multiple closures can be registered and they will be called in order of
36 /// their registration. If a closure returns `Err` then no further closures
37 /// will be called and the spawn operation will immediately return with a
40 /// # Notes and Safety
42 /// This closure will be run in the context of the child process after a
43 /// `fork`. This primarily means that any modifications made to memory on
44 /// behalf of this closure will **not** be visible to the parent process.
45 /// This is often a very constrained environment where normal operations
46 /// like `malloc` or acquiring a mutex are not guaranteed to work (due to
47 /// other threads perhaps still running when the `fork` was run).
49 /// This also means that all resources such as file descriptors and
50 /// memory-mapped regions got duplicated. It is your responsibility to make
51 /// sure that the closure does not violate library invariants by making
52 /// invalid use of these duplicates.
54 /// When this closure is run, aspects such as the stdio file descriptors and
55 /// working directory have successfully been changed, so output to these
56 /// locations may not appear where intended.
57 #[stable(feature = "process_pre_exec", since = "1.34.0")]
58 unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
60 F: FnMut() -> io::Result<()> + Send + Sync + 'static;
62 /// Schedules a closure to be run just before the `exec` function is
65 /// This method is stable and usable, but it should be unsafe. To fix
66 /// that, it got deprecated in favor of the unsafe [`pre_exec`].
68 /// [`pre_exec`]: #tymethod.pre_exec
69 #[stable(feature = "process_exec", since = "1.15.0")]
70 #[rustc_deprecated(since = "1.37.0", reason = "should be unsafe, use `pre_exec` instead")]
71 fn before_exec<F>(&mut self, f: F) -> &mut process::Command
73 F: FnMut() -> io::Result<()> + Send + Sync + 'static,
75 unsafe { self.pre_exec(f) }
78 /// Performs all the required setup by this `Command`, followed by calling
79 /// the `execvp` syscall.
81 /// On success this function will not return, and otherwise it will return
82 /// an error indicating why the exec (or another part of the setup of the
83 /// `Command`) failed.
85 /// `exec` not returning has the same implications as calling
86 /// [`process::exit`] – no destructors on the current stack or any other
87 /// thread’s stack will be run. Therefore, it is recommended to only call
88 /// `exec` at a point where it is fine to not run any destructors. Note,
89 /// that the `execvp` syscall independently guarantees that all memory is
90 /// freed and all file descriptors with the `CLOEXEC` option (set by default
91 /// on all file descriptors opened by the standard library) are closed.
93 /// This function, unlike `spawn`, will **not** `fork` the process to create
94 /// a new child. Like spawn, however, the default behavior for the stdio
95 /// descriptors will be to inherited from the current process.
97 /// [`process::exit`]: ../../../process/fn.exit.html
101 /// The process may be in a "broken state" if this function returns in
102 /// error. For example the working directory, environment variables, signal
103 /// handling settings, various user/group information, or aspects of stdio
104 /// file descriptors may have changed. If a "transactional spawn" is
105 /// required to gracefully handle errors it is recommended to use the
106 /// cross-platform `spawn` instead.
107 #[stable(feature = "process_exec2", since = "1.9.0")]
108 fn exec(&mut self) -> io::Error;
110 /// Set executable argument
112 /// Set the first process argument, `argv[0]`, to something other than the
113 /// default executable path.
114 #[stable(feature = "process_set_argv0", since = "1.45.0")]
115 fn arg0<S>(&mut self, arg: S) -> &mut process::Command
120 #[stable(feature = "rust1", since = "1.0.0")]
121 impl CommandExt for process::Command {
122 fn uid(&mut self, id: u16) -> &mut process::Command {
123 self.as_inner_mut().uid(id);
127 fn gid(&mut self, id: u16) -> &mut process::Command {
128 self.as_inner_mut().gid(id);
132 unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
134 F: FnMut() -> io::Result<()> + Send + Sync + 'static,
136 self.as_inner_mut().pre_exec(Box::new(f));
140 fn exec(&mut self) -> io::Error {
141 self.as_inner_mut().exec(sys::process::Stdio::Inherit)
144 fn arg0<S>(&mut self, arg: S) -> &mut process::Command
148 self.as_inner_mut().set_arg_0(arg.as_ref());
153 /// Unix-specific extensions to [`process::ExitStatus`].
155 /// [`process::ExitStatus`]: ../../../../std/process/struct.ExitStatus.html
156 #[stable(feature = "rust1", since = "1.0.0")]
157 pub trait ExitStatusExt {
158 /// Creates a new `ExitStatus` from the raw underlying `i32` return value of
160 #[stable(feature = "exit_status_from", since = "1.12.0")]
161 fn from_raw(raw: i32) -> Self;
163 /// If the process was terminated by a signal, returns that signal.
164 #[stable(feature = "rust1", since = "1.0.0")]
165 fn signal(&self) -> Option<i32>;
168 #[stable(feature = "rust1", since = "1.0.0")]
169 impl ExitStatusExt for process::ExitStatus {
170 fn from_raw(raw: i32) -> Self {
171 process::ExitStatus::from_inner(From::from(raw))
174 fn signal(&self) -> Option<i32> {
175 self.as_inner().signal()
179 #[stable(feature = "process_extensions", since = "1.2.0")]
180 impl FromRawFd for process::Stdio {
181 unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio {
182 let fd = sys::fd::FileDesc::new(fd);
183 let io = sys::process::Stdio::Fd(fd);
184 process::Stdio::from_inner(io)
188 #[stable(feature = "process_extensions", since = "1.2.0")]
189 impl AsRawFd for process::ChildStdin {
190 fn as_raw_fd(&self) -> RawFd {
191 self.as_inner().fd().raw()
195 #[stable(feature = "process_extensions", since = "1.2.0")]
196 impl AsRawFd for process::ChildStdout {
197 fn as_raw_fd(&self) -> RawFd {
198 self.as_inner().fd().raw()
202 #[stable(feature = "process_extensions", since = "1.2.0")]
203 impl AsRawFd for process::ChildStderr {
204 fn as_raw_fd(&self) -> RawFd {
205 self.as_inner().fd().raw()
209 #[stable(feature = "into_raw_os", since = "1.4.0")]
210 impl IntoRawFd for process::ChildStdin {
211 fn into_raw_fd(self) -> RawFd {
212 self.into_inner().into_fd().into_raw()
216 #[stable(feature = "into_raw_os", since = "1.4.0")]
217 impl IntoRawFd for process::ChildStdout {
218 fn into_raw_fd(self) -> RawFd {
219 self.into_inner().into_fd().into_raw()
223 #[stable(feature = "into_raw_os", since = "1.4.0")]
224 impl IntoRawFd for process::ChildStderr {
225 fn into_raw_fd(self) -> RawFd {
226 self.into_inner().into_fd().into_raw()
230 /// Returns the OS-assigned process identifier associated with this process's parent.
231 #[stable(feature = "unix_ppid", since = "1.27.0")]
232 pub fn parent_id() -> u32 {
233 crate::sys::os::getppid()