diff --git a/jni/daemon/daemon.c b/jni/daemon/daemon.c index 43994827e..0fa64ad8b 100644 --- a/jni/daemon/daemon.c +++ b/jni/daemon/daemon.c @@ -24,6 +24,9 @@ pthread_t sepol_patch; static void *request_handler(void *args) { + // Setup the default error handler for threads + err_handler = exit_thread; + int client = *((int *) args); free(args); client_request req = read_int(client); diff --git a/jni/daemon/late_start.c b/jni/daemon/late_start.c index e7ce857af..06e446267 100644 --- a/jni/daemon/late_start.c +++ b/jni/daemon/late_start.c @@ -1,19 +1,23 @@ /* late_start.c - late_start service actions */ +#include #include #include #include "magisk.h" #include "daemon.h" +#include "utils.h" void late_start(int client) { LOGI("** late_start service mode running\n"); // ack write_int(client, 0); - // TODO: Do something close(client); // Wait till the full patch is done pthread_join(sepol_patch, NULL); + + // Run scripts after full patch, most reliable way to run scripts + exec_common_script("service"); } diff --git a/jni/daemon/log_monitor.c b/jni/daemon/log_monitor.c index 89a22875b..e6e4b0aa6 100644 --- a/jni/daemon/log_monitor.c +++ b/jni/daemon/log_monitor.c @@ -14,8 +14,11 @@ #include "daemon.h" static void *logger_thread(void *args) { + // Setup error handler + err_handler = exit_thread; + char buffer[PATH_MAX]; - xrename("/cache/magisk.log", "/cache/last_magisk.log"); + rename("/cache/magisk.log", "/cache/last_magisk.log"); FILE *logfile = xfopen("/cache/magisk.log", "w"); // Disable buffering setbuf(logfile, NULL); diff --git a/jni/daemon/post_fs.c b/jni/daemon/post_fs.c index 8b18cf35a..19c28f7c7 100644 --- a/jni/daemon/post_fs.c +++ b/jni/daemon/post_fs.c @@ -11,7 +11,11 @@ void post_fs(int client) { LOGI("** post-fs mode running\n"); // ack write_int(client, 0); - // TODO: Do something + + // TODO: Simple bind mounts + close(client); + +unblock: unblock_boot_process(); } diff --git a/jni/daemon/post_fs_data.c b/jni/daemon/post_fs_data.c index 5f24c7dfc..99b6b9aac 100644 --- a/jni/daemon/post_fs_data.c +++ b/jni/daemon/post_fs_data.c @@ -19,6 +19,7 @@ static char *loopsetup(const char *img) { char device[20]; struct loop_info64 info; int i, lfd, ffd; + memset(&info, 0, sizeof(info)); // First get an empty loop device for (i = 0; i <= 7; ++i) { sprintf(device, "/dev/block/loop%d", i); @@ -31,9 +32,18 @@ static char *loopsetup(const char *img) { ffd = xopen(img, O_RDWR); if (ioctl(lfd, LOOP_SET_FD, ffd) == -1) return NULL; + strcpy((char *) info.lo_file_name, img); + ioctl(lfd, LOOP_SET_STATUS64, &info); return strdup(device); } +static void *start_magisk_hide(void *args) { + // Setup default error handler for thread + err_handler = exit_thread; + launch_magiskhide(-1); + return NULL; +} + char *mount_image(const char *img, const char *target) { char *device = loopsetup(img); if (device) @@ -54,15 +64,21 @@ void post_fs_data(int client) { char *magiskimg = mount_image("/data/magisk.img", "/magisk"); free(magiskimg); + // TODO: Magic Mounts, modules etc. + + // Run common scripts + exec_common_script("post-fs-data"); + // Start magiskhide if enabled char *hide_prop = getprop("persist.magisk.hide"); if (hide_prop) { - if (strcmp(hide_prop, "1") == 0) - launch_magiskhide(-1); + if (strcmp(hide_prop, "1") == 0) { + pthread_t thread; + xpthread_create(&thread, NULL, start_magisk_hide, NULL); + } free(hide_prop); } unblock: unblock_boot_process(); - return; } diff --git a/jni/magiskhide/magiskhide.c b/jni/magiskhide/magiskhide.c index 69d94842b..cd2aa1239 100644 --- a/jni/magiskhide/magiskhide.c +++ b/jni/magiskhide/magiskhide.c @@ -52,7 +52,8 @@ void launch_magiskhide(int client) { LOGI("* Starting MagiskHide\n"); hideEnabled = 1; - setprop("persist.magisk.hide", "1"); + init_resetprop(); + setprop2("persist.magisk.hide", "1", 0); hide_sensitive_props(); diff --git a/jni/magiskhide/pre_process.c b/jni/magiskhide/pre_process.c index 20a48a058..b8473b9a2 100644 --- a/jni/magiskhide/pre_process.c +++ b/jni/magiskhide/pre_process.c @@ -46,7 +46,6 @@ void hide_sensitive_props() { LOGI("hide_pre_proc: Hiding sensitive props\n"); // Hide all sensitive props - init_resetprop(); char *value; for (int i = 0; prop_key[i]; ++i) { value = getprop(prop_key[i]); diff --git a/jni/utils/misc.c b/jni/utils/misc.c index b731a8f31..d5d614cdf 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "magisk.h" #include "utils.h" @@ -117,7 +118,8 @@ void ps(void (*func)(int)) { DIR *dir; struct dirent *entry; - dir = xopendir("/proc"); + if (!(dir = xopendir("/proc"))) + return; while ((entry = xreaddir(dir))) { if (entry->d_type == DT_DIR) { @@ -216,22 +218,60 @@ void setup_sighandlers(void (*handler)(int)) { int run_command(int *fd, const char *path, char *const argv[]) { int sv[2]; - if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) == -1) - return -1; - // We use sv[0], give them sv[1] for communication - *fd = sv[1]; + + if (fd) { + if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) == -1) + return -1; + // We use sv[0], give them sv[1] for communication + *fd = sv[1]; + } int pid = fork(); if (pid != 0) { - close(sv[0]); + if (fd) close(sv[0]); return pid; } - close(sv[1]); - dup2(sv[0], STDIN_FILENO); - dup2(sv[0], STDOUT_FILENO); - dup2(sv[0], STDERR_FILENO); + if (fd) { + close(sv[1]); + xdup2(sv[0], STDIN_FILENO); + xdup2(sv[0], STDOUT_FILENO); + xdup2(sv[0], STDERR_FILENO); + } else { + int null = open("/dev/null", O_RDWR); + xdup2(null, STDIN_FILENO); + xdup2(null, STDOUT_FILENO); + xdup2(null, STDERR_FILENO); + } + execv(path, argv); PLOGE("execv"); return -1; } + +#define MAGISK_CORE "/magisk/.core/" + +void exec_common_script(const char* stage) { + DIR *dir; + struct dirent *entry; + char buf[PATH_MAX]; + snprintf(buf, sizeof(buf), MAGISK_CORE "%s.d", stage); + + if (!(dir = opendir(buf))) + return; + + while ((entry = xreaddir(dir))) { + if (entry->d_type == DT_REG) { + snprintf(buf, sizeof(buf), MAGISK_CORE "%s.d/%s", stage, entry->d_name); + if (access(buf, X_OK) == -1) + continue; + LOGI("%s.d: exec [%s]\n", stage, entry->d_name); + char *const command[] = { "sh", buf, NULL }; + int pid = run_command(NULL, "/system/bin/sh", command); + if (pid != -1) + waitpid(pid, NULL, 0); + } + } + + closedir(dir); +} diff --git a/jni/utils/utils.h b/jni/utils/utils.h index 5008099d5..69be519a6 100644 --- a/jni/utils/utils.h +++ b/jni/utils/utils.h @@ -74,5 +74,6 @@ void unlock_blocks(); void unblock_boot_process(); void setup_sighandlers(void (*handler)(int)); int run_command(int *fd, const char *path, char *const argv[]); +void exec_common_script(const char* stage); #endif