From fdf167db11ad2a69509981402fd8d62f86634082 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 20 Jan 2019 15:20:34 -0500 Subject: [PATCH] Get API level from build.prop --- native/jni/daemon/bootstages.cpp | 24 +++++++++++------- native/jni/include/magisk.h | 1 + native/jni/include/resetprop.h | 2 ++ native/jni/resetprop/resetprop.cpp | 40 +++++++++++++++++------------- 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/native/jni/daemon/bootstages.cpp b/native/jni/daemon/bootstages.cpp index 530d5b697..010bc6eeb 100644 --- a/native/jni/daemon/bootstages.cpp +++ b/native/jni/daemon/bootstages.cpp @@ -32,6 +32,7 @@ static vector module_list; static bool seperate_vendor; char *system_block, *vendor_block, *magiskloop; +int SDK_INT = -1; static int bind_mount(const char *from, const char *to); extern void auto_start_magiskhide(); @@ -436,19 +437,25 @@ static bool magisk_env() { xmkdir(SECURE_DIR "/post-fs-data.d", 0755); xmkdir(SECURE_DIR "/service.d", 0755); - auto sdk_prop = getprop("ro.build.version.sdk"); - int sdk = sdk_prop.empty() ? -1 : atoi(sdk_prop.c_str()); + parse_prop_file("/system/build.prop", [](auto key, auto val) -> bool { + if (strcmp(key, "ro.build.version.sdk") == 0) { + LOGI("* Device API level: %s\n", val); + SDK_INT = atoi(val); + return false; + } + return true; + }); LOGI("* Mounting mirrors"); auto mounts = file_to_vector("/proc/mounts"); bool system_as_root = false; for (auto &line : mounts) { - if (line.find(" /system_root ") != string::npos) { + if (str_contains(line, " /system_root ")) { bind_mount("/system_root/system", MIRRDIR "/system"); sscanf(line.c_str(), "%s", buf); system_block = strdup2(buf); system_as_root = true; - } else if (!system_as_root && line.find(" /system ") != string::npos) { + } else if (!system_as_root && str_contains(line, " /system ")) { sscanf(line.c_str(), "%s %*s %s", buf, buf2); system_block = strdup2(buf); xmount(system_block, MIRRDIR "/system", buf2, MS_RDONLY, nullptr); @@ -457,7 +464,7 @@ static bool magisk_env() { #else LOGI("mount: %s\n", MIRRDIR "/system"); #endif - } else if (line.find(" /vendor ") != string::npos) { + } else if (str_contains(line, " /vendor ")) { seperate_vendor = true; sscanf(line.c_str(), "%s %*s %s", buf, buf2); vendor_block = strdup2(buf); @@ -468,9 +475,8 @@ static bool magisk_env() { #else LOGI("mount: %s\n", MIRRDIR "/vendor"); #endif - } else if (sdk >= 24 && - line.find(" /proc ") != string::npos && - line.find("hidepid=2") == string::npos) { + } 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"); } @@ -835,7 +841,7 @@ void post_fs_data(int client) { snprintf(buf, PATH_MAX, "%s/%s/system.prop", MOUNTPOINT, module); if (access(buf, F_OK) == 0) { LOGI("%s: loading [system.prop]\n", module); - load_prop_file(buf, 0); + load_prop_file(buf, false); } // Check whether enable auto_mount snprintf(buf, PATH_MAX, "%s/%s/auto_mount", MOUNTPOINT, module); diff --git a/native/jni/include/magisk.h b/native/jni/include/magisk.h index 2ed55913c..78f489acd 100644 --- a/native/jni/include/magisk.h +++ b/native/jni/include/magisk.h @@ -39,6 +39,7 @@ #define MAGISKHIDE_PROP "persist.magisk.hide" extern char *argv0; /* For changing process name */ +extern int SDK_INT; #define applet_names ((const char *[]) { "magisk", "su", "resetprop", "magiskhide", "imgtool", nullptr }) #define init_applet ((const char *[]) { "magiskpolicy", "supolicy", nullptr }) diff --git a/native/jni/include/resetprop.h b/native/jni/include/resetprop.h index e61ec2f2e..b9cc9d340 100644 --- a/native/jni/include/resetprop.h +++ b/native/jni/include/resetprop.h @@ -4,10 +4,12 @@ #pragma once #include +#include int prop_exist(const char *name); int setprop(const char *name, const char *value, bool trigger = true); std::string getprop(const char *name, bool persist = false); void getprop(void (*callback)(const char *, const char *, void *), void *cookie, bool persist = false); int deleteprop(const char *name, bool persist = false); +int parse_prop_file(const char *filename, const std::function &cb); int load_prop_file(const char *filename, bool trigger = true); diff --git a/native/jni/resetprop/resetprop.cpp b/native/jni/resetprop/resetprop.cpp index 80a21d8f7..92bec3780 100644 --- a/native/jni/resetprop/resetprop.cpp +++ b/native/jni/resetprop/resetprop.cpp @@ -206,47 +206,53 @@ int deleteprop(const char *name, bool persist) { return __system_property_del(name) && !(persist && strncmp(name, "persist.", 8) == 0); } -int load_prop_file(const char *filename, bool trigger) { - if (init_resetprop()) return -1; - LOGD("resetprop: Load prop file [%s]\n", filename); +int parse_prop_file(const char *filename, const function &cb) { + LOGD("resetprop: Parse prop file [%s]\n", filename); FILE *fp = xfopen(filename, "re"); if (fp == nullptr) { LOGE("Cannot open [%s]\n", filename); return 1; } - char *line = nullptr, *pch; + char *line = nullptr, *eql; size_t len; ssize_t read; - int comment = 0, i; + int i; while ((read = getline(&line, &len, fp)) != -1) { // Remove the trailing newline if (line[read - 1] == '\n') { - line[read - 1] = '\0'; - --read; + line[--read] = '\0'; } - comment = 0; + bool comment = false; for (i = 0; i < read; ++i) { // Ignore starting spaces if (line[i] == ' ') continue; - else { - // A line starting with # is ignored - if (line[i] == '#') comment = 1; - break; - } + // A line starting with # is ignored + if (line[i] == '#') comment = true; + break; } if (comment) continue; - pch = strchr(line, '='); + eql = strchr(line, '='); // Ignore invalid formats - if ( ((pch == nullptr) || (i >= (pch - line))) || (pch >= line + read - 1) ) continue; + if ( ((eql == nullptr) || (i >= (eql - line))) || (eql >= line + read - 1) ) + continue; // Separate the string - *pch = '\0'; - setprop(line + i, pch + 1, trigger); + *eql = '\0'; + if (!cb(line + i, eql + 1)) + break; } free(line); fclose(fp); return 0; } +int load_prop_file(const char *filename, bool trigger) { + if (init_resetprop()) return -1; + return parse_prop_file(filename, [=](auto key, auto val) -> bool { + setprop(key, val, trigger); + return true; + }); +} + int resetprop_main(int argc, char *argv[]) { log_cb.d = [](auto fmt, auto ap) -> int { return verbose ? vfprintf(stderr, fmt, ap) : 0; };