Skip to content

Commit 8a24a28

Browse files
committed
Improve Wait Documentation
1 parent d0628e1 commit 8a24a28

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

src/sys/wait.rs

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ mod ffi {
1616
target_os = "android")))]
1717
libc_bitflags!(
1818
pub flags WaitPidFlag: c_int {
19+
/// Returns immediately if no child has exited
1920
WNOHANG,
21+
/// Returns if a child has been stopped, but isn't being traced by ptrace.
2022
WUNTRACED,
2123
}
2224
);
@@ -25,13 +27,24 @@ libc_bitflags!(
2527
target_os = "android"))]
2628
libc_bitflags!(
2729
pub flags WaitPidFlag: c_int {
30+
/// Returns immediately if no child has exited
2831
WNOHANG,
32+
/// Returns if a child has been stopped, but isn't being traced by ptrace.
2933
WUNTRACED,
34+
/// Waits for children that have terminated.
3035
WEXITED,
36+
/// Waits for previously stopped children that have been resumed with `SIGCONT`.
3137
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.
3548
__WCLONE,
3649
}
3750
);
@@ -42,11 +55,15 @@ const WSTOPPED: WaitPidFlag = WUNTRACED;
4255

4356
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
4457
pub enum WaitStatus {
58+
/// Signifies that the process has exited, providing the PID and associated exit status.
4559
Exited(Pid, i8),
60+
/// Signifies that the process was killed by a signal, providing the PID and associated signal.
4661
Signaled(Pid, Signal, bool),
62+
/// Signifies that the process was stopped by a signal, providing the PID and associated signal.
4763
Stopped(Pid, Signal),
4864
#[cfg(any(target_os = "linux", target_os = "android"))]
4965
PtraceEvent(Pid, Signal, c_int),
66+
/// Signifies that the process received a `SIGCONT` signal, and thus continued.
5067
Continued(Pid),
5168
StillAlive
5269
}
@@ -215,24 +232,53 @@ fn decode(pid : Pid, status: i32) -> WaitStatus {
215232
}
216233
}
217234

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+
{
219261
use self::WaitStatus::*;
220262

221263
let mut status: i32 = 0;
222264

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());
227266

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+
};
229274

230-
Ok(match try!(Errno::result(res)) {
275+
Errno::result(res).map(|res| match res {
231276
0 => StillAlive,
232277
res => decode(Pid::from_raw(res), status),
233278
})
234279
}
235280

281+
/// Equivalent to `waitpid(-1, None)`, which waits on any child process of the current process.
236282
pub fn wait() -> Result<WaitStatus> {
237283
waitpid(None, None)
238284
}

0 commit comments

Comments
 (0)