darwin_libproc/
name.rs

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