use std::{
io,
process::{Output, Stdio},
};
use tokio::{io::AsyncWriteExt as _, process::Command};
pub(crate) trait ProcessTree: Sized + Send {
fn prepare(command: &mut Command);
fn attach(child: &tokio::process::Child) -> std::io::Result<Self>;
fn kill(&mut self);
}
#[cfg(unix)]
pub(crate) struct SupervisedTree {
pid: Option<u32>,
}
#[cfg(unix)]
impl ProcessTree for SupervisedTree {
fn prepare(command: &mut Command) {
rho_tools::process_session::start_new_session(command.as_std_mut());
}
fn attach(child: &tokio::process::Child) -> std::io::Result<Self> {
Ok(Self { pid: child.id() })
}
fn kill(&mut self) {
let Some(pid) = self.pid.take().and_then(|pid| i32::try_from(pid).ok()) else {
return;
};
let _ = unsafe { libc::kill(-pid, libc::SIGKILL) };
}
}
#[cfg(windows)]
pub(crate) struct SupervisedTree(rho_tools::process_supervision::WindowsJob);
#[cfg(windows)]
impl ProcessTree for SupervisedTree {
fn prepare(command: &mut Command) {
rho_tools::process_supervision::WindowsJob::prepare(command);
}
fn attach(child: &tokio::process::Child) -> std::io::Result<Self> {
rho_tools::process_supervision::WindowsJob::attach(child).map(Self)
}
fn kill(&mut self) {
self.0.kill();
}
}
impl Drop for SupervisedTree {
fn drop(&mut self) {
self.kill();
}
}
pub(crate) async fn supervised_output(command: &mut Command) -> io::Result<Output> {
run_supervised(command, None).await
}
pub(crate) async fn supervised_output_with_input(
command: &mut Command,
input: Vec<u8>,
) -> io::Result<Output> {
run_supervised(command, Some(input)).await
}
async fn run_supervised(command: &mut Command, input: Option<Vec<u8>>) -> io::Result<Output> {
SupervisedTree::prepare(command);
if input.is_some() {
command.stdin(Stdio::piped());
}
let mut child = command
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let _tree = SupervisedTree::attach(&child)?;
let stdin = child.stdin.take();
let write = async move {
if let (Some(input), Some(mut stdin)) = (input, stdin) {
let _ = stdin.write_all(&input).await;
}
};
let ((), output) = tokio::join!(write, child.wait_with_output());
output
}
#[cfg(all(test, unix))]
pub(crate) fn process_is_running(pid: i32) -> bool {
if unsafe { libc::kill(pid, 0) } == -1 {
return false;
}
#[cfg(target_os = "linux")]
{
let Ok(stat) = std::fs::read_to_string(format!("/proc/{pid}/stat")) else {
return false;
};
let Some((_, fields)) = stat.rsplit_once(") ") else {
return true;
};
!fields.starts_with("Z ")
}
#[cfg(not(target_os = "linux"))]
true
}