Skip to content

Commit

Permalink
Merge branch 'pascua28:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
zelodev authored Sep 5, 2024
2 parents fe7de9c + dfb60b4 commit 8b4d3a0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 46 deletions.
41 changes: 18 additions & 23 deletions manager/src/main/jni/cgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,28 @@ namespace cgroup {
return -1;
}

static int switch_cgroup(int pid, int cuid, int cpid, const char *name) {
int switch_cgroup(int pid) {
char buf[PATH_MAX];
if (cuid != -1 && cpid != -1) {
snprintf(buf, PATH_MAX, "/acct/uid_%d/pid_%d/%s", cuid, cpid, name);
} else {
snprintf(buf, PATH_MAX, "/acct/%s", name);
}

int fd = open(buf, O_WRONLY | O_APPEND);
if (fd == -1)
return -1;
// TODO: add more later on
const char* cgroup_paths[] = {
"/sys/fs/cgroup/uid_0/cgroup.procs",
"/acct/uid_0/cgroup.procs",
"/acct/cgroup.procs"
};

snprintf(buf, PATH_MAX, "%d\n", pid);
if (write(fd, buf, strlen(buf)) == -1) {
close(fd);
return -1;
for (const char* path : cgroup_paths) {
int fd = open(path, O_WRONLY | O_APPEND);
if (fd != -1) {
snprintf(buf, sizeof(buf), "%d\n", pid);
if (write(fd, buf, strlen(buf)) != -1) {
close(fd);
return 0;
}
close(fd);
}
}

close(fd);
return 0;
}

int switch_cgroup(int pid, int cuid, int cpid) {
int res = 0;
res += switch_cgroup(pid, cuid, cpid, "cgroup.procs");
res += switch_cgroup(pid, cuid, cpid, "tasks");
return res;
return -1;
}

}
2 changes: 1 addition & 1 deletion manager/src/main/jni/cgroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace cgroup {
int get_cgroup(int pid, int* cuid, int *cpid);
int switch_cgroup(int pid, int cuid, int cpid);
int switch_cgroup(int pid);
}

#endif // CGROUP_H
27 changes: 5 additions & 22 deletions manager/src/main/jni/starter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static int switch_cgroup() {
printf("info: cgroup is /uid_%d/pid_%d\n", s_cuid, s_cpid);
fflush(stdout);

if (cgroup::switch_cgroup(spid, -1, -1) != 0) {
if (cgroup::switch_cgroup(spid) != 0) {
printf("warn: can't switch cgroup\n");
fflush(stdout);
return -1;
Expand Down Expand Up @@ -381,26 +381,6 @@ using main_func = int (*)(int, char *[]);

static main_func applet_main[] = {starter_main, nullptr};

static void move_cgroup(int dpid) {
const char* cgroup_path = "/sys/fs/cgroup/uid_0/cgroup.procs";
char buf[8];
snprintf(buf, sizeof(buf), "%d\n", dpid);

if (getuid() != 1000)
return;

int fd = open(cgroup_path, O_WRONLY);
if (fd < 0)
return;

if (write(fd, buf, strlen(buf)) == -1) {
close(fd);
return;
}

close(fd);
}

static int fork_daemon(int returnParent) {
pid_t child = fork();
if (child < 0) {
Expand Down Expand Up @@ -442,7 +422,10 @@ static int fork_daemon(int returnParent) {
exit(EXIT_SUCCESS);
}

move_cgroup(getpid());
if (getuid() == 1000) {
if (cgroup::switch_cgroup(getpid()) == 0)
LOGD("cgroup switched!");
}

// Second child
return 0;
Expand Down

0 comments on commit 8b4d3a0

Please sign in to comment.