use std::os::fd::{BorrowedFd, OwnedFd};
use rustix::fs::{fstat, fstatfs};
use rustix::io::Errno;
use rustix::process::{pidfd_open, test_kill_process, waitid, Pid, PidfdFlags, WaitId, WaitIdOptions};
use super::proc_read::{ProcDir, ProcOpenFailure, ReadFailure};
use crate::identity::Record;
pub(crate) const PIDFD_THREAD: PidfdFlags = PidfdFlags::from_bits_retain(libc::PIDFD_THREAD);
const PID_FS_MAGIC: i64 = 0x5049_4446;
pub(crate) trait Kernel: Sync {
fn pidfd_open(&self, target: Pid, flags: PidfdFlags) -> Result<OwnedFd, Errno>;
fn pidfs_ino(&self, fd: BorrowedFd<'_>) -> Result<Option<u64>, Errno>;
fn open_proc(&self) -> Result<ProcDir, ProcOpenFailure>;
fn read(&self, dir: &ProcDir, path: &str) -> Result<Vec<u8>, ReadFailure>;
fn read_link(&self, dir: &ProcDir, path: &str) -> Result<Vec<u8>, ReadFailure>;
fn pids(&self, dir: &ProcDir) -> Result<Vec<u32>, ReadFailure>;
fn tids(&self, dir: &ProcDir, pid: u32) -> Result<Vec<u32>, ReadFailure>;
fn signal_probe(&self, target: Pid) -> Result<(), Errno>;
fn peek(&self, pidfd: BorrowedFd<'_>) -> Result<Option<Record>, Errno>;
fn consume(&self, pidfd: BorrowedFd<'_>) -> Result<Option<Record>, Errno>;
}
pub(crate) struct HostKernel;
impl Kernel for HostKernel {
fn pidfd_open(&self, target: Pid, flags: PidfdFlags) -> Result<OwnedFd, Errno> {
pidfd_open(target, flags)
}
fn pidfs_ino(&self, fd: BorrowedFd<'_>) -> Result<Option<u64>, Errno> {
if fstatfs(fd)?.f_type as i64 != PID_FS_MAGIC {
return Ok(None);
}
Ok(Some(fstat(fd)?.st_ino as u64))
}
fn open_proc(&self) -> Result<ProcDir, ProcOpenFailure> {
ProcDir::open()
}
fn read(&self, dir: &ProcDir, path: &str) -> Result<Vec<u8>, ReadFailure> {
dir.read(path)
}
fn read_link(&self, dir: &ProcDir, path: &str) -> Result<Vec<u8>, ReadFailure> {
dir.read_link(path)
}
fn pids(&self, dir: &ProcDir) -> Result<Vec<u32>, ReadFailure> {
dir.pids()
}
fn tids(&self, dir: &ProcDir, pid: u32) -> Result<Vec<u32>, ReadFailure> {
dir.tids(pid)
}
fn signal_probe(&self, target: Pid) -> Result<(), Errno> {
test_kill_process(target)
}
fn peek(&self, pidfd: BorrowedFd<'_>) -> Result<Option<Record>, Errno> {
waitid_record(
pidfd,
WaitIdOptions::EXITED | WaitIdOptions::NOHANG | WaitIdOptions::NOWAIT,
)
}
fn consume(&self, pidfd: BorrowedFd<'_>) -> Result<Option<Record>, Errno> {
waitid_record(pidfd, WaitIdOptions::EXITED | WaitIdOptions::NOHANG)
}
}
fn waitid_record(fd: BorrowedFd<'_>, options: WaitIdOptions) -> Result<Option<Record>, Errno> {
debug_assert!(
options.contains(WaitIdOptions::NOHANG),
"a blocking waitid on a zombie a tracer holds sleeps until the tracer lets go"
);
loop {
match waitid(WaitId::PidFd(fd), options) {
Ok(status) => {
return Ok(status.map(|st| Record {
si_code: st.raw_code(),
si_status: st.exit_status().or_else(|| st.terminating_signal()).unwrap_or(0),
}))
}
Err(Errno::INTR) => continue,
Err(e) => return Err(e),
}
}
}
forward_kernel! {
fn pidfd_open(&self, target: Pid, flags: PidfdFlags) -> Result<OwnedFd, Errno>;
fn pidfs_ino(&self, fd: BorrowedFd<'_>) -> Result<Option<u64>, Errno>;
fn open_proc(&self) -> Result<ProcDir, ProcOpenFailure>;
fn read(&self, dir: &ProcDir, path: &str) -> Result<Vec<u8>, ReadFailure>;
fn read_link(&self, dir: &ProcDir, path: &str) -> Result<Vec<u8>, ReadFailure>;
fn pids(&self, dir: &ProcDir) -> Result<Vec<u32>, ReadFailure>;
fn tids(&self, dir: &ProcDir, pid: u32) -> Result<Vec<u32>, ReadFailure>;
fn signal_probe(&self, target: Pid) -> Result<(), Errno>;
fn peek(&self, pidfd: BorrowedFd<'_>) -> Result<Option<Record>, Errno>;
fn consume(&self, pidfd: BorrowedFd<'_>) -> Result<Option<Record>, Errno>;
}
#[cfg(test)]
#[path = "kernel/fake.rs"]
pub(crate) mod fake;
#[cfg(test)]
#[path = "kernel_tests.rs"]
mod kernel_tests;