From e00e6509ee769c4f8b91314c35302d83c9d53858 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 22 Apr 2017 01:40:07 +0800 Subject: [PATCH] Add error code for magiskhide --- jni/magiskhide/list_manager.c | 31 ++++++++++++++++++++----------- jni/magiskhide/magiskhide.c | 32 ++++++++++++++++++++++++-------- jni/magiskhide/magiskhide.h | 9 +++++++++ jni/magiskhide/proc_monitor.c | 2 +- jni/utils/vector.c | 4 ++++ 5 files changed, 58 insertions(+), 20 deletions(-) diff --git a/jni/magiskhide/list_manager.c b/jni/magiskhide/list_manager.c index 8e9a2340c..bd3d94e02 100644 --- a/jni/magiskhide/list_manager.c +++ b/jni/magiskhide/list_manager.c @@ -11,14 +11,16 @@ #include "magiskhide.h" int add_list(char *proc) { - if (!hideEnabled) - return 1; + if (!hideEnabled) { + free(proc); + return HIDE_NOT_ENABLED; + } char *line; struct vector *new_list, *temp = hide_list; new_list = xmalloc(sizeof(*new_list)); if (new_list == NULL) - return 1; + return HIDE_ERROR; vec_init(new_list); vec_for_each(hide_list, line) { @@ -27,7 +29,7 @@ int add_list(char *proc) { free(proc); vec_destroy(new_list); free(new_list); - return 2; + return HIDE_ITEM_EXIST; } vec_push_back(new_list, line); } @@ -45,19 +47,22 @@ int add_list(char *proc) { vec_destroy(temp); free(temp); if (vector_to_file(HIDELIST, hide_list)) - return 1; - return 0; + return HIDE_ERROR; + return HIDE_SUCCESS; } int rm_list(char *proc) { - if (!hideEnabled) - return 1; + if (!hideEnabled) { + free(proc); + return HIDE_NOT_ENABLED; + } + hide_ret ret = HIDE_ERROR; char *line; struct vector *new_list, *temp; temp = new_list = xmalloc(sizeof(*new_list)); if (new_list == NULL) - return 1; + goto error; vec_init(new_list); vec_for_each(hide_list, line) { @@ -77,14 +82,18 @@ int rm_list(char *proc) { pthread_mutex_lock(&hide_lock); hide_list = new_list; pthread_mutex_unlock(&hide_lock); + ret = HIDE_SUCCESS; if (vector_to_file(HIDELIST, hide_list)) - return 1; + ret = HIDE_ERROR; + } else { + ret = HIDE_ITEM_NOT_EXIST; } +error: free(proc); vec_destroy(temp); free(temp); - return 0; + return ret; } int init_list() { diff --git a/jni/magiskhide/magiskhide.c b/jni/magiskhide/magiskhide.c index a279276e2..69d94842b 100644 --- a/jni/magiskhide/magiskhide.c +++ b/jni/magiskhide/magiskhide.c @@ -44,7 +44,7 @@ static void usage(char *arg0) { void launch_magiskhide(int client) { if (hideEnabled) { - write_int(client, 0); + write_int(client, HIDE_IS_ENABLED); close(client); return; } @@ -79,7 +79,7 @@ void launch_magiskhide(int client) { // Initialize the mutex lock pthread_mutex_init(&hide_lock, NULL); - write_int(client, 0); + write_int(client, HIDE_SUCCESS); close(client); // Get thread reference @@ -90,7 +90,7 @@ void launch_magiskhide(int client) { error: hideEnabled = 0; - write_int(client, 1); + write_int(client, HIDE_ERROR); close(client); if (hide_pid != -1) { int kill = -1; @@ -105,7 +105,7 @@ error: void stop_magiskhide(int client) { if (!hideEnabled) { - write_int(client, 0); + write_int(client, HIDE_NOT_ENABLED); close(client); return; } @@ -116,7 +116,7 @@ void stop_magiskhide(int client) { setprop("persist.magisk.hide", "0"); pthread_kill(proc_monitor_thread, SIGUSR1); - write_int(client, 0); + write_int(client, HIDE_SUCCESS); close(client); } @@ -147,10 +147,26 @@ int magiskhide_main(int argc, char *argv[]) { if (req == ADD_HIDELIST || req == RM_HIDELIST) { write_string(fd, argv[2]); } - int code = read_int(fd); + hide_ret code = read_int(fd); close(fd); - if (code) { - fprintf(stderr, "Error occured in MagiskHide daemon\n"); + switch (code) { + case HIDE_ERROR: + fprintf(stderr, "Error occured in daemon...\n"); + break; + case HIDE_SUCCESS: + break; + case HIDE_NOT_ENABLED: + fprintf(stderr, "Magisk hide is not enabled yet\n"); + break; + case HIDE_IS_ENABLED: + fprintf(stderr, "Magisk hide is already enabled\n"); + break; + case HIDE_ITEM_EXIST: + fprintf(stderr, "Process [%s] already exists in hide list\n", argv[2]); + break; + case HIDE_ITEM_NOT_EXIST: + fprintf(stderr, "Process [%s] does not exist in hide list\n", argv[2]); + break; } return code; } diff --git a/jni/magiskhide/magiskhide.h b/jni/magiskhide/magiskhide.h index 4565f4579..e1f6d05aa 100644 --- a/jni/magiskhide/magiskhide.h +++ b/jni/magiskhide/magiskhide.h @@ -8,6 +8,15 @@ #define ENFORCE_FILE "/sys/fs/selinux/enforce" #define POLICY_FILE "/sys/fs/selinux/policy" +typedef enum { + HIDE_ERROR = -1, + HIDE_SUCCESS = 0, + HIDE_IS_ENABLED, + HIDE_NOT_ENABLED, + HIDE_ITEM_EXIST, + HIDE_ITEM_NOT_EXIST +} hide_ret; + // Kill process void kill_proc(int pid); diff --git a/jni/magiskhide/proc_monitor.c b/jni/magiskhide/proc_monitor.c index 049ae864c..3ee1c5540 100644 --- a/jni/magiskhide/proc_monitor.c +++ b/jni/magiskhide/proc_monitor.c @@ -141,7 +141,7 @@ void proc_monitor() { // Send pause signal ASAP if (kill(pid, SIGSTOP) == -1) continue; - LOGI("proc_monitor: %s(PID=%d ns=%s)\n", processName, pid, buffer); + LOGI("proc_monitor: %s (PID=%d ns=%s)\n", processName, pid, buffer); // Unmount start xwrite(sv[0], &pid, sizeof(pid)); diff --git a/jni/utils/vector.c b/jni/utils/vector.c index 2f19efcf7..63e5cafc9 100644 --- a/jni/utils/vector.c +++ b/jni/utils/vector.c @@ -6,6 +6,7 @@ #include "vector.h" void vec_init(struct vector *v) { + if (v == NULL) return; vec_size(v) = 0; vec_cap(v) = 1; vec_entry(v) = malloc(sizeof(void*)); @@ -22,6 +23,7 @@ void vec_push_back(struct vector *v, void *p) { } void vec_sort(struct vector *v, int (*compar)(const void *, const void *)) { + if (v == NULL) return; qsort(vec_entry(v), vec_size(v), sizeof(void*), compar); } @@ -29,6 +31,7 @@ void vec_sort(struct vector *v, int (*compar)(const void *, const void *)) { * use in cases when each element requires special cleanup */ void vec_destroy(struct vector *v) { + if (v == NULL) return; vec_size(v) = 0; vec_cap(v) = 0; free(vec_entry(v)); @@ -39,6 +42,7 @@ void vec_destroy(struct vector *v) { * Shall be the general case */ void vec_deep_destroy(struct vector *v) { + if (v == NULL) return; void *e; vec_for_each(v, e) { free(e);