use std::os::unix::process::ExitStatusExt;
use std::time::Duration;
use super::{signal_by_token, ExitWatch, MacosProcess, SignalByTokenOutcome};
use crate::child::spawn::identity_macos_tests::other_unique_id;
use crate::error::Error;
use crate::identity::kinfo::kinfo_proc;
use crate::identity::{FakeKernel, HostKernel, Kernel as _, ProcessId, UniqInfo, UniqRead};
use crate::os_process::drive::exited_held;
use crate::os_process::{kill, open, terminate, Exit, OsProcess};
use crate::signal::{Sent, Sig};
use crate::test_child::{wait_until_zombie, StdChild};
use crate::wait::exit_only::macos_tests::unique_of;
use crate::wait::macos::reap_registration;
#[skuld::test]
fn a_watch_on_a_live_process_reads_not_exited() {
let (_child, id) = blocker();
let watch = ExitWatch::proc_exit(&HostKernel, id.pid()).unwrap();
assert!(!watch.exited().unwrap());
}
#[skuld::test]
fn a_look_at_an_exited_process_does_not_consume_the_exit() {
let (mut child, id) = blocker();
let watch = ExitWatch::proc_exit(&HostKernel, id.pid()).unwrap();
child.kill().unwrap();
wait_until_zombie(id.pid());
assert!(watch.exited().unwrap());
assert!(watch.exited().unwrap(), "the exit is still there for the next look");
}
#[skuld::test]
fn a_watch_armed_past_registration_reads_exited() {
let watch = ExitWatch::proc_exit(&HostKernel, i32::MAX as u32).unwrap();
assert!(watch.exited().unwrap());
}
fn blocker() -> (StdChild, ProcessId) {
let child = crate::test_child::blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
(child, id)
}
fn a_kernel_that_fires_the_exit_watch(pid: u32) -> FakeKernel<'static> {
FakeKernel::over(&HostKernel).on_subscribe_refused(ExitWatch::registration(pid), libc::ESRCH)
}
fn own_unique_info(id: ProcessId) -> UniqInfo {
let UniqRead::Found(info) = HostKernel.unique_info(id.pid()) else {
panic!("the live blocker has a unique id");
};
info
}
#[skuld::test]
fn one_open_serves_look_signal_and_wait() {
let (mut child, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
assert!(
matches!(p.exited().unwrap(), Exit::Running(_)),
"a live process reads Running"
);
assert!(!exited_held(&*p, Some(Duration::ZERO)).unwrap());
assert_eq!(p.signal(Sig::Kill).unwrap(), Sent::Delivered);
assert!(exited_held(&*p, None).unwrap());
assert_eq!(
child.wait().unwrap().signal(),
Some(libc::SIGKILL),
"it died of the SIGKILL"
);
}
#[skuld::test]
fn a_held_process_looks_through_the_kernel_it_was_opened_with() {
use crate::os_process::open_with;
let (_child, id) = blocker();
let UniqRead::Found(own) = HostKernel.unique_info(id.pid()) else {
panic!("the live blocker has a unique id");
};
let kernel = FakeKernel::over(&HostKernel)
.on_unique_info(id.pid(), UniqRead::Found(own))
.on_unique_info(id.pid(), UniqRead::Gone)
.on_unique_info(id.pid(), UniqRead::Gone);
let p = open_with(kernel, id)
.unwrap()
.expect("the open reads the process's own id");
assert!(
matches!(p.exited().unwrap(), Exit::Exited),
"the look is the kernel's, not the live blocker's"
);
}
#[skuld::test]
fn open_with_wrong_start_token_is_none_and_sends_nothing() {
let (_child, id) = blocker();
let sends = crate::send_log::Capture::start();
let other = id.with_other_value();
assert!(open(other).unwrap().is_none(), "a wrong start token opens nothing");
assert_eq!(sends.by_identity(), vec![], "nothing may be sent to a stranger");
let p = open(id).unwrap().expect("the blocker is alive");
assert!(!exited_held(&*p, Some(Duration::ZERO)).unwrap());
}
#[skuld::test]
fn kill_and_terminate_of_a_held_process_that_is_gone_are_ok() {
let (mut child, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
child.kill().unwrap();
child.wait().unwrap();
kill(&*p).unwrap();
terminate(&*p).unwrap();
}
#[skuld::test]
fn open_after_reap_is_none() {
let (mut child, id) = blocker();
child.kill().unwrap();
child.wait().unwrap();
assert!(open(id).unwrap().is_none());
}
#[skuld::test]
fn exited_before_first_look_returns_true() {
let (mut child, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
child.kill().unwrap();
wait_until_zombie(id.pid());
crate::wait::block_probe::take();
assert!(exited_held(&*p, Some(Duration::ZERO)).unwrap());
assert!(
crate::wait::block_probe::take().is_empty(),
"an exit the watch holds is found by the look, with no wait armed"
);
}
#[skuld::test]
fn exited_stays_true_after_a_wait_consumed_the_event() {
let (mut child, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
child.kill().unwrap();
assert!(exited_held(&*p, None).unwrap());
assert!(
matches!(p.exited().unwrap(), Exit::Exited),
"a later look still says Exited"
);
}
#[skuld::test]
fn signal_reaches_a_process_that_execed_after_open() {
use std::io::Write;
let mut cmd = std::process::Command::new("sh");
cmd.args(["-c", "read line; exec cat"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped());
let mut child = StdChild::spawn(&mut cmd);
let id = ProcessId::of(child.id()).found().expect("identity of live child");
let p = open(id).unwrap().expect("a live process opens");
let mut stdin = child.stdin.take().expect("piped stdin");
let mut stdout = child.stdout.take().expect("piped stdout");
stdin.write_all(b"go\n").unwrap();
crate::test_child::assert_echoes(&mut stdin, &mut stdout);
let sends = crate::send_log::Capture::start();
assert_eq!(
p.signal(Sig::Kill).unwrap(),
Sent::Delivered,
"an exec'd process still runs: it is not Gone"
);
assert_eq!(
sends.by_identity(),
vec![(id.pid(), libc::SIGKILL), (id.pid(), libc::SIGKILL)],
"the first token was stale (exec changed p_idversion), the second carried the new one"
);
assert!(exited_held(&*p, None).unwrap());
assert_eq!(
child.wait().unwrap().signal(),
Some(libc::SIGKILL),
"it died of the SIGKILL"
);
}
#[skuld::test]
fn signal_to_a_zombie_is_gone_after_one_attempt() {
let (mut child, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
child.kill().unwrap();
wait_until_zombie(id.pid());
let sends = crate::send_log::Capture::start();
assert_eq!(p.signal(Sig::Kill).unwrap(), Sent::Gone);
assert_eq!(sends.by_identity().len(), 1, "a zombie is not signalled again");
}
#[skuld::test]
fn signal_after_reap_is_gone() {
let (mut child, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
child.kill().unwrap();
child.wait().unwrap();
assert_eq!(p.signal(Sig::Kill).unwrap(), Sent::Gone);
}
#[skuld::test]
fn a_refused_reread_after_esrch_keeps_the_errno() {
use crate::os_process::open_with;
let (mut child, id) = blocker();
let kernel = FakeKernel::over(&HostKernel)
.on_unique_info(id.pid(), UniqRead::Found(own_unique_info(id)))
.on_unique_info(id.pid(), UniqRead::Refused(libc::EPERM));
let p = open_with(kernel, id).unwrap().expect("a live process opens");
child.kill().unwrap();
wait_until_zombie(id.pid());
let err = p.signal(Sig::Kill).expect_err("a refused re-read is an error");
let Error::Io(io) = &err else {
panic!("a refused re-read is Error::Io: {err:?}");
};
assert_eq!(io.kind(), std::io::ErrorKind::PermissionDenied, "{err:?}");
}
#[skuld::test]
fn a_token_send_refused_while_the_exit_has_begun_is_exiting() {
let (_child, id) = blocker();
let known = own_unique_info(id);
let kernel = FakeKernel::over(&HostKernel).on_kinfo(id.pid(), kinfo_proc::of_host_with_exit(id.pid(), true));
let mut idversion = known.id_version;
let refused = crate::signal::seams::refuse_kills();
let outcome = signal_by_token(&kernel, id.pid(), known.unique_id, &mut idversion, Sig::Kill);
drop(refused);
assert_eq!(
outcome.expect("an exiting process is no refuser"),
SignalByTokenOutcome::Exiting
);
assert!(
kernel.all_rows_taken(),
"the exit was read once, from the kernel the send was given"
);
}
#[skuld::test]
fn a_token_send_refused_with_no_exit_begun_keeps_the_raw_eperm() {
let (_child, id) = blocker();
let known = own_unique_info(id);
let kernel = FakeKernel::over(&HostKernel).on_kinfo(id.pid(), kinfo_proc::of_host_with_exit(id.pid(), false));
let mut idversion = known.id_version;
let refused = crate::signal::seams::refuse_kills();
let outcome = signal_by_token(&kernel, id.pid(), known.unique_id, &mut idversion, Sig::Kill);
drop(refused);
let err = outcome.expect_err("a live refuser is an error");
assert_eq!(err.raw_os_error(), Some(libc::EPERM), "{err}");
assert!(kernel.all_rows_taken(), "the exit was read, and found running");
}
#[skuld::test]
fn a_token_send_refused_with_an_unreadable_exit_keeps_the_raw_eperm() {
let (_child, id) = blocker();
let known = own_unique_info(id);
let kernel = FakeKernel::over(&HostKernel).on_kinfo_refused(id.pid(), libc::EACCES);
let mut idversion = known.id_version;
let refused = crate::signal::seams::refuse_kills();
let outcome = signal_by_token(&kernel, id.pid(), known.unique_id, &mut idversion, Sig::Kill);
drop(refused);
let err = outcome.expect_err("an unreadable exit settles nothing");
assert_eq!(err.raw_os_error(), Some(libc::EPERM), "{err}");
assert!(kernel.all_rows_taken(), "the exit was read, and refused");
}
#[skuld::test]
fn a_sigterm_refused_with_eperm_keeps_the_raw_eperm_whatever_the_exit() {
let (_child, id) = blocker();
let known = own_unique_info(id);
let kernel = FakeKernel::over(&HostKernel).on_kinfo(id.pid(), kinfo_proc::of_host_with_exit(id.pid(), true));
let mut idversion = known.id_version;
let refused = crate::signal::seams::refuse_kills();
let outcome = signal_by_token(&kernel, id.pid(), known.unique_id, &mut idversion, Sig::Term);
drop(refused);
let err = outcome.expect_err("only a SIGKILL's refusal is read for an exit");
assert_eq!(err.raw_os_error(), Some(libc::EPERM), "{err}");
assert_eq!(kernel.kinfo_reads(id.pid()), 0, "the record is never read");
}
#[skuld::test]
fn a_pid_taken_or_a_read_refused_after_the_exit_was_read_settles_nothing() {
type Second = fn(u32) -> UniqRead;
let cases: [(Second, Option<SignalByTokenOutcome>); 3] = [
(other_unique_id, Some(SignalByTokenOutcome::Gone)),
(|_| UniqRead::Gone, Some(SignalByTokenOutcome::Gone)),
(|_| UniqRead::Refused(libc::EACCES), None),
];
for (second, expected) in cases {
let (_child, id) = blocker();
let known = own_unique_info(id);
let kernel = FakeKernel::over(&HostKernel)
.on_unique_info(id.pid(), UniqRead::Found(known))
.on_kinfo(id.pid(), kinfo_proc::of_host_with_exit(id.pid(), true))
.on_unique_info(id.pid(), second(id.pid()));
let mut idversion = known.id_version;
let refused = crate::signal::seams::refuse_kills();
let outcome = signal_by_token(&kernel, id.pid(), known.unique_id, &mut idversion, Sig::Kill);
drop(refused);
match expected {
Some(expected) => assert_eq!(outcome.expect("answered"), expected),
None => {
let err = outcome.expect_err("a refused read settles nothing");
assert_eq!(err.raw_os_error(), Some(libc::EPERM), "{err}");
}
}
assert!(kernel.all_rows_taken(), "both id reads and the exit read were made");
}
}
#[skuld::test]
fn a_token_send_refused_for_a_pid_that_is_gone_is_gone() {
let (_child, id) = blocker();
let known = own_unique_info(id);
let kernel = FakeKernel::over(&HostKernel).on_unique_info(id.pid(), UniqRead::Gone);
let mut idversion = known.id_version;
let refused = crate::signal::seams::refuse_kills();
let outcome = signal_by_token(&kernel, id.pid(), known.unique_id, &mut idversion, Sig::Kill);
drop(refused);
assert_eq!(
outcome.expect("a gone process is no refuser"),
SignalByTokenOutcome::Gone
);
assert!(kernel.all_rows_taken());
}
#[skuld::test]
fn a_token_send_refused_for_a_pid_that_names_another_process_is_gone() {
let (_child, id) = blocker();
let known = own_unique_info(id);
let kernel = FakeKernel::over(&HostKernel)
.on_unique_info(id.pid(), other_unique_id(id.pid()))
.on_kinfo(id.pid(), kinfo_proc::of_host_with_exit(id.pid(), true));
let mut idversion = known.id_version;
let refused = crate::signal::seams::refuse_kills();
let outcome = signal_by_token(&kernel, id.pid(), known.unique_id, &mut idversion, Sig::Kill);
drop(refused);
assert_eq!(
outcome.expect("another process's pid is no refuser"),
SignalByTokenOutcome::Gone
);
assert_eq!(
kernel.kinfo_reads(id.pid()),
0,
"another process's record is never read"
);
}
#[skuld::test]
fn a_token_send_refused_by_a_zombie_is_exited_unreaped() {
let (_child, id) = blocker();
let known = own_unique_info(id);
let mut zombie = kinfo_proc::of_host_with_exit(id.pid(), true);
zombie.kp_proc.p_stat = libc::SZOMB as libc::c_char;
let kernel = FakeKernel::over(&HostKernel).on_kinfo(id.pid(), zombie);
let mut idversion = known.id_version;
let refused = crate::signal::seams::refuse_kills();
let outcome = signal_by_token(&kernel, id.pid(), known.unique_id, &mut idversion, Sig::Kill);
drop(refused);
assert_eq!(
outcome.expect("a zombie is no refuser"),
SignalByTokenOutcome::ExitedUnreaped
);
}
#[skuld::test]
fn signal_to_a_process_whose_exit_began_is_delivered() {
use crate::os_process::open_with;
let (_child, id) = blocker();
let kernel = FakeKernel::over(&HostKernel)
.on_unique_info(id.pid(), UniqRead::Found(own_unique_info(id)))
.on_kinfo(id.pid(), kinfo_proc::of_host_with_exit(id.pid(), true));
let p = open_with(kernel, id).unwrap().expect("a live process opens");
let refused = crate::signal::seams::refuse_kills();
let sent = p.signal(Sig::Kill);
drop(refused);
assert_eq!(sent.unwrap(), Sent::Delivered);
}
#[skuld::test]
fn an_open_and_a_send_arm_no_watch_and_the_first_look_arms_it() {
let (_child, id) = blocker();
let p = MacosProcess::open(HostKernel, id)
.unwrap()
.expect("a live process opens");
assert!(p.watch.get().is_none(), "an open arms nothing");
assert_eq!(p.signal(Sig::Kill).unwrap(), Sent::Delivered);
assert!(p.watch.get().is_none(), "a send arms nothing");
assert!(exited_held(&p, None).unwrap());
assert!(p.watch.get().is_some(), "the first look arms the watch");
}
#[skuld::test]
fn a_fired_watch_on_a_process_is_exited_only_once_it_is_a_zombie() {
let (_child, id) = blocker();
let kernel = std::sync::Arc::new(a_kernel_that_fires_the_exit_watch(id.pid()));
let p = MacosProcess::open(std::sync::Arc::clone(&kernel), id)
.unwrap()
.expect("a live process opens");
assert!(
matches!(p.exited(), Ok(Exit::Recheck)),
"the watch fired, the process is not a zombie"
);
assert!(
kernel.all_rows_taken(),
"the first look's registration consumed the refusal"
);
assert_eq!(p.signal(Sig::Kill).unwrap(), Sent::Delivered);
wait_until_zombie(id.pid());
assert!(matches!(p.exited(), Ok(Exit::Exited)));
}
fn a_child_with_a_fired_watch(
kernel: impl FnOnce(FakeKernel<'static>, ProcessId) -> FakeKernel<'static>,
) -> (
StdChild,
std::process::ChildStdin,
ProcessId,
crate::os_process::macos::child::MacosChild<std::sync::Arc<FakeKernel<'static>>>,
std::sync::Arc<FakeKernel<'static>>,
) {
use crate::os_process::macos::child::MacosChild;
let mut child = crate::test_child::blocker();
let stdin = child.stdin.take().expect("piped stdin");
let id = ProcessId::of(child.id()).found().expect("identity of live child");
let unique = unique_of(id.pid());
let kernel = std::sync::Arc::new(kernel(a_kernel_that_fires_the_exit_watch(id.pid()), id));
let mac = MacosChild::new(std::sync::Arc::clone(&kernel), id.pid(), unique).expect("watch the child");
(child, stdin, id, mac, kernel)
}
#[skuld::test]
fn a_fired_watch_on_a_child_is_exited_only_once_it_is_a_zombie() {
let (_child, stdin, id, mac, kernel) = a_child_with_a_fired_watch(|kernel, _| kernel);
assert!(
matches!(mac.exited(), Ok(Exit::Recheck)),
"the watch fired, the child is not a zombie"
);
assert!(
kernel.all_rows_taken(),
"the adoption's registration consumed the refusal"
);
drop(stdin);
wait_until_zombie(id.pid());
assert!(matches!(mac.exited(), Ok(Exit::Exited)));
}
#[skuld::test]
fn the_first_look_at_an_unfired_watch_arms_it_by_one_read_and_reads_no_state() {
use crate::os_process::macos::child::MacosChild;
let (_child, id) = blocker();
let info = own_unique_info(id);
let found = || UniqRead::Found(info);
let refused = || UniqRead::Refused(libc::EPERM);
let process_kernel = FakeKernel::over(&HostKernel)
.on_unique_info(id.pid(), found())
.on_unique_info(id.pid(), found())
.on_unique_info(id.pid(), refused());
let p = MacosProcess::open(process_kernel, id)
.unwrap()
.expect("a live process opens");
let child_kernel = FakeKernel::over(&HostKernel)
.on_unique_info(id.pid(), found())
.on_unique_info(id.pid(), found())
.on_unique_info(id.pid(), refused());
let mac = MacosChild::new(child_kernel, id.pid(), info.unique_id).expect("watch the child");
assert!(
matches!(p.exited(), Ok(Exit::Running(_))),
"the first look arms the watch with one read of the unique id and reads no state: Running"
);
assert!(
matches!(p.exited(), Ok(Exit::Running(_))),
"a look at an armed, unfired watch reads nothing"
);
assert!(
matches!(mac.exited(), Ok(Exit::Running(_))),
"an unfired watch on a running child reads its parent and nothing else"
);
}
#[skuld::test]
fn a_wait_through_a_fired_watch_returns_a_process_that_reads_dead() {
use crate::identity::Liveness;
use crate::os_process::open_with;
let (_child, id) = blocker();
let kernel = std::sync::Arc::new(a_kernel_that_fires_the_exit_watch(id.pid()));
let p = open_with(std::sync::Arc::clone(&kernel), id)
.unwrap()
.expect("a live process opens");
assert!(
!exited_held(&*p, Some(Duration::ZERO)).unwrap(),
"a fired watch on a process that runs is not an exit"
);
assert!(kernel.all_rows_taken(), "the watch's registration consumed the refusal");
assert_eq!(p.signal(Sig::Kill).unwrap(), Sent::Delivered);
assert!(exited_held(&*p, None).unwrap());
assert_eq!(id.is_alive(), Liveness::Dead);
}
#[cfg(feature = "tokio")]
#[skuld::test]
async fn a_tokio_wait_through_a_fired_watch_returns_a_process_that_reads_dead() {
use crate::identity::Liveness;
use crate::os_process::open_with;
use crate::tokio::drive::exited_held;
let (_child, id) = blocker();
let kernel = std::sync::Arc::new(a_kernel_that_fires_the_exit_watch(id.pid()));
let p = open_with(std::sync::Arc::clone(&kernel), id)
.unwrap()
.expect("a live process opens");
assert!(
!exited_held(&*p, Some(Duration::ZERO)).await.unwrap(),
"a fired watch on a process that runs is not an exit"
);
assert!(kernel.all_rows_taken(), "the watch's registration consumed the refusal");
assert_eq!(p.signal(Sig::Kill).unwrap(), Sent::Delivered);
assert!(exited_held(&*p, None).await.unwrap());
assert_eq!(id.is_alive(), Liveness::Dead);
}
fn assert_a_refused_confirmation_is_unassessable_with_the_errno(p: &dyn OsProcess) {
let err = p.exited().expect_err("a refused read is an error");
assert!(matches!(err, Error::Unassessable { .. }), "{err:?}");
assert_eq!(crate::error::first_os_code(&err), Some(libc::EPERM), "{err:?}");
}
#[skuld::test]
fn a_refused_confirmation_of_a_process_is_unassessable_with_the_errno() {
use crate::os_process::open_with;
let (_child, id) = blocker();
let own = UniqRead::Found(own_unique_info(id));
let kernel = std::sync::Arc::new(
a_kernel_that_fires_the_exit_watch(id.pid())
.on_unique_info(id.pid(), own)
.on_unique_info(id.pid(), own)
.on_unique_info(id.pid(), UniqRead::Refused(libc::EPERM)),
);
let p = open_with(std::sync::Arc::clone(&kernel), id)
.unwrap()
.expect("a live process opens");
assert_a_refused_confirmation_is_unassessable_with_the_errno(&*p);
assert!(
kernel.all_rows_taken(),
"the open, the arming and the confirming read each read"
);
}
#[skuld::test]
fn a_refused_confirmation_of_a_child_is_unassessable_with_the_errno() {
let (_child, stdin, _id, mac, kernel) =
a_child_with_a_fired_watch(|kernel, id| kernel.on_unique_info(id.pid(), UniqRead::Refused(libc::EPERM)));
assert_a_refused_confirmation_is_unassessable_with_the_errno(&mac);
assert!(
kernel.all_rows_taken(),
"the adoption's registration was refused, and the confirming read was refused"
);
drop(stdin);
}
#[skuld::test]
fn a_watch_armed_while_another_process_holds_the_pid_reads_exited() {
use crate::os_process::open_with;
let (_child, id) = blocker();
let kernel = FakeKernel::over(&HostKernel)
.on_unique_info(id.pid(), UniqRead::Found(own_unique_info(id)))
.on_unique_info(id.pid(), other_unique_id(id.pid()))
.on_unique_info(id.pid(), other_unique_id(id.pid()));
let p = open_with(kernel, id).unwrap().expect("a live process opens");
assert!(
matches!(p.exited(), Ok(Exit::Exited)),
"the process that was opened is not the one holding the pid"
);
}
#[skuld::test]
fn a_refused_read_at_the_arming_is_unassessable_with_the_errno() {
use crate::os_process::open_with;
let (_child, id) = blocker();
let kernel = FakeKernel::over(&HostKernel)
.on_unique_info(id.pid(), UniqRead::Found(own_unique_info(id)))
.on_unique_info(id.pid(), UniqRead::Refused(libc::EPERM));
let p = open_with(kernel, id).unwrap().expect("a live process opens");
let err = p.exited().expect_err("a refused read is an error");
assert!(matches!(err, Error::Unassessable { .. }), "{err:?}");
assert_eq!(crate::error::first_os_code(&err), Some(libc::EPERM), "{err:?}");
}
#[skuld::test]
fn open_with_a_refused_read_is_unassessable_with_the_errno() {
let (_child, id) = blocker();
let kernel = FakeKernel::over(&HostKernel).on_unique_info(id.pid(), UniqRead::Refused(libc::EPERM));
let err = MacosProcess::open(kernel, id).expect_err("a refused read is an error");
let Error::Unassessable {
source: Some(source), ..
} = &err
else {
panic!("a refused read is Unassessable with its errno as source: {err:?}");
};
assert_eq!(crate::error::first_os_code(&**source), Some(libc::EPERM));
}
#[skuld::test]
fn a_refused_identity_read_for_an_open_keeps_its_errno_as_the_source() {
let kernel = FakeKernel::new().on_unique_info(77, UniqRead::Refused(libc::EPERM));
let err = super::read_for_open(&kernel, 77, "it was not opened").expect_err("refused");
match &err {
Error::Unassessable { detail, .. } => {
assert_eq!(detail, "pid 77 identity could not be read; it was not opened");
}
other => panic!("expected Unassessable, got {other:?}"),
}
assert_eq!(crate::error::first_os_code(&err), Some(libc::EPERM), "{err:?}");
}
#[skuld::test]
fn a_refused_has_exited_read_is_unassessable_with_its_errno_as_the_source_and_words_none() {
for (situation, said) in [
(
"its exit watch fired, but whether it has exited could not be read",
"pid 77: its exit watch fired, but whether it has exited could not be read",
),
(
"its exit watch has not fired, and whether it has exited could not be read",
"pid 77: its exit watch has not fired, and whether it has exited could not be read",
),
] {
let err = super::exit_read_refused(77, libc::EPERM, situation);
let Error::Unassessable { detail, .. } = &err else {
panic!("{err:?}");
};
assert_eq!(crate::error::first_os_code(&err), Some(libc::EPERM), "{err:?}");
assert_eq!(detail.as_str(), said);
assert!(!crate::error::said(&err).contains("errno"), "{err:?}");
}
}
#[skuld::test]
fn a_refused_read_past_an_unfired_watch_is_unassessable_with_the_errno() {
use crate::identity::{short_info, LAUNCHD};
use crate::os_process::macos::child::MacosChild;
for zombie_read in [false, true] {
let (_child, id) = blocker();
let read = own_unique_info(id);
let mut kernel = FakeKernel::over(&HostKernel).on_short_bsd(id.pid(), Ok(short_info(LAUNCHD, libc::SRUN)));
if zombie_read {
kernel = kernel
.on_unique_info(id.pid(), UniqRead::Found(read))
.on_unique_info(id.pid(), UniqRead::Found(read));
}
kernel = kernel.on_unique_info(id.pid(), UniqRead::Refused(libc::EPERM));
let mac = MacosChild::new(kernel, id.pid(), read.unique_id).expect("watch the child");
let err = mac.exited().expect_err("a refused read is an error");
let Error::Unassessable { detail, .. } = &err else {
panic!("{err:?}");
};
assert_eq!(crate::error::first_os_code(&err), Some(libc::EPERM), "{err:?}");
assert_eq!(
detail,
&format!(
"pid {}: its exit watch has not fired, and whether it has exited could not be read",
id.pid()
),
"zombie read: {zombie_read}"
);
}
}
#[skuld::test]
fn an_unfired_watch_on_a_running_child_another_process_holds_is_a_recheck() {
use crate::identity::{short_info, LAUNCHD};
use crate::os_process::macos::child::MacosChild;
use crate::wait::exit_only::macos_tests::{echild_yet_resolvable, held_by_a_tracer_pid};
let (pid, unique) = echild_yet_resolvable();
let kernel = [LAUNCHD, held_by_a_tracer_pid()]
.into_iter()
.fold(FakeKernel::over(&HostKernel), |kernel, parent| {
kernel
.on_short_bsd(pid, Ok(short_info(parent, libc::SRUN)))
.on_short_bsd(pid, Ok(short_info(parent, libc::SRUN)))
});
let mac = MacosChild::new(kernel, pid, unique).expect("watch the stand-in");
for parent in [LAUNCHD, held_by_a_tracer_pid()] {
assert!(
matches!(mac.exited(), Ok(Exit::Recheck)),
"held by pid {parent}, the child is no zombie and its watch cannot fire"
);
}
}
#[skuld::test]
fn a_child_that_was_reaped_is_exited_with_its_watch_unfired_and_remembered() {
use crate::os_process::macos::child::MacosChild;
for reaped in ["gone", "another process"] {
let (_child, id) = blocker();
let read = own_unique_info(id);
let answer = match reaped {
"gone" => UniqRead::Gone,
_ => other_unique_id(id.pid()),
};
let kernel = FakeKernel::over(&HostKernel)
.on_unique_info(id.pid(), answer)
.on_unique_info(id.pid(), UniqRead::Refused(libc::EPERM));
let mac = MacosChild::new(kernel, id.pid(), read.unique_id).expect("watch the child");
assert!(matches!(mac.exited(), Ok(Exit::Exited)), "{reaped}");
assert!(
matches!(mac.exited(), Ok(Exit::Exited)),
"a confirmed exit is not read again: {reaped}"
);
}
}
#[skuld::test]
fn a_confirmed_exit_is_remembered_and_later_looks_read_nothing() {
use crate::identity::{short_info, LAUNCHD};
use crate::os_process::open_with;
let (_child, id) = blocker();
let own = UniqRead::Found(own_unique_info(id));
let kernel = std::sync::Arc::new(
a_kernel_that_fires_the_exit_watch(id.pid())
.on_unique_info(id.pid(), own)
.on_unique_info(id.pid(), own)
.on_unique_info(id.pid(), own)
.on_short_bsd(id.pid(), Ok(short_info(LAUNCHD, libc::SRUN)))
.on_unique_info(id.pid(), own)
.on_unique_info(id.pid(), own)
.on_short_bsd(id.pid(), Ok(short_info(LAUNCHD, libc::SZOMB)))
.on_unique_info(id.pid(), own),
);
let p = open_with(std::sync::Arc::clone(&kernel), id)
.unwrap()
.expect("a live process opens");
assert!(
matches!(p.exited(), Ok(Exit::Recheck)),
"the watch fired and the first confirming read finds it running"
);
assert!(
matches!(p.exited(), Ok(Exit::Exited)),
"the second confirming read finds a zombie"
);
assert!(kernel.all_rows_taken(), "the two confirming looks took every row");
assert!(matches!(p.exited(), Ok(Exit::Exited)));
assert!(matches!(p.exited(), Ok(Exit::Exited)));
}
#[skuld::test]
fn pid_zero_is_refused_unassessable() {
let p = MacosProcess {
kernel: HostKernel,
id: ProcessId::from_parts_for_test(0, crate::identity::Key::for_test(1)),
unique: 1,
idversion: std::sync::atomic::AtomicI32::new(1),
watch: std::sync::OnceLock::new(),
};
let sends = crate::send_log::Capture::start();
let err = p.signal(Sig::Kill).expect_err("pid 0 is no target");
assert!(
matches!(err, Error::Unassessable { source: None, .. }),
"pid 0 is refused as Unassessable: {err:?}"
);
assert_eq!(sends.by_identity(), vec![], "nothing reached the kernel");
}
#[skuld::test]
fn a_pid_zero_identity_never_reaches_kill() {
let bogus = crate::identity::ProcessId::from_parts_for_test(0, crate::identity::Key::for_test(1));
assert!(
open(bogus).expect("a pid-0 identity resolves to a stranger").is_none(),
"a pid-0 identity opens nothing"
);
assert_eq!(
crate::identity::ProcessId::current().is_alive(),
crate::identity::Liveness::Alive,
"the caller must have survived - nothing may signal process group 0"
);
}
#[skuld::test]
fn two_threads_waiting_on_one_process_both_return_after_it_exits() {
let (_child, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
let p: &dyn OsProcess = &*p;
std::thread::scope(|scope| {
let waiters = [
scope.spawn(|| exited_held(p, None)),
scope.spawn(|| exited_held(p, None)),
];
assert_eq!(p.signal(Sig::Kill).unwrap(), Sent::Delivered);
for waiter in waiters {
assert!(
waiter.join().unwrap().unwrap(),
"every wait ends when the process exits"
);
}
});
assert!(
matches!(p.exited().unwrap(), Exit::Exited),
"and a later look says Exited"
);
}
#[skuld::test]
fn esrch_at_registration_reads_as_exited_and_rechecks() {
use crate::os_process::child::{adopt_std_over, TryObserveOutcome};
let mut child = crate::test_child::blocker().into_inner();
drop(child.stdin.take());
let id = ProcessId::of(child.id()).found().expect("identity");
wait_until_zombie(id.pid());
let kernel = std::sync::Arc::new(a_kernel_that_fires_the_exit_watch(id.pid()).on_peek(id.pid(), Ok(None)));
let os = adopt_std_over(
{
let kernel = std::sync::Arc::clone(&kernel);
move |_, _| kernel
},
child,
id,
)
.unwrap_or_else(|(e, _)| panic!("adopt: {e}"));
assert!(matches!(os.exited().unwrap(), Exit::Exited));
assert!(matches!(os.try_observe().unwrap(), TryObserveOutcome::Recheck));
assert!(
kernel.all_rows_taken(),
"the adoption's registration was refused, and the first try_observe's peek took its row"
);
assert!(matches!(os.try_observe().unwrap(), TryObserveOutcome::Exited(_)));
}
fn a_running_child_with_a_watch(
kernel: impl FnOnce(u32) -> FakeKernel<'static>,
) -> (
StdChild,
std::process::ChildStdin,
ProcessId,
crate::os_process::macos::child::MacosChild<std::sync::Arc<FakeKernel<'static>>>,
std::sync::Arc<FakeKernel<'static>>,
) {
use crate::os_process::macos::child::MacosChild;
let mut child = crate::test_child::blocker();
let stdin = child.stdin.take().expect("piped stdin");
let id = ProcessId::of(child.id()).found().expect("identity of live child");
let UniqRead::Found(read) = HostKernel.unique_info(id.pid()) else {
panic!("the blocker's unique id must be readable");
};
let kernel = std::sync::Arc::new(kernel(id.pid()));
let mac = MacosChild::new(std::sync::Arc::clone(&kernel), id.pid(), read.unique_id).expect("watch the child");
(child, stdin, id, mac, kernel)
}
fn a_kernel_with_a_silent_exit_watch(pid: u32) -> FakeKernel<'static> {
FakeKernel::over(&HostKernel).on_subscribe(ExitWatch::registration(pid))
}
#[skuld::test]
fn an_exit_wait_that_found_no_registration_rechecks_instead_of_waiting() {
use crate::os_process::drive::Step;
let (_child, stdin, _id, mac, kernel) = a_running_child_with_a_watch(|pid| {
FakeKernel::over(&HostKernel)
.on_subscribe(reap_registration(pid))
.on_subscribe_refused(reap_registration(pid), libc::ESRCH)
});
let mut wait = mac.exit_wait().expect("arm the wait");
assert!(
matches!(wait.look(), Ok(Step::Recheck)),
"no event will come: the look must not wait"
);
assert!(
kernel.all_rows_taken(),
"the adoption made its registration and the exit wait's was refused"
);
drop(wait);
drop(stdin);
}
#[skuld::test]
fn an_exit_wait_whose_drain_read_the_exit_rechecks_while_the_shared_watch_says_running() {
use crate::os_process::drive::Step;
let (_child, stdin, id, mac, kernel) = a_running_child_with_a_watch(a_kernel_with_a_silent_exit_watch);
let mut wait = mac.exit_wait().expect("arm the wait");
drop(stdin);
wait_until_zombie(id.pid());
assert!(
matches!(wait.look(), Ok(Step::Recheck)),
"the drain read NOTE_EXIT and the shared watch says Running"
);
assert!(kernel.all_rows_taken(), "the adoption armed the silent exit watch");
drop(wait);
}
#[skuld::test]
fn an_exit_wait_whose_drain_read_the_reap_is_done_because_the_pid_is_gone() {
use crate::os_process::drive::Step;
let (mut child, stdin, id, mac, kernel) = a_running_child_with_a_watch(a_kernel_with_a_silent_exit_watch);
let mut wait = mac.exit_wait().expect("arm the wait");
drop(stdin);
wait_until_zombie(id.pid());
child.wait().unwrap();
assert!(
matches!(wait.look(), Ok(Step::Done(()))),
"NOTE_EXIT and NOTE_REAP were drained, and the pid is gone"
);
assert!(kernel.all_rows_taken(), "the adoption armed the silent exit watch");
}
#[skuld::test]
fn esrch_at_the_reap_registration_adopts_a_running_child() {
use crate::os_process::macos::child::MacosChild;
let mut child = crate::test_child::blocker();
let stdin = child.stdin.take().expect("piped stdin");
let id = ProcessId::of(child.id()).found().expect("identity");
let UniqRead::Found(read) = HostKernel.unique_info(id.pid()) else {
panic!("the blocker's unique id must be readable");
};
let kernel = std::sync::Arc::new(
FakeKernel::over(&HostKernel).on_subscribe_refused(reap_registration(id.pid()), libc::ESRCH),
);
let os = MacosChild::new(std::sync::Arc::clone(&kernel), id.pid(), read.unique_id)
.expect("the reap's ESRCH is no failure");
assert!(kernel.all_rows_taken(), "the reap registration consumed the refusal");
assert!(
matches!(os.exited().unwrap(), Exit::Running(_)),
"the exit watch registered on its own: the child runs"
);
drop(stdin);
}
#[skuld::test]
fn a_child_whose_tracer_is_exiting_at_launchd_is_a_recheck() {
use crate::identity::{short_info, LAUNCHD};
use crate::os_process::child::{OsChild, TryObserveOutcome};
use crate::os_process::macos::child::MacosChild;
use crate::wait::exit_only::macos_tests::echild_yet_resolvable;
let (pid, unique) = echild_yet_resolvable();
let tracer = crate::test_child::blocker();
let tracer_pid = tracer.id();
let kernel = FakeKernel::over(&HostKernel)
.on_short_bsd(pid, Ok(short_info(tracer_pid, libc::SRUN)))
.on_kinfo(
tracer_pid,
kinfo_proc::of_host_held_by(tracer_pid, std::process::id() as libc::pid_t, LAUNCHD as libc::pid_t),
);
let mac = MacosChild::new(kernel, pid, unique).expect("watch the stand-in");
let look = mac.try_observe().expect("look");
assert!(matches!(look, TryObserveOutcome::Recheck), "{look:?}");
}
const KQUEUE_MARKER: &str = "COSCA_TESTCFG_MACCHILD_KQUEUE";
const KQUEUE_CASE_ENV: &str = "COSCA_TESTCFG_MACCHILD_KQUEUE_CASE";
#[skuld::test]
fn adoption_fails_when_a_kqueue_cannot_be_made() {
if !crate::test_child::is_marked_fixture_reexec(KQUEUE_MARKER) {
crate::test_child::run_fixture_case(
crate::test_child::fixture_path!(adoption_fails_when_a_kqueue_cannot_be_made),
KQUEUE_MARKER,
KQUEUE_CASE_ENV,
"",
);
return;
}
use crate::os_process::child::adopt_std;
let (child, stdin) = crate::test_child::blocker().into_parts();
let id = ProcessId::of(child.id()).found().expect("identity");
let refused = crate::test_child::refuse_new_descriptors();
let adopted = adopt_std(child, id);
let limit = refused.limit;
drop(refused);
let (error, mut child) = match adopted {
Ok(_) => panic!("adopted with the descriptor table full (limit {limit})"),
Err(refused) => refused,
};
let Error::Io(e) = &error else {
panic!("a refused kqueue is an io error: {error:?}")
};
assert_eq!(e.raw_os_error(), Some(libc::EMFILE), "{e:?}");
drop(stdin);
child.wait().expect("the child is handed back unreaped");
}
#[skuld::test]
fn try_observe_of_a_running_child_waits_on_the_reap_kqueue() {
use std::os::fd::AsRawFd;
use crate::os_process::child::{adopted_blocker, TryObserveOutcome};
use crate::os_process::WaitFor;
use crate::wait::exit_only::Reap;
let (os, id, stdin) = adopted_blocker();
let Exit::Running(WaitFor::Readable(exit_fd)) = os.exited().unwrap() else {
panic!("a live child reads Running");
};
let TryObserveOutcome::Running(WaitFor::Readable(reap_fd)) = os.try_observe().unwrap() else {
panic!("a running child is waited on its reap kqueue");
};
assert_ne!(reap_fd.as_raw_fd(), exit_fd.as_raw_fd());
drop(stdin);
assert!(exited_held(&*os, None).unwrap());
wait_until_zombie(id.pid());
assert!(matches!(os.try_observe().unwrap(), TryObserveOutcome::Exited(_)));
assert!(matches!(os.try_reap().unwrap(), Reap::Reaped(_)));
}
#[skuld::test]
fn peek_of_an_exited_child_consumes_nothing() {
use crate::os_process::child::{adopted_blocker, PeekOutcome};
use crate::wait::exit_only::Reap;
let (os, id, stdin) = adopted_blocker();
drop(stdin);
assert!(exited_held(&*os, None).unwrap());
wait_until_zombie(id.pid());
assert!(matches!(os.peek().unwrap(), PeekOutcome::Exit(_)));
assert!(matches!(os.peek().unwrap(), PeekOutcome::Exit(_)));
assert!(matches!(os.try_reap().unwrap(), Reap::Reaped(_)));
}