#[cfg(target_os = "linux")]
use super::fixtures::{try_wait, wait_status, Blocker};
#[cfg(target_os = "linux")]
use crate::child::owned::{ChildWaitOutcome, State};
#[cfg(target_os = "linux")]
use crate::signal::Sig;
#[cfg(target_os = "linux")]
use crate::test_child::reaped_elsewhere::kill_own_child;
#[cfg(target_os = "linux")]
use crate::test_groups::{tracer_group, Group};
#[cfg(target_os = "linux")]
use crate::test_support::own_tracer;
#[cfg(target_os = "linux")]
#[skuld::test]
fn try_wait_leaves_a_ptrace_stop_for_the_tracer(#[fixture(tracer_group)] _group: &Group) {
use std::os::unix::process::ExitStatusExt as _;
let b = Blocker::spawn();
let pid = b.pid as libc::pid_t;
own_tracer::seize_and_stop(pid);
assert!(
matches!(try_wait(&b.owned), Ok(ChildWaitOutcome::DeadlinePassed)),
"a stop is not an exit"
);
assert!(matches!(b.owned.lock().state, State::N));
let mut status = 0;
let reaped = unsafe { libc::waitpid(pid, &mut status, libc::WNOHANG | libc::__WALL) };
assert_eq!(reaped, pid);
assert!(
libc::WIFSTOPPED(status),
"try_wait consumed the tracer's stop: {status:#x}"
);
kill_own_child(b.pid);
assert_eq!(wait_status(&b.owned).signal(), Some(libc::SIGKILL));
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn kill_ends_a_child_stopped_for_its_tracer(#[fixture(tracer_group)] _group: &Group) {
use std::os::unix::process::ExitStatusExt as _;
let mut b = Blocker::spawn();
let pid = b.pid as libc::pid_t;
own_tracer::seize_and_stop(pid);
b.owned.signal(Sig::Kill).expect("kill");
own_tracer::resume_unless_dying(pid);
b.end_child();
assert_eq!(
wait_status(&b.owned).signal(),
Some(libc::SIGKILL),
"the handle's kill did not end the stopped tracee"
);
}
#[cfg(target_os = "macos")]
mod macos {
use std::time::Duration;
use super::super::fixtures::{try_wait, wait_status, Blocker};
use crate::child::owned::{ChildWaitOutcome, State};
use crate::identity::{uniq_fault, ReadPurpose, UniqRead};
use crate::os_process::child::ExitRecord;
use crate::signal::Sig;
use crate::test_child::{run_fixture_output_within, step, watchdog};
use crate::test_groups::{tracer_group, Group};
use crate::test_support::tracer::{attach_settled, debugger_signed_copy, settled_stop, AttachError};
const MARKER: &str = "COSCA_TESTCFG_SHARED_TRACER";
const DRIVER_BOUND: Duration = Duration::from_secs(12);
const BOUND: Duration = Duration::from_secs(8);
pub(super) fn run_signed(fixture: &str) {
let dir = tempfile::tempdir().expect("tempdir");
let exe = debugger_signed_copy(dir.path());
match run_fixture_output_within(fixture, MARKER, &exe, DRIVER_BOUND) {
Ok(output) => crate::test_child::assert_fixture_passed(fixture, &output),
Err(output) => panic!(
"fixture {fixture} was still running after {DRIVER_BOUND:?}, so it was killed:\n{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
),
}
}
fn attach_and_confirm_stop(blocker: &Blocker) {
step("attach and settle");
match attach_settled(blocker.pid) {
Ok(()) => {}
Err(AttachError::Errno(e)) => panic!("attach: {}", std::io::Error::from_raw_os_error(e)),
Err(AttachError::Exited { code, status }) => {
panic!("the tracee exited before it stopped (si_code {code}, si_status {status})")
}
}
}
fn kill_stopped(blocker: &Blocker) {
let pid = blocker.pid;
blocker.owned.signal(Sig::Kill).expect("kill");
let stopped = settled_stop(pid);
assert!(
matches!(stopped, Ok(Some(_))),
"the handle's SIGKILL ended or released a traced child: {stopped:?}"
);
let rc = unsafe { libc::ptrace(libc::PT_KILL, pid as libc::pid_t, std::ptr::null_mut(), 0) };
assert_eq!(rc, 0, "PT_KILL: {}", std::io::Error::last_os_error());
}
fn assert_killed(status: std::process::ExitStatus) {
assert_eq!(
std::os::unix::process::ExitStatusExt::signal(&status),
Some(libc::SIGKILL),
"{status:?}"
);
}
#[skuld::test]
fn try_wait_on_a_child_this_process_traces_returns_none_while_it_is_stopped(
#[fixture(tracer_group)] _group: &Group,
) {
if !crate::test_child::is_marked_fixture_reexec(MARKER) {
return run_signed(crate::test_child::fixture_path!(
try_wait_on_a_child_this_process_traces_returns_none_while_it_is_stopped,
Group
));
}
let _dog = watchdog("try_wait on a stopped tracee", BOUND);
step("spawn");
let b = Blocker::spawn();
attach_and_confirm_stop(&b);
step("try_wait");
assert!(
matches!(try_wait(&b.owned), Ok(ChildWaitOutcome::DeadlinePassed)),
"a stop is not an exit"
);
assert!(matches!(b.owned.lock().state, State::N));
kill_stopped(&b);
assert_killed(wait_status(&b.owned));
}
#[skuld::test]
fn a_child_this_process_traces_is_reaped_fully(#[fixture(tracer_group)] _group: &Group) {
if !crate::test_child::is_marked_fixture_reexec(MARKER) {
return run_signed(crate::test_child::fixture_path!(
a_child_this_process_traces_is_reaped_fully,
Group
));
}
let _dog = watchdog("reaped fully", BOUND);
step("spawn");
let b = Blocker::spawn();
attach_and_confirm_stop(&b);
kill_stopped(&b);
step("wait");
assert_killed(wait_status(&b.owned));
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
let r = unsafe {
libc::waitid(
libc::P_PID,
b.pid as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOHANG | libc::WNOWAIT,
)
};
assert_eq!(r, -1);
assert_eq!(std::io::Error::last_os_error().raw_os_error(), Some(libc::ECHILD));
}
#[skuld::test]
fn a_failed_start_read_in_the_second_reap_neither_panics_nor_leaves_n(#[fixture(tracer_group)] _group: &Group) {
if !crate::test_child::is_marked_fixture_reexec(MARKER) {
return run_signed(crate::test_child::fixture_path!(
a_failed_start_read_in_the_second_reap_neither_panics_nor_leaves_n,
Group
));
}
crate::log_capture::install();
let _dog = watchdog("failed start read", BOUND);
step("spawn");
let b = Blocker::spawn();
attach_and_confirm_stop(&b);
let marker = format!("second reap of pid {}", b.pid);
let mark = crate::log_capture::mark();
let _forced = uniq_fault::force_uniq_read_once(ReadPurpose::SecondPeek, UniqRead::Refused(libc::EPERM));
kill_stopped(&b);
assert_killed(wait_status(&b.owned));
assert!(
matches!(b.owned.lock().state, State::E(ExitRecord::Status(_))),
"the first reap's status is cached"
);
assert_eq!(crate::log_capture::levels_since(mark, &marker), vec![log::Level::Warn]);
}
}