• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Unix handling of child processes.
2 //!
3 //! Right now the only "fancy" thing about this is how we implement the
4 //! `Future` implementation on `Child` to get the exit status. Unix offers
5 //! no way to register a child with epoll, and the only real way to get a
6 //! notification when a process exits is the SIGCHLD signal.
7 //!
8 //! Signal handling in general is *super* hairy and complicated, and it's even
9 //! more complicated here with the fact that signals are coalesced, so we may
10 //! not get a SIGCHLD-per-child.
11 //!
12 //! Our best approximation here is to check *all spawned processes* for all
13 //! SIGCHLD signals received. To do that we create a `Signal`, implemented in
14 //! the `tokio-net` crate, which is a stream over signals being received.
15 //!
16 //! Later when we poll the process's exit status we simply check to see if a
17 //! SIGCHLD has happened since we last checked, and while that returns "yes" we
18 //! keep trying.
19 //!
20 //! Note that this means that this isn't really scalable, but then again
21 //! processes in general aren't scalable (e.g. millions) so it shouldn't be that
22 //! bad in theory...
23 
24 pub(crate) mod orphan;
25 use orphan::{OrphanQueue, OrphanQueueImpl, Wait};
26 
27 mod reap;
28 use reap::Reaper;
29 
30 use crate::io::{AsyncRead, AsyncWrite, PollEvented, ReadBuf};
31 use crate::process::kill::Kill;
32 use crate::process::SpawnedChild;
33 use crate::runtime::signal::Handle as SignalHandle;
34 use crate::signal::unix::{signal, Signal, SignalKind};
35 
36 use mio::event::Source;
37 use mio::unix::SourceFd;
38 use std::fmt;
39 use std::fs::File;
40 use std::future::Future;
41 use std::io;
42 use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
43 use std::pin::Pin;
44 use std::process::{Child as StdChild, ExitStatus, Stdio};
45 use std::task::Context;
46 use std::task::Poll;
47 
48 impl Wait for StdChild {
id(&self) -> u3249     fn id(&self) -> u32 {
50         self.id()
51     }
52 
try_wait(&mut self) -> io::Result<Option<ExitStatus>>53     fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
54         self.try_wait()
55     }
56 }
57 
58 impl Kill for StdChild {
kill(&mut self) -> io::Result<()>59     fn kill(&mut self) -> io::Result<()> {
60         self.kill()
61     }
62 }
63 
64 cfg_not_has_const_mutex_new! {
65     fn get_orphan_queue() -> &'static OrphanQueueImpl<StdChild> {
66         use crate::util::once_cell::OnceCell;
67 
68         static ORPHAN_QUEUE: OnceCell<OrphanQueueImpl<StdChild>> = OnceCell::new();
69 
70         ORPHAN_QUEUE.get(OrphanQueueImpl::new)
71     }
72 }
73 
74 cfg_has_const_mutex_new! {
75     fn get_orphan_queue() -> &'static OrphanQueueImpl<StdChild> {
76         static ORPHAN_QUEUE: OrphanQueueImpl<StdChild> = OrphanQueueImpl::new();
77 
78         &ORPHAN_QUEUE
79     }
80 }
81 
82 pub(crate) struct GlobalOrphanQueue;
83 
84 impl fmt::Debug for GlobalOrphanQueue {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result85     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
86         get_orphan_queue().fmt(fmt)
87     }
88 }
89 
90 impl GlobalOrphanQueue {
reap_orphans(handle: &SignalHandle)91     pub(crate) fn reap_orphans(handle: &SignalHandle) {
92         get_orphan_queue().reap_orphans(handle)
93     }
94 }
95 
96 impl OrphanQueue<StdChild> for GlobalOrphanQueue {
push_orphan(&self, orphan: StdChild)97     fn push_orphan(&self, orphan: StdChild) {
98         get_orphan_queue().push_orphan(orphan)
99     }
100 }
101 
102 #[must_use = "futures do nothing unless polled"]
103 pub(crate) struct Child {
104     inner: Reaper<StdChild, GlobalOrphanQueue, Signal>,
105 }
106 
107 impl fmt::Debug for Child {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result108     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
109         fmt.debug_struct("Child")
110             .field("pid", &self.inner.id())
111             .finish()
112     }
113 }
114 
spawn_child(cmd: &mut std::process::Command) -> io::Result<SpawnedChild>115 pub(crate) fn spawn_child(cmd: &mut std::process::Command) -> io::Result<SpawnedChild> {
116     let mut child = cmd.spawn()?;
117     let stdin = child.stdin.take().map(stdio).transpose()?;
118     let stdout = child.stdout.take().map(stdio).transpose()?;
119     let stderr = child.stderr.take().map(stdio).transpose()?;
120 
121     let signal = signal(SignalKind::child())?;
122 
123     Ok(SpawnedChild {
124         child: Child {
125             inner: Reaper::new(child, GlobalOrphanQueue, signal),
126         },
127         stdin,
128         stdout,
129         stderr,
130     })
131 }
132 
133 impl Child {
id(&self) -> u32134     pub(crate) fn id(&self) -> u32 {
135         self.inner.id()
136     }
137 
try_wait(&mut self) -> io::Result<Option<ExitStatus>>138     pub(crate) fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
139         self.inner.inner_mut().try_wait()
140     }
141 }
142 
143 impl Kill for Child {
kill(&mut self) -> io::Result<()>144     fn kill(&mut self) -> io::Result<()> {
145         self.inner.kill()
146     }
147 }
148 
149 impl Future for Child {
150     type Output = io::Result<ExitStatus>;
151 
poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>152     fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
153         Pin::new(&mut self.inner).poll(cx)
154     }
155 }
156 
157 #[derive(Debug)]
158 pub(crate) struct Pipe {
159     // Actually a pipe is not a File. However, we are reusing `File` to get
160     // close on drop. This is a similar trick as `mio`.
161     fd: File,
162 }
163 
164 impl<T: IntoRawFd> From<T> for Pipe {
from(fd: T) -> Self165     fn from(fd: T) -> Self {
166         let fd = unsafe { File::from_raw_fd(fd.into_raw_fd()) };
167         Self { fd }
168     }
169 }
170 
171 impl<'a> io::Read for &'a Pipe {
read(&mut self, bytes: &mut [u8]) -> io::Result<usize>172     fn read(&mut self, bytes: &mut [u8]) -> io::Result<usize> {
173         (&self.fd).read(bytes)
174     }
175 }
176 
177 impl<'a> io::Write for &'a Pipe {
write(&mut self, bytes: &[u8]) -> io::Result<usize>178     fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
179         (&self.fd).write(bytes)
180     }
181 
flush(&mut self) -> io::Result<()>182     fn flush(&mut self) -> io::Result<()> {
183         (&self.fd).flush()
184     }
185 
write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize>186     fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
187         (&self.fd).write_vectored(bufs)
188     }
189 }
190 
191 impl AsRawFd for Pipe {
as_raw_fd(&self) -> RawFd192     fn as_raw_fd(&self) -> RawFd {
193         self.fd.as_raw_fd()
194     }
195 }
196 
convert_to_stdio(io: ChildStdio) -> io::Result<Stdio>197 pub(crate) fn convert_to_stdio(io: ChildStdio) -> io::Result<Stdio> {
198     let mut fd = io.inner.into_inner()?.fd;
199 
200     // Ensure that the fd to be inherited is set to *blocking* mode, as this
201     // is the default that virtually all programs expect to have. Those
202     // programs that know how to work with nonblocking stdio will know how to
203     // change it to nonblocking mode.
204     set_nonblocking(&mut fd, false)?;
205 
206     Ok(Stdio::from(fd))
207 }
208 
209 impl Source for Pipe {
register( &mut self, registry: &mio::Registry, token: mio::Token, interest: mio::Interest, ) -> io::Result<()>210     fn register(
211         &mut self,
212         registry: &mio::Registry,
213         token: mio::Token,
214         interest: mio::Interest,
215     ) -> io::Result<()> {
216         SourceFd(&self.as_raw_fd()).register(registry, token, interest)
217     }
218 
reregister( &mut self, registry: &mio::Registry, token: mio::Token, interest: mio::Interest, ) -> io::Result<()>219     fn reregister(
220         &mut self,
221         registry: &mio::Registry,
222         token: mio::Token,
223         interest: mio::Interest,
224     ) -> io::Result<()> {
225         SourceFd(&self.as_raw_fd()).reregister(registry, token, interest)
226     }
227 
deregister(&mut self, registry: &mio::Registry) -> io::Result<()>228     fn deregister(&mut self, registry: &mio::Registry) -> io::Result<()> {
229         SourceFd(&self.as_raw_fd()).deregister(registry)
230     }
231 }
232 
233 pub(crate) struct ChildStdio {
234     inner: PollEvented<Pipe>,
235 }
236 
237 impl fmt::Debug for ChildStdio {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result238     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
239         self.inner.fmt(fmt)
240     }
241 }
242 
243 impl AsRawFd for ChildStdio {
as_raw_fd(&self) -> RawFd244     fn as_raw_fd(&self) -> RawFd {
245         self.inner.as_raw_fd()
246     }
247 }
248 
249 impl AsyncWrite for ChildStdio {
poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<io::Result<usize>>250     fn poll_write(
251         self: Pin<&mut Self>,
252         cx: &mut Context<'_>,
253         buf: &[u8],
254     ) -> Poll<io::Result<usize>> {
255         self.inner.poll_write(cx, buf)
256     }
257 
poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>>258     fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
259         Poll::Ready(Ok(()))
260     }
261 
poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>>262     fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
263         Poll::Ready(Ok(()))
264     }
265 
poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[io::IoSlice<'_>], ) -> Poll<Result<usize, io::Error>>266     fn poll_write_vectored(
267         self: Pin<&mut Self>,
268         cx: &mut Context<'_>,
269         bufs: &[io::IoSlice<'_>],
270     ) -> Poll<Result<usize, io::Error>> {
271         self.inner.poll_write_vectored(cx, bufs)
272     }
273 
is_write_vectored(&self) -> bool274     fn is_write_vectored(&self) -> bool {
275         true
276     }
277 }
278 
279 impl AsyncRead for ChildStdio {
poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll<io::Result<()>>280     fn poll_read(
281         self: Pin<&mut Self>,
282         cx: &mut Context<'_>,
283         buf: &mut ReadBuf<'_>,
284     ) -> Poll<io::Result<()>> {
285         // Safety: pipes support reading into uninitialized memory
286         unsafe { self.inner.poll_read(cx, buf) }
287     }
288 }
289 
set_nonblocking<T: AsRawFd>(fd: &mut T, nonblocking: bool) -> io::Result<()>290 fn set_nonblocking<T: AsRawFd>(fd: &mut T, nonblocking: bool) -> io::Result<()> {
291     unsafe {
292         let fd = fd.as_raw_fd();
293         let previous = libc::fcntl(fd, libc::F_GETFL);
294         if previous == -1 {
295             return Err(io::Error::last_os_error());
296         }
297 
298         let new = if nonblocking {
299             previous | libc::O_NONBLOCK
300         } else {
301             previous & !libc::O_NONBLOCK
302         };
303 
304         let r = libc::fcntl(fd, libc::F_SETFL, new);
305         if r == -1 {
306             return Err(io::Error::last_os_error());
307         }
308     }
309 
310     Ok(())
311 }
312 
stdio<T>(io: T) -> io::Result<ChildStdio> where T: IntoRawFd,313 pub(super) fn stdio<T>(io: T) -> io::Result<ChildStdio>
314 where
315     T: IntoRawFd,
316 {
317     // Set the fd to nonblocking before we pass it to the event loop
318     let mut pipe = Pipe::from(io);
319     set_nonblocking(&mut pipe, true)?;
320 
321     PollEvented::new(pipe).map(|inner| ChildStdio { inner })
322 }
323