Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit cada5fb

Browse files
committed
Update PidFd for the new I/O safety APIs.
1 parent 1ae1eee commit cada5fb

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

library/std/src/os/linux/process.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![unstable(feature = "linux_pidfd", issue = "82971")]
44

55
use crate::io::Result;
6-
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
6+
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
77
use crate::process;
88
use crate::sealed::Sealed;
99
#[cfg(not(doc))]
@@ -69,19 +69,37 @@ impl IntoInner<FileDesc> for PidFd {
6969

7070
impl AsRawFd for PidFd {
7171
fn as_raw_fd(&self) -> RawFd {
72-
self.as_inner().raw()
72+
self.as_inner().as_raw_fd()
7373
}
7474
}
7575

7676
impl FromRawFd for PidFd {
7777
unsafe fn from_raw_fd(fd: RawFd) -> Self {
78-
Self::from_inner(FileDesc::new(fd))
78+
Self::from_inner(FileDesc::from_raw_fd(fd))
7979
}
8080
}
8181

8282
impl IntoRawFd for PidFd {
8383
fn into_raw_fd(self) -> RawFd {
84-
self.into_inner().into_raw()
84+
self.into_inner().into_raw_fd()
85+
}
86+
}
87+
88+
impl AsFd for PidFd {
89+
fn as_fd(&self) -> BorrowedFd<'_> {
90+
self.as_inner().as_fd()
91+
}
92+
}
93+
94+
impl From<OwnedFd> for PidFd {
95+
fn from(fd: OwnedFd) -> Self {
96+
Self::from_inner(FileDesc::from_inner(fd))
97+
}
98+
}
99+
100+
impl From<PidFd> for OwnedFd {
101+
fn from(pid_fd: PidFd) -> Self {
102+
pid_fd.into_inner().into_inner()
85103
}
86104
}
87105

library/std/src/sys/unix/process/process_unix.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::io::{self, Error, ErrorKind};
44
use crate::mem;
55
use crate::num::NonZeroI32;
66
use crate::os::raw::NonZero_c_int;
7+
use crate::os::unix::io::FromRawFd;
78
use crate::ptr;
89
use crate::sys;
910
use crate::sys::cvt;
@@ -97,7 +98,9 @@ impl Command {
9798
drop(env_lock);
9899
drop(output);
99100

100-
let mut p = Process::new(pid, pidfd);
101+
// Safety: We obtained the pidfd from calling `clone3` with
102+
// `CLONE_PIDFD` so it's valid an otherwise unowned.
103+
let mut p = unsafe { Process::new(pid, pidfd) };
101104
let mut bytes = [0; 8];
102105

103106
// loop to handle EINTR
@@ -446,7 +449,8 @@ impl Command {
446449
None => None,
447450
};
448451

449-
let mut p = Process::new(0, -1);
452+
// Safety: -1 indicates we don't have a pidfd.
453+
let mut p = unsafe { Process::new(0, -1) };
450454

451455
struct PosixSpawnFileActions<'a>(&'a mut MaybeUninit<libc::posix_spawn_file_actions_t>);
452456

@@ -545,14 +549,16 @@ pub struct Process {
545549

546550
impl Process {
547551
#[cfg(target_os = "linux")]
548-
fn new(pid: pid_t, pidfd: pid_t) -> Self {
552+
unsafe fn new(pid: pid_t, pidfd: pid_t) -> Self {
549553
use crate::sys_common::FromInner;
550-
let pidfd = (pidfd >= 0).then(|| PidFd::from_inner(sys::fd::FileDesc::new(pidfd)));
554+
// Safety: If `pidfd` is nonnegative, we assume it's valid and otherwise unowned.
555+
let pidfd = (pidfd >= 0)
556+
.then(|| PidFd::from_inner(unsafe { sys::fd::FileDesc::from_raw_fd(pidfd) }));
551557
Process { pid, status: None, pidfd }
552558
}
553559

554560
#[cfg(not(target_os = "linux"))]
555-
fn new(pid: pid_t, _pidfd: pid_t) -> Self {
561+
unsafe fn new(pid: pid_t, _pidfd: pid_t) -> Self {
556562
Process { pid, status: None }
557563
}
558564

0 commit comments

Comments
 (0)