]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/ext/process.rs
fa8670b4aecac0caa0c06b0da07f2989385b4322
[rust.git] / src / libstd / 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 ///
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: u32) -> &mut process::Command;
22
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: u32) -> &mut process::Command;
27
28     /// Schedules a closure to be run just before the `exec` function is
29     /// invoked.
30     ///
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.
34     ///
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
38     /// failure.
39     ///
40     /// # Notes and Safety
41     ///
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).
48     ///
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.
53     ///
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
59     where
60         F: FnMut() -> io::Result<()> + Send + Sync + 'static;
61
62     /// Schedules a closure to be run just before the `exec` function is
63     /// invoked.
64     ///
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`].
67     ///
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
72     where
73         F: FnMut() -> io::Result<()> + Send + Sync + 'static,
74     {
75         unsafe { self.pre_exec(f) }
76     }
77
78     /// Performs all the required setup by this `Command`, followed by calling
79     /// the `execvp` syscall.
80     ///
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.
84     ///
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.
92     ///
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.
96     ///
97     /// [`process::exit`]: ../../../process/fn.exit.html
98     ///
99     /// # Notes
100     ///
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;
109
110     /// Set executable argument
111     ///
112     /// Set the first process argument, `argv[0]`, to something other than the
113     /// default executable path.
114     #[unstable(feature = "process_set_argv0", issue = "66510")]
115     fn arg0<S>(&mut self, arg: S) -> &mut process::Command
116     where
117         S: AsRef<OsStr>;
118 }
119
120 #[stable(feature = "rust1", since = "1.0.0")]
121 impl CommandExt for process::Command {
122     fn uid(&mut self, id: u32) -> &mut process::Command {
123         self.as_inner_mut().uid(id);
124         self
125     }
126
127     fn gid(&mut self, id: u32) -> &mut process::Command {
128         self.as_inner_mut().gid(id);
129         self
130     }
131
132     unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
133     where
134         F: FnMut() -> io::Result<()> + Send + Sync + 'static,
135     {
136         self.as_inner_mut().pre_exec(Box::new(f));
137         self
138     }
139
140     fn exec(&mut self) -> io::Error {
141         self.as_inner_mut().exec(sys::process::Stdio::Inherit)
142     }
143
144     fn arg0<S>(&mut self, arg: S) -> &mut process::Command
145     where
146         S: AsRef<OsStr>,
147     {
148         self.as_inner_mut().set_arg_0(arg.as_ref());
149         self
150     }
151 }
152
153 /// Unix-specific extensions to [`process::ExitStatus`].
154 ///
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
159     /// a process.
160     #[stable(feature = "exit_status_from", since = "1.12.0")]
161     fn from_raw(raw: i32) -> Self;
162
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>;
166 }
167
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))
172     }
173
174     fn signal(&self) -> Option<i32> {
175         self.as_inner().signal()
176     }
177 }
178
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)
185     }
186 }
187
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()
192     }
193 }
194
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()
199     }
200 }
201
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()
206     }
207 }
208
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()
213     }
214 }
215
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()
220     }
221 }
222
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()
227     }
228 }
229
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()
234 }