Skip to content

Commit

Permalink
Maintain a list of pre-init mounts
Browse files Browse the repository at this point in the history
Keep track of everything to unmount
  • Loading branch information
topjohnwu committed Jul 17, 2019
1 parent aa47966 commit 736729f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
10 changes: 6 additions & 4 deletions native/jni/core/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ static void main_daemon() {
restore_rootcon();

// Unmount pre-init patches
umount2("/init", MNT_DETACH);
umount2("/init.rc", MNT_DETACH);
umount2("/system/lib/libselinux.so", MNT_DETACH);
umount2("/system/lib64/libselinux.so", MNT_DETACH);
if (access(ROOTMNT, F_OK) == 0) {
file_readline(ROOTMNT, [](auto line) -> bool {
umount2(line.data(), MNT_DETACH);
return true;
}, true);
}

int fd = xopen("/dev/null", O_RDWR | O_CLOEXEC);
xdup2(fd, STDOUT_FILENO);
Expand Down
2 changes: 2 additions & 0 deletions native/jni/include/magisk.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#define BLOCKDIR MAGISKTMP "/block"
#define BBPATH MAGISKTMP "/busybox"
#define MODULEMNT MAGISKTMP "/modules"
#define ROOTOVL MAGISKTMP "/rootdir"
#define ROOTMNT ROOTOVL "/.mount_list"
#define SECURE_DIR "/data/adb"
#define MODULEROOT SECURE_DIR "/modules"
#define MODULEUPGRADE SECURE_DIR "/modules_update"
Expand Down
8 changes: 7 additions & 1 deletion native/jni/init/rootdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,14 @@ static void sbin_overlay(const raw_data &self, const raw_data &config) {
xsymlink("./magiskinit", "/sbin/supolicy");
}

#define ROOTOVL MAGISKTMP "/rootdir"
#define ROOTMIR MIRRDIR "/system_root"
#define ROOTBLK BLOCKDIR "/system_root"
#define MONOPOLICY "/sepolicy"
#define PATCHPOLICY "/sbin/.se"
#define LIBSELINUX "/system/" LIBNAME "/libselinux.so"

static string mount_list;

static void magic_mount(int dirfd, const string &path) {
DIR *dir = xfdopendir(dirfd);
for (dirent *entry; (entry = readdir(dir));) {
Expand All @@ -248,6 +249,8 @@ static void magic_mount(int dirfd, const string &path) {
string src = ROOTOVL + dest;
LOGD("Mount [%s] -> [%s]\n", src.data(), dest.data());
xmount(src.data(), dest.data(), nullptr, MS_BIND, nullptr);
mount_list += dest;
mount_list += '\n';
}
}
}
Expand Down Expand Up @@ -376,6 +379,9 @@ void SARCommon::patch_rootdir() {
src = xopen(ROOTOVL, O_RDONLY | O_CLOEXEC);
magic_mount(src, "");
close(src);
dest = xopen(ROOTMNT, O_WRONLY | O_CREAT | O_CLOEXEC);
write(dest, mount_list.data(), mount_list.length());
close(dest);
}

#define FSR "/first_stage_ramdisk"
Expand Down

0 comments on commit 736729f

Please sign in to comment.