use std::io;
#[cfg(unix)]
use std::process::ExitStatus;
use crate::os_process::child::{ExitRecord, Foreign, PeekOutcome};
#[cfg(target_os = "linux")]
#[path = "exit_only/linux.rs"]
pub(crate) mod linux;
#[cfg(target_os = "macos")]
#[path = "exit_only/macos.rs"]
mod macos;
#[cfg(windows)]
#[path = "exit_only/windows.rs"]
mod windows;
#[cfg(target_os = "linux")]
use linux as backend;
#[cfg(target_os = "macos")]
use macos as backend;
#[cfg(windows)]
use windows as backend;
#[cfg(all(target_os = "macos", test))]
pub(crate) use macos::second_reap;
#[cfg(all(target_os = "macos", test))]
#[path = "exit_only/macos_tests.rs"]
pub(crate) mod macos_tests;
#[cfg(test)]
#[cfg_attr(
windows,
allow(
dead_code,
reason = "the Unix holder steps and record rewrites have no Windows caller"
)
)]
#[path = "exit_only/seams.rs"]
pub(crate) mod seams;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Reap {
Reaped(ExitRecord),
Running,
#[cfg_attr(
windows,
allow(dead_code, reason = "a process handle pins its process: no foreign reap")
)]
Foreign(Foreign),
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum Target<'a> {
#[cfg(target_os = "linux")]
PidFd(std::os::fd::BorrowedFd<'a>),
#[cfg(target_os = "macos")]
Pid {
pid: u32,
unique: Option<u64>,
_lt: std::marker::PhantomData<&'a ()>,
},
#[cfg(windows)]
Handle(std::os::windows::io::BorrowedHandle<'a>),
}
#[cfg(target_os = "macos")]
impl Target<'_> {
pub(crate) fn pid(pid: u32, unique: Option<u64>) -> Target<'static> {
Target::Pid {
pid,
unique,
_lt: std::marker::PhantomData,
}
}
}
pub(crate) fn peek(target: &Target<'_>) -> io::Result<PeekOutcome> {
#[cfg(test)]
if let Some(forced) = seams::take_forced_peek() {
return forced;
}
backend::peek(target)
}
#[cfg(target_os = "macos")]
pub(crate) fn peek_verified(target: &Target<'_>) -> io::Result<PeekOutcome> {
#[cfg(test)]
if let Some(forced) = seams::take_forced_peek() {
return forced;
}
backend::peek_with(target, true)
}
#[cfg(target_os = "macos")]
pub(crate) fn peek_traced(target: &Target<'_>) -> io::Result<(PeekOutcome, Option<u32>)> {
#[cfg(test)]
if let Some(forced) = seams::take_forced_peek() {
return forced.map(|peeked| (peeked, None));
}
backend::peek_holder(target, false)
}
pub(crate) fn try_reap(target: &Target<'_>) -> io::Result<Reap> {
backend::try_reap(target)
}
#[cfg(unix)]
#[derive(Debug, Clone, Copy)]
pub(crate) struct Record {
pub(crate) si_code: i32,
pub(crate) si_status: i32,
}
#[cfg(unix)]
fn consumed(record: Record) -> Record {
#[cfg(all(test, target_os = "macos"))]
if let Some(si_code) = seams::take_forced_si_code() {
return Record { si_code, ..record };
}
record
}
#[cfg(unix)]
pub(crate) fn is_exit_record(si_code: i32) -> bool {
matches!(si_code, libc::CLD_EXITED | libc::CLD_KILLED | libc::CLD_DUMPED)
}
#[cfg(unix)]
pub(crate) fn exit_record_of(record: Record) -> ExitRecord {
use std::os::unix::process::ExitStatusExt;
let Record { si_code, si_status } = record;
let raw = match si_code {
libc::CLD_EXITED => (si_status & 0xFF) << 8,
libc::CLD_KILLED => si_status & 0x7F,
libc::CLD_DUMPED => (si_status & 0x7F) | 0x80,
other => return ExitRecord::Unreadable { si_code: other },
};
ExitRecord::Status(ExitStatus::from_raw(raw))
}
#[cfg(test)]
#[path = "exit_only_tests.rs"]
mod exit_only_tests;