From 14aa6041ec25072be95a2c0feb7599607decee35 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 17 Feb 2019 22:30:23 -0500 Subject: [PATCH] Use a better function to read through files --- native/jni/core/bootstages.cpp | 23 ++++++++--------- native/jni/magiskhide/hide_utils.cpp | 7 +++--- native/jni/magiskhide/proc_monitor.cpp | 31 ++++++++++------------- native/jni/utils/file.cpp | 35 +++++++++++++------------- native/jni/utils/include/utils.h | 4 ++- 5 files changed, 49 insertions(+), 51 deletions(-) diff --git a/native/jni/core/bootstages.cpp b/native/jni/core/bootstages.cpp index 41299e2dc..fbd5fba05 100644 --- a/native/jni/core/bootstages.cpp +++ b/native/jni/core/bootstages.cpp @@ -378,35 +378,35 @@ static bool magisk_env() { xmkdir(SECURE_DIR "/service.d", 0755); LOGI("* Mounting mirrors"); - auto mounts = file_to_vector("/proc/mounts"); bool system_as_root = false; - for (auto &line : mounts) { + file_readline("/proc/mounts", [&](string_view &line) -> bool { if (str_contains(line, " /system_root ")) { bind_mount("/system_root/system", MIRRDIR "/system"); - sscanf(line.c_str(), "%s", buf); + sscanf(line.data(), "%s", buf); system_block = strdup(buf); system_as_root = true; } else if (!system_as_root && str_contains(line, " /system ")) { - sscanf(line.c_str(), "%s %*s %s", buf, buf2); + sscanf(line.data(), "%s %*s %s", buf, buf2); system_block = strdup(buf); xmount(system_block, MIRRDIR "/system", buf2, MS_RDONLY, nullptr); VLOGI("mount", system_block, MIRRDIR "/system"); } else if (str_contains(line, " /vendor ")) { seperate_vendor = true; - sscanf(line.c_str(), "%s %*s %s", buf, buf2); + sscanf(line.data(), "%s %*s %s", buf, buf2); vendor_block = strdup(buf); xmkdir(MIRRDIR "/vendor", 0755); xmount(vendor_block, MIRRDIR "/vendor", buf2, MS_RDONLY, nullptr); VLOGI("mount", vendor_block, MIRRDIR "/vendor"); } else if (str_contains(line, " /data ")) { - sscanf(line.c_str(), "%s", buf); + sscanf(line.data(), "%s", buf); data_block = strdup(buf); } else if (SDK_INT >= 24 && str_contains(line, " /proc ") && !str_contains(line, "hidepid=2")) { // Enforce hidepid xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009"); } - } + return true; + }); if (!seperate_vendor) { xsymlink(MIRRDIR "/system/vendor", MIRRDIR "/vendor"); VLOGI("link", MIRRDIR "/system/vendor", MIRRDIR "/vendor"); @@ -492,12 +492,11 @@ static void collect_modules() { static bool check_data() { bool mnt = false; bool data = false; - auto mounts = file_to_vector("/proc/mounts"); - for (auto &line : mounts) { - if (line.find(" /data ") != string::npos && - line.find("tmpfs") == string::npos) + file_readline("/proc/mounts", [&](string_view &s) -> bool { + if (str_contains(s, " /data ") && !str_contains(s, "tmpfs")) mnt = true; - } + return true; + }); if (mnt) { auto crypto = getprop("ro.crypto.state"); if (!crypto.empty()) { diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index 3661f99dc..15000c64e 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -263,9 +263,10 @@ bool init_list() { // Migrate old hide list into database if (access(LEGACY_LIST, R_OK) == 0) { - auto tmp = file_to_vector(LEGACY_LIST); - for (auto &s : tmp) - add_list(s.c_str()); + file_readline(LEGACY_LIST, [](string_view &s) -> bool { + add_list(s.data()); + return true; + }); unlink(LEGACY_LIST); } diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index 7c85c20ce..a1afe3991 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -97,41 +97,36 @@ static bool is_pid_safetynet_process(const int pid) { } static void hide_daemon(int pid) { - LOGD("hide_daemon: handling PID=[%d]\n", pid); - char buffer[4096]; - vector mounts; - - manage_selinux(); - clean_magisk_props(); - if (switch_mnt_ns(pid)) goto exit; + LOGD("hide_daemon: handling PID=[%d]\n", pid); + manage_selinux(); + clean_magisk_props(); snprintf(buffer, sizeof(buffer), "/proc/%d", pid); chdir(buffer); - mounts = file_to_vector("mounts"); // Unmount dummy skeletons and /sbin links - for (auto &s : mounts) { + file_readline("mounts", [&](string_view &s) -> bool { if (str_contains(s, "tmpfs /system/") || str_contains(s, "tmpfs /vendor/") || str_contains(s, "tmpfs /sbin")) { - sscanf(s.c_str(), "%*s %4096s", buffer); + sscanf(s.data(), "%*s %4096s", buffer); lazy_unmount(buffer); } - } - - // Re-read mount infos - mounts = file_to_vector("mounts"); + return true; + }); // Unmount everything under /system, /vendor, and data mounts - for (auto &s : mounts) { + file_readline("mounts", [&](string_view &s) -> bool { if ((str_contains(s, " /system/") || str_contains(s, " /vendor/")) && - (str_contains(s, system_block) || str_contains(s, vendor_block) || str_contains(s, data_block))) { - sscanf(s.c_str(), "%*s %4096s", buffer); + (str_contains(s, system_block) || str_contains(s, vendor_block) || + str_contains(s, data_block))) { + sscanf(s.data(), "%*s %4096s", buffer); lazy_unmount(buffer); } - } + return true; + }); exit: // Send resume signal diff --git a/native/jni/utils/file.cpp b/native/jni/utils/file.cpp index 0f470d875..00bd5a2d6 100644 --- a/native/jni/utils/file.cpp +++ b/native/jni/utils/file.cpp @@ -384,25 +384,26 @@ void write_zero(int fd, size_t size) { lseek(fd, pos + size, SEEK_SET); } -vector file_to_vector(const char *filename) { - vector arr; - if (access(filename, R_OK) != 0) - return arr; - char *line = nullptr; - size_t len = 0; - ssize_t read; - +void file_readline(const char *filename, const function &fn, bool trim) { FILE *fp = xfopen(filename, "re"); if (fp == nullptr) - return arr; - - while ((read = getline(&line, &len, fp)) != -1) { - // Remove end newline - if (line[read - 1] == '\n') - line[read - 1] = '\0'; - arr.emplace_back(line); + return; + size_t len = 1024; + char *buf = (char *) malloc(len); + char *start; + ssize_t read; + while ((read = getline(&buf, &len, fp)) >= 0) { + start = buf; + if (trim) { + while (buf[read - 1] == '\n' || buf[read - 1] == ' ') + buf[read-- - 1] = '\0'; + while (*start == ' ') + ++start; + } + string_view s(start); + if (!fn(s)) + break; } fclose(fp); - free(line); - return arr; + free(buf); } diff --git a/native/jni/utils/include/utils.h b/native/jni/utils/include/utils.h index 181f51022..d03bb3573 100644 --- a/native/jni/utils/include/utils.h +++ b/native/jni/utils/include/utils.h @@ -140,13 +140,15 @@ void write_zero(int fd, size_t size); #include #include +#include +#include #define str_contains(s, ss) ((ss) != nullptr && (s).find(ss) != std::string::npos) #define str_starts(s, ss) ((ss) != nullptr && (s).compare(0, strlen(ss), ss) == 0) // file.cpp -std::vector file_to_vector(const char *filename); +void file_readline(const char *filename, const std::function &fn, bool trim = false); // misc.cpp