Skip to content

Commit 0343ae2

Browse files
pid_open
1 parent cef41f2 commit 0343ae2

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1111
(#[1662](https://github.com/nix-rust/nix/pull/1662))
1212
- Added `pidfd_getfd` on Linux.
1313
([#1868](https://github.com/nix-rust/nix/pull/1868))
14+
- Added `pid_open` on Linux.
15+
([#1868](https://github.com/nix-rust/nix/pull/1868))
1416

1517
### Changed
1618

src/sys/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,6 @@ feature! {
227227
pub mod timer;
228228
}
229229

230-
#[cfg(target_os = "linux")]
230+
#[cfg(all(target_os = "linux", feature = "process"))]
231231
/// pidfd related functionality
232232
pub mod pidfd;

src/sys/pidfd.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::errno::Errno;
2+
use crate::unistd::Pid;
23
use crate::Result;
34
use std::convert::TryFrom;
45
use std::os::unix::io::RawFd;
@@ -29,3 +30,33 @@ pub fn pidfd_getfd(pidfd: RawFd, targetfd: RawFd) -> Result<RawFd> {
2930
_ => unreachable!(),
3031
}
3132
}
33+
34+
/// Creates a file descriptor that refers to the process whose PID is specified in `pid`. The file
35+
/// descriptor is returned as the function result; the close-on-exec flag is set on the file
36+
/// descriptor.
37+
///
38+
/// If `pidfd_nonblock == true` returns a nonblocking file descriptor. If the process
39+
/// referred to by the file descriptor has not yet terminated,
40+
/// then an attempt to wait on the file descriptor using
41+
/// waitid(2) will immediately return the error EAGAIN rather
42+
/// than blocking.
43+
pub fn pid_open(pid_t: Pid, pidfd_nonblock: bool) -> Result<RawFd> {
44+
#[allow(clippy::useless_conversion)] // Not useless on all OSes
45+
match unsafe {
46+
libc::syscall(
47+
libc::SYS_pidfd_open,
48+
pid_t,
49+
if pidfd_nonblock {
50+
// MUSL is missing the `PIDFD_NONBLOCK` alias, so the underlying `O_NONBLOCK` needs
51+
// to be used for compatibility.
52+
libc::PIDFD_NONBLOCK
53+
} else {
54+
0
55+
},
56+
)
57+
} {
58+
-1 => Err(Errno::last()),
59+
fd @ 0.. => Ok(RawFd::try_from(fd).unwrap()),
60+
_ => unreachable!(),
61+
}
62+
}

0 commit comments

Comments
 (0)