Micro optimizations

This commit is contained in:
topjohnwu 2018-11-13 02:07:02 -05:00
parent 9592a69986
commit 3a2a2a4ffa
6 changed files with 26 additions and 26 deletions

View File

@ -506,21 +506,21 @@ static bool magisk_env() {
strcmp(entry->d_name, ".core") == 0 || strcmp(entry->d_name, ".core") == 0 ||
strcmp(entry->d_name, "lost+found") == 0) strcmp(entry->d_name, "lost+found") == 0)
continue; continue;
snprintf(buf, PATH_MAX, "%s/%s/remove", MOUNTPOINT, entry->d_name); snprintf(buf, PATH_MAX, "%s/%s", MOUNTPOINT, entry->d_name);
if (access(buf, F_OK) == 0) { chdir(buf);
snprintf(buf, PATH_MAX, "%s/%s", MOUNTPOINT, entry->d_name); if (access("remove", F_OK) == 0) {
chdir("..");
rm_rf(buf); rm_rf(buf);
continue; continue;
} }
snprintf(buf, PATH_MAX, "%s/%s/update", MOUNTPOINT, entry->d_name); unlink("update");
unlink(buf); if (access("disable", F_OK) == 0)
snprintf(buf, PATH_MAX, "%s/%s/disable", MOUNTPOINT, entry->d_name);
if (access(buf, F_OK) == 0)
continue; continue;
module_list.push_back(entry->d_name); module_list.push_back(entry->d_name);
} }
} }
closedir(dir); closedir(dir);
chdir("/");
return trim_img(MAINIMG, MOUNTPOINT, magiskloop) == 0; return trim_img(MAINIMG, MOUNTPOINT, magiskloop) == 0;
} }

View File

@ -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() { int connect_daemon() {
struct sockaddr_un sun; struct sockaddr_un sun;
socklen_t len = setup_sockaddr(&sun, MAIN_SOCKET); socklen_t len = setup_sockaddr(&sun, MAIN_SOCKET);

View File

@ -37,6 +37,7 @@ enum {
// daemon.c // daemon.c
int connect_daemon(); int connect_daemon();
int switch_mnt_ns(int pid);
// log_monitor.c // log_monitor.c

View File

@ -72,9 +72,10 @@ static void hide_daemon(int pid) {
if (switch_mnt_ns(pid)) if (switch_mnt_ns(pid))
goto exit; goto exit;
snprintf(buffer, sizeof(buffer), "/proc/%d/mounts", pid); snprintf(buffer, sizeof(buffer), "/proc/%d", pid);
file_to_vector(buffer, mounts); chdir(buffer);
file_to_vector("mounts", mounts);
// Unmount dummy skeletons and /sbin links // Unmount dummy skeletons and /sbin links
for (auto &s : mounts) { for (auto &s : mounts) {
if (s.contains("tmpfs /system/") || s.contains("tmpfs /vendor/") || s.contains("tmpfs /sbin")) { 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(); mounts.clear();
// Re-read mount infos // Re-read mount infos
snprintf(buffer, sizeof(buffer), "/proc/%d/mounts", pid); file_to_vector("mounts", mounts);
file_to_vector(buffer, mounts);
// Unmount everything under /system, /vendor, and loop mounts // Unmount everything under /system, /vendor, and loop mounts
for (auto &s : mounts) { for (auto &s : mounts) {

View File

@ -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_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(int err, int *fd, void (*cb)(void), const char *argv0, ...);
int exec_command_sync(const char *argv0, ...); int exec_command_sync(const char *argv0, ...);
int switch_mnt_ns(int pid);
int fork_dont_care(); int fork_dont_care();
void gen_rand_str(char *buf, int len); void gen_rand_str(char *buf, int len);
int strend(const char *s1, const char *s2); int strend(const char *s1, const char *s2);

View File

@ -67,20 +67,6 @@ ssize_t fdgets(char *buf, const size_t size, int fd) {
return len; 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 fork_dont_care() {
int pid = xfork(); int pid = xfork();
if (pid) { if (pid) {