diff --git a/native/jni/core/bootstages.cpp b/native/jni/core/bootstages.cpp index 26325b90f..6b9a328c6 100644 --- a/native/jni/core/bootstages.cpp +++ b/native/jni/core/bootstages.cpp @@ -380,7 +380,7 @@ static bool magisk_env() { LOGI("* Mounting mirrors"); bool system_as_root = false; - file_readline("/proc/mounts", [&](string_view &line) -> bool { + file_readline("/proc/mounts", [&](string_view line) -> bool { if (str_contains(line, " /system_root ")) { bind_mount("/system_root/system", MIRRDIR "/system"); sscanf(line.data(), "%s", buf); @@ -494,7 +494,7 @@ static void collect_modules() { static bool check_data() { bool mnt = false; bool data = false; - file_readline("/proc/mounts", [&](string_view &s) -> bool { + file_readline("/proc/mounts", [&](string_view s) -> bool { if (str_contains(s, " /data ") && !str_contains(s, "tmpfs")) mnt = true; return true; diff --git a/native/jni/core/daemon.cpp b/native/jni/core/daemon.cpp index ae444f930..96a610dcb 100644 --- a/native/jni/core/daemon.cpp +++ b/native/jni/core/daemon.cpp @@ -118,9 +118,9 @@ static void main_daemon() { // Get API level 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); + if (key == "ro.build.version.sdk") { + LOGI("* Device API level: %s\n", val.data()); + SDK_INT = atoi(val.data()); return false; } return true; diff --git a/native/jni/include/resetprop.h b/native/jni/include/resetprop.h index b9cc9d340..576e0546a 100644 --- a/native/jni/include/resetprop.h +++ b/native/jni/include/resetprop.h @@ -11,5 +11,4 @@ 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); +void load_prop_file(const char *filename, bool trigger = true); diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index 64e72cece..485d483e5 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -233,7 +233,7 @@ bool init_list() { // Migrate old hide list into database if (access(LEGACY_LIST, R_OK) == 0) { - file_readline(LEGACY_LIST, [](string_view &s) -> bool { + file_readline(LEGACY_LIST, [](string_view s) -> bool { add_list(s.data()); return true; }); diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index 8bc81e248..cfd488711 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -79,7 +79,7 @@ static long xptrace(int request, pid_t pid, void *addr = nullptr, intptr_t data return xptrace(true, request, pid, addr, reinterpret_cast(data)); } -static bool parse_packages_xml(string_view &s) { +static bool parse_packages_xml(string_view s) { if (!str_starts(s, " */ @@ -144,7 +144,7 @@ static void hide_daemon(int pid) { vector targets; // Unmount dummy skeletons and /sbin links - file_readline("/proc/self/mounts", [&](string_view &s) -> bool { + file_readline("/proc/self/mounts", [&](string_view s) -> bool { if (str_contains(s, "tmpfs /system/") || str_contains(s, "tmpfs /vendor/") || str_contains(s, "tmpfs /sbin")) { char *path = (char *) s.data(); @@ -160,7 +160,7 @@ static void hide_daemon(int pid) { targets.clear(); // Unmount everything under /system, /vendor, and data mounts - file_readline("/proc/self/mounts", [&](string_view &s) -> bool { + file_readline("/proc/self/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))) { diff --git a/native/jni/resetprop/resetprop.cpp b/native/jni/resetprop/resetprop.cpp index 4c968f7d1..e29b26bbf 100644 --- a/native/jni/resetprop/resetprop.cpp +++ b/native/jni/resetprop/resetprop.cpp @@ -206,49 +206,11 @@ int deleteprop(const char *name, bool persist) { return __system_property_del(name) && !(persist && strncmp(name, "persist.", 8) == 0); } -int parse_prop_file(const char *filename, const function &cb) { +void load_prop_file(const char *filename, bool trigger) { + if (init_resetprop()) return; 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, *eql; - size_t len; - ssize_t read; - int i; - while ((read = getline(&line, &len, fp)) != -1) { - // Remove the trailing newline - if (line[read - 1] == '\n') { - line[--read] = '\0'; - } - bool comment = false; - for (i = 0; i < read; ++i) { - // Ignore starting spaces - if (line[i] == ' ') continue; - // A line starting with # is ignored - if (line[i] == '#') comment = true; - break; - } - if (comment) continue; - eql = strchr(line, '='); - // Ignore invalid formats - if ( ((eql == nullptr) || (i >= (eql - line))) || (eql >= line + read - 1) ) - continue; - // Separate the string - *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); + parse_prop_file(filename, [=](auto key, auto val) -> bool { + setprop(key.data(), val.data(), trigger); return true; }); } @@ -269,7 +231,8 @@ int resetprop_main(int argc, char *argv[]) { switch (argv[0][idx]) { case '-': if (strcmp(argv[0], "--file") == 0 && argc == 2) { - return load_prop_file(argv[1], trigger); + load_prop_file(argv[1], trigger); + return 0; } else if (strcmp(argv[0], "--delete") == 0 && argc == 2) { return deleteprop(argv[1], persist); } else if (strcmp(argv[0], "--help") == 0) { diff --git a/native/jni/utils/file.cpp b/native/jni/utils/file.cpp index abd5a1de3..a37b81dde 100644 --- a/native/jni/utils/file.cpp +++ b/native/jni/utils/file.cpp @@ -367,8 +367,8 @@ void write_zero(int fd, size_t size) { } } -void file_readline(const char *filename, const function &fn, bool trim) { - FILE *fp = xfopen(filename, "re"); +void file_readline(const char *file, const function &fn, bool trim) { + FILE *fp = xfopen(file, "re"); if (fp == nullptr) return; size_t len = 1024; @@ -383,10 +383,22 @@ void file_readline(const char *filename, const function &fn while (*start == ' ') ++start; } - string_view s(start); - if (!fn(s)) + if (!fn(start)) break; } fclose(fp); free(buf); } + +void parse_prop_file(const char *file, const function &fn) { + file_readline(file, [&](string_view line_view) -> bool { + char *line = (char *) line_view.data(); + if (line[0] == '#') + return true; + char *eql = strchr(line, '='); + if (eql == nullptr || eql == line) + return true; + *eql = '\0'; + return fn(line, eql + 1); + }, true); +} diff --git a/native/jni/utils/include/utils.h b/native/jni/utils/include/utils.h index dc551d5d8..6eed44b50 100644 --- a/native/jni/utils/include/utils.h +++ b/native/jni/utils/include/utils.h @@ -172,7 +172,9 @@ private: // file.cpp -void file_readline(const char *filename, const std::function &fn, bool trim = false); +void file_readline(const char *file, const std::function &fn, bool trim = false); +void parse_prop_file(const char *file, const std::function + &fn); void *__mmap(const char *filename, size_t *size, bool rw); template