Debian Codegraph @

third_party/rust/whatsys/src/apple.rs

raw
1 /* Based on code from sysinfo: https://crates.io/crates/sysinfo
2 * Original licenses: MIT
3 * Original author: Guillaume Gomez
4 * License file: https://github.com/GuillaumeGomez/sysinfo/blob/master/LICENSE
5 */
6
7 use libc::c_int;
8
9 fn get_system_info(value: c_int) -> Option<String> {
10 let mut mib: [c_int; 2] = [libc::CTL_KERN, value];
11 let mut size = 0;
12
13 // Call first to get size
14 unsafe {
15 libc::sysctl(
16 mib.as_mut_ptr(),
17 2,
18 std::ptr::null_mut(),
19 &mut size,
20 std::ptr::null_mut(),
21 0,
22 )
23 };
24
25 // exit early if we did not update the size
26 if size == 0 {
27 return None;
28 }
29
30 // set the buffer to the correct size
31 let mut buf = vec![0_u8; size as usize];
32
33 if unsafe {
34 libc::sysctl(
35 mib.as_mut_ptr(),
36 2,
37 buf.as_mut_ptr() as _,
38 &mut size,
39 std::ptr::null_mut(),
40 0,
41 )
42 } == -1
43 {
44 // If command fails return default
45 None
46 } else {
47 if let Some(pos) = buf.iter().position(|x| *x == 0) {
48 // Shrink buffer to terminate the null bytes
49 buf.resize(pos, 0);
50 }
51
52 String::from_utf8(buf).ok()
53 }
54 }
55
56 /// Get the version of the currently running kernel.
57 ///
58 /// Returns `None` if an error occured.
59 pub fn kernel_version() -> Option<String> {
60 get_system_info(libc::KERN_OSRELEASE)
61 }