ksud: support services. close #60

This commit is contained in:
tiann 2023-01-16 12:22:47 +08:00
parent 2222a999b9
commit f98066cb99
No known key found for this signature in database
GPG Key ID: 6D3F65FFD9559C06
3 changed files with 54 additions and 2 deletions

View File

@ -89,7 +89,7 @@ pub fn run() -> Result<()> {
} }
Commands::Install => event::install(), Commands::Install => event::install(),
Commands::Sepolicy => todo!(), Commands::Sepolicy => todo!(),
Commands::Services => todo!(), Commands::Services => event::on_services(),
}; };
if let Err(e) = &result { if let Err(e) = &result {

View File

@ -1,6 +1,9 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use crate::{defs, utils::{mount_image, ensure_clean_dir}}; use crate::{
defs,
utils::{ensure_clean_dir, mount_image},
};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use subprocess::Exec; use subprocess::Exec;
@ -102,6 +105,17 @@ pub fn on_post_data_fs() -> Result<()> {
Ok(()) Ok(())
} }
pub fn on_services() -> Result<()> {
// exec modules service.sh scripts
if !crate::utils::is_safe_mode().unwrap_or(false) {
let _ = crate::module::exec_services();
} else {
println!("safe mode, skip module post-fs-data scripts");
}
Ok(())
}
pub fn on_boot_completed() -> Result<()> { pub fn on_boot_completed() -> Result<()> {
let module_update_img = Path::new(defs::MODULE_UPDATE_IMG); let module_update_img = Path::new(defs::MODULE_UPDATE_IMG);
let module_img = Path::new(defs::MODULE_IMG); let module_img = Path::new(defs::MODULE_IMG);

View File

@ -190,6 +190,44 @@ pub fn exec_post_fs_data() -> Result<()> {
Ok(()) Ok(())
} }
/// execute every modules' service.sh
pub fn exec_services() -> Result<()> {
let modules_dir = Path::new(defs::MODULE_DIR);
let dir = std::fs::read_dir(modules_dir)?;
for entry in dir.flatten() {
let path = entry.path();
let disabled = path.join(defs::DISABLE_FILE_NAME);
if disabled.exists() {
println!("{} is disabled, skip", path.display());
continue;
}
let service = path.join("service.sh");
if !service.exists() {
continue;
}
println!("exec {} service.sh", path.display());
// pre_exec is unsafe!
unsafe {
Command::new("/system/bin/sh")
.arg(&service)
.process_group(0)
.pre_exec(|| {
// ignore the error?
let _ = switch_cgroups();
Ok(())
})
.current_dir(path)
.env("KSU", "true")
.spawn() // don't wait
.with_context(|| format!("Failed to exec {}", service.display()))?;
}
}
Ok(())
}
const RESETPROP: &[u8] = include_bytes!("./resetprop"); const RESETPROP: &[u8] = include_bytes!("./resetprop");
const RESETPROP_PATH: &str = concatcp!(defs::WORKING_DIR, "/resetprop"); const RESETPROP_PATH: &str = concatcp!(defs::WORKING_DIR, "/resetprop");