From 490a784993cb2e3ef202c0291d547de3cd2ad601 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 28 May 2022 22:39:44 -0700 Subject: [PATCH] Handle zygote restarts --- native/jni/core/bootstages.cpp | 11 ++++++++++- native/jni/core/core.hpp | 1 + native/jni/core/daemon.cpp | 4 ++++ native/jni/core/magisk.cpp | 8 +++++--- native/jni/core/package.cpp | 2 +- native/jni/include/daemon.hpp | 3 +++ native/jni/init/magiskrc.inc | 8 ++++++++ native/jni/su/su_daemon.cpp | 2 +- 8 files changed, 33 insertions(+), 6 deletions(-) diff --git a/native/jni/core/bootstages.cpp b/native/jni/core/bootstages.cpp index 5d80057a0..5cc305a2c 100644 --- a/native/jni/core/bootstages.cpp +++ b/native/jni/core/bootstages.cpp @@ -356,7 +356,7 @@ void boot_complete(int client) { DAEMON_STATE = STATE_BOOT_COMPLETE; setup_logfile(false); - LOGI("** boot_complete triggered\n"); + LOGI("** boot-complete triggered\n"); if (safe_mode) return; @@ -366,5 +366,14 @@ void boot_complete(int client) { xmkdir(SECURE_DIR, 0700); // Ensure manager exists + need_pkg_refresh(); get_manager(0, nullptr, true); } + +void zygote_restart(int client) { + close(client); + + LOGI("** zygote restarted\n"); + pkg_xml_ino = 0; + prune_su_access(); +} diff --git a/native/jni/core/core.hpp b/native/jni/core/core.hpp index ae9d7cc32..b07b0bd8c 100644 --- a/native/jni/core/core.hpp +++ b/native/jni/core/core.hpp @@ -5,6 +5,7 @@ extern bool RECOVERY_MODE; extern int DAEMON_STATE; +extern std::atomic pkg_xml_ino; // Daemon state enum : int { diff --git a/native/jni/core/daemon.cpp b/native/jni/core/daemon.cpp index eb2107c42..384791bde 100644 --- a/native/jni/core/daemon.cpp +++ b/native/jni/core/daemon.cpp @@ -150,6 +150,9 @@ static void handle_request_async(int client, int code, const sock_cred &cred) { case MainRequest::BOOT_COMPLETE: boot_complete(client); break; + case MainRequest::ZYGOTE_RESTART: + zygote_restart(client); + break; case MainRequest::SQLITE_CMD: exec_sql(client); break; @@ -233,6 +236,7 @@ static void handle_request(pollfd *pfd) { case MainRequest::POST_FS_DATA: case MainRequest::LATE_START: case MainRequest::BOOT_COMPLETE: + case MainRequest::ZYGOTE_RESTART: case MainRequest::SQLITE_CMD: case MainRequest::GET_PATH: case MainRequest::DENYLIST: diff --git a/native/jni/core/magisk.cpp b/native/jni/core/magisk.cpp index cee5033f7..114de3ce0 100644 --- a/native/jni/core/magisk.cpp +++ b/native/jni/core/magisk.cpp @@ -29,9 +29,8 @@ Options: Advanced Options (Internal APIs): --daemon manually start magisk daemon --stop remove all magisk changes and stop daemon - --[init trigger] start service for init trigger - Supported init triggers: - post-fs-data, service, boot-complete + --[init trigger] callback on init triggers. Valid triggers: + post-fs-data, service, boot-complete, zygote-restart --unlock-blocks set BLKROSET flag to OFF for all block devices --restorecon restore selinux context on Magisk files --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) { close(connect_daemon(MainRequest::BOOT_COMPLETE)); return 0; + } else if (argv[1] == "--zygote-restart"sv) { + close(connect_daemon(MainRequest::ZYGOTE_RESTART)); + return 0; } else if (argv[1] == "--denylist"sv) { return denylist_cli(argc - 1, argv + 1); } else if (argc >= 3 && argv[1] == "--sqlite"sv) { diff --git a/native/jni/core/package.cpp b/native/jni/core/package.cpp index 8a5d3c145..1c3d2b8da 100644 --- a/native/jni/core/package.cpp +++ b/native/jni/core/package.cpp @@ -14,7 +14,7 @@ using namespace std; // so performance is absolutely critical. Most operations should either have its result cached // or simply skipped unless necessary. -static atomic pkg_xml_ino = 0; +atomic pkg_xml_ino = 0; static atomic_flag skip_check; static pthread_mutex_t pkg_lock = PTHREAD_MUTEX_INITIALIZER; diff --git a/native/jni/include/daemon.hpp b/native/jni/include/daemon.hpp index 9ba91896c..4dccb75a4 100644 --- a/native/jni/include/daemon.hpp +++ b/native/jni/include/daemon.hpp @@ -28,6 +28,7 @@ enum : int { POST_FS_DATA, LATE_START, BOOT_COMPLETE, + ZYGOTE_RESTART, DENYLIST, SQLITE_CMD, REMOVE_MODULES, @@ -81,6 +82,7 @@ void android_logging(); void post_fs_data(int client); void late_start(int client); void boot_complete(int client); +void zygote_restart(int client); void denylist_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); @@ -90,6 +92,7 @@ void preserve_stub_apk(); bool need_pkg_refresh(); std::vector get_app_no_list(); int get_manager(int user_id = 0, std::string *pkg = nullptr, bool install = false); +void prune_su_access(); // Denylist void initialize_denylist(); diff --git a/native/jni/init/magiskrc.inc b/native/jni/init/magiskrc.inc index ae9771ecb..e8dd418db 100644 --- a/native/jni/init/magiskrc.inc +++ b/native/jni/init/magiskrc.inc @@ -31,4 +31,12 @@ constexpr char MAGISK_RC[] = "on property:sys.boot_completed=1\n" " exec %1$s/magisk --boot-complete\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" ; diff --git a/native/jni/su/su_daemon.cpp b/native/jni/su/su_daemon.cpp index f163af33e..f9411b3c0 100644 --- a/native/jni/su/su_daemon.cpp +++ b/native/jni/su/su_daemon.cpp @@ -137,7 +137,7 @@ bool uid_granted_root(int uid) { return granted; } -static void prune_su_access() { +void prune_su_access() { vector app_no_list = get_app_no_list(); vector rm_uids; char query[256], *err;