Add PidFd type and seal traits
Improve docs Split do_fork into two Make do_fork unsafe Add target attribute to create_pidfd field in Command Add method to get create_pidfd value
This commit is contained in:
parent
ef03de2e6a
commit
619fd96868
4 changed files with 289 additions and 120 deletions
|
@ -2,40 +2,142 @@
|
|||
|
||||
#![unstable(feature = "linux_pidfd", issue = "none")]
|
||||
|
||||
use crate::process;
|
||||
use crate::sys_common::AsInnerMut;
|
||||
use crate::io::Result;
|
||||
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||
use crate::process;
|
||||
use crate::sys::fd::FileDesc;
|
||||
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
|
||||
|
||||
/// Os-specific extensions to [`process::Child`]
|
||||
/// This type represents a file descriptor that refers to a process.
|
||||
///
|
||||
/// [`process::Child`]: crate::process::Child
|
||||
pub trait ChildExt {
|
||||
/// Obtains the pidfd created for this child process, if available.
|
||||
///
|
||||
/// A pidfd will only ever be available if `create_pidfd(true)` was called
|
||||
/// when the corresponding `Command` was created.
|
||||
///
|
||||
/// Even if `create_pidfd(true)` is called, a pidfd may not be available
|
||||
/// due to an older version of Linux being in use, or if
|
||||
/// some other error occured.
|
||||
///
|
||||
/// See `man pidfd_open` for more details about pidfds.
|
||||
fn pidfd(&self) -> Result<i32>;
|
||||
/// A `PidFd` can be obtained by setting the corresponding option on [`Command`]
|
||||
/// with [`create_pidfd`]. Subsequently, the created pidfd can be retrieved
|
||||
/// from the [`Child`] by calling [`pidfd`] or [`take_pidfd`].
|
||||
///
|
||||
/// Example:
|
||||
/// ```no_run
|
||||
/// #![feature(linux_pidfd)]
|
||||
/// use std::os::linux::process::{CommandExt, ChildExt};
|
||||
/// use std::process::Command;
|
||||
///
|
||||
/// let mut child = Command::new("echo")
|
||||
/// .create_pidfd(true)
|
||||
/// .spawn()
|
||||
/// .expect("Failed to spawn child");
|
||||
///
|
||||
/// let pidfd = child
|
||||
/// .take_pidfd()
|
||||
/// .expect("Failed to retrieve pidfd");
|
||||
///
|
||||
/// // The file descriptor will be closed when `pidfd` is dropped.
|
||||
/// ```
|
||||
/// Refer to the man page of `pidfd_open(2)` for further details.
|
||||
///
|
||||
/// [`Command`]: process::Command
|
||||
/// [`create_pidfd`]: CommandExt::create_pidfd
|
||||
/// [`Child`]: process::Child
|
||||
/// [`pidfd`]: fn@ChildExt::pidfd
|
||||
/// [`take_pidfd`]: ChildExt::take_pidfd
|
||||
#[derive(Debug)]
|
||||
pub struct PidFd {
|
||||
inner: FileDesc,
|
||||
}
|
||||
|
||||
/// Os-specific extensions to [`process::Command`]
|
||||
impl AsInner<FileDesc> for PidFd {
|
||||
fn as_inner(&self) -> &FileDesc {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl FromInner<FileDesc> for PidFd {
|
||||
fn from_inner(inner: FileDesc) -> PidFd {
|
||||
PidFd { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoInner<FileDesc> for PidFd {
|
||||
fn into_inner(self) -> FileDesc {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawFd for PidFd {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.as_inner().raw()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRawFd for PidFd {
|
||||
unsafe fn from_raw_fd(fd: RawFd) -> Self {
|
||||
Self::from_inner(FileDesc::new(fd))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoRawFd for PidFd {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
self.into_inner().into_raw()
|
||||
}
|
||||
}
|
||||
|
||||
mod private_child_ext {
|
||||
pub trait Sealed {}
|
||||
impl Sealed for crate::process::Child {}
|
||||
}
|
||||
|
||||
/// Os-specific extensions for [`Child`]
|
||||
///
|
||||
/// [`process::Command`]: crate::process::Command
|
||||
pub trait CommandExt {
|
||||
/// Sets whether or this `Command` will attempt to create a pidfd
|
||||
/// for the child. If this method is never called, a pidfd will
|
||||
/// not be crated.
|
||||
/// [`Child`]: process::Child
|
||||
pub trait ChildExt: private_child_ext::Sealed {
|
||||
/// Obtains a reference to the [`PidFd`] created for this [`Child`], if available.
|
||||
///
|
||||
/// The pidfd can be retrieved from the child via [`ChildExt::pidfd`]
|
||||
/// A pidfd will only be available if its creation was requested with
|
||||
/// [`create_pidfd`] when the corresponding [`Command`] was created.
|
||||
///
|
||||
/// Even if requested, a pidfd may not be available due to an older
|
||||
/// version of Linux being in use, or if some other error occurred.
|
||||
///
|
||||
/// [`Command`]: process::Command
|
||||
/// [`create_pidfd`]: CommandExt::create_pidfd
|
||||
/// [`Child`]: process::Child
|
||||
fn pidfd(&self) -> Result<&PidFd>;
|
||||
|
||||
/// Takes ownership of the [`PidFd`] created for this [`Child`], if available.
|
||||
///
|
||||
/// A pidfd will only be available if its creation was requested with
|
||||
/// [`create_pidfd`] when the corresponding [`Command`] was created.
|
||||
///
|
||||
/// Even if requested, a pidfd may not be available due to an older
|
||||
/// version of Linux being in use, or if some other error occurred.
|
||||
///
|
||||
/// [`Command`]: process::Command
|
||||
/// [`create_pidfd`]: CommandExt::create_pidfd
|
||||
/// [`Child`]: process::Child
|
||||
fn take_pidfd(&mut self) -> Result<PidFd>;
|
||||
}
|
||||
|
||||
mod private_command_ext {
|
||||
pub trait Sealed {}
|
||||
impl Sealed for crate::process::Command {}
|
||||
}
|
||||
|
||||
/// Os-specific extensions for [`Command`]
|
||||
///
|
||||
/// [`Command`]: process::Command
|
||||
pub trait CommandExt: private_command_ext::Sealed {
|
||||
/// Sets whether a [`PidFd`](struct@PidFd) should be created for the [`Child`]
|
||||
/// spawned by this [`Command`].
|
||||
/// By default, no pidfd will be created.
|
||||
///
|
||||
/// The pidfd can be retrieved from the child with [`pidfd`] or [`take_pidfd`].
|
||||
///
|
||||
/// A pidfd will only be created if it is possible to do so
|
||||
/// in a guaranteed race-free manner (e.g. if the `clone3` system call is
|
||||
/// supported). Otherwise, [`ChildExit::pidfd`] will return an error.
|
||||
/// in a guaranteed race-free manner (e.g. if the `clone3` system call
|
||||
/// is supported). Otherwise, [`pidfd`] will return an error.
|
||||
///
|
||||
/// [`Command`]: process::Command
|
||||
/// [`Child`]: process::Child
|
||||
/// [`pidfd`]: fn@ChildExt::pidfd
|
||||
/// [`take_pidfd`]: ChildExt::take_pidfd
|
||||
fn create_pidfd(&mut self, val: bool) -> &mut process::Command;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue