Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
HuskyDG committed Dec 28, 2023
1 parent 06ce616 commit 3d98ed6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export MAGISKTMP="$(magisk --path)"

## Donate

- I am having financial difficulties. If you like my work, you can donate at http://paypal.me/huskydg
- If you like my work, you can donate at http://paypal.me/huskydg

## Source code

Expand Down
1 change: 0 additions & 1 deletion magisk-module/customize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ if [ "$KSU" == true ] && [ "$KSU_VER_CODE" -ge 11210 ]; then
mv -f "$MODPATH/post-fs-data.sh" "$MODPATH/post-mount.sh"
fi

ui_print "- I am having financial difficulties"
ui_print "- If you like my work, you can donate at http://paypal.me/huskydg"

ui_print
70 changes: 25 additions & 45 deletions native/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,29 @@ const char *overlay_name = "overlay";
static std::vector<mount_info> collect_mounts() {
// sort mountinfo, skip unnecessary mounts
std::vector<mount_info> mountinfo;
do {
auto current_mount_info = parse_mount_info("self");
std::reverse(current_mount_info.begin(), current_mount_info.end());
for (auto &info : current_mount_info) {
for (auto &part : SYSTEM_PARTITIONS) {
if (starts_with(info.target.data(), string(part + "/").data()) || info.target == part) {
for (auto &s : reversed(mountinfo)) {
// /a/b/c <--- under a (skip)
// /a
// /a/b
if (s.target == info.target || starts_with(info.target.data(), string(s.target + "/").data())) {
LOGD("mountinfo: mountpoint %s is hidden under %s\n", info.target.data(), s.target.data());
goto next_mountpoint;
}
auto current_mount_info = parse_mount_info("self");
std::reverse(current_mount_info.begin(), current_mount_info.end());
for (auto &info : current_mount_info) {
for (auto &part : SYSTEM_PARTITIONS) {
if (starts_with(info.target.data(), string(part + "/").data()) || info.target == part) {
for (auto &s : reversed(mountinfo)) {
// /a/b/c <--- under a (skip)
// /a
// /a/b
if (s.target == info.target || starts_with(info.target.data(), string(s.target + "/").data())) {
LOGD("mountinfo: mountpoint %s is hidden under %s\n", info.target.data(), s.target.data());
goto next_mountpoint;
}
LOGD("mountinfo: device (%u:%u)%s on %s type %s (%s)\n", major(info.device), minor(info.device),
(info.root != "/")? info.root.data() : "", info.target.data(), info.type.data(), info.fs_option.data());
mountinfo.emplace_back(info);
break;
}
LOGD("mountinfo: device (%u:%u)%s on %s type %s (%s)\n", major(info.device), minor(info.device),
(info.root != "/")? info.root.data() : "", info.target.data(), info.type.data(), info.fs_option.data());
mountinfo.emplace_back(info);
break;
}
next_mountpoint:
continue;
}
} while(false);
next_mountpoint:
continue;
}
return mountinfo;
}

Expand Down Expand Up @@ -103,18 +101,6 @@ static int unmount_ksu_overlay() {
return 0;
}

static bool unshare_mount(const char *mnt_point) {
int fd = open(mnt_point, O_PATH);
if (fd < 0)
return false;
string fd_path = "/proc/self/fd/";
fd_path += std::to_string(fd);
bool ret = !mount("", fd_path.data(), nullptr, MS_PRIVATE | MS_REC, nullptr) &&
!mount("", fd_path.data(), nullptr, MS_SHARED | MS_REC, nullptr);
close(fd);
return ret;
}

static std::string get_lowerdirs(std::vector<std::string> list, const char *sub) {
std::string lowerdir = "";
for (auto it = list.begin(); it != list.end(); it++) {
Expand Down Expand Up @@ -361,13 +347,13 @@ int main(int argc, const char **argv) {
std::string upperdir = upper + info;
std::string workerdir = worker + "/" + std::to_string(st.st_dev) + "/" + std::to_string(st.st_ino);
std::string masterdir = overlay_tmpdir + "/master" + info;
bool module_node_is_dir = is_dir(masterdir.data());
bool module_node_exist = fexist(masterdir.data());
bool upper_node_is_dir = is_dir(upperdir.data());
bool upper_node_exist = fexist(upperdir.data());
#define module_node_is_dir (is_dir(masterdir.data()))
#define module_node_exist (fexist(masterdir.data()))
#define upper_node_is_dir (is_dir(upperdir.data()))
#define upper_node_exist (fexist(upperdir.data()))
if (lstat(tmp_mount.data(), &st) != 0 || // target does not exist, it could be deleted by modules
(((upper_node_exist && !upper_node_is_dir) ||
(!upper_node_exist && module_node_exist && !module_node_is_dir)) && S_ISDIR(st.st_mode))) // module add file but original is folder
(((upper_node_exist && !upper_node_is_dir) || // in upperdir, it is file
(!upper_node_exist && module_node_exist && !module_node_is_dir)) && S_ISDIR(st.st_mode))) // module node is file but in lowerdir it is folder
continue;
for (auto &s : mount_list) {
// only care about mountpoint under overlayfs mounted subdirectories
Expand Down Expand Up @@ -495,8 +481,6 @@ int main(int argc, const char **argv) {
mounted.clear();
goto subtree_mounts; // fall back to mount subtree
}
if (!unshare_mount(info.data()))
LOGE("unshare mount failed: [%s]\n", info.data());
mounted.emplace_back(info);
}
goto inject_mirrors;
Expand All @@ -518,8 +502,6 @@ int main(int argc, const char **argv) {
LOGE("mount failed, skip!\n");
continue;
}
if (!unshare_mount(buf + strlen(overlay_tmpdir.data())))
LOGE("unshare mount failed: [%s]\n", buf + strlen(overlay_tmpdir.data()));
mounted.emplace_back(buf + strlen(overlay_tmpdir.data()));
}
closedir(dirfp);
Expand All @@ -533,8 +515,6 @@ int main(int argc, const char **argv) {
std::string mirror_dir = string(mirrors) + info;
if (access(mirror_dir.data(), F_OK) == 0) {
xmount(info.data(), mirror_dir.data(), nullptr, MS_BIND | MS_REC, nullptr);
if (!unshare_mount(mirror_dir.data()))
LOGE("unshare mount failed: [%s]\n", mirror_dir.data());
}
}
}
Expand Down

0 comments on commit 3d98ed6

Please sign in to comment.