]>
Commit | Line | Data |
---|---|---|
1 | /*** | |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include <dirent.h> | |
21 | #include <errno.h> | |
22 | #include <ftw.h> | |
23 | #include <limits.h> | |
24 | #include <signal.h> | |
25 | #include <stddef.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <sys/stat.h> | |
29 | #include <sys/statfs.h> | |
30 | #include <sys/types.h> | |
31 | #include <sys/xattr.h> | |
32 | #include <unistd.h> | |
33 | ||
34 | #include "alloc-util.h" | |
35 | #include "cgroup-util.h" | |
36 | #include "def.h" | |
37 | #include "dirent-util.h" | |
38 | #include "extract-word.h" | |
39 | #include "fd-util.h" | |
40 | #include "fileio.h" | |
41 | #include "format-util.h" | |
42 | #include "fs-util.h" | |
43 | #include "log.h" | |
44 | #include "login-util.h" | |
45 | #include "macro.h" | |
46 | #include "missing.h" | |
47 | #include "mkdir.h" | |
48 | #include "parse-util.h" | |
49 | #include "path-util.h" | |
50 | #include "proc-cmdline.h" | |
51 | #include "process-util.h" | |
52 | #include "set.h" | |
53 | #include "special.h" | |
54 | #include "stat-util.h" | |
55 | #include "stdio-util.h" | |
56 | #include "string-table.h" | |
57 | #include "string-util.h" | |
58 | #include "strv.h" | |
59 | #include "unit-name.h" | |
60 | #include "user-util.h" | |
61 | ||
62 | int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) { | |
63 | _cleanup_free_ char *fs = NULL; | |
64 | FILE *f; | |
65 | int r; | |
66 | ||
67 | assert(_f); | |
68 | ||
69 | r = cg_get_path(controller, path, "cgroup.procs", &fs); | |
70 | if (r < 0) | |
71 | return r; | |
72 | ||
73 | f = fopen(fs, "re"); | |
74 | if (!f) | |
75 | return -errno; | |
76 | ||
77 | *_f = f; | |
78 | return 0; | |
79 | } | |
80 | ||
81 | int cg_read_pid(FILE *f, pid_t *_pid) { | |
82 | unsigned long ul; | |
83 | ||
84 | /* Note that the cgroup.procs might contain duplicates! See | |
85 | * cgroups.txt for details. */ | |
86 | ||
87 | assert(f); | |
88 | assert(_pid); | |
89 | ||
90 | errno = 0; | |
91 | if (fscanf(f, "%lu", &ul) != 1) { | |
92 | ||
93 | if (feof(f)) | |
94 | return 0; | |
95 | ||
96 | return errno > 0 ? -errno : -EIO; | |
97 | } | |
98 | ||
99 | if (ul <= 0) | |
100 | return -EIO; | |
101 | ||
102 | *_pid = (pid_t) ul; | |
103 | return 1; | |
104 | } | |
105 | ||
106 | int cg_read_event( | |
107 | const char *controller, | |
108 | const char *path, | |
109 | const char *event, | |
110 | char **val) { | |
111 | ||
112 | _cleanup_free_ char *events = NULL, *content = NULL; | |
113 | char *p, *line; | |
114 | int r; | |
115 | ||
116 | r = cg_get_path(controller, path, "cgroup.events", &events); | |
117 | if (r < 0) | |
118 | return r; | |
119 | ||
120 | r = read_full_file(events, &content, NULL); | |
121 | if (r < 0) | |
122 | return r; | |
123 | ||
124 | p = content; | |
125 | while ((line = strsep(&p, "\n"))) { | |
126 | char *key; | |
127 | ||
128 | key = strsep(&line, " "); | |
129 | if (!key || !line) | |
130 | return -EINVAL; | |
131 | ||
132 | if (strcmp(key, event)) | |
133 | continue; | |
134 | ||
135 | *val = strdup(line); | |
136 | return 0; | |
137 | } | |
138 | ||
139 | return -ENOENT; | |
140 | } | |
141 | ||
142 | bool cg_ns_supported(void) { | |
143 | static thread_local int enabled = -1; | |
144 | ||
145 | if (enabled >= 0) | |
146 | return enabled; | |
147 | ||
148 | if (access("/proc/self/ns/cgroup", F_OK) == 0) | |
149 | enabled = 1; | |
150 | else | |
151 | enabled = 0; | |
152 | ||
153 | return enabled; | |
154 | } | |
155 | ||
156 | int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) { | |
157 | _cleanup_free_ char *fs = NULL; | |
158 | int r; | |
159 | DIR *d; | |
160 | ||
161 | assert(_d); | |
162 | ||
163 | /* This is not recursive! */ | |
164 | ||
165 | r = cg_get_path(controller, path, NULL, &fs); | |
166 | if (r < 0) | |
167 | return r; | |
168 | ||
169 | d = opendir(fs); | |
170 | if (!d) | |
171 | return -errno; | |
172 | ||
173 | *_d = d; | |
174 | return 0; | |
175 | } | |
176 | ||
177 | int cg_read_subgroup(DIR *d, char **fn) { | |
178 | struct dirent *de; | |
179 | ||
180 | assert(d); | |
181 | assert(fn); | |
182 | ||
183 | FOREACH_DIRENT_ALL(de, d, return -errno) { | |
184 | char *b; | |
185 | ||
186 | if (de->d_type != DT_DIR) | |
187 | continue; | |
188 | ||
189 | if (dot_or_dot_dot(de->d_name)) | |
190 | continue; | |
191 | ||
192 | b = strdup(de->d_name); | |
193 | if (!b) | |
194 | return -ENOMEM; | |
195 | ||
196 | *fn = b; | |
197 | return 1; | |
198 | } | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
203 | int cg_rmdir(const char *controller, const char *path) { | |
204 | _cleanup_free_ char *p = NULL; | |
205 | int r; | |
206 | ||
207 | r = cg_get_path(controller, path, NULL, &p); | |
208 | if (r < 0) | |
209 | return r; | |
210 | ||
211 | r = rmdir(p); | |
212 | if (r < 0 && errno != ENOENT) | |
213 | return -errno; | |
214 | ||
215 | r = cg_hybrid_unified(); | |
216 | if (r < 0) | |
217 | return r; | |
218 | if (r == 0) | |
219 | return 0; | |
220 | ||
221 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) { | |
222 | r = cg_rmdir(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path); | |
223 | if (r < 0) | |
224 | log_warning_errno(r, "Failed to remove compat systemd cgroup %s: %m", path); | |
225 | } | |
226 | ||
227 | return 0; | |
228 | } | |
229 | ||
230 | int cg_kill( | |
231 | const char *controller, | |
232 | const char *path, | |
233 | int sig, | |
234 | CGroupFlags flags, | |
235 | Set *s, | |
236 | cg_kill_log_func_t log_kill, | |
237 | void *userdata) { | |
238 | ||
239 | _cleanup_set_free_ Set *allocated_set = NULL; | |
240 | bool done = false; | |
241 | int r, ret = 0; | |
242 | pid_t my_pid; | |
243 | ||
244 | assert(sig >= 0); | |
245 | ||
246 | /* Don't send SIGCONT twice. Also, SIGKILL always works even when process is suspended, hence don't send | |
247 | * SIGCONT on SIGKILL. */ | |
248 | if (IN_SET(sig, SIGCONT, SIGKILL)) | |
249 | flags &= ~CGROUP_SIGCONT; | |
250 | ||
251 | /* This goes through the tasks list and kills them all. This | |
252 | * is repeated until no further processes are added to the | |
253 | * tasks list, to properly handle forking processes */ | |
254 | ||
255 | if (!s) { | |
256 | s = allocated_set = set_new(NULL); | |
257 | if (!s) | |
258 | return -ENOMEM; | |
259 | } | |
260 | ||
261 | my_pid = getpid_cached(); | |
262 | ||
263 | do { | |
264 | _cleanup_fclose_ FILE *f = NULL; | |
265 | pid_t pid = 0; | |
266 | done = true; | |
267 | ||
268 | r = cg_enumerate_processes(controller, path, &f); | |
269 | if (r < 0) { | |
270 | if (ret >= 0 && r != -ENOENT) | |
271 | return r; | |
272 | ||
273 | return ret; | |
274 | } | |
275 | ||
276 | while ((r = cg_read_pid(f, &pid)) > 0) { | |
277 | ||
278 | if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid) | |
279 | continue; | |
280 | ||
281 | if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid)) | |
282 | continue; | |
283 | ||
284 | if (log_kill) | |
285 | log_kill(pid, sig, userdata); | |
286 | ||
287 | /* If we haven't killed this process yet, kill | |
288 | * it */ | |
289 | if (kill(pid, sig) < 0) { | |
290 | if (ret >= 0 && errno != ESRCH) | |
291 | ret = -errno; | |
292 | } else { | |
293 | if (flags & CGROUP_SIGCONT) | |
294 | (void) kill(pid, SIGCONT); | |
295 | ||
296 | if (ret == 0) | |
297 | ret = 1; | |
298 | } | |
299 | ||
300 | done = false; | |
301 | ||
302 | r = set_put(s, PID_TO_PTR(pid)); | |
303 | if (r < 0) { | |
304 | if (ret >= 0) | |
305 | return r; | |
306 | ||
307 | return ret; | |
308 | } | |
309 | } | |
310 | ||
311 | if (r < 0) { | |
312 | if (ret >= 0) | |
313 | return r; | |
314 | ||
315 | return ret; | |
316 | } | |
317 | ||
318 | /* To avoid racing against processes which fork | |
319 | * quicker than we can kill them we repeat this until | |
320 | * no new pids need to be killed. */ | |
321 | ||
322 | } while (!done); | |
323 | ||
324 | return ret; | |
325 | } | |
326 | ||
327 | int cg_kill_recursive( | |
328 | const char *controller, | |
329 | const char *path, | |
330 | int sig, | |
331 | CGroupFlags flags, | |
332 | Set *s, | |
333 | cg_kill_log_func_t log_kill, | |
334 | void *userdata) { | |
335 | ||
336 | _cleanup_set_free_ Set *allocated_set = NULL; | |
337 | _cleanup_closedir_ DIR *d = NULL; | |
338 | int r, ret; | |
339 | char *fn; | |
340 | ||
341 | assert(path); | |
342 | assert(sig >= 0); | |
343 | ||
344 | if (!s) { | |
345 | s = allocated_set = set_new(NULL); | |
346 | if (!s) | |
347 | return -ENOMEM; | |
348 | } | |
349 | ||
350 | ret = cg_kill(controller, path, sig, flags, s, log_kill, userdata); | |
351 | ||
352 | r = cg_enumerate_subgroups(controller, path, &d); | |
353 | if (r < 0) { | |
354 | if (ret >= 0 && r != -ENOENT) | |
355 | return r; | |
356 | ||
357 | return ret; | |
358 | } | |
359 | ||
360 | while ((r = cg_read_subgroup(d, &fn)) > 0) { | |
361 | _cleanup_free_ char *p = NULL; | |
362 | ||
363 | p = strjoin(path, "/", fn); | |
364 | free(fn); | |
365 | if (!p) | |
366 | return -ENOMEM; | |
367 | ||
368 | r = cg_kill_recursive(controller, p, sig, flags, s, log_kill, userdata); | |
369 | if (r != 0 && ret >= 0) | |
370 | ret = r; | |
371 | } | |
372 | if (ret >= 0 && r < 0) | |
373 | ret = r; | |
374 | ||
375 | if (flags & CGROUP_REMOVE) { | |
376 | r = cg_rmdir(controller, path); | |
377 | if (r < 0 && ret >= 0 && !IN_SET(r, -ENOENT, -EBUSY)) | |
378 | return r; | |
379 | } | |
380 | ||
381 | return ret; | |
382 | } | |
383 | ||
384 | int cg_migrate( | |
385 | const char *cfrom, | |
386 | const char *pfrom, | |
387 | const char *cto, | |
388 | const char *pto, | |
389 | CGroupFlags flags) { | |
390 | ||
391 | bool done = false; | |
392 | _cleanup_set_free_ Set *s = NULL; | |
393 | int r, ret = 0; | |
394 | pid_t my_pid; | |
395 | ||
396 | assert(cfrom); | |
397 | assert(pfrom); | |
398 | assert(cto); | |
399 | assert(pto); | |
400 | ||
401 | s = set_new(NULL); | |
402 | if (!s) | |
403 | return -ENOMEM; | |
404 | ||
405 | my_pid = getpid_cached(); | |
406 | ||
407 | do { | |
408 | _cleanup_fclose_ FILE *f = NULL; | |
409 | pid_t pid = 0; | |
410 | done = true; | |
411 | ||
412 | r = cg_enumerate_processes(cfrom, pfrom, &f); | |
413 | if (r < 0) { | |
414 | if (ret >= 0 && r != -ENOENT) | |
415 | return r; | |
416 | ||
417 | return ret; | |
418 | } | |
419 | ||
420 | while ((r = cg_read_pid(f, &pid)) > 0) { | |
421 | ||
422 | /* This might do weird stuff if we aren't a | |
423 | * single-threaded program. However, we | |
424 | * luckily know we are not */ | |
425 | if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid) | |
426 | continue; | |
427 | ||
428 | if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid)) | |
429 | continue; | |
430 | ||
431 | /* Ignore kernel threads. Since they can only | |
432 | * exist in the root cgroup, we only check for | |
433 | * them there. */ | |
434 | if (cfrom && | |
435 | (isempty(pfrom) || path_equal(pfrom, "/")) && | |
436 | is_kernel_thread(pid) > 0) | |
437 | continue; | |
438 | ||
439 | r = cg_attach(cto, pto, pid); | |
440 | if (r < 0) { | |
441 | if (ret >= 0 && r != -ESRCH) | |
442 | ret = r; | |
443 | } else if (ret == 0) | |
444 | ret = 1; | |
445 | ||
446 | done = false; | |
447 | ||
448 | r = set_put(s, PID_TO_PTR(pid)); | |
449 | if (r < 0) { | |
450 | if (ret >= 0) | |
451 | return r; | |
452 | ||
453 | return ret; | |
454 | } | |
455 | } | |
456 | ||
457 | if (r < 0) { | |
458 | if (ret >= 0) | |
459 | return r; | |
460 | ||
461 | return ret; | |
462 | } | |
463 | } while (!done); | |
464 | ||
465 | return ret; | |
466 | } | |
467 | ||
468 | int cg_migrate_recursive( | |
469 | const char *cfrom, | |
470 | const char *pfrom, | |
471 | const char *cto, | |
472 | const char *pto, | |
473 | CGroupFlags flags) { | |
474 | ||
475 | _cleanup_closedir_ DIR *d = NULL; | |
476 | int r, ret = 0; | |
477 | char *fn; | |
478 | ||
479 | assert(cfrom); | |
480 | assert(pfrom); | |
481 | assert(cto); | |
482 | assert(pto); | |
483 | ||
484 | ret = cg_migrate(cfrom, pfrom, cto, pto, flags); | |
485 | ||
486 | r = cg_enumerate_subgroups(cfrom, pfrom, &d); | |
487 | if (r < 0) { | |
488 | if (ret >= 0 && r != -ENOENT) | |
489 | return r; | |
490 | ||
491 | return ret; | |
492 | } | |
493 | ||
494 | while ((r = cg_read_subgroup(d, &fn)) > 0) { | |
495 | _cleanup_free_ char *p = NULL; | |
496 | ||
497 | p = strjoin(pfrom, "/", fn); | |
498 | free(fn); | |
499 | if (!p) | |
500 | return -ENOMEM; | |
501 | ||
502 | r = cg_migrate_recursive(cfrom, p, cto, pto, flags); | |
503 | if (r != 0 && ret >= 0) | |
504 | ret = r; | |
505 | } | |
506 | ||
507 | if (r < 0 && ret >= 0) | |
508 | ret = r; | |
509 | ||
510 | if (flags & CGROUP_REMOVE) { | |
511 | r = cg_rmdir(cfrom, pfrom); | |
512 | if (r < 0 && ret >= 0 && !IN_SET(r, -ENOENT, -EBUSY)) | |
513 | return r; | |
514 | } | |
515 | ||
516 | return ret; | |
517 | } | |
518 | ||
519 | int cg_migrate_recursive_fallback( | |
520 | const char *cfrom, | |
521 | const char *pfrom, | |
522 | const char *cto, | |
523 | const char *pto, | |
524 | CGroupFlags flags) { | |
525 | ||
526 | int r; | |
527 | ||
528 | assert(cfrom); | |
529 | assert(pfrom); | |
530 | assert(cto); | |
531 | assert(pto); | |
532 | ||
533 | r = cg_migrate_recursive(cfrom, pfrom, cto, pto, flags); | |
534 | if (r < 0) { | |
535 | char prefix[strlen(pto) + 1]; | |
536 | ||
537 | /* This didn't work? Then let's try all prefixes of the destination */ | |
538 | ||
539 | PATH_FOREACH_PREFIX(prefix, pto) { | |
540 | int q; | |
541 | ||
542 | q = cg_migrate_recursive(cfrom, pfrom, cto, prefix, flags); | |
543 | if (q >= 0) | |
544 | return q; | |
545 | } | |
546 | } | |
547 | ||
548 | return r; | |
549 | } | |
550 | ||
551 | static const char *controller_to_dirname(const char *controller) { | |
552 | const char *e; | |
553 | ||
554 | assert(controller); | |
555 | ||
556 | /* Converts a controller name to the directory name below | |
557 | * /sys/fs/cgroup/ we want to mount it to. Effectively, this | |
558 | * just cuts off the name= prefixed used for named | |
559 | * hierarchies, if it is specified. */ | |
560 | ||
561 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) { | |
562 | if (cg_hybrid_unified() > 0) | |
563 | controller = SYSTEMD_CGROUP_CONTROLLER_HYBRID; | |
564 | else | |
565 | controller = SYSTEMD_CGROUP_CONTROLLER_LEGACY; | |
566 | } | |
567 | ||
568 | e = startswith(controller, "name="); | |
569 | if (e) | |
570 | return e; | |
571 | ||
572 | return controller; | |
573 | } | |
574 | ||
575 | static int join_path_legacy(const char *controller, const char *path, const char *suffix, char **fs) { | |
576 | const char *dn; | |
577 | char *t = NULL; | |
578 | ||
579 | assert(fs); | |
580 | assert(controller); | |
581 | ||
582 | dn = controller_to_dirname(controller); | |
583 | ||
584 | if (isempty(path) && isempty(suffix)) | |
585 | t = strappend("/sys/fs/cgroup/", dn); | |
586 | else if (isempty(path)) | |
587 | t = strjoin("/sys/fs/cgroup/", dn, "/", suffix); | |
588 | else if (isempty(suffix)) | |
589 | t = strjoin("/sys/fs/cgroup/", dn, "/", path); | |
590 | else | |
591 | t = strjoin("/sys/fs/cgroup/", dn, "/", path, "/", suffix); | |
592 | if (!t) | |
593 | return -ENOMEM; | |
594 | ||
595 | *fs = t; | |
596 | return 0; | |
597 | } | |
598 | ||
599 | static int join_path_unified(const char *path, const char *suffix, char **fs) { | |
600 | char *t; | |
601 | ||
602 | assert(fs); | |
603 | ||
604 | if (isempty(path) && isempty(suffix)) | |
605 | t = strdup("/sys/fs/cgroup"); | |
606 | else if (isempty(path)) | |
607 | t = strappend("/sys/fs/cgroup/", suffix); | |
608 | else if (isempty(suffix)) | |
609 | t = strappend("/sys/fs/cgroup/", path); | |
610 | else | |
611 | t = strjoin("/sys/fs/cgroup/", path, "/", suffix); | |
612 | if (!t) | |
613 | return -ENOMEM; | |
614 | ||
615 | *fs = t; | |
616 | return 0; | |
617 | } | |
618 | ||
619 | int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) { | |
620 | int r; | |
621 | ||
622 | assert(fs); | |
623 | ||
624 | if (!controller) { | |
625 | char *t; | |
626 | ||
627 | /* If no controller is specified, we return the path | |
628 | * *below* the controllers, without any prefix. */ | |
629 | ||
630 | if (!path && !suffix) | |
631 | return -EINVAL; | |
632 | ||
633 | if (!suffix) | |
634 | t = strdup(path); | |
635 | else if (!path) | |
636 | t = strdup(suffix); | |
637 | else | |
638 | t = strjoin(path, "/", suffix); | |
639 | if (!t) | |
640 | return -ENOMEM; | |
641 | ||
642 | *fs = path_kill_slashes(t); | |
643 | return 0; | |
644 | } | |
645 | ||
646 | if (!cg_controller_is_valid(controller)) | |
647 | return -EINVAL; | |
648 | ||
649 | r = cg_all_unified(); | |
650 | if (r < 0) | |
651 | return r; | |
652 | if (r > 0) | |
653 | r = join_path_unified(path, suffix, fs); | |
654 | else | |
655 | r = join_path_legacy(controller, path, suffix, fs); | |
656 | if (r < 0) | |
657 | return r; | |
658 | ||
659 | path_kill_slashes(*fs); | |
660 | return 0; | |
661 | } | |
662 | ||
663 | static int controller_is_accessible(const char *controller) { | |
664 | int r; | |
665 | ||
666 | assert(controller); | |
667 | ||
668 | /* Checks whether a specific controller is accessible, | |
669 | * i.e. its hierarchy mounted. In the unified hierarchy all | |
670 | * controllers are considered accessible, except for the named | |
671 | * hierarchies */ | |
672 | ||
673 | if (!cg_controller_is_valid(controller)) | |
674 | return -EINVAL; | |
675 | ||
676 | r = cg_all_unified(); | |
677 | if (r < 0) | |
678 | return r; | |
679 | if (r > 0) { | |
680 | /* We don't support named hierarchies if we are using | |
681 | * the unified hierarchy. */ | |
682 | ||
683 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) | |
684 | return 0; | |
685 | ||
686 | if (startswith(controller, "name=")) | |
687 | return -EOPNOTSUPP; | |
688 | ||
689 | } else { | |
690 | const char *cc, *dn; | |
691 | ||
692 | dn = controller_to_dirname(controller); | |
693 | cc = strjoina("/sys/fs/cgroup/", dn); | |
694 | ||
695 | if (laccess(cc, F_OK) < 0) | |
696 | return -errno; | |
697 | } | |
698 | ||
699 | return 0; | |
700 | } | |
701 | ||
702 | int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) { | |
703 | int r; | |
704 | ||
705 | assert(controller); | |
706 | assert(fs); | |
707 | ||
708 | /* Check if the specified controller is actually accessible */ | |
709 | r = controller_is_accessible(controller); | |
710 | if (r < 0) | |
711 | return r; | |
712 | ||
713 | return cg_get_path(controller, path, suffix, fs); | |
714 | } | |
715 | ||
716 | static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { | |
717 | assert(path); | |
718 | assert(sb); | |
719 | assert(ftwbuf); | |
720 | ||
721 | if (typeflag != FTW_DP) | |
722 | return 0; | |
723 | ||
724 | if (ftwbuf->level < 1) | |
725 | return 0; | |
726 | ||
727 | (void) rmdir(path); | |
728 | return 0; | |
729 | } | |
730 | ||
731 | int cg_trim(const char *controller, const char *path, bool delete_root) { | |
732 | _cleanup_free_ char *fs = NULL; | |
733 | int r = 0, q; | |
734 | ||
735 | assert(path); | |
736 | ||
737 | r = cg_get_path(controller, path, NULL, &fs); | |
738 | if (r < 0) | |
739 | return r; | |
740 | ||
741 | errno = 0; | |
742 | if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0) { | |
743 | if (errno == ENOENT) | |
744 | r = 0; | |
745 | else if (errno > 0) | |
746 | r = -errno; | |
747 | else | |
748 | r = -EIO; | |
749 | } | |
750 | ||
751 | if (delete_root) { | |
752 | if (rmdir(fs) < 0 && errno != ENOENT) | |
753 | return -errno; | |
754 | } | |
755 | ||
756 | q = cg_hybrid_unified(); | |
757 | if (q < 0) | |
758 | return q; | |
759 | if (q > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) { | |
760 | q = cg_trim(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, delete_root); | |
761 | if (q < 0) | |
762 | log_warning_errno(q, "Failed to trim compat systemd cgroup %s: %m", path); | |
763 | } | |
764 | ||
765 | return r; | |
766 | } | |
767 | ||
768 | int cg_create(const char *controller, const char *path) { | |
769 | _cleanup_free_ char *fs = NULL; | |
770 | int r; | |
771 | ||
772 | r = cg_get_path_and_check(controller, path, NULL, &fs); | |
773 | if (r < 0) | |
774 | return r; | |
775 | ||
776 | r = mkdir_parents(fs, 0755); | |
777 | if (r < 0) | |
778 | return r; | |
779 | ||
780 | if (mkdir(fs, 0755) < 0) { | |
781 | ||
782 | if (errno == EEXIST) | |
783 | return 0; | |
784 | ||
785 | return -errno; | |
786 | } | |
787 | ||
788 | r = cg_hybrid_unified(); | |
789 | if (r < 0) | |
790 | return r; | |
791 | ||
792 | if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) { | |
793 | r = cg_create(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path); | |
794 | if (r < 0) | |
795 | log_warning_errno(r, "Failed to create compat systemd cgroup %s: %m", path); | |
796 | } | |
797 | ||
798 | return 1; | |
799 | } | |
800 | ||
801 | int cg_create_and_attach(const char *controller, const char *path, pid_t pid) { | |
802 | int r, q; | |
803 | ||
804 | assert(pid >= 0); | |
805 | ||
806 | r = cg_create(controller, path); | |
807 | if (r < 0) | |
808 | return r; | |
809 | ||
810 | q = cg_attach(controller, path, pid); | |
811 | if (q < 0) | |
812 | return q; | |
813 | ||
814 | /* This does not remove the cgroup on failure */ | |
815 | return r; | |
816 | } | |
817 | ||
818 | int cg_attach(const char *controller, const char *path, pid_t pid) { | |
819 | _cleanup_free_ char *fs = NULL; | |
820 | char c[DECIMAL_STR_MAX(pid_t) + 2]; | |
821 | int r; | |
822 | ||
823 | assert(path); | |
824 | assert(pid >= 0); | |
825 | ||
826 | r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs); | |
827 | if (r < 0) | |
828 | return r; | |
829 | ||
830 | if (pid == 0) | |
831 | pid = getpid_cached(); | |
832 | ||
833 | xsprintf(c, PID_FMT "\n", pid); | |
834 | ||
835 | r = write_string_file(fs, c, 0); | |
836 | if (r < 0) | |
837 | return r; | |
838 | ||
839 | r = cg_hybrid_unified(); | |
840 | if (r < 0) | |
841 | return r; | |
842 | ||
843 | if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) { | |
844 | r = cg_attach(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, pid); | |
845 | if (r < 0) | |
846 | log_warning_errno(r, "Failed to attach "PID_FMT" to compat systemd cgroup %s: %m", pid, path); | |
847 | } | |
848 | ||
849 | return 0; | |
850 | } | |
851 | ||
852 | int cg_attach_fallback(const char *controller, const char *path, pid_t pid) { | |
853 | int r; | |
854 | ||
855 | assert(controller); | |
856 | assert(path); | |
857 | assert(pid >= 0); | |
858 | ||
859 | r = cg_attach(controller, path, pid); | |
860 | if (r < 0) { | |
861 | char prefix[strlen(path) + 1]; | |
862 | ||
863 | /* This didn't work? Then let's try all prefixes of | |
864 | * the destination */ | |
865 | ||
866 | PATH_FOREACH_PREFIX(prefix, path) { | |
867 | int q; | |
868 | ||
869 | q = cg_attach(controller, prefix, pid); | |
870 | if (q >= 0) | |
871 | return q; | |
872 | } | |
873 | } | |
874 | ||
875 | return r; | |
876 | } | |
877 | ||
878 | int cg_set_group_access( | |
879 | const char *controller, | |
880 | const char *path, | |
881 | mode_t mode, | |
882 | uid_t uid, | |
883 | gid_t gid) { | |
884 | ||
885 | _cleanup_free_ char *fs = NULL; | |
886 | int r; | |
887 | ||
888 | if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID) | |
889 | return 0; | |
890 | ||
891 | if (mode != MODE_INVALID) | |
892 | mode &= 0777; | |
893 | ||
894 | r = cg_get_path(controller, path, NULL, &fs); | |
895 | if (r < 0) | |
896 | return r; | |
897 | ||
898 | r = chmod_and_chown(fs, mode, uid, gid); | |
899 | if (r < 0) | |
900 | return r; | |
901 | ||
902 | r = cg_hybrid_unified(); | |
903 | if (r < 0) | |
904 | return r; | |
905 | if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) { | |
906 | r = cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, mode, uid, gid); | |
907 | if (r < 0) | |
908 | log_debug_errno(r, "Failed to set group access on compatibility systemd cgroup %s, ignoring: %m", path); | |
909 | } | |
910 | ||
911 | return 0; | |
912 | } | |
913 | ||
914 | int cg_set_task_access( | |
915 | const char *controller, | |
916 | const char *path, | |
917 | mode_t mode, | |
918 | uid_t uid, | |
919 | gid_t gid) { | |
920 | ||
921 | _cleanup_free_ char *fs = NULL; | |
922 | int r; | |
923 | ||
924 | assert(path); | |
925 | ||
926 | if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID) | |
927 | return 0; | |
928 | ||
929 | if (mode != MODE_INVALID) | |
930 | mode &= 0666; | |
931 | ||
932 | /* For both the legacy and unified hierarchies, "cgroup.procs" is the main entry point for PIDs */ | |
933 | r = cg_get_path(controller, path, "cgroup.procs", &fs); | |
934 | if (r < 0) | |
935 | return r; | |
936 | ||
937 | r = chmod_and_chown(fs, mode, uid, gid); | |
938 | if (r < 0) | |
939 | return r; | |
940 | ||
941 | r = cg_unified_controller(controller); | |
942 | if (r < 0) | |
943 | return r; | |
944 | if (r == 0) { | |
945 | const char *fn; | |
946 | ||
947 | /* Compatibility: on cgroupsv1 always keep values for the legacy files "tasks" and | |
948 | * "cgroup.clone_children" in sync with "cgroup.procs". Since this is legacy stuff, we don't care if | |
949 | * this fails. */ | |
950 | ||
951 | FOREACH_STRING(fn, | |
952 | "tasks", | |
953 | "cgroup.clone_children") { | |
954 | ||
955 | fs = mfree(fs); | |
956 | ||
957 | r = cg_get_path(controller, path, fn, &fs); | |
958 | if (r < 0) | |
959 | log_debug_errno(r, "Failed to get path for %s of %s, ignoring: %m", fn, path); | |
960 | ||
961 | r = chmod_and_chown(fs, mode, uid, gid); | |
962 | if (r < 0) | |
963 | log_debug_errno(r, "Failed to to change ownership/access mode for %s of %s, ignoring: %m", fn, path); | |
964 | } | |
965 | } else { | |
966 | /* On the unified controller, we want to permit subtree controllers too. */ | |
967 | ||
968 | fs = mfree(fs); | |
969 | r = cg_get_path(controller, path, "cgroup.subtree_control", &fs); | |
970 | if (r < 0) | |
971 | return r; | |
972 | ||
973 | r = chmod_and_chown(fs, mode, uid, gid); | |
974 | if (r < 0) | |
975 | return r; | |
976 | } | |
977 | ||
978 | r = cg_hybrid_unified(); | |
979 | if (r < 0) | |
980 | return r; | |
981 | if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) { | |
982 | /* Always propagate access mode from unified to legacy controller */ | |
983 | ||
984 | r = cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, mode, uid, gid); | |
985 | if (r < 0) | |
986 | log_debug_errno(r, "Failed to set task access on compatibility systemd cgroup %s, ignoring: %m", path); | |
987 | } | |
988 | ||
989 | return 0; | |
990 | } | |
991 | ||
992 | int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags) { | |
993 | _cleanup_free_ char *fs = NULL; | |
994 | int r; | |
995 | ||
996 | assert(path); | |
997 | assert(name); | |
998 | assert(value || size <= 0); | |
999 | ||
1000 | r = cg_get_path(controller, path, NULL, &fs); | |
1001 | if (r < 0) | |
1002 | return r; | |
1003 | ||
1004 | if (setxattr(fs, name, value, size, flags) < 0) | |
1005 | return -errno; | |
1006 | ||
1007 | return 0; | |
1008 | } | |
1009 | ||
1010 | int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size) { | |
1011 | _cleanup_free_ char *fs = NULL; | |
1012 | ssize_t n; | |
1013 | int r; | |
1014 | ||
1015 | assert(path); | |
1016 | assert(name); | |
1017 | ||
1018 | r = cg_get_path(controller, path, NULL, &fs); | |
1019 | if (r < 0) | |
1020 | return r; | |
1021 | ||
1022 | n = getxattr(fs, name, value, size); | |
1023 | if (n < 0) | |
1024 | return -errno; | |
1025 | ||
1026 | return (int) n; | |
1027 | } | |
1028 | ||
1029 | int cg_pid_get_path(const char *controller, pid_t pid, char **path) { | |
1030 | _cleanup_fclose_ FILE *f = NULL; | |
1031 | char line[LINE_MAX]; | |
1032 | const char *fs, *controller_str; | |
1033 | size_t cs = 0; | |
1034 | int unified; | |
1035 | ||
1036 | assert(path); | |
1037 | assert(pid >= 0); | |
1038 | ||
1039 | if (controller) { | |
1040 | if (!cg_controller_is_valid(controller)) | |
1041 | return -EINVAL; | |
1042 | } else | |
1043 | controller = SYSTEMD_CGROUP_CONTROLLER; | |
1044 | ||
1045 | unified = cg_unified_controller(controller); | |
1046 | if (unified < 0) | |
1047 | return unified; | |
1048 | if (unified == 0) { | |
1049 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) | |
1050 | controller_str = SYSTEMD_CGROUP_CONTROLLER_LEGACY; | |
1051 | else | |
1052 | controller_str = controller; | |
1053 | ||
1054 | cs = strlen(controller_str); | |
1055 | } | |
1056 | ||
1057 | fs = procfs_file_alloca(pid, "cgroup"); | |
1058 | f = fopen(fs, "re"); | |
1059 | if (!f) | |
1060 | return errno == ENOENT ? -ESRCH : -errno; | |
1061 | ||
1062 | FOREACH_LINE(line, f, return -errno) { | |
1063 | char *e, *p; | |
1064 | ||
1065 | truncate_nl(line); | |
1066 | ||
1067 | if (unified) { | |
1068 | e = startswith(line, "0:"); | |
1069 | if (!e) | |
1070 | continue; | |
1071 | ||
1072 | e = strchr(e, ':'); | |
1073 | if (!e) | |
1074 | continue; | |
1075 | } else { | |
1076 | char *l; | |
1077 | size_t k; | |
1078 | const char *word, *state; | |
1079 | bool found = false; | |
1080 | ||
1081 | l = strchr(line, ':'); | |
1082 | if (!l) | |
1083 | continue; | |
1084 | ||
1085 | l++; | |
1086 | e = strchr(l, ':'); | |
1087 | if (!e) | |
1088 | continue; | |
1089 | ||
1090 | *e = 0; | |
1091 | FOREACH_WORD_SEPARATOR(word, k, l, ",", state) { | |
1092 | if (k == cs && memcmp(word, controller_str, cs) == 0) { | |
1093 | found = true; | |
1094 | break; | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | if (!found) | |
1099 | continue; | |
1100 | } | |
1101 | ||
1102 | p = strdup(e + 1); | |
1103 | if (!p) | |
1104 | return -ENOMEM; | |
1105 | ||
1106 | *path = p; | |
1107 | return 0; | |
1108 | } | |
1109 | ||
1110 | return -ENODATA; | |
1111 | } | |
1112 | ||
1113 | int cg_install_release_agent(const char *controller, const char *agent) { | |
1114 | _cleanup_free_ char *fs = NULL, *contents = NULL; | |
1115 | const char *sc; | |
1116 | int r; | |
1117 | ||
1118 | assert(agent); | |
1119 | ||
1120 | r = cg_unified_controller(controller); | |
1121 | if (r < 0) | |
1122 | return r; | |
1123 | if (r > 0) /* doesn't apply to unified hierarchy */ | |
1124 | return -EOPNOTSUPP; | |
1125 | ||
1126 | r = cg_get_path(controller, NULL, "release_agent", &fs); | |
1127 | if (r < 0) | |
1128 | return r; | |
1129 | ||
1130 | r = read_one_line_file(fs, &contents); | |
1131 | if (r < 0) | |
1132 | return r; | |
1133 | ||
1134 | sc = strstrip(contents); | |
1135 | if (isempty(sc)) { | |
1136 | r = write_string_file(fs, agent, 0); | |
1137 | if (r < 0) | |
1138 | return r; | |
1139 | } else if (!path_equal(sc, agent)) | |
1140 | return -EEXIST; | |
1141 | ||
1142 | fs = mfree(fs); | |
1143 | r = cg_get_path(controller, NULL, "notify_on_release", &fs); | |
1144 | if (r < 0) | |
1145 | return r; | |
1146 | ||
1147 | contents = mfree(contents); | |
1148 | r = read_one_line_file(fs, &contents); | |
1149 | if (r < 0) | |
1150 | return r; | |
1151 | ||
1152 | sc = strstrip(contents); | |
1153 | if (streq(sc, "0")) { | |
1154 | r = write_string_file(fs, "1", 0); | |
1155 | if (r < 0) | |
1156 | return r; | |
1157 | ||
1158 | return 1; | |
1159 | } | |
1160 | ||
1161 | if (!streq(sc, "1")) | |
1162 | return -EIO; | |
1163 | ||
1164 | return 0; | |
1165 | } | |
1166 | ||
1167 | int cg_uninstall_release_agent(const char *controller) { | |
1168 | _cleanup_free_ char *fs = NULL; | |
1169 | int r; | |
1170 | ||
1171 | r = cg_unified_controller(controller); | |
1172 | if (r < 0) | |
1173 | return r; | |
1174 | if (r > 0) /* Doesn't apply to unified hierarchy */ | |
1175 | return -EOPNOTSUPP; | |
1176 | ||
1177 | r = cg_get_path(controller, NULL, "notify_on_release", &fs); | |
1178 | if (r < 0) | |
1179 | return r; | |
1180 | ||
1181 | r = write_string_file(fs, "0", 0); | |
1182 | if (r < 0) | |
1183 | return r; | |
1184 | ||
1185 | fs = mfree(fs); | |
1186 | ||
1187 | r = cg_get_path(controller, NULL, "release_agent", &fs); | |
1188 | if (r < 0) | |
1189 | return r; | |
1190 | ||
1191 | r = write_string_file(fs, "", 0); | |
1192 | if (r < 0) | |
1193 | return r; | |
1194 | ||
1195 | return 0; | |
1196 | } | |
1197 | ||
1198 | int cg_is_empty(const char *controller, const char *path) { | |
1199 | _cleanup_fclose_ FILE *f = NULL; | |
1200 | pid_t pid; | |
1201 | int r; | |
1202 | ||
1203 | assert(path); | |
1204 | ||
1205 | r = cg_enumerate_processes(controller, path, &f); | |
1206 | if (r == -ENOENT) | |
1207 | return 1; | |
1208 | if (r < 0) | |
1209 | return r; | |
1210 | ||
1211 | r = cg_read_pid(f, &pid); | |
1212 | if (r < 0) | |
1213 | return r; | |
1214 | ||
1215 | return r == 0; | |
1216 | } | |
1217 | ||
1218 | int cg_is_empty_recursive(const char *controller, const char *path) { | |
1219 | int r; | |
1220 | ||
1221 | assert(path); | |
1222 | ||
1223 | /* The root cgroup is always populated */ | |
1224 | if (controller && (isempty(path) || path_equal(path, "/"))) | |
1225 | return false; | |
1226 | ||
1227 | r = cg_unified_controller(controller); | |
1228 | if (r < 0) | |
1229 | return r; | |
1230 | if (r > 0) { | |
1231 | _cleanup_free_ char *t = NULL; | |
1232 | ||
1233 | /* On the unified hierarchy we can check empty state | |
1234 | * via the "populated" attribute of "cgroup.events". */ | |
1235 | ||
1236 | r = cg_read_event(controller, path, "populated", &t); | |
1237 | if (r < 0) | |
1238 | return r; | |
1239 | ||
1240 | return streq(t, "0"); | |
1241 | } else { | |
1242 | _cleanup_closedir_ DIR *d = NULL; | |
1243 | char *fn; | |
1244 | ||
1245 | r = cg_is_empty(controller, path); | |
1246 | if (r <= 0) | |
1247 | return r; | |
1248 | ||
1249 | r = cg_enumerate_subgroups(controller, path, &d); | |
1250 | if (r == -ENOENT) | |
1251 | return 1; | |
1252 | if (r < 0) | |
1253 | return r; | |
1254 | ||
1255 | while ((r = cg_read_subgroup(d, &fn)) > 0) { | |
1256 | _cleanup_free_ char *p = NULL; | |
1257 | ||
1258 | p = strjoin(path, "/", fn); | |
1259 | free(fn); | |
1260 | if (!p) | |
1261 | return -ENOMEM; | |
1262 | ||
1263 | r = cg_is_empty_recursive(controller, p); | |
1264 | if (r <= 0) | |
1265 | return r; | |
1266 | } | |
1267 | if (r < 0) | |
1268 | return r; | |
1269 | ||
1270 | return true; | |
1271 | } | |
1272 | } | |
1273 | ||
1274 | int cg_split_spec(const char *spec, char **controller, char **path) { | |
1275 | char *t = NULL, *u = NULL; | |
1276 | const char *e; | |
1277 | ||
1278 | assert(spec); | |
1279 | ||
1280 | if (*spec == '/') { | |
1281 | if (!path_is_safe(spec)) | |
1282 | return -EINVAL; | |
1283 | ||
1284 | if (path) { | |
1285 | t = strdup(spec); | |
1286 | if (!t) | |
1287 | return -ENOMEM; | |
1288 | ||
1289 | *path = path_kill_slashes(t); | |
1290 | } | |
1291 | ||
1292 | if (controller) | |
1293 | *controller = NULL; | |
1294 | ||
1295 | return 0; | |
1296 | } | |
1297 | ||
1298 | e = strchr(spec, ':'); | |
1299 | if (!e) { | |
1300 | if (!cg_controller_is_valid(spec)) | |
1301 | return -EINVAL; | |
1302 | ||
1303 | if (controller) { | |
1304 | t = strdup(spec); | |
1305 | if (!t) | |
1306 | return -ENOMEM; | |
1307 | ||
1308 | *controller = t; | |
1309 | } | |
1310 | ||
1311 | if (path) | |
1312 | *path = NULL; | |
1313 | ||
1314 | return 0; | |
1315 | } | |
1316 | ||
1317 | t = strndup(spec, e-spec); | |
1318 | if (!t) | |
1319 | return -ENOMEM; | |
1320 | if (!cg_controller_is_valid(t)) { | |
1321 | free(t); | |
1322 | return -EINVAL; | |
1323 | } | |
1324 | ||
1325 | if (isempty(e+1)) | |
1326 | u = NULL; | |
1327 | else { | |
1328 | u = strdup(e+1); | |
1329 | if (!u) { | |
1330 | free(t); | |
1331 | return -ENOMEM; | |
1332 | } | |
1333 | ||
1334 | if (!path_is_safe(u) || | |
1335 | !path_is_absolute(u)) { | |
1336 | free(t); | |
1337 | free(u); | |
1338 | return -EINVAL; | |
1339 | } | |
1340 | ||
1341 | path_kill_slashes(u); | |
1342 | } | |
1343 | ||
1344 | if (controller) | |
1345 | *controller = t; | |
1346 | else | |
1347 | free(t); | |
1348 | ||
1349 | if (path) | |
1350 | *path = u; | |
1351 | else | |
1352 | free(u); | |
1353 | ||
1354 | return 0; | |
1355 | } | |
1356 | ||
1357 | int cg_mangle_path(const char *path, char **result) { | |
1358 | _cleanup_free_ char *c = NULL, *p = NULL; | |
1359 | char *t; | |
1360 | int r; | |
1361 | ||
1362 | assert(path); | |
1363 | assert(result); | |
1364 | ||
1365 | /* First, check if it already is a filesystem path */ | |
1366 | if (path_startswith(path, "/sys/fs/cgroup")) { | |
1367 | ||
1368 | t = strdup(path); | |
1369 | if (!t) | |
1370 | return -ENOMEM; | |
1371 | ||
1372 | *result = path_kill_slashes(t); | |
1373 | return 0; | |
1374 | } | |
1375 | ||
1376 | /* Otherwise, treat it as cg spec */ | |
1377 | r = cg_split_spec(path, &c, &p); | |
1378 | if (r < 0) | |
1379 | return r; | |
1380 | ||
1381 | return cg_get_path(c ?: SYSTEMD_CGROUP_CONTROLLER, p ?: "/", NULL, result); | |
1382 | } | |
1383 | ||
1384 | int cg_get_root_path(char **path) { | |
1385 | char *p, *e; | |
1386 | int r; | |
1387 | ||
1388 | assert(path); | |
1389 | ||
1390 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p); | |
1391 | if (r < 0) | |
1392 | return r; | |
1393 | ||
1394 | e = endswith(p, "/" SPECIAL_INIT_SCOPE); | |
1395 | if (!e) | |
1396 | e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */ | |
1397 | if (!e) | |
1398 | e = endswith(p, "/system"); /* even more legacy */ | |
1399 | if (e) | |
1400 | *e = 0; | |
1401 | ||
1402 | *path = p; | |
1403 | return 0; | |
1404 | } | |
1405 | ||
1406 | int cg_shift_path(const char *cgroup, const char *root, const char **shifted) { | |
1407 | _cleanup_free_ char *rt = NULL; | |
1408 | char *p; | |
1409 | int r; | |
1410 | ||
1411 | assert(cgroup); | |
1412 | assert(shifted); | |
1413 | ||
1414 | if (!root) { | |
1415 | /* If the root was specified let's use that, otherwise | |
1416 | * let's determine it from PID 1 */ | |
1417 | ||
1418 | r = cg_get_root_path(&rt); | |
1419 | if (r < 0) | |
1420 | return r; | |
1421 | ||
1422 | root = rt; | |
1423 | } | |
1424 | ||
1425 | p = path_startswith(cgroup, root); | |
1426 | if (p && p > cgroup) | |
1427 | *shifted = p - 1; | |
1428 | else | |
1429 | *shifted = cgroup; | |
1430 | ||
1431 | return 0; | |
1432 | } | |
1433 | ||
1434 | int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) { | |
1435 | _cleanup_free_ char *raw = NULL; | |
1436 | const char *c; | |
1437 | int r; | |
1438 | ||
1439 | assert(pid >= 0); | |
1440 | assert(cgroup); | |
1441 | ||
1442 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw); | |
1443 | if (r < 0) | |
1444 | return r; | |
1445 | ||
1446 | r = cg_shift_path(raw, root, &c); | |
1447 | if (r < 0) | |
1448 | return r; | |
1449 | ||
1450 | if (c == raw) { | |
1451 | *cgroup = raw; | |
1452 | raw = NULL; | |
1453 | } else { | |
1454 | char *n; | |
1455 | ||
1456 | n = strdup(c); | |
1457 | if (!n) | |
1458 | return -ENOMEM; | |
1459 | ||
1460 | *cgroup = n; | |
1461 | } | |
1462 | ||
1463 | return 0; | |
1464 | } | |
1465 | ||
1466 | int cg_path_decode_unit(const char *cgroup, char **unit) { | |
1467 | char *c, *s; | |
1468 | size_t n; | |
1469 | ||
1470 | assert(cgroup); | |
1471 | assert(unit); | |
1472 | ||
1473 | n = strcspn(cgroup, "/"); | |
1474 | if (n < 3) | |
1475 | return -ENXIO; | |
1476 | ||
1477 | c = strndupa(cgroup, n); | |
1478 | c = cg_unescape(c); | |
1479 | ||
1480 | if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) | |
1481 | return -ENXIO; | |
1482 | ||
1483 | s = strdup(c); | |
1484 | if (!s) | |
1485 | return -ENOMEM; | |
1486 | ||
1487 | *unit = s; | |
1488 | return 0; | |
1489 | } | |
1490 | ||
1491 | static bool valid_slice_name(const char *p, size_t n) { | |
1492 | ||
1493 | if (!p) | |
1494 | return false; | |
1495 | ||
1496 | if (n < strlen("x.slice")) | |
1497 | return false; | |
1498 | ||
1499 | if (memcmp(p + n - 6, ".slice", 6) == 0) { | |
1500 | char buf[n+1], *c; | |
1501 | ||
1502 | memcpy(buf, p, n); | |
1503 | buf[n] = 0; | |
1504 | ||
1505 | c = cg_unescape(buf); | |
1506 | ||
1507 | return unit_name_is_valid(c, UNIT_NAME_PLAIN); | |
1508 | } | |
1509 | ||
1510 | return false; | |
1511 | } | |
1512 | ||
1513 | static const char *skip_slices(const char *p) { | |
1514 | assert(p); | |
1515 | ||
1516 | /* Skips over all slice assignments */ | |
1517 | ||
1518 | for (;;) { | |
1519 | size_t n; | |
1520 | ||
1521 | p += strspn(p, "/"); | |
1522 | ||
1523 | n = strcspn(p, "/"); | |
1524 | if (!valid_slice_name(p, n)) | |
1525 | return p; | |
1526 | ||
1527 | p += n; | |
1528 | } | |
1529 | } | |
1530 | ||
1531 | int cg_path_get_unit(const char *path, char **ret) { | |
1532 | const char *e; | |
1533 | char *unit; | |
1534 | int r; | |
1535 | ||
1536 | assert(path); | |
1537 | assert(ret); | |
1538 | ||
1539 | e = skip_slices(path); | |
1540 | ||
1541 | r = cg_path_decode_unit(e, &unit); | |
1542 | if (r < 0) | |
1543 | return r; | |
1544 | ||
1545 | /* We skipped over the slices, don't accept any now */ | |
1546 | if (endswith(unit, ".slice")) { | |
1547 | free(unit); | |
1548 | return -ENXIO; | |
1549 | } | |
1550 | ||
1551 | *ret = unit; | |
1552 | return 0; | |
1553 | } | |
1554 | ||
1555 | int cg_pid_get_unit(pid_t pid, char **unit) { | |
1556 | _cleanup_free_ char *cgroup = NULL; | |
1557 | int r; | |
1558 | ||
1559 | assert(unit); | |
1560 | ||
1561 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1562 | if (r < 0) | |
1563 | return r; | |
1564 | ||
1565 | return cg_path_get_unit(cgroup, unit); | |
1566 | } | |
1567 | ||
1568 | /** | |
1569 | * Skip session-*.scope, but require it to be there. | |
1570 | */ | |
1571 | static const char *skip_session(const char *p) { | |
1572 | size_t n; | |
1573 | ||
1574 | if (isempty(p)) | |
1575 | return NULL; | |
1576 | ||
1577 | p += strspn(p, "/"); | |
1578 | ||
1579 | n = strcspn(p, "/"); | |
1580 | if (n < strlen("session-x.scope")) | |
1581 | return NULL; | |
1582 | ||
1583 | if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) { | |
1584 | char buf[n - 8 - 6 + 1]; | |
1585 | ||
1586 | memcpy(buf, p + 8, n - 8 - 6); | |
1587 | buf[n - 8 - 6] = 0; | |
1588 | ||
1589 | /* Note that session scopes never need unescaping, | |
1590 | * since they cannot conflict with the kernel's own | |
1591 | * names, hence we don't need to call cg_unescape() | |
1592 | * here. */ | |
1593 | ||
1594 | if (!session_id_valid(buf)) | |
1595 | return false; | |
1596 | ||
1597 | p += n; | |
1598 | p += strspn(p, "/"); | |
1599 | return p; | |
1600 | } | |
1601 | ||
1602 | return NULL; | |
1603 | } | |
1604 | ||
1605 | /** | |
1606 | * Skip user@*.service, but require it to be there. | |
1607 | */ | |
1608 | static const char *skip_user_manager(const char *p) { | |
1609 | size_t n; | |
1610 | ||
1611 | if (isempty(p)) | |
1612 | return NULL; | |
1613 | ||
1614 | p += strspn(p, "/"); | |
1615 | ||
1616 | n = strcspn(p, "/"); | |
1617 | if (n < strlen("user@x.service")) | |
1618 | return NULL; | |
1619 | ||
1620 | if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) { | |
1621 | char buf[n - 5 - 8 + 1]; | |
1622 | ||
1623 | memcpy(buf, p + 5, n - 5 - 8); | |
1624 | buf[n - 5 - 8] = 0; | |
1625 | ||
1626 | /* Note that user manager services never need unescaping, | |
1627 | * since they cannot conflict with the kernel's own | |
1628 | * names, hence we don't need to call cg_unescape() | |
1629 | * here. */ | |
1630 | ||
1631 | if (parse_uid(buf, NULL) < 0) | |
1632 | return NULL; | |
1633 | ||
1634 | p += n; | |
1635 | p += strspn(p, "/"); | |
1636 | ||
1637 | return p; | |
1638 | } | |
1639 | ||
1640 | return NULL; | |
1641 | } | |
1642 | ||
1643 | static const char *skip_user_prefix(const char *path) { | |
1644 | const char *e, *t; | |
1645 | ||
1646 | assert(path); | |
1647 | ||
1648 | /* Skip slices, if there are any */ | |
1649 | e = skip_slices(path); | |
1650 | ||
1651 | /* Skip the user manager, if it's in the path now... */ | |
1652 | t = skip_user_manager(e); | |
1653 | if (t) | |
1654 | return t; | |
1655 | ||
1656 | /* Alternatively skip the user session if it is in the path... */ | |
1657 | return skip_session(e); | |
1658 | } | |
1659 | ||
1660 | int cg_path_get_user_unit(const char *path, char **ret) { | |
1661 | const char *t; | |
1662 | ||
1663 | assert(path); | |
1664 | assert(ret); | |
1665 | ||
1666 | t = skip_user_prefix(path); | |
1667 | if (!t) | |
1668 | return -ENXIO; | |
1669 | ||
1670 | /* And from here on it looks pretty much the same as for a | |
1671 | * system unit, hence let's use the same parser from here | |
1672 | * on. */ | |
1673 | return cg_path_get_unit(t, ret); | |
1674 | } | |
1675 | ||
1676 | int cg_pid_get_user_unit(pid_t pid, char **unit) { | |
1677 | _cleanup_free_ char *cgroup = NULL; | |
1678 | int r; | |
1679 | ||
1680 | assert(unit); | |
1681 | ||
1682 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1683 | if (r < 0) | |
1684 | return r; | |
1685 | ||
1686 | return cg_path_get_user_unit(cgroup, unit); | |
1687 | } | |
1688 | ||
1689 | int cg_path_get_machine_name(const char *path, char **machine) { | |
1690 | _cleanup_free_ char *u = NULL; | |
1691 | const char *sl; | |
1692 | int r; | |
1693 | ||
1694 | r = cg_path_get_unit(path, &u); | |
1695 | if (r < 0) | |
1696 | return r; | |
1697 | ||
1698 | sl = strjoina("/run/systemd/machines/unit:", u); | |
1699 | return readlink_malloc(sl, machine); | |
1700 | } | |
1701 | ||
1702 | int cg_pid_get_machine_name(pid_t pid, char **machine) { | |
1703 | _cleanup_free_ char *cgroup = NULL; | |
1704 | int r; | |
1705 | ||
1706 | assert(machine); | |
1707 | ||
1708 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1709 | if (r < 0) | |
1710 | return r; | |
1711 | ||
1712 | return cg_path_get_machine_name(cgroup, machine); | |
1713 | } | |
1714 | ||
1715 | int cg_path_get_session(const char *path, char **session) { | |
1716 | _cleanup_free_ char *unit = NULL; | |
1717 | char *start, *end; | |
1718 | int r; | |
1719 | ||
1720 | assert(path); | |
1721 | ||
1722 | r = cg_path_get_unit(path, &unit); | |
1723 | if (r < 0) | |
1724 | return r; | |
1725 | ||
1726 | start = startswith(unit, "session-"); | |
1727 | if (!start) | |
1728 | return -ENXIO; | |
1729 | end = endswith(start, ".scope"); | |
1730 | if (!end) | |
1731 | return -ENXIO; | |
1732 | ||
1733 | *end = 0; | |
1734 | if (!session_id_valid(start)) | |
1735 | return -ENXIO; | |
1736 | ||
1737 | if (session) { | |
1738 | char *rr; | |
1739 | ||
1740 | rr = strdup(start); | |
1741 | if (!rr) | |
1742 | return -ENOMEM; | |
1743 | ||
1744 | *session = rr; | |
1745 | } | |
1746 | ||
1747 | return 0; | |
1748 | } | |
1749 | ||
1750 | int cg_pid_get_session(pid_t pid, char **session) { | |
1751 | _cleanup_free_ char *cgroup = NULL; | |
1752 | int r; | |
1753 | ||
1754 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1755 | if (r < 0) | |
1756 | return r; | |
1757 | ||
1758 | return cg_path_get_session(cgroup, session); | |
1759 | } | |
1760 | ||
1761 | int cg_path_get_owner_uid(const char *path, uid_t *uid) { | |
1762 | _cleanup_free_ char *slice = NULL; | |
1763 | char *start, *end; | |
1764 | int r; | |
1765 | ||
1766 | assert(path); | |
1767 | ||
1768 | r = cg_path_get_slice(path, &slice); | |
1769 | if (r < 0) | |
1770 | return r; | |
1771 | ||
1772 | start = startswith(slice, "user-"); | |
1773 | if (!start) | |
1774 | return -ENXIO; | |
1775 | end = endswith(start, ".slice"); | |
1776 | if (!end) | |
1777 | return -ENXIO; | |
1778 | ||
1779 | *end = 0; | |
1780 | if (parse_uid(start, uid) < 0) | |
1781 | return -ENXIO; | |
1782 | ||
1783 | return 0; | |
1784 | } | |
1785 | ||
1786 | int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) { | |
1787 | _cleanup_free_ char *cgroup = NULL; | |
1788 | int r; | |
1789 | ||
1790 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1791 | if (r < 0) | |
1792 | return r; | |
1793 | ||
1794 | return cg_path_get_owner_uid(cgroup, uid); | |
1795 | } | |
1796 | ||
1797 | int cg_path_get_slice(const char *p, char **slice) { | |
1798 | const char *e = NULL; | |
1799 | ||
1800 | assert(p); | |
1801 | assert(slice); | |
1802 | ||
1803 | /* Finds the right-most slice unit from the beginning, but | |
1804 | * stops before we come to the first non-slice unit. */ | |
1805 | ||
1806 | for (;;) { | |
1807 | size_t n; | |
1808 | ||
1809 | p += strspn(p, "/"); | |
1810 | ||
1811 | n = strcspn(p, "/"); | |
1812 | if (!valid_slice_name(p, n)) { | |
1813 | ||
1814 | if (!e) { | |
1815 | char *s; | |
1816 | ||
1817 | s = strdup(SPECIAL_ROOT_SLICE); | |
1818 | if (!s) | |
1819 | return -ENOMEM; | |
1820 | ||
1821 | *slice = s; | |
1822 | return 0; | |
1823 | } | |
1824 | ||
1825 | return cg_path_decode_unit(e, slice); | |
1826 | } | |
1827 | ||
1828 | e = p; | |
1829 | p += n; | |
1830 | } | |
1831 | } | |
1832 | ||
1833 | int cg_pid_get_slice(pid_t pid, char **slice) { | |
1834 | _cleanup_free_ char *cgroup = NULL; | |
1835 | int r; | |
1836 | ||
1837 | assert(slice); | |
1838 | ||
1839 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1840 | if (r < 0) | |
1841 | return r; | |
1842 | ||
1843 | return cg_path_get_slice(cgroup, slice); | |
1844 | } | |
1845 | ||
1846 | int cg_path_get_user_slice(const char *p, char **slice) { | |
1847 | const char *t; | |
1848 | assert(p); | |
1849 | assert(slice); | |
1850 | ||
1851 | t = skip_user_prefix(p); | |
1852 | if (!t) | |
1853 | return -ENXIO; | |
1854 | ||
1855 | /* And now it looks pretty much the same as for a system | |
1856 | * slice, so let's just use the same parser from here on. */ | |
1857 | return cg_path_get_slice(t, slice); | |
1858 | } | |
1859 | ||
1860 | int cg_pid_get_user_slice(pid_t pid, char **slice) { | |
1861 | _cleanup_free_ char *cgroup = NULL; | |
1862 | int r; | |
1863 | ||
1864 | assert(slice); | |
1865 | ||
1866 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1867 | if (r < 0) | |
1868 | return r; | |
1869 | ||
1870 | return cg_path_get_user_slice(cgroup, slice); | |
1871 | } | |
1872 | ||
1873 | char *cg_escape(const char *p) { | |
1874 | bool need_prefix = false; | |
1875 | ||
1876 | /* This implements very minimal escaping for names to be used | |
1877 | * as file names in the cgroup tree: any name which might | |
1878 | * conflict with a kernel name or is prefixed with '_' is | |
1879 | * prefixed with a '_'. That way, when reading cgroup names it | |
1880 | * is sufficient to remove a single prefixing underscore if | |
1881 | * there is one. */ | |
1882 | ||
1883 | /* The return value of this function (unlike cg_unescape()) | |
1884 | * needs free()! */ | |
1885 | ||
1886 | if (IN_SET(p[0], 0, '_', '.') || | |
1887 | streq(p, "notify_on_release") || | |
1888 | streq(p, "release_agent") || | |
1889 | streq(p, "tasks") || | |
1890 | startswith(p, "cgroup.")) | |
1891 | need_prefix = true; | |
1892 | else { | |
1893 | const char *dot; | |
1894 | ||
1895 | dot = strrchr(p, '.'); | |
1896 | if (dot) { | |
1897 | CGroupController c; | |
1898 | size_t l = dot - p; | |
1899 | ||
1900 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { | |
1901 | const char *n; | |
1902 | ||
1903 | n = cgroup_controller_to_string(c); | |
1904 | ||
1905 | if (l != strlen(n)) | |
1906 | continue; | |
1907 | ||
1908 | if (memcmp(p, n, l) != 0) | |
1909 | continue; | |
1910 | ||
1911 | need_prefix = true; | |
1912 | break; | |
1913 | } | |
1914 | } | |
1915 | } | |
1916 | ||
1917 | if (need_prefix) | |
1918 | return strappend("_", p); | |
1919 | ||
1920 | return strdup(p); | |
1921 | } | |
1922 | ||
1923 | char *cg_unescape(const char *p) { | |
1924 | assert(p); | |
1925 | ||
1926 | /* The return value of this function (unlike cg_escape()) | |
1927 | * doesn't need free()! */ | |
1928 | ||
1929 | if (p[0] == '_') | |
1930 | return (char*) p+1; | |
1931 | ||
1932 | return (char*) p; | |
1933 | } | |
1934 | ||
1935 | #define CONTROLLER_VALID \ | |
1936 | DIGITS LETTERS \ | |
1937 | "_" | |
1938 | ||
1939 | bool cg_controller_is_valid(const char *p) { | |
1940 | const char *t, *s; | |
1941 | ||
1942 | if (!p) | |
1943 | return false; | |
1944 | ||
1945 | if (streq(p, SYSTEMD_CGROUP_CONTROLLER)) | |
1946 | return true; | |
1947 | ||
1948 | s = startswith(p, "name="); | |
1949 | if (s) | |
1950 | p = s; | |
1951 | ||
1952 | if (IN_SET(*p, 0, '_')) | |
1953 | return false; | |
1954 | ||
1955 | for (t = p; *t; t++) | |
1956 | if (!strchr(CONTROLLER_VALID, *t)) | |
1957 | return false; | |
1958 | ||
1959 | if (t - p > FILENAME_MAX) | |
1960 | return false; | |
1961 | ||
1962 | return true; | |
1963 | } | |
1964 | ||
1965 | int cg_slice_to_path(const char *unit, char **ret) { | |
1966 | _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL; | |
1967 | const char *dash; | |
1968 | int r; | |
1969 | ||
1970 | assert(unit); | |
1971 | assert(ret); | |
1972 | ||
1973 | if (streq(unit, SPECIAL_ROOT_SLICE)) { | |
1974 | char *x; | |
1975 | ||
1976 | x = strdup(""); | |
1977 | if (!x) | |
1978 | return -ENOMEM; | |
1979 | *ret = x; | |
1980 | return 0; | |
1981 | } | |
1982 | ||
1983 | if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN)) | |
1984 | return -EINVAL; | |
1985 | ||
1986 | if (!endswith(unit, ".slice")) | |
1987 | return -EINVAL; | |
1988 | ||
1989 | r = unit_name_to_prefix(unit, &p); | |
1990 | if (r < 0) | |
1991 | return r; | |
1992 | ||
1993 | dash = strchr(p, '-'); | |
1994 | ||
1995 | /* Don't allow initial dashes */ | |
1996 | if (dash == p) | |
1997 | return -EINVAL; | |
1998 | ||
1999 | while (dash) { | |
2000 | _cleanup_free_ char *escaped = NULL; | |
2001 | char n[dash - p + sizeof(".slice")]; | |
2002 | ||
2003 | /* Don't allow trailing or double dashes */ | |
2004 | if (IN_SET(dash[1], 0, '-')) | |
2005 | return -EINVAL; | |
2006 | ||
2007 | strcpy(stpncpy(n, p, dash - p), ".slice"); | |
2008 | if (!unit_name_is_valid(n, UNIT_NAME_PLAIN)) | |
2009 | return -EINVAL; | |
2010 | ||
2011 | escaped = cg_escape(n); | |
2012 | if (!escaped) | |
2013 | return -ENOMEM; | |
2014 | ||
2015 | if (!strextend(&s, escaped, "/", NULL)) | |
2016 | return -ENOMEM; | |
2017 | ||
2018 | dash = strchr(dash+1, '-'); | |
2019 | } | |
2020 | ||
2021 | e = cg_escape(unit); | |
2022 | if (!e) | |
2023 | return -ENOMEM; | |
2024 | ||
2025 | if (!strextend(&s, e, NULL)) | |
2026 | return -ENOMEM; | |
2027 | ||
2028 | *ret = s; | |
2029 | s = NULL; | |
2030 | ||
2031 | return 0; | |
2032 | } | |
2033 | ||
2034 | int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) { | |
2035 | _cleanup_free_ char *p = NULL; | |
2036 | int r; | |
2037 | ||
2038 | r = cg_get_path(controller, path, attribute, &p); | |
2039 | if (r < 0) | |
2040 | return r; | |
2041 | ||
2042 | return write_string_file(p, value, 0); | |
2043 | } | |
2044 | ||
2045 | int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) { | |
2046 | _cleanup_free_ char *p = NULL; | |
2047 | int r; | |
2048 | ||
2049 | r = cg_get_path(controller, path, attribute, &p); | |
2050 | if (r < 0) | |
2051 | return r; | |
2052 | ||
2053 | return read_one_line_file(p, ret); | |
2054 | } | |
2055 | ||
2056 | int cg_get_keyed_attribute(const char *controller, const char *path, const char *attribute, const char **keys, char **values) { | |
2057 | _cleanup_free_ char *filename = NULL, *content = NULL; | |
2058 | char *line, *p; | |
2059 | int i, r; | |
2060 | ||
2061 | for (i = 0; keys[i]; i++) | |
2062 | values[i] = NULL; | |
2063 | ||
2064 | r = cg_get_path(controller, path, attribute, &filename); | |
2065 | if (r < 0) | |
2066 | return r; | |
2067 | ||
2068 | r = read_full_file(filename, &content, NULL); | |
2069 | if (r < 0) | |
2070 | return r; | |
2071 | ||
2072 | p = content; | |
2073 | while ((line = strsep(&p, "\n"))) { | |
2074 | char *key; | |
2075 | ||
2076 | key = strsep(&line, " "); | |
2077 | ||
2078 | for (i = 0; keys[i]; i++) { | |
2079 | if (streq(key, keys[i])) { | |
2080 | values[i] = strdup(line); | |
2081 | break; | |
2082 | } | |
2083 | } | |
2084 | } | |
2085 | ||
2086 | for (i = 0; keys[i]; i++) { | |
2087 | if (!values[i]) { | |
2088 | for (i = 0; keys[i]; i++) { | |
2089 | free(values[i]); | |
2090 | values[i] = NULL; | |
2091 | } | |
2092 | return -ENOENT; | |
2093 | } | |
2094 | } | |
2095 | ||
2096 | return 0; | |
2097 | } | |
2098 | ||
2099 | int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path) { | |
2100 | CGroupController c; | |
2101 | int r; | |
2102 | ||
2103 | /* This one will create a cgroup in our private tree, but also | |
2104 | * duplicate it in the trees specified in mask, and remove it | |
2105 | * in all others */ | |
2106 | ||
2107 | /* First create the cgroup in our own hierarchy. */ | |
2108 | r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path); | |
2109 | if (r < 0) | |
2110 | return r; | |
2111 | ||
2112 | /* If we are in the unified hierarchy, we are done now */ | |
2113 | r = cg_all_unified(); | |
2114 | if (r < 0) | |
2115 | return r; | |
2116 | if (r > 0) | |
2117 | return 0; | |
2118 | ||
2119 | /* Otherwise, do the same in the other hierarchies */ | |
2120 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { | |
2121 | CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c); | |
2122 | const char *n; | |
2123 | ||
2124 | n = cgroup_controller_to_string(c); | |
2125 | ||
2126 | if (mask & bit) | |
2127 | (void) cg_create(n, path); | |
2128 | else if (supported & bit) | |
2129 | (void) cg_trim(n, path, true); | |
2130 | } | |
2131 | ||
2132 | return 0; | |
2133 | } | |
2134 | ||
2135 | int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) { | |
2136 | CGroupController c; | |
2137 | int r; | |
2138 | ||
2139 | r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid); | |
2140 | if (r < 0) | |
2141 | return r; | |
2142 | ||
2143 | r = cg_all_unified(); | |
2144 | if (r < 0) | |
2145 | return r; | |
2146 | if (r > 0) | |
2147 | return 0; | |
2148 | ||
2149 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { | |
2150 | CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c); | |
2151 | const char *p = NULL; | |
2152 | ||
2153 | if (!(supported & bit)) | |
2154 | continue; | |
2155 | ||
2156 | if (path_callback) | |
2157 | p = path_callback(bit, userdata); | |
2158 | ||
2159 | if (!p) | |
2160 | p = path; | |
2161 | ||
2162 | (void) cg_attach_fallback(cgroup_controller_to_string(c), p, pid); | |
2163 | } | |
2164 | ||
2165 | return 0; | |
2166 | } | |
2167 | ||
2168 | int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) { | |
2169 | Iterator i; | |
2170 | void *pidp; | |
2171 | int r = 0; | |
2172 | ||
2173 | SET_FOREACH(pidp, pids, i) { | |
2174 | pid_t pid = PTR_TO_PID(pidp); | |
2175 | int q; | |
2176 | ||
2177 | q = cg_attach_everywhere(supported, path, pid, path_callback, userdata); | |
2178 | if (q < 0 && r >= 0) | |
2179 | r = q; | |
2180 | } | |
2181 | ||
2182 | return r; | |
2183 | } | |
2184 | ||
2185 | int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) { | |
2186 | CGroupController c; | |
2187 | int r = 0, q; | |
2188 | ||
2189 | if (!path_equal(from, to)) { | |
2190 | r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, CGROUP_REMOVE); | |
2191 | if (r < 0) | |
2192 | return r; | |
2193 | } | |
2194 | ||
2195 | q = cg_all_unified(); | |
2196 | if (q < 0) | |
2197 | return q; | |
2198 | if (q > 0) | |
2199 | return r; | |
2200 | ||
2201 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { | |
2202 | CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c); | |
2203 | const char *p = NULL; | |
2204 | ||
2205 | if (!(supported & bit)) | |
2206 | continue; | |
2207 | ||
2208 | if (to_callback) | |
2209 | p = to_callback(bit, userdata); | |
2210 | ||
2211 | if (!p) | |
2212 | p = to; | |
2213 | ||
2214 | (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, cgroup_controller_to_string(c), p, 0); | |
2215 | } | |
2216 | ||
2217 | return 0; | |
2218 | } | |
2219 | ||
2220 | int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root) { | |
2221 | CGroupController c; | |
2222 | int r, q; | |
2223 | ||
2224 | r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root); | |
2225 | if (r < 0) | |
2226 | return r; | |
2227 | ||
2228 | q = cg_all_unified(); | |
2229 | if (q < 0) | |
2230 | return q; | |
2231 | if (q > 0) | |
2232 | return r; | |
2233 | ||
2234 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { | |
2235 | CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c); | |
2236 | ||
2237 | if (!(supported & bit)) | |
2238 | continue; | |
2239 | ||
2240 | (void) cg_trim(cgroup_controller_to_string(c), path, delete_root); | |
2241 | } | |
2242 | ||
2243 | return 0; | |
2244 | } | |
2245 | ||
2246 | int cg_mask_to_string(CGroupMask mask, char **ret) { | |
2247 | _cleanup_free_ char *s = NULL; | |
2248 | size_t n = 0, allocated = 0; | |
2249 | bool space = false; | |
2250 | CGroupController c; | |
2251 | ||
2252 | assert(ret); | |
2253 | ||
2254 | if (mask == 0) { | |
2255 | *ret = NULL; | |
2256 | return 0; | |
2257 | } | |
2258 | ||
2259 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { | |
2260 | const char *k; | |
2261 | size_t l; | |
2262 | ||
2263 | if (!(mask & CGROUP_CONTROLLER_TO_MASK(c))) | |
2264 | continue; | |
2265 | ||
2266 | k = cgroup_controller_to_string(c); | |
2267 | l = strlen(k); | |
2268 | ||
2269 | if (!GREEDY_REALLOC(s, allocated, n + space + l + 1)) | |
2270 | return -ENOMEM; | |
2271 | ||
2272 | if (space) | |
2273 | s[n] = ' '; | |
2274 | memcpy(s + n + space, k, l); | |
2275 | n += space + l; | |
2276 | ||
2277 | space = true; | |
2278 | } | |
2279 | ||
2280 | assert(s); | |
2281 | ||
2282 | s[n] = 0; | |
2283 | *ret = s; | |
2284 | s = NULL; | |
2285 | ||
2286 | return 0; | |
2287 | } | |
2288 | ||
2289 | int cg_mask_from_string(const char *value, CGroupMask *mask) { | |
2290 | assert(mask); | |
2291 | assert(value); | |
2292 | ||
2293 | for (;;) { | |
2294 | _cleanup_free_ char *n = NULL; | |
2295 | CGroupController v; | |
2296 | int r; | |
2297 | ||
2298 | r = extract_first_word(&value, &n, NULL, 0); | |
2299 | if (r < 0) | |
2300 | return r; | |
2301 | if (r == 0) | |
2302 | break; | |
2303 | ||
2304 | v = cgroup_controller_from_string(n); | |
2305 | if (v < 0) | |
2306 | continue; | |
2307 | ||
2308 | *mask |= CGROUP_CONTROLLER_TO_MASK(v); | |
2309 | } | |
2310 | return 0; | |
2311 | } | |
2312 | ||
2313 | int cg_mask_supported(CGroupMask *ret) { | |
2314 | CGroupMask mask = 0; | |
2315 | int r; | |
2316 | ||
2317 | /* Determines the mask of supported cgroup controllers. Only | |
2318 | * includes controllers we can make sense of and that are | |
2319 | * actually accessible. */ | |
2320 | ||
2321 | r = cg_all_unified(); | |
2322 | if (r < 0) | |
2323 | return r; | |
2324 | if (r > 0) { | |
2325 | _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL; | |
2326 | ||
2327 | /* In the unified hierarchy we can read the supported | |
2328 | * and accessible controllers from a the top-level | |
2329 | * cgroup attribute */ | |
2330 | ||
2331 | r = cg_get_root_path(&root); | |
2332 | if (r < 0) | |
2333 | return r; | |
2334 | ||
2335 | r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path); | |
2336 | if (r < 0) | |
2337 | return r; | |
2338 | ||
2339 | r = read_one_line_file(path, &controllers); | |
2340 | if (r < 0) | |
2341 | return r; | |
2342 | ||
2343 | r = cg_mask_from_string(controllers, &mask); | |
2344 | if (r < 0) | |
2345 | return r; | |
2346 | ||
2347 | /* Currently, we support the cpu, memory, io and pids | |
2348 | * controller in the unified hierarchy, mask | |
2349 | * everything else off. */ | |
2350 | mask &= CGROUP_MASK_CPU | CGROUP_MASK_MEMORY | CGROUP_MASK_IO | CGROUP_MASK_PIDS; | |
2351 | ||
2352 | } else { | |
2353 | CGroupController c; | |
2354 | ||
2355 | /* In the legacy hierarchy, we check whether which | |
2356 | * hierarchies are mounted. */ | |
2357 | ||
2358 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { | |
2359 | const char *n; | |
2360 | ||
2361 | n = cgroup_controller_to_string(c); | |
2362 | if (controller_is_accessible(n) >= 0) | |
2363 | mask |= CGROUP_CONTROLLER_TO_MASK(c); | |
2364 | } | |
2365 | } | |
2366 | ||
2367 | *ret = mask; | |
2368 | return 0; | |
2369 | } | |
2370 | ||
2371 | int cg_kernel_controllers(Set *controllers) { | |
2372 | _cleanup_fclose_ FILE *f = NULL; | |
2373 | int r; | |
2374 | ||
2375 | assert(controllers); | |
2376 | ||
2377 | /* Determines the full list of kernel-known controllers. Might | |
2378 | * include controllers we don't actually support, arbitrary | |
2379 | * named hierarchies and controllers that aren't currently | |
2380 | * accessible (because not mounted). */ | |
2381 | ||
2382 | f = fopen("/proc/cgroups", "re"); | |
2383 | if (!f) { | |
2384 | if (errno == ENOENT) | |
2385 | return 0; | |
2386 | return -errno; | |
2387 | } | |
2388 | ||
2389 | /* Ignore the header line */ | |
2390 | (void) read_line(f, (size_t) -1, NULL); | |
2391 | ||
2392 | for (;;) { | |
2393 | char *controller; | |
2394 | int enabled = 0; | |
2395 | ||
2396 | errno = 0; | |
2397 | if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) { | |
2398 | ||
2399 | if (feof(f)) | |
2400 | break; | |
2401 | ||
2402 | if (ferror(f) && errno > 0) | |
2403 | return -errno; | |
2404 | ||
2405 | return -EBADMSG; | |
2406 | } | |
2407 | ||
2408 | if (!enabled) { | |
2409 | free(controller); | |
2410 | continue; | |
2411 | } | |
2412 | ||
2413 | if (!cg_controller_is_valid(controller)) { | |
2414 | free(controller); | |
2415 | return -EBADMSG; | |
2416 | } | |
2417 | ||
2418 | r = set_consume(controllers, controller); | |
2419 | if (r < 0) | |
2420 | return r; | |
2421 | } | |
2422 | ||
2423 | return 0; | |
2424 | } | |
2425 | ||
2426 | static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN; | |
2427 | ||
2428 | /* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd. This | |
2429 | * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on | |
2430 | * /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains | |
2431 | * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools. | |
2432 | * | |
2433 | * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep cgroup v2 | |
2434 | * process management but disable the compat dual layout, we return %true on | |
2435 | * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified(). | |
2436 | */ | |
2437 | static thread_local bool unified_systemd_v232; | |
2438 | ||
2439 | static int cg_unified_update(void) { | |
2440 | ||
2441 | struct statfs fs; | |
2442 | ||
2443 | /* Checks if we support the unified hierarchy. Returns an | |
2444 | * error when the cgroup hierarchies aren't mounted yet or we | |
2445 | * have any other trouble determining if the unified hierarchy | |
2446 | * is supported. */ | |
2447 | ||
2448 | if (unified_cache >= CGROUP_UNIFIED_NONE) | |
2449 | return 0; | |
2450 | ||
2451 | if (statfs("/sys/fs/cgroup/", &fs) < 0) | |
2452 | return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/\" failed: %m"); | |
2453 | ||
2454 | if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) { | |
2455 | log_debug("Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy"); | |
2456 | unified_cache = CGROUP_UNIFIED_ALL; | |
2457 | } else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) { | |
2458 | if (statfs("/sys/fs/cgroup/unified/", &fs) == 0 && | |
2459 | F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) { | |
2460 | log_debug("Found cgroup2 on /sys/fs/cgroup/unified, unified hierarchy for systemd controller"); | |
2461 | unified_cache = CGROUP_UNIFIED_SYSTEMD; | |
2462 | unified_systemd_v232 = false; | |
2463 | } else { | |
2464 | if (statfs("/sys/fs/cgroup/systemd/", &fs) < 0) | |
2465 | return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/systemd\" failed: %m"); | |
2466 | ||
2467 | if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) { | |
2468 | log_debug("Found cgroup2 on /sys/fs/cgroup/systemd, unified hierarchy for systemd controller (v232 variant)"); | |
2469 | unified_cache = CGROUP_UNIFIED_SYSTEMD; | |
2470 | unified_systemd_v232 = true; | |
2471 | } else if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC)) { | |
2472 | log_debug("Found cgroup on /sys/fs/cgroup/systemd, legacy hierarchy"); | |
2473 | unified_cache = CGROUP_UNIFIED_NONE; | |
2474 | } else { | |
2475 | log_debug("Unexpected filesystem type %llx mounted on /sys/fs/cgroup/systemd, assuming legacy hierarchy", | |
2476 | (unsigned long long) fs.f_type); | |
2477 | unified_cache = CGROUP_UNIFIED_NONE; | |
2478 | } | |
2479 | } | |
2480 | } else { | |
2481 | log_debug("Unknown filesystem type %llx mounted on /sys/fs/cgroup.", | |
2482 | (unsigned long long) fs.f_type); | |
2483 | return -ENOMEDIUM; | |
2484 | } | |
2485 | ||
2486 | return 0; | |
2487 | } | |
2488 | ||
2489 | int cg_unified_controller(const char *controller) { | |
2490 | int r; | |
2491 | ||
2492 | r = cg_unified_update(); | |
2493 | if (r < 0) | |
2494 | return r; | |
2495 | ||
2496 | if (unified_cache == CGROUP_UNIFIED_NONE) | |
2497 | return false; | |
2498 | ||
2499 | if (unified_cache >= CGROUP_UNIFIED_ALL) | |
2500 | return true; | |
2501 | ||
2502 | return streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER); | |
2503 | } | |
2504 | ||
2505 | int cg_all_unified(void) { | |
2506 | int r; | |
2507 | ||
2508 | r = cg_unified_update(); | |
2509 | if (r < 0) | |
2510 | return r; | |
2511 | ||
2512 | return unified_cache >= CGROUP_UNIFIED_ALL; | |
2513 | } | |
2514 | ||
2515 | int cg_hybrid_unified(void) { | |
2516 | int r; | |
2517 | ||
2518 | r = cg_unified_update(); | |
2519 | if (r < 0) | |
2520 | return r; | |
2521 | ||
2522 | return unified_cache == CGROUP_UNIFIED_SYSTEMD && !unified_systemd_v232; | |
2523 | } | |
2524 | ||
2525 | int cg_unified_flush(void) { | |
2526 | unified_cache = CGROUP_UNIFIED_UNKNOWN; | |
2527 | ||
2528 | return cg_unified_update(); | |
2529 | } | |
2530 | ||
2531 | int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) { | |
2532 | _cleanup_free_ char *fs = NULL; | |
2533 | CGroupController c; | |
2534 | int r; | |
2535 | ||
2536 | assert(p); | |
2537 | ||
2538 | if (supported == 0) | |
2539 | return 0; | |
2540 | ||
2541 | r = cg_all_unified(); | |
2542 | if (r < 0) | |
2543 | return r; | |
2544 | if (r == 0) /* on the legacy hiearchy there's no joining of controllers defined */ | |
2545 | return 0; | |
2546 | ||
2547 | r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, p, "cgroup.subtree_control", &fs); | |
2548 | if (r < 0) | |
2549 | return r; | |
2550 | ||
2551 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { | |
2552 | CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c); | |
2553 | const char *n; | |
2554 | ||
2555 | if (!(supported & bit)) | |
2556 | continue; | |
2557 | ||
2558 | n = cgroup_controller_to_string(c); | |
2559 | { | |
2560 | char s[1 + strlen(n) + 1]; | |
2561 | ||
2562 | s[0] = mask & bit ? '+' : '-'; | |
2563 | strcpy(s + 1, n); | |
2564 | ||
2565 | r = write_string_file(fs, s, 0); | |
2566 | if (r < 0) | |
2567 | log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs); | |
2568 | } | |
2569 | } | |
2570 | ||
2571 | return 0; | |
2572 | } | |
2573 | ||
2574 | bool cg_is_unified_wanted(void) { | |
2575 | static thread_local int wanted = -1; | |
2576 | int r; | |
2577 | bool b; | |
2578 | const bool is_default = DEFAULT_HIERARCHY == CGROUP_UNIFIED_ALL; | |
2579 | ||
2580 | /* If we have a cached value, return that. */ | |
2581 | if (wanted >= 0) | |
2582 | return wanted; | |
2583 | ||
2584 | /* If the hierarchy is already mounted, then follow whatever | |
2585 | * was chosen for it. */ | |
2586 | if (cg_unified_flush() >= 0) | |
2587 | return (wanted = unified_cache >= CGROUP_UNIFIED_ALL); | |
2588 | ||
2589 | /* Otherwise, let's see what the kernel command line has to say. | |
2590 | * Since checking is expensive, cache a non-error result. */ | |
2591 | r = proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b); | |
2592 | ||
2593 | return (wanted = r > 0 ? b : is_default); | |
2594 | } | |
2595 | ||
2596 | bool cg_is_legacy_wanted(void) { | |
2597 | static thread_local int wanted = -1; | |
2598 | ||
2599 | /* If we have a cached value, return that. */ | |
2600 | if (wanted >= 0) | |
2601 | return wanted; | |
2602 | ||
2603 | /* Check if we have cgroups2 already mounted. */ | |
2604 | if (cg_unified_flush() >= 0 && | |
2605 | unified_cache == CGROUP_UNIFIED_ALL) | |
2606 | return (wanted = false); | |
2607 | ||
2608 | /* Otherwise, assume that at least partial legacy is wanted, | |
2609 | * since cgroups2 should already be mounted at this point. */ | |
2610 | return (wanted = true); | |
2611 | } | |
2612 | ||
2613 | bool cg_is_hybrid_wanted(void) { | |
2614 | static thread_local int wanted = -1; | |
2615 | int r; | |
2616 | bool b; | |
2617 | const bool is_default = DEFAULT_HIERARCHY >= CGROUP_UNIFIED_SYSTEMD; | |
2618 | /* We default to true if the default is "hybrid", obviously, | |
2619 | * but also when the default is "unified", because if we get | |
2620 | * called, it means that unified hierarchy was not mounted. */ | |
2621 | ||
2622 | /* If we have a cached value, return that. */ | |
2623 | if (wanted >= 0) | |
2624 | return wanted; | |
2625 | ||
2626 | /* If the hierarchy is already mounted, then follow whatever | |
2627 | * was chosen for it. */ | |
2628 | if (cg_unified_flush() >= 0 && | |
2629 | unified_cache == CGROUP_UNIFIED_ALL) | |
2630 | return (wanted = false); | |
2631 | ||
2632 | /* Otherwise, let's see what the kernel command line has to say. | |
2633 | * Since checking is expensive, cache a non-error result. */ | |
2634 | r = proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b); | |
2635 | ||
2636 | /* The meaning of the kernel option is reversed wrt. to the return value | |
2637 | * of this function, hence the negation. */ | |
2638 | return (wanted = r > 0 ? !b : is_default); | |
2639 | } | |
2640 | ||
2641 | int cg_weight_parse(const char *s, uint64_t *ret) { | |
2642 | uint64_t u; | |
2643 | int r; | |
2644 | ||
2645 | if (isempty(s)) { | |
2646 | *ret = CGROUP_WEIGHT_INVALID; | |
2647 | return 0; | |
2648 | } | |
2649 | ||
2650 | r = safe_atou64(s, &u); | |
2651 | if (r < 0) | |
2652 | return r; | |
2653 | ||
2654 | if (u < CGROUP_WEIGHT_MIN || u > CGROUP_WEIGHT_MAX) | |
2655 | return -ERANGE; | |
2656 | ||
2657 | *ret = u; | |
2658 | return 0; | |
2659 | } | |
2660 | ||
2661 | const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX] = { | |
2662 | [CGROUP_IO_RBPS_MAX] = CGROUP_LIMIT_MAX, | |
2663 | [CGROUP_IO_WBPS_MAX] = CGROUP_LIMIT_MAX, | |
2664 | [CGROUP_IO_RIOPS_MAX] = CGROUP_LIMIT_MAX, | |
2665 | [CGROUP_IO_WIOPS_MAX] = CGROUP_LIMIT_MAX, | |
2666 | }; | |
2667 | ||
2668 | static const char* const cgroup_io_limit_type_table[_CGROUP_IO_LIMIT_TYPE_MAX] = { | |
2669 | [CGROUP_IO_RBPS_MAX] = "IOReadBandwidthMax", | |
2670 | [CGROUP_IO_WBPS_MAX] = "IOWriteBandwidthMax", | |
2671 | [CGROUP_IO_RIOPS_MAX] = "IOReadIOPSMax", | |
2672 | [CGROUP_IO_WIOPS_MAX] = "IOWriteIOPSMax", | |
2673 | }; | |
2674 | ||
2675 | DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type, CGroupIOLimitType); | |
2676 | ||
2677 | int cg_cpu_shares_parse(const char *s, uint64_t *ret) { | |
2678 | uint64_t u; | |
2679 | int r; | |
2680 | ||
2681 | if (isempty(s)) { | |
2682 | *ret = CGROUP_CPU_SHARES_INVALID; | |
2683 | return 0; | |
2684 | } | |
2685 | ||
2686 | r = safe_atou64(s, &u); | |
2687 | if (r < 0) | |
2688 | return r; | |
2689 | ||
2690 | if (u < CGROUP_CPU_SHARES_MIN || u > CGROUP_CPU_SHARES_MAX) | |
2691 | return -ERANGE; | |
2692 | ||
2693 | *ret = u; | |
2694 | return 0; | |
2695 | } | |
2696 | ||
2697 | int cg_blkio_weight_parse(const char *s, uint64_t *ret) { | |
2698 | uint64_t u; | |
2699 | int r; | |
2700 | ||
2701 | if (isempty(s)) { | |
2702 | *ret = CGROUP_BLKIO_WEIGHT_INVALID; | |
2703 | return 0; | |
2704 | } | |
2705 | ||
2706 | r = safe_atou64(s, &u); | |
2707 | if (r < 0) | |
2708 | return r; | |
2709 | ||
2710 | if (u < CGROUP_BLKIO_WEIGHT_MIN || u > CGROUP_BLKIO_WEIGHT_MAX) | |
2711 | return -ERANGE; | |
2712 | ||
2713 | *ret = u; | |
2714 | return 0; | |
2715 | } | |
2716 | ||
2717 | bool is_cgroup_fs(const struct statfs *s) { | |
2718 | return is_fs_type(s, CGROUP_SUPER_MAGIC) || | |
2719 | is_fs_type(s, CGROUP2_SUPER_MAGIC); | |
2720 | } | |
2721 | ||
2722 | bool fd_is_cgroup_fs(int fd) { | |
2723 | struct statfs s; | |
2724 | ||
2725 | if (fstatfs(fd, &s) < 0) | |
2726 | return -errno; | |
2727 | ||
2728 | return is_cgroup_fs(&s); | |
2729 | } | |
2730 | ||
2731 | static const char *cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = { | |
2732 | [CGROUP_CONTROLLER_CPU] = "cpu", | |
2733 | [CGROUP_CONTROLLER_CPUACCT] = "cpuacct", | |
2734 | [CGROUP_CONTROLLER_IO] = "io", | |
2735 | [CGROUP_CONTROLLER_BLKIO] = "blkio", | |
2736 | [CGROUP_CONTROLLER_MEMORY] = "memory", | |
2737 | [CGROUP_CONTROLLER_DEVICES] = "devices", | |
2738 | [CGROUP_CONTROLLER_PIDS] = "pids", | |
2739 | }; | |
2740 | ||
2741 | DEFINE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController); |