|
| 1 | +//! Linux-specific extensions to primitives in the `std::process` module. |
| 2 | +
|
| 3 | +#![unstable(feature = "linux_pidfd", issue = "82971")] |
| 4 | + |
| 5 | +use crate::io::Result; |
| 6 | +use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; |
| 7 | +use crate::process; |
| 8 | +#[cfg(not(doc))] |
| 9 | +use crate::sys::fd::FileDesc; |
| 10 | +use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner}; |
| 11 | + |
| 12 | +#[cfg(doc)] |
| 13 | +struct FileDesc; |
| 14 | + |
| 15 | +/// This type represents a file descriptor that refers to a process. |
| 16 | +/// |
| 17 | +/// A `PidFd` can be obtained by setting the corresponding option on [`Command`] |
| 18 | +/// with [`create_pidfd`]. Subsequently, the created pidfd can be retrieved |
| 19 | +/// from the [`Child`] by calling [`pidfd`] or [`take_pidfd`]. |
| 20 | +/// |
| 21 | +/// Example: |
| 22 | +/// ```no_run |
| 23 | +/// #![feature(linux_pidfd)] |
| 24 | +/// use std::os::linux::process::{CommandExt, ChildExt}; |
| 25 | +/// use std::process::Command; |
| 26 | +/// |
| 27 | +/// let mut child = Command::new("echo") |
| 28 | +/// .create_pidfd(true) |
| 29 | +/// .spawn() |
| 30 | +/// .expect("Failed to spawn child"); |
| 31 | +/// |
| 32 | +/// let pidfd = child |
| 33 | +/// .take_pidfd() |
| 34 | +/// .expect("Failed to retrieve pidfd"); |
| 35 | +/// |
| 36 | +/// // The file descriptor will be closed when `pidfd` is dropped. |
| 37 | +/// ``` |
| 38 | +/// Refer to the man page of [`pidfd_open(2)`] for further details. |
| 39 | +/// |
| 40 | +/// [`Command`]: process::Command |
| 41 | +/// [`create_pidfd`]: CommandExt::create_pidfd |
| 42 | +/// [`Child`]: process::Child |
| 43 | +/// [`pidfd`]: fn@ChildExt::pidfd |
| 44 | +/// [`take_pidfd`]: ChildExt::take_pidfd |
| 45 | +/// [`pidfd_open(2)`]: https://man7.org/linux/man-pages/man2/pidfd_open.2.html |
| 46 | +#[derive(Debug)] |
| 47 | +pub struct PidFd { |
| 48 | + inner: FileDesc, |
| 49 | +} |
| 50 | + |
| 51 | +impl AsInner<FileDesc> for PidFd { |
| 52 | + fn as_inner(&self) -> &FileDesc { |
| 53 | + &self.inner |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl FromInner<FileDesc> for PidFd { |
| 58 | + fn from_inner(inner: FileDesc) -> PidFd { |
| 59 | + PidFd { inner } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl IntoInner<FileDesc> for PidFd { |
| 64 | + fn into_inner(self) -> FileDesc { |
| 65 | + self.inner |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl AsRawFd for PidFd { |
| 70 | + fn as_raw_fd(&self) -> RawFd { |
| 71 | + self.as_inner().raw() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl FromRawFd for PidFd { |
| 76 | + unsafe fn from_raw_fd(fd: RawFd) -> Self { |
| 77 | + Self::from_inner(FileDesc::new(fd)) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl IntoRawFd for PidFd { |
| 82 | + fn into_raw_fd(self) -> RawFd { |
| 83 | + self.into_inner().into_raw() |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +mod private_child_ext { |
| 88 | + pub trait Sealed {} |
| 89 | + impl Sealed for crate::process::Child {} |
| 90 | +} |
| 91 | + |
| 92 | +/// Os-specific extensions for [`Child`] |
| 93 | +/// |
| 94 | +/// [`Child`]: process::Child |
| 95 | +pub trait ChildExt: private_child_ext::Sealed { |
| 96 | + /// Obtains a reference to the [`PidFd`] created for this [`Child`], if available. |
| 97 | + /// |
| 98 | + /// A pidfd will only be available if its creation was requested with |
| 99 | + /// [`create_pidfd`] when the corresponding [`Command`] was created. |
| 100 | + /// |
| 101 | + /// Even if requested, a pidfd may not be available due to an older |
| 102 | + /// version of Linux being in use, or if some other error occurred. |
| 103 | + /// |
| 104 | + /// [`Command`]: process::Command |
| 105 | + /// [`create_pidfd`]: CommandExt::create_pidfd |
| 106 | + /// [`Child`]: process::Child |
| 107 | + fn pidfd(&self) -> Result<&PidFd>; |
| 108 | + |
| 109 | + /// Takes ownership of the [`PidFd`] created for this [`Child`], if available. |
| 110 | + /// |
| 111 | + /// A pidfd will only be available if its creation was requested with |
| 112 | + /// [`create_pidfd`] when the corresponding [`Command`] was created. |
| 113 | + /// |
| 114 | + /// Even if requested, a pidfd may not be available due to an older |
| 115 | + /// version of Linux being in use, or if some other error occurred. |
| 116 | + /// |
| 117 | + /// [`Command`]: process::Command |
| 118 | + /// [`create_pidfd`]: CommandExt::create_pidfd |
| 119 | + /// [`Child`]: process::Child |
| 120 | + fn take_pidfd(&mut self) -> Result<PidFd>; |
| 121 | +} |
| 122 | + |
| 123 | +mod private_command_ext { |
| 124 | + pub trait Sealed {} |
| 125 | + impl Sealed for crate::process::Command {} |
| 126 | +} |
| 127 | + |
| 128 | +/// Os-specific extensions for [`Command`] |
| 129 | +/// |
| 130 | +/// [`Command`]: process::Command |
| 131 | +pub trait CommandExt: private_command_ext::Sealed { |
| 132 | + /// Sets whether a [`PidFd`](struct@PidFd) should be created for the [`Child`] |
| 133 | + /// spawned by this [`Command`]. |
| 134 | + /// By default, no pidfd will be created. |
| 135 | + /// |
| 136 | + /// The pidfd can be retrieved from the child with [`pidfd`] or [`take_pidfd`]. |
| 137 | + /// |
| 138 | + /// A pidfd will only be created if it is possible to do so |
| 139 | + /// in a guaranteed race-free manner (e.g. if the `clone3` system call |
| 140 | + /// is supported). Otherwise, [`pidfd`] will return an error. |
| 141 | + /// |
| 142 | + /// [`Command`]: process::Command |
| 143 | + /// [`Child`]: process::Child |
| 144 | + /// [`pidfd`]: fn@ChildExt::pidfd |
| 145 | + /// [`take_pidfd`]: ChildExt::take_pidfd |
| 146 | + fn create_pidfd(&mut self, val: bool) -> &mut process::Command; |
| 147 | +} |
| 148 | + |
| 149 | +impl CommandExt for process::Command { |
| 150 | + fn create_pidfd(&mut self, val: bool) -> &mut process::Command { |
| 151 | + self.as_inner_mut().create_pidfd(val); |
| 152 | + self |
| 153 | + } |
| 154 | +} |
0 commit comments