From 0f55fcafe88e3330f3b38d82eef19814f122a1ab Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 12 Feb 2019 16:13:31 -0500 Subject: [PATCH] Migrate EXT4 images instead of removing them --- native/jni/core/bootstages.cpp | 38 +++++++++++++++++++++++++------- native/jni/utils/include/utils.h | 12 +++++++--- native/jni/utils/misc.cpp | 6 +---- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/native/jni/core/bootstages.cpp b/native/jni/core/bootstages.cpp index e0bd27921..651316efe 100644 --- a/native/jni/core/bootstages.cpp +++ b/native/jni/core/bootstages.cpp @@ -324,9 +324,7 @@ static void exec_common_script(const char* stage) { continue; LOGI("%s.d: exec [%s]\n", stage, entry->d_name); exec_t exec { .pre_exec = strcmp(stage, "post-fs-data") ? set_path : set_mirror_path }; - int pid = exec_command(exec, MIRRDIR "/system/bin/sh", entry->d_name); - if (pid != -1) - waitpid(pid, nullptr, 0); + exec_command_sync(exec, MIRRDIR "/system/bin/sh", entry->d_name); } } @@ -342,9 +340,7 @@ static void exec_module_script(const char* stage) { continue; LOGI("%s: exec [%s.sh]\n", module, stage); exec_t exec { .pre_exec = strcmp(stage, "post-fs-data") ? set_path : set_mirror_path }; - int pid = exec_command(exec, MIRRDIR "/system/bin/sh", buf2); - if (pid != -1) - waitpid(pid, nullptr, 0); + exec_command_sync(exec, MIRRDIR "/system/bin/sh", buf2); } } @@ -417,8 +413,6 @@ static bool magisk_env() { // Remove legacy stuffs unlink("/data/magisk.img"); unlink("/data/magisk_debug.log"); - unlink(SECURE_DIR "/magisk.img"); - unlink(SECURE_DIR "/magisk_merge.img"); // Legacy support symlink(MAGISKTMP, "/sbin/.core"); @@ -487,7 +481,35 @@ static bool magisk_env() { return true; } +/* Too lazy to do it natively, let's write some scripts */ +static const char migrate_cmd[] = +"IMG=%s;" +"MNT=/dev/img_mnt;" +"e2fsck -yf $IMG;" +"mkdir -p $MNT;" +"for num in 0 1 2 3 4 5 6 7; do" +" losetup /dev/block/loop${num} $IMG || continue;" +" mount -t ext4 /dev/block/loop${num} $MNT;" +" rm -rf $MNT/lost+found $MNT/.core;" +" magisk --clone $MNT " MODULEROOT ";" +" umount $MNT;" +" rm -rf $MNT;" +" losetup -d /dev/block/loop${num};" +" break;" +"done;" +"rm -rf $IMG"; + static void upgrade_modules() { + const char *legacy_imgs[] = {SECURE_DIR "/magisk.img", SECURE_DIR "/magisk_merge.img"}; + for (auto img : legacy_imgs) { + if (access(img, F_OK) == 0) { + LOGI("* Migrating %s\n", img); + exec_t exec { .pre_exec = set_path }; + char cmds[sizeof(migrate_cmd) + 32]; + sprintf(cmds, migrate_cmd, img); + exec_command_sync(exec, "/system/bin/sh", "-c", cmds); + } + } DIR *dir; struct dirent *entry; if ((dir = opendir(MODULEUPGRADE))) { diff --git a/native/jni/utils/include/utils.h b/native/jni/utils/include/utils.h index e8d9cfbcc..fa384e1b7 100644 --- a/native/jni/utils/include/utils.h +++ b/native/jni/utils/include/utils.h @@ -162,11 +162,17 @@ int exec_command(exec_t &exec, Args &&...args) { exec.argv = argv; return exec_command(exec); } -int exec_command_sync(const char **argv); +int exec_command_sync(exec_t &exec); +template +int exec_command_sync(exec_t &exec, Args &&...args) { + const char *argv[] = {args..., nullptr}; + exec.argv = argv; + return exec_command_sync(exec); +} template int exec_command_sync(Args &&...args) { - const char *argv[] = {args..., nullptr}; - return exec_command_sync(argv); + exec_t exec{}; + return exec_command_sync(exec, args...); } #endif diff --git a/native/jni/utils/misc.cpp b/native/jni/utils/misc.cpp index 1c9217c89..3c3891655 100644 --- a/native/jni/utils/misc.cpp +++ b/native/jni/utils/misc.cpp @@ -11,13 +11,10 @@ #include #include #include -#include #include #include -using namespace std; - unsigned get_shell_uid() { struct passwd* ppwd = getpwnam("shell"); if (nullptr == ppwd) @@ -195,8 +192,7 @@ int exec_command(exec_t &exec) { exit(-1); } -int exec_command_sync(const char **argv) { - exec_t exec { .argv = argv }; +int exec_command_sync(exec_t &exec) { int pid, status; pid = exec_command(exec); if (pid < 0)