@@ -4,42 +4,26 @@ use unistd::Pid;
4
4
5
5
use sys:: signal:: Signal ;
6
6
7
- mod ffi {
8
- use libc:: { pid_t, c_int} ;
9
-
10
- extern {
11
- pub fn waitpid ( pid : pid_t , status : * mut c_int , options : c_int ) -> pid_t ;
12
- }
13
- }
14
-
15
- #[ cfg( not( any( target_os = "linux" ,
16
- target_os = "android" ) ) ) ]
17
- libc_bitflags ! (
18
- pub flags WaitPidFlag : c_int {
19
- WNOHANG ,
20
- WUNTRACED ,
21
- }
22
- ) ;
23
-
24
- #[ cfg( any( target_os = "linux" ,
25
- target_os = "android" ) ) ]
26
7
libc_bitflags ! (
27
8
pub flags WaitPidFlag : c_int {
28
9
WNOHANG ,
29
10
WUNTRACED ,
30
11
WEXITED ,
31
12
WCONTINUED ,
32
- WNOWAIT , // Don't reap, just poll status.
33
- __WNOTHREAD, // Don't wait on children of other threads in this group
34
- __WALL, // Wait on all children, regardless of type
13
+ WSTOPPED ,
14
+ /// Don't reap, just poll status.
15
+ WNOWAIT ,
16
+ /// Don't wait on children of other threads in this group
17
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
18
+ __WNOTHREAD,
19
+ /// Wait on all children, regardless of type
20
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
21
+ __WALL,
22
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
35
23
__WCLONE,
36
24
}
37
25
) ;
38
26
39
- #[ cfg( any( target_os = "linux" ,
40
- target_os = "android" ) ) ]
41
- const WSTOPPED : WaitPidFlag = WUNTRACED ;
42
-
43
27
/// Possible return values from `wait()` or `waitpid()`.
44
28
///
45
29
/// Each status (other than `StillAlive`) describes a state transition
@@ -54,7 +38,7 @@ pub enum WaitStatus {
54
38
/// The process exited normally (as with `exit()` or returning from
55
39
/// `main`) with the given exit code. This case matches the C macro
56
40
/// `WIFEXITED(status)`; the second field is `WEXITSTATUS(status)`.
57
- Exited ( Pid , i8 ) ,
41
+ Exited ( Pid , i32 ) ,
58
42
/// The process was killed by the given signal. The third field
59
43
/// indicates whether the signal generated a core dump. This case
60
44
/// matches the C macro `WIFSIGNALED(status)`; the last two fields
@@ -112,149 +96,52 @@ impl WaitStatus {
112
96
}
113
97
}
114
98
115
- #[ cfg( any( target_os = "linux" ,
116
- target_os = "android" ) ) ]
117
99
mod status {
118
100
use sys:: signal:: Signal ;
119
- use libc:: c_int;
120
- use libc:: SIGTRAP ;
101
+ use libc:: { self , c_int, SIGTRAP } ;
121
102
122
103
pub fn exited ( status : i32 ) -> bool {
123
- ( status & 0x7F ) == 0
104
+ unsafe { libc :: WIFEXITED ( status ) }
124
105
}
125
106
126
- pub fn exit_status ( status : i32 ) -> i8 {
127
- ( ( status & 0xFF00 ) >> 8 ) as i8
107
+ pub fn exit_status ( status : i32 ) -> i32 {
108
+ unsafe { libc :: WEXITSTATUS ( status ) }
128
109
}
129
110
130
111
pub fn signaled ( status : i32 ) -> bool {
131
- ( ( ( ( status & 0x7f ) + 1 ) as i8 ) >> 1 ) > 0
112
+ unsafe { libc :: WIFSIGNALED ( status ) }
132
113
}
133
114
134
115
pub fn term_signal ( status : i32 ) -> Signal {
135
- Signal :: from_c_int ( status & 0x7f ) . unwrap ( )
116
+ Signal :: from_c_int ( unsafe { libc :: WTERMSIG ( status ) } ) . unwrap ( )
136
117
}
137
118
138
119
pub fn dumped_core ( status : i32 ) -> bool {
139
- ( status & 0x80 ) != 0
120
+ unsafe { libc :: WCOREDUMP ( status ) }
140
121
}
141
122
142
123
pub fn stopped ( status : i32 ) -> bool {
143
- ( status & 0xff ) == 0x7f
124
+ unsafe { libc :: WIFSTOPPED ( status ) }
144
125
}
145
126
146
127
pub fn stop_signal ( status : i32 ) -> Signal {
147
- // Keep only 7 bits of the signal: the high bit
148
- // is used to indicate syscall stops, below.
149
- Signal :: from_c_int ( ( status & 0x7F00 ) >> 8 ) . unwrap ( )
128
+ Signal :: from_c_int ( unsafe { libc:: WSTOPSIG ( status) } ) . unwrap ( )
150
129
}
151
130
152
131
pub fn syscall_stop ( status : i32 ) -> bool {
153
132
// From ptrace(2), setting PTRACE_O_TRACESYSGOOD has the effect
154
133
// of delivering SIGTRAP | 0x80 as the signal number for syscall
155
134
// stops. This allows easily distinguishing syscall stops from
156
135
// genuine SIGTRAP signals.
157
- ( ( status & 0xFF00 ) >> 8 ) == SIGTRAP | 0x80
136
+ unsafe { libc :: WSTOPSIG ( status ) == SIGTRAP | 0x80 }
158
137
}
159
138
160
139
pub fn stop_additional ( status : i32 ) -> c_int {
161
140
( status >> 16 ) as c_int
162
141
}
163
142
164
143
pub fn continued ( status : i32 ) -> bool {
165
- status == 0xFFFF
166
- }
167
- }
168
-
169
- #[ cfg( any( target_os = "macos" ,
170
- target_os = "ios" ) ) ]
171
- mod status {
172
- use sys:: signal:: { Signal , SIGCONT } ;
173
-
174
- const WCOREFLAG : i32 = 0x80 ;
175
- const WSTOPPED : i32 = 0x7f ;
176
-
177
- fn wstatus ( status : i32 ) -> i32 {
178
- status & 0x7F
179
- }
180
-
181
- pub fn exit_status ( status : i32 ) -> i8 {
182
- ( ( status >> 8 ) & 0xFF ) as i8
183
- }
184
-
185
- pub fn stop_signal ( status : i32 ) -> Signal {
186
- Signal :: from_c_int ( status >> 8 ) . unwrap ( )
187
- }
188
-
189
- pub fn continued ( status : i32 ) -> bool {
190
- wstatus ( status) == WSTOPPED && stop_signal ( status) == SIGCONT
191
- }
192
-
193
- pub fn stopped ( status : i32 ) -> bool {
194
- wstatus ( status) == WSTOPPED && stop_signal ( status) != SIGCONT
195
- }
196
-
197
- pub fn exited ( status : i32 ) -> bool {
198
- wstatus ( status) == 0
199
- }
200
-
201
- pub fn signaled ( status : i32 ) -> bool {
202
- wstatus ( status) != WSTOPPED && wstatus ( status) != 0
203
- }
204
-
205
- pub fn term_signal ( status : i32 ) -> Signal {
206
- Signal :: from_c_int ( wstatus ( status) ) . unwrap ( )
207
- }
208
-
209
- pub fn dumped_core ( status : i32 ) -> bool {
210
- ( status & WCOREFLAG ) != 0
211
- }
212
- }
213
-
214
- #[ cfg( any( target_os = "freebsd" ,
215
- target_os = "openbsd" ,
216
- target_os = "dragonfly" ,
217
- target_os = "netbsd" ) ) ]
218
- mod status {
219
- use sys:: signal:: Signal ;
220
-
221
- const WCOREFLAG : i32 = 0x80 ;
222
- const WSTOPPED : i32 = 0x7f ;
223
-
224
- fn wstatus ( status : i32 ) -> i32 {
225
- status & 0x7F
226
- }
227
-
228
- pub fn stopped ( status : i32 ) -> bool {
229
- wstatus ( status) == WSTOPPED
230
- }
231
-
232
- pub fn stop_signal ( status : i32 ) -> Signal {
233
- Signal :: from_c_int ( status >> 8 ) . unwrap ( )
234
- }
235
-
236
- pub fn signaled ( status : i32 ) -> bool {
237
- wstatus ( status) != WSTOPPED && wstatus ( status) != 0 && status != 0x13
238
- }
239
-
240
- pub fn term_signal ( status : i32 ) -> Signal {
241
- Signal :: from_c_int ( wstatus ( status) ) . unwrap ( )
242
- }
243
-
244
- pub fn exited ( status : i32 ) -> bool {
245
- wstatus ( status) == 0
246
- }
247
-
248
- pub fn exit_status ( status : i32 ) -> i8 {
249
- ( status >> 8 ) as i8
250
- }
251
-
252
- pub fn continued ( status : i32 ) -> bool {
253
- status == 0x13
254
- }
255
-
256
- pub fn dumped_core ( status : i32 ) -> bool {
257
- ( status & WCOREFLAG ) != 0
144
+ unsafe { libc:: WIFCONTINUED ( status) }
258
145
}
259
146
}
260
147
@@ -299,7 +186,7 @@ pub fn waitpid<P: Into<Option<Pid>>>(pid: P, options: Option<WaitPidFlag>) -> Re
299
186
None => 0
300
187
} ;
301
188
302
- let res = unsafe { ffi :: waitpid ( pid. into ( ) . unwrap_or ( Pid :: from_raw ( -1 ) ) . into ( ) , & mut status as * mut c_int , option_bits) } ;
189
+ let res = unsafe { libc :: waitpid ( pid. into ( ) . unwrap_or ( Pid :: from_raw ( -1 ) ) . into ( ) , & mut status as * mut c_int , option_bits) } ;
303
190
304
191
Ok ( match try!( Errno :: result ( res) ) {
305
192
0 => StillAlive ,
0 commit comments