Improve magiskhide stability

This commit is contained in:
topjohnwu 2017-05-04 02:58:37 +08:00
parent 05ed29133b
commit 396afaa181
5 changed files with 28 additions and 18 deletions

View File

@ -17,8 +17,7 @@ int add_list(char *proc) {
}
char *line;
struct vector *new_list, *temp = hide_list;
new_list = xmalloc(sizeof(*new_list));
struct vector *new_list = xmalloc(sizeof(*new_list));
if (new_list == NULL)
return HIDE_ERROR;
vec_init(new_list);
@ -40,14 +39,17 @@ int add_list(char *proc) {
// Critical region
pthread_mutex_lock(&hide_lock);
vec_destroy(hide_list);
free(hide_list);
hide_list = new_list;
pthread_mutex_unlock(&hide_lock);
// Free old list
vec_destroy(temp);
free(temp);
if (vector_to_file(HIDELIST, hide_list))
pthread_mutex_lock(&file_lock);
if (vector_to_file(HIDELIST, hide_list)) {
pthread_mutex_unlock(&file_lock);
return HIDE_ERROR;
}
pthread_mutex_unlock(&file_lock);
return HIDE_SUCCESS;
}
@ -59,8 +61,8 @@ int rm_list(char *proc) {
hide_ret ret = HIDE_ERROR;
char *line;
struct vector *new_list, *temp;
temp = new_list = xmalloc(sizeof(*new_list));
int do_rm = 0;
struct vector *new_list = xmalloc(sizeof(*new_list));
if (new_list == NULL)
goto error;
vec_init(new_list);
@ -69,30 +71,35 @@ int rm_list(char *proc) {
if (strcmp(line, proc) == 0) {
free(proc);
proc = line;
temp = hide_list;
do_rm = 1;
continue;
}
vec_push_back(new_list, line);
}
if (temp == hide_list) {
if (do_rm) {
LOGI("hide_list rm: [%s]\n", proc);
ps_filter_proc_name(proc, kill_proc);
// Critical region
pthread_mutex_lock(&hide_lock);
vec_destroy(hide_list);
free(hide_list);
hide_list = new_list;
pthread_mutex_unlock(&hide_lock);
ret = HIDE_SUCCESS;
pthread_mutex_lock(&file_lock);
if (vector_to_file(HIDELIST, hide_list))
ret = HIDE_ERROR;
pthread_mutex_unlock(&file_lock);
} else {
ret = HIDE_ITEM_NOT_EXIST;
vec_destroy(new_list);
free(new_list);
}
error:
free(proc);
vec_destroy(temp);
free(temp);
return ret;
}
@ -125,6 +132,7 @@ int destroy_list() {
}
void add_hide_list(int client) {
err_handler = do_nothing;
char *proc = read_string(client);
// ack
write_int(client, add_list(proc));
@ -132,6 +140,7 @@ void add_hide_list(int client) {
}
void rm_hide_list(int client) {
err_handler = do_nothing;
char *proc = read_string(client);
// ack
write_int(client, rm_list(proc));

View File

@ -23,7 +23,7 @@ struct vector *hide_list = NULL;
int hideEnabled = 0;
static pthread_t proc_monitor_thread;
pthread_mutex_t hide_lock;
pthread_mutex_t hide_lock, file_lock;
void kill_proc(int pid) {
kill(pid, SIGTERM);
@ -89,6 +89,7 @@ void launch_magiskhide(int client) {
// Initialize the mutex lock
pthread_mutex_init(&hide_lock, NULL);
pthread_mutex_init(&file_lock, NULL);
write_int(client, HIDE_SUCCESS);
close(client);

View File

@ -37,6 +37,6 @@ int destroy_list();
extern int sv[2], hide_pid, hideEnabled;
extern struct vector *hide_list;
extern pthread_mutex_t hide_lock;
extern pthread_mutex_t hide_lock, file_lock;
#endif

View File

@ -70,7 +70,7 @@ void proc_monitor() {
err_handler = proc_monitor_err;
int pid;
char buffer[512];
char buffer[4096];
// Get the mount namespace of init
read_namespace(1, init_ns, 32);
@ -82,13 +82,14 @@ void proc_monitor() {
sleep(2);
ps_filter_proc_name("zygote", store_zygote_ns);
}
ps_filter_proc_name("zygote64", store_zygote_ns);
switch(zygote_num) {
case 1:
LOGI("proc_monitor: zygote ns=%s\n", zygote_ns[0]);
break;
case 2:
LOGI("proc_monitor: zygote (1) ns=%s (2) ns=%s\n", zygote_ns[0], zygote_ns[1]);
LOGI("proc_monitor: zygote (32-bit) ns=%s (64-bit) ns=%s\n", zygote_ns[0], zygote_ns[1]);
break;
}

View File

@ -151,8 +151,7 @@ static void proc_name_filter(int pid) {
return;
fdgets(buf, sizeof(buf), fd);
}
if (strstr(buf, ps_filter_pattern)) {
// printf("%d: %s\n", pid, buf);
if (strcmp(buf, ps_filter_pattern) == 0) {
ps_filter_cb(pid);
}
close(fd);