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