From 754fafcfe9de13364369b6e4ea8975e78a2b62cb Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 12 Feb 2018 02:48:15 +0800 Subject: [PATCH] Check logd before logging --- native/jni/core/daemon.c | 6 +-- native/jni/core/log_monitor.c | 71 ++++++++++++++++++++++-------- native/jni/include/daemon.h | 12 ++--- native/jni/include/logging.h | 1 + native/jni/magiskhide/magiskhide.c | 11 +++++ 5 files changed, 73 insertions(+), 28 deletions(-) diff --git a/native/jni/core/daemon.c b/native/jni/core/daemon.c index cdce32f13..7658016db 100644 --- a/native/jni/core/daemon.c +++ b/native/jni/core/daemon.c @@ -265,6 +265,9 @@ 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,9 +277,6 @@ 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/native/jni/core/log_monitor.c b/native/jni/core/log_monitor.c index fb33b6c8f..19f1efbac 100644 --- a/native/jni/core/log_monitor.c +++ b/native/jni/core/log_monitor.c @@ -12,8 +12,10 @@ #include "magisk.h" #include "utils.h" +#include "resetprop.h" extern int is_daemon_init; +int logd = 0; static int am_proc_start_filter(const char *log) { return strstr(log, "am_proc_start") != NULL; @@ -44,9 +46,20 @@ struct log_listener log_events[] = { }; #ifdef MAGISK_DEBUG -static int debug_log_pid, debug_log_fd; +static int debug_log_pid = -1, debug_log_fd = -1; #endif +static void check_logd() { + char *prop = getprop("init.svc.logd"); + if (prop != NULL) { + free(prop); + logd = 1; + } else { + LOGD("log_monitor: logd not started, disable logging"); + logd = 0; + } +} + static void *logger_thread(void *args) { int log_fd = -1, log_pid; char line[4096]; @@ -54,6 +67,15 @@ static void *logger_thread(void *args) { LOGD("log_monitor: logger start"); while (1) { + if (!logd) { + // Disable all services + for (int i = 0; i < (sizeof(log_events) / sizeof(struct log_listener)); ++i) { + close(log_events[i].fd); + log_events[i].fd = -1; + } + return NULL; + } + // Start logcat log_pid = exec_command(0, &log_fd, NULL, "logcat", "-b", "events", "-b", "main", "-v", "threadtime", "-s", "am_proc_start", "-s", "Magisk", NULL); while (fdgets(line, sizeof(line), log_fd)) { @@ -75,6 +97,8 @@ static void *logger_thread(void *args) { // Clear buffer before restart exec_command_sync("logcat", "-b", "events", "-b", "main", "-c", NULL); + + check_logd(); } // Should never be here, but well... @@ -139,40 +163,49 @@ static void *debug_magisk_log_thread(void *args) { void monitor_logs() { pthread_t thread; - // Start log file dumper before monitor - xpthread_create(&thread, NULL, magisk_log_thread, NULL); - pthread_detach(thread); + check_logd(); - // Start logcat monitor - xpthread_create(&thread, NULL, logger_thread, NULL); - pthread_detach(thread); + if (logd) { + // Start log file dumper before monitor + xpthread_create(&thread, NULL, magisk_log_thread, NULL); + pthread_detach(thread); + // Start logcat monitor + xpthread_create(&thread, NULL, logger_thread, NULL); + pthread_detach(thread); + } } void start_debug_full_log() { #ifdef MAGISK_DEBUG - // Log everything initially - debug_log_fd = xopen(DEBUG_LOG, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644); - debug_log_pid = exec_command(0, &debug_log_fd, NULL, "logcat", "-v", "threadtime", NULL); - close(debug_log_fd); + if (logd) { + // Log everything initially + debug_log_fd = xopen(DEBUG_LOG, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0644); + debug_log_pid = exec_command(0, &debug_log_fd, NULL, "logcat", "-v", "threadtime", NULL); + close(debug_log_fd); + } #endif } void stop_debug_full_log() { #ifdef MAGISK_DEBUG // Stop recording the boot logcat after every boot task is done - kill(debug_log_pid, SIGTERM); - waitpid(debug_log_pid, NULL, 0); - // Start debug thread - start_debug_log(); + if (debug_log_pid > 0) { + kill(debug_log_pid, SIGTERM); + waitpid(debug_log_pid, NULL, 0); + // Start debug thread + start_debug_log(); + } #endif } void start_debug_log() { #ifdef MAGISK_DEBUG - pthread_t thread; - // Start debug thread - xpthread_create(&thread, NULL, debug_magisk_log_thread, NULL); - pthread_detach(thread); + if (logd) { + pthread_t thread; + // Start debug thread + xpthread_create(&thread, NULL, debug_magisk_log_thread, NULL); + pthread_detach(thread); + } #endif } diff --git a/native/jni/include/daemon.h b/native/jni/include/daemon.h index 5dfdc36d6..3a30c26df 100644 --- a/native/jni/include/daemon.h +++ b/native/jni/include/daemon.h @@ -13,18 +13,17 @@ extern int is_daemon_init, seperate_vendor; // Commands require connecting to daemon enum { DO_NOTHING = 0, - LAUNCH_MAGISKHIDE, - STOP_MAGISKHIDE, - ADD_HIDELIST, - RM_HIDELIST, - LS_HIDELIST, SUPERUSER, CHECK_VERSION, CHECK_VERSION_CODE, POST_FS, POST_FS_DATA, LATE_START, - TEST + LAUNCH_MAGISKHIDE, + STOP_MAGISKHIDE, + ADD_HIDELIST, + RM_HIDELIST, + LS_HIDELIST }; // Return codes for daemon @@ -32,6 +31,7 @@ enum { DAEMON_ERROR = -1, DAEMON_SUCCESS = 0, ROOT_REQUIRED, + LOGD_DISABLED, HIDE_IS_ENABLED, HIDE_NOT_ENABLED, HIDE_ITEM_EXIST, diff --git a/native/jni/include/logging.h b/native/jni/include/logging.h index 21908a785..93d7c4c30 100644 --- a/native/jni/include/logging.h +++ b/native/jni/include/logging.h @@ -58,6 +58,7 @@ struct log_listener { }; extern struct log_listener log_events[]; +extern int logd; void monitor_logs(); void start_debug_full_log(); diff --git a/native/jni/magiskhide/magiskhide.c b/native/jni/magiskhide/magiskhide.c index 339343aa9..0010a3f53 100644 --- a/native/jni/magiskhide/magiskhide.c +++ b/native/jni/magiskhide/magiskhide.c @@ -49,6 +49,14 @@ void launch_magiskhide(int client) { return; } + if (!logd) { + if (client > 0) { + write_int(client, LOGD_DISABLED); + close(client); + } + return; + } + hideEnabled = 1; LOGI("* Starting MagiskHide\n"); @@ -136,6 +144,9 @@ int magiskhide_main(int argc, char *argv[]) { case ROOT_REQUIRED: fprintf(stderr, "Root is required for this operation\n"); return code; + case LOGD_DISABLED: + fprintf(stderr, "Logd is not running, cannot run logcat\n"); + return (code); case HIDE_NOT_ENABLED: fprintf(stderr, "Magisk hide is not enabled yet\n"); return code;