Make sure boot stages are mutually exclusive

This commit is contained in:
topjohnwu 2020-12-15 03:40:37 -08:00
parent 7ff22c68c7
commit 086059ec30
4 changed files with 26 additions and 28 deletions

View File

@ -15,7 +15,6 @@
using namespace std; using namespace std;
static bool pfs_done = false;
static bool safe_mode = false; static bool safe_mode = false;
/********* /*********
@ -269,21 +268,26 @@ static bool check_key_combo() {
return true; return true;
} }
/**************** /***********************
* Entry points * * Boot Stage Handlers *
****************/ ***********************/
static pthread_mutex_t stage_lock = PTHREAD_MUTEX_INITIALIZER;
void post_fs_data(int client) { void post_fs_data(int client) {
// ack // ack
write_int(client, 0); write_int(client, 0);
close(client); close(client);
mutex_guard lock(stage_lock);
if (getenv("REMOUNT_ROOT")) if (getenv("REMOUNT_ROOT"))
xmount(nullptr, "/", nullptr, MS_REMOUNT | MS_RDONLY, nullptr); xmount(nullptr, "/", nullptr, MS_REMOUNT | MS_RDONLY, nullptr);
if (!check_data()) if (!check_data())
goto unblock_init; goto unblock_init;
DAEMON_STATE = STATE_POST_FS_DATA;
setup_logfile(true); setup_logfile(true);
LOGI("** post-fs-data mode running\n"); LOGI("** post-fs-data mode running\n");
@ -320,25 +324,27 @@ void post_fs_data(int client) {
handle_modules(); handle_modules();
} }
pfs_done = true;
early_abort: early_abort:
// We still do magic mount because root itself might need it // We still do magic mount because root itself might need it
magic_mount(); magic_mount();
DAEMON_STATE = STATE_POST_FS_DATA_DONE;
unblock_init: unblock_init:
close(xopen(UNBLOCKFILE, O_RDONLY | O_CREAT, 0)); close(xopen(UNBLOCKFILE, O_RDONLY | O_CREAT, 0));
} }
void late_start(int client) { void late_start(int client) {
LOGI("** late_start service mode running\n");
// ack // ack
write_int(client, 0); write_int(client, 0);
close(client); close(client);
mutex_guard lock(stage_lock);
run_finally fin([]{ DAEMON_STATE = STATE_LATE_START_DONE; });
setup_logfile(false); 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; return;
exec_common_scripts("service"); exec_common_scripts("service");
@ -346,13 +352,16 @@ void late_start(int client) {
} }
void boot_complete(int client) { void boot_complete(int client) {
LOGI("** boot_complete triggered\n");
// ack // ack
write_int(client, 0); write_int(client, 0);
close(client); close(client);
mutex_guard lock(stage_lock);
DAEMON_STATE = STATE_BOOT_COMPLETE;
setup_logfile(false); setup_logfile(false);
LOGI("** boot_complete triggered\n");
if (safe_mode) if (safe_mode)
return; return;
@ -360,8 +369,7 @@ void boot_complete(int client) {
if (access(SECURE_DIR, F_OK) != 0) if (access(SECURE_DIR, F_OK) != 0)
xmkdir(SECURE_DIR, 0700); xmkdir(SECURE_DIR, 0700);
if (pfs_done) auto_start_magiskhide();
auto_start_magiskhide();
if (!check_manager()) { if (!check_manager()) {
if (access(MANAGERAPK, F_OK) == 0) { if (access(MANAGERAPK, F_OK) == 0) {

View File

@ -21,7 +21,7 @@ using namespace std;
int SDK_INT = -1; int SDK_INT = -1;
bool RECOVERY_MODE = false; bool RECOVERY_MODE = false;
string MAGISKTMP; string MAGISKTMP;
int DAEMON_STATE = STATE_UNKNOWN; int DAEMON_STATE = STATE_NONE;
static struct stat self_st; static struct stat self_st;
@ -99,19 +99,8 @@ static void handle_request(int client) {
break; 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 // Simple requests
switch (req_code) {
case CHECK_VERSION: case CHECK_VERSION:
write_string(client, MAGISK_VERSION ":MAGISK"); write_string(client, MAGISK_VERSION ":MAGISK");
goto shortcut; goto shortcut;

View File

@ -32,10 +32,11 @@ enum {
// Daemon state // Daemon state
enum { enum {
STATE_NONE,
STATE_POST_FS_DATA, STATE_POST_FS_DATA,
STATE_LATE_START, STATE_POST_FS_DATA_DONE,
STATE_BOOT_COMPLETE, STATE_LATE_START_DONE,
STATE_UNKNOWN STATE_BOOT_COMPLETE
}; };
extern int SDK_INT; extern int SDK_INT;

View File

@ -247,7 +247,7 @@ int launch_magiskhide() {
return DAEMON_ERROR; return DAEMON_ERROR;
hide_sensitive_props(); hide_sensitive_props();
if (DAEMON_STATE >= STATE_BOOT_COMPLETE) if (DAEMON_STATE >= STATE_BOOT_COMPLETE || DAEMON_STATE == STATE_NONE)
hide_late_sensitive_props(); hide_late_sensitive_props();
// Start monitoring // Start monitoring