use rustix::io::Errno;
use rustix::process::Pid;
use super::signal_probe;
use crate::error::Error;
use crate::identity::probe::SignalProbe;
use crate::identity::stat_parse::stat_fixture;
use crate::identity::{ticks_host, ticks_id, Existence, FakeKernel, Key, Liveness, ProcessId, Resolved};
pub(super) const NO_PROCESS_CAN_HOLD: u32 = i32::MAX as u32;
const NO_PROCESS_CAN_HOLD_TARGET: Pid = Pid::from_raw(i32::MAX).unwrap();
fn unavailable_views(pid: u32) -> [(&'static str, FakeKernel<'static>); 3] {
[
("diverged", ticks_host(pid).diverged_view()),
("unassessable", ticks_host(pid).unreadable_view()),
("no /proc", ticks_host(pid).open_proc_fails(Errno::MFILE)),
]
}
#[skuld::test]
fn of_a_live_pid_is_unknown_when_the_view_is_unavailable() {
for (name, kernel) in unavailable_views(std::process::id()) {
let (got, cause) = ProcessId::of_with(&kernel, std::process::id());
assert!(matches!(got, Resolved::Unknown), "{name}: got {got:?}");
assert!(
cause.into_error("the process").is_some(),
"{name}: the view is the cause"
);
}
}
#[skuld::test]
fn the_line_of_an_unavailable_view_says_the_os_error() {
crate::log_capture::install();
let pid = std::process::id();
let kernel = ticks_host(pid).open_proc_fails(Errno::MFILE);
let mark = crate::log_capture::mark();
let (got, _) = ProcessId::of_with(&kernel, pid);
assert!(matches!(got, Resolved::Unknown), "{got:?}");
let records = crate::log_capture::records_since_on_current_thread(mark, &format!("/proc/{pid}/stat not read"));
let os_error = std::io::Error::from(Errno::MFILE).to_string();
assert!(records.iter().any(|(_, line)| line.contains(&os_error)), "{records:?}");
}
#[skuld::test]
fn of_a_pid_no_process_can_hold_is_gone_whatever_the_view() {
for (name, kernel) in unavailable_views(NO_PROCESS_CAN_HOLD) {
let kernel = kernel.on_signal_probe(NO_PROCESS_CAN_HOLD_TARGET, Err(Errno::SRCH));
let got = ProcessId::of_with(&kernel, NO_PROCESS_CAN_HOLD).0;
assert!(matches!(got, Resolved::Gone), "{name}: got {got:?}");
}
}
#[skuld::test]
fn exists_and_is_alive_are_unknown_for_a_live_process_when_the_view_is_unavailable() {
let id = ticks_id(std::process::id());
for (name, kernel) in unavailable_views(std::process::id()) {
assert_eq!(id.exists_with(&kernel).0, Existence::Unknown, "{name}");
assert_eq!(id.is_alive_with(&kernel), Liveness::Unknown, "{name}");
}
}
#[skuld::test]
fn exists_and_is_alive_are_gone_and_dead_for_a_pid_no_process_can_hold_whatever_the_view() {
let id = ProcessId::from_parts_for_test(NO_PROCESS_CAN_HOLD, Key::for_test(1));
for (name, kernel) in unavailable_views(NO_PROCESS_CAN_HOLD) {
let kernel = kernel.on_signal_probe(NO_PROCESS_CAN_HOLD_TARGET, Err(Errno::SRCH));
assert_eq!(id.exists_with(&kernel).0, Existence::Gone, "{name}");
assert_eq!(id.is_alive_with(&kernel), Liveness::Dead, "{name}");
}
}
#[skuld::test]
fn a_live_process_is_present_and_alive_under_the_ordinary_view() {
let id = ProcessId::current();
assert_eq!(id.exists(), Existence::Present);
assert_eq!(id.is_alive(), Liveness::Alive);
}
#[skuld::test]
fn a_stat_that_cannot_be_read_is_gone_only_when_kill_says_esrch() {
const PID: u32 = 4242;
let cases: [(i32, Result<(), Errno>, bool); 3] = [
(libc::ENOENT, Ok(()), false),
(libc::ENOENT, Err(Errno::SRCH), true),
(libc::EACCES, Err(Errno::PERM), false),
];
for (errno, probe, gone) in cases {
let kernel = ticks_host(PID)
.same_view()
.on_read(&format!("{PID}/stat"), Err(errno))
.on_signal_probe(Pid::from_raw(PID as i32).unwrap(), probe);
let got = ProcessId::of_with(&kernel, PID).0;
match (gone, &got) {
(true, Resolved::Gone) | (false, Resolved::Unknown) => {}
_ => panic!("stat {errno}, kill {probe:?}: got {got:?}"),
}
}
let found = ticks_host(PID)
.same_view()
.on_read(&format!("{PID}/stat"), Ok(&stat_fixture("S", 424242)));
match ProcessId::of_with(&found, PID).0 {
Resolved::Found(id) => assert_eq!(id.key(), Key::for_test(424242)),
other => panic!("a readable stat must resolve, got {other:?}"),
}
}
#[skuld::test]
fn a_number_that_is_no_kill_target_is_never_probed() {
let kernel = FakeKernel::new();
for pid in [0, i32::MAX as u32 + 1, u32::MAX] {
assert_eq!(signal_probe(&kernel, pid), SignalProbe::NotAPid, "{pid}");
}
}
fn cause_error_of_live_read(kernel: &FakeKernel<'_>) -> Option<Error> {
let (got, cause) = ProcessId::of_with(kernel, std::process::id());
assert!(matches!(got, Resolved::Unknown), "got {got:?}");
cause.into_error("the spawned child")
}
#[skuld::test]
fn the_error_for_a_host_without_openat2_names_the_requirement_and_the_errno() {
let kernel = ticks_host(std::process::id()).open_proc_fails(Errno::NOSYS);
let err = cause_error_of_live_read(&kernel).expect("no openat2 is an error");
assert_eq!(
err.to_string(),
"identifying the spawned child is not supported on linux: cosca requires openat2 \
(Linux \u{2265} 5.6), refused here: openat2 answered ENOSYS"
);
}
#[skuld::test]
fn the_error_for_a_diverged_view_carries_the_view() {
let kernel = ticks_host(std::process::id()).diverged_view();
let err = cause_error_of_live_read(&kernel).expect("a diverged view is an error");
assert!(
matches!(&err, Error::Unassessable { detail, .. }
if detail == "the spawned child identity could not be read: this process's /proc is an outer pid namespace's"),
"{err:?}"
);
}
#[skuld::test]
fn the_error_for_an_unopenable_proc_keeps_the_os_error_as_its_source() {
let kernel = ticks_host(std::process::id()).open_proc_fails(Errno::MFILE);
let err = cause_error_of_live_read(&kernel).expect("no /proc is an error");
let Error::Unassessable { detail, .. } = &err else {
panic!("{err:?}");
};
assert_eq!(
detail,
"the spawned child identity could not be read: /proc could not be opened"
);
assert_eq!(
crate::error::first_os_code(&err),
Some(Errno::MFILE.raw_os_error()),
"{err:?}"
);
}
#[skuld::test]
fn the_error_for_a_failed_pidfd_call_keeps_the_errno_as_its_source() {
let err = crate::identity::call_error("pid 4242", crate::identity::FailedCall::pidfd_open(Errno::MFILE));
let Error::Unassessable { detail, .. } = &err else {
panic!("{err:?}");
};
assert_eq!(detail, "pid 4242 identity could not be read: pidfd_open");
assert_eq!(
crate::error::first_os_code(&err),
Some(Errno::MFILE.raw_os_error()),
"{err:?}"
);
}
fn unknown_reads_of_a_stat(stat: i32, recheck: Option<i32>) -> [Option<Error>; 2] {
const PID: u32 = 4242;
let mut kernel = ticks_host(PID)
.same_view()
.on_read(&format!("{PID}/stat"), Err(stat))
.on_signal_probe(Pid::from_raw(PID as i32).unwrap(), Ok(()));
if let Some(recheck) = recheck {
kernel = kernel.on_read("self/stat", Err(recheck));
}
let (got, cause) = ProcessId::of_with(&kernel, PID);
assert!(matches!(got, Resolved::Unknown), "{stat}/{recheck:?}: got {got:?}");
let of = cause.into_error("pid 4242");
let (existence, cause) = ProcessId::from_parts_for_test(PID, Key::for_test(1)).exists_with(&kernel);
assert_eq!(existence, Existence::Unknown, "{stat}/{recheck:?}");
[of, cause.into_error("pid 4242")]
}
#[skuld::test]
fn a_refused_stat_read_of_a_live_pid_is_unknown_and_unsupported() {
for (stat, recheck, refusing) in [
(libc::ENOSYS, None, libc::ENOSYS),
(libc::EPERM, Some(libc::ENOSYS), libc::ENOSYS),
] {
for error in unknown_reads_of_a_stat(stat, recheck) {
let Some(err @ Error::Unsupported { .. }) = error else {
panic!("{stat}/{recheck:?}: expected Unsupported, got {error:?}");
};
assert_eq!(crate::error::first_os_code(&err), Some(refusing), "{stat}/{recheck:?}");
}
}
}
#[skuld::test]
fn an_unreadable_stat_of_a_live_pid_is_unknown_and_unassessable_naming_pid_and_errno() {
for (stat, recheck, below) in [
(libc::EIO, None, libc::EIO),
(libc::EPERM, Some(libc::EMFILE), libc::EMFILE),
] {
for error in unknown_reads_of_a_stat(stat, recheck) {
let Some(err @ Error::Unassessable { .. }) = error else {
panic!("{stat}/{recheck:?}: expected Unassessable, got {error:?}");
};
let text = crate::error::Report(&err).to_string();
assert!(
text.contains("pid 4242") && text.contains("4242/stat"),
"{stat}: {text}"
);
assert_eq!(
crate::error::first_os_code(&err),
Some(below),
"{stat}/{recheck:?}: {text}"
);
}
}
}
#[skuld::test]
fn no_error_is_blamed_on_a_fine_view() {
let kernel = ticks_host(4242)
.same_view()
.on_read("4242/stat", Err(libc::ENOENT))
.on_signal_probe(Pid::from_raw(4242).unwrap(), Ok(()));
let (got, cause) = ProcessId::of_with(&kernel, 4242);
assert!(matches!(got, Resolved::Unknown), "got {got:?}");
assert!(cause.into_error("the spawned child").is_none());
}