From bade4f2c6a8dcceab4d13f23015c03f0b4af6ca7 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 21 Aug 2021 00:58:51 -0700 Subject: [PATCH] Make xhook log as Magisk --- native/jni/core/core.hpp | 1 - native/jni/core/logging.cpp | 24 ++++++++++++++---------- native/jni/external/Android.mk | 2 +- native/jni/include/daemon.hpp | 2 +- native/jni/zygisk/entry.cpp | 6 +++--- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/native/jni/core/core.hpp b/native/jni/core/core.hpp index 0aea2da01..4c1fe0dd1 100644 --- a/native/jni/core/core.hpp +++ b/native/jni/core/core.hpp @@ -8,7 +8,6 @@ extern int DAEMON_STATE; void unlock_blocks(); void reboot(); -void magisk_logging(); void start_log_daemon(); void setup_logfile(bool reset); diff --git a/native/jni/core/logging.cpp b/native/jni/core/logging.cpp index 436840a4e..e66b8e4d0 100644 --- a/native/jni/core/logging.cpp +++ b/native/jni/core/logging.cpp @@ -149,6 +149,20 @@ static int magisk_log(int prio, const char *fmt, va_list ap) { return len; } +// Used to override external C library logging +extern "C" int magisk_log_print(int prio, const char *tag, const char *fmt, ...) { + char buf[4096]; + auto len = strlcpy(buf, tag, sizeof(buf)); + // Prevent format specifications in the tag + std::replace(buf, buf + len, '%', '_'); + snprintf(buf + len, sizeof(buf) - len, ": %s", fmt); + va_list argv; + va_start(argv, fmt); + int ret = magisk_log(prio, buf, argv); + va_end(argv); + return ret; +} + #define mlog(prio) [](auto fmt, auto ap){ return magisk_log(ANDROID_LOG_##prio, fmt, ap); } void magisk_logging() { log_cb.d = mlog(DEBUG); @@ -158,16 +172,6 @@ void magisk_logging() { log_cb.ex = nop_ex; } -#define alog(prio) [](auto fmt, auto ap){ \ -return __android_log_vprint(ANDROID_LOG_##prio, "Magisk", fmt, ap); } -void android_logging() { - log_cb.d = alog(DEBUG); - log_cb.i = alog(INFO); - log_cb.w = alog(WARN); - log_cb.e = alog(ERROR); - log_cb.ex = nop_ex; -} - void start_log_daemon() { int fds[2]; if (pipe2(fds, O_CLOEXEC) == 0) { diff --git a/native/jni/external/Android.mk b/native/jni/external/Android.mk index 34985d154..98c00c967 100644 --- a/native/jni/external/Android.mk +++ b/native/jni/external/Android.mk @@ -365,7 +365,7 @@ include $(CLEAR_VARS) LOCAL_MODULE:= libxhook LOCAL_C_INCLUDES := $(LOCAL_PATH)/xhook/libxhook/jni LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) -LOCAL_CFLAGS := -Wall -Wextra -Werror -fvisibility=hidden +LOCAL_CFLAGS := -Wall -Wextra -Werror -fvisibility=hidden -D__android_log_print=magisk_log_print LOCAL_CONLYFLAGS := -std=c11 LOCAL_SRC_FILES := \ xhook/libxhook/jni/xh_log.c \ diff --git a/native/jni/include/daemon.hpp b/native/jni/include/daemon.hpp index 50b4b9446..b10121b1e 100644 --- a/native/jni/include/daemon.hpp +++ b/native/jni/include/daemon.hpp @@ -48,7 +48,7 @@ enum : int { int connect_daemon(bool create = false); -void android_logging(); +void magisk_logging(); // Daemon handlers void post_fs_data(int client); diff --git a/native/jni/zygisk/entry.cpp b/native/jni/zygisk/entry.cpp index d317e5951..c611dbb00 100644 --- a/native/jni/zygisk/entry.cpp +++ b/native/jni/zygisk/entry.cpp @@ -81,7 +81,7 @@ static void inject_cleanup_wait() { __attribute__((constructor)) static void inject_init() { if (char *env = getenv(INJECT_ENV_2)) { - android_logging(); + magisk_logging(); LOGD("zygisk: inject 2nd stage\n"); active_threads = 1; unsetenv(INJECT_ENV_2); @@ -100,7 +100,7 @@ static void inject_init() { active_threads++; new_daemon_thread(&unload_first_stage, env); } else if (getenv(INJECT_ENV_1)) { - android_logging(); + magisk_logging(); LOGD("zygisk: inject 1st stage\n"); char *ld = getenv("LD_PRELOAD"); @@ -128,7 +128,7 @@ static void inject_init() { // Start code for magiskd IPC int app_process_main(int argc, char *argv[]) { - android_logging(); + magisk_logging(); if (int fd = connect_daemon(); fd >= 0) { write_int(fd, ZYGISK_REQUEST);