File tree 3 files changed +37
-2
lines changed
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/).
13
13
([ #1912 ] ( https://github.com/nix-rust/nix/pull/1912 ) )
14
14
- Added ` pidfd_getfd ` on Linux.
15
15
([ #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 ) )
16
18
17
19
### Changed
18
20
Original file line number Diff line number Diff line change @@ -227,6 +227,6 @@ feature! {
227
227
pub mod timer;
228
228
}
229
229
230
- #[ cfg( target_os = "linux" ) ]
230
+ #[ cfg( all ( target_os = "linux" , feature = "process" ) ) ]
231
231
/// pidfd related functionality
232
232
pub mod pidfd;
Original file line number Diff line number Diff line change 1
1
use crate :: errno:: Errno ;
2
+ use crate :: unistd:: Pid ;
2
3
use crate :: Result ;
3
4
use std:: convert:: TryFrom ;
4
5
use std:: os:: unix:: io:: { AsRawFd , FromRawFd , OwnedFd } ;
@@ -40,4 +41,36 @@ pub fn pidfd_getfd<PFd: AsRawFd, TFd: AsRawFd>(
40
41
}
41
42
_ => unreachable ! ( ) ,
42
43
}
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