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