diff --git a/native/src/core/daemon.cpp b/native/src/core/daemon.cpp index 432f43c9e..7122db803 100644 --- a/native/src/core/daemon.cpp +++ b/native/src/core/daemon.cpp @@ -132,12 +132,12 @@ const MagiskD &MagiskD::get() { return *reinterpret_cast(&rust::get_magiskd()); } -const rust::MagiskD &MagiskD::as_rust() const { - return *reinterpret_cast(this); +const rust::MagiskD *MagiskD::operator->() const { + return reinterpret_cast(this); } -void MagiskD::boot_stage_handler(int client, int code) const { - as_rust().boot_stage_handler(client, code); +const rust::MagiskD &MagiskD::as_rust() const { + return *operator->(); } void MagiskD::reboot() const { @@ -196,7 +196,7 @@ static void handle_request_sync(int client, int code) { write_int(client, MAGISK_VER_CODE); break; case +RequestCode::START_DAEMON: - rust::get_magiskd().setup_logfile(); + MagiskD::get()->setup_logfile(); break; case +RequestCode::STOP_DAEMON: { // Unmount all overlays @@ -298,7 +298,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::get()->boot_stage_handler(fd, code); }); } } diff --git a/native/src/core/daemon.rs b/native/src/core/daemon.rs index 25e3858db..b5710ba72 100644 --- a/native/src/core/daemon.rs +++ b/native/src/core/daemon.rs @@ -97,16 +97,8 @@ impl MagiskD { } } -mod cxx_extern { - use base::libc::c_char; - - extern "C" { - pub fn get_magisk_tmp() -> *const c_char; - } -} - pub fn get_magisk_tmp() -> &'static Utf8CStr { - unsafe { Utf8CStr::from_ptr(cxx_extern::get_magisk_tmp()).unwrap_unchecked() } + unsafe { Utf8CStr::from_ptr(super::ffi::get_magisk_tmp()).unwrap_unchecked() } } pub fn daemon_entry() { diff --git a/native/src/core/db.cpp b/native/src/core/db.cpp index 3c6f13959..8a8b669f5 100644 --- a/native/src/core/db.cpp +++ b/native/src/core/db.cpp @@ -116,7 +116,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] = rust::get_magiskd().is_emulator(); + data[ZYGISK_CONFIG] = MagiskD::get()->is_emulator(); } int db_settings::get_idx(string_view key) const { diff --git a/native/src/core/include/core.hpp b/native/src/core/include/core.hpp index 74a53ef12..1acfa1992 100644 --- a/native/src/core/include/core.hpp +++ b/native/src/core/include/core.hpp @@ -8,6 +8,7 @@ #include #include "socket.hpp" +#include "../core-rs.hpp" #define AID_ROOT 0 #define AID_SHELL 2000 @@ -18,28 +19,6 @@ #define to_app_id(uid) (uid % AID_USER_OFFSET) #define to_user_id(uid) (uid / AID_USER_OFFSET) -namespace rust { -struct MagiskD; -} - -struct MagiskD { - // Make sure only references can exist - ~MagiskD() = delete; - - // Binding to Rust - static const MagiskD &get(); - void boot_stage_handler(int client, int code) const; - - // C++ implementation - void reboot() const; - bool post_fs_data() const; - void late_start() const; - void boot_complete() const; - -private: - const rust::MagiskD &as_rust() const; -}; - // Return codes for daemon enum class RespondCode : int { ERROR = -1, @@ -61,11 +40,9 @@ extern bool zygisk_enabled; extern std::vector *module_list; void reset_zygisk(bool restore); -extern "C" const char *get_magisk_tmp(); int connect_daemon(int req, bool create = false); std::string find_preinit_device(); void unlock_blocks(); -void reboot(); // Poll control using poll_callback = void(*)(pollfd*); @@ -115,6 +92,3 @@ int denylist_cli(int argc, char **argv); void initialize_denylist(); bool is_deny_target(int uid, std::string_view process); void revert_unmount(); - -// Include last to prevent recursive include issues -#include "../core-rs.hpp" diff --git a/native/src/core/include/daemon.hpp b/native/src/core/include/daemon.hpp new file mode 100644 index 000000000..7bbc0e54a --- /dev/null +++ b/native/src/core/include/daemon.hpp @@ -0,0 +1,26 @@ +#pragma once + +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(); diff --git a/native/src/core/lib.rs b/native/src/core/lib.rs index bd55eb65b..87dcf33d2 100644 --- a/native/src/core/lib.rs +++ b/native/src/core/lib.rs @@ -53,7 +53,9 @@ pub mod ffi { } unsafe extern "C++" { - include!("include/core.hpp"); + include!("include/daemon.hpp"); + + fn get_magisk_tmp() -> *const c_char; #[cxx_name = "MagiskD"] type CxxMagiskD;