Unmount everything under ksu loop

This commit is contained in:
Nullptr
2023-02-25 11:00:35 +08:00
parent 6d9cc560cc
commit 9ff1e27a7d

View File

@@ -6,31 +6,36 @@
using namespace std::string_view_literals; using namespace std::string_view_literals;
static void lazy_unmount(const char* mountpoint) { namespace {
if (umount2(mountpoint, MNT_DETACH) != -1) { constexpr auto KSU_MODULE_DIR = "/data/adb/ksu/modules"sv;
LOGD("Unmounted (%s)", mountpoint);
} else { void lazy_unmount(const char* mountpoint) {
LOGW("Failed to unmount: %s (%s)", strerror(errno), mountpoint); if (umount2(mountpoint, MNT_DETACH) != -1) {
LOGD("Unmounted (%s)", mountpoint);
} else {
LOGW("Failed to unmount: %s (%s)", strerror(errno), mountpoint);
}
} }
} }
#define PARSE_OPT(name, flag) \ #define PARSE_OPT(name, flag) \
if (opt == name) { \ if (opt == (name)) { \
flags |= (flag); \ flags |= (flag); \
return true; \ return true; \
} }
void revert_unmount() { void revert_unmount() {
std::string ksu_loop;
std::vector<std::string> targets; std::vector<std::string> targets;
std::list<std::pair<std::string, std::string>> backups; std::list<std::pair<std::string, std::string>> backups;
targets.emplace_back("/data/adb/ksu/modules"); targets.emplace_back(KSU_MODULE_DIR);
parse_mnt("/proc/self/mounts", [&](mntent* mentry) { parse_mnt("/proc/self/mounts", [&](mntent* mentry) {
if (str_starts(mentry->mnt_fsname, "/data/adb/")) { if (mentry->mnt_dir == KSU_MODULE_DIR) {
targets.emplace_back(mentry->mnt_dir); ksu_loop = mentry->mnt_fsname;
} }
if (mentry->mnt_type == "overlay"sv) { if (mentry->mnt_type == "overlay"sv) {
if (str_contains(mentry->mnt_opts, "/data/adb/ksu/modules")) { if (str_contains(mentry->mnt_opts, KSU_MODULE_DIR)) {
targets.emplace_back(mentry->mnt_dir); targets.emplace_back(mentry->mnt_dir);
} else { } else {
backups.emplace_back(mentry->mnt_dir, mentry->mnt_opts); backups.emplace_back(mentry->mnt_dir, mentry->mnt_opts);
@@ -38,6 +43,12 @@ void revert_unmount() {
} }
return true; return true;
}); });
parse_mnt("/proc/self/mounts", [&](mntent* mentry) {
if (mentry->mnt_fsname == ksu_loop) {
targets.emplace_back(mentry->mnt_dir);
}
return true;
});
for (auto& s: reversed(targets)) { for (auto& s: reversed(targets)) {
lazy_unmount(s.data()); lazy_unmount(s.data());