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;
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) {

View File

@ -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;

View File

@ -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;

View File

@ -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