Rename rules to preinit

It is possible that we will allow more preinit files for modules.
Rename the partition and folders from rules to preinit.
This commit is contained in:
topjohnwu
2023-03-16 04:07:00 -07:00
committed by John Wu
parent 7048aa1014
commit 4e2b88b3d0
18 changed files with 110 additions and 118 deletions

View File

@@ -60,20 +60,20 @@ static void mount_mirrors() {
restorecon();
}
// check and mount rules
if (struct stat st{}; stat((MAGISKTMP + "/" BLOCKDIR "/rules").data(), &st) == 0 && (st.st_mode & S_IFBLK)) {
dev_t rules_dev = st.st_rdev;
// Check and mount preinit mirror
if (struct stat st{}; stat((MAGISKTMP + "/" PREINITDEV).data(), &st) == 0 && (st.st_mode & S_IFBLK)) {
dev_t preinit_dev = st.st_rdev;
for (const auto &info: self_mount_info) {
if (info.root == "/" && info.device == rules_dev) {
if (info.root == "/" && info.device == preinit_dev) {
auto flags = split_ro(info.fs_option, ",");
auto rw = std::any_of(flags.begin(), flags.end(), [](const auto &flag) {
return flag == "rw"sv;
});
if (!rw) continue;
string custom_rules_dir = find_rules_dir(info.target.data());
xmkdir(custom_rules_dir.data(), 0700);
auto rules_dir = MAGISKTMP + "/" RULESDIR;
mount_mirror(custom_rules_dir, rules_dir);
string preinit_dir = resolve_preinit_dir(info.target.data());
xmkdir(preinit_dir.data(), 0700);
auto mirror_dir = MAGISKTMP + "/" PREINITMIRR;
mount_mirror(preinit_dir, mirror_dir);
break;
}
}
@@ -104,7 +104,7 @@ static void mount_mirrors() {
}
}
dev_t find_rules_device() {
dev_t find_preinit_device() {
const int UNKNOWN = 0;
const int PERSIST = 1;
const int METADATA = 2;
@@ -113,12 +113,12 @@ dev_t find_rules_device() {
int matched = UNKNOWN;
dev_t rules_dev = 0;
bool encrypted = getprop("ro.crypto.state") == "encrypted";
string custom_rules_dir;
string preinit_dir;
bool mount = getuid() == 0 && getenv("MAGISKTMP");
for (const auto &info: parse_mount_info("self")) {
if (info.target.ends_with(RULESDIR))
if (info.target.ends_with(PREINITMIRR))
return info.device;
if (info.root != "/" || info.source.find("/dm-") != string::npos)
continue;
@@ -146,17 +146,17 @@ dev_t find_rules_device() {
} else continue;
if (mount) {
custom_rules_dir = find_rules_dir(info.target.data());
preinit_dir = resolve_preinit_dir(info.target.data());
}
rules_dev = info.device;
matched = new_matched;
}
if (!custom_rules_dir.empty()) {
auto rules_dir = getenv("MAGISKTMP") + "/rules"s;
mkdirs(custom_rules_dir.data(), 0700);
mkdirs(rules_dir.data(), 0700);
xmount(custom_rules_dir.data(), rules_dir.data(), nullptr, MS_BIND, nullptr);
if (!preinit_dir.empty()) {
auto mirror_dir = string(getenv("MAGISKTMP")) + "/" PREINITMIRR;
mkdirs(preinit_dir.data(), 0700);
mkdirs(mirror_dir.data(), 0700);
xmount(preinit_dir.data(), mirror_dir.data(), nullptr, MS_BIND, nullptr);
}
return rules_dev;

View File

@@ -6,7 +6,7 @@
extern bool RECOVERY_MODE;
extern std::atomic<ino_t> pkg_xml_ino;
dev_t find_rules_device();
dev_t find_preinit_device();
void unlock_blocks();
void reboot();
void start_log_daemon();

View File

@@ -122,7 +122,6 @@ int magisk_main(int argc, char *argv[]) {
do_reboot = 1;
} else {
usage();
exit(1);
}
int fd = connect_daemon(MainRequest::REMOVE_MODULES);
write_int(fd, do_reboot);
@@ -134,8 +133,8 @@ int magisk_main(int argc, char *argv[]) {
return 0;
} else if (argc >= 3 && argv[1] == "--install-module"sv) {
install_module(argv[2]);
} else if (argv[1] == "--rules-device"sv) {
auto dev = find_rules_device();
} else if (argv[1] == "--preinit-device"sv) {
auto dev = find_preinit_device();
if (dev) printf("%u:%u\n", major(dev), minor(dev));
return dev ? 0 : 1;
}

View File

@@ -437,7 +437,7 @@ void handle_modules() {
}
static int check_rules_dir(char *buf, size_t sz) {
int off = ssprintf(buf, sz, "%s/%s", MAGISKTMP.data(), RULESDIR);
int off = ssprintf(buf, sz, "%s/%s", MAGISKTMP.data(), PREINITMIRR);
struct stat st1{};
struct stat st2{};
if (xstat(buf, &st1) < 0 || xstat(MODULEROOT, &st2) < 0)