vvb2060 2021-02-27 23:40:55 +08:00 committed by John Wu
parent 39982d57ef
commit b693d13b93

View File

@ -148,7 +148,22 @@ shortcut:
static void magisk_logging(); static void magisk_logging();
static void daemon_entry(int ppid) { static int switch_cgroup(const char *cgroup, int pid) {
char buf[32];
snprintf(buf, sizeof(buf), "%s/cgroup.procs", cgroup);
int fd = open(buf, O_WRONLY | O_APPEND | O_CLOEXEC);
if (fd == -1)
return -1;
snprintf(buf, sizeof(buf), "%d\n", pid);
if (xwrite(fd, buf, strlen(buf)) == -1) {
close(fd);
return -1;
}
close(fd);
return 0;
}
[[noreturn]] static void daemon_entry() {
magisk_logging(); magisk_logging();
int fd = xopen("/dev/null", O_WRONLY); int fd = xopen("/dev/null", O_WRONLY);
@ -166,22 +181,15 @@ static void daemon_entry(int ppid) {
LOGI(NAME_WITH_VER(Magisk) " daemon started\n"); LOGI(NAME_WITH_VER(Magisk) " daemon started\n");
// Make sure ppid is not in acct // Escape from cgroup
char src[64], dest[64]; int pid = getpid();
sprintf(src, "/acct/uid_0/pid_%d", ppid); if (switch_cgroup("/acct", pid) && switch_cgroup("/sys/fs/cgroup", pid))
if (access(src, F_OK) == 0) { LOGW("Can't switch cgroup\n");
sprintf(dest, "/acct/uid_0/pid_%d", getpid());
rename(src, dest);
}
sprintf(src, "/sys/fs/cgroup/uid_0/pid_%d", ppid);
if (access(src, F_OK) == 0) {
sprintf(dest, "/sys/fs/cgroup/uid_0/pid_%d", getpid());
rename(src, dest);
}
// Get self stat // Get self stat
xreadlink("/proc/self/exe", src, sizeof(src)); char buf[64];
MAGISKTMP = dirname(src); xreadlink("/proc/self/exe", buf, sizeof(buf));
MAGISKTMP = dirname(buf);
xstat("/proc/self/exe", &self_st); xstat("/proc/self/exe", &self_st);
// Get API level // Get API level
@ -253,11 +261,10 @@ int connect_daemon(bool create) {
exit(1); exit(1);
} }
int ppid = getpid();
LOGD("client: launching new main daemon process\n"); LOGD("client: launching new main daemon process\n");
if (fork_dont_care() == 0) { if (fork_dont_care() == 0) {
close(fd); close(fd);
daemon_entry(ppid); daemon_entry();
} }
while (connect(fd, (struct sockaddr*) &sun, len)) while (connect(fd, (struct sockaddr*) &sun, len))