cosca 0.9.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! Linux: a spawn that fails because its root was reaped by another party before its identity was
//! read, for a root that left a member in the process group that is its containment: a session, or
//! the group a cgroup leaf that did not take the root falls back to. On 6.9+ the cleanup kills the
//! group through the root's pidfd while it has a member, whoever reaped the root; below 6.9 it kills
//! the root alone and warns once that members of its group may be left running.

use crate::child::spawn::failure::expect_may_have_started_with;
use crate::child::spawn::fault;
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};
use crate::test_child::reaped_elsewhere::reap_behind;
use crate::test_child::Console;
use crate::test_groups::{cgroup, pidfd_group_signal, Group};

/// A session root `sh` that leaves a member in its group, says `started` and exits, as
/// `sh -c 'sleep 300 & exit'` does. Its member is [`ROOT_WITH_A_MEMBER`]'s.
pub(crate) const ROOT_LEAVING_A_MEMBER: [&str; 3] = [
    "sh",
    "-c",
    "exec 3<&0; (cat <&3 3<&- >/dev/null && echo survived >&2) & echo started",
];

/// Arms the spawn's `BeforeIdentity` hook: it waits for the member, then reaps the root elsewhere,
/// so the identity read finds it `Gone`. Dropping the guard disarms it.
pub(crate) fn arm_reaped_elsewhere(started: crate::PipeReader) -> Armed {
    let pid = std::rc::Rc::new(std::cell::Cell::new(0));
    let hook = fault::set_at(fault::SpawnPoint::BeforeIdentity, {
        let pid = std::rc::Rc::clone(&pid);
        move || {
            pid.set(fault::spawn_pid());
            await_started(started);
            reap_behind(fault::spawn_pid());
        }
    });
    Armed { _hook: hook, pid }
}

/// [`Failure::arm`]'s guard.
pub(crate) struct Armed {
    _hook: crate::oneshot_hook::Armed,
    pid: std::rc::Rc<std::cell::Cell<u32>>,
}

impl Armed {
    /// The pid of the root the spawn forked; set once the hook has run.
    pub(crate) fn pid(&self) -> u32 {
        let pid = self.pid.get();
        assert_ne!(pid, 0, "the spawn's hook has run");
        pid
    }
}

/// The record the cleanup owes when it cannot kill the group of `pid`: one `warn` that names it.
pub(crate) fn left_running_note(pid: u32) -> String {
    format!("members of the process group of pid {pid} may be left running")
}

/// What a failed spawn did, as the tests below read it.
pub(crate) struct Failed {
    pub(crate) fate: ChildFate,
    pub(crate) steps: Vec<GroupStep>,
    pub(crate) member: Member,
    /// The levels of the records that say members of the root's group may be left running.
    pub(crate) left_running: Vec<log::Level>,
    /// The `cleanup` of the spawn's error: what failed in its teardown.
    pub(crate) cleanup: Option<crate::error::Failures>,
}

/// The root's containment, whose process group the root leads.
#[derive(Debug, Clone, Copy)]
pub(crate) enum Container {
    Session,
    /// A cgroup leaf whose placement write is made to fail, so the process group set before the
    /// spawn is the containment. Asks for `BestEffort`: `Strongest` fails that spawn.
    LeafNotTaken,
    /// `BestEffort` with no cgroup leaf made (what a host without a delegated cgroup gives), so the
    /// process group set before the spawn is the containment.
    Leafless,
}

impl Container {
    /// The mode to contain the root with, and a guard that ends a forced placement failure.
    pub(crate) fn arm(self) -> (crate::ContainMode, PlacementForced) {
        match self {
            Container::Session => (crate::ContainMode::Session, PlacementForced),
            Container::LeafNotTaken => {
                crate::containment::cgroup::fault::set_force_placement_write_result(
                    crate::containment::cgroup::fault::ForcedWrite::Returns0,
                );
                (crate::ContainMode::BestEffort, PlacementForced)
            }
            Container::Leafless => {
                crate::containment::cgroup::fault::set_force_leaf_open_failure(true);
                (crate::ContainMode::BestEffort, PlacementForced)
            }
        }
    }
}

/// Clears this thread's forced placement result, which only the forked child takes, and a forced
/// leaf failure the spawn did not reach.
pub(crate) struct PlacementForced;

impl Drop for PlacementForced {
    fn drop(&mut self) {
        crate::containment::cgroup::fault::take_force_placement_write_result();
        crate::containment::cgroup::fault::take_force_leaf_open_failure();
    }
}

