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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;
/// Get the foreground process name for a given child PID.
///
/// Uses `proc_pidinfo(PROC_PIDTBSDINFO)` to read `e_tpgid` — the foreground
/// process group ID of the child's controlling terminal. Then `proc_name()`
/// to resolve that PID to a short command name.
///
/// This is the macOS equivalent of Linux's `/proc/{pid}/stat` → tpgid → `/proc/{tpgid}/comm`.
pub fn foreground_process_name(child_pid: u32) -> Option<String> {
if child_pid == 0 {
return None;
}
// Get BSD info for the child process — contains e_tpgid
let mut info: libc::proc_bsdinfo = unsafe { std::mem::zeroed() };
let size = std::mem::size_of::<libc::proc_bsdinfo>() as libc::c_int;
let ret = unsafe {
libc::proc_pidinfo(
child_pid as libc::c_int,
libc::PROC_PIDTBSDINFO,
0,
&mut info as *mut _ as *mut libc::c_void,
size,
)
};
if ret != size {
return None;
}
let fg_pid = info.e_tpgid;
if fg_pid == 0 {
return None;
}
// Resolve the foreground PID to a process name
let mut buf = [0u8; libc::MAXCOMLEN + 1];
let len = unsafe {
libc::proc_name(
fg_pid as libc::c_int,
buf.as_mut_ptr() as *mut libc::c_void,
buf.len() as u32,
)
};
if len <= 0 {
return None;
}
let len = len as usize;
let end = buf[..len].iter().position(|&b| b == 0).unwrap_or(len);
Some(String::from_utf8_lossy(&buf[..end]).into_owned())
}
/// Get the current working directory of a process.
///
/// Uses `proc_pidinfo(PROC_PIDVNODEPATHINFO)` to read `pvi_cdir.vip_path`.
pub fn process_cwd(pid: u32) -> Option<PathBuf> {
if pid == 0 {
return None;
}
let mut pathinfo: libc::proc_vnodepathinfo = unsafe { std::mem::zeroed() };
let size = std::mem::size_of::<libc::proc_vnodepathinfo>() as libc::c_int;
let ret = unsafe {
libc::proc_pidinfo(
pid as libc::c_int,
libc::PROC_PIDVNODEPATHINFO,
0,
&mut pathinfo as *mut _ as *mut libc::c_void,
size,
)
};
if ret != size {
return None;
}
// vip_path is [[c_char; 32]; 32] in libc (workaround for old Rust const generics).
// Reinterpret as flat bytes (total MAXPATHLEN = 1024).
let vip_path = unsafe {
std::slice::from_raw_parts(
pathinfo.pvi_cdir.vip_path.as_ptr() as *const u8,
libc::MAXPATHLEN as usize,
)
};
let nul = vip_path.iter().position(|&b| b == 0)?;
if nul == 0 {
return None;
}
Some(PathBuf::from(OsStr::from_bytes(&vip_path[..nul])))
}
|