#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
#![doc(
html_favicon_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
)]
use std::ffi::OsStr;
use std::fmt;
use std::path::Path;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use std::thread;
#[cfg(unix)]
use async_io::Async;
#[cfg(unix)]
use std::convert::{TryFrom, TryInto};
#[cfg(unix)]
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd};
#[cfg(windows)]
use blocking::Unblock;
use async_lock::OnceCell;
use event_listener::{Event, EventListener};
use futures_lite::{future, io, prelude::*};
#[doc(no_inline)]
pub use std::process::{ExitStatus, Output, Stdio};
#[cfg(unix)]
pub mod unix;
#[cfg(windows)]
pub mod windows;
mod sealed {
pub trait Sealed {}
}
struct Reaper {
sigchld: Event,
zombies: Mutex<Vec<std::process::Child>>,
pipe: Pipe,
}
impl Reaper {
fn get() -> &'static Self {
static REAPER: OnceCell<Reaper> = OnceCell::new();
REAPER.get_or_init_blocking(|| {
thread::Builder::new()
.name("async-process".to_string())
.spawn(|| REAPER.wait_blocking().reap())
.expect("cannot spawn async-process thread");
Reaper {
sigchld: Event::new(),
zombies: Mutex::new(Vec::new()),
pipe: Pipe::new().expect("cannot create SIGCHLD pipe"),
}
})
}
fn reap(&'static self) -> ! {
loop {
self.pipe.wait();
self.sigchld.notify(std::usize::MAX);
let mut zombies = self.zombies.lock().unwrap();
let mut i = 0;
while i < zombies.len() {
if let Ok(None) = zombies[i].try_wait() {
i += 1;
} else {
zombies.swap_remove(i);
}
}
}
}
fn register(&'static self, child: &std::process::Child) -> io::Result<()> {
self.pipe.register(child)
}
}
cfg_if::cfg_if! {
if #[cfg(windows)] {
use std::ffi::c_void;
use std::os::windows::io::AsRawHandle;
use std::sync::mpsc;
use windows_sys::Win32::{
Foundation::{BOOLEAN, HANDLE},
System::Threading::{
RegisterWaitForSingleObject, INFINITE, WT_EXECUTEINWAITTHREAD, WT_EXECUTEONLYONCE,
},
};
struct Pipe {
sender: mpsc::SyncSender<()>,
receiver: Mutex<mpsc::Receiver<()>>,
}
impl Pipe {
fn new() -> io::Result<Pipe> {
let (sender, receiver) = mpsc::sync_channel(1);
Ok(Pipe {
sender,
receiver: Mutex::new(receiver),
})
}
fn wait(&self) {
self.receiver.lock().unwrap().recv().ok();
}
fn register(&self, child: &std::process::Child) -> io::Result<()> {
unsafe extern "system" fn callback(_: *mut c_void, _: BOOLEAN) {
Reaper::get().pipe.sender.try_send(()).ok();
}
let mut wait_object = 0;
let ret = unsafe {
RegisterWaitForSingleObject(
&mut wait_object,
child.as_raw_handle() as HANDLE,
Some(callback),
std::ptr::null_mut(),
INFINITE,
WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE,
)
};
if ret == 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
}
}
fn wrap<T>(io: T) -> io::Result<Unblock<T>> {
Ok(Unblock::new(io))
}
} else if #[cfg(unix)] {
use async_signal::{Signal, Signals};
struct Pipe {
signals: Signals,
}
impl Pipe {
fn new() -> io::Result<Pipe> {
Ok(Pipe {
signals: Signals::new(Some(Signal::Child))?,
})
}
fn wait(&self) {
async_io::block_on((&self.signals).next());
}
fn register(&self, _child: &std::process::Child) -> io::Result<()> {
Ok(())
}
}
fn wrap<T: std::os::unix::io::AsRawFd>(io: T) -> io::Result<Async<T>> {
Async::new(io)
}
}
}
struct ChildGuard {
inner: Option<std::process::Child>,
reap_on_drop: bool,
kill_on_drop: bool,
}
impl ChildGuard {
fn get_mut(&mut self) -> &mut std::process::Child {
self.inner.as_mut().unwrap()
}
}
impl Drop for ChildGuard {
fn drop(&mut self) {
if self.kill_on_drop {
self.get_mut().kill().ok();
}
if self.reap_on_drop {
let mut zombies = Reaper::get().zombies.lock().unwrap();
if let Ok(None) = self.get_mut().try_wait() {
zombies.push(self.inner.take().unwrap());
}
}
}
}
pub struct Child {
pub stdin: Option<ChildStdin>,
pub stdout: Option<ChildStdout>,
pub stderr: Option<ChildStderr>,
child: Arc<Mutex<ChildGuard>>,
}
impl Child {
fn new(cmd: &mut Command) -> io::Result<Child> {
let reaper = Reaper::get();
let mut child = cmd.inner.spawn()?;
let stdin = child.stdin.take().map(wrap).transpose()?.map(ChildStdin);
let stdout = child.stdout.take().map(wrap).transpose()?.map(ChildStdout);
let stderr = child.stderr.take().map(wrap).transpose()?.map(ChildStderr);
reaper.register(&child)?;
Ok(Child {
stdin,
stdout,
stderr,
child: Arc::new(Mutex::new(ChildGuard {
inner: Some(child),
reap_on_drop: cmd.reap_on_drop,
kill_on_drop: cmd.kill_on_drop,
})),
})
}
pub fn id(&self) -> u32 {
self.child.lock().unwrap().get_mut().id()
}
pub fn kill(&mut self) -> io::Result<()> {
self.child.lock().unwrap().get_mut().kill()
}
pub fn try_status(&mut self) -> io::Result<Option<ExitStatus>> {
self.child.lock().unwrap().get_mut().try_wait()
}
pub fn status(&mut self) -> impl Future<Output = io::Result<ExitStatus>> {
self.stdin.take();
let child = self.child.clone();
async move {
let listener = EventListener::new(&Reaper::get().sigchld);
let mut listening = false;
futures_lite::pin!(listener);
loop {
if let Some(status) = child.lock().unwrap().get_mut().try_wait()? {
return Ok(status);
}
if listening {
listener.as_mut().await;
listening = false;
} else {
listener.as_mut().listen();
listening = true;
}
}
}
}
pub fn output(mut self) -> impl Future<Output = io::Result<Output>> {
let status = self.status();
let stdout = self.stdout.take();
let stdout = async move {
let mut v = Vec::new();
if let Some(mut s) = stdout {
s.read_to_end(&mut v).await?;
}
io::Result::Ok(v)
};
let stderr = self.stderr.take();
let stderr = async move {
let mut v = Vec::new();
if let Some(mut s) = stderr {
s.read_to_end(&mut v).await?;
}
io::Result::Ok(v)
};
async move {
let (stdout, stderr) = future::try_zip(stdout, stderr).await?;
let status = status.await?;
Ok(Output {
status,
stdout,
stderr,
})
}
}
}
impl fmt::Debug for Child {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Child")
.field("stdin", &self.stdin)
.field("stdout", &self.stdout)
.field("stderr", &self.stderr)
.finish()
}
}
#[derive(Debug)]
pub struct ChildStdin(
#[cfg(windows)] Unblock<std::process::ChildStdin>,
#[cfg(unix)] Async<std::process::ChildStdin>,
);
impl ChildStdin {
pub async fn into_stdio(self) -> io::Result<std::process::Stdio> {
cfg_if::cfg_if! {
if #[cfg(windows)] {
Ok(self.0.into_inner().await.into())
} else if #[cfg(unix)] {
let child_stdin = self.0.into_inner()?;
blocking_fd(rustix::fd::AsFd::as_fd(&child_stdin))?;
Ok(child_stdin.into())
}
}
}
}
impl io::AsyncWrite for ChildStdin {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.0).poll_write(cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.0).poll_flush(cx)
}
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.0).poll_close(cx)
}
}
#[cfg(unix)]
impl AsRawFd for ChildStdin {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
#[cfg(unix)]
impl AsFd for ChildStdin {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}
#[cfg(unix)]
impl TryFrom<ChildStdin> for OwnedFd {
type Error = io::Error;
fn try_from(value: ChildStdin) -> Result<Self, Self::Error> {
value.0.try_into()
}
}
#[derive(Debug)]
pub struct ChildStdout(
#[cfg(windows)] Unblock<std::process::ChildStdout>,
#[cfg(unix)] Async<std::process::ChildStdout>,
);
impl ChildStdout {
pub async fn into_stdio(self) -> io::Result<std::process::Stdio> {
cfg_if::cfg_if! {
if #[cfg(windows)] {
Ok(self.0.into_inner().await.into())
} else if #[cfg(unix)] {
let child_stdout = self.0.into_inner()?;
blocking_fd(rustix::fd::AsFd::as_fd(&child_stdout))?;
Ok(child_stdout.into())
}
}
}
}
impl io::AsyncRead for ChildStdout {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.0).poll_read(cx, buf)
}
}
#[cfg(unix)]
impl AsRawFd for ChildStdout {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
#[cfg(unix)]
impl AsFd for ChildStdout {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}
#[cfg(unix)]
impl TryFrom<ChildStdout> for OwnedFd {
type Error = io::Error;
fn try_from(value: ChildStdout) -> Result<Self, Self::Error> {
value.0.try_into()
}
}
#[derive(Debug)]
pub struct ChildStderr(
#[cfg(windows)] Unblock<std::process::ChildStderr>,
#[cfg(unix)] Async<std::process::ChildStderr>,
);
impl ChildStderr {
pub async fn into_stdio(self) -> io::Result<std::process::Stdio> {
cfg_if::cfg_if! {
if #[cfg(windows)] {
Ok(self.0.into_inner().await.into())
} else if #[cfg(unix)] {
let child_stderr = self.0.into_inner()?;
blocking_fd(rustix::fd::AsFd::as_fd(&child_stderr))?;
Ok(child_stderr.into())
}
}
}
}
impl io::AsyncRead for ChildStderr {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.0).poll_read(cx, buf)
}
}
#[cfg(unix)]
impl AsRawFd for ChildStderr {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
#[cfg(unix)]
impl AsFd for ChildStderr {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}
#[cfg(unix)]
impl TryFrom<ChildStderr> for OwnedFd {
type Error = io::Error;
fn try_from(value: ChildStderr) -> Result<Self, Self::Error> {
value.0.try_into()
}
}
pub struct Command {
inner: std::process::Command,
stdin: bool,
stdout: bool,
stderr: bool,
reap_on_drop: bool,
kill_on_drop: bool,
}
impl Command {
pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
Self::from(std::process::Command::new(program))
}
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
self.inner.arg(arg);
self
}
pub fn args<I, S>(&mut self, args: I) -> &mut Command
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.inner.args(args);
self
}
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.inner.env(key, val);
self
}
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.inner.envs(vars);
self
}
pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
self.inner.env_remove(key);
self
}
pub fn env_clear(&mut self) -> &mut Command {
self.inner.env_clear();
self
}
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command {
self.inner.current_dir(dir);
self
}
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
self.stdin = true;
self.inner.stdin(cfg);
self
}
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
self.stdout = true;
self.inner.stdout(cfg);
self
}
pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
self.stderr = true;
self.inner.stderr(cfg);
self
}
pub fn reap_on_drop(&mut self, reap_on_drop: bool) -> &mut Command {
self.reap_on_drop = reap_on_drop;
self
}
pub fn kill_on_drop(&mut self, kill_on_drop: bool) -> &mut Command {
self.kill_on_drop = kill_on_drop;
self
}
pub fn spawn(&mut self) -> io::Result<Child> {
if !self.stdin {
self.inner.stdin(Stdio::inherit());
}
if !self.stdout {
self.inner.stdout(Stdio::inherit());
}
if !self.stderr {
self.inner.stderr(Stdio::inherit());
}
Child::new(self)
}
pub fn status(&mut self) -> impl Future<Output = io::Result<ExitStatus>> {
let child = self.spawn();
async { child?.status().await }
}
pub fn output(&mut self) -> impl Future<Output = io::Result<Output>> {
if !self.stdin {
self.inner.stdin(Stdio::null());
}
if !self.stdout {
self.inner.stdout(Stdio::piped());
}
if !self.stderr {
self.inner.stderr(Stdio::piped());
}
let child = Child::new(self);
async { child?.output().await }
}
}
impl From<std::process::Command> for Command {
fn from(inner: std::process::Command) -> Self {
Self {
inner,
stdin: false,
stdout: false,
stderr: false,
reap_on_drop: true,
kill_on_drop: false,
}
}
}
impl fmt::Debug for Command {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
f.debug_struct("Command")
.field("inner", &self.inner)
.field("stdin", &self.stdin)
.field("stdout", &self.stdout)
.field("stderr", &self.stderr)
.field("reap_on_drop", &self.reap_on_drop)
.field("kill_on_drop", &self.kill_on_drop)
.finish()
} else {
fmt::Debug::fmt(&self.inner, f)
}
}
}
#[cfg(unix)]
fn blocking_fd(fd: rustix::fd::BorrowedFd<'_>) -> io::Result<()> {
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
rustix::io::ioctl_fionbio(fd, false)?;
} else {
let previous = rustix::fs::fcntl_getfl(fd)?;
let new = previous & !rustix::fs::OFlags::NONBLOCK;
if new != previous {
rustix::fs::fcntl_setfl(fd, new)?;
}
}
}
Ok(())
}
#[cfg(unix)]
mod test {
#[test]
fn test_into_inner() {
futures_lite::future::block_on(async {
use crate::Command;
use std::io::Result;
use std::process::Stdio;
use std::str::from_utf8;
use futures_lite::AsyncReadExt;
let mut ls_child = Command::new("cat")
.arg("Cargo.toml")
.stdout(Stdio::piped())
.spawn()?;
let stdio: Stdio = ls_child.stdout.take().unwrap().into_stdio().await?;
let mut echo_child = Command::new("grep")
.arg("async")
.stdin(stdio)
.stdout(Stdio::piped())
.spawn()?;
let mut buf = vec![];
let mut stdout = echo_child.stdout.take().unwrap();
stdout.read_to_end(&mut buf).await?;
dbg!(from_utf8(&buf).unwrap_or(""));
Result::Ok(())
})
.unwrap();
}
}