Skip to content

Commit

Permalink
pid_open
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanWoollett-Light committed Nov 16, 2022
1 parent 592a685 commit d655abc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## [Unreleased] - ReleaseDate
### Added

- Added `pid_open`.
([#1868](https://github.com/nix-rust/nix/pull/1868))
- Added `pidfd_getfd`.
([#1868](https://github.com/nix-rust/nix/pull/1868))
- Add `MntFlags` and `unmount` on all of the BSDs.
Expand Down
1 change: 1 addition & 0 deletions src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,6 @@ feature! {
pub mod timer;
}

#[cfg(feature = "process")]
/// pidfd related functionality
pub mod pidfd;
29 changes: 29 additions & 0 deletions src/sys/pidfd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::errno::Errno;
use crate::unistd::Pid;
use crate::Result;
use std::convert::TryFrom;
use std::os::unix::io::RawFd;
Expand Down Expand Up @@ -29,3 +30,31 @@ pub fn pidfd_getfd(pidfd: RawFd, targetfd: RawFd) -> Result<RawFd> {
_ => unreachable!(),
}
}

/// Creates a file descriptor that refers to the process whose PID is specified in `pid`. The file
/// descriptor is returned as the function result; the close-on-exec flag is set on the file
/// descriptor.
///
/// If `pidfd_nonblock == true` returns a nonblocking file descriptor. If the process
/// referred to by the file descriptor has not yet terminated,
/// then an attempt to wait on the file descriptor using
/// waitid(2) will immediately return the error EAGAIN rather
/// than blocking.
pub fn pid_open(pid_t: Pid, pidfd_nonblock: bool) -> Result<RawFd> {
#[allow(clippy::useless_conversion)] // Not useless on all OSes
match unsafe {
libc::syscall(
libc::SYS_pidfd_open,
pid_t,
if pidfd_nonblock {
libc::PIDFD_NONBLOCK
} else {
0
},
)
} {
-1 => Err(Errno::last()),
fd @ 0.. => Ok(RawFd::try_from(fd).unwrap()),
_ => unreachable!(),
}
}

0 comments on commit d655abc

Please sign in to comment.