use crate::containment::unix::fault::{record_kill_group, record_term_group};
use crate::log_capture::warns_since;
use crate::test_child::reaped_elsewhere::{
assert_one_reaped_elsewhere_warn, assert_one_untrusted_number_warn, kill_own_child, reap_behind, MAY_STILL_RUN,
REAPED_ELSEWHERE,
};
use crate::test_child::Console;
#[cfg(target_os = "linux")]
use crate::test_groups::{pidfd_group_signal, Group};
fn session_sleep() -> crate::tokio::Command {
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(["sleep", "300"]);
cmd.contain_with(crate::ContainMode::Session);
cmd
}
#[cfg(target_os = "linux")]
fn session_cat() -> crate::tokio::Command {
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(["cat"]);
cmd.contain_with(crate::ContainMode::Session);
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd
}
fn foreign_reaped_group_root() -> (crate::tokio::Child, u32) {
let child = session_sleep().spawn().expect("spawn");
let pid = child.pid();
kill_own_child(pid);
reap_behind(pid);
(child, pid)
}
#[skuld::test]
async fn tokio_kill_tree_after_a_foreign_reap_sends_no_killpg() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let (mut child, pgid) = foreign_reaped_group_root();
let mark = crate::log_capture::mark();
let err = child
.kill_tree()
.expect_err("a group whose root was reaped elsewhere is not assessable");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
recorder.killed(),
Vec::<i32>::new(),
"no killpg by a reaped root's number"
);
assert_one_reaped_elsewhere_warn(mark, pgid);
}
#[skuld::test]
async fn tokio_terminate_tree_after_a_foreign_reap_sends_no_signal() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_term_group();
let (child, pgid) = foreign_reaped_group_root();
let mark = crate::log_capture::mark();
let err = child
.terminate_tree()
.expect_err("a group whose root was reaped elsewhere is not assessable");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
recorder.termed(),
Vec::<i32>::new(),
"no SIGTERM by a reaped root's number"
);
assert_one_reaped_elsewhere_warn(mark, pgid);
}
#[skuld::test]
async fn tokio_kill_tree_after_own_wait_whose_pid_reads_unknown_sends_no_killpg() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let mut child = session_sleep().spawn().expect("spawn");
let pid = child.pid();
kill_own_child(pid);
child.wait().await.expect("this handle reaps the root");
let _unknown = crate::child::fault::force_next_root_read(crate::identity::Resolved::Unknown);
let mark = crate::log_capture::mark();
let err = child
.kill_tree()
.expect_err("a group whose root's pid cannot be read is not assessable");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
recorder.killed(),
Vec::<i32>::new(),
"no killpg by a number that may name another process"
);
assert_one_untrusted_number_warn(mark, pid);
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_kill_tree_of_a_root_its_handle_cannot_read_kills_it_through_its_pidfd_alone() {
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
use crate::test_child::reaped_elsewhere::ROOT_KILLED_BY_PIDFD;
use crate::wait::exit_only::seams::{assert_peeks_exhausted, force_peek_once};
use std::os::unix::process::ExitStatusExt as _;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let mut child = session_cat().spawn().expect("spawn");
let stdin = child.stdin().expect("stdin pipe");
let pid = child.pid();
let mark = crate::log_capture::mark();
let sends = crate::send_log::Capture::start();
let forced = force_peek_once(Err(std::io::Error::other("forced peek failure")));
let err = child
.kill_tree()
.expect_err("a group whose root nothing can answer for is not assessable");
assert_peeks_exhausted();
drop(forced);
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
recorder.killed(),
Vec::<i32>::new(),
"no killpg by a number that may name another process"
);
assert_eq!(
sends.entries(),
[(pid, crate::signal::Sig::Kill, crate::send_log::Via::Pidfd)],
"the root is killed through its pidfd, and nothing else is sent"
);
assert_one_untrusted_number_warn(mark, pid);
let warns = warns_since(mark, "");
assert!(warns[0].contains(ROOT_KILLED_BY_PIDFD), "{warns:?}");
drop(stdin);
let status = child.wait().await.expect("reap the root");
assert_eq!(status.signal(), Some(libc::SIGKILL), "the pidfd kill ended the root");
}
#[cfg(target_os = "linux")]
fn term_ignoring_session_cat() -> crate::tokio::Command {
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(["sh", "-c", r#"trap "" TERM; echo ready; exec cat"#]);
cmd.contain_with(crate::ContainMode::Session);
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd.stdout(crate::Stdio::pipe_out()).expect("stdout pipe");
cmd
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_terminate_tree_of_a_root_its_handle_cannot_read_terms_it_through_its_pidfd_alone() {
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
use crate::test_child::reaped_elsewhere::ROOT_TERMED_BY_PIDFD;
use crate::wait::exit_only::seams::{assert_peeks_exhausted, force_peek_once};
use std::os::unix::process::ExitStatusExt as _;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let termed = record_term_group();
let mut child = session_cat().spawn().expect("spawn");
let stdin = child.stdin().expect("stdin pipe");
let pid = child.pid();
let mark = crate::log_capture::mark();
let sends = crate::send_log::Capture::start();
let forced = force_peek_once(Err(std::io::Error::other("forced peek failure")));
let err = child
.terminate_tree()
.expect_err("a group whose root nothing can answer for is not assessable");
assert_peeks_exhausted();
drop(forced);
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
termed.termed(),
Vec::<i32>::new(),
"no SIGTERM by a number that may name another process"
);
assert_eq!(
sends.entries(),
[(pid, crate::signal::Sig::Term, crate::send_log::Via::Pidfd)],
"the root is signalled through its pidfd, and nothing else is sent"
);
assert_one_untrusted_number_warn(mark, pid);
let warns = warns_since(mark, "");
assert!(warns[0].contains(ROOT_TERMED_BY_PIDFD), "{warns:?}");
drop(stdin);
let status = child.wait().await.expect("reap the root");
assert_eq!(status.signal(), Some(libc::SIGTERM), "the pidfd SIGTERM ended the root");
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_graceful_shutdown_tree_of_a_root_its_handle_cannot_read_escalates_through_its_pidfd_alone() {
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
use crate::signal::Sig::{Kill, Term};
use crate::test_child::reaped_elsewhere::{ROOT_KILLED_BY_PIDFD, ROOT_TERMED_BY_PIDFD};
use crate::wait::exit_only::seams::{assert_peeks_exhausted, force_peek_once};
use std::os::unix::process::ExitStatusExt as _;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let killed = record_kill_group();
let termed = record_term_group();
let mut child = term_ignoring_session_cat().spawn().expect("spawn");
let stdin = child.stdin().expect("stdin pipe");
let pid = child.pid();
let mut ready = String::new();
::tokio::io::AsyncBufReadExt::read_line(
&mut ::tokio::io::BufReader::new(child.stdout().expect("stdout pipe")),
&mut ready,
)
.await
.expect("the root ignores SIGTERM");
let mark = crate::log_capture::mark();
let sends = crate::send_log::Capture::start();
let forced = force_peek_once(Err(std::io::Error::other("forced peek failure")));
let err = child
.graceful_shutdown_tree(std::time::Duration::ZERO)
.await
.expect_err("a group whose root nothing can answer for is not assessable");
assert_peeks_exhausted();
drop(forced);
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(killed.killed(), Vec::<i32>::new(), "no killpg by the root's number");
assert_eq!(termed.termed(), Vec::<i32>::new(), "no SIGTERM by the root's number");
let via_pidfd = crate::send_log::Via::Pidfd;
assert_eq!(
sends.entries(),
[(pid, Term, via_pidfd), (pid, Kill, via_pidfd)],
"the SIGTERM, then the kill, both through the pidfd"
);
assert_one_untrusted_number_warn(mark, pid);
let warns = warns_since(mark, "");
assert!(warns[0].contains(ROOT_TERMED_BY_PIDFD), "{warns:?}");
assert!(warns[0].contains(ROOT_KILLED_BY_PIDFD), "{warns:?}");
drop(stdin);
let status = child.wait().await.expect("reap the root");
assert_eq!(status.signal(), Some(libc::SIGKILL), "the kill ended the root");
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_graceful_shutdown_tree_of_a_root_its_handle_cannot_read_sends_no_kill_to_one_that_exited() {
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
use crate::signal::Sig::Term;
use crate::test_child::reaped_elsewhere::{ROOT_KILLED_BY_PIDFD, ROOT_TERMED_BY_PIDFD};
use crate::wait::exit_only::seams::{assert_peeks_exhausted, force_peek_once};
use std::os::unix::process::ExitStatusExt as _;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let mut child = session_cat().spawn().expect("spawn");
let stdin = child.stdin().expect("stdin pipe");
let pid = child.pid();
let mark = crate::log_capture::mark();
let sends = std::rc::Rc::new(crate::send_log::Capture::start());
let forced = force_peek_once(Err(std::io::Error::other("forced peek failure")));
let exited_first = crate::graceful_hooks::at(crate::graceful_hooks::HookPoint::AfterTerminate, {
let sends = std::rc::Rc::clone(&sends);
move || {
assert_eq!(
sends.entries(),
[(pid, Term, crate::send_log::Via::Pidfd)],
"the SIGTERM went out before the grace"
);
drop(stdin);
crate::test_child::wait_until_zombie(pid);
}
});
let past = crate::tokio::wait::tokio_now()
.checked_sub(std::time::Duration::from_secs(3600))
.expect("an hour before now");
let lagging = crate::tokio::wait::now_override::install(past);
let err = child
.graceful_shutdown_tree(std::time::Duration::from_secs(60))
.await
.expect_err("a group whose root nothing can answer for is not assessable");
drop(lagging);
drop(exited_first);
assert_peeks_exhausted();
drop(forced);
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
sends.entries(),
[(pid, Term, crate::send_log::Via::Pidfd)],
"only the SIGTERM, through the pidfd"
);
assert_one_untrusted_number_warn(mark, pid);
let warns = warns_since(mark, "");
assert!(warns[0].contains(ROOT_TERMED_BY_PIDFD), "{warns:?}");
assert!(!warns[0].contains(ROOT_KILLED_BY_PIDFD), "{warns:?}");
let status = child.wait().await.expect("reap the root");
assert_eq!(status.signal(), Some(libc::SIGTERM), "the SIGTERM ended the root");
}
#[skuld::test]
async fn tokio_graceful_shutdown_tree_after_a_foreign_reap_warns_once_and_ends() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let killed = record_kill_group();
let termed = record_term_group();
let (mut child, pgid) = foreign_reaped_group_root();
let mark = crate::log_capture::mark();
let err = child
.graceful_shutdown_tree(std::time::Duration::from_secs(1))
.await
.expect_err("a group whose root was reaped elsewhere is not assessable");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
killed.killed(),
Vec::<i32>::new(),
"no killpg by a reaped root's number"
);
assert_eq!(
termed.termed(),
Vec::<i32>::new(),
"no SIGTERM by a reaped root's number"
);
assert_one_reaped_elsewhere_warn(mark, pgid);
let warns = warns_since(mark, "");
assert_eq!(warns.len(), 1, "exactly one warn: {warns:?}");
assert!(
!warns[0].contains("was sent before it"),
"no SIGTERM was sent: {warns:?}"
);
}
#[skuld::test]
async fn tokio_graceful_shutdown_tree_after_own_wait_claims_no_sigterm_it_did_not_send() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
use crate::child::fault::force_next_root_read;
use crate::identity::Resolved;
use crate::test_child::reaped_elsewhere::UNTRUSTED_NUMBER;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let killed = record_kill_group();
let termed = record_term_group();
let mut child = session_sleep().spawn().expect("spawn");
let pid = child.pid();
kill_own_child(pid);
child.wait().await.expect("this handle reaps the root");
let other = child.id().expect(crate::test_child::IDENTITY_READ).with_other_value();
let _reads = force_next_root_read(Resolved::Found(other));
let _unreadable = crate::graceful_hooks::at(crate::graceful_hooks::HookPoint::AfterTerminate, || {
std::mem::forget(force_next_root_read(Resolved::Unknown))
});
let mark = crate::log_capture::mark();
let err = child
.graceful_shutdown_tree(std::time::Duration::ZERO)
.await
.expect_err("a sweep whose root's pid cannot be read is not assessable");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
termed.termed(),
Vec::<i32>::new(),
"no SIGTERM by a reaped root's number"
);
assert_eq!(
killed.killed(),
Vec::<i32>::new(),
"no killpg by a reaped root's number"
);
let warns = warns_since(mark, "");
assert_eq!(warns.len(), 1, "exactly one warn: {warns:?}");
assert!(warns[0].contains(UNTRUSTED_NUMBER), "{warns:?}");
#[cfg(target_os = "linux")]
assert!(
!warns[0].contains("was sent before it"),
"no SIGTERM was sent: {warns:?}"
);
#[cfg(target_os = "macos")]
assert!(
warns[0].contains("though the SIGTERM to any marker holders was sent before it"),
"the SIGTERM went to the marker holders alone: {warns:?}"
);
}
#[skuld::test]
async fn tokio_kill_tree_of_a_backend_forgotten_unproven_treats_the_root_as_unknown() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let groups = record_kill_group();
#[cfg(target_os = "macos")]
let holders = crate::containment::fdmarker::fault::record_holder_kills();
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
cmd.contain_with(crate::ContainMode::Session);
let mut child = cmd.spawn().expect("spawn");
let pid = child.pid();
child
.proc_mut()
.forget_as("was forgotten by a test", super::proc_source::Forgotten::Unproven);
let mark = crate::log_capture::mark();
#[cfg(target_os = "linux")]
let sends = crate::send_log::Capture::start();
let result = child.kill_tree();
assert_eq!(
crate::log_capture::records_since_on_current_thread(mark, REAPED_ELSEWHERE),
[],
"a root not shown reaped is not called reaped"
);
assert_eq!(
groups.killed(),
Vec::<i32>::new(),
"no killpg by a number that may name another process"
);
#[cfg(target_os = "linux")]
{
use crate::test_child::reaped_elsewhere::ROOT_KILLED_BY_PIDFD;
let err = result.expect_err("a group whose root nothing can answer for is not assessable");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
sends.entries(),
[(pid, crate::signal::Sig::Kill, crate::send_log::Via::Pidfd)],
"the root alone is killed, through its pidfd"
);
let warns = warns_since(mark, "");
assert_eq!(warns.len(), 1, "exactly one warn: {warns:?}");
assert!(
warns[0].contains(MAY_STILL_RUN),
"it says the root may still be running: {warns:?}"
);
assert!(
warns[0].contains(&format!("pgid {pid}")),
"it names the group: {warns:?}"
);
assert!(warns[0].contains(ROOT_KILLED_BY_PIDFD), "{warns:?}");
}
#[cfg(target_os = "macos")]
{
let _ = MAY_STILL_RUN;
crate::child::unpinned_public_tests::assert_holders_only(
&result.expect_err("the sweep skipped the rest of the tree"),
"kill_tree",
crate::child::unpinned_public_tests::Untrusted::Unknown,
);
assert!(
holders.killed().contains(&pid),
"the root holds the marker: {:?}",
holders.killed()
);
}
drop(writer);
reap_behind(pid);
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_graceful_shutdown_tree_of_a_backend_forgotten_unproven_shuts_the_root_down_through_its_pidfd() {
use crate::graceful_hooks::{at, HookPoint};
use crate::signal::Sig::Term;
use crate::test_child::reaped_elsewhere::{reap_behind_status, ROOT_KILLED_BY_PIDFD, ROOT_TERMED_BY_PIDFD};
use std::os::unix::process::ExitStatusExt as _;
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let killed = record_kill_group();
let termed = record_term_group();
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
cmd.contain_with(crate::ContainMode::Session);
let mut child = cmd.spawn().expect("spawn");
let pid = child.pid();
child
.proc_mut()
.forget_as("was forgotten by a test", super::proc_source::Forgotten::Unproven);
let mark = crate::log_capture::mark();
let sends = std::rc::Rc::new(crate::send_log::Capture::start());
let exited_first = at(HookPoint::AfterTerminate, {
let sends = std::rc::Rc::clone(&sends);
move || {
assert_eq!(
sends.entries(),
[(pid, Term, crate::send_log::Via::Pidfd)],
"the SIGTERM went out before the grace"
);
drop(writer);
crate::test_child::wait_until_zombie(pid);
}
});
let err = child
.graceful_shutdown_tree(std::time::Duration::ZERO)
.await
.expect_err("a group whose root nothing can answer for is not assessable");
drop(exited_first);
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
sends.entries(),
[(pid, Term, crate::send_log::Via::Pidfd)],
"only the SIGTERM, through the pidfd"
);
assert_eq!(killed.killed(), Vec::<i32>::new(), "no killpg by the root's number");
assert_eq!(termed.termed(), Vec::<i32>::new(), "no SIGTERM by the root's number");
let warns = warns_since(mark, "");
assert_eq!(warns.len(), 1, "exactly one warn: {warns:?}");
assert!(warns[0].contains(MAY_STILL_RUN), "{warns:?}");
assert!(warns[0].contains(ROOT_TERMED_BY_PIDFD), "{warns:?}");
assert!(!warns[0].contains(ROOT_KILLED_BY_PIDFD), "{warns:?}");
assert_eq!(
reap_behind_status(pid).signal(),
Some(libc::SIGTERM),
"the SIGTERM ended the root"
);
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_graceful_shutdown_tree_of_a_backend_forgotten_unproven_shuts_its_group_down(
#[fixture(pidfd_group_signal)] _group: &Group,
) {
crate::tokio::test_runtime::assert_current_thread();
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
cmd.contain_with(crate::ContainMode::Session);
let mut child = cmd.spawn().expect("spawn");
let pid = child.pid();
child
.proc_mut()
.forget_as("was forgotten by a test", super::proc_source::Forgotten::Unproven);
let capture = crate::send_log::Capture::start();
let result = child.graceful_shutdown_tree(std::time::Duration::ZERO).await;
let steps = capture.group_steps();
crate::send_log::assert_group_signalled_first_through_the_pidfd(&steps, libc::SIGTERM);
match result {
Err(crate::error::Error::Unassessable { detail, source: None }) => {
assert!(detail.contains("could not say whether it was reaped"), "{detail}");
assert!(!detail.contains(REAPED_ELSEWHERE), "{detail}");
}
other => panic!("expected the tree down without the root's status, got {other:?}"),
}
drop(writer);
reap_behind(pid);
}
#[cfg(target_os = "macos")]
async fn escaped_holder_tree(
mode: crate::ContainMode,
ignore_term: bool,
) -> (
crate::tokio::Child,
u32,
crate::tokio::ChildStdin,
crate::tokio::ChildStderr,
) {
use ::tokio::io::AsyncBufReadExt as _;
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args([
"sh",
"-c",
&crate::test_child::reaped_elsewhere::escaped_holder_script(ignore_term),
]);
cmd.contain_with(mode);
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd.stdout(crate::Stdio::pipe_out()).expect("stdout pipe");
cmd.stderr(crate::Stdio::pipe_out()).expect("stderr pipe");
let mut child = cmd.spawn().expect("spawn");
assert_eq!(child.containment(), crate::Containment::FdMarker);
let mut line = String::new();
::tokio::io::BufReader::new(child.stdout().expect("stdout pipe"))
.read_line(&mut line)
.await
.expect("read the holder's pid");
let holder: u32 = line.trim().parse().expect("the holder's pid");
crate::test_child::reaped_elsewhere::assert_left_the_group(holder, child.pid());
let stdin = child.stdin().expect("stdin pipe");
let stderr = child.stderr().expect("stderr pipe");
(child, holder, stdin, stderr)
}
#[cfg(target_os = "macos")]
async fn assert_holder_dies(mut stderr: crate::tokio::ChildStderr) {
use ::tokio::io::AsyncReadExt as _;
let mut rest = Vec::new();
stderr
.read_to_end(&mut rest)
.await
.expect("the holder's stderr reaches EOF");
assert!(rest.is_empty(), "unexpected stderr from the holder: {rest:?}");
}
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_kill_tree_after_own_wait_and_reuse_kills_a_holder_that_left_the_group() {
use crate::identity::Resolved;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let groups = record_kill_group();
let walks = crate::containment::treewalk::fault::record_walks();
let holders = crate::containment::fdmarker::fault::record_holder_kills();
let (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::Session, false).await;
child.kill().expect("kill the root");
child.wait().await.expect("this handle reaps the root");
let other = child.id().expect(crate::test_child::IDENTITY_READ).with_other_value();
let _reused = crate::child::fault::force_next_root_read(Resolved::Found(other));
child.kill_tree().expect("every holder was reached");
assert_eq!(
groups.killed(),
Vec::<i32>::new(),
"no killpg by a reaped root's number"
);
assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
assert!(
holders.killed().contains(&holder),
"the holder that left the group is killed: {:?}",
holders.killed()
);
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(
warns_since(mark, ""),
Vec::<String>::new(),
"the tree was killed: the drop does not warn"
);
assert_holder_dies(stderr).await;
}
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_kill_tree_after_own_wait_of_a_marker_with_a_group_is_ok_and_marks_the_tree_killed() {
use crate::identity::Resolved;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let groups = record_kill_group();
let walks = crate::containment::treewalk::fault::record_walks();
let holders = crate::containment::fdmarker::fault::record_holder_kills();
let (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::Session, false).await;
let pid = child.pid();
child.kill().expect("kill the root");
child.wait().await.expect("this handle reaps the root");
let _gone = crate::child::fault::force_root_reads(Resolved::Gone);
child.kill_tree().expect("the group and every holder were reached");
let by_id = groups.killed();
assert!(
!by_id.is_empty() && by_id.iter().all(|g| *g == pid as i32),
"the group is killed by its id: {by_id:?}"
);
assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
assert_eq!(
holders.signals_to(holder),
[nix::sys::signal::Signal::SIGKILL],
"the holder that left the group is killed"
);
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(
warns_since(mark, ""),
Vec::<String>::new(),
"the tree was killed: the drop does not warn"
);
assert_holder_dies(stderr).await;
}
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_graceful_shutdown_tree_after_own_wait_of_a_marker_with_a_group_answers_the_collected_status() {
use crate::identity::Resolved;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let killed = record_kill_group();
let termed = record_term_group();
let walks = crate::containment::treewalk::fault::record_walks();
let holders = crate::containment::fdmarker::fault::record_holder_kills();
let (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::Session, true).await;
let pid = child.pid();
child.kill().expect("kill the root");
let collected = child.wait().await.expect("this handle reaps the root");
let _gone = crate::child::fault::force_root_reads(Resolved::Gone);
let status = child
.graceful_shutdown_tree(std::time::Duration::ZERO)
.await
.expect("the group and every holder were reached");
assert_eq!(status, collected, "the status the wait collected");
for (what, by_id) in [("SIGTERM", termed.termed()), ("kill", killed.killed())] {
assert!(
!by_id.is_empty() && by_id.iter().all(|g| *g == pid as i32),
"the group's {what} by its id: {by_id:?}"
);
}
assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
assert_eq!(
holders.signals_to(holder),
[nix::sys::signal::Signal::SIGTERM, nix::sys::signal::Signal::SIGKILL],
"the SIGTERM, then the kill"
);
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(
warns_since(mark, ""),
Vec::<String>::new(),
"the tree was killed: the drop does not warn"
);
assert_holder_dies(stderr).await;
}
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_kill_tree_after_a_foreign_reap_kills_a_holder_that_left_the_group() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let groups = record_kill_group();
let walks = crate::containment::treewalk::fault::record_walks();
let holders = crate::containment::fdmarker::fault::record_holder_kills();
let (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::Session, false).await;
let pid = child.pid();
child.kill().expect("kill the root");
reap_behind(pid);
let mark = crate::log_capture::mark();
let err = child
.kill_tree()
.expect_err("a group whose root was reaped elsewhere is not assessable");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
groups.killed(),
Vec::<i32>::new(),
"no killpg by a reaped root's number"
);
assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
assert!(
holders.killed().contains(&holder),
"the holder that left the group is killed: {:?}",
holders.killed()
);
assert_one_reaped_elsewhere_warn(mark, pid);
assert_holder_dies(stderr).await;
}
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_graceful_shutdown_tree_after_a_foreign_reap_kills_a_holder_that_ignores_sigterm() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let killed = record_kill_group();
let termed = record_term_group();
let walks = crate::containment::treewalk::fault::record_walks();
let holders = crate::containment::fdmarker::fault::record_holder_kills();
let (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::Session, true).await;
let pid = child.pid();
child.kill().expect("kill the root");
reap_behind(pid);
let mark = crate::log_capture::mark();
let err = child
.graceful_shutdown_tree(std::time::Duration::ZERO)
.await
.expect_err("a group whose root was reaped elsewhere is not assessable");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
killed.killed(),
Vec::<i32>::new(),
"no killpg by a reaped root's number"
);
assert_eq!(
termed.termed(),
Vec::<i32>::new(),
"no SIGTERM by a reaped root's number"
);
assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
assert_eq!(
holders.signals_to(holder),
[nix::sys::signal::Signal::SIGTERM, nix::sys::signal::Signal::SIGKILL],
"the SIGTERM, then the kill"
);
assert_one_reaped_elsewhere_warn(mark, pid);
let warns = warns_since(mark, "");
assert_eq!(warns.len(), 1, "exactly one warn: {warns:?}");
assert_holder_dies(stderr).await;
}
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_kill_tree_of_a_walk_marker_after_a_foreign_reap_walks_nothing_and_kills_the_holder() {
use crate::test_child::reaped_elsewhere::assert_one_reaped_elsewhere_walk_warn;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let walks = crate::containment::treewalk::fault::record_walks();
let holders = crate::containment::fdmarker::fault::record_holder_kills();
let (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::TreeWalk, false).await;
let pid = child.pid();
child.kill().expect("kill the root");
reap_behind(pid);
let mark = crate::log_capture::mark();
let result = child.kill_tree();
assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
assert!(
matches!(result, Err(crate::error::Error::Unassessable { .. })),
"a walk whose root was reaped elsewhere is not assessable: {result:?}"
);
assert!(
holders.killed().contains(&holder),
"the holder is killed: {:?}",
holders.killed()
);
assert_one_reaped_elsewhere_walk_warn(mark, pid);
assert_holder_dies(stderr).await;
}
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_kill_tree_of_a_walk_marker_after_own_wait_and_reuse_walks_nothing_and_kills_the_holder() {
use crate::identity::Resolved;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let walks = crate::containment::treewalk::fault::record_walks();
let holders = crate::containment::fdmarker::fault::record_holder_kills();
let (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::TreeWalk, false).await;
child.kill().expect("kill the root");
child.wait().await.expect("this handle reaps the root");
let other = child.id().expect(crate::test_child::IDENTITY_READ).with_other_value();
let _reused = crate::child::fault::force_next_root_read(Resolved::Found(other));
let result = child.kill_tree();
assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
result.expect("every holder was reached");
assert!(
holders.killed().contains(&holder),
"the holder is killed: {:?}",
holders.killed()
);
assert_holder_dies(stderr).await;
}