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