1 /* SPDX-License-Identifier: GPL-2.0 */
7 #include <linux/limits.h>
13 #include <sys/inotify.h>
15 #include <sys/types.h>
19 #include "cgroup_util.h"
20 #include "../clone3/clone3_selftests.h"
22 /* Returns read len on success, or -errno on failure. */
23 static ssize_t read_text(const char *path, char *buf, size_t max_len)
28 fd = open(path, O_RDONLY);
32 len = read(fd, buf, max_len - 1);
38 return len < 0 ? -errno : len;
41 /* Returns written len on success, or -errno on failure. */
42 static ssize_t write_text(const char *path, char *buf, ssize_t len)
46 fd = open(path, O_WRONLY | O_APPEND);
50 len = write(fd, buf, len);
52 return len < 0 ? -errno : len;
55 char *cg_name(const char *root, const char *name)
57 size_t len = strlen(root) + strlen(name) + 2;
58 char *ret = malloc(len);
60 snprintf(ret, len, "%s/%s", root, name);
65 char *cg_name_indexed(const char *root, const char *name, int index)
67 size_t len = strlen(root) + strlen(name) + 10;
68 char *ret = malloc(len);
70 snprintf(ret, len, "%s/%s_%d", root, name, index);
75 char *cg_control(const char *cgroup, const char *control)
77 size_t len = strlen(cgroup) + strlen(control) + 2;
78 char *ret = malloc(len);
80 snprintf(ret, len, "%s/%s", cgroup, control);
85 /* Returns 0 on success, or -errno on failure. */
86 int cg_read(const char *cgroup, const char *control, char *buf, size_t len)
91 snprintf(path, sizeof(path), "%s/%s", cgroup, control);
93 ret = read_text(path, buf, len);
94 return ret >= 0 ? 0 : ret;
97 int cg_read_strcmp(const char *cgroup, const char *control,
104 /* Handle the case of comparing against empty string */
108 size = strlen(expected) + 1;
114 if (cg_read(cgroup, control, buf, size)) {
119 ret = strcmp(expected, buf);
124 int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
128 if (cg_read(cgroup, control, buf, sizeof(buf)))
131 return strstr(buf, needle) ? 0 : -1;
134 long cg_read_long(const char *cgroup, const char *control)
138 if (cg_read(cgroup, control, buf, sizeof(buf)))
144 long cg_read_key_long(const char *cgroup, const char *control, const char *key)
149 if (cg_read(cgroup, control, buf, sizeof(buf)))
152 ptr = strstr(buf, key);
156 return atol(ptr + strlen(key));
159 long cg_read_lc(const char *cgroup, const char *control)
162 const char delim[] = "\n";
166 if (cg_read(cgroup, control, buf, sizeof(buf)))
169 for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
175 /* Returns 0 on success, or -errno on failure. */
176 int cg_write(const char *cgroup, const char *control, char *buf)
179 ssize_t len = strlen(buf), ret;
181 snprintf(path, sizeof(path), "%s/%s", cgroup, control);
182 ret = write_text(path, buf, len);
183 return ret == len ? 0 : ret;
186 int cg_write_numeric(const char *cgroup, const char *control, long value)
191 ret = sprintf(buf, "%lu", value);
195 return cg_write(cgroup, control, buf);
198 int cg_find_unified_root(char *root, size_t len)
200 char buf[10 * PAGE_SIZE];
201 char *fs, *mount, *type;
202 const char delim[] = "\n\t ";
204 if (read_text("/proc/self/mounts", buf, sizeof(buf)) <= 0)
209 * cgroup /sys/fs/cgroup cgroup2 rw,seclabel,noexec,relatime 0 0
211 for (fs = strtok(buf, delim); fs; fs = strtok(NULL, delim)) {
212 mount = strtok(NULL, delim);
213 type = strtok(NULL, delim);
218 if (strcmp(type, "cgroup2") == 0) {
219 strncpy(root, mount, len);
227 int cg_create(const char *cgroup)
229 return mkdir(cgroup, 0755);
232 int cg_wait_for_proc_count(const char *cgroup, int count)
234 char buf[10 * PAGE_SIZE] = {0};
238 for (attempts = 10; attempts >= 0; attempts--) {
241 if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
244 for (ptr = buf; *ptr; ptr++)
257 int cg_killall(const char *cgroup)
262 /* If cgroup.kill exists use it. */
263 if (!cg_write(cgroup, "cgroup.kill", "1"))
266 if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
269 while (ptr < buf + sizeof(buf)) {
270 int pid = strtol(ptr, &ptr, 10);
278 if (kill(pid, SIGKILL))
285 int cg_destroy(const char *cgroup)
291 if (ret && errno == EBUSY) {
297 if (ret && errno == ENOENT)
303 int cg_enter(const char *cgroup, int pid)
307 snprintf(pidbuf, sizeof(pidbuf), "%d", pid);
308 return cg_write(cgroup, "cgroup.procs", pidbuf);
311 int cg_enter_current(const char *cgroup)
313 return cg_write(cgroup, "cgroup.procs", "0");
316 int cg_enter_current_thread(const char *cgroup)
318 return cg_write(cgroup, "cgroup.threads", "0");
321 int cg_run(const char *cgroup,
322 int (*fn)(const char *cgroup, void *arg),
330 } else if (pid == 0) {
333 snprintf(buf, sizeof(buf), "%d", getpid());
334 if (cg_write(cgroup, "cgroup.procs", buf))
336 exit(fn(cgroup, arg));
338 waitpid(pid, &retcode, 0);
339 if (WIFEXITED(retcode))
340 return WEXITSTATUS(retcode);
346 pid_t clone_into_cgroup(int cgroup_fd)
348 #ifdef CLONE_ARGS_SIZE_VER2
351 struct __clone_args args = {
352 .flags = CLONE_INTO_CGROUP,
353 .exit_signal = SIGCHLD,
357 pid = sys_clone3(&args, sizeof(struct __clone_args));
359 * Verify that this is a genuine test failure:
360 * ENOSYS -> clone3() not available
361 * E2BIG -> CLONE_INTO_CGROUP not available
363 if (pid < 0 && (errno == ENOSYS || errno == E2BIG))
374 int clone_reap(pid_t pid, int options)
382 ret = waitid(P_PID, pid, &info, options | __WALL | __WNOTHREAD);
389 if (options & WEXITED) {
390 if (WIFEXITED(info.si_status))
391 return WEXITSTATUS(info.si_status);
394 if (options & WSTOPPED) {
395 if (WIFSTOPPED(info.si_status))
396 return WSTOPSIG(info.si_status);
399 if (options & WCONTINUED) {
400 if (WIFCONTINUED(info.si_status))
407 int dirfd_open_opath(const char *dir)
409 return open(dir, O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW | O_PATH);
412 #define close_prot_errno(fd) \
419 static int clone_into_cgroup_run_nowait(const char *cgroup,
420 int (*fn)(const char *cgroup, void *arg),
426 cgroup_fd = dirfd_open_opath(cgroup);
430 pid = clone_into_cgroup(cgroup_fd);
431 close_prot_errno(cgroup_fd);
433 exit(fn(cgroup, arg));
438 int cg_run_nowait(const char *cgroup,
439 int (*fn)(const char *cgroup, void *arg),
444 pid = clone_into_cgroup_run_nowait(cgroup, fn, arg);
448 /* Genuine test failure. */
449 if (pid < 0 && errno != ENOSYS)
456 snprintf(buf, sizeof(buf), "%d", getpid());
457 if (cg_write(cgroup, "cgroup.procs", buf))
459 exit(fn(cgroup, arg));
465 int get_temp_fd(void)
467 return open(".", O_TMPFILE | O_RDWR | O_EXCL);
470 int alloc_pagecache(int fd, size_t size)
481 if (ftruncate(fd, size))
484 for (i = 0; i < size; i += sizeof(buf))
485 read(fd, buf, sizeof(buf));
493 int alloc_anon(const char *cgroup, void *arg)
495 size_t size = (unsigned long)arg;
499 for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
506 int is_swap_enabled(void)
509 const char delim[] = "\n";
513 if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
516 for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
522 int set_oom_adj_score(int pid, int score)
527 sprintf(path, "/proc/%d/oom_score_adj", pid);
529 fd = open(path, O_WRONLY | O_APPEND);
533 len = dprintf(fd, "%d", score);
543 int proc_mount_contains(const char *option)
545 char buf[4 * PAGE_SIZE];
548 read = read_text("/proc/mounts", buf, sizeof(buf));
552 return strstr(buf, option) != NULL;
555 ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t size)
561 snprintf(path, sizeof(path), "/proc/%s/%s",
562 thread ? "thread-self" : "self", item);
564 snprintf(path, sizeof(path), "/proc/%d/%s", pid, item);
566 ret = read_text(path, buf, size);
567 return ret < 0 ? -1 : ret;
570 int proc_read_strstr(int pid, bool thread, const char *item, const char *needle)
574 if (proc_read_text(pid, thread, item, buf, sizeof(buf)) < 0)
577 return strstr(buf, needle) ? 0 : -1;
580 int clone_into_cgroup_run_wait(const char *cgroup)
585 cgroup_fd = dirfd_open_opath(cgroup);
589 pid = clone_into_cgroup(cgroup_fd);
590 close_prot_errno(cgroup_fd);
598 * We don't care whether this fails. We only care whether the initial
601 (void)clone_reap(pid, WEXITED);
605 static int __prepare_for_wait(const char *cgroup, const char *filename)
609 fd = inotify_init1(0);
613 ret = inotify_add_watch(fd, cg_control(cgroup, filename), IN_MODIFY);
622 int cg_prepare_for_wait(const char *cgroup)
624 return __prepare_for_wait(cgroup, "cgroup.events");
627 int memcg_prepare_for_wait(const char *cgroup)
629 return __prepare_for_wait(cgroup, "memory.events");
632 int cg_wait_for(int fd)
635 struct pollfd fds = {
641 ret = poll(&fds, 1, 10000);
650 if (ret > 0 && fds.revents & POLLIN) {