-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add Linux-specific pidfd process extensions (take 2) #81825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
694be09
Add Linux-specific pidfd process extensions
Aaron1011 ef03de2
Typo fix
joshtriplett 619fd96
Add PidFd type and seal traits
voidc c3321d3
Add tracking issue and link to man-page
voidc 12fbabd
Do not call getpid wrapper after fork in tests
voidc 2a4d012
Add dummy FileDesc struct for doc target
voidc 4a832d3
Check whether clone3 syscall exists in pidfd test
voidc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
#![doc(cfg(target_os = "linux"))] | ||
|
||
pub mod fs; | ||
pub mod process; | ||
pub mod raw; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
//! Linux-specific extensions to primitives in the `std::process` module. | ||
|
||
#![unstable(feature = "linux_pidfd", issue = "82971")] | ||
|
||
use crate::io::Result; | ||
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; | ||
use crate::process; | ||
#[cfg(not(doc))] | ||
use crate::sys::fd::FileDesc; | ||
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner}; | ||
|
||
#[cfg(doc)] | ||
struct FileDesc; | ||
|
||
/// This type represents a file descriptor that refers to a process. | ||
/// | ||
/// 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 | ||
/// [`pidfd_open(2)`]: https://man7.org/linux/man-pages/man2/pidfd_open.2.html | ||
#[derive(Debug)] | ||
pub struct PidFd { | ||
inner: FileDesc, | ||
} | ||
|
||
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`] | ||
/// | ||
/// [`Child`]: process::Child | ||
pub trait ChildExt: private_child_ext::Sealed { | ||
/// Obtains a reference to 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 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, [`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; | ||
} | ||
|
||
impl CommandExt for process::Command { | ||
fn create_pidfd(&mut self, val: bool) -> &mut process::Command { | ||
self.as_inner_mut().create_pidfd(val); | ||
self | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.