]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/cgroup-util.c
cgroup-util: be more strict when processing slice unit names
[thirdparty/systemd.git] / src / shared / cgroup-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <unistd.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <ftw.h>
31
32 #include "cgroup-util.h"
33 #include "set.h"
34 #include "macro.h"
35 #include "util.h"
36 #include "formats-util.h"
37 #include "process-util.h"
38 #include "path-util.h"
39 #include "unit-name.h"
40 #include "fileio.h"
41 #include "special.h"
42 #include "mkdir.h"
43 #include "login-shared.h"
44
45 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
46 _cleanup_free_ char *fs = NULL;
47 FILE *f;
48 int r;
49
50 assert(_f);
51
52 r = cg_get_path(controller, path, "cgroup.procs", &fs);
53 if (r < 0)
54 return r;
55
56 f = fopen(fs, "re");
57 if (!f)
58 return -errno;
59
60 *_f = f;
61 return 0;
62 }
63
64 int cg_read_pid(FILE *f, pid_t *_pid) {
65 unsigned long ul;
66
67 /* Note that the cgroup.procs might contain duplicates! See
68 * cgroups.txt for details. */
69
70 assert(f);
71 assert(_pid);
72
73 errno = 0;
74 if (fscanf(f, "%lu", &ul) != 1) {
75
76 if (feof(f))
77 return 0;
78
79 return errno ? -errno : -EIO;
80 }
81
82 if (ul <= 0)
83 return -EIO;
84
85 *_pid = (pid_t) ul;
86 return 1;
87 }
88
89 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
90 _cleanup_free_ char *fs = NULL;
91 int r;
92 DIR *d;
93
94 assert(_d);
95
96 /* This is not recursive! */
97
98 r = cg_get_path(controller, path, NULL, &fs);
99 if (r < 0)
100 return r;
101
102 d = opendir(fs);
103 if (!d)
104 return -errno;
105
106 *_d = d;
107 return 0;
108 }
109
110 int cg_read_subgroup(DIR *d, char **fn) {
111 struct dirent *de;
112
113 assert(d);
114 assert(fn);
115
116 FOREACH_DIRENT(de, d, return -errno) {
117 char *b;
118
119 if (de->d_type != DT_DIR)
120 continue;
121
122 if (streq(de->d_name, ".") ||
123 streq(de->d_name, ".."))
124 continue;
125
126 b = strdup(de->d_name);
127 if (!b)
128 return -ENOMEM;
129
130 *fn = b;
131 return 1;
132 }
133
134 return 0;
135 }
136
137 int cg_rmdir(const char *controller, const char *path) {
138 _cleanup_free_ char *p = NULL;
139 int r;
140
141 r = cg_get_path(controller, path, NULL, &p);
142 if (r < 0)
143 return r;
144
145 r = rmdir(p);
146 if (r < 0 && errno != ENOENT)
147 return -errno;
148
149 return 0;
150 }
151
152 int cg_kill(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, Set *s) {
153 _cleanup_set_free_ Set *allocated_set = NULL;
154 bool done = false;
155 int r, ret = 0;
156 pid_t my_pid;
157
158 assert(sig >= 0);
159
160 /* This goes through the tasks list and kills them all. This
161 * is repeated until no further processes are added to the
162 * tasks list, to properly handle forking processes */
163
164 if (!s) {
165 s = allocated_set = set_new(NULL);
166 if (!s)
167 return -ENOMEM;
168 }
169
170 my_pid = getpid();
171
172 do {
173 _cleanup_fclose_ FILE *f = NULL;
174 pid_t pid = 0;
175 done = true;
176
177 r = cg_enumerate_processes(controller, path, &f);
178 if (r < 0) {
179 if (ret >= 0 && r != -ENOENT)
180 return r;
181
182 return ret;
183 }
184
185 while ((r = cg_read_pid(f, &pid)) > 0) {
186
187 if (ignore_self && pid == my_pid)
188 continue;
189
190 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
191 continue;
192
193 /* If we haven't killed this process yet, kill
194 * it */
195 if (kill(pid, sig) < 0) {
196 if (ret >= 0 && errno != ESRCH)
197 ret = -errno;
198 } else {
199 if (sigcont && sig != SIGKILL)
200 kill(pid, SIGCONT);
201
202 if (ret == 0)
203 ret = 1;
204 }
205
206 done = false;
207
208 r = set_put(s, LONG_TO_PTR(pid));
209 if (r < 0) {
210 if (ret >= 0)
211 return r;
212
213 return ret;
214 }
215 }
216
217 if (r < 0) {
218 if (ret >= 0)
219 return r;
220
221 return ret;
222 }
223
224 /* To avoid racing against processes which fork
225 * quicker than we can kill them we repeat this until
226 * no new pids need to be killed. */
227
228 } while (!done);
229
230 return ret;
231 }
232
233 int cg_kill_recursive(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, bool rem, Set *s) {
234 _cleanup_set_free_ Set *allocated_set = NULL;
235 _cleanup_closedir_ DIR *d = NULL;
236 int r, ret = 0;
237 char *fn;
238
239 assert(path);
240 assert(sig >= 0);
241
242 if (!s) {
243 s = allocated_set = set_new(NULL);
244 if (!s)
245 return -ENOMEM;
246 }
247
248 ret = cg_kill(controller, path, sig, sigcont, ignore_self, s);
249
250 r = cg_enumerate_subgroups(controller, path, &d);
251 if (r < 0) {
252 if (ret >= 0 && r != -ENOENT)
253 return r;
254
255 return ret;
256 }
257
258 while ((r = cg_read_subgroup(d, &fn)) > 0) {
259 _cleanup_free_ char *p = NULL;
260
261 p = strjoin(path, "/", fn, NULL);
262 free(fn);
263 if (!p)
264 return -ENOMEM;
265
266 r = cg_kill_recursive(controller, p, sig, sigcont, ignore_self, rem, s);
267 if (ret >= 0 && r != 0)
268 ret = r;
269 }
270
271 if (ret >= 0 && r < 0)
272 ret = r;
273
274 if (rem) {
275 r = cg_rmdir(controller, path);
276 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
277 return r;
278 }
279
280 return ret;
281 }
282
283 int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, bool ignore_self) {
284 bool done = false;
285 _cleanup_set_free_ Set *s = NULL;
286 int r, ret = 0;
287 pid_t my_pid;
288
289 assert(cfrom);
290 assert(pfrom);
291 assert(cto);
292 assert(pto);
293
294 s = set_new(NULL);
295 if (!s)
296 return -ENOMEM;
297
298 my_pid = getpid();
299
300 do {
301 _cleanup_fclose_ FILE *f = NULL;
302 pid_t pid = 0;
303 done = true;
304
305 r = cg_enumerate_processes(cfrom, pfrom, &f);
306 if (r < 0) {
307 if (ret >= 0 && r != -ENOENT)
308 return r;
309
310 return ret;
311 }
312
313 while ((r = cg_read_pid(f, &pid)) > 0) {
314
315 /* This might do weird stuff if we aren't a
316 * single-threaded program. However, we
317 * luckily know we are not */
318 if (ignore_self && pid == my_pid)
319 continue;
320
321 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
322 continue;
323
324 r = cg_attach(cto, pto, pid);
325 if (r < 0) {
326 if (ret >= 0 && r != -ESRCH)
327 ret = r;
328 } else if (ret == 0)
329 ret = 1;
330
331 done = false;
332
333 r = set_put(s, LONG_TO_PTR(pid));
334 if (r < 0) {
335 if (ret >= 0)
336 return r;
337
338 return ret;
339 }
340 }
341
342 if (r < 0) {
343 if (ret >= 0)
344 return r;
345
346 return ret;
347 }
348 } while (!done);
349
350 return ret;
351 }
352
353 int cg_migrate_recursive(
354 const char *cfrom,
355 const char *pfrom,
356 const char *cto,
357 const char *pto,
358 bool ignore_self,
359 bool rem) {
360
361 _cleanup_closedir_ DIR *d = NULL;
362 int r, ret = 0;
363 char *fn;
364
365 assert(cfrom);
366 assert(pfrom);
367 assert(cto);
368 assert(pto);
369
370 ret = cg_migrate(cfrom, pfrom, cto, pto, ignore_self);
371
372 r = cg_enumerate_subgroups(cfrom, pfrom, &d);
373 if (r < 0) {
374 if (ret >= 0 && r != -ENOENT)
375 return r;
376
377 return ret;
378 }
379
380 while ((r = cg_read_subgroup(d, &fn)) > 0) {
381 _cleanup_free_ char *p = NULL;
382
383 p = strjoin(pfrom, "/", fn, NULL);
384 free(fn);
385 if (!p) {
386 if (ret >= 0)
387 return -ENOMEM;
388
389 return ret;
390 }
391
392 r = cg_migrate_recursive(cfrom, p, cto, pto, ignore_self, rem);
393 if (r != 0 && ret >= 0)
394 ret = r;
395 }
396
397 if (r < 0 && ret >= 0)
398 ret = r;
399
400 if (rem) {
401 r = cg_rmdir(cfrom, pfrom);
402 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
403 return r;
404 }
405
406 return ret;
407 }
408
409 int cg_migrate_recursive_fallback(
410 const char *cfrom,
411 const char *pfrom,
412 const char *cto,
413 const char *pto,
414 bool ignore_self,
415 bool rem) {
416
417 int r;
418
419 assert(cfrom);
420 assert(pfrom);
421 assert(cto);
422 assert(pto);
423
424 r = cg_migrate_recursive(cfrom, pfrom, cto, pto, ignore_self, rem);
425 if (r < 0) {
426 char prefix[strlen(pto) + 1];
427
428 /* This didn't work? Then let's try all prefixes of the destination */
429
430 PATH_FOREACH_PREFIX(prefix, pto) {
431 r = cg_migrate_recursive(cfrom, pfrom, cto, prefix, ignore_self, rem);
432 if (r >= 0)
433 break;
434 }
435 }
436
437 return 0;
438 }
439
440 static const char *normalize_controller(const char *controller) {
441
442 assert(controller);
443
444 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
445 return "systemd";
446 else if (startswith(controller, "name="))
447 return controller + 5;
448 else
449 return controller;
450 }
451
452 static int join_path(const char *controller, const char *path, const char *suffix, char **fs) {
453 char *t = NULL;
454
455 if (!isempty(controller)) {
456 if (!isempty(path) && !isempty(suffix))
457 t = strjoin("/sys/fs/cgroup/", controller, "/", path, "/", suffix, NULL);
458 else if (!isempty(path))
459 t = strjoin("/sys/fs/cgroup/", controller, "/", path, NULL);
460 else if (!isempty(suffix))
461 t = strjoin("/sys/fs/cgroup/", controller, "/", suffix, NULL);
462 else
463 t = strappend("/sys/fs/cgroup/", controller);
464 } else {
465 if (!isempty(path) && !isempty(suffix))
466 t = strjoin(path, "/", suffix, NULL);
467 else if (!isempty(path))
468 t = strdup(path);
469 else
470 return -EINVAL;
471 }
472
473 if (!t)
474 return -ENOMEM;
475
476 *fs = path_kill_slashes(t);
477 return 0;
478 }
479
480 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
481 const char *p;
482 static thread_local bool good = false;
483
484 assert(fs);
485
486 if (controller && !cg_controller_is_valid(controller, true))
487 return -EINVAL;
488
489 if (_unlikely_(!good)) {
490 int r;
491
492 r = path_is_mount_point("/sys/fs/cgroup", false);
493 if (r < 0)
494 return r;
495 if (r == 0)
496 return -ENOENT;
497
498 /* Cache this to save a few stat()s */
499 good = true;
500 }
501
502 p = controller ? normalize_controller(controller) : NULL;
503
504 return join_path(p, path, suffix, fs);
505 }
506
507 static int check_hierarchy(const char *p) {
508 const char *cc;
509
510 assert(p);
511
512 if (!filename_is_valid(p))
513 return 0;
514
515 /* Check if this controller actually really exists */
516 cc = strjoina("/sys/fs/cgroup/", p);
517 if (laccess(cc, F_OK) < 0)
518 return -errno;
519
520 return 0;
521 }
522
523 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
524 const char *p;
525 int r;
526
527 assert(fs);
528
529 if (!cg_controller_is_valid(controller, true))
530 return -EINVAL;
531
532 /* Normalize the controller syntax */
533 p = normalize_controller(controller);
534
535 /* Check if this controller actually really exists */
536 r = check_hierarchy(p);
537 if (r < 0)
538 return r;
539
540 return join_path(p, path, suffix, fs);
541 }
542
543 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
544 assert(path);
545 assert(sb);
546 assert(ftwbuf);
547
548 if (typeflag != FTW_DP)
549 return 0;
550
551 if (ftwbuf->level < 1)
552 return 0;
553
554 rmdir(path);
555 return 0;
556 }
557
558 int cg_trim(const char *controller, const char *path, bool delete_root) {
559 _cleanup_free_ char *fs = NULL;
560 int r = 0;
561
562 assert(path);
563
564 r = cg_get_path(controller, path, NULL, &fs);
565 if (r < 0)
566 return r;
567
568 errno = 0;
569 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0)
570 r = errno ? -errno : -EIO;
571
572 if (delete_root) {
573 if (rmdir(fs) < 0 && errno != ENOENT)
574 return -errno;
575 }
576
577 return r;
578 }
579
580 int cg_delete(const char *controller, const char *path) {
581 _cleanup_free_ char *parent = NULL;
582 int r;
583
584 assert(path);
585
586 r = path_get_parent(path, &parent);
587 if (r < 0)
588 return r;
589
590 r = cg_migrate_recursive(controller, path, controller, parent, false, true);
591 return r == -ENOENT ? 0 : r;
592 }
593
594 int cg_create(const char *controller, const char *path) {
595 _cleanup_free_ char *fs = NULL;
596 int r;
597
598 r = cg_get_path_and_check(controller, path, NULL, &fs);
599 if (r < 0)
600 return r;
601
602 r = mkdir_parents(fs, 0755);
603 if (r < 0)
604 return r;
605
606 if (mkdir(fs, 0755) < 0) {
607
608 if (errno == EEXIST)
609 return 0;
610
611 return -errno;
612 }
613
614 return 1;
615 }
616
617 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
618 int r, q;
619
620 assert(pid >= 0);
621
622 r = cg_create(controller, path);
623 if (r < 0)
624 return r;
625
626 q = cg_attach(controller, path, pid);
627 if (q < 0)
628 return q;
629
630 /* This does not remove the cgroup on failure */
631 return r;
632 }
633
634 int cg_attach(const char *controller, const char *path, pid_t pid) {
635 _cleanup_free_ char *fs = NULL;
636 char c[DECIMAL_STR_MAX(pid_t) + 2];
637 int r;
638
639 assert(path);
640 assert(pid >= 0);
641
642 r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
643 if (r < 0)
644 return r;
645
646 if (pid == 0)
647 pid = getpid();
648
649 snprintf(c, sizeof(c), PID_FMT"\n", pid);
650
651 return write_string_file_no_create(fs, c);
652 }
653
654 int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
655 int r;
656
657 assert(controller);
658 assert(path);
659 assert(pid >= 0);
660
661 r = cg_attach(controller, path, pid);
662 if (r < 0) {
663 char prefix[strlen(path) + 1];
664
665 /* This didn't work? Then let's try all prefixes of
666 * the destination */
667
668 PATH_FOREACH_PREFIX(prefix, path) {
669 r = cg_attach(controller, prefix, pid);
670 if (r >= 0)
671 break;
672 }
673 }
674
675 return 0;
676 }
677
678 int cg_set_group_access(
679 const char *controller,
680 const char *path,
681 mode_t mode,
682 uid_t uid,
683 gid_t gid) {
684
685 _cleanup_free_ char *fs = NULL;
686 int r;
687
688 assert(path);
689
690 if (mode != MODE_INVALID)
691 mode &= 0777;
692
693 r = cg_get_path(controller, path, NULL, &fs);
694 if (r < 0)
695 return r;
696
697 return chmod_and_chown(fs, mode, uid, gid);
698 }
699
700 int cg_set_task_access(
701 const char *controller,
702 const char *path,
703 mode_t mode,
704 uid_t uid,
705 gid_t gid) {
706
707 _cleanup_free_ char *fs = NULL, *procs = NULL;
708 int r;
709
710 assert(path);
711
712 if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
713 return 0;
714
715 if (mode != MODE_INVALID)
716 mode &= 0666;
717
718 r = cg_get_path(controller, path, "cgroup.procs", &fs);
719 if (r < 0)
720 return r;
721
722 r = chmod_and_chown(fs, mode, uid, gid);
723 if (r < 0)
724 return r;
725
726 /* Compatibility, Always keep values for "tasks" in sync with
727 * "cgroup.procs" */
728 r = cg_get_path(controller, path, "tasks", &procs);
729 if (r < 0)
730 return r;
731
732 return chmod_and_chown(procs, mode, uid, gid);
733 }
734
735 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
736 _cleanup_fclose_ FILE *f = NULL;
737 char line[LINE_MAX];
738 const char *fs;
739 size_t cs;
740
741 assert(path);
742 assert(pid >= 0);
743
744 if (controller) {
745 if (!cg_controller_is_valid(controller, true))
746 return -EINVAL;
747
748 controller = normalize_controller(controller);
749 } else
750 controller = SYSTEMD_CGROUP_CONTROLLER;
751
752 fs = procfs_file_alloca(pid, "cgroup");
753
754 f = fopen(fs, "re");
755 if (!f)
756 return errno == ENOENT ? -ESRCH : -errno;
757
758 cs = strlen(controller);
759
760 FOREACH_LINE(line, f, return -errno) {
761 char *l, *p, *e;
762 size_t k;
763 const char *word, *state;
764 bool found = false;
765
766 truncate_nl(line);
767
768 l = strchr(line, ':');
769 if (!l)
770 continue;
771
772 l++;
773 e = strchr(l, ':');
774 if (!e)
775 continue;
776
777 *e = 0;
778
779 FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
780
781 if (k == cs && memcmp(word, controller, cs) == 0) {
782 found = true;
783 break;
784 }
785
786 if (k == 5 + cs &&
787 memcmp(word, "name=", 5) == 0 &&
788 memcmp(word+5, controller, cs) == 0) {
789 found = true;
790 break;
791 }
792 }
793
794 if (!found)
795 continue;
796
797 p = strdup(e + 1);
798 if (!p)
799 return -ENOMEM;
800
801 *path = p;
802 return 0;
803 }
804
805 return -ENOENT;
806 }
807
808 int cg_install_release_agent(const char *controller, const char *agent) {
809 _cleanup_free_ char *fs = NULL, *contents = NULL;
810 char *sc;
811 int r;
812
813 assert(agent);
814
815 r = cg_get_path(controller, NULL, "release_agent", &fs);
816 if (r < 0)
817 return r;
818
819 r = read_one_line_file(fs, &contents);
820 if (r < 0)
821 return r;
822
823 sc = strstrip(contents);
824 if (sc[0] == 0) {
825 r = write_string_file_no_create(fs, agent);
826 if (r < 0)
827 return r;
828 } else if (!streq(sc, agent))
829 return -EEXIST;
830
831 free(fs);
832 fs = NULL;
833 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
834 if (r < 0)
835 return r;
836
837 free(contents);
838 contents = NULL;
839 r = read_one_line_file(fs, &contents);
840 if (r < 0)
841 return r;
842
843 sc = strstrip(contents);
844 if (streq(sc, "0")) {
845 r = write_string_file_no_create(fs, "1");
846 if (r < 0)
847 return r;
848
849 return 1;
850 }
851
852 if (!streq(sc, "1"))
853 return -EIO;
854
855 return 0;
856 }
857
858 int cg_uninstall_release_agent(const char *controller) {
859 _cleanup_free_ char *fs = NULL;
860 int r;
861
862 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
863 if (r < 0)
864 return r;
865
866 r = write_string_file_no_create(fs, "0");
867 if (r < 0)
868 return r;
869
870 free(fs);
871 fs = NULL;
872
873 r = cg_get_path(controller, NULL, "release_agent", &fs);
874 if (r < 0)
875 return r;
876
877 r = write_string_file_no_create(fs, "");
878 if (r < 0)
879 return r;
880
881 return 0;
882 }
883
884 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
885 _cleanup_fclose_ FILE *f = NULL;
886 pid_t pid = 0, self_pid;
887 bool found = false;
888 int r;
889
890 assert(path);
891
892 r = cg_enumerate_processes(controller, path, &f);
893 if (r < 0)
894 return r == -ENOENT ? 1 : r;
895
896 self_pid = getpid();
897
898 while ((r = cg_read_pid(f, &pid)) > 0) {
899
900 if (ignore_self && pid == self_pid)
901 continue;
902
903 found = true;
904 break;
905 }
906
907 if (r < 0)
908 return r;
909
910 return !found;
911 }
912
913 int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
914 _cleanup_closedir_ DIR *d = NULL;
915 char *fn;
916 int r;
917
918 assert(path);
919
920 r = cg_is_empty(controller, path, ignore_self);
921 if (r <= 0)
922 return r;
923
924 r = cg_enumerate_subgroups(controller, path, &d);
925 if (r < 0)
926 return r == -ENOENT ? 1 : r;
927
928 while ((r = cg_read_subgroup(d, &fn)) > 0) {
929 _cleanup_free_ char *p = NULL;
930
931 p = strjoin(path, "/", fn, NULL);
932 free(fn);
933 if (!p)
934 return -ENOMEM;
935
936 r = cg_is_empty_recursive(controller, p, ignore_self);
937 if (r <= 0)
938 return r;
939 }
940
941 if (r < 0)
942 return r;
943
944 return 1;
945 }
946
947 int cg_split_spec(const char *spec, char **controller, char **path) {
948 const char *e;
949 char *t = NULL, *u = NULL;
950 _cleanup_free_ char *v = NULL;
951
952 assert(spec);
953
954 if (*spec == '/') {
955 if (!path_is_safe(spec))
956 return -EINVAL;
957
958 if (path) {
959 t = strdup(spec);
960 if (!t)
961 return -ENOMEM;
962
963 *path = path_kill_slashes(t);
964 }
965
966 if (controller)
967 *controller = NULL;
968
969 return 0;
970 }
971
972 e = strchr(spec, ':');
973 if (!e) {
974 if (!cg_controller_is_valid(spec, true))
975 return -EINVAL;
976
977 if (controller) {
978 t = strdup(normalize_controller(spec));
979 if (!t)
980 return -ENOMEM;
981
982 *controller = t;
983 }
984
985 if (path)
986 *path = NULL;
987
988 return 0;
989 }
990
991 v = strndup(spec, e-spec);
992 if (!v)
993 return -ENOMEM;
994 t = strdup(normalize_controller(v));
995 if (!t)
996 return -ENOMEM;
997 if (!cg_controller_is_valid(t, true)) {
998 free(t);
999 return -EINVAL;
1000 }
1001
1002 if (streq(e+1, "")) {
1003 u = strdup("/");
1004 if (!u) {
1005 free(t);
1006 return -ENOMEM;
1007 }
1008 } else {
1009 u = strdup(e+1);
1010 if (!u) {
1011 free(t);
1012 return -ENOMEM;
1013 }
1014
1015 if (!path_is_safe(u) ||
1016 !path_is_absolute(u)) {
1017 free(t);
1018 free(u);
1019 return -EINVAL;
1020 }
1021
1022 path_kill_slashes(u);
1023 }
1024
1025 if (controller)
1026 *controller = t;
1027 else
1028 free(t);
1029
1030 if (path)
1031 *path = u;
1032 else
1033 free(u);
1034
1035 return 0;
1036 }
1037
1038 int cg_mangle_path(const char *path, char **result) {
1039 _cleanup_free_ char *c = NULL, *p = NULL;
1040 char *t;
1041 int r;
1042
1043 assert(path);
1044 assert(result);
1045
1046 /* First, check if it already is a filesystem path */
1047 if (path_startswith(path, "/sys/fs/cgroup")) {
1048
1049 t = strdup(path);
1050 if (!t)
1051 return -ENOMEM;
1052
1053 *result = path_kill_slashes(t);
1054 return 0;
1055 }
1056
1057 /* Otherwise, treat it as cg spec */
1058 r = cg_split_spec(path, &c, &p);
1059 if (r < 0)
1060 return r;
1061
1062 return cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);
1063 }
1064
1065 int cg_get_root_path(char **path) {
1066 char *p, *e;
1067 int r;
1068
1069 assert(path);
1070
1071 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1072 if (r < 0)
1073 return r;
1074
1075 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE);
1076 if (e)
1077 *e = 0;
1078
1079 *path = p;
1080 return 0;
1081 }
1082
1083 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1084 _cleanup_free_ char *rt = NULL;
1085 char *p;
1086 int r;
1087
1088 assert(cgroup);
1089 assert(shifted);
1090
1091 if (!root) {
1092 /* If the root was specified let's use that, otherwise
1093 * let's determine it from PID 1 */
1094
1095 r = cg_get_root_path(&rt);
1096 if (r < 0)
1097 return r;
1098
1099 root = rt;
1100 }
1101
1102 p = path_startswith(cgroup, root);
1103 if (p)
1104 *shifted = p - 1;
1105 else
1106 *shifted = cgroup;
1107
1108 return 0;
1109 }
1110
1111 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1112 _cleanup_free_ char *raw = NULL;
1113 const char *c;
1114 int r;
1115
1116 assert(pid >= 0);
1117 assert(cgroup);
1118
1119 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
1120 if (r < 0)
1121 return r;
1122
1123 r = cg_shift_path(raw, root, &c);
1124 if (r < 0)
1125 return r;
1126
1127 if (c == raw) {
1128 *cgroup = raw;
1129 raw = NULL;
1130 } else {
1131 char *n;
1132
1133 n = strdup(c);
1134 if (!n)
1135 return -ENOMEM;
1136
1137 *cgroup = n;
1138 }
1139
1140 return 0;
1141 }
1142
1143 int cg_path_decode_unit(const char *cgroup, char **unit){
1144 char *c, *s;
1145 size_t n;
1146
1147 assert(cgroup);
1148 assert(unit);
1149
1150 n = strcspn(cgroup, "/");
1151 if (n < 3)
1152 return -ENXIO;
1153
1154 c = strndupa(cgroup, n);
1155 c = cg_unescape(c);
1156
1157 if (!unit_name_is_valid(c, TEMPLATE_INVALID))
1158 return -ENXIO;
1159
1160 s = strdup(c);
1161 if (!s)
1162 return -ENOMEM;
1163
1164 *unit = s;
1165 return 0;
1166 }
1167
1168 static bool valid_slice_name(const char *p, size_t n) {
1169
1170 if (!p)
1171 return false;
1172
1173 if (n < strlen("x.slice"))
1174 return false;
1175
1176 if (memcmp(p + n - 6, ".slice", 6) == 0) {
1177 char buf[n+1], *c;
1178
1179 memcpy(buf, p, n);
1180 buf[n] = 0;
1181
1182 c = cg_unescape(buf);
1183
1184 return unit_name_is_valid(c, TEMPLATE_INVALID);
1185 }
1186
1187 return false;
1188 }
1189
1190 static const char *skip_slices(const char *p) {
1191 assert(p);
1192
1193 /* Skips over all slice assignments */
1194
1195 for (;;) {
1196 size_t n;
1197
1198 p += strspn(p, "/");
1199
1200 n = strcspn(p, "/");
1201 if (!valid_slice_name(p, n))
1202 return p;
1203
1204 p += n;
1205 }
1206 }
1207
1208 int cg_path_get_unit(const char *path, char **ret) {
1209 const char *e;
1210 char *unit;
1211 int r;
1212
1213 assert(path);
1214 assert(ret);
1215
1216 e = skip_slices(path);
1217
1218 r = cg_path_decode_unit(e, &unit);
1219 if (r < 0)
1220 return r;
1221
1222 /* We skipped over the slices, don't accept any now */
1223 if (endswith(unit, ".slice")) {
1224 free(unit);
1225 return -ENXIO;
1226 }
1227
1228 *ret = unit;
1229 return 0;
1230 }
1231
1232 int cg_pid_get_unit(pid_t pid, char **unit) {
1233 _cleanup_free_ char *cgroup = NULL;
1234 int r;
1235
1236 assert(unit);
1237
1238 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1239 if (r < 0)
1240 return r;
1241
1242 return cg_path_get_unit(cgroup, unit);
1243 }
1244
1245 /**
1246 * Skip session-*.scope, but require it to be there.
1247 */
1248 static const char *skip_session(const char *p) {
1249 size_t n;
1250
1251 if (isempty(p))
1252 return NULL;
1253
1254 p += strspn(p, "/");
1255
1256 n = strcspn(p, "/");
1257 if (n < strlen("session-x.scope"))
1258 return NULL;
1259
1260 if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1261 char buf[n - 8 - 6 + 1];
1262
1263 memcpy(buf, p + 8, n - 8 - 6);
1264 buf[n - 8 - 6] = 0;
1265
1266 /* Note that session scopes never need unescaping,
1267 * since they cannot conflict with the kernel's own
1268 * names, hence we don't need to call cg_unescape()
1269 * here. */
1270
1271 if (!session_id_valid(buf))
1272 return false;
1273
1274 p += n;
1275 p += strspn(p, "/");
1276 return p;
1277 }
1278
1279 return NULL;
1280 }
1281
1282 /**
1283 * Skip user@*.service, but require it to be there.
1284 */
1285 static const char *skip_user_manager(const char *p) {
1286 size_t n;
1287
1288 if (isempty(p))
1289 return NULL;
1290
1291 p += strspn(p, "/");
1292
1293 n = strcspn(p, "/");
1294 if (n < strlen("user@x.service"))
1295 return NULL;
1296
1297 if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1298 char buf[n - 5 - 8 + 1];
1299
1300 memcpy(buf, p + 5, n - 5 - 8);
1301 buf[n - 5 - 8] = 0;
1302
1303 /* Note that user manager services never need unescaping,
1304 * since they cannot conflict with the kernel's own
1305 * names, hence we don't need to call cg_unescape()
1306 * here. */
1307
1308 if (parse_uid(buf, NULL) < 0)
1309 return NULL;
1310
1311 p += n;
1312 p += strspn(p, "/");
1313
1314 return p;
1315 }
1316
1317 return NULL;
1318 }
1319
1320 static const char *skip_user_prefix(const char *path) {
1321 const char *e, *t;
1322
1323 assert(path);
1324
1325 /* Skip slices, if there are any */
1326 e = skip_slices(path);
1327
1328 /* Skip the user manager, if it's in the path now... */
1329 t = skip_user_manager(e);
1330 if (t)
1331 return t;
1332
1333 /* Alternatively skip the user session if it is in the path... */
1334 return skip_session(e);
1335 }
1336
1337 int cg_path_get_user_unit(const char *path, char **ret) {
1338 const char *t;
1339
1340 assert(path);
1341 assert(ret);
1342
1343 t = skip_user_prefix(path);
1344 if (!t)
1345 return -ENXIO;
1346
1347 /* And from here on it looks pretty much the same as for a
1348 * system unit, hence let's use the same parser from here
1349 * on. */
1350 return cg_path_get_unit(t, ret);
1351 }
1352
1353 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1354 _cleanup_free_ char *cgroup = NULL;
1355 int r;
1356
1357 assert(unit);
1358
1359 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1360 if (r < 0)
1361 return r;
1362
1363 return cg_path_get_user_unit(cgroup, unit);
1364 }
1365
1366 int cg_path_get_machine_name(const char *path, char **machine) {
1367 _cleanup_free_ char *u = NULL, *sl = NULL;
1368 int r;
1369
1370 r = cg_path_get_unit(path, &u);
1371 if (r < 0)
1372 return r;
1373
1374 sl = strjoin("/run/systemd/machines/unit:", u, NULL);
1375 if (!sl)
1376 return -ENOMEM;
1377
1378 return readlink_malloc(sl, machine);
1379 }
1380
1381 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1382 _cleanup_free_ char *cgroup = NULL;
1383 int r;
1384
1385 assert(machine);
1386
1387 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1388 if (r < 0)
1389 return r;
1390
1391 return cg_path_get_machine_name(cgroup, machine);
1392 }
1393
1394 int cg_path_get_session(const char *path, char **session) {
1395 _cleanup_free_ char *unit = NULL;
1396 char *start, *end;
1397 int r;
1398
1399 assert(path);
1400
1401 r = cg_path_get_unit(path, &unit);
1402 if (r < 0)
1403 return r;
1404
1405 start = startswith(unit, "session-");
1406 if (!start)
1407 return -ENXIO;
1408 end = endswith(start, ".scope");
1409 if (!end)
1410 return -ENXIO;
1411
1412 *end = 0;
1413 if (!session_id_valid(start))
1414 return -ENXIO;
1415
1416 if (session) {
1417 char *rr;
1418
1419 rr = strdup(start);
1420 if (!rr)
1421 return -ENOMEM;
1422
1423 *session = rr;
1424 }
1425
1426 return 0;
1427 }
1428
1429 int cg_pid_get_session(pid_t pid, char **session) {
1430 _cleanup_free_ char *cgroup = NULL;
1431 int r;
1432
1433 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1434 if (r < 0)
1435 return r;
1436
1437 return cg_path_get_session(cgroup, session);
1438 }
1439
1440 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1441 _cleanup_free_ char *slice = NULL;
1442 char *start, *end;
1443 int r;
1444
1445 assert(path);
1446
1447 r = cg_path_get_slice(path, &slice);
1448 if (r < 0)
1449 return r;
1450
1451 start = startswith(slice, "user-");
1452 if (!start)
1453 return -ENXIO;
1454 end = endswith(start, ".slice");
1455 if (!end)
1456 return -ENXIO;
1457
1458 *end = 0;
1459 if (parse_uid(start, uid) < 0)
1460 return -ENXIO;
1461
1462 return 0;
1463 }
1464
1465 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1466 _cleanup_free_ char *cgroup = NULL;
1467 int r;
1468
1469 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1470 if (r < 0)
1471 return r;
1472
1473 return cg_path_get_owner_uid(cgroup, uid);
1474 }
1475
1476 int cg_path_get_slice(const char *p, char **slice) {
1477 const char *e = NULL;
1478
1479 assert(p);
1480 assert(slice);
1481
1482 /* Finds the right-most slice unit from the beginning, but
1483 * stops before we come to the first non-slice unit. */
1484
1485 for (;;) {
1486 size_t n;
1487
1488 p += strspn(p, "/");
1489
1490 n = strcspn(p, "/");
1491 if (!valid_slice_name(p, n)) {
1492
1493 if (!e) {
1494 char *s;
1495
1496 s = strdup("-.slice");
1497 if (!s)
1498 return -ENOMEM;
1499
1500 *slice = s;
1501 return 0;
1502 }
1503
1504 return cg_path_decode_unit(e, slice);
1505 }
1506
1507 e = p;
1508 p += n;
1509 }
1510 }
1511
1512 int cg_pid_get_slice(pid_t pid, char **slice) {
1513 _cleanup_free_ char *cgroup = NULL;
1514 int r;
1515
1516 assert(slice);
1517
1518 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1519 if (r < 0)
1520 return r;
1521
1522 return cg_path_get_slice(cgroup, slice);
1523 }
1524
1525 int cg_path_get_user_slice(const char *p, char **slice) {
1526 const char *t;
1527 assert(p);
1528 assert(slice);
1529
1530 t = skip_user_prefix(p);
1531 if (!t)
1532 return -ENXIO;
1533
1534 /* And now it looks pretty much the same as for a system
1535 * slice, so let's just use the same parser from here on. */
1536 return cg_path_get_slice(t, slice);
1537 }
1538
1539 int cg_pid_get_user_slice(pid_t pid, char **slice) {
1540 _cleanup_free_ char *cgroup = NULL;
1541 int r;
1542
1543 assert(slice);
1544
1545 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1546 if (r < 0)
1547 return r;
1548
1549 return cg_path_get_user_slice(cgroup, slice);
1550 }
1551
1552 char *cg_escape(const char *p) {
1553 bool need_prefix = false;
1554
1555 /* This implements very minimal escaping for names to be used
1556 * as file names in the cgroup tree: any name which might
1557 * conflict with a kernel name or is prefixed with '_' is
1558 * prefixed with a '_'. That way, when reading cgroup names it
1559 * is sufficient to remove a single prefixing underscore if
1560 * there is one. */
1561
1562 /* The return value of this function (unlike cg_unescape())
1563 * needs free()! */
1564
1565 if (p[0] == 0 ||
1566 p[0] == '_' ||
1567 p[0] == '.' ||
1568 streq(p, "notify_on_release") ||
1569 streq(p, "release_agent") ||
1570 streq(p, "tasks"))
1571 need_prefix = true;
1572 else {
1573 const char *dot;
1574
1575 dot = strrchr(p, '.');
1576 if (dot) {
1577
1578 if (dot - p == 6 && memcmp(p, "cgroup", 6) == 0)
1579 need_prefix = true;
1580 else {
1581 char *n;
1582
1583 n = strndupa(p, dot - p);
1584
1585 if (check_hierarchy(n) >= 0)
1586 need_prefix = true;
1587 }
1588 }
1589 }
1590
1591 if (need_prefix)
1592 return strappend("_", p);
1593 else
1594 return strdup(p);
1595 }
1596
1597 char *cg_unescape(const char *p) {
1598 assert(p);
1599
1600 /* The return value of this function (unlike cg_escape())
1601 * doesn't need free()! */
1602
1603 if (p[0] == '_')
1604 return (char*) p+1;
1605
1606 return (char*) p;
1607 }
1608
1609 #define CONTROLLER_VALID \
1610 DIGITS LETTERS \
1611 "_"
1612
1613 bool cg_controller_is_valid(const char *p, bool allow_named) {
1614 const char *t, *s;
1615
1616 if (!p)
1617 return false;
1618
1619 if (allow_named) {
1620 s = startswith(p, "name=");
1621 if (s)
1622 p = s;
1623 }
1624
1625 if (*p == 0 || *p == '_')
1626 return false;
1627
1628 for (t = p; *t; t++)
1629 if (!strchr(CONTROLLER_VALID, *t))
1630 return false;
1631
1632 if (t - p > FILENAME_MAX)
1633 return false;
1634
1635 return true;
1636 }
1637
1638 int cg_slice_to_path(const char *unit, char **ret) {
1639 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1640 const char *dash;
1641
1642 assert(unit);
1643 assert(ret);
1644
1645 if (streq(unit, "-.slice")) {
1646 char *x;
1647
1648 x = strdup("");
1649 if (!x)
1650 return -ENOMEM;
1651 *ret = x;
1652 return 0;
1653 }
1654
1655 if (!unit_name_is_valid(unit, TEMPLATE_INVALID))
1656 return -EINVAL;
1657
1658 if (!endswith(unit, ".slice"))
1659 return -EINVAL;
1660
1661 p = unit_name_to_prefix(unit);
1662 if (!p)
1663 return -ENOMEM;
1664
1665 dash = strchr(p, '-');
1666
1667 /* Don't allow initial dashes */
1668 if (dash == p)
1669 return -EINVAL;
1670
1671 while (dash) {
1672 _cleanup_free_ char *escaped = NULL;
1673 char n[dash - p + sizeof(".slice")];
1674
1675 /* Don't allow trailing or double dashes */
1676 if (dash[1] == 0 || dash[1] == '-')
1677 return -EINVAL;
1678
1679 strcpy(stpncpy(n, p, dash - p), ".slice");
1680 if (!unit_name_is_valid(n, TEMPLATE_INVALID))
1681 return -EINVAL;
1682
1683 escaped = cg_escape(n);
1684 if (!escaped)
1685 return -ENOMEM;
1686
1687 if (!strextend(&s, escaped, "/", NULL))
1688 return -ENOMEM;
1689
1690 dash = strchr(dash+1, '-');
1691 }
1692
1693 e = cg_escape(unit);
1694 if (!e)
1695 return -ENOMEM;
1696
1697 if (!strextend(&s, e, NULL))
1698 return -ENOMEM;
1699
1700 *ret = s;
1701 s = NULL;
1702
1703 return 0;
1704 }
1705
1706 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
1707 _cleanup_free_ char *p = NULL;
1708 int r;
1709
1710 r = cg_get_path(controller, path, attribute, &p);
1711 if (r < 0)
1712 return r;
1713
1714 return write_string_file_no_create(p, value);
1715 }
1716
1717 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
1718 _cleanup_free_ char *p = NULL;
1719 int r;
1720
1721 r = cg_get_path(controller, path, attribute, &p);
1722 if (r < 0)
1723 return r;
1724
1725 return read_one_line_file(p, ret);
1726 }
1727
1728 static const char mask_names[] =
1729 "cpu\0"
1730 "cpuacct\0"
1731 "blkio\0"
1732 "memory\0"
1733 "devices\0";
1734
1735 int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path) {
1736 CGroupControllerMask bit = 1;
1737 const char *n;
1738 int r;
1739
1740 /* This one will create a cgroup in our private tree, but also
1741 * duplicate it in the trees specified in mask, and remove it
1742 * in all others */
1743
1744 /* First create the cgroup in our own hierarchy. */
1745 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1746 if (r < 0)
1747 return r;
1748
1749 /* Then, do the same in the other hierarchies */
1750 NULSTR_FOREACH(n, mask_names) {
1751 if (mask & bit)
1752 cg_create(n, path);
1753 else if (supported & bit)
1754 cg_trim(n, path, true);
1755
1756 bit <<= 1;
1757 }
1758
1759 return 0;
1760 }
1761
1762 int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
1763 CGroupControllerMask bit = 1;
1764 const char *n;
1765 int r;
1766
1767 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
1768 if (r < 0)
1769 return r;
1770
1771 NULSTR_FOREACH(n, mask_names) {
1772
1773 if (supported & bit) {
1774 const char *p = NULL;
1775
1776 if (path_callback)
1777 p = path_callback(bit, userdata);
1778
1779 if (!p)
1780 p = path;
1781
1782 cg_attach_fallback(n, path, pid);
1783 }
1784
1785 bit <<= 1;
1786 }
1787
1788 return 0;
1789 }
1790
1791 int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
1792 Iterator i;
1793 void *pidp;
1794 int r = 0;
1795
1796 SET_FOREACH(pidp, pids, i) {
1797 pid_t pid = PTR_TO_LONG(pidp);
1798 int q;
1799
1800 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
1801 if (q < 0)
1802 r = q;
1803 }
1804
1805 return r;
1806 }
1807
1808 int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
1809 CGroupControllerMask bit = 1;
1810 const char *n;
1811 int r;
1812
1813 if (!path_equal(from, to)) {
1814 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, false, true);
1815 if (r < 0)
1816 return r;
1817 }
1818
1819 NULSTR_FOREACH(n, mask_names) {
1820 if (supported & bit) {
1821 const char *p = NULL;
1822
1823 if (to_callback)
1824 p = to_callback(bit, userdata);
1825
1826 if (!p)
1827 p = to;
1828
1829 cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, n, p, false, false);
1830 }
1831
1832 bit <<= 1;
1833 }
1834
1835 return 0;
1836 }
1837
1838 int cg_trim_everywhere(CGroupControllerMask supported, const char *path, bool delete_root) {
1839 CGroupControllerMask bit = 1;
1840 const char *n;
1841 int r;
1842
1843 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
1844 if (r < 0)
1845 return r;
1846
1847 NULSTR_FOREACH(n, mask_names) {
1848 if (supported & bit)
1849 cg_trim(n, path, delete_root);
1850
1851 bit <<= 1;
1852 }
1853
1854 return 0;
1855 }
1856
1857 CGroupControllerMask cg_mask_supported(void) {
1858 CGroupControllerMask bit = 1, mask = 0;
1859 const char *n;
1860
1861 NULSTR_FOREACH(n, mask_names) {
1862 if (check_hierarchy(n) >= 0)
1863 mask |= bit;
1864
1865 bit <<= 1;
1866 }
1867
1868 return mask;
1869 }
1870
1871 int cg_kernel_controllers(Set *controllers) {
1872 _cleanup_fclose_ FILE *f = NULL;
1873 char buf[LINE_MAX];
1874 int r;
1875
1876 assert(controllers);
1877
1878 f = fopen("/proc/cgroups", "re");
1879 if (!f) {
1880 if (errno == ENOENT)
1881 return 0;
1882 return -errno;
1883 }
1884
1885 /* Ignore the header line */
1886 (void) fgets(buf, sizeof(buf), f);
1887
1888 for (;;) {
1889 char *controller;
1890 int enabled = 0;
1891
1892 errno = 0;
1893 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
1894
1895 if (feof(f))
1896 break;
1897
1898 if (ferror(f) && errno)
1899 return -errno;
1900
1901 return -EBADMSG;
1902 }
1903
1904 if (!enabled) {
1905 free(controller);
1906 continue;
1907 }
1908
1909 if (!filename_is_valid(controller)) {
1910 free(controller);
1911 return -EBADMSG;
1912 }
1913
1914 r = set_consume(controllers, controller);
1915 if (r < 0)
1916 return r;
1917 }
1918
1919 return 0;
1920 }