mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-24 03:37:58 +00:00
Proper implementation of cgroup migration
https://www.kernel.org/doc/Documentation/admin-guide/cgroup-v1/cgroups.rst https://www.kernel.org/doc/Documentation/admin-guide/cgroup-v2.rst
This commit is contained in:
parent
39982d57ef
commit
b693d13b93
@ -148,7 +148,22 @@ shortcut:
|
||||
|
||||
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();
|
||||
|
||||
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");
|
||||
|
||||
// Make sure ppid is not in acct
|
||||
char src[64], dest[64];
|
||||
sprintf(src, "/acct/uid_0/pid_%d", ppid);
|
||||
if (access(src, F_OK) == 0) {
|
||||
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);
|
||||
}
|
||||
// Escape from cgroup
|
||||
int pid = getpid();
|
||||
if (switch_cgroup("/acct", pid) && switch_cgroup("/sys/fs/cgroup", pid))
|
||||
LOGW("Can't switch cgroup\n");
|
||||
|
||||
// Get self stat
|
||||
xreadlink("/proc/self/exe", src, sizeof(src));
|
||||
MAGISKTMP = dirname(src);
|
||||
char buf[64];
|
||||
xreadlink("/proc/self/exe", buf, sizeof(buf));
|
||||
MAGISKTMP = dirname(buf);
|
||||
xstat("/proc/self/exe", &self_st);
|
||||
|
||||
// Get API level
|
||||
@ -253,11 +261,10 @@ int connect_daemon(bool create) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int ppid = getpid();
|
||||
LOGD("client: launching new main daemon process\n");
|
||||
if (fork_dont_care() == 0) {
|
||||
close(fd);
|
||||
daemon_entry(ppid);
|
||||
daemon_entry();
|
||||
}
|
||||
|
||||
while (connect(fd, (struct sockaddr*) &sun, len))
|
||||
|
Loading…
x
Reference in New Issue
Block a user