Use a better function to read through files

This commit is contained in:
topjohnwu 2019-02-17 22:30:23 -05:00
parent fb55fe184c
commit 14aa6041ec
5 changed files with 49 additions and 51 deletions

View File

@ -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()) {

View File

@ -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);
}

View File

@ -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<string> 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

View File

@ -384,25 +384,26 @@ void write_zero(int fd, size_t size) {
lseek(fd, pos + size, SEEK_SET);
}
vector<string> file_to_vector(const char *filename) {
vector<string> 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<bool (string_view&)> &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);
}

View File

@ -140,13 +140,15 @@ void write_zero(int fd, size_t size);
#include <string>
#include <vector>
#include <functional>
#include <string_view>
#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<std::string> file_to_vector(const char *filename);
void file_readline(const char *filename, const std::function<bool (std::string_view&)> &fn, bool trim = false);
// misc.cpp