From 1e45c63ea5c7685e488625d42c14810f0cdcd3a6 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 8 Nov 2020 03:44:23 -0800 Subject: [PATCH] Scan for zygote periodically Fix #3417 --- native/jni/magiskhide/hide_utils.cpp | 2 +- native/jni/magiskhide/magiskhide.hpp | 1 - native/jni/magiskhide/proc_monitor.cpp | 34 +++++++++++++++++++------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index fd8f8d611..66fc629b3 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -275,7 +275,7 @@ int stop_magiskhide() { void auto_start_magiskhide() { if (hide_enabled()) { - pthread_kill(proc_monitor_thread, SIGZYGOTE); + pthread_kill(proc_monitor_thread, SIGALRM); hide_late_sensitive_props(); } else if (SDK_INT >= 19) { db_settings dbs; diff --git a/native/jni/magiskhide/magiskhide.hpp b/native/jni/magiskhide/magiskhide.hpp index a3841defd..75a07d648 100644 --- a/native/jni/magiskhide/magiskhide.hpp +++ b/native/jni/magiskhide/magiskhide.hpp @@ -13,7 +13,6 @@ #include #define SIGTERMTHRD SIGUSR1 -#define SIGZYGOTE SIGUSR2 // CLI entries int launch_magiskhide(); diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index e5914f3fa..72ee8dbb2 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -107,6 +107,14 @@ void update_uid_map() { } } +static bool is_zygote_done() { +#ifdef __LP64__ + return zygote_map.size() >= 2; +#else + return zygote_map.size() >= 1; +#endif +} + static void check_zygote() { crawl_procfs([](int pid) -> bool { char buf[512]; @@ -119,6 +127,12 @@ static void check_zygote() { } return true; }); + if (is_zygote_done()) { + // Stop periodic scanning + timeval val { .tv_sec = 0, .tv_usec = 0 }; + itimerval interval { .it_interval = val, .it_value = val }; + setitimer(ITIMER_REAL, &interval, nullptr); + } } #define APP_PROC "/system/bin/app_process" @@ -171,10 +185,6 @@ static void inotify_event(int) { check_zygote(); } -static void check_zygote(int) { - check_zygote(); -} - // Workaround for the lack of pthread_cancel static void term_thread(int) { LOGD("proc_monitor: cleaning up\n"); @@ -323,6 +333,7 @@ void proc_monitor() { sigemptyset(&block_set); sigaddset(&block_set, SIGTERMTHRD); sigaddset(&block_set, SIGIO); + sigaddset(&block_set, SIGALRM); pthread_sigmask(SIG_UNBLOCK, &block_set, nullptr); struct sigaction act{}; @@ -330,13 +341,19 @@ void proc_monitor() { sigaction(SIGTERMTHRD, &act, nullptr); act.sa_handler = inotify_event; sigaction(SIGIO, &act, nullptr); - act.sa_handler = check_zygote; - sigaction(SIGZYGOTE, &act, nullptr); + act.sa_handler = [](int){ check_zygote(); }; + sigaction(SIGALRM, &act, nullptr); setup_inotify(); - // First find existing zygotes + // First try find existing zygotes check_zygote(); + if (!is_zygote_done()) { + // Periodic scan every 250ms + timeval val { .tv_sec = 0, .tv_usec = 250000 }; + itimerval interval { .it_interval = val, .it_value = val }; + setitimer(ITIMER_REAL, &interval, nullptr); + } int status; @@ -344,8 +361,7 @@ void proc_monitor() { const int pid = waitpid(-1, &status, __WALL | __WNOTHREAD); if (pid < 0) { if (errno == ECHILD) { - /* This mean we have nothing to wait, sleep - * and wait till signal interruption */ + // Nothing to wait yet, sleep and wait till signal interruption LOGD("proc_monitor: nothing to monitor, wait for signal\n"); struct timespec ts = { .tv_sec = INT_MAX,