diff --git a/native/jni/core/applets.cpp b/native/jni/core/applets.cpp index de9f5f161..4932bc9e0 100644 --- a/native/jni/core/applets.cpp +++ b/native/jni/core/applets.cpp @@ -8,17 +8,19 @@ #include #include -static int (*applet_main[]) (int, char *[]) = - { magisk_main, su_client_main, resetprop_main, magiskhide_main, nullptr }; +using namespace std::literals; -[[noreturn]] static void call_applets(int argc, char *argv[]) { +static int (*applet_main[]) (int, char *[]) = + { su_client_main, resetprop_main, magiskhide_main, nullptr }; + +[[noreturn]] static void call_applet(int argc, char **argv) { // Applets for (int i = 0; applet_names[i]; ++i) { if (strcmp(basename(argv[0]), applet_names[i]) == 0) { exit((*applet_main[i])(argc, argv)); } } - fprintf(stderr, "%s: applet not found\n", argv[0]); + fprintf(stderr, "%s: applet not found\n", basename(argv[0])); exit(1); } @@ -28,11 +30,24 @@ int main(int argc, char *argv[]) { cmdline_logging(); init_argv0(argc, argv); - if ((strcmp(basename(argv[0]), "magisk") == 0 && argc > 1 && argv[1][0] != '-')) { + if (basename(argv[0]) == "magisk.bin"sv) { + if (argc == 1) + return 1; + // Running through wrapper --argc; ++argv; } - call_applets(argc, argv); + if (basename(argv[0]) == "magisk"sv) { + if (argc > 1 && argv[1][0] != '-') { + // Calling applet via magisk [applet] args + --argc; + ++argv; + } else { + return magisk_main(argc, argv); + } + } + + call_applet(argc, argv); } diff --git a/native/jni/core/init.cpp b/native/jni/core/init.cpp index c999270f1..8a10e868b 100644 --- a/native/jni/core/init.cpp +++ b/native/jni/core/init.cpp @@ -25,8 +25,10 @@ #include "binaries.h" #ifdef USE_64BIT #include "binaries_arch64.h" +#define LIBNAME "lib64" #else #include "binaries_arch.h" +#define LIBNAME "lib" #endif #include "magiskrc.h" @@ -560,6 +562,11 @@ static inline void patch_socket_name(const char *path) { munmap(buf, size); } +static const char wrapper[] = +"#!/system/bin/sh\n" +"export LD_LIBRARY_PATH=\"$LD_LIBRARY_PATH:/apex/com.android.runtime/" LIBNAME "\"\n" +"exec /sbin/magisk.bin \"$0\" \"$@\"\n"; + void MagiskInit::setup_overlay() { char path[128]; int fd; @@ -583,8 +590,17 @@ void MagiskInit::setup_overlay() { fd = xopen("/sbin/magiskinit", O_WRONLY | O_CREAT, 0755); write(fd, self.buf, self.sz); close(fd); - dump_magisk("/sbin/magisk", 0755); - patch_socket_name("/sbin/magisk"); + if (access("/system/apex", F_OK) == 0) { + dump_magisk("/sbin/magisk.bin", 0755); + patch_socket_name("/sbin/magisk.bin"); + setfilecon("/sbin/magisk.bin", "u:object_r:" SEPOL_FILE_DOMAIN ":s0"); + fd = xopen("/sbin/magisk", O_WRONLY | O_CREAT, 0755); + write(fd, wrapper, sizeof(wrapper) - 1); + close(fd); + } else { + dump_magisk("/sbin/magisk", 0755); + patch_socket_name("/sbin/magisk"); + } setfilecon("/sbin/magisk", "u:object_r:" SEPOL_FILE_DOMAIN ":s0"); setfilecon("/sbin/magiskinit", "u:object_r:" SEPOL_FILE_DOMAIN ":s0"); diff --git a/native/jni/include/magisk.h b/native/jni/include/magisk.h index e3dee09e7..b60fc1164 100644 --- a/native/jni/include/magisk.h +++ b/native/jni/include/magisk.h @@ -19,14 +19,13 @@ #define MODULEUPGRADE SECURE_DIR "/modules_update" #define DATABIN SECURE_DIR "/magisk" #define MAGISKDB SECURE_DIR "/magisk.db" -#define SIMPLEMOUNT SECURE_DIR "/magisk_simple" #define BOOTCOUNT SECURE_DIR "/.boot_count" #define MANAGERAPK DATABIN "/magisk.apk" // Legacy crap #define LEGACYCORE MODULEROOT "/.core" -constexpr const char *applet_names[] = { "magisk", "su", "resetprop", "magiskhide", nullptr }; +constexpr const char *applet_names[] = { "su", "resetprop", "magiskhide", nullptr }; // Multi-call entrypoints int magisk_main(int argc, char *argv[]);