MCPcopy Create free account
hub / github.com/evilsocket/cake / sysctl_string

Function sysctl_string

cake-core/src/cake/sharding/discovery.rs:276–299  ·  view source on GitHub ↗
(name: &str)

Source from the content-addressed store, hash-verified

274#[cfg(any(target_os = "macos", target_os = "ios"))]
275#[allow(dead_code)]
276fn sysctl_string(name: &str) -> Option<String> {
277 use std::ffi::CString;
278 let c_name = CString::new(name).ok()?;
279 let mut len: usize = 0;
280 // First call to get buffer size
281 let ret = unsafe {
282 libc::sysctlbyname(c_name.as_ptr(), std::ptr::null_mut(), &mut len, std::ptr::null_mut(), 0)
283 };
284 if ret != 0 || len == 0 {
285 return None;
286 }
287 let mut buf = vec![0u8; len];
288 let ret = unsafe {
289 libc::sysctlbyname(c_name.as_ptr(), buf.as_mut_ptr() as *mut _, &mut len, std::ptr::null_mut(), 0)
290 };
291 if ret != 0 {
292 return None;
293 }
294 // Strip trailing null bytes
295 while buf.last() == Some(&0) {
296 buf.pop();
297 }
298 String::from_utf8(buf).ok().filter(|s| !s.is_empty())
299}
300
301/// Read a sysctl u64 value using the C API.
302#[cfg(any(target_os = "macos", target_os = "ios"))]

Callers 1

detect_apple_chipFunction · 0.85

Calls 2

as_ptrMethod · 0.80
as_mut_ptrMethod · 0.80

Tested by

no test coverage detected