Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions userspace/ksud/src/metamodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ pub fn get_metamodule_path() -> Option<PathBuf> {
result
}

/// Get Metamodule Id
pub fn get_metamodule_id() -> Option<String> {
get_metamodule_path().and_then(|path| {
path.file_name()
.and_then(|os_str| os_str.to_str())
.map(ToString::to_string)
})
}

/// Check if metamodule exists
pub fn has_metamodule() -> bool {
get_metamodule_path().is_some()
Expand Down Expand Up @@ -227,7 +236,9 @@ pub fn exec_metauninstall_script(module_id: &str) -> Result<()> {
let result = Command::new(assets::BUSYBOX_PATH)
.args(["sh", metauninstall_path.to_str().unwrap()])
.current_dir(metauninstall_path.parent().unwrap())
.envs(crate::module::get_common_script_envs())
.envs(crate::module::get_common_script_envs(
get_metamodule_id().as_deref(),
))
Comment thread
AlexLiuDev233 marked this conversation as resolved.
.env("MODULE_ID", module_id)
.status()?;

Expand All @@ -250,7 +261,9 @@ pub fn exec_mount_script(module_dir: &str) -> Result<()> {

let result = Command::new(assets::BUSYBOX_PATH)
.args(["sh", mount_script.to_str().unwrap()])
.envs(crate::module::get_common_script_envs())
.envs(crate::module::get_common_script_envs(
get_metamodule_id().as_deref(),
))
Comment thread
AlexLiuDev233 marked this conversation as resolved.
.env("MODULE_DIR", module_dir)
.status()?;

Expand Down
25 changes: 14 additions & 11 deletions userspace/ksud/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use anyhow::{Context, Result, anyhow, bail, ensure};
use const_format::concatcp;
use is_executable::is_executable;
use java_properties::PropertiesIter;
use log::{debug, info, warn};
use log::{debug, error, info, warn};
use regex_lite::Regex;

use std::fs::{copy, rename};
Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn validate_module_id(module_id: &str) -> Result<()> {
}

/// Get common environment variables for script execution
pub fn get_common_script_envs() -> Vec<(&'static str, String)> {
pub fn get_common_script_envs(module_id: Option<&str>) -> Vec<(&'static str, String)> {
let mut envs = vec![
("ASH_STANDALONE", "1".to_string()),
("KSU", "true".to_string()),
Expand All @@ -74,14 +74,22 @@ pub fn get_common_script_envs() -> Vec<(&'static str, String)> {
),
];

if let Some(id) = module_id {
if validate_module_id(id).is_ok() {
envs.push(("KSU_MODULE", id.to_string()));
} else {
error!("Invalid module_id provided: {id}");
}
}
Comment thread
AlexLiuDev233 marked this conversation as resolved.

if ksucalls::is_late_load() {
envs.push(("KSU_LATE_LOAD", "1".to_string()));
}

envs
}

fn exec_install_script(module_file: &str, is_metamodule: bool) -> Result<()> {
fn exec_install_script(module_file: &str, is_metamodule: bool, module_id: &str) -> Result<()> {
let realpath = std::fs::canonicalize(module_file)
.with_context(|| format!("realpath: {module_file} failed"))?;

Expand All @@ -91,7 +99,7 @@ fn exec_install_script(module_file: &str, is_metamodule: bool) -> Result<()> {

let result = Command::new(assets::BUSYBOX_PATH)
.args(["sh", "-c", &install_script])
.envs(get_common_script_envs())
.envs(get_common_script_envs(Some(module_id)))
.env("OUTFD", "1")
.env("ZIPFILE", realpath)
.status()?;
Expand Down Expand Up @@ -224,12 +232,7 @@ pub fn exec_script<T: AsRef<Path>>(path: T, wait: bool) -> Result<()> {
.current_dir(path.as_ref().parent().unwrap())
.arg("sh")
.arg(path.as_ref())
.envs(get_common_script_envs());

// Set KSU_MODULE environment variable if module_id was validated successfully
if let Some(id) = validated_module_id {
command = command.env("KSU_MODULE", id);
}
.envs(get_common_script_envs(validated_module_id));

let result = if wait {
command.status().map(|_| ())
Expand Down Expand Up @@ -515,7 +518,7 @@ fn install_module_to_system(zip: &str) -> Result<()> {

// Execute install script
println!("- Running module installer");
exec_install_script(zip, is_metamodule)?;
exec_install_script(zip, is_metamodule, module_id)?;

let module_dir = Path::new(MODULE_DIR).join(module_id);
ensure_dir_exists(&module_dir)?;
Expand Down