]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/ext/process.rs
unix ExitStatus: Provide .core_dumped
[rust.git] / library / std / src / sys / unix / ext / process.rs
1 //! Unix-specific extensions to primitives in the `std::process` module.
2
3 #![stable(feature = "rust1", since = "1.0.0")]
4
5 use crate::ffi::OsStr;
6 use crate::io;
7 use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
8 use crate::process;
9 use crate::sys;
10 use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
11
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(
20         &mut self,
21         #[cfg(not(target_os = "vxworks"))] id: u32,
22         #[cfg(target_os = "vxworks")] id: u16,
23     ) -> &mut process::Command;
24
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")]
28     fn gid(
29         &mut self,
30         #[cfg(not(target_os = "vxworks"))] id: u32,
31         #[cfg(target_os = "vxworks")] id: u16,
32     ) -> &mut process::Command;
33
34     /// Schedules a closure to be run just before the `exec` function is
35     /// invoked.
36     ///
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.
40     ///
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
44     /// failure.
45     ///
46     /// # Notes and Safety
47     ///
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).
54     ///
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.
59     ///
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
65     where
66         F: FnMut() -> io::Result<()> + Send + Sync + 'static;
67
68     /// Schedules a closure to be run just before the `exec` function is
69     /// invoked.
70     ///
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`].
73     ///
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
78     where
79         F: FnMut() -> io::Result<()> + Send + Sync + 'static,
80     {
81         unsafe { self.pre_exec(f) }
82     }
83
84     /// Performs all the required setup by this `Command`, followed by calling
85     /// the `execvp` syscall.
86     ///
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.
90     ///
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.
98     ///
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.
102     ///
103     /// # Notes
104     ///
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;
113
114     /// Set executable argument
115     ///
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
120     where
121         S: AsRef<OsStr>;
122 }
123
124 #[stable(feature = "rust1", since = "1.0.0")]
125 impl CommandExt for process::Command {
126     fn uid(
127         &mut self,
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);
132         self
133     }
134
135     fn gid(
136         &mut self,
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);
141         self
142     }
143
144     unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
145     where
146         F: FnMut() -> io::Result<()> + Send + Sync + 'static,
147     {
148         self.as_inner_mut().pre_exec(Box::new(f));
149         self
150     }
151
152     fn exec(&mut self) -> io::Error {
153         self.as_inner_mut().exec(sys::process::Stdio::Inherit)
154     }
155
156     fn arg0<S>(&mut self, arg: S) -> &mut process::Command
157     where
158         S: AsRef<OsStr>,
159     {
160         self.as_inner_mut().set_arg_0(arg.as_ref());
161         self
162     }
163 }
164
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
169     /// a process.
170     #[stable(feature = "exit_status_from", since = "1.12.0")]
171     fn from_raw(raw: i32) -> Self;
172
173     /// If the process was terminated by a signal, returns that signal.
174     ///
175     /// Ie, if `WIFSIGNALED`, this returns `WTERMSIG`.
176     #[stable(feature = "rust1", since = "1.0.0")]
177     fn signal(&self) -> Option<i32>;
178
179     /// If the process was terminated by a signal, says whether it dumped core.
180     #[unstable(feature = "unix_process_wait_more", issue = "none")]
181     fn core_dumped(&self) -> bool;
182
183     /// Returns the underlying raw `wait` status.
184     #[unstable(feature = "unix_process_wait_more", issue = "none")]
185     fn into_raw(self) -> i32;
186 }
187
188 #[stable(feature = "rust1", since = "1.0.0")]
189 impl ExitStatusExt for process::ExitStatus {
190     fn from_raw(raw: i32) -> Self {
191         process::ExitStatus::from_inner(From::from(raw))
192     }
193
194     fn signal(&self) -> Option<i32> {
195         self.as_inner().signal()
196     }
197
198     fn core_dumped(&self) -> bool {
199         self.as_inner().core_dumped()
200     }
201
202     fn into_raw(self) -> i32 {
203         self.as_inner().into_raw().into()
204     }
205 }
206
207 #[stable(feature = "process_extensions", since = "1.2.0")]
208 impl FromRawFd for process::Stdio {
209     unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio {
210         let fd = sys::fd::FileDesc::new(fd);
211         let io = sys::process::Stdio::Fd(fd);
212         process::Stdio::from_inner(io)
213     }
214 }
215
216 #[stable(feature = "process_extensions", since = "1.2.0")]
217 impl AsRawFd for process::ChildStdin {
218     fn as_raw_fd(&self) -> RawFd {
219         self.as_inner().fd().raw()
220     }
221 }
222
223 #[stable(feature = "process_extensions", since = "1.2.0")]
224 impl AsRawFd for process::ChildStdout {
225     fn as_raw_fd(&self) -> RawFd {
226         self.as_inner().fd().raw()
227     }
228 }
229
230 #[stable(feature = "process_extensions", since = "1.2.0")]
231 impl AsRawFd for process::ChildStderr {
232     fn as_raw_fd(&self) -> RawFd {
233         self.as_inner().fd().raw()
234     }
235 }
236
237 #[stable(feature = "into_raw_os", since = "1.4.0")]
238 impl IntoRawFd for process::ChildStdin {
239     fn into_raw_fd(self) -> RawFd {
240         self.into_inner().into_fd().into_raw()
241     }
242 }
243
244 #[stable(feature = "into_raw_os", since = "1.4.0")]
245 impl IntoRawFd for process::ChildStdout {
246     fn into_raw_fd(self) -> RawFd {
247         self.into_inner().into_fd().into_raw()
248     }
249 }
250
251 #[stable(feature = "into_raw_os", since = "1.4.0")]
252 impl IntoRawFd for process::ChildStderr {
253     fn into_raw_fd(self) -> RawFd {
254         self.into_inner().into_fd().into_raw()
255     }
256 }
257
258 /// Returns the OS-assigned process identifier associated with this process's parent.
259 #[stable(feature = "unix_ppid", since = "1.27.0")]
260 pub fn parent_id() -> u32 {
261     crate::sys::os::getppid()
262 }