diff --git a/userspace/ksud/src/cli.rs b/userspace/ksud/src/cli.rs index 117a2d87..17b81537 100644 --- a/userspace/ksud/src/cli.rs +++ b/userspace/ksud/src/cli.rs @@ -37,6 +37,13 @@ enum Commands { /// Install KernelSU userspace component to system Install, + /// Uninstall KernelSU modules and itself(LKM Only) + Uninstall { + /// magiskboot path, if not specified, will search from $PATH + #[arg(long, default_value = None)] + magiskboot_path: Option, + }, + /// SELinux policy Patch tool Sepolicy { #[command(subcommand)] @@ -301,6 +308,7 @@ pub fn run() -> Result<()> { } } Commands::Install => utils::install(), + Commands::Uninstall { magiskboot_path } => utils::uninstall(magiskboot_path), 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/module.rs b/userspace/ksud/src/module.rs index ccb5367f..ac3ff213 100644 --- a/userspace/ksud/src/module.rs +++ b/userspace/ksud/src/module.rs @@ -604,13 +604,21 @@ pub fn disable_module(id: &str) -> Result<()> { } pub fn disable_all_modules() -> Result<()> { + mark_all_modules(defs::DISABLE_FILE_NAME) +} + +pub fn uninstall_all_modules() -> Result<()> { + mark_all_modules(defs::REMOVE_FILE_NAME) +} + +fn mark_all_modules(flag_file: &str) -> Result<()> { // we assume the module dir is already mounted let dir = std::fs::read_dir(defs::MODULE_DIR)?; for entry in dir.flatten() { let path = entry.path(); - let disable_flag = path.join(defs::DISABLE_FILE_NAME); - if let Err(e) = ensure_file_exists(disable_flag) { - warn!("Failed to disable module: {}: {}", path.display(), e); + let flag = path.join(flag_file); + if let Err(e) = ensure_file_exists(flag) { + warn!("Failed to mark module: {}: {}", path.display(), e); } } diff --git a/userspace/ksud/src/utils.rs b/userspace/ksud/src/utils.rs index 1c8d66bf..7d9db4ba 100644 --- a/userspace/ksud/src/utils.rs +++ b/userspace/ksud/src/utils.rs @@ -5,7 +5,7 @@ use std::{ path::Path, }; -use crate::{assets, defs, ksucalls, restorecon}; +use crate::{assets, boot_patch, defs, ksucalls, module, restorecon}; use std::fs::metadata; #[allow(unused_imports)] use std::fs::{set_permissions, Permissions}; @@ -217,6 +217,16 @@ pub fn install() -> Result<()> { Ok(()) } +pub fn uninstall(magiskboot_path: Option) -> Result<()> { + 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) +} + // TODO: use libxcp to improve the speed if cross's MSRV is 1.70 pub fn copy_sparse_file, Q: AsRef>( src: P,