From a3a1aed723b4aebec0a6d3c7fd2e132b2bb44930 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 27 May 2019 16:27:19 -0700 Subject: [PATCH] Don't check zygote in busy loop --- native/jni/magiskhide/proc_monitor.cpp | 41 +++++++++----------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index f2a82b8d0..f68f93960 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -123,25 +123,17 @@ void update_uid_map() { } static void check_zygote() { - int min_zyg = 1; - if (access("/system/bin/app_process64", R_OK) == 0) - min_zyg = 2; - for (;;) { - crawl_procfs([](int pid) -> bool { - char buf[512]; - snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); - if (FILE *f = fopen(buf, "re"); f) { - fgets(buf, sizeof(buf), f); - if (strncmp(buf, "zygote", 6) == 0 && parse_ppid(pid) == 1) - new_zygote(pid); - fclose(f); - } - return true; - }); - if (zygote_map.size() >= min_zyg) - break; - usleep(10000); - } + crawl_procfs([](int pid) -> bool { + char buf[512]; + snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); + if (FILE *f = fopen(buf, "re"); f) { + fgets(buf, sizeof(buf), f); + if (strncmp(buf, "zygote", 6) == 0 && parse_ppid(pid) == 1) + new_zygote(pid); + fclose(f); + } + return true; + }); } #define APP_PROC "/system/bin/app_process" @@ -189,14 +181,9 @@ static void inotify_event(int) { char buf[512]; auto event = reinterpret_cast(buf); read(inotify_fd, buf, sizeof(buf)); - if ((event->mask & IN_CLOSE_WRITE) && strcmp(event->name, "packages.xml") == 0) { - LOGD("proc_monitor: /data/system/packages.xml updated\n"); - uid_proc_map.clear(); - file_readline("/data/system/packages.xml", parse_packages_xml, true); - } else if (event->mask & IN_ACCESS) { - LOGD("proc_monitor: app_process access\n"); - check_zygote(); - } + if ((event->mask & IN_CLOSE_WRITE) && event->name == "packages.xml"sv) + update_uid_map(); + check_zygote(); } // Workaround for the lack of pthread_cancel