Move module list into MagiskD

This commit is contained in:
topjohnwu
2025-01-26 19:41:34 +08:00
committed by John Wu
parent 4dcd733ddd
commit 0d8d6290a3
9 changed files with 78 additions and 75 deletions
+25 -29
View File
@@ -86,7 +86,7 @@ bool dir_node::prepare() {
return upgrade_to_tmpfs;
}
void dir_node::collect_module_files(const char *module, int dfd) {
void dir_node::collect_module_files(std::string_view module, int dfd) {
auto dir = xopen_dir(xopenat(dfd, name().data(), O_RDONLY | O_CLOEXEC));
if (!dir)
return;
@@ -149,7 +149,9 @@ void module_node::mount() {
VLOGD("delete", "null", node_path().data());
return;
}
std::string path = module + (parent()->root()->prefix + node_path());
std::string path{module.begin(), module.end()};
path += parent()->root()->prefix;
path += node_path();
string mnt_src = module_mnt + path;
{
string src = MODULEROOT "/" + path;
@@ -267,9 +269,7 @@ static void inject_zygisk_libs(root_node *system) {
}
}
vector<module_info> *module_list;
void load_modules() {
static void load_modules(const rust::Vec<ModuleInfo> &module_list) {
node_entry::module_mnt = get_magisk_tmp() + "/"s MODULEMNT "/";
auto root = make_unique<root_node>("");
@@ -278,14 +278,14 @@ void load_modules() {
char buf[4096];
LOGI("* Loading modules\n");
for (const auto &m : *module_list) {
const char *module = m.name.data();
char *b = buf + ssprintf(buf, sizeof(buf), "%s/" MODULEMNT "/%s/", get_magisk_tmp(), module);
for (const auto &m : module_list) {
char *b = buf + ssprintf(buf, sizeof(buf), "%s/" MODULEMNT "/%.*s/",
get_magisk_tmp(), (int) m.name.size(), m.name.data());
// Read props
strcpy(b, "system.prop");
if (access(buf, F_OK) == 0) {
LOGI("%s: loading [system.prop]\n", module);
LOGI("%.*s: loading [system.prop]\n", (int) m.name.size(), m.name.data());
// Do NOT go through property service as it could cause boot lock
load_prop_file(buf, true);
}
@@ -300,10 +300,10 @@ void load_modules() {
if (access(buf, F_OK) != 0)
continue;
LOGI("%s: loading mount files\n", module);
LOGI("%.*s: loading mount files\n", (int) m.name.size(), m.name.data());
b[-1] = '\0';
int fd = xopen(buf, O_RDONLY | O_CLOEXEC);
system->collect_module_files(module, fd);
system->collect_module_files({ m.name.begin(), m.name.end() }, fd);
close(fd);
}
if (get_magisk_tmp() != "/sbin"sv || !str_contains(getenv("PATH") ?: "", "/sbin")) {
@@ -392,8 +392,9 @@ static void foreach_module(Func fn) {
}
}
static void collect_modules(bool open_zygisk) {
foreach_module([=](int dfd, dirent *entry, int modfd) {
static rust::Vec<ModuleInfo> collect_modules(bool open_zygisk) {
rust::Vec<ModuleInfo> modules;
foreach_module([&](int dfd, dirent *entry, int modfd) {
if (faccessat(modfd, "remove", F_OK, 0) == 0) {
LOGI("%s: remove\n", entry->d_name);
auto uninstaller = MODULEROOT + "/"s + entry->d_name + "/uninstall.sh";
@@ -407,7 +408,7 @@ static void collect_modules(bool open_zygisk) {
if (faccessat(modfd, "disable", F_OK, 0) == 0)
return;
module_info info;
ModuleInfo info{};
if (zygisk_enabled) {
// Riru and its modules are not compatible with zygisk
if (entry->d_name == "riru-core"sv || faccessat(modfd, "riru", F_OK, 0) == 0) {
@@ -417,11 +418,13 @@ static void collect_modules(bool open_zygisk) {
if (open_zygisk) {
#if defined(__arm__)
info.z32 = openat(modfd, "zygisk/armeabi-v7a.so", O_RDONLY | O_CLOEXEC);
info.z64 = -1;
#elif defined(__aarch64__)
info.z32 = openat(modfd, "zygisk/armeabi-v7a.so", O_RDONLY | O_CLOEXEC);
info.z64 = openat(modfd, "zygisk/arm64-v8a.so", O_RDONLY | O_CLOEXEC);
#elif defined(__i386__)
info.z32 = openat(modfd, "zygisk/x86.so", O_RDONLY | O_CLOEXEC);
info.z64 = -1;
#elif defined(__x86_64__)
info.z32 = openat(modfd, "zygisk/x86.so", O_RDONLY | O_CLOEXEC);
info.z64 = openat(modfd, "zygisk/x86_64.so", O_RDONLY | O_CLOEXEC);
@@ -441,7 +444,7 @@ static void collect_modules(bool open_zygisk) {
}
}
info.name = entry->d_name;
module_list->push_back(info);
modules.push_back(std::move(info));
});
if (zygisk_enabled) {
bool use_memfd = true;
@@ -461,23 +464,23 @@ static void collect_modules(bool open_zygisk) {
}
return fd;
};
std::for_each(module_list->begin(), module_list->end(), [&](module_info &info) {
std::for_each(modules.begin(),modules.end(), [&](ModuleInfo &info) {
info.z32 = convert_to_memfd(info.z32);
#if defined(__LP64__)
info.z64 = convert_to_memfd(info.z64);
#endif
});
}
return modules;
}
void handle_modules() {
void MagiskD::handle_modules() const noexcept {
prepare_modules();
collect_modules(false);
exec_module_scripts("post-fs-data");
exec_module_scripts("post-fs-data", collect_modules(false));
// Recollect modules (module scripts could remove itself)
module_list->clear();
collect_modules(true);
auto list = collect_modules(true);
load_modules(list);
set_module_list(std::move(list));
}
static int check_rules_dir(char *buf, size_t sz) {
@@ -517,10 +520,3 @@ void remove_modules() {
});
rm_rf(MODULEROOT);
}
void exec_module_scripts(const char *stage) {
vector<string_view> module_names;
std::transform(module_list->begin(), module_list->end(), std::back_inserter(module_names),
[](const module_info &info) -> string_view { return info.name; });
exec_module_scripts(stage, module_names);
}