std/os/unix/process.rs
1//! Unix-specific extensions to primitives in the [`std::process`] module.
2//!
3//! [`std::process`]: crate::process
4
5#![stable(feature = "rust1", since = "1.0.0")]
6
7use crate::ffi::OsStr;
8use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
9use crate::path::Path;
10use crate::sys::process::ChildPipe;
11use crate::sys::{AsInner, AsInnerMut, FromInner, IntoInner};
12use crate::{io, process, sys};
13
14cfg_select! {
15 any(target_os = "vxworks", target_os = "espidf", target_os = "horizon", target_os = "vita") => {
16 type UserId = u16;
17 type GroupId = u16;
18 }
19 target_os = "nto" => {
20 // Both IDs are signed, see `sys/target_nto.h` of the QNX Neutrino SDP.
21 // Only positive values should be used, see e.g.
22 // https://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.lib_ref/topic/s/setuid.html
23 type UserId = i32;
24 type GroupId = i32;
25 }
26 _ => {
27 type UserId = u32;
28 type GroupId = u32;
29 }
30}
31
32/// Unix-specific extensions to the [`process::Command`] builder.
33#[stable(feature = "rust1", since = "1.0.0")]
34pub impl(self) trait CommandExt {
35 /// Sets the child process's user ID. This translates to a
36 /// `setuid` call in the child process. Failure in the `setuid`
37 /// call will cause the spawn to fail.
38 ///
39 /// # Notes
40 ///
41 /// This will also trigger a call to `setgroups(0, NULL)` in the child
42 /// process if no groups have been specified.
43 /// This removes supplementary groups that might have given the child
44 /// unwanted permissions.
45 #[stable(feature = "rust1", since = "1.0.0")]
46 fn uid(&mut self, id: UserId) -> &mut process::Command;
47
48 /// Similar to `uid`, but sets the group ID of the child process. This has
49 /// the same semantics as the `uid` field.
50 #[stable(feature = "rust1", since = "1.0.0")]
51 fn gid(&mut self, id: GroupId) -> &mut process::Command;
52
53 /// Sets the supplementary group IDs for the calling process. Translates to
54 /// a `setgroups` call in the child process.
55 #[unstable(feature = "setgroups", issue = "90747")]
56 fn groups(&mut self, groups: &[GroupId]) -> &mut process::Command;
57
58 /// Schedules a closure to be run just before the `exec` function is
59 /// invoked.
60 ///
61 /// The closure is allowed to return an I/O error whose OS error code will
62 /// be communicated back to the parent and returned as an error from when
63 /// the spawn was requested.
64 ///
65 /// Multiple closures can be registered and they will be called in order of
66 /// their registration. If a closure returns `Err` then no further closures
67 /// will be called and the spawn operation will immediately return with a
68 /// failure.
69 ///
70 /// # Notes and Safety
71 ///
72 /// This closure will be run in the context of the child process after a
73 /// `fork`. This primarily means that any modifications made to memory on
74 /// behalf of this closure will **not** be visible to the parent process.
75 /// This is often a very constrained environment where normal operations
76 /// like `malloc`, accessing environment variables through [`std::env`]
77 /// or acquiring a mutex are not guaranteed to work (due to
78 /// other threads perhaps still running when the `fork` was run).
79 ///
80 /// Note that the list of allocating functions includes [`Error::new`] and
81 /// [`Error::other`]. To signal a non-trivial error, prefer [`panic!`].
82 ///
83 /// For further details refer to the [POSIX fork() specification]
84 /// and the equivalent documentation for any targeted
85 /// platform, especially the requirements around *async-signal-safety*.
86 ///
87 /// This also means that all resources such as file descriptors and
88 /// memory-mapped regions got duplicated. It is your responsibility to make
89 /// sure that the closure does not violate library invariants by making
90 /// invalid use of these duplicates.
91 ///
92 /// Panicking in the closure is safe only if all the format arguments for the
93 /// panic message can be safely formatted; this is because although
94 /// `Command` calls [`std::panic::always_abort`](crate::panic::always_abort)
95 /// before calling the pre_exec hook, panic will still try to format the
96 /// panic message.
97 ///
98 /// When this closure is run, aspects such as the stdio file descriptors and
99 /// working directory have successfully been changed, so output to these
100 /// locations might not appear where intended.
101 ///
102 /// [POSIX fork() specification]:
103 /// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html
104 /// [`std::env`]: mod@crate::env
105 /// [`Error::new`]: crate::io::Error::new
106 /// [`Error::other`]: crate::io::Error::other
107 #[stable(feature = "process_pre_exec", since = "1.34.0")]
108 unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
109 where
110 F: FnMut() -> io::Result<()> + Send + Sync + 'static;
111
112 /// Schedules a closure to be run just before the `exec` function is
113 /// invoked.
114 ///
115 /// `before_exec` used to be a safe method, but it needs to be unsafe since the closure may only
116 /// perform operations that are *async-signal-safe*. Hence it got deprecated in favor of the
117 /// unsafe [`pre_exec`]. Meanwhile, Rust gained the ability to make an existing safe method
118 /// fully unsafe in a new edition, which is how `before_exec` became `unsafe`. It still also
119 /// remains deprecated; `pre_exec` should be used instead.
120 ///
121 /// [`pre_exec`]: CommandExt::pre_exec
122 #[stable(feature = "process_exec", since = "1.15.0")]
123 #[deprecated(since = "1.37.0", note = "should be unsafe, use `pre_exec` instead")]
124 #[rustc_deprecated_safe_2024(audit_that = "the closure is async-signal-safe")]
125 unsafe fn before_exec<F>(&mut self, f: F) -> &mut process::Command
126 where
127 F: FnMut() -> io::Result<()> + Send + Sync + 'static,
128 {
129 unsafe { self.pre_exec(f) }
130 }
131
132 /// Performs all the required setup by this `Command`, followed by calling
133 /// the `execvp` syscall.
134 ///
135 /// On success this function will not return, and otherwise it will return
136 /// an error indicating why the exec (or another part of the setup of the
137 /// `Command`) failed.
138 ///
139 /// `exec` not returning has the same implications as calling
140 /// [`process::exit`] – no destructors on the current stack or any other
141 /// thread’s stack will be run. Therefore, it is recommended to only call
142 /// `exec` at a point where it is fine to not run any destructors. Note,
143 /// that the `execvp` syscall independently guarantees that all memory is
144 /// freed and all file descriptors with the `CLOEXEC` option (set by default
145 /// on all file descriptors opened by the standard library) are closed.
146 ///
147 /// This function, unlike `spawn`, will **not** `fork` the process to create
148 /// a new child. Like spawn, however, the default behavior for the stdio
149 /// descriptors will be to inherit them from the current process.
150 ///
151 /// # Notes
152 ///
153 /// The process may be in a "broken state" if this function returns in
154 /// error. For example the working directory, environment variables, signal
155 /// handling settings, various user/group information, or aspects of stdio
156 /// file descriptors may have changed. If a "transactional spawn" is
157 /// required to gracefully handle errors it is recommended to use the
158 /// cross-platform `spawn` instead.
159 #[stable(feature = "process_exec2", since = "1.9.0")]
160 #[must_use]
161 fn exec(&mut self) -> io::Error;
162
163 /// Set executable argument
164 ///
165 /// Set the first process argument, `argv[0]`, to something other than the
166 /// default executable path.
167 #[stable(feature = "process_set_argv0", since = "1.45.0")]
168 fn arg0<S>(&mut self, arg: S) -> &mut process::Command
169 where
170 S: AsRef<OsStr>;
171
172 /// Sets the process group ID (PGID) of the child process. Equivalent to a
173 /// `setpgid` call in the child process, but may be more efficient.
174 ///
175 /// Process groups determine which processes receive signals.
176 ///
177 /// # Examples
178 ///
179 /// Pressing Ctrl-C in a terminal will send SIGINT to all processes in
180 /// the current foreground process group. By spawning the `sleep`
181 /// subprocess in a new process group, it will not receive SIGINT from the
182 /// terminal.
183 ///
184 /// The parent process could install a signal handler and manage the
185 /// subprocess on its own terms.
186 ///
187 /// A process group ID of 0 will use the process ID as the PGID.
188 ///
189 /// ```no_run
190 /// use std::process::Command;
191 /// use std::os::unix::process::CommandExt;
192 ///
193 /// Command::new("sleep")
194 /// .arg("10")
195 /// .process_group(0)
196 /// .spawn()?
197 /// .wait()?;
198 /// #
199 /// # Ok::<_, Box<dyn std::error::Error>>(())
200 /// ```
201 #[stable(feature = "process_set_process_group", since = "1.64.0")]
202 fn process_group(&mut self, pgroup: i32) -> &mut process::Command;
203
204 /// Set the root of the child process. This calls `chroot` in the child process before executing
205 /// the command.
206 ///
207 /// This happens before changing to the directory specified with
208 /// [`process::Command::current_dir`], and that directory will be relative to the new root.
209 ///
210 /// If no directory has been specified with [`process::Command::current_dir`], this will set the
211 /// directory to `/`, to avoid leaving the current directory outside the chroot. (This is an
212 /// intentional difference from the underlying `chroot` system call.)
213 #[unstable(feature = "process_chroot", issue = "141298")]
214 fn chroot<P: AsRef<Path>>(&mut self, dir: P) -> &mut process::Command;
215
216 #[unstable(feature = "process_setsid", issue = "105376")]
217 fn setsid(&mut self, setsid: bool) -> &mut process::Command;
218}
219
220#[stable(feature = "rust1", since = "1.0.0")]
221impl CommandExt for process::Command {
222 fn uid(&mut self, id: UserId) -> &mut process::Command {
223 self.as_inner_mut().uid(id);
224 self
225 }
226
227 fn gid(&mut self, id: GroupId) -> &mut process::Command {
228 self.as_inner_mut().gid(id);
229 self
230 }
231
232 fn groups(&mut self, groups: &[GroupId]) -> &mut process::Command {
233 self.as_inner_mut().groups(groups);
234 self
235 }
236
237 unsafe fn pre_exec<F>(&mut self, f: F) -> &mut process::Command
238 where
239 F: FnMut() -> io::Result<()> + Send + Sync + 'static,
240 {
241 self.as_inner_mut().pre_exec(Box::new(f));
242 self
243 }
244
245 fn exec(&mut self) -> io::Error {
246 // NOTE: This may *not* be safe to call after `libc::fork`, because it
247 // may allocate. That may be worth fixing at some point in the future.
248 self.as_inner_mut().exec(sys::process::Stdio::Inherit)
249 }
250
251 fn arg0<S>(&mut self, arg: S) -> &mut process::Command
252 where
253 S: AsRef<OsStr>,
254 {
255 self.as_inner_mut().set_arg_0(arg.as_ref());
256 self
257 }
258
259 fn process_group(&mut self, pgroup: i32) -> &mut process::Command {
260 self.as_inner_mut().pgroup(pgroup);
261 self
262 }
263
264 fn chroot<P: AsRef<Path>>(&mut self, dir: P) -> &mut process::Command {
265 self.as_inner_mut().chroot(dir.as_ref());
266 self
267 }
268
269 fn setsid(&mut self, setsid: bool) -> &mut process::Command {
270 self.as_inner_mut().setsid(setsid);
271 self
272 }
273}
274
275/// Unix-specific extensions to [`process::ExitStatus`] and
276/// [`ExitStatusError`](process::ExitStatusError).
277///
278/// On Unix, `ExitStatus` **does not necessarily represent an exit status**, as
279/// passed to the `_exit` system call or returned by
280/// [`ExitStatus::code()`](crate::process::ExitStatus::code). It represents **any wait status**
281/// as returned by one of the `wait` family of system
282/// calls.
283///
284/// A Unix wait status (a Rust `ExitStatus`) can represent a Unix exit status, but can also
285/// represent other kinds of process event.
286#[stable(feature = "rust1", since = "1.0.0")]
287pub impl(self) trait ExitStatusExt {
288 /// Creates a new `ExitStatus` or `ExitStatusError` from the raw underlying integer status
289 /// value from `wait`
290 ///
291 /// The value should be a **wait status, not an exit status**.
292 ///
293 /// # Panics
294 ///
295 /// Panics on an attempt to make an `ExitStatusError` from a wait status of `0`.
296 ///
297 /// Making an `ExitStatus` always succeeds and never panics.
298 #[stable(feature = "exit_status_from", since = "1.12.0")]
299 fn from_raw(raw: i32) -> Self;
300
301 /// If the process was terminated by a signal, returns that signal.
302 ///
303 /// In other words, if `WIFSIGNALED`, this returns `WTERMSIG`.
304 #[stable(feature = "rust1", since = "1.0.0")]
305 fn signal(&self) -> Option<i32>;
306
307 /// If the process was terminated by a signal, says whether it dumped core.
308 #[stable(feature = "unix_process_wait_more", since = "1.58.0")]
309 fn core_dumped(&self) -> bool;
310
311 /// If the process was stopped by a signal, returns that signal.
312 ///
313 /// In other words, if `WIFSTOPPED`, this returns `WSTOPSIG`. This is only possible if the status came from
314 /// a `wait` system call which was passed `WUNTRACED`, and was then converted into an `ExitStatus`.
315 #[stable(feature = "unix_process_wait_more", since = "1.58.0")]
316 fn stopped_signal(&self) -> Option<i32>;
317
318 /// Whether the process was continued from a stopped status.
319 ///
320 /// Ie, `WIFCONTINUED`. This is only possible if the status came from a `wait` system call
321 /// which was passed `WCONTINUED`, and was then converted into an `ExitStatus`.
322 #[stable(feature = "unix_process_wait_more", since = "1.58.0")]
323 fn continued(&self) -> bool;
324
325 /// Returns the underlying raw `wait` status.
326 ///
327 /// The returned integer is a **wait status, not an exit status**.
328 #[stable(feature = "unix_process_wait_more", since = "1.58.0")]
329 fn into_raw(self) -> i32;
330}
331
332#[stable(feature = "rust1", since = "1.0.0")]
333impl ExitStatusExt for process::ExitStatus {
334 fn from_raw(raw: i32) -> Self {
335 process::ExitStatus::from_inner(From::from(raw))
336 }
337
338 fn signal(&self) -> Option<i32> {
339 self.as_inner().signal()
340 }
341
342 fn core_dumped(&self) -> bool {
343 self.as_inner().core_dumped()
344 }
345
346 fn stopped_signal(&self) -> Option<i32> {
347 self.as_inner().stopped_signal()
348 }
349
350 fn continued(&self) -> bool {
351 self.as_inner().continued()
352 }
353
354 fn into_raw(self) -> i32 {
355 self.as_inner().into_raw().into()
356 }
357}
358
359#[unstable(feature = "exit_status_error", issue = "84908")]
360impl ExitStatusExt for process::ExitStatusError {
361 fn from_raw(raw: i32) -> Self {
362 process::ExitStatus::from_raw(raw)
363 .exit_ok()
364 .expect_err("<ExitStatusError as ExitStatusExt>::from_raw(0) but zero is not an error")
365 }
366
367 fn signal(&self) -> Option<i32> {
368 self.into_status().signal()
369 }
370
371 fn core_dumped(&self) -> bool {
372 self.into_status().core_dumped()
373 }
374
375 fn stopped_signal(&self) -> Option<i32> {
376 self.into_status().stopped_signal()
377 }
378
379 fn continued(&self) -> bool {
380 self.into_status().continued()
381 }
382
383 fn into_raw(self) -> i32 {
384 self.into_status().into_raw()
385 }
386}
387
388#[unstable(feature = "unix_send_signal", issue = "141975")]
389pub impl(self) trait ChildExt {
390 /// Sends a signal to a child process.
391 ///
392 /// # Errors
393 ///
394 /// This function will return an error if the signal is invalid. The integer values associated
395 /// with signals are implementation-specific, so it's encouraged to use a crate that provides
396 /// posix bindings.
397 ///
398 /// # Examples
399 ///
400 /// ```rust
401 /// #![feature(unix_send_signal)]
402 ///
403 /// use std::{io, os::unix::process::ChildExt, process::{Command, Stdio}};
404 ///
405 /// use libc::SIGTERM;
406 ///
407 /// fn main() -> io::Result<()> {
408 /// # if cfg!(not(all(target_vendor = "apple", not(target_os = "macos")))) {
409 /// let child = Command::new("cat").stdin(Stdio::piped()).spawn()?;
410 /// child.send_signal(SIGTERM)?;
411 /// # }
412 /// Ok(())
413 /// }
414 /// ```
415 fn send_signal(&self, signal: i32) -> io::Result<()>;
416
417 /// Sends a signal to a child process's process group.
418 ///
419 /// # Errors
420 ///
421 /// This function will return an error if the signal is invalid or if the
422 /// child process does not have a process group. The integer values
423 /// associated with signals are implementation-specific, so it's encouraged
424 /// to use a crate that provides posix bindings.
425 ///
426 /// # Examples
427 ///
428 /// ```rust
429 /// #![feature(unix_send_signal)]
430 ///
431 /// use std::{io, os::unix::process::{ChildExt, CommandExt}, process::{Command, Stdio}};
432 ///
433 /// use libc::SIGTERM;
434 ///
435 /// fn main() -> io::Result<()> {
436 /// # if cfg!(not(all(target_vendor = "apple", not(target_os = "macos")))) {
437 /// let child = Command::new("cat")
438 /// .stdin(Stdio::piped())
439 /// .process_group(0)
440 /// .spawn()?;
441 /// child.send_process_group_signal(SIGTERM)?;
442 /// # }
443 /// Ok(())
444 /// }
445 /// ```
446 #[unstable(feature = "unix_send_signal", issue = "141975")]
447 fn send_process_group_signal(&self, signal: i32) -> io::Result<()>;
448
449 /// Forces the child process's process group to exit.
450 ///
451 /// This is analogous to [`Child::kill`] but applies to every process in
452 /// the child process's process group.
453 ///
454 /// Use [`CommandExt::process_group`] to assign a child process to an
455 /// existing process group, or to make it the leader of a new process group.
456 /// By default spawned processes are in the parent's process group.
457 ///
458 /// # Examples
459 ///
460 /// ```rust
461 /// #![feature(unix_kill_process_group)]
462 ///
463 /// use std::{os::unix::process::{ChildExt, CommandExt}, process::{Command, Stdio}};
464 ///
465 /// fn main() -> std::io::Result<()> {
466 /// let mut child = Command::new("cat")
467 /// .stdin(Stdio::piped())
468 /// .process_group(0)
469 /// .spawn()?;
470 /// child.kill_process_group()?;
471 /// Ok(())
472 /// }
473 /// ```
474 ///
475 /// [`Child::kill`]: process::Child::kill
476 #[unstable(feature = "unix_kill_process_group", issue = "156537")]
477 fn kill_process_group(&mut self) -> io::Result<()>;
478}
479
480#[unstable(feature = "unix_send_signal", issue = "141975")]
481impl ChildExt for process::Child {
482 fn send_signal(&self, signal: i32) -> io::Result<()> {
483 self.handle.send_signal(signal)
484 }
485
486 fn send_process_group_signal(&self, signal: i32) -> io::Result<()> {
487 self.handle.send_process_group_signal(signal)
488 }
489
490 #[cfg(not(target_os = "espidf"))]
491 fn kill_process_group(&mut self) -> io::Result<()> {
492 self.handle.send_process_group_signal(libc::SIGKILL)
493 }
494
495 #[cfg(target_os = "espidf")]
496 fn kill_process_group(&mut self) -> io::Result<()> {
497 Err(io::Error::new(
498 io::ErrorKind::Unsupported,
499 "process groups are not supported on espidf",
500 ))
501 }
502}
503
504#[stable(feature = "process_extensions", since = "1.2.0")]
505impl FromRawFd for process::Stdio {
506 #[inline]
507 unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio {
508 let fd = sys::fd::FileDesc::from_raw_fd(fd);
509 let io = sys::process::Stdio::Fd(fd);
510 process::Stdio::from_inner(io)
511 }
512}
513
514#[stable(feature = "io_safety", since = "1.63.0")]
515impl From<OwnedFd> for process::Stdio {
516 /// Takes ownership of a file descriptor and returns a [`Stdio`](process::Stdio)
517 /// that can attach a stream to it.
518 #[inline]
519 fn from(fd: OwnedFd) -> process::Stdio {
520 let fd = sys::fd::FileDesc::from_inner(fd);
521 let io = sys::process::Stdio::Fd(fd);
522 process::Stdio::from_inner(io)
523 }
524}
525
526#[stable(feature = "process_extensions", since = "1.2.0")]
527impl AsRawFd for process::ChildStdin {
528 #[inline]
529 fn as_raw_fd(&self) -> RawFd {
530 self.as_inner().as_raw_fd()
531 }
532}
533
534#[stable(feature = "process_extensions", since = "1.2.0")]
535impl AsRawFd for process::ChildStdout {
536 #[inline]
537 fn as_raw_fd(&self) -> RawFd {
538 self.as_inner().as_raw_fd()
539 }
540}
541
542#[stable(feature = "process_extensions", since = "1.2.0")]
543impl AsRawFd for process::ChildStderr {
544 #[inline]
545 fn as_raw_fd(&self) -> RawFd {
546 self.as_inner().as_raw_fd()
547 }
548}
549
550#[stable(feature = "into_raw_os", since = "1.4.0")]
551impl IntoRawFd for process::ChildStdin {
552 #[inline]
553 fn into_raw_fd(self) -> RawFd {
554 self.into_inner().into_inner().into_raw_fd()
555 }
556}
557
558#[stable(feature = "into_raw_os", since = "1.4.0")]
559impl IntoRawFd for process::ChildStdout {
560 #[inline]
561 fn into_raw_fd(self) -> RawFd {
562 self.into_inner().into_inner().into_raw_fd()
563 }
564}
565
566#[stable(feature = "into_raw_os", since = "1.4.0")]
567impl IntoRawFd for process::ChildStderr {
568 #[inline]
569 fn into_raw_fd(self) -> RawFd {
570 self.into_inner().into_inner().into_raw_fd()
571 }
572}
573
574#[stable(feature = "io_safety", since = "1.63.0")]
575impl AsFd for crate::process::ChildStdin {
576 #[inline]
577 fn as_fd(&self) -> BorrowedFd<'_> {
578 self.as_inner().as_fd()
579 }
580}
581
582#[stable(feature = "io_safety", since = "1.63.0")]
583impl From<crate::process::ChildStdin> for OwnedFd {
584 /// Takes ownership of a [`ChildStdin`](crate::process::ChildStdin)'s file descriptor.
585 #[inline]
586 fn from(child_stdin: crate::process::ChildStdin) -> OwnedFd {
587 child_stdin.into_inner().into_inner()
588 }
589}
590
591/// Creates a `ChildStdin` from the provided `OwnedFd`.
592///
593/// The provided file descriptor must point to a pipe
594/// with the `CLOEXEC` flag set.
595#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
596impl From<OwnedFd> for process::ChildStdin {
597 #[inline]
598 fn from(fd: OwnedFd) -> process::ChildStdin {
599 let pipe = ChildPipe::from_inner(fd);
600 process::ChildStdin::from_inner(pipe)
601 }
602}
603
604#[stable(feature = "io_safety", since = "1.63.0")]
605impl AsFd for crate::process::ChildStdout {
606 #[inline]
607 fn as_fd(&self) -> BorrowedFd<'_> {
608 self.as_inner().as_fd()
609 }
610}
611
612#[stable(feature = "io_safety", since = "1.63.0")]
613impl From<crate::process::ChildStdout> for OwnedFd {
614 /// Takes ownership of a [`ChildStdout`](crate::process::ChildStdout)'s file descriptor.
615 #[inline]
616 fn from(child_stdout: crate::process::ChildStdout) -> OwnedFd {
617 child_stdout.into_inner().into_inner()
618 }
619}
620
621/// Creates a `ChildStdout` from the provided `OwnedFd`.
622///
623/// The provided file descriptor must point to a pipe
624/// with the `CLOEXEC` flag set.
625#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
626impl From<OwnedFd> for process::ChildStdout {
627 #[inline]
628 fn from(fd: OwnedFd) -> process::ChildStdout {
629 let pipe = ChildPipe::from_inner(fd);
630 process::ChildStdout::from_inner(pipe)
631 }
632}
633
634#[stable(feature = "io_safety", since = "1.63.0")]
635impl AsFd for crate::process::ChildStderr {
636 #[inline]
637 fn as_fd(&self) -> BorrowedFd<'_> {
638 self.as_inner().as_fd()
639 }
640}
641
642#[stable(feature = "io_safety", since = "1.63.0")]
643impl From<crate::process::ChildStderr> for OwnedFd {
644 /// Takes ownership of a [`ChildStderr`](crate::process::ChildStderr)'s file descriptor.
645 #[inline]
646 fn from(child_stderr: crate::process::ChildStderr) -> OwnedFd {
647 child_stderr.into_inner().into_inner()
648 }
649}
650
651/// Creates a `ChildStderr` from the provided `OwnedFd`.
652///
653/// The provided file descriptor must point to a pipe
654/// with the `CLOEXEC` flag set.
655#[stable(feature = "child_stream_from_fd", since = "1.74.0")]
656impl From<OwnedFd> for process::ChildStderr {
657 #[inline]
658 fn from(fd: OwnedFd) -> process::ChildStderr {
659 let pipe = ChildPipe::from_inner(fd);
660 process::ChildStderr::from_inner(pipe)
661 }
662}
663
664/// Returns the OS-assigned process identifier associated with this process's parent.
665#[must_use]
666#[stable(feature = "unix_ppid", since = "1.27.0")]
667pub fn parent_id() -> u32 {
668 crate::sys::process::getppid()
669}