feat: Adding a KPM monitor to handle KPM file creation and deletion events

This commit is contained in:
ShirkNeko
2025-04-07 21:18:14 +08:00
parent 1b700fb8e0
commit de089b7b73
3 changed files with 67 additions and 0 deletions

View File

@@ -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(())
}

57
userspace/ksud/src/kpm.rs Normal file
View File

@@ -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(())
}