WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / cgroup / cgroup_util.c
blob027014662fb2612171dae23586b8c8038276d70e
1 /* SPDX-License-Identifier: GPL-2.0 */
3 #define _GNU_SOURCE
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <linux/limits.h>
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <unistd.h>
17 #include "cgroup_util.h"
18 #include "../clone3/clone3_selftests.h"
20 static ssize_t read_text(const char *path, char *buf, size_t max_len)
22 ssize_t len;
23 int fd;
25 fd = open(path, O_RDONLY);
26 if (fd < 0)
27 return fd;
29 len = read(fd, buf, max_len - 1);
30 if (len < 0)
31 goto out;
33 buf[len] = 0;
34 out:
35 close(fd);
36 return len;
39 static ssize_t write_text(const char *path, char *buf, ssize_t len)
41 int fd;
43 fd = open(path, O_WRONLY | O_APPEND);
44 if (fd < 0)
45 return fd;
47 len = write(fd, buf, len);
48 if (len < 0) {
49 close(fd);
50 return len;
53 close(fd);
55 return len;
58 char *cg_name(const char *root, const char *name)
60 size_t len = strlen(root) + strlen(name) + 2;
61 char *ret = malloc(len);
63 snprintf(ret, len, "%s/%s", root, name);
65 return ret;
68 char *cg_name_indexed(const char *root, const char *name, int index)
70 size_t len = strlen(root) + strlen(name) + 10;
71 char *ret = malloc(len);
73 snprintf(ret, len, "%s/%s_%d", root, name, index);
75 return ret;
78 char *cg_control(const char *cgroup, const char *control)
80 size_t len = strlen(cgroup) + strlen(control) + 2;
81 char *ret = malloc(len);
83 snprintf(ret, len, "%s/%s", cgroup, control);
85 return ret;
88 int cg_read(const char *cgroup, const char *control, char *buf, size_t len)
90 char path[PATH_MAX];
92 snprintf(path, sizeof(path), "%s/%s", cgroup, control);
94 if (read_text(path, buf, len) >= 0)
95 return 0;
97 return -1;
100 int cg_read_strcmp(const char *cgroup, const char *control,
101 const char *expected)
103 size_t size;
104 char *buf;
105 int ret;
107 /* Handle the case of comparing against empty string */
108 if (!expected)
109 return -1;
110 else
111 size = strlen(expected) + 1;
113 buf = malloc(size);
114 if (!buf)
115 return -1;
117 if (cg_read(cgroup, control, buf, size)) {
118 free(buf);
119 return -1;
122 ret = strcmp(expected, buf);
123 free(buf);
124 return ret;
127 int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
129 char buf[PAGE_SIZE];
131 if (cg_read(cgroup, control, buf, sizeof(buf)))
132 return -1;
134 return strstr(buf, needle) ? 0 : -1;
137 long cg_read_long(const char *cgroup, const char *control)
139 char buf[128];
141 if (cg_read(cgroup, control, buf, sizeof(buf)))
142 return -1;
144 return atol(buf);
147 long cg_read_key_long(const char *cgroup, const char *control, const char *key)
149 char buf[PAGE_SIZE];
150 char *ptr;
152 if (cg_read(cgroup, control, buf, sizeof(buf)))
153 return -1;
155 ptr = strstr(buf, key);
156 if (!ptr)
157 return -1;
159 return atol(ptr + strlen(key));
162 long cg_read_lc(const char *cgroup, const char *control)
164 char buf[PAGE_SIZE];
165 const char delim[] = "\n";
166 char *line;
167 long cnt = 0;
169 if (cg_read(cgroup, control, buf, sizeof(buf)))
170 return -1;
172 for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
173 cnt++;
175 return cnt;
178 int cg_write(const char *cgroup, const char *control, char *buf)
180 char path[PATH_MAX];
181 ssize_t len = strlen(buf);
183 snprintf(path, sizeof(path), "%s/%s", cgroup, control);
185 if (write_text(path, buf, len) == len)
186 return 0;
188 return -1;
191 int cg_find_unified_root(char *root, size_t len)
193 char buf[10 * PAGE_SIZE];
194 char *fs, *mount, *type;
195 const char delim[] = "\n\t ";
197 if (read_text("/proc/self/mounts", buf, sizeof(buf)) <= 0)
198 return -1;
201 * Example:
202 * cgroup /sys/fs/cgroup cgroup2 rw,seclabel,noexec,relatime 0 0
204 for (fs = strtok(buf, delim); fs; fs = strtok(NULL, delim)) {
205 mount = strtok(NULL, delim);
206 type = strtok(NULL, delim);
207 strtok(NULL, delim);
208 strtok(NULL, delim);
209 strtok(NULL, delim);
211 if (strcmp(type, "cgroup2") == 0) {
212 strncpy(root, mount, len);
213 return 0;
217 return -1;
220 int cg_create(const char *cgroup)
222 return mkdir(cgroup, 0644);
225 int cg_wait_for_proc_count(const char *cgroup, int count)
227 char buf[10 * PAGE_SIZE] = {0};
228 int attempts;
229 char *ptr;
231 for (attempts = 10; attempts >= 0; attempts--) {
232 int nr = 0;
234 if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
235 break;
237 for (ptr = buf; *ptr; ptr++)
238 if (*ptr == '\n')
239 nr++;
241 if (nr >= count)
242 return 0;
244 usleep(100000);
247 return -1;
250 int cg_killall(const char *cgroup)
252 char buf[PAGE_SIZE];
253 char *ptr = buf;
255 if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
256 return -1;
258 while (ptr < buf + sizeof(buf)) {
259 int pid = strtol(ptr, &ptr, 10);
261 if (pid == 0)
262 break;
263 if (*ptr)
264 ptr++;
265 else
266 break;
267 if (kill(pid, SIGKILL))
268 return -1;
271 return 0;
274 int cg_destroy(const char *cgroup)
276 int ret;
278 retry:
279 ret = rmdir(cgroup);
280 if (ret && errno == EBUSY) {
281 cg_killall(cgroup);
282 usleep(100);
283 goto retry;
286 if (ret && errno == ENOENT)
287 ret = 0;
289 return ret;
292 int cg_enter(const char *cgroup, int pid)
294 char pidbuf[64];
296 snprintf(pidbuf, sizeof(pidbuf), "%d", pid);
297 return cg_write(cgroup, "cgroup.procs", pidbuf);
300 int cg_enter_current(const char *cgroup)
302 return cg_write(cgroup, "cgroup.procs", "0");
305 int cg_enter_current_thread(const char *cgroup)
307 return cg_write(cgroup, "cgroup.threads", "0");
310 int cg_run(const char *cgroup,
311 int (*fn)(const char *cgroup, void *arg),
312 void *arg)
314 int pid, retcode;
316 pid = fork();
317 if (pid < 0) {
318 return pid;
319 } else if (pid == 0) {
320 char buf[64];
322 snprintf(buf, sizeof(buf), "%d", getpid());
323 if (cg_write(cgroup, "cgroup.procs", buf))
324 exit(EXIT_FAILURE);
325 exit(fn(cgroup, arg));
326 } else {
327 waitpid(pid, &retcode, 0);
328 if (WIFEXITED(retcode))
329 return WEXITSTATUS(retcode);
330 else
331 return -1;
335 pid_t clone_into_cgroup(int cgroup_fd)
337 #ifdef CLONE_ARGS_SIZE_VER2
338 pid_t pid;
340 struct __clone_args args = {
341 .flags = CLONE_INTO_CGROUP,
342 .exit_signal = SIGCHLD,
343 .cgroup = cgroup_fd,
346 pid = sys_clone3(&args, sizeof(struct __clone_args));
348 * Verify that this is a genuine test failure:
349 * ENOSYS -> clone3() not available
350 * E2BIG -> CLONE_INTO_CGROUP not available
352 if (pid < 0 && (errno == ENOSYS || errno == E2BIG))
353 goto pretend_enosys;
355 return pid;
357 pretend_enosys:
358 #endif
359 errno = ENOSYS;
360 return -ENOSYS;
363 int clone_reap(pid_t pid, int options)
365 int ret;
366 siginfo_t info = {
367 .si_signo = 0,
370 again:
371 ret = waitid(P_PID, pid, &info, options | __WALL | __WNOTHREAD);
372 if (ret < 0) {
373 if (errno == EINTR)
374 goto again;
375 return -1;
378 if (options & WEXITED) {
379 if (WIFEXITED(info.si_status))
380 return WEXITSTATUS(info.si_status);
383 if (options & WSTOPPED) {
384 if (WIFSTOPPED(info.si_status))
385 return WSTOPSIG(info.si_status);
388 if (options & WCONTINUED) {
389 if (WIFCONTINUED(info.si_status))
390 return 0;
393 return -1;
396 int dirfd_open_opath(const char *dir)
398 return open(dir, O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW | O_PATH);
401 #define close_prot_errno(fd) \
402 if (fd >= 0) { \
403 int _e_ = errno; \
404 close(fd); \
405 errno = _e_; \
408 static int clone_into_cgroup_run_nowait(const char *cgroup,
409 int (*fn)(const char *cgroup, void *arg),
410 void *arg)
412 int cgroup_fd;
413 pid_t pid;
415 cgroup_fd = dirfd_open_opath(cgroup);
416 if (cgroup_fd < 0)
417 return -1;
419 pid = clone_into_cgroup(cgroup_fd);
420 close_prot_errno(cgroup_fd);
421 if (pid == 0)
422 exit(fn(cgroup, arg));
424 return pid;
427 int cg_run_nowait(const char *cgroup,
428 int (*fn)(const char *cgroup, void *arg),
429 void *arg)
431 int pid;
433 pid = clone_into_cgroup_run_nowait(cgroup, fn, arg);
434 if (pid > 0)
435 return pid;
437 /* Genuine test failure. */
438 if (pid < 0 && errno != ENOSYS)
439 return -1;
441 pid = fork();
442 if (pid == 0) {
443 char buf[64];
445 snprintf(buf, sizeof(buf), "%d", getpid());
446 if (cg_write(cgroup, "cgroup.procs", buf))
447 exit(EXIT_FAILURE);
448 exit(fn(cgroup, arg));
451 return pid;
454 int get_temp_fd(void)
456 return open(".", O_TMPFILE | O_RDWR | O_EXCL);
459 int alloc_pagecache(int fd, size_t size)
461 char buf[PAGE_SIZE];
462 struct stat st;
463 int i;
465 if (fstat(fd, &st))
466 goto cleanup;
468 size += st.st_size;
470 if (ftruncate(fd, size))
471 goto cleanup;
473 for (i = 0; i < size; i += sizeof(buf))
474 read(fd, buf, sizeof(buf));
476 return 0;
478 cleanup:
479 return -1;
482 int alloc_anon(const char *cgroup, void *arg)
484 size_t size = (unsigned long)arg;
485 char *buf, *ptr;
487 buf = malloc(size);
488 for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
489 *ptr = 0;
491 free(buf);
492 return 0;
495 int is_swap_enabled(void)
497 char buf[PAGE_SIZE];
498 const char delim[] = "\n";
499 int cnt = 0;
500 char *line;
502 if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
503 return -1;
505 for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
506 cnt++;
508 return cnt > 1;
511 int set_oom_adj_score(int pid, int score)
513 char path[PATH_MAX];
514 int fd, len;
516 sprintf(path, "/proc/%d/oom_score_adj", pid);
518 fd = open(path, O_WRONLY | O_APPEND);
519 if (fd < 0)
520 return fd;
522 len = dprintf(fd, "%d", score);
523 if (len < 0) {
524 close(fd);
525 return len;
528 close(fd);
529 return 0;
532 ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t size)
534 char path[PATH_MAX];
536 if (!pid)
537 snprintf(path, sizeof(path), "/proc/%s/%s",
538 thread ? "thread-self" : "self", item);
539 else
540 snprintf(path, sizeof(path), "/proc/%d/%s", pid, item);
542 return read_text(path, buf, size);
545 int proc_read_strstr(int pid, bool thread, const char *item, const char *needle)
547 char buf[PAGE_SIZE];
549 if (proc_read_text(pid, thread, item, buf, sizeof(buf)) < 0)
550 return -1;
552 return strstr(buf, needle) ? 0 : -1;
555 int clone_into_cgroup_run_wait(const char *cgroup)
557 int cgroup_fd;
558 pid_t pid;
560 cgroup_fd = dirfd_open_opath(cgroup);
561 if (cgroup_fd < 0)
562 return -1;
564 pid = clone_into_cgroup(cgroup_fd);
565 close_prot_errno(cgroup_fd);
566 if (pid < 0)
567 return -1;
569 if (pid == 0)
570 exit(EXIT_SUCCESS);
573 * We don't care whether this fails. We only care whether the initial
574 * clone succeeded.
576 (void)clone_reap(pid, WEXITED);
577 return 0;