use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;
pub fn foreground_process_name(child_pid: u32) -> Option<String> {
if child_pid == 0 {
return None;
}
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;
}
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())
}
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;
}
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])))
}