blob: 001ef03ee8476755acebf3a94fa3ed14cb629f08 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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()
}
|