use std::time::{Duration, SystemTime};
use super::probe::{classify_unreadable, SignalProbe};
use super::stat_parse::parse_starttime_jiffies;
use super::{Liveness, RawPid, Resolved, StartToken};
fn signal_probe(pid: RawPid) -> SignalProbe {
let Some(p) = super::probe::signal_target(pid) else {
return SignalProbe::NotAPid;
};
if unsafe { libc::kill(p, 0) } == 0 {
return SignalProbe::Signalable;
}
match std::io::Error::last_os_error().raw_os_error() {
Some(libc::ESRCH) => SignalProbe::NoSuchProcess,
_ => SignalProbe::Denied,
}
}
fn read_stat(pid: RawPid) -> Resolved<Vec<u8>> {
match std::fs::read(format!("/proc/{pid}/stat")) {
Ok(bytes) => Resolved::Found(bytes),
Err(e) => match classify_unreadable(signal_probe(pid)) {
Resolved::Gone => Resolved::Gone,
_ => {
log::debug!("/proc/{pid}/stat unreadable ({e}) but the pid is not provably gone");
Resolved::Unknown
}
},
}
}
pub(super) fn start_token(pid: RawPid) -> Resolved<StartToken> {
match read_stat(pid) {
Resolved::Found(stat) => match parse_starttime_jiffies(&stat) {
Some(j) => Resolved::Found(StartToken::from_raw(j)),
None => {
log::debug!("/proc/{pid}/stat has no parseable starttime");
Resolved::Unknown
}
},
Resolved::Gone => Resolved::Gone,
Resolved::Unknown => Resolved::Unknown,
}
}
pub(super) fn is_running(pid: RawPid, start: StartToken) -> Liveness {
match read_stat(pid) {
Resolved::Found(stat) => super::stat_parse::running_from_stat(&stat, start),
Resolved::Gone => Liveness::Dead, Resolved::Unknown => Liveness::Unknown,
}
}
pub(super) fn current_token() -> Resolved<StartToken> {
start_token(std::process::id())
}
pub(super) fn created_at(start: StartToken) -> Option<SystemTime> {
let jiffies = start.raw();
let hz = clock_ticks_per_sec()?;
let btime = boot_time_secs()?;
let secs = btime + jiffies / hz;
let nanos = ((jiffies % hz) * 1_000_000_000 / hz) as u32;
Some(SystemTime::UNIX_EPOCH + Duration::new(secs, nanos))
}
fn clock_ticks_per_sec() -> Option<u64> {
let hz = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
(hz > 0).then_some(hz as u64)
}
fn boot_time_secs() -> Option<u64> {
std::fs::read_to_string("/proc/stat")
.ok()?
.lines()
.find_map(|l| l.strip_prefix("btime "))
.and_then(|v| v.trim().parse::<u64>().ok())
}
use super::persist::{Scope, ScopeReadError};
const BOOT_ID_PATH: &str = "/proc/sys/kernel/random/boot_id";
const PID_NS_PATH: &str = "/proc/self/ns/pid";
pub(super) fn session_scope() -> Result<Scope, ScopeReadError> {
session_scope_at(std::path::Path::new(BOOT_ID_PATH), std::path::Path::new(PID_NS_PATH))
}
pub(super) fn session_scope_at(
boot_id_path: &std::path::Path,
pid_ns_path: &std::path::Path,
) -> Result<Scope, ScopeReadError> {
use std::os::unix::fs::MetadataExt;
let boot_id = std::fs::read_to_string(boot_id_path).map_err(|source| ScopeReadError {
path: boot_id_path.display().to_string(),
source,
})?;
let boot_id = boot_id.trim();
if boot_id.is_empty() {
return Err(ScopeReadError {
path: boot_id_path.display().to_string(),
source: std::io::Error::new(std::io::ErrorKind::InvalidData, "boot_id is empty"),
});
}
let pid_ns = std::fs::metadata(pid_ns_path)
.map_err(|source| ScopeReadError {
path: pid_ns_path.display().to_string(),
source,
})?
.ino();
Ok(Scope {
boot_id: Some(boot_id.to_owned()),
pid_ns: Some(pid_ns),
})
}