Skip to content

Commit b28789a

Browse files
backslashxxpowellnormarsuntk
authored
ksud: probe for more workdir candidates (#12)
- reuses old ksu functions - makes sure its an empty dir - adapted from rsuntk@141f010 tiann@71cb86c Co-authored-by: powellnorma <[email protected]> Co-authored-by: Rissu <[email protected]>
1 parent 9ff996b commit b28789a

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

userspace/ksud/src/defs.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ pub const MODULE_UPDATE_DIR: &str = concatcp!(ADB_DIR, "modules_update/");
2424

2525
pub const KSUD_VERBOSE_LOG_FILE: &str = concatcp!(ADB_DIR, "verbose");
2626

27-
pub const TEMP_DIR: &str = "/debug_ramdisk";
2827
pub const MODULE_WEB_DIR: &str = "webroot";
2928
pub const MODULE_ACTION_SH: &str = "action.sh";
3029
pub const DISABLE_FILE_NAME: &str = "disable";
3130
pub const UPDATE_FILE_NAME: &str = "update";
3231
pub const REMOVE_FILE_NAME: &str = "remove";
3332
pub const SKIP_MOUNT_FILE_NAME: &str = "skip_mount";
34-
pub const MAGIC_MOUNT_WORK_DIR: &str = concatcp!(TEMP_DIR, "/workdir");
3533

3634
pub const VERSION_CODE: &str = include_str!(concat!(env!("OUT_DIR"), "/VERSION_CODE"));
3735
pub const VERSION_NAME: &str = include_str!(concat!(env!("OUT_DIR"), "/VERSION_NAME"));

userspace/ksud/src/init_event.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::defs::{KSU_MOUNT_SOURCE, NO_MOUNT_PATH, NO_TMPFS_PATH, TEMP_DIR};
1+
use crate::defs::{KSU_MOUNT_SOURCE, NO_MOUNT_PATH, NO_TMPFS_PATH};
22
use crate::module::{handle_updated_modules, prune_modules};
33
use crate::{assets, defs, ksucalls, restorecon, utils};
44
use anyhow::{Context, Result};
@@ -69,7 +69,7 @@ pub fn on_post_data_fs() -> Result<()> {
6969

7070
// mount temp dir
7171
if !Path::new(NO_TMPFS_PATH).exists() {
72-
if let Err(e) = mount(KSU_MOUNT_SOURCE, TEMP_DIR, "tmpfs", MountFlags::empty(), "") {
72+
if let Err(e) = mount(KSU_MOUNT_SOURCE, utils::get_tmp_path(), "tmpfs", MountFlags::empty(), "") {
7373
warn!("do temp dir mount failed: {}", e);
7474
}
7575
} else {

userspace/ksud/src/magic_mount.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::defs::{
2-
DISABLE_FILE_NAME, KSU_MOUNT_SOURCE, MAGIC_MOUNT_WORK_DIR, MODULE_DIR, SKIP_MOUNT_FILE_NAME,
2+
DISABLE_FILE_NAME, KSU_MOUNT_SOURCE, MODULE_DIR, SKIP_MOUNT_FILE_NAME,
33
};
44
use crate::magic_mount::NodeFileType::{Directory, RegularFile, Symlink, Whiteout};
55
use crate::restorecon::{lgetfilecon, lsetfilecon};
6-
use crate::utils::ensure_dir_exists;
6+
use crate::utils::{ensure_dir_exists, get_work_dir};
77
use anyhow::{Context, Result, bail};
88
use extattr::lgetxattr;
99
use rustix::fs::{
@@ -417,7 +417,7 @@ fn do_magic_mount<P: AsRef<Path>, WP: AsRef<Path>>(
417417
pub fn magic_mount() -> Result<()> {
418418
if let Some(root) = collect_module_files()? {
419419
log::debug!("collected: {:#?}", root);
420-
let tmp_dir = PathBuf::from(MAGIC_MOUNT_WORK_DIR);
420+
let tmp_dir = PathBuf::from(get_work_dir());
421421
ensure_dir_exists(&tmp_dir)?;
422422
mount(KSU_MOUNT_SOURCE, &tmp_dir, "tmpfs", MountFlags::empty(), "").context("mount tmp")?;
423423
mount_change(&tmp_dir, MountPropagationFlags::PRIVATE).context("make tmp private")?;

userspace/ksud/src/utils.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{Context, Error, Ok, Result, bail};
22
use std::{
3-
fs::{File, OpenOptions, create_dir_all, remove_file, write},
3+
fs::{self, File, OpenOptions, create_dir_all, remove_file, write},
44
io::{
55
ErrorKind::{AlreadyExists, NotFound},
66
Write,
@@ -177,6 +177,38 @@ pub fn has_magisk() -> bool {
177177
which::which("magisk").is_ok()
178178
}
179179

180+
fn is_ok_empty(dir: &str) -> bool {
181+
use std::result::Result::Ok;
182+
183+
match fs::read_dir(dir) {
184+
Ok(mut entries) => entries.next().is_none(),
185+
Err(_) => false,
186+
}
187+
}
188+
189+
pub fn get_tmp_path() -> String {
190+
let dirs = [
191+
"/debug_ramdisk",
192+
"/patch_hw",
193+
"/oem",
194+
"/root",
195+
"/sbin",
196+
];
197+
198+
// find empty directory
199+
for dir in dirs {
200+
if is_ok_empty(dir) {
201+
return dir.to_string();
202+
}
203+
}
204+
"".to_string()
205+
}
206+
207+
pub fn get_work_dir() -> String {
208+
let tmp_path = get_tmp_path();
209+
format!("{}/workdir/", tmp_path)
210+
}
211+
180212
#[cfg(target_os = "android")]
181213
fn link_ksud_to_bin() -> Result<()> {
182214
let ksu_bin = PathBuf::from(defs::DAEMON_PATH);

0 commit comments

Comments
 (0)