async_process/lib.rs
1//! Async interface for working with processes.
2//!
3//! This crate is an async version of [`std::process`].
4//!
5//! # Implementation
6//!
7//! A background thread named "async-process" is lazily created on first use, which waits for
8//! spawned child processes to exit and then calls the `wait()` syscall to clean up the "zombie"
9//! processes. This is unlike the `process` API in the standard library, where dropping a running
10//! `Child` leaks its resources.
11//!
12//! This crate uses [`async-io`] for async I/O on Unix-like systems.
13//!
14//! [`async-io`]: https://docs.rs/async-io
15//!
16//! # Examples
17//!
18//! Spawn a process and collect its output:
19//!
20//! ```no_run
21//! # futures_lite::future::block_on(async {
22//! use async_process::Command;
23//!
24//! let out = Command::new("echo").arg("hello").arg("world").output().await?;
25//! assert_eq!(out.stdout, b"hello world\n");
26//! # std::io::Result::Ok(()) });
27//! ```
28//!
29//! Read the output line-by-line as it gets produced:
30//!
31//! ```no_run
32//! # futures_lite::future::block_on(async {
33//! use async_process::{Command, Stdio};
34//! use futures_lite::{io::BufReader, prelude::*};
35//!
36//! let mut child = Command::new("find")
37//! .arg(".")
38//! .stdout(Stdio::piped())
39//! .spawn()?;
40//!
41//! let mut lines = BufReader::new(child.stdout.take().unwrap()).lines();
42//!
43//! while let Some(line) = lines.next().await {
44//! println!("{}", line?);
45//! }
46//! # std::io::Result::Ok(()) });
47//! ```
48
49#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
50#![doc(
51 html_favicon_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
52)]
53#![doc(
54 html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
55)]
56
57use std::convert::Infallible;
58use std::ffi::OsStr;
59use std::fmt;
60use std::path::Path;
61use std::pin::Pin;
62use std::sync::atomic::{AtomicUsize, Ordering};
63use std::sync::{Arc, Mutex, OnceLock};
64use std::task::{Context, Poll};
65use std::thread;
66
67#[cfg(unix)]
68use async_io::Async;
69#[cfg(unix)]
70use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd};
71
72use futures_lite::{future, io, prelude::*};
73
74#[doc(no_inline)]
75pub use std::process::{ExitStatus, Output, Stdio};
76
77#[cfg(unix)]
78pub mod unix;
79
80mod reaper;
81
82mod sealed {
83 pub trait Sealed {}
84}
85
86#[cfg(test)]
87static DRIVER_THREAD_SPAWNED: std::sync::atomic::AtomicBool =
88 std::sync::atomic::AtomicBool::new(false);
89
90/// The zombie process reaper.
91///
92/// This structure reaps zombie processes and emits the `SIGCHLD` signal.
93struct Reaper {
94 /// Underlying system reaper.
95 sys: reaper::Reaper,
96
97 /// The number of tasks polling the SIGCHLD event.
98 ///
99 /// If this is zero, the `async-process` thread must be spawned.
100 drivers: AtomicUsize,
101
102 /// Number of live `Child` instances currently running.
103 ///
104 /// This is used to prevent the reaper thread from being spawned right as the program closes,
105 /// when the reaper thread isn't needed. This represents the number of active processes.
106 child_count: AtomicUsize,
107}
108
109impl Reaper {
110 /// Get the singleton instance of the reaper.
111 fn get() -> &'static Self {
112 static REAPER: OnceLock<Reaper> = OnceLock::new();
113
114 REAPER.get_or_init(|| Reaper {
115 sys: reaper::Reaper::new(),
116 drivers: AtomicUsize::new(0),
117 child_count: AtomicUsize::new(0),
118 })
119 }
120
121 /// Ensure that the reaper is driven.
122 ///
123 /// If there are no active `driver()` callers, this will spawn the `async-process` thread.
124 #[inline]
125 fn ensure_driven(&'static self) {
126 if self
127 .drivers
128 .compare_exchange(0, 1, Ordering::SeqCst, Ordering::Acquire)
129 .is_ok()
130 {
131 self.start_driver_thread();
132 }
133 }
134
135 /// Start the `async-process` thread.
136 #[cold]
137 fn start_driver_thread(&'static self) {
138 #[cfg(test)]
139 DRIVER_THREAD_SPAWNED
140 .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
141 .unwrap_or_else(|_| unreachable!("Driver thread already spawned"));
142
143 thread::Builder::new()
144 .name("async-process".to_string())
145 .spawn(move || {
146 let driver = async move {
147 // No need to bump self.drivers, it was already bumped in ensure_driven.
148 let guard = self.sys.lock().await;
149 self.sys.reap(guard).await
150 };
151
152 #[cfg(unix)]
153 async_io::block_on(driver);
154
155 #[cfg(not(unix))]
156 future::block_on(driver);
157 })
158 .expect("cannot spawn async-process thread");
159 }
160
161 /// Register a process with this reaper.
162 fn register(&'static self, child: std::process::Child) -> io::Result<reaper::ChildGuard> {
163 self.ensure_driven();
164 self.sys.register(child)
165 }
166}
167
168cfg_if::cfg_if! {
169 if #[cfg(unix)] {
170 /// Wrap a file descriptor into a non-blocking I/O type.
171 fn wrap<T: std::os::unix::io::AsFd>(io: T) -> io::Result<Async<T>> {
172 Async::new(io)
173 }
174 }
175}
176
177/// A guard that can kill child processes, or push them into the zombie list.
178struct ChildGuard {
179 inner: reaper::ChildGuard,
180 reap_on_drop: bool,
181 kill_on_drop: bool,
182 reaper: &'static Reaper,
183}
184
185impl ChildGuard {
186 fn get_mut(&mut self) -> &mut std::process::Child {
187 self.inner.get_mut()
188 }
189}
190
191// When the last reference to the child process is dropped, push it into the zombie list.
192impl Drop for ChildGuard {
193 fn drop(&mut self) {
194 if self.kill_on_drop {
195 self.get_mut().kill().ok();
196 }
197 if self.reap_on_drop {
198 self.inner.reap(&self.reaper.sys);
199 }
200
201 // Decrement number of children.
202 self.reaper.child_count.fetch_sub(1, Ordering::Acquire);
203 }
204}
205
206/// A spawned child process.
207///
208/// The process can be in running or exited state. Use [`status()`][`Child::status()`] or
209/// [`output()`][`Child::output()`] to wait for it to exit.
210///
211/// If the [`Child`] is dropped, the process keeps running in the background.
212///
213/// # Examples
214///
215/// Spawn a process and wait for it to complete:
216///
217/// ```no_run
218/// # futures_lite::future::block_on(async {
219/// use async_process::Command;
220///
221/// Command::new("cp").arg("a.txt").arg("b.txt").status().await?;
222/// # std::io::Result::Ok(()) });
223/// ```
224pub struct Child {
225 /// The handle for writing to the child's standard input (stdin), if it has been captured.
226 pub stdin: Option<ChildStdin>,
227
228 /// The handle for reading from the child's standard output (stdout), if it has been captured.
229 pub stdout: Option<ChildStdout>,
230
231 /// The handle for reading from the child's standard error (stderr), if it has been captured.
232 pub stderr: Option<ChildStderr>,
233
234 /// The inner child process handle.
235 child: Arc<Mutex<ChildGuard>>,
236}
237
238impl Child {
239 /// Wraps the inner child process handle and registers it in the global process list.
240 ///
241 /// The "async-process" thread waits for processes in the global list and cleans up the
242 /// resources when they exit.
243 fn new(cmd: &mut Command) -> io::Result<Child> {
244 // Make sure the reaper exists before we spawn the child process.
245 let reaper = Reaper::get();
246 let mut child = cmd.inner.spawn()?;
247
248 // Convert sync I/O types into async I/O types.
249 let stdin = child.stdin.take().map(wrap).transpose()?.map(ChildStdin);
250 let stdout = child.stdout.take().map(wrap).transpose()?.map(ChildStdout);
251 let stderr = child.stderr.take().map(wrap).transpose()?.map(ChildStderr);
252
253 // Bump the child count.
254 reaper.child_count.fetch_add(1, Ordering::Relaxed);
255
256 // Register the child process in the global list.
257 let inner = reaper.register(child)?;
258
259 Ok(Child {
260 stdin,
261 stdout,
262 stderr,
263 child: Arc::new(Mutex::new(ChildGuard {
264 inner,
265 reap_on_drop: cmd.reap_on_drop,
266 kill_on_drop: cmd.kill_on_drop,
267 reaper,
268 })),
269 })
270 }
271
272 /// Returns the OS-assigned process identifier associated with this child.
273 ///
274 /// # Examples
275 ///
276 /// ```no_run
277 /// # futures_lite::future::block_on(async {
278 /// use async_process::Command;
279 ///
280 /// let mut child = Command::new("ls").spawn()?;
281 /// println!("id: {}", child.id());
282 /// # std::io::Result::Ok(()) });
283 /// ```
284 pub fn id(&self) -> u32 {
285 self.child.lock().unwrap().get_mut().id()
286 }
287
288 /// Forces the child process to exit.
289 ///
290 /// If the child has already exited, an [`InvalidInput`] error is returned.
291 ///
292 /// This is equivalent to sending a SIGKILL on Unix platforms.
293 ///
294 /// [`InvalidInput`]: `std::io::ErrorKind::InvalidInput`
295 ///
296 /// # Examples
297 ///
298 /// ```no_run
299 /// # futures_lite::future::block_on(async {
300 /// use async_process::Command;
301 ///
302 /// let mut child = Command::new("yes").spawn()?;
303 /// child.kill()?;
304 /// println!("exit status: {}", child.status().await?);
305 /// # std::io::Result::Ok(()) });
306 /// ```
307 pub fn kill(&mut self) -> io::Result<()> {
308 self.child.lock().unwrap().get_mut().kill()
309 }
310
311 /// Returns the exit status if the process has exited.
312 ///
313 /// Unlike [`status()`][`Child::status()`], this method will not drop the stdin handle.
314 ///
315 /// # Examples
316 ///
317 /// ```no_run
318 /// # futures_lite::future::block_on(async {
319 /// use async_process::Command;
320 ///
321 /// let mut child = Command::new("ls").spawn()?;
322 ///
323 /// match child.try_status()? {
324 /// None => println!("still running"),
325 /// Some(status) => println!("exited with: {}", status),
326 /// }
327 /// # std::io::Result::Ok(()) });
328 /// ```
329 pub fn try_status(&mut self) -> io::Result<Option<ExitStatus>> {
330 self.child.lock().unwrap().get_mut().try_wait()
331 }
332
333 /// Drops the stdin handle and waits for the process to exit.
334 ///
335 /// Closing the stdin of the process helps avoid deadlocks. It ensures that the process does
336 /// not block waiting for input from the parent process while the parent waits for the child to
337 /// exit.
338 ///
339 /// # Examples
340 ///
341 /// ```no_run
342 /// # futures_lite::future::block_on(async {
343 /// use async_process::{Command, Stdio};
344 ///
345 /// let mut child = Command::new("cp")
346 /// .arg("a.txt")
347 /// .arg("b.txt")
348 /// .spawn()?;
349 ///
350 /// println!("exit status: {}", child.status().await?);
351 /// # std::io::Result::Ok(()) });
352 /// ```
353 pub fn status(&mut self) -> impl Future<Output = io::Result<ExitStatus>> {
354 self.stdin.take();
355 let child = self.child.clone();
356
357 async move { Reaper::get().sys.status(&child).await }
358 }
359
360 /// Drops the stdin handle and collects the output of the process.
361 ///
362 /// Closing the stdin of the process helps avoid deadlocks. It ensures that the process does
363 /// not block waiting for input from the parent process while the parent waits for the child to
364 /// exit.
365 ///
366 /// In order to capture the output of the process, [`Command::stdout()`] and
367 /// [`Command::stderr()`] must be configured with [`Stdio::piped()`].
368 ///
369 /// # Examples
370 ///
371 /// ```no_run
372 /// # futures_lite::future::block_on(async {
373 /// use async_process::{Command, Stdio};
374 ///
375 /// let child = Command::new("ls")
376 /// .stdout(Stdio::piped())
377 /// .stderr(Stdio::piped())
378 /// .spawn()?;
379 ///
380 /// let out = child.output().await?;
381 /// # std::io::Result::Ok(()) });
382 /// ```
383 pub fn output(mut self) -> impl Future<Output = io::Result<Output>> {
384 // A future that waits for the exit status.
385 let status = self.status();
386
387 // A future that collects stdout.
388 let stdout = self.stdout.take();
389 let stdout = async move {
390 let mut v = Vec::new();
391 if let Some(mut s) = stdout {
392 s.read_to_end(&mut v).await?;
393 }
394 io::Result::Ok(v)
395 };
396
397 // A future that collects stderr.
398 let stderr = self.stderr.take();
399 let stderr = async move {
400 let mut v = Vec::new();
401 if let Some(mut s) = stderr {
402 s.read_to_end(&mut v).await?;
403 }
404 io::Result::Ok(v)
405 };
406
407 async move {
408 let (stdout, stderr) = future::try_zip(stdout, stderr).await?;
409 let status = status.await?;
410 Ok(Output {
411 status,
412 stdout,
413 stderr,
414 })
415 }
416 }
417}
418
419impl fmt::Debug for Child {
420 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
421 f.debug_struct("Child")
422 .field("stdin", &self.stdin)
423 .field("stdout", &self.stdout)
424 .field("stderr", &self.stderr)
425 .finish()
426 }
427}
428
429/// A handle to a child process's standard input (stdin).
430///
431/// When a [`ChildStdin`] is dropped, the underlying handle gets closed. If the child process was
432/// previously blocked on input, it becomes unblocked after dropping.
433#[derive(Debug)]
434pub struct ChildStdin(#[cfg(unix)] Async<std::process::ChildStdin>);
435
436impl ChildStdin {
437 /// Convert async_process::ChildStdin into std::process::Stdio.
438 ///
439 /// You can use it to associate to the next process.
440 ///
441 /// # Examples
442 ///
443 /// ```no_run
444 /// # futures_lite::future::block_on(async {
445 /// use async_process::Command;
446 /// use std::process::Stdio;
447 ///
448 /// let mut ls_child = Command::new("ls").stdin(Stdio::piped()).spawn()?;
449 /// let stdio:Stdio = ls_child.stdin.take().unwrap().into_stdio().await?;
450 ///
451 /// let mut echo_child = Command::new("echo").arg("./").stdout(stdio).spawn()?;
452 ///
453 /// # std::io::Result::Ok(()) });
454 /// ```
455 pub async fn into_stdio(self) -> io::Result<std::process::Stdio> {
456 cfg_if::cfg_if! {
457 if #[cfg(unix)] {
458 let child_stdin = self.0.into_inner()?;
459 blocking_fd(rustix::fd::AsFd::as_fd(&child_stdin))?;
460 Ok(child_stdin.into())
461 }
462 }
463 }
464}
465
466impl io::AsyncWrite for ChildStdin {
467 fn poll_write(
468 mut self: Pin<&mut Self>,
469 cx: &mut Context<'_>,
470 buf: &[u8],
471 ) -> Poll<io::Result<usize>> {
472 Pin::new(&mut self.0).poll_write(cx, buf)
473 }
474
475 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
476 Pin::new(&mut self.0).poll_flush(cx)
477 }
478
479 fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
480 Pin::new(&mut self.0).poll_close(cx)
481 }
482}
483
484#[cfg(unix)]
485impl AsRawFd for ChildStdin {
486 fn as_raw_fd(&self) -> RawFd {
487 self.0.as_raw_fd()
488 }
489}
490
491#[cfg(unix)]
492impl AsFd for ChildStdin {
493 fn as_fd(&self) -> BorrowedFd<'_> {
494 self.0.as_fd()
495 }
496}
497
498#[cfg(unix)]
499impl TryFrom<ChildStdin> for OwnedFd {
500 type Error = io::Error;
501
502 fn try_from(value: ChildStdin) -> Result<Self, Self::Error> {
503 value.0.try_into()
504 }
505}
506
507/// A handle to a child process's standard output (stdout).
508///
509/// When a [`ChildStdout`] is dropped, the underlying handle gets closed.
510#[derive(Debug)]
511pub struct ChildStdout(#[cfg(unix)] Async<std::process::ChildStdout>);
512
513impl ChildStdout {
514 /// Convert async_process::ChildStdout into std::process::Stdio.
515 ///
516 /// You can use it to associate to the next process.
517 ///
518 /// # Examples
519 ///
520 /// ```no_run
521 /// # futures_lite::future::block_on(async {
522 /// use async_process::Command;
523 /// use std::process::Stdio;
524 /// use std::io::Read;
525 /// use futures_lite::AsyncReadExt;
526 ///
527 /// let mut ls_child = Command::new("ls").stdout(Stdio::piped()).spawn()?;
528 /// let stdio:Stdio = ls_child.stdout.take().unwrap().into_stdio().await?;
529 ///
530 /// let mut echo_child = Command::new("echo").stdin(stdio).stdout(Stdio::piped()).spawn()?;
531 /// let mut buf = vec![];
532 /// echo_child.stdout.take().unwrap().read(&mut buf).await;
533 /// # std::io::Result::Ok(()) });
534 /// ```
535 pub async fn into_stdio(self) -> io::Result<std::process::Stdio> {
536 cfg_if::cfg_if! {
537 if #[cfg(unix)] {
538 let child_stdout = self.0.into_inner()?;
539 blocking_fd(rustix::fd::AsFd::as_fd(&child_stdout))?;
540 Ok(child_stdout.into())
541 }
542 }
543 }
544}
545
546impl io::AsyncRead for ChildStdout {
547 fn poll_read(
548 mut self: Pin<&mut Self>,
549 cx: &mut Context<'_>,
550 buf: &mut [u8],
551 ) -> Poll<io::Result<usize>> {
552 Pin::new(&mut self.0).poll_read(cx, buf)
553 }
554}
555
556#[cfg(unix)]
557impl AsRawFd for ChildStdout {
558 fn as_raw_fd(&self) -> RawFd {
559 self.0.as_raw_fd()
560 }
561}
562
563#[cfg(unix)]
564impl AsFd for ChildStdout {
565 fn as_fd(&self) -> BorrowedFd<'_> {
566 self.0.as_fd()
567 }
568}
569
570#[cfg(unix)]
571impl TryFrom<ChildStdout> for OwnedFd {
572 type Error = io::Error;
573
574 fn try_from(value: ChildStdout) -> Result<Self, Self::Error> {
575 value.0.try_into()
576 }
577}
578
579/// A handle to a child process's standard error (stderr).
580///
581/// When a [`ChildStderr`] is dropped, the underlying handle gets closed.
582#[derive(Debug)]
583pub struct ChildStderr(#[cfg(unix)] Async<std::process::ChildStderr>);
584
585impl ChildStderr {
586 /// Convert async_process::ChildStderr into std::process::Stdio.
587 ///
588 /// You can use it to associate to the next process.
589 ///
590 /// # Examples
591 ///
592 /// ```no_run
593 /// # futures_lite::future::block_on(async {
594 /// use async_process::Command;
595 /// use std::process::Stdio;
596 ///
597 /// let mut ls_child = Command::new("ls").arg("x").stderr(Stdio::piped()).spawn()?;
598 /// let stdio:Stdio = ls_child.stderr.take().unwrap().into_stdio().await?;
599 ///
600 /// let mut echo_child = Command::new("echo").stdin(stdio).spawn()?;
601 /// # std::io::Result::Ok(()) });
602 /// ```
603 pub async fn into_stdio(self) -> io::Result<std::process::Stdio> {
604 cfg_if::cfg_if! {
605 if #[cfg(unix)] {
606 let child_stderr = self.0.into_inner()?;
607 blocking_fd(rustix::fd::AsFd::as_fd(&child_stderr))?;
608 Ok(child_stderr.into())
609 }
610 }
611 }
612}
613
614impl io::AsyncRead for ChildStderr {
615 fn poll_read(
616 mut self: Pin<&mut Self>,
617 cx: &mut Context<'_>,
618 buf: &mut [u8],
619 ) -> Poll<io::Result<usize>> {
620 Pin::new(&mut self.0).poll_read(cx, buf)
621 }
622}
623
624#[cfg(unix)]
625impl AsRawFd for ChildStderr {
626 fn as_raw_fd(&self) -> RawFd {
627 self.0.as_raw_fd()
628 }
629}
630
631#[cfg(unix)]
632impl AsFd for ChildStderr {
633 fn as_fd(&self) -> BorrowedFd<'_> {
634 self.0.as_fd()
635 }
636}
637
638#[cfg(unix)]
639impl TryFrom<ChildStderr> for OwnedFd {
640 type Error = io::Error;
641
642 fn try_from(value: ChildStderr) -> Result<Self, Self::Error> {
643 value.0.try_into()
644 }
645}
646
647/// Runs the driver for the asynchronous processes.
648///
649/// This future takes control of global structures related to driving [`Child`]ren and reaping
650/// zombie processes. These responsibilities include listening for the `SIGCHLD` signal and
651/// making sure zombie processes are successfully waited on.
652///
653/// If multiple tasks run `driver()` at once, only one will actually drive the reaper; the other
654/// ones will just sleep. If a task that is driving the reaper is dropped, a previously sleeping
655/// task will take over. If all tasks driving the reaper are dropped, the "async-process" thread
656/// will be spawned. The "async-process" thread just blocks on this future and will automatically
657/// be spawned if no tasks are driving the reaper once a [`Child`] is created.
658///
659/// This future will never complete. It is intended to be ran on a background task in your
660/// executor of choice.
661///
662/// # Examples
663///
664/// ```no_run
665/// use async_executor::Executor;
666/// use async_process::{driver, Command};
667///
668/// # futures_lite::future::block_on(async {
669/// // Create an executor and run on it.
670/// let ex = Executor::new();
671/// ex.run(async {
672/// // Run the driver future in the background.
673/// ex.spawn(driver()).detach();
674///
675/// // Run a command.
676/// Command::new("ls").output().await.ok();
677/// }).await;
678/// # });
679/// ```
680#[allow(clippy::manual_async_fn)]
681#[inline]
682pub fn driver() -> impl Future<Output = Infallible> + Send + 'static {
683 async {
684 // Get the reaper.
685 let reaper = Reaper::get();
686
687 // Make sure the reaper knows we're driving it.
688 reaper.drivers.fetch_add(1, Ordering::SeqCst);
689
690 // Decrement the driver count when this future is dropped.
691 let _guard = CallOnDrop(|| {
692 let prev_count = reaper.drivers.fetch_sub(1, Ordering::SeqCst);
693
694 // If this was the last driver, and there are still resources actively using the
695 // reaper, make sure that there is a thread driving the reaper.
696 if prev_count == 1
697 && (reaper.child_count.load(Ordering::SeqCst) > 0 || reaper.sys.has_zombies())
698 {
699 reaper.ensure_driven();
700 }
701 });
702
703 // Acquire the reaper lock and start polling the SIGCHLD event.
704 let guard = reaper.sys.lock().await;
705 reaper.sys.reap(guard).await
706 }
707}
708
709/// A builder for spawning processes.
710///
711/// # Examples
712///
713/// ```no_run
714/// # futures_lite::future::block_on(async {
715/// use async_process::Command;
716///
717/// let output = Command::new("sh").arg("-c").arg("echo hello").output().await?;
718/// # std::io::Result::Ok(()) });
719/// ```
720pub struct Command {
721 inner: std::process::Command,
722 stdin: bool,
723 stdout: bool,
724 stderr: bool,
725 reap_on_drop: bool,
726 kill_on_drop: bool,
727}
728
729impl Command {
730 /// Constructs a new [`Command`] for launching `program`.
731 ///
732 /// The initial configuration (the working directory and environment variables) is inherited
733 /// from the current process.
734 ///
735 /// # Examples
736 ///
737 /// ```
738 /// use async_process::Command;
739 ///
740 /// let mut cmd = Command::new("ls");
741 /// ```
742 pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
743 Self::from(std::process::Command::new(program))
744 }
745
746 /// Adds a single argument to pass to the program.
747 ///
748 /// # Examples
749 ///
750 /// ```
751 /// use async_process::Command;
752 ///
753 /// let mut cmd = Command::new("echo");
754 /// cmd.arg("hello");
755 /// cmd.arg("world");
756 /// ```
757 pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
758 self.inner.arg(arg);
759 self
760 }
761
762 /// Adds multiple arguments to pass to the program.
763 ///
764 /// # Examples
765 ///
766 /// ```
767 /// use async_process::Command;
768 ///
769 /// let mut cmd = Command::new("echo");
770 /// cmd.args(&["hello", "world"]);
771 /// ```
772 pub fn args<I, S>(&mut self, args: I) -> &mut Command
773 where
774 I: IntoIterator<Item = S>,
775 S: AsRef<OsStr>,
776 {
777 self.inner.args(args);
778 self
779 }
780
781 /// Configures an environment variable for the new process.
782 ///
783 /// Note that environment variable names are case-sensitive.
784 ///
785 /// # Examples
786 ///
787 /// ```
788 /// use async_process::Command;
789 ///
790 /// let mut cmd = Command::new("ls");
791 /// cmd.env("PATH", "/bin");
792 /// ```
793 pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
794 where
795 K: AsRef<OsStr>,
796 V: AsRef<OsStr>,
797 {
798 self.inner.env(key, val);
799 self
800 }
801
802 /// Configures multiple environment variables for the new process.
803 ///
804 /// Note that environment variable names are case-sensitive.
805 ///
806 /// # Examples
807 ///
808 /// ```
809 /// use async_process::Command;
810 ///
811 /// let mut cmd = Command::new("ls");
812 /// cmd.envs(vec![("PATH", "/bin"), ("TERM", "xterm-256color")]);
813 /// ```
814 pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
815 where
816 I: IntoIterator<Item = (K, V)>,
817 K: AsRef<OsStr>,
818 V: AsRef<OsStr>,
819 {
820 self.inner.envs(vars);
821 self
822 }
823
824 /// Gets an iterator of the arguments that will be passed to the program.
825 ///
826 /// # Examples
827 ///
828 /// ```
829 /// use std::ffi::OsStr;
830 /// use async_process::Command;
831 ///
832 /// let mut cmd = Command::new("echo");
833 /// cmd.arg("first").arg("second");
834 /// let args: Vec<&OsStr> = cmd.get_args().collect();
835 /// assert_eq!(args, &["first", "second"]);
836 /// ```
837 pub fn get_args(&self) -> std::process::CommandArgs<'_> {
838 self.inner.get_args()
839 }
840
841 /// Gets an iterator of the environment variables explicitly set for the child process.
842 ///
843 /// # Examples
844 ///
845 /// ```
846 /// use std::ffi::OsStr;
847 /// use async_process::Command;
848 ///
849 /// let mut cmd = Command::new("ls");
850 /// cmd.env("TERM", "dumb").env_remove("TZ");
851 /// let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();
852 /// assert_eq!(envs, &[
853 /// (OsStr::new("TERM"), Some(OsStr::new("dumb"))),
854 /// (OsStr::new("TZ"), None)
855 /// ]);
856 /// ```
857 pub fn get_envs(&self) -> std::process::CommandEnvs<'_> {
858 self.inner.get_envs()
859 }
860
861 /// Gets the working directory for the child process.
862 ///
863 /// This returns [`None`] if the working directory will not be changed.
864 ///
865 /// # Examples
866 ///
867 /// ```
868 /// use std::path::Path;
869 /// use async_process::Command;
870 ///
871 /// let mut cmd = Command::new("ls");
872 /// assert_eq!(cmd.get_current_dir(), None);
873 /// cmd.current_dir("/bin");
874 /// assert_eq!(cmd.get_current_dir(), Some(Path::new("/bin")));
875 /// ```
876 pub fn get_current_dir(&self) -> Option<&Path> {
877 self.inner.get_current_dir()
878 }
879
880 /// Gets the path to the program that was given to [`Command::new`].
881 ///
882 /// # Examples
883 ///
884 /// ```
885 /// use async_process::Command;
886 ///
887 /// let cmd = Command::new("echo");
888 /// assert_eq!(cmd.get_program(), "echo");
889 /// ```
890 pub fn get_program(&self) -> &OsStr {
891 self.inner.get_program()
892 }
893
894 /// Removes an environment variable mapping.
895 ///
896 /// # Examples
897 ///
898 /// ```
899 /// use async_process::Command;
900 ///
901 /// let mut cmd = Command::new("ls");
902 /// cmd.env_remove("PATH");
903 /// ```
904 pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
905 self.inner.env_remove(key);
906 self
907 }
908
909 /// Removes all environment variable mappings.
910 ///
911 /// # Examples
912 ///
913 /// ```
914 /// use async_process::Command;
915 ///
916 /// let mut cmd = Command::new("ls");
917 /// cmd.env_clear();
918 /// ```
919 pub fn env_clear(&mut self) -> &mut Command {
920 self.inner.env_clear();
921 self
922 }
923
924 /// Configures the working directory for the new process.
925 ///
926 /// # Examples
927 ///
928 /// ```
929 /// use async_process::Command;
930 ///
931 /// let mut cmd = Command::new("ls");
932 /// cmd.current_dir("/");
933 /// ```
934 pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command {
935 self.inner.current_dir(dir);
936 self
937 }
938
939 /// Configures the standard input (stdin) for the new process.
940 ///
941 /// # Examples
942 ///
943 /// ```
944 /// use async_process::{Command, Stdio};
945 ///
946 /// let mut cmd = Command::new("cat");
947 /// cmd.stdin(Stdio::null());
948 /// ```
949 pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
950 self.stdin = true;
951 self.inner.stdin(cfg);
952 self
953 }
954
955 /// Configures the standard output (stdout) for the new process.
956 ///
957 /// # Examples
958 ///
959 /// ```
960 /// use async_process::{Command, Stdio};
961 ///
962 /// let mut cmd = Command::new("ls");
963 /// cmd.stdout(Stdio::piped());
964 /// ```
965 pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
966 self.stdout = true;
967 self.inner.stdout(cfg);
968 self
969 }
970
971 /// Configures the standard error (stderr) for the new process.
972 ///
973 /// # Examples
974 ///
975 /// ```
976 /// use async_process::{Command, Stdio};
977 ///
978 /// let mut cmd = Command::new("ls");
979 /// cmd.stderr(Stdio::piped());
980 /// ```
981 pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
982 self.stderr = true;
983 self.inner.stderr(cfg);
984 self
985 }
986
987 /// Configures whether to reap the zombie process when [`Child`] is dropped.
988 ///
989 /// When the process finishes, it becomes a "zombie" and some resources associated with it
990 /// remain until [`Child::try_status()`], [`Child::status()`], or [`Child::output()`] collects
991 /// its exit code.
992 ///
993 /// If its exit code is never collected, the resources may leak forever. This crate has a
994 /// background thread named "async-process" that collects such "zombie" processes and then
995 /// "reaps" them, thus preventing the resource leaks.
996 ///
997 /// The default value of this option is `true`.
998 ///
999 /// # Examples
1000 ///
1001 /// ```
1002 /// use async_process::{Command, Stdio};
1003 ///
1004 /// let mut cmd = Command::new("cat");
1005 /// cmd.reap_on_drop(false);
1006 /// ```
1007 pub fn reap_on_drop(&mut self, reap_on_drop: bool) -> &mut Command {
1008 self.reap_on_drop = reap_on_drop;
1009 self
1010 }
1011
1012 /// Configures whether to kill the process when [`Child`] is dropped.
1013 ///
1014 /// The default value of this option is `false`.
1015 ///
1016 /// # Examples
1017 ///
1018 /// ```
1019 /// use async_process::{Command, Stdio};
1020 ///
1021 /// let mut cmd = Command::new("cat");
1022 /// cmd.kill_on_drop(true);
1023 /// ```
1024 pub fn kill_on_drop(&mut self, kill_on_drop: bool) -> &mut Command {
1025 self.kill_on_drop = kill_on_drop;
1026 self
1027 }
1028
1029 /// Executes the command and returns the [`Child`] handle to it.
1030 ///
1031 /// If not configured, stdin, stdout and stderr will be set to [`Stdio::inherit()`].
1032 ///
1033 /// # Examples
1034 ///
1035 /// ```no_run
1036 /// # futures_lite::future::block_on(async {
1037 /// use async_process::Command;
1038 ///
1039 /// let child = Command::new("ls").spawn()?;
1040 /// # std::io::Result::Ok(()) });
1041 /// ```
1042 pub fn spawn(&mut self) -> io::Result<Child> {
1043 if !self.stdin {
1044 self.inner.stdin(Stdio::inherit());
1045 }
1046 if !self.stdout {
1047 self.inner.stdout(Stdio::inherit());
1048 }
1049 if !self.stderr {
1050 self.inner.stderr(Stdio::inherit());
1051 }
1052
1053 Child::new(self)
1054 }
1055
1056 /// Executes the command, waits for it to exit, and returns the exit status.
1057 ///
1058 /// If not configured, stdin, stdout and stderr will be set to [`Stdio::inherit()`].
1059 ///
1060 /// # Examples
1061 ///
1062 /// ```no_run
1063 /// # futures_lite::future::block_on(async {
1064 /// use async_process::Command;
1065 ///
1066 /// let status = Command::new("cp")
1067 /// .arg("a.txt")
1068 /// .arg("b.txt")
1069 /// .status()
1070 /// .await?;
1071 /// # std::io::Result::Ok(()) });
1072 /// ```
1073 pub fn status(&mut self) -> impl Future<Output = io::Result<ExitStatus>> {
1074 let child = self.spawn();
1075 async { child?.status().await }
1076 }
1077
1078 /// Executes the command and collects its output.
1079 ///
1080 /// If not configured, stdin will be set to [`Stdio::null()`], and stdout and stderr will be
1081 /// set to [`Stdio::piped()`].
1082 ///
1083 /// # Examples
1084 ///
1085 /// ```no_run
1086 /// # futures_lite::future::block_on(async {
1087 /// use async_process::Command;
1088 ///
1089 /// let output = Command::new("cat")
1090 /// .arg("a.txt")
1091 /// .output()
1092 /// .await?;
1093 /// # std::io::Result::Ok(()) });
1094 /// ```
1095 pub fn output(&mut self) -> impl Future<Output = io::Result<Output>> {
1096 if !self.stdin {
1097 self.inner.stdin(Stdio::null());
1098 }
1099 if !self.stdout {
1100 self.inner.stdout(Stdio::piped());
1101 }
1102 if !self.stderr {
1103 self.inner.stderr(Stdio::piped());
1104 }
1105
1106 let child = Child::new(self);
1107 async { child?.output().await }
1108 }
1109}
1110
1111impl From<std::process::Command> for Command {
1112 fn from(inner: std::process::Command) -> Self {
1113 Self {
1114 inner,
1115 stdin: false,
1116 stdout: false,
1117 stderr: false,
1118 reap_on_drop: true,
1119 kill_on_drop: false,
1120 }
1121 }
1122}
1123
1124impl fmt::Debug for Command {
1125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1126 if f.alternate() {
1127 f.debug_struct("Command")
1128 .field("inner", &self.inner)
1129 .field("stdin", &self.stdin)
1130 .field("stdout", &self.stdout)
1131 .field("stderr", &self.stderr)
1132 .field("reap_on_drop", &self.reap_on_drop)
1133 .field("kill_on_drop", &self.kill_on_drop)
1134 .finish()
1135 } else {
1136 // Stdlib outputs command-line in Debug for Command. This does the
1137 // same, if not in "alternate" (long pretty-printed) mode.
1138 // This is useful for logs, for example.
1139 fmt::Debug::fmt(&self.inner, f)
1140 }
1141 }
1142}
1143
1144/// Moves `Fd` out of non-blocking mode.
1145#[cfg(unix)]
1146fn blocking_fd(fd: rustix::fd::BorrowedFd<'_>) -> io::Result<()> {
1147 cfg_if::cfg_if! {
1148 // ioctl(FIONBIO) sets the flag atomically, but we use this only on Linux
1149 // for now, as with the standard library, because it seems to behave
1150 // differently depending on the platform.
1151 // https://github.com/rust-lang/rust/commit/efeb42be2837842d1beb47b51bb693c7474aba3d
1152 // https://github.com/libuv/libuv/blob/e9d91fccfc3e5ff772d5da90e1c4a24061198ca0/src/unix/poll.c#L78-L80
1153 // https://github.com/tokio-rs/mio/commit/0db49f6d5caf54b12176821363d154384357e70a
1154 if #[cfg(target_os = "linux")] {
1155 rustix::io::ioctl_fionbio(fd, false)?;
1156 } else {
1157 let previous = rustix::fs::fcntl_getfl(fd)?;
1158 let new = previous & !rustix::fs::OFlags::NONBLOCK;
1159 if new != previous {
1160 rustix::fs::fcntl_setfl(fd, new)?;
1161 }
1162 }
1163 }
1164 Ok(())
1165}
1166
1167struct CallOnDrop<F: FnMut()>(F);
1168
1169impl<F: FnMut()> Drop for CallOnDrop<F> {
1170 fn drop(&mut self) {
1171 (self.0)();
1172 }
1173}
1174
1175#[cfg(test)]
1176mod test {
1177 #[test]
1178 fn polled_driver() {
1179 use super::{driver, Command};
1180 use futures_lite::future;
1181 use futures_lite::prelude::*;
1182
1183 let is_thread_spawned =
1184 || super::DRIVER_THREAD_SPAWNED.load(std::sync::atomic::Ordering::SeqCst);
1185
1186 #[cfg(unix)]
1187 fn command() -> Command {
1188 let mut cmd = Command::new("sh");
1189 cmd.arg("-c").arg("echo hello");
1190 cmd
1191 }
1192
1193 #[cfg(unix)]
1194 const OUTPUT: &[u8] = b"hello\n";
1195
1196 future::block_on(async {
1197 // Thread should not be spawned off the bat.
1198 assert!(!is_thread_spawned());
1199
1200 // Spawn a driver.
1201 let mut driver1 = Box::pin(driver());
1202 future::poll_once(&mut driver1).await;
1203 assert!(!is_thread_spawned());
1204
1205 // We should be able to run the driver in parallel with a process future.
1206 async {
1207 (&mut driver1).await;
1208 }
1209 .or(async {
1210 let output = command().output().await.unwrap();
1211 assert_eq!(output.stdout, OUTPUT);
1212 })
1213 .await;
1214 assert!(!is_thread_spawned());
1215
1216 // Spawn a second driver.
1217 let mut driver2 = Box::pin(driver());
1218 future::poll_once(&mut driver2).await;
1219 assert!(!is_thread_spawned());
1220
1221 // Poll both drivers in parallel.
1222 async {
1223 (&mut driver1).await;
1224 }
1225 .or(async {
1226 (&mut driver2).await;
1227 })
1228 .or(async {
1229 let output = command().output().await.unwrap();
1230 assert_eq!(output.stdout, OUTPUT);
1231 })
1232 .await;
1233 assert!(!is_thread_spawned());
1234
1235 // Once one is dropped, the other should take over.
1236 drop(driver1);
1237 assert!(!is_thread_spawned());
1238
1239 // Poll driver2 in parallel with a process future.
1240 async {
1241 (&mut driver2).await;
1242 }
1243 .or(async {
1244 let output = command().output().await.unwrap();
1245 assert_eq!(output.stdout, OUTPUT);
1246 })
1247 .await;
1248 assert!(!is_thread_spawned());
1249
1250 // Once driver2 is dropped, the thread should not be spawned, as there are no active
1251 // child processes..
1252 drop(driver2);
1253 assert!(!is_thread_spawned());
1254
1255 // We should now be able to poll the process future independently, it will spawn the
1256 // thread.
1257 let output = command().output().await.unwrap();
1258 assert_eq!(output.stdout, OUTPUT);
1259 assert!(is_thread_spawned());
1260 });
1261 }
1262}