diff --git a/native/jni/main.cpp b/native/jni/main.cpp index ad9ecd2..8e524c8 100644 --- a/native/jni/main.cpp +++ b/native/jni/main.cpp @@ -16,6 +16,8 @@ using namespace std; #define TO_STR std::string("") +#define MNT_FLAGS (MS_LAZYTIME | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_DIRSYNC | MS_NOATIME | MS_NODIRATIME | MS_RELATIME | MS_STRICTATIME | MS_NOSYMFOLLOW | MS_MANDLOCK | MS_SILENT) + int log_fd = -1; std::string overlay_tmpdir = ""; std::vector mountinfo; @@ -57,7 +59,7 @@ static int do_remount(int flags = 0, int exclude_flags = 0) { int fd = open(info.data(), O_PATH); string fd_path = "/proc/self/fd/"; fd_path += std::to_string(fd); - LOGD("%s [%s] (%s)\n", (mount(nullptr, fd_path.data(), nullptr, MS_REMOUNT | (stvfs.f_flag & ~exclude_flags) | flags, nullptr) == 0)? + LOGD("%s [%s] (%s)\n", (mount(nullptr, fd_path.data(), nullptr, MS_REMOUNT | (stvfs.f_flag & MNT_FLAGS & ~exclude_flags) | flags, nullptr) == 0)? "remounted" : "remount failed", info.data(), mnt.type.data()); close(fd); } else { @@ -214,8 +216,10 @@ int main(int argc, const char **argv) { std::reverse(mountinfo.begin(), mountinfo.end()); for (auto &info : SYSTEM_PARTITIONS ) { struct stat st; + struct statvfs FS_BUF; if (lstat(info.data(), &st) || !S_ISDIR(st.st_mode)) continue; + statvfs(info.data(), &FS_BUF); std::string tmp_mount = overlay_tmpdir + info; mkdirs(tmp_mount.data(), 0); @@ -277,14 +281,14 @@ int main(int argc, const char **argv) { // 1 - read-write default // 2 - read-only locked - if (OVERLAY_MODE == 2 || xmount("overlay", tmp_mount.data(), "overlay", MS_RDONLY | MS_NOATIME, opts.data())) { + if (OVERLAY_MODE == 2 || xmount("overlay", tmp_mount.data(), "overlay", MS_RDONLY | (FS_BUF.f_flag & MNT_FLAGS), opts.data())) { opts = TO_STR + "lowerdir=" + upperdir + ":" + get_lowerdirs(module_list, info.data()) + info; - if (xmount("overlay", tmp_mount.data(), "overlay", MS_RDONLY | MS_NOATIME, opts.data())) { + if (xmount("overlay", tmp_mount.data(), "overlay", MS_RDONLY | (FS_BUF.f_flag & MNT_FLAGS), opts.data())) { LOGW("Unable to add [%s], ignore!\n", info.data()); continue; } @@ -299,8 +303,10 @@ int main(int argc, const char **argv) { for (auto &mnt : mountinfo) { auto info = mnt.target; struct stat st; + struct statvfs FS_BUF; if (stat(info.data(), &st)) continue; + statvfs(info.data(), &FS_BUF); std::string tmp_mount = overlay_tmpdir + info; std::string upperdir = upper + info; std::string workerdir = worker + "/" + std::to_string(st.st_dev) + "/" + std::to_string(st.st_ino); @@ -388,7 +394,7 @@ int main(int argc, const char **argv) { // 1 - read-write default // 2 - read-only locked - if (OVERLAY_MODE == 2 || xmount("overlay", tmp_mount.data(), "overlay", MS_RDONLY | MS_NOATIME, opts.data())) { + if (OVERLAY_MODE == 2 || xmount("overlay", tmp_mount.data(), "overlay", MS_RDONLY | (FS_BUF.f_flag & MNT_FLAGS), opts.data())) { opts = TO_STR + "lowerdir=" + upperdir + @@ -397,7 +403,7 @@ int main(int argc, const char **argv) { info; if (!str_empty(context_opt.data())) opts += TO_STR + "," + context_opt; - if (xmount("overlay", tmp_mount.data(), "overlay", MS_RDONLY | MS_NOATIME, opts.data())) { + if (xmount("overlay", tmp_mount.data(), "overlay", MS_RDONLY | (FS_BUF.f_flag & MNT_FLAGS), opts.data())) { // for some reason, overlayfs does not support some filesystems such as vfat, tmpfs, f2fs // then bind mount it back but we will not be able to modify its content LOGW("mount overlayfs failed, fall to bind mount!\n"); diff --git a/native/jni/utils.cpp b/native/jni/utils.cpp index 148682a..6646dd8 100644 --- a/native/jni/utils.cpp +++ b/native/jni/utils.cpp @@ -1,5 +1,6 @@ #include "logging.hpp" #include "base.hpp" +#include "utils.hpp" char *random_strc(int n){ int urandom_fd = open("/dev/urandom", O_RDONLY); @@ -29,7 +30,7 @@ bool fexist(const char *path) { struct stat st; return lstat(path, &st) == 0; } - + bool is_dir(const char *path) { struct stat st; @@ -142,9 +143,35 @@ int dump_file(const char *src, const char *dest) { int verbose_mount(const char *a, const char *b, const char *c, int d, const char *e) { int ret = mount(a,b,c,d,e); + std::string mount_opts = ""; + if (d & MS_PRIVATE) mount_opts += ",private"; + else if (d & MS_SLAVE) mount_opts += ",slave"; + else if (d & MS_SHARED) mount_opts += ",shared"; + else if (d & MS_UNBINDABLE) mount_opts += ",unbindable"; + else { + if (d & MS_RDONLY) mount_opts += ",ro"; else if ((d & MS_BIND) == 0) mount_opts += ",rw"; + if (d & MS_LAZYTIME) mount_opts += ",lazytime"; + if (d & MS_NODEV) mount_opts += ",nodev"; + if (d & MS_NOEXEC) mount_opts += ",noexec"; + if (d & MS_NOSUID) mount_opts += ",nosuid"; + if (d & MS_SYNCHRONOUS) mount_opts += ",sync"; + if (d & MS_NOATIME) mount_opts += ",noatime"; + if (d & MS_NODIRATIME) mount_opts += ",nodiratime"; + if (d & MS_RELATIME) mount_opts += ",relatime"; + if (d & MS_STRICTATIME) mount_opts += ",strictatime"; + if (d & MS_NOSYMFOLLOW) mount_opts += ",nosymfollow"; + if (d & MS_MANDLOCK) mount_opts += ",mand"; + if (d & MS_SILENT) mount_opts += ",silent"; + if (d & MS_REMOUNT) mount_opts += ",remount"; + if (d & MS_BIND) mount_opts += ",bind"; + } + if (d & MS_REC) mount_opts += ",rec"; + if (e) { + mount_opts += std::string(",") + e; + } if (ret == 0) { LOGD("mount: %s%s%s%s\n", b, (a != nullptr && a[0] != '\0')? std::string(std::string(" <- ") + a).data() : "", - c? std::string(std::string(" (") + c + ")").data() : "", e? std::string(std::string(" [") + e + "]").data() : ""); + c? std::string(std::string(" (") + c + ")").data() : "", (!str_empty(mount_opts.data()))? std::string(std::string(" [") + (mount_opts.data() + 1) + "]").data() : ""); } else { PLOGE("mount: %s%s", (a != nullptr && a[0] != '\0')? std::string(std::string(a) + " -> ").data() : "", b); } @@ -217,6 +244,6 @@ int setfilecon(const char *path, const char *con) { } void freecon(char *con) { - free(con); + free(con); }