diff --git a/native/jni/core/bootstages.cpp b/native/jni/core/bootstages.cpp index 49feba470..70b7ec592 100644 --- a/native/jni/core/bootstages.cpp +++ b/native/jni/core/bootstages.cpp @@ -15,7 +15,6 @@ using namespace std; -static bool pfs_done = false; static bool safe_mode = false; /********* @@ -269,21 +268,26 @@ static bool check_key_combo() { return true; } -/**************** - * Entry points * - ****************/ +/*********************** + * Boot Stage Handlers * + ***********************/ + +static pthread_mutex_t stage_lock = PTHREAD_MUTEX_INITIALIZER; void post_fs_data(int client) { // ack write_int(client, 0); close(client); + mutex_guard lock(stage_lock); + if (getenv("REMOUNT_ROOT")) xmount(nullptr, "/", nullptr, MS_REMOUNT | MS_RDONLY, nullptr); if (!check_data()) goto unblock_init; + DAEMON_STATE = STATE_POST_FS_DATA; setup_logfile(true); LOGI("** post-fs-data mode running\n"); @@ -320,25 +324,27 @@ void post_fs_data(int client) { handle_modules(); } - pfs_done = true; - early_abort: // We still do magic mount because root itself might need it magic_mount(); + DAEMON_STATE = STATE_POST_FS_DATA_DONE; unblock_init: close(xopen(UNBLOCKFILE, O_RDONLY | O_CREAT, 0)); } void late_start(int client) { - LOGI("** late_start service mode running\n"); // ack write_int(client, 0); close(client); + mutex_guard lock(stage_lock); + run_finally fin([]{ DAEMON_STATE = STATE_LATE_START_DONE; }); setup_logfile(false); - if (!pfs_done || safe_mode) + LOGI("** late_start service mode running\n"); + + if (DAEMON_STATE < STATE_POST_FS_DATA_DONE || safe_mode) return; exec_common_scripts("service"); @@ -346,13 +352,16 @@ void late_start(int client) { } void boot_complete(int client) { - LOGI("** boot_complete triggered\n"); // ack write_int(client, 0); close(client); + mutex_guard lock(stage_lock); + DAEMON_STATE = STATE_BOOT_COMPLETE; setup_logfile(false); + LOGI("** boot_complete triggered\n"); + if (safe_mode) return; @@ -360,8 +369,7 @@ void boot_complete(int client) { if (access(SECURE_DIR, F_OK) != 0) xmkdir(SECURE_DIR, 0700); - if (pfs_done) - auto_start_magiskhide(); + auto_start_magiskhide(); if (!check_manager()) { if (access(MANAGERAPK, F_OK) == 0) { diff --git a/native/jni/core/daemon.cpp b/native/jni/core/daemon.cpp index 6e2828d57..d2b87057a 100644 --- a/native/jni/core/daemon.cpp +++ b/native/jni/core/daemon.cpp @@ -21,7 +21,7 @@ using namespace std; int SDK_INT = -1; bool RECOVERY_MODE = false; string MAGISKTMP; -int DAEMON_STATE = STATE_UNKNOWN; +int DAEMON_STATE = STATE_NONE; static struct stat self_st; @@ -99,19 +99,8 @@ static void handle_request(int client) { break; } - switch (req_code) { - // In case of init trigger launches, set the corresponding states - case POST_FS_DATA: - DAEMON_STATE = STATE_POST_FS_DATA; - break; - case LATE_START: - DAEMON_STATE = STATE_LATE_START; - break; - case BOOT_COMPLETE: - DAEMON_STATE = STATE_BOOT_COMPLETE; - break; - // Simple requests + switch (req_code) { case CHECK_VERSION: write_string(client, MAGISK_VERSION ":MAGISK"); goto shortcut; diff --git a/native/jni/include/daemon.hpp b/native/jni/include/daemon.hpp index 7aeacecf2..3464b1cd4 100644 --- a/native/jni/include/daemon.hpp +++ b/native/jni/include/daemon.hpp @@ -32,10 +32,11 @@ enum { // Daemon state enum { + STATE_NONE, STATE_POST_FS_DATA, - STATE_LATE_START, - STATE_BOOT_COMPLETE, - STATE_UNKNOWN + STATE_POST_FS_DATA_DONE, + STATE_LATE_START_DONE, + STATE_BOOT_COMPLETE }; extern int SDK_INT; diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index 66fc629b3..b098d4c1c 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -247,7 +247,7 @@ int launch_magiskhide() { return DAEMON_ERROR; hide_sensitive_props(); - if (DAEMON_STATE >= STATE_BOOT_COMPLETE) + if (DAEMON_STATE >= STATE_BOOT_COMPLETE || DAEMON_STATE == STATE_NONE) hide_late_sensitive_props(); // Start monitoring