use std::ffi::{CStr, c_void};
use std::mem::{MaybeUninit, size_of};
unsafe extern "C" {
pub fn mach_host_self() -> libc::mach_port_t;
}
pub type ResponsibleFn = unsafe extern "C" fn(libc::pid_t) -> libc::pid_t;
pub fn responsible_fn() -> Option<ResponsibleFn> {
let name = c"responsibility_get_pid_responsible_for_pid";
let sym = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) };
if sym.is_null() {
return None;
}
Some(unsafe { std::mem::transmute::<*mut c_void, ResponsibleFn>(sym) })
}
pub fn sysctl<T: Copy>(name: &CStr) -> Option<T> {
let mut value = MaybeUninit::<T>::uninit();
let mut len = size_of::<T>();
let rc = unsafe {
libc::sysctlbyname(
name.as_ptr(),
value.as_mut_ptr().cast(),
&mut len,
std::ptr::null_mut(),
0,
)
};
(rc == 0 && len == size_of::<T>()).then(|| unsafe { value.assume_init() })
}