sys: add kernel_version() util to get the running kernel version

pull/1/head
Alessandro Decina 4 years ago
parent 8f5a17b542
commit 4bd0cde199

@ -4,9 +4,12 @@ mod perf_event;
#[cfg(test)] #[cfg(test)]
mod fake; mod fake;
use std::io; use std::{
ffi::{CStr, CString},
io, mem,
};
use libc::{c_int, c_long, c_ulong, pid_t}; use libc::{c_int, c_long, c_ulong, pid_t, utsname};
pub(crate) use bpf::*; pub(crate) use bpf::*;
#[cfg(test)] #[cfg(test)]
@ -69,3 +72,29 @@ unsafe fn syscall_impl(call: Syscall) -> SysResult {
Ok(ret) Ok(ret)
} }
pub(crate) fn kernel_version() -> Result<(u32, u32, u32), ()> {
unsafe {
let mut v = mem::zeroed::<utsname>();
if libc::uname(&mut v as *mut _) != 0 {
return Err(());
}
let mut major = 0u32;
let mut minor = 0u32;
let mut patch = 0u32;
let format = CString::new("%u.%u.%u").unwrap();
if libc::sscanf(
v.release.as_ptr(),
format.as_ptr(),
&mut major as *mut u32,
&mut minor as *mut _,
&mut patch as *mut _,
) != 3
{
return Err(());
}
Ok((major, minor, patch))
}
}

Loading…
Cancel
Save