Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions library/std/src/os/linux/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ impl PidFd {
pub fn try_wait(&self) -> Result<Option<ExitStatus>> {
Ok(self.inner.try_wait()?.map(FromInner::from_inner))
}

/// Obtains a duplicate of the file descriptor `targetfd`
///
/// So we can get file descriptors from another process
/// without relying on that process to explicitly send them via
/// `SCM_RIGHTS` or similar mechanisms
pub fn getfd(&self, targetfd: RawFd) -> Result<OwnedFd> {
self.inner.getfd(targetfd)
}
}

impl AsInner<InnerPidFd> for PidFd {
Expand Down
9 changes: 8 additions & 1 deletion library/std/src/sys/pal/unix/linux/pidfd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::io;
use crate::os::fd::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use crate::os::fd::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
use crate::sys::fd::FileDesc;
use crate::sys::process::ExitStatus;
use crate::sys::{AsInner, FromInner, IntoInner, cvt};
Expand Down Expand Up @@ -95,6 +95,13 @@ impl PidFd {
.map(drop)
}

#[cfg(any(test, target_env = "gnu", target_env = "musl"))]
pub fn getfd(&self, targetfd: RawFd) -> io::Result<OwnedFd> {
let fd =
cvt(unsafe { libc::syscall(libc::SYS_pidfd_getfd, self.0.as_raw_fd(), targetfd, 0) })?;
Ok(unsafe { OwnedFd::from_raw_fd(fd as RawFd) })
}

pub fn wait(&self) -> io::Result<ExitStatus> {
let r = self.waitid(libc::WEXITED)?;
match r {
Expand Down
Loading