use portable_pty::ChildKiller;
#[cfg(unix)]
#[expect(
unsafe_code,
reason = "libc::killpg is FFI and unsafe by signature; the wrapper passes a validated pid and signal and treats ESRCH as success"
)]
fn kill_group(pid: u32, signal: i32) -> std::io::Result<()> {
let pgid = pid as libc::pid_t;
let ret = unsafe { libc::killpg(pgid, signal) };
if ret == -1 {
let err = std::io::Error::last_os_error();
if err.raw_os_error() == Some(libc::ESRCH) {
return Ok(());
}
return Err(err);
}
Ok(())
}
#[cfg(unix)]
pub(crate) fn sigterm_group(
pid: u32,
_killer: &mut (dyn ChildKiller + Send + Sync),
) -> std::io::Result<()> {
kill_group(pid, libc::SIGTERM)
}
#[cfg(unix)]
pub(crate) fn sigkill_group(
pid: u32,
_killer: &mut (dyn ChildKiller + Send + Sync),
) -> std::io::Result<()> {
kill_group(pid, libc::SIGKILL)
}
#[cfg(not(unix))]
pub(crate) fn sigterm_group(
_pid: u32,
killer: &mut (dyn ChildKiller + Send + Sync),
) -> std::io::Result<()> {
killer.kill()
}
#[cfg(not(unix))]
pub(crate) fn sigkill_group(
_pid: u32,
killer: &mut (dyn ChildKiller + Send + Sync),
) -> std::io::Result<()> {
killer.kill()
}