use crate::error::ChildFate;
use crate::tokio::Command;
use crate::wait::exit_only::seams::force_peeks;
fn live(kill_on_drop: bool) -> (crate::tokio::Child, std::io::PipeWriter) {
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = Command::new();
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: std::io::PipeWriter) {
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"
);
}
fn a_failed_peek_leaves_the_child_running(kill_on_drop: bool) {
crate::tokio::test_runtime::assert_current_thread();
let (mut child, writer) = live(kill_on_drop);
let id = child.id();
let looks = force_peeks((0..4).map(|_| Err(std::io::Error::other("forced peek failure 5d1b"))));
let fate = child.tear_down_now();
drop(looks);
collect(id, writer);
assert_eq!(fate, ChildFate::Running { id: Some(id) }, "kill_on_drop={kill_on_drop}");
}
#[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();
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();
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, 3, "exactly one peek was taken");
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_running_not_gone() {
a_failed_peek_leaves_the_child_running(true);
}
#[skuld::test]
async fn a_disarmed_teardown_with_a_failed_peek_says_running_not_gone() {
a_failed_peek_leaves_the_child_running(false);
}
fn forgotten(reaped_elsewhere: bool) -> (crate::tokio::Child, std::io::PipeWriter) {
let (mut child, writer) = live(true);
child
.os
.proc_mut()
.forget_as("was forgotten by a test", reaped_elsewhere);
(child, writer)
}
#[skuld::test]
async fn the_drop_of_a_child_forgotten_unproven_says_running() {
crate::tokio::test_runtime::assert_current_thread();
let (mut child, writer) = forgotten(false);
let id = child.id();
let fate = child.tear_down_now();
collect(id, writer);
assert_eq!(fate, ChildFate::Running { id: Some(id) });
}
#[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();
let fate = child.tear_down_now();
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("sends nothing"),
Sent::Unverified
);
assert_eq!(
proven
.os
.proc
.as_ref()
.expect("backend")
.signal(Sig::Kill)
.expect("sends nothing"),
Sent::Gone
);
collect(unproven.id(), w1);
collect(proven.id(), w2);
}
#[skuld::test]
async fn a_failed_password_write_on_an_unproven_backend_leaves_the_child_running() {
crate::tokio::test_runtime::assert_current_thread();
for (reaped_elsewhere, expected) in [(false, None), (true, Some(ChildFate::Gone))] {
let (child, writer) = forgotten(reaped_elsewhere);
let id = child.id();
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();
child.kill().expect("kill");
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();
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::wait::exit_only::{Foreign, Peek};
crate::tokio::test_runtime::assert_current_thread();
let (mut child, writer) = live(false);
let id = child.id();
let looks = force_peeks((0..4).map(|_| Ok(Peek::Foreign(Foreign::Orphaned))));
let fate = child.tear_down_now();
drop(looks);
collect(id, writer);
assert_eq!(fate, ChildFate::Gone);
}