@@ -16,7 +16,9 @@ mod ffi {
16
16
target_os = "android" ) ) ) ]
17
17
libc_bitflags ! (
18
18
pub flags WaitPidFlag : c_int {
19
+ /// Returns immediately if no child has exited
19
20
WNOHANG ,
21
+ /// Returns if a child has been stopped, but isn't being traced by ptrace.
20
22
WUNTRACED ,
21
23
}
22
24
) ;
@@ -25,13 +27,24 @@ libc_bitflags!(
25
27
target_os = "android" ) ) ]
26
28
libc_bitflags ! (
27
29
pub flags WaitPidFlag : c_int {
30
+ /// Returns immediately if no child has exited
28
31
WNOHANG ,
32
+ /// Returns if a child has been stopped, but isn't being traced by ptrace.
29
33
WUNTRACED ,
34
+ /// Waits for children that have terminated.
30
35
WEXITED ,
36
+ /// Waits for previously stopped children that have been resumed with `SIGCONT`.
31
37
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
38
+ /// Leave the child in a waitable state; a later wait call can be used to again retrieve
39
+ /// the child status information.
40
+ WNOWAIT ,
41
+ /// Don't wait on children of other threads in this group
42
+ __WNOTHREAD,
43
+ /// Wait for all children, regardless of type (clone or non-clone)
44
+ __WALL,
45
+ /// Wait for "clone" children only. If omitted then wait for "non-clone" children only.
46
+ /// (A "clone" child is one which delivers no signal, or a signal other than `SIGCHLD` to
47
+ /// its parent upon termination.) This option is ignored if `__WALL` is also specified.
35
48
__WCLONE,
36
49
}
37
50
) ;
@@ -42,11 +55,15 @@ const WSTOPPED: WaitPidFlag = WUNTRACED;
42
55
43
56
#[ derive( Eq , PartialEq , Clone , Copy , Debug ) ]
44
57
pub enum WaitStatus {
58
+ /// Signifies that the process has exited, providing the PID and associated exit status.
45
59
Exited ( Pid , i8 ) ,
60
+ /// Signifies that the process was killed by a signal, providing the PID and associated signal.
46
61
Signaled ( Pid , Signal , bool ) ,
62
+ /// Signifies that the process was stopped by a signal, providing the PID and associated signal.
47
63
Stopped ( Pid , Signal ) ,
48
64
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
49
65
PtraceEvent ( Pid , Signal , c_int ) ,
66
+ /// Signifies that the process received a `SIGCONT` signal, and thus continued.
50
67
Continued ( Pid ) ,
51
68
StillAlive
52
69
}
@@ -215,24 +232,53 @@ fn decode(pid : Pid, status: i32) -> WaitStatus {
215
232
}
216
233
}
217
234
218
- pub fn waitpid < P : Into < Option < Pid > > > ( pid : P , options : Option < WaitPidFlag > ) -> Result < WaitStatus > {
235
+ /// Waits for and returns events that are received from the given supplied process or process group
236
+ /// ID, and associated options.
237
+ ///
238
+ /// # Usage Notes
239
+ ///
240
+ /// - If the value of PID is greater than `0`, it will wait on the child with that PID.
241
+ /// - If the value of the PID is less than `-1`, it will wait on any child that
242
+ /// belongs to the process group with the absolute value of the supplied PID.
243
+ /// - If the value of the PID is `0`, it will wait on any child process that has the same
244
+ /// group ID as the current process.
245
+ /// - If the value of the PID is `-1`, it will wait on any child process of the current process.
246
+ /// - If the value of the PID is `None`, the value of PID is set to `-1`.
247
+ ///
248
+ /// # Possible Error Values
249
+ ///
250
+ /// If this function returns an error, the error value will be one of the following:
251
+ ///
252
+ /// - **ECHILD**: The process does not exist or is not a child of the current process.
253
+ /// - This may also happen if a child process has the `SIGCHLD` signal masked or set to
254
+ /// `SIG_IGN`.
255
+ /// - **EINTR**: `WNOHANG` was not set and either an unblocked signal or a `SIGCHLD` was caught.
256
+ /// - **EINVAL**: The supplied options were invalid.
257
+ pub fn waitpid < P , O > ( pid : P , options : O ) -> Result < WaitStatus >
258
+ where P : Into < Option < Pid > > ,
259
+ O : Into < Option < WaitPidFlag > >
260
+ {
219
261
use self :: WaitStatus :: * ;
220
262
221
263
let mut status: i32 = 0 ;
222
264
223
- let option_bits = match options {
224
- Some ( bits) => bits. bits ( ) ,
225
- None => 0
226
- } ;
265
+ let option_bits = options. into ( ) . map_or ( 0 , |bits| bits. bits ( ) ) ;
227
266
228
- let res = unsafe { ffi:: waitpid ( pid. into ( ) . unwrap_or ( Pid :: from_raw ( -1 ) ) . into ( ) , & mut status as * mut c_int , option_bits) } ;
267
+ let res = unsafe {
268
+ ffi:: waitpid ( pid. into ( ) . unwrap_or (
269
+ Pid :: from_raw ( -1 ) ) . into ( ) ,
270
+ & mut status as * mut c_int ,
271
+ option_bits
272
+ )
273
+ } ;
229
274
230
- Ok ( match try! ( Errno :: result ( res) ) {
275
+ Errno :: result ( res) . map ( |res| match res {
231
276
0 => StillAlive ,
232
277
res => decode ( Pid :: from_raw ( res) , status) ,
233
278
} )
234
279
}
235
280
281
+ /// Equivalent to `waitpid(-1, None)`, which waits on any child process of the current process.
236
282
pub fn wait ( ) -> Result < WaitStatus > {
237
283
waitpid ( None , None )
238
284
}
0 commit comments