From 8ec3086cdd0f9698c47aae6814e6eceb33d5dc7b Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 28 Oct 2018 04:24:53 -0400 Subject: [PATCH] Make sure magisklogd is properly initialized --- native/jni/daemon/bootstages.c | 4 +--- native/jni/daemon/log_daemon.c | 30 ++++++++++++++++-------------- native/jni/daemon/socket.c | 6 +++--- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/native/jni/daemon/bootstages.c b/native/jni/daemon/bootstages.c index 0ac684d07..5141540d9 100644 --- a/native/jni/daemon/bootstages.c +++ b/native/jni/daemon/bootstages.c @@ -587,6 +587,7 @@ static const char wrapper[] = "exec /sbin/magisk.bin \"${0##*/}\" \"$@\"\n"; void startup() { + android_logging(); if (!check_data()) unblock_boot_process(); @@ -782,9 +783,6 @@ void startup() { xsymlink(MIRRDIR "/bin/busybox", BBPATH "/busybox"); } - // Preserve a copy of logcat - cp_afc("/system/bin/logcat", MIRRDIR "/bin/logcat"); - // Start post-fs-data mode execl("/sbin/magisk.bin", "magisk", "--post-fs-data", NULL); } diff --git a/native/jni/daemon/log_daemon.c b/native/jni/daemon/log_daemon.c index 23cacacc1..413aa4b16 100644 --- a/native/jni/daemon/log_daemon.c +++ b/native/jni/daemon/log_daemon.c @@ -19,8 +19,7 @@ #include "flags.h" int log_daemon_started = 0; -static struct vector log_cmd, clear_cmd; -static int sockfd; +static struct vector log_cmd, clear_cmd;; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; enum { @@ -111,12 +110,6 @@ static void *logcat_thread(void *args) { static void log_daemon() { setsid(); - struct sockaddr_un sun; - socklen_t len = setup_sockaddr(&sun, LOG_SOCKET); - sockfd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); - if (xbind(sockfd, (struct sockaddr*) &sun, len)) - exit(1); - xlisten(sockfd, 10); LOGI("Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") logger started\n"); strcpy(argv0, "magisklogd"); @@ -132,11 +125,11 @@ static void log_daemon() { // Construct cmdline vec_init(&log_cmd); - vec_push_back(&log_cmd, MIRRDIR "/bin/logcat"); + vec_push_back(&log_cmd, MIRRDIR "/system/bin/logcat"); // Test whether these buffers actually works const char* b[] = { "main", "events", "crash" }; for (int i = 0; i < 3; ++i) { - if (exec_command_sync(MIRRDIR "/bin/logcat", "-b", b[i], "-d", "-f", "/dev/null", NULL) == 0) + if (exec_command_sync(MIRRDIR "/system/bin/logcat", "-b", b[i], "-d", "-f", "/dev/null", NULL) == 0) vec_push_back_all(&log_cmd, "-b", b[i], NULL); } chmod("/dev/null", 0666); @@ -156,6 +149,13 @@ static void log_daemon() { xpthread_create(&thread, NULL, logcat_thread, NULL); pthread_detach(thread); + // Handle socket requests + struct sockaddr_un sun; + socklen_t len = setup_sockaddr(&sun, LOG_SOCKET); + int sockfd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (xbind(sockfd, (struct sockaddr*) &sun, len)) + exit(1); + xlisten(sockfd, 10); while(1) { int fd = xaccept4(sockfd, NULL, NULL, SOCK_CLOEXEC); switch(read_int(fd)) { @@ -165,23 +165,25 @@ static void log_daemon() { events[HIDE_EVENT].fd = fd; pthread_mutex_unlock(&lock); break; + case HANDSHAKE: + write_int(fd, HANDSHAKE); default: close(fd); - break; } } } int start_log_daemon() { if (!log_daemon_started) { - if (exec_command_sync(MIRRDIR "/bin/logcat", "-d", "-f", "/dev/null", NULL) == 0) { + if (exec_command_sync(MIRRDIR "/system/bin/logcat", "-d", "-f", "/dev/null", NULL) == 0) { if (fork_dont_care() == 0) log_daemon(); - // Wait till we can connect to log_daemon + log_daemon_started = 1; + // Wait till we can connect to log_daemon and receive ack int fd = connect_log_daemon(); write_int(fd, HANDSHAKE); + read_int(fd); close(fd); - log_daemon_started = 1; } chmod("/dev/null", 0666); } diff --git a/native/jni/daemon/socket.c b/native/jni/daemon/socket.c index d861b151a..9456c2fe7 100644 --- a/native/jni/daemon/socket.c +++ b/native/jni/daemon/socket.c @@ -143,21 +143,21 @@ void send_fd(int sockfd, int fd) { int read_int(int fd) { int val; - if (xxread(fd, &val, sizeof(int)) != sizeof(int)) + if (xxread(fd, &val, sizeof(val)) != sizeof(val)) return -1; return val; } int read_int_be(int fd) { uint32_t val; - if (xxread(fd, &val, sizeof(val)) != sizeof(int)) + if (xxread(fd, &val, sizeof(val)) != sizeof(val)) return -1; return ntohl(val); } void write_int(int fd, int val) { if (fd < 0) return; - xwrite(fd, &val, sizeof(int)); + xwrite(fd, &val, sizeof(val)); } void write_int_be(int fd, int val) {