use std::io;
use super::held_teardown::{tear_down_held, warn_of_teardown_failures, HeldTeardown};
use super::policy::SpawnPolicy;
use super::Diagnosis;
use crate::child::owned::owned_tests::teardown::{
fire_on_block, held_owned_blocker as held_blocker, held_owned_blocker_reporting, slow_warns_during,
};
use crate::child::owned::owned_tests::teardown_lines::{self, Found, Kill};
use crate::child::owned::teardown::{Disposition, Expect, HandleTeardown, Past};
use crate::child::owned::OwnedChild;
use crate::containment::cgroup::{fault, PlacedAtRemoval, Subtree, Swept};
use crate::elevation::front::Front;
use crate::error::{ChildFate, Error};
use crate::log_capture::warns_since;
use crate::os_process::child::{ExitRecord, Foreign, KillRefusal};
use crate::os_process::fake::{failed_signal, FakeProcess, ReapAnswer};
use crate::test_child::tear_down_held_holding;
fn held_front(script: impl FnOnce(FakeProcess) -> FakeProcess) -> (OwnedChild, crate::PipeWriter) {
held_owned_blocker_reporting(crate::child::front_kill_tests::sudo_report(), script)
}
fn killed_subtree() -> Subtree {
Subtree::new(
Some(7),
None,
None,
Swept::default(),
PlacedAtRemoval::default(),
crate::containment::cgroup::test_support::kill_record(true),
)
}
#[skuld::test]
fn a_refused_kill_whose_look_failed_is_handed_back() {
let (owned, stdin) = held_blocker(|f| {
f.signals([Err(io::Error::from(io::ErrorKind::PermissionDenied))])
.reaps([ReapAnswer::Err(io::ErrorKind::Other)])
});
let (_, fate, handed_back) = tear_down_held_holding(owned, None, None, &mut Diagnosis::default(), stdin);
assert_eq!(fate, ChildFate::Unknown);
handed_back.unreaped();
handed_back.wait();
}
#[skuld::test]
fn a_front_whose_look_failed_is_handed_back_and_the_error_says_so() {
let (owned, stdin) = held_front(|f| f.reaps([ReapAnswer::Err(io::ErrorKind::Other)]));
let (detail, fate, handed_back) = tear_down_held_holding(owned, None, None, &mut Diagnosis::default(), stdin);
assert_eq!(fate, ChildFate::Unknown);
handed_back.unreaped();
let detail = detail.expect("the teardown says what it did with the front");
assert!(
detail.contains("could not be waited on") && detail.contains("handed back to the caller"),
"{detail}"
);
handed_back.wait();
}
#[skuld::test]
fn a_front_whose_place_cannot_be_read_is_handed_back_and_the_error_says_so() {
let (owned, stdin) = held_front(|f| f.reaps([ReapAnswer::Running]));
let subtree = killed_subtree();
let (detail, fate, handed_back) = {
let _unreadable = fault::fail_pidfd_info();
tear_down_held_holding(owned, None, Some(&subtree), &mut Diagnosis::default(), stdin)
};
assert!(matches!(fate, ChildFate::Running { .. }), "{fate:?}");
handed_back.unreaped();
let detail = detail.expect("the teardown says what it did with the front");
assert!(
detail.contains("where it is cannot be read") && detail.contains("handed back to the caller"),
"{detail}"
);
handed_back.wait();
}
#[skuld::test]
fn a_front_whose_reap_failed_after_the_leaf_kill_is_handed_back_and_the_error_says_so() {
let (owned, stdin) = held_front(|f| f.reaps([ReapAnswer::Running, ReapAnswer::Err(io::ErrorKind::Other)]));
let subtree = killed_subtree();
let (detail, fate, handed_back) = {
let _placed = fault::force_pidfd_cgroup_id(7);
tear_down_held_holding(owned, None, Some(&subtree), &mut Diagnosis::default(), stdin)
};
assert_eq!(fate, ChildFate::Killed);
handed_back.unreaped();
let detail = detail.expect("the teardown says what it did with the front");
assert!(
detail.contains("could not be waited on") && detail.contains("handed back to the caller"),
"{detail}"
);
handed_back.wait();
}
#[skuld::test]
fn a_child_that_is_not_a_front_is_killed_and_reaped_and_nothing_is_handed_back() {
crate::log_capture::install();
let (owned, stdin) = held_blocker(|f| f);
let pid = owned.pid().expect("the held child has a pid");
let mark = crate::log_capture::mark();
let (detail, fate, handed_back) = tear_down_held_holding(owned, None, None, &mut Diagnosis::default(), stdin);
assert_eq!(fate, ChildFate::Reaped);
assert!(handed_back.is_none(), "a reaped child is not handed back");
assert_eq!(detail, None, "a child that is not a front has nothing to say of it");
assert_eq!(warns_since(mark, ""), Vec::<String>::new());
assert_eq!(
crate::child::front_kill_tests::reap(pid),
None,
"the teardown reaped it"
);
}
#[skuld::test]
fn a_front_reaped_elsewhere_is_noted_without_a_hand_back() {
let (owned, stdin) = held_front(|f| f.reaps([ReapAnswer::Foreign(Foreign::Gone)]));
let (detail, fate, handed_back) = tear_down_held_holding(owned, None, None, &mut Diagnosis::default(), stdin);
assert_eq!(fate, ChildFate::Gone);
assert!(handed_back.is_none());
let detail = detail.expect("the teardown says what it did with the front");
assert!(
detail.contains("could not be waited on") && !detail.contains("handed back"),
"{detail}"
);
}
#[skuld::test]
fn a_running_front_with_no_subtree_is_handed_back_and_the_error_says_so() {
let (owned, stdin) = held_front(|f| f.reaps([ReapAnswer::Running]));
let (detail, fate, handed_back) = tear_down_held_holding(owned, None, None, &mut Diagnosis::default(), stdin);
assert!(matches!(fate, ChildFate::Running { .. }), "{fate:?}");
handed_back.unreaped();
let detail = detail.expect("the teardown says what it did with the front");
assert!(
detail.contains("the elevated program may be running; handed back to the caller in the error"),
"{detail}"
);
handed_back.wait();
}
#[skuld::test]
fn the_diagnosis_leads_the_one_warn_of_the_teardown() {
crate::log_capture::install();
for front in [None, Some(Front::Sudo)] {
let script = |f: FakeProcess| {
f.signals([failed_signal(libc::EPERM)])
.reaps([ReapAnswer::Err(io::ErrorKind::Other)])
};
let (owned, stdin) = if front.is_some() {
held_front(script)
} else {
held_blocker(script)
};
let mut why = Diagnosis::default();
why.add("diagnosis 7c2e");
let mark = crate::log_capture::mark();
let (_, _, handed_back) = tear_down_held_holding(owned, None, None, &mut why, stdin);
drop(why);
let warns = warns_since(mark, "");
assert_eq!(warns.len(), 1, "{front:?}: {warns:?}");
assert!(
warns[0].starts_with("teardown after a failed spawn: diagnosis 7c2e; "),
"{front:?}: {warns:?}"
);
handed_back.wait();
}
}
#[skuld::test]
fn a_front_that_has_exited_is_reaped_without_waiting() {
let (owned, stdin) = held_front(|f| f);
let mut blocker = ReapsBlocker::new(&owned, stdin);
blocker.close_stdin();
assert!(owned.exited_by(None).expect("watch the front"), "the front exited");
let (detail, fate, handed_back) = tear_down_held_holding(owned, None, None, &mut Diagnosis::default(), ());
assert_eq!(fate, ChildFate::Reaped);
assert!(handed_back.is_none());
let detail = detail.expect("the teardown says what it did with the front");
assert!(detail.contains("and it was reaped"), "{detail}");
assert_eq!(blocker.finish(), None, "the teardown reaped the front");
}
fn tear_down_leaving(owned: OwnedChild, why: &mut Diagnosis) -> (Option<String>, ChildFate, Option<crate::Unreaped>) {
let how = HeldTeardown {
id: None,
reach: None,
disposition: Disposition::Leave,
policy: SpawnPolicy::SYNC,
};
let (detail, torn) = tear_down_held(owned, how, why);
(detail, torn.fate, torn.unreaped)
}
#[skuld::test]
fn a_refused_kill_under_leave_is_left_running_and_warned_and_nothing_is_handed_back() {
crate::log_capture::install();
let (owned, stdin) = held_blocker(|f| {
f.signals([Err(io::Error::from(io::ErrorKind::PermissionDenied))])
.reaps([ReapAnswer::Running])
});
let blocker = ReapsBlocker::new(&owned, stdin);
let mark = crate::log_capture::mark();
let (_, fate, unreaped) = tear_down_leaving(owned, &mut Diagnosis::default());
assert_eq!(fate, ChildFate::Running { id: None });
assert!(unreaped.is_none(), "a caller that leaves it is handed nothing back");
let warns = warns_since(mark, "");
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(
warns[0].contains("leaving it running") && !warns[0].contains("handed back"),
"{warns:?}"
);
blocker.finish().expect("the left child is still unreaped");
}
#[skuld::test]
fn a_running_front_under_leave_is_noted_as_left_unreaped_and_nothing_is_handed_back() {
crate::log_capture::install();
let (owned, stdin) = held_front(|f| f.reaps([ReapAnswer::Running]));
let blocker = ReapsBlocker::new(&owned, stdin);
let mark = crate::log_capture::mark();
let (detail, fate, unreaped) = tear_down_leaving(owned, &mut Diagnosis::default());
assert_eq!(fate, ChildFate::Running { id: None });
assert!(unreaped.is_none());
let detail = detail.expect("the teardown says what it did with the front");
assert!(
detail.contains("the elevated program may be running; left unreaped") && !detail.contains("handed back"),
"{detail}"
);
let warns = warns_since(mark, "");
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(
warns[0].contains("is left running and unreaped") && !warns[0].contains("handed back"),
"{warns:?}"
);
blocker.finish().expect("the left front is still unreaped");
}
#[skuld::test]
fn a_front_with_no_pid_is_named_as_such() {
crate::log_capture::install();
let (owned, stdin) = held_blocker(|f| f);
let blocker = ReapsBlocker::new(&owned, stdin);
let pidfd = owned.pidfd().expect("a Linux child has its pidfd");
let pidless = OwnedChild::over_pidfd(
crate::identity::HostKernel,
None,
pidfd.try_clone_to_owned().expect("dup the pidfd"),
crate::elevation::front::TrackedKind::Front(Front::Sudo),
);
let mark = crate::log_capture::mark();
let (detail, _, unreaped) = tear_down_leaving(pidless, &mut Diagnosis::default());
assert!(unreaped.is_none());
let detail = detail.expect("the teardown says what it did with the front");
assert!(detail.contains("the spawned child is what sudo left"), "{detail}");
let warns = warns_since(mark, "");
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(
warns[0].starts_with("teardown after a failed spawn: the elevation front (pid unknown)"),
"{warns:?}"
);
drop(owned);
blocker.finish().expect("the left front is still unreaped");
}
fn teardown_of(kill: teardown_lines::Kill, found: teardown_lines::Found, past: Past) -> (HandleTeardown, String) {
let row = teardown_lines::rows()
.into_iter()
.find(|row| row.kill == kill && row.found == found)
.expect("the table has the row");
let expect = Expect {
refusal: KillRefusal::Permission,
past,
};
let torn = HandleTeardown::new(teardown_lines::outcome(&row, Disposition::Leave), expect);
(torn, row.leave)
}
fn after_a_failed_wait(past: Past) -> HandleTeardown {
teardown_of(Kill::Delivered, Found::WaitFailed, past).0
}
#[cfg(debug_assertions)]
#[skuld::test]
#[should_panic(expected = "a wait on a child's own handle failed")]
fn a_failed_wait_on_a_pidfd_nothing_looked_at_breaks_the_contract() {
crate::log_capture::install();
after_a_failed_wait(Past::Clean).say_as_failed_spawn(&mut Diagnosis::default());
}
#[cfg(debug_assertions)]
#[skuld::test]
#[should_panic(expected = "never ran the program was refused")]
fn a_refused_kill_of_a_child_that_never_ran_breaks_the_contract() {
crate::log_capture::install();
let (torn, _) = teardown_of(Kill::Refused, Found::Running, Past::Unexecuted);
torn.say_as_failed_spawn(&mut Diagnosis::default());
}
#[skuld::test]
fn a_failed_wait_on_a_pidfd_a_look_failed_on_is_not_a_breach() {
crate::log_capture::install();
let mark = crate::log_capture::mark();
after_a_failed_wait(Past::WaitMayFail).say_as_failed_spawn(&mut Diagnosis::default());
assert_eq!(warns_since(mark, "").len(), 1);
}
struct ReapsBlocker {
stdin: Option<crate::PipeWriter>,
pid: Option<u32>,
}
impl ReapsBlocker {
fn new(owned: &OwnedChild, stdin: crate::PipeWriter) -> ReapsBlocker {
ReapsBlocker {
stdin: Some(stdin),
pid: Some(owned.pid().expect("the held child has a pid")),
}
}
fn close_stdin(&mut self) {
drop(self.stdin.take());
}
fn finish(mut self) -> Option<std::process::ExitStatus> {
self.close_stdin();
crate::child::front_kill_tests::reap(self.pid.take().expect("the blocker is not yet reaped"))
}
}
impl Drop for ReapsBlocker {
fn drop(&mut self) {
self.close_stdin();
if let Some(pid) = self.pid.take() {
if !std::thread::panicking() {
crate::child::front_kill_tests::reap(pid);
}
}
}
}
#[skuld::test]
fn a_pidfd_allows_a_refusal_of_permission_alone() {
let (owned, stdin) = held_blocker(|f| f);
let _reaped = ReapsBlocker::new(&owned, stdin);
assert_eq!(owned.kill_refusal(), KillRefusal::Permission);
}
fn after_a_pidfd_kill_refused_with(errno: i32, past: Past) -> ChildFate {
crate::log_capture::install();
let (owned, stdin) = held_blocker(|f| f.signals([failed_signal(errno)]));
let _reaped = ReapsBlocker::new(&owned, stdin);
super::held_teardown::tear_down_failed_spawn(
owned,
None,
past,
Disposition::Leave,
SpawnPolicy::SYNC,
&mut Diagnosis::default(),
)
.fate
}
#[skuld::test]
fn a_pidfd_kill_refused_with_eperm_is_no_breach() {
let fate = after_a_pidfd_kill_refused_with(libc::EPERM, Past::Clean);
assert!(matches!(fate, ChildFate::Running { .. }), "{fate:?}");
}
#[cfg(debug_assertions)]
#[skuld::test]
#[should_panic(expected = "for a reason other than permission")]
fn a_pidfd_kill_refused_with_another_errno_breaks_the_contract() {
after_a_pidfd_kill_refused_with(libc::EIO, Past::Clean);
}
#[skuld::test]
fn a_clean_teardown_with_a_diagnosis_owed_is_one_labelled_warn() {
crate::log_capture::install();
let (torn, clause) = teardown_of(Kill::Delivered, Found::Exited, Past::Clean);
let mut why = Diagnosis::default();
why.add("the identity check failed");
let mark = crate::log_capture::mark();
torn.say_as_failed_spawn(&mut why);
drop(why);
assert_eq!(
warns_since(mark, ""),
[format!(
"teardown after a failed spawn: the identity check failed; {clause}"
)]
);
}
fn tear_down_front_under(
owned: OwnedChild,
subtree: Option<&Subtree>,
policy: SpawnPolicy,
) -> (Option<String>, ChildFate, Option<crate::Unreaped>) {
let how = HeldTeardown {
id: None,
reach: subtree.map(|subtree| subtree as &dyn crate::child::owned::teardown::Reach),
disposition: Disposition::HandBack,
policy,
};
let (detail, torn) = tear_down_held(owned, how, &mut Diagnosis::default());
(detail, torn.fate, torn.unreaped)
}
#[skuld::test]
fn the_wait_for_a_front_its_leafs_kill_reached_warns_past_its_policys_threshold() {
let record = ExitRecord::Status(std::process::ExitStatus::default());
let mut fake = None;
let (owned, stdin) = held_front(|f| {
fake = Some(f.clone());
f.reaps([
ReapAnswer::Running,
ReapAnswer::Running,
ReapAnswer::Running,
ReapAnswer::Exited(record),
])
});
let _blocker = ReapsBlocker::new(&owned, stdin);
let _fire = fire_on_block(&fake.expect("the script ran"));
let subtree = killed_subtree();
let mut handed_back = None;
let warns = slow_warns_during(|| {
let _placed = fault::force_pidfd_cgroup_id(7);
handed_back = Some(tear_down_front_under(owned, Some(&subtree), SpawnPolicy::WARNS_AT_ONCE).2);
});
assert!(
handed_back.expect("the teardown ran").is_none(),
"a reaped front is not handed back"
);
assert_eq!(warns, 1);
}
#[skuld::test]
fn the_unreaped_a_failed_spawn_hands_back_for_a_front_does_what_its_policy_says_on_drop() {
crate::log_capture::install();
let record = ExitRecord::Status(std::process::ExitStatus::default());
let (owned, stdin) = held_front(|f| f.reaps([ReapAnswer::Running, ReapAnswer::Exited(record)]));
let _blocker = ReapsBlocker::new(&owned, stdin);
let (_, _, handed_back) = tear_down_front_under(owned, None, SpawnPolicy::LET_GO);
let handed_back = handed_back.expect("a front left running is handed back");
let mark = crate::log_capture::mark();
drop(handed_back);
let warns = warns_since(mark, "");
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(
warns[0].contains("dropped without wait or leak") && !warns[0].contains("waited for it"),
"{warns:?}"
);
}
fn remains_with_two_failures() -> crate::child::owned::teardown::Remains {
crate::child::owned::teardown::Remains {
fate: ChildFate::Killed,
uncollected: false,
refused: None,
failures: crate::error::Failures::collect([
Error::io_cause(std::io::Error::other("refused kill 51f3")),
Error::io_cause(std::io::Error::other("failed wait 51f3")),
]),
}
}
#[skuld::test]
fn a_failed_spawns_teardown_failures_its_own_warn_did_not_say_are_each_warned_of() {
crate::log_capture::install();
let mark = crate::log_capture::mark();
warn_of_teardown_failures(&remains_with_two_failures(), false);
let warns = warns_since(mark, "teardown of its child failed");
assert_eq!(warns.len(), 2, "{warns:?}");
assert!(warns[0].contains("refused kill 51f3"), "{warns:?}");
assert!(warns[1].contains("failed wait 51f3"), "{warns:?}");
}
#[skuld::test]
fn a_failed_spawns_teardown_failures_its_own_warn_said_are_not_warned_of_again() {
crate::log_capture::install();
let mark = crate::log_capture::mark();
warn_of_teardown_failures(&remains_with_two_failures(), true);
assert_eq!(warns_since(mark, "teardown of its child failed"), Vec::<String>::new());
}