File tree Expand file tree Collapse file tree 3 files changed +37
-2
lines changed Expand file tree Collapse file tree 3 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1313 ([ #1912 ] ( https://github.com/nix-rust/nix/pull/1912 ) )
1414- Added ` pidfd_getfd ` on Linux.
1515 ([ #1868 ] ( https://github.com/nix-rust/nix/pull/1868 ) )
16+ - Added ` pid_open ` on Linux.
17+ ([ #1868 ] ( https://github.com/nix-rust/nix/pull/1868 ) )
1618
1719### Changed
1820
Original file line number Diff line number Diff 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
232232pub mod pidfd;
Original file line number Diff line number Diff line change 11use crate :: errno:: Errno ;
2+ use crate :: unistd:: Pid ;
23use crate :: Result ;
34use std:: convert:: TryFrom ;
45use std:: os:: unix:: io:: { AsRawFd , FromRawFd , OwnedFd } ;
@@ -40,4 +41,36 @@ pub fn pidfd_getfd<PFd: AsRawFd, TFd: AsRawFd>(
4041 }
4142 _ => unreachable ! ( ) ,
4243 }
43- }
44+ }
45+
46+ /// Creates a file descriptor that refers to the process whose PID is specified in `pid`. The file
47+ /// descriptor is returned as the function result; the close-on-exec flag is set on the file
48+ /// descriptor.
49+ ///
50+ /// If `nonblock == true` returns a nonblocking file descriptor. If the process
51+ /// referred to by the file descriptor has not yet terminated,
52+ /// then an attempt to wait on the file descriptor using
53+ /// waitid(2) will immediately return the error EAGAIN rather
54+ /// than blocking.
55+ pub fn pid_open ( pid : Pid , nonblock : bool ) -> Result < OwnedFd > {
56+ #[ allow( clippy:: useless_conversion) ] // Not useless on all OSes
57+ match unsafe {
58+ libc:: syscall (
59+ libc:: SYS_pidfd_open ,
60+ pid,
61+ if nonblock {
62+ // MUSL is missing the `PIDFD_NONBLOCK` alias, so the underlying `O_NONBLOCK` needs
63+ // to be used for compatibility.
64+ libc:: PIDFD_NONBLOCK
65+ } else {
66+ 0
67+ } ,
68+ )
69+ } {
70+ -1 => Err ( Errno :: last ( ) ) ,
71+ fd @ 0 .. => {
72+ Ok ( unsafe { OwnedFd :: from_raw_fd ( i32:: try_from ( fd) . unwrap ( ) ) } )
73+ }
74+ _ => unreachable ! ( ) ,
75+ }
76+ }
You can’t perform that action at this time.
0 commit comments