Debian Codegraph @

src/sys/unix.rs

raw symbols SCIP
1 use std::io;
2 use std::mem::MaybeUninit;
3 use std::process::Child;
4 #[cfg(feature = "timeout")]
5 use std::time::Instant;
6
7 // A handle on Unix is just the PID.
8 #[derive(Copy, Clone)]
9 pub struct Handle(u32);
10
11 pub fn get_handle(child: &Child) -> Handle {
12 Handle(child.id())
13 }
14
15 // This blocks until the child exits, without reaping the child.
16 pub fn wait_noreap(handle: Handle) -> io::Result<()> {
17 loop {
18 let mut siginfo = MaybeUninit::zeroed();
19 let ret = unsafe {
20 libc::waitid(
21 libc::P_PID,
22 handle.0 as libc::id_t,
23 siginfo.as_mut_ptr(),
24 libc::WEXITED | libc::WNOWAIT,
25 )
26 };
27 if ret == 0 {
28 return Ok(());
29 }
30 let error = io::Error::last_os_error();
31 if error.kind() != io::ErrorKind::Interrupted {
32 return Err(error);
33 }
34 // We were interrupted. Loop and retry.
35 }
36 }
37
38 // This checks whether the child has already exited, without reaping the child.
39 pub fn try_wait_noreap(handle: Handle) -> io::Result<bool> {
40 let mut siginfo: libc::siginfo_t;
41 let ret = unsafe {
42 // Darwin doesn't touch the siginfo_t struct if the child hasn't exited
43 // yet. It expects us to have zeroed it ahead of time:
44 //
45 // The state of the siginfo structure in this case
46 // is undefined. Some implementations bzero it, some
47 // (like here) leave it untouched for efficiency.
48 //
49 // Thus the most portable check for "no matching pid with
50 // WNOHANG" is to store a zero into si_pid before
51 // invocation, then check for a non-zero value afterwards.
52 //
53 // https://github.com/opensource-apple/xnu/blob/0a798f6738bc1db01281fc08ae024145e84df927/bsd/kern/kern_exit.c#L2150-L2156
54 siginfo = std::mem::zeroed();
55 libc::waitid(
56 libc::P_PID,
57 handle.0 as libc::id_t,
58 &mut siginfo,
59 libc::WEXITED | libc::WNOWAIT | libc::WNOHANG,
60 )
61 };
62 if ret != 0 {
63 // EINTR should be impossible here
64 Err(io::Error::last_os_error())
65 } else if siginfo.si_signo == libc::SIGCHLD {
66 // The child has exited.
67 Ok(true)
68 } else if siginfo.si_signo == 0 {
69 // The child has not exited.
70 Ok(false)
71 } else {
72 // This should be impossible if we called waitid correctly. But it will
73 // show up on macOS if we forgot to zero the siginfo_t above, for example.
74 Err(io::Error::other(format!(
75 "unexpected si_signo from waitid: {}",
76 siginfo.si_signo
77 )))
78 }
79 }
80
81 // This blocks until either the child exits or the deadline passes, without reaping the child.
82 #[cfg(feature = "timeout")]
83 pub fn wait_deadline_noreap(handle: Handle, deadline: Instant) -> io::Result<bool> {
84 let mut sigchld_waiter = sigchld::Waiter::new()?;
85 loop {
86 // Has the child exited?
87 if try_wait_noreap(handle)? {
88 return Ok(true);
89 }
90 // Has the deadline passed?
91 if deadline < Instant::now() {
92 return Ok(false);
93 }
94 // Wait for the next SIGCHLD and check again. Note that this returns immediately if a
95 // SIGCHLD has arrived since the last wait.
96 sigchld_waiter.wait_deadline(deadline)?;
97 }
98 }