Simplify MagiskD Rust/C++ FFI

This commit is contained in:
topjohnwu 2024-12-03 15:51:17 -08:00
parent 805da67c23
commit 09c7ac754b
6 changed files with 31 additions and 67 deletions

View File

@ -148,8 +148,8 @@ static bool check_safe_mode() {
* Boot Stage Handlers *
***********************/
bool MagiskD::post_fs_data() const {
as_rust().setup_logfile();
bool MagiskD::post_fs_data() const noexcept {
setup_logfile();
LOGI("** post-fs-data mode running\n");
@ -194,8 +194,8 @@ bool MagiskD::post_fs_data() const {
return safe_mode;
}
void MagiskD::late_start() const {
as_rust().setup_logfile();
void MagiskD::late_start() const noexcept {
setup_logfile();
LOGI("** late_start service mode running\n");
@ -203,8 +203,8 @@ void MagiskD::late_start() const {
exec_module_scripts("service");
}
void MagiskD::boot_complete() const {
as_rust().setup_logfile();
void MagiskD::boot_complete() const noexcept {
setup_logfile();
LOGI("** boot-complete triggered\n");

View File

@ -128,20 +128,8 @@ static void poll_ctrl_handler(pollfd *pfd) {
}
}
const MagiskD &MagiskD::get() {
return *reinterpret_cast<const MagiskD*>(&rust::get_magiskd());
}
const rust::MagiskD *MagiskD::operator->() const {
return reinterpret_cast<const rust::MagiskD*>(this);
}
const rust::MagiskD &MagiskD::as_rust() const {
return *operator->();
}
void MagiskD::reboot() const {
if (as_rust().is_recovery())
void MagiskD::reboot() const noexcept {
if (is_recovery())
exec_command_sync("/system/bin/reboot", "recovery");
else
exec_command_sync("/system/bin/reboot");
@ -171,7 +159,7 @@ static void handle_request_async(int client, int code, const sock_cred &cred) {
write_int(client, 0);
close(client);
if (do_reboot) {
MagiskD::get().reboot();
MagiskD().reboot();
}
break;
}
@ -196,7 +184,7 @@ static void handle_request_sync(int client, int code) {
write_int(client, MAGISK_VER_CODE);
break;
case +RequestCode::START_DAEMON:
MagiskD::get()->setup_logfile();
MagiskD().setup_logfile();
break;
case +RequestCode::STOP_DAEMON: {
// Unmount all overlays
@ -298,7 +286,7 @@ static void handle_request(pollfd *pfd) {
exec_task([=, fd = client.release()] { handle_request_async(fd, code, cred); });
} else {
exec_task([=, fd = client.release()] {
MagiskD::get()->boot_stage_handler(fd, code);
MagiskD().boot_stage_handler(fd, code);
});
}
}

View File

@ -1,7 +1,7 @@
use std::fs::File;
use std::io;
use std::io::BufReader;
use std::sync::{Mutex, OnceLock};
use std::{io, mem};
use base::libc::{O_CLOEXEC, O_RDONLY};
use base::{
@ -10,7 +10,7 @@ use base::{
};
use crate::consts::MAIN_CONFIG;
use crate::ffi::{get_magisk_tmp, CxxMagiskD, RequestCode};
use crate::ffi::{get_magisk_tmp, RequestCode};
use crate::get_prop;
use crate::logging::magisk_logging;
@ -64,7 +64,7 @@ impl MagiskD {
match code {
RequestCode::POST_FS_DATA => {
if check_data() && !state.contains(BootState::PostFsDataDone) {
if self.as_cxx().post_fs_data() {
if self.post_fs_data() {
state.set(BootState::SafeMode);
}
state.set(BootState::PostFsDataDone);
@ -75,7 +75,7 @@ impl MagiskD {
unsafe { libc::close(client) };
if state.contains(BootState::PostFsDataDone) && !state.contains(BootState::SafeMode)
{
self.as_cxx().late_start();
self.late_start();
state.set(BootState::LateStartDone);
}
}
@ -83,7 +83,7 @@ impl MagiskD {
unsafe { libc::close(client) };
if state.contains(BootState::PostFsDataDone) {
state.set(BootState::BootComplete);
self.as_cxx().boot_complete()
self.boot_complete()
}
}
_ => {
@ -91,11 +91,6 @@ impl MagiskD {
}
}
}
#[inline(always)]
fn as_cxx(&self) -> &CxxMagiskD {
unsafe { mem::transmute(self) }
}
}
pub fn daemon_entry() {

View File

@ -108,7 +108,7 @@ db_settings::db_settings() {
data[SU_MULTIUSER_MODE] = MULTIUSER_MODE_OWNER_ONLY;
data[SU_MNT_NS] = NAMESPACE_MODE_REQUESTER;
data[DENYLIST_CONFIG] = false;
data[ZYGISK_CONFIG] = MagiskD::get()->is_emulator();
data[ZYGISK_CONFIG] = MagiskD().is_emulator();
data[BOOTLOOP_COUNT] = 0;
}

View File

@ -2,29 +2,6 @@
#include <base.hpp>
namespace rust {
struct MagiskD;
}
struct MagiskD {
// Make sure only references can exist
~MagiskD() = delete;
// Binding to Rust
static const MagiskD &get();
// C++ implementation
void reboot() const;
bool post_fs_data() const;
void late_start() const;
void boot_complete() const;
const rust::MagiskD *operator->() const;
private:
const rust::MagiskD &as_rust() const;
};
const char *get_magisk_tmp();
// Rust bindings

View File

@ -74,12 +74,6 @@ pub mod ffi {
fn resolve_preinit_dir(base_dir: Utf8CStrRef) -> String;
fn switch_mnt_ns(pid: i32) -> i32;
#[cxx_name = "MagiskD"]
type CxxMagiskD;
fn post_fs_data(self: &CxxMagiskD) -> bool;
fn late_start(self: &CxxMagiskD);
fn boot_complete(self: &CxxMagiskD);
}
extern "Rust" {
@ -99,18 +93,28 @@ pub mod ffi {
unsafe fn persist_get_props(prop_cb: Pin<&mut PropCb>);
unsafe fn persist_delete_prop(name: Utf8CStrRef) -> bool;
unsafe fn persist_set_prop(name: Utf8CStrRef, value: Utf8CStrRef) -> bool;
#[namespace = "rust"]
fn daemon_entry();
}
#[namespace = "rust"]
// FFI for MagiskD
extern "Rust" {
fn daemon_entry();
type MagiskD;
fn get_magiskd() -> &'static MagiskD;
fn setup_logfile(self: &MagiskD);
fn is_emulator(self: &MagiskD) -> bool;
fn is_recovery(self: &MagiskD) -> bool;
fn boot_stage_handler(self: &MagiskD, client: i32, code: i32);
#[cxx_name = "MagiskD"]
fn get_magiskd() -> &'static MagiskD;
}
unsafe extern "C++" {
#[allow(dead_code)]
fn reboot(self: &MagiskD);
fn post_fs_data(self: &MagiskD) -> bool;
fn late_start(self: &MagiskD);
fn boot_complete(self: &MagiskD);
}
}