use std::os::fd::OwnedFd;
use rustix::io::Errno;
use rustix::process::Pid;
use crate::error::Error;
use crate::identity::{FakeKernel, HostKernel, Key, ProcessId};
const PARENT: u32 = 4242;
const SELF: u32 = 4243;
const PARENT_INO: u64 = 900;
const SELF_INO: u64 = 5;
fn stand_in() -> OwnedFd {
OwnedFd::from(std::fs::File::open("/dev/null").expect("/dev/null opens"))
}
fn stat(pid: u32, ppid: u32, start: u64) -> Vec<u8> {
format!("{pid} (x) S {ppid} {ppid} 1 0 -1 0 0 0 0 0 0 0 0 0 20 0 1 0 {start} 0 0").into_bytes()
}
fn me() -> ProcessId {
ProcessId::from_parts_for_test(SELF, Key::Pidfs(SELF_INO))
}
fn table_without_self_stat(parent_start: u64) -> FakeKernel<'static> {
FakeKernel::over(&HostKernel)
.same_view()
.on_pids(Ok(&[PARENT, SELF]))
.on_pidfd_open(PARENT, Ok(stand_in()))
.on_pidfs_ino(PARENT, Ok(Some(PARENT_INO)))
.on_pidfd_open(SELF, Ok(stand_in()))
.on_pidfs_ino(SELF, Ok(Some(SELF_INO)))
.on_read(&format!("{PARENT}/stat"), Ok(&stat(PARENT, 1, parent_start)))
}
fn table(parent_start: u64) -> FakeKernel<'static> {
table_without_self_stat(parent_start).on_read(&format!("{SELF}/stat"), Ok(&stat(SELF, PARENT, 2_000)))
}
#[skuld::test]
fn the_parent_of_a_pidfs_id_is_the_pidfs_id_of_its_parent() {
let parent = me().parent_with(&table(1_000)).expect("a readable table");
assert_eq!(
parent,
Some(ProcessId::from_parts_for_test(PARENT, Key::Pidfs(PARENT_INO)))
);
}
#[skuld::test]
fn a_parent_that_started_after_its_child_is_named_while_the_childs_ppid_still_names_it() {
for parent_start in [2_000, 3_000] {
assert_eq!(
me().parent_with(&table(parent_start)).expect("a readable table"),
Some(ProcessId::from_parts_for_test(PARENT, Key::Pidfs(PARENT_INO))),
"a parent started at {parent_start}"
);
}
}
#[skuld::test]
fn a_pidfs_id_whose_start_time_is_gone_has_no_parent() {
let kernel = table_without_self_stat(1_000)
.on_read(&format!("{SELF}/stat"), Err(libc::ENOENT))
.on_signal_probe(Pid::from_raw(SELF as i32).unwrap(), Err(Errno::SRCH));
assert_eq!(me().parent_with(&kernel).expect("gone is not an error"), None);
}
#[skuld::test]
fn a_recycled_pidfs_id_whose_new_holder_cannot_be_read_has_no_parent() {
let kernel = table_without_self_stat(1_000)
.on_pidfs_ino(SELF, Ok(Some(SELF_INO + 1)))
.on_read(&format!("{SELF}/stat"), Err(libc::EACCES))
.on_signal_probe(Pid::from_raw(SELF as i32).unwrap(), Ok(()));
assert_eq!(me().parent_with(&kernel).expect("a recycled pid is not an error"), None);
}
#[skuld::test]
fn a_pidfs_id_whose_start_time_cannot_be_read_is_unassessable() {
let kernel = table_without_self_stat(1_000)
.on_read(&format!("{SELF}/stat"), Err(libc::EACCES))
.on_signal_probe(Pid::from_raw(SELF as i32).unwrap(), Ok(()));
match me().parent_with(&kernel) {
Err(Error::Unassessable { detail, .. }) => {
assert!(
detail.contains(&format!("pid {SELF} exists but could not be queried")),
"{detail}"
);
}
other => panic!("expected Unassessable, got {other:?}"),
}
}
#[skuld::test]
fn an_id_whose_own_read_shows_another_ppid_has_no_parent() {
for parent_start in [1_000, 2_000] {
let kernel = table(parent_start).on_read_after(&format!("{SELF}/stat"), 1, Ok(&stat(SELF, 1, 2_000)));
assert_eq!(
me().parent_with(&kernel).expect("a readable table"),
None,
"a parent started at {parent_start}"
);
}
}
#[skuld::test]
fn a_pidfs_id_recycled_between_the_reads_whose_new_holder_cannot_be_read_has_no_parent() {
let kernel = table(1_000)
.on_pidfs_ino_after_read(SELF, &format!("{PARENT}/stat"), Ok(Some(SELF_INO + 1)))
.on_read_after(&format!("{SELF}/stat"), 1, Err(libc::EACCES))
.on_signal_probe(Pid::from_raw(SELF as i32).unwrap(), Ok(()));
assert_eq!(me().parent_with(&kernel).expect("a recycled pid is not an error"), None);
}
#[skuld::test]
fn a_childs_second_read_that_fails_is_the_subject_of_the_error() {
let kernel = table(1_000)
.on_read_after(&format!("{SELF}/stat"), 1, Err(libc::EACCES))
.on_signal_probe(Pid::from_raw(SELF as i32).unwrap(), Ok(()));
match me().parent_with(&kernel) {
Err(Error::Unassessable { detail, .. }) => {
assert!(
detail.contains(&format!("pid {SELF} exists but could not be queried")),
"{detail}"
);
}
other => panic!("expected Unassessable, got {other:?}"),
}
}
fn ticks_table_without_parent_stat() -> FakeKernel<'static> {
FakeKernel::over(&HostKernel)
.same_view()
.on_pids(Ok(&[PARENT, SELF]))
.on_pidfd_open(PARENT, Ok(stand_in()))
.on_pidfs_ino(PARENT, Ok(None))
.on_pidfd_open(SELF, Ok(stand_in()))
.on_pidfs_ino(SELF, Ok(None))
.on_read(&format!("{SELF}/stat"), Ok(&stat(SELF, PARENT, 2_000)))
}
fn ticks_me() -> ProcessId {
ProcessId::from_parts_for_test(SELF, Key::Ticks(2_000))
}
fn ticks_table() -> FakeKernel<'static> {
ticks_table_without_parent_stat().on_read(&format!("{PARENT}/stat"), Ok(&stat(PARENT, 1, 1_000)))
}
#[skuld::test]
fn a_parent_that_loses_its_number_after_the_childs_read_is_not_named() {
let held = ticks_table();
assert_eq!(
ticks_me().parent_with(&held).expect("a readable table"),
Some(ProcessId::from_parts_for_test(PARENT, Key::Ticks(1_000)))
);
let lost = ticks_table().on_read_after(&format!("{PARENT}/stat"), 1, Ok(&stat(PARENT, 1, 1_001)));
assert_eq!(ticks_me().parent_with(&lost).expect("a readable table"), None);
}
#[skuld::test]
fn a_ticks_id_whose_pid_shows_another_start_time_at_the_second_read_has_no_parent() {
let kernel = ticks_table().on_read_after(&format!("{SELF}/stat"), 1, Ok(&stat(SELF, PARENT, 2_001)));
assert_eq!(ticks_me().parent_with(&kernel).expect("a readable table"), None);
}
#[skuld::test]
fn a_pidfs_id_whose_pid_now_shows_a_ticks_key_has_no_parent() {
let kernel = table_without_self_stat(1_000)
.on_pidfs_ino(SELF, Ok(None))
.on_read(&format!("{SELF}/stat"), Err(libc::EACCES))
.on_signal_probe(Pid::from_raw(SELF as i32).unwrap(), Ok(()));
assert_eq!(me().parent_with(&kernel).expect("a recycled pid is not an error"), None);
}
#[skuld::test]
fn a_ticks_id_whose_record_cannot_be_read_is_unassessable() {
let kernel = ticks_table_without_parent_stat()
.on_read(&format!("{SELF}/stat"), Err(libc::EACCES))
.on_signal_probe(Pid::from_raw(SELF as i32).unwrap(), Ok(()));
match ticks_me().parent_with(&kernel) {
Err(Error::Unassessable { detail, .. }) => {
assert!(
detail.contains(&format!("pid {SELF} exists but could not be queried")),
"{detail}"
);
}
other => panic!("expected Unassessable, got {other:?}"),
}
}
#[skuld::test]
fn a_parent_that_cannot_be_proved_again_is_the_subject_of_the_error() {
let kernel = ticks_table()
.on_read_after(&format!("{PARENT}/stat"), 1, Err(libc::EACCES))
.on_signal_probe(Pid::from_raw(PARENT as i32).unwrap(), Ok(()));
match ticks_me().parent_with(&kernel) {
Err(Error::Unassessable { detail, .. }) => {
assert!(
detail.contains(&format!("ppid {PARENT} exists but could not be queried")),
"{detail}"
);
assert!(detail.contains(&format!("reading pid {SELF}")), "{detail}");
}
other => panic!("expected Unassessable, got {other:?}"),
}
}
#[skuld::test]
fn a_parent_that_cannot_be_read_is_the_subject_of_the_error() {
let kernel = ticks_table_without_parent_stat()
.on_read(&format!("{PARENT}/stat"), Err(libc::EACCES))
.on_signal_probe(Pid::from_raw(PARENT as i32).unwrap(), Ok(()));
match ticks_me().parent_with(&kernel) {
Err(Error::Unassessable { detail, .. }) => {
assert!(
detail.contains(&format!("ppid {PARENT} exists but could not be queried")),
"{detail}"
);
assert!(!detail.contains("reading pid"), "{detail}");
}
other => panic!("expected Unassessable, got {other:?}"),
}
}