From d959c35723968d660d555529dd46f50fec8d1158 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 18 Apr 2020 18:50:25 -0700 Subject: [PATCH] Make cleaner mount info --- native/jni/core/module.cpp | 2 +- native/jni/init/init.hpp | 1 - native/jni/init/mount.cpp | 6 ++++-- native/jni/init/rootdir.cpp | 20 ++++++++++++-------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/native/jni/core/module.cpp b/native/jni/core/module.cpp index 7b7cbff1b..c5a3c3659 100644 --- a/native/jni/core/module.cpp +++ b/native/jni/core/module.cpp @@ -165,7 +165,7 @@ public: template T *emplace_or_get(string_view name, Args &...args) { return iter_to_node(insert(children.find(name), type_id(), - [&](auto _) { return new T(std::forward(args)...); }, true)); + [&](auto _) { return new T(std::forward(args)...); }, true)); } // Return upgraded node or null if rejected diff --git a/native/jni/init/init.hpp b/native/jni/init/init.hpp index 23b6382df..8a749755b 100644 --- a/native/jni/init/init.hpp +++ b/native/jni/init/init.hpp @@ -69,7 +69,6 @@ class SARBase : public MagiskInit { protected: raw_data config; std::vector overlays; - std::string tmp_dir; void backup_files(); void patch_rootdir(); diff --git a/native/jni/init/mount.cpp b/native/jni/init/mount.cpp index 73887e00d..138f7430e 100644 --- a/native/jni/init/mount.cpp +++ b/native/jni/init/mount.cpp @@ -153,11 +153,13 @@ static void switch_root(const string &path) { static void mount_persist(const char *dev_base, const char *mnt_base) { string mnt_point = mnt_base + "/persist"s; strcpy(partname, "persist"); - sprintf(block_dev, "%s/persist", dev_base); + xrealpath(dev_base, block_dev); + char *s = block_dev + strlen(block_dev); + strcpy(s, "/persist"); if (setup_block(false) < 0) { // Fallback to cache strcpy(partname, "cache"); - sprintf(block_dev, "%s/cache", dev_base); + strcpy(s, "/cache"); if (setup_block(false) < 0) { // Try NVIDIA's BS strcpy(partname, "CAC"); diff --git a/native/jni/init/rootdir.cpp b/native/jni/init/rootdir.cpp index 628681595..998a7ebb6 100644 --- a/native/jni/init/rootdir.cpp +++ b/native/jni/init/rootdir.cpp @@ -222,24 +222,28 @@ static void magic_mount(const string &sdir, const string &ddir = "") { #define LIBSELINUX "/system/" LIBNAME "/libselinux.so" void SARBase::patch_rootdir() { + char tmp_dir[128]; + // TODO: dynamic paths - tmp_dir = "/sbin"; + char *p = tmp_dir + sprintf(tmp_dir, "%s", "/sbin"); - setup_tmp(tmp_dir.data(), self, config); - persist_dir = tmp_dir + "/" MIRRDIR "/persist"; + setup_tmp(tmp_dir, self, config); + persist_dir = string(tmp_dir) + "/" MIRRDIR "/persist"; - chdir(tmp_dir.data()); + chdir(tmp_dir); // Mount system_root mirror struct stat st; xstat("/", &st); xmkdir(ROOTMIR, 0755); mknod(ROOTBLK, S_IFBLK | 0600, st.st_dev); - if (xmount(ROOTBLK, ROOTMIR, "ext4", MS_RDONLY, nullptr)) - xmount(ROOTBLK, ROOTMIR, "erofs", MS_RDONLY, nullptr); + strcpy(p, "/" ROOTBLK); + if (xmount(tmp_dir, ROOTMIR, "ext4", MS_RDONLY, nullptr)) + xmount(tmp_dir, ROOTMIR, "erofs", MS_RDONLY, nullptr); + *p = '\0'; // Recreate original sbin structure if necessary - if (tmp_dir == "/sbin") + if (tmp_dir == "/sbin"sv) recreate_sbin(ROOTMIR "/sbin", true); // Patch init @@ -303,7 +307,7 @@ void SARBase::patch_rootdir() { if (connect(sockfd, (struct sockaddr*) &sun, setup_sockaddr(&sun, INIT_SOCKET)) == 0) { LOGD("ACK init tracer to write backup files\n"); // Let tracer know where tmp_dir is - write_string(sockfd, tmp_dir.data()); + write_string(sockfd, tmp_dir); // Wait for tracer to finish copying files int ack; read(sockfd, &ack, sizeof(ack));