use super::{kill_group, term_group};
fn await_zombie(pid: u32) {
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
loop {
let rc = unsafe { libc::waitid(libc::P_PID, pid as libc::id_t, &mut info, libc::WEXITED | libc::WNOWAIT) };
if rc == 0 {
return;
}
let err = std::io::Error::last_os_error();
if err.raw_os_error() == Some(libc::EINTR) {
continue;
}
panic!("waitid failed: {err}");
}
}
#[test]
fn kill_group_and_term_group_reject_non_positive_pgid() {
for pgid in [0, -1, i32::MIN] {
let kill_err = kill_group(pgid).expect_err("kill_group(non-positive) must be Err");
assert!(
matches!(kill_err, crate::error::Error::Unassessable { .. }),
"got {kill_err:?}"
);
let term_err = term_group(pgid).expect_err("term_group(non-positive) must be Err");
assert!(
matches!(term_err, crate::error::Error::Unassessable { .. }),
"got {term_err:?}"
);
}
}
#[test]
fn kill_group_on_owned_group_succeeds() {
use std::os::unix::process::CommandExt;
let _guard = crate::child::spawn::spawn_lock();
let mut child = std::process::Command::new("sleep")
.arg("60")
.process_group(0)
.spawn()
.expect("spawn sleep");
let pgid = child.id() as i32;
assert!(kill_group(pgid).is_ok(), "kill_group on owned group must succeed");
let status = child.wait().expect("wait after kill_group");
assert!(
!status.success(),
"kill_group must have actually killed the leader, got {status:?}"
);
}
#[test]
fn term_group_on_owned_group_succeeds() {
use std::os::unix::process::CommandExt;
let _guard = crate::child::spawn::spawn_lock();
let mut child = std::process::Command::new("sleep")
.arg("60")
.process_group(0)
.spawn()
.expect("spawn sleep");
let pgid = child.id() as i32;
assert!(term_group(pgid).is_ok(), "term_group on owned group must succeed");
let status = child.wait().expect("wait after term_group");
assert!(
!status.success(),
"term_group must have actually delivered SIGTERM and ended the leader, got {status:?}"
);
}
#[test]
fn kill_group_on_an_all_zombie_group_is_ok() {
use std::os::unix::process::CommandExt;
let _guard = crate::child::spawn::spawn_lock();
let child = std::process::Command::new("true")
.process_group(0)
.spawn()
.expect("spawn true");
let pid = child.id();
await_zombie(pid);
#[cfg(target_os = "macos")]
assert_eq!(
nix::sys::signal::killpg(nix::unistd::Pid::from_raw(pid as i32), None),
Err(nix::errno::Errno::EPERM),
"macOS reports EPERM for an all-zombie group; if this changed, the arm under test moved"
);
assert!(
kill_group(pid as i32).is_ok(),
"a group holding only a zombie is already down"
);
let mut child = child;
child.wait().expect("reap");
}
#[test]
fn term_group_on_an_all_zombie_group_is_ok() {
use std::os::unix::process::CommandExt;
let _guard = crate::child::spawn::spawn_lock();
let child = std::process::Command::new("true")
.process_group(0)
.spawn()
.expect("spawn true");
let pid = child.id();
await_zombie(pid);
#[cfg(target_os = "macos")]
assert_eq!(
nix::sys::signal::killpg(
nix::unistd::Pid::from_raw(pid as i32),
nix::sys::signal::Signal::SIGTERM
),
Err(nix::errno::Errno::EPERM),
"macOS reports EPERM for an all-zombie group; if this changed, the arm under test moved"
);
#[cfg(target_os = "linux")]
assert_eq!(
nix::sys::signal::killpg(
nix::unistd::Pid::from_raw(pid as i32),
nix::sys::signal::Signal::SIGTERM
),
Ok(()),
"Linux reports Ok for an all-zombie group (no live member to refuse); if this changed, \
the arm under test moved"
);
assert!(
term_group(pid as i32).is_ok(),
"a group holding only a zombie is already down"
);
let mut child = child;
child.wait().expect("reap");
}