File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed 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 if the status is not 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 => { } ,
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