use std::os::unix::process::ExitStatusExt as _;
use super::sys::{proc_state, ProcState};
use super::{sys, Cause, Mode, Report, Tracee, TracerHelper, Until};
use crate::identity::kinfo::P_WEXIT;
use crate::test_groups::{tracer_group, Group};
fn tracee() -> (crate::Child, std::io::PipeWriter) {
tracee_with(Tracee::Plain)
}
fn tracee_with(kind: Tracee) -> (crate::Child, std::io::PipeWriter) {
tracee_receiving(kind, &[])
}
fn tracee_receiving(kind: Tracee, signals: &[i32]) -> (crate::Child, std::io::PipeWriter) {
let (mut child, ready) = super::spawn_tracee(kind);
for &signal in signals {
let caught = kind == Tracee::CatchSigterm && signal == libc::SIGTERM;
assert_receives(child.id().pid(), &ready, signal, caught);
}
let stdin = child.stdin().expect("the tracee's stdin is piped");
(child, stdin)
}
fn bit(signal: i32) -> u32 {
debug_assert!((1..32).contains(&signal), "signal {signal} is outside the mask");
1 << (signal - 1)
}
fn assert_receives(pid: u32, ready: &super::Ready, signal: i32, caught: bool) {
assert_eq!(
ready.blocked & bit(signal),
0,
"precondition: the tracee blocks signal {signal}"
);
assert_eq!(
ready.ignored & bit(signal),
0,
"precondition: the tracee ignores signal {signal}"
);
assert_eq!(
ready.caught & bit(signal) != 0,
caught,
"precondition: whether the tracee catches signal {signal}"
);
if matches!(signal, libc::SIGTSTP | libc::SIGTTIN | libc::SIGTTOU) {
assert_job_control_reaches(pid, ready, signal);
}
}
fn assert_job_control_reaches(pid: u32, ready: &super::Ready, signal: i32) {
assert_eq!(
ready.pgrp, pid as libc::pid_t,
"precondition: the tracee does not lead its own process group. Spawn it as Tracee::OwnGroup"
);
let jobc = match crate::identity::kinfo::kinfo(pid as _) {
crate::identity::Resolved::Found(info) => info.e_jobc(),
crate::identity::Resolved::Gone => panic!("precondition: the tracee {pid} is gone"),
crate::identity::Resolved::Unknown => panic!("precondition: kinfo refused to read the tracee {pid}"),
};
assert_ne!(
jobc, 0,
"precondition: the tracee's process group is orphaned (pg_jobc {jobc}), so XNU discards \
signal {signal} sent to it while traced. Spawn it as Tracee::OwnGroup"
);
}
fn label(report: Report) -> String {
match report {
Report::Attached => "attached".to_string(),
Report::Exited => "exited".to_string(),
Report::Reaped => "reaped".to_string(),
Report::Detached => "detached".to_string(),
Report::Error {
cause: Cause::Errno(errno),
state,
} => format!("error {errno} {state}"),
Report::Error {
cause: Cause::Event(event),
state,
} => format!("error {event} {state}"),
Report::State(state) => state,
Report::Blocking { state, until } => match until {
Until::Eof => format!("blocking {state} eof"),
Until::Exit => format!("blocking {state} exit"),
},
}
}
fn expect(th: &mut TracerHelper<'_>, pattern: &[&str]) {
debug_assert!(
pattern.last().is_some_and(|last| !last.ends_with('*')),
"a pattern ends with a report: {pattern:?}"
);
let mut seen = Vec::new();
let mut at = 0;
while at < pattern.len() {
let report = th
.session
.next_report()
.unwrap_or_else(|| panic!("the helper exited after {seen:?}; expected {pattern:?}"));
let report = label(report);
loop {
match pattern[at].strip_suffix('*') {
Some(repeated) if repeated == report => break,
Some(_) => at += 1,
None => {
assert_eq!(report, pattern[at], "after {seen:?}, expecting {pattern:?}");
at += 1;
break;
}
}
}
seen.push(report);
}
}
fn err(cause: impl std::fmt::Display, state: &str) -> String {
format!("error {cause} {state}")
}
const HELD: [&str; 5] = ["S0", "S1", "S1h", "S1hs", "blocking S1h eof"];
const RELEASED: [&str; 4] = ["S2", "attached", "S3", "blocking S3 eof"];
const TO_S3: [&str; 7] = ["S0", "S1", "S2", "S2b*", "attached", "S3", "blocking S3 eof"];
const DETACHED: [&str; 4] = ["S4", "S4b*", "detached", DONE];
const REAPED: [&str; 4] = ["S5", "blocking S5 exit", "reaped", DONE];
const DONE: &str = "blocking done eof";
fn send(pid: u32, signal: i32) {
assert_eq!(sys::kill(pid, signal), Ok(()), "kill({pid}, {signal})");
}
thread_local! {
static FORCED_STOP: std::cell::Cell<Option<i32>> = const { std::cell::Cell::new(None) };
}
struct ForcedStop;
impl ForcedStop {
fn arm(signal: i32) -> ForcedStop {
FORCED_STOP.set(Some(signal));
ForcedStop
}
}
impl Drop for ForcedStop {
fn drop(&mut self) {
FORCED_STOP.set(None);
}
}
fn await_change(pid: u32) -> libc::siginfo_t {
if let Some(signal) = FORCED_STOP.take() {
return record(libc::CLD_STOPPED, signal);
}
sys::peek(pid, libc::WEXITED | libc::WSTOPPED).expect("waitid the tracee")
}
fn record(code: i32, status: i32) -> libc::siginfo_t {
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
info.si_signo = libc::SIGCHLD;
info.si_code = code;
info.si_status = status;
info
}
fn assert_exited_cleanly(tracee: crate::Child) {
let status = tracee.wait().expect("wait for the tracee");
assert!(status.success(), "expected the tracee's own clean exit, got {status:?}");
}
fn stopped_by(info: &libc::siginfo_t, state: ProcState) -> Option<i32> {
debug_assert!(
state.p_stat != libc::SZOMB || state.p_flag & P_WEXIT != 0,
"a zombie without P_WEXIT: XNU sets P_LEXIT before SZOMB ({state:?})"
);
(info.si_code == libc::CLD_STOPPED && state.p_flag & P_WEXIT == 0).then_some(info.si_status)
}
fn assert_sigkilled(tracee: crate::Child) {
assert_eq!(
super::unreaped_helpers(),
0,
"assert_sigkilled before the tracer helper was reaped"
);
let pid = tracee.id().pid();
let info = await_change(pid);
if let Some(signal) = stopped_by(&info, proc_state(pid)) {
tracee.kill().expect("kill the stopped tracee");
panic!("expected SIGKILL, but the tracee is stopped by {signal}");
}
let status = tracee.wait().expect("wait for the tracee");
assert_eq!(status.signal(), Some(libc::SIGKILL), "expected SIGKILL, got {status:?}");
}
fn assert_terminated(tracee: crate::Child) {
let status = tracee.wait().expect("wait for the tracee");
assert_eq!(status.signal(), Some(libc::SIGTERM), "expected SIGTERM, got {status:?}");
}
fn end_detached(tracee: crate::Child, stdin: std::io::PipeWriter) {
tracee.kill().expect("kill the detached tracee");
drop(stdin);
assert_sigkilled(tracee);
}
fn end_stopped(tracee: crate::Child) {
tracee.kill().expect("kill the stopped tracee");
assert_sigkilled(tracee);
}
fn peek_exit(pid: u32) -> Result<libc::pid_t, i32> {
sys::peek(pid, libc::WEXITED | libc::WNOHANG).map(|info| info.si_pid)
}
fn assert_handed_back(pid: u32) {
assert_eq!(
peek_exit(pid),
Ok(pid as libc::pid_t),
"the reaped tracee's zombie is not this test's"
);
}
fn assert_not_handed_back(pid: u32) {
assert_eq!(peek_exit(pid), Err(libc::ECHILD), "the tracee came back without a reap");
}
fn assert_is_our_child(pid: u32) {
assert_eq!(
sys::peek_child(pid),
Ok(()),
"the detached tracee is not this test's child"
);
}
fn panic_of(f: impl FnOnce()) -> String {
let payload = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).expect_err("no panic");
match payload.downcast::<String>() {
Ok(message) => *message,
Err(payload) => payload
.downcast::<&str>()
.map(|message| message.to_string())
.expect("a string panic payload"),
}
}
#[skuld::test]
fn feasibility_facts(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S1:hold").attach(&mut tracee);
expect(&mut th, &HELD);
assert_eq!(sys::pbi_status(pid), Ok(libc::SSTOP), "the held tracee's status");
assert_eq!(
sys::peek_child(pid),
Err(libc::ECHILD),
"the traced tracee's parent peek"
);
th.signal();
expect(&mut th, &RELEASED);
th.signal();
expect(&mut th, &DETACHED);
drop(th);
send(pid, libc::SIGSTOP);
drop(stdin);
let info = await_change(pid);
assert_eq!(
(info.si_code, info.si_status),
(libc::CLD_STOPPED, libc::SIGSTOP),
"the detached tracee after SIGSTOP"
);
end_stopped(tracee);
}
fn check_receives(kind: Tracee, signal: i32, caught: bool) -> Result<(), String> {
let (mut tracee, ready) = super::spawn_tracee(kind);
let stdin = tracee.stdin().expect("the tracee's stdin is piped");
let pid = tracee.id().pid();
let checked = std::panic::catch_unwind(|| assert_receives(pid, &ready, signal, caught));
drop(stdin);
assert_exited_cleanly(tracee);
checked.map_err(|payload| match payload.downcast::<String>() {
Ok(message) => *message,
Err(payload) => payload
.downcast::<&str>()
.map(|message| message.to_string())
.expect("a string panic payload"),
})
}
#[skuld::test]
fn the_tracees_meet_the_preconditions(#[fixture(tracer_group)] _group: &Group) {
for signal in [libc::SIGTSTP, libc::SIGTTIN, libc::SIGCONT] {
assert_eq!(
check_receives(Tracee::OwnGroup, signal, false),
Ok(()),
"signal {signal}"
);
}
assert_eq!(check_receives(Tracee::Plain, libc::SIGTERM, false), Ok(()));
assert_eq!(check_receives(Tracee::CatchSigterm, libc::SIGTERM, true), Ok(()));
}
#[skuld::test]
fn a_blocked_signal_fails_the_precondition(#[fixture(tracer_group)] _group: &Group) {
let message = check_receives(Tracee::BlockSigtstp, libc::SIGTSTP, false).expect_err("no failure");
assert!(message.contains("blocks signal 18"), "{message}");
}
#[skuld::test]
fn a_blocked_tracee_blocks_the_signal_on_every_thread(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, ready) = super::spawn_tracee(Tracee::BlockSigtstp);
let stdin = tracee.stdin().expect("the tracee's stdin is piped");
let pid = tracee.id().pid();
assert_job_control_reaches(pid, &ready, libc::SIGTSTP);
let message = panic_of(|| assert_receives(pid, &ready, libc::SIGTSTP, false));
assert!(message.contains("blocks signal 18"), "{message}");
send(pid, libc::SIGTSTP);
drop(stdin);
let info = await_change(pid);
if info.si_code == libc::CLD_STOPPED {
end_stopped(tracee);
panic!("the tracee was stopped by signal {}", info.si_status);
}
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn a_tracee_a_test_sends_a_job_control_stop_must_lead_its_own_group(#[fixture(tracer_group)] _group: &Group) {
let message = panic_of(|| drop(tracee_receiving(Tracee::Plain, &[libc::SIGTSTP])));
assert!(message.contains("does not lead its own process group"), "{message}");
}
#[skuld::test]
fn an_ignored_signal_fails_the_precondition(#[fixture(tracer_group)] _group: &Group) {
let message = check_receives(Tracee::IgnoreSigtstp, libc::SIGTSTP, false).expect_err("no failure");
assert!(message.contains("ignores signal 18"), "{message}");
}
#[skuld::test]
fn a_missing_handler_fails_the_precondition(#[fixture(tracer_group)] _group: &Group) {
let message = check_receives(Tracee::Plain, libc::SIGTERM, true).expect_err("no failure");
assert!(message.contains("catches signal 15"), "{message}");
}
#[skuld::test]
fn a_shared_group_fails_the_precondition_for_a_job_control_stop(#[fixture(tracer_group)] _group: &Group) {
let message = check_receives(Tracee::Plain, libc::SIGTSTP, false).expect_err("no failure");
assert!(message.contains("does not lead its own process group"), "{message}");
}
#[skuld::test]
fn an_orphaned_group_fails_the_precondition_for_a_job_control_stop(#[fixture(tracer_group)] _group: &Group) {
let message = check_receives(Tracee::OwnSession, libc::SIGTSTP, false).expect_err("no failure");
assert!(message.contains("process group is orphaned"), "{message}");
}
#[skuld::test]
fn an_orphaned_group_does_not_matter_for_other_signals(#[fixture(tracer_group)] _group: &Group) {
assert_eq!(check_receives(Tracee::OwnSession, libc::SIGTERM, false), Ok(()));
}
#[skuld::test]
fn a_client_helper_reports_only_the_protocol(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start(Mode::Auto).attach(&mut tracee);
assert_eq!(th.recv(), Report::Attached);
drop(stdin);
assert_eq!(th.recv(), Report::Reaped);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn attach_refuses_a_reaped_tracee(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
drop(stdin);
let status = tracee.wait().expect("wait for the tracee");
assert!(status.success(), "expected the tracee's own clean exit, got {status:?}");
let pending = super::start(Mode::Auto);
let message = panic_of(|| drop(pending.attach(&mut tracee)));
assert!(message.contains("not this process's unreaped child"), "{message}");
}
#[skuld::test]
fn dropping_a_helper_that_awaits_the_tracees_exit_kills_it_and_fails(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th = at_s4(&mut tracee, "S4:ESRCH");
expect(&mut th, &["S4", "S6", "blocking S6 exit"]);
drop(stdin);
let message = panic_of(|| drop(th));
assert!(message.contains("waited in S6"), "{message}");
drop(tracee);
}
fn malformed(line: &[u8]) {
let mut pending = super::start_forced(Mode::Auto, "");
std::io::Write::write_all(pending.session.signal_tx(), line).expect("write the line");
drop(pending.session.signal_tx.take());
assert_eq!(pending.session.next_report(), None);
let status = pending.session.finish();
assert_eq!(
status.code(),
Some(super::machine::MALFORMED_PID_LINE),
"after {:?}: {status}",
String::from_utf8_lossy(line)
);
}
#[skuld::test]
fn s_minus_1_a_stray_line_is_malformed(#[fixture(tracer_group)] _group: &Group) {
malformed(b"not a pid\n");
}
#[skuld::test]
fn s_minus_1_a_non_numeric_pid_is_malformed(#[fixture(tracer_group)] _group: &Group) {
malformed(b"pid abc\n");
}
#[skuld::test]
fn s_minus_1_an_empty_pid_is_malformed(#[fixture(tracer_group)] _group: &Group) {
malformed(b"pid \n");
}
#[skuld::test]
fn s_minus_1_pid_0_is_malformed(#[fixture(tracer_group)] _group: &Group) {
malformed(b"pid 0\n");
}
#[skuld::test]
fn s_minus_1_a_negative_pid_is_malformed(#[fixture(tracer_group)] _group: &Group) {
malformed(b"pid -1\n");
}
#[skuld::test]
fn s_minus_1_non_utf8_is_malformed(#[fixture(tracer_group)] _group: &Group) {
malformed(b"pid \xff\n");
}
#[skuld::test]
fn s_minus_1_eof_inside_the_line_is_malformed(#[fixture(tracer_group)] _group: &Group) {
malformed(b"pid 99999998");
}
#[skuld::test]
fn s_minus_1_eof_exits_without_a_report(#[fixture(tracer_group)] _group: &Group) {
let mut pending = super::start_forced(Mode::Auto, "");
drop(pending.session.signal_tx.take());
assert_eq!(pending.session.next_report(), None);
assert!(pending.session.finish().success());
}
#[skuld::test]
fn s0_a_receipt_error_fails(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th = super::start_forced(Mode::Auto, "S0:ESRCH").attach(&mut tracee);
expect(&mut th, &["S0", &err(libc::ESRCH, "S0"), DONE]);
drop(th);
drop(stdin);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s1_an_attach_error_fails(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th = super::start_forced(Mode::Auto, "S1:EPERM").attach(&mut tracee);
expect(&mut th, &["S0", "S1", &err(libc::EPERM, "S1"), DONE]);
drop(th);
drop(stdin);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s1h_a_signal_byte_goes_to_release(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S1:hold").attach(&mut tracee);
expect(&mut th, &HELD);
th.signal();
expect(&mut th, &RELEASED);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn a_stop_record_of_an_exiting_tracee_is_its_kill(#[fixture(tracer_group)] _group: &Group) {
let (tracee, _stdin) = tracee();
let pid = tracee.id().pid();
send(pid, libc::SIGKILL);
sys::peek(pid, libc::WEXITED).expect("waitid the killed tracee");
let _stale = ForcedStop::arm(libc::SIGKILL);
assert_sigkilled(tracee);
}
#[skuld::test]
fn a_stopped_tracee_reads_as_stopped(#[fixture(tracer_group)] _group: &Group) {
let (tracee, _stdin) = tracee();
let pid = tracee.id().pid();
send(pid, libc::SIGSTOP);
let info = await_change(pid);
assert_eq!(
stopped_by(&info, proc_state(pid)),
Some(libc::SIGSTOP),
"the tracee after SIGSTOP"
);
end_stopped(tracee);
}
#[skuld::test]
fn stopped_by_keys_on_p_wexit_alone() {
let others = !P_WEXIT;
let rows = [
(libc::CLD_STOPPED, libc::SIGKILL, libc::SSTOP, P_WEXIT, None),
(libc::CLD_STOPPED, libc::SIGKILL, libc::SSTOP, P_WEXIT | others, None),
(libc::CLD_STOPPED, libc::SIGKILL, libc::SZOMB, P_WEXIT, None),
(libc::CLD_STOPPED, libc::SIGSTOP, libc::SSTOP, 0, Some(libc::SIGSTOP)),
(
libc::CLD_STOPPED,
libc::SIGSTOP,
libc::SSTOP,
others,
Some(libc::SIGSTOP),
),
(libc::CLD_STOPPED, libc::SIGTSTP, libc::SRUN, 0, Some(libc::SIGTSTP)),
(libc::CLD_TRAPPED, libc::SIGTRAP, libc::SSTOP, 0, None),
(libc::CLD_CONTINUED, libc::SIGCONT, libc::SRUN, 0, None),
(libc::CLD_KILLED, libc::SIGKILL, libc::SZOMB, P_WEXIT, None),
(libc::CLD_EXITED, 0, libc::SZOMB, P_WEXIT, None),
];
for (code, status, p_stat, p_flag, expected) in rows {
let state = ProcState { p_stat, p_flag };
assert_eq!(
stopped_by(&record(code, status), state),
expected,
"si_code {code}, si_status {status}, {state:?}"
);
}
}
#[skuld::test]
#[cfg(debug_assertions)]
#[should_panic(expected = "a zombie without P_WEXIT")]
fn stopped_by_asserts_a_zombie_has_begun_to_exit() {
let state = ProcState {
p_stat: libc::SZOMB,
p_flag: 0,
};
stopped_by(&record(libc::CLD_EXITED, 0), state);
}
#[skuld::test]
fn assert_sigkilled_needs_the_helpers_reaped(#[fixture(tracer_group)] _group: &Group) {
let (tracee, stdin) = tracee();
let pending = super::start(Mode::Auto);
drop(stdin);
let message = panic_of(|| assert_sigkilled(tracee));
assert!(message.contains("before the tracer helper was reaped"), "{message}");
drop(pending);
}
#[skuld::test]
fn s1h_eof_exits_and_xnu_kills_the_tracee(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th = super::start_forced(Mode::Auto, "S1:hold").attach(&mut tracee);
expect(&mut th, &HELD);
drop(th.session.signal_tx.take());
expect(&mut th, &[DONE]);
assert_eq!(th.session.next_report(), None);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
#[skuld::test]
fn s1h_note_exit_goes_to_reap(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th = super::start_forced(Mode::Auto, "S1:hold,S1h:NOTE_EXIT,S5:ok").attach(&mut tracee);
expect(&mut th, &["S0", "S1", "S1h", "S5", "reaped", DONE]);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
fn s1h_peeks_until_the_error(force: &str) {
let (mut tracee, stdin) = tracee();
let mut th = super::start_forced(Mode::Auto, force).attach(&mut tracee);
expect(&mut th, &["S0", "S1", "S1h", &err(libc::EINVAL, "S1h"), DONE]);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
#[skuld::test]
fn s1h_a_failed_stop_peek_fails(#[fixture(tracer_group)] _group: &Group) {
s1h_peeks_until_the_error("S1:hold,S1hstop:EINVAL");
}
#[skuld::test]
fn s1h_a_tracee_not_stopped_yet_backs_off(#[fixture(tracer_group)] _group: &Group) {
s1h_peeks_until_the_error("S1:hold,S1hstop:none,S1hstop:EINVAL");
}
#[skuld::test]
fn s1h_a_settling_stop_backs_off(#[fixture(tracer_group)] _group: &Group) {
s1h_peeks_until_the_error("S1:hold,S1hstop:settling,S1hstop:EINVAL");
}
#[skuld::test]
fn s1h_a_lone_sigchld_reads_as_the_timeout(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th = super::start_forced(Mode::Auto, "S1:hold,S1h:SIGCHLD").attach(&mut tracee);
expect(&mut th, &HELD);
drop(th.session.signal_tx.take());
expect(&mut th, &[DONE]);
assert_eq!(th.session.next_report(), None);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
fn never_ran(thread: &libc::proc_threadinfo) -> bool {
thread.pth_user_time == 0 && thread.pth_system_time == 0
}
fn read_threads(pid: u32) -> Vec<libc::proc_threadinfo> {
sys::threads(pid)
.expect("list the tracee's threads")
.into_iter()
.map(|(_, info)| info.expect("no tracee thread exits while the test reads them"))
.collect()
}
fn await_settled(pid: u32) {
let parked = [libc::TH_STATE_WAITING, libc::TH_STATE_STOPPED];
let mut backoff = std::time::Duration::from_millis(1);
loop {
match sys::pbi_status(pid) {
Ok(libc::SSTOP) => {
let threads = read_threads(pid);
if threads
.iter()
.all(|thread| never_ran(thread) || parked.contains(&thread.pth_run_state))
{
return;
}
}
Ok(libc::SZOMB) => panic!("the held tracee exited"),
Ok(_) => {}
Err(e) => panic!("the held tracee is gone: errno {e}"),
}
std::thread::sleep(backoff);
backoff = (backoff * 2).min(std::time::Duration::from_millis(50));
}
}
#[skuld::test]
fn a_stop_with_a_never_started_thread_settles(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee_with(Tracee::UnstartedThread);
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S1:hold").attach(&mut tracee);
expect(&mut th, &["S0", "S1", "S1h"]);
await_settled(pid);
assert!(
read_threads(pid).iter().any(|thread| never_ran(thread)
&& thread.pth_run_state == libc::TH_STATE_UNINTERRUPTIBLE
&& thread.pth_flags & libc::TH_FLAGS_SWAPPED != 0),
"no never-started thread reads as uninterruptible with no kernel stack: the premise of \
the exemption under test"
);
th.signal();
expect(
&mut th,
&["S1hs*", "blocking S1h eof*", "S2", "attached", "S3", "blocking S3 eof"],
);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
fn from_held<'a>(tracee: &'a mut crate::Child, force: &str) -> TracerHelper<'a> {
let mut th = super::start_forced(Mode::Auto, force).attach(tracee);
expect(&mut th, &HELD);
th.signal();
th
}
#[skuld::test]
fn s2_ebusy_backs_off_then_retries(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = from_held(&mut tracee, "S1:hold,S2:EBUSY");
expect(&mut th, &["S2", "S2b", "attached", "S3", "blocking S3 eof"]);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s2_a_tracee_not_stopped_yet_backs_off(#[fixture(tracer_group)] _group: &Group) {
s2_backs_off_before_the_release("S1:hold,S2stop:none");
}
#[skuld::test]
fn s2_a_settling_stop_backs_off(#[fixture(tracer_group)] _group: &Group) {
s2_backs_off_before_the_release("S1:hold,S2stop:settling");
}
fn s2_backs_off_before_the_release(force: &str) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = from_held(&mut tracee, force);
expect(&mut th, &["S2", "S2b", "attached", "S3", "blocking S3 eof"]);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s2_passes_a_stopping_signal_through(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee_receiving(Tracee::CatchSigterm, &[libc::SIGTERM]);
let mut th = from_held(&mut tracee, "S1:hold,S2stop:SIGTERM");
expect(&mut th, &["S2", "S2s"]);
drop(stdin);
expect(&mut th, &["S2b", "S2b*", &err("NOTE_EXIT", "S2b"), DONE]);
drop(th);
let status = tracee.wait().expect("wait for the tracee");
assert_eq!(
status.code(),
Some(super::SIGTERM_EXIT),
"expected the SIGTERM handler's exit, got {status:?}"
);
}
#[skuld::test]
fn s2_keeps_a_stop_signal_until_after_the_detach(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee_receiving(Tracee::OwnGroup, &[libc::SIGTSTP]);
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S1:hold,S2stop:SIGTSTP").attach(&mut tracee);
expect(&mut th, &HELD);
send(pid, libc::SIGSTOP);
th.signal();
expect(
&mut th,
&["S2", "S2k", "S2b", "S2b*", "attached", "S3", "blocking S3 eof"],
);
th.signal();
expect(&mut th, &["S4", "S4b*", "S4r", "detached", DONE]);
drop(th);
drop(stdin);
assert_job_stopped(pid, libc::SIGTSTP);
end_stopped(tracee);
}
fn s2_fails(force: &str) {
let (mut tracee, stdin) = tracee();
let mut th = super::start_forced(Mode::Auto, force).attach(&mut tracee);
expect(&mut th, &["S0", "S1", "S2", &err(libc::EINVAL, "S2"), DONE]);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
#[skuld::test]
fn s2_a_failed_stop_peek_fails(#[fixture(tracer_group)] _group: &Group) {
s2_fails("S2stop:EINVAL");
}
#[skuld::test]
fn s2_a_failed_pass_through_fails(#[fixture(tracer_group)] _group: &Group) {
s2_fails("S2stop:SIGTERM,S2cont:EINVAL");
}
#[skuld::test]
fn s2_another_errno_fails(#[fixture(tracer_group)] _group: &Group) {
s2_fails("S2:EINVAL");
}
fn s2b_event_fails(event: &str) {
let (mut tracee, stdin) = tracee();
let force = format!("S2:EBUSY,S2b:{event}");
let mut th = super::start_forced(Mode::Auto, &force).attach(&mut tracee);
expect(&mut th, &["S0", "S1", "S2", "S2b", &err(event, "S2b"), DONE]);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
#[skuld::test]
fn s2b_note_exit_fails(#[fixture(tracer_group)] _group: &Group) {
s2b_event_fails("NOTE_EXIT");
}
#[skuld::test]
fn s2b_a_signal_byte_fails(#[fixture(tracer_group)] _group: &Group) {
s2b_event_fails("SIGNAL");
}
#[skuld::test]
fn s2b_eof_fails(#[fixture(tracer_group)] _group: &Group) {
s2b_event_fails("EOF");
}
#[skuld::test]
fn s2b_a_lone_sigchld_reads_as_the_timeout(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = from_held(&mut tracee, "S1:hold,S2:EBUSY,S2b:SIGCHLD");
expect(&mut th, &["S2", "S2b", "attached", "S3", "blocking S3 eof"]);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s3_auto_note_exit_goes_to_reap(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s3_auto_note_exit_wins_over_a_signal_byte_in_the_same_batch(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S3:NOTE_EXIT+SIGNAL").attach(&mut tracee);
expect(
&mut th,
&["S0", "S1", "S2", "S2b*", "attached", "S3", "S5", "blocking S5 exit"],
);
drop(stdin);
expect(&mut th, &["reaped", DONE]);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s3_auto_a_signal_byte_goes_to_detach(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "").attach(&mut tracee);
expect(&mut th, &TO_S3);
th.signal();
expect(&mut th, &DETACHED);
assert_is_our_child(pid);
drop(th);
end_detached(tracee, stdin);
}
#[skuld::test]
fn s3_auto_eof_goes_to_detach(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(th.session.signal_tx.take());
expect(&mut th, &DETACHED);
assert_is_our_child(pid);
drop(th);
end_detached(tracee, stdin);
}
#[skuld::test]
fn s5_eintr_retries(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S5:EINTR").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s5_another_errno_fails(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S5:ECHILD").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &["S5", &err(libc::ECHILD, "S5"), DONE]);
assert_not_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s5_a_stop_is_not_a_reap(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S3:NOTE_EXIT").attach(&mut tracee);
expect(
&mut th,
&["S0", "S1", "S2", "S2b*", "attached", "S3", "S5", "blocking S5 exit"],
);
send(pid, libc::SIGSTOP);
expect(&mut th, &[&err(libc::EINVAL, "S5"), DONE]);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
const EXITED: [&str; 4] = ["blocking S3 exit", "exited", "S3x", "blocking S3x eof"];
#[skuld::test]
fn s3_hold_note_exit_reports_exited_then_a_signal_byte_reaps(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Hold, "").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &EXITED);
assert_not_handed_back(pid);
th.signal();
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s3x_eof_reaps(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Hold, "").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &EXITED);
drop(th.session.signal_tx.take());
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
fn s3x_injected_release(event: &str) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Hold, &format!("S3x:{event}")).attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &["blocking S3 exit", "exited", "S3x"]);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s3x_an_injected_signal_byte_reaps(#[fixture(tracer_group)] _group: &Group) {
s3x_injected_release("SIGNAL");
}
#[skuld::test]
fn s3x_an_injected_eof_reaps(#[fixture(tracer_group)] _group: &Group) {
s3x_injected_release("EOF");
}
#[skuld::test]
fn s3x_note_exit_fails(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Hold, "S3x:NOTE_EXIT").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(
&mut th,
&["blocking S3 exit", "exited", "S3x", &err("NOTE_EXIT", "S3x"), DONE],
);
assert_not_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s3x_a_lone_sigchld_is_ignored(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Hold, "S3x:SIGCHLD,S3x:NOTE_EXIT").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(
&mut th,
&["blocking S3 exit", "exited", "S3x", &err("NOTE_EXIT", "S3x"), DONE],
);
assert_not_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s3_hold_a_zombie_wait_that_finds_no_exit_fails(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Hold, "S3:NOTE_EXIT").attach(&mut tracee);
expect(
&mut th,
&["S0", "S1", "S2", "S2b*", "attached", "S3", "blocking S3 exit"],
);
send(pid, libc::SIGSTOP);
expect(&mut th, &[&err(libc::EINVAL, "S3"), DONE]);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
#[skuld::test]
fn s3_hold_a_signal_byte_without_note_exit_goes_to_detach(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Hold, "").attach(&mut tracee);
expect(&mut th, &TO_S3);
th.signal();
expect(&mut th, &DETACHED);
assert_is_our_child(pid);
drop(th);
end_detached(tracee, stdin);
}
#[skuld::test]
fn s3_hold_eof_without_note_exit_goes_to_detach(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Hold, "").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(th.session.signal_tx.take());
expect(&mut th, &DETACHED);
assert_is_our_child(pid);
drop(th);
end_detached(tracee, stdin);
}
fn s3_hold_batch_passes_s3x(event: &str) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let force = format!("S3:NOTE_EXIT+{event}");
let mut th = super::start_forced(Mode::Hold, &force).attach(&mut tracee);
expect(
&mut th,
&["S0", "S1", "S2", "S2b*", "attached", "S3", "blocking S3 exit"],
);
drop(stdin);
expect(&mut th, &["exited", "S3x", "S5", "blocking S5 exit", "reaped", DONE]);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s3_hold_note_exit_and_a_signal_byte_in_one_batch_pass_s3x_to_reap(#[fixture(tracer_group)] _group: &Group) {
s3_hold_batch_passes_s3x("SIGNAL");
}
#[skuld::test]
fn s3_hold_note_exit_and_eof_in_one_batch_pass_s3x_to_reap(#[fixture(tracer_group)] _group: &Group) {
s3_hold_batch_passes_s3x("EOF");
}
fn at_s4<'a>(tracee: &'a mut crate::Child, force: &str) -> TracerHelper<'a> {
let mut th = super::start_forced(Mode::Auto, force).attach(tracee);
expect(&mut th, &TO_S3);
th.signal();
th
}
#[skuld::test]
fn s4_ebusy_backs_off_then_retries(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = at_s4(&mut tracee, "S4:EBUSY,S4sigstop:1");
expect(&mut th, &["S4", "S4b", "S4b*", "detached", DONE]);
assert_is_our_child(pid);
drop(th);
end_detached(tracee, stdin);
}
#[skuld::test]
fn s4_a_tracee_not_stopped_yet_backs_off(#[fixture(tracer_group)] _group: &Group) {
s4_backs_off_before_the_detach("none");
}
#[skuld::test]
fn s4_a_settling_stop_backs_off(#[fixture(tracer_group)] _group: &Group) {
s4_backs_off_before_the_detach("settling");
}
fn s4_backs_off_before_the_detach(stop: &str) {
let (mut tracee, stdin) = tracee();
let force = format!("S1:hold,S2:ok,S3:SIGNAL,S4sigstop:0,S4stop:{stop}");
let mut th = from_held(&mut tracee, &force);
expect(&mut th, &["S2", "attached", "S3", "S4", "S4b", "detached", DONE]);
drop(th);
end_detached(tracee, stdin);
}
#[skuld::test]
fn s4b_note_exit_goes_to_reap(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = at_s4(&mut tracee, "S4:EBUSY,S4b:NOTE_EXIT");
expect(&mut th, &["S4", "S4b", "S5", "blocking S5 exit"]);
drop(stdin);
expect(&mut th, &["reaped", DONE]);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
fn s4b_ignores(event: &str) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = at_s4(&mut tracee, &format!("S4:EBUSY,S4sigstop:1,S4b:{event}"));
expect(&mut th, &["S4", "S4b", "S4b*", "detached", DONE]);
assert_is_our_child(pid);
drop(th);
end_detached(tracee, stdin);
}
#[skuld::test]
fn s4b_a_signal_byte_is_ignored(#[fixture(tracer_group)] _group: &Group) {
s4b_ignores("SIGNAL");
}
#[skuld::test]
fn s4b_eof_is_ignored(#[fixture(tracer_group)] _group: &Group) {
s4b_ignores("EOF");
}
#[skuld::test]
fn s4b_a_lone_sigchld_is_ignored(#[fixture(tracer_group)] _group: &Group) {
s4b_ignores("SIGCHLD");
}
fn s4_goes_to_s6(force: &str) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = at_s4(&mut tracee, force);
expect(&mut th, &["S4", "S6", "blocking S6 exit"]);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s4_esrch_goes_to_exiting(#[fixture(tracer_group)] _group: &Group) {
s4_goes_to_s6("S4:ESRCH");
}
#[skuld::test]
fn s4_a_sigstop_esrch_goes_to_exiting(#[fixture(tracer_group)] _group: &Group) {
s4_goes_to_s6("S4sigstop:ESRCH,S4:EINVAL");
}
#[skuld::test]
fn s4_a_pass_through_on_an_exiting_tracee_goes_to_exiting(#[fixture(tracer_group)] _group: &Group) {
s4_goes_to_s6("S4sigstop:0,S4stop:SIGTERM,S4cont:ESRCH");
}
#[skuld::test]
fn s4_esrch_after_note_exit_goes_to_reap(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = at_s4(&mut tracee, "seed:NOTE_EXIT,S4:ESRCH");
expect(&mut th, &["S4", "S5", "blocking S5 exit"]);
drop(stdin);
expect(&mut th, &["reaped", DONE]);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
fn s4_fails(force: &str, errno: i32) {
let (mut tracee, stdin) = tracee();
let mut th = at_s4(&mut tracee, force);
expect(&mut th, &["S4", &err(errno, "S4"), DONE]);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
#[skuld::test]
fn s4_eperm_fails(#[fixture(tracer_group)] _group: &Group) {
s4_fails("S4:EPERM", libc::EPERM);
}
#[skuld::test]
fn s4_another_errno_fails(#[fixture(tracer_group)] _group: &Group) {
s4_fails("S4:EINVAL", libc::EINVAL);
}
#[skuld::test]
fn s4_a_sigstop_error_fails(#[fixture(tracer_group)] _group: &Group) {
s4_fails("S4sigstop:EPERM,S4:EINVAL", libc::EPERM);
}
#[skuld::test]
fn s4_a_failed_stop_peek_fails(#[fixture(tracer_group)] _group: &Group) {
s4_fails("S4stop:EINVAL", libc::EINVAL);
}
#[skuld::test]
fn s4_a_failed_pass_through_fails(#[fixture(tracer_group)] _group: &Group) {
s4_fails("S4sigstop:0,S4stop:SIGTERM,S4cont:EINVAL", libc::EINVAL);
}
fn s6_ignores(eof: bool) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = at_s4(&mut tracee, "S4:ESRCH");
expect(&mut th, &["S4", "S6", "blocking S6 exit"]);
if eof {
drop(th.session.signal_tx.take());
} else {
th.signal();
}
expect(&mut th, &["S6", "blocking S6 exit"]);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s6_a_signal_byte_is_ignored(#[fixture(tracer_group)] _group: &Group) {
s6_ignores(false);
}
#[skuld::test]
fn s6_eof_is_ignored(#[fixture(tracer_group)] _group: &Group) {
s6_ignores(true);
}
#[skuld::test]
fn s6_a_lone_sigchld_is_ignored(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th = at_s4(&mut tracee, "S4:ESRCH,S6:SIGCHLD");
expect(&mut th, &["S4", "S6", "blocking S6 exit"]);
drop(stdin);
expect(&mut th, &REAPED);
drop(th);
assert_exited_cleanly(tracee);
}
fn assert_job_stopped(pid: u32, signal: i32) {
let info = await_change(pid);
assert!(
info.si_code == libc::CLD_STOPPED && [signal, libc::SIGSTOP].contains(&info.si_status),
"the detached tracee is not stopped by {signal} or SIGSTOP: si_code {}, si_status {}",
info.si_code,
info.si_status
);
}
fn released_with_pending<'a>(tracee: &'a mut crate::Child, force: &str, signal: i32) -> TracerHelper<'a> {
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, force).attach(tracee);
expect(&mut th, &HELD);
send(pid, signal);
th.signal();
expect(&mut th, &["S2", "attached", "S3"]);
th
}
#[skuld::test]
fn s3_passes_a_stopping_signal_through(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee_receiving(Tracee::Plain, &[libc::SIGTERM]);
let pid = tracee.id().pid();
let mut th = released_with_pending(&mut tracee, "S1:hold", libc::SIGTERM);
expect(&mut th, &["blocking S3 eof", "S3s", "blocking S3 eof"]);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_terminated(tracee);
}
#[skuld::test]
fn s3_keeps_a_stop_signal_until_after_the_detach(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee_receiving(Tracee::OwnGroup, &[libc::SIGTSTP]);
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "").attach(&mut tracee);
expect(&mut th, &TO_S3);
send(pid, libc::SIGTSTP);
expect(&mut th, &["S3k", "blocking S3 eof"]);
th.signal();
expect(&mut th, &["S4", "S4b*", "S4r", "detached", DONE]);
drop(th);
drop(stdin);
assert_job_stopped(pid, libc::SIGTSTP);
end_stopped(tracee);
}
#[skuld::test]
fn s3_keeps_only_the_first_stop_signal(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee_receiving(Tracee::OwnGroup, &[libc::SIGTSTP, libc::SIGTTIN]);
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "").attach(&mut tracee);
expect(&mut th, &TO_S3);
send(pid, libc::SIGTSTP);
expect(&mut th, &["S3k", "blocking S3 eof"]);
send(pid, libc::SIGTTIN);
expect(&mut th, &["S3k", "blocking S3 eof"]);
th.signal();
expect(&mut th, &["S4", "S4b*", "S4r", "detached", DONE]);
drop(th);
drop(stdin);
assert_job_stopped(pid, libc::SIGTSTP);
end_stopped(tracee);
}
#[skuld::test]
fn s3_a_sigcont_drops_a_kept_stop_signal(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee_receiving(Tracee::OwnGroup, &[libc::SIGTSTP, libc::SIGCONT]);
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "").attach(&mut tracee);
expect(&mut th, &TO_S3);
send(pid, libc::SIGTSTP);
expect(&mut th, &["S3k", "blocking S3 eof"]);
send(pid, libc::SIGCONT);
expect(&mut th, &["S3s", "blocking S3 eof"]);
th.signal();
expect(&mut th, &DETACHED);
drop(th);
drop(stdin);
let info = await_change(pid);
assert!(
info.si_code != libc::CLD_STOPPED || info.si_status != libc::SIGTSTP,
"the dropped SIGTSTP stopped the tracee"
);
if info.si_code == libc::CLD_STOPPED {
end_stopped(tracee);
} else {
assert_exited_cleanly(tracee);
}
}
#[skuld::test]
fn s4_keeps_a_stop_signal_until_after_the_detach(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee_receiving(Tracee::OwnGroup, &[libc::SIGTSTP]);
let pid = tracee.id().pid();
let mut th = released_with_pending(&mut tracee, "S1:hold,S3:SIGNAL,S4sigstop:0", libc::SIGTSTP);
expect(&mut th, &["S4", "S4b*", "S4k"]);
send(pid, libc::SIGSTOP);
expect(&mut th, &["S4b*", "S4r", "detached", DONE]);
drop(th);
drop(stdin);
assert_job_stopped(pid, libc::SIGTSTP);
end_stopped(tracee);
}
#[skuld::test]
fn s4_a_failed_resend_fails(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee_receiving(Tracee::OwnGroup, &[libc::SIGTSTP]);
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S4r:EPERM").attach(&mut tracee);
expect(&mut th, &TO_S3);
send(pid, libc::SIGTSTP);
expect(&mut th, &["S3k", "blocking S3 eof"]);
th.signal();
expect(&mut th, &["S4", "S4b*", &err(libc::EPERM, "S4"), DONE]);
drop(th);
end_detached(tracee, stdin);
}
#[skuld::test]
fn s3_a_sigchld_without_a_stop_changes_nothing(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S3:SIGCHLD").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn s3_a_pass_through_on_an_exiting_tracee_waits_for_note_exit(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S3:SIGCHLD,S3stop:SIGTERM,S3cont:ESRCH").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &REAPED);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
fn s3_fails(force: &str) {
let (mut tracee, stdin) = tracee();
let mut th = super::start_forced(Mode::Auto, force).attach(&mut tracee);
expect(
&mut th,
&[
"S0",
"S1",
"S2",
"S2b*",
"attached",
"S3",
&err(libc::EINVAL, "S3"),
DONE,
],
);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
#[skuld::test]
fn s3_a_failed_stop_peek_fails(#[fixture(tracer_group)] _group: &Group) {
s3_fails("S3:SIGCHLD,S3stop:EINVAL");
}
#[skuld::test]
fn s3_a_settling_stop_peeks_again(#[fixture(tracer_group)] _group: &Group) {
s3_fails("S3:SIGCHLD,S3stop:settling,S3stop:EINVAL");
}
#[skuld::test]
fn s3_a_failed_pass_through_fails(#[fixture(tracer_group)] _group: &Group) {
s3_fails("S3:SIGCHLD,S3stop:SIGTERM,S3cont:EINVAL");
}
#[skuld::test]
fn s4_passes_a_stopping_signal_through_before_detaching(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee_receiving(Tracee::Plain, &[libc::SIGTERM]);
let pid = tracee.id().pid();
let mut th = released_with_pending(&mut tracee, "S1:hold,S3:SIGNAL", libc::SIGTERM);
expect(
&mut th,
&["S4", "S4b*", "S4s", "S4b*", "S5", "blocking S5 exit", "reaped", DONE],
);
assert_handed_back(pid);
drop(th);
drop(stdin);
assert_terminated(tracee);
}
#[skuld::test]
fn a_failed_report_write_exits(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th = super::start_forced(Mode::Auto, "S1:hold").attach(&mut tracee);
expect(&mut th, &HELD);
th.session.reports = None;
th.signal();
drop(th.session.signal_tx.take());
th.session.finish();
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
#[skuld::test]
fn a_failed_attached_write_ends_the_run(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th = from_held(&mut tracee, "S1:hold,gone:attached");
expect(&mut th, &["S2", DONE]);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
#[skuld::test]
fn a_failed_pass_through_trace_write_ends_the_run(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th = from_held(&mut tracee, "S1:hold,S2stop:SIGTERM,S2cont:ok,gone:state S2s");
expect(&mut th, &["S2", DONE]);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
#[skuld::test]
fn a_failed_s3_pass_through_trace_write_ends_the_run(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th =
super::start_forced(Mode::Auto, "S3:SIGCHLD,S3stop:SIGTERM,S3cont:ok,gone:state S3s").attach(&mut tracee);
expect(&mut th, &["S0", "S1", "S2", "S2b*", "attached", "S3", DONE]);
drop(th);
drop(stdin);
assert_sigkilled(tracee);
}
#[skuld::test]
fn a_failed_exited_write_ends_the_run(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Hold, "gone:exited").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &["blocking S3 exit", DONE]);
assert_not_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn a_failed_blocking_write_ends_the_run(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "gone:blocking S5 exit").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &["S5", DONE]);
assert_not_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn a_failed_reaped_write_ends_the_run(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "gone:reaped").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &["S5", "blocking S5 exit", DONE]);
assert_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn a_failed_detached_write_ends_the_run(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let mut th = at_s4(&mut tracee, "gone:detached");
expect(&mut th, &["S4", "S4b*", DONE]);
drop(th);
end_detached(tracee, stdin);
}
#[skuld::test]
fn a_failed_error_write_ends_the_run(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let force = format!("S0:ESRCH,gone:{}", err(libc::ESRCH, "S0"));
let mut th = super::start_forced(Mode::Auto, &force).attach(&mut tracee);
expect(&mut th, &["S0", DONE]);
drop(th);
drop(stdin);
assert_exited_cleanly(tracee);
}
#[skuld::test]
fn done_ignores_a_signal_byte_and_holds(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, stdin) = tracee();
let pid = tracee.id().pid();
let mut th = super::start_forced(Mode::Auto, "S5:ECHILD").attach(&mut tracee);
expect(&mut th, &TO_S3);
drop(stdin);
expect(&mut th, &["S5", &err(libc::ECHILD, "S5"), DONE]);
th.signal();
expect(&mut th, &["done", DONE]);
assert_not_handed_back(pid);
drop(th);
assert_exited_cleanly(tracee);
}