Use static methods in cxx-rs

This commit is contained in:
topjohnwu 2025-02-02 02:42:18 +08:00
parent 58a25a3e2b
commit 59622d1688
11 changed files with 51 additions and 59 deletions

View File

@ -146,7 +146,7 @@ static void handle_request_async(int client, int code, const sock_cred &cred) {
break;
case +RequestCode::ZYGOTE_RESTART: {
LOGI("** zygote restarted\n");
auto &daemon = MagiskD();
auto &daemon = MagiskD::Get();
daemon.prune_su_access();
scan_deny_apps();
daemon.zygisk_reset(false);
@ -154,7 +154,7 @@ static void handle_request_async(int client, int code, const sock_cred &cred) {
break;
}
case +RequestCode::SQLITE_CMD:
MagiskD().db_exec(client);
MagiskD::Get().db_exec(client);
break;
case +RequestCode::REMOVE_MODULES: {
int do_reboot = read_int(client);
@ -162,12 +162,12 @@ static void handle_request_async(int client, int code, const sock_cred &cred) {
write_int(client, 0);
close(client);
if (do_reboot) {
MagiskD().reboot();
MagiskD::Get().reboot();
}
break;
}
case +RequestCode::ZYGISK:
MagiskD().zygisk_handler(client);
MagiskD::Get().zygisk_handler(client);
break;
default:
__builtin_unreachable();
@ -289,7 +289,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().boot_stage_handler(fd, code);
MagiskD::Get().boot_stage_handler(fd, code);
});
}
}
@ -332,7 +332,7 @@ static void daemon_entry() {
setcon(MAGISK_PROC_CON);
rust::daemon_entry();
SDK_INT = MagiskD().sdk_int();
SDK_INT = MagiskD::Get().sdk_int();
// Escape from cgroup
int pid = getpid();

View File

@ -73,6 +73,10 @@ pub struct MagiskD {
}
impl MagiskD {
pub fn get() -> &'static MagiskD {
unsafe { MAGISKD.get().unwrap_unchecked() }
}
pub fn is_recovery(&self) -> bool {
self.is_recovery
}
@ -300,7 +304,3 @@ fn check_data() -> bool {
}
false
}
pub fn get_magiskd() -> &'static MagiskD {
unsafe { MAGISKD.get().unwrap_unchecked() }
}

View File

