2016-12-18 22:36:21 -08:00
|
|
|
//! Unix handling of child processes
|
|
|
|
|
//!
|
|
|
|
|
//! Right now the only "fancy" thing about this is how we implement the
|
|
|
|
|
//! `Future` implementation on `Child` to get the exit status. Unix offers
|
|
|
|
|
//! no way to register a child with epoll, and the only real way to get a
|
|
|
|
|
//! notification when a process exits is the SIGCHLD signal.
|
|
|
|
|
//!
|
|
|
|
|
//! Signal handling in general is *super* hairy and complicated, and it's even
|
|
|
|
|
//! more complicated here with the fact that signals are coalesced, so we may
|
|
|
|
|
//! not get a SIGCHLD-per-child.
|
|
|
|
|
//!
|
|
|
|
|
//! Our best approximation here is to check *all spawned processes* for all
|
|
|
|
|
//! SIGCHLD signals received. To do that we create a `Signal`, implemented in
|
|
|
|
|
//! the `tokio-signal` crate, which is a stream over signals being received.
|
|
|
|
|
//!
|
|
|
|
|
//! Later when we poll the process's exit status we simply check to see if a
|
|
|
|
|
//! SIGCHLD has happened since we last checked, and while that returns "yes" we
|
|
|
|
|
//! keep trying.
|
|
|
|
|
//!
|
|
|
|
|
//! Note that this means that this isn't really scalable, but then again
|
|
|
|
|
//! processes in general aren't scalable (e.g. millions) so it shouldn't be that
|
|
|
|
|
//! bad in theory...
|
|
|
|
|
|
2016-09-07 00:13:11 -07:00
|
|
|
extern crate libc;
|
|
|
|
|
extern crate tokio_signal;
|
|
|
|
|
|
2019-05-25 15:19:36 -07:00
|
|
|
mod reap;
|
2016-09-07 00:13:11 -07:00
|
|
|
|
2016-12-18 22:36:21 -08:00
|
|
|
use futures::future::FlattenStream;
|
2019-05-25 15:19:36 -07:00
|
|
|
use futures::{Future, Poll};
|
2017-03-09 09:16:04 -08:00
|
|
|
use mio::unix::{EventedFd, UnixReady};
|
|
|
|
|
use mio::{PollOpt, Ready, Token};
|
|
|
|
|
use mio::event::Evented;
|
2016-12-18 22:36:21 -08:00
|
|
|
use mio;
|
2019-05-25 15:39:38 -07:00
|
|
|
use self::reap::{Kill, Reaper, Wait};
|
2016-09-07 00:13:11 -07:00
|
|
|
use self::tokio_signal::unix::Signal;
|
2017-06-17 16:40:21 -07:00
|
|
|
use std::fmt;
|
2019-05-25 15:19:36 -07:00
|
|
|
use std::io;
|
|
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
|
|
|
|
use std::process::{self, ExitStatus};
|
2019-05-29 20:30:29 -07:00
|
|
|
use super::SpawnedChild;
|
2017-03-09 09:16:04 -08:00
|
|
|
use tokio_io::IoFuture;
|
2018-05-13 14:50:42 -07:00
|
|
|
use tokio_reactor::{Handle, PollEvented};
|
2016-09-07 00:13:11 -07:00
|
|
|
|
2019-05-25 15:19:36 -07:00
|
|
|
impl Wait for process::Child {
|
|
|
|
|
fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
|
|
|
|
|
self.try_wait()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Kill for process::Child {
|
|
|
|
|
fn kill(&mut self) -> io::Result<()> {
|
|
|
|
|
self.kill()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-17 16:46:36 -07:00
|
|
|
#[must_use = "futures do nothing unless polled"]
|
2016-09-07 00:13:11 -07:00
|
|
|
pub struct Child {
|
2019-05-25 15:39:38 -07:00
|
|
|
inner: Reaper<process::Child, FlattenStream<IoFuture<Signal>>>,
|
2016-09-07 00:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-17 16:40:21 -07:00
|
|
|
impl fmt::Debug for Child {
|
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
fmt.debug_struct("Child")
|
|
|
|
|
.field("pid", &self.inner.id())
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 20:30:29 -07:00
|
|
|
pub(crate) fn spawn_child(cmd: &mut process::Command, handle: &Handle) -> io::Result<SpawnedChild> {
|
|
|
|
|
let mut child = cmd.spawn()?;
|
|
|
|
|
let stdin = stdio(child.stdin.take(), handle)?;
|
|
|
|
|
let stdout = stdio(child.stdout.take(), handle)?;
|
|
|
|
|
let stderr = stdio(child.stderr.take(), handle)?;
|
|
|
|
|
|
|
|
|
|
let signal = Signal::with_handle(libc::SIGCHLD, handle).flatten_stream();
|
|
|
|
|
Ok(SpawnedChild {
|
|
|
|
|
child: Child {
|
|
|
|
|
inner: Reaper::new(child, signal),
|
|
|
|
|
},
|
|
|
|
|
stdin,
|
|
|
|
|
stdout,
|
|
|
|
|
stderr,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 22:36:21 -08:00
|
|
|
impl Child {
|
2016-09-07 00:13:11 -07:00
|
|
|
|
|
|
|
|
pub fn id(&self) -> u32 {
|
2016-12-18 22:36:21 -08:00
|
|
|
self.inner.id()
|
2016-09-07 00:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn kill(&mut self) -> io::Result<()> {
|
2019-05-25 15:19:36 -07:00
|
|
|
self.inner.kill()
|
2016-09-07 00:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-18 22:36:21 -08:00
|
|
|
pub fn poll_exit(&mut self) -> Poll<ExitStatus, io::Error> {
|
2019-05-25 15:19:36 -07:00
|
|
|
self.inner.poll()
|
2016-09-07 00:13:11 -07:00
|
|
|
}
|
|
|
|
|
}
|
2016-12-12 00:34:07 -08:00
|
|
|
|
2017-06-17 16:40:21 -07:00
|
|
|
#[derive(Debug)]
|
2016-12-12 00:34:07 -08:00
|
|
|
pub struct Fd<T>(T);
|
|
|
|
|
|
|
|
|
|
impl<T: io::Read> io::Read for Fd<T> {
|
|
|
|
|
fn read(&mut self, bytes: &mut [u8]) -> io::Result<usize> {
|
|
|
|
|
self.0.read(bytes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: io::Write> io::Write for Fd<T> {
|
|
|
|
|
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
|
|
|
|
|
self.0.write(bytes)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
|
self.0.flush()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 23:47:17 +09:00
|
|
|
impl<T> AsRawFd for Fd<T> where T: AsRawFd {
|
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
|
self.0.as_raw_fd()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-12 00:34:07 -08:00
|
|
|
pub type ChildStdin = PollEvented<Fd<process::ChildStdin>>;
|
|
|
|
|
pub type ChildStdout = PollEvented<Fd<process::ChildStdout>>;
|
|
|
|
|
pub type ChildStderr = PollEvented<Fd<process::ChildStderr>>;
|
|
|
|
|
|
|
|
|
|
impl<T> Evented for Fd<T> where T: AsRawFd {
|
|
|
|
|
fn register(&self,
|
|
|
|
|
poll: &mio::Poll,
|
|
|
|
|
token: Token,
|
|
|
|
|
interest: Ready,
|
|
|
|
|
opts: PollOpt)
|
|
|
|
|
-> io::Result<()> {
|
2018-09-26 23:47:17 +09:00
|
|
|
EventedFd(&self.as_raw_fd()).register(poll,
|
|
|
|
|
token,
|
|
|
|
|
interest | UnixReady::hup(),
|
|
|
|
|
opts)
|
2016-12-12 00:34:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn reregister(&self,
|
|
|
|
|
poll: &mio::Poll,
|
|
|
|
|
token: Token,
|
|
|
|
|
interest: Ready,
|
|
|
|
|
opts: PollOpt)
|
|
|
|
|
-> io::Result<()> {
|
2018-09-26 23:47:17 +09:00
|
|
|
EventedFd(&self.as_raw_fd()).reregister(poll,
|
|
|
|
|
token,
|
|
|
|
|
interest | UnixReady::hup(),
|
|
|
|
|
opts)
|
2016-12-12 00:34:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn deregister(&self, poll: &mio::Poll) -> io::Result<()> {
|
2018-09-26 23:47:17 +09:00
|
|
|
EventedFd(&self.as_raw_fd()).deregister(poll)
|
2016-12-12 00:34:07 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stdio<T>(option: Option<T>, handle: &Handle)
|
|
|
|
|
-> io::Result<Option<PollEvented<Fd<T>>>>
|
|
|
|
|
where T: AsRawFd
|
|
|
|
|
{
|
|
|
|
|
let io = match option {
|
|
|
|
|
Some(io) => io,
|
|
|
|
|
None => return Ok(None),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Set the fd to nonblocking before we pass it to the event loop
|
|
|
|
|
unsafe {
|
|
|
|
|
let fd = io.as_raw_fd();
|
|
|
|
|
let r = libc::fcntl(fd, libc::F_GETFL);
|
|
|
|
|
if r == -1 {
|
|
|
|
|
return Err(io::Error::last_os_error())
|
|
|
|
|
}
|
|
|
|
|
let r = libc::fcntl(fd, libc::F_SETFL, r | libc::O_NONBLOCK);
|
|
|
|
|
if r == -1 {
|
|
|
|
|
return Err(io::Error::last_os_error())
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-13 14:50:42 -07:00
|
|
|
let io = try!(PollEvented::new_with_handle(Fd(io), handle));
|
2016-12-12 00:34:07 -08:00
|
|
|
Ok(Some(io))
|
|
|
|
|
}
|