use std::time::Duration;
#[cfg(unix)]
pub fn configure_command_for_process_group(command: &mut tokio::process::Command) {
unsafe {
command.pre_exec(|| {
if libc::setpgid(0, 0) != 0 {
return Err(std::io::Error::last_os_error());
}
Ok(())
});
}
}
#[cfg(not(unix))]
pub fn configure_command_for_process_group(command: &mut tokio::process::Command) {
let _ = command;
}
#[cfg(unix)]
fn send_signal_to_process_group_or_pid(pid: u32, signal: i32) -> bool {
if pid == 0 {
return false;
}
let raw_pid = pid as libc::pid_t;
let group_ok = unsafe { libc::kill(-raw_pid, signal) == 0 };
if group_ok {
return true;
}
unsafe { libc::kill(raw_pid, signal) == 0 }
}
#[cfg(unix)]
pub fn send_sigterm(pid: u32) -> bool {
send_signal_to_process_group_or_pid(pid, libc::SIGTERM)
}
#[cfg(unix)]
pub fn send_sigkill(pid: u32) -> bool {
send_signal_to_process_group_or_pid(pid, libc::SIGKILL)
}
#[cfg(not(unix))]
pub fn send_sigterm(pid: u32) -> bool {
taskkill(pid, false)
}
#[cfg(not(unix))]
pub fn send_sigkill(pid: u32) -> bool {
taskkill(pid, true)
}
#[cfg(not(unix))]
fn taskkill(pid: u32, force: bool) -> bool {
if pid == 0 {
return false;
}
let mut command = std::process::Command::new("taskkill");
command.args(["/PID", &pid.to_string(), "/T"]);
if force {
command.arg("/F");
}
command
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.is_ok_and(|status| status.success())
}
pub async fn terminate_process_tree(pid: u32, child: &mut tokio::process::Child, grace: Duration) {
#[cfg(unix)]
{
if !send_sigterm(pid) {
let _ = child.start_kill();
}
let exited = tokio::time::timeout(grace, child.wait()).await;
if exited.is_err() {
let _ = send_sigkill(pid);
let _ = tokio::time::timeout(Duration::from_secs(1), child.wait()).await;
}
}
#[cfg(not(unix))]
{
if !send_sigterm(pid) {
let _ = child.start_kill();
}
if tokio::time::timeout(grace, child.wait()).await.is_err() {
if !send_sigkill(pid) {
let _ = child.start_kill();
}
let _ = tokio::time::timeout(Duration::from_secs(1), child.wait()).await;
}
}
}