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