refactor(ksud): refactor ksud's kpm
- remove useless code - ignore create dir all return - when kpm file delete failed, return Error
This commit is contained in:
51
userspace/ksud/Cargo.lock
generated
51
userspace/ksud/Cargo.lock
generated
@@ -594,16 +594,6 @@ dependencies = [
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fs4"
|
||||
version = "0.13.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8640e34b88f7652208ce9e88b1a37a2ae95227d84abec377ccd3c5cfeb141ed4"
|
||||
dependencies = [
|
||||
"rustix 1.0.7",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fsevent-sys"
|
||||
version = "4.1.0"
|
||||
@@ -849,7 +839,6 @@ dependencies = [
|
||||
"encoding_rs",
|
||||
"env_logger",
|
||||
"extattr",
|
||||
"fs4",
|
||||
"getopts",
|
||||
"humansize",
|
||||
"is_executable",
|
||||
@@ -859,10 +848,10 @@ dependencies = [
|
||||
"log",
|
||||
"nom",
|
||||
"notify",
|
||||
"procfs",
|
||||
"regex-lite",
|
||||
"rust-embed",
|
||||
"rustix 0.38.34",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha1",
|
||||
"sha256",
|
||||
@@ -1085,31 +1074,6 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "procfs"
|
||||
version = "0.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc5b72d8145275d844d4b5f6d4e1eef00c8cd889edb6035c21675d1bb1f45c9f"
|
||||
dependencies = [
|
||||
"bitflags 2.8.0",
|
||||
"chrono",
|
||||
"flate2",
|
||||
"hex",
|
||||
"procfs-core",
|
||||
"rustix 0.38.44",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "procfs-core"
|
||||
version = "0.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "239df02d8349b06fc07398a3a1697b06418223b1c7725085e801e7c0fc6a12ec"
|
||||
dependencies = [
|
||||
"bitflags 2.8.0",
|
||||
"chrono",
|
||||
"hex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.40"
|
||||
@@ -1221,19 +1185,6 @@ dependencies = [
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154"
|
||||
dependencies = [
|
||||
"bitflags 2.8.0",
|
||||
"errno 0.3.10",
|
||||
"libc",
|
||||
"linux-raw-sys 0.4.15",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.0.7"
|
||||
|
||||
@@ -45,7 +45,6 @@ sha1 = "0.10"
|
||||
tempfile = "3"
|
||||
chrono = "0.4"
|
||||
regex-lite = "0.1"
|
||||
fs4 = "0.13"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
||||
[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
|
||||
@@ -54,7 +53,6 @@ rustix = { git = "https://github.com/Kernel-SU/rustix.git", branch = "main", fea
|
||||
] }
|
||||
# some android specific dependencies which compiles under unix are also listed here for convenience of coding
|
||||
android-properties = { version = "0.2", features = ["bionic-deprecated"] }
|
||||
procfs = "0.17"
|
||||
|
||||
[target.'cfg(target_os = "android")'.dependencies]
|
||||
android_logger = { version = "0.14", default-features = false }
|
||||
@@ -65,3 +63,4 @@ codegen-units = 1
|
||||
lto = "fat"
|
||||
opt-level = 3
|
||||
strip = true
|
||||
split-debuginfo = "unpacked"
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::{
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use anyhow::{Result, anyhow, bail};
|
||||
use anyhow::{Result, bail};
|
||||
use notify::{RecursiveMode, Watcher};
|
||||
|
||||
use crate::ksucalls::ksuctl;
|
||||
@@ -37,7 +37,6 @@ fn kpm_ioctl(cmd: &mut KsuKpmCmd) -> std::io::Result<()> {
|
||||
}
|
||||
|
||||
/// Convert raw kernel return code to `Result`.
|
||||
#[inline(always)]
|
||||
fn check_ret(rc: i32) -> Result<i32> {
|
||||
if rc < 0 {
|
||||
bail!("KPM error: {}", std::io::Error::from_raw_os_error(-rc));
|
||||
@@ -46,8 +45,11 @@ fn check_ret(rc: i32) -> Result<i32> {
|
||||
}
|
||||
|
||||
/// Load a `.kpm` into kernel space.
|
||||
pub fn kpm_load(path: &str, args: Option<&str>) -> Result<()> {
|
||||
let path_c = CString::new(path)?;
|
||||
pub fn kpm_load<P>(path: P, args: Option<&str>) -> Result<()>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let path_c = CString::new(path.as_ref().to_string_lossy().to_string())?;
|
||||
let args_c = args.map(CString::new).transpose()?;
|
||||
|
||||
let mut result: i32 = -1;
|
||||
@@ -193,10 +195,10 @@ pub fn check_kpm_version() -> Result<String> {
|
||||
|
||||
/// Create `/data/adb/kpm` with 0o777 if missing.
|
||||
pub fn ensure_kpm_dir() -> Result<()> {
|
||||
fs::create_dir_all(KPM_DIR)?;
|
||||
let _ = fs::create_dir_all(KPM_DIR);
|
||||
let meta = fs::metadata(KPM_DIR)?;
|
||||
|
||||
if meta.permissions().mode() & 0o777 != 0o777 {
|
||||
if meta.permissions().mode() != 0o777 {
|
||||
fs::set_permissions(KPM_DIR, fs::Permissions::from_mode(0o777))?;
|
||||
}
|
||||
Ok(())
|
||||
@@ -215,7 +217,7 @@ pub fn start_kpm_watcher() -> Result<()> {
|
||||
|
||||
let mut watcher = notify::recommended_watcher(|res: Result<_, _>| match res {
|
||||
Ok(evt) => handle_kpm_event(evt),
|
||||
Err(e) => log::error!("KPM: watcher error: {e:?}"),
|
||||
Err(e) => log::error!("KPM: watcher error: {e}"),
|
||||
})?;
|
||||
watcher.watch(Path::new(KPM_DIR), RecursiveMode::NonRecursive)?;
|
||||
log::info!("KPM: watcher active on {KPM_DIR}");
|
||||
@@ -227,7 +229,7 @@ fn handle_kpm_event(evt: notify::Event) {
|
||||
for p in evt.paths {
|
||||
if let Some(ex) = p.extension()
|
||||
&& ex == OsStr::new("kpm")
|
||||
&& load_kpm(&p).is_err()
|
||||
&& kpm_load(&p, None).is_err()
|
||||
{
|
||||
log::warn!("KPM: failed to load {}", p.display());
|
||||
}
|
||||
@@ -235,24 +237,6 @@ fn handle_kpm_event(evt: notify::Event) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Load single `.kpm` file.
|
||||
pub fn load_kpm(path: &Path) -> Result<()> {
|
||||
let s = path.to_str().ok_or_else(|| anyhow!("bad path"))?;
|
||||
kpm_load(s, None)
|
||||
}
|
||||
|
||||
/// Unload module and delete file.
|
||||
pub fn unload_kpm(name: &str) -> Result<()> {
|
||||
kpm_unload(name)?;
|
||||
|
||||
if let Some(p) = find_kpm_file(name)? {
|
||||
let _ = fs::remove_file(&p);
|
||||
log::info!("KPM: deleted {}", p.display());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Locate `/data/adb/kpm/<name>.kpm`.
|
||||
fn find_kpm_file(name: &str) -> Result<Option<PathBuf>> {
|
||||
let dir = Path::new(KPM_DIR);
|
||||
@@ -280,12 +264,25 @@ pub fn remove_all_kpms() -> Result<()> {
|
||||
if !dir.is_dir() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
for entry in fs::read_dir(dir)? {
|
||||
let p = entry?.path();
|
||||
if let Some(ex) = p.extension()
|
||||
&& ex == OsStr::new("kpm")
|
||||
&& let Some(name) = p.file_stem().and_then(|s| s.to_str())
|
||||
&& let Err(e) = unload_kpm(name)
|
||||
&& let Err(e) = (|| -> Result<()> {
|
||||
kpm_unload(name)?;
|
||||
|
||||
if let Some(p) = find_kpm_file(name)? {
|
||||
if let Err(e) = fs::remove_file(&p) {
|
||||
log::warn!("KPM: delete {} failed: {e}", p.display());
|
||||
return Err(e.into());
|
||||
}
|
||||
log::info!("KPM: deleted {}", p.display());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})()
|
||||
{
|
||||
log::error!("KPM: unload {name} failed: {e}");
|
||||
}
|
||||
@@ -310,7 +307,7 @@ pub fn load_kpm_modules() -> Result<()> {
|
||||
if let Some(ex) = p.extension()
|
||||
&& ex == OsStr::new("kpm")
|
||||
{
|
||||
match load_kpm(&p) {
|
||||
match kpm_load(&p, None) {
|
||||
Ok(_) => ok += 1,
|
||||
Err(e) => {
|
||||
log::warn!("KPM: load {} failed: {e}", p.display());
|
||||
|
||||
Reference in New Issue
Block a user