diff options
Diffstat (limited to 'src/platform/linux.rs')
| -rw-r--r-- | src/platform/linux.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/platform/linux.rs b/src/platform/linux.rs new file mode 100644 index 00000000..001ef03e --- /dev/null +++ b/src/platform/linux.rs @@ -0,0 +1,27 @@ +use std::path::PathBuf; + +/// Get the foreground process name for a given child PID. +/// Uses /proc/<pid>/stat to find the tpgid, then /proc/<tpgid>/comm. +pub fn foreground_process_name(child_pid: u32) -> Option<String> { + // /proc/<pid>/stat format: "pid (comm) state ppid pgrp session tty_nr tpgid ..." + // The (comm) field can contain spaces and parens, so we find the last ')' first. + let stat = std::fs::read_to_string(format!("/proc/{child_pid}/stat")).ok()?; + let rest = stat.get(stat.rfind(')')? + 2..)?; + let fields: Vec<&str> = rest.split_whitespace().collect(); + // After (comm): state(0) ppid(1) pgrp(2) session(3) tty_nr(4) tpgid(5) + let tpgid: i32 = fields.get(5)?.parse().ok()?; + if tpgid <= 0 { + return None; + } + let comm = std::fs::read_to_string(format!("/proc/{tpgid}/comm")).ok()?; + Some(comm.trim().to_string()) +} + +/// Get the current working directory of a process. +/// Uses /proc/<pid>/cwd symlink. +pub fn process_cwd(pid: u32) -> Option<PathBuf> { + if pid == 0 { + return None; + } + std::fs::read_link(format!("/proc/{pid}/cwd")).ok() +} |
