blob: 8f7131dcf1ff545afc7b40e152e528d03a7b1154 [file] [log] [blame]
Roman Gushchin84092dbc2018-05-11 19:03:49 +01001/* 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
19static 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;
33out:
34 close(fd);
35 return len;
36}
37
Dan Carpenter53c3daf2018-06-07 11:30:02 +030038static ssize_t write_text(const char *path, char *buf, ssize_t len)
Roman Gushchin84092dbc2018-05-11 19:03:49 +010039{
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
57char *cg_name(const char *root, const char *name)
58{
59 size_t len = strlen(root) + strlen(name) + 2;
60 char *ret = malloc(len);
61
Roman Gushchinadb31be2018-05-22 11:10:31 +010062 snprintf(ret, len, "%s/%s", root, name);
Roman Gushchin84092dbc2018-05-11 19:03:49 +010063
64 return ret;
65}
66
67char *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
Roman Gushchinadb31be2018-05-22 11:10:31 +010072 snprintf(ret, len, "%s/%s_%d", root, name, index);
Roman Gushchin84092dbc2018-05-11 19:03:49 +010073
74 return ret;
75}
76
Roman Gushchin5313bfe2019-04-19 10:03:06 -070077char *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
Roman Gushchin84092dbc2018-05-11 19:03:49 +010087int 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
99int cg_read_strcmp(const char *cgroup, const char *control,
100 const char *expected)
101{
Jay Kamat48c2bb02018-09-07 14:34:04 -0700102 size_t size;
Roman Gushchin84092dbc2018-05-11 19:03:49 +0100103 char *buf;
Jay Kamat48c2bb02018-09-07 14:34:04 -0700104 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;
Roman Gushchin84092dbc2018-05-11 19:03:49 +0100111
112 buf = malloc(size);
113 if (!buf)
114 return -1;
115
Jay Kamat48c2bb02018-09-07 14:34:04 -0700116 if (cg_read(cgroup, control, buf, size)) {
117 free(buf);
Roman Gushchin84092dbc2018-05-11 19:03:49 +0100118 return -1;
Jay Kamat48c2bb02018-09-07 14:34:04 -0700119 }
Roman Gushchin84092dbc2018-05-11 19:03:49 +0100120
Jay Kamat48c2bb02018-09-07 14:34:04 -0700121 ret = strcmp(expected, buf);
122 free(buf);
123 return ret;
Roman Gushchin84092dbc2018-05-11 19:03:49 +0100124}
125
126int 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
136long 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
146long 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
Michal Koutný11318982019-10-04 12:57:42 +0200161long 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
Roman Gushchin84092dbc2018-05-11 19:03:49 +0100177int cg_write(const char *cgroup, const char *control, char *buf)
178{
179 char path[PATH_MAX];
Dan Carpenter53c3daf2018-06-07 11:30:02 +0300180 ssize_t len = strlen(buf);
Roman Gushchin84092dbc2018-05-11 19:03:49 +0100181
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
190int 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
Chris Downb59b1ba2019-08-02 21:49:15 -0700210 if (strcmp(type, "cgroup2") == 0) {
Roman Gushchin84092dbc2018-05-11 19:03:49 +0100211 strncpy(root, mount, len);
212 return 0;
213 }
214 }
215
216 return -1;
217}
218
219int cg_create(const char *cgroup)
220{
221 return mkdir(cgroup, 0644);
222}
223
Roman Gushchin5313bfe2019-04-19 10:03:06 -0700224int 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
249int cg_killall(const char *cgroup)
Roman Gushchin84092dbc2018-05-11 19:03:49 +0100250{
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
273int cg_destroy(const char *cgroup)
274{
275 int ret;
276
277retry:
278 ret = rmdir(cgroup);
279 if (ret && errno == EBUSY) {
Roman Gushchinff9fb7cb2019-04-19 10:03:05 -0700280 cg_killall(cgroup);
Roman Gushchin84092dbc2018-05-11 19:03:49 +0100281 usleep(100);
282 goto retry;
283 }
284
285 if (ret && errno == ENOENT)
286 ret = 0;
287
288 return ret;
289}
290
Roman Gushchin5313bfe2019-04-19 10:03:06 -0700291int 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
Claudiod863cb03fc2018-07-18 19:33:58 +0200299int cg_enter_current(const char *cgroup)
300{
Michal Koutný58c9f752019-10-04 12:57:41 +0200301 return cg_write(cgroup, "cgroup.procs", "0");
302}
Claudiod863cb03fc2018-07-18 19:33:58 +0200303
Michal Koutný58c9f752019-10-04 12:57:41 +0200304int cg_enter_current_thread(const char *cgroup)
305{
306 return cg_write(cgroup, "cgroup.threads", "0");
Claudiod863cb03fc2018-07-18 19:33:58 +0200307}
308
Roman Gushchin84092dbc2018-05-11 19:03:49 +0100309int 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
334int 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
353int get_temp_fd(void)
354{
355 return open(".", O_TMPFILE | O_RDWR | O_EXCL);
356}
357
358int 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
377cleanup:
378 return -1;
379}
380
381int 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}
Mike Rapoport478b2782018-05-15 19:05:53 +0300393
394int 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}
Jay Kamata9877852018-09-07 14:34:05 -0700409
410int 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}
Roman Gushchin5313bfe2019-04-19 10:03:06 -0700430
Michal Koutný58c9f752019-10-04 12:57:41 +0200431ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t size)
Roman Gushchin5313bfe2019-04-19 10:03:06 -0700432{
433 char path[PATH_MAX];
434
Michal Koutný58c9f752019-10-04 12:57:41 +0200435 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);
Roman Gushchin5313bfe2019-04-19 10:03:06 -0700440
441 return read_text(path, buf, size);
442}
Michal Koutný11318982019-10-04 12:57:42 +0200443
444int 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}