From f1d9015e5f425a66a8093ef63dc0833ac75c267a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 15 Jun 2019 22:25:09 -0700 Subject: [PATCH] Move load kernel info out of class --- native/jni/init/early_mount.cpp | 10 +++++----- native/jni/init/getinfo.cpp | 24 ++++++++++++------------ native/jni/init/init.cpp | 18 ++++++++++-------- native/jni/init/init.h | 6 +++--- native/jni/init/rootfs.cpp | 2 +- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/native/jni/init/early_mount.cpp b/native/jni/init/early_mount.cpp index 472d2bf9e..c4b4a572c 100644 --- a/native/jni/init/early_mount.cpp +++ b/native/jni/init/early_mount.cpp @@ -76,14 +76,14 @@ static void setup_block(const char *partname, char *block_dev) { bool MagiskInit::read_dt_fstab(const char *name, char *partname, char *fstype) { char path[128]; int fd; - sprintf(path, "%s/fstab/%s/dev", cmd.dt_dir, name); + sprintf(path, "%s/fstab/%s/dev", cmd->dt_dir, name); if ((fd = xopen(path, O_RDONLY | O_CLOEXEC)) >= 0) { read(fd, path, sizeof(path)); close(fd); // Some custom treble use different names, so use what we read char *part = rtrim(strrchr(path, '/') + 1); - sprintf(partname, "%s%s", part, strend(part, cmd.slot) ? cmd.slot : ""); - sprintf(path, "%s/fstab/%s/type", cmd.dt_dir, name); + sprintf(partname, "%s%s", part, strend(part, cmd->slot) ? cmd->slot : ""); + sprintf(path, "%s/fstab/%s/type", cmd->dt_dir, name); if ((fd = xopen(path, O_RDONLY | O_CLOEXEC)) >= 0) { read(fd, fstype, 32); close(fd); @@ -111,9 +111,9 @@ void MagiskInit::early_mount() { char fstype[32]; char block_dev[64]; - if (cmd.system_as_root) { + if (cmd->system_as_root) { LOGD("Early mount system_root\n"); - sprintf(partname, "system%s", cmd.slot); + sprintf(partname, "system%s", cmd->slot); setup_block(partname, block_dev); xmkdir("/system_root", 0755); if (xmount(block_dev, "/system_root", "ext4", MS_RDONLY, nullptr)) diff --git a/native/jni/init/getinfo.cpp b/native/jni/init/getinfo.cpp index a90c7b916..3f9bf2e95 100644 --- a/native/jni/init/getinfo.cpp +++ b/native/jni/init/getinfo.cpp @@ -92,7 +92,7 @@ static bool check_key_combo() { return false; } -void MagiskInit::load_kernel_info() { +void load_kernel_info(cmdline *cmd) { // Communicate with kernel using procfs and sysfs xmkdir("/proc", 0755); xmount("proc", "/proc", "proc", 0, nullptr); @@ -106,14 +106,14 @@ void MagiskInit::load_kernel_info() { parse_cmdline([&](auto key, auto value) -> void { LOGD("cmdline: [%s]=[%s]\n", key.data(), value); if (key == "androidboot.slot_suffix") { - strcpy(cmd.slot, value); + strcpy(cmd->slot, value); } else if (key == "androidboot.slot") { - cmd.slot[0] = '_'; - strcpy(cmd.slot + 1, value); + cmd->slot[0] = '_'; + strcpy(cmd->slot + 1, value); } else if (key == "skip_initramfs") { - cmd.system_as_root = true; + cmd->system_as_root = true; } else if (key == "androidboot.android_dt_dir") { - strcpy(cmd.dt_dir, value); + strcpy(cmd->dt_dir, value); } else if (key == "enter_recovery") { enter_recovery = value[0] == '1'; } else if (key == "androidboot.hardware") { @@ -140,13 +140,13 @@ void MagiskInit::load_kernel_info() { if (recovery_mode) { LOGD("Running in recovery mode, waiting for key...\n"); - cmd.system_as_root = !check_key_combo(); + cmd->system_as_root = !check_key_combo(); } - if (cmd.dt_dir[0] == '\0') - strcpy(cmd.dt_dir, DEFAULT_DT_DIR); + if (cmd->dt_dir[0] == '\0') + strcpy(cmd->dt_dir, DEFAULT_DT_DIR); - LOGD("system_as_root=[%d]\n", cmd.system_as_root); - LOGD("slot=[%s]\n", cmd.slot); - LOGD("dt_dir=[%s]\n", cmd.dt_dir); + LOGD("system_as_root=[%d]\n", cmd->system_as_root); + LOGD("slot=[%s]\n", cmd->slot); + LOGD("dt_dir=[%s]\n", cmd->dt_dir); } diff --git a/native/jni/init/init.cpp b/native/jni/init/init.cpp index 80d50754a..2df588336 100644 --- a/native/jni/init/init.cpp +++ b/native/jni/init/init.cpp @@ -120,7 +120,7 @@ static int dump_manager(const char *path, mode_t mode) { void MagiskInit::preset() { root = open("/", O_RDONLY | O_CLOEXEC); - if (cmd.system_as_root) { + if (cmd->system_as_root) { // Clear rootfs LOGD("Cleaning rootfs\n"); frm_rf(root, { "overlay", "proc", "sys" }); @@ -171,10 +171,6 @@ void MagiskInit::start() { if (null > STDERR_FILENO) close(null); - setup_klog(); - - load_kernel_info(); - full_read("/init", &self.buf, &self.sz); full_read("/.backup/.magisk", &config.buf, &config.sz); @@ -192,7 +188,6 @@ void MagiskInit::test() { chroot("."); chdir("/"); - load_kernel_info(); preset(); early_mount(); setup_rootfs(); @@ -200,7 +195,9 @@ void MagiskInit::test() { } static int test_main(int, char *argv[]) { - MagiskInit init(argv); + cmdline cmd{}; + load_kernel_info(&cmd); + MagiskInit init(argv, &cmd); init.test(); return 0; } @@ -223,7 +220,12 @@ int main(int argc, char *argv[]) { if (getpid() != 1) return 1; - MagiskInit init(argv); + setup_klog(); + + cmdline cmd{}; + load_kernel_info(&cmd); + + MagiskInit init(argv, &cmd); // Run the main routine init.start(); diff --git a/native/jni/init/init.h b/native/jni/init/init.h index 078651a38..5920c0f92 100644 --- a/native/jni/init/init.h +++ b/native/jni/init/init.h @@ -13,7 +13,7 @@ struct raw_data { class MagiskInit { private: - cmdline cmd{}; + cmdline *cmd; raw_data self{}; raw_data config{}; int root = -1; @@ -24,7 +24,6 @@ private: bool mnt_product = false; bool mnt_odm = false; - void load_kernel_info(); void preset(); void early_mount(); void setup_rootfs(); @@ -34,7 +33,7 @@ private: void re_exec_init(); public: - explicit MagiskInit(char *argv[]) : argv(argv) {} + explicit MagiskInit(char *argv[], cmdline *cmd) : cmd(cmd), argv(argv) {} void start(); void test(); }; @@ -46,4 +45,5 @@ static inline bool is_lnk(const char *name) { return S_ISLNK(st.st_mode); } +void load_kernel_info(cmdline *cmd); int dump_magisk(const char *path, mode_t mode); diff --git a/native/jni/init/rootfs.cpp b/native/jni/init/rootfs.cpp index 609c39b3b..20783b5dc 100644 --- a/native/jni/init/rootfs.cpp +++ b/native/jni/init/rootfs.cpp @@ -40,7 +40,7 @@ constexpr const char wrapper[] = void MagiskInit::setup_rootfs() { bool patch_init = patch_sepolicy(); - if (cmd.system_as_root) { + if (cmd->system_as_root) { // Clone rootfs LOGD("Clone root dir from system to rootfs\n"); int system_root = xopen("/system_root", O_RDONLY | O_CLOEXEC);