@ -73,10 +73,6 @@ impl Default for MntNsMode {
}
}
pub fn get_default_db_settings() -> DbSettings {
DbSettings::default()
}
impl DbEntryKey {
fn to_str(self) -> &'static str {
match self {

View File

@ -370,7 +370,7 @@ int enable_deny() {
denylist_enforced = true;
if (!MagiskD().zygisk_enabled()) {
if (!MagiskD::Get().zygisk_enabled()) {
if (new_daemon_thread(&logcat)) {
denylist_enforced = false;
return DenyResponse::ERROR;
@ -385,7 +385,7 @@ int enable_deny() {
}
}
MagiskD().set_db_setting(DbEntryKey::DenylistConfig, true);
MagiskD::Get().set_db_setting(DbEntryKey::DenylistConfig, true);
return DenyResponse::OK;
}
@ -393,13 +393,13 @@ int disable_deny() {
if (denylist_enforced.exchange(false)) {
LOGI("* Disable DenyList\n");
}
MagiskD().set_db_setting(DbEntryKey::DenylistConfig, false);
MagiskD::Get().set_db_setting(DbEntryKey::DenylistConfig, false);
return DenyResponse::OK;
}
void initialize_denylist() {
if (!denylist_enforced) {
if (MagiskD().get_db_setting(DbEntryKey::DenylistConfig))
if (MagiskD::Get().get_db_setting(DbEntryKey::DenylistConfig))
enable_deny();
}
}

View File

@ -7,6 +7,8 @@
#include <atomic>
#include <functional>
#include <base.hpp>
#include "socket.hpp"
#include "../core-rs.hpp"
@ -26,10 +28,17 @@ enum class RespondCode : int {
END
};
struct ModuleInfo;
extern std::string native_bridge;
// Daemon
int connect_daemon(int req, bool create = false);
const char *get_magisk_tmp();
void unlock_blocks();
bool setup_magisk_env();
bool check_key_combo();
void restore_zygisk_prop();
// Poll control
using poll_callback = void(*)(pollfd*);
@ -50,6 +59,10 @@ void disable_modules();
void remove_modules();
// Scripting
void install_apk(rust::Utf8CStr apk);
void uninstall_pkg(rust::Utf8CStr pkg);
void exec_common_scripts(rust::Utf8CStr stage);
void exec_module_scripts(rust::Utf8CStr stage, const rust::Vec<ModuleInfo> &module_list);
void exec_script(const char *script);
void clear_pkg(const char *pkg, int user_id);
[[noreturn]] void install_module(const char *file);
@ -61,3 +74,10 @@ void initialize_denylist();
void scan_deny_apps();
bool is_deny_target(int uid, std::string_view process);
void revert_unmount(int pid = -1) noexcept;
void update_deny_flags(int uid, rust::Str process, uint32_t &flags);
// Rust bindings
static inline rust::Utf8CStr get_magisk_tmp_rs() { return get_magisk_tmp(); }
static inline rust::String resolve_preinit_dir_rs(rust::Utf8CStr base_dir) {
return resolve_preinit_dir(base_dir.c_str());
}

View File

@ -1,23 +0,0 @@
#pragma once
#include <base.hpp>
struct ModuleInfo;
const char *get_magisk_tmp();
bool setup_magisk_env();
bool check_key_combo();
void disable_modules();
void exec_common_scripts(rust::Utf8CStr stage);
void exec_module_scripts(rust::Utf8CStr stage, const rust::Vec<ModuleInfo> &module_list);
void install_apk(rust::Utf8CStr apk);
void uninstall_pkg(rust::Utf8CStr pkg);
void update_deny_flags(int uid, rust::Str process, uint32_t &flags);
void initialize_denylist();
void restore_zygisk_prop();
// Rust bindings
static inline rust::Utf8CStr get_magisk_tmp_rs() { return get_magisk_tmp(); }
static inline rust::String resolve_preinit_dir_rs(rust::Utf8CStr base_dir) {
return resolve_preinit_dir(base_dir.c_str());
}

View File

@ -7,13 +7,11 @@
#![allow(clippy::missing_safety_doc)]
use base::Utf8CStr;
use daemon::{daemon_entry, get_magiskd, MagiskD};
use db::get_default_db_settings;
use daemon::{daemon_entry, MagiskD};
use logging::{android_logging, setup_logfile, zygisk_close_logd, zygisk_get_logd, zygisk_logging};
use mount::{clean_mounts, find_preinit_device, revert_unmount};
use resetprop::{persist_delete_prop, persist_get_prop, persist_get_props, persist_set_prop};
use socket::{recv_fd, recv_fds, send_fd, send_fds};
use su::get_default_root_settings;
use zygisk::zygisk_should_load_module;
#[path = "../include/consts.rs"]
@ -74,7 +72,7 @@ pub mod ffi {
#[cxx_name = "Utf8CStr"]
type Utf8CStrRef<'a> = base::ffi::Utf8CStrRef<'a>;
include!("include/ffi.hpp");
include!("include/core.hpp");
#[cxx_name = "get_magisk_tmp_rs"]
fn get_magisk_tmp() -> Utf8CStrRef<'static>;
@ -236,12 +234,17 @@ pub mod ffi {
#[cxx_name = "get_root_settings"]
fn get_root_settings_for_cxx(&self, uid: i32, settings: &mut RootSettings) -> bool;
#[cxx_name = "DbSettings"]
fn get_default_db_settings() -> DbSettings;
#[cxx_name = "RootSettings"]
fn get_default_root_settings() -> RootSettings;
#[cxx_name = "MagiskD"]
fn get_magiskd() -> &'static MagiskD;
#[Self = DbSettings]
#[cxx_name = "New"]
fn default() -> DbSettings;
#[Self = RootSettings]
#[cxx_name = "New"]
fn default() -> RootSettings;
#[Self = MagiskD]
#[cxx_name = "Get"]
fn get() -> &'static MagiskD;
}
unsafe extern "C++" {
#[allow(dead_code)]

View File

@ -26,7 +26,7 @@ static void set_script_env() {
char new_path[4096];
ssprintf(new_path, sizeof(new_path), "%s:%s", getenv("PATH"), get_magisk_tmp());
setenv("PATH", new_path, 1);
if (MagiskD().zygisk_enabled())
if (MagiskD::Get().zygisk_enabled())
setenv("ZYGISK_ENABLED", "1", 1);
};

View File

@ -139,7 +139,3 @@ impl MagiskD {
granted
}
}
pub fn get_default_root_settings() -> RootSettings {
RootSettings::default()
}

View File

@ -18,7 +18,7 @@ static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
static shared_ptr<su_info> cached;
su_info::su_info(int uid) :
uid(uid), eval_uid(-1), cfg(DbSettings()), access(RootSettings()),
uid(uid), eval_uid(-1), cfg(DbSettings::New()), access(RootSettings::New()),
mgr_uid(-1), timestamp(0), _lock(PTHREAD_MUTEX_INITIALIZER) {}
su_info::~su_info() {
@ -44,7 +44,7 @@ void su_info::refresh() {
void su_info::check_db() {
eval_uid = uid;
auto &daemon = MagiskD();
auto &daemon = MagiskD::Get();
daemon.get_db_settings(cfg);
// Check multiuser settings

@ -1 +1 @@
Subproject commit 49f3912d9c6e02dc05facccce5dbda5451016ab2
Subproject commit 0ea6d507cee40da9581b71bd531a4e3ac0a37164