use std::{
ffi::{c_int, c_void, OsStr},
os::unix::ffi::OsStrExt,
path::Path,
sync::OnceLock,
time::Duration,
};
use rustix::{
io::Errno,
process::{kill_process, Pid},
};
use crate::{FileIdentity, MemoryKind, ResourceUsage, Signal};
const KINFO_PROC_SIZE: usize = 648;
const STARTTIME_SEC_OFFSET: usize = 0;
const STARTTIME_USEC_OFFSET: usize = 8;
const P_STAT_OFFSET: usize = 36;
const P_PID_OFFSET: usize = 40;
const PARENT_PID_OFFSET: usize = 560;
const SZOMB: u8 = 5;
#[repr(C, align(8))]
struct KinfoProc([u8; KINFO_PROC_SIZE]);
fn kinfo_proc(pid: u32) -> Option<KinfoProc> {
read_kinfo_proc(pid).ok().flatten()
}
fn read_kinfo_proc(pid: u32) -> Result<Option<KinfoProc>, ()> {
let pid = c_int::try_from(pid).ok().filter(|pid| *pid > 0).ok_or(())?;
let mut mib = [libc::CTL_KERN, libc::KERN_PROC, libc::KERN_PROC_PID, pid];
let mut info = KinfoProc([0; KINFO_PROC_SIZE]);
let mut len: libc::size_t = KINFO_PROC_SIZE;
#[allow(unsafe_code)]
let result = unsafe {
libc::sysctl(
mib.as_mut_ptr(),
mib.len() as libc::c_uint,
info.0.as_mut_ptr().cast::<c_void>(),
&mut len,
std::ptr::null_mut(),
0,
)
};
if result != 0 {
return Err(());
}
if len == 0 {
return Ok(None);
}
if len != KINFO_PROC_SIZE {
return Err(());
}
let reported_pid = i32::from_ne_bytes(
info.0[P_PID_OFFSET..P_PID_OFFSET + 4]
.try_into()
.map_err(|_| ())?,
);
if reported_pid != pid {
return Err(());
}
Ok(Some(info))
}
pub(crate) fn read_identity(pid: u32) -> crate::IdentityRead {
use crate::IdentityRead;
match read_kinfo_proc(pid) {
Ok(None) => IdentityRead::Gone,
Ok(Some(info)) if info.0[P_STAT_OFFSET] == SZOMB => IdentityRead::Gone,
Ok(Some(info)) => birth_time(&info).map_or(IdentityRead::Unknown, IdentityRead::Alive),
Err(()) => IdentityRead::Unknown,
}
}
pub(crate) fn exists(pid: u32) -> bool {
start_time(pid).is_some()
}
pub(crate) fn parent_pid(pid: u32) -> Option<u32> {
parent_pid_using(pid, bsd_parent_pid)
}
fn parent_pid_using(pid: u32, bsd_parent: impl FnOnce(u32) -> Option<u32>) -> Option<u32> {
bsd_parent(pid).or_else(|| {
let info = kinfo_proc(pid)?;
if info.0[P_STAT_OFFSET] == SZOMB {
return None;
}
let parent = i32::from_ne_bytes(
info.0[PARENT_PID_OFFSET..PARENT_PID_OFFSET + 4]
.try_into()
.ok()?,
);
u32::try_from(parent).ok()
})
}
fn bsd_parent_pid(pid: u32) -> Option<u32> {
let pid = c_int::try_from(pid).ok()?;
#[allow(unsafe_code)]
let mut info: libc::proc_bsdinfo = unsafe { std::mem::zeroed() };
let size = std::mem::size_of_val(&info) as c_int;
#[allow(unsafe_code)]
let read = unsafe {
libc::proc_pidinfo(
pid,
libc::PROC_PIDTBSDINFO,
0,
(&mut info as *mut libc::proc_bsdinfo).cast(),
size,
)
};
(read == size && info.pbi_pid == pid as u32 && info.pbi_status != u32::from(SZOMB))
.then_some(info.pbi_ppid)
}
pub(crate) fn start_time(pid: u32) -> Option<u64> {
let info = kinfo_proc(pid)?;
if info.0[P_STAT_OFFSET] == SZOMB {
return None;
}
birth_time(&info)
}
pub fn owned_child_start_time(pid: u32) -> std::io::Result<u64> {
crate::child_exit::observe(pid)?;
let invalid = || {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
"could not verify the owned child's exact kernel birth time",
)
};
let info = kinfo_proc(pid).ok_or_else(invalid)?;
birth_time(&info).ok_or_else(invalid)
}
fn birth_time(info: &KinfoProc) -> Option<u64> {
let seconds = i64::from_ne_bytes(
info.0[STARTTIME_SEC_OFFSET..STARTTIME_SEC_OFFSET + 8]
.try_into()
.ok()?,
);
let micros = i32::from_ne_bytes(
info.0[STARTTIME_USEC_OFFSET..STARTTIME_USEC_OFFSET + 4]
.try_into()
.ok()?,
);
let seconds = u64::try_from(seconds).ok()?;
let micros = u64::try_from(micros).ok()?;
seconds.checked_mul(1_000_000)?.checked_add(micros)
}
pub(crate) fn executable_identity(pid: u32) -> Option<FileIdentity> {
let pid = c_int::try_from(pid).ok()?;
let mut buffer = vec![0u8; libc::PROC_PIDPATHINFO_MAXSIZE as usize];
#[allow(unsafe_code)]
let written = unsafe {
libc::proc_pidpath(
pid,
buffer.as_mut_ptr().cast::<c_void>(),
buffer.len() as u32,
)
};
let written = usize::try_from(written).ok().filter(|&n| n > 0)?;
buffer.truncate(written.min(buffer.len()));
crate::file_identity(Path::new(OsStr::from_bytes(&buffer)))
}
pub(crate) fn resource_usage(pid: u32) -> Option<ResourceUsage> {
resource_usage_with_children(pid, false)
}
pub(crate) fn resource_usage_with_children(pid: u32, children: bool) -> Option<ResourceUsage> {
let (mut usage, child_user, child_system) = resource_usage_parts(pid)?;
if children {
usage.cpu_user += child_user;
usage.cpu_system += child_system;
}
Some(usage)
}
pub(crate) fn resource_usage_parts(
pid: u32,
) -> Option<(ResourceUsage, std::time::Duration, std::time::Duration)> {
let raw_pid = c_int::try_from(pid).ok()?;
let mut info = libc::rusage_info_v2 {
ri_uuid: [0; 16],
ri_user_time: 0,
ri_system_time: 0,
ri_pkg_idle_wkups: 0,
ri_interrupt_wkups: 0,
ri_pageins: 0,
ri_wired_size: 0,
ri_resident_size: 0,
ri_phys_footprint: 0,
ri_proc_start_abstime: 0,
ri_proc_exit_abstime: 0,
ri_child_user_time: 0,
ri_child_system_time: 0,
ri_child_pkg_idle_wkups: 0,
ri_child_interrupt_wkups: 0,
ri_child_pageins: 0,
ri_child_elapsed_abstime: 0,
ri_diskio_bytesread: 0,
ri_diskio_byteswritten: 0,
};
#[allow(unsafe_code)]
let result = unsafe {
libc::proc_pid_rusage(
raw_pid,
libc::RUSAGE_INFO_V2,
(&mut info as *mut libc::rusage_info_v2).cast::<libc::rusage_info_t>(),
)
};
if result != 0 {
return None;
}
start_time(pid)?;
Some((
ResourceUsage {
memory_bytes: info.ri_phys_footprint,
memory_kind: MemoryKind::PhysFootprint,
swap_bytes: None,
cpu_user: mach_ticks_to_duration(info.ri_user_time)?,
cpu_system: mach_ticks_to_duration(info.ri_system_time)?,
},
mach_ticks_to_duration(info.ri_child_user_time)?,
mach_ticks_to_duration(info.ri_child_system_time)?,
))
}
fn mach_ticks_to_duration(ticks: u64) -> Option<Duration> {
let (numer, denom) = mach_timebase()?;
let nanos = u128::from(ticks) * u128::from(numer) / u128::from(denom);
Some(Duration::from_nanos(
u64::try_from(nanos).unwrap_or(u64::MAX),
))
}
#[allow(deprecated)]
fn mach_timebase() -> Option<(u32, u32)> {
static TIMEBASE: OnceLock<(u32, u32)> = OnceLock::new();
timebase_using(&TIMEBASE, || {
let mut info = libc::mach_timebase_info { numer: 0, denom: 0 };
#[allow(unsafe_code)]
let result = unsafe { libc::mach_timebase_info(&mut info) };
if result == 0 && info.numer != 0 && info.denom != 0 {
Some((info.numer, info.denom))
} else {
None
}
})
}
fn timebase_using(
cache: &OnceLock<(u32, u32)>,
read: impl FnOnce() -> Option<(u32, u32)>,
) -> Option<(u32, u32)> {
if let Some(ratio) = cache.get() {
return Some(*ratio);
}
let ratio = read().filter(|(numer, denom)| *numer != 0 && *denom != 0)?;
let _ = cache.set(ratio);
cache.get().copied()
}
#[cfg(test)]
mod timebase_tests {
#[test]
fn a_failed_timebase_is_unknown_and_not_cached_as_intel_units() {
let cache = std::sync::OnceLock::new();
assert_eq!(super::timebase_using(&cache, || None), None);
assert!(cache.get().is_none());
assert_eq!(super::timebase_using(&cache, || Some((125, 0))), None);
assert_eq!(
super::timebase_using(&cache, || Some((125, 3))),
Some((125, 3))
);
assert_eq!(
super::timebase_using(&cache, || panic!("a successful ratio is reused")),
Some((125, 3))
);
}
}
pub(crate) fn signal(pid: u32, signal: Signal) -> Result<(), Errno> {
let raw = i32::try_from(pid)
.ok()
.and_then(Pid::from_raw)
.ok_or(Errno::SRCH)?;
let signal = match signal {
Signal::Terminate => rustix::process::Signal::TERM,
Signal::Kill => rustix::process::Signal::KILL,
};
kill_process(raw, signal)
}
#[cfg(test)]
mod owned_child_birth_tests {
use super::*;
#[test]
fn owned_child_birth_survives_exit_without_reaping_and_refuses_non_children() {
struct OwnedChild(std::process::Child);
impl Drop for OwnedChild {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}
let mut child = OwnedChild(
std::process::Command::new("/bin/sleep")
.arg("0.05")
.spawn()
.unwrap(),
);
let pid = child.0.id();
let birth = owned_child_start_time(pid).unwrap();
let deadline = std::time::Instant::now() + Duration::from_secs(2);
while crate::child_exit::observe(pid).unwrap().is_none() {
assert!(
std::time::Instant::now() < deadline,
"owned child did not exit"
);
std::thread::sleep(Duration::from_millis(1));
}
assert_eq!(owned_child_start_time(pid).unwrap(), birth);
assert!(
start_time(pid).is_none(),
"ordinary liveness still filters zombies"
);
assert!(
owned_child_start_time(std::process::id()).is_err(),
"a process that is not our child cannot authorize pin ownership"
);
assert!(child.0.wait().unwrap().success());
assert!(
owned_child_start_time(pid).is_err(),
"a reaped child is not owned"
);
}
}
#[cfg(test)]
mod parent_tests {
#[test]
fn current_parent_is_readable_when_bsd_process_info_is_refused() {
#[allow(unsafe_code)]
let expected = unsafe { libc::getppid() } as u32;
assert_eq!(
super::parent_pid_using(std::process::id(), |_| None),
Some(expected),
"a denied BSD query must not hide the sysctl record's current parent"
);
assert_eq!(
super::parent_pid_using(u32::MAX, |_| None),
None,
"the fallback must still refuse an invalid process identity"
);
}
}