duct-0.13.5/.cargo_vcs_info.json0000644000000001121400705751600121160ustar { "git": { "sha1": "8165fadca4bcd4f464d868817de2ad3176b107a0" } } duct-0.13.5/.github/workflows/ci.yml010064400017500001750000000010171376151060700154400ustar 00000000000000name: tests on: [push] jobs: test: name: Rust ${{ matrix.rust_version }} on ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: matrix: rust_version: [stable, beta, nightly] os: [ubuntu-latest, windows-latest, macOS-latest] steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: ${{ matrix.rust_version }} override: true - run: cargo test - name: test duct_sh run: cargo test working-directory: ./duct_sh duct-0.13.5/.gitignore010064400017500001750000000000221376151060700127100ustar 00000000000000target Cargo.lock duct-0.13.5/Cargo.lock0000644000000064321400705751600101040ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "duct" version = "0.13.5" dependencies = [ "libc", "once_cell", "os_pipe", "shared_child", "tempdir", ] [[package]] name = "fuchsia-cprng" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" [[package]] name = "libc" version = "0.2.85" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" [[package]] name = "once_cell" version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" [[package]] name = "os_pipe" version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fb233f06c2307e1f5ce2ecad9f8121cffbbee2c95428f44ea85222e460d0d213" dependencies = [ "libc", "winapi", ] [[package]] name = "rand" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" dependencies = [ "fuchsia-cprng", "libc", "rand_core 0.3.1", "rdrand", "winapi", ] [[package]] name = "rand_core" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" dependencies = [ "rand_core 0.4.2", ] [[package]] name = "rand_core" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" [[package]] name = "rdrand" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" dependencies = [ "rand_core 0.3.1", ] [[package]] name = "remove_dir_all" version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" dependencies = [ "winapi", ] [[package]] name = "shared_child" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8cebcf3a403e4deafaf34dc882c4a1b6a648b43e5670aa2e4bb985914eaeb2d2" dependencies = [ "libc", "winapi", ] [[package]] name = "tempdir" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" dependencies = [ "rand", "remove_dir_all", ] [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" duct-0.13.5/Cargo.toml0000644000000021171400705751600101230ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "duct" version = "0.13.5" authors = ["oconnor663@gmail.com"] description = "a library for running child processes" documentation = "https://docs.rs/duct" readme = "README.md" keywords = ["process", "subprocess", "command", "child", "pipe"] license = "MIT" repository = "https://github.com/oconnor663/duct.rs" [dependencies.once_cell] version = "1.0.1" [dependencies.os_pipe] version = "0.9.0" [dependencies.shared_child] version = "0.3.4" [dev-dependencies.tempdir] version = "0.3.7" [target."cfg(unix)".dependencies.libc] version = "0.2.43" duct-0.13.5/Cargo.toml.orig010064400017500001750000000007751400705666600136320ustar 00000000000000[package] name = "duct" version = "0.13.5" authors = ["oconnor663@gmail.com"] description = "a library for running child processes" repository = "https://github.com/oconnor663/duct.rs" documentation = "https://docs.rs/duct" readme = "README.md" license = "MIT" keywords = ["process", "subprocess", "command", "child", "pipe"] edition = "2018" [dependencies] os_pipe = "0.9.0" shared_child = "0.3.4" once_cell = "1.0.1" [target.'cfg(unix)'.dependencies] libc = "0.2.43" [dev-dependencies] tempdir = "0.3.7" duct-0.13.5/LICENSE010064400017500001750000000020261376151060700117330ustar 00000000000000The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. duct-0.13.5/README.md010064400017500001750000000037501400705714200122040ustar 00000000000000# duct.rs [![Actions Status](https://github.com/oconnor663/duct.rs/workflows/tests/badge.svg)](https://github.com/oconnor663/duct.rs/actions) [![crates.io](https://img.shields.io/crates/v/duct.svg)](https://crates.io/crates/duct) [![docs.rs](https://docs.rs/duct/badge.svg)](https://docs.rs/duct) Duct is a library for running child processes. Duct makes it easy to build pipelines and redirect IO like a shell. At the same time, Duct helps you write correct, portable code: whitespace is never significant, errors from child processes get reported by default, and a variety of [gotchas, bugs, and platform inconsistencies](https://github.com/oconnor663/duct.py/blob/master/gotchas.md) are handled for you the Right Way™. - [Documentation](https://docs.rs/duct) - [Crate](https://crates.io/crates/duct) - [GitHub repo](https://github.com/oconnor663/duct.rs) - [the same library, in Python](https://github.com/oconnor663/duct.py) Examples -------- Run a command without capturing any output. Here "hi" is printed directly to the terminal: ```rust use duct::cmd; cmd!("echo", "hi").run()?; ``` Capture the standard output of a command. Here "hi" is returned as a `String`: ```rust let stdout = cmd!("echo", "hi").read()?; assert_eq!(stdout, "hi"); ``` Capture the standard output of a pipeline: ```rust let stdout = cmd!("echo", "hi").pipe(cmd!("sed", "s/i/o/")).read()?; assert_eq!(stdout, "ho"); ``` Merge standard error into standard output and read both incrementally: ```rust use duct::cmd; use std::io::prelude::*; use std::io::BufReader; let big_cmd = cmd!("bash", "-c", "echo out && echo err 1>&2"); let reader = big_cmd.stderr_to_stdout().reader()?; let mut lines = BufReader::new(reader).lines(); assert_eq!(lines.next().unwrap()?, "out"); assert_eq!(lines.next().unwrap()?, "err"); ``` Children that exit with a non-zero status return an error by default: ```rust let result = cmd!("false").run(); assert!(result.is_err()); let result = cmd!("false").unchecked().run(); assert!(result.is_ok()); ``` duct-0.13.5/README.tpl010064400017500001750000000004721376151060700124070ustar 00000000000000# {{crate}}.rs [![Actions Status](https://github.com/oconnor663/duct.rs/workflows/tests/badge.svg)](https://github.com/oconnor663/duct.rs/actions) [![crates.io](https://img.shields.io/crates/v/duct.svg)](https://crates.io/crates/duct) [![docs.rs](https://docs.rs/duct/badge.svg)](https://docs.rs/duct) {{readme}} duct-0.13.5/src/bin/cat.rs010064400017500001750000000003211376151060700133760ustar 00000000000000#![deny(warnings)] use std::io::{copy, stdin, stdout}; fn main() { let stdin_handle = stdin(); let stdout_handle = stdout(); copy(&mut stdin_handle.lock(), &mut stdout_handle.lock()).unwrap(); } duct-0.13.5/src/bin/child_grandchild.rs010064400017500001750000000022061376151060700160750ustar 00000000000000use duct::cmd; use std::env; use std::io; use std::thread; use std::time::Duration; fn run_child() -> io::Result<()> { // What the child does is start the grandchild (which is this same exe with // an extra flag), print "started" to indicate that that's done, and then // wait on the grandchild. let grandchild = cmd!(env::current_exe()?, "--grandchild").start()?; println!("started"); grandchild.wait()?; Ok(()) } fn run_grandchild() -> io::Result<()> { // All the grandchild does is sleep "forever". (Forever equals one day.) // // Ultimately the test is going to leak this sleeping process. That's kind // of the point of what it's testing, and it's hard to avoid without // creating some sort of race condition. Apologies to anyone who runs this // test thousands of times a day. thread::sleep(Duration::from_secs(24 * 60 * 60)); Ok(()) } fn main() -> io::Result<()> { let args: Vec = env::args().collect(); if args.len() > 1 { assert_eq!(args.len(), 2); assert_eq!(&args[1], "--grandchild"); run_grandchild() } else { run_child() } } duct-0.13.5/src/bin/echo.rs010064400017500001750000000001711376151060700135500ustar 00000000000000#![deny(warnings)] use std::env::args; fn main() { println!("{}", args().skip(1).collect::>().join(" ")); } duct-0.13.5/src/bin/exe_in_dir.rs010064400017500001750000000050121376151060700147360ustar 00000000000000#![deny(warnings)] extern crate duct; use std::env::{args, current_dir, set_current_dir}; use std::io; use std::path::Path; use std::process::exit; #[cfg(unix)] fn check_executable(path: &Path) { use std::os::unix::fs::MetadataExt; const EXECUTABLE_BIT: u32 = 0o100; let metadata = path.metadata().unwrap(); let mode = metadata.mode(); if mode & EXECUTABLE_BIT == 0 { println!( "Expected {:?} to be publicly executable, but found mode {:o}.", path, mode ); exit(1); } } #[cfg(windows)] fn check_executable(_: &Path) { // no-op } fn main() { // The first command line arg is a path to an executable, and the rest are // its arguments. Chdir to the parent dir, and then try to execute the // program as a single-component Path (*not* a string). Duct should // automatically add the leading ./ for us so that it works on Unix. (And if // the executable doesn't actually exist, duct should prevent it from // matching some other command in the PATH, on either Unix or Windows.) let args_vec: Vec<_> = args().collect(); if args_vec.len() < 2 { println!("must have at least 1 exe arg"); exit(1); } // Parse out the exe name and the dir it's in. let exe_full_path = Path::new(&args_vec[1]); let exe_parent = exe_full_path.parent().unwrap(); let exe_name: &Path = exe_full_path.file_name().unwrap().as_ref(); // Sanity check that exe actually exists. if !exe_full_path.is_file() { println!(r#""{}" is not a file."#, exe_full_path.display()); exit(1); } // If we try to execute a non-executable file on Unix, we'll end up with a // "file not found" error. We really don't want to confuse those with the // errors we get from bad Path handling. Explicitly check for that here. check_executable(exe_full_path); // Chdir to that dir. set_current_dir(exe_parent).unwrap(); // Run the child! let res = duct::cmd(exe_name, &args_vec[2..]).run(); // Check what the child did, and exit appropriately. if let Err(err) = res { println!( "error executing command {:?} in dir {:?}", exe_name, current_dir().unwrap() ); if err.kind() == io::ErrorKind::NotFound { println!("File not found during execution! Path sanitization is TOTALLY BROKEN!"); exit(1); } else { println!("Unexpected IO error: {:?}", err); exit(1); } } // Success! } duct-0.13.5/src/bin/print_env.rs010064400017500001750000000003051376151060700146350ustar 00000000000000#![deny(warnings)] fn main() { let var_name = std::env::args().nth(1).unwrap(); if let Some(value) = std::env::var_os(var_name) { println!("{}", value.to_string_lossy()); } } duct-0.13.5/src/bin/pwd.rs010064400017500001750000000001741376151060700134270ustar 00000000000000#![deny(warnings)] use std::env::current_dir; fn main() { println!("{}", current_dir().unwrap().to_string_lossy()); } duct-0.13.5/src/bin/sleep.rs010064400017500001750000000003251376151060700137430ustar 00000000000000#![deny(warnings)] fn main() { let secs: f32 = std::env::args().nth(1).unwrap().parse().unwrap(); let millis = (1000f32 * secs) as u64; std::thread::sleep(std::time::Duration::from_millis(millis)); } duct-0.13.5/src/bin/status.rs010064400017500001750000000003531376151060700141570ustar 00000000000000#![deny(warnings)] use std::env::args; use std::process::exit; fn main() { let status = args() .nth(1) .map(|s| i32::from_str_radix(&s, 10).expect("not a valid status")) .unwrap_or(0); exit(status); } duct-0.13.5/src/bin/x_to_y.rs010064400017500001750000000004221376151060700141320ustar 00000000000000#![deny(warnings)] use std::io::prelude::*; use std::io::{stdin, stdout}; fn main() { let mut input: String = String::new(); stdin().read_to_string(&mut input).unwrap(); let output = input.replace('x', "y"); stdout().write_all(output.as_ref()).unwrap(); } duct-0.13.5/src/lib.rs010064400017500001750000002176731376151060700126510ustar 00000000000000//! Duct is a library for running child processes. Duct makes it easy to build //! pipelines and redirect IO like a shell. At the same time, Duct helps you //! write correct, portable code: whitespace is never significant, errors from //! child processes get reported by default, and a variety of [gotchas, bugs, //! and platform //! inconsistencies](https://github.com/oconnor663/duct.py/blob/master/gotchas.md) //! are handled for you the Right Way™. //! //! - [Documentation](https://docs.rs/duct) //! - [Crate](https://crates.io/crates/duct) //! - [GitHub repo](https://github.com/oconnor663/duct.rs) //! - [the same library, in Python](https://github.com/oconnor663/duct.py) //! //! Examples //! -------- //! //! Run a command without capturing any output. Here "hi" is printed directly //! to the terminal: //! //! ``` //! # fn main() -> Result<(), Box> { //! # if cfg!(not(windows)) { //! use duct::cmd; //! cmd!("echo", "hi").run()?; //! # } //! # Ok(()) //! # } //! ``` //! //! Capture the standard output of a command. Here "hi" is returned as a //! `String`: //! //! ``` //! # use duct::cmd; //! # fn main() -> Result<(), Box> { //! # if cfg!(not(windows)) { //! let stdout = cmd!("echo", "hi").read()?; //! assert_eq!(stdout, "hi"); //! # } //! # Ok(()) //! # } //! ``` //! //! Capture the standard output of a pipeline: //! //! ``` //! # use duct::cmd; //! # fn main() -> Result<(), Box> { //! # if cfg!(not(windows)) { //! let stdout = cmd!("echo", "hi").pipe(cmd!("sed", "s/i/o/")).read()?; //! assert_eq!(stdout, "ho"); //! # } //! # Ok(()) //! # } //! ``` //! //! Merge standard error into standard output and read both incrementally: //! //! ``` //! # fn main() -> Result<(), Box> { //! # if cfg!(not(windows)) { //! use duct::cmd; //! use std::io::prelude::*; //! use std::io::BufReader; //! //! let big_cmd = cmd!("bash", "-c", "echo out && echo err 1>&2"); //! let reader = big_cmd.stderr_to_stdout().reader()?; //! let mut lines = BufReader::new(reader).lines(); //! assert_eq!(lines.next().unwrap()?, "out"); //! assert_eq!(lines.next().unwrap()?, "err"); //! # } //! # Ok(()) //! # } //! ``` //! //! Children that exit with a non-zero status return an error by default: //! //! ``` //! # use duct::cmd; //! # fn main() -> Result<(), Box> { //! # if cfg!(not(windows)) { //! let result = cmd!("false").run(); //! assert!(result.is_err()); //! let result = cmd!("false").unchecked().run(); //! assert!(result.is_ok()); //! # } //! # Ok(()) //! # } //! ``` use once_cell::sync::OnceCell; use shared_child::SharedChild; use std::collections::HashMap; use std::ffi::{OsStr, OsString}; use std::fmt; use std::fs::File; use std::io; use std::io::prelude::*; use std::mem; use std::path::{Path, PathBuf}; use std::process::{Command, ExitStatus, Output, Stdio}; use std::sync::{Arc, Mutex}; use std::thread::JoinHandle; #[cfg(not(windows))] use std::os::unix::prelude::*; #[cfg(windows)] use std::os::windows::prelude::*; /// Unix-specific extensions to duct, for sending signals. #[cfg(unix)] pub mod unix; // enums defined below use ExpressionInner::*; use IoExpressionInner::*; /// Create a command given a program name and a collection of arguments. See /// also the [`cmd!`](macro.cmd.html) macro, which doesn't require a collection. /// /// # Example /// /// ``` /// use duct::cmd; /// /// let args = vec!["foo", "bar", "baz"]; /// /// # // NOTE: Normally this wouldn't work on Windows, but we have an "echo" /// # // binary that gets built for our main tests, and it's sitting around by /// # // the time we get here. If this ever stops working, then we can disable /// # // the tests that depend on it. /// let output = cmd("echo", &args).read(); /// /// assert_eq!("foo bar baz", output.unwrap()); /// ``` pub fn cmd(program: T, args: U) -> Expression where T: IntoExecutablePath, U: IntoIterator, U::Item: Into, { let mut argv_vec = Vec::new(); argv_vec.push(program.to_executable()); argv_vec.extend(args.into_iter().map(Into::::into)); Expression::new(Cmd(argv_vec)) } /// Create a command with any number of of positional arguments, which may be /// different types (anything that implements /// [`Into`](https://doc.rust-lang.org/std/convert/trait.From.html)). /// See also the [`cmd`](fn.cmd.html) function, which takes a collection of /// arguments. /// /// # Example /// /// ``` /// /// use duct::cmd; /// use std::path::Path; /// /// fn main() { /// let arg1 = "foo"; /// let arg2 = "bar".to_owned(); /// let arg3 = Path::new("baz"); /// /// let output = cmd!("echo", arg1, arg2, arg3).read(); /// /// assert_eq!("foo bar baz", output.unwrap()); /// } /// ``` #[macro_export] macro_rules! cmd { ( $program:expr $(, $arg:expr )* $(,)? ) => { { use std::ffi::OsString; let args: &[OsString] = &[$( Into::::into($arg) ),*]; $crate::cmd($program, args) } }; } /// The central objects in Duct, Expressions are created with /// [`cmd`](fn.cmd.html) or [`cmd!`](macro.cmd.html), combined with /// [`pipe`](struct.Expression.html#method.pipe), and finally executed with /// [`run`](struct.Expression.html#method.run), /// [`read`](struct.Expression.html#method.read), /// [`start`](struct.Expression.html#method.start), or /// [`reader`](struct.Expression.html#method.reader). They also support several /// methods to control their execution, like /// [`stdin_bytes`](struct.Expression.html#method.stdin_bytes), /// [`stdout_capture`](struct.Expression.html#method.stdout_capture), /// [`env`](struct.Expression.html#method.env), and /// [`unchecked`](struct.Expression.html#method.unchecked). /// /// Expressions are immutable, and they do a lot of /// [`Arc`](https://doc.rust-lang.org/std/sync/struct.Arc.html) sharing /// internally, so all of the methods below take `&self` and return a new /// `Expression` cheaply. /// /// Expressions using `pipe` form trees, and the order in which you call /// different methods can matter, just like it matters where you put /// redirections in Bash. For example, each of these expressions suppresses /// output differently: /// /// ```no_run /// # use duct::cmd; /// # fn main() -> std::io::Result<()> { /// // Only suppress stderr on the left side. /// cmd!("foo").stderr_null().pipe(cmd!("bar")).run()?; /// /// // Only suppress stderr on the right side. /// cmd!("foo").pipe(cmd!("bar").stderr_null()).run()?; /// /// // Suppress stderr on both sides. /// cmd!("foo").pipe(cmd!("bar")).stderr_null().run()?; /// # Ok(()) /// # } /// ``` #[derive(Clone)] #[must_use] pub struct Expression(Arc); impl Expression { /// Execute an expression, wait for it to complete, and return a /// [`std::process::Output`](https://doc.rust-lang.org/std/process/struct.Output.html) /// object containing the results. Nothing is captured by default, but if /// you build the expression with /// [`stdout_capture`](struct.Expression.html#method.stdout_capture) or /// [`stderr_capture`](struct.Expression.html#method.stderr_capture) then /// the `Output` will hold those captured bytes. /// /// # Errors /// /// In addition to all the IO errors possible with /// [`std::process::Command`](https://doc.rust-lang.org/std/process/struct.Command.html), /// `run` will return an /// [`ErrorKind::Other`](https://doc.rust-lang.org/std/io/enum.ErrorKind.html) /// IO error if child returns a non-zero exit status. To suppress this error /// and return an `Output` even when the exit status is non-zero, use the /// [`unchecked`](struct.Expression.html#method.unchecked) method. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let output = cmd!("echo", "hi").stdout_capture().run().unwrap(); /// assert_eq!(b"hi\n".to_vec(), output.stdout); /// # } /// # } /// ``` pub fn run(&self) -> io::Result { // This could be optimized to avoid creating a background threads, by // using the current thread to read stdout or stderr if only one of // them is captured, or by using async IO to read both. self.start()?.into_output() } /// Execute an expression, capture its standard output, and return the /// captured output as a `String`. This is a convenience wrapper around /// [`reader`](struct.Expression.html#method.reader). Like backticks and /// `$()` in the shell, `read` trims trailing newlines. /// /// # Errors /// /// In addition to all the errors possible with /// [`run`](struct.Expression.html#method.run), `read` will return an error /// if the captured bytes aren't valid UTF-8. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let output = cmd!("echo", "hi").stdout_capture().read().unwrap(); /// assert_eq!("hi", output); /// # } /// # } /// ``` pub fn read(&self) -> io::Result { let mut reader = self.reader()?; let mut output = String::new(); reader.read_to_string(&mut output)?; while output.ends_with('\n') || output.ends_with('\r') { output.truncate(output.len() - 1); } Ok(output) } /// Start running an expression, and immediately return a /// [`Handle`](struct.Handle.html) that represents all the child processes. /// This is analogous to the /// [`spawn`](https://doc.rust-lang.org/std/process/struct.Command.html#method.spawn) /// method in the standard library. The `Handle` may be shared between /// multiple threads. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let handle = cmd!("echo", "hi").stdout_capture().start().unwrap(); /// let output = handle.wait().unwrap(); /// assert_eq!(b"hi\n".to_vec(), output.stdout); /// # } /// # } /// ``` pub fn start(&self) -> io::Result { let stdout_capture = OutputCaptureContext::new(); let stderr_capture = OutputCaptureContext::new(); let context = IoContext::new(&stdout_capture, &stderr_capture); Ok(Handle { inner: self.0.start(context)?, result: OnceCell::new(), readers: Mutex::new(( stdout_capture.maybe_read_thread(), stderr_capture.maybe_read_thread(), )), }) } /// Start running an expression, and immediately return a /// [`ReaderHandle`](struct.ReaderHandle.html) attached to the child's /// stdout. This is similar to `.stdout_capture().start()`, but it returns /// the reader to the caller rather than reading from a background thread. /// /// Note that because this method doesn't read child output on a background /// thread, it's a best practice to only create one `ReaderHandle` at a /// time. Child processes with a lot of output will eventually block if /// their stdout pipe isn't read from. If you have multiple children /// running, but you're only reading from one of them at a time, that could /// block the others and lead to performance issues or deadlocks. For /// reading from multiple children at once, prefer /// `.stdout_capture().start()`. /// /// # Example /// /// ``` /// # use duct::cmd; /// # use std::io::prelude::*; /// # fn main() { /// # if cfg!(not(windows)) { /// let mut reader = cmd!("echo", "hi").reader().unwrap(); /// let mut stdout = Vec::new(); /// reader.read_to_end(&mut stdout).unwrap(); /// assert_eq!(b"hi\n".to_vec(), stdout); /// # } /// # } /// ``` pub fn reader(&self) -> io::Result { let stdout_capture = OutputCaptureContext::new(); let stderr_capture = OutputCaptureContext::new(); let context = IoContext::new(&stdout_capture, &stderr_capture); let handle = Handle { inner: self.stdout_capture().0.start(context)?, result: OnceCell::new(), readers: Mutex::new((None, stderr_capture.maybe_read_thread())), }; Ok(ReaderHandle { handle, reader: stdout_capture.pair.into_inner().expect("pipe opened").0, }) } /// Join two expressions into a pipe expression, where the standard output /// of the left will be hooked up to the standard input of the right, like /// `|` in the shell. /// /// # Errors /// /// During execution, if one side of the pipe returns a non-zero exit /// status, that becomes the status of the whole pipe, similar to Bash's /// `pipefail` option. If both sides return non-zero, and one of them is /// [`unchecked`](struct.Expression.html#method.unchecked), then the checked /// side wins. Otherwise the right side wins. /// /// During spawning, if the left side of the pipe spawns successfully, but /// the right side fails to spawn, the left side will be killed and /// awaited. That's necessary to return the spawn error immediately, /// without leaking the left side as a zombie. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let output = cmd!("echo", "hi").pipe(cmd!("sed", "s/h/p/")).read(); /// assert_eq!("pi", output.unwrap()); /// # } /// # } /// ``` pub fn pipe>(&self, right: T) -> Expression { Self::new(Pipe(self.clone(), right.into())) } /// Use bytes or a string as input for an expression, like `<<<` in the /// shell. A worker thread will write the input at runtime. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// // Many types implement Into>. Here's a string. /// let output = cmd!("cat").stdin_bytes("foo").read().unwrap(); /// assert_eq!("foo", output); /// /// // And here's a byte slice. /// let output = cmd!("cat").stdin_bytes(&b"foo"[..]).read().unwrap(); /// assert_eq!("foo", output); /// # } /// # } /// ``` pub fn stdin_bytes>>(&self, bytes: T) -> Expression { Self::new(Io(StdinBytes(Arc::new(bytes.into())), self.clone())) } /// Open a file at the given path and use it as input for an expression, /// like `<` in the shell. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// // Many types implement Into, including &str. /// let output = cmd!("head", "-c", "3").stdin_path("/dev/zero").read().unwrap(); /// assert_eq!("\0\0\0", output); /// # } /// # } /// ``` pub fn stdin_path>(&self, path: T) -> Expression { Self::new(Io(StdinPath(path.into()), self.clone())) } /// Use an already opened file or pipe as input for an expression. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let input_file = std::fs::File::open("/dev/zero").unwrap(); /// let output = cmd!("head", "-c", "3").stdin_file(input_file).read().unwrap(); /// assert_eq!("\0\0\0", output); /// # } /// # } /// ``` #[cfg(not(windows))] pub fn stdin_file(&self, file: T) -> Expression { Self::new(Io(StdinFile(into_file(file)), self.clone())) } #[cfg(windows)] pub fn stdin_file(&self, file: T) -> Expression { Self::new(Io(StdinFile(into_file(file)), self.clone())) } /// Use `/dev/null` (or `NUL` on Windows) as input for an expression. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let output = cmd!("cat").stdin_null().read().unwrap(); /// assert_eq!("", output); /// # } /// # } /// ``` pub fn stdin_null(&self) -> Expression { Self::new(Io(StdinNull, self.clone())) } /// Open a file at the given path and use it as output for an expression, /// like `>` in the shell. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # use std::io::prelude::*; /// # if cfg!(not(windows)) { /// // Many types implement Into, including &str. /// let path = cmd!("mktemp").read().unwrap(); /// cmd!("echo", "wee").stdout_path(&path).run().unwrap(); /// let mut output = String::new(); /// std::fs::File::open(&path).unwrap().read_to_string(&mut output).unwrap(); /// assert_eq!("wee\n", output); /// # } /// # } /// ``` pub fn stdout_path>(&self, path: T) -> Expression { Self::new(Io(StdoutPath(path.into()), self.clone())) } /// Use an already opened file or pipe as output for an expression. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # use std::io::prelude::*; /// # if cfg!(not(windows)) { /// let path = cmd!("mktemp").read().unwrap(); /// let file = std::fs::File::create(&path).unwrap(); /// cmd!("echo", "wee").stdout_file(file).run().unwrap(); /// let mut output = String::new(); /// std::fs::File::open(&path).unwrap().read_to_string(&mut output).unwrap(); /// assert_eq!("wee\n", output); /// # } /// # } /// ``` #[cfg(not(windows))] pub fn stdout_file(&self, file: T) -> Expression { Self::new(Io(StdoutFile(into_file(file)), self.clone())) } #[cfg(windows)] pub fn stdout_file(&self, file: T) -> Expression { Self::new(Io(StdoutFile(into_file(file)), self.clone())) } /// Use `/dev/null` (or `NUL` on Windows) as output for an expression. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// // This echo command won't print anything. /// cmd!("echo", "foo", "bar", "baz").stdout_null().run().unwrap(); /// /// // And you won't get anything even if you try to read its output! The /// // null redirect happens farther down in the expression tree than the /// // implicit `stdout_capture`, and so it takes precedence. /// let output = cmd!("echo", "foo", "bar", "baz").stdout_null().read().unwrap(); /// assert_eq!("", output); /// # } /// ``` pub fn stdout_null(&self) -> Expression { Self::new(Io(StdoutNull, self.clone())) } /// Capture the standard output of an expression. The captured bytes will /// be available on the `stdout` field of the /// [`std::process::Output`](https://doc.rust-lang.org/std/process/struct.Output.html) /// object returned by [`run`](struct.Expression.html#method.run) or /// [`wait`](struct.Handle.html#method.wait). Output is read by a /// background thread, so the child will never block writing to stdout. But /// note that [`read`](struct.Expression.html#method.read) and /// [`reader`](struct.Expression.html#method.reader) can be more /// convenient, and they don't require the background thread. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// // The most direct way to read stdout bytes is `stdout_capture`. /// let output1 = cmd!("echo", "foo").stdout_capture().run().unwrap().stdout; /// assert_eq!(&b"foo\n"[..], &output1[..]); /// /// // The `read` method is a shorthand for `stdout_capture`, and it also /// // does string parsing and newline trimming. /// let output2 = cmd!("echo", "foo").read().unwrap(); /// assert_eq!("foo", output2) /// # } /// # } /// ``` pub fn stdout_capture(&self) -> Expression { Self::new(Io(StdoutCapture, self.clone())) } /// Join the standard output of an expression to its standard error pipe, /// similar to `1>&2` in the shell. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let output = cmd!("echo", "foo").stdout_to_stderr().stderr_capture().run().unwrap(); /// assert_eq!(&b"foo\n"[..], &output.stderr[..]); /// # } /// # } /// ``` pub fn stdout_to_stderr(&self) -> Expression { Self::new(Io(StdoutToStderr, self.clone())) } /// Open a file at the given path and use it as error output for an /// expression, like `2>` in the shell. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # use std::io::prelude::*; /// # if cfg!(not(windows)) { /// // Many types implement Into, including &str. /// let path = cmd!("mktemp").read().unwrap(); /// cmd!("sh", "-c", "echo wee >&2").stderr_path(&path).run().unwrap(); /// let mut error_output = String::new(); /// std::fs::File::open(&path).unwrap().read_to_string(&mut error_output).unwrap(); /// assert_eq!("wee\n", error_output); /// # } /// # } /// ``` pub fn stderr_path>(&self, path: T) -> Expression { Self::new(Io(StderrPath(path.into()), self.clone())) } /// Use an already opened file or pipe as error output for an expression. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # use std::io::prelude::*; /// # if cfg!(not(windows)) { /// let path = cmd!("mktemp").read().unwrap(); /// let file = std::fs::File::create(&path).unwrap(); /// cmd!("sh", "-c", "echo wee >&2").stderr_file(file).run().unwrap(); /// let mut error_output = String::new(); /// std::fs::File::open(&path).unwrap().read_to_string(&mut error_output).unwrap(); /// assert_eq!("wee\n", error_output); /// # } /// # } /// ``` #[cfg(not(windows))] pub fn stderr_file(&self, file: T) -> Expression { Self::new(Io(StderrFile(into_file(file)), self.clone())) } #[cfg(windows)] pub fn stderr_file(&self, file: T) -> Expression { Self::new(Io(StderrFile(into_file(file)), self.clone())) } /// Use `/dev/null` (or `NUL` on Windows) as error output for an expression. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// // This echo-to-stderr command won't print anything. /// cmd!("sh", "-c", "echo foo bar baz >&2").stderr_null().run().unwrap(); /// # } /// # } /// ``` pub fn stderr_null(&self) -> Expression { Self::new(Io(StderrNull, self.clone())) } /// Capture the error output of an expression. The captured bytes will be /// available on the `stderr` field of the `Output` object returned by /// [`run`](struct.Expression.html#method.run) or /// [`wait`](struct.Handle.html#method.wait). Output is read by a /// background thread, so the child will never block writing to stderr. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let output_obj = cmd!("sh", "-c", "echo foo >&2").stderr_capture().run().unwrap(); /// assert_eq!(&b"foo\n"[..], &output_obj.stderr[..]); /// # } /// # } /// ``` pub fn stderr_capture(&self) -> Expression { Self::new(Io(StderrCapture, self.clone())) } /// Join the standard error of an expression to its standard output pipe, /// similar to `2>&1` in the shell. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let error_output = cmd!("sh", "-c", "echo foo >&2").stderr_to_stdout().read().unwrap(); /// assert_eq!("foo", error_output); /// # } /// # } /// ``` pub fn stderr_to_stdout(&self) -> Expression { Self::new(Io(StderrToStdout, self.clone())) } /// Swap the stdout and stderr of an expression. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let output = cmd!("sh", "-c", "echo foo && echo bar >&2") /// .stdout_stderr_swap() /// .stdout_capture() /// .stderr_capture() /// .run() /// .unwrap(); /// assert_eq!(b"bar\n", &*output.stdout); /// assert_eq!(b"foo\n", &*output.stderr); /// # } /// # } /// ``` pub fn stdout_stderr_swap(&self) -> Expression { Self::new(Io(StdoutStderrSwap, self.clone())) } /// Set the working directory where the expression will execute. /// /// Note that in some languages (Rust and Python at least), there are /// tricky platform differences in the way relative exe paths interact with /// child working directories. In particular, the exe path will be /// interpreted relative to the child dir on Unix, but relative to the /// parent dir on Windows. Duct prefers the Windows behavior, and in order /// to get that behavior on all platforms it calls /// [`std::fs::canonicalize`](https://doc.rust-lang.org/std/fs/fn.canonicalize.html) /// on relative exe paths when `dir` is in use. Paths in this sense are any /// program name containing a path separator, regardless of the type. (Note /// also that `Path` and `PathBuf` program names get a `./` prepended to /// them automatically by the /// [`IntoExecutablePath`](trait.IntoExecutablePath.html) trait, and so /// will always contain a separator.) /// /// # Errors /// /// Canonicalization can fail on some filesystems, or if the current /// directory has been removed, and /// [`run`](struct.Expression.html#method.run) will return those errors /// rather than trying any sneaky workarounds. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let output = cmd!("pwd").dir("/").read().unwrap(); /// assert_eq!("/", output); /// # } /// # } /// ``` pub fn dir>(&self, path: T) -> Expression { Self::new(Io(Dir(path.into()), self.clone())) } /// Set a variable in the expression's environment. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// let output = cmd!("sh", "-c", "echo $FOO").env("FOO", "bar").read().unwrap(); /// assert_eq!("bar", output); /// # } /// # } /// ``` pub fn env(&self, name: T, val: U) -> Expression where T: Into, U: Into, { Self::new(Io( Env(canonicalize_env_var_name(name.into()), val.into()), self.clone(), )) } /// Remove a variable from the expression's environment. /// /// Note that all the environment functions try to do whatever the platform /// does with respect to case sensitivity. That means that /// `env_remove("foo")` will unset the uppercase variable `FOO` on Windows, /// but not on Unix. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # if cfg!(not(windows)) { /// std::env::set_var("TESTING", "true"); /// let output = cmd!("sh", "-c", "echo a${TESTING}b") /// .env_remove("TESTING") /// .read() /// .unwrap(); /// assert_eq!("ab", output); /// # } /// # } /// ``` pub fn env_remove(&self, name: T) -> Expression where T: Into, { Self::new(Io( EnvRemove(canonicalize_env_var_name(name.into())), self.clone(), )) } /// Set the expression's entire environment, from a collection of /// name-value pairs (like a `HashMap`). Note that some environment /// variables are required for normal program execution (like `SystemRoot` /// on Windows), so copying the parent's environment is usually preferable /// to starting with an empty one. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// # use std::collections::HashMap; /// # if cfg!(not(windows)) { /// let mut env_map: HashMap<_, _> = std::env::vars().collect(); /// env_map.insert("FOO".into(), "bar".into()); /// let output = cmd!("sh", "-c", "echo $FOO").full_env(&env_map).read().unwrap(); /// assert_eq!("bar", output); /// // The IntoIterator/Into bounds are pretty flexible. Passing /// // by value works here too. /// let output = cmd!("sh", "-c", "echo $FOO").full_env(env_map).read().unwrap(); /// assert_eq!("bar", output); /// # } /// # } /// ``` pub fn full_env(&self, name_vals: T) -> Expression where T: IntoIterator, U: Into, V: Into, { let env_map = name_vals .into_iter() .map(|(k, v)| (canonicalize_env_var_name(k.into()), v.into())) .collect(); Self::new(Io(FullEnv(env_map), self.clone())) } /// Prevent a non-zero exit status from causing /// [`run`](struct.Expression.html#method.run) or /// [`read`](struct.Expression.html#method.read) to return an error. The /// unchecked exit code will still be there on the `Output` returned by /// `run`; its value doesn't change. /// /// "Uncheckedness" sticks to an exit code as it bubbles up through /// complicated pipelines, but it doesn't "infect" other exit codes. So for /// example, if only one sub-expression in a pipe has `unchecked`, then /// errors returned by the other side will still be checked. That said, /// most commonly you'll just call `unchecked` right before `run`, and /// it'll apply to an entire expression. /// /// # Example /// /// Note the differences among these three cases: /// /// ```no_run /// # use duct::cmd; /// # fn main() -> std::io::Result<()> { /// // Don't check errors on the left side. /// cmd!("foo").unchecked().pipe(cmd!("bar")).run()?; /// /// // Don't check errors on the right side. /// cmd!("foo").pipe(cmd!("bar").unchecked()).run()?; /// /// // Don't check errors on either side. /// cmd!("foo").pipe(cmd!("bar")).unchecked().run()?; /// # Ok(()) /// # } /// ``` pub fn unchecked(&self) -> Expression { Self::new(Io(Unchecked, self.clone())) } /// Add a hook for modifying /// [`std::process::Command`](https://doc.rust-lang.org/std/process/struct.Command.html) /// objects immediately before they're executed. /// /// The hook is called for each command in its sub-expression, and each time the expression is /// executed. The call happens after other features like `stdout` and `env` have been applied, /// so any changes made by the hook take priority. More than one hook can be added, in which /// case the innermost is executed last. For example, if one call to `before_spawn` is applied /// to an entire pipe expression, and another call is applied to just one command within the /// pipe, the hook for the entire pipeline will be called first over the command where both /// hooks apply. /// /// This is intended for rare and tricky cases, like callers who want to change the group ID of /// their child processes, or who want to run code in `before_exec`. Most callers shouldn't /// need to use it. /// /// # Example /// /// ``` /// # use duct::cmd; /// # fn main() { /// let output = cmd!("echo", "foo") /// .before_spawn(|cmd| { /// // Sneakily add an extra argument. /// cmd.arg("bar"); /// Ok(()) /// }) /// .read() /// .unwrap(); /// assert_eq!("foo bar", output); /// # } /// ``` pub fn before_spawn(&self, hook: F) -> Expression where F: Fn(&mut Command) -> io::Result<()> + Send + Sync + 'static, { Self::new(Io(BeforeSpawn(BeforeSpawnHook::new(hook)), self.clone())) } fn new(inner: ExpressionInner) -> Expression { Expression(Arc::new(inner)) } } // Delegate to the ExpressionInner for debug formatting. This avoids printing // redundant Expression() constructors around everything. impl fmt::Debug for Expression { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(f) } } // Implemening Into for references lets us accept both references // and values in `pipe`. impl<'a> From<&'a Expression> for Expression { fn from(expr: &Expression) -> Expression { expr.clone() } } /// A handle to a running expression, returned by the /// [`start`](struct.Expression.html#method.start) method. /// /// Calling `start` followed by /// [`into_output`](struct.Handle.html#method.into_output) on the handle is /// equivalent to [`run`](struct.Expression.html#method.run). Note that unlike /// [`std::process::Child`](https://doc.rust-lang.org/std/process/struct.Child.html), /// most of the methods on `Handle` take `&self` rather than `&mut self`, and a /// `Handle` may be shared between multiple threads. /// /// Like `std::process::Child`, `Handle` doesn't do anything special in its /// destructor. If you drop a handle without waiting on it, child processes and /// background IO threads will keep running, and the children will become /// [zombie processes](https://en.wikipedia.org/wiki/Zombie_process) when they /// exit. That's a resource leak, similar to leaking memory or file handles. /// Note that in contrast to `Handle`, a /// [`ReaderHandle`](struct.ReaderHandle.html) kills child processes in its /// destructor, to avoid creating zombies. /// /// See the [`shared_child`](https://github.com/oconnor663/shared_child.rs) /// crate for implementation details behind making handles thread safe. #[derive(Debug)] pub struct Handle { inner: HandleInner, result: OnceCell<(ExpressionStatus, Output)>, readers: Mutex<(Option, Option)>, } impl Handle { /// Wait for the running expression to finish, and return a reference to its /// [`std::process::Output`](https://doc.rust-lang.org/std/process/struct.Output.html). /// Multiple threads may wait at the same time. /// /// # Errors /// /// In addition to all the IO errors possible with /// [`std::process::Child`](https://doc.rust-lang.org/std/process/struct.Child.html), /// `wait` will return an /// [`ErrorKind::Other`](https://doc.rust-lang.org/std/io/enum.ErrorKind.html) /// IO error if child returns a non-zero exit status. To suppress this /// error and return an `Output` even when the exit status is non-zero, use /// the [`unchecked`](struct.Expression.html#method.unchecked) method. pub fn wait(&self) -> io::Result<&Output> { // Await the children and any threads that are reading their output. // Another caller may already have done this. let (expression_status, output) = wait_on_handle_and_ouput(self)?; // If the child returned a non-zero exit status, and that's a checked // error, return the error. if expression_status.is_checked_error() { return Err(io::Error::new( io::ErrorKind::Other, expression_status.message(), )); } Ok(output) } /// Check whether the running expression is finished. If it is, return a /// reference to its /// [`std::process::Output`](https://doc.rust-lang.org/std/process/struct.Output.html). /// If it's still running, return `Ok(None)`. /// /// # Errors /// /// In addition to all the IO errors possible with /// [`std::process::Child`](https://doc.rust-lang.org/std/process/struct.Child.html), /// `try_wait` will return an /// [`ErrorKind::Other`](https://doc.rust-lang.org/std/io/enum.ErrorKind.html) /// IO error if child returns a non-zero exit status. To suppress this /// error and return an `Output` even when the exit status is non-zero, use /// the [`unchecked`](struct.Expression.html#method.unchecked) method. pub fn try_wait(&self) -> io::Result> { if self.inner.wait(WaitMode::Nonblocking)?.is_none() { Ok(None) } else { self.wait().map(Some) } } /// Wait for the running expression to finish, and then return a /// [`std::process::Output`](https://doc.rust-lang.org/std/process/struct.Output.html) /// object containing the results, including any captured output. This /// consumes the `Handle`. Calling /// [`start`](struct.Expression.html#method.start) followed by /// `into_output` is equivalent to /// [`run`](struct.Expression.html#method.run). /// /// # Errors /// /// In addition to all the IO errors possible with /// [`std::process::Child`](https://doc.rust-lang.org/std/process/struct.Child.html), /// `into_output` will return an /// [`ErrorKind::Other`](https://doc.rust-lang.org/std/io/enum.ErrorKind.html) /// IO error if child returns a non-zero exit status. To suppress this /// error and return an `Output` even when the exit status is non-zero, use /// the [`unchecked`](struct.Expression.html#method.unchecked) method. pub fn into_output(self) -> io::Result { self.wait()?; let (_, output) = self.result.into_inner().expect("result missing"); Ok(output) } /// Kill the running expression and await all the child processes. Any /// errors that would normally result from a non-zero exit status are /// ignored, as with /// [`unchecked`](struct.Expression.html#method.unchecked). /// /// Note that as with /// [`std::process::Child::kill`](https://doc.rust-lang.org/beta/std/process/struct.Child.html#method.kill), /// this does not kill any grandchild processes that the children have /// spawned on their own. It only kills the child processes that Duct /// spawned itself. See /// [`gotchas.md`](https://github.com/oconnor663/duct.py/blob/master/gotchas.md) /// for an extensive discussion of this behavior. pub fn kill(&self) -> io::Result<()> { self.inner.kill()?; // This wait cleans up the child but does not return an error for a // non-zero exit status. // // Note that we *must not* call wait_on_handle_and_ouput here. There // might be un-signaled grandchild processes holding the output pipe, // and we can't expect them to exit promptly. We only want to reap our // immediate zombie children here. See gotchas.md for more on why we // can't do better. self.inner.wait(WaitMode::Blocking)?; Ok(()) } /// Return a `Vec` containing the PIDs of all of the child processes. /// The PIDs are given in pipeline order, from left to right. pub fn pids(&self) -> Vec { self.inner.pids() } } // Does a blocking wait on the handle, if it hasn't been awaited yet. This // includes collection the output results from reader threads. After calling // this function, the result cell is guaranteed to be populated. This does not // do any status checking. fn wait_on_handle_and_ouput(handle: &Handle) -> io::Result<&(ExpressionStatus, Output)> { // Take the reader threads lock and then see if a result has already been // collected. Doing this check inside the lock avoids racing to fill the // result if it's empty. let mut readers_lock = handle.readers.lock().expect("readers lock poisoned"); if let Some(result) = handle.result.get() { // This handle has already been waited on. Return the same result // again. Ok(result) } else { // This handle hasn't been waited on yet. Do that now. If waiting on // the children returns an error, just short-circuit with that. This // shouldn't really happen. let status = handle .inner .wait(WaitMode::Blocking)? .expect("blocking wait can't return None"); // Now that we have an exit status, we need to join the output reader // threads, if any. We're already holding the lock that we need. let (stdout_reader, stderr_reader) = &mut *readers_lock; // If either of the reader threads returned an error, just // short-circuit with that. Future calls to this function will panic. // But this really shouldn't happen. let stdout = stdout_reader .take() .map(|t| t.join().expect("stdout reader error")) .unwrap_or(Ok(Vec::new()))?; let stderr = stderr_reader .take() .map(|t| t.join().expect("stderr reader error")) .unwrap_or(Ok(Vec::new()))?; let output = Output { status: status.status, stdout, stderr, }; Ok(handle.result.get_or_init(|| (status, output))) } } #[derive(Debug)] enum ExpressionInner { Cmd(Vec), Pipe(Expression, Expression), Io(IoExpressionInner, Expression), } impl ExpressionInner { fn start(&self, context: IoContext) -> io::Result { Ok(match self { Cmd(argv) => HandleInner::Child(start_argv(argv, context)?), Pipe(left, right) => { HandleInner::Pipe(Box::new(PipeHandle::start(left, right, context)?)) } Io(io_inner, expr) => start_io(io_inner, expr, context)?, }) } } #[derive(Debug)] enum HandleInner { Child(ChildHandle), // If the left side of a pipe fails to start, there's nothing to wait for, // and we return an error immediately. But if the right side fails to start, // the caller still needs to wait on the left, and we must return a handle. // Thus the handle preserves the right side's errors here. Pipe(Box), StdinBytes(Box), Unchecked(Box), } impl HandleInner { fn wait(&self, mode: WaitMode) -> io::Result> { match self { HandleInner::Child(child_handle) => child_handle.wait(mode), HandleInner::Pipe(pipe_handle) => pipe_handle.wait(mode), HandleInner::StdinBytes(stdin_bytes_handle) => stdin_bytes_handle.wait(mode), HandleInner::Unchecked(inner_handle) => { Ok(inner_handle.wait(mode)?.map(|mut status| { status.checked = false; status })) } } } fn kill(&self) -> io::Result<()> { match self { HandleInner::Child(child_handle) => child_handle.kill(), HandleInner::Pipe(pipe_handle) => pipe_handle.kill(), HandleInner::StdinBytes(stdin_bytes_handle) => stdin_bytes_handle.kill(), HandleInner::Unchecked(inner_handle) => inner_handle.kill(), } } fn pids(&self) -> Vec { match self { HandleInner::Child(child_handle) => vec![child_handle.child.id()], HandleInner::Pipe(pipe_handle) => pipe_handle.pids(), HandleInner::StdinBytes(stdin_bytes_handle) => stdin_bytes_handle.inner_handle.pids(), HandleInner::Unchecked(inner_handle) => inner_handle.pids(), } } } fn start_argv(argv: &[OsString], context: IoContext) -> io::Result { let exe = canonicalize_exe_path_for_dir(&argv[0], &context)?; let mut command = Command::new(exe); command.args(&argv[1..]); // TODO: Avoid unnecessary dup'ing here. command.stdin(context.stdin.into_stdio()?); command.stdout(context.stdout.into_stdio()?); command.stderr(context.stderr.into_stdio()?); if let Some(dir) = context.dir { command.current_dir(dir); } command.env_clear(); for (name, val) in context.env { command.env(name, val); } // The innermost hooks are pushed last, and we execute them last. for hook in context.before_spawn_hooks.iter() { hook.call(&mut command)?; } let shared_child = SharedChild::spawn(&mut command)?; let command_string = format!("{:?}", argv); Ok(ChildHandle { child: shared_child, command_string: command_string, }) } #[derive(Debug)] struct ChildHandle { child: shared_child::SharedChild, command_string: String, } impl ChildHandle { fn wait(&self, mode: WaitMode) -> io::Result> { let maybe_status = match mode { WaitMode::Blocking => Some(self.child.wait()?), WaitMode::Nonblocking => self.child.try_wait()?, }; if let Some(status) = maybe_status { Ok(Some(ExpressionStatus { status: status, checked: true, command: self.command_string.clone(), })) } else { Ok(None) } } fn kill(&self) -> io::Result<()> { self.child.kill() } } #[derive(Debug)] struct PipeHandle { left_handle: HandleInner, right_handle: HandleInner, } impl PipeHandle { fn start(left: &Expression, right: &Expression, context: IoContext) -> io::Result { let (reader, writer) = os_pipe::pipe()?; // dup'ing stdin/stdout isn't strictly necessary, but no big deal let mut left_context = context.try_clone()?; left_context.stdout = IoValue::Handle(into_file(writer)); let mut right_context = context; right_context.stdin = IoValue::Handle(into_file(reader)); // Errors starting the left side just short-circuit us. let left_handle = left.0.start(left_context)?; // Now the left side is started. If the right side fails to start, we // can't let the left side turn into a zombie. We have to await it, and // that means we have to kill it first. let right_result = right.0.start(right_context); match right_result { Ok(right_handle) => Ok(PipeHandle { left_handle: left_handle, right_handle: right_handle, }), Err(e) => { // Realistically, kill should never return an error. If it // does, it's probably due to some bug in this library or one // of its dependencies. If that happens just propagate the // error and accept that we're probably leaking something. left_handle.kill()?; // Similarly, this private API wait should never return an // error. It might return a non-zero status, but here that's // still an Ok result. left_handle.wait(WaitMode::Blocking)?; Err(e) } } } fn wait(&self, mode: WaitMode) -> io::Result> { // Wait on both sides first, without propagating any errors. let left_wait_result = self.left_handle.wait(mode); let right_wait_result = self.right_handle.wait(mode); // Now we deal with errors from either of those waits. The left wait // happened first, so that one takes precedence. Note that this is the // reverse order of exit status precedence. let left_status = left_wait_result?; let right_status = right_wait_result?; // If both waits succeeded, return one of the two statuses. Ok(pipe_status_precedence(left_status, right_status)) } // As with wait, we need to call kill on both sides even if the left side // returns an error. fn kill(&self) -> io::Result<()> { let left_kill_result = self.left_handle.kill(); let right_kill_result = self.right_handle.kill(); // As with wait, the left side happened first, so its errors take // precedence. left_kill_result.and(right_kill_result) } fn pids(&self) -> Vec { let mut pids = self.left_handle.pids(); pids.extend_from_slice(&self.right_handle.pids()); pids } } // The rules of precedence are: // 1) If either side unfinished, the result is unfinished. // 2) Checked errors trump unchecked errors. // 3) Any errors trump success. // 4) All else equal, the right side wins. fn pipe_status_precedence( left_maybe_status: Option, right_maybe_status: Option, ) -> Option { let (left_status, right_status) = match (left_maybe_status, right_maybe_status) { (Some(left), Some(right)) => (left, right), _ => return None, }; Some(if right_status.is_checked_error() { right_status } else if left_status.is_checked_error() { left_status } else if !right_status.status.success() { right_status } else { left_status }) } fn start_io( io_inner: &IoExpressionInner, expr_inner: &Expression, mut context: IoContext, ) -> io::Result { match io_inner { StdinBytes(v) => { return Ok(HandleInner::StdinBytes(Box::new(StdinBytesHandle::start( expr_inner, context, Arc::clone(v), )?))); } StdinPath(p) => { context.stdin = IoValue::Handle(File::open(p)?); } StdinFile(f) => { context.stdin = IoValue::Handle(f.try_clone()?); } StdinNull => { context.stdin = IoValue::Null; } StdoutPath(p) => { context.stdout = IoValue::Handle(File::create(p)?); } StdoutFile(f) => { context.stdout = IoValue::Handle(f.try_clone()?); } StdoutNull => { context.stdout = IoValue::Null; } StdoutCapture => { context.stdout = IoValue::Handle(into_file(context.stdout_capture.write_pipe()?)); } StdoutToStderr => { context.stdout = context.stderr.try_clone()?; } StderrPath(p) => { context.stderr = IoValue::Handle(File::create(p)?); } StderrFile(f) => { context.stderr = IoValue::Handle(f.try_clone()?); } StderrNull => { context.stderr = IoValue::Null; } StderrCapture => { context.stderr = IoValue::Handle(into_file(context.stderr_capture.write_pipe()?)); } StderrToStdout => { context.stderr = context.stdout.try_clone()?; } StdoutStderrSwap => { mem::swap(&mut context.stdout, &mut context.stderr); } Dir(p) => { context.dir = Some(p.clone()); } Env(name, val) => { context.env.insert(name.clone(), val.clone()); } EnvRemove(name) => { context.env.remove(name); } FullEnv(map) => { context.env = map.clone(); } Unchecked => { let inner_handle = expr_inner.0.start(context)?; return Ok(HandleInner::Unchecked(Box::new(inner_handle))); } BeforeSpawn(hook) => { context.before_spawn_hooks.push(hook.clone()); } } expr_inner.0.start(context) } #[derive(Debug)] struct StdinBytesHandle { inner_handle: HandleInner, writer_thread: SharedThread>, } impl StdinBytesHandle { fn start( expression: &Expression, mut context: IoContext, input: Arc>, ) -> io::Result { let (reader, mut writer) = os_pipe::pipe()?; context.stdin = IoValue::Handle(into_file(reader)); let inner = expression.0.start(context)?; // We only spawn the writer thread if the expression started // successfully, so that start errors won't leak a zombie thread. let thread = std::thread::spawn(move || writer.write_all(&input)); Ok(StdinBytesHandle { inner_handle: inner, writer_thread: SharedThread::new(thread), }) } fn wait(&self, mode: WaitMode) -> io::Result> { // We're responsible for joining the writer thread and not leaving a zombie. // But waiting on the inner child can return an error, and in that case we // don't know whether the child is still running or not. The rule in // nonblocking mode is "clean up as much as we can, but never block," so we // can't wait on the writer thread. But the rule in blocking mode is "clean // up everything, even if some cleanup returns errors," so we must wait // regardless of what's going on with the child. let wait_res = self.inner_handle.wait(mode); if mode.should_join_background_thread(&wait_res) { // Join the writer thread. Broken pipe errors here are expected if // the child exited without reading all of its input, so we suppress // them. Return other errors though. match self.writer_thread.join() { Err(err) if err.kind() != io::ErrorKind::BrokenPipe => { return Err(clone_io_error(err)); } _ => {} } } wait_res } fn kill(&self) -> io::Result<()> { self.inner_handle.kill() } } #[derive(Debug)] enum IoExpressionInner { StdinBytes(Arc>), StdinPath(PathBuf), StdinFile(File), StdinNull, StdoutPath(PathBuf), StdoutFile(File), StdoutNull, StdoutCapture, StdoutToStderr, StderrPath(PathBuf), StderrFile(File), StderrNull, StderrCapture, StderrToStdout, StdoutStderrSwap, Dir(PathBuf), Env(OsString, OsString), EnvRemove(OsString), FullEnv(HashMap), Unchecked, BeforeSpawn(BeforeSpawnHook), } #[derive(Clone)] struct BeforeSpawnHook { inner: Arc io::Result<()> + Send + Sync>, } impl BeforeSpawnHook { fn new(hook: F) -> Self where F: Fn(&mut Command) -> io::Result<()> + Send + Sync + 'static, { Self { inner: Arc::new(hook), } } fn call(&self, command: &mut Command) -> io::Result<()> { (self.inner)(command) } } impl fmt::Debug for BeforeSpawnHook { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "") } } // An IoContext represents the file descriptors child processes are talking to at execution time. // It's initialized in run(), with dups of the stdin/stdout/stderr pipes, and then passed down to // sub-expressions. Compound expressions will clone() it, and redirections will modify it. #[derive(Debug)] struct IoContext<'a> { stdin: IoValue, stdout: IoValue, stderr: IoValue, stdout_capture: &'a OutputCaptureContext, stderr_capture: &'a OutputCaptureContext, dir: Option, env: HashMap, before_spawn_hooks: Vec, } impl<'a> IoContext<'a> { // Returns (context, stdout_reader, stderr_reader). fn new( stdout_capture: &'a OutputCaptureContext, stderr_capture: &'a OutputCaptureContext, ) -> Self { Self { stdin: IoValue::ParentStdin, stdout: IoValue::ParentStdout, stderr: IoValue::ParentStderr, stdout_capture, stderr_capture, dir: None, env: std::env::vars_os().collect(), before_spawn_hooks: Vec::new(), } } fn try_clone(&self) -> io::Result> { Ok(IoContext { stdin: self.stdin.try_clone()?, stdout: self.stdout.try_clone()?, stderr: self.stderr.try_clone()?, stdout_capture: self.stdout_capture, stderr_capture: self.stderr_capture, dir: self.dir.clone(), env: self.env.clone(), before_spawn_hooks: self.before_spawn_hooks.clone(), }) } } #[derive(Debug)] enum IoValue { ParentStdin, ParentStdout, ParentStderr, Null, // We store all handles as File, even when they're e.g. anonymous pipes, // using the into_file() conversion below. The File type is a very thin // wrapper around the raw handle, but it gives us try_clone() and drop(). Handle(File), } impl IoValue { fn try_clone(&self) -> io::Result { Ok(match self { IoValue::ParentStdin => IoValue::ParentStdin, IoValue::ParentStdout => IoValue::ParentStdout, IoValue::ParentStderr => IoValue::ParentStderr, IoValue::Null => IoValue::Null, IoValue::Handle(f) => IoValue::Handle(f.try_clone()?), }) } fn into_stdio(self) -> io::Result { Ok(match self { IoValue::ParentStdin => os_pipe::dup_stdin()?.into(), IoValue::ParentStdout => os_pipe::dup_stdout()?.into(), IoValue::ParentStderr => os_pipe::dup_stderr()?.into(), IoValue::Null => Stdio::null(), IoValue::Handle(f) => f.into(), }) } } // We would rather convert an fd-owning object directly into a // std::process::Stdio, since all you can do with that is give it to a // std::process::Command. Unfortunately, Stdio doesn't provide a try_clone // method, and we need that in order to pass the object to multiple children. // As a workaround, convert the object to a std::fs::File. All we will use this // File for is try_clone and Into, which should be sound on any type of // descriptor. (Some types will lead to an error, like a TcpStream, but that's // not unsound.) If we discover any unsound cases, we might have to replace // this with a new trait. #[cfg(not(windows))] fn into_file(handle: T) -> File { unsafe { File::from_raw_fd(handle.into_raw_fd()) } } #[cfg(windows)] fn into_file(handle: T) -> File { unsafe { File::from_raw_handle(handle.into_raw_handle()) } } // This struct keeps track of a child exit status, whether or not it's been // unchecked(), and what the command was that gave it (for error messages). #[derive(Clone, Debug)] struct ExpressionStatus { status: ExitStatus, checked: bool, command: String, } impl ExpressionStatus { fn is_checked_error(&self) -> bool { self.checked && !self.status.success() } fn message(&self) -> String { format!( "command {} exited with code {}", self.command, self.exit_code_string() ) } #[cfg(not(windows))] fn exit_code_string(&self) -> String { if self.status.code().is_none() { return format!("", self.status.signal().unwrap()); } self.status.code().unwrap().to_string() } #[cfg(windows)] fn exit_code_string(&self) -> String { self.status.code().unwrap().to_string() } } fn canonicalize_exe_path_for_dir(exe_name: &OsStr, context: &IoContext) -> io::Result { // There's a tricky interaction between exe paths and `dir`. Exe paths can // be relative, and so we have to ask: Is an exe path interpreted relative // to the parent's cwd, or the child's? The answer is that it's platform // dependent! >.< (Windows uses the parent's cwd, but because of the // fork-chdir-exec pattern, Unix usually uses the child's.) // // We want to use the parent's cwd consistently, because that saves the // caller from having to worry about whether `dir` will have side effects, // and because it's easy for the caller to use Path::join if they want to. // That means that when `dir` is in use, we need to detect exe names that // are relative paths, and absolutify them. We want to do that as little as // possible though, both because canonicalization can fail, and because we // prefer to let the caller control the child's argv[0]. // // We never want to absolutify a name like "emacs", because that's probably // a program in the PATH rather than a local file. So we look for slashes // in the name to determine what's a filepath and what isn't. Note that // anything given as a std::path::Path will always have a slash by the time // we get here, because we specialize the IntoExecutablePath trait to // prepend a ./ to them when they're relative. This leaves the case where // Windows users might pass a local file like "foo.bat" as a string, which // we can't distinguish from a global program name. However, because the // Windows has the preferred "relative to parent's cwd" behavior already, // this case actually works without our help. (The thing Windows users have // to watch out for instead is local files shadowing global program names, // which I don't think we can or should prevent.) let has_separator = exe_name .to_string_lossy() .chars() .any(std::path::is_separator); let is_relative = Path::new(exe_name).is_relative(); if context.dir.is_some() && has_separator && is_relative { Path::new(exe_name).canonicalize().map(Into::into) } else { Ok(exe_name.to_owned()) } } // We want to allow Path("foo") to refer to the local file "./foo" on // Unix, and we want to *prevent* Path("echo") from referring to the // global "echo" command on either Unix or Windows. Prepend a dot to all // relative paths to accomplish both of those. fn dotify_relative_exe_path(path: &Path) -> PathBuf { // This is a no-op if path is absolute or begins with a Windows prefix. Path::new(".").join(path) } /// An implementation detail of [`cmd`](fn.cmd.html), to distinguish paths from /// other string types. /// /// `Path("foo.sh")` means the file named `foo.sh` in the current directory. /// However if you try to execute that path with /// [`std::process::Command`](https://doc.rust-lang.org/std/process/struct.Command.html), /// Unix will get upset that it doesn't have a leading `./`. Rust knows that the /// string is a path, but that distinction gets lost by the time execution /// happens. /// /// To execute relative paths correctly, duct prepends the `./` to them /// automatically. This trait captures the distinction between the path types /// and other types of strings, which don't get modified. See the trait bounds /// on [`cmd`](fn.cmd.html). pub trait IntoExecutablePath { fn to_executable(self) -> OsString; } // TODO: Get rid of most of these impls once specialization lands. impl<'a> IntoExecutablePath for &'a Path { fn to_executable(self) -> OsString { dotify_relative_exe_path(self).into() } } impl IntoExecutablePath for PathBuf { fn to_executable(self) -> OsString { dotify_relative_exe_path(&self).into() } } impl<'a> IntoExecutablePath for &'a PathBuf { fn to_executable(self) -> OsString { dotify_relative_exe_path(self).into() } } impl<'a> IntoExecutablePath for &'a str { fn to_executable(self) -> OsString { self.into() } } impl IntoExecutablePath for String { fn to_executable(self) -> OsString { self.into() } } impl<'a> IntoExecutablePath for &'a String { fn to_executable(self) -> OsString { self.into() } } impl<'a> IntoExecutablePath for &'a OsStr { fn to_executable(self) -> OsString { self.into() } } impl IntoExecutablePath for OsString { fn to_executable(self) -> OsString { self } } impl<'a> IntoExecutablePath for &'a OsString { fn to_executable(self) -> OsString { self.into() } } // io::Error doesn't implement clone directly, so we kind of hack it together. fn clone_io_error(error: &io::Error) -> io::Error { if let Some(code) = error.raw_os_error() { io::Error::from_raw_os_error(code) } else { io::Error::new(error.kind(), error.to_string()) } } #[derive(Debug)] struct SharedThread { result: OnceCell, handle: Mutex>>, } // A thread that sticks its result in a lazy cell, so that multiple callers can see it. impl SharedThread { fn new(handle: JoinHandle) -> Self { SharedThread { result: OnceCell::new(), handle: Mutex::new(Some(handle)), } } // If the other thread panicked, this will panic. fn join(&self) -> &T { let mut handle_lock = self.handle.lock().expect("shared thread handle poisoned"); if let Some(handle) = handle_lock.take() { let ret = handle.join().expect("panic on shared thread"); self.result .set(ret) .map_err(|_| "result cell unexpectedly full") .unwrap(); } self.result.get().expect("result cell unexpectedly empty") } } #[derive(Clone, Copy, Debug)] enum WaitMode { Blocking, Nonblocking, } impl WaitMode { fn should_join_background_thread( &self, expression_result: &io::Result>, ) -> bool { // Nonblocking waits can only join associated background threads if the // running expression is finished (that is, when the thread is // guaranteed to finish soon). Blocking waits should always join, even // in the presence of errors. if let WaitMode::Blocking = self { true } else if let Ok(Some(_)) = expression_result { true } else { false } } } #[cfg(windows)] fn canonicalize_env_var_name(name: OsString) -> OsString { // On Windows, because env vars are case-insensitive, we uppercase all env // var names. That makes assignments and deletions in our internal map work // the same way they would on the real environment. match name.into_string() { Ok(name) => name.to_uppercase().into(), // If the name isn't valid Unicode then just leave it as is. Err(name) => name, } } #[cfg(not(windows))] fn canonicalize_env_var_name(name: OsString) -> OsString { // No-op on all other platforms. name } type ReaderThread = JoinHandle>>; #[derive(Debug)] struct OutputCaptureContext { pair: OnceCell<(os_pipe::PipeReader, os_pipe::PipeWriter)>, } impl OutputCaptureContext { fn new() -> Self { Self { pair: OnceCell::new(), } } fn write_pipe(&self) -> io::Result { let (_, writer) = self.pair.get_or_try_init(|| os_pipe::pipe())?; writer.try_clone() } // Only spawn a read thread if the write pipe was used. fn maybe_read_thread(self) -> Option { if let Some((mut reader, _)) = self.pair.into_inner() { Some(std::thread::spawn(move || { let mut output = Vec::new(); reader.read_to_end(&mut output)?; Ok(output) })) } else { None } } } /// An incremental reader created with the /// [`Expression::reader`](struct.Expression.html#method.reader) method. /// /// When this reader reaches EOF, it automatically calls /// [`wait`](struct.Handle.html#method.wait) on the inner handle. If the child /// returns a non-zero exit status, the read at EOF will return an error, /// unless you use [`unchecked`](struct.Expression.html#method.unchecked). /// /// If the reader is dropped before reaching EOF, it calls /// [`kill`](struct.ReaderHandle.html#method.kill) in its destructor. /// /// Both `ReaderHandle` and `&ReaderHandle` implement /// [`std::io::Read`](https://doc.rust-lang.org/std/io/trait.Read.html). That /// makes it possible for one thread to /// [`kill`](struct.ReaderHandle.html#method.kill) the `ReaderHandle` while /// another thread is reading it. That can be useful for effectively canceling /// the read and unblocking the reader thread. However, note that killed child /// processes return a non-zero exit status, which is an error for the reader /// by default, unless you use /// [`unchecked`](struct.Expression.html#method.unchecked). /// /// # Example /// /// ``` /// # fn main() -> Result<(), Box> { /// # if cfg!(not(windows)) { /// use duct::cmd; /// use duct::ReaderHandle; /// use std::sync::Arc; /// use std::io::prelude::*; /// /// // This child process prints a single byte and then sleeps. /// // /// // CAUTION: Using Bash for this example would probably hang, because Bash /// // would spawn a `sleep` grandchild processes, and that grandchild wouldn't /// // receive the kill signal. /// let python_child = "\ /// import sys /// import time /// print() /// sys.stdout.flush() /// time.sleep(24 * 60 * 60) /// "; /// let reader: ReaderHandle = cmd!("python3", "-c", python_child) /// .unchecked() /// .reader()?; /// /// // Spawn two threads that both try to read the single byte. Whichever one /// // succeeds then calls kill() to unblock the other. /// let arc_reader: Arc = Arc::new(reader); /// let mut threads = Vec::new(); /// for _ in 0..2 { /// let arc_reader = arc_reader.clone(); /// threads.push(std::thread::spawn(move || -> std::io::Result<()> { /// let mut single_byte = [0u8]; /// (&*arc_reader).read(&mut single_byte)?; /// arc_reader.kill()?; /// Ok(()) /// })); /// } /// /// // Join both threads. Because of the kill() above, both threads will exit /// // quickly. /// for thread in threads { /// thread.join().unwrap()?; /// } /// # } /// # Ok(()) /// # } /// ``` #[derive(Debug)] pub struct ReaderHandle { handle: Handle, reader: os_pipe::PipeReader, } impl ReaderHandle { /// Check whether the underlying expression is finished. This is equivalent /// to [`Handle::try_wait`](struct.Handle.html#method.try_wait). If the /// `ReaderHandle` has indicated EOF successfully, then it's guaranteed /// that this method will return `Ok(Some(_))`. /// /// Note that the /// [`stdout`](https://doc.rust-lang.org/std/process/struct.Output.html#structfield.stdout) /// field of the returned /// [`Output`](https://doc.rust-lang.org/std/process/struct.Output.html) /// will always be empty, because the `ReaderHandle` itself owns the /// child's stdout pipe. pub fn try_wait(&self) -> io::Result> { self.handle.try_wait() } /// Kill the underlying expression and await all the child processes. /// /// Any errors that would normally result from a non-zero exit status are /// ignored during this wait, as with /// [`Handle::kill`](struct.Handle.html#method.kill). /// /// Note that as with /// [`std::process::Child::kill`](https://doc.rust-lang.org/beta/std/process/struct.Child.html#method.kill), /// this does not kill any grandchild processes that the children have /// spawned on their own. It only kills the child processes that Duct /// spawned itself. This is **especially relevant** for `ReaderHandle`, /// because if you're using `kill` to unblock another thread that's /// reading, an unkilled grandchild process might keep the child's stdout /// pipe open and keep your reader thread blocked. For that use case, you /// need to ensure that any grandchild processes your child might spawn are /// going to be short-lived. See /// [`gotchas.md`](https://github.com/oconnor663/duct.py/blob/master/gotchas.md) /// for an extensive discussion of these issues. pub fn kill(&self) -> io::Result<()> { self.handle.kill() } /// Return a `Vec` containing the PIDs of all of the child processes. /// The PIDs are given in pipeline order, from left to right. pub fn pids(&self) -> Vec { self.handle.pids() } } impl<'a> Read for &'a ReaderHandle { /// Note that if you don't use /// [`unchecked`](struct.Expression.html#method.unchecked), and the child /// returns a non-zero exit status, the final call to `read` will return an /// error, just as [`run`](struct.Expression.html#method.run) would. fn read(&mut self, buf: &mut [u8]) -> io::Result { let n = (&self.reader).read(buf)?; if n == 0 && buf.len() > 0 { // EOF detected. Wait on the child to clean it up before returning. self.handle.wait()?; } Ok(n) } } impl Read for ReaderHandle { /// Note that if you don't use /// [`unchecked`](struct.Expression.html#method.unchecked), and the child /// returns a non-zero exit status, the final call to `read` will return an /// error, just as [`run`](struct.Expression.html#method.run) would. fn read(&mut self, buf: &mut [u8]) -> io::Result { (&*self).read(buf) } } impl Drop for ReaderHandle { fn drop(&mut self) { // Just call kill() unconditionally. If wait() has already happened, // this has no effect. let _ = self.handle.kill(); } } #[cfg(test)] mod test; duct-0.13.5/src/test.rs010064400017500001750000000463601376151060700130530ustar 00000000000000use super::{cmd, Expression}; use std; use std::collections::HashMap; use std::env; use std::env::consts::EXE_EXTENSION; use std::ffi::OsString; use std::fs::File; use std::io; use std::io::prelude::*; use std::path::{Path, PathBuf}; use std::process::Command; use std::str; use std::sync::{Arc, Once}; use tempdir::TempDir; // Include a copy of the sh function, because we have a lot of old tests that // use it, and it's a lot easier than managing a circular dependency between // duct and duct_sh. pub fn sh(command: &'static str) -> Expression { let argv = shell_command_argv(command.into()); cmd(&argv[0], &argv[1..]) } #[cfg(unix)] fn shell_command_argv(command: OsString) -> Vec { vec!["/bin/sh".into(), "-c".into(), command] } #[cfg(windows)] fn shell_command_argv(command: OsString) -> Vec { let comspec = std::env::var_os("COMSPEC").unwrap_or_else(|| "cmd.exe".into()); vec![comspec, "/C".into(), command] } pub fn path_to_exe(name: &str) -> PathBuf { // This project defines some associated binaries for testing, and we shell out to them in // these tests. `cargo test` doesn't automatically build associated binaries, so this // function takes care of building them explicitly. static CARGO_BUILD_ONCE: Once = Once::new(); CARGO_BUILD_ONCE.call_once(|| { let build_status = Command::new("cargo") .arg("build") .arg("--quiet") .status() .unwrap(); assert!( build_status.success(), "Cargo failed to build associated binaries." ); }); Path::new("target") .join("debug") .join(name) .with_extension(EXE_EXTENSION) } pub fn true_cmd() -> Expression { cmd!(path_to_exe("status"), "0") } fn false_cmd() -> Expression { cmd!(path_to_exe("status"), "1") } #[test] fn test_cmd() { let output = cmd!(path_to_exe("echo"), "hi").read().unwrap(); assert_eq!("hi", output); } #[test] fn test_sh() { // Windows compatible. let output = sh("echo hi").read().unwrap(); assert_eq!("hi", output); } #[test] fn test_start() { let handle1 = cmd!(path_to_exe("echo"), "hi") .stdout_capture() .start() .unwrap(); let handle2 = cmd!(path_to_exe("echo"), "lo") .stdout_capture() .start() .unwrap(); let output1 = handle1.wait().unwrap(); let output2 = handle2.wait().unwrap(); assert_eq!("hi", str::from_utf8(&output1.stdout).unwrap().trim()); assert_eq!("lo", str::from_utf8(&output2.stdout).unwrap().trim()); } #[test] fn test_error() { let result = false_cmd().run(); if let Err(err) = result { assert_eq!(err.kind(), io::ErrorKind::Other); } else { panic!("Expected a status error."); } } #[test] fn test_unchecked() { let unchecked_false = false_cmd().unchecked(); // Unchecked errors shouldn't cause `run` to return an error. let output = unchecked_false .pipe(cmd!(path_to_exe("echo"), "waa")) .stdout_capture() .run() .unwrap(); // The value of the exit code is preserved. assert_eq!(1, output.status.code().unwrap()); assert_eq!("waa", String::from_utf8_lossy(&output.stdout).trim()); } #[test] fn test_unchecked_in_pipe() { let zero = cmd!(path_to_exe("status"), "0"); let one = cmd!(path_to_exe("status"), "1"); let two = cmd!(path_to_exe("status"), "2"); // Right takes precedence over left. let output = one.pipe(two.clone()).unchecked().run().unwrap(); assert_eq!(2, output.status.code().unwrap()); // Except that checked on the left takes precedence over unchecked on // the right. let output = one.pipe(two.unchecked()).unchecked().run().unwrap(); assert_eq!(1, output.status.code().unwrap()); // Right takes precedence over the left again if they're both unchecked. let output = one .unchecked() .pipe(two.unchecked()) .unchecked() .run() .unwrap(); assert_eq!(2, output.status.code().unwrap()); // Except that if the right is a success, the left takes precedence. let output = one .unchecked() .pipe(zero.unchecked()) .unchecked() .run() .unwrap(); assert_eq!(1, output.status.code().unwrap()); // Even if the right is checked. let output = one.unchecked().pipe(zero).unchecked().run().unwrap(); assert_eq!(1, output.status.code().unwrap()); } #[test] fn test_pipe() { let output = sh("echo xxx") .pipe(cmd!(path_to_exe("x_to_y"))) .read() .unwrap(); assert_eq!("yyy", output); // Check that errors on either side are propagated. let result = true_cmd().pipe(false_cmd()).run(); assert!(result.is_err()); let result = false_cmd().pipe(true_cmd()).run(); assert!(result.is_err()); } #[test] fn test_pipe_with_kill() { // Make sure both sides get killed. let sleep_cmd = cmd!(path_to_exe("sleep"), "1000000"); // Note that we don't use unchecked() here. This tests that kill suppresses // exit status errors. let handle = sleep_cmd.pipe(sleep_cmd.clone()).start().unwrap(); handle.kill().unwrap(); // But calling wait again should be an error, because of the status. handle.wait().unwrap_err(); } #[test] fn test_pipe_start() { let nonexistent_cmd = cmd!(path_to_exe("nonexistent!!!")); let sleep_cmd = cmd!(path_to_exe("sleep"), "1000000"); // Errors starting the left side of a pipe are returned immediately, and // the right side is never started. nonexistent_cmd.pipe(&sleep_cmd).start().unwrap_err(); // Errors starting the right side are also returned immediately, and the // the left side is killed first. sleep_cmd.pipe(nonexistent_cmd).start().unwrap_err(); } #[test] fn test_multiple_threads() { // Wait on the sleep command in a background thread, while the main thread // kills it. let sleep_cmd = cmd!(path_to_exe("sleep"), "1000000"); let handle = Arc::new(sleep_cmd.unchecked().start().unwrap()); let arc_clone = handle.clone(); let wait_thread = std::thread::spawn(move || { arc_clone.wait().unwrap(); }); handle.kill().unwrap(); wait_thread.join().unwrap(); } #[test] fn test_nonblocking_waits() { let sleep_cmd = cmd!(path_to_exe("sleep"), "1000000"); // Make sure pipelines handle try_wait correctly. let handle = sleep_cmd.pipe(&sleep_cmd).unchecked().start().unwrap(); // Make sure try_wait doesn't block on it. assert!(handle.try_wait().unwrap().is_none()); handle.kill().unwrap(); } #[test] fn test_input() { let expr = cmd!(path_to_exe("x_to_y")).stdin_bytes("xxx"); let output = expr.read().unwrap(); assert_eq!("yyy", output); } #[test] fn test_stderr() { let (mut reader, writer) = ::os_pipe::pipe().unwrap(); sh("echo hi>&2").stderr_file(writer).run().unwrap(); let mut s = String::new(); reader.read_to_string(&mut s).unwrap(); assert_eq!(s.trim(), "hi"); } #[test] fn test_null() { let expr = cmd!(path_to_exe("cat")) .stdin_null() .stdout_null() .stderr_null(); let output = expr.read().unwrap(); assert_eq!("", output); } #[test] fn test_path() { let dir = TempDir::new("test_path").unwrap(); let input_file = dir.path().join("input_file"); let output_file = dir.path().join("output_file"); File::create(&input_file) .unwrap() .write_all(b"xxx") .unwrap(); let expr = cmd!(path_to_exe("x_to_y")) .stdin_path(&input_file) .stdout_path(&output_file); let output = expr.read().unwrap(); assert_eq!("", output); let mut file_output = String::new(); File::open(&output_file) .unwrap() .read_to_string(&mut file_output) .unwrap(); assert_eq!("yyy", file_output); } #[test] fn test_swapping() { let output = sh("echo hi") .stdout_to_stderr() .stderr_capture() .run() .unwrap(); let stderr = str::from_utf8(&output.stderr).unwrap().trim(); assert_eq!("hi", stderr); // Windows compatible. (Requires no space before the ">".) let output = sh("echo hi>&2").stderr_to_stdout().read().unwrap(); assert_eq!("hi", output); } #[test] fn test_file() { let dir = TempDir::new("test_file").unwrap(); let file = dir.path().join("file"); File::create(&file).unwrap().write_all(b"example").unwrap(); let expr = cmd!(path_to_exe("cat")).stdin_file(File::open(&file).unwrap()); let output = expr.read().unwrap(); assert_eq!(output, "example"); } #[test] fn test_ergonomics() { let mystr = "owned string".to_owned(); let mypathbuf = Path::new("a/b/c").to_owned(); let myvec = vec![1, 2, 3]; // These are nonsense expressions. We just want to make sure they compile. let _ = sh("true") .stdin_path(&*mystr) .stdin_bytes(&*myvec) .stdout_path(&*mypathbuf); let _ = sh("true") .stdin_path(mystr) .stdin_bytes(myvec) .stdout_path(mypathbuf); // Unfortunately, this one doesn't work with our Into> bound on input(). // TODO: Is it worth having these impls for &Vec in other cases? // let _ = sh("true").stdin_path(&mystr).stdin_bytes(&myvec).stdout_path(&mypathbuf); } #[test] fn test_capture_both() { // Windows compatible, no space before ">", and we trim newlines at the end to avoid // dealing with the different kinds. let output = sh("echo hi && echo lo>&2") .stdout_capture() .stderr_capture() .run() .unwrap(); assert_eq!("hi", str::from_utf8(&output.stdout).unwrap().trim()); assert_eq!("lo", str::from_utf8(&output.stderr).unwrap().trim()); } #[test] fn test_dir() { // This test checks the interaction of `dir` and relative exe paths. // Make sure that's actually what we're testing. let pwd_path = path_to_exe("pwd"); assert!(pwd_path.is_relative()); let pwd = cmd!(pwd_path); // First assert that ordinary commands happen in the parent's dir. let pwd_output = pwd.read().unwrap(); let pwd_path = Path::new(&pwd_output); assert_eq!(pwd_path, env::current_dir().unwrap()); // Now create a temp dir and make sure we can set dir to it. This // also tests the interaction of `dir` and relative exe paths. let dir = TempDir::new("duct_test").unwrap(); let pwd_output = pwd.dir(dir.path()).read().unwrap(); let pwd_path = Path::new(&pwd_output); // pwd_path isn't totally canonical on Windows, because it // doesn't have a prefix. Thus we have to canonicalize both // sides. (This also handles symlinks in TMP_DIR.) assert_eq!( pwd_path.canonicalize().unwrap(), dir.path().canonicalize().unwrap() ); } #[test] fn test_env() { let output = cmd!(path_to_exe("print_env"), "foo") .env("foo", "bar") .read() .unwrap(); assert_eq!("bar", output); } #[test] fn test_full_env() { // Note that it's important that no other tests use this variable name, // because the test runner is multithreaded. let var_name = "TEST_FULL_ENV"; // Capture the parent env, and make sure it does *not* contain our variable. let clean_env: HashMap = env::vars().collect(); assert!( !clean_env.contains_key(var_name), "why is this variable set?" ); // Run a child process with that map passed to full_env(). It should be guaranteed not to // see our variable, regardless of any outer env() calls or changes in the parent. let clean_child = cmd!(path_to_exe("print_env"), var_name).full_env(clean_env); // Dirty the parent env. Should be suppressed. env::set_var(var_name, "junk1"); // And make an outer env() call. Should also be suppressed. let dirty_child = clean_child.env(var_name, "junk2"); // Check that neither of those have any effect. let output = dirty_child.read().unwrap(); assert_eq!("", output); } #[test] fn test_env_remove() { // Set an environment variable in the parent. Note that it's important that // no other tests use this variable name, because the test runner is // multithreaded. let var_name = "TEST_ENV_REMOVE"; env::set_var(var_name, "junk2"); // Run a command that observes the variable. let output1 = cmd!(path_to_exe("print_env"), var_name).read().unwrap(); assert_eq!("junk2", output1); // Run the same command with that variable removed. let output2 = cmd!(path_to_exe("print_env"), var_name) .env_remove(var_name) .read() .unwrap(); assert_eq!("", output2); } #[test] fn test_env_remove_case_sensitivity() { // Env var deletion is particularly sensitive to the differences in // case-sensitivity between Unix and Windows. The semantics of env_remove // in duct must *match the platform*. // Set an environment variable in the parent. Note that it's important that // no other tests use this variable name, because the test runner is // multithreaded. let var_name = "TEST_ENV_REMOVE_CASE_SENSITIVITY"; env::set_var(var_name, "abc123"); // Run a command that tries to clear the same variable, but in lowercase. let output1 = cmd!(path_to_exe("print_env"), var_name) .env_remove(var_name.to_lowercase()) .read() .unwrap(); // Now try to clear that variable from the parent environment, again using // lowercase, and run the same command without `env_remove`. env::remove_var(var_name.to_lowercase()); let output2 = cmd!(path_to_exe("print_env"), var_name).read().unwrap(); // On Unix, env vars are case sensitive, and we don't expect either removal // to have any effect. On Windows, they're insensitive, and we expect both // removals to work. The key thing is that both approaches to removal have // the *same effect*. assert_eq!(output1, output2, "failed to match platform behavior!!!"); // Go ahead and assert the exact expected output, just in case. If these // assertions ever break, it might be this test's fault and not the code's. if cfg!(windows) { assert_eq!(output1, ""); } else { assert_eq!(output1, "abc123"); } } #[test] fn test_broken_pipe() { // If the input writing thread fills up its pipe buffer, writing will block. If the process // on the other end of the pipe exits while writer is waiting, the write will return an // error. We need to swallow that error, rather than returning it. let myvec = vec![0; 1_000_000]; true_cmd().stdin_bytes(myvec).run().unwrap(); } #[test] fn test_silly() { // A silly test, purely for coverage. crate::IoValue::Null.try_clone().unwrap(); } #[test] fn test_path_sanitization() { // We don't do any chdir'ing in this process, because the tests runner is multithreaded, // and we don't want to screw up anyone else's relative paths. Instead, we shell out to a // small test process that does that for us. cmd!(path_to_exe("exe_in_dir"), path_to_exe("status"), "0") .run() .unwrap(); } #[test] fn test_before_spawn_hook() { let (reader, mut writer) = os_pipe::pipe().unwrap(); let expr = cmd!(path_to_exe("cat")).before_spawn(move |cmd| { let reader_clone = reader.try_clone()?; cmd.stdin(reader_clone); Ok(()) }); writer.write_all(b"foobar").unwrap(); drop(writer); let output = expr.read().unwrap(); assert_eq!("foobar", output); } #[test] fn test_trailing_comma() { let output = cmd!(path_to_exe("echo"), "trailing",).read().unwrap(); assert_eq!("trailing", output); } #[test] fn test_no_argument() { let output = cmd!(path_to_exe("echo")).read().unwrap(); assert_eq!("", output); } #[test] fn test_dropping_reader() { // Use an explicit stderr pipe to test the ReaderHandle's drop behavior. let (mut stderr_reader, stderr_writer) = os_pipe::pipe().unwrap(); let mut reader_handle = cmd!(path_to_exe("sleep"), "1000000") .stdout_file(stderr_writer) .reader() .unwrap(); // A zero-length read doesn't block. let n = reader_handle.read(&mut []).unwrap(); assert_eq!(n, 0); // Try-wait returns None. let output = reader_handle.try_wait().unwrap(); assert!(output.is_none()); // Now we drop the reader. This kills the child. drop(reader_handle); // Now that the child is killed, reading the stderr pipe will not block. // (Note that our copy was closed when the temporary Expression above // dropped.) let mut stderr = Vec::new(); let n = stderr_reader.read_to_end(&mut stderr).unwrap(); assert_eq!(n, 0); } #[test] fn test_kill_with_grandchild() -> io::Result<()> { // We're going to start a child process, and that child is going to start a // grandchild. The grandchild is going to sleep forever (1 day). We'll read // some output from the child to make sure it's done starting the // grandchild, and then we'll kill the child. Now, the grandchild will not // be killed, and it will still hold a write handle to the stdout pipe. So // this tests that the wait done by kill only waits on the child to exit, // and does not wait on IO to finish. // // This test leaks the grandchild process. I'm sorry. // Capturing stderr means an IO thread is spawned, even though we're using // a ReaderHandle to read stdout. What we're testing here is that kill() // doesn't wait on that IO thread. let mut reader = cmd!(path_to_exe("child_grandchild")) .stderr_capture() .reader()?; // Read "started" from the child to make sure we don't kill it before it // starts the grandchild. let mut started_read = [0; 7]; reader.read_exact(&mut started_read)?; assert_eq!(&started_read, b"started"); // Ok, this had better not block! reader.kill() } #[test] fn test_debug_format() { let e = cmd!("foo", "bar", "baz").pipe(cmd!("bing", "bong")); assert_eq!( format!("{:?}", e), r#"Pipe(Cmd(["foo", "bar", "baz"]), Cmd(["bing", "bong"]))"#, ); } #[test] fn test_reader_try_wait() -> io::Result<()> { // Create a ReaderHandle for a cat process. Give cat 1 MB of data to echo // back to us, so that it will block on its stdout pipe until we start // reading. let bytes = vec![42; 1_000_000]; let mut cat_reader = cmd!(path_to_exe("cat")) .stdin_bytes(bytes.clone()) .reader()?; assert!(cat_reader.try_wait()?.is_none()); let mut output = Vec::new(); cat_reader.read_to_end(&mut output)?; assert_eq!(output, bytes); let output = cat_reader.try_wait()?.expect("is some"); assert!(output.status.success()); assert!(output.stdout.is_empty()); assert!(output.stderr.is_empty()); Ok(()) } #[test] fn test_pids() -> io::Result<()> { let handle = true_cmd().start()?; let pids = handle.pids(); assert_eq!(pids.len(), 1); handle.wait()?; let reader = true_cmd().reader()?; let pids = reader.pids(); assert_eq!(pids.len(), 1); std::io::copy(&mut &reader, &mut std::io::sink())?; let handle = true_cmd() .pipe(true_cmd().stdout_null().pipe(true_cmd())) .start()?; let pids = handle.pids(); assert_eq!(pids.len(), 3); handle.wait()?; let reader = true_cmd() .pipe(true_cmd().stdout_null().pipe(true_cmd())) .reader()?; let pids = reader.pids(); assert_eq!(pids.len(), 3); std::io::copy(&mut &reader, &mut std::io::sink())?; Ok(()) } duct-0.13.5/src/unix.rs010064400017500001750000000046331376151060700130540ustar 00000000000000extern crate libc; use std::io; use super::{Handle, HandleInner, PipeHandle}; use shared_child::unix::SharedChildExt; pub trait HandleExt { /// Send a signal to all child processes running under this expression. fn send_signal(&self, signal: libc::c_int) -> io::Result<()>; } impl HandleExt for Handle { fn send_signal(&self, signal: libc::c_int) -> io::Result<()> { self.inner.send_signal(signal) } } impl HandleExt for HandleInner { fn send_signal(&self, signal: libc::c_int) -> io::Result<()> { match *self { HandleInner::Child(ref child_handle) => child_handle.child.send_signal(signal), HandleInner::Pipe(ref pipe_handle) => pipe_handle.send_signal(signal), HandleInner::StdinBytes(ref stdin_bytes_handle) => { stdin_bytes_handle.inner_handle.send_signal(signal) } HandleInner::Unchecked(ref inner_handle) => inner_handle.send_signal(signal), } } } impl HandleExt for PipeHandle { /// Signals both parts of this `pipe` expression. Returns an error if signalling one of the /// expressions returned an error. fn send_signal(&self, signal: libc::c_int) -> io::Result<()> { let left_result = self.left_handle.send_signal(signal); let right_result = self.right_handle.send_signal(signal); left_result.and(right_result) } } #[cfg(test)] mod tests { use super::{libc, HandleExt}; use crate::cmd; use crate::test::path_to_exe; use std::os::unix::process::ExitStatusExt; use std::sync::Arc; use std::thread; #[test] fn test_send_signal_from_other_thread() { let sleep_cmd = cmd(path_to_exe("sleep"), &["1000000"]); let handle = Arc::new(sleep_cmd.unchecked().start().unwrap()); let handle_clone = handle.clone(); thread::spawn(move || handle_clone.send_signal(libc::SIGABRT).unwrap()); let status = handle.wait().unwrap(); assert_eq!(Some(libc::SIGABRT), status.status.signal()); } #[test] fn test_send_signal_to_pipe() { let sleep_cmd = cmd(path_to_exe("sleep"), &["1000000"]); let handle = sleep_cmd .pipe(sleep_cmd.clone()) .unchecked() .start() .unwrap(); handle.send_signal(libc::SIGABRT).unwrap(); let status = handle.wait().unwrap(); assert_eq!(Some(libc::SIGABRT), status.status.signal()); } }