From 21984fac8b2ab542b69cb462b08aaa38308e266c Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 25 May 2019 16:08:53 -0700 Subject: [PATCH] Add API for running independent proc_monitor test --- native/jni/magiskhide/hide_utils.cpp | 7 +++++++ native/jni/magiskhide/magiskhide.cpp | 17 +++++++++++------ native/jni/magiskhide/magiskhide.h | 1 + native/jni/magiskhide/proc_monitor.cpp | 23 +++++++---------------- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index b95c40953..839155111 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -328,3 +328,10 @@ void auto_start_magiskhide() { }); } } + +void test_proc_monitor() { + if (procfp == nullptr && (procfp = opendir("/proc")) == nullptr) + exit(1); + proc_monitor(); + exit(0); +} diff --git a/native/jni/magiskhide/magiskhide.cpp b/native/jni/magiskhide/magiskhide.cpp index 12fae8039..11f601f5d 100644 --- a/native/jni/magiskhide/magiskhide.cpp +++ b/native/jni/magiskhide/magiskhide.cpp @@ -13,6 +13,8 @@ #include "magiskhide.h" +using namespace std::literals; + bool hide_enabled = false; [[noreturn]] static void usage(char *arg0) { @@ -26,6 +28,7 @@ bool hide_enabled = false; " --add PKG [PROC] Add a new target to the hide list\n" " --rm PKG [PROC] Remove from the hide list\n" " --ls List the current hide list\n" + " --test Run process monitor test\n" , arg0); exit(1); } @@ -77,18 +80,20 @@ int magiskhide_main(int argc, char *argv[]) { usage(argv[0]); int req; - if (strcmp(argv[1], "--enable") == 0) + if (argv[1] == "--enable"sv) req = LAUNCH_MAGISKHIDE; - else if (strcmp(argv[1], "--disable") == 0) + else if (argv[1] == "--disable"sv) req = STOP_MAGISKHIDE; - else if (strcmp(argv[1], "--add") == 0 && argc > 2) + else if (argv[1] == "--add"sv) req = ADD_HIDELIST; - else if (strcmp(argv[1], "--rm") == 0 && argc > 2) + else if (argv[1] == "--rm"sv) req = RM_HIDELIST; - else if (strcmp(argv[1], "--ls") == 0) + else if (argv[1] == "--ls"sv) req = LS_HIDELIST; - else if (strcmp(argv[1], "--status") == 0) + else if (argv[1] == "--status"sv) req = HIDE_STATUS; + else if (argv[1] == "--test"sv) + test_proc_monitor(); else usage(argv[0]); diff --git a/native/jni/magiskhide/magiskhide.h b/native/jni/magiskhide/magiskhide.h index e60ba8a51..f1bfee153 100644 --- a/native/jni/magiskhide/magiskhide.h +++ b/native/jni/magiskhide/magiskhide.h @@ -25,6 +25,7 @@ int stop_magiskhide(); int add_list(int client); int rm_list(int client); void ls_list(int client); +[[noreturn]] void test_proc_monitor(); // Process monitoring void proc_monitor(); diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index 0881112c8..17f347e1c 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -1,12 +1,3 @@ -/* proc_monitor.cpp - Monitor am_proc_start events and unmount - * - * We monitor the listed APK files from /data/app until they get opened - * via inotify to detect a new app launch. - * - * If it's a target we pause it ASAP, and fork a new process to join - * its mount namespace and do all the unmounting/mocking. - */ - #include #include #include @@ -188,9 +179,6 @@ static void setup_inotify() { } else { inotify_add_watch(inotify_fd, APP_PROC, IN_ACCESS); } - - // First find existing zygotes - check_zygote(); } /************************* @@ -291,12 +279,12 @@ static void term_thread(int) { * Ptrace Madness ******************/ -/* Ptrace is super tricky, preserve all excessive debug in code +/* Ptrace is super tricky, preserve all excessive logging in code * but disable when actually building for usage (you won't want * your logcat spammed with new thread events from all apps) */ -//#define PTRACE_LOG(fmt, args...) LOGD("PID=[%d] " fmt, pid, ##args) -#define PTRACE_LOG(...) +#define PTRACE_LOG(fmt, args...) LOGD("PID=[%d] " fmt, pid, ##args) +//#define PTRACE_LOG(...) static void detach_pid(int pid, int signal = 0) { char path[128]; @@ -371,7 +359,7 @@ static bool check_pid(int pid) { } } } - PTRACE_LOG("not our target\n"); + PTRACE_LOG("[%s] not our target\n", cmdline); detach_pid(pid); return true; } @@ -414,6 +402,9 @@ void proc_monitor() { setup_inotify(); + // First find existing zygotes + check_zygote(); + int status; for (;;) {