Scan for zygote periodically

Fix #3417
This commit is contained in:
topjohnwu 2020-11-08 03:44:23 -08:00
parent b14a260827
commit 1e45c63ea5
3 changed files with 26 additions and 11 deletions

View File

@ -275,7 +275,7 @@ int stop_magiskhide() {
void auto_start_magiskhide() { void auto_start_magiskhide() {
if (hide_enabled()) { if (hide_enabled()) {
pthread_kill(proc_monitor_thread, SIGZYGOTE); pthread_kill(proc_monitor_thread, SIGALRM);
hide_late_sensitive_props(); hide_late_sensitive_props();
} else if (SDK_INT >= 19) { } else if (SDK_INT >= 19) {
db_settings dbs; db_settings dbs;

View File

@ -13,7 +13,6 @@
#include <daemon.hpp> #include <daemon.hpp>
#define SIGTERMTHRD SIGUSR1 #define SIGTERMTHRD SIGUSR1
#define SIGZYGOTE SIGUSR2
// CLI entries // CLI entries
int launch_magiskhide(); int launch_magiskhide();

View File

@ -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() { static void check_zygote() {
crawl_procfs([](int pid) -> bool { crawl_procfs([](int pid) -> bool {
char buf[512]; char buf[512];
@ -119,6 +127,12 @@ static void check_zygote() {
} }
return true; 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" #define APP_PROC "/system/bin/app_process"
@ -171,10 +185,6 @@ static void inotify_event(int) {
check_zygote(); check_zygote();
} }
static void check_zygote(int) {
check_zygote();
}
// Workaround for the lack of pthread_cancel // Workaround for the lack of pthread_cancel
static void term_thread(int) { static void term_thread(int) {
LOGD("proc_monitor: cleaning up\n"); LOGD("proc_monitor: cleaning up\n");
@ -323,6 +333,7 @@ void proc_monitor() {
sigemptyset(&block_set); sigemptyset(&block_set);
sigaddset(&block_set, SIGTERMTHRD); sigaddset(&block_set, SIGTERMTHRD);
sigaddset(&block_set, SIGIO); sigaddset(&block_set, SIGIO);
sigaddset(&block_set, SIGALRM);
pthread_sigmask(SIG_UNBLOCK, &block_set, nullptr); pthread_sigmask(SIG_UNBLOCK, &block_set, nullptr);
struct sigaction act{}; struct sigaction act{};
@ -330,13 +341,19 @@ void proc_monitor() {
sigaction(SIGTERMTHRD, &act, nullptr); sigaction(SIGTERMTHRD, &act, nullptr);
act.sa_handler = inotify_event; act.sa_handler = inotify_event;
sigaction(SIGIO, &act, nullptr); sigaction(SIGIO, &act, nullptr);
act.sa_handler = check_zygote; act.sa_handler = [](int){ check_zygote(); };
sigaction(SIGZYGOTE, &act, nullptr); sigaction(SIGALRM, &act, nullptr);
setup_inotify(); setup_inotify();
// First find existing zygotes // First try find existing zygotes
check_zygote(); 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; int status;
@ -344,8 +361,7 @@ void proc_monitor() {
const int pid = waitpid(-1, &status, __WALL | __WNOTHREAD); const int pid = waitpid(-1, &status, __WALL | __WNOTHREAD);
if (pid < 0) { if (pid < 0) {
if (errno == ECHILD) { if (errno == ECHILD) {
/* This mean we have nothing to wait, sleep // Nothing to wait yet, sleep and wait till signal interruption
* and wait till signal interruption */
LOGD("proc_monitor: nothing to monitor, wait for signal\n"); LOGD("proc_monitor: nothing to monitor, wait for signal\n");
struct timespec ts = { struct timespec ts = {
.tv_sec = INT_MAX, .tv_sec = INT_MAX,