use crate::containment::dispatch::TeardownCaller;
use crate::error::ChildFate;
use crate::test_child::Console;
use crate::wait::exit_only::seams::force_peeks;
fn live(kill_on_drop: bool) -> (crate::tokio::Child, crate::PipeWriter) {
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.kill_on_drop(kill_on_drop);
(cmd.spawn().expect("spawn"), writer)
}
fn collect(id: crate::identity::ProcessId, writer: crate::PipeWriter) -> std::process::ExitStatus {
use std::os::unix::process::ExitStatusExt as _;
drop(writer);
let mut status = 0;
let reaped = unsafe { libc::waitpid(id.pid() as libc::pid_t, &mut status, 0) };
assert_eq!(
reaped,
id.pid() as libc::pid_t,
"the forgotten child is still ours to collect"
);
std::process::ExitStatus::from_raw(status)
}
fn a_failed_peek_answers_what_the_drop_did(kill_on_drop: bool) {
crate::tokio::test_runtime::assert_current_thread();
let (mut child, writer) = live(kill_on_drop);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let looks = force_peeks((0..4).map(|_| Err(std::io::Error::other("forced peek failure 5d1b"))));
let fate = child.tear_down_now(TeardownCaller::Drop);
drop(looks);
collect(id, writer);
assert_eq!(
fate,
expected_fate_of_a_failed_peek(kill_on_drop, id),
"kill_on_drop={kill_on_drop}"
);
}
fn expected_fate_of_a_failed_peek(kill_on_drop: bool, id: crate::identity::ProcessId) -> ChildFate {
if kill_on_drop {
ChildFate::Killed
} else {
ChildFate::Running { id: Some(id) }
}
}
#[skuld::test]
async fn a_disarmed_teardown_with_a_failed_peek_looks_and_warns_once() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, writer) = live(false);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let looks = force_peeks((0..4).map(|_| Err(std::io::Error::other("forced peek failure 8e2a"))));
let mark = crate::log_capture::mark();
let fate = child.tear_down_now(TeardownCaller::Drop);
let left = crate::wait::exit_only::seams::forced_peeks_left();
drop(looks);
collect(id, writer);
assert_eq!(fate, ChildFate::Running { id: Some(id) });
assert_eq!(left, 2, "two peeks were taken: the read and the forget's own look");
assert_eq!(
crate::log_capture::records_since(mark, "forced peek failure 8e2a").len(),
1,
"exactly one warning"
);
}
#[skuld::test]
async fn an_armed_teardown_with_a_failed_peek_says_killed_not_gone() {
a_failed_peek_answers_what_the_drop_did(true);
}
#[skuld::test]
async fn a_disarmed_teardown_with_a_failed_peek_says_running_not_gone() {
a_failed_peek_answers_what_the_drop_did(false);
}
fn forgotten(reaped_elsewhere: bool) -> (crate::tokio::Child, crate::PipeWriter) {
let (mut child, writer) = live(true);
child.os.proc_mut().forget_as(
"was forgotten by a test",
if reaped_elsewhere {
super::proc_source::Forgotten::ReapedElsewhere
} else {
super::proc_source::Forgotten::Unproven
},
);
(child, writer)
}
#[skuld::test]
async fn a_backend_forgotten_as_reaped_elsewhere_is_not_an_own_reap() {
crate::tokio::test_runtime::assert_current_thread();
let (mut child, writer) = forgotten(true);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let state = child.os.proc_mut().state();
let _fate = child.tear_down_now(TeardownCaller::Drop);
collect(id, writer);
assert!(
matches!(state, crate::signal::RootState::Reaped { own: false }),
"{state:?}"
);
}
#[skuld::test]
async fn the_drop_of_a_child_forgotten_unproven_claims_no_reap() {
crate::tokio::test_runtime::assert_current_thread();
let (mut child, writer) = forgotten(false);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let fate = child.tear_down_now(TeardownCaller::Drop);
let status = collect(id, writer);
#[cfg(target_os = "linux")]
{
assert_eq!(fate, ChildFate::Killed);
crate::test_child::assert_killed("the child", status);
}
#[cfg(not(target_os = "linux"))]
let _ = status;
#[cfg(target_os = "macos")]
assert_eq!(fate, ChildFate::Running { id: Some(id) });
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn the_drop_of_an_unproven_backend_whose_root_its_pidfd_shows_gone_says_gone() {
use crate::wait::exit_only::seams::{assert_peeks_exhausted, force_peek_once};
crate::tokio::test_runtime::assert_current_thread();
let (mut child, writer) = live(true);
let pid = child.pid();
child.kill().expect("kill the root");
drop(writer);
crate::test_child::assert_killed("the root", crate::test_child::reaped_elsewhere::reap_behind_status(pid));
let failed = force_peek_once(Err(std::io::Error::other("forced peek failure 3c7f")));
child.kill().expect("a root reaped elsewhere has nothing left to kill");
assert_peeks_exhausted();
drop(failed);
let fate = child.tear_down_now(TeardownCaller::Drop);
assert_eq!(fate, ChildFate::Gone);
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_refused_kill_of_an_unproven_backend_at_drop_is_named() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, writer) = forgotten(false);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let refused = super::fault::force_kill_failure();
let mark = crate::log_capture::mark();
let fate = child.tear_down_now(TeardownCaller::Drop);
drop(refused);
collect(id, writer);
assert_eq!(fate, ChildFate::Running { id: Some(id) });
let warns = crate::log_capture::records_since(mark, "could not be terminated on drop");
assert_eq!(warns.len(), 1, "the refused kill is named once: {warns:?}");
let wording = format!(
"async child {} could not be terminated on drop{}",
id.pid(),
crate::child::owned::teardown::LEAVING_RUNNING
);
assert!(warns[0].contains(&wording), "the warn says {wording:?}: {warns:?}");
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_refused_kill_whose_look_at_drop_fails_is_left_running_and_unreaped() {
use crate::os_process::child::PeekOutcome;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, writer) = live(true);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let refused = super::fault::force_kill_failure();
let fail = || Err(std::io::Error::other("forced peek failure 6c4e"));
let looks = force_peeks([fail(), Ok(PeekOutcome::Running), fail(), fail()]);
let mark = crate::log_capture::mark();
let fate = child.tear_down_now(TeardownCaller::Drop);
let left = crate::wait::exit_only::seams::forced_peeks_left();
drop(looks);
drop(refused);
collect(id, writer);
assert_eq!(
fate,
ChildFate::Unknown,
"a look that failed shows nothing of the child"
);
assert_eq!(
left, 0,
"the read, the forget's look, the gate and the forget's second look"
);
let warns = crate::log_capture::records_since(mark, "could not be terminated on drop");
assert_eq!(warns.len(), 1, "{warns:?}");
let wording = format!(
"async child {} could not be terminated on drop{}",
id.pid(),
crate::child::owned::teardown::LEAVING_RUNNING
);
assert!(warns[0].contains(&wording), "the warn says {wording:?}: {warns:?}");
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_refused_kill_of_a_child_released_to_tokio_is_left_running_and_not_unreaped() {
use crate::os_process::child::PeekOutcome;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, writer) = live(true);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let refused = super::fault::force_kill_failure();
let looks = force_peeks((0..4).map(|_| Ok(PeekOutcome::Running)));
let mark = crate::log_capture::mark();
let fate = child.tear_down_now(TeardownCaller::Drop);
drop(looks);
drop(refused);
drop(writer);
assert_eq!(fate, ChildFate::Running { id: Some(id) });
let warns = crate::log_capture::records_since(mark, "could not be terminated on drop");
assert_eq!(warns.len(), 1, "{warns:?}");
let wording = format!(
"async child {} could not be terminated on drop; leaving it running",
id.pid()
);
assert!(warns[0].ends_with(&wording), "the warn ends {wording:?}: {warns:?}");
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_refused_kill_of_a_child_shown_reaped_elsewhere_at_drop_says_it_is_gone() {
use crate::os_process::child::{Foreign, PeekOutcome};
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, writer) = live(true);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let refused = super::fault::force_kill_failure();
let looks = force_peeks([
Ok(PeekOutcome::Running),
Ok(PeekOutcome::Running),
Ok(PeekOutcome::Foreign(Foreign::Gone)),
]);
let mark = crate::log_capture::mark();
let fate = child.tear_down_now(TeardownCaller::Drop);
let left = crate::wait::exit_only::seams::forced_peeks_left();
drop(looks);
drop(refused);
collect(id, writer);
assert_eq!(fate, ChildFate::Gone);
assert_eq!(left, 0, "the read, the forget's look and the gate");
let warns = crate::log_capture::records_since(mark, "could not be terminated on drop");
assert_eq!(warns.len(), 1, "{warns:?}");
let wording = format!(
"async child {} could not be terminated on drop; it is already gone",
id.pid()
);
assert!(warns[0].ends_with(&wording), "the warn ends {wording:?}: {warns:?}");
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn an_unproven_backend_reads_its_kept_pidfd_for_whether_the_child_runs() {
use crate::signal::Sig;
crate::tokio::test_runtime::assert_current_thread();
let (child, writer) = forgotten(false);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let proc = child.os.proc.as_ref().expect("backend");
assert!(proc.is_running().expect("the pidfd answers"), "the child lives");
proc.signal(Sig::Kill).expect("kill through the pidfd");
crate::test_child::assert_killed("the child", collect(id, writer));
assert!(!proc.is_running().expect("the pidfd answers"), "the child is gone");
}
#[skuld::test]
async fn the_drop_of_a_child_forgotten_as_reaped_elsewhere_says_gone() {
crate::tokio::test_runtime::assert_current_thread();
let (mut child, writer) = forgotten(true);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let fate = child.tear_down_now(TeardownCaller::Drop);
collect(id, writer);
assert_eq!(fate, ChildFate::Gone);
}
#[skuld::test]
async fn a_kill_of_a_forgotten_backend_is_gone_only_with_proof() {
use crate::signal::{Sent, Sig};
crate::tokio::test_runtime::assert_current_thread();
let (unproven, w1) = forgotten(false);
let (proven, w2) = forgotten(true);
assert_eq!(
unproven
.os
.proc
.as_ref()
.expect("backend")
.signal(Sig::Kill)
.expect("the kill is answered"),
if cfg!(target_os = "linux") {
Sent::Delivered
} else {
Sent::Unverified
}
);
assert_eq!(
proven
.os
.proc
.as_ref()
.expect("backend")
.signal(Sig::Kill)
.expect("sends nothing"),
Sent::Gone
);
collect(unproven.id().expect(crate::test_child::IDENTITY_READ), w1);
collect(proven.id().expect(crate::test_child::IDENTITY_READ), w2);
}
#[skuld::test]
async fn a_failed_password_write_on_a_forgotten_backend_is_gone_only_with_proof() {
crate::tokio::test_runtime::assert_current_thread();
let unproven = if cfg!(target_os = "linux") {
Some(ChildFate::Killed)
} else {
None
};
for (reaped_elsewhere, expected) in [(false, unproven), (true, Some(ChildFate::Gone))] {
let (child, writer) = forgotten(reaped_elsewhere);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let (_err, fate) = crate::tokio::spawn::finish_elevated(
child,
Err(crate::error::Error::Elevation {
kind: crate::error::ElevationErrorKind::AuthFailed,
detail: "forced password-write failure".into(),
}),
)
.expect_err("a failed write fails the spawn")
.expect_may_have_started_with();
collect(id, writer);
assert_eq!(
fate,
expected.unwrap_or(ChildFate::Running { id: Some(id) }),
"reaped_elsewhere={reaped_elsewhere}"
);
}
}
fn record_waits(id: crate::identity::ProcessId) -> bool {
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
let rc = unsafe {
libc::waitid(
libc::P_PID,
id.pid() as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOHANG | libc::WNOWAIT,
)
};
if rc == 0 {
return unsafe { info.si_pid() } != 0;
}
assert_eq!(std::io::Error::last_os_error().raw_os_error(), Some(libc::ECHILD));
false
}
#[skuld::test]
async fn reaped_is_reported_only_for_a_status_that_was_collected() {
crate::tokio::test_runtime::assert_current_thread();
let (mut child, writer) = live(true);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
child.kill().expect("kill");
drop(writer);
crate::test_child::assert_zombie_sigkilled("the killed child", id.pid());
let (fate, _) = child.wait_and_reap_blocking();
let waiting = record_waits(id);
assert_eq!(fate, ChildFate::Reaped);
assert!(
!waiting,
"wait_and_reap_blocking reported Reaped with the record uncollected"
);
let (mut child, _writer) = live(true);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let proc = child.os.proc.take().expect("backend");
let fate = proc.reap_now(id.pid(), Some(id));
let waiting = record_waits(id);
assert_eq!(fate, ChildFate::Reaped);
assert!(!waiting, "reap_now reported Reaped with the record uncollected");
}
#[cfg(target_os = "macos")]
#[skuld::test]
async fn macos_an_orphaned_child_at_drop_is_gone() {
use crate::os_process::child::{Foreign, PeekOutcome};
crate::tokio::test_runtime::assert_current_thread();
let (mut child, writer) = live(false);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let looks = force_peeks((0..4).map(|_| Ok(PeekOutcome::Foreign(Foreign::Orphaned))));
let fate = child.tear_down_now(TeardownCaller::Drop);
drop(looks);
collect(id, writer);
assert_eq!(fate, ChildFate::Gone);
}
#[skuld::test]
async fn a_teardown_named_a_failed_run_words_its_warn_as_one() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, writer) = live(true);
let id = child.id().expect(crate::test_child::IDENTITY_READ);
let refused = super::fault::force_kill_failure();
let mark = crate::log_capture::mark();
let fate = child.tear_down_now(TeardownCaller::FailedRun);
drop(refused);
drop(writer);
assert_eq!(fate, ChildFate::Running { id: Some(id) });
let warns = crate::log_capture::records_since(mark, "leaving it running");
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(warns[0].starts_with("teardown after a failed run: "), "{warns:?}");
}