use std::io::Read as _;
use crate::test_child::Console;
use crate::child::owned::seams::record_root_teardowns;
use crate::child::spawn::fault::{release_at_drop_kill, release_between_kill_and_wait};
use crate::command::Command;
use crate::containment::treewalk::fault::record_walks;
use crate::containment::unix::fault::record_kill_group;
#[cfg(target_os = "linux")]
use crate::test_groups::{cgroup, Group};
use crate::{ContainMode, Containment, Stdio};
#[cfg(target_os = "linux")]
const ROOT_LEAVING_A_CAT_THAT_SAYS_EOF: &str = "exec 3<&0; { cat <&3 3<&- && echo eof; } & echo started; exit 0";
const WARN: &str = "Child::drop: the root is already reaped";
#[cfg(target_os = "macos")]
const SESSION_CONTAINMENT: Containment = Containment::FdMarker;
#[cfg(not(target_os = "macos"))]
const SESSION_CONTAINMENT: Containment = Containment::Session;
fn session(argv: &[&str]) -> Command {
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(argv.iter().copied());
cmd.contain_with(ContainMode::Session);
cmd
}
fn session_blocker() -> (crate::Child, crate::PipeWriter) {
let mut cmd = session(crate::test_child::BLOCKER_ARGV);
cmd.stdin(Stdio::pipe_in()).expect("stdin pipe");
let mut child = cmd.spawn().expect("spawn");
let stdin = child.stdin().expect("stdin pipe");
(child, stdin)
}
fn root_that_leaves_a_cat() -> Command {
root_that_leaves("exec 3<&0; cat <&3 3<&- & echo started; exit 0")
}
fn root_that_leaves(script: &str) -> Command {
let mut cmd = session(&["sh", "-c", script]);
cmd.stdin(Stdio::pipe_in()).expect("stdin pipe");
cmd.stdout(Stdio::pipe_out()).expect("stdout pipe");
cmd.stderr(Stdio::pipe_out()).expect("stderr pipe");
cmd
}
#[cfg_attr(
target_os = "macos",
allow(dead_code, reason = "the echo round trip is the non-macOS test's")
)]
struct CatPipes {
stdin: crate::PipeWriter,
stdout: crate::PipeReader,
stderr: crate::PipeReader,
}
fn spawn_cat_tree() -> (crate::Child, CatPipes) {
let mut child = root_that_leaves_a_cat().spawn().expect("spawn");
assert_eq!(child.containment(), SESSION_CONTAINMENT);
let mut stdout = child.stdout().expect("stdout pipe");
let mut started = [0u8; 8];
stdout.read_exact(&mut started).expect("read `started`");
assert_eq!(&started, b"started\n");
let stdin = child.stdin().expect("stdin pipe");
let stderr = child.stderr().expect("stderr pipe");
(child, CatPipes { stdin, stdout, stderr })
}
fn drop_warns_since(mark: usize) -> Vec<(log::Level, String)> {
crate::log_capture::records_since_on_current_thread(mark, WARN)
}
#[skuld::test]
fn dropping_a_waited_on_process_group_child_sends_no_killpg_and_warns_with_the_pgid() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::log_capture::install();
let recorder = record_kill_group();
let teardowns = record_root_teardowns();
let child = session(&["true"]).spawn().expect("spawn");
let pid = child.pid();
child.wait().expect("reap the root");
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(
recorder.killed(),
Vec::<i32>::new(),
"a reaped root's number may name another group: no killpg"
);
assert_eq!(
teardowns.count(),
0,
"a reaped root is neither killed nor waited for by pid"
);
let warns = drop_warns_since(mark);
assert_eq!(warns.len(), 1, "exactly one warn, got {warns:?}");
let (level, text) = &warns[0];
assert_eq!(*level, log::Level::Warn);
assert!(text.contains(&format!("pgid {pid}")), "the warn names the pgid: {text}");
assert!(
text.contains("kill_tree()") && text.contains("wait()"),
"the warn says how to end descendants: {text}"
);
assert!(
text.contains("so this drop does not") && text.contains("by this drop"),
"the drop calls itself a drop: {text}"
);
}
#[skuld::test]
fn tearing_down_a_waited_on_process_group_child_after_a_failed_run_does_not_call_itself_a_drop() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::log_capture::install();
let _recorder = record_kill_group();
let child = session(&["true"]).spawn().expect("spawn");
child.wait().expect("reap the root");
let mark = crate::log_capture::mark();
let (fate, handed_back) =
crate::test_child::tear_down_holding(child, (), crate::child::owned::teardown::Disposition::HandBack);
assert!(handed_back.is_none(), "the teardown handed the child back");
assert_eq!(fate, crate::error::ChildFate::Reaped);
let warns = crate::log_capture::records_since_on_current_thread(mark, "the root is already reaped");
assert_eq!(warns.len(), 1, "exactly one warn, got {warns:?}");
let text = &warns[0].1;
assert!(
text.starts_with("teardown after a failed run: the root is already reaped"),
"{text}"
);
assert!(
text.contains("so this teardown does not") && text.contains("by this teardown") && !text.contains("drop"),
"{text}"
);
}
#[skuld::test]
fn dropping_a_running_process_group_child_still_kills_the_group_and_does_not_warn() {
crate::log_capture::install();
let recorder = record_kill_group();
let teardowns = record_root_teardowns();
let (child, stdin) = session_blocker();
let blocker = release_at_drop_kill(stdin);
let pid = child.pid();
let mark = crate::log_capture::mark();
drop(child);
recorder.assert_killed_only(pid as i32);
blocker.assert_killed();
assert_eq!(teardowns.count(), 1, "an unreaped root is killed and reaped");
assert_eq!(drop_warns_since(mark), []);
}
#[skuld::test]
fn dropping_an_exited_but_unreaped_process_group_child_still_kills_the_group() {
crate::log_capture::install();
let recorder = record_kill_group();
let (child, stdin) = session_blocker();
let pid = child.pid();
child.kill().expect("kill the root");
drop(stdin);
crate::test_child::assert_zombie_sigkilled("the root", pid);
let mark = crate::log_capture::mark();
drop(child);
recorder.assert_killed_only(pid as i32);
assert_eq!(drop_warns_since(mark), []);
}
#[skuld::test]
fn kill_tree_before_wait_ends_a_descendant_of_an_exited_root() {
let (child, mut pipes) = spawn_cat_tree();
child.kill_tree().expect("kill_tree");
_ = std::io::Write::write_all(&mut pipes.stdin, b"x");
let mut echoed = [0u8; 1];
let n = pipes.stdout.read(&mut echoed).unwrap_or(0);
assert_eq!(n, 0, "kill_tree must end the cat");
child.wait().expect("reap the root");
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn drop_after_wait_leaves_a_descendant_running_until_it_is_ended_explicitly() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
let (child, mut pipes) = spawn_cat_tree();
child.wait().expect("reap the root");
drop(child);
crate::test_child::assert_echoes(&mut pipes.stdin, &mut pipes.stdout);
drop(pipes.stdin);
let mut rest = Vec::new();
pipes
.stderr
.read_to_end(&mut rest)
.expect("stderr reaches EOF once the cat is gone");
}
#[skuld::test]
fn dropping_a_waited_on_tree_walk_child_walks_nothing_and_warns_with_the_root_pid() {
crate::log_capture::install();
let walks = record_walks();
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(["true"]);
cmd.contain_with(ContainMode::TreeWalk);
let child = cmd.spawn().expect("spawn");
let pid = child.pid();
child.wait().expect("reap the root");
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(
walks.walked(),
Vec::<u32>::new(),
"a reaped root's number names no tree"
);
let warns = drop_warns_since(mark);
assert_eq!(warns.len(), 1, "exactly one warn, got {warns:?}");
let (level, text) = &warns[0];
assert_eq!(*level, log::Level::Warn);
assert!(
text.contains(&format!("root pid {pid}")),
"the warn names the root: {text}"
);
assert!(
text.contains("kill_tree()"),
"the warn says how to end descendants: {text}"
);
}
#[skuld::test]
fn dropping_a_running_tree_walk_child_walks_from_its_pid_and_does_not_warn() {
crate::log_capture::install();
let walks = record_walks();
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.contain_with(ContainMode::TreeWalk);
cmd.stdin(Stdio::pipe_in()).expect("stdin pipe");
let mut child = cmd.spawn().expect("spawn");
let blocker = release_at_drop_kill(child.stdin().expect("stdin pipe"));
let pid = child.pid();
let mark = crate::log_capture::mark();
drop(child);
let walked = walks.walked();
assert!(!walked.is_empty() && walked.iter().all(|w| *w == pid), "{walked:?}");
assert_eq!(drop_warns_since(mark), []);
blocker.assert_killed();
}
fn waited_on_blocker_root() -> (u32, crate::Child) {
let (child, stdin) = session_blocker();
let pid = child.pid();
child.kill().expect("kill the root");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().expect("reap the root"));
(pid, child)
}
#[skuld::test]
fn dropping_a_waited_on_child_whose_number_reads_unknown_still_sends_no_killpg() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::log_capture::install();
let recorder = record_kill_group();
let teardowns = record_root_teardowns();
let (_, child) = waited_on_blocker_root();
let _unknown = super::fault::force_next_root_read(crate::identity::Resolved::Unknown);
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(
recorder.killed(),
Vec::<i32>::new(),
"this handle reaped the root: no killpg"
);
assert_eq!(teardowns.count(), 0);
assert_eq!(drop_warns_since(mark).len(), 1);
}
#[skuld::test]
fn dropping_an_unreaped_child_whose_number_reads_unknown_kills_and_logs_the_failed_read() {
crate::log_capture::install();
let recorder = record_kill_group();
let teardowns = record_root_teardowns();
let (child, stdin) = session_blocker();
let blocker = release_at_drop_kill(stdin);
let pid = child.pid();
let _unknown = super::fault::force_next_root_read(crate::identity::Resolved::Unknown);
let mark = crate::log_capture::mark();
drop(child);
recorder.assert_killed_only(pid as i32);
blocker.assert_killed();
assert_eq!(teardowns.count(), 1);
assert_eq!(drop_warns_since(mark), []);
let reads = crate::log_capture::records_since_on_current_thread(mark, "could not be read");
assert_eq!(reads.len(), 1, "{reads:?}");
assert_eq!(reads[0].0, log::Level::Debug);
assert!(reads[0].1.contains(&format!("{pid}")), "{reads:?}");
}
#[skuld::test]
fn dropping_a_foreign_reaped_process_group_child_sends_no_killpg() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::log_capture::install();
let recorder = record_kill_group();
let teardowns = record_root_teardowns();
let (child, stdin) = session_blocker();
let pid = child.pid();
child.kill().expect("kill the root");
drop(stdin);
crate::test_child::assert_zombie_sigkilled("the root", pid);
let mut status = 0;
let reaped = unsafe { libc::waitpid(pid as libc::pid_t, &mut status, 0) };
assert_eq!(reaped, pid as libc::pid_t, "{}", std::io::Error::last_os_error());
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(recorder.killed(), Vec::<i32>::new());
assert_eq!(
teardowns.count(),
0,
"the number may belong to another child by now: no kill and no wait by pid"
);
assert_eq!(drop_warns_since(mark).len(), 1);
}
#[skuld::test]
fn drop_after_kill_tree_and_wait_skips_at_debug_and_does_not_warn() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::log_capture::install();
let recorder = record_kill_group();
let child = session(&["true"]).spawn().expect("spawn");
let pid = child.pid();
child.kill_tree().expect("kill_tree");
child.wait().expect("reap the root");
recorder.assert_killed_only(pid as i32);
let sent = recorder.killed();
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(recorder.killed(), sent, "the drop sends no further killpg");
assert_skipped_at_debug(mark);
}
#[skuld::test]
fn drop_after_graceful_shutdown_tree_skips_at_debug_and_does_not_warn() {
use std::os::unix::process::ExitStatusExt as _;
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::log_capture::install();
let recorder = record_kill_group();
let (child, stdin) = session_blocker();
let pid = child.pid();
let _release = crate::graceful_hooks::release_at(crate::graceful_hooks::HookPoint::AfterTerminate, stdin);
let status = child
.graceful_shutdown_tree(std::time::Duration::from_secs(60))
.expect("graceful_shutdown_tree");
assert_eq!(
status.signal(),
Some(libc::SIGTERM),
"the root dies to the group's SIGTERM: {status:?}"
);
recorder.assert_killed_only(pid as i32);
let mark = crate::log_capture::mark();
drop(child);
assert_skipped_at_debug(mark);
}
#[skuld::test]
fn a_failed_elevated_spawns_cleanup_drops_its_handle_silently() {
crate::log_capture::install();
let recorder = record_kill_group();
let (child, stdin) = session_blocker();
let blocker = release_between_kill_and_wait(stdin);
let pid = child.pid();
let mark = crate::log_capture::mark();
let (err, fate) = super::spawn::finish_elevated(
child,
Err(crate::error::Error::Io(std::io::Error::other("no password"))),
)
.expect_err("the password write failed")
.expect_may_have_started_with();
assert!(matches!(err, crate::error::Error::Elevation { .. }), "{err:?}");
assert_eq!(
fate,
crate::error::ChildFate::Reaped,
"the group's kill and the root's reap"
);
recorder.assert_killed_only(pid as i32);
blocker.assert_killed();
assert_eq!(crate::log_capture::records_since_on_current_thread(mark, WARN), []);
}
#[skuld::test]
fn drop_after_a_failed_kill_tree_and_wait_still_warns() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::log_capture::install();
let recorder = record_kill_group();
recorder.fail_with_unassessable();
let child = session(&["true"]).spawn().expect("spawn");
child.kill_tree().expect_err("the forced refusal");
child.wait().expect("reap the root");
let mark = crate::log_capture::mark();
drop(child);
let records = drop_warns_since(mark);
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn);
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn drop_after_an_incomplete_tree_walk_kill_and_wait_still_warns() {
use crate::containment::treewalk::fault::LeftBehind;
crate::log_capture::install();
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving(LeftBehind::Descendant);
let left = child
.kill_tree()
.expect_err("a tree walk that left a member is an error");
assert!(matches!(left, crate::error::Error::MemberNotKilled { .. }), "{left:?}");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().expect("reap the root"));
let mark = crate::log_capture::mark();
drop(child);
let records = drop_warns_since(mark);
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn);
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn a_second_kill_tree_after_a_walk_left_a_member_reports_it_again_and_does_not_mark_the_tree() {
use crate::containment::treewalk::fault::LeftBehind;
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving(LeftBehind::Descendant);
let first = child.kill_tree().expect_err("the walk left a member");
let second = child
.kill_tree()
.expect_err("the member the first walk left is not shown killed");
let pid_of = |error: &crate::error::Error| match error {
crate::error::Error::MemberNotKilled { pid, .. } => *pid,
other => panic!("expected MemberNotKilled, got {other:?}"),
};
assert_eq!(pid_of(&second), pid_of(&first), "{first:?}, {second:?}");
assert!(!child.tree_killed.is_set(), "a member was left");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().expect("reap the root"));
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn a_drop_after_a_kill_tree_that_left_a_member_warns_of_it() {
use crate::containment::treewalk::fault::LeftBehind;
crate::log_capture::install();
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving(LeftBehind::Descendant);
child.kill_tree().expect_err("the walk left a member");
let mark = crate::log_capture::mark();
drop(child);
let records = crate::log_capture::records_since_on_current_thread(mark, "was not killed");
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn);
drop(stdin);
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn a_kill_tree_after_wait_reports_the_member_an_earlier_kill_left() {
use crate::containment::treewalk::fault::LeftBehind;
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving(LeftBehind::Descendant);
let first = child.kill_tree().expect_err("the walk left a member");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().expect("reap the root"));
let second = child
.kill_tree()
.expect_err("the root is gone, and the member is not shown killed");
let pid_of = |error: &crate::error::Error| match error {
crate::error::Error::MemberNotKilled { pid, .. } => *pid,
other => panic!("expected MemberNotKilled, got {other:?}"),
};
assert_eq!(pid_of(&second), pid_of(&first), "{first:?}, {second:?}");
assert!(!child.tree_killed.is_set(), "a member was left");
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn a_drop_after_a_kill_tree_that_left_a_member_and_a_wait_warns_of_it() {
use crate::containment::treewalk::fault::LeftBehind;
crate::log_capture::install();
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving(LeftBehind::Descendant);
child.kill_tree().expect_err("the walk left a member");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().expect("reap the root"));
let mark = crate::log_capture::mark();
drop(child);
let records = crate::log_capture::records_since_on_current_thread(mark, "was not killed");
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn);
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn drop_after_a_walk_that_left_only_the_root_and_a_backstop_kill_warns_of_an_unvouched_tree() {
use crate::containment::treewalk::fault::LeftBehind;
crate::log_capture::install();
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving(LeftBehind::Root);
child.kill_tree().expect("the backstop killed the only member left");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().expect("reap the root"));
let mark = crate::log_capture::mark();
drop(child);
let warns = drop_warns_since(mark);
assert_eq!(warns.len(), 1, "{warns:?}");
assert_eq!(warns[0].0, log::Level::Warn, "{warns:?}");
assert!(warns[0].1.contains("could not be vouched for"), "{warns:?}");
assert!(!warns[0].1.contains("members"), "{warns:?}");
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn kill_tree_on_a_tree_walk_whose_root_is_reaped_is_unassessable_and_does_not_mark_the_tree_killed() {
crate::log_capture::install();
let (child, stdin) = crate::test_child::held_treewalk_blocker();
drop(stdin);
child.wait().expect("reap the root");
let err = child
.kill_tree()
.expect_err("a root that is gone leaves no tree to walk: nothing was killed");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert!(!child.tree_killed.is_set(), "a walk that found no root killed nothing");
let mark = crate::log_capture::mark();
drop(child);
let records = drop_warns_since(mark);
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn, "{records:?}");
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn a_drop_whose_walk_leaves_a_descendant_warns_once_and_kills_the_root() {
use crate::containment::treewalk::fault::LeftBehind;
crate::log_capture::install();
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving(LeftBehind::Descendant);
let blocker = release_between_kill_and_wait(stdin);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let mark = crate::log_capture::mark();
drop(child);
blocker.assert_killed();
let records = crate::log_capture::records_since_on_current_thread(mark, "was not killed");
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn);
let drop_records = crate::log_capture::warns_since(mark, "Child::drop");
assert_eq!(drop_records.len(), 1, "one report of the event: {drop_records:?}");
assert_eq!(
id.exists(),
crate::identity::Existence::Gone,
"the drop reaped the root"
);
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn a_drop_whose_walk_leaves_only_the_root_warns_it_could_not_vouch_for_the_tree() {
use crate::containment::treewalk::fault::LeftBehind;
crate::log_capture::install();
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving(LeftBehind::Root);
let blocker = release_between_kill_and_wait(stdin);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let mark = crate::log_capture::mark();
drop(child);
blocker.assert_killed();
let warns = crate::log_capture::warns_since(mark, "Child::drop");
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(warns[0].contains("could not be vouched for"), "{warns:?}");
assert!(!warns[0].contains("members"), "{warns:?}");
assert_eq!(
id.exists(),
crate::identity::Existence::Gone,
"the drop reaped the root"
);
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn kill_tree_after_a_walk_that_left_a_descendant_and_the_root_names_the_descendant_alone() {
use crate::containment::treewalk::fault::LeftBehind;
crate::log_capture::install();
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving(LeftBehind::RootAndDescendant);
let root = child.pid();
let mark = crate::log_capture::mark();
let err = child.kill_tree().expect_err("a descendant was left");
assert!(
matches!(err, crate::error::Error::MemberNotKilled { pid, .. } if pid == root.wrapping_add(1)),
"{err:?}"
);
let stale = crate::log_capture::records_since_on_current_thread(mark, &format!("pid {root} was not killed"));
assert_eq!(stale, [], "the root was killed by the handle");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().expect("reap the root"));
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn a_drop_whose_walk_leaves_a_descendant_and_the_root_names_the_descendant_alone() {
use crate::containment::treewalk::fault::LeftBehind;
crate::log_capture::install();
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving(LeftBehind::RootAndDescendant);
let blocker = release_between_kill_and_wait(stdin);
let root = child.pid();
let mark = crate::log_capture::mark();
drop(child);
blocker.assert_killed();
let warns = crate::log_capture::warns_since(mark, "");
assert_eq!(warns.len(), 1, "one report of the event: {warns:?}");
let text = &warns[0];
assert!(
text.contains(&format!("pid {} was not killed", root.wrapping_add(1))),
"{text}"
);
assert!(!text.contains(&format!("pid {root} was not killed")), "{text}");
}
#[track_caller]
fn assert_skipped_at_debug(mark: usize) {
let records = drop_warns_since(mark);
assert_eq!(records.len(), 1, "exactly one skip record, got {records:?}");
assert_eq!(records[0].0, log::Level::Debug, "{records:?}");
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn cgroup_drop_after_wait_still_kills_the_tree_and_does_not_warn(#[fixture(cgroup)] _group: &Group) {
crate::log_capture::install();
let mut cmd = root_that_leaves(ROOT_LEAVING_A_CAT_THAT_SAYS_EOF);
cmd.contain_with(ContainMode::Strongest);
let mut child = cmd.spawn().expect("spawn");
assert_eq!(child.containment(), Containment::CgroupV2);
let mut started = [0u8; 8];
let mut stdout = child.stdout().expect("stdout pipe");
stdout.read_exact(&mut started).expect("read `started`");
let stdin = child.stdin().expect("stdin pipe");
let mut stderr = child.stderr().expect("stderr pipe");
child.wait().expect("reap the root");
let release = crate::child::front_cgroup_tests::release_after_cgroup_kill(stdin);
let mark = crate::log_capture::mark();
drop(child);
release.assert_killed();
assert_eq!(drop_warns_since(mark), []);
let mut rest = Vec::new();
stderr
.read_to_end(&mut rest)
.expect("the cgroup kill ended the cat, closing stderr");
let mut said = Vec::new();
stdout.read_to_end(&mut said).expect("every holder of stdout is gone");
crate::child::front_cgroup_tests::assert_nothing_ended_by_itself(&said);
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn cgroup_drop_of_an_exited_unreaped_root_kills_its_orphaned_descendant(#[fixture(cgroup)] _group: &Group) {
crate::log_capture::install();
let mut cmd = root_that_leaves(ROOT_LEAVING_A_CAT_THAT_SAYS_EOF);
cmd.contain_with(ContainMode::Strongest);
let mut child = cmd.spawn().expect("spawn");
assert_eq!(child.containment(), Containment::CgroupV2);
let mut started = [0u8; 8];
let mut stdout = child.stdout().expect("stdout pipe");
stdout.read_exact(&mut started).expect("read `started`");
let stdin = child.stdin().expect("stdin pipe");
let mut stderr = child.stderr().expect("stderr pipe");
crate::test_child::wait_until_zombie(child.pid());
let release = crate::child::front_cgroup_tests::release_after_cgroup_kill(stdin);
let mark = crate::log_capture::mark();
drop(child);
release.assert_killed();
assert_eq!(drop_warns_since(mark), []);
let mut rest = Vec::new();
stderr
.read_to_end(&mut rest)
.expect("the cgroup kill ended the cat, closing stderr");
let mut said = Vec::new();
stdout.read_to_end(&mut said).expect("every holder of stdout is gone");
crate::child::front_cgroup_tests::assert_nothing_ended_by_itself(&said);
}
#[cfg(target_os = "macos")]
#[skuld::test]
fn fd_marker_drop_after_wait_sweeps_the_marker_holders_but_names_nothing_by_the_roots_number() {
crate::log_capture::install();
let recorder = record_kill_group();
let walks = record_walks();
let sweeps = crate::containment::fdmarker::fault::record_holder_kills();
let mut cmd = root_that_leaves_a_cat();
cmd.contain_with(ContainMode::Strongest);
let mut child = cmd.spawn().expect("spawn");
assert_eq!(child.containment(), Containment::FdMarker);
let mut started = [0u8; 8];
child
.stdout()
.expect("stdout pipe")
.read_exact(&mut started)
.expect("read `started`");
let _stdin = child.stdin().expect("stdin pipe");
let mut stderr = child.stderr().expect("stderr pipe");
child.wait().expect("reap the root");
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(recorder.killed(), Vec::<i32>::new(), "no killpg after the reap");
assert_eq!(walks.walked(), Vec::<u32>::new(), "no ppid walk from the reaped root");
assert!(!sweeps.killed().is_empty(), "the marker-holder sweep still runs");
assert_eq!(drop_warns_since(mark).len(), 1);
let mut rest = Vec::new();
stderr
.read_to_end(&mut rest)
.expect("the sweep ended the cat, closing stderr, with stdin still held");
}
#[cfg(target_os = "macos")]
fn reaped_blocker_root(mode: ContainMode) -> (u32, crate::Child) {
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(Stdio::pipe_in()).expect("stdin pipe");
cmd.contain_with(mode);
let mut child = cmd.spawn().expect("spawn");
let stdin = child.stdin().expect("stdin pipe");
assert_eq!(child.containment(), Containment::FdMarker);
let pid = child.pid();
child.kill().expect("kill the root");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().expect("reap the root"));
(pid, child)
}
#[cfg(target_os = "macos")]
#[skuld::test]
fn fd_marker_without_a_group_drop_after_wait_only_sweeps_the_marker_holders() {
crate::log_capture::install();
let walks = record_walks();
let (pid, child) = reaped_blocker_root(ContainMode::TreeWalk);
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(walks.walked(), Vec::<u32>::new(), "no ppid walk from the reaped root");
let warns = drop_warns_since(mark);
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(warns[0].1.contains(&format!("root pid {pid}")), "{warns:?}");
}
#[cfg(target_os = "macos")]
#[skuld::test]
fn fd_marker_with_a_group_drop_after_wait_neither_signals_the_group_nor_walks() {
crate::log_capture::install();
let recorder = record_kill_group();
let walks = record_walks();
let (pid, child) = reaped_blocker_root(ContainMode::Strongest);
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(recorder.killed(), Vec::<i32>::new(), "no killpg after the reap");
assert_eq!(walks.walked(), Vec::<u32>::new(), "no ppid walk from the reaped root");
let warns = drop_warns_since(mark);
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(warns[0].1.contains(&format!("pgid {pid}")), "{warns:?}");
}
#[skuld::test]
fn a_root_is_reaped_by_its_own_reap_or_by_its_number_reading_gone_or_different() {
use crate::identity::{ProcessId, Resolved};
let id = ProcessId::from_parts_for_test(4242, crate::identity::Key::for_test(7));
let other = id.with_other_value();
for (own, now, reaped) in [
(true, Resolved::Found(id), true),
(true, Resolved::Found(other), true),
(true, Resolved::Gone, true),
(true, Resolved::Unknown, true),
(false, Resolved::Found(id), false),
(false, Resolved::Found(other), true),
(false, Resolved::Gone, true),
(false, Resolved::Unknown, false),
] {
assert_eq!(super::root_reaped(own, Some(id), now), reaped, "own {own}, now {now:?}");
}
}
#[skuld::test]
fn a_root_with_no_identity_is_reaped_by_its_own_reap_or_a_number_naming_no_process() {
use crate::identity::{ProcessId, Resolved};
let other = ProcessId::from_parts_for_test(4242, crate::identity::Key::for_test(7));
for (own, now, reaped) in [
(true, Resolved::Found(other), true),
(true, Resolved::Gone, true),
(true, Resolved::Unknown, true),
(false, Resolved::Found(other), false),
(false, Resolved::Gone, true),
(false, Resolved::Unknown, false),
] {
assert_eq!(super::root_reaped(own, None, now), reaped, "own {own}, now {now:?}");
}
}