ksud: support common post-fs-data.d and service.d

This commit is contained in:
tiann
2023-02-02 08:39:19 +08:00
parent 2bba088319
commit cd825e34da
2 changed files with 70 additions and 8 deletions

View File

@@ -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);
}

View File

@@ -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!