Cleanup file descriptors and add more info

This commit is contained in:
topjohnwu
2017-07-08 23:51:58 +08:00
parent b9968aa1e6
commit b570b363d9
12 changed files with 41 additions and 53 deletions

View File

@@ -53,6 +53,7 @@ int hide_daemon() {
// Set the process name
strcpy(argv0, "magiskhide_daemon");
LOGD("hide_daemon: listening for hide requests");
// When an error occurs, report its failure to main process
err_handler = hide_daemon_err;

View File

@@ -16,7 +16,6 @@
#include "daemon.h"
#include "resetprop.h"
int sv[2], hide_pid = -1;
struct vector *hide_list = NULL;
int hideEnabled = 0;
@@ -29,7 +28,7 @@ void kill_proc(int pid) {
static void usage(char *arg0) {
fprintf(stderr,
"MagiskHide v" xstr(MAGISK_VERSION) " (by topjohnwu) - Hide Magisk!\n\n"
"MagiskHide v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Hide Magisk!\n\n"
"%s [--options [arguments...] ]\n\n"
"Options:\n"
" --enable: Start the magiskhide daemon\n"
@@ -63,19 +62,6 @@ void launch_magiskhide(int client) {
hide_sensitive_props();
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) == -1)
goto error;
/*
* The setns system call do not support multithread processes
* We have to fork a new process, and communicate with sockets
*/
if (hide_daemon())
goto error;
close(sv[1]);
// Initialize the mutex lock
pthread_mutex_init(&hide_lock, NULL);
pthread_mutex_init(&file_lock, NULL);
@@ -87,8 +73,10 @@ void launch_magiskhide(int client) {
// Add SafetyNet by default
add_list(strdup("com.google.android.gms.unstable"));
write_int(client, DAEMON_SUCCESS);
close(client);
if (client > 0) {
write_int(client, DAEMON_SUCCESS);
close(client);
}
// Get thread reference
proc_monitor_thread = pthread_self();
@@ -98,15 +86,9 @@ void launch_magiskhide(int client) {
error:
hideEnabled = 0;
write_int(client, DAEMON_ERROR);
close(client);
if (hide_pid != -1) {
int kill = -1;
// Kill hide daemon
write(sv[0], &kill, sizeof(kill));
close(sv[0]);
waitpid(hide_pid, NULL, 0);
hide_pid = -1;
if (client > 0) {
write_int(client, DAEMON_ERROR);
close(client);
}
return;
}

View File

@@ -22,6 +22,8 @@ static char init_ns[32], zygote_ns[2][32];
static int log_pid, log_fd;
static char *buffer;
int sv[2], hide_pid = -1;
// Workaround for the lack of pthread_cancel
static void quit_pthread(int sig) {
err_handler = do_nothing;
@@ -103,10 +105,22 @@ void proc_monitor() {
LOGI("proc_monitor: zygote ns=%s\n", zygote_ns[0]);
break;
case 2:
LOGI("proc_monitor: zygote (32-bit) ns=%s (64-bit) ns=%s\n", zygote_ns[0], zygote_ns[1]);
LOGI("proc_monitor: zygote ns=%s zygote64 ns=%s\n", zygote_ns[0], zygote_ns[1]);
break;
}
if (socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) == -1)
quit_pthread(SIGUSR1);
/*
* The setns system call do not support multithread processes
* We have to fork a new process, and communicate with sockets
*/
if (hide_daemon())
quit_pthread(SIGUSR1);
close(sv[1]);
while (1) {
// Clear previous buffer
system("logcat -b events -c");