mprober_lib/kernel/
mod.rs

1use std::io::ErrorKind;
2
3use crate::scanner_rust::{generic_array::typenum::U48, ScannerAscii, ScannerError};
4
5/// Get the kernel version by reading the `/proc/version` file.
6///
7/// ```rust
8/// use mprober_lib::kernel;
9///
10/// let kernel_version = kernel::get_kernel_version().unwrap();
11///
12/// println!("{kernel_version}");
13/// ```
14#[inline]
15pub fn get_kernel_version() -> Result<String, ScannerError> {
16    let mut sc: ScannerAscii<_, U48> = ScannerAscii::scan_path2("/proc/version")?;
17
18    sc.drop_next_bytes(14)?.ok_or(ErrorKind::UnexpectedEof)?;
19
20    let v = sc.next_raw()?.ok_or(ErrorKind::UnexpectedEof)?;
21
22    Ok(unsafe { String::from_utf8_unchecked(v) })
23}