#[cfg(unix)]
mod unix {
use std::os::unix::process::ExitStatusExt;
use std::process::ExitStatus;
use crate::os_process::child::ExitRecord;
use crate::wait::exit_only::{exit_record_of, is_exit_record, Record};
fn status(code: i32, si_status: i32) -> ExitRecord {
exit_record_of(Record {
si_code: code,
si_status,
})
}
#[skuld::test]
fn an_exited_record_gives_the_low_byte_as_the_code() {
assert_eq!(
status(libc::CLD_EXITED, 0x0A00_0107),
ExitRecord::Status(ExitStatus::from_raw(7 << 8))
);
}
#[skuld::test]
fn killed_and_dumped_records_give_the_signal() {
assert_eq!(
status(libc::CLD_KILLED, 0x0A00_0009),
ExitRecord::Status(ExitStatus::from_raw(9))
);
assert_eq!(
status(libc::CLD_DUMPED, 0x0A00_000B),
ExitRecord::Status(ExitStatus::from_raw(0x0B | 0x80))
);
}
#[skuld::test]
fn a_trapped_record_is_not_an_exit_record() {
for code in [libc::CLD_TRAPPED, libc::CLD_STOPPED, libc::CLD_CONTINUED, 0, 99] {
assert!(!is_exit_record(code), "si_code {code}");
assert_eq!(status(code, 5), ExitRecord::Unreadable { si_code: code });
}
for code in [libc::CLD_EXITED, libc::CLD_KILLED, libc::CLD_DUMPED] {
assert!(is_exit_record(code), "si_code {code}");
}
}
}
#[cfg(target_os = "linux")]
mod linux {
use std::os::fd::AsFd as _;
use std::os::unix::process::ExitStatusExt;
use std::process::ExitStatus;
use rustix::process::{pidfd_open, Pid, PidfdFlags};
use crate::identity::{FakeKernel, HostKernel, Record};
use crate::os_process::child::ExitRecord;
use crate::wait::exit_only::{try_reap, Reap, Target};
#[skuld::test]
fn a_reap_consumes_through_the_kernel_that_peeked() {
let child = crate::test_child::blocker();
let pid = child.id();
let pidfd = pidfd_open(Pid::from_raw(pid as i32).expect("a positive pid"), PidfdFlags::empty())
.expect("the child's pidfd opens");
let exited = || {
Ok(Some(Record {
si_code: libc::CLD_EXITED,
si_status: 7,
}))
};
let kernel = FakeKernel::over(&HostKernel)
.on_peek(pid, exited())
.on_consume(pid, exited());
let reaped = try_reap(&Target::pidfd(&kernel, pidfd.as_fd())).expect("the reap answers");
assert_eq!(reaped, Reap::Reaped(ExitRecord::Status(ExitStatus::from_raw(7 << 8))));
assert!(
kernel.all_rows_taken(),
"the look was the stated peek and the reap the stated consume"
);
}
}
#[cfg(windows)]
mod windows {
use std::os::windows::io::AsHandle as _;
use crate::wait::exit_only::{peek, Target};
#[skuld::test]
fn a_failed_wait_on_our_own_handle_is_a_contract_breach() {
let unwaitable = crate::test_child::unwaitable::unwaitable_handle();
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
peek(&Target::Handle(unwaitable.as_handle()))
}));
if cfg!(debug_assertions) {
assert!(outcome.is_err(), "a debug build asserts the contract");
} else {
outcome.expect("release returns").expect_err("a breach is an error");
}
}
}