From de089b7b735be62101f8a85e1bffed0ff968320a Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Mon, 7 Apr 2025 21:18:14 +0800 Subject: [PATCH] feat: Adding a KPM monitor to handle KPM file creation and deletion events --- userspace/ksud/Cargo.toml | 1 + userspace/ksud/src/init_event.rs | 9 +++++ userspace/ksud/src/kpm.rs | 57 ++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 userspace/ksud/src/kpm.rs diff --git a/userspace/ksud/Cargo.toml b/userspace/ksud/Cargo.toml index 5c1d1ab2..b307df45 100644 --- a/userspace/ksud/Cargo.toml +++ b/userspace/ksud/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +notify = "6.1" anyhow = "1" clap = { version = "4", features = ["derive"] } const_format = "0.2" diff --git a/userspace/ksud/src/init_event.rs b/userspace/ksud/src/init_event.rs index dcd27fab..960ad4f2 100644 --- a/userspace/ksud/src/init_event.rs +++ b/userspace/ksud/src/init_event.rs @@ -9,6 +9,8 @@ use std::path::Path; pub fn on_post_data_fs() -> Result<()> { ksucalls::report_post_fs_data(); + kpm::start_kpm_watcher()?; + utils::umask(0); #[cfg(unix)] @@ -98,6 +100,13 @@ pub fn on_post_data_fs() -> Result<()> { run_stage("post-mount", true); + for entry in std::fs::read_dir(kpm::KPM_DIR)? { + let path = entry?.path(); + if path.extension().map_or(false, |ext| ext == "kpm") { + let _ = kpm::load_kpm(&path); + } + } + Ok(()) } diff --git a/userspace/ksud/src/kpm.rs b/userspace/ksud/src/kpm.rs new file mode 100644 index 00000000..f1c07462 --- /dev/null +++ b/userspace/ksud/src/kpm.rs @@ -0,0 +1,57 @@ +use anyhow::Result; +use notify::{Watcher, RecommendedWatcher, RecursiveMode, EventKind}; +use std::path::Path; + +const KPM_DIR: &str = "/data/adb/kpm"; + +pub fn start_kpm_watcher() -> Result<()> { + let mut watcher = notify::recommended_watcher(|res| { + match res { + Ok(event) => handle_kpm_event(event), + Err(e) => log::error!("watch error: {:?}", e), + } + })?; + + watcher.watch(Path::new(KPM_DIR), RecursiveMode::NonRecursive)?; + Ok(()) +} + +fn handle_kpm_event(event: notify::Event) { + if event.kind.is_create() { + event.paths.iter().for_each(|path| { + if path.extension().map_or(false, |ext| ext == "kpm") { + let _ = load_kpm(path); + } + }); + } + + if event.kind.is_remove() { + event.paths.iter().for_each(|path| { + if let Some(name) = path.file_stem() { + let _ = unload_kpm(name.to_string_lossy().as_ref()); + } + }); + } +} + +fn load_kpm(path: &Path) -> Result<()> { + let status = std::process::Command::new("/data/adb/ksu/bin/kpmmgr") + .args(["load", path.to_str().unwrap(), ""]) + .status()?; + + if status.success() { + log::info!("Loaded KPM: {}", path.display()); + } + Ok(()) +} + +fn unload_kpm(name: &str) -> Result<()> { + let status = std::process::Command::new("/data/adb/ksu/bin/kpmmgr") + .args(["unload", name]) + .status()?; + + if status.success() { + log::info!("Unloaded KPM: {}", name); + } + Ok(()) +} \ No newline at end of file