use std::{
convert::TryInto,
io::{Error, Result},
process::Child,
};
use nix::{
sys::signal::{kill, Signal},
unistd::Pid,
};
pub trait UnixChildExt {
fn signal(&mut self, sig: Signal) -> Result<()>;
}
impl UnixChildExt for Child {
fn signal(&mut self, sig: Signal) -> Result<()> {
let pid = Pid::from_raw(self.id().try_into().expect("Command PID > i32::MAX"));
kill(pid, sig).map_err(Error::from)
}
}
#[cfg(feature = "with-tokio")]
impl UnixChildExt for ::tokio::process::Child {
fn signal(&mut self, sig: Signal) -> Result<()> {
if let Some(id) = self.id() {
let pid = Pid::from_raw(id.try_into().expect("Command PID > i32::MAX"));
kill(pid, sig).map_err(Error::from)
} else {
Ok(())
}
}
}