From 85bf01eb65135cd6031567c4d53e8d04f9c5f5ae Mon Sep 17 00:00:00 2001 From: tiann Date: Wed, 1 Feb 2023 20:23:42 +0800 Subject: [PATCH] ksud: use logcat log --- userspace/ksud/Cargo.lock | 19 ++++++++++++++++++ userspace/ksud/Cargo.toml | 1 + userspace/ksud/src/cli.rs | 18 +++++++++++++++-- userspace/ksud/src/event.rs | 39 ++++++++++++++++++++++-------------- userspace/ksud/src/module.rs | 21 ++++++++----------- 5 files changed, 68 insertions(+), 30 deletions(-) diff --git a/userspace/ksud/Cargo.lock b/userspace/ksud/Cargo.lock index b22639b7..410ca32a 100644 --- a/userspace/ksud/Cargo.lock +++ b/userspace/ksud/Cargo.lock @@ -35,6 +35,24 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7eb209b1518d6bb87b283c20095f5228ecda460da70b44f0802523dea6da04" +[[package]] +name = "android_log-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85965b6739a430150bdd138e2374a98af0c3ee0d030b3bb7fc3bddff58d0102e" + +[[package]] +name = "android_logger" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037f3e1da32ddba7770530e69258b742c15ad67bdf90e5f6b35f4b6db9a60eb7" +dependencies = [ + "android_log-sys", + "env_logger", + "log", + "once_cell", +] + [[package]] name = "anyhow" version = "1.0.68" @@ -601,6 +619,7 @@ name = "ksud" version = "0.1.0" dependencies = [ "android-properties", + "android_logger", "anyhow", "clap", "const_format", diff --git a/userspace/ksud/Cargo.toml b/userspace/ksud/Cargo.toml index bc184ebd..8dcb6e31 100644 --- a/userspace/ksud/Cargo.toml +++ b/userspace/ksud/Cargo.toml @@ -31,6 +31,7 @@ derive-new = "0.5" [target.'cfg(target_os = "android")'.dependencies] sys-mount = "2.0.1" android-properties = { version = "0.2.2", features = ["bionic-deprecated"] } +android_logger = "0.12" [profile.release] strip = true diff --git a/userspace/ksud/src/cli.rs b/userspace/ksud/src/cli.rs index 6d9d5558..6641a719 100644 --- a/userspace/ksud/src/cli.rs +++ b/userspace/ksud/src/cli.rs @@ -1,5 +1,8 @@ use anyhow::{Ok, Result}; use clap::Parser; +#[cfg(target_os="android")] +use android_logger::Config; +use log::LevelFilter; use crate::{apk_sign, debug, event, module}; @@ -117,15 +120,26 @@ enum Module { } pub fn run() -> Result<()> { + #[cfg(target_os="android")] + android_logger::init_once( + Config::default() + .with_max_level(LevelFilter::Trace) // limit log level + .with_tag("KernelSU") // logs will show under mytag tag + ); + + #[cfg(not(target_os="android"))] + env_logger::init(); + let cli = Args::parse(); + log::info!("command: {:?}", cli.command); + let result = match cli.command { Commands::Daemon => event::daemon(), Commands::PostFsData => event::on_post_data_fs(), Commands::BootCompleted => event::on_boot_completed(), Commands::Module { command } => { - env_logger::init(); match command { Module::Install { zip } => module::install_module(zip), @@ -138,7 +152,7 @@ pub fn run() -> Result<()> { Commands::Install => event::install(), Commands::Sepolicy { command } => match command { Sepolicy::Patch { sepolicy } => crate::sepolicy::live_patch(&sepolicy), - Sepolicy::Apply { file } => crate::sepolicy::apply_file(&file), + Sepolicy::Apply { file } => crate::sepolicy::apply_file(file), }, Commands::Services => event::on_services(), diff --git a/userspace/ksud/src/event.rs b/userspace/ksud/src/event.rs index a4009c24..a11fcad6 100644 --- a/userspace/ksud/src/event.rs +++ b/userspace/ksud/src/event.rs @@ -5,16 +5,17 @@ use crate::{ utils::{ensure_clean_dir, ensure_dir_exists}, }; use anyhow::{bail, Context, Result}; +use log::{info, warn}; fn mount_partition(partition: &str, lowerdir: &mut Vec) -> Result<()> { if lowerdir.is_empty() { - println!("partition: {partition} lowerdir is empty"); + warn!("partition: {partition} lowerdir is empty"); return Ok(()); } // if /partition is a symlink and linked to /system/partition, then we don't need to overlay it separately - if Path::new(&format!("/{}", partition)).read_link().is_ok() { - println!("partition: {} is a symlink", partition); + if Path::new(&format!("/{partition}")).read_link().is_ok() { + warn!("partition: {partition} is a symlink"); return Ok(()); } // add /partition as the lowerest dir @@ -22,7 +23,7 @@ fn mount_partition(partition: &str, lowerdir: &mut Vec) -> Result<()> { lowerdir.push(lowest_dir.clone()); let lowerdir = lowerdir.join(":"); - println!("partition: {partition} lowerdir: {lowerdir}"); + info!("partition: {partition} lowerdir: {lowerdir}"); mount::mount_overlay(&lowerdir, &lowest_dir) } @@ -49,13 +50,13 @@ pub fn do_systemless_mount(module_dir: &str) -> Result<()> { } let disabled = module.join(defs::DISABLE_FILE_NAME).exists(); if disabled { - println!("module: {} is disabled, ignore!", module.display()); + info!("module: {} is disabled, ignore!", module.display()); continue; } let module_system = Path::new(&module).join("system"); if !module_system.as_path().exists() { - println!("module: {} has no system overlay.", module.display()); + info!("module: {} has no system overlay.", module.display()); continue; } system_lowerdir.push(format!("{}", module_system.display())); @@ -74,11 +75,15 @@ pub fn do_systemless_mount(module_dir: &str) -> Result<()> { } // mount /system first - let _ = mount_partition("system", &mut system_lowerdir); + if let Err(e) = mount_partition("system", &mut system_lowerdir) { + warn!("mount system failed: {e}"); + } // mount other partitions for (k, mut v) in partition_lowerdir { - let _ = mount_partition(&k, &mut v); + if let Err(e) = mount_partition(&k, &mut v) { + warn!("mount {k} failed: {e}"); + } } Ok(()) @@ -118,26 +123,30 @@ pub fn on_post_data_fs() -> Result<()> { return Ok(()); } - println!("mount {} to {}", target_update_img, module_dir); + info!("mount {target_update_img} to {module_dir}"); mount::mount_ext4(target_update_img, module_dir)?; // load sepolicy.rule if crate::module::load_sepolicy_rule().is_err() { - println!("load sepolicy.rule failed"); + warn!("load sepolicy.rule failed"); } // mount systemless overlay if let Err(e) = do_systemless_mount(module_dir) { - println!("do systemless mount failed: {}", e); + warn!("do systemless mount failed: {}", e); } // module mounted, exec modules post-fs-data scripts if !crate::utils::is_safe_mode() { // todo: Add timeout - let _ = crate::module::exec_post_fs_data(); - let _ = crate::module::load_system_prop(); + if let Err(e) = crate::module::exec_post_fs_data() { + warn!("exec post-fs-data scripts failed: {}", e); + } + if let Err(e) = crate::module::load_system_prop() { + warn!("load system.prop failed: {}", e); + } } else { - println!("safe mode, skip module post-fs-data scripts"); + warn!("safe mode, skip module post-fs-data scripts"); } Ok(()) @@ -148,7 +157,7 @@ pub fn on_services() -> Result<()> { if !crate::utils::is_safe_mode() { let _ = crate::module::exec_services(); } else { - println!("safe mode, skip module service scripts"); + warn!("safe mode, skip module service scripts"); } Ok(()) diff --git a/userspace/ksud/src/module.rs b/userspace/ksud/src/module.rs index e4154655..36d6c97e 100644 --- a/userspace/ksud/src/module.rs +++ b/userspace/ksud/src/module.rs @@ -204,7 +204,7 @@ pub fn exec_post_fs_data() -> Result<()> { let path = entry.path(); let disabled = path.join(defs::DISABLE_FILE_NAME); if disabled.exists() { - println!("{} is disabled, skip", path.display()); + warn!("{} is disabled, skip", path.display()); continue; } @@ -212,7 +212,7 @@ pub fn exec_post_fs_data() -> Result<()> { if !post_fs_data.exists() { continue; } - println!("exec {} post-fs-data.sh", path.display()); + info!("exec {} post-fs-data.sh", path.display()); let mut command_new; let mut command; @@ -255,7 +255,7 @@ pub fn exec_services() -> Result<()> { let path = entry.path(); let disabled = path.join(defs::DISABLE_FILE_NAME); if disabled.exists() { - println!("{} is disabled, skip", path.display()); + warn!("{} is disabled, skip", path.display()); continue; } @@ -263,7 +263,7 @@ pub fn exec_services() -> Result<()> { if !service.exists() { continue; } - println!("exec {} service.sh", path.display()); + info!("exec {} service.sh", path.display()); let mut command_new; let mut command; @@ -305,7 +305,7 @@ pub fn load_system_prop() -> Result<()> { let path = entry.path(); let disabled = path.join(defs::DISABLE_FILE_NAME); if disabled.exists() { - println!("{} is disabled, skip", path.display()); + info!("{} is disabled, skip", path.display()); continue; } @@ -313,7 +313,7 @@ pub fn load_system_prop() -> Result<()> { if !system_prop.exists() { continue; } - println!("load {} system.prop", path.display()); + info!("load {} system.prop", path.display()); // resetprop -n --file system.prop Command::new(assets::RESETPROP_PATH) @@ -355,7 +355,6 @@ fn do_install_module(zip: String) -> Result<()> { let Some(module_id) = module_prop.get("id") else { bail!("module id not found in module.prop!"); }; - info!("module id: {}", module_id); let modules_img = Path::new(defs::MODULE_IMG); let modules_update_img = Path::new(defs::MODULE_UPDATE_IMG); @@ -532,9 +531,7 @@ where pub fn uninstall_module(id: String) -> Result<()> { do_module_update(defs::MODULE_UPDATE_TMP_DIR, &id, |mid, update_dir| { let dir = Path::new(update_dir); - if !dir.exists() { - bail!("No module installed"); - } + ensure!(dir.exists(), "No module installed"); // iterate the modules_update dir, find the module to be removed let dir = std::fs::read_dir(dir)?; @@ -574,9 +571,7 @@ pub fn uninstall_module(id: String) -> Result<()> { fn do_enable_module(module_dir: &str, mid: &str, enable: bool) -> Result<()> { let src_module_path = format!("{module_dir}/{mid}"); let src_module = Path::new(&src_module_path); - if !src_module.exists() { - bail!("module: {} not found!", mid); - } + ensure!(src_module.exists(), "module: {} not found!", mid); let disable_path = src_module.join(defs::DISABLE_FILE_NAME); if enable {