use std::io;
#[cfg(unix)]
use std::process::ExitStatus;
#[cfg(not(target_os = "linux"))]
use crate::os_process::child::PeekOutcome;
#[cfg(unix)]
use crate::os_process::child::{ExitRecord, Foreign};
#[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(any(target_os = "linux", target_os = "macos"))]
use crate::identity::Kernel;
#[cfg(all(target_os = "macos", test))]
#[path = "exit_only/macos_tests.rs"]
pub(crate) mod macos_tests;
#[cfg(unix)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Reap {
Reaped(ExitRecord),
Running,
Foreign(Foreign),
}
#[derive(Clone, Copy)]
pub(crate) enum Target<'a> {
#[cfg(target_os = "linux")]
PidFd {
fd: std::os::fd::BorrowedFd<'a>,
kernel: &'a dyn Kernel,
},
#[cfg(target_os = "macos")]
Pid {
pid: u32,
unique: Option<u64>,
kernel: &'a dyn Kernel,
},
#[cfg(windows)]
Handle(std::os::windows::io::BorrowedHandle<'a>),
}
impl std::fmt::Debug for Target<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
#[cfg(target_os = "linux")]
Target::PidFd { fd, .. } => f.debug_tuple("PidFd").field(fd).finish(),
#[cfg(target_os = "macos")]
Target::Pid { pid, unique, .. } => f.debug_struct("Pid").field("pid", pid).field("unique", unique).finish(),
#[cfg(windows)]
Target::Handle(handle) => f.debug_tuple("Handle").field(handle).finish(),
}
}
}
#[cfg(target_os = "macos")]
impl<'a> Target<'a> {
pub(crate) fn pid(kernel: &'a dyn Kernel, pid: u32, unique: Option<u64>) -> Target<'a> {
Target::Pid { pid, unique, kernel }
}
}
#[cfg(target_os = "linux")]
impl<'a> Target<'a> {
pub(crate) fn pidfd(kernel: &'a dyn Kernel, fd: std::os::fd::BorrowedFd<'a>) -> Target<'a> {
Target::PidFd { fd, kernel }
}
}
#[cfg(not(target_os = "linux"))]
pub(crate) fn peek(target: &Target<'_>) -> io::Result<PeekOutcome> {
backend::peek(target)
}
#[cfg(target_os = "macos")]
pub(crate) fn peek_verified(target: &Target<'_>) -> io::Result<PeekOutcome> {
backend::peek_with(target, true)
}
#[cfg(target_os = "macos")]
pub(crate) fn peek_traced(target: &Target<'_>) -> io::Result<(PeekOutcome, Option<u32>)> {
backend::peek_holder(target, false)
}
#[cfg(unix)]
pub(crate) fn try_reap(target: &Target<'_>) -> io::Result<Reap> {
backend::try_reap(target)
}
#[cfg(unix)]
pub(crate) use crate::identity::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;