Handle zygote restarts

This commit is contained in:
topjohnwu 2022-05-28 22:39:44 -07:00
parent 9c774f96db
commit 490a784993
8 changed files with 33 additions and 6 deletions

View File

@ -356,7 +356,7 @@ void boot_complete(int client) {
DAEMON_STATE = STATE_BOOT_COMPLETE; DAEMON_STATE = STATE_BOOT_COMPLETE;
setup_logfile(false); setup_logfile(false);
LOGI("** boot_complete triggered\n"); LOGI("** boot-complete triggered\n");
if (safe_mode) if (safe_mode)
return; return;
@ -366,5 +366,14 @@ void boot_complete(int client) {
xmkdir(SECURE_DIR, 0700); xmkdir(SECURE_DIR, 0700);
// Ensure manager exists // Ensure manager exists
need_pkg_refresh();
get_manager(0, nullptr, true); get_manager(0, nullptr, true);
} }
void zygote_restart(int client) {
close(client);
LOGI("** zygote restarted\n");
pkg_xml_ino = 0;
prune_su_access();
}

View File

@ -5,6 +5,7 @@
extern bool RECOVERY_MODE; extern bool RECOVERY_MODE;
extern int DAEMON_STATE; extern int DAEMON_STATE;
extern std::atomic<ino_t> pkg_xml_ino;
// Daemon state // Daemon state
enum : int { enum : int {

View File

@ -150,6 +150,9 @@ static void handle_request_async(int client, int code, const sock_cred &cred) {
case MainRequest::BOOT_COMPLETE: case MainRequest::BOOT_COMPLETE:
boot_complete(client); boot_complete(client);
break; break;
case MainRequest::ZYGOTE_RESTART:
zygote_restart(client);
break;
case MainRequest::SQLITE_CMD: case MainRequest::SQLITE_CMD:
exec_sql(client); exec_sql(client);
break; break;
@ -233,6 +236,7 @@ static void handle_request(pollfd *pfd) {
case MainRequest::POST_FS_DATA: case MainRequest::POST_FS_DATA:
case MainRequest::LATE_START: case MainRequest::LATE_START:
case MainRequest::BOOT_COMPLETE: case MainRequest::BOOT_COMPLETE:
case MainRequest::ZYGOTE_RESTART:
case MainRequest::SQLITE_CMD: case MainRequest::SQLITE_CMD:
case MainRequest::GET_PATH: case MainRequest::GET_PATH:
case MainRequest::DENYLIST: case MainRequest::DENYLIST:

View File

@ -29,9 +29,8 @@ Options:
Advanced Options (Internal APIs): Advanced Options (Internal APIs):
--daemon manually start magisk daemon --daemon manually start magisk daemon
--stop remove all magisk changes and stop daemon --stop remove all magisk changes and stop daemon
--[init trigger] start service for init trigger --[init trigger] callback on init triggers. Valid triggers:
Supported init triggers: post-fs-data, service, boot-complete, zygote-restart
post-fs-data, service, boot-complete
--unlock-blocks set BLKROSET flag to OFF for all block devices --unlock-blocks set BLKROSET flag to OFF for all block devices
--restorecon restore selinux context on Magisk files --restorecon restore selinux context on Magisk files
--clone-attr SRC DEST clone permission, owner, and selinux context --clone-attr SRC DEST clone permission, owner, and selinux context
@ -95,6 +94,9 @@ int magisk_main(int argc, char *argv[]) {
} else if (argv[1] == "--boot-complete"sv) { } else if (argv[1] == "--boot-complete"sv) {
close(connect_daemon(MainRequest::BOOT_COMPLETE)); close(connect_daemon(MainRequest::BOOT_COMPLETE));
return 0; return 0;
} else if (argv[1] == "--zygote-restart"sv) {
close(connect_daemon(MainRequest::ZYGOTE_RESTART));
return 0;
} else if (argv[1] == "--denylist"sv) { } else if (argv[1] == "--denylist"sv) {
return denylist_cli(argc - 1, argv + 1); return denylist_cli(argc - 1, argv + 1);
} else if (argc >= 3 && argv[1] == "--sqlite"sv) { } else if (argc >= 3 && argv[1] == "--sqlite"sv) {

View File

@ -14,7 +14,7 @@ using namespace std;
// so performance is absolutely critical. Most operations should either have its result cached // so performance is absolutely critical. Most operations should either have its result cached
// or simply skipped unless necessary. // or simply skipped unless necessary.
static atomic<ino_t> pkg_xml_ino = 0; atomic<ino_t> pkg_xml_ino = 0;
static atomic_flag skip_check; static atomic_flag skip_check;
static pthread_mutex_t pkg_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t pkg_lock = PTHREAD_MUTEX_INITIALIZER;

View File

@ -28,6 +28,7 @@ enum : int {
POST_FS_DATA, POST_FS_DATA,
LATE_START, LATE_START,
BOOT_COMPLETE, BOOT_COMPLETE,
ZYGOTE_RESTART,
DENYLIST, DENYLIST,
SQLITE_CMD, SQLITE_CMD,
REMOVE_MODULES, REMOVE_MODULES,
@ -81,6 +82,7 @@ void android_logging();
void post_fs_data(int client); void post_fs_data(int client);
void late_start(int client); void late_start(int client);
void boot_complete(int client); void boot_complete(int client);
void zygote_restart(int client);
void denylist_handler(int client, const sock_cred *cred); void denylist_handler(int client, const sock_cred *cred);
void su_daemon_handler(int client, const sock_cred *cred); void su_daemon_handler(int client, const sock_cred *cred);
void zygisk_handler(int client, const sock_cred *cred); void zygisk_handler(int client, const sock_cred *cred);
@ -90,6 +92,7 @@ void preserve_stub_apk();
bool need_pkg_refresh(); bool need_pkg_refresh();
std::vector<bool> get_app_no_list(); std::vector<bool> get_app_no_list();
int get_manager(int user_id = 0, std::string *pkg = nullptr, bool install = false); int get_manager(int user_id = 0, std::string *pkg = nullptr, bool install = false);
void prune_su_access();
// Denylist // Denylist
void initialize_denylist(); void initialize_denylist();

View File

@ -31,4 +31,12 @@ constexpr char MAGISK_RC[] =
"on property:sys.boot_completed=1\n" "on property:sys.boot_completed=1\n"
" exec %1$s/magisk --boot-complete\n" " exec %1$s/magisk --boot-complete\n"
"\n" "\n"
"on property:init.svc.zygote=restarting\n"
" exec %1$s/magisk --zygote-restart\n"
"\n"
"on property:init.svc.zygote=stopped\n"
" exec %1$s/magisk --zygote-restart\n"
"\n"
; ;

View File

@ -137,7 +137,7 @@ bool uid_granted_root(int uid) {
return granted; return granted;
} }
static void prune_su_access() { void prune_su_access() {
vector<bool> app_no_list = get_app_no_list(); vector<bool> app_no_list = get_app_no_list();
vector<int> rm_uids; vector<int> rm_uids;
char query[256], *err; char query[256], *err;