feat: Adding a KPM monitor to handle KPM file creation and deletion events
This commit is contained in:
@@ -6,6 +6,7 @@ edition = "2024"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
notify = "6.1"
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
clap = { version = "4", features = ["derive"] }
|
clap = { version = "4", features = ["derive"] }
|
||||||
const_format = "0.2"
|
const_format = "0.2"
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ use std::path::Path;
|
|||||||
pub fn on_post_data_fs() -> Result<()> {
|
pub fn on_post_data_fs() -> Result<()> {
|
||||||
ksucalls::report_post_fs_data();
|
ksucalls::report_post_fs_data();
|
||||||
|
|
||||||
|
kpm::start_kpm_watcher()?;
|
||||||
|
|
||||||
utils::umask(0);
|
utils::umask(0);
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
@@ -98,6 +100,13 @@ pub fn on_post_data_fs() -> Result<()> {
|
|||||||
|
|
||||||
run_stage("post-mount", true);
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
57
userspace/ksud/src/kpm.rs
Normal file
57
userspace/ksud/src/kpm.rs
Normal 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(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user