diff --git a/native/jni/core/bootstages.c b/native/jni/core/bootstages.c index 0f084f5a9..377484eb2 100644 --- a/native/jni/core/bootstages.c +++ b/native/jni/core/bootstages.c @@ -662,7 +662,7 @@ void startup() { } // Start post-fs-data mode - execl("/sbin/magisk", "magisk", "--post-fs-data", NULL); + execl("/sbin/magisk.bin", "magisk", "--post-fs-data", NULL); } void post_fs_data(int client) { diff --git a/native/jni/core/daemon.c b/native/jni/core/daemon.c index 9714c0fff..d76b684cd 100644 --- a/native/jni/core/daemon.c +++ b/native/jni/core/daemon.c @@ -83,6 +83,9 @@ static void *request_handler(void *args) { case LATE_START: late_start(client); break; + case MONITOR: + /* Do NOT close the client, make it hold */ + break; default: close(client); break; @@ -117,13 +120,6 @@ void main_daemon() { xdup2(fd, STDIN_FILENO); close(fd); - // Block user signals - sigset_t block_set; - sigemptyset(&block_set); - sigaddset(&block_set, SIGUSR1); - sigaddset(&block_set, SIGUSR2); - pthread_sigmask(SIG_SETMASK, &block_set, NULL); - // Start the log monitor monitor_logs(); @@ -138,6 +134,19 @@ void main_daemon() { // Change process name strcpy(argv0, "magiskd"); + // Block all user signals + sigset_t block_set; + sigemptyset(&block_set); + sigaddset(&block_set, SIGUSR1); + sigaddset(&block_set, SIGUSR2); + pthread_sigmask(SIG_SETMASK, &block_set, NULL); + + // Ignore SIGPIPE + struct sigaction act; + memset(&act, 0, sizeof(act)); + act.sa_handler = SIG_IGN; + sigaction(SIGPIPE, &act, NULL); + // Loop forever to listen for requests while(1) { int *client = xmalloc(sizeof(int)); diff --git a/native/jni/core/log_monitor.c b/native/jni/core/log_monitor.c index 657c6cf04..8f8de7777 100644 --- a/native/jni/core/log_monitor.c +++ b/native/jni/core/log_monitor.c @@ -85,6 +85,26 @@ static void *socket_thread(void *args) { } } +static void *monitor_thread(void *args) { + // Block SIGPIPE to prevent interruption + sigset_t block_set; + sigemptyset(&block_set); + sigaddset(&block_set, SIGPIPE); + pthread_sigmask(SIG_SETMASK, &block_set, NULL); + // Give the main daemon some time before we monitor it + sleep(5); + int fd; + char b[1]; + do { + fd = connect_daemon(); + write_int(fd, MONITOR); + // This should hold unless the daemon is killed + read(fd, b, sizeof(b)); + // The main daemon crashed, spawn a new one + close(fd); + } while (1); +} + void log_daemon() { setsid(); strcpy(argv0, "magisklogd"); @@ -96,6 +116,13 @@ void log_daemon() { xlisten(sockfd, 1); LOGI("Magisk v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") logger started\n"); + // Start invincible mode monitor + // TODO: Remove this when all crashes are fixed + pthread_t t; + pthread_create(&t, NULL, monitor_thread, NULL); + pthread_detach(t); + + // Set SIGPIPE handler struct sigaction act; memset(&act, 0, sizeof(act)); act.sa_handler = sigpipe_handler; diff --git a/native/jni/include/daemon.h b/native/jni/include/daemon.h index 49203bd79..401c2b76d 100644 --- a/native/jni/include/daemon.h +++ b/native/jni/include/daemon.h @@ -25,6 +25,7 @@ enum { ADD_HIDELIST, RM_HIDELIST, LS_HIDELIST, + MONITOR, HIDE_CONNECT }; diff --git a/native/jni/su b/native/jni/su index ead8c03a3..69b226b00 160000 --- a/native/jni/su +++ b/native/jni/su @@ -1 +1 @@ -Subproject commit ead8c03a3a6eed6621ffcf4bc2fd12429da3596e +Subproject commit 69b226b005e3f87c4c872fa3382e05006969118e