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
use std::io;
use std::mem;
use std::ptr;
fn list_pids(r#type: u32, typeinfo: u32) -> io::Result<Vec<libc::pid_t>> {
let size = unsafe {
darwin_libproc_sys::proc_listpids(r#type, typeinfo, ptr::null_mut(), 0)
};
if size <= 0 {
return Err(io::Error::last_os_error());
}
let capacity = size as usize / mem::size_of::<libc::pid_t>();
let mut buffer: Vec<libc::pid_t> = Vec::with_capacity(capacity);
let result = unsafe {
darwin_libproc_sys::proc_listpids(
r#type,
typeinfo,
buffer.as_mut_ptr() as *mut libc::c_void,
size,
)
};
if result <= 0 {
return Err(io::Error::last_os_error());
}
let pids_count = result as usize / mem::size_of::<libc::pid_t>();
unsafe {
buffer.set_len(pids_count);
}
Ok(buffer)
}
pub fn all_pids() -> io::Result<Vec<libc::pid_t>> {
list_pids(darwin_libproc_sys::PROC_ALL_PIDS, 0)
}
pub fn pgrp_only_pids(pgrpid: libc::pid_t) -> io::Result<Vec<libc::pid_t>> {
list_pids(darwin_libproc_sys::PROC_PGRP_ONLY, pgrpid as u32)
}
pub fn tty_only_pids(tty: libc::c_int) -> io::Result<Vec<libc::pid_t>> {
list_pids(darwin_libproc_sys::PROC_TTY_ONLY, tty as u32)
}
pub fn uid_only_pids(uid: libc::uid_t) -> io::Result<Vec<libc::pid_t>> {
list_pids(darwin_libproc_sys::PROC_UID_ONLY, uid)
}
pub fn ruid_only_pids(ruid: libc::uid_t) -> io::Result<Vec<libc::pid_t>> {
list_pids(darwin_libproc_sys::PROC_RUID_ONLY, ruid)
}
pub fn ppid_only_pids(ppid: libc::pid_t) -> io::Result<Vec<libc::pid_t>> {
list_pids(darwin_libproc_sys::PROC_PPID_ONLY, ppid as u32)
}