mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-04-24 18:51:32 +00:00
Move MagiskInit::exec_init to rust
This commit is contained in:
parent
449204e380
commit
c99f4a591b
@ -58,6 +58,7 @@ pub mod ffi {
|
|||||||
|
|
||||||
extern "Rust" {
|
extern "Rust" {
|
||||||
fn prepare_data(self: &MagiskInit);
|
fn prepare_data(self: &MagiskInit);
|
||||||
|
fn exec_init(self: &MagiskInit);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "C++" {
|
unsafe extern "C++" {
|
||||||
@ -90,7 +91,6 @@ pub mod ffi {
|
|||||||
// SELinux
|
// SELinux
|
||||||
unsafe fn patch_sepolicy(self: &MagiskInit, in_: *const c_char, out: *const c_char);
|
unsafe fn patch_sepolicy(self: &MagiskInit, in_: *const c_char, out: *const c_char);
|
||||||
fn hijack_sepolicy(self: &mut MagiskInit) -> bool;
|
fn hijack_sepolicy(self: &mut MagiskInit) -> bool;
|
||||||
fn exec_init(self: &MagiskInit);
|
|
||||||
fn legacy_system_as_root(self: &mut MagiskInit);
|
fn legacy_system_as_root(self: &mut MagiskInit);
|
||||||
fn rootfs(self: &mut MagiskInit);
|
fn rootfs(self: &mut MagiskInit);
|
||||||
fn start(self: &mut MagiskInit);
|
fn start(self: &mut MagiskInit);
|
||||||
|
@ -210,17 +210,6 @@ mount_root:
|
|||||||
return is_two_stage;
|
return is_two_stage;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MagiskInit::exec_init() const noexcept {
|
|
||||||
// Unmount in reverse order
|
|
||||||
for (auto i = mount_list.size(); i > 0; --i) {
|
|
||||||
auto &p = mount_list[i - 1];
|
|
||||||
if (xumount2(p.data(), MNT_DETACH) == 0)
|
|
||||||
LOGD("Unmount [%s]\n", p.data());
|
|
||||||
}
|
|
||||||
execve("/init", argv, environ);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MagiskInit::setup_tmp(const char *path) const noexcept {
|
void MagiskInit::setup_tmp(const char *path) const noexcept {
|
||||||
LOGD("Setup Magisk tmp at %s\n", path);
|
LOGD("Setup Magisk tmp at %s\n", path);
|
||||||
chdir("/data");
|
chdir("/data");
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use cxx::CxxString;
|
||||||
use std::{
|
use std::{
|
||||||
collections::BTreeSet,
|
collections::BTreeSet,
|
||||||
ops::Bound::{Excluded, Unbounded},
|
ops::Bound::{Excluded, Unbounded},
|
||||||
@ -5,15 +6,18 @@ use std::{
|
|||||||
ptr::null as nullptr,
|
ptr::null as nullptr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use cxx::CxxString;
|
|
||||||
|
|
||||||
use crate::ffi::MagiskInit;
|
use crate::ffi::MagiskInit;
|
||||||
use base::{
|
use base::{
|
||||||
cstr, debug,
|
cstr, debug, libc,
|
||||||
libc::{chdir, chroot, mount, MS_MOVE},
|
libc::{chdir, chroot, execve, exit, mount, umount2, MNT_DETACH, MS_MOVE},
|
||||||
parse_mount_info, raw_cstr, Directory, FsPath, LibcReturn, LoggedResult, StringExt, Utf8CStr,
|
parse_mount_info, raw_cstr, Directory, FsPath, LibcReturn, LoggedResult, ResultExt, StringExt,
|
||||||
|
Utf8CStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
static environ: *const *mut libc::c_char;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn switch_root(path: &Utf8CStr) {
|
pub fn switch_root(path: &Utf8CStr) {
|
||||||
let res: LoggedResult<()> = try {
|
let res: LoggedResult<()> = try {
|
||||||
debug!("Switch root to {}", path);
|
debug!("Switch root to {}", path);
|
||||||
@ -94,4 +98,23 @@ impl MagiskInit {
|
|||||||
}
|
}
|
||||||
inner().ok();
|
inner().ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn exec_init(&self) {
|
||||||
|
unsafe {
|
||||||
|
for p in self.mount_list.iter().rev() {
|
||||||
|
if umount2(p.as_ptr().cast(), MNT_DETACH)
|
||||||
|
.as_os_err()
|
||||||
|
.log()
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
debug!("Unmount [{}]", p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
execve(raw_cstr!("/init"), self.argv.cast(), environ.cast())
|
||||||
|
.as_os_err()
|
||||||
|
.log()
|
||||||
|
.ok();
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user