use std::io::Read as _;
use std::os::unix::process::ExitStatusExt as _;
use crate::containment::dispatch::{TreeKillOutcome, TreeKilled};
use crate::identity::{FakeKernel, HostKernel, ProcessId};
use crate::test_child::{live_exiting_member as live_member, release_unsignalled as release, StdChild};
use super::treewalk_view_tests::{assert_unassessable_naming, live_member_with_descendant, walks};
const EXITING: u32 = 0x40_014c;
fn exiting_root(root: ProcessId) -> FakeKernel<'static> {
with_exiting_root(FakeKernel::over(&HostKernel), root)
}
pub(super) fn with_exiting_root(kernel: FakeKernel<'static>, root: ProcessId) -> FakeKernel<'static> {
let pid = root.pid();
let stat = format!("{pid} (sh) S 1 {pid} {pid} 0 -1 {EXITING} 0 0 0 0 0 0 0 0 20 0 1 0 1 0 0");
kernel
.on_tids(pid, Ok(&[pid]))
.on_read(&format!("{pid}/task/{pid}/stat"), Ok(stat.as_bytes()))
}
#[track_caller]
fn assert_both_signalled(who: &str, mut child: StdChild, signo: i32) {
drop(child.stdin.take());
let mut printed = String::new();
child
.stdout
.take()
.expect("piped stdout")
.read_to_string(&mut printed)
.expect("read to EOF");
assert_eq!(printed, "", "{who}: the descendant must have been signalled");
assert_eq!(child.wait().expect("reap the root").signal(), Some(signo), "{who}");
}
#[skuld::test]
fn a_kill_of_a_root_that_has_begun_to_exit_kills_its_members_and_is_unassessable() {
for (name, walk) in walks().into_iter().filter(|(name, _)| name.contains("kill")) {
let (child, root, _descendant) = live_member_with_descendant();
let kernel = exiting_root(root);
let result = walk(&kernel, root);
assert_unassessable_naming(result, &["had begun to exit", &format!("pid {}", root.pid())]);
assert_both_signalled(name, child, libc::SIGKILL);
}
}
#[skuld::test]
fn a_terminate_of_a_root_that_has_begun_to_exit_signals_its_members_and_lists_them_incomplete() {
let (child, root, descendant) = live_member_with_descendant();
let kernel = exiting_root(root);
let walked = super::terminate(&kernel, root, &super::ByIdentity::new(&kernel, root)).expect("the table reads");
let super::Terminated::Walked(listing) = walked else {
panic!("the members were walked");
};
assert_eq!(listing.members, [descendant]);
assert!(
!listing.complete,
"the exit's reparenting may have taken members out of the walk"
);
assert_both_signalled("terminate", child, libc::SIGTERM);
}
#[skuld::test]
fn a_root_that_has_begun_to_exit_with_no_member_is_no_root_and_nothing_is_sent() {
let (child, id) = live_member();
let kernel = exiting_root(id);
assert!(
matches!(
super::hard_kill(&kernel, id, &super::ByIdentity::new(&kernel, id)).outcome(),
TreeKillOutcome::NoRoot(..)
),
"the root has no tree to walk"
);
let termed = super::terminate(&kernel, id, &super::ByIdentity::new(&kernel, id));
assert!(
matches!(termed, Ok(super::Terminated::RootExited(_))),
"nothing is sent"
);
release(child);
}
fn flag_unreadable(root: ProcessId) -> FakeKernel<'static> {
FakeKernel::over(&HostKernel).on_tids_failing_after_open(root.pid(), libc::EIO)
}
#[skuld::test]
fn a_kill_whose_exit_flag_cannot_be_read_kills_its_members_and_answers_with_the_read_error() {
for (name, walk) in walks().into_iter().filter(|(name, _)| name.contains("kill")) {
let (child, root, _descendant) = live_member_with_descendant();
let kernel = flag_unreadable(root);
let result = walk(&kernel, root);
assert_unassessable_naming(
result,
&["exit has begun could not be read", &format!("pid {}", root.pid())],
);
assert_both_signalled(name, child, libc::SIGKILL);
}
}
#[skuld::test]
fn a_terminate_whose_exit_flag_cannot_be_read_signals_its_members_and_lists_them_incomplete() {
let (child, root, descendant) = live_member_with_descendant();
let kernel = flag_unreadable(root);
let walked = super::terminate(&kernel, root, &super::ByIdentity::new(&kernel, root)).expect("members were found");
let super::Terminated::Walked(listing) = walked else {
panic!("the members were walked");
};
assert_eq!(listing.members, [descendant]);
assert!(
!listing.complete,
"a listing with an unread flag cannot vouch for the tree"
);
assert_both_signalled("terminate", child, libc::SIGTERM);
}
#[skuld::test]
fn a_walk_with_no_member_whose_exit_flag_cannot_be_read_sends_nothing() {
let (child, root) = live_member();
let kernel = flag_unreadable(root);
assert!(
matches!(
super::hard_kill(&kernel, root, &super::ByIdentity::new(&kernel, root)).outcome(),
TreeKillOutcome::Unwalked(crate::error::Error::Unassessable { .. })
),
"no member was found and the flag is unread"
);
let termed = super::terminate(&kernel, root, &super::ByIdentity::new(&kernel, root));
assert!(
matches!(termed, Err(crate::error::Error::Unassessable { .. })),
"nothing is sent"
);
release(child);
}
#[skuld::test]
fn a_kill_of_a_root_that_has_begun_to_exit_with_only_unread_candidates_says_so() {
let (child, root, descendant) = live_member_with_descendant();
let kernel = with_exiting_root(
FakeKernel::over(&HostKernel).on_pidfd_open(descendant.pid(), Err(rustix::io::Errno::MFILE)),
root,
);
let TreeKillOutcome::Ran(Err(left)) =
super::hard_kill(&kernel, root, &super::ByIdentity::new(&kernel, root)).outcome()
else {
panic!("the unread candidate is left");
};
let said = left
.members
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join("; ");
assert!(
said.contains(&format!("pid {}", descendant.pid())),
"the candidate is named: {said}"
);
assert!(said.contains("had begun to exit"), "the root's exit is named: {said}");
release_candidate(child);
}
fn release_candidate(mut child: StdChild) {
use std::os::unix::process::ExitStatusExt as _;
let mut printed = String::new();
drop(child.stdin.take());
child
.stdout
.take()
.expect("piped stdout")
.read_to_string(&mut printed)
.expect("read to EOF");
assert_eq!(
printed, "released\n",
"the unread candidate must not have been signalled"
);
assert_eq!(child.wait().expect("reap the root").signal(), Some(libc::SIGKILL));
}
fn walker(root: ProcessId) -> crate::containment::mechanism::Mechanism {
crate::containment::mechanism::Mechanism::Walk(super::Walker::new(root))
}
fn names_the_exit(entries: &[crate::error::Error]) -> bool {
entries.iter().any(|entry| {
matches!(entry, crate::error::Error::Unassessable { detail, .. } if detail.contains("had begun to exit"))
})
}
#[skuld::test]
fn a_begun_root_whose_own_kill_was_left_is_not_ok_once_a_later_kill_ends_it() {
use super::fault::{force_incomplete_once, LeftBehind};
let (child, root, _descendant) = live_member_with_descendant();
let kernel = exiting_root(root);
force_incomplete_once(LeftBehind::Root);
let outcome = super::hard_kill(&kernel, root, &super::ByIdentity::new(&kernel, root)).outcome();
let answer = TreeKilled::default().settle(Some(&walker(root)), outcome, true).answer;
assert!(
matches!(&answer, Err(crate::error::Error::Unassessable { detail, .. }) if detail.contains("had begun to exit")),
"{answer:?}"
);
assert_both_signalled("hard_kill", child, libc::SIGKILL);
}
#[skuld::test]
fn a_kill_of_a_begun_root_that_leaves_a_member_says_the_exit_had_begun() {
use super::fault::{force_incomplete_once, LeftBehind};
let (child, root, _descendant) = live_member_with_descendant();
let kernel = exiting_root(root);
force_incomplete_once(LeftBehind::Descendant);
let TreeKillOutcome::Ran(Err(left)) =
super::hard_kill(&kernel, root, &super::ByIdentity::new(&kernel, root)).outcome()
else {
panic!("a member is left");
};
assert!(names_the_exit(&left.members), "{:?}", left.members);
assert_both_signalled("hard_kill", child, libc::SIGKILL);
}
#[skuld::test]
fn an_unreadable_exit_flag_with_everything_killed_is_not_something_left_running() {
let (child, root, _descendant) = live_member_with_descendant();
let kernel = flag_unreadable(root);
let killed = TreeKilled::default();
let outcome = super::hard_kill(&kernel, root, &super::ByIdentity::new(&kernel, root)).outcome();
let first = killed.settle(Some(&walker(root)), outcome, true).answer;
assert!(
matches!(&first, Err(crate::error::Error::Unassessable { detail, .. }) if detail.contains("could not be read")),
"the read's error is the answer: {first:?}"
);
let later = killed
.settle(Some(&walker(root)), TreeKillOutcome::NoRoot(root.pid(), None), true)
.answer;
let Err(crate::error::Error::Unassessable { detail, .. }) = &later else {
panic!("the tree is not vouched for: {later:?}");
};
assert!(
detail.contains("whether the root's exit had begun"),
"the answer names the unread exit: {detail}"
);
assert!(
!detail.contains("a kill left something running"),
"nothing was left running: {detail}"
);
assert_both_signalled("hard_kill", child, libc::SIGKILL);
}
#[skuld::test]
fn a_graceful_sweep_over_a_root_caught_exiting_stands_on_its_own_listing() {
let tree = crate::test_child::foreign::tree_of(crate::test_child::TERM_IGNORING_TREE_ARGV);
let crate::test_child::foreign::SurvivorTree {
mut child,
process,
members,
..
} = tree;
let settling = process.begin_graceful().expect("the SIGTERM walk");
let kernel = with_exiting_root(FakeKernel::over(&HostKernel), process.id());
process
.kill_tree_settling("graceful_shutdown_tree", &kernel, Some(&settling))
.expect("every member the call's own listing reached is killed");
for member in &members {
assert_eq!(
member.is_alive(),
crate::identity::Liveness::Dead,
"{member:?} ignored the SIGTERM and is killed"
);
}
assert_eq!(child.wait().expect("reap the root").signal(), Some(libc::SIGKILL));
}