Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / cgroup / cgroup_util.c
blob075cb0c730149907414f0aa7b1f2957d7e8361e9
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"
19 static ssize_t read_text(const char *path, char *buf, size_t max_len)
21 ssize_t len;
22 int fd;
24 fd = open(path, O_RDONLY);
25 if (fd < 0)
26 return fd;
28 len = read(fd, buf, max_len - 1);
29 if (len < 0)
30 goto out;
32 buf[len] = 0;
33 out:
34 close(fd);
35 return len;
38 static ssize_t write_text(const char *path, char *buf, ssize_t len)
40 int fd;
42 fd = open(path, O_WRONLY | O_APPEND);
43 if (fd < 0)
44 return fd;
46 len = write(fd, buf, len);
47 if (len < 0) {
48 close(fd);
49 return len;
52 close(fd);
54 return len;
57 char *cg_name(const char *root, const char *name)
59 size_t len = strlen(root) + strlen(name) + 2;
60 char *ret = malloc(len);
62 snprintf(ret, len, "%s/%s", root, name);
64 return ret;
67 char *cg_name_indexed(const char *root, const char *name, int index)
69 size_t len = strlen(root) + strlen(name) + 10;
70 char *ret = malloc(len);
72 snprintf(ret, len, "%s/%s_%d", root, name, index);
74 return ret;
77 int cg_read(const char *cgroup, const char *control, char *buf, size_t len)
79 char path[PATH_MAX];
81 snprintf(path, sizeof(path), "%s/%s", cgroup, control);
83 if (read_text(path, buf, len) >= 0)
84 return 0;
86 return -1;
89 int cg_read_strcmp(const char *cgroup, const char *control,
90 const char *expected)
92 size_t size;
93 char *buf;
94 int ret;
96 /* Handle the case of comparing against empty string */
97 if (!expected)
98 size = 32;
99 else
100 size = strlen(expected) + 1;
102 buf = malloc(size);
103 if (!buf)
104 return -1;
106 if (cg_read(cgroup, control, buf, size)) {
107 free(buf);
108 return -1;
111 ret = strcmp(expected, buf);
112 free(buf);
113 return ret;
116 int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
118 char buf[PAGE_SIZE];
120 if (cg_read(cgroup, control, buf, sizeof(buf)))
121 return -1;
123 return strstr(buf, needle) ? 0 : -1;
126 long cg_read_long(const char *cgroup, const char *control)
128 char buf[128];
130 if (cg_read(cgroup, control, buf, sizeof(buf)))
131 return -1;
133 return atol(buf);
136 long cg_read_key_long(const char *cgroup, const char *control, const char *key)
138 char buf[PAGE_SIZE];
139 char *ptr;
141 if (cg_read(cgroup, control, buf, sizeof(buf)))
142 return -1;
144 ptr = strstr(buf, key);
145 if (!ptr)
146 return -1;
148 return atol(ptr + strlen(key));
151 int cg_write(const char *cgroup, const char *control, char *buf)
153 char path[PATH_MAX];
154 ssize_t len = strlen(buf);
156 snprintf(path, sizeof(path), "%s/%s", cgroup, control);
158 if (write_text(path, buf, len) == len)
159 return 0;
161 return -1;
164 int cg_find_unified_root(char *root, size_t len)
166 char buf[10 * PAGE_SIZE];
167 char *fs, *mount, *type;
168 const char delim[] = "\n\t ";
170 if (read_text("/proc/self/mounts", buf, sizeof(buf)) <= 0)
171 return -1;
174 * Example:
175 * cgroup /sys/fs/cgroup cgroup2 rw,seclabel,noexec,relatime 0 0
177 for (fs = strtok(buf, delim); fs; fs = strtok(NULL, delim)) {
178 mount = strtok(NULL, delim);
179 type = strtok(NULL, delim);
180 strtok(NULL, delim);
181 strtok(NULL, delim);
182 strtok(NULL, delim);
184 if (strcmp(type, "cgroup2") == 0) {
185 strncpy(root, mount, len);
186 return 0;
190 return -1;
193 int cg_create(const char *cgroup)
195 return mkdir(cgroup, 0644);
198 static int cg_killall(const char *cgroup)
200 char buf[PAGE_SIZE];
201 char *ptr = buf;
203 if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
204 return -1;
206 while (ptr < buf + sizeof(buf)) {
207 int pid = strtol(ptr, &ptr, 10);
209 if (pid == 0)
210 break;
211 if (*ptr)
212 ptr++;
213 else
214 break;
215 if (kill(pid, SIGKILL))
216 return -1;
219 return 0;
222 int cg_destroy(const char *cgroup)
224 int ret;
226 retry:
227 ret = rmdir(cgroup);
228 if (ret && errno == EBUSY) {
229 ret = cg_killall(cgroup);
230 if (ret)
231 return ret;
232 usleep(100);
233 goto retry;
236 if (ret && errno == ENOENT)
237 ret = 0;
239 return ret;
242 int cg_enter_current(const char *cgroup)
244 char pidbuf[64];
246 snprintf(pidbuf, sizeof(pidbuf), "%d", getpid());
247 return cg_write(cgroup, "cgroup.procs", pidbuf);
250 int cg_run(const char *cgroup,
251 int (*fn)(const char *cgroup, void *arg),
252 void *arg)
254 int pid, retcode;
256 pid = fork();
257 if (pid < 0) {
258 return pid;
259 } else if (pid == 0) {
260 char buf[64];
262 snprintf(buf, sizeof(buf), "%d", getpid());
263 if (cg_write(cgroup, "cgroup.procs", buf))
264 exit(EXIT_FAILURE);
265 exit(fn(cgroup, arg));
266 } else {
267 waitpid(pid, &retcode, 0);
268 if (WIFEXITED(retcode))
269 return WEXITSTATUS(retcode);
270 else
271 return -1;
275 int cg_run_nowait(const char *cgroup,
276 int (*fn)(const char *cgroup, void *arg),
277 void *arg)
279 int pid;
281 pid = fork();
282 if (pid == 0) {
283 char buf[64];
285 snprintf(buf, sizeof(buf), "%d", getpid());
286 if (cg_write(cgroup, "cgroup.procs", buf))
287 exit(EXIT_FAILURE);
288 exit(fn(cgroup, arg));
291 return pid;
294 int get_temp_fd(void)
296 return open(".", O_TMPFILE | O_RDWR | O_EXCL);
299 int alloc_pagecache(int fd, size_t size)
301 char buf[PAGE_SIZE];
302 struct stat st;
303 int i;
305 if (fstat(fd, &st))
306 goto cleanup;
308 size += st.st_size;
310 if (ftruncate(fd, size))
311 goto cleanup;
313 for (i = 0; i < size; i += sizeof(buf))
314 read(fd, buf, sizeof(buf));
316 return 0;
318 cleanup:
319 return -1;
322 int alloc_anon(const char *cgroup, void *arg)
324 size_t size = (unsigned long)arg;
325 char *buf, *ptr;
327 buf = malloc(size);
328 for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
329 *ptr = 0;
331 free(buf);
332 return 0;
335 int is_swap_enabled(void)
337 char buf[PAGE_SIZE];
338 const char delim[] = "\n";
339 int cnt = 0;
340 char *line;
342 if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
343 return -1;
345 for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
346 cnt++;
348 return cnt > 1;
351 int set_oom_adj_score(int pid, int score)
353 char path[PATH_MAX];
354 int fd, len;
356 sprintf(path, "/proc/%d/oom_score_adj", pid);
358 fd = open(path, O_WRONLY | O_APPEND);
359 if (fd < 0)
360 return fd;
362 len = dprintf(fd, "%d", score);
363 if (len < 0) {
364 close(fd);
365 return len;
368 close(fd);
369 return 0;