Add error code for magiskhide

This commit is contained in:
topjohnwu 2017-04-22 01:40:07 +08:00
parent be5739508b
commit e00e6509ee
5 changed files with 58 additions and 20 deletions

View File

@ -11,14 +11,16 @@
#include "magiskhide.h" #include "magiskhide.h"
int add_list(char *proc) { int add_list(char *proc) {
if (!hideEnabled) if (!hideEnabled) {
return 1; free(proc);
return HIDE_NOT_ENABLED;
}
char *line; char *line;
struct vector *new_list, *temp = hide_list; struct vector *new_list, *temp = hide_list;
new_list = xmalloc(sizeof(*new_list)); new_list = xmalloc(sizeof(*new_list));
if (new_list == NULL) if (new_list == NULL)
return 1; return HIDE_ERROR;
vec_init(new_list); vec_init(new_list);
vec_for_each(hide_list, line) { vec_for_each(hide_list, line) {
@ -27,7 +29,7 @@ int add_list(char *proc) {
free(proc); free(proc);
vec_destroy(new_list); vec_destroy(new_list);
free(new_list); free(new_list);
return 2; return HIDE_ITEM_EXIST;
} }
vec_push_back(new_list, line); vec_push_back(new_list, line);
} }
@ -45,19 +47,22 @@ int add_list(char *proc) {
vec_destroy(temp); vec_destroy(temp);
free(temp); free(temp);
if (vector_to_file(HIDELIST, hide_list)) if (vector_to_file(HIDELIST, hide_list))
return 1; return HIDE_ERROR;
return 0; return HIDE_SUCCESS;
} }
int rm_list(char *proc) { int rm_list(char *proc) {
if (!hideEnabled) if (!hideEnabled) {
return 1; free(proc);
return HIDE_NOT_ENABLED;
}
hide_ret ret = HIDE_ERROR;
char *line; char *line;
struct vector *new_list, *temp; struct vector *new_list, *temp;
temp = new_list = xmalloc(sizeof(*new_list)); temp = new_list = xmalloc(sizeof(*new_list));
if (new_list == NULL) if (new_list == NULL)
return 1; goto error;
vec_init(new_list); vec_init(new_list);
vec_for_each(hide_list, line) { vec_for_each(hide_list, line) {
@ -77,14 +82,18 @@ int rm_list(char *proc) {
pthread_mutex_lock(&hide_lock); pthread_mutex_lock(&hide_lock);
hide_list = new_list; hide_list = new_list;
pthread_mutex_unlock(&hide_lock); pthread_mutex_unlock(&hide_lock);
ret = HIDE_SUCCESS;
if (vector_to_file(HIDELIST, hide_list)) if (vector_to_file(HIDELIST, hide_list))
return 1; ret = HIDE_ERROR;
} else {
ret = HIDE_ITEM_NOT_EXIST;
} }
error:
free(proc); free(proc);
vec_destroy(temp); vec_destroy(temp);
free(temp); free(temp);
return 0; return ret;
} }
int init_list() { int init_list() {

View File

@ -44,7 +44,7 @@ static void usage(char *arg0) {
void launch_magiskhide(int client) { void launch_magiskhide(int client) {
if (hideEnabled) { if (hideEnabled) {
write_int(client, 0); write_int(client, HIDE_IS_ENABLED);
close(client); close(client);
return; return;
} }
@ -79,7 +79,7 @@ void launch_magiskhide(int client) {
// Initialize the mutex lock // Initialize the mutex lock
pthread_mutex_init(&hide_lock, NULL); pthread_mutex_init(&hide_lock, NULL);
write_int(client, 0); write_int(client, HIDE_SUCCESS);
close(client); close(client);
// Get thread reference // Get thread reference
@ -90,7 +90,7 @@ void launch_magiskhide(int client) {
error: error:
hideEnabled = 0; hideEnabled = 0;
write_int(client, 1); write_int(client, HIDE_ERROR);
close(client); close(client);
if (hide_pid != -1) { if (hide_pid != -1) {
int kill = -1; int kill = -1;
@ -105,7 +105,7 @@ error:
void stop_magiskhide(int client) { void stop_magiskhide(int client) {
if (!hideEnabled) { if (!hideEnabled) {
write_int(client, 0); write_int(client, HIDE_NOT_ENABLED);
close(client); close(client);
return; return;
} }
@ -116,7 +116,7 @@ void stop_magiskhide(int client) {
setprop("persist.magisk.hide", "0"); setprop("persist.magisk.hide", "0");
pthread_kill(proc_monitor_thread, SIGUSR1); pthread_kill(proc_monitor_thread, SIGUSR1);
write_int(client, 0); write_int(client, HIDE_SUCCESS);
close(client); close(client);
} }
@ -147,10 +147,26 @@ int magiskhide_main(int argc, char *argv[]) {
if (req == ADD_HIDELIST || req == RM_HIDELIST) { if (req == ADD_HIDELIST || req == RM_HIDELIST) {
write_string(fd, argv[2]); write_string(fd, argv[2]);
} }
int code = read_int(fd); hide_ret code = read_int(fd);
close(fd); close(fd);
if (code) { switch (code) {
fprintf(stderr, "Error occured in MagiskHide daemon\n"); 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; return code;
} }

View File

@ -8,6 +8,15 @@
#define ENFORCE_FILE "/sys/fs/selinux/enforce" #define ENFORCE_FILE "/sys/fs/selinux/enforce"
#define POLICY_FILE "/sys/fs/selinux/policy" #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 // Kill process
void kill_proc(int pid); void kill_proc(int pid);

View File

@ -6,6 +6,7 @@
#include "vector.h" #include "vector.h"
void vec_init(struct vector *v) { void vec_init(struct vector *v) {
if (v == NULL) return;
vec_size(v) = 0; vec_size(v) = 0;
vec_cap(v) = 1; vec_cap(v) = 1;
vec_entry(v) = malloc(sizeof(void*)); 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 *)) { 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); 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 * use in cases when each element requires special cleanup
*/ */
void vec_destroy(struct vector *v) { void vec_destroy(struct vector *v) {
if (v == NULL) return;
vec_size(v) = 0; vec_size(v) = 0;
vec_cap(v) = 0; vec_cap(v) = 0;
free(vec_entry(v)); free(vec_entry(v));
@ -39,6 +42,7 @@ void vec_destroy(struct vector *v) {
* Shall be the general case * Shall be the general case
*/ */
void vec_deep_destroy(struct vector *v) { void vec_deep_destroy(struct vector *v) {
if (v == NULL) return;
void *e; void *e;
vec_for_each(v, e) { vec_for_each(v, e) {
free(e); free(e);