use super::{proc_state, read_proc_state, StateUnknown};
use crate::containment::cgroup::test_support::{handle_of, pidfd_of, KillOnDrop};
use crate::identity::{FakeKernel, HostKernel, DIVERGED_REASON, UNREADABLE_STATUS_REASON};
use crate::test_child::namespaces as ns;
use crate::test_child::{fixture_path, member_command};
use crate::test_groups::{namespaces, pidfd_group_signal, Group};
#[skuld::test]
fn a_live_process_has_a_state_under_the_ordinary_view() {
assert!(proc_state(
&HostKernel,
handle_of(std::process::id(), &pidfd_of(std::process::id()))
)
.is_some());
}
#[skuld::test]
fn an_unreaped_exited_child_is_a_zombie() {
let mut child = crate::test_spawn::spawn(&mut std::process::Command::new("true")).expect("spawn true");
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
loop {
let rc = unsafe {
libc::waitid(
libc::P_PID,
child.id() as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOWAIT,
)
};
if rc == 0 {
break;
}
let err = std::io::Error::last_os_error();
assert_eq!(err.raw_os_error(), Some(libc::EINTR), "waitid: {err}");
}
assert_eq!(read_proc_state(&HostKernel, child.id()).ok(), Some('Z'));
assert_eq!(
proc_state(&HostKernel, handle_of(child.id(), &pidfd_of(child.id()))),
Some('Z'),
"its pidfd says it exited"
);
child.wait().expect("reap");
}
#[skuld::test]
fn a_nonexistent_pid_has_no_state_under_the_ordinary_view() {
let pid = u32::MAX - 1;
assert_eq!(
proc_state(&HostKernel, handle_of(pid, &pidfd_of(std::process::id()))),
None
);
let Err(StateUnknown::Unreadable(e)) = read_proc_state(&HostKernel, pid) else {
panic!("expected Unreadable");
};
assert_eq!(e.kind(), std::io::ErrorKind::NotFound);
}
#[skuld::test]
fn an_unparsable_stat_is_named() {
assert!(matches!(
super::state_from_stat(b"garbage".to_vec()),
Err(StateUnknown::Unparsable(s)) if s == b"garbage"
));
}
#[skuld::test]
fn no_state_is_read_when_the_view_is_diverged_or_unassessable() {
let me = std::process::id();
let diverged = FakeKernel::over(&HostKernel).diverged_view();
match read_proc_state(&diverged, me).expect_err("no state") {
StateUnknown::View(v) => assert_eq!(v.reason, DIVERGED_REASON),
other => panic!("{other:?}"),
}
assert_eq!(proc_state(&diverged, handle_of(me, &pidfd_of(me))), None);
let unreadable = FakeKernel::over(&HostKernel).unreadable_view();
match read_proc_state(&unreadable, me).expect_err("no state") {
StateUnknown::View(v) => assert_eq!(v.reason, UNREADABLE_STATUS_REASON),
other => panic!("{other:?}"),
}
assert_eq!(proc_state(&unreadable, handle_of(me, &pidfd_of(me))), None);
}
#[skuld::test]
fn namespaces_an_outer_procfs_gives_no_state(#[fixture(namespaces)] _group: &Group) {
ns::run(fixture_path!(fixture_state_outer));
}
#[skuld::test]
fn fixture_state_outer() {
if !ns::is_child() {
return;
}
ns::enter_new_pid_ns_for_children();
ns::run(fixture_path!(fixture_state_inner));
}
#[skuld::test]
fn fixture_state_inner() {
if !ns::is_child_in_new_pid_ns() {
return;
}
let outer = std::fs::read("/proc/1/stat").expect("the outer /proc/1/stat is readable by path");
assert!(
crate::identity::stat_parse::parse_state(&outer).is_some(),
"control: read by path, pid 1 has a state: {outer:?}"
);
assert_eq!(proc_state(&HostKernel, handle_of(1, &pidfd_of(1))), None);
}
#[skuld::test]
fn namespaces_a_stat_mounted_over_gives_no_state(#[fixture(namespaces)] _group: &Group) {
ns::run(fixture_path!(fixture_state_overmount));
}
#[skuld::test]
fn fixture_state_overmount() {
if !ns::is_child() {
return;
}
ns::enter_private_mount_ns();
let mut child = crate::test_spawn::spawn(&mut member_command(0)).expect("spawn the member");
let _guard = KillOnDrop::of(&child);
crate::test_child::await_member_ready(&mut child);
let pid = child.id();
assert!(
proc_state(&HostKernel, handle_of(pid, &pidfd_of(pid))).is_some(),
"the member has a state before the mount"
);
let scratch = tempfile::tempdir().expect("tempdir");
let fake = scratch.path().join("stat");
let zeros = ["0"; 16].join(" ");
std::fs::write(&fake, format!("{pid} (fake) Z 1 1 {zeros} 1 0\n")).expect("write the fake stat");
ns::bind_over(&fake, std::path::Path::new(&format!("/proc/{pid}/stat")));
assert_eq!(proc_state(&HostKernel, handle_of(pid, &pidfd_of(pid))), None);
}
#[cfg(debug_assertions)]
#[skuld::test]
#[should_panic(expected = "POLLNVAL")]
fn a_closed_pidfd_breaks_the_polls_contract() {
use std::os::fd::{AsRawFd, BorrowedFd};
let file = std::fs::File::open("/dev/null").expect("open /dev/null");
let closed = file.as_raw_fd();
drop(file);
let pidfd = unsafe { BorrowedFd::borrow_raw(closed) };
let child = crate::containment::ChildHandle {
pid: std::process::id(),
pidfd,
};
let _ = proc_state(&HostKernel, child);
}
#[skuld::test]
fn a_pidfd_that_polls_in_error_gives_no_state() {
use std::os::fd::AsFd as _;
let (reader, writer) = std::io::pipe().expect("a pipe");
drop(reader);
let child = crate::containment::ChildHandle {
pid: std::process::id(),
pidfd: writer.as_fd(),
};
assert_eq!(proc_state(&HostKernel, child), None);
}
#[skuld::test]
fn a_thread_group_whose_leader_exited_has_no_state() {
use std::io::BufRead;
let mut cmd = crate::test_child::fixture_command(fixture_path!(fixture_leader_exits));
cmd.stdin(std::process::Stdio::piped());
let mut child = crate::test_spawn::spawn(&mut cmd).expect("spawn the fixture");
let pid = child.id();
let pidfd = pidfd_of(pid);
let mut stderr = std::io::BufReader::new(child.stderr.take().expect("piped stderr"));
let mut seen = String::new();
loop {
let mut line = String::new();
let read = stderr.read_line(&mut line).expect("read the fixture");
seen.push_str(&line);
assert_ne!(read, 0, "the fixture ended before its leader exited: {seen}");
if line.trim() == LEADER_EXITED {
break;
}
}
assert_eq!(
read_proc_state(&HostKernel, pid).ok(),
Some('Z'),
"control: /proc shows the leader's state"
);
assert_eq!(proc_state(&HostKernel, handle_of(pid, &pidfd)), None);
drop(child.stdin.take());
child.wait().expect("reap the fixture");
}
#[skuld::test]
fn a_thread_group_whose_leader_exited_is_alive_to_its_pidfs_identity(
#[fixture(pidfd_group_signal)] _group: &crate::test_groups::Group,
) {
use std::io::BufRead;
use crate::identity::{Existence, Liveness, ProcessId, Resolved};
let mut cmd = crate::test_child::fixture_command(fixture_path!(fixture_leader_exits));
cmd.stdin(std::process::Stdio::piped());
let mut child = crate::test_spawn::spawn(&mut cmd).expect("spawn the fixture");
let pid = child.id();
let mut stderr = std::io::BufReader::new(child.stderr.take().expect("piped stderr"));
let mut seen = String::new();
loop {
let mut line = String::new();
let read = stderr.read_line(&mut line).expect("read the fixture");
seen.push_str(&line);
assert_ne!(read, 0, "the fixture ended before its leader exited: {seen}");
if line.trim() == LEADER_EXITED {
break;
}
}
assert_eq!(
read_proc_state(&HostKernel, pid).ok(),
Some('Z'),
"control: /proc shows the leader's state"
);
let Resolved::Found(id) = ProcessId::of(pid) else {
panic!("a live group has an identity")
};
assert!(id.pidfs_ino().is_some(), "a pidfs host makes pidfs ids");
assert_eq!(id.exists(), Existence::Present);
assert_eq!(id.is_alive(), Liveness::Alive);
drop(child.stdin.take());
child.wait().expect("reap the fixture");
}
const LEADER_EXITED: &str = "COSCA_LEADER_EXITED";
extern "C" fn end_this_thread(_: libc::c_int) {
unsafe { libc::syscall(libc::SYS_exit, 0) };
}
#[skuld::test]
fn fixture_leader_exits() {
if !ns::is_child() {
return;
}
let pid = std::process::id();
unsafe {
libc::signal(
libc::SIGUSR2,
end_this_thread as extern "C" fn(libc::c_int) as libc::sighandler_t,
)
};
let sent = unsafe { libc::syscall(libc::SYS_tgkill, pid, pid, libc::SIGUSR2) };
assert_eq!(sent, 0, "tgkill: {}", std::io::Error::last_os_error());
while super::read_proc_state(&HostKernel, pid).ok() != Some('Z') {
std::thread::yield_now();
}
eprintln!("{LEADER_EXITED}");
let mut sink = Vec::new();
_ = std::io::Read::read_to_end(&mut std::io::stdin(), &mut sink);
unsafe { libc::_exit(0) };
}