/// Spawns the root in `container`, which is reaped elsewhere before the identity read: the spawn's
/// fate, the group steps it recorded and the root's member.
fn failed_spawn(container: Container) -> Failed {
    crate::log_capture::install();
    let (stdio, started, member) = Member::pipes();
    // Released between the cleanup's kill and wait: a missed kill fails `assert_killed`, not the wait.
    let release = member.release_root_when_a_teardown_waits();
    let armed = arm_reaped_elsewhere(started);
    let (mode, placement) = container.arm();
    let capture = Capture::start();
    let mark = crate::log_capture::mark();
    let spawned = {
        let mut cmd = crate::test_child::command(Console::Hidden);
        cmd.args(ROOT_LEAVING_A_MEMBER);
        cmd.contain_with(mode);
        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();
    let pid = armed.pid();
    drop((armed, placement, release));
    let error = match spawned {
        Ok(child) => panic!("the spawn must fail at its identity read: {:?}", child.id()),
        Err(e) => e,
    };
    let cleanup = error.failures().cloned();
    let (_err, fate) = expect_may_have_started_with(error);
    let left_running = crate::log_capture::levels_since(mark, &left_running_note(pid));
    Failed {
        fate,
        steps,
        member,
        left_running,
        cleanup,
    }
}

/// 6.9+: the cleanup kills the root's group through its pidfd. Mutant: "the cleanup says members
/// may be left running although it killed the group".
fn kills_the_group(container: Container) {
    let failed = failed_spawn(container);
    assert_group_signalled_first_through_the_pidfd(&failed.steps, libc::SIGKILL);
    failed.member.assert_killed();
    assert!(
        failed.left_running.is_empty(),
        "the group was killed, so nothing is left running: {:?}",
        failed.left_running
    );
    assert_eq!(failed.fate, ChildFate::Gone, "the root was reaped before any kill");
    assert!(failed.cleanup.is_none(), "nothing failed: {:?}", failed.cleanup);
}

/// Below 6.9 the old behaviour holds: the kernel refuses the group's send through the pidfd,
/// nothing goes by the group's number, and its member lives on, which the cleanup says in one
/// `warn` naming the root. Mutants: "a refused flag falls back to the group's number"; "the refusal
/// is only a `debug`".
fn below_6_9_sends_nothing_to_the_group(container: Container) {
    let old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    let refused = crate::containment::unix::fault::refuse_group_sends();
    let groups = crate::containment::unix::fault::record_kill_group();
    let failed = failed_spawn(container);
    drop((old_kernel, refused));
    assert_eq!(groups.killed(), Vec::<i32>::new(), "no kill of the group by its number");
    assert!(
        failed
            .steps
            .iter()
            .all(|s| *s == GroupStep::Group(Route::Pidfd, libc::SIGKILL)),
        "nothing but the refused send through the pidfd: {:?}",
        failed.steps
    );
    failed.member.assert_survived();
    assert_eq!(
        failed.left_running,
        [log::Level::Warn],
        "one warn says members of the root's group may be left running"
    );
    // The refused kill of the group is a failure of the teardown: the error's `cleanup` holds it.
    let cleanup = failed
        .cleanup
        .expect("the refused group kill is the teardown's failure");
    assert_eq!(cleanup.iter().len(), 1, "{cleanup:?}");
    assert!(
        crate::error::said(&**cleanup.first()).contains("killing the process group of pid")
            && crate::error::first_os_code(&**cleanup.first()) == Some(libc::EINVAL),
        "{cleanup:?}"
    );
}

#[skuld::test]
fn a_spawn_whose_root_was_reaped_before_its_identity_read_kills_the_group_left_behind(
    #[fixture(pidfd_group_signal)] _group: &Group,
) {
    kills_the_group(Container::Session);
}

#[skuld::test]
fn a_spawn_whose_root_was_reaped_before_its_identity_read_below_6_9_sends_nothing_to_the_group() {
    below_6_9_sends_nothing_to_the_group(Container::Session);
}

#[skuld::test]
fn cgroup_a_spawn_whose_leaf_did_not_take_its_root_reaped_before_its_identity_read_kills_the_group_left_behind(
    #[fixture(cgroup)] _cgroup: &Group,
    #[fixture(pidfd_group_signal)] _group: &Group,
) {
    kills_the_group(Container::LeafNotTaken);
}

#[skuld::test]
fn cgroup_a_spawn_whose_leaf_did_not_take_its_root_reaped_before_its_identity_read_below_6_9_sends_nothing_to_the_group(
    #[fixture(cgroup)] _cgroup: &Group,
) {
    below_6_9_sends_nothing_to_the_group(Container::LeafNotTaken);
}