Skip to content

Commit cdb82a3

Browse files
committed
Revert "Drop some legacy codes (tiann#1981)"
This reverts commit fd09ccf.
1 parent 087559d commit cdb82a3

File tree

4 files changed

+122
-35
lines changed

4 files changed

+122
-35
lines changed

userspace/ksud/src/defs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub const MODULE_UPDATE_TMP_DIR: &str = concatcp!(ADB_DIR, "modules_update/");
2929
pub const SYSTEM_RW_DIR: &str = concatcp!(MODULE_DIR, ".rw/");
3030

3131
pub const TEMP_DIR: &str = "/debug_ramdisk";
32+
pub const TEMP_DIR_LEGACY: &str = "/sbin";
33+
3234
pub const MODULE_WEB_DIR: &str = "webroot";
3335
pub const DISABLE_FILE_NAME: &str = "disable";
3436
pub const UPDATE_FILE_NAME: &str = "update";

userspace/ksud/src/init_event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub fn on_post_data_fs() -> Result<()> {
191191
}
192192

193193
// mount temp dir
194-
if let Err(e) = mount::mount_tmpfs(defs::TEMP_DIR) {
194+
if let Err(e) = mount::mount_tmpfs(utils::get_tmp_path()) {
195195
warn!("do temp dir mount failed: {}", e);
196196
}
197197

userspace/ksud/src/mount.rs

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,21 @@ pub fn mount_ext4(source: impl AsRef<Path>, target: impl AsRef<Path>) -> Result<
6363
.attach(source)
6464
.with_context(|| "Failed to attach loop")?;
6565
let lo = new_loopback.path().ok_or(anyhow!("no loop"))?;
66-
let fs = fsopen("ext4", FsOpenFlags::FSOPEN_CLOEXEC)?;
67-
let fs = fs.as_fd();
68-
fsconfig_set_string(fs, "source", lo)?;
69-
fsconfig_create(fs)?;
70-
let mount = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?;
71-
move_mount(
72-
mount.as_fd(),
73-
"",
74-
CWD,
75-
target.as_ref(),
76-
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
77-
)?;
66+
if let Result::Ok(fs) = fsopen("ext4", FsOpenFlags::FSOPEN_CLOEXEC) {
67+
let fs = fs.as_fd();
68+
fsconfig_set_string(fs, "source", lo)?;
69+
fsconfig_create(fs)?;
70+
let mount = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?;
71+
move_mount(
72+
mount.as_fd(),
73+
"",
74+
CWD,
75+
target.as_ref(),
76+
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
77+
)?;
78+
} else {
79+
mount(lo, target.as_ref(), "ext4", MountFlags::empty(), "")?;
80+
}
7881
Ok(())
7982
}
8083

@@ -154,18 +157,27 @@ pub fn mount_overlayfs(
154157
#[cfg(any(target_os = "linux", target_os = "android"))]
155158
pub fn mount_tmpfs(dest: impl AsRef<Path>) -> Result<()> {
156159
info!("mount tmpfs on {}", dest.as_ref().display());
157-
let fs = fsopen("tmpfs", FsOpenFlags::FSOPEN_CLOEXEC)?;
158-
let fs = fs.as_fd();
159-
fsconfig_set_string(fs, "source", KSU_OVERLAY_SOURCE)?;
160-
fsconfig_create(fs)?;
161-
let mount = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?;
162-
move_mount(
163-
mount.as_fd(),
164-
"",
165-
CWD,
166-
dest.as_ref(),
167-
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
168-
)?;
160+
if let Result::Ok(fs) = fsopen("tmpfs", FsOpenFlags::FSOPEN_CLOEXEC) {
161+
let fs = fs.as_fd();
162+
fsconfig_set_string(fs, "source", KSU_OVERLAY_SOURCE)?;
163+
fsconfig_create(fs)?;
164+
let mount = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?;
165+
move_mount(
166+
mount.as_fd(),
167+
"",
168+
CWD,
169+
dest.as_ref(),
170+
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
171+
)?;
172+
} else {
173+
mount(
174+
KSU_OVERLAY_SOURCE,
175+
dest.as_ref(),
176+
"tmpfs",
177+
MountFlags::empty(),
178+
"",
179+
)?;
180+
}
169181
Ok(())
170182
}
171183

@@ -176,20 +188,29 @@ pub fn bind_mount(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
176188
from.as_ref().display(),
177189
to.as_ref().display()
178190
);
179-
let tree = open_tree(
191+
if let Result::Ok(tree) = open_tree(
180192
CWD,
181193
from.as_ref(),
182194
OpenTreeFlags::OPEN_TREE_CLOEXEC
183195
| OpenTreeFlags::OPEN_TREE_CLONE
184196
| OpenTreeFlags::AT_RECURSIVE,
185-
)?;
186-
move_mount(
187-
tree.as_fd(),
188-
"",
189-
CWD,
190-
to.as_ref(),
191-
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
192-
)?;
197+
) {
198+
move_mount(
199+
tree.as_fd(),
200+
"",
201+
CWD,
202+
to.as_ref(),
203+
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
204+
)?;
205+
} else {
206+
mount(
207+
from.as_ref(),
208+
to.as_ref(),
209+
"",
210+
MountFlags::BIND | MountFlags::REC,
211+
"",
212+
)?;
213+
}
193214
Ok(())
194215
}
195216

