ksud: refine mount

- `magic_mount` now takes the tmp_path from its argument
- `.notmpfs` and `.nomount` now behave the same

skip:
ksud: cache tmp path (74ce44de21963fddc83897846eed28eca89d78aa)

Co-authored-by: shatyuka <shatyuka@qq.com>
Signed-off-by: Faris <rsuntk@yukiprjkt.my.id>
This commit is contained in:
5ec1cff
2025-10-19 03:00:52 +07:00
committed by ShirkNeko
parent cc0a3590ce
commit b1564b77a2
3 changed files with 16 additions and 15 deletions

View File

@@ -7,6 +7,7 @@ use crate::{
module::{handle_updated_modules, prune_modules},
restorecon, uid_scanner, utils,
};
use crate::utils::find_tmp_path;
use anyhow::{Context, Result};
use log::{info, warn};
use rustix::fs::{MountFlags, mount};
@@ -86,11 +87,15 @@ pub fn on_post_data_fs() -> Result<()> {
warn!("KPM: Failed to load KPM modules: {}", e);
}
let tmpfs_path = find_tmp_path();
// for compatibility
let no_mount = Path::new(NO_TMPFS_PATH).exists() || Path::new(NO_MOUNT_PATH).exists();
// mount temp dir
if !Path::new(NO_TMPFS_PATH).exists() {
if !no_mount {
if let Err(e) = mount(
KSU_MOUNT_SOURCE,
utils::get_tmp_path(),
&tmpfs_path,
"tmpfs",
MountFlags::empty(),
"",
@@ -113,9 +118,10 @@ pub fn on_post_data_fs() -> Result<()> {
}
// mount module systemlessly by magic mount
if !Path::new(NO_MOUNT_PATH).exists() {
if let Err(e) = mount_modules_systemlessly() {
warn!("do systemless mount failed: {}", e);
#[cfg(target_os = "android")]
if !no_mount {
if let Err(e) = crate::magic_mount::magic_mount(&tmpfs_path) {
warn!("do systemless mount failed: {e}");
}
} else {
info!("no mount requested");
@@ -148,7 +154,7 @@ pub fn on_post_data_fs() -> Result<()> {
#[cfg(target_os = "android")]
pub fn mount_modules_systemlessly() -> Result<()> {
crate::magic_mount::magic_mount()
crate::magic_mount::magic_mount(&find_tmp_path())
}
#[cfg(not(target_os = "android"))]

View File

@@ -21,7 +21,7 @@ use crate::{
defs::{DISABLE_FILE_NAME, KSU_MOUNT_SOURCE, MODULE_DIR, SKIP_MOUNT_FILE_NAME},
magic_mount::NodeFileType::{Directory, RegularFile, Symlink, Whiteout},
restorecon::{lgetfilecon, lsetfilecon},
utils::{ensure_dir_exists, get_work_dir},
utils::get_work_dir,
};
const REPLACE_DIR_XATTR: &str = "trusted.overlay.opaque";
@@ -435,10 +435,10 @@ fn do_magic_mount<P: AsRef<Path>, WP: AsRef<Path>>(
Ok(())
}
pub fn magic_mount() -> Result<()> {
pub fn magic_mount(tmp_path: &String) -> Result<()> {
if let Some(root) = collect_module_files()? {
log::debug!("collected: {:#?}", root);
let tmp_dir = PathBuf::from(get_work_dir());
let tmp_dir = Path::new(tmp_path).join("workdir");
ensure_dir_exists(&tmp_dir)?;
mount(KSU_MOUNT_SOURCE, &tmp_dir, "tmpfs", MountFlags::empty(), "").context("mount tmp")?;
mount_change(&tmp_dir, MountPropagationFlags::PRIVATE).context("make tmp private")?;

View File

@@ -183,7 +183,7 @@ fn is_ok_empty(dir: &str) -> bool {
}
}
pub fn get_tmp_path() -> String {
pub fn find_tmp_path() -> String {
let dirs = ["/debug_ramdisk", "/patch_hw", "/oem", "/root", "/sbin"];
// find empty directory
@@ -195,11 +195,6 @@ pub fn get_tmp_path() -> String {
"".to_string()
}
pub fn get_work_dir() -> String {
let tmp_path = get_tmp_path();
format!("{}/workdir/", tmp_path)
}
#[cfg(target_os = "android")]
fn link_ksud_to_bin() -> Result<()> {
let ksu_bin = PathBuf::from(defs::DAEMON_PATH);