use std::collections::HashMap;
use std::sync::OnceLock;
use arrayvec::ArrayString;
fn clock_ticks_per_sec() -> i64 {
static TICKS: OnceLock<i64> = OnceLock::new();
*TICKS.get_or_init(|| {
let ticks = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
if ticks <= 0 { 100 } else { ticks }
})
}
pub fn read_proc_comm(pid: u32) -> Option<String> {
let path = proc_path(pid, "comm");
let mut buf = [0u8; 64];
let mut file = std::fs::File::open(path.as_str()).ok()?;
use std::io::Read;
let n = file.read(&mut buf).ok()?;
let s = std::str::from_utf8(&buf[..n]).ok()?;
Some(s.trim().to_string())
}
fn proc_path(pid: u32, suffix: &str) -> ArrayString<32> {
use std::fmt::Write;
let mut buf = ArrayString::new();
write!(buf, "/proc/{pid}/{suffix}").unwrap();
buf
}
pub fn read_proc_start_time_ns(pid: u32) -> u64 {
let path = proc_path(pid, "stat");
let stat = match std::fs::read_to_string(path.as_str()) {
Ok(s) => s,
Err(_) => return 0,
};
let after_comm = match stat.rfind(") ") {
Some(pos) => pos + 2,
None => return 0,
};
let mut rest = &stat[after_comm..];
for _ in 0..19 {
if let Some(pos) = rest.find(' ') {
rest = &rest[pos + 1..];
} else {
return 0;
}
}
let starttime_jiffies: u64 = match rest.split_whitespace().next() {
Some(s) => s.parse().unwrap_or(0),
None => return 0,
};
if starttime_jiffies == 0 {
return 0;
}
(starttime_jiffies as u128 * 1_000_000_000 / clock_ticks_per_sec() as u128) as u64
}
fn uid_passwd_map() -> &'static HashMap<u32, String> {
static MAP: OnceLock<HashMap<u32, String>> = OnceLock::new();
MAP.get_or_init(|| {
let mut map = HashMap::new();
if let Ok(passwd) = std::fs::read_to_string("/etc/passwd") {
for entry in passwd.lines() {
let mut parts = entry.splitn(4, ':');
let name = parts.next();
let _shell = parts.next(); let uid_str = parts.next();
if let (Some(name), Some(uid_str)) = (name, uid_str)
&& let Ok(uid) = uid_str.parse::<u32>()
{
map.insert(uid, name.to_string());
}
}
}
map
})
}
pub fn parse_proc_entry(pid: u32) -> Option<(crate::types::PidNode, crate::types::ProcInfo)> {
let path = proc_path(pid, "status");
let status = std::fs::read_to_string(path.as_str()).ok()?;
let mut ppid = 0u32;
let mut cmd = String::new();
let mut user = String::new();
let mut tgid = 0u32;
for line in status.lines() {
if let Some(val) = line.strip_prefix("PPid:") {
ppid = val.trim().parse().unwrap_or(0);
} else if let Some(val) = line.strip_prefix("Name:") {
cmd = val.trim().to_string();
} else if let Some(val) = line.strip_prefix("Uid:") {
if let Some(uid_str) = val.split_whitespace().next()
&& let Ok(uid) = uid_str.parse::<u32>()
{
user = uid_to_username(uid).unwrap_or_else(|| "unknown".to_string());
} else {
user = "unknown".to_string();
}
} else if let Some(val) = line.strip_prefix("Tgid:") {
tgid = val.trim().parse().unwrap_or(0);
}
}
let start_time_ns = read_proc_start_time_ns(pid);
Some((
crate::types::PidNode {
ppid,
cmd: cmd.clone(),
},
crate::types::ProcInfo {
cmd,
user,
ppid,
tgid,
start_time_ns,
},
))
}
pub fn uid_to_username(uid: u32) -> Option<String> {
uid_passwd_map().get(&uid).cloned()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_read_proc_comm_pid1() {
let comm = read_proc_comm(1);
assert!(comm.is_some(), "PID 1 should exist");
assert!(!comm.unwrap().is_empty());
}
#[test]
fn test_read_proc_comm_nonexistent() {
assert!(read_proc_comm(0x7FFFFFFF).is_none());
}
#[test]
fn test_read_proc_start_time_ns_pid1() {
let ns = read_proc_start_time_ns(1);
assert!(ns > 0, "PID 1 start_time_ns should be > 0, got {ns}");
}
#[test]
fn test_read_proc_start_time_ns_nonexistent() {
assert_eq!(read_proc_start_time_ns(0x7FFFFFFF), 0);
}
#[test]
fn test_uid_to_username_root() {
let name = uid_to_username(0);
assert_eq!(name.as_deref(), Some("root"));
}
#[test]
fn test_uid_to_username_nonexistent() {
assert!(uid_to_username(0xFFFFFFFF).is_none());
}
}