Migrate to STL

This commit is contained in:
topjohnwu
2019-01-19 23:59:37 -05:00
parent 03c39e692a
commit 3e4c12cf56
23 changed files with 226 additions and 548 deletions
+46 -39
View File
@@ -13,6 +13,8 @@
#include <dirent.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <vector>
#include <string>
#include "magisk.h"
#include "db.h"
@@ -23,8 +25,10 @@
#include "selinux.h"
#include "flags.h"
using namespace std;
static char buf[PATH_MAX], buf2[PATH_MAX];
static Vector<CharArray> module_list;
static vector<string> module_list;
static bool seperate_vendor;
char *system_block, *vendor_block, *magiskloop;
@@ -42,9 +46,9 @@ extern void auto_start_magiskhide();
#define IS_SKEL 0x04 /* mount from skeleton */
#define IS_MODULE 0x08 /* mount from module */
#define IS_DIR(n) (n->type == DT_DIR)
#define IS_LNK(n) (n->type == DT_LNK)
#define IS_REG(n) (n->type == DT_REG)
#define IS_DIR(n) ((n)->type == DT_DIR)
#define IS_LNK(n) ((n)->type == DT_LNK)
#define IS_REG(n) ((n)->type == DT_REG)
class node_entry {
public:
@@ -56,15 +60,15 @@ public:
private:
const char *module; /* Only used when status & IS_MODULE */
const CharArray name;
const string name;
uint8_t type;
uint8_t status;
node_entry *parent;
Vector<node_entry *> children;
vector<node_entry *> children;
node_entry(const char *, const char *, uint8_t type);
bool is_root();
CharArray get_path();
string get_path();
node_entry *insert(node_entry *);
void clone_skeleton();
int get_path(char *path);
@@ -87,7 +91,7 @@ bool node_entry::is_root() {
return parent == nullptr;
}
CharArray node_entry::get_path() {
string node_entry::get_path() {
get_path(buf);
return buf;
}
@@ -123,7 +127,7 @@ void node_entry::create_module_tree(const char *module) {
DIR *dir;
struct dirent *entry;
CharArray full_path = get_path();
auto full_path = get_path();
snprintf(buf, PATH_MAX, "%s/%s%s", MOUNTPOINT, module, full_path.c_str());
if (!(dir = xopendir(buf)))
@@ -133,7 +137,7 @@ void node_entry::create_module_tree(const char *module) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
// Create new node
node_entry *node = new node_entry(module, entry->d_name, entry->d_type);
auto node = new node_entry(module, entry->d_name, entry->d_type);
snprintf(buf, PATH_MAX, "%s/%s", full_path.c_str(), entry->d_name);
/*
@@ -185,7 +189,7 @@ void node_entry::clone_skeleton() {
struct node_entry *dummy;
// Clone the structure
CharArray full_path = get_path();
auto full_path = get_path();
snprintf(buf, PATH_MAX, "%s%s", MIRRDIR, full_path.c_str());
if (!(dir = xopendir(buf)))
return;
@@ -200,10 +204,10 @@ void node_entry::clone_skeleton() {
if (status & IS_SKEL) {
file_attr attr;
getattr(full_path, &attr);
getattr(full_path.c_str(), &attr);
LOGI("mnt_tmpfs : %s\n", full_path.c_str());
xmount("tmpfs", full_path, "tmpfs", 0, nullptr);
setattr(full_path, &attr);
xmount("tmpfs", full_path.c_str(), "tmpfs", 0, nullptr);
setattr(full_path.c_str(), &attr);
}
for (auto &child : children) {
@@ -254,9 +258,9 @@ void node_entry::clone_skeleton() {
void node_entry::magic_mount() {
if (status & IS_MODULE) {
// Mount module item
CharArray real_path = get_path();
auto real_path = get_path();
snprintf(buf, PATH_MAX, "%s/%s%s", MOUNTPOINT, module, real_path.c_str());
bind_mount(buf, real_path);
bind_mount(buf, real_path.c_str());
} else if (status & IS_SKEL) {
// The node is labeled to be cloned with skeleton, lets do it
clone_skeleton();
@@ -330,7 +334,8 @@ static void exec_common_script(const char* stage) {
}
static void exec_module_script(const char* stage) {
for (const char *module : module_list) {
for (const auto &m : module_list) {
const auto module = m.c_str();
snprintf(buf2, PATH_MAX, "%s/%s/%s.sh", MOUNTPOINT, module, stage);
if (access(buf2, F_OK) == -1)
continue;
@@ -364,7 +369,7 @@ static void simple_mount(const char *path) {
if (access(buf2, F_OK) == -1)
continue;
if (entry->d_type == DT_DIR) {
simple_mount(CharArray(buf2));
simple_mount(string(buf2).c_str());
} else if (entry->d_type == DT_REG) {
// Actual file path
snprintf(buf, PATH_MAX, "%s%s", SIMPLEMOUNT, buf2);
@@ -431,21 +436,20 @@ static bool magisk_env() {
xmkdir(SECURE_DIR "/post-fs-data.d", 0755);
xmkdir(SECURE_DIR "/service.d", 0755);
CharArray sdk_prop = getprop("ro.build.version.sdk");
int sdk = sdk_prop.empty() ? -1 : atoi(sdk_prop);
auto sdk_prop = getprop("ro.build.version.sdk");
int sdk = sdk_prop.empty() ? -1 : atoi(sdk_prop.c_str());
LOGI("* Mounting mirrors");
Vector<CharArray> mounts;
file_to_vector("/proc/mounts", mounts);
auto mounts = file_to_vector("/proc/mounts");
bool system_as_root = false;
for (auto &line : mounts) {
if (line.contains(" /system_root ")) {
if (line.find(" /system_root ") != string::npos) {
bind_mount("/system_root/system", MIRRDIR "/system");
sscanf(line, "%s", buf);
sscanf(line.c_str(), "%s", buf);
system_block = strdup2(buf);
system_as_root = true;
} else if (!system_as_root && line.contains(" /system ")) {
sscanf(line, "%s %*s %s", buf, buf2);
} else if (!system_as_root && line.find(" /system ") != string::npos) {
sscanf(line.c_str(), "%s %*s %s", buf, buf2);
system_block = strdup2(buf);
xmount(system_block, MIRRDIR "/system", buf2, MS_RDONLY, nullptr);
#ifdef MAGISK_DEBUG
@@ -453,9 +457,9 @@ static bool magisk_env() {
#else
LOGI("mount: %s\n", MIRRDIR "/system");
#endif
} else if (line.contains(" /vendor ")) {
} else if (line.find(" /vendor ") != string::npos) {
seperate_vendor = true;
sscanf(line, "%s %*s %s", buf, buf2);
sscanf(line.c_str(), "%s %*s %s", buf, buf2);
vendor_block = strdup2(buf);
xmkdir(MIRRDIR "/vendor", 0755);
xmount(buf, MIRRDIR "/vendor", buf2, MS_RDONLY, nullptr);
@@ -464,7 +468,9 @@ static bool magisk_env() {
#else
LOGI("mount: %s\n", MIRRDIR "/vendor");
#endif
} else if (sdk >= 24 && line.contains(" /proc ") && !line.contains("hidepid=2")) {
} else if (sdk >= 24 &&
line.find(" /proc ") != string::npos &&
line.find("hidepid=2") == string::npos) {
// Enforce hidepid
xmount(nullptr, "/proc", nullptr, MS_REMOUNT, "hidepid=2,gid=3009");
}
@@ -508,7 +514,7 @@ static void collect_modules() {
}
unlink("update");
if (access("disable", F_OK))
module_list.push_back(entry->d_name);
module_list.emplace_back(entry->d_name);
chdir("..");
}
}
@@ -559,7 +565,7 @@ static bool prepare_img() {
static void install_apk(const char *apk) {
setfilecon(apk, "u:object_r:" SEPOL_FILE_DOMAIN ":s0");
while (1) {
while (true) {
sleep(5);
LOGD("apk_install: attempting to install APK\n");
int fd = -1, pid;
@@ -586,14 +592,14 @@ static void install_apk(const char *apk) {
static bool check_data() {
bool mnt = false;
bool data = false;
Vector<CharArray> mounts;
file_to_vector("/proc/mounts", mounts);
auto mounts = file_to_vector("/proc/mounts");
for (auto &line : mounts) {
if (line.contains(" /data ") && !line.contains("tmpfs"))
if (line.find(" /data ") != string::npos &&
line.find("tmpfs") == string::npos)
mnt = true;
}
if (mnt) {
CharArray crypto = getprop("ro.crypto.state");
auto crypto = getprop("ro.crypto.state");
if (!crypto.empty()) {
if (crypto == "unencrypted") {
// Unencrypted, we can directly access data
@@ -811,11 +817,11 @@ void post_fs_data(int client) {
exec_module_script("post-fs-data");
// Recollect modules
module_list.clear(true);
module_list.clear();
collect_modules();
// Create the system root entry
node_entry *sys_root = new node_entry("system", IS_INTER);
auto sys_root = new node_entry("system", IS_INTER);
// Vendor root entry
node_entry *ven_root = nullptr;
@@ -823,7 +829,8 @@ void post_fs_data(int client) {
bool has_modules = false;
LOGI("* Loading modules\n");
for (const char *module : module_list) {
for (const auto &m : module_list) {
const auto module = m.c_str();
// Read props
snprintf(buf, PATH_MAX, "%s/%s/system.prop", MOUNTPOINT, module);
if (access(buf, F_OK) == 0) {
@@ -911,7 +918,7 @@ core_only:
}
// All boot stage done, cleanup
module_list.clear(true);
module_list.clear();
}
void boot_complete(int client) {
+7 -8
View File
@@ -12,14 +12,17 @@
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <vector>
#include "magisk.h"
#include "utils.h"
#include "daemon.h"
#include "flags.h"
using namespace std;
bool log_daemon_started = false;
static Vector<const char *> log_cmd, clear_cmd;
static vector<const char *> log_cmd, clear_cmd;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
enum {
@@ -68,7 +71,7 @@ static void *monitor_thread(void *) {
sleep(5);
int fd;
char b;
while (1) {
while (true) {
fd = connect_daemon();
write_int(fd, HANDSHAKE);
// This should hold unless the daemon is killed
@@ -136,11 +139,7 @@ static void log_daemon() {
}
chmod("/dev/null", 0666);
clear_cmd = log_cmd;
log_cmd.push_back("-v");
log_cmd.push_back("threadtime");
log_cmd.push_back("-s");
log_cmd.push_back("am_proc_start");
log_cmd.push_back("Magisk");
log_cmd.insert(log_cmd.end(), { "-v", "threadtime", "-s", "am_proc_start", "Magisk" });
#ifdef MAGISK_DEBUG
log_cmd.push_back("*:F");
#endif
@@ -163,7 +162,7 @@ static void log_daemon() {
if (xbind(sockfd, (struct sockaddr*) &sun, len))
exit(1);
xlisten(sockfd, 10);
while(1) {
while(true) {
int fd = xaccept4(sockfd, nullptr, nullptr, SOCK_CLOEXEC);
switch(read_int(fd)) {
case HIDE_CONNECT: