Get API level from build.prop

This commit is contained in:
topjohnwu 2019-01-20 15:20:34 -05:00
parent a4f8bd4ee0
commit fdf167db11
4 changed files with 41 additions and 26 deletions

View File

@ -32,6 +32,7 @@ static vector<string> module_list;
static bool seperate_vendor; static bool seperate_vendor;
char *system_block, *vendor_block, *magiskloop; char *system_block, *vendor_block, *magiskloop;
int SDK_INT = -1;
static int bind_mount(const char *from, const char *to); static int bind_mount(const char *from, const char *to);
extern void auto_start_magiskhide(); extern void auto_start_magiskhide();
@ -436,19 +437,25 @@ static bool magisk_env() {
xmkdir(SECURE_DIR "/post-fs-data.d", 0755); xmkdir(SECURE_DIR "/post-fs-data.d", 0755);
xmkdir(SECURE_DIR "/service.d", 0755); xmkdir(SECURE_DIR "/service.d", 0755);
auto sdk_prop = getprop("ro.build.version.sdk"); parse_prop_file("/system/build.prop", [](auto key, auto val) -> bool {
int sdk = sdk_prop.empty() ? -1 : atoi(sdk_prop.c_str()); 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"); LOGI("* Mounting mirrors");
auto mounts = file_to_vector("/proc/mounts"); auto mounts = file_to_vector("/proc/mounts");
bool system_as_root = false; bool system_as_root = false;
for (auto &line : mounts) { for (auto &line : mounts) {
if (line.find(" /system_root ") != string::npos) { if (str_contains(line, " /system_root ")) {
bind_mount("/system_root/system", MIRRDIR "/system"); bind_mount("/system_root/system", MIRRDIR "/system");
sscanf(line.c_str(), "%s", buf); sscanf(line.c_str(), "%s", buf);
system_block = strdup2(buf); system_block = strdup2(buf);
system_as_root = true; 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); sscanf(line.c_str(), "%s %*s %s", buf, buf2);
system_block = strdup2(buf); system_block = strdup2(buf);
xmount(system_block, MIRRDIR "/system", buf2, MS_RDONLY, nullptr); xmount(system_block, MIRRDIR "/system", buf2, MS_RDONLY, nullptr);
@ -457,7 +464,7 @@ static bool magisk_env() {
#else #else
LOGI("mount: %s\n", MIRRDIR "/system"); LOGI("mount: %s\n", MIRRDIR "/system");
#endif #endif
} else if (line.find(" /vendor ") != string::npos) { } else if (str_contains(line, " /vendor ")) {
seperate_vendor = true; seperate_vendor = true;
sscanf(line.c_str(), "%s %*s %s", buf, buf2); sscanf(line.c_str(), "%s %*s %s", buf, buf2);
vendor_block = strdup2(buf); vendor_block = strdup2(buf);
@ -468,9 +475,8 @@ static bool magisk_env() {
#else #else
LOGI("mount: %s\n", MIRRDIR "/vendor"); LOGI("mount: %s\n", MIRRDIR "/vendor");
#endif #endif
} else if (sdk >= 24 && } else if (SDK_INT >= 24 &&
line.find(" /proc ") != string::npos && str_contains(line, " /proc ") && !str_contains(line, "hidepid=2")) {
line.find("hidepid=2") == string::npos) {
// Enforce hidepid // Enforce hidepid
xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009"); 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); snprintf(buf, PATH_MAX, "%s/%s/system.prop", MOUNTPOINT, module);
if (access(buf, F_OK) == 0) { if (access(buf, F_OK) == 0) {
LOGI("%s: loading [system.prop]\n", module); LOGI("%s: loading [system.prop]\n", module);
load_prop_file(buf, 0); load_prop_file(buf, false);
} }
// Check whether enable auto_mount // Check whether enable auto_mount
snprintf(buf, PATH_MAX, "%s/%s/auto_mount", MOUNTPOINT, module); snprintf(buf, PATH_MAX, "%s/%s/auto_mount", MOUNTPOINT, module);

View File

@ -39,6 +39,7 @@
#define MAGISKHIDE_PROP "persist.magisk.hide" #define MAGISKHIDE_PROP "persist.magisk.hide"
extern char *argv0; /* For changing process name */ extern char *argv0; /* For changing process name */
extern int SDK_INT;
#define applet_names ((const char *[]) { "magisk", "su", "resetprop", "magiskhide", "imgtool", nullptr }) #define applet_names ((const char *[]) { "magisk", "su", "resetprop", "magiskhide", "imgtool", nullptr })
#define init_applet ((const char *[]) { "magiskpolicy", "supolicy", nullptr }) #define init_applet ((const char *[]) { "magiskpolicy", "supolicy", nullptr })

View File

@ -4,10 +4,12 @@
#pragma once #pragma once
#include <string> #include <string>
#include <functional>
int prop_exist(const char *name); int prop_exist(const char *name);
int setprop(const char *name, const char *value, bool trigger = true); int setprop(const char *name, const char *value, bool trigger = true);
std::string getprop(const char *name, bool persist = false); std::string getprop(const char *name, bool persist = false);
void getprop(void (*callback)(const char *, const char *, void *), void *cookie, 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 deleteprop(const char *name, bool persist = false);
int parse_prop_file(const char *filename, const std::function<bool (const char *, const char *)> &cb);
int load_prop_file(const char *filename, bool trigger = true); int load_prop_file(const char *filename, bool trigger = true);

View File

@ -206,47 +206,53 @@ int deleteprop(const char *name, bool persist) {
return __system_property_del(name) && !(persist && strncmp(name, "persist.", 8) == 0); return __system_property_del(name) && !(persist && strncmp(name, "persist.", 8) == 0);
} }
int load_prop_file(const char *filename, bool trigger) { int parse_prop_file(const char *filename, const function<bool (const char *, const char *)> &cb) {
if (init_resetprop()) return -1; LOGD("resetprop: Parse prop file [%s]\n", filename);
LOGD("resetprop: Load prop file [%s]\n", filename);
FILE *fp = xfopen(filename, "re"); FILE *fp = xfopen(filename, "re");
if (fp == nullptr) { if (fp == nullptr) {
LOGE("Cannot open [%s]\n", filename); LOGE("Cannot open [%s]\n", filename);
return 1; return 1;
} }
char *line = nullptr, *pch; char *line = nullptr, *eql;
size_t len; size_t len;
ssize_t read; ssize_t read;
int comment = 0, i; int i;
while ((read = getline(&line, &len, fp)) != -1) { while ((read = getline(&line, &len, fp)) != -1) {
// Remove the trailing newline // Remove the trailing newline
if (line[read - 1] == '\n') { if (line[read - 1] == '\n') {
line[read - 1] = '\0'; line[--read] = '\0';
--read;
} }
comment = 0; bool comment = false;
for (i = 0; i < read; ++i) { for (i = 0; i < read; ++i) {
// Ignore starting spaces // Ignore starting spaces
if (line[i] == ' ') continue; if (line[i] == ' ') continue;
else {
// A line starting with # is ignored // A line starting with # is ignored
if (line[i] == '#') comment = 1; if (line[i] == '#') comment = true;
break; break;
} }
}
if (comment) continue; if (comment) continue;
pch = strchr(line, '='); eql = strchr(line, '=');
// Ignore invalid formats // 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 // Separate the string
*pch = '\0'; *eql = '\0';
setprop(line + i, pch + 1, trigger); if (!cb(line + i, eql + 1))
break;
} }
free(line); free(line);
fclose(fp); fclose(fp);
return 0; 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[]) { int resetprop_main(int argc, char *argv[]) {
log_cb.d = [](auto fmt, auto ap) -> int { return verbose ? vfprintf(stderr, fmt, ap) : 0; }; log_cb.d = [](auto fmt, auto ap) -> int { return verbose ? vfprintf(stderr, fmt, ap) : 0; };