Add CLI to detect MagiskHide status

This commit is contained in:
topjohnwu 2018-11-16 00:37:41 -05:00
parent 572e078d87
commit 3aad9d8166
5 changed files with 58 additions and 44 deletions

View File

@ -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 {

View File

@ -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) {}
}

View File

@ -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;
}

View File

@ -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<CharArray> hide_list;
@ -37,7 +37,8 @@ enum {
STOP_MAGISKHIDE,
ADD_HIDELIST,
RM_HIDELIST,
LS_HIDELIST
LS_HIDELIST,
HIDE_STATUS,
};
enum {

View File

@ -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);