darwin_libproc/
pid_path.rs

1use std::ffi::OsString;
2use std::io;
3use std::os::unix::ffi::OsStringExt;
4use std::path::PathBuf;
5
6/// Fetch executable absolute path for process with `pid` provided.
7pub fn pid_path(pid: libc::pid_t) -> io::Result<PathBuf> {
8    let mut buf: Vec<u8> =
9        Vec::with_capacity(darwin_libproc_sys::PROC_PIDPATHINFO_MAXSIZE);
10
11    let result = unsafe {
12        darwin_libproc_sys::proc_pidpath(
13            pid,
14            buf.as_mut_ptr() as *mut libc::c_void,
15            buf.capacity() as u32,
16        )
17    };
18
19    if result <= 0 {
20        Err(io::Error::last_os_error())
21    } else {
22        unsafe {
23            buf.set_len(result as usize);
24        }
25
26        Ok(OsString::from_vec(buf).into())
27    }
28}