userspace/ksud/src/utils.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use anyhow::{bail, Context, Error, Ok, Result};
22
use std::{
3-
fs::{create_dir_all, remove_file, write, File, OpenOptions},
3+
fs::{self, create_dir_all, remove_file, write, File, OpenOptions},
44
io::{
55
ErrorKind::{AlreadyExists, NotFound},
66
Write,
77
},
88
path::Path,
99
process::Command,
10+
sync::OnceLock,
1011
};
1112

1213
use crate::{assets, boot_patch, defs, ksucalls, module, restorecon};
14+
use std::fs::metadata;
1315
#[allow(unused_imports)]
1416
use std::fs::{set_permissions, Permissions};
1517
#[cfg(unix)]
@@ -187,6 +189,68 @@ pub fn has_magisk() -> bool {
187189
which::which("magisk").is_ok()
188190
}
189191

192+
fn is_ok_empty(dir: &str) -> bool {
193+
use std::result::Result::Ok;
194+
195+
match fs::read_dir(dir) {
196+
Ok(mut entries) => entries.next().is_none(),
197+
Err(_) => false,
198+
}
199+
}
200+
201+
fn find_temp_path() -> String {
202+
use std::result::Result::Ok;
203+
204+
if is_ok_empty(defs::TEMP_DIR) {
205+
return defs::TEMP_DIR.to_string();
206+
}
207+
208+
// Try to create a random directory in /dev/
209+
let r = tempfile::tempdir_in("/dev/");
210+
match r {
211+
Ok(tmp_dir) => {
212+
if let Some(path) = tmp_dir.into_path().to_str() {
213+
return path.to_string();
214+
}
215+
}
216+
Err(_e) => {}
217+
}
218+
219+
let dirs = [
220+
defs::TEMP_DIR,
221+
"/patch_hw",
222+
"/oem",
223+
"/root",
224+
defs::TEMP_DIR_LEGACY,
225+
];
226+
227+
// find empty directory
228+
for dir in dirs {
229+
if is_ok_empty(dir) {
230+
return dir.to_string();
231+
}
232+
}
233+
234+
// Fallback to non-empty directory
235+
for dir in dirs {
236+
if metadata(dir).is_ok() {
237+
return dir.to_string();
238+
}
239+
}
240+
241+
"".to_string()
242+
}
243+
244+
pub fn get_tmp_path() -> &'static str {
245+
static CHOSEN_TMP_PATH: OnceLock<String> = OnceLock::new();
246+
247+
CHOSEN_TMP_PATH.get_or_init(|| {
248+
let r = find_temp_path();
249+
log::info!("Chosen temp_path: {}", r);
250+
r
251+
})
252+
}
253+
190254
#[cfg(target_os = "android")]
191255
fn link_ksud_to_bin() -> Result<()> {
192256
let ksu_bin = PathBuf::from(defs::DAEMON_PATH);

0 commit comments

Comments
 (0)