diff --git a/userspace/ksud/src/boot_patch.rs b/userspace/ksud/src/boot_patch.rs index 55d0a65e..2a218407 100644 --- a/userspace/ksud/src/boot_patch.rs +++ b/userspace/ksud/src/boot_patch.rs @@ -168,7 +168,7 @@ fn do_cpio_cmd(magiskboot: &Path, workdir: &Path, cmd: &str) -> Result<()> { } fn do_vendor_init_boot_cpio_cmd(magiskboot: &Path, workdir: &Path, cmd: &str) -> Result<()> { - let vendor_init_boot_cpio = workdir.join("vendor_ramdisk").join("init_boot.cpio"); + let vendor_init_boot_cpio = workdir.join("vendor_ramdisk").join("init_boot.cpio"); let status = Command::new(magiskboot) .current_dir(workdir) .stdout(Stdio::null()) @@ -200,11 +200,7 @@ fn is_magisk_patched_vendor_init_boot(magiskboot: &Path, workdir: &Path) -> Resu .current_dir(workdir) .stdout(Stdio::null()) .stderr(Stdio::null()) - .args([ - "cpio", - vendor_init_boot_cpio.to_str().unwrap(), - "test", - ]) + .args(["cpio", vendor_init_boot_cpio.to_str().unwrap(), "test"]) .status()?; // 0: stock, 1: magisk @@ -284,8 +280,12 @@ pub fn restore( let no_ramdisk = !workdir.join("ramdisk.cpio").exists(); let is_kernelsu_patched = is_kernelsu_patched(&magiskboot, workdir)?; - let is_kernelsu_patched_vendor_init_boot = is_kernelsu_patched_vendor_init_boot(&magiskboot, workdir)?; - ensure!(is_kernelsu_patched || is_kernelsu_patched_vendor_init_boot, "boot image is not patched by KernelSU"); + let is_kernelsu_patched_vendor_init_boot = + is_kernelsu_patched_vendor_init_boot(&magiskboot, workdir)?; + ensure!( + is_kernelsu_patched || is_kernelsu_patched_vendor_init_boot, + "boot image is not patched by KernelSU" + ); let mut new_boot = None; let mut from_backup = false; @@ -321,12 +321,13 @@ pub fn restore( // vendor init_boot restore do_vendor_init_boot_cpio_cmd(&magiskboot, workdir, "rm kernelsu.ko")?; // if init.real exists, restore it - let status = do_vendor_init_boot_cpio_cmd(&magiskboot, workdir, "exists init.real").is_ok(); + let status = + do_vendor_init_boot_cpio_cmd(&magiskboot, workdir, "exists init.real").is_ok(); if status { do_vendor_init_boot_cpio_cmd(&magiskboot, workdir, "mv init.real init")?; } else { - let vendor_init_boot = workdir.join("vendor_ramdisk").join("init_boot.cpio"); - std::fs::remove_file(vendor_init_boot )?; + let vendor_init_boot = workdir.join("vendor_ramdisk").join("init_boot.cpio"); + std::fs::remove_file(vendor_init_boot)?; } } else { // remove kernelsu.ko @@ -524,7 +525,8 @@ fn do_patch( bail!("No compatible ramdisk found."); } let is_magisk_patched = is_magisk_patched(&magiskboot, workdir)?; - let is_magisk_patched_vendor_init_boot = is_magisk_patched_vendor_init_boot(&magiskboot, workdir)?; + let is_magisk_patched_vendor_init_boot = + is_magisk_patched_vendor_init_boot(&magiskboot, workdir)?; ensure!( !is_magisk_patched || !is_magisk_patched_vendor_init_boot, "Cannot work with Magisk patched image" @@ -532,7 +534,8 @@ fn do_patch( println!("- Adding KernelSU LKM"); let is_kernelsu_patched = is_kernelsu_patched(&magiskboot, workdir)?; - let is_kernelsu_patched_vendor_init_boot = is_kernelsu_patched_vendor_init_boot(&magiskboot, workdir)?; + let is_kernelsu_patched_vendor_init_boot = + is_kernelsu_patched_vendor_init_boot(&magiskboot, workdir)?; let mut need_backup = false; if !is_kernelsu_patched || (no_ramdisk && !is_kernelsu_patched_vendor_init_boot) { @@ -740,7 +743,6 @@ fn find_boot_image( Path::new(&format!("/dev/block/by-name/init_boot{slot_suffix}")).exists(); let vendor_boot_exist = Path::new(&format!("/dev/block/by-name/vendor_boot{slot_suffix}")).exists(); - let boot_partition = if !is_replace_kernel && init_boot_exist && !skip_init { format!("/dev/block/by-name/init_boot{slot_suffix}") } else if !is_replace_kernel && vendor_boot_exist && !skip_init { diff --git a/userspace/ksud/src/init_event.rs b/userspace/ksud/src/init_event.rs index 68aafb6f..7d0e9915 100644 --- a/userspace/ksud/src/init_event.rs +++ b/userspace/ksud/src/init_event.rs @@ -1,11 +1,11 @@ use crate::defs::{KSU_MOUNT_SOURCE, NO_MOUNT_PATH, NO_TMPFS_PATH}; +use crate::kpm; use crate::module::{handle_updated_modules, prune_modules}; use crate::{assets, defs, ksucalls, restorecon, utils}; use anyhow::{Context, Result}; use log::{info, warn}; use rustix::fs::{MountFlags, mount}; use std::path::Path; -use crate::kpm; pub fn on_post_data_fs() -> Result<()> { ksucalls::report_post_fs_data(); @@ -72,7 +72,13 @@ pub fn on_post_data_fs() -> Result<()> { // mount temp dir if !Path::new(NO_TMPFS_PATH).exists() { - if let Err(e) = mount(KSU_MOUNT_SOURCE, utils::get_tmp_path(), "tmpfs", MountFlags::empty(), "") { + if let Err(e) = mount( + KSU_MOUNT_SOURCE, + utils::get_tmp_path(), + "tmpfs", + MountFlags::empty(), + "", + ) { warn!("do temp dir mount failed: {}", e); } } else { diff --git a/userspace/ksud/src/kpm.rs b/userspace/ksud/src/kpm.rs index 92121258..7a8c80a9 100644 --- a/userspace/ksud/src/kpm.rs +++ b/userspace/ksud/src/kpm.rs @@ -1,9 +1,9 @@ use anyhow::Result; -use notify::{Watcher, RecursiveMode}; -use std::path::Path; -use std::fs; use anyhow::anyhow; +use notify::{RecursiveMode, Watcher}; use std::ffi::OsStr; +use std::fs; +use std::path::Path; pub const KPM_DIR: &str = "/data/adb/kpm"; pub const KPMMGR_PATH: &str = "/data/adb/ksu/bin/kpmmgr"; @@ -28,11 +28,9 @@ 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), - } + let mut watcher = notify::recommended_watcher(|res| match res { + Ok(event) => handle_kpm_event(event), + Err(e) => log::error!("monitoring error: {:?}", e), })?; watcher.watch(Path::new(KPM_DIR), RecursiveMode::NonRecursive)?; @@ -80,7 +78,9 @@ fn handle_modify_event(paths: Vec) { // 加载 KPM 模块 pub fn load_kpm(path: &Path) -> Result<()> { - let path_str = path.to_str().ok_or_else(|| anyhow!("Invalid path: {}", path.display()))?; + let path_str = path + .to_str() + .ok_or_else(|| anyhow!("Invalid path: {}", path.display()))?; let status = std::process::Command::new(KPMMGR_PATH) .args(["load", path_str, ""]) .status()?; @@ -168,7 +168,6 @@ pub fn load_kpm_modules() -> Result<()> { } } } - if path.extension().is_some_and(|ext| ext == "kpm") { match load_kpm(&path) { Ok(()) => log::info!("Successfully loaded KPM module: {}", path.display()), @@ -178,4 +177,4 @@ pub fn load_kpm_modules() -> Result<()> { } Ok(()) -} \ No newline at end of file +} diff --git a/userspace/ksud/src/magic_mount.rs b/userspace/ksud/src/magic_mount.rs index 5a36dd7a..b4799df6 100644 --- a/userspace/ksud/src/magic_mount.rs +++ b/userspace/ksud/src/magic_mount.rs @@ -1,6 +1,4 @@ -use crate::defs::{ - DISABLE_FILE_NAME, KSU_MOUNT_SOURCE, MODULE_DIR, SKIP_MOUNT_FILE_NAME, -}; +use crate::defs::{DISABLE_FILE_NAME, KSU_MOUNT_SOURCE, MODULE_DIR, SKIP_MOUNT_FILE_NAME}; use crate::magic_mount::NodeFileType::{Directory, RegularFile, Symlink, Whiteout}; use crate::restorecon::{lgetfilecon, lsetfilecon}; use crate::utils::{ensure_dir_exists, get_work_dir}; diff --git a/userspace/ksud/src/main.rs b/userspace/ksud/src/main.rs index e0543d73..4d183907 100644 --- a/userspace/ksud/src/main.rs +++ b/userspace/ksud/src/main.rs @@ -5,6 +5,7 @@ mod cli; mod debug; mod defs; mod init_event; +mod kpm; mod ksucalls; #[cfg(target_os = "android")] mod magic_mount; @@ -14,7 +15,6 @@ mod restorecon; mod sepolicy; mod su; mod utils; -mod kpm; fn main() -> anyhow::Result<()> { cli::run() diff --git a/userspace/ksud/src/utils.rs b/userspace/ksud/src/utils.rs index d6a9da26..a1602bef 100644 --- a/userspace/ksud/src/utils.rs +++ b/userspace/ksud/src/utils.rs @@ -187,13 +187,7 @@ fn is_ok_empty(dir: &str) -> bool { } pub fn get_tmp_path() -> String { - let dirs = [ - "/debug_ramdisk", - "/patch_hw", - "/oem", - "/root", - "/sbin", - ]; + let dirs = ["/debug_ramdisk", "/patch_hw", "/oem", "/root", "/sbin"]; // find empty directory for dir in dirs { @@ -205,8 +199,8 @@ pub fn get_tmp_path() -> String { } pub fn get_work_dir() -> String { - let tmp_path = get_tmp_path(); - format!("{}/workdir/", tmp_path) + let tmp_path = get_tmp_path(); + format!("{}/workdir/", tmp_path) } #[cfg(target_os = "android")]