Make sure magisklogd is properly initialized

This commit is contained in:
topjohnwu 2018-10-28 04:24:53 -04:00
parent 5fc7079023
commit 8ec3086cdd
3 changed files with 20 additions and 20 deletions

View File

@ -587,6 +587,7 @@ static const char wrapper[] =
"exec /sbin/magisk.bin \"${0##*/}\" \"$@\"\n"; "exec /sbin/magisk.bin \"${0##*/}\" \"$@\"\n";
void startup() { void startup() {
android_logging();
if (!check_data()) if (!check_data())
unblock_boot_process(); unblock_boot_process();
@ -782,9 +783,6 @@ void startup() {
xsymlink(MIRRDIR "/bin/busybox", BBPATH "/busybox"); xsymlink(MIRRDIR "/bin/busybox", BBPATH "/busybox");
} }
// Preserve a copy of logcat
cp_afc("/system/bin/logcat", MIRRDIR "/bin/logcat");
// Start post-fs-data mode // Start post-fs-data mode
execl("/sbin/magisk.bin", "magisk", "--post-fs-data", NULL); execl("/sbin/magisk.bin", "magisk", "--post-fs-data", NULL);
} }

View File

@ -19,8 +19,7 @@
#include "flags.h" #include "flags.h"
int log_daemon_started = 0; int log_daemon_started = 0;
static struct vector log_cmd, clear_cmd; static struct vector log_cmd, clear_cmd;;
static int sockfd;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
enum { enum {
@ -111,12 +110,6 @@ static void *logcat_thread(void *args) {
static void log_daemon() { static void log_daemon() {
setsid(); 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"); LOGI("Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") logger started\n");
strcpy(argv0, "magisklogd"); strcpy(argv0, "magisklogd");
@ -132,11 +125,11 @@ static void log_daemon() {
// Construct cmdline // Construct cmdline
vec_init(&log_cmd); 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 // Test whether these buffers actually works
const char* b[] = { "main", "events", "crash" }; const char* b[] = { "main", "events", "crash" };
for (int i = 0; i < 3; ++i) { 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); vec_push_back_all(&log_cmd, "-b", b[i], NULL);
} }
chmod("/dev/null", 0666); chmod("/dev/null", 0666);
@ -156,6 +149,13 @@ static void log_daemon() {
xpthread_create(&thread, NULL, logcat_thread, NULL); xpthread_create(&thread, NULL, logcat_thread, NULL);
pthread_detach(thread); 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) { while(1) {
int fd = xaccept4(sockfd, NULL, NULL, SOCK_CLOEXEC); int fd = xaccept4(sockfd, NULL, NULL, SOCK_CLOEXEC);
switch(read_int(fd)) { switch(read_int(fd)) {
@ -165,23 +165,25 @@ static void log_daemon() {
events[HIDE_EVENT].fd = fd; events[HIDE_EVENT].fd = fd;
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
break; break;
case HANDSHAKE:
write_int(fd, HANDSHAKE);
default: default:
close(fd); close(fd);
break;
} }
} }
} }
int start_log_daemon() { int start_log_daemon() {
if (!log_daemon_started) { 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) if (fork_dont_care() == 0)
log_daemon(); 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(); int fd = connect_log_daemon();
write_int(fd, HANDSHAKE); write_int(fd, HANDSHAKE);
read_int(fd);
close(fd); close(fd);
log_daemon_started = 1;
} }
chmod("/dev/null", 0666); chmod("/dev/null", 0666);
} }

View File

@ -143,21 +143,21 @@ void send_fd(int sockfd, int fd) {
int read_int(int fd) { int read_int(int fd) {
int val; int val;
if (xxread(fd, &val, sizeof(int)) != sizeof(int)) if (xxread(fd, &val, sizeof(val)) != sizeof(val))
return -1; return -1;
return val; return val;
} }
int read_int_be(int fd) { int read_int_be(int fd) {
uint32_t val; uint32_t val;
if (xxread(fd, &val, sizeof(val)) != sizeof(int)) if (xxread(fd, &val, sizeof(val)) != sizeof(val))
return -1; return -1;
return ntohl(val); return ntohl(val);
} }
void write_int(int fd, int val) { void write_int(int fd, int val) {
if (fd < 0) return; if (fd < 0) return;
xwrite(fd, &val, sizeof(int)); xwrite(fd, &val, sizeof(val));
} }
void write_int_be(int fd, int val) { void write_int_be(int fd, int val) {