From 3a2a2a4ffa86627ac70f351a75808d20499cc832 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 13 Nov 2018 02:07:02 -0500 Subject: [PATCH] Micro optimizations --- native/jni/daemon/bootstages.cpp | 14 +++++++------- native/jni/daemon/daemon.cpp | 14 ++++++++++++++ native/jni/include/daemon.h | 1 + native/jni/magiskhide/proc_monitor.cpp | 8 ++++---- native/jni/utils/include/utils.h | 1 - native/jni/utils/misc.cpp | 14 -------------- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/native/jni/daemon/bootstages.cpp b/native/jni/daemon/bootstages.cpp index f5b96072b..111b5700a 100644 --- a/native/jni/daemon/bootstages.cpp +++ b/native/jni/daemon/bootstages.cpp @@ -506,21 +506,21 @@ static bool magisk_env() { strcmp(entry->d_name, ".core") == 0 || strcmp(entry->d_name, "lost+found") == 0) continue; - snprintf(buf, PATH_MAX, "%s/%s/remove", MOUNTPOINT, entry->d_name); - if (access(buf, F_OK) == 0) { - snprintf(buf, PATH_MAX, "%s/%s", MOUNTPOINT, entry->d_name); + snprintf(buf, PATH_MAX, "%s/%s", MOUNTPOINT, entry->d_name); + chdir(buf); + if (access("remove", F_OK) == 0) { + chdir(".."); rm_rf(buf); continue; } - snprintf(buf, PATH_MAX, "%s/%s/update", MOUNTPOINT, entry->d_name); - unlink(buf); - snprintf(buf, PATH_MAX, "%s/%s/disable", MOUNTPOINT, entry->d_name); - if (access(buf, F_OK) == 0) + unlink("update"); + if (access("disable", F_OK) == 0) continue; module_list.push_back(entry->d_name); } } closedir(dir); + chdir("/"); return trim_img(MAINIMG, MOUNTPOINT, magiskloop) == 0; } diff --git a/native/jni/daemon/daemon.cpp b/native/jni/daemon/daemon.cpp index 3f72be6bd..1712c5d57 100644 --- a/native/jni/daemon/daemon.cpp +++ b/native/jni/daemon/daemon.cpp @@ -132,6 +132,20 @@ static void main_daemon() { } } +int switch_mnt_ns(int pid) { + char mnt[32]; + snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid); + if(access(mnt, R_OK) == -1) return 1; // Maybe process died.. + + int fd, ret; + fd = xopen(mnt, O_RDONLY); + if (fd < 0) return 1; + // Switch to its namespace + ret = xsetns(fd, 0); + close(fd); + return ret; +} + int connect_daemon() { struct sockaddr_un sun; socklen_t len = setup_sockaddr(&sun, MAIN_SOCKET); diff --git a/native/jni/include/daemon.h b/native/jni/include/daemon.h index 15f377c5d..44fc6b2e0 100644 --- a/native/jni/include/daemon.h +++ b/native/jni/include/daemon.h @@ -37,6 +37,7 @@ enum { // daemon.c int connect_daemon(); +int switch_mnt_ns(int pid); // log_monitor.c diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index c38e0ff60..10179b3d2 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -72,9 +72,10 @@ static void hide_daemon(int pid) { if (switch_mnt_ns(pid)) goto exit; - snprintf(buffer, sizeof(buffer), "/proc/%d/mounts", pid); - file_to_vector(buffer, mounts); + snprintf(buffer, sizeof(buffer), "/proc/%d", pid); + chdir(buffer); + file_to_vector("mounts", mounts); // Unmount dummy skeletons and /sbin links for (auto &s : mounts) { if (s.contains("tmpfs /system/") || s.contains("tmpfs /vendor/") || s.contains("tmpfs /sbin")) { @@ -85,8 +86,7 @@ static void hide_daemon(int pid) { mounts.clear(); // Re-read mount infos - snprintf(buffer, sizeof(buffer), "/proc/%d/mounts", pid); - file_to_vector(buffer, mounts); + file_to_vector("mounts", mounts); // Unmount everything under /system, /vendor, and loop mounts for (auto &s : mounts) { diff --git a/native/jni/utils/include/utils.h b/native/jni/utils/include/utils.h index a91480f5e..6f71bef76 100644 --- a/native/jni/utils/include/utils.h +++ b/native/jni/utils/include/utils.h @@ -94,7 +94,6 @@ int is_num(const char *s); int exec_array(int err, int *fd, void (*cb)(void), const char *argv[]); int exec_command(int err, int *fd, void (*cb)(void), const char *argv0, ...); int exec_command_sync(const char *argv0, ...); -int switch_mnt_ns(int pid); int fork_dont_care(); void gen_rand_str(char *buf, int len); int strend(const char *s1, const char *s2); diff --git a/native/jni/utils/misc.cpp b/native/jni/utils/misc.cpp index d7b145ed0..e1a2faec2 100644 --- a/native/jni/utils/misc.cpp +++ b/native/jni/utils/misc.cpp @@ -67,20 +67,6 @@ ssize_t fdgets(char *buf, const size_t size, int fd) { return len; } -int switch_mnt_ns(int pid) { - char mnt[32]; - snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid); - if(access(mnt, R_OK) == -1) return 1; // Maybe process died.. - - int fd, ret; - fd = xopen(mnt, O_RDONLY); - if (fd < 0) return 1; - // Switch to its namespace - ret = xsetns(fd, 0); - close(fd); - return ret; -} - int fork_dont_care() { int pid = xfork(); if (pid) {