Move MagiskInit::second_stage to rust

This commit is contained in:
LoveSy 2025-01-31 12:35:42 +08:00 committed by John Wu
parent 143743d0b0
commit dc9d5a4cac
3 changed files with 31 additions and 23 deletions

View File

@ -13,6 +13,7 @@ mod mount;
mod rootdir;
mod getinfo;
mod init;
mod twostage;
#[cxx::bridge]
pub mod ffi {
@ -86,7 +87,6 @@ pub mod ffi {
// Two stage init
fn redirect_second_stage(self: &MagiskInit);
fn first_stage(self: &MagiskInit);
fn second_stage(self: &mut MagiskInit);
// SELinux
unsafe fn patch_sepolicy(self: &MagiskInit, in_: *const c_char, out: *const c_char);

View File

@ -61,25 +61,3 @@ void MagiskInit::redirect_second_stage() const noexcept {
}
xmount("/data/init", "/init", nullptr, MS_BIND, nullptr);
}
void MagiskInit::second_stage() noexcept {
LOGI("Second Stage Init\n");
umount2("/init", MNT_DETACH);
umount2(INIT_PATH, MNT_DETACH); // just in case
unlink("/data/init");
// Make sure init dmesg logs won't get messed up
argv[0] = (char *) INIT_PATH;
// Some weird devices like meizu, uses 2SI but still have legacy rootfs
struct statfs sfs{};
statfs("/", &sfs);
if (sfs.f_type == RAMFS_MAGIC || sfs.f_type == TMPFS_MAGIC) {
// We are still on rootfs, so make sure we will execute the init of the 2nd stage
unlink("/init");
xsymlink(INIT_PATH, "/init");
patch_rw_root();
} else {
patch_ro_root();
}
}

View File

@ -0,0 +1,30 @@
use crate::ffi::MagiskInit;
use base::{cstr, info, raw_cstr, FsPath, ResultExt, libc::{statfs, umount2, MNT_DETACH, TMPFS_MAGIC}};
use std::ffi::c_long;
impl MagiskInit {
pub(crate) fn second_stage(&mut self) {
info!("Second Stage Init");
unsafe {
umount2(raw_cstr!("/init"), MNT_DETACH);
umount2(raw_cstr!("/system/bin/init"), MNT_DETACH); // just in case
FsPath::from(cstr!("/data/init")).remove().ok();
// Make sure init dmesg logs won't get messed up
*self.argv = raw_cstr!("/system/bin/init") as *mut _;
// Some weird devices like meizu, uses 2SI but still have legacy rootfs
let mut sfs: statfs = std::mem::zeroed();
statfs(raw_cstr!("/"), std::ptr::from_mut(&mut sfs));
if sfs.f_type == 0x858458f6 || sfs.f_type as c_long == TMPFS_MAGIC {
// We are still on rootfs, so make sure we will execute the init of the 2nd stage
let init_path = FsPath::from(cstr!("/init"));
init_path.remove().ok();
FsPath::from(cstr!("/system/bin/init")).symlink_to(init_path).log().ok();
self.patch_rw_root();
} else {
self.patch_ro_root();
}
}
}
}