use crate::child::spawn::fault;
use crate::child::spawn::identity_group_tests::{Container, ROOT_LEAVING_A_MEMBER};
use crate::error::ChildFate;
use crate::send_log::{assert_group_signalled_first_through_the_pidfd, Capture, GroupStep, Route};
use crate::test_child::member::{await_started, Member, RELEASE_FD, ROOT_WITH_A_MEMBER};
use crate::test_child::Console;
use crate::test_groups::{cgroup, pidfd_group_signal, Group};
#[derive(Debug, Clone, Copy)]
enum Root {
Running,
Exited,
ReapedBehind,
}
impl Root {
fn argv(self) -> [&'static str; 3] {
match self {
Root::Running | Root::ReapedBehind => ROOT_WITH_A_MEMBER,
Root::Exited => ROOT_LEAVING_A_MEMBER,
}
}
fn fate(self) -> ChildFate {
match self {
Root::Running | Root::Exited => ChildFate::Reaped,
Root::ReapedBehind => ChildFate::Gone,
}
}
}
struct Failed {
fate: ChildFate,
steps: Vec<GroupStep>,
member: Member,
pid: u32,
group_warns: Vec<String>,
}
fn left_running_note() -> String {
format!(
"members of the process group of {} may be left running",
crate::child::spawn::named(None)
)
}
fn failed_spawn(root: Root, mode: crate::ContainMode, placement: impl Sized) -> Failed {
failed_spawn_as(root, mode, placement, None)
}
fn failed_spawn_as(
root: Root,
mode: crate::ContainMode,
placement: impl Sized,
front: Option<crate::elevation::front::Front>,
) -> Failed {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (stdio, started, member) = Member::pipes();
let release = member.release_root_when_a_teardown_waits();
let reaps = fault::record_teardown_reaps();
let fronted = front.is_some();
let capture = Capture::start();
let mark = crate::log_capture::mark();
let spawned = {
let _failing = fault::fail_tokio_spawns_after_fork_then(move |pid| {
await_started(started);
if matches!(root, Root::ReapedBehind) {
unsafe { libc::kill(pid as i32, libc::SIGKILL) };
crate::test_child::reaped_elsewhere::reap_behind(pid);
}
});
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(root.argv());
cmd.contain_with(mode);
cmd.set_elevation_front(front);
cmd.stdin(stdio.stdin).expect("set stdin");
cmd.stdout(stdio.stdout).expect("set stdout");
cmd.stderr(stdio.stderr).expect("set stderr");
cmd.fd(RELEASE_FD, stdio.release).expect("set the release fd");
cmd.spawn()
};
let steps = capture.group_steps();
drop(placement);
drop(release);
if matches!(root, Root::Running) && !fronted {
reaps.assert_killed();
}
let pid = fault::take_forgotten_pid().expect("the seam forked a child");
let (_err, fate) = match spawned {
Ok(child) => panic!("the spawn must fail after its fork: {:?}", child.id()),
Err(e) => crate::child::spawn::failure::expect_may_have_started_with(e),
};
let group_warns = crate::log_capture::warns_since(mark, "left running")
.into_iter()
.filter(|text| text.contains("process group"))
.collect();
Failed {
fate,
steps,
member,
pid,
group_warns,
}
}
fn kills_the_group(root: Root, container: Container) {
let (mode, placement) = container.arm();
let failed = failed_spawn(root, mode, placement);
assert_eq!(failed.fate, root.fate(), "{root:?}");
assert_group_signalled_first_through_the_pidfd(&failed.steps, libc::SIGKILL);
failed.member.assert_killed();
assert_eq!(failed.group_warns, Vec::<String>::new(), "the group was killed");
}
fn below_6_9_sends_nothing_to_the_group(root: Root, container: Container) {
let old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
let (mode, placement) = container.arm();
let failed = failed_spawn(root, mode, placement);
drop(old_kernel);
let Failed {
fate,
steps,
member,
group_warns,
..
} = failed;
assert_eq!(fate, root.fate(), "{root:?}");
assert_eq!(
steps.first(),
Some(&GroupStep::Group(Route::Pidfd, libc::SIGKILL)),
"the group is sent the kill through the pidfd first: {steps:?}"
);
assert!(
!steps.iter().any(|s| matches!(
s,
GroupStep::Group(Route::Number, _)
| GroupStep::Leader(Route::Number, _)
| GroupStep::Member(Route::Number, ..)
)),
"nothing goes by a number: {steps:?}"
);
member.assert_survived();
assert_eq!(
group_warns.len(),
1,
"one warn says the members may be left running: {group_warns:?}"
);
assert!(group_warns[0].contains(&left_running_note()), "{group_warns:?}");
}
fn below_6_9_kills_the_group_of_a_root_its_leaf_did_not_take_by_its_number() {
let old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
let (mode, placement) = Container::LeafNotTaken.arm();
let failed = failed_spawn(Root::Running, mode, placement);
drop(old_kernel);
assert_eq!(failed.fate, ChildFate::Reaped);
assert_eq!(
failed.steps,
[
GroupStep::Group(Route::Pidfd, libc::SIGKILL),
GroupStep::Check,
GroupStep::Group(Route::Number, libc::SIGKILL)
]
);
failed.member.assert_killed();
assert_eq!(failed.group_warns, Vec::<String>::new(), "the group was killed");
}
#[skuld::test]
async fn a_tokio_spawn_failing_after_its_fork_kills_the_group_its_running_root_left_a_member_in(
#[fixture(pidfd_group_signal)] _group: &Group,
) {
kills_the_group(Root::Running, Container::Session);
}
#[skuld::test]
async fn a_tokio_spawn_failing_after_its_fork_kills_the_group_its_exited_root_left_a_member_in(
#[fixture(pidfd_group_signal)] _group: &Group,
) {
kills_the_group(Root::Exited, Container::Session);
}
#[skuld::test]
async fn a_tokio_spawn_failing_after_its_fork_kills_the_group_its_foreign_reaped_root_left_a_member_in(
#[fixture(pidfd_group_signal)] _group: &Group,
) {
kills_the_group(Root::ReapedBehind, Container::Session);
}
#[skuld::test]
async fn a_tokio_spawn_failing_after_its_fork_below_6_9_sends_nothing_to_its_running_roots_group() {
below_6_9_sends_nothing_to_the_group(Root::Running, Container::Session);
}
#[skuld::test]
async fn a_tokio_spawn_failing_after_its_fork_below_6_9_sends_nothing_to_its_exited_roots_group() {
below_6_9_sends_nothing_to_the_group(Root::Exited, Container::Session);
}
#[skuld::test]
async fn a_tokio_spawn_failing_after_its_fork_below_6_9_sends_nothing_to_its_foreign_reaped_roots_group() {
below_6_9_sends_nothing_to_the_group(Root::ReapedBehind, Container::Session);
}
#[skuld::test]
async fn a_tokio_spawn_failing_after_its_fork_under_a_tree_walk_sends_no_group_anything(
#[fixture(pidfd_group_signal)] _group: &Group,
) {
let failed = failed_spawn(Root::Running, crate::ContainMode::TreeWalk, ());
assert_eq!(failed.fate, ChildFate::Reaped, "killed and reaped through its pidfd");
assert!(failed.steps.is_empty(), "no group is sent anything: {:?}", failed.steps);
failed.member.assert_survived();
}
#[skuld::test]
async fn a_tokio_exec_failure_kills_a_group_with_no_member_and_sends_nothing_by_number() {
crate::tokio::test_runtime::assert_current_thread();
let capture = Capture::start();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(["cosca-no-such-program-142"]);
cmd.contain_with(crate::ContainMode::Session);
let (_err, fate) = crate::child::spawn::failure::expect_may_have_started_with(
cmd.spawn().expect_err("a missing program fails the spawn"),
);
assert_eq!(fate, ChildFate::Gone, "std had collected the child whose exec failed");
let steps = capture.group_steps();
assert_eq!(
steps,
[GroupStep::Group(Route::Pidfd, libc::SIGKILL)],
"one kill through the pidfd, answered with no member, and nothing by a number"
);
}
#[skuld::test]
async fn a_tokio_spawn_failing_after_its_fork_kills_the_group_its_leafless_strongest_root_left_a_member_in(
#[fixture(pidfd_group_signal)] _group: &Group,
) {
kills_the_group(Root::Running, Container::Leafless);
}
#[skuld::test]
async fn a_tokio_spawn_failing_after_its_fork_below_6_9_sends_nothing_to_its_leafless_strongest_roots_group() {
below_6_9_sends_nothing_to_the_group(Root::Running, Container::Leafless);
}
#[skuld::test]
async fn a_nested_tokio_spawn_failing_after_its_fork_sends_no_group_anything() {
use crate::test_own_process::{own_process, test_path};
use crate::test_spawn::spawn;
let Some(_done) = own_process(
test_path!(a_nested_tokio_spawn_failing_after_its_fork_sends_no_group_anything),
spawn,
) else {
return;
};
std::env::set_var(crate::containment::NESTED_ENV, "1");
let failed = failed_spawn(Root::Running, crate::ContainMode::Session, ());
assert_eq!(failed.fate, ChildFate::Reaped, "killed and reaped through its pidfd");
assert!(failed.steps.is_empty(), "no group is sent anything: {:?}", failed.steps);
failed.member.assert_survived();
}
#[skuld::test]
async fn cgroup_a_tokio_spawn_failing_after_its_fork_kills_the_group_of_a_root_its_leaf_did_not_take(
#[fixture(cgroup)] _cgroup: &Group,
#[fixture(pidfd_group_signal)] _group: &Group,
) {
kills_the_group(Root::Running, Container::LeafNotTaken);
}
#[skuld::test]
async fn cgroup_a_tokio_spawn_failing_after_its_fork_below_6_9_kills_the_group_of_a_root_its_leaf_did_not_take_by_its_number(
#[fixture(cgroup)] _cgroup: &Group,
) {
below_6_9_kills_the_group_of_a_root_its_leaf_did_not_take_by_its_number();
}
#[skuld::test]
async fn a_tokio_spawn_of_a_front_failing_after_its_fork_sends_its_group_nothing() {
use crate::elevation::{Backend, ElevatedVia};
let front = crate::elevation::front::front(Some(&ElevatedVia::Wrapped(Backend::Sudo)));
assert!(front.is_some(), "sudo launches a front");
let failed = failed_spawn_as(Root::Running, crate::ContainMode::Session, (), front);
let Failed {
fate,
steps,
member,
pid,
..
} = failed;
assert_eq!(fate, ChildFate::Running { id: None }, "the front is left running");
assert!(steps.is_empty(), "the front's group is sent nothing: {steps:?}");
unsafe { libc::kill(pid as i32, libc::SIGKILL) };
crate::child::front_kill_tests::reap(pid).expect("the front is left unreaped");
let said = member.end();
assert!(said.contains("survived"), "the group's member was killed: {said:?}");
}
#[skuld::test]
async fn a_tokio_spawn_failing_after_its_fork_whose_group_kill_is_refused_warns_once() {
let refused = crate::containment::unix::fault::refuse_group_sends();
let failed = failed_spawn(Root::Running, crate::ContainMode::Session, ());
drop(refused);
let Failed {
fate,
member,
group_warns,
..
} = failed;
assert_eq!(
fate,
ChildFate::Reaped,
"the root is killed and reaped through its pidfd"
);
member.assert_survived();
assert_eq!(group_warns.len(), 1, "{group_warns:?}");
let named = crate::child::spawn::named(None);
assert!(
group_warns[0].contains(&format!("could not kill the process group of {named}"))
&& group_warns[0].contains("may be left running"),
"{group_warns:?}"
);
}
#[skuld::test]
async fn a_tokio_exec_failure_whose_group_kill_is_refused_warns_that_the_reaped_child_may_never_have_run() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let refused = crate::containment::unix::fault::refuse_group_sends();
let mark = crate::log_capture::mark();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(["cosca-no-such-program-142"]);
cmd.contain_with(crate::ContainMode::Session);
let (_err, fate) = crate::child::spawn::failure::expect_may_have_started_with(
cmd.spawn().expect_err("a missing program fails the spawn"),
);
drop(refused);
assert_eq!(fate, ChildFate::Gone, "std had collected the child whose exec failed");
let warns = crate::log_capture::warns_since(mark, "the process group of");
assert_eq!(warns.len(), 1, "{warns:?}");
let named = crate::child::spawn::named(None);
assert!(
warns[0].contains(&format!("could not kill the process group of {named}")),
"{warns:?}"
);
assert!(
warns[0].contains("already reaped, so it may never have run the program"),
"{warns:?}"
);
}
#[skuld::test]
async fn a_tokio_exec_failure_below_6_9_warns_that_the_reaped_child_may_never_have_run() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
let capture = Capture::start();
let mark = crate::log_capture::mark();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(["cosca-no-such-program-142"]);
cmd.contain_with(crate::ContainMode::Session);
let (_err, fate) = crate::child::spawn::failure::expect_may_have_started_with(
cmd.spawn().expect_err("a missing program fails the spawn"),
);
drop(old_kernel);
assert_eq!(fate, ChildFate::Gone, "std had collected the child whose exec failed");
assert_eq!(capture.group_steps(), [GroupStep::Group(Route::Pidfd, libc::SIGKILL)]);
let warns = crate::log_capture::warns_since(mark, "the process group of");
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(warns[0].contains(&left_running_note()), "{warns:?}");
assert!(
warns[0].contains("already reaped, so it may never have run the program"),
"{warns:?}"
);
}