Debian Codegraph @

third_party/rust/whatsys/src/linux.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 /// Get the version of the currently running kernel.
8 ///
9 /// **Note**: The kernel version might include arbitrary alphanumeric suffixes.
10 ///
11 /// Returns `None` if an error occured.
12 pub fn kernel_version() -> Option<String> {
13 let mut raw = std::mem::MaybeUninit::<libc::utsname>::zeroed();
14
15 unsafe {
16 // SAFETY: We created the pointer from a value on the stack.
17 if libc::uname(raw.as_mut_ptr()) == 0 {
18 // SAFETY: If `uname` succesfully returns it filled in the data.
19 let info = raw.assume_init();
20
21 let release = info
22 .release
23 .iter()
24 .filter(|c| **c != 0)
25 .map(|c| *c as u8 as char)
26 .collect::<String>();
27
28 Some(release)
29 } else {
30 None
31 }
32 }
33 }