From 3aad9d8166c8ca85ee03774bcafba863e9540799 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 16 Nov 2018 00:37:41 -0500 Subject: [PATCH] Add CLI to detect MagiskHide status --- .../full/java/com/topjohnwu/magisk/Const.java | 2 +- .../full/java/com/topjohnwu/magisk/Data.java | 8 +- native/jni/magiskhide/magiskhide.cpp | 85 ++++++++++--------- native/jni/magiskhide/magiskhide.h | 5 +- native/jni/magiskhide/proc_monitor.cpp | 2 +- 5 files changed, 58 insertions(+), 44 deletions(-) diff --git a/app/src/full/java/com/topjohnwu/magisk/Const.java b/app/src/full/java/com/topjohnwu/magisk/Const.java index fadecea29..a7d8a1584 100644 --- a/app/src/full/java/com/topjohnwu/magisk/Const.java +++ b/app/src/full/java/com/topjohnwu/magisk/Const.java @@ -44,11 +44,11 @@ public class Const { public static final int USER_ID = Process.myUid() / 100000; public static final class MAGISK_VER { - public static final int REMOVE_LEGACY_LINK = 1630; public static final int SEPOL_REFACTOR = 1640; public static final int FIX_ENV = 1650; public static final int DBVER_SIX = 17000; public static final int CMDLINE_DB = 17305; + public static final int HIDE_STATUS = 17315; } public static class ID { diff --git a/app/src/full/java/com/topjohnwu/magisk/Data.java b/app/src/full/java/com/topjohnwu/magisk/Data.java index b2bbabbf8..af6ce3efb 100644 --- a/app/src/full/java/com/topjohnwu/magisk/Data.java +++ b/app/src/full/java/com/topjohnwu/magisk/Data.java @@ -91,8 +91,12 @@ public class Data { try { magiskVersionString = ShellUtils.fastCmd("magisk -v").split(":")[0]; magiskVersionCode = Integer.parseInt(ShellUtils.fastCmd("magisk -V")); - String s = ShellUtils.fastCmd(("resetprop -p ") + Const.MAGISKHIDE_PROP); - magiskHide = s.isEmpty() || Integer.parseInt(s) != 0; + if (magiskVersionCode >= Const.MAGISK_VER.HIDE_STATUS) { + magiskHide = Shell.su("magiskhide --status").exec().isSuccess(); + } else { + String s = ShellUtils.fastCmd(("resetprop -p ") + Const.MAGISKHIDE_PROP); + magiskHide = s.isEmpty() || Integer.parseInt(s) != 0; + } } catch (NumberFormatException ignored) {} } diff --git a/native/jni/magiskhide/magiskhide.cpp b/native/jni/magiskhide/magiskhide.cpp index efd9a798f..4a1d09a0f 100644 --- a/native/jni/magiskhide/magiskhide.cpp +++ b/native/jni/magiskhide/magiskhide.cpp @@ -14,15 +14,16 @@ #include "resetprop.h" #include "flags.h" -int hide_enabled = 0; +bool hide_enabled = false; static pthread_t proc_monitor_thread; pthread_mutex_t list_lock; [[noreturn]] static void usage(char *arg0) { fprintf(stderr, - "MagiskHide v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Hide Magisk!\n\n" + "MagiskHide v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu)\n\n" "Usage: %s [--options [arguments...] ]\n\n" "Options:\n" + " --status Return the status of MagiskHide\n" " --enable Start magiskhide\n" " --disable Stop magiskhide\n" " --add PROCESS Add PROCESS to the hide list\n" @@ -43,7 +44,7 @@ int launch_magiskhide(int client) { return LOGCAT_DISABLED; } - hide_enabled = 1; + hide_enabled = true; LOGI("* Starting MagiskHide\n"); deleteprop(MAGISKHIDE_PROP, true); @@ -70,14 +71,14 @@ int launch_magiskhide(int client) { proc_monitor(); error: - hide_enabled = 0; + hide_enabled = false; return DAEMON_ERROR; } int stop_magiskhide() { LOGI("* Stopping MagiskHide\n"); - hide_enabled = 0; + hide_enabled = false; setprop(MAGISKHIDE_PROP, "0"); // Remove without actually removing persist props deleteprop(MAGISKHIDE_PROP); @@ -119,6 +120,9 @@ void magiskhide_handler(int client) { ls_list(client); client = -1; break; + case HIDE_STATUS: + res = hide_enabled ? HIDE_IS_ENABLED : HIDE_NOT_ENABLED; + break; } write_int(client, res); @@ -126,61 +130,66 @@ void magiskhide_handler(int client) { } int magiskhide_main(int argc, char *argv[]) { - if (argc < 2) { + if (argc < 2) usage(argv[0]); - } - int req; - if (strcmp(argv[1], "--enable") == 0) { - req = LAUNCH_MAGISKHIDE; - } else if (strcmp(argv[1], "--disable") == 0) { - req = STOP_MAGISKHIDE; - } else if (strcmp(argv[1], "--add") == 0 && argc > 2) { - req = ADD_HIDELIST; - } else if (strcmp(argv[1], "--rm") == 0 && argc > 2) { - req = RM_HIDELIST; - } else if (strcmp(argv[1], "--ls") == 0) { - req = LS_HIDELIST; - } else { - usage(argv[0]); - } + int req; + if (strcmp(argv[1], "--enable") == 0) + req = LAUNCH_MAGISKHIDE; + else if (strcmp(argv[1], "--disable") == 0) + req = STOP_MAGISKHIDE; + else if (strcmp(argv[1], "--add") == 0 && argc > 2) + req = ADD_HIDELIST; + else if (strcmp(argv[1], "--rm") == 0 && argc > 2) + req = RM_HIDELIST; + else if (strcmp(argv[1], "--ls") == 0) + req = LS_HIDELIST; + else if (strcmp(argv[1], "--status") == 0) + req = HIDE_STATUS; + else + usage(argv[0]); + + // Send request int fd = connect_daemon(); write_int(fd, MAGISKHIDE); write_int(fd, req); - if (req == ADD_HIDELIST || req == RM_HIDELIST) { + if (req == ADD_HIDELIST || req == RM_HIDELIST) write_string(fd, argv[2]); - } + + // Get response int code = read_int(fd); switch (code) { case DAEMON_SUCCESS: break; - case ROOT_REQUIRED: - fprintf(stderr, "Root is required for this operation\n"); - return code; case LOGCAT_DISABLED: fprintf(stderr, "Logcat is disabled, cannot start MagiskHide\n"); - return code; + break; case HIDE_NOT_ENABLED: - fprintf(stderr, "MagiskHide is not enabled yet\n"); - return code; + fprintf(stderr, "MagiskHide is not enabled\n"); + break; case HIDE_IS_ENABLED: - fprintf(stderr, "MagiskHide is already enabled\n"); - return code; + fprintf(stderr, "MagiskHide is enabled\n"); + break; case HIDE_ITEM_EXIST: fprintf(stderr, "Process [%s] already exists in hide list\n", argv[2]); - return code; + break; case HIDE_ITEM_NOT_EXIST: fprintf(stderr, "Process [%s] does not exist in hide list\n", argv[2]); - return code; + break; + + /* Errors */ + case ROOT_REQUIRED: + fprintf(stderr, "Root is required for this operation\n"); + break; case DAEMON_ERROR: default: fprintf(stderr, "Error occured in daemon...\n"); - return code; + return DAEMON_ERROR; } - if (req == LS_HIDELIST) { - int argc = read_int(fd); - for (int i = 0; i < argc; ++i) { + if (code == DAEMON_SUCCESS && req == LS_HIDELIST) { + int cnt = read_int(fd); + for (int i = 0; i < cnt; ++i) { char *s = read_string(fd); printf("%s\n", s); free(s); @@ -188,5 +197,5 @@ int magiskhide_main(int argc, char *argv[]) { } close(fd); - return 0; + return req == HIDE_STATUS ? (code == HIDE_IS_ENABLED ? 0 : 1) : code != DAEMON_SUCCESS; } diff --git a/native/jni/magiskhide/magiskhide.h b/native/jni/magiskhide/magiskhide.h index 3e743c2fd..ad57a4a3f 100644 --- a/native/jni/magiskhide/magiskhide.h +++ b/native/jni/magiskhide/magiskhide.h @@ -28,7 +28,7 @@ void clean_magisk_props(); int add_list(const char *proc); bool init_list(); -extern int hide_enabled; +extern bool hide_enabled; extern pthread_mutex_t list_lock; extern Vector hide_list; @@ -37,7 +37,8 @@ enum { STOP_MAGISKHIDE, ADD_HIDELIST, RM_HIDELIST, - LS_HIDELIST + LS_HIDELIST, + HIDE_STATUS, }; enum { diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index 56cf0669a..65943174f 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -29,7 +29,7 @@ extern char *system_block, *vendor_block, *magiskloop; static void term_thread(int) { LOGD("proc_monitor: running cleanup\n"); hide_list.clear(true); - hide_enabled = 0; + hide_enabled = false; close(sockfd); sockfd = -1; pthread_mutex_destroy(&list_lock);