Skip to content

Commit d655abc

Browse files
pid_open
1 parent 592a685 commit d655abc

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
66
## [Unreleased] - ReleaseDate
77
### Added
88

9+
- Added `pid_open`.
10+
([#1868](https://github.com/nix-rust/nix/pull/1868))
911
- Added `pidfd_getfd`.
1012
([#1868](https://github.com/nix-rust/nix/pull/1868))
1113
- Add `MntFlags` and `unmount` on all of the BSDs.

src/sys/mod.rs

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

230+
#[cfg(feature = "process")]
230231
/// pidfd related functionality
231232
pub mod pidfd;

src/sys/pidfd.rs

Lines changed: 29 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,31 @@ 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+
libc::PIDFD_NONBLOCK
51+
} else {
52+
0
53+
},
54+
)
55+
} {
56+
-1 => Err(Errno::last()),
57+
fd @ 0.. => Ok(RawFd::try_from(fd).unwrap()),
58+
_ => unreachable!(),
59+
}
60+
}

0 commit comments

Comments
 (0)