From d0650403211452abf62ba2f6c1b1b633e7e6fe75 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 18 Dec 2017 02:51:27 +0800 Subject: [PATCH] Fix magiskinit invincible mode and logcat monitor --- core/jni/core/daemon.c | 6 +++--- core/jni/core/log_monitor.c | 16 +++++++++++----- core/jni/core/magiskinit.c | 5 +++-- core/jni/include/magisk.h | 1 - 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/core/jni/core/daemon.c b/core/jni/core/daemon.c index 6720fab32..4101ac09c 100644 --- a/core/jni/core/daemon.c +++ b/core/jni/core/daemon.c @@ -262,9 +262,6 @@ void start_daemon() { exit(1); xlisten(fd, 10); - // Start the log monitor - monitor_logs(); - if ((is_daemon_init = (access(MAGISKTMP, F_OK) == 0))) { // Restart stuffs if the daemon is restarted exec_command_sync("logcat", "-b", "all", "-c", NULL); @@ -274,6 +271,9 @@ void start_daemon() { daemon_init(); } + // Start the log monitor + monitor_logs(); + LOGI("Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") daemon started\n"); // Change process name diff --git a/core/jni/core/log_monitor.c b/core/jni/core/log_monitor.c index 2b44f3e02..6ac5a1077 100644 --- a/core/jni/core/log_monitor.c +++ b/core/jni/core/log_monitor.c @@ -39,7 +39,14 @@ static void *logger_thread(void *args) { if (kill(log_pid, 0)) break; } - // Clear buffer if restart required + + // Cleanup + close(log_fd); + log_fd = -1; + kill(log_pid, SIGTERM); + waitpid(log_pid, NULL, 0); + + // Clear buffer before restart exec_command_sync("logcat", "-b", "all", "-c", NULL); } @@ -54,7 +61,6 @@ static void *magisk_log_thread(void *args) { struct vector logs; vec_init(&logs); - FILE *log; int pipefd[2]; if (xpipe2(pipefd, O_CLOEXEC) == -1) return NULL; @@ -64,14 +70,14 @@ static void *magisk_log_thread(void *args) { LOGD("log_monitor: magisk log dumper start"); + FILE *log; + char *ss; for (char *line; xxread(pipefd[0], &line, sizeof(line)) > 0; free(line)) { - char *ss; if ((ss = strstr(line, " Magisk")) && (ss[-1] != 'D') && (ss[-1] != 'V')) { if (!have_data) { if ((have_data = check_data())) { // Dump buffered logs to file - rename(LOGFILE, LASTLOG); - log = xfopen(LOGFILE, "a"); + log = xfopen(LOGFILE, "w"); setbuf(log, NULL); char *tmp; vec_for_each(&logs, tmp) { diff --git a/core/jni/core/magiskinit.c b/core/jni/core/magiskinit.c index 53b8db0fe..1b9389c12 100644 --- a/core/jni/core/magiskinit.c +++ b/core/jni/core/magiskinit.c @@ -380,9 +380,9 @@ static void magisk_init_daemon() { destroy_policydb(); // Keep Magisk daemon always alive - struct sockaddr_un sun; - fd = setup_socket(&sun); while (1) { + struct sockaddr_un sun; + fd = setup_socket(&sun); while (connect(fd, (struct sockaddr*) &sun, sizeof(sun))) usleep(10000); /* Wait 10 ms after each try */ @@ -391,6 +391,7 @@ static void magisk_init_daemon() { /* If things went here, it means the other side of the socket is closed * We restart the daemon again */ + close(fd); if (fork_dont_care() == 0) { execv("/sbin/magisk", (char *[]) { "resetprop", "magisk.daemon", "1", NULL } ); exit(1); diff --git a/core/jni/include/magisk.h b/core/jni/include/magisk.h index 8ab218e25..9baa64792 100644 --- a/core/jni/include/magisk.h +++ b/core/jni/include/magisk.h @@ -14,7 +14,6 @@ #endif #define LOGFILE "/cache/magisk.log" -#define LASTLOG "/cache/last_magisk.log" #define DEBUG_LOG "/data/adb/magisk_debug.log" #define UNBLOCKFILE "/dev/.magisk.unblock" #define PATCHDONE "/dev/.magisk.patch.done"