diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/whatsys/src/apple.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/whatsys/src/apple.rs')
-rw-r--r-- | third_party/rust/whatsys/src/apple.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/third_party/rust/whatsys/src/apple.rs b/third_party/rust/whatsys/src/apple.rs new file mode 100644 index 0000000000..000fbe36cb --- /dev/null +++ b/third_party/rust/whatsys/src/apple.rs @@ -0,0 +1,61 @@ +/* Based on code from sysinfo: https://crates.io/crates/sysinfo + * Original licenses: MIT + * Original author: Guillaume Gomez + * License file: https://github.com/GuillaumeGomez/sysinfo/blob/master/LICENSE + */ + +use libc::c_int; + +fn get_system_info(value: c_int) -> Option<String> { + let mut mib: [c_int; 2] = [libc::CTL_KERN, value]; + let mut size = 0; + + // Call first to get size + unsafe { + libc::sysctl( + mib.as_mut_ptr(), + 2, + std::ptr::null_mut(), + &mut size, + std::ptr::null_mut(), + 0, + ) + }; + + // exit early if we did not update the size + if size == 0 { + return None; + } + + // set the buffer to the correct size + let mut buf = vec![0_u8; size as usize]; + + if unsafe { + libc::sysctl( + mib.as_mut_ptr(), + 2, + buf.as_mut_ptr() as _, + &mut size, + std::ptr::null_mut(), + 0, + ) + } == -1 + { + // If command fails return default + None + } else { + if let Some(pos) = buf.iter().position(|x| *x == 0) { + // Shrink buffer to terminate the null bytes + buf.resize(pos, 0); + } + + String::from_utf8(buf).ok() + } +} + +/// Get the version of the currently running kernel. +/// +/// Returns `None` if an error occured. +pub fn kernel_version() -> Option<String> { + get_system_info(libc::KERN_OSRELEASE) +} |