Skip to content

Commit 1db8022

Browse files
yujincheng08topjohnwu
authored andcommitted
Move worker mount to magiskinit
1 parent 838e1e2 commit 1db8022

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

native/src/core/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use daemon::{daemon_entry, find_apk_path, get_magiskd, MagiskD};
99
use logging::{
1010
android_logging, magisk_logging, zygisk_close_logd, zygisk_get_logd, zygisk_logging,
1111
};
12-
use mount::{find_preinit_device, revert_unmount, setup_mounts};
12+
use mount::{find_preinit_device, revert_unmount, setup_mounts, clean_mounts};
1313
use resetprop::{persist_delete_prop, persist_get_prop, persist_get_props, persist_set_prop};
1414

1515
mod cert;
@@ -92,6 +92,7 @@ pub mod ffi {
9292
fn find_apk_path(pkg: Utf8CStrRef, data: &mut [u8]) -> usize;
9393
fn read_certificate(fd: i32, version: i32) -> Vec<u8>;
9494
fn setup_mounts();
95+
fn clean_mounts();
9596
fn find_preinit_device() -> String;
9697
fn revert_unmount(pid: i32);
9798
unsafe fn persist_get_prop(name: Utf8CStrRef, prop_cb: Pin<&mut PropCb>);

native/src/core/module.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,9 @@ void tmpfs_node::mount() {
157157
if (!isa<tmpfs_node>(parent())) {
158158
auto worker_dir = worker_path();
159159
mkdirs(worker_dir.data(), 0);
160-
bind_mount("tmpfs", worker_dir.data(), worker_dir.data());
161160
clone_attr(exist() ? node_path().data() : parent()->node_path().data(), worker_dir.data());
162161
dir_node::mount();
163-
VLOGD(replace() ? "replace" : "move", worker_dir.data(), node_path().data());
164-
xmount(worker_dir.data(), node_path().data(), nullptr, MS_MOVE, nullptr);
162+
bind_mount(replace() ? "replace" : "move", worker_dir.data(), node_path().data());
165163
xmount(nullptr, node_path().data(), nullptr, MS_REMOUNT | MS_BIND | MS_RDONLY, nullptr);
166164
} else {
167165
const string dest = worker_path();
@@ -333,10 +331,7 @@ void load_modules() {
333331
}
334332

335333
// cleanup mounts
336-
ssprintf(buf, sizeof(buf), "%s/" WORKERDIR, get_magisk_tmp());
337-
xumount2(buf, MNT_DETACH);
338-
ssprintf(buf, sizeof(buf), "%s/" MODULEMNT, get_magisk_tmp());
339-
xumount2(buf, MNT_DETACH);
334+
clean_mounts();
340335
}
341336

342337
/************************

native/src/core/mount.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,36 +94,40 @@ pub fn setup_mounts() {
9494
ptr::null(),
9595
)
9696
.as_os_err()?;
97-
libc::mount(
98-
ptr::null(),
97+
}
98+
};
99+
}
100+
101+
pub fn clean_mounts() {
102+
let magisk_tmp = get_magisk_tmp();
103+
104+
let mut buf = Utf8CStrBufArr::default();
105+
106+
let module_mnt = FsPathBuf::new(&mut buf).join(magisk_tmp).join(MODULEMNT);
107+
let _: LoggedResult<()> = try {
108+
unsafe {
109+
libc::umount2(
99110
module_mnt.as_ptr(),
100-
ptr::null(),
101-
libc::MS_PRIVATE,
102-
ptr::null(),
111+
libc::MNT_DETACH,
103112
)
104113
.as_os_err()?;
105114
}
106115
};
107116

108-
// Prepare worker
109117
let worker_dir = FsPathBuf::new(&mut buf).join(magisk_tmp).join(WORKERDIR);
110118
let _: LoggedResult<()> = try {
111-
worker_dir.mkdir(0)?;
112119
unsafe {
113120
libc::mount(
114-
worker_dir.as_ptr(),
121+
ptr::null(),
115122
worker_dir.as_ptr(),
116123
ptr::null(),
117-
libc::MS_BIND,
124+
libc::MS_PRIVATE | libc::MS_REC,
118125
ptr::null(),
119126
)
120127
.as_os_err()?;
121-
libc::mount(
122-
ptr::null(),
128+
libc::umount2(
123129
worker_dir.as_ptr(),
124-
ptr::null(),
125-
libc::MS_PRIVATE,
126-
ptr::null(),
130+
libc::MNT_DETACH,
127131
)
128132
.as_os_err()?;
129133
}

native/src/init/mount.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ void MagiskInit::setup_tmp(const char *path) {
236236

237237
xmkdir(INTLROOT, 0711);
238238
xmkdir(DEVICEDIR, 0711);
239+
xmkdir(WORKERDIR, 0);
239240

240241
mount_preinit_dir(preinit_dev);
241242

@@ -251,6 +252,9 @@ void MagiskInit::setup_tmp(const char *path) {
251252

252253
chdir(path);
253254

255+
// Prepare worker
256+
xmount(WORKERDIR, WORKERDIR, nullptr, MS_BIND, nullptr);
257+
254258
// Use isolated devpts if kernel support
255259
if (access("/dev/pts/ptmx", F_OK) == 0) {
256260
xmkdirs(SHELLPTS, 0755);

0 commit comments

Comments
 (0)