ksud: Only start the monitor and load the module when KPM is enabled

Co-authored-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
Co-authored-by: mihoy3rd <159522651+mihoy3rd@users.noreply.github.com>
This commit is contained in:
ShirkNeko
2025-09-10 14:56:23 +08:00
parent d83fc2aec6
commit 3ed1d9aebc
2 changed files with 55 additions and 88 deletions

View File

@@ -10,8 +10,6 @@ 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)]
@@ -105,10 +103,12 @@ pub fn on_post_data_fs() -> Result<()> {
info!("no mount requested");
}
run_stage("post-mount", true);
// load kpm modules
if kpm::is_kpm_enabled()? {
kpm::start_kpm_watcher()?;
kpm::load_kpm_modules()?;
}
run_stage("post-mount", true);
Ok(())
}

View File

@@ -4,20 +4,19 @@ use notify::{RecursiveMode, Watcher};
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::process::Command;
pub const KPM_DIR: &str = "/data/adb/kpm";
pub const KPMMGR_PATH: &str = "/data/adb/ksu/bin/kpmmgr";
// 确保 KPM 目录存在,如果不存在则创建
pub fn ensure_kpm_dir() -> Result<()> {
if !Path::new(KPM_DIR).exists() {
fs::create_dir_all(KPM_DIR)?;
}
Ok(())
}
// 启动 KPM 监视器
pub fn start_kpm_watcher() -> Result<()> {
ensure_kpm_dir()?;
// 检查 KPM 是否启用
if !is_kpm_enabled()? {
log::warn!("KPM is not enabled. Disabling all KPM-related functionality.");
return Ok(());
}
// 检查是否处于安全模式
if crate::utils::is_safe_mode() {
@@ -28,6 +27,7 @@ pub fn start_kpm_watcher() -> Result<()> {
return Ok(());
}
// 启动文件系统监视器
let mut watcher = notify::recommended_watcher(|res| match res {
Ok(event) => handle_kpm_event(event),
Err(e) => log::error!("monitoring error: {:?}", e),
@@ -37,16 +37,40 @@ pub fn start_kpm_watcher() -> Result<()> {
Ok(())
}
// 检查 KPM 版本
pub fn is_kpm_enabled() -> Result<bool> {
// KPM 文件夹不存在则视为未启用
if !Path::new(KPM_DIR).exists() {
return Ok(false);
}
let output = Command::new(KPMMGR_PATH)
.args(["version"])
.output()?;
if output.status.success() {
let output_str = String::from_utf8_lossy(&output.stdout);
if output_str.to_lowercase().contains("error") {
Ok(false)
} else {
Ok(true)
}
} else {
Err(anyhow!("Failed to check KPM version: {:?}", output.stderr))
}
}
// 处理 KPM 事件
pub fn handle_kpm_event(event: notify::Event) {
fn handle_kpm_event(event: notify::Event) {
match event.kind {
notify::EventKind::Create(_) => handle_create_event(event.paths),
notify::EventKind::Remove(_) => handle_remove_event(event.paths),
notify::EventKind::Modify(_) => handle_modify_event(event.paths),
_ => {}
}
}
// 处理创建事件
fn handle_create_event(paths: Vec<std::path::PathBuf>) {
for path in paths {
if path.extension() == Some(OsStr::new("kpm")) {
@@ -57,106 +81,49 @@ fn handle_create_event(paths: Vec<std::path::PathBuf>) {
}
}
fn handle_remove_event(paths: Vec<std::path::PathBuf>) {
for path in paths {
if let Some(name) = path.file_stem().and_then(|s| s.to_str()) {
if let Err(e) = unload_kpm(name) {
log::warn!("Failed to unload {}: {}", name, e);
}
if let Err(e) = fs::remove_file(&path) {
log::error!("Failed to delete file: {}: {}", path.display(), e);
}
}
}
}
// 处理修改事件
fn handle_modify_event(paths: Vec<std::path::PathBuf>) {
for path in paths {
log::info!("Modified file: {}", path.display());
}
}
// 加载 KPM 模块
pub fn load_kpm(path: &Path) -> Result<()> {
// 加载单个 KPM 模块
fn load_kpm(path: &Path) -> Result<()> {
let path_str = path
.to_str()
.ok_or_else(|| anyhow!("Invalid path: {}", path.display()))?;
let status = std::process::Command::new(KPMMGR_PATH)
let status = Command::new(KPMMGR_PATH)
.args(["load", path_str, ""])
.status()?;
if status.success() {
log::info!("Loaded KPM: {}", path.display());
}
Ok(())
}
// 卸载 KPM 模块并尝试删除对应文件
pub fn unload_kpm(name: &str) -> Result<()> {
let status = std::process::Command::new(KPMMGR_PATH)
.args(["unload", name])
.status()
.map_err(|e| anyhow!("Failed to execute kpmmgr: {}", e))?;
if status.success() {
let kpm_path = find_kpm_file(name)?;
if let Some(path) = kpm_path {
fs::remove_file(&path)
.map_err(|e| anyhow!("Failed to delete KPM file: {}: {}", path.display(), e))?;
log::info!("Deleted KPM file: {}", path.display());
}
log::info!("Successfully unloaded KPM: {}", name);
} else {
log::warn!("KPM unloading may have failed: {}", name);
log::warn!("KPM unloading may have failed: {}", path.display());
}
Ok(())
}
// 通过名称查找 KPM 文件
fn find_kpm_file(name: &str) -> Result<Option<std::path::PathBuf>> {
let kpm_dir = Path::new(KPM_DIR);
if !kpm_dir.exists() {
return Ok(None);
}
for entry in fs::read_dir(kpm_dir)? {
let path = entry?.path();
if let Some(file_name) = path.file_stem() {
if let Some(file_name_str) = file_name.to_str() {
if file_name_str == name && path.extension() == Some(OsStr::new("kpm")) {
return Ok(Some(path));
}
}
}
}
Ok(None)
}
// 安全模式下删除所有 KPM 模块
pub fn remove_all_kpms() -> Result<()> {
ensure_kpm_dir()?;
fn remove_all_kpms() -> Result<()> {
for entry in fs::read_dir(KPM_DIR)? {
let path = entry?.path();
if path.extension().is_some_and(|ext| ext == "kpm") {
if let Some(name) = path.file_stem() {
if let Err(e) = unload_kpm(name.to_string_lossy().as_ref()) {
log::error!("Failed to remove KPM: {}", e);
}
if let Err(e) = fs::remove_file(&path) {
log::error!("Failed to delete file: {}: {}", path.display(), e);
}
}
}
}
Ok(())
}
// 加载 KPM 模块
// 遍历加载所有 KPM 模块
pub fn load_kpm_modules() -> Result<()> {
ensure_kpm_dir()?;
if !is_kpm_enabled()? {
log::warn!("KPM is not enabled. Disabling all KPM-related functionality.");
return Ok(());
}
for entry in std::fs::read_dir(KPM_DIR)? {
let path = entry?.path();