File tree 3 files changed +35
-1
lines changed
3 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
12
12
([ #672 ] ( https://github.com/nix-rust/nix/pull/672 ) )
13
13
- Added protocol families in ` AddressFamily ` enum.
14
14
([ #647 ] ( https://github.com/nix-rust/nix/pull/647 ) )
15
+ - Added the ` pid() ` method to ` WaitStatus ` for extracting the PID.
16
+ ([ #722 ] ( https://github.com/nix-rust/nix/pull/722 ) )
15
17
16
18
### Changed
17
19
- Renamed existing ` ptrace ` wrappers to encourage namespacing ([ #692 ] ( https://github.com/nix-rust/nix/pull/692 ) )
Original file line number Diff line number Diff line change @@ -93,6 +93,25 @@ pub enum WaitStatus {
93
93
StillAlive
94
94
}
95
95
96
+ impl WaitStatus {
97
+ /// Extracts the PID from the WaitStatus unless it equals StillAlive.
98
+ pub fn pid ( & self ) -> Option < Pid > {
99
+ use self :: WaitStatus :: * ;
100
+ match * self {
101
+ Exited ( p, _) => Some ( p) ,
102
+ Signaled ( p, _, _) => Some ( p) ,
103
+ Stopped ( p, _) => Some ( p) ,
104
+ Continued ( p) => Some ( p) ,
105
+ StillAlive => None ,
106
+
107
+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
108
+ PtraceEvent ( p, _, _) => Some ( p) ,
109
+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
110
+ PtraceSyscall ( p) => Some ( p) ,
111
+ }
112
+ }
113
+ }
114
+
96
115
#[ cfg( any( target_os = "linux" ,
97
116
target_os = "android" ) ) ]
98
117
mod status {
Original file line number Diff line number Diff line change @@ -37,6 +37,19 @@ fn test_wait_exit() {
37
37
}
38
38
}
39
39
40
+ #[ test]
41
+ fn test_waitstatus_pid ( ) {
42
+ let _m = :: FORK_MTX . lock ( ) . expect ( "Mutex got poisoned by another test" ) ;
43
+
44
+ match fork ( ) . unwrap ( ) {
45
+ Child => unsafe { _exit ( 0 ) } ,
46
+ Parent { child } => {
47
+ let status = waitpid ( child, None ) . unwrap ( ) ;
48
+ assert_eq ! ( status. pid( ) , Some ( child) ) ;
49
+ }
50
+ }
51
+ }
52
+
40
53
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
41
54
// FIXME: qemu-user doesn't implement ptrace on most arches
42
55
#[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
@@ -47,7 +60,7 @@ mod ptrace {
47
60
use nix:: sys:: wait:: * ;
48
61
use nix:: unistd:: * ;
49
62
use nix:: unistd:: ForkResult :: * ;
50
- use std:: { ptr, process } ;
63
+ use std:: ptr;
51
64
use libc:: _exit;
52
65
53
66
fn ptrace_child ( ) -> ! {
You can’t perform that action at this time.
0 commit comments