#![cfg(unix)]
use std::io::Read;
use std::os::unix::process::ExitStatusExt;
use std::time::Duration;
use crate::wait::fault;
fn spawn_term_ignoring_blocker() -> std::process::Child {
let mut child = crate::test_spawn::spawn(
std::process::Command::new("sh")
.args(["-c", "trap '' TERM; echo r; exec cat"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped()),
)
.expect("spawn");
let mut buf = [0u8; 1];
child
.stdout
.take()
.expect("piped stdout")
.read_exact(&mut buf)
.expect("readiness byte");
child
}
#[skuld::test]
fn foreign_graceful_lone_watch_error_still_escalates() {
let mut child = spawn_term_ignoring_blocker();
let p = crate::Process::from_pid(child.id()).found().expect("resolves");
fault::set_force_watch_error(true);
let err = p
.graceful_shutdown(Duration::from_secs(30))
.expect_err("the watch error must surface");
assert!(
!fault::armed(),
"seam not consumed — the watch did not run on this thread"
);
assert!(matches!(err, crate::error::Error::Io(_)), "got {err:?}");
let status = child.wait().expect("reap");
assert_eq!(
status.signal(),
Some(libc::SIGKILL),
"child must be force-killed despite the watch error, got {status:?}"
);
}
#[skuld::test]
fn foreign_graceful_tree_watch_error_still_sweeps() {
let mut child = spawn_term_ignoring_blocker();
let p = crate::Process::from_pid(child.id()).found().expect("resolves");
fault::set_force_watch_error(true);
let err = p
.graceful_shutdown_tree(Duration::from_secs(30))
.expect_err("the watch error must surface");
assert!(
!fault::armed(),
"seam not consumed — the watch did not run on this thread"
);
assert!(matches!(err, crate::error::Error::Io(_)), "got {err:?}");
let status = child.wait().expect("reap");
assert_eq!(
status.signal(),
Some(libc::SIGKILL),
"root must be swept despite the watch error, got {status:?}"
);
}
#[cfg(target_os = "linux")]
const ESCALATION_CASES: [([Option<rustix::io::Errno>; 3], &str, bool); 3] = [
([None, Some(rustix::io::Errno::PERM), None], "process wait", true),
([None, None, Some(rustix::io::Errno::ACCESS)], "process kill", false),
(
[None, Some(rustix::io::Errno::PERM), Some(rustix::io::Errno::NOSYS)],
"process kill",
false,
),
];
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_refused_pidfd_open_during_graceful_shutdown_names_the_step_that_hit_it() {
for (script, op, killed) in ESCALATION_CASES {
let mut child = spawn_term_ignoring_blocker();
let p = crate::Process::from_pid(child.id()).found().expect("resolves");
let forced = crate::wait::backend::fault::force_pidfd_open_script(script);
let result = p.graceful_shutdown(Duration::ZERO);
drop(forced);
match result {
Err(e @ crate::error::Error::Unsupported { .. }) => {
assert!(
e.to_string().starts_with(&format!("{op} is not supported")),
"{script:?}: {e}"
)
}
other => panic!("{script:?}: expected Unsupported naming {op}, got {other:?}"),
}
if !killed {
child
.kill()
.expect("the escalation was refused, so the child is still ours to kill");
}
assert_eq!(child.wait().expect("reap").signal(), Some(libc::SIGKILL), "{script:?}");
}
}