From dc9d5a4cac8f2eba3b9d4d6d755a25795eea14b1 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Fri, 31 Jan 2025 12:35:42 +0800 Subject: [PATCH] Move MagiskInit::second_stage to rust --- native/src/init/lib.rs | 2 +- native/src/init/twostage.cpp | 22 ---------------------- native/src/init/twostage.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 native/src/init/twostage.rs diff --git a/native/src/init/lib.rs b/native/src/init/lib.rs index ee28a1781..c3c338c16 100644 --- a/native/src/init/lib.rs +++ b/native/src/init/lib.rs @@ -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); diff --git a/native/src/init/twostage.cpp b/native/src/init/twostage.cpp index 1684544f4..810a1073b 100644 --- a/native/src/init/twostage.cpp +++ b/native/src/init/twostage.cpp @@ -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(); - } -} diff --git a/native/src/init/twostage.rs b/native/src/init/twostage.rs new file mode 100644 index 000000000..5d98eec4d --- /dev/null +++ b/native/src/init/twostage.rs @@ -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(); + } + } + } +}