use std::os::fd::{AsFd, BorrowedFd};
use std::process::Child;
use rustix::event::{PollFd, PollFlags};
use rustix::process::{pidfd_open, waitid, Pid, PidfdFlags, WaitId, WaitIdOptions};
use super::namespaces as ns;
use super::StdChild;
macro_rules! in_fresh_pid_ns {
($test:ident, $driver:ident, $init:ident, $body:path) => {
$crate::test_child::pid_reuse::in_fresh_pid_ns!(@define $test, $driver, $init, $body, [#[fixture(namespaces)] _group: &crate::test_groups::Group]);
};
($test:ident, $driver:ident, $init:ident, $body:path, cgroup) => {
$crate::test_child::pid_reuse::in_fresh_pid_ns!(@define $test, $driver, $init, $body, [
#[fixture(namespaces)] _group: &crate::test_groups::Group,
#[fixture(cgroup)] _cgroup: &crate::test_groups::Group
]);
};
($test:ident, $driver:ident, $init:ident, $body:path, pidfs) => {
$crate::test_child::pid_reuse::in_fresh_pid_ns!(@define $test, $driver, $init, $body, [
#[fixture(namespaces)] _group: &crate::test_groups::Group,
#[fixture(pidfd_group_signal)] _pidfs: &crate::test_groups::Group
]);
};
($test:ident, $driver:ident, $init:ident, $body:path, tracer) => {
$crate::test_child::pid_reuse::in_fresh_pid_ns!(@define $test, $driver, $init, $body, [
#[fixture(namespaces)] _group: &crate::test_groups::Group,
#[fixture(tracer_group)] _tracer: &crate::test_groups::Group
]);
};
(@define $test:ident, $driver:ident, $init:ident, $body:path, [$($params:tt)*]) => {
#[skuld::test]
fn $test($($params)*) {
crate::test_child::namespaces::run(crate::test_child::fixture_path!($driver));
}
#[skuld::test]
fn $driver() {
if !crate::test_child::namespaces::is_child() {
return;
}
crate::test_child::namespaces::enter_new_pid_ns_for_children();
crate::test_child::namespaces::run(crate::test_child::fixture_path!($init));
}
#[skuld::test]
fn $init() {
if !crate::test_child::namespaces::is_child_in_new_pid_ns() {
return;
}
crate::test_child::namespaces::enter_private_mount_ns();
crate::test_child::namespaces::mount_proc(std::path::Path::new("/proc"));
$body();
}
};
}
pub(crate) use in_fresh_pid_ns;
fn pid_of(pid: u32) -> Pid {
Pid::from_raw(pid as i32).expect("a child's pid is nonzero")
}
pub(crate) fn wait_pollin(pidfd: BorrowedFd<'_>) {
let mut fds = [PollFd::new(&pidfd, PollFlags::IN)];
crate::wait::poll_ready(&mut fds).expect("poll(pidfd)");
}
pub(crate) fn reap_behind_and_reuse(pid: u32) -> StdChild {
let pidfd = pidfd_open(pid_of(pid), PidfdFlags::empty()).expect("pidfd_open the child");
wait_pollin(pidfd.as_fd());
let reaped = waitid(WaitId::Pid(pid_of(pid)), WaitIdOptions::EXITED).expect("raw reap");
assert!(reaped.is_some(), "the raw reap must consume an exit record");
drop(pidfd);
reuse(pid)
}
pub(crate) fn reuse(pid: u32) -> StdChild {
ns::set_last_pid(pid - 1);
let reuser = super::blocker();
assert_eq!(
reuser.id(),
pid,
"precondition: the reuser must take the reaped child's pid"
);
reuser
}
pub(crate) fn reuser_id(
id: crate::identity::ProcessId,
same_tick: bool,
) -> crate::identity::Resolved<crate::identity::ProcessId> {
crate::identity::Resolved::Found(if same_tick { id } else { id.with_other_value() })
}
pub(crate) fn sigusr1_and_wait(mut reuser: StdChild) -> Option<i32> {
signal_usr1(&reuser);
std::os::unix::process::ExitStatusExt::signal(&reuser.wait().expect("wait for the reuser"))
}
pub(crate) fn signal_usr1(reuser: &Child) {
let rc = unsafe { libc::kill(reuser.id() as i32, libc::SIGUSR1) };
assert_eq!(rc, 0, "kill(SIGUSR1): {}", std::io::Error::last_os_error());
}