1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 #define _GNU_SOURCE
4
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>
16
17 #include "cgroup_util.h"
18
read_text(const char * path,char * buf,size_t max_len)19 static ssize_t read_text(const char *path, char *buf, size_t max_len)
20 {
21 ssize_t len;
22 int fd;
23
24 fd = open(path, O_RDONLY);
25 if (fd < 0)
26 return fd;
27
28 len = read(fd, buf, max_len - 1);
29 if (len < 0)
30 goto out;
31
32 buf[len] = 0;
33 out:
34 close(fd);
35 return len;
36 }
37
write_text(const char * path,char * buf,ssize_t len)38 static ssize_t write_text(const char *path, char *buf, ssize_t len)
39 {
40 int fd;
41
42 fd = open(path, O_WRONLY | O_APPEND);
43 if (fd < 0)
44 return fd;
45
46 len = write(fd, buf, len);
47 if (len < 0) {
48 close(fd);
49 return len;
50 }
51
52 close(fd);
53
54 return len;
55 }
56
cg_name(const char * root,const char * name)57 char *cg_name(const char *root, const char *name)
58 {
59 size_t len = strlen(root) + strlen(name) + 2;
60 char *ret = malloc(len);
61
62 snprintf(ret, len, "%s/%s", root, name);
63
64 return ret;
65 }
66
cg_name_indexed(const char * root,const char * name,int index)67 char *cg_name_indexed(const char *root, const char *name, int index)
68 {
69 size_t len = strlen(root) + strlen(name) + 10;
70 char *ret = malloc(len);
71
72 snprintf(ret, len, "%s/%s_%d", root, name, index);
73
74 return ret;
75 }
76
cg_control(const char * cgroup,const char * control)77 char *cg_control(const char *cgroup, const char *control)
78 {
79 size_t len = strlen(cgroup) + strlen(control) + 2;
80 char *ret = malloc(len);
81
82 snprintf(ret, len, "%s/%s", cgroup, control);
83
84 return ret;
85 }
86
cg_read(const char * cgroup,const char * control,char * buf,size_t len)87 int cg_read(const char *cgroup, const char *control, char *buf, size_t len)
88 {
89 char path[PATH_MAX];
90
91 snprintf(path, sizeof(path), "%s/%s", cgroup, control);
92
93 if (read_text(path, buf, len) >= 0)
94 return 0;
95
96 return -1;
97 }
98
cg_read_strcmp(const char * cgroup,const char * control,const char * expected)99 int cg_read_strcmp(const char *cgroup, const char *control,
100 const char *expected)
101 {
102 size_t size;
103 char *buf;
104 int ret;
105
106 /* Handle the case of comparing against empty string */
107 if (!expected)
108 size = 32;
109 else
110 size = strlen(expected) + 1;
111
112 buf = malloc(size);
113 if (!buf)
114 return -1;
115
116 if (cg_read(cgroup, control, buf, size)) {
117 free(buf);
118 return -1;
119 }
120
121 ret = strcmp(expected, buf);
122 free(buf);
123 return ret;
124 }
125
cg_read_strstr(const char * cgroup,const char * control,const char * needle)126 int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
127 {
128 char buf[PAGE_SIZE];
129
130 if (cg_read(cgroup, control, buf, sizeof(buf)))
131 return -1;
132
133 return strstr(buf, needle) ? 0 : -1;
134 }
135
cg_read_long(const char * cgroup,const char * control)136 long cg_read_long(const char *cgroup, const char *control)
137 {
138 char buf[128];
139
140 if (cg_read(cgroup, control, buf, sizeof(buf)))
141 return -1;
142
143 return atol(buf);
144 }
145
cg_read_key_long(const char * cgroup,const char * control,const char * key)146 long cg_read_key_long(const char *cgroup, const char *control, const char *key)
147 {
148 char buf[PAGE_SIZE];
149 char *ptr;
150
151 if (cg_read(cgroup, control, buf, sizeof(buf)))
152 return -1;
153
154 ptr = strstr(buf, key);
155 if (!ptr)
156 return -1;
157
158 return atol(ptr + strlen(key));
159 }
160
cg_read_lc(const char * cgroup,const char * control)161 long cg_read_lc(const char *cgroup, const char *control)
162 {
163 char buf[PAGE_SIZE];
164 const char delim[] = "\n";
165 char *line;
166 long cnt = 0;
167
168 if (cg_read(cgroup, control, buf, sizeof(buf)))
169 return -1;
170
171 for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
172 cnt++;
173
174 return cnt;
175 }
176
cg_write(const char * cgroup,const char * control,char * buf)177 int cg_write(const char *cgroup, const char *control, char *buf)
178 {
179 char path[PATH_MAX];
180 ssize_t len = strlen(buf);
181
182 snprintf(path, sizeof(path), "%s/%s", cgroup, control);
183
184 if (write_text(path, buf, len) == len)
185 return 0;
186
187 return -1;
188 }
189
cg_find_unified_root(char * root,size_t len)190 int cg_find_unified_root(char *root, size_t len)
191 {
192 char buf[10 * PAGE_SIZE];
193 char *fs, *mount, *type;
194 const char delim[] = "\n\t ";
195
196 if (read_text("/proc/self/mounts", buf, sizeof(buf)) <= 0)
197 return -1;
198
199 /*
200 * Example:
201 * cgroup /sys/fs/cgroup cgroup2 rw,seclabel,noexec,relatime 0 0
202 */
203 for (fs = strtok(buf, delim); fs; fs = strtok(NULL, delim)) {
204 mount = strtok(NULL, delim);
205 type = strtok(NULL, delim);
206 strtok(NULL, delim);
207 strtok(NULL, delim);
208 strtok(NULL, delim);
209
210 if (strcmp(type, "cgroup2") == 0) {
211 strncpy(root, mount, len);
212 return 0;
213 }
214 }
215
216 return -1;
217 }
218
cg_create(const char * cgroup)219 int cg_create(const char *cgroup)
220 {
221 return mkdir(cgroup, 0644);
222 }
223
cg_wait_for_proc_count(const char * cgroup,int count)224 int cg_wait_for_proc_count(const char *cgroup, int count)
225 {
226 char buf[10 * PAGE_SIZE] = {0};
227 int attempts;
228 char *ptr;
229
230 for (attempts = 10; attempts >= 0; attempts--) {
231 int nr = 0;
232
233 if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
234 break;
235
236 for (ptr = buf; *ptr; ptr++)
237 if (*ptr == '\n')
238 nr++;
239
240 if (nr >= count)
241 return 0;
242
243 usleep(100000);
244 }
245
246 return -1;
247 }
248
cg_killall(const char * cgroup)249 int cg_killall(const char *cgroup)
250 {
251 char buf[PAGE_SIZE];
252 char *ptr = buf;
253
254 if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
255 return -1;
256
257 while (ptr < buf + sizeof(buf)) {
258 int pid = strtol(ptr, &ptr, 10);
259
260 if (pid == 0)
261 break;
262 if (*ptr)
263 ptr++;
264 else
265 break;
266 if (kill(pid, SIGKILL))
267 return -1;
268 }
269
270 return 0;
271 }
272
cg_destroy(const char * cgroup)273 int cg_destroy(const char *cgroup)
274 {
275 int ret;
276
277 retry:
278 ret = rmdir(cgroup);
279 if (ret && errno == EBUSY) {
280 cg_killall(cgroup);
281 usleep(100);
282 goto retry;
283 }
284
285 if (ret && errno == ENOENT)
286 ret = 0;
287
288 return ret;
289 }
290
cg_enter(const char * cgroup,int pid)291 int cg_enter(const char *cgroup, int pid)
292 {
293 char pidbuf[64];
294
295 snprintf(pidbuf, sizeof(pidbuf), "%d", pid);
296 return cg_write(cgroup, "cgroup.procs", pidbuf);
297 }
298
cg_enter_current(const char * cgroup)299 int cg_enter_current(const char *cgroup)
300 {
301 return cg_write(cgroup, "cgroup.procs", "0");
302 }
303
cg_enter_current_thread(const char * cgroup)304 int cg_enter_current_thread(const char *cgroup)
305 {
306 return cg_write(cgroup, "cgroup.threads", "0");
307 }
308
cg_run(const char * cgroup,int (* fn)(const char * cgroup,void * arg),void * arg)309 int cg_run(const char *cgroup,
310 int (*fn)(const char *cgroup, void *arg),
311 void *arg)
312 {
313 int pid, retcode;
314
315 pid = fork();
316 if (pid < 0) {
317 return pid;
318 } else if (pid == 0) {
319 char buf[64];
320
321 snprintf(buf, sizeof(buf), "%d", getpid());
322 if (cg_write(cgroup, "cgroup.procs", buf))
323 exit(EXIT_FAILURE);
324 exit(fn(cgroup, arg));
325 } else {
326 waitpid(pid, &retcode, 0);
327 if (WIFEXITED(retcode))
328 return WEXITSTATUS(retcode);
329 else
330 return -1;
331 }
332 }
333
cg_run_nowait(const char * cgroup,int (* fn)(const char * cgroup,void * arg),void * arg)334 int cg_run_nowait(const char *cgroup,
335 int (*fn)(const char *cgroup, void *arg),
336 void *arg)
337 {
338 int pid;
339
340 pid = fork();
341 if (pid == 0) {
342 char buf[64];
343
344 snprintf(buf, sizeof(buf), "%d", getpid());
345 if (cg_write(cgroup, "cgroup.procs", buf))
346 exit(EXIT_FAILURE);
347 exit(fn(cgroup, arg));
348 }
349
350 return pid;
351 }
352
get_temp_fd(void)353 int get_temp_fd(void)
354 {
355 return open(".", O_TMPFILE | O_RDWR | O_EXCL);
356 }
357
alloc_pagecache(int fd,size_t size)358 int alloc_pagecache(int fd, size_t size)
359 {
360 char buf[PAGE_SIZE];
361 struct stat st;
362 int i;
363
364 if (fstat(fd, &st))
365 goto cleanup;
366
367 size += st.st_size;
368
369 if (ftruncate(fd, size))
370 goto cleanup;
371
372 for (i = 0; i < size; i += sizeof(buf))
373 read(fd, buf, sizeof(buf));
374
375 return 0;
376
377 cleanup:
378 return -1;
379 }
380
alloc_anon(const char * cgroup,void * arg)381 int alloc_anon(const char *cgroup, void *arg)
382 {
383 size_t size = (unsigned long)arg;
384 char *buf, *ptr;
385
386 buf = malloc(size);
387 for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
388 *ptr = 0;
389
390 free(buf);
391 return 0;
392 }
393
is_swap_enabled(void)394 int is_swap_enabled(void)
395 {
396 char buf[PAGE_SIZE];
397 const char delim[] = "\n";
398 int cnt = 0;
399 char *line;
400
401 if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
402 return -1;
403
404 for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
405 cnt++;
406
407 return cnt > 1;
408 }
409
set_oom_adj_score(int pid,int score)410 int set_oom_adj_score(int pid, int score)
411 {
412 char path[PATH_MAX];
413 int fd, len;
414
415 sprintf(path, "/proc/%d/oom_score_adj", pid);
416
417 fd = open(path, O_WRONLY | O_APPEND);
418 if (fd < 0)
419 return fd;
420
421 len = dprintf(fd, "%d", score);
422 if (len < 0) {
423 close(fd);
424 return len;
425 }
426
427 close(fd);
428 return 0;
429 }
430
proc_read_text(int pid,bool thread,const char * item,char * buf,size_t size)431 ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t size)
432 {
433 char path[PATH_MAX];
434
435 if (!pid)
436 snprintf(path, sizeof(path), "/proc/%s/%s",
437 thread ? "thread-self" : "self", item);
438 else
439 snprintf(path, sizeof(path), "/proc/%d/%s", pid, item);
440
441 return read_text(path, buf, size);
442 }
443
proc_read_strstr(int pid,bool thread,const char * item,const char * needle)444 int proc_read_strstr(int pid, bool thread, const char *item, const char *needle)
445 {
446 char buf[PAGE_SIZE];
447
448 if (proc_read_text(pid, thread, item, buf, sizeof(buf)) < 0)
449 return -1;
450
451 return strstr(buf, needle) ? 0 : -1;
452 }
453