use std::os::fd::AsFd;
use rustix::process::{pidfd_open, Pid, PidfdFlags, WaitId, WaitIdOptions};
use crate::identity::fault::alias_token;
use crate::identity::StartToken;
use crate::send_log::{Capture, Via};
use crate::signal::Sig;
use crate::test_child::pid_reuse::{
in_fresh_pid_ns, reap_behind_and_reuse, sigusr1_and_peek, sigusr1_and_wait, wait_pollin,
};
use crate::test_groups::namespaces;
use crate::tokio::Command;
fn runtime() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("current_thread runtime")
}
fn spawn_blocker() -> (crate::tokio::Child, std::io::PipeWriter) {
spawn_blocker_with(true)
}
fn spawn_blocker_with(kill_on_drop: bool) -> (crate::tokio::Child, std::io::PipeWriter) {
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = Command::new();
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
cmd.kill_on_drop(kill_on_drop);
(cmd.spawn().expect("spawn"), writer)
}
#[skuld::test]
fn tokio_child_holds_its_handshake_pidfd() {
runtime().block_on(async {
let (mut child, writer) = spawn_blocker();
let pid = child.id().pid();
let crate::tokio::child::ProcSource::Tokio { pidfd, .. } = child.proc_mut() else {
panic!("a fresh child is a tokio backend");
};
let dir = crate::identity::ProcDir::open().expect("/proc opens");
let target = crate::identity::pidfd_pid_in_view(&dir, pidfd.as_fd());
assert!(
matches!(target, Ok(crate::identity::PidfdTarget::Pid(p)) if p == pid),
"the held pidfd must name the child: {target:?}"
);
drop(writer);
child.wait().await.expect("wait");
});
}
fn foreign_reaped_and_reused(
child: &crate::tokio::Child,
writer: std::io::PipeWriter,
) -> (std::process::Child, impl Sized) {
let token = StartToken::from_raw(child.id().start_token_raw());
drop(writer);
let reuser = reap_behind_and_reuse(child.id().pid());
let alias = alias_token(reuser.id(), token);
(reuser, alias)
}
fn kill_after_foreign_reap_and_reuse_body() {
runtime().block_on(async {
let (mut child, writer) = spawn_blocker();
let (reuser, _alias) = foreign_reaped_and_reused(&child, writer);
child.kill().expect("a kill of a foreign-reaped child answers Ok");
assert_eq!(
sigusr1_and_wait(reuser),
Some(libc::SIGUSR1),
"the reuser must have been signalled by the test alone"
);
});
}
in_fresh_pid_ns!(
namespaces_tokio_kill_after_foreign_reap_and_reuse_signals_nothing,
fixture_tokio_kill_reuse_driver,
fixture_tokio_kill_reuse_init,
kill_after_foreign_reap_and_reuse_body
);
fn drop_kill_after_foreign_reap_and_reuse_body() {
runtime().block_on(async {
let (child, writer) = spawn_blocker();
let (reuser, _alias) = foreign_reaped_and_reused(&child, writer);
let log = Capture::start();
drop(child);
assert_eq!(log.entries(), vec![], "the drop sees the reap and sends nothing");
assert_eq!(
sigusr1_and_wait(reuser),
Some(libc::SIGUSR1),
"the reuser must have been signalled by the test alone"
);
});
}
in_fresh_pid_ns!(
namespaces_tokio_drop_kill_after_foreign_reap_and_reuse_signals_nothing,
fixture_tokio_drop_kill_reuse_driver,
fixture_tokio_drop_kill_reuse_init,
drop_kill_after_foreign_reap_and_reuse_body
);
fn send_log_failed_attempt_body() {
runtime().block_on(async {
let (mut child, writer) = spawn_blocker();
let pid = child.id().pid();
drop(writer);
let pidfd = pidfd_open(Pid::from_raw(pid as i32).expect("pid"), PidfdFlags::empty()).expect("pidfd_open");
wait_pollin(pidfd.as_fd());
let reaped = rustix::process::waitid(
rustix::process::WaitId::Pid(Pid::from_raw(pid as i32).expect("pid")),
rustix::process::WaitIdOptions::EXITED,
)
.expect("raw reap");
assert!(reaped.is_some());
let log = Capture::start();
child.kill().expect("Ok");
assert_eq!(log.entries(), vec![(pid, Sig::Kill, Via::Pidfd)]);
});
}
in_fresh_pid_ns!(
namespaces_tokio_kill_records_a_gone_attempt_through_the_pidfd,
fixture_tokio_kill_gone_driver,
fixture_tokio_kill_gone_init,
send_log_failed_attempt_body
);
fn is_open(fd: std::os::fd::RawFd) -> bool {
unsafe { libc::fcntl(fd, libc::F_GETFD) != -1 }
}
fn drop_after_a_foreign_reap_closes_cosca_pidfd_body() {
runtime().block_on(async {
let (mut child, writer) = spawn_blocker();
let pid = child.id().pid();
let crate::tokio::child::ProcSource::Tokio { pidfd, .. } = child.proc_mut() else {
panic!("a fresh child is a tokio backend");
};
let cosca_pidfd = std::os::fd::AsRawFd::as_raw_fd(pidfd);
assert!(is_open(cosca_pidfd), "cosca holds its pidfd while the child lives");
drop(writer);
crate::test_child::wait_until_zombie(pid);
let mut status = 0;
let reaped = unsafe { libc::waitpid(pid as libc::pid_t, &mut status, 0) };
assert_eq!(reaped, pid as libc::pid_t, "{}", std::io::Error::last_os_error());
drop(child);
assert!(!is_open(cosca_pidfd), "the drop must close cosca's own pidfd");
});
}
in_fresh_pid_ns!(
namespaces_tokio_drop_after_a_foreign_reap_closes_cosca_pidfd,
fixture_tokio_drop_pidfd_driver,
fixture_tokio_drop_pidfd_init,
drop_after_a_foreign_reap_closes_cosca_pidfd_body
);
fn failed_write() -> Result<(), crate::error::Error> {
Err(crate::error::Error::Elevation {
kind: crate::error::ElevationErrorKind::AuthFailed,
detail: "forced password-write failure".into(),
})
}
fn elevation_detail(err: crate::error::Error) -> String {
match err {
crate::error::Error::Elevation { detail, .. } => detail,
other => panic!("expected an Elevation error, got {other:?}"),
}
}
#[skuld::test]
fn finish_elevated_after_a_foreign_reap_does_not_claim_a_termination() {
runtime().block_on(async {
let (child, writer) = spawn_blocker();
let pid = child.id().pid();
drop(writer);
crate::test_child::wait_until_zombie(pid);
let mut status = 0;
let reaped = unsafe { libc::waitpid(pid as libc::pid_t, &mut status, 0) };
assert_eq!(reaped, pid as libc::pid_t, "{}", std::io::Error::last_os_error());
let reaps = crate::child::spawn::fault::record_teardown_reaps();
let (err, fate) = crate::tokio::spawn::finish_elevated(child, failed_write())
.expect_err("the spawn fails")
.expect_may_have_started_with();
assert_eq!(fate, crate::error::ChildFate::Gone);
let detail = elevation_detail(err);
assert!(detail.contains("could not be terminated"), "{detail}");
assert!(!detail.contains("was terminated"), "{detail}");
assert_eq!(reaps.recorded(), vec![], "nothing was waited on");
});
}
fn finish_elevated_after_a_foreign_reap_and_reuse_waits_for_nothing_body() {
use std::cell::{Cell, RefCell};
use std::rc::Rc;
runtime().block_on(async {
let (child, writer) = spawn_blocker();
let (reuser, _alias) = foreign_reaped_and_reused(&child, writer);
let reuser = Rc::new(RefCell::new(Some(reuser)));
let died = Rc::new(Cell::new(false));
let _hook = crate::child::spawn::fault::set_between_kill_and_wait({
let (reuser, died) = (Rc::clone(&reuser), Rc::clone(&died));
move || {
let reuser = reuser.borrow();
assert_eq!(
sigusr1_and_peek(reuser.as_ref().expect("the reuser")),
Some(libc::SIGUSR1)
);
died.set(true);
}
});
let reaps = crate::child::spawn::fault::record_teardown_reaps();
let (err, fate) = crate::tokio::spawn::finish_elevated(child, failed_write())
.expect_err("the spawn fails")
.expect_may_have_started_with();
assert_eq!(fate, crate::error::ChildFate::Gone);
let detail = elevation_detail(err);
assert!(detail.contains("could not be terminated"), "{detail}");
assert_eq!(reaps.recorded(), vec![], "nothing was waited on");
let mut reuser = reuser.borrow_mut().take().expect("the reuser");
if !died.get() {
crate::test_child::pid_reuse::signal_usr1(&reuser);
}
let status = reuser
.wait()
.expect("the reuser's exit record must not have been taken by the teardown");
assert_eq!(
std::os::unix::process::ExitStatusExt::signal(&status),
Some(libc::SIGUSR1),
"the reuser must have been signalled by the test alone"
);
});
}
in_fresh_pid_ns!(
namespaces_tokio_finish_elevated_after_a_foreign_reap_and_reuse_waits_for_nothing,
fixture_tokio_finish_elevated_driver,
fixture_tokio_finish_elevated_init,
finish_elevated_after_a_foreign_reap_and_reuse_waits_for_nothing_body
);
fn finish_elevated_after_a_delivered_kill_and_a_foreign_reap_and_reuse_waits_for_nothing_body() {
use std::cell::RefCell;
use std::rc::Rc;
runtime().block_on(async {
let (child, writer) = spawn_blocker();
let pid = child.id().pid();
let stranger = Rc::new(RefCell::new(None));
let _hook = crate::child::spawn::fault::set_between_kill_and_wait({
let stranger = Rc::clone(&stranger);
move || {
drop(writer);
let reuser = reap_behind_and_reuse(pid);
assert_eq!(sigusr1_and_peek(&reuser), Some(libc::SIGUSR1));
*stranger.borrow_mut() = Some(reuser);
}
});
let reaps = crate::child::spawn::fault::record_teardown_reaps();
let (err, fate) = crate::tokio::spawn::finish_elevated(child, failed_write())
.expect_err("the spawn fails")
.expect_may_have_started_with();
assert_eq!(fate, crate::error::ChildFate::Killed);
let detail = elevation_detail(err);
assert!(detail.contains("was terminated"), "{detail}");
assert_eq!(
reaps.recorded(),
vec![],
"the stranger's exit was not recorded as a reap"
);
let mut stranger = stranger.borrow_mut().take().expect("the hook must have run");
let status = stranger
.wait()
.expect("the stranger's exit record must not have been taken by the teardown");
assert_eq!(
std::os::unix::process::ExitStatusExt::signal(&status),
Some(libc::SIGUSR1)
);
});
}
in_fresh_pid_ns!(
namespaces_tokio_finish_elevated_after_a_delivered_kill_and_a_foreign_reap_and_reuse_waits_for_nothing,
fixture_tokio_finish_elevated_delivered_driver,
fixture_tokio_finish_elevated_delivered_init,
finish_elevated_after_a_delivered_kill_and_a_foreign_reap_and_reuse_waits_for_nothing_body
);
fn drop_after_foreign_reap_and_reuse_reaps_nothing_body() {
runtime().block_on(async {
let (child, writer) = spawn_blocker();
let (reuser, _alias) = foreign_reaped_and_reused(&child, writer);
let reuser_pid = reuser.id();
assert_eq!(
sigusr1_and_peek(&reuser),
Some(libc::SIGUSR1),
"the reuser must have been signalled by the test alone"
);
drop(child);
let pid = Pid::from_raw(reuser_pid as i32).expect("pid");
let record = rustix::process::waitid(WaitId::Pid(pid), WaitIdOptions::EXITED)
.expect("the reuser's exit record must still be unconsumed")
.expect("an exit record");
assert_eq!(record.terminating_signal(), Some(libc::SIGUSR1));
drop(reuser);
});
}
in_fresh_pid_ns!(
namespaces_tokio_drop_after_foreign_reap_and_reuse_reaps_nothing,
fixture_tokio_drop_reaps_nothing_driver,
fixture_tokio_drop_reaps_nothing_init,
drop_after_foreign_reap_and_reuse_reaps_nothing_body
);
fn disarmed_drop_after_foreign_reap_and_reuse_reaps_nothing_body() {
runtime().block_on(async {
let (child, writer) = spawn_blocker_with(false);
let (reuser, _alias) = foreign_reaped_and_reused(&child, writer);
let reuser_pid = reuser.id();
assert_eq!(
sigusr1_and_peek(&reuser),
Some(libc::SIGUSR1),
"the reuser must have been signalled by the test alone"
);
drop(child);
let pid = Pid::from_raw(reuser_pid as i32).expect("pid");
let record = rustix::process::waitid(WaitId::Pid(pid), WaitIdOptions::EXITED)
.expect("the reuser's exit record must still be unconsumed")
.expect("an exit record");
assert_eq!(record.terminating_signal(), Some(libc::SIGUSR1));
drop(reuser);
});
}
in_fresh_pid_ns!(
namespaces_tokio_disarmed_drop_after_foreign_reap_and_reuse_reaps_nothing,
fixture_tokio_disarmed_drop_driver,
fixture_tokio_disarmed_drop_init,
disarmed_drop_after_foreign_reap_and_reuse_reaps_nothing_body
);
fn first_look_sees_it_running() -> impl Sized {
use crate::wait::exit_only::seams::force_peek_once;
use crate::wait::exit_only::Peek;
force_peek_once(Ok(Peek::Running))
}
fn armed_drop_whose_first_look_missed_the_reap_body() {
runtime().block_on(async {
let (child, writer) = spawn_blocker();
let pid = child.id().pid();
let (reuser, _alias) = foreign_reaped_and_reused(&child, writer);
let root = super::drop_fault::record();
let log = Capture::start();
let _look = first_look_sees_it_running();
drop(child);
assert_eq!(root.kills(), 1, "the armed drop reached its root kill");
assert_eq!(root.forgets(), 1, "the second look forgot tokio's Child");
assert_eq!(log.entries(), vec![(pid, Sig::Kill, Via::Pidfd)]);
assert_eq!(
sigusr1_and_wait(reuser),
Some(libc::SIGUSR1),
"the reuser must have been signalled by the test alone"
);
});
}
in_fresh_pid_ns!(
namespaces_tokio_armed_drop_whose_first_look_missed_the_reap_leaves_the_stranger_alone,
fixture_tokio_first_look_armed_live_driver,
fixture_tokio_first_look_armed_live_init,
armed_drop_whose_first_look_missed_the_reap_body
);
fn drop_whose_first_look_missed_the_reap_reaps_nothing(kill_on_drop: bool) {
runtime().block_on(async {
let (child, writer) = spawn_blocker_with(kill_on_drop);
let (reuser, _alias) = foreign_reaped_and_reused(&child, writer);
let reuser_pid = reuser.id();
assert_eq!(sigusr1_and_peek(&reuser), Some(libc::SIGUSR1));
let _look = first_look_sees_it_running();
drop(child);
let pid = Pid::from_raw(reuser_pid as i32).expect("pid");
let record = rustix::process::waitid(WaitId::Pid(pid), WaitIdOptions::EXITED)
.expect("the reuser's exit record must still be unconsumed")
.expect("an exit record");
assert_eq!(record.terminating_signal(), Some(libc::SIGUSR1));
drop(reuser);
});
}
fn armed_zombie_drop_whose_first_look_missed_the_reap_body() {
drop_whose_first_look_missed_the_reap_reaps_nothing(true);
}
fn disarmed_zombie_drop_whose_first_look_missed_the_reap_body() {
drop_whose_first_look_missed_the_reap_reaps_nothing(false);
}
in_fresh_pid_ns!(
namespaces_tokio_armed_zombie_drop_whose_first_look_missed_the_reap_reaps_nothing,
fixture_tokio_first_look_armed_zombie_driver,
fixture_tokio_first_look_armed_zombie_init,
armed_zombie_drop_whose_first_look_missed_the_reap_body
);
in_fresh_pid_ns!(
namespaces_tokio_disarmed_zombie_drop_whose_first_look_missed_the_reap_reaps_nothing,
fixture_tokio_first_look_disarmed_zombie_driver,
fixture_tokio_first_look_disarmed_zombie_init,
disarmed_zombie_drop_whose_first_look_missed_the_reap_body
);
fn finish_elevated_of_an_aliased_process_group_sends_no_killpg_body() {
runtime().block_on(async {
let recorder = crate::containment::unix::fault::record_kill_group();
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = Command::new();
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
cmd.contain_with(crate::ContainMode::Session);
let child = cmd.spawn().expect("spawn");
assert!(
child.carries_recyclable_pgid(),
"the test needs a number-named group kill"
);
let (_reuser, _alias) = foreign_reaped_and_reused(&child, writer);
let (err, fate) = crate::tokio::spawn::finish_elevated(child, failed_write())
.expect_err("the spawn fails")
.expect_may_have_started_with();
assert_eq!(fate, crate::error::ChildFate::Gone);
let detail = elevation_detail(err);
assert_eq!(
recorder.killed(),
Vec::<i32>::new(),
"no killpg by a reaped root's number ({detail})"
);
});
}
in_fresh_pid_ns!(
namespaces_tokio_finish_elevated_of_an_aliased_process_group_sends_no_killpg,
fixture_tokio_first_look_process_group_driver,
fixture_tokio_first_look_process_group_init,
finish_elevated_of_an_aliased_process_group_sends_no_killpg_body
);
#[skuld::test]
fn a_failed_pidfd_peek_is_unknown_so_the_child_is_forgotten() {
use crate::wait::exit_only::seams::force_peeks;
runtime().block_on(async {
let (mut child, _writer) = spawn_blocker();
let _failed = force_peeks((0..2).map(|_| Err(std::io::Error::other("forced peek failure"))));
assert!(
matches!(child.proc_mut().state(), crate::signal::RootState::Unknown(_)),
"a child nothing can answer for is not shown to be ours"
);
let forgot = child.proc_mut().forget_if_foreign();
assert!(
forgot.is_some(),
"a child nothing can answer for is not tokio's to reap by pid"
);
assert!(
child.proc_mut().is_reaped(),
"the forgotten child is not tokio's any more"
);
});
}
fn after_foreign_reap_and_reuse(use_wait: bool) {
runtime().block_on(async {
let (mut child, writer) = spawn_blocker();
let (mut reuser, _alias) = foreign_reaped_and_reused(&child, writer);
assert_eq!(
sigusr1_and_peek(&reuser),
Some(libc::SIGUSR1),
"the stranger is a zombie"
);
let answer = if use_wait {
child.wait().await.map(Some)
} else {
child.try_wait()
};
let err = answer.expect_err("a foreign-reaped child has no status to give");
assert!(
matches!(&err, crate::error::Error::Io(e) if e.raw_os_error() == Some(libc::ECHILD)),
"{err:?}"
);
let status = reuser.wait().expect("the stranger's record must not have been taken");
assert_eq!(
std::os::unix::process::ExitStatusExt::signal(&status),
Some(libc::SIGUSR1)
);
});
}
fn try_wait_after_foreign_reap_and_reuse_body() {
after_foreign_reap_and_reuse(false);
}
fn wait_after_foreign_reap_and_reuse_body() {
after_foreign_reap_and_reuse(true);
}
in_fresh_pid_ns!(
namespaces_tokio_try_wait_after_foreign_reap_and_reuse_takes_nothing,
fixture_tokio_try_wait_reuse_driver,
fixture_tokio_try_wait_reuse_init,
try_wait_after_foreign_reap_and_reuse_body
);
in_fresh_pid_ns!(
namespaces_tokio_wait_after_foreign_reap_and_reuse_takes_nothing,
fixture_tokio_wait_reuse_driver,
fixture_tokio_wait_reuse_init,
wait_after_foreign_reap_and_reuse_body
);
fn wait_parked_then_foreign_reap_and_reuse_body() {
runtime().block_on(async {
let (mut child, writer) = spawn_blocker();
let id = child.id();
let mut waiting = std::pin::pin!(child.wait());
let parked = std::future::poll_fn(|cx| {
std::task::Poll::Ready(std::future::Future::poll(waiting.as_mut(), cx).is_pending())
})
.await;
assert!(parked, "wait must be parked on a live child");
let token = StartToken::from_raw(id.start_token_raw());
drop(writer);
let mut reuser = reap_behind_and_reuse(id.pid());
let _alias = alias_token(reuser.id(), token);
assert_eq!(
sigusr1_and_peek(&reuser),
Some(libc::SIGUSR1),
"the stranger is a zombie"
);
let answer = waiting.await;
let status = reuser.wait();
assert!(
matches!(&answer, Err(crate::error::Error::Io(e)) if e.raw_os_error() == Some(libc::ECHILD)),
"wait answered {answer:?}; the stranger's own wait: {status:?}"
);
let status = status.expect("the stranger's record must not have been taken");
assert_eq!(
std::os::unix::process::ExitStatusExt::signal(&status),
Some(libc::SIGUSR1)
);
});
}
in_fresh_pid_ns!(
namespaces_tokio_wait_parked_then_foreign_reap_and_reuse_takes_nothing,
fixture_tokio_wait_parked_driver,
fixture_tokio_wait_parked_init,
wait_parked_then_foreign_reap_and_reuse_body
);
fn spawn_failure_teardown_leaves_the_stranger_alone(attach_arm: bool) {
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use crate::child::spawn::fault;
use crate::identity::{ProcessId, Resolved};
runtime().block_on(async {
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = Command::new();
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
let stranger = Rc::new(RefCell::new(None));
let alias = Rc::new(RefCell::new(None));
let died = Rc::new(Cell::new(false));
let point = if attach_arm {
fault::SpawnPoint::BeforeAttach
} else {
fault::SpawnPoint::BeforeIdentity
};
let _spawn_hook = fault::set_at(point, {
let (stranger, alias) = (Rc::clone(&stranger), Rc::clone(&alias));
move || {
let pid = fault::spawn_pid();
let Resolved::Found(id) = ProcessId::of(pid) else {
panic!("the child must be readable before it is reaped")
};
let token = StartToken::from_raw(id.start_token_raw());
drop(writer);
let reuser = reap_behind_and_reuse(pid);
*alias.borrow_mut() = Some(alias_token(reuser.id(), token));
*stranger.borrow_mut() = Some(reuser);
}
});
let _wait_hook = fault::set_between_kill_and_wait({
let (stranger, died) = (Rc::clone(&stranger), Rc::clone(&died));
move || {
let stranger = stranger.borrow();
assert_eq!(
sigusr1_and_peek(stranger.as_ref().expect("the stranger")),
Some(libc::SIGUSR1),
"the stranger must have been signalled by the test alone"
);
died.set(true);
}
});
if attach_arm {
fault::set_force_attach_failure(true);
} else {
fault::set_force_identity_vanished(true);
}
let err = cmd.spawn().err();
fault::set_force_attach_failure(false);
fault::set_force_identity_vanished(false);
let (err, fate) = crate::child::spawn::failure::expect_may_have_started_with(
err.expect("the forced failure must fail the spawn"),
);
assert_eq!(fate, crate::error::ChildFate::Gone, "the child was reaped elsewhere");
assert_eq!(
matches!(err, crate::error::Error::Containment { .. }),
attach_arm,
"the spawn must fail in the arm under test: {err:?}"
);
let mut stranger = stranger.borrow_mut().take().expect("the hook must have run");
if !died.get() {
crate::test_child::pid_reuse::signal_usr1(&stranger);
}
let status = stranger
.wait()
.expect("the stranger's exit record must not have been taken by the teardown");
assert_eq!(
std::os::unix::process::ExitStatusExt::signal(&status),
Some(libc::SIGUSR1),
"the stranger must have been signalled by the test alone"
);
drop(alias);
});
}
fn spawn_identity_failure_teardown_body() {
spawn_failure_teardown_leaves_the_stranger_alone(false);
}
fn spawn_attach_failure_teardown_body() {
spawn_failure_teardown_leaves_the_stranger_alone(true);
}
in_fresh_pid_ns!(
namespaces_tokio_spawn_identity_failure_teardown_leaves_the_stranger_alone,
fixture_tokio_spawn_identity_driver,
fixture_tokio_spawn_identity_init,
spawn_identity_failure_teardown_body
);
in_fresh_pid_ns!(
namespaces_tokio_spawn_attach_failure_teardown_leaves_the_stranger_alone,
fixture_tokio_spawn_attach_driver,
fixture_tokio_spawn_attach_init,
spawn_attach_failure_teardown_body
);
fn spawn_identity_gone_after_a_reap_at(point: crate::child::spawn::fault::SpawnPoint) {
use std::cell::RefCell;
use std::rc::Rc;
use crate::child::spawn::fault;
use crate::identity::{ProcessId, Resolved};
runtime().block_on(async {
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = Command::new();
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
let stranger = Rc::new(RefCell::new(None));
let alias: Rc<RefCell<Option<Box<dyn std::any::Any>>>> = Rc::default();
let _hook = fault::set_at(point, {
let (stranger, alias) = (Rc::clone(&stranger), Rc::clone(&alias));
move || {
let pid = fault::spawn_pid();
let Resolved::Found(id) = ProcessId::of(pid) else {
panic!("the child must be readable before it is reaped")
};
let token = StartToken::from_raw(id.start_token_raw());
drop(writer);
let reuser = reap_behind_and_reuse(pid);
*alias.borrow_mut() = Some(Box::new(alias_token(reuser.id(), token)));
*stranger.borrow_mut() = Some(reuser);
}
});
let (err, fate) = match cmd.spawn() {
Ok(child) => panic!("the spawn took the stranger for its child: {:?}", child.id()),
Err(e) => crate::child::spawn::failure::expect_may_have_started_with(e),
};
assert_eq!(fate, crate::error::ChildFate::Gone, "the child was reaped elsewhere");
let stranger = stranger.borrow_mut().take().expect("the hook must have run");
assert!(
matches!(&err, crate::error::Error::Io(e) if e.to_string().contains("reaped by another party")),
"a child reaped before its identity was read is Gone, not Unassessable: {err:?}"
);
assert_eq!(
sigusr1_and_wait(stranger),
Some(libc::SIGUSR1),
"the stranger must have been signalled by the test alone"
);
drop(alias);
});
}
fn spawn_identity_after_foreign_reap_and_reuse_is_gone_body() {
spawn_identity_gone_after_a_reap_at(crate::child::spawn::fault::SpawnPoint::BeforeIdentity);
}
in_fresh_pid_ns!(
namespaces_tokio_spawn_identity_after_foreign_reap_and_reuse_is_gone,
fixture_tokio_spawn_identity_gone_driver,
fixture_tokio_spawn_identity_gone_init,
spawn_identity_after_foreign_reap_and_reuse_is_gone_body
);
fn treewalk_root_is_the_verified_identity_body() {
use std::cell::RefCell;
use std::rc::Rc;
use crate::child::spawn::fault;
use crate::identity::fault::alias_token;
use crate::identity::{ProcessId, Resolved, StartToken};
runtime().block_on(async {
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = Command::new();
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
cmd.contain_with(crate::ContainMode::TreeWalk);
let stranger = Rc::new(RefCell::new(None));
let alias: Rc<RefCell<Option<Box<dyn std::any::Any>>>> = Rc::default();
let _hook = fault::set_at(fault::SpawnPoint::BeforeAttach, {
let (stranger, alias) = (Rc::clone(&stranger), Rc::clone(&alias));
move || {
let pid = fault::spawn_pid();
let Resolved::Found(id) = ProcessId::of(pid) else {
panic!("the child must be readable before it is reaped")
};
drop(writer);
let reuser = reap_behind_and_reuse(pid);
let token = StartToken::from_raw(id.start_token_raw() + 1);
*alias.borrow_mut() = Some(Box::new(alias_token(reuser.id(), token)));
*stranger.borrow_mut() = Some(reuser);
}
});
let mut child = match cmd.spawn() {
Ok(child) => child,
Err(e) => panic!("a reap after the verified identity read must not fail the spawn: {e:?}"),
};
let stranger = stranger.borrow_mut().take().expect("the hook must have run");
assert_eq!(
stranger.id(),
child.id().pid(),
"the stranger must hold the child's pid"
);
assert_eq!(
child.test_treewalk_root(),
Some(child.id()),
"the walk's root must be the verified identity, not whoever holds the pid now"
);
child.kill_tree().expect("a tree whose root was reaped answers Ok");
assert_eq!(
sigusr1_and_wait(stranger),
Some(libc::SIGUSR1),
"the stranger must have been signalled by the test alone"
);
});
}
in_fresh_pid_ns!(
namespaces_tokio_treewalk_root_is_the_verified_identity,
fixture_tokio_treewalk_root_driver,
fixture_tokio_treewalk_root_init,
treewalk_root_is_the_verified_identity_body
);
fn a_panicking_logger_in_reap_nows_via_pidfd_log_body() {
use std::cell::RefCell;
use std::rc::Rc;
use crate::child::spawn::fault;
crate::log_capture::install();
runtime().block_on(async {
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = Command::new();
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
let stranger = Rc::new(RefCell::new(None));
let _spawn_hook = fault::set_at(fault::SpawnPoint::BeforeIdentity, {
let stranger = Rc::clone(&stranger);
move || {
let pid = fault::spawn_pid();
drop(writer);
let reuser = reap_behind_and_reuse(pid);
assert_eq!(sigusr1_and_peek(&reuser), Some(libc::SIGUSR1));
*stranger.borrow_mut() = Some(reuser);
}
});
fault::set_force_identity_vanished(true);
let unwound = {
let _panics = crate::log_capture::panic_on("it is already gone");
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| cmd.spawn().map(drop)))
};
fault::set_force_identity_vanished(false);
assert!(unwound.is_err(), "the logger must have panicked out of the spawn");
let mut stranger = stranger.borrow_mut().take().expect("the hook must have run");
let status = stranger
.wait()
.expect("the stranger's exit record must not have been taken by the teardown");
assert_eq!(
std::os::unix::process::ExitStatusExt::signal(&status),
Some(libc::SIGUSR1)
);
});
}
in_fresh_pid_ns!(
namespaces_a_panicking_logger_in_reap_nows_via_pidfd_log_leaves_the_stranger_alone,
fixture_panicking_logger_reap_now_driver,
fixture_panicking_logger_reap_now_init,
a_panicking_logger_in_reap_nows_via_pidfd_log_body
);
fn a_panicking_logger_in_drops_via_pidfd_log_body() {
use crate::wait::exit_only::seams::force_peek_once;
use crate::wait::exit_only::Peek;
crate::log_capture::install();
runtime().block_on(async {
let (child, writer) = spawn_blocker();
let (reuser, _alias) = foreign_reaped_and_reused(&child, writer);
let reuser_pid = reuser.id();
assert_eq!(sigusr1_and_peek(&reuser), Some(libc::SIGUSR1));
let _first_look = force_peek_once(Ok(Peek::Running));
let unwound = {
let _panics = crate::log_capture::panic_on("it is already gone");
std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || drop(child)))
};
assert!(unwound.is_err(), "the logger must have panicked out of the drop");
let pid = Pid::from_raw(reuser_pid as i32).expect("pid");
let record = rustix::process::waitid(WaitId::Pid(pid), WaitIdOptions::EXITED)
.expect("the reuser's exit record must still be unconsumed")
.expect("an exit record");
assert_eq!(record.terminating_signal(), Some(libc::SIGUSR1));
drop(reuser);
});
}
in_fresh_pid_ns!(
namespaces_a_panicking_logger_in_drops_via_pidfd_log_leaves_the_stranger_alone,
fixture_panicking_logger_drop_driver,
fixture_panicking_logger_drop_init,
a_panicking_logger_in_drops_via_pidfd_log_body
);