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}");
}
}
#[skuld::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:?}"
);
}
}
#[skuld::test]
fn kill_group_on_owned_group_succeeds() {
use std::os::unix::process::CommandExt;
use std::os::unix::process::ExitStatusExt as _;
let mut child = crate::test_spawn::spawn(
std::process::Command::new("cat")
.stdin(std::process::Stdio::piped())
.process_group(0),
)
.expect("spawn cat");
let stdin = child.stdin.take().expect("piped stdin");
let pgid = child.id() as i32;
assert!(kill_group(pgid).is_ok(), "kill_group on owned group must succeed");
drop(stdin);
let status = child.wait().expect("wait after kill_group");
assert_eq!(
status.signal(),
Some(libc::SIGKILL),
"kill_group must have actually killed the leader, got {status:?}"
);
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn kill_group_dooms_the_group_before_it_is_listed() {
use std::io::BufRead as _;
use std::os::unix::process::CommandExt;
use std::os::unix::process::ExitStatusExt as _;
let mut child = crate::test_spawn::spawn(
std::process::Command::new("sh")
.arg("-c")
.arg("while read x; do (echo spawned; exec cat) & done")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.process_group(0),
)
.expect("spawn the leader");
let mut stdin = child.stdin.take().expect("piped stdin");
let mut stdout = std::io::BufReader::new(child.stdout.take().expect("piped stdout"));
let pgid = child.id() as i32;
let seen = std::rc::Rc::new(std::cell::RefCell::new(None::<String>));
let record = seen.clone();
let _armed = super::group::fault::set_after_listing(move || {
crate::test_child::write_to_possibly_dead_stdin(&mut stdin, b"x\n");
let mut line = String::new();
stdout.read_line(&mut line).expect("read to a line or EOF");
*record.borrow_mut() = Some(line);
});
kill_group(pgid).expect("kill_group on an owned group");
let status = child.wait().expect("reap the leader");
let line = seen.borrow_mut().take().expect("the hook ran in converge");
assert_eq!(
line, "",
"a member forked after the listing: the first killpg did not doom the group"
);
assert_eq!(status.signal(), Some(libc::SIGKILL), "got {status:?}");
}
#[skuld::test]
fn term_group_on_owned_group_succeeds() {
use std::os::unix::process::{CommandExt, ExitStatusExt as _};
let mut child = crate::test_spawn::spawn(
std::process::Command::new("cat")
.stdin(std::process::Stdio::piped())
.process_group(0),
)
.expect("spawn cat");
let stdin = child.stdin.take().expect("piped stdin");
let pgid = child.id() as i32;
assert!(term_group(pgid).is_ok(), "term_group on owned group must succeed");
drop(stdin);
let status = child.wait().expect("wait after term_group");
assert_eq!(
status.signal(),
Some(libc::SIGTERM),
"term_group must have actually delivered SIGTERM and ended the leader, got {status:?}"
);
}
#[skuld::test]
fn kill_group_on_an_all_zombie_group_is_ok() {
use std::os::unix::process::CommandExt;
let child = crate::test_spawn::spawn(std::process::Command::new("true").process_group(0)).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");
}
#[skuld::test]
fn term_group_on_an_all_zombie_group_is_ok() {
use std::os::unix::process::CommandExt;
let child = crate::test_spawn::spawn(std::process::Command::new("true").process_group(0)).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");
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn kill_group_and_term_group_without_openat2_are_unsupported() {
use crate::error::Error;
use crate::identity::proc_view_fault::force_openat2_errno;
for (name, call) in [("kill_group", kill_group as fn(i32) -> _), ("term_group", term_group)] {
let forced = force_openat2_errno(rustix::io::Errno::NOSYS);
let err = call(i32::MAX).expect_err("no openat2, no listing");
drop(forced);
match err {
Error::Unsupported { op, platform, detail } => {
assert_eq!(platform, "linux", "{name}");
assert!(op.contains(&i32::MAX.to_string()), "{name}: {op}");
assert!(detail.contains("cosca requires openat2"), "{name}: {detail}");
}
other => panic!("{name}: expected Unsupported, got {other:?}"),
}
}
}