From c282a8f32805730acb672c87781df342f42286f3 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 3 Jun 2017 04:31:01 +0800 Subject: [PATCH] Loop for every for logging --- jni/daemon/log_monitor.c | 26 +++++--- jni/magisk.h | 1 + jni/magiskhide/hide_daemon.c | 2 +- jni/magiskhide/magiskhide.h | 3 - jni/magiskhide/proc_monitor.c | 121 ++++++++++++++++++---------------- jni/utils/misc.c | 12 ++-- 6 files changed, 90 insertions(+), 75 deletions(-) diff --git a/jni/daemon/log_monitor.c b/jni/daemon/log_monitor.c index 475f6208a..4113af052 100644 --- a/jni/daemon/log_monitor.c +++ b/jni/daemon/log_monitor.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "magisk.h" #include "utils.h" @@ -23,17 +24,26 @@ static void *logger_thread(void *args) { FILE *logfile = xfdopen(xopen(LOGFILE, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644), "w"); // Disable buffering setbuf(logfile, NULL); - // Start logcat - char *const command[] = { "logcat", "-s", "Magisk", "-v", "time", NULL }; - int fd; - run_command(&fd, "/system/bin/logcat", command); - while (fdgets(buffer, PATH_MAX, fd)) { - fprintf(logfile, "%s", buffer); + int fd, log_pid; + + while (1) { + // Start logcat + char *const command[] = { "logcat", "-s", "Magisk", "-v", "time", NULL }; + log_pid = run_command(&fd, "/system/bin/logcat", command); + while (fdgets(buffer, PATH_MAX, fd)) { + fprintf(logfile, "%s", buffer); + } + + // For some reason it went here, clear buffer and restart + close(fd); + kill(log_pid, SIGTERM); + waitpid(log_pid, NULL, 0); + system("logcat -c"); } + // Should never be here, but well... - free(buffer); - close(fd); fclose(logfile); + free(buffer); return NULL; } diff --git a/jni/magisk.h b/jni/magisk.h index d808c02f5..bf84c9914 100644 --- a/jni/magisk.h +++ b/jni/magisk.h @@ -32,6 +32,7 @@ #define MOUNTPOINT "/magisk" #define COREDIR MOUNTPOINT "/.core" #define HOSTSFILE COREDIR "/hosts" +#define HIDELIST COREDIR "/hidelist" #define MAINIMG "/data/magisk.img" #define DATABIN "/data/magisk" #define MANAGERAPK DATABIN "/magisk.apk" diff --git a/jni/magiskhide/hide_daemon.c b/jni/magiskhide/hide_daemon.c index 82f1cccb0..a1f593d70 100644 --- a/jni/magiskhide/hide_daemon.c +++ b/jni/magiskhide/hide_daemon.c @@ -113,7 +113,7 @@ int hide_daemon() { // Unmount loop mounts vec_for_each_r(&mount_list, line) { - if (strstr(line, "/dev/block/loop") && !strstr(line, DUMMYPATH)) { + if (strstr(line, "/dev/block/loop") && !strstr(line, DUMMDIR)) { sscanf(line, "%*s %512s", buffer); lazy_unmount(buffer); } diff --git a/jni/magiskhide/magiskhide.h b/jni/magiskhide/magiskhide.h index f36d4a23f..898f2d869 100644 --- a/jni/magiskhide/magiskhide.h +++ b/jni/magiskhide/magiskhide.h @@ -3,9 +3,6 @@ #include -#define HIDELIST "/magisk/.core/magiskhide/hidelist" -#define DUMMYPATH "/dev/magisk/dummy" - // Kill process void kill_proc(int pid); diff --git a/jni/magiskhide/proc_monitor.c b/jni/magiskhide/proc_monitor.c index a059f92c3..aa9c75810 100644 --- a/jni/magiskhide/proc_monitor.c +++ b/jni/magiskhide/proc_monitor.c @@ -101,75 +101,82 @@ void proc_monitor() { break; } - // Monitor am_proc_start - system("logcat -b events -c"); - char *const command[] = { "logcat", "-b", "events", "-v", "raw", "-s", "am_proc_start", NULL }; - log_pid = run_command(&log_fd, "/system/bin/logcat", command); + while (1) { + // Clear previous buffer + system("logcat -b events -c"); - while(fdgets(buffer, PATH_MAX, log_fd)) { - int ret, comma = 0; - char *pos = buffer, *line, processName[256]; + // Monitor am_proc_start + char *const command[] = { "logcat", "-b", "events", "-v", "raw", "-s", "am_proc_start", NULL }; + log_pid = run_command(&log_fd, "/system/bin/logcat", command); - while(1) { - pos = strchr(pos, ','); - if(pos == NULL) - break; - pos[0] = ' '; - ++comma; - } + while(fdgets(buffer, PATH_MAX, log_fd)) { + int ret, comma = 0; + char *pos = buffer, *line, processName[256]; - if (comma == 6) - ret = sscanf(buffer, "[%*d %d %*d %*d %256s", &pid, processName); - else - ret = sscanf(buffer, "[%*d %d %*d %256s", &pid, processName); + while(1) { + pos = strchr(pos, ','); + if(pos == NULL) + break; + pos[0] = ' '; + ++comma; + } - if(ret != 2) - continue; + if (comma == 6) + ret = sscanf(buffer, "[%*d %d %*d %*d %256s", &pid, processName); + else + ret = sscanf(buffer, "[%*d %d %*d %256s", &pid, processName); - ret = 0; + if(ret != 2) + continue; - // Critical region - pthread_mutex_lock(&hide_lock); - vec_for_each(hide_list, line) { - if (strcmp(processName, line) == 0) { - while(1) { - ret = 1; - for (int i = 0; i < zygote_num; ++i) { - read_namespace(pid, buffer, 32); - if (strcmp(buffer, zygote_ns[i]) == 0) { - usleep(50); - ret = 0; - break; + ret = 0; + + // Critical region + pthread_mutex_lock(&hide_lock); + vec_for_each(hide_list, line) { + if (strcmp(processName, line) == 0) { + while(1) { + ret = 1; + for (int i = 0; i < zygote_num; ++i) { + read_namespace(pid, buffer, 32); + if (strcmp(buffer, zygote_ns[i]) == 0) { + usleep(50); + ret = 0; + break; + } } + if (ret) break; } - if (ret) break; + + ret = 0; + + // Send pause signal ASAP + if (kill(pid, SIGSTOP) == -1) continue; + + LOGI("proc_monitor: %s (PID=%d ns=%s)\n", processName, pid, buffer); + + // Unmount start + xwrite(sv[0], &pid, sizeof(pid)); + + // Get the hide daemon return code + xxread(sv[0], &ret, sizeof(ret)); + LOGD("proc_monitor: hide daemon response code: %d\n", ret); + break; } + } + pthread_mutex_unlock(&hide_lock); - ret = 0; - - // Send pause signal ASAP - if (kill(pid, SIGSTOP) == -1) continue; - - LOGI("proc_monitor: %s (PID=%d ns=%s)\n", processName, pid, buffer); - - // Unmount start - xwrite(sv[0], &pid, sizeof(pid)); - - // Get the hide daemon return code - xxread(sv[0], &ret, sizeof(ret)); - LOGD("proc_monitor: hide daemon response code: %d\n", ret); - break; + if (ret) { + // Wait hide process to kill itself + waitpid(hide_pid, NULL, 0); + quit_pthread(SIGUSR1); } } - pthread_mutex_unlock(&hide_lock); - if (ret) { - // Wait hide process to kill itself - waitpid(hide_pid, NULL, 0); - quit_pthread(SIGUSR1); - } + // For some reason it went here, restart logging + kill(log_pid, SIGTERM); + waitpid(log_pid, NULL, 0); + close(log_fd); + log_pid = 0; } - - // Should never be here - quit_pthread(SIGUSR1); } diff --git a/jni/utils/misc.c b/jni/utils/misc.c index 749f8f2b1..3b9924aeb 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -67,7 +67,7 @@ int file_to_vector(const char* filename, struct vector *v) { size_t len = 0; ssize_t read; - FILE *fp = xfopen(filename, "r"); + FILE *fp = fopen(filename, "r"); if (fp == NULL) return 1; @@ -105,16 +105,16 @@ int isNum(const char *s) { /* Read a whole line from file descriptor */ ssize_t fdgets(char *buf, const size_t size, int fd) { - ssize_t read = 0; + ssize_t len = 0; buf[0] = '\0'; - while (xread(fd, buf + read, 1) && read < size - 1) { - if (buf[read] == '\0' || buf[read++] == '\n') { - buf[read] = '\0'; + while (read(fd, buf + len, 1) >= 0 && len < size - 1) { + if (buf[len] == '\0' || buf[len++] == '\n') { + buf[len] = '\0'; break; } } buf[size - 1] = '\0'; - return read; + return len; } /* Call func for each process */