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

View File

@ -23,7 +23,7 @@ struct vector *hide_list = NULL;
int hideEnabled = 0; int hideEnabled = 0;
static pthread_t proc_monitor_thread; static pthread_t proc_monitor_thread;
pthread_mutex_t hide_lock; pthread_mutex_t hide_lock, file_lock;
void kill_proc(int pid) { void kill_proc(int pid) {
kill(pid, SIGTERM); kill(pid, SIGTERM);
@ -89,6 +89,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);
pthread_mutex_init(&file_lock, NULL);
write_int(client, HIDE_SUCCESS); write_int(client, HIDE_SUCCESS);
close(client); close(client);

View File

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

View File

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

View File

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