Move worker mount to magiskinit

This commit is contained in:
LoveSy 2024-08-14 21:36:15 +08:00 committed by John Wu
parent 838e1e254d
commit 1db80228e8
4 changed files with 26 additions and 22 deletions

View File

@ -9,7 +9,7 @@ use daemon::{daemon_entry, find_apk_path, get_magiskd, MagiskD};
use logging::{ use logging::{
android_logging, magisk_logging, zygisk_close_logd, zygisk_get_logd, zygisk_logging, android_logging, magisk_logging, zygisk_close_logd, zygisk_get_logd, zygisk_logging,
}; };
use mount::{find_preinit_device, revert_unmount, setup_mounts}; use mount::{find_preinit_device, revert_unmount, setup_mounts, clean_mounts};
use resetprop::{persist_delete_prop, persist_get_prop, persist_get_props, persist_set_prop}; use resetprop::{persist_delete_prop, persist_get_prop, persist_get_props, persist_set_prop};
mod cert; mod cert;
@ -92,6 +92,7 @@ pub mod ffi {
fn find_apk_path(pkg: Utf8CStrRef, data: &mut [u8]) -> usize; fn find_apk_path(pkg: Utf8CStrRef, data: &mut [u8]) -> usize;
fn read_certificate(fd: i32, version: i32) -> Vec<u8>; fn read_certificate(fd: i32, version: i32) -> Vec<u8>;
fn setup_mounts(); fn setup_mounts();
fn clean_mounts();
fn find_preinit_device() -> String; fn find_preinit_device() -> String;
fn revert_unmount(pid: i32); fn revert_unmount(pid: i32);
unsafe fn persist_get_prop(name: Utf8CStrRef, prop_cb: Pin<&mut PropCb>); unsafe fn persist_get_prop(name: Utf8CStrRef, prop_cb: Pin<&mut PropCb>);

View File

@ -157,11 +157,9 @@ void tmpfs_node::mount() {
if (!isa<tmpfs_node>(parent())) { if (!isa<tmpfs_node>(parent())) {
auto worker_dir = worker_path(); auto worker_dir = worker_path();
mkdirs(worker_dir.data(), 0); mkdirs(worker_dir.data(), 0);
bind_mount("tmpfs", worker_dir.data(), worker_dir.data());
clone_attr(exist() ? node_path().data() : parent()->node_path().data(), worker_dir.data()); clone_attr(exist() ? node_path().data() : parent()->node_path().data(), worker_dir.data());
dir_node::mount(); dir_node::mount();
VLOGD(replace() ? "replace" : "move", worker_dir.data(), node_path().data()); bind_mount(replace() ? "replace" : "move", worker_dir.data(), node_path().data());
xmount(worker_dir.data(), node_path().data(), nullptr, MS_MOVE, nullptr);
xmount(nullptr, node_path().data(), nullptr, MS_REMOUNT | MS_BIND | MS_RDONLY, nullptr); xmount(nullptr, node_path().data(), nullptr, MS_REMOUNT | MS_BIND | MS_RDONLY, nullptr);
} else { } else {
const string dest = worker_path(); const string dest = worker_path();
@ -333,10 +331,7 @@ void load_modules() {
} }
// cleanup mounts // cleanup mounts
ssprintf(buf, sizeof(buf), "%s/" WORKERDIR, get_magisk_tmp()); clean_mounts();
xumount2(buf, MNT_DETACH);
ssprintf(buf, sizeof(buf), "%s/" MODULEMNT, get_magisk_tmp());
xumount2(buf, MNT_DETACH);
} }
/************************ /************************

View File

@ -94,36 +94,40 @@ pub fn setup_mounts() {
ptr::null(), ptr::null(),
) )
.as_os_err()?; .as_os_err()?;
libc::mount( }
ptr::null(), };
}
pub fn clean_mounts() {
let magisk_tmp = get_magisk_tmp();
let mut buf = Utf8CStrBufArr::default();
let module_mnt = FsPathBuf::new(&mut buf).join(magisk_tmp).join(MODULEMNT);
let _: LoggedResult<()> = try {
unsafe {
libc::umount2(
module_mnt.as_ptr(), module_mnt.as_ptr(),
ptr::null(), libc::MNT_DETACH,
libc::MS_PRIVATE,
ptr::null(),
) )
.as_os_err()?; .as_os_err()?;
} }
}; };
// Prepare worker
let worker_dir = FsPathBuf::new(&mut buf).join(magisk_tmp).join(WORKERDIR); let worker_dir = FsPathBuf::new(&mut buf).join(magisk_tmp).join(WORKERDIR);
let _: LoggedResult<()> = try { let _: LoggedResult<()> = try {
worker_dir.mkdir(0)?;
unsafe { unsafe {
libc::mount( libc::mount(
worker_dir.as_ptr(), ptr::null(),
worker_dir.as_ptr(), worker_dir.as_ptr(),
ptr::null(), ptr::null(),
libc::MS_BIND, libc::MS_PRIVATE | libc::MS_REC,
ptr::null(), ptr::null(),
) )
.as_os_err()?; .as_os_err()?;
libc::mount( libc::umount2(
ptr::null(),
worker_dir.as_ptr(), worker_dir.as_ptr(),
ptr::null(), libc::MNT_DETACH,
libc::MS_PRIVATE,
ptr::null(),
) )
.as_os_err()?; .as_os_err()?;
} }

View File

@ -236,6 +236,7 @@ void MagiskInit::setup_tmp(const char *path) {
xmkdir(INTLROOT, 0711); xmkdir(INTLROOT, 0711);
xmkdir(DEVICEDIR, 0711); xmkdir(DEVICEDIR, 0711);
xmkdir(WORKERDIR, 0);
mount_preinit_dir(preinit_dev); mount_preinit_dir(preinit_dev);
@ -251,6 +252,9 @@ void MagiskInit::setup_tmp(const char *path) {
chdir(path); chdir(path);
// Prepare worker
xmount(WORKERDIR, WORKERDIR, nullptr, MS_BIND, nullptr);
// Use isolated devpts if kernel support // Use isolated devpts if kernel support
if (access("/dev/pts/ptmx", F_OK) == 0) { if (access("/dev/pts/ptmx", F_OK) == 0) {
xmkdirs(SHELLPTS, 0755); xmkdirs(SHELLPTS, 0755);