From 3aac979caaf55af50be95d7bc4f2b94b0b86aa84 Mon Sep 17 00:00:00 2001 From: weishu Date: Tue, 23 Apr 2024 15:32:15 +0800 Subject: [PATCH] ksud: skip removing module if no modules exist, also add uninstall mangaer, reboot --- userspace/ksud/src/boot_patch.rs | 4 ++-- userspace/ksud/src/cli.rs | 4 ++-- userspace/ksud/src/utils.rs | 27 +++++++++++++++++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/userspace/ksud/src/boot_patch.rs b/userspace/ksud/src/boot_patch.rs index f605e79a..0162b601 100644 --- a/userspace/ksud/src/boot_patch.rs +++ b/userspace/ksud/src/boot_patch.rs @@ -189,10 +189,10 @@ pub fn restore( new_boot = Some(PathBuf::from(backup_path)); from_backup = true; } else { - println!("Warning: no backup {KSU_BACKUP_DIR}/{KSU_BACKUP_FILE_PREFIX}{sha} found!"); + println!("- Warning: no backup {KSU_BACKUP_DIR}/{KSU_BACKUP_FILE_PREFIX}{sha} found!"); } } else { - println!("Warning: no backup found!"); + println!("- Cannot found backup image!"); } if new_boot.is_none() { diff --git a/userspace/ksud/src/cli.rs b/userspace/ksud/src/cli.rs index 17b81537..3bc620c1 100644 --- a/userspace/ksud/src/cli.rs +++ b/userspace/ksud/src/cli.rs @@ -41,7 +41,7 @@ enum Commands { Uninstall { /// magiskboot path, if not specified, will search from $PATH #[arg(long, default_value = None)] - magiskboot_path: Option, + magiskboot: Option, }, /// SELinux policy Patch tool @@ -308,7 +308,7 @@ pub fn run() -> Result<()> { } } Commands::Install => utils::install(), - Commands::Uninstall { magiskboot_path } => utils::uninstall(magiskboot_path), + Commands::Uninstall { magiskboot } => utils::uninstall(magiskboot), Commands::Sepolicy { command } => match command { Sepolicy::Patch { sepolicy } => crate::sepolicy::live_patch(&sepolicy), Sepolicy::Apply { file } => crate::sepolicy::apply_file(file), diff --git a/userspace/ksud/src/utils.rs b/userspace/ksud/src/utils.rs index 7d9db4ba..9da92588 100644 --- a/userspace/ksud/src/utils.rs +++ b/userspace/ksud/src/utils.rs @@ -1,8 +1,12 @@ use anyhow::{bail, Context, Error, Ok, Result}; use std::{ fs::{create_dir_all, remove_file, write, File, OpenOptions}, - io::{ErrorKind::AlreadyExists, ErrorKind::NotFound, Write}, + io::{ + ErrorKind::{AlreadyExists, NotFound}, + Write, + }, path::Path, + process::Command, }; use crate::{assets, boot_patch, defs, ksucalls, module, restorecon}; @@ -218,13 +222,24 @@ pub fn install() -> Result<()> { } pub fn uninstall(magiskboot_path: Option) -> Result<()> { - println!("- Uninstall modules.."); - module::uninstall_all_modules()?; - module::prune_modules()?; + if Path::new(defs::MODULE_DIR).exists() { + println!("- Uninstall modules.."); + module::uninstall_all_modules()?; + module::prune_modules()?; + } println!("- Removing directories.."); std::fs::remove_dir_all(defs::WORKING_DIR)?; - println!("- Uninstall KernelSU itself.."); - boot_patch::restore(None, magiskboot_path, true) + std::fs::remove_file(defs::DAEMON_PATH)?; + println!("- Restore boot image.."); + boot_patch::restore(None, magiskboot_path, true)?; + println!("- Uninstall KernelSU manager.."); + Command::new("pm") + .args(["uninstall", "me.weishu.kernelsu"]) + .spawn()?; + println!("- Rebooting in 5 seconds.."); + std::thread::sleep(std::time::Duration::from_secs(5)); + Command::new("reboot").spawn()?; + Ok(()) } // TODO: use libxcp to improve the speed if cross's MSRV is 1.70