From cd825e34da9ce0c02c2258bd4b305494a5141da3 Mon Sep 17 00:00:00 2001 From: tiann Date: Thu, 2 Feb 2023 08:39:19 +0800 Subject: [PATCH] ksud: support common post-fs-data.d and service.d --- userspace/ksud/src/event.rs | 6 +++ userspace/ksud/src/module.rs | 72 ++++++++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/userspace/ksud/src/event.rs b/userspace/ksud/src/event.rs index f9e593bd..632e8dc1 100644 --- a/userspace/ksud/src/event.rs +++ b/userspace/ksud/src/event.rs @@ -139,6 +139,9 @@ pub fn on_post_data_fs() -> Result<()> { // module mounted, exec modules post-fs-data scripts if !crate::utils::is_safe_mode() { // todo: Add timeout + if let Err(e) = crate::module::exec_common_scripts("post-fs-data.d", true) { + warn!("exec common post-fs-data scripts failed: {}", e); + } if let Err(e) = crate::module::exec_post_fs_data() { warn!("exec post-fs-data scripts failed: {}", e); } @@ -155,6 +158,9 @@ pub fn on_post_data_fs() -> Result<()> { pub fn on_services() -> Result<()> { // exec modules service.sh scripts if !crate::utils::is_safe_mode() { + if let Err(e) = crate::module::exec_common_scripts("service.d", false) { + warn!("exec common service scripts failed: {}", e); + } if let Err(e) = crate::module::exec_services() { warn!("exec service scripts failed: {}", e); } diff --git a/userspace/ksud/src/module.rs b/userspace/ksud/src/module.rs index 25929914..5e30325c 100644 --- a/userspace/ksud/src/module.rs +++ b/userspace/ksud/src/module.rs @@ -1,15 +1,13 @@ use crate::{ - defs, + assets, defs, mount, restorecon::{restore_syscon, setsyscon}, sepolicy, utils::*, - assets, - mount, }; use const_format::concatcp; use java_properties::PropertiesIter; -use log::{info, warn, debug}; +use log::{debug, info, warn}; use std::{ collections::HashMap, env::var as env_var, @@ -217,7 +215,10 @@ pub fn exec_post_fs_data() -> Result<()> { let mut command_new; let mut command; if !is_executable(&post_fs_data) { - debug!("{} is not executable, use /system/bin/sh!", post_fs_data.display()); + debug!( + "{} is not executable, use /system/bin/sh!", + post_fs_data.display() + ); command_new = Command::new("sh"); command = command_new.arg(&post_fs_data); } else { @@ -249,6 +250,59 @@ pub fn exec_post_fs_data() -> Result<()> { Ok(()) } +pub fn exec_common_scripts(dir: &str, wait: bool) -> Result<()> { + let script_dir = Path::new(defs::WORKING_DIR).join(dir); + if !script_dir.exists() { + info!("{} not exists, skip", script_dir.display()); + return Ok(()); + } + + let dir = std::fs::read_dir(&script_dir)?; + for entry in dir.flatten() { + let path = entry.path(); + if !path.ends_with(".sh") { + warn!("{} is not a shell script, skip", path.display()); + continue; + } + + if !is_executable(&path) { + warn!("{} is not executable, skip", path.display()); + continue; + } + + info!("exec {}", path.display()); + + let mut command = Command::new(&path); + let command = command + .process_group(0) + .current_dir(&script_dir) + .env( + "PATH", + format!("{}:{}", env_var("PATH").unwrap(), defs::BINARY_DIR), + ) + .env("KSU", "true"); + + let command = unsafe { + command.pre_exec(|| { + switch_cgroups(); + Ok(()) + }) + }; + + if !wait { + command + .spawn() // don't wait + .with_context(|| format!("Failed to exec {}", path.display()))?; + } else { + command + .status() + .with_context(|| format!("Failed to exec {}", path.display()))?; + } + } + + Ok(()) +} + /// execute every modules' service.sh pub fn exec_services() -> Result<()> { let modules_dir = Path::new(defs::MODULE_DIR); @@ -270,7 +324,10 @@ pub fn exec_services() -> Result<()> { let mut command_new; let mut command; if !is_executable(&service) { - debug!("{} is not executable, use /system/bin/sh!", service.display()); + debug!( + "{} is not executable, use /system/bin/sh!", + service.display() + ); command_new = Command::new("sh"); command = command_new.arg(&service); } else { @@ -302,7 +359,6 @@ pub fn exec_services() -> Result<()> { } pub fn load_system_prop() -> Result<()> { - let modules_dir = Path::new(defs::MODULE_DIR); let dir = std::fs::read_dir(modules_dir)?; for entry in dir.flatten() { @@ -474,7 +530,7 @@ fn do_install_module(zip: String) -> Result<()> { Ok(()) } -pub fn install_module(zip: String) -> Result<()> { +pub fn install_module(zip: String) -> Result<()> { let result = do_install_module(zip); if result.is_err() { // error happened, do some cleanup!