shared_child-1.0.0/.cargo_vcs_info.json0000644000000001120000000000100134350ustar { "git": { "sha1": "ed1c4f6eed646608390a4130bb6de78d630a7b0f" } } shared_child-1.0.0/.github/workflows/ci.yml000064400000000000000000000011470072674642500170010ustar 00000000000000name: tests on: push: branches: - "*" # not on tags pull_request: env: RUSTFLAGS: "-D warnings" RUST_BACKTRACE: "1" jobs: cargo_tests: name: ${{ matrix.os }} ${{ matrix.rust_channel }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: os: ["ubuntu-latest", "macOS-latest", "windows-latest"] rust_channel: [stable, beta, nightly] steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: ${{ matrix.rust_channel }} profile: minimal override: true - run: cargo test shared_child-1.0.0/.gitignore000064400000000000000000000000220072674642500142450ustar 00000000000000target Cargo.lock shared_child-1.0.0/Cargo.toml0000644000000020120000000000100114340ustar # 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "shared_child" version = "1.0.0" authors = ["jacko"] description = "a library for using child processes from multiple threads" documentation = "https://docs.rs/shared_child" readme = "README.md" keywords = ["command", "process", "child", "subprocess"] categories = ["os"] license = "MIT" repository = "https://github.com/oconnor663/shared_child.rs" [target."cfg(not(windows))".dependencies.libc] version = "0.2.42" [target."cfg(windows)".dependencies.winapi] version = "0.3.5" features = ["synchapi", "winbase", "winerror", "winnt"] shared_child-1.0.0/Cargo.toml.orig000064400000000000000000000010700072674642500151500ustar 00000000000000[package] name = "shared_child" version = "1.0.0" authors = ["jacko"] license = "MIT" repository = "https://github.com/oconnor663/shared_child.rs" documentation = "https://docs.rs/shared_child" readme = "README.md" description = "a library for using child processes from multiple threads" keywords = ["command", "process", "child", "subprocess"] categories = ["os"] edition = "2018" [target.'cfg(not(windows))'.dependencies] libc = "0.2.42" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3.5", features = ["synchapi", "winbase", "winerror", "winnt"] } shared_child-1.0.0/LICENSE000064400000000000000000000020260072674642500132700ustar 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. shared_child-1.0.0/README.md000064400000000000000000000056100072674642500135440ustar 00000000000000# shared_child.rs [![Actions Status](https://github.com/oconnor663/shared_child.rs/workflows/tests/badge.svg)](https://github.com/oconnor663/shared_child.rs/actions) [![crates.io](https://img.shields.io/crates/v/shared_child.svg)](https://crates.io/crates/shared_child) [![docs.rs](https://docs.rs/shared_child/badge.svg)](https://docs.rs/shared_child) A library for awaiting and killing child processes from multiple threads. - [Docs](https://docs.rs/shared_child) - [Crate](https://crates.io/crates/shared_child) - [Repo](https://github.com/oconnor663/shared_child.rs) The [`std::process::Child`](https://doc.rust-lang.org/std/process/struct.Child.html) type in the standard library provides [`wait`](https://doc.rust-lang.org/std/process/struct.Child.html#method.wait) and [`kill`](https://doc.rust-lang.org/std/process/struct.Child.html#method.kill) methods that take `&mut self`, making it impossible to kill a child process while another thread is waiting on it. That design works around a race condition in Unix's `waitpid` function, where a PID might get reused as soon as the wait returns, so a signal sent around the same time could accidentally get delivered to the wrong process. However with the newer POSIX `waitid` function, we can wait on a child without freeing its PID for reuse. That makes it safe to send signals concurrently. Windows has actually always supported this, by preventing PID reuse while there are still open handles to a child process. This library wraps `std::process::Child` for concurrent use, backed by these APIs. Compatibility note: The `libc` crate doesn't currently support `waitid` on NetBSD or OpenBSD, or on older versions of OSX. There [might also be](https://bugs.python.org/msg167016) some version of OSX where the `waitid` function exists but is broken. We can add a "best effort" workaround using `waitpid` for these platforms as we run into them. Please [file an issue](https://github.com/oconnor663/shared_child.rs/issues/new) if you hit this. ## Example ```rust use shared_child::SharedChild; use std::process::Command; use std::sync::Arc; // Spawn a child that will just sleep for a long time, // and put it in an Arc to share between threads. let mut command = Command::new("python"); command.arg("-c").arg("import time; time.sleep(1000000000)"); let shared_child = SharedChild::spawn(&mut command).unwrap(); let child_arc = Arc::new(shared_child); // On another thread, wait on the child process. let child_arc_clone = child_arc.clone(); let thread = std::thread::spawn(move || { child_arc_clone.wait().unwrap() }); // While the other thread is waiting, kill the child process. // This wouldn't be possible with e.g. Arc> from // the standard library, because the waiting thread would be // holding the mutex. child_arc.kill().unwrap(); // Join the waiting thread and get the exit status. let exit_status = thread.join().unwrap(); assert!(!exit_status.success()); ``` shared_child-1.0.0/README.tpl000064400000000000000000000005520072674642500137430ustar 00000000000000# {{crate}}.rs [![Actions Status](https://github.com/oconnor663/shared_child.rs/workflows/tests/badge.svg)](https://github.com/oconnor663/shared_child.rs/actions) [![crates.io](https://img.shields.io/crates/v/shared_child.svg)](https://crates.io/crates/shared_child) [![docs.rs](https://docs.rs/shared_child/badge.svg)](https://docs.rs/shared_child) {{readme}} shared_child-1.0.0/src/lib.rs000064400000000000000000000431510072674642500141720ustar 00000000000000//! A library for awaiting and killing child processes from multiple threads. //! //! - [Docs](https://docs.rs/shared_child) //! - [Crate](https://crates.io/crates/shared_child) //! - [Repo](https://github.com/oconnor663/shared_child.rs) //! //! The //! [`std::process::Child`](https://doc.rust-lang.org/std/process/struct.Child.html) //! type in the standard library provides //! [`wait`](https://doc.rust-lang.org/std/process/struct.Child.html#method.wait) //! and //! [`kill`](https://doc.rust-lang.org/std/process/struct.Child.html#method.kill) //! methods that take `&mut self`, making it impossible to kill a child process //! while another thread is waiting on it. That design works around a race //! condition in Unix's `waitpid` function, where a PID might get reused as soon //! as the wait returns, so a signal sent around the same time could //! accidentally get delivered to the wrong process. //! //! However with the newer POSIX `waitid` function, we can wait on a child //! without freeing its PID for reuse. That makes it safe to send signals //! concurrently. Windows has actually always supported this, by preventing PID //! reuse while there are still open handles to a child process. This library //! wraps `std::process::Child` for concurrent use, backed by these APIs. //! //! Compatibility note: The `libc` crate doesn't currently support `waitid` on //! NetBSD or OpenBSD, or on older versions of OSX. There [might also //! be](https://bugs.python.org/msg167016) some version of OSX where the //! `waitid` function exists but is broken. We can add a "best effort" //! workaround using `waitpid` for these platforms as we run into them. Please //! [file an issue](https://github.com/oconnor663/shared_child.rs/issues/new) if //! you hit this. //! //! # Example //! //! ```rust //! use shared_child::SharedChild; //! use std::process::Command; //! use std::sync::Arc; //! //! // Spawn a child that will just sleep for a long time, //! // and put it in an Arc to share between threads. //! let mut command = Command::new("python"); //! command.arg("-c").arg("import time; time.sleep(1000000000)"); //! let shared_child = SharedChild::spawn(&mut command).unwrap(); //! let child_arc = Arc::new(shared_child); //! //! // On another thread, wait on the child process. //! let child_arc_clone = child_arc.clone(); //! let thread = std::thread::spawn(move || { //! child_arc_clone.wait().unwrap() //! }); //! //! // While the other thread is waiting, kill the child process. //! // This wouldn't be possible with e.g. Arc> from //! // the standard library, because the waiting thread would be //! // holding the mutex. //! child_arc.kill().unwrap(); //! //! // Join the waiting thread and get the exit status. //! let exit_status = thread.join().unwrap(); //! assert!(!exit_status.success()); //! ``` use std::io; use std::process::{Child, ChildStderr, ChildStdin, ChildStdout, Command, ExitStatus}; use std::sync::{Condvar, Mutex}; mod sys; // Publish the Unix-only SharedChildExt trait. #[cfg(unix)] pub mod unix; #[derive(Debug)] pub struct SharedChild { // This lock provides shared access to kill() and wait(). We never hold it // during a blocking wait, though, so that non-blocking waits and kills can // go through. (Blocking waits use libc::waitid with the WNOWAIT flag.) child: Mutex, // When there are multiple waiting threads, one of them will actually wait // on the child, and the rest will block on this condvar. state_lock: Mutex, state_condvar: Condvar, } impl SharedChild { /// Spawn a new `SharedChild` from a /// [`std::process::Command`](https://doc.rust-lang.org/std/process/struct.Command.html). pub fn spawn(command: &mut Command) -> io::Result { let child = command.spawn()?; Ok(Self { child: Mutex::new(child), state_lock: Mutex::new(NotWaiting), state_condvar: Condvar::new(), }) } /// Construct a new `SharedChild` from an already spawned /// [`std::process::Child`](https://doc.rust-lang.org/std/process/struct.Child.html). /// /// This constructor needs to know whether `child` has already been waited on, and the only way /// to find that out is to call `child.try_wait()` internally. If the child process is /// currently a zombie, that call will clean it up as a side effect. The [`SharedChild::spawn`] /// constructor doesn't need to do this. pub fn new(mut child: Child) -> io::Result { let state = match child.try_wait()? { Some(status) => Exited(status), None => NotWaiting, }; Ok(Self { child: Mutex::new(child), state_lock: Mutex::new(state), state_condvar: Condvar::new(), }) } /// Return the child process ID. pub fn id(&self) -> u32 { self.child.lock().unwrap().id() } fn get_handle(&self) -> sys::Handle { sys::get_handle(&self.child.lock().unwrap()) } /// Wait for the child to exit, blocking the current thread, and return its /// exit status. pub fn wait(&self) -> io::Result { let mut state = self.state_lock.lock().unwrap(); loop { match *state { NotWaiting => { // Either no one is waiting on the child yet, or a previous // waiter failed. That means we need to do it ourselves. // Break out of this loop. break; } Waiting => { // Another thread is already waiting on the child. We'll // block until it signal us on the condvar, then loop again. // Spurious wakeups could bring us here multiple times // though, see the Condvar docs. state = self.state_condvar.wait(state).unwrap(); } Exited(exit_status) => return Ok(exit_status), } } // If we get here, we have the state lock, and we're the thread // responsible for waiting on the child. Set the state to Waiting and // then release the state lock, so that other threads can observe it // while we block. Afterwards we must leave the Waiting state before // this function exits, or other waiters will deadlock. *state = Waiting; drop(state); // Block until the child exits without reaping it. (On Unix, that means // we need to call libc::waitid with the WNOWAIT flag. On Windows // waiting never reaps.) That makes it safe for another thread to kill // while we're here, without racing against some process reusing the // child's PID. Having only one thread in this section is important, // because POSIX doesn't guarantee much about what happens when multiple // threads wait on a child at the same time: // http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_13 let noreap_result = sys::wait_without_reaping(self.get_handle()); // Now either we hit an error, or the child has exited and needs to be // reaped. Retake the state lock and handle all the different exit // cases. No matter what happened/happens, we'll leave the Waiting state // and signal the state condvar. let mut state = self.state_lock.lock().unwrap(); // The child has already exited, so this wait should clean up without blocking. let final_result = noreap_result.and_then(|_| self.child.lock().unwrap().wait()); *state = if let Ok(exit_status) = final_result { Exited(exit_status) } else { NotWaiting }; self.state_condvar.notify_all(); final_result } /// Return the child's exit status if it has already exited. If the child is /// still running, return `Ok(None)`. pub fn try_wait(&self) -> io::Result> { let mut status = self.state_lock.lock().unwrap(); // Unlike wait() above, we don't loop on the Condvar here. If the status // is Waiting or Exited, we return immediately. However, if the status // is NotWaiting, we'll do a non-blocking wait below, in case the child // has already exited. match *status { NotWaiting => {} Waiting => return Ok(None), Exited(exit_status) => return Ok(Some(exit_status)), }; // No one is waiting on the child. Check to see if it's already exited. // If it has, put ourselves in the Exited state. (There can't be any // other waiters to signal, because the state was NotWaiting when we // started, and we're still holding the status lock.) if sys::try_wait_without_reaping(self.get_handle())? { // The child has exited. Reap it. This should not block. let exit_status = self.child.lock().unwrap().wait()?; *status = Exited(exit_status); Ok(Some(exit_status)) } else { Ok(None) } } /// Send a kill signal to the child. On Unix this sends SIGKILL, and you /// should call `wait` afterwards to avoid leaving a zombie. If the process /// has already been waited on, this returns `Ok(())` and does nothing. pub fn kill(&self) -> io::Result<()> { let status = self.state_lock.lock().unwrap(); if let Exited(_) = *status { return Ok(()); } // The child is still running. Kill it. This assumes that the wait // functions above will never hold the child lock during a blocking // wait. self.child.lock().unwrap().kill() } /// Consume the `SharedChild` and return the /// [`std::process::Child`](https://doc.rust-lang.org/std/process/struct.Child.html) /// it contains. /// /// We never reap the child process except by calling `wait` or `try_wait` /// on it, so the child object's inner state is correct, even if it was /// waited on while it was shared. pub fn into_inner(self) -> Child { self.child.into_inner().unwrap() } /// Take the child's /// [`stdin`](https://doc.rust-lang.org/std/process/struct.Child.html#structfield.stdin) /// handle, if any. /// /// This will only return `Some` the first time it's called, and then only if the `Command` /// that created the child was configured with `.stdin(Stdio::piped())`. pub fn take_stdin(&self) -> Option { self.child.lock().unwrap().stdin.take() } /// Take the child's /// [`stdout`](https://doc.rust-lang.org/std/process/struct.Child.html#structfield.stdout) /// handle, if any. /// /// This will only return `Some` the first time it's called, and then only if the `Command` /// that created the child was configured with `.stdout(Stdio::piped())`. pub fn take_stdout(&self) -> Option { self.child.lock().unwrap().stdout.take() } /// Take the child's /// [`stderr`](https://doc.rust-lang.org/std/process/struct.Child.html#structfield.stderr) /// handle, if any. /// /// This will only return `Some` the first time it's called, and then only if the `Command` /// that created the child was configured with `.stderr(Stdio::piped())`. pub fn take_stderr(&self) -> Option { self.child.lock().unwrap().stderr.take() } } #[derive(Debug)] enum ChildState { NotWaiting, Waiting, Exited(ExitStatus), } use crate::ChildState::*; #[cfg(test)] mod tests { use super::*; use std::error::Error; use std::process::{Command, Stdio}; use std::sync::Arc; // Python isn't available on some Unix platforms, e.g. Android, so we need this instead. #[cfg(unix)] pub fn true_cmd() -> Command { Command::new("true") } #[cfg(not(unix))] pub fn true_cmd() -> Command { let mut cmd = Command::new("python"); cmd.arg("-c").arg(""); cmd } // Python isn't available on some Unix platforms, e.g. Android, so we need this instead. #[cfg(unix)] pub fn sleep_forever_cmd() -> Command { let mut cmd = Command::new("sleep"); cmd.arg("1000000"); cmd } #[cfg(not(unix))] pub fn sleep_forever_cmd() -> Command { let mut cmd = Command::new("python"); cmd.arg("-c").arg("import time; time.sleep(1000000)"); cmd } // Python isn't available on some Unix platforms, e.g. Android, so we need this instead. #[cfg(unix)] pub fn cat_cmd() -> Command { Command::new("cat") } #[cfg(not(unix))] pub fn cat_cmd() -> Command { let mut cmd = Command::new("python"); cmd.arg("-c").arg(""); cmd } #[test] fn test_wait() { let child = SharedChild::spawn(&mut true_cmd()).unwrap(); // Test the id() function while we're at it. let id = child.id(); assert!(id > 0); let status = child.wait().unwrap(); assert_eq!(status.code().unwrap(), 0); } #[test] fn test_kill() { let child = SharedChild::spawn(&mut sleep_forever_cmd()).unwrap(); child.kill().unwrap(); let status = child.wait().unwrap(); assert!(!status.success()); } #[test] fn test_try_wait() { let child = SharedChild::spawn(&mut sleep_forever_cmd()).unwrap(); let maybe_status = child.try_wait().unwrap(); assert_eq!(maybe_status, None); child.kill().unwrap(); // The child will handle that signal asynchronously, so we check it // repeatedly in a busy loop. let mut maybe_status = None; while let None = maybe_status { maybe_status = child.try_wait().unwrap(); } assert!(maybe_status.is_some()); assert!(!maybe_status.unwrap().success()); } #[test] fn test_many_waiters() { let child = Arc::new(SharedChild::spawn(&mut sleep_forever_cmd()).unwrap()); let mut threads = Vec::new(); for _ in 0..10 { let clone = child.clone(); threads.push(std::thread::spawn(move || clone.wait())); } child.kill().unwrap(); for thread in threads { thread.join().unwrap().unwrap(); } } #[test] fn test_waitid_after_exit_doesnt_hang() { // There are ominous reports (https://bugs.python.org/issue10812) of a // broken waitid implementation on OSX, which might hang forever if it // tries to wait on a child that's already exited. let child = true_cmd().spawn().unwrap(); sys::wait_without_reaping(sys::get_handle(&child)).unwrap(); // At this point the child has definitely exited. Wait again to test // that a second wait doesn't block. sys::wait_without_reaping(sys::get_handle(&child)).unwrap(); } #[test] fn test_into_inner_before_wait() { let shared_child = SharedChild::spawn(&mut sleep_forever_cmd()).unwrap(); let mut child = shared_child.into_inner(); child.kill().unwrap(); child.wait().unwrap(); } #[test] fn test_into_inner_after_wait() { // This makes sure the child's inner state is valid. If we used waitpid // on the side, the inner child would try to wait again and cause an // error. let shared_child = SharedChild::spawn(&mut sleep_forever_cmd()).unwrap(); shared_child.kill().unwrap(); shared_child.wait().unwrap(); let mut child = shared_child.into_inner(); // The child has already been waited on, so kill should be an error. let kill_err = child.kill().unwrap_err(); if cfg!(windows) { assert_eq!(std::io::ErrorKind::PermissionDenied, kill_err.kind()); } else { assert_eq!(std::io::ErrorKind::InvalidInput, kill_err.kind()); } // But wait should succeed. child.wait().unwrap(); } #[test] fn test_new() -> Result<(), Box> { // Spawn a short-lived child. let mut command = cat_cmd(); command.stdin(Stdio::piped()); command.stdout(Stdio::null()); let mut child = command.spawn()?; let child_stdin = child.stdin.take().unwrap(); // Construct a SharedChild from the Child, which has not yet been waited on. The child is // blocked on stdin, so we know it hasn't yet exited. let mut shared_child = SharedChild::new(child).unwrap(); assert!(matches!( *shared_child.state_lock.lock().unwrap(), NotWaiting, )); // Now close the child's stdin. This will cause the child to exit. drop(child_stdin); // Construct more SharedChild objects from the same child, in a loop. Eventually one of // them will notice that the child has exited. loop { shared_child = SharedChild::new(shared_child.into_inner())?; if let Exited(status) = &*shared_child.state_lock.lock().unwrap() { assert!(status.success()); return Ok(()); } } } #[test] fn test_takes() -> Result<(), Box> { let mut command = true_cmd(); command.stdin(Stdio::piped()); command.stdout(Stdio::piped()); command.stderr(Stdio::piped()); let shared_child = SharedChild::spawn(&mut command)?; assert!(shared_child.take_stdin().is_some()); assert!(shared_child.take_stdout().is_some()); assert!(shared_child.take_stderr().is_some()); assert!(shared_child.take_stdin().is_none()); assert!(shared_child.take_stdout().is_none()); assert!(shared_child.take_stderr().is_none()); shared_child.wait()?; Ok(()) } } shared_child-1.0.0/src/sys/mod.rs000064400000000000000000000001620072674642500150140ustar 00000000000000#[cfg(unix)] #[path = "unix.rs"] mod sys; #[cfg(windows)] #[path = "windows.rs"] mod sys; pub use self::sys::*; shared_child-1.0.0/src/sys/unix.rs000064400000000000000000000050550072674642500152260ustar 00000000000000use std; use std::io; use std::process::Child; // A handle on Unix is just the PID. pub struct Handle(u32); pub fn get_handle(child: &Child) -> Handle { Handle(child.id()) } // This blocks until a child exits, without reaping the child. pub fn wait_without_reaping(handle: Handle) -> io::Result<()> { loop { let ret = unsafe { let mut siginfo = std::mem::zeroed(); libc::waitid( libc::P_PID, handle.0 as libc::id_t, &mut siginfo, libc::WEXITED | libc::WNOWAIT, ) }; if ret == 0 { return Ok(()); } let error = io::Error::last_os_error(); if error.kind() != io::ErrorKind::Interrupted { return Err(error); } // We were interrupted. Loop and retry. } } // This checks whether the child has already exited, without reaping the child. pub fn try_wait_without_reaping(handle: Handle) -> io::Result { let mut siginfo: libc::siginfo_t; let ret = unsafe { // Darwin doesn't touch the siginfo_t struct if the child hasn't exited // yet. It expects us to have zeroed it ahead of time: // // The state of the siginfo structure in this case // is undefined. Some implementations bzero it, some // (like here) leave it untouched for efficiency. // // Thus the most portable check for "no matching pid with // WNOHANG" is to store a zero into si_pid before // invocation, then check for a non-zero value afterwards. // // https://github.com/opensource-apple/xnu/blob/0a798f6738bc1db01281fc08ae024145e84df927/bsd/kern/kern_exit.c#L2150-L2156 siginfo = std::mem::zeroed(); libc::waitid( libc::P_PID, handle.0 as libc::id_t, &mut siginfo, libc::WEXITED | libc::WNOWAIT | libc::WNOHANG, ) }; if ret != 0 { // EINTR should be impossible here Err(io::Error::last_os_error()) } else if siginfo.si_signo == libc::SIGCHLD { // The child has exited. Ok(true) } else if siginfo.si_signo == 0 { // The child has not exited. Ok(false) } else { // This should be impossible if we called waitid correctly. But it will // show up on macOS if we forgot to zero the siginfo_t above, for example. Err(io::Error::new( io::ErrorKind::Other, format!("unexpected si_signo from waitid: {}", siginfo.si_signo), )) } } shared_child-1.0.0/src/sys/windows.rs000064400000000000000000000027110072674642500157310ustar 00000000000000use std::io; use std::os::windows::io::{AsRawHandle, RawHandle}; use std::process::Child; use winapi::shared::winerror::WAIT_TIMEOUT; use winapi::um::synchapi::WaitForSingleObject; use winapi::um::winbase::{INFINITE, WAIT_OBJECT_0}; use winapi::um::winnt::HANDLE; pub struct Handle(RawHandle); // Kind of like a child PID on Unix, it's important not to keep the handle // around after the child has been cleaned up. The best solution would be to // have the handle actually borrow the child, but we need to keep the child // unborrowed. Instead we just avoid storing them. pub fn get_handle(child: &Child) -> Handle { Handle(child.as_raw_handle()) } // This is very similar to libstd's Child::wait implementation, because the // basic wait on Windows doesn't reap. The main difference is that this can be // called without &mut Child. pub fn wait_without_reaping(handle: Handle) -> io::Result<()> { let wait_ret = unsafe { WaitForSingleObject(handle.0 as HANDLE, INFINITE) }; if wait_ret != WAIT_OBJECT_0 { Err(io::Error::last_os_error()) } else { Ok(()) } } pub fn try_wait_without_reaping(handle: Handle) -> io::Result { let wait_ret = unsafe { WaitForSingleObject(handle.0 as HANDLE, 0) }; if wait_ret == WAIT_OBJECT_0 { // Child has exited. Ok(true) } else if wait_ret == WAIT_TIMEOUT { // Child has not exited yet. Ok(false) } else { Err(io::Error::last_os_error()) } } shared_child-1.0.0/src/unix.rs000064400000000000000000000026620072674642500144110ustar 00000000000000//! Unix-only extensions, for sending signals. use std::io; pub trait SharedChildExt { /// Send a signal to the child process with `libc::kill`. If the process /// has already been waited on, this returns `Ok(())` and does nothing. fn send_signal(&self, signal: libc::c_int) -> io::Result<()>; } impl SharedChildExt for super::SharedChild { fn send_signal(&self, signal: libc::c_int) -> io::Result<()> { let status = self.state_lock.lock().unwrap(); if let super::ChildState::Exited(_) = *status { return Ok(()); } // The child is still running. Signal it. Holding the state lock // is important to prevent a PID race. // This assumes that the wait methods will never hold the child // lock during a blocking wait, since we need it to get the pid. let pid = self.id() as libc::pid_t; match unsafe { libc::kill(pid, signal) } { -1 => Err(io::Error::last_os_error()), _ => Ok(()), } } } #[cfg(test)] mod tests { use super::SharedChildExt; use crate::tests::*; use crate::SharedChild; use std::os::unix::process::ExitStatusExt; #[test] fn test_send_signal() { let child = SharedChild::spawn(&mut sleep_forever_cmd()).unwrap(); child.send_signal(libc::SIGABRT).unwrap(); let status = child.wait().unwrap(); assert_eq!(Some(libc::SIGABRT), status.signal()); } }