diff --git a/userspace/ksud/src/boot_patch.rs b/userspace/ksud/src/boot_patch.rs index c2661c68fc76..31a027970066 100644 --- a/userspace/ksud/src/boot_patch.rs +++ b/userspace/ksud/src/boot_patch.rs @@ -51,49 +51,21 @@ pub fn get_kernel_version() -> Result<(i32, i32, i32)> { } #[cfg(target_os = "android")] -fn parse_kmi(version: &str) -> Result { +fn parse_kmi() -> Result { use regex::Regex; + let uname = rustix::system::uname(); + let version = uname.release().to_string_lossy(); let re = Regex::new(r"(.* )?(\d+\.\d+)(\S+)?(android\d+)(.*)")?; let cap = re - .captures(version) + .captures(&version) .ok_or_else(|| anyhow::anyhow!("Failed to get KMI from boot/modules"))?; let android_version = cap.get(4).map_or("", |m| m.as_str()); let kernel_version = cap.get(2).map_or("", |m| m.as_str()); Ok(format!("{android_version}-{kernel_version}")) } -#[cfg(target_os = "android")] -fn parse_kmi_from_uname() -> Result { - let uname = rustix::system::uname(); - let version = uname.release().to_string_lossy(); - parse_kmi(&version) -} - -#[cfg(target_os = "android")] -fn parse_kmi_from_modules() -> Result { - use std::io::BufRead; - // find a *.ko in /vendor/lib/modules - let modfile = std::fs::read_dir("/vendor/lib/modules")? - .filter_map(Result::ok) - .find(|entry| entry.path().extension().map_or(false, |ext| ext == "ko")) - .map(|entry| entry.path()) - .ok_or_else(|| anyhow!("No kernel module found"))?; - let output = Command::new("modinfo").arg(modfile).output()?; - for line in output.stdout.lines().map_while(Result::ok) { - if line.starts_with("vermagic") { - return parse_kmi(&line); - } - } - anyhow::bail!("Parse KMI from modules failed") -} - -#[cfg(target_os = "android")] -pub fn get_current_kmi() -> Result { - parse_kmi_from_uname().or_else(|_| parse_kmi_from_modules()) -} - #[cfg(not(target_os = "android"))] -pub fn get_current_kmi() -> Result { +pub fn parse_kmi() -> Result { bail!("Unsupported platform") }