#[cfg(any(target_os = "linux", target_os = "macos"))]
use crate::test_child::Console;
#[cfg(target_os = "macos")]
#[skuld::test]
fn kill_tree_reports_an_ordinary_group_refusal_through_the_real_dispatch_path() {
let mut child = crate::test_child::command(Console::Hidden)
.executable("/usr/bin/true")
.arg("true") .contain_with(crate::ContainMode::Strongest)
.spawn()
.expect("spawn a contained macOS root");
child.test_force_fdmarker_pgid(0);
let err = child
.kill_tree()
.expect_err("an unsignallable (pgid 0) group channel must report Err, not silently succeed");
assert!(
matches!(err, crate::error::Error::Unassessable { source: None, .. }),
"an invalid-pgid refusal is the ORDINARY, expected outcome unix::signal_group's own \
guard documents — got {err:?} instead"
);
drop(child);
}
#[cfg(target_os = "linux")]
fn child_whose_tree_kill_fails() -> (crate::Child, tempfile::TempDir) {
let dir = tempfile::tempdir().expect("tempdir");
let leaf_path = dir.path().join("cosca-drop-kill-fail-leaf");
std::fs::create_dir(&leaf_path).expect("create the leaf");
std::fs::write(leaf_path.join("occupant"), "").expect("keep the leaf unremovable");
std::fs::create_dir(leaf_path.join("cgroup.kill")).expect("make cgroup.kill a directory");
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(crate::Stdio::pipe()).expect("set stdin pipe");
let mut child = cmd.spawn().expect("spawn");
child.tree = crate::containment::Tree::contained(crate::containment::Mechanism::Cgroup(
crate::containment::cgroup::test_support::entered_leaf_at(leaf_path),
));
(child, dir)
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_failed_runs_teardown_returns_its_tree_kills_failure() {
crate::log_capture::install();
let (mut child, _leaf) = child_whose_tree_kill_fails();
let stdin = child.stdin().expect("piped stdin");
let blocker = crate::child::spawn::fault::release_between_kill_and_wait(stdin);
let torn = child.tear_down_or_hand_back(crate::child::owned::teardown::Disposition::HandBack);
blocker.assert_killed();
assert_eq!(torn.fate, crate::error::ChildFate::Reaped);
assert!(torn.unreaped.is_none());
let cleanup = torn.cleanup.expect("the tree kill's failure is the teardown's");
assert_eq!(cleanup.iter().len(), 1, "{cleanup:?}");
assert!(
matches!(&**cleanup.first(), crate::error::Error::Containment { .. })
&& crate::error::first_os_code(&**cleanup.first()) == Some(libc::EISDIR),
"{cleanup:?}"
);
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn drop_warns_instead_of_asserting_on_a_real_teardown_mechanism_failure() {
crate::log_capture::install();
let (mut child, _leaf) = child_whose_tree_kill_fails();
let stdin = child.stdin().expect("piped stdin");
let blocker = crate::child::spawn::fault::release_at_drop_kill(stdin);
let forced = child
.kill_tree()
.expect_err("the forced cgroup.kill failure must surface from kill_tree");
assert!(
matches!(&forced, crate::error::Error::Containment { .. })
&& crate::error::first_os_code(&forced) == Some(libc::EISDIR),
"the forced failure must be a mechanism-class Error::Containment over EISDIR, got {forced:?}"
);
let mark = crate::log_capture::mark();
let marker = "Child::drop: contained-tree teardown did not fully succeed";
std::thread::spawn(move || log::warn!("{marker}: from another thread"))
.join()
.expect("emit from another thread");
let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(child)));
assert!(
unwound.is_ok(),
"Child::drop must not panic on a real teardown-mechanism failure: {unwound:?}"
);
blocker.assert_killed();
let records = crate::log_capture::records_since_on_current_thread(mark, marker);
assert_eq!(
records.iter().map(|(level, _)| *level).collect::<Vec<_>>(),
[log::Level::Warn],
"a real teardown-mechanism failure during Drop must be logged at warn, got {records:?}"
);
}
#[skuld::test]
fn child_kill_error_keeps_the_raw_errno() {
use crate::os_process::child::PeekOutcome;
use crate::os_process::fake::FakeProcess;
#[cfg(unix)]
const REFUSED: i32 = libc::EPERM;
#[cfg(windows)]
const REFUSED: i32 = 5;
let fake = FakeProcess::new([])
.peeks([Ok(PeekOutcome::Running)])
.signals([Err(std::io::Error::from_raw_os_error(REFUSED))]);
let child = crate::test_child::over_fake::child_over(&fake);
let err = child.kill().expect_err("the scripted signal is refused");
assert!(
matches!(&err, crate::error::Error::Io(e) if e.raw_os_error() == Some(REFUSED)),
"{err:?}"
);
assert_eq!(fake.signal_calls(), [crate::signal::Sig::Kill]);
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn drop_closes_the_childs_untaken_stdout() {
use std::os::fd::AsRawFd as _;
if !crate::containment::cgroup::test_support::alone(crate::test_child::fixture_path!(
drop_closes_the_childs_untaken_stdout
)) {
return;
}
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(crate::Stdio::pipe_in()).expect("set stdin pipe");
cmd.stdout(crate::Stdio::pipe_out()).expect("set stdout pipe");
let mut child = cmd.spawn().expect("spawn");
let teardown = crate::child::spawn::fault::release_between_kill_and_wait(child.stdin().expect("piped stdin"));
let Some(crate::pipe::ParentEnd::Reader(stdout)) = child.pipes.get(&crate::stdio::Fd::STDOUT) else {
panic!("piped stdout");
};
let stdout = stdout.as_raw_fd();
assert_ne!(
unsafe { libc::fcntl(stdout, libc::F_GETFD) },
-1,
"open while the child lives"
);
drop(child);
let after = unsafe { libc::fcntl(stdout, libc::F_GETFD) };
assert_eq!(after, -1, "the drop must close the child's untaken stdout");
teardown.assert_killed();
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
fn a_tree_walk_kill_tree_sends_the_roots_kill_through_the_childs_handle() {
use crate::signal::{Sent, Sig};
let (stdin, _writer) = crate::test_child::held_writer_stdin();
let mut cmd = crate::test_child::command(crate::test_child::Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
cmd.contain_with(crate::ContainMode::TreeWalk);
let mut child = cmd.spawn().expect("spawn");
let fake = child.proc.script_over(|f| {
f.signals([Ok(Sent::Delivered), Ok(Sent::Delivered)])
.exits([crate::os_process::fake::Answer::Exited])
});
#[cfg(unix)]
let sends = crate::send_log::Capture::start();
child.kill_tree().expect("the handle took both kills");
assert_eq!(
fake.signal_calls(),
vec![Sig::Kill, Sig::Kill],
"the walk's, then the backstop's"
);
#[cfg(unix)]
{
assert_eq!(
sends.by_identity(),
vec![],
"the root was sent a signal by its identity"
);
assert!(
child.try_wait().expect("look at the live process").is_none(),
"the real root exited (a kill by its number or identity would do this)"
);
}
}
#[cfg(all(unix, not(target_os = "macos")))]
#[skuld::test]
fn a_tree_walk_terminate_tree_sends_the_roots_term_through_the_childs_handle() {
use crate::signal::{Sent, Sig};
let (stdin, _writer) = crate::test_child::held_writer_stdin();
let mut cmd = crate::test_child::command(crate::test_child::Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
cmd.contain_with(crate::ContainMode::TreeWalk);
let mut child = cmd.spawn().expect("spawn");
let fake = child.proc.script_over(|f| f.signals([Ok(Sent::Delivered)]));
let sends = crate::send_log::Capture::start();
child.terminate_tree().expect("the handle took the SIGTERM");
assert_eq!(fake.signal_calls(), vec![Sig::Graceful(crate::signal::Signal::Term)]);
assert_eq!(
sends.by_identity(),
vec![],
"the root was sent a signal by its identity"
);
assert!(
child.try_wait().expect("look at the live process").is_none(),
"the real root exited (a SIGTERM by its identity would do this)"
);
}