]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/cgroup-util.c
cgroup-util: minor coding style adjustment
[thirdparty/systemd.git] / src / basic / cgroup-util.c
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 && r != -ENOENT && r != -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 && r != -ENOENT && r != -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_warning_errno(r, "Failed to set group access on compat systemd cgroup %s: %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, *procs = 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 r = cg_get_path(controller, path, "cgroup.procs", &fs);
933 if (r < 0)
934 return r;
935
936 r = chmod_and_chown(fs, mode, uid, gid);
937 if (r < 0)
938 return r;
939
940 r = cg_unified_controller(controller);
941 if (r < 0)
942 return r;
943 if (r == 0) {
944 /* Compatibility, Always keep values for "tasks" in sync with
945 * "cgroup.procs" */
946 if (cg_get_path(controller, path, "tasks", &procs) >= 0)
947 (void) chmod_and_chown(procs, mode, uid, gid);
948 }
949
950 r = cg_hybrid_unified();
951 if (r < 0)
952 return r;
953 if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
954 r = cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, mode, uid, gid);
955 if (r < 0)
956 log_warning_errno(r, "Failed to set task access on compat systemd cgroup %s: %m", path);
957 }
958
959 return 0;
960 }
961
962 int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags) {
963 _cleanup_free_ char *fs = NULL;
964 int r;
965
966 assert(path);
967 assert(name);
968 assert(value || size <= 0);
969
970 r = cg_get_path(controller, path, NULL, &fs);
971 if (r < 0)
972 return r;
973
974 if (setxattr(fs, name, value, size, flags) < 0)
975 return -errno;
976
977 return 0;
978 }
979
980 int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size) {
981 _cleanup_free_ char *fs = NULL;
982 ssize_t n;
983 int r;
984
985 assert(path);
986 assert(name);
987
988 r = cg_get_path(controller, path, NULL, &fs);
989 if (r < 0)
990 return r;
991
992 n = getxattr(fs, name, value, size);
993 if (n < 0)
994 return -errno;
995
996 return (int) n;
997 }
998
999 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
1000 _cleanup_fclose_ FILE *f = NULL;
1001 char line[LINE_MAX];
1002 const char *fs, *controller_str;
1003 size_t cs = 0;
1004 int unified;
1005
1006 assert(path);
1007 assert(pid >= 0);
1008
1009 if (controller) {
1010 if (!cg_controller_is_valid(controller))
1011 return -EINVAL;
1012 } else
1013 controller = SYSTEMD_CGROUP_CONTROLLER;
1014
1015 unified = cg_unified_controller(controller);
1016 if (unified < 0)
1017 return unified;
1018 if (unified == 0) {
1019 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
1020 controller_str = SYSTEMD_CGROUP_CONTROLLER_LEGACY;
1021 else
1022 controller_str = controller;
1023
1024 cs = strlen(controller_str);
1025 }
1026
1027 fs = procfs_file_alloca(pid, "cgroup");
1028 f = fopen(fs, "re");
1029 if (!f)
1030 return errno == ENOENT ? -ESRCH : -errno;
1031
1032 FOREACH_LINE(line, f, return -errno) {
1033 char *e, *p;
1034
1035 truncate_nl(line);
1036
1037 if (unified) {
1038 e = startswith(line, "0:");
1039 if (!e)
1040 continue;
1041
1042 e = strchr(e, ':');
1043 if (!e)
1044 continue;
1045 } else {
1046 char *l;
1047 size_t k;
1048 const char *word, *state;
1049 bool found = false;
1050
1051 l = strchr(line, ':');
1052 if (!l)
1053 continue;
1054
1055 l++;
1056 e = strchr(l, ':');
1057 if (!e)
1058 continue;
1059
1060 *e = 0;
1061 FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
1062 if (k == cs && memcmp(word, controller_str, cs) == 0) {
1063 found = true;
1064 break;
1065 }
1066 }
1067
1068 if (!found)
1069 continue;
1070 }
1071
1072 p = strdup(e + 1);
1073 if (!p)
1074 return -ENOMEM;
1075
1076 *path = p;
1077 return 0;
1078 }
1079
1080 return -ENODATA;
1081 }
1082
1083 int cg_install_release_agent(const char *controller, const char *agent) {
1084 _cleanup_free_ char *fs = NULL, *contents = NULL;
1085 const char *sc;
1086 int r;
1087
1088 assert(agent);
1089
1090 r = cg_unified_controller(controller);
1091 if (r < 0)
1092 return r;
1093 if (r > 0) /* doesn't apply to unified hierarchy */
1094 return -EOPNOTSUPP;
1095
1096 r = cg_get_path(controller, NULL, "release_agent", &fs);
1097 if (r < 0)
1098 return r;
1099
1100 r = read_one_line_file(fs, &contents);
1101 if (r < 0)
1102 return r;
1103
1104 sc = strstrip(contents);
1105 if (isempty(sc)) {
1106 r = write_string_file(fs, agent, 0);
1107 if (r < 0)
1108 return r;
1109 } else if (!path_equal(sc, agent))
1110 return -EEXIST;
1111
1112 fs = mfree(fs);
1113 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
1114 if (r < 0)
1115 return r;
1116
1117 contents = mfree(contents);
1118 r = read_one_line_file(fs, &contents);
1119 if (r < 0)
1120 return r;
1121
1122 sc = strstrip(contents);
1123 if (streq(sc, "0")) {
1124 r = write_string_file(fs, "1", 0);
1125 if (r < 0)
1126 return r;
1127
1128 return 1;
1129 }
1130
1131 if (!streq(sc, "1"))
1132 return -EIO;
1133
1134 return 0;
1135 }
1136
1137 int cg_uninstall_release_agent(const char *controller) {
1138 _cleanup_free_ char *fs = NULL;
1139 int r;
1140
1141 r = cg_unified_controller(controller);
1142 if (r < 0)
1143 return r;
1144 if (r > 0) /* Doesn't apply to unified hierarchy */
1145 return -EOPNOTSUPP;
1146
1147 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
1148 if (r < 0)
1149 return r;
1150
1151 r = write_string_file(fs, "0", 0);
1152 if (r < 0)
1153 return r;
1154
1155 fs = mfree(fs);
1156
1157 r = cg_get_path(controller, NULL, "release_agent", &fs);
1158 if (r < 0)
1159 return r;
1160
1161 r = write_string_file(fs, "", 0);
1162 if (r < 0)
1163 return r;
1164
1165 return 0;
1166 }
1167
1168 int cg_is_empty(const char *controller, const char *path) {
1169 _cleanup_fclose_ FILE *f = NULL;
1170 pid_t pid;
1171 int r;
1172
1173 assert(path);
1174
1175 r = cg_enumerate_processes(controller, path, &f);
1176 if (r == -ENOENT)
1177 return 1;
1178 if (r < 0)
1179 return r;
1180
1181 r = cg_read_pid(f, &pid);
1182 if (r < 0)
1183 return r;
1184
1185 return r == 0;
1186 }
1187
1188 int cg_is_empty_recursive(const char *controller, const char *path) {
1189 int r;
1190
1191 assert(path);
1192
1193 /* The root cgroup is always populated */
1194 if (controller && (isempty(path) || path_equal(path, "/")))
1195 return false;
1196
1197 r = cg_unified_controller(controller);
1198 if (r < 0)
1199 return r;
1200 if (r > 0) {
1201 _cleanup_free_ char *t = NULL;
1202
1203 /* On the unified hierarchy we can check empty state
1204 * via the "populated" attribute of "cgroup.events". */
1205
1206 r = cg_read_event(controller, path, "populated", &t);
1207 if (r < 0)
1208 return r;
1209
1210 return streq(t, "0");
1211 } else {
1212 _cleanup_closedir_ DIR *d = NULL;
1213 char *fn;
1214
1215 r = cg_is_empty(controller, path);
1216 if (r <= 0)
1217 return r;
1218
1219 r = cg_enumerate_subgroups(controller, path, &d);
1220 if (r == -ENOENT)
1221 return 1;
1222 if (r < 0)
1223 return r;
1224
1225 while ((r = cg_read_subgroup(d, &fn)) > 0) {
1226 _cleanup_free_ char *p = NULL;
1227
1228 p = strjoin(path, "/", fn);
1229 free(fn);
1230 if (!p)
1231 return -ENOMEM;
1232
1233 r = cg_is_empty_recursive(controller, p);
1234 if (r <= 0)
1235 return r;
1236 }
1237 if (r < 0)
1238 return r;
1239
1240 return true;
1241 }
1242 }
1243
1244 int cg_split_spec(const char *spec, char **controller, char **path) {
1245 char *t = NULL, *u = NULL;
1246 const char *e;
1247
1248 assert(spec);
1249
1250 if (*spec == '/') {
1251 if (!path_is_safe(spec))
1252 return -EINVAL;
1253
1254 if (path) {
1255 t = strdup(spec);
1256 if (!t)
1257 return -ENOMEM;
1258
1259 *path = path_kill_slashes(t);
1260 }
1261
1262 if (controller)
1263 *controller = NULL;
1264
1265 return 0;
1266 }
1267
1268 e = strchr(spec, ':');
1269 if (!e) {
1270 if (!cg_controller_is_valid(spec))
1271 return -EINVAL;
1272
1273 if (controller) {
1274 t = strdup(spec);
1275 if (!t)
1276 return -ENOMEM;
1277
1278 *controller = t;
1279 }
1280
1281 if (path)
1282 *path = NULL;
1283
1284 return 0;
1285 }
1286
1287 t = strndup(spec, e-spec);
1288 if (!t)
1289 return -ENOMEM;
1290 if (!cg_controller_is_valid(t)) {
1291 free(t);
1292 return -EINVAL;
1293 }
1294
1295 if (isempty(e+1))
1296 u = NULL;
1297 else {
1298 u = strdup(e+1);
1299 if (!u) {
1300 free(t);
1301 return -ENOMEM;
1302 }
1303
1304 if (!path_is_safe(u) ||
1305 !path_is_absolute(u)) {
1306 free(t);
1307 free(u);
1308 return -EINVAL;
1309 }
1310
1311 path_kill_slashes(u);
1312 }
1313
1314 if (controller)
1315 *controller = t;
1316 else
1317 free(t);
1318
1319 if (path)
1320 *path = u;
1321 else
1322 free(u);
1323
1324 return 0;
1325 }
1326
1327 int cg_mangle_path(const char *path, char **result) {
1328 _cleanup_free_ char *c = NULL, *p = NULL;
1329 char *t;
1330 int r;
1331
1332 assert(path);
1333 assert(result);
1334
1335 /* First, check if it already is a filesystem path */
1336 if (path_startswith(path, "/sys/fs/cgroup")) {
1337
1338 t = strdup(path);
1339 if (!t)
1340 return -ENOMEM;
1341
1342 *result = path_kill_slashes(t);
1343 return 0;
1344 }
1345
1346 /* Otherwise, treat it as cg spec */
1347 r = cg_split_spec(path, &c, &p);
1348 if (r < 0)
1349 return r;
1350
1351 return cg_get_path(c ?: SYSTEMD_CGROUP_CONTROLLER, p ?: "/", NULL, result);
1352 }
1353
1354 int cg_get_root_path(char **path) {
1355 char *p, *e;
1356 int r;
1357
1358 assert(path);
1359
1360 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1361 if (r < 0)
1362 return r;
1363
1364 e = endswith(p, "/" SPECIAL_INIT_SCOPE);
1365 if (!e)
1366 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */
1367 if (!e)
1368 e = endswith(p, "/system"); /* even more legacy */
1369 if (e)
1370 *e = 0;
1371
1372 *path = p;
1373 return 0;
1374 }
1375
1376 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1377 _cleanup_free_ char *rt = NULL;
1378 char *p;
1379 int r;
1380
1381 assert(cgroup);
1382 assert(shifted);
1383
1384 if (!root) {
1385 /* If the root was specified let's use that, otherwise
1386 * let's determine it from PID 1 */
1387
1388 r = cg_get_root_path(&rt);
1389 if (r < 0)
1390 return r;
1391
1392 root = rt;
1393 }
1394
1395 p = path_startswith(cgroup, root);
1396 if (p && p > cgroup)
1397 *shifted = p - 1;
1398 else
1399 *shifted = cgroup;
1400
1401 return 0;
1402 }
1403
1404 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1405 _cleanup_free_ char *raw = NULL;
1406 const char *c;
1407 int r;
1408
1409 assert(pid >= 0);
1410 assert(cgroup);
1411
1412 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
1413 if (r < 0)
1414 return r;
1415
1416 r = cg_shift_path(raw, root, &c);
1417 if (r < 0)
1418 return r;
1419
1420 if (c == raw) {
1421 *cgroup = raw;
1422 raw = NULL;
1423 } else {
1424 char *n;
1425
1426 n = strdup(c);
1427 if (!n)
1428 return -ENOMEM;
1429
1430 *cgroup = n;
1431 }
1432
1433 return 0;
1434 }
1435
1436 int cg_path_decode_unit(const char *cgroup, char **unit) {
1437 char *c, *s;
1438 size_t n;
1439
1440 assert(cgroup);
1441 assert(unit);
1442
1443 n = strcspn(cgroup, "/");
1444 if (n < 3)
1445 return -ENXIO;
1446
1447 c = strndupa(cgroup, n);
1448 c = cg_unescape(c);
1449
1450 if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
1451 return -ENXIO;
1452
1453 s = strdup(c);
1454 if (!s)
1455 return -ENOMEM;
1456
1457 *unit = s;
1458 return 0;
1459 }
1460
1461 static bool valid_slice_name(const char *p, size_t n) {
1462
1463 if (!p)
1464 return false;
1465
1466 if (n < strlen("x.slice"))
1467 return false;
1468
1469 if (memcmp(p + n - 6, ".slice", 6) == 0) {
1470 char buf[n+1], *c;
1471
1472 memcpy(buf, p, n);
1473 buf[n] = 0;
1474
1475 c = cg_unescape(buf);
1476
1477 return unit_name_is_valid(c, UNIT_NAME_PLAIN);
1478 }
1479
1480 return false;
1481 }
1482
1483 static const char *skip_slices(const char *p) {
1484 assert(p);
1485
1486 /* Skips over all slice assignments */
1487
1488 for (;;) {
1489 size_t n;
1490
1491 p += strspn(p, "/");
1492
1493 n = strcspn(p, "/");
1494 if (!valid_slice_name(p, n))
1495 return p;
1496
1497 p += n;
1498 }
1499 }
1500
1501 int cg_path_get_unit(const char *path, char **ret) {
1502 const char *e;
1503 char *unit;
1504 int r;
1505
1506 assert(path);
1507 assert(ret);
1508
1509 e = skip_slices(path);
1510
1511 r = cg_path_decode_unit(e, &unit);
1512 if (r < 0)
1513 return r;
1514
1515 /* We skipped over the slices, don't accept any now */
1516 if (endswith(unit, ".slice")) {
1517 free(unit);
1518 return -ENXIO;
1519 }
1520
1521 *ret = unit;
1522 return 0;
1523 }
1524
1525 int cg_pid_get_unit(pid_t pid, char **unit) {
1526 _cleanup_free_ char *cgroup = NULL;
1527 int r;
1528
1529 assert(unit);
1530
1531 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1532 if (r < 0)
1533 return r;
1534
1535 return cg_path_get_unit(cgroup, unit);
1536 }
1537
1538 /**
1539 * Skip session-*.scope, but require it to be there.
1540 */
1541 static const char *skip_session(const char *p) {
1542 size_t n;
1543
1544 if (isempty(p))
1545 return NULL;
1546
1547 p += strspn(p, "/");
1548
1549 n = strcspn(p, "/");
1550 if (n < strlen("session-x.scope"))
1551 return NULL;
1552
1553 if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1554 char buf[n - 8 - 6 + 1];
1555
1556 memcpy(buf, p + 8, n - 8 - 6);
1557 buf[n - 8 - 6] = 0;
1558
1559 /* Note that session scopes never need unescaping,
1560 * since they cannot conflict with the kernel's own
1561 * names, hence we don't need to call cg_unescape()
1562 * here. */
1563
1564 if (!session_id_valid(buf))
1565 return false;
1566
1567 p += n;
1568 p += strspn(p, "/");
1569 return p;
1570 }
1571
1572 return NULL;
1573 }
1574
1575 /**
1576 * Skip user@*.service, but require it to be there.
1577 */
1578 static const char *skip_user_manager(const char *p) {
1579 size_t n;
1580
1581 if (isempty(p))
1582 return NULL;
1583
1584 p += strspn(p, "/");
1585
1586 n = strcspn(p, "/");
1587 if (n < strlen("user@x.service"))
1588 return NULL;
1589
1590 if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1591 char buf[n - 5 - 8 + 1];
1592
1593 memcpy(buf, p + 5, n - 5 - 8);
1594 buf[n - 5 - 8] = 0;
1595
1596 /* Note that user manager services never need unescaping,
1597 * since they cannot conflict with the kernel's own
1598 * names, hence we don't need to call cg_unescape()
1599 * here. */
1600
1601 if (parse_uid(buf, NULL) < 0)
1602 return NULL;
1603
1604 p += n;
1605 p += strspn(p, "/");
1606
1607 return p;
1608 }
1609
1610 return NULL;
1611 }
1612
1613 static const char *skip_user_prefix(const char *path) {
1614 const char *e, *t;
1615
1616 assert(path);
1617
1618 /* Skip slices, if there are any */
1619 e = skip_slices(path);
1620
1621 /* Skip the user manager, if it's in the path now... */
1622 t = skip_user_manager(e);
1623 if (t)
1624 return t;
1625
1626 /* Alternatively skip the user session if it is in the path... */
1627 return skip_session(e);
1628 }
1629
1630 int cg_path_get_user_unit(const char *path, char **ret) {
1631 const char *t;
1632
1633 assert(path);
1634 assert(ret);
1635
1636 t = skip_user_prefix(path);
1637 if (!t)
1638 return -ENXIO;
1639
1640 /* And from here on it looks pretty much the same as for a
1641 * system unit, hence let's use the same parser from here
1642 * on. */
1643 return cg_path_get_unit(t, ret);
1644 }
1645
1646 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1647 _cleanup_free_ char *cgroup = NULL;
1648 int r;
1649
1650 assert(unit);
1651
1652 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1653 if (r < 0)
1654 return r;
1655
1656 return cg_path_get_user_unit(cgroup, unit);
1657 }
1658
1659 int cg_path_get_machine_name(const char *path, char **machine) {
1660 _cleanup_free_ char *u = NULL;
1661 const char *sl;
1662 int r;
1663
1664 r = cg_path_get_unit(path, &u);
1665 if (r < 0)
1666 return r;
1667
1668 sl = strjoina("/run/systemd/machines/unit:", u);
1669 return readlink_malloc(sl, machine);
1670 }
1671
1672 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1673 _cleanup_free_ char *cgroup = NULL;
1674 int r;
1675
1676 assert(machine);
1677
1678 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1679 if (r < 0)
1680 return r;
1681
1682 return cg_path_get_machine_name(cgroup, machine);
1683 }
1684
1685 int cg_path_get_session(const char *path, char **session) {
1686 _cleanup_free_ char *unit = NULL;
1687 char *start, *end;
1688 int r;
1689
1690 assert(path);
1691
1692 r = cg_path_get_unit(path, &unit);
1693 if (r < 0)
1694 return r;
1695
1696 start = startswith(unit, "session-");
1697 if (!start)
1698 return -ENXIO;
1699 end = endswith(start, ".scope");
1700 if (!end)
1701 return -ENXIO;
1702
1703 *end = 0;
1704 if (!session_id_valid(start))
1705 return -ENXIO;
1706
1707 if (session) {
1708 char *rr;
1709
1710 rr = strdup(start);
1711 if (!rr)
1712 return -ENOMEM;
1713
1714 *session = rr;
1715 }
1716
1717 return 0;
1718 }
1719
1720 int cg_pid_get_session(pid_t pid, char **session) {
1721 _cleanup_free_ char *cgroup = NULL;
1722 int r;
1723
1724 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1725 if (r < 0)
1726 return r;
1727
1728 return cg_path_get_session(cgroup, session);
1729 }
1730
1731 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1732 _cleanup_free_ char *slice = NULL;
1733 char *start, *end;
1734 int r;
1735
1736 assert(path);
1737
1738 r = cg_path_get_slice(path, &slice);
1739 if (r < 0)
1740 return r;
1741
1742 start = startswith(slice, "user-");
1743 if (!start)
1744 return -ENXIO;
1745 end = endswith(start, ".slice");
1746 if (!end)
1747 return -ENXIO;
1748
1749 *end = 0;
1750 if (parse_uid(start, uid) < 0)
1751 return -ENXIO;
1752
1753 return 0;
1754 }
1755
1756 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1757 _cleanup_free_ char *cgroup = NULL;
1758 int r;
1759
1760 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1761 if (r < 0)
1762 return r;
1763
1764 return cg_path_get_owner_uid(cgroup, uid);
1765 }
1766
1767 int cg_path_get_slice(const char *p, char **slice) {
1768 const char *e = NULL;
1769
1770 assert(p);
1771 assert(slice);
1772
1773 /* Finds the right-most slice unit from the beginning, but
1774 * stops before we come to the first non-slice unit. */
1775
1776 for (;;) {
1777 size_t n;
1778
1779 p += strspn(p, "/");
1780
1781 n = strcspn(p, "/");
1782 if (!valid_slice_name(p, n)) {
1783
1784 if (!e) {
1785 char *s;
1786
1787 s = strdup(SPECIAL_ROOT_SLICE);
1788 if (!s)
1789 return -ENOMEM;
1790
1791 *slice = s;
1792 return 0;
1793 }
1794
1795 return cg_path_decode_unit(e, slice);
1796 }
1797
1798 e = p;
1799 p += n;
1800 }
1801 }
1802
1803 int cg_pid_get_slice(pid_t pid, char **slice) {
1804 _cleanup_free_ char *cgroup = NULL;
1805 int r;
1806
1807 assert(slice);
1808
1809 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1810 if (r < 0)
1811 return r;
1812
1813 return cg_path_get_slice(cgroup, slice);
1814 }
1815
1816 int cg_path_get_user_slice(const char *p, char **slice) {
1817 const char *t;
1818 assert(p);
1819 assert(slice);
1820
1821 t = skip_user_prefix(p);
1822 if (!t)
1823 return -ENXIO;
1824
1825 /* And now it looks pretty much the same as for a system
1826 * slice, so let's just use the same parser from here on. */
1827 return cg_path_get_slice(t, slice);
1828 }
1829
1830 int cg_pid_get_user_slice(pid_t pid, char **slice) {
1831 _cleanup_free_ char *cgroup = NULL;
1832 int r;
1833
1834 assert(slice);
1835
1836 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1837 if (r < 0)
1838 return r;
1839
1840 return cg_path_get_user_slice(cgroup, slice);
1841 }
1842
1843 char *cg_escape(const char *p) {
1844 bool need_prefix = false;
1845
1846 /* This implements very minimal escaping for names to be used
1847 * as file names in the cgroup tree: any name which might
1848 * conflict with a kernel name or is prefixed with '_' is
1849 * prefixed with a '_'. That way, when reading cgroup names it
1850 * is sufficient to remove a single prefixing underscore if
1851 * there is one. */
1852
1853 /* The return value of this function (unlike cg_unescape())
1854 * needs free()! */
1855
1856 if (p[0] == 0 ||
1857 p[0] == '_' ||
1858 p[0] == '.' ||
1859 streq(p, "notify_on_release") ||
1860 streq(p, "release_agent") ||
1861 streq(p, "tasks") ||
1862 startswith(p, "cgroup."))
1863 need_prefix = true;
1864 else {
1865 const char *dot;
1866
1867 dot = strrchr(p, '.');
1868 if (dot) {
1869 CGroupController c;
1870 size_t l = dot - p;
1871
1872 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1873 const char *n;
1874
1875 n = cgroup_controller_to_string(c);
1876
1877 if (l != strlen(n))
1878 continue;
1879
1880 if (memcmp(p, n, l) != 0)
1881 continue;
1882
1883 need_prefix = true;
1884 break;
1885 }
1886 }
1887 }
1888
1889 if (need_prefix)
1890 return strappend("_", p);
1891
1892 return strdup(p);
1893 }
1894
1895 char *cg_unescape(const char *p) {
1896 assert(p);
1897
1898 /* The return value of this function (unlike cg_escape())
1899 * doesn't need free()! */
1900
1901 if (p[0] == '_')
1902 return (char*) p+1;
1903
1904 return (char*) p;
1905 }
1906
1907 #define CONTROLLER_VALID \
1908 DIGITS LETTERS \
1909 "_"
1910
1911 bool cg_controller_is_valid(const char *p) {
1912 const char *t, *s;
1913
1914 if (!p)
1915 return false;
1916
1917 if (streq(p, SYSTEMD_CGROUP_CONTROLLER))
1918 return true;
1919
1920 s = startswith(p, "name=");
1921 if (s)
1922 p = s;
1923
1924 if (*p == 0 || *p == '_')
1925 return false;
1926
1927 for (t = p; *t; t++)
1928 if (!strchr(CONTROLLER_VALID, *t))
1929 return false;
1930
1931 if (t - p > FILENAME_MAX)
1932 return false;
1933
1934 return true;
1935 }
1936
1937 int cg_slice_to_path(const char *unit, char **ret) {
1938 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1939 const char *dash;
1940 int r;
1941
1942 assert(unit);
1943 assert(ret);
1944
1945 if (streq(unit, SPECIAL_ROOT_SLICE)) {
1946 char *x;
1947
1948 x = strdup("");
1949 if (!x)
1950 return -ENOMEM;
1951 *ret = x;
1952 return 0;
1953 }
1954
1955 if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
1956 return -EINVAL;
1957
1958 if (!endswith(unit, ".slice"))
1959 return -EINVAL;
1960
1961 r = unit_name_to_prefix(unit, &p);
1962 if (r < 0)
1963 return r;
1964
1965 dash = strchr(p, '-');
1966
1967 /* Don't allow initial dashes */
1968 if (dash == p)
1969 return -EINVAL;
1970
1971 while (dash) {
1972 _cleanup_free_ char *escaped = NULL;
1973 char n[dash - p + sizeof(".slice")];
1974
1975 /* Don't allow trailing or double dashes */
1976 if (dash[1] == 0 || dash[1] == '-')
1977 return -EINVAL;
1978
1979 strcpy(stpncpy(n, p, dash - p), ".slice");
1980 if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
1981 return -EINVAL;
1982
1983 escaped = cg_escape(n);
1984 if (!escaped)
1985 return -ENOMEM;
1986
1987 if (!strextend(&s, escaped, "/", NULL))
1988 return -ENOMEM;
1989
1990 dash = strchr(dash+1, '-');
1991 }
1992
1993 e = cg_escape(unit);
1994 if (!e)
1995 return -ENOMEM;
1996
1997 if (!strextend(&s, e, NULL))
1998 return -ENOMEM;
1999
2000 *ret = s;
2001 s = NULL;
2002
2003 return 0;
2004 }
2005
2006 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
2007 _cleanup_free_ char *p = NULL;
2008 int r;
2009
2010 r = cg_get_path(controller, path, attribute, &p);
2011 if (r < 0)
2012 return r;
2013
2014 return write_string_file(p, value, 0);
2015 }
2016
2017 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
2018 _cleanup_free_ char *p = NULL;
2019 int r;
2020
2021 r = cg_get_path(controller, path, attribute, &p);
2022 if (r < 0)
2023 return r;
2024
2025 return read_one_line_file(p, ret);
2026 }
2027
2028 int cg_get_keyed_attribute(const char *controller, const char *path, const char *attribute, const char **keys, char **values) {
2029 _cleanup_free_ char *filename = NULL, *content = NULL;
2030 char *line, *p;
2031 int i, r;
2032
2033 for (i = 0; keys[i]; i++)
2034 values[i] = NULL;
2035
2036 r = cg_get_path(controller, path, attribute, &filename);
2037 if (r < 0)
2038 return r;
2039
2040 r = read_full_file(filename, &content, NULL);
2041 if (r < 0)
2042 return r;
2043
2044 p = content;
2045 while ((line = strsep(&p, "\n"))) {
2046 char *key;
2047
2048 key = strsep(&line, " ");
2049
2050 for (i = 0; keys[i]; i++) {
2051 if (streq(key, keys[i])) {
2052 values[i] = strdup(line);
2053 break;
2054 }
2055 }
2056 }
2057
2058 for (i = 0; keys[i]; i++) {
2059 if (!values[i]) {
2060 for (i = 0; keys[i]; i++) {
2061 free(values[i]);
2062 values[i] = NULL;
2063 }
2064 return -ENOENT;
2065 }
2066 }
2067
2068 return 0;
2069 }
2070
2071 int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path) {
2072 CGroupController c;
2073 int r;
2074
2075 /* This one will create a cgroup in our private tree, but also
2076 * duplicate it in the trees specified in mask, and remove it
2077 * in all others */
2078
2079 /* First create the cgroup in our own hierarchy. */
2080 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
2081 if (r < 0)
2082 return r;
2083
2084 /* If we are in the unified hierarchy, we are done now */
2085 r = cg_all_unified();
2086 if (r < 0)
2087 return r;
2088 if (r > 0)
2089 return 0;
2090
2091 /* Otherwise, do the same in the other hierarchies */
2092 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2093 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2094 const char *n;
2095
2096 n = cgroup_controller_to_string(c);
2097
2098 if (mask & bit)
2099 (void) cg_create(n, path);
2100 else if (supported & bit)
2101 (void) cg_trim(n, path, true);
2102 }
2103
2104 return 0;
2105 }
2106
2107 int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
2108 CGroupController c;
2109 int r;
2110
2111 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
2112 if (r < 0)
2113 return r;
2114
2115 r = cg_all_unified();
2116 if (r < 0)
2117 return r;
2118 if (r > 0)
2119 return 0;
2120
2121 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2122 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2123 const char *p = NULL;
2124
2125 if (!(supported & bit))
2126 continue;
2127
2128 if (path_callback)
2129 p = path_callback(bit, userdata);
2130
2131 if (!p)
2132 p = path;
2133
2134 (void) cg_attach_fallback(cgroup_controller_to_string(c), p, pid);
2135 }
2136
2137 return 0;
2138 }
2139
2140 int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
2141 Iterator i;
2142 void *pidp;
2143 int r = 0;
2144
2145 SET_FOREACH(pidp, pids, i) {
2146 pid_t pid = PTR_TO_PID(pidp);
2147 int q;
2148
2149 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
2150 if (q < 0 && r >= 0)
2151 r = q;
2152 }
2153
2154 return r;
2155 }
2156
2157 int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
2158 CGroupController c;
2159 int r = 0, q;
2160
2161 if (!path_equal(from, to)) {
2162 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, CGROUP_REMOVE);
2163 if (r < 0)
2164 return r;
2165 }
2166
2167 q = cg_all_unified();
2168 if (q < 0)
2169 return q;
2170 if (q > 0)
2171 return r;
2172
2173 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2174 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2175 const char *p = NULL;
2176
2177 if (!(supported & bit))
2178 continue;
2179
2180 if (to_callback)
2181 p = to_callback(bit, userdata);
2182
2183 if (!p)
2184 p = to;
2185
2186 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, cgroup_controller_to_string(c), p, 0);
2187 }
2188
2189 return 0;
2190 }
2191
2192 int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root) {
2193 CGroupController c;
2194 int r, q;
2195
2196 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
2197 if (r < 0)
2198 return r;
2199
2200 q = cg_all_unified();
2201 if (q < 0)
2202 return q;
2203 if (q > 0)
2204 return r;
2205
2206 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2207 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2208
2209 if (!(supported & bit))
2210 continue;
2211
2212 (void) cg_trim(cgroup_controller_to_string(c), path, delete_root);
2213 }
2214
2215 return 0;
2216 }
2217
2218 int cg_mask_to_string(CGroupMask mask, char **ret) {
2219 const char *controllers[_CGROUP_CONTROLLER_MAX + 1];
2220 CGroupController c;
2221 int i = 0;
2222 char *s;
2223
2224 assert(ret);
2225
2226 if (mask == 0) {
2227 *ret = NULL;
2228 return 0;
2229 }
2230
2231 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2232
2233 if (!(mask & CGROUP_CONTROLLER_TO_MASK(c)))
2234 continue;
2235
2236 controllers[i++] = cgroup_controller_to_string(c);
2237 controllers[i] = NULL;
2238 }
2239
2240 s = strv_join((char **)controllers, NULL);
2241 if (!s)
2242 return -ENOMEM;
2243
2244 *ret = s;
2245 return 0;
2246 }
2247
2248 int cg_mask_from_string(const char *value, CGroupMask *mask) {
2249 assert(mask);
2250 assert(value);
2251
2252 for (;;) {
2253 _cleanup_free_ char *n = NULL;
2254 CGroupController v;
2255 int r;
2256
2257 r = extract_first_word(&value, &n, NULL, 0);
2258 if (r < 0)
2259 return r;
2260 if (r == 0)
2261 break;
2262
2263 v = cgroup_controller_from_string(n);
2264 if (v < 0)
2265 continue;
2266
2267 *mask |= CGROUP_CONTROLLER_TO_MASK(v);
2268 }
2269 return 0;
2270 }
2271
2272 int cg_mask_supported(CGroupMask *ret) {
2273 CGroupMask mask = 0;
2274 int r;
2275
2276 /* Determines the mask of supported cgroup controllers. Only
2277 * includes controllers we can make sense of and that are
2278 * actually accessible. */
2279
2280 r = cg_all_unified();
2281 if (r < 0)
2282 return r;
2283 if (r > 0) {
2284 _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
2285
2286 /* In the unified hierarchy we can read the supported
2287 * and accessible controllers from a the top-level
2288 * cgroup attribute */
2289
2290 r = cg_get_root_path(&root);
2291 if (r < 0)
2292 return r;
2293
2294 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path);
2295 if (r < 0)
2296 return r;
2297
2298 r = read_one_line_file(path, &controllers);
2299 if (r < 0)
2300 return r;
2301
2302 r = cg_mask_from_string(controllers, &mask);
2303 if (r < 0)
2304 return r;
2305
2306 /* Currently, we support the cpu, memory, io and pids
2307 * controller in the unified hierarchy, mask
2308 * everything else off. */
2309 mask &= CGROUP_MASK_CPU | CGROUP_MASK_MEMORY | CGROUP_MASK_IO | CGROUP_MASK_PIDS;
2310
2311 } else {
2312 CGroupController c;
2313
2314 /* In the legacy hierarchy, we check whether which
2315 * hierarchies are mounted. */
2316
2317 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2318 const char *n;
2319
2320 n = cgroup_controller_to_string(c);
2321 if (controller_is_accessible(n) >= 0)
2322 mask |= CGROUP_CONTROLLER_TO_MASK(c);
2323 }
2324 }
2325
2326 *ret = mask;
2327 return 0;
2328 }
2329
2330 int cg_kernel_controllers(Set *controllers) {
2331 _cleanup_fclose_ FILE *f = NULL;
2332 char buf[LINE_MAX];
2333 int r;
2334
2335 assert(controllers);
2336
2337 /* Determines the full list of kernel-known controllers. Might
2338 * include controllers we don't actually support, arbitrary
2339 * named hierarchies and controllers that aren't currently
2340 * accessible (because not mounted). */
2341
2342 f = fopen("/proc/cgroups", "re");
2343 if (!f) {
2344 if (errno == ENOENT)
2345 return 0;
2346 return -errno;
2347 }
2348
2349 /* Ignore the header line */
2350 (void) fgets(buf, sizeof(buf), f);
2351
2352 for (;;) {
2353 char *controller;
2354 int enabled = 0;
2355
2356 errno = 0;
2357 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
2358
2359 if (feof(f))
2360 break;
2361
2362 if (ferror(f) && errno > 0)
2363 return -errno;
2364
2365 return -EBADMSG;
2366 }
2367
2368 if (!enabled) {
2369 free(controller);
2370 continue;
2371 }
2372
2373 if (!cg_controller_is_valid(controller)) {
2374 free(controller);
2375 return -EBADMSG;
2376 }
2377
2378 r = set_consume(controllers, controller);
2379 if (r < 0)
2380 return r;
2381 }
2382
2383 return 0;
2384 }
2385
2386 static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN;
2387
2388 /* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd. This
2389 * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on
2390 * /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains
2391 * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools.
2392 *
2393 * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep cgroup v2
2394 * process management but disable the compat dual layout, we return %true on
2395 * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
2396 */
2397 static thread_local bool unified_systemd_v232;
2398
2399 static int cg_unified_update(void) {
2400
2401 struct statfs fs;
2402
2403 /* Checks if we support the unified hierarchy. Returns an
2404 * error when the cgroup hierarchies aren't mounted yet or we
2405 * have any other trouble determining if the unified hierarchy
2406 * is supported. */
2407
2408 if (unified_cache >= CGROUP_UNIFIED_NONE)
2409 return 0;
2410
2411 if (statfs("/sys/fs/cgroup/", &fs) < 0)
2412 return -errno;
2413
2414 if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC))
2415 unified_cache = CGROUP_UNIFIED_ALL;
2416 else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) {
2417 if (statfs("/sys/fs/cgroup/unified/", &fs) == 0 &&
2418 F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2419 unified_cache = CGROUP_UNIFIED_SYSTEMD;
2420 unified_systemd_v232 = false;
2421 } else if (statfs("/sys/fs/cgroup/systemd/", &fs) == 0 &&
2422 F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2423 unified_cache = CGROUP_UNIFIED_SYSTEMD;
2424 unified_systemd_v232 = true;
2425 } else {
2426 if (statfs("/sys/fs/cgroup/systemd/", &fs) < 0)
2427 return -errno;
2428 if (!F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC))
2429 return -ENOMEDIUM;
2430 unified_cache = CGROUP_UNIFIED_NONE;
2431 }
2432 } else
2433 return -ENOMEDIUM;
2434
2435 return 0;
2436 }
2437
2438 int cg_unified_controller(const char *controller) {
2439 int r;
2440
2441 r = cg_unified_update();
2442 if (r < 0)
2443 return r;
2444
2445 if (unified_cache == CGROUP_UNIFIED_NONE)
2446 return false;
2447
2448 if (unified_cache >= CGROUP_UNIFIED_ALL)
2449 return true;
2450
2451 return streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER);
2452 }
2453
2454 int cg_all_unified(void) {
2455 int r;
2456
2457 r = cg_unified_update();
2458 if (r < 0)
2459 return r;
2460
2461 return unified_cache >= CGROUP_UNIFIED_ALL;
2462 }
2463
2464 int cg_hybrid_unified(void) {
2465 int r;
2466
2467 r = cg_unified_update();
2468 if (r < 0)
2469 return r;
2470
2471 return unified_cache == CGROUP_UNIFIED_SYSTEMD && !unified_systemd_v232;
2472 }
2473
2474 int cg_unified_flush(void) {
2475 unified_cache = CGROUP_UNIFIED_UNKNOWN;
2476
2477 return cg_unified_update();
2478 }
2479
2480 int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
2481 _cleanup_free_ char *fs = NULL;
2482 CGroupController c;
2483 int r;
2484
2485 assert(p);
2486
2487 if (supported == 0)
2488 return 0;
2489
2490 r = cg_all_unified();
2491 if (r < 0)
2492 return r;
2493 if (r == 0) /* on the legacy hiearchy there's no joining of controllers defined */
2494 return 0;
2495
2496 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, p, "cgroup.subtree_control", &fs);
2497 if (r < 0)
2498 return r;
2499
2500 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2501 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2502 const char *n;
2503
2504 if (!(supported & bit))
2505 continue;
2506
2507 n = cgroup_controller_to_string(c);
2508 {
2509 char s[1 + strlen(n) + 1];
2510
2511 s[0] = mask & bit ? '+' : '-';
2512 strcpy(s + 1, n);
2513
2514 r = write_string_file(fs, s, 0);
2515 if (r < 0)
2516 log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs);
2517 }
2518 }
2519
2520 return 0;
2521 }
2522
2523 bool cg_is_unified_wanted(void) {
2524 static thread_local int wanted = -1;
2525 int r;
2526 bool b;
2527 const bool is_default = DEFAULT_HIERARCHY == CGROUP_UNIFIED_ALL;
2528
2529 /* If we have a cached value, return that. */
2530 if (wanted >= 0)
2531 return wanted;
2532
2533 /* If the hierarchy is already mounted, then follow whatever
2534 * was chosen for it. */
2535 if (cg_unified_flush() >= 0)
2536 return (wanted = unified_cache >= CGROUP_UNIFIED_ALL);
2537
2538 /* Otherwise, let's see what the kernel command line has to say.
2539 * Since checking is expensive, cache a non-error result. */
2540 r = proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b);
2541
2542 return (wanted = r > 0 ? b : is_default);
2543 }
2544
2545 bool cg_is_legacy_wanted(void) {
2546 static thread_local int wanted = -1;
2547
2548 /* If we have a cached value, return that. */
2549 if (wanted >= 0)
2550 return wanted;
2551
2552 /* Check if we have cgroups2 already mounted. */
2553 if (cg_unified_flush() >= 0 &&
2554 unified_cache == CGROUP_UNIFIED_ALL)
2555 return (wanted = false);
2556
2557 /* Otherwise, assume that at least partial legacy is wanted,
2558 * since cgroups2 should already be mounted at this point. */
2559 return (wanted = true);
2560 }
2561
2562 bool cg_is_hybrid_wanted(void) {
2563 static thread_local int wanted = -1;
2564 int r;
2565 bool b;
2566 const bool is_default = DEFAULT_HIERARCHY >= CGROUP_UNIFIED_SYSTEMD;
2567 /* We default to true if the default is "hybrid", obviously,
2568 * but also when the default is "unified", because if we get
2569 * called, it means that unified hierarchy was not mounted. */
2570
2571 /* If we have a cached value, return that. */
2572 if (wanted >= 0)
2573 return wanted;
2574
2575 /* If the hierarchy is already mounted, then follow whatever
2576 * was chosen for it. */
2577 if (cg_unified_flush() >= 0 &&
2578 unified_cache == CGROUP_UNIFIED_ALL)
2579 return (wanted = false);
2580
2581 /* Otherwise, let's see what the kernel command line has to say.
2582 * Since checking is expensive, cache a non-error result. */
2583 r = proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b);
2584
2585 /* The meaning of the kernel option is reversed wrt. to the return value
2586 * of this function, hence the negation. */
2587 return (wanted = r > 0 ? !b : is_default);
2588 }
2589
2590 int cg_weight_parse(const char *s, uint64_t *ret) {
2591 uint64_t u;
2592 int r;
2593
2594 if (isempty(s)) {
2595 *ret = CGROUP_WEIGHT_INVALID;
2596 return 0;
2597 }
2598
2599 r = safe_atou64(s, &u);
2600 if (r < 0)
2601 return r;
2602
2603 if (u < CGROUP_WEIGHT_MIN || u > CGROUP_WEIGHT_MAX)
2604 return -ERANGE;
2605
2606 *ret = u;
2607 return 0;
2608 }
2609
2610 const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX] = {
2611 [CGROUP_IO_RBPS_MAX] = CGROUP_LIMIT_MAX,
2612 [CGROUP_IO_WBPS_MAX] = CGROUP_LIMIT_MAX,
2613 [CGROUP_IO_RIOPS_MAX] = CGROUP_LIMIT_MAX,
2614 [CGROUP_IO_WIOPS_MAX] = CGROUP_LIMIT_MAX,
2615 };
2616
2617 static const char* const cgroup_io_limit_type_table[_CGROUP_IO_LIMIT_TYPE_MAX] = {
2618 [CGROUP_IO_RBPS_MAX] = "IOReadBandwidthMax",
2619 [CGROUP_IO_WBPS_MAX] = "IOWriteBandwidthMax",
2620 [CGROUP_IO_RIOPS_MAX] = "IOReadIOPSMax",
2621 [CGROUP_IO_WIOPS_MAX] = "IOWriteIOPSMax",
2622 };
2623
2624 DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type, CGroupIOLimitType);
2625
2626 int cg_cpu_shares_parse(const char *s, uint64_t *ret) {
2627 uint64_t u;
2628 int r;
2629
2630 if (isempty(s)) {
2631 *ret = CGROUP_CPU_SHARES_INVALID;
2632 return 0;
2633 }
2634
2635 r = safe_atou64(s, &u);
2636 if (r < 0)
2637 return r;
2638
2639 if (u < CGROUP_CPU_SHARES_MIN || u > CGROUP_CPU_SHARES_MAX)
2640 return -ERANGE;
2641
2642 *ret = u;
2643 return 0;
2644 }
2645
2646 int cg_blkio_weight_parse(const char *s, uint64_t *ret) {
2647 uint64_t u;
2648 int r;
2649
2650 if (isempty(s)) {
2651 *ret = CGROUP_BLKIO_WEIGHT_INVALID;
2652 return 0;
2653 }
2654
2655 r = safe_atou64(s, &u);
2656 if (r < 0)
2657 return r;
2658
2659 if (u < CGROUP_BLKIO_WEIGHT_MIN || u > CGROUP_BLKIO_WEIGHT_MAX)
2660 return -ERANGE;
2661
2662 *ret = u;
2663 return 0;
2664 }
2665
2666 bool is_cgroup_fs(const struct statfs *s) {
2667 return is_fs_type(s, CGROUP_SUPER_MAGIC) ||
2668 is_fs_type(s, CGROUP2_SUPER_MAGIC);
2669 }
2670
2671 bool fd_is_cgroup_fs(int fd) {
2672 struct statfs s;
2673
2674 if (fstatfs(fd, &s) < 0)
2675 return -errno;
2676
2677 return is_cgroup_fs(&s);
2678 }
2679
2680 static const char *cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = {
2681 [CGROUP_CONTROLLER_CPU] = "cpu",
2682 [CGROUP_CONTROLLER_CPUACCT] = "cpuacct",
2683 [CGROUP_CONTROLLER_IO] = "io",
2684 [CGROUP_CONTROLLER_BLKIO] = "blkio",
2685 [CGROUP_CONTROLLER_MEMORY] = "memory",
2686 [CGROUP_CONTROLLER_DEVICES] = "devices",
2687 [CGROUP_CONTROLLER_PIDS] = "pids",
2688 };
2689
2690 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController);