Skip to main content

relibc/header/sys_wait/
mod.rs

1//! `sys/wait.h` implementation.
2//!
3//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_wait.h.html>.
4
5use crate::{
6    error::ResultExt,
7    out::Out,
8    platform::{
9        Pal, Sys,
10        types::{c_int, pid_t},
11    },
12};
13
14/// Do not hang if no status is available; return immediately.
15pub const WNOHANG: c_int = 1;
16/// Report status of stopped child process.
17pub const WUNTRACED: c_int = 2;
18/// Report status of continued child process.
19pub const WCONTINUED: c_int = 8;
20
21/// Status is returned for any child that has stopped upon receipt of a signal.
22pub const WSTOPPED: c_int = 2;
23/// Wait for processes that have terminated.
24pub const WEXITED: c_int = 4;
25/// Keep the process whose status is returned in `infop` in a waitable state.
26pub const WNOWAIT: c_int = 0x0100_0000;
27
28/// Non-POSIX, see <https://www.man7.org/linux/man-pages/man2/waitpid.2.html>.
29///
30/// Do not wait for children of other threads in the same thread group.
31pub const __WNOTHREAD: c_int = 0x2000_0000;
32/// Non-POSIX, see <https://www.man7.org/linux/man-pages/man2/waitpid.2.html>.
33///
34/// Wait for all children regardless of type ("clone" or "non-clone").
35pub const __WALL: c_int = 0x4000_0000;
36/// Non-POSIX, see <https://www.man7.org/linux/man-pages/man2/waitpid.2.html>.
37///
38/// Wait for "clone" children only.
39#[allow(overflowing_literals)]
40pub const __WCLONE: c_int = 0x8000_0000;
41
42/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/wait.html>.
43///
44/// Obtains status information pertaining to one of the caller's child
45/// processes.
46///
47/// If returning because the status of a child process is available, returns a
48/// value equal to the process ID of the child process for which status is
49/// reported. If returning due to the delivery of a signal to the calling
50/// process, returns `-1` and sets errno to `EINTR`. If otherwise failing,
51/// returns `-1` and sets errno.
52#[unsafe(no_mangle)]
53pub unsafe extern "C" fn wait(stat_loc: *mut c_int) -> pid_t {
54    unsafe { waitpid(!0, stat_loc, 0) }
55}
56
57/*
58 * TODO: implement idtype_t, id_t, and siginfo_t
59 *
60 * #[unsafe(no_mangle)]
61 * pub unsafe extern "C" fn waitid(
62 *     idtype: idtype_t,
63 *     id: id_t,
64 *     infop: siginfo_t,
65 *     options: c_int
66 *  ) -> c_int {
67 *      unimplemented!();
68 *  }
69 */
70
71/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/waitpid.html>.
72///
73/// Obtains status information pertaining to one of the caller's child
74/// processes.
75///
76/// If returning because the status of a child process is available, returns a
77/// value equal to the process ID of the child process for which status is
78/// reported. If returning due to the delivery of a signal to the calling
79/// process, returns `-1` and sets errno to `EINTR`. if invoked with `WNOHANG`
80/// set in `options`, it has at least one child process specified by `pid` for
81/// wchich status is not available, and status is not available for any process
82/// specified by `pid`, `0` is returned. If otherwise failing, returns `-1` and
83/// sets errno.
84#[unsafe(no_mangle)]
85pub unsafe extern "C" fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t {
86    Sys::waitpid(pid, unsafe { Out::nullable(stat_loc) }, options).or_minus_one_errno()
87}