mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-11-25 02:55:33 +00:00
Move some global state into Rust
This commit is contained in:
parent
7c2e93d266
commit
6c0966b795
@ -243,13 +243,6 @@ static bool magisk_env() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void reboot() {
|
||||
if (RECOVERY_MODE)
|
||||
exec_command_sync("/system/bin/reboot", "recovery");
|
||||
else
|
||||
exec_command_sync("/system/bin/reboot");
|
||||
}
|
||||
|
||||
static bool check_data() {
|
||||
bool mnt = false;
|
||||
file_readline("/proc/mounts", [&](string_view s) {
|
||||
@ -347,11 +340,11 @@ static bool check_key_combo() {
|
||||
|
||||
extern int disable_deny();
|
||||
|
||||
static void post_fs_data() {
|
||||
void MagiskD::post_fs_data() const {
|
||||
if (!check_data())
|
||||
return;
|
||||
|
||||
rust::get_magiskd().setup_logfile();
|
||||
setup_logfile();
|
||||
|
||||
LOGI("** post-fs-data mode running\n");
|
||||
|
||||
@ -390,8 +383,8 @@ early_abort:
|
||||
boot_state |= FLAG_POST_FS_DATA_DONE;
|
||||
}
|
||||
|
||||
static void late_start() {
|
||||
rust::get_magiskd().setup_logfile();
|
||||
void MagiskD::late_start() const {
|
||||
setup_logfile();
|
||||
|
||||
LOGI("** late_start service mode running\n");
|
||||
|
||||
@ -401,9 +394,9 @@ static void late_start() {
|
||||
boot_state |= FLAG_LATE_START_DONE;
|
||||
}
|
||||
|
||||
static void boot_complete() {
|
||||
void MagiskD::boot_complete() const {
|
||||
boot_state |= FLAG_BOOT_COMPLETE;
|
||||
rust::get_magiskd().setup_logfile();
|
||||
setup_logfile();
|
||||
|
||||
LOGI("** boot-complete triggered\n");
|
||||
|
||||
@ -422,22 +415,23 @@ void boot_stage_handler(int client, int code) {
|
||||
// Make sure boot stage execution is always serialized
|
||||
static pthread_mutex_t stage_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
mutex_guard lock(stage_lock);
|
||||
MagiskD daemon;
|
||||
|
||||
switch (code) {
|
||||
case MainRequest::POST_FS_DATA:
|
||||
if ((boot_state & FLAG_POST_FS_DATA_DONE) == 0)
|
||||
post_fs_data();
|
||||
daemon.post_fs_data();
|
||||
close(client);
|
||||
break;
|
||||
case MainRequest::LATE_START:
|
||||
close(client);
|
||||
if ((boot_state & FLAG_POST_FS_DATA_DONE) && (boot_state & FLAG_SAFE_MODE) == 0)
|
||||
late_start();
|
||||
daemon.late_start();
|
||||
break;
|
||||
case MainRequest::BOOT_COMPLETE:
|
||||
close(client);
|
||||
if ((boot_state & FLAG_SAFE_MODE) == 0)
|
||||
boot_complete();
|
||||
daemon.boot_complete();
|
||||
break;
|
||||
default:
|
||||
__builtin_unreachable();
|
||||
|
@ -14,8 +14,6 @@ using namespace std;
|
||||
|
||||
int SDK_INT = -1;
|
||||
|
||||
bool RECOVERY_MODE = false;
|
||||
|
||||
static struct stat self_st;
|
||||
|
||||
static map<int, poll_callback> *poll_map;
|
||||
@ -130,6 +128,13 @@ static void poll_ctrl_handler(pollfd *pfd) {
|
||||
}
|
||||
}
|
||||
|
||||
void MagiskD::reboot() const {
|
||||
if (is_recovery())
|
||||
exec_command_sync("/system/bin/reboot", "recovery");
|
||||
else
|
||||
exec_command_sync("/system/bin/reboot");
|
||||
}
|
||||
|
||||
static void handle_request_async(int client, int code, const sock_cred &cred) {
|
||||
switch (code) {
|
||||
case MainRequest::DENYLIST:
|
||||
@ -153,7 +158,10 @@ static void handle_request_async(int client, int code, const sock_cred &cred) {
|
||||
remove_modules();
|
||||
write_int(client, 0);
|
||||
close(client);
|
||||
if (do_reboot) reboot();
|
||||
if (do_reboot) {
|
||||
MagiskD daemon;
|
||||
daemon.reboot();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case MainRequest::ZYGISK:
|
||||
@ -371,14 +379,6 @@ static void daemon_entry() {
|
||||
ssprintf(path, sizeof(path), "%s/" ROOTOVL, tmp);
|
||||
rm_rf(path);
|
||||
|
||||
// Load config status
|
||||
ssprintf(path, sizeof(path), "%s/" MAIN_CONFIG, tmp);
|
||||
parse_prop_file(path, [](auto key, auto val) -> bool {
|
||||
if (key == "RECOVERYMODE" && val == "true")
|
||||
RECOVERY_MODE = true;
|
||||
return true;
|
||||
});
|
||||
|
||||
// Use isolated devpts if kernel support
|
||||
if (access("/dev/pts/ptmx", F_OK) == 0) {
|
||||
ssprintf(path, sizeof(path), "%s/" SHELLPTS, tmp);
|
||||
|
@ -1,11 +1,16 @@
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::BufReader;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
use base::{cstr, Directory, ResultExt, Utf8CStr, Utf8CStrBuf, Utf8CStrBufRef, WalkResult};
|
||||
use base::libc::{O_CLOEXEC, O_RDONLY};
|
||||
use base::{
|
||||
cstr, BufReadExt, Directory, FsPathBuf, ResultExt, Utf8CStr, Utf8CStrBuf, Utf8CStrBufArr,
|
||||
Utf8CStrBufRef, WalkResult,
|
||||
};
|
||||
|
||||
use crate::get_prop;
|
||||
use crate::logging::magisk_logging;
|
||||
use crate::{get_prop, MAIN_CONFIG};
|
||||
|
||||
// Global magiskd singleton
|
||||
pub static MAGISKD: OnceLock<MagiskD> = OnceLock::new();
|
||||
@ -14,12 +19,17 @@ pub static MAGISKD: OnceLock<MagiskD> = OnceLock::new();
|
||||
pub struct MagiskD {
|
||||
pub logd: Mutex<Option<File>>,
|
||||
is_emulator: bool,
|
||||
is_recovery: bool,
|
||||
}
|
||||
|
||||
impl MagiskD {
|
||||
pub fn is_emulator(&self) -> bool {
|
||||
self.is_emulator
|
||||
}
|
||||
|
||||
pub fn is_recovery(&self) -> bool {
|
||||
self.is_recovery
|
||||
}
|
||||
}
|
||||
|
||||
mod cxx_extern {
|
||||
@ -41,9 +51,27 @@ pub fn daemon_entry() {
|
||||
}
|
||||
let is_emulator = qemu == "1";
|
||||
|
||||
// Load config status
|
||||
let mut buf = Utf8CStrBufArr::<64>::new();
|
||||
let path = FsPathBuf::new(&mut buf)
|
||||
.join(get_magisk_tmp())
|
||||
.join(MAIN_CONFIG!());
|
||||
let mut is_recovery = false;
|
||||
if let Ok(file) = path.open(O_RDONLY | O_CLOEXEC) {
|
||||
let mut file = BufReader::new(file);
|
||||
file.foreach_props(|key, val| {
|
||||
if key == "RECOVERYMODE" {
|
||||
is_recovery = val == "true";
|
||||
return false;
|
||||
}
|
||||
true
|
||||
});
|
||||
}
|
||||
|
||||
let magiskd = MagiskD {
|
||||
logd: Default::default(),
|
||||
is_emulator,
|
||||
is_recovery,
|
||||
};
|
||||
magiskd.start_log_daemon();
|
||||
MAGISKD.set(magiskd).ok();
|
||||
|
@ -19,6 +19,22 @@
|
||||
#define to_app_id(uid) (uid % AID_USER_OFFSET)
|
||||
#define to_user_id(uid) (uid / AID_USER_OFFSET)
|
||||
|
||||
struct MagiskD {
|
||||
// Binding to Rust
|
||||
void setup_logfile() const noexcept { impl.setup_logfile(); }
|
||||
bool is_emulator() const noexcept { return impl.is_emulator(); }
|
||||
bool is_recovery() const noexcept { return impl.is_recovery(); }
|
||||
|
||||
// C++ implementation
|
||||
void reboot() const;
|
||||
void post_fs_data() const;
|
||||
void late_start() const;
|
||||
void boot_complete() const;
|
||||
|
||||
private:
|
||||
const rust::MagiskD &impl = rust::get_magiskd();
|
||||
};
|
||||
|
||||
// Daemon command codes
|
||||
namespace MainRequest {
|
||||
enum : int {
|
||||
@ -65,7 +81,6 @@ struct module_info {
|
||||
#endif
|
||||
};
|
||||
|
||||
extern bool RECOVERY_MODE;
|
||||
extern bool zygisk_enabled;
|
||||
extern std::vector<module_info> *module_list;
|
||||
|
||||
|
@ -52,6 +52,7 @@ pub mod ffi {
|
||||
fn close_log_pipe(self: &MagiskD);
|
||||
fn setup_logfile(self: &MagiskD);
|
||||
fn is_emulator(self: &MagiskD) -> bool;
|
||||
fn is_recovery(self: &MagiskD) -> bool;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,3 +20,10 @@ macro_rules! LOG_PIPE {
|
||||
concat!($crate::INTLROOT!(), "/log")
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! MAIN_CONFIG {
|
||||
() => {
|
||||
concat!($crate::INTLROOT!(), "/config")
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user