Commit d3b7b419 authored by topjohnwu's avatar topjohnwu

Fix kmsg logging in magiskinit

parent da159e46
...@@ -31,31 +31,44 @@ constexpr int (*init_applet_main[])(int, char *[]) = ...@@ -31,31 +31,44 @@ constexpr int (*init_applet_main[])(int, char *[]) =
#ifdef MAGISK_DEBUG #ifdef MAGISK_DEBUG
static FILE *kmsg; static FILE *kmsg;
static char kmsg_buf[4096];
static int vprintk(const char *fmt, va_list ap) { static int vprintk(const char *fmt, va_list ap) {
fprintf(kmsg, "magiskinit: "); vsnprintf(kmsg_buf + 12, sizeof(kmsg_buf) - 12, fmt, ap);
return vfprintf(kmsg, fmt, ap); return fprintf(kmsg, "%s", kmsg_buf);
} }
static void setup_klog() { void setup_klog() {
mknod("/kmsg", S_IFCHR | 0666, makedev(1, 11)); // Shut down first 3 fds
int fd = xopen("/kmsg", O_WRONLY | O_CLOEXEC); int null;
kmsg = fdopen(fd, "w"); if (access("/dev/null", W_OK) == 0) {
setbuf(kmsg, nullptr); null = xopen("/dev/null", O_RDWR | O_CLOEXEC);
unlink("/kmsg"); } else {
log_cb.d = log_cb.i = log_cb.w = log_cb.e = vprintk;
log_cb.ex = nop_ex;
// Prevent file descriptor confusion
mknod("/null", S_IFCHR | 0666, makedev(1, 3)); mknod("/null", S_IFCHR | 0666, makedev(1, 3));
int null = xopen("/null", O_RDWR | O_CLOEXEC); null = xopen("/null", O_RDWR | O_CLOEXEC);
unlink("/null"); unlink("/null");
}
xdup3(null, STDIN_FILENO, O_CLOEXEC); xdup3(null, STDIN_FILENO, O_CLOEXEC);
xdup3(null, STDOUT_FILENO, O_CLOEXEC); xdup3(null, STDOUT_FILENO, O_CLOEXEC);
xdup3(null, STDERR_FILENO, O_CLOEXEC); xdup3(null, STDERR_FILENO, O_CLOEXEC);
if (null > STDERR_FILENO) if (null > STDERR_FILENO)
close(null); close(null);
int fd;
if (access("/proc/kmsg", W_OK) == 0) {
fd = xopen("/proc/kmsg", O_WRONLY | O_CLOEXEC);
} else {
mknod("/kmsg", S_IFCHR | 0666, makedev(1, 11));
fd = xopen("/kmsg", O_WRONLY | O_CLOEXEC);
unlink("/kmsg");
}
kmsg = fdopen(fd, "w");
setbuf(kmsg, nullptr);
log_cb.d = log_cb.i = log_cb.w = log_cb.e = vprintk;
log_cb.ex = nop_ex;
strcpy(kmsg_buf, "magiskinit: ");
} }
#else #else
#define setup_klog(...) void setup_klog() {}
#endif #endif
static bool unxz(int fd, const uint8_t *buf, size_t size) { static bool unxz(int fd, const uint8_t *buf, size_t size) {
...@@ -131,13 +144,11 @@ public: ...@@ -131,13 +144,11 @@ public:
} }
}; };
class TestInit : public SARInit { class TestInit : public BaseInit {
public: public:
TestInit(char *argv[], cmdline *cmd) : SARInit(argv, cmd) {}; TestInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {};
void start() override { void start() override {
early_mount(); // Write init tests here
patch_rootdir();
cleanup();
} }
}; };
......
...@@ -168,3 +168,4 @@ public: ...@@ -168,3 +168,4 @@ public:
void load_kernel_info(cmdline *cmd); void load_kernel_info(cmdline *cmd);
int dump_magisk(const char *path, mode_t mode); int dump_magisk(const char *path, mode_t mode);
int magisk_proxy_main(int argc, char *argv[]); int magisk_proxy_main(int argc, char *argv[]);
void setup_klog();
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include <utils.h> #include <utils.h>
#include "init.h" #include "init.h"
#include "flags.h"
#include "magiskrc.h" #include "magiskrc.h"
#ifdef USE_64BIT #ifdef USE_64BIT
...@@ -466,23 +465,6 @@ void AFirstStageInit::prepare() { ...@@ -466,23 +465,6 @@ void AFirstStageInit::prepare() {
rename("/.backup/init", "/init"); rename("/.backup/init", "/init");
} }
#ifdef MAGISK_DEBUG
static FILE *kmsg;
static int vprintk(const char *fmt, va_list ap) {
fprintf(kmsg, "magiskinit: ");
return vfprintf(kmsg, fmt, ap);
}
static void setup_klog() {
int fd = xopen("/proc/kmsg", O_WRONLY | O_CLOEXEC);
kmsg = fdopen(fd, "w");
setbuf(kmsg, nullptr);
log_cb.d = log_cb.i = log_cb.w = log_cb.e = vprintk;
log_cb.ex = nop_ex;
}
#else
#define setup_klog(...)
#endif
int magisk_proxy_main(int argc, char *argv[]) { int magisk_proxy_main(int argc, char *argv[]) {
setup_klog(); setup_klog();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment