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
use std::io;
use std::mem;

fn pid_info<T>(
    pid: libc::pid_t,
    flavor: libc::c_int,
    arg: u64,
) -> io::Result<T> {
    let mut info = mem::MaybeUninit::<T>::uninit();
    let size = mem::size_of::<T>() as libc::c_int;

    let result = unsafe {
        darwin_libproc_sys::proc_pidinfo(
            pid,
            flavor,
            arg,
            info.as_mut_ptr() as *mut libc::c_void,
            size,
        )
    };

    match result {
        value if value <= 0 => Err(io::Error::last_os_error()),
        value if value != size => Err(io::Error::new(
            io::ErrorKind::Other,
            "invalid value returned",
        )),
        _ => unsafe { Ok(info.assume_init()) },
    }
}

/// Returns filled `proc_taskinfo` struct for `pid` given.
pub fn task_info(
    pid: libc::pid_t,
) -> io::Result<darwin_libproc_sys::proc_taskinfo> {
    pid_info(pid, darwin_libproc_sys::PROC_PIDTASKINFO, 0)
}

/// Returns filled `proc_taskallinfo` struct for `pid` given.
pub fn task_all_info(
    pid: libc::pid_t,
) -> io::Result<darwin_libproc_sys::proc_taskallinfo> {
    pid_info(pid, darwin_libproc_sys::PROC_PIDTASKALLINFO, 0)
}

/// Returns filled `proc_vnodepathinfo` struct for pid given.
pub fn vnode_path_info(
    pid: libc::pid_t,
) -> io::Result<darwin_libproc_sys::proc_vnodepathinfo> {
    pid_info(pid, darwin_libproc_sys::PROC_PIDVNODEPATHINFO, 0)
}