chiark / gitweb /
64220ac085c0cbed5c5bcb12d76258e2ec0c4402
[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
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, NULL);
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 (p && p > cgroup)
1333                 *shifted = p - 1;
1334         else
1335                 *shifted = cgroup;
1336
1337         return 0;
1338 }
1339
1340 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1341         _cleanup_free_ char *raw = NULL;
1342         const char *c;
1343         int r;
1344
1345         assert(pid >= 0);
1346         assert(cgroup);
1347
1348         r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
1349         if (r < 0)
1350                 return r;
1351
1352         log_debug_elogind("Shifting path: \"%s\" (PID %u, root: \"%s\")",
1353                           raw, pid, root ? root : "NULL");
1354         r = cg_shift_path(raw, root, &c);
1355         if (r < 0)
1356                 return r;
1357
1358         if (c == raw) {
1359                 *cgroup = raw;
1360                 raw = NULL;
1361         } else {
1362                 char *n;
1363
1364                 n = strdup(c);
1365                 if (!n)
1366                         return -ENOMEM;
1367
1368                 *cgroup = n;
1369         }
1370         log_debug_elogind("Resulting cgroup:\"%s\"", *cgroup);
1371
1372         return 0;
1373 }
1374
1375 #if 0 /// UNNEEDED by elogind
1376 int cg_path_decode_unit(const char *cgroup, char **unit) {
1377         char *c, *s;
1378         size_t n;
1379
1380         assert(cgroup);
1381         assert(unit);
1382
1383         n = strcspn(cgroup, "/");
1384         if (n < 3)
1385                 return -ENXIO;
1386
1387         c = strndupa(cgroup, n);
1388         c = cg_unescape(c);
1389
1390         if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
1391                 return -ENXIO;
1392
1393         s = strdup(c);
1394         if (!s)
1395                 return -ENOMEM;
1396
1397         *unit = s;
1398         return 0;
1399 }
1400
1401 static bool valid_slice_name(const char *p, size_t n) {
1402
1403         if (!p)
1404                 return false;
1405
1406         if (n < strlen("x.slice"))
1407                 return false;
1408
1409         if (memcmp(p + n - 6, ".slice", 6) == 0) {
1410                 char buf[n+1], *c;
1411
1412                 memcpy(buf, p, n);
1413                 buf[n] = 0;
1414
1415                 c = cg_unescape(buf);
1416
1417                 return unit_name_is_valid(c, UNIT_NAME_PLAIN);
1418         }
1419
1420         return false;
1421 }
1422
1423 static const char *skip_slices(const char *p) {
1424         assert(p);
1425
1426         /* Skips over all slice assignments */
1427
1428         for (;;) {
1429                 size_t n;
1430
1431                 p += strspn(p, "/");
1432
1433                 n = strcspn(p, "/");
1434                 if (!valid_slice_name(p, n))
1435                         return p;
1436
1437                 p += n;
1438         }
1439 }
1440
1441 int cg_path_get_unit(const char *path, char **ret) {
1442         const char *e;
1443         char *unit;
1444         int r;
1445
1446         assert(path);
1447         assert(ret);
1448
1449         e = skip_slices(path);
1450
1451         r = cg_path_decode_unit(e, &unit);
1452         if (r < 0)
1453                 return r;
1454
1455         /* We skipped over the slices, don't accept any now */
1456         if (endswith(unit, ".slice")) {
1457                 free(unit);
1458                 return -ENXIO;
1459         }
1460
1461         *ret = unit;
1462         return 0;
1463 }
1464
1465 int cg_pid_get_unit(pid_t pid, char **unit) {
1466         _cleanup_free_ char *cgroup = NULL;
1467         int r;
1468
1469         assert(unit);
1470
1471         r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1472         if (r < 0)
1473                 return r;
1474
1475         return cg_path_get_unit(cgroup, unit);
1476 }
1477
1478 /**
1479  * Skip session-*.scope, but require it to be there.
1480  */
1481 static const char *skip_session(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("session-x.scope"))
1491                 return NULL;
1492
1493         if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1494                 char buf[n - 8 - 6 + 1];
1495
1496                 memcpy(buf, p + 8, n - 8 - 6);
1497                 buf[n - 8 - 6] = 0;
1498
1499                 /* Note that session scopes 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 (!session_id_valid(buf))
1505                         return false;
1506
1507                 p += n;
1508                 p += strspn(p, "/");
1509                 return p;
1510         }
1511
1512         return NULL;
1513 }
1514
1515 /**
1516  * Skip user@*.service, but require it to be there.
1517  */
1518 static const char *skip_user_manager(const char *p) {
1519         size_t n;
1520
1521         if (isempty(p))
1522                 return NULL;
1523
1524         p += strspn(p, "/");
1525
1526         n = strcspn(p, "/");
1527         if (n < strlen("user@x.service"))
1528                 return NULL;
1529
1530         if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1531                 char buf[n - 5 - 8 + 1];
1532
1533                 memcpy(buf, p + 5, n - 5 - 8);
1534                 buf[n - 5 - 8] = 0;
1535
1536                 /* Note that user manager services never need unescaping,
1537                  * since they cannot conflict with the kernel's own
1538                  * names, hence we don't need to call cg_unescape()
1539                  * here. */
1540
1541                 if (parse_uid(buf, NULL) < 0)
1542                         return NULL;
1543
1544                 p += n;
1545                 p += strspn(p, "/");
1546
1547                 return p;
1548         }
1549
1550         return NULL;
1551 }
1552
1553 static const char *skip_user_prefix(const char *path) {
1554         const char *e, *t;
1555
1556         assert(path);
1557
1558         /* Skip slices, if there are any */
1559         e = skip_slices(path);
1560
1561         /* Skip the user manager, if it's in the path now... */
1562         t = skip_user_manager(e);
1563         if (t)
1564                 return t;
1565
1566         /* Alternatively skip the user session if it is in the path... */
1567         return skip_session(e);
1568 }
1569
1570 int cg_path_get_user_unit(const char *path, char **ret) {
1571         const char *t;
1572
1573         assert(path);
1574         assert(ret);
1575
1576         t = skip_user_prefix(path);
1577         if (!t)
1578                 return -ENXIO;
1579
1580         /* And from here on it looks pretty much the same as for a
1581          * system unit, hence let's use the same parser from here
1582          * on. */
1583         return cg_path_get_unit(t, ret);
1584 }
1585
1586 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1587         _cleanup_free_ char *cgroup = NULL;
1588         int r;
1589
1590         assert(unit);
1591
1592         r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1593         if (r < 0)
1594                 return r;
1595
1596         return cg_path_get_user_unit(cgroup, unit);
1597 }
1598
1599 int cg_path_get_machine_name(const char *path, char **machine) {
1600         _cleanup_free_ char *u = NULL;
1601         const char *sl;
1602         int r;
1603
1604         r = cg_path_get_unit(path, &u);
1605         if (r < 0)
1606                 return r;
1607
1608         sl = strjoina("/run/systemd/machines/unit:", u);
1609         return readlink_malloc(sl, machine);
1610 }
1611
1612 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1613         _cleanup_free_ char *cgroup = NULL;
1614         int r;
1615
1616         assert(machine);
1617
1618         r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1619         if (r < 0)
1620                 return r;
1621
1622         return cg_path_get_machine_name(cgroup, machine);
1623 }
1624 #endif // 0
1625
1626 int cg_path_get_session(const char *path, char **session) {
1627 #if 0 /// UNNEEDED by elogind
1628         _cleanup_free_ char *unit = NULL;
1629         char *start, *end;
1630         int r;
1631
1632         assert(path);
1633
1634         r = cg_path_get_unit(path, &unit);
1635         if (r < 0)
1636                 return r;
1637
1638         start = startswith(unit, "session-");
1639         if (!start)
1640                 return -ENXIO;
1641         end = endswith(start, ".scope");
1642         if (!end)
1643                 return -ENXIO;
1644
1645         *end = 0;
1646         if (!session_id_valid(start))
1647                 return -ENXIO;
1648 #else
1649         /* Elogind uses a flat hierarchy, just "/SESSION".  The only
1650            wrinkle is that SESSION might be escaped.  */
1651         const char *e, *n, *start;
1652
1653         assert(path);
1654         log_debug_elogind("path is \"%s\"", path);
1655         assert(path[0] == '/');
1656
1657         e = path + 1;
1658         n = strchrnul(e, '/');
1659         if (e == n)
1660                 return -ENOENT;
1661
1662         start = strndupa(e, n - e);
1663         start = cg_unescape(start);
1664
1665         if (!start[0])
1666                 return -ENOENT;
1667 #endif // 0
1668
1669         if (session) {
1670                 char *rr;
1671
1672                 log_debug_elogind("found session: \"%s\"", start);
1673                 rr = strdup(start);
1674                 if (!rr)
1675                         return -ENOMEM;
1676
1677                 *session = rr;
1678         }
1679
1680         return 0;
1681 }
1682
1683 int cg_pid_get_session(pid_t pid, char **session) {
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_session(cgroup, session);
1692 }
1693
1694 #if 0 /// UNNEEDED by elogind
1695 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1696         _cleanup_free_ char *slice = NULL;
1697         char *start, *end;
1698         int r;
1699
1700         assert(path);
1701
1702         r = cg_path_get_slice(path, &slice);
1703         if (r < 0)
1704                 return r;
1705
1706         start = startswith(slice, "user-");
1707         if (!start)
1708                 return -ENXIO;
1709         end = endswith(start, ".slice");
1710         if (!end)
1711                 return -ENXIO;
1712
1713         *end = 0;
1714         if (parse_uid(start, uid) < 0)
1715                 return -ENXIO;
1716
1717         return 0;
1718 }
1719
1720 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1721         _cleanup_free_ char *cgroup = NULL;
1722         int r;
1723
1724         r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1725         if (r < 0)
1726                 return r;
1727
1728         return cg_path_get_owner_uid(cgroup, uid);
1729 }
1730
1731 int cg_path_get_slice(const char *p, char **slice) {
1732         const char *e = NULL;
1733
1734         assert(p);
1735         assert(slice);
1736
1737         /* Finds the right-most slice unit from the beginning, but
1738          * stops before we come to the first non-slice unit. */
1739
1740         for (;;) {
1741                 size_t n;
1742
1743                 p += strspn(p, "/");
1744
1745                 n = strcspn(p, "/");
1746                 if (!valid_slice_name(p, n)) {
1747
1748                         if (!e) {
1749                                 char *s;
1750
1751                                 s = strdup(SPECIAL_ROOT_SLICE);
1752                                 if (!s)
1753                                         return -ENOMEM;
1754
1755                                 *slice = s;
1756                                 return 0;
1757                         }
1758
1759                         return cg_path_decode_unit(e, slice);
1760                 }
1761
1762                 e = p;
1763                 p += n;
1764         }
1765 }
1766
1767 int cg_pid_get_slice(pid_t pid, char **slice) {
1768         _cleanup_free_ char *cgroup = NULL;
1769         int r;
1770
1771         assert(slice);
1772
1773         r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1774         if (r < 0)
1775                 return r;
1776
1777         return cg_path_get_slice(cgroup, slice);
1778 }
1779
1780 int cg_path_get_user_slice(const char *p, char **slice) {
1781         const char *t;
1782         assert(p);
1783         assert(slice);
1784
1785         t = skip_user_prefix(p);
1786         if (!t)
1787                 return -ENXIO;
1788
1789         /* And now it looks pretty much the same as for a system
1790          * slice, so let's just use the same parser from here on. */
1791         return cg_path_get_slice(t, slice);
1792 }
1793
1794 int cg_pid_get_user_slice(pid_t pid, char **slice) {
1795         _cleanup_free_ char *cgroup = NULL;
1796         int r;
1797
1798         assert(slice);
1799
1800         r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1801         if (r < 0)
1802                 return r;
1803
1804         return cg_path_get_user_slice(cgroup, slice);
1805 }
1806 #endif // 0
1807
1808 char *cg_escape(const char *p) {
1809         bool need_prefix = false;
1810
1811         /* This implements very minimal escaping for names to be used
1812          * as file names in the cgroup tree: any name which might
1813          * conflict with a kernel name or is prefixed with '_' is
1814          * prefixed with a '_'. That way, when reading cgroup names it
1815          * is sufficient to remove a single prefixing underscore if
1816          * there is one. */
1817
1818         /* The return value of this function (unlike cg_unescape())
1819          * needs free()! */
1820
1821         if (p[0] == 0 ||
1822             p[0] == '_' ||
1823             p[0] == '.' ||
1824             streq(p, "notify_on_release") ||
1825             streq(p, "release_agent") ||
1826             streq(p, "tasks") ||
1827             startswith(p, "cgroup."))
1828                 need_prefix = true;
1829         else {
1830                 const char *dot;
1831
1832                 dot = strrchr(p, '.');
1833                 if (dot) {
1834                         CGroupController c;
1835                         size_t l = dot - p;
1836
1837                         for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1838                                 const char *n;
1839
1840                                 n = cgroup_controller_to_string(c);
1841
1842                                 if (l != strlen(n))
1843                                         continue;
1844
1845                                 if (memcmp(p, n, l) != 0)
1846                                         continue;
1847
1848                                 need_prefix = true;
1849                                 break;
1850                         }
1851                 }
1852         }
1853
1854         if (need_prefix)
1855                 return strappend("_", p);
1856
1857         return strdup(p);
1858 }
1859
1860 char *cg_unescape(const char *p) {
1861         assert(p);
1862
1863         /* The return value of this function (unlike cg_escape())
1864          * doesn't need free()! */
1865
1866         if (p[0] == '_')
1867                 return (char*) p+1;
1868
1869         return (char*) p;
1870 }
1871
1872 #define CONTROLLER_VALID                        \
1873         DIGITS LETTERS                          \
1874         "_"
1875
1876 bool cg_controller_is_valid(const char *p) {
1877         const char *t, *s;
1878
1879         if (!p)
1880                 return false;
1881
1882         s = startswith(p, "name=");
1883         if (s)
1884                 p = s;
1885
1886         if (*p == 0 || *p == '_')
1887                 return false;
1888
1889         for (t = p; *t; t++)
1890                 if (!strchr(CONTROLLER_VALID, *t))
1891                         return false;
1892
1893         if (t - p > FILENAME_MAX)
1894                 return false;
1895
1896         return true;
1897 }
1898
1899 #if 0 /// UNNEEDED by elogind
1900 int cg_slice_to_path(const char *unit, char **ret) {
1901         _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1902         const char *dash;
1903         int r;
1904
1905         assert(unit);
1906         assert(ret);
1907
1908         if (streq(unit, SPECIAL_ROOT_SLICE)) {
1909                 char *x;
1910
1911                 x = strdup("");
1912                 if (!x)
1913                         return -ENOMEM;
1914                 *ret = x;
1915                 return 0;
1916         }
1917
1918         if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
1919                 return -EINVAL;
1920
1921         if (!endswith(unit, ".slice"))
1922                 return -EINVAL;
1923
1924         r = unit_name_to_prefix(unit, &p);
1925         if (r < 0)
1926                 return r;
1927
1928         dash = strchr(p, '-');
1929
1930         /* Don't allow initial dashes */
1931         if (dash == p)
1932                 return -EINVAL;
1933
1934         while (dash) {
1935                 _cleanup_free_ char *escaped = NULL;
1936                 char n[dash - p + sizeof(".slice")];
1937
1938                 /* Don't allow trailing or double dashes */
1939                 if (dash[1] == 0 || dash[1] == '-')
1940                         return -EINVAL;
1941
1942                 strcpy(stpncpy(n, p, dash - p), ".slice");
1943                 if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
1944                         return -EINVAL;
1945
1946                 escaped = cg_escape(n);
1947                 if (!escaped)
1948                         return -ENOMEM;
1949
1950                 if (!strextend(&s, escaped, "/", NULL))
1951                         return -ENOMEM;
1952
1953                 dash = strchr(dash+1, '-');
1954         }
1955
1956         e = cg_escape(unit);
1957         if (!e)
1958                 return -ENOMEM;
1959
1960         if (!strextend(&s, e, NULL))
1961                 return -ENOMEM;
1962
1963         *ret = s;
1964         s = NULL;
1965
1966         return 0;
1967 }
1968 #endif // 0
1969
1970 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
1971         _cleanup_free_ char *p = NULL;
1972         int r;
1973
1974         r = cg_get_path(controller, path, attribute, &p);
1975         if (r < 0)
1976                 return r;
1977
1978         return write_string_file(p, value, 0);
1979 }
1980
1981 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
1982         _cleanup_free_ char *p = NULL;
1983         int r;
1984
1985         r = cg_get_path(controller, path, attribute, &p);
1986         if (r < 0)
1987                 return r;
1988
1989         return read_one_line_file(p, ret);
1990 }
1991
1992 #if 0 /// UNNEEDED by elogind
1993 int cg_get_keyed_attribute(const char *controller, const char *path, const char *attribute, const char **keys, char **values) {
1994         _cleanup_free_ char *filename = NULL, *content = NULL;
1995         char *line, *p;
1996         int i, r;
1997
1998         for (i = 0; keys[i]; i++)
1999                 values[i] = NULL;
2000
2001         r = cg_get_path(controller, path, attribute, &filename);
2002         if (r < 0)
2003                 return r;
2004
2005         r = read_full_file(filename, &content, NULL);
2006         if (r < 0)
2007                 return r;
2008
2009         p = content;
2010         while ((line = strsep(&p, "\n"))) {
2011                 char *key;
2012
2013                 key = strsep(&line, " ");
2014
2015                 for (i = 0; keys[i]; i++) {
2016                         if (streq(key, keys[i])) {
2017                                 values[i] = strdup(line);
2018                                 break;
2019                         }
2020                 }
2021         }
2022
2023         for (i = 0; keys[i]; i++) {
2024                 if (!values[i]) {
2025                         for (i = 0; keys[i]; i++) {
2026                                 free(values[i]);
2027                                 values[i] = NULL;
2028                         }
2029                         return -ENOENT;
2030                 }
2031         }
2032
2033         return 0;
2034 }
2035
2036 int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path) {
2037         CGroupController c;
2038         int r, unified;
2039
2040         /* This one will create a cgroup in our private tree, but also
2041          * duplicate it in the trees specified in mask, and remove it
2042          * in all others */
2043
2044         /* First create the cgroup in our own hierarchy. */
2045         r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
2046         if (r < 0)
2047                 return r;
2048
2049         /* If we are in the unified hierarchy, we are done now */
2050         unified = cg_all_unified();
2051         if (unified < 0)
2052                 return unified;
2053         if (unified > 0)
2054                 return 0;
2055
2056         /* Otherwise, do the same in the other hierarchies */
2057         for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2058                 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2059                 const char *n;
2060
2061                 n = cgroup_controller_to_string(c);
2062
2063                 if (mask & bit)
2064                         (void) cg_create(n, path);
2065                 else if (supported & bit)
2066                         (void) cg_trim(n, path, true);
2067         }
2068
2069         return 0;
2070 }
2071
2072 int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
2073         CGroupController c;
2074         int r, unified;
2075
2076         r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
2077         if (r < 0)
2078                 return r;
2079
2080         unified = cg_all_unified();
2081         if (unified < 0)
2082                 return unified;
2083         if (unified > 0)
2084                 return 0;
2085
2086         for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2087                 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2088                 const char *p = NULL;
2089
2090                 if (!(supported & bit))
2091                         continue;
2092
2093                 if (path_callback)
2094                         p = path_callback(bit, userdata);
2095
2096                 if (!p)
2097                         p = path;
2098
2099                 (void) cg_attach_fallback(cgroup_controller_to_string(c), p, pid);
2100         }
2101
2102         return 0;
2103 }
2104
2105 int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
2106         Iterator i;
2107         void *pidp;
2108         int r = 0;
2109
2110         SET_FOREACH(pidp, pids, i) {
2111                 pid_t pid = PTR_TO_PID(pidp);
2112                 int q;
2113
2114                 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
2115                 if (q < 0 && r >= 0)
2116                         r = q;
2117         }
2118
2119         return r;
2120 }
2121
2122 int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
2123         CGroupController c;
2124         int r = 0, unified;
2125
2126         if (!path_equal(from, to))  {
2127                 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, CGROUP_REMOVE);
2128                 if (r < 0)
2129                         return r;
2130         }
2131
2132         unified = cg_all_unified();
2133         if (unified < 0)
2134                 return unified;
2135         if (unified > 0)
2136                 return r;
2137
2138         for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2139                 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2140                 const char *p = NULL;
2141
2142                 if (!(supported & bit))
2143                         continue;
2144
2145                 if (to_callback)
2146                         p = to_callback(bit, userdata);
2147
2148                 if (!p)
2149                         p = to;
2150
2151                 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, cgroup_controller_to_string(c), p, 0);
2152         }
2153
2154         return 0;
2155 }
2156
2157 int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root) {
2158         CGroupController c;
2159         int r, unified;
2160
2161         r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
2162         if (r < 0)
2163                 return r;
2164
2165         unified = cg_all_unified();
2166         if (unified < 0)
2167                 return unified;
2168         if (unified > 0)
2169                 return r;
2170
2171         for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2172                 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2173
2174                 if (!(supported & bit))
2175                         continue;
2176
2177                 (void) cg_trim(cgroup_controller_to_string(c), path, delete_root);
2178         }
2179
2180         return 0;
2181 }
2182 #endif // 0
2183
2184 int cg_mask_supported(CGroupMask *ret) {
2185         CGroupMask mask = 0;
2186         int r, unified;
2187
2188         /* Determines the mask of supported cgroup controllers. Only
2189          * includes controllers we can make sense of and that are
2190          * actually accessible. */
2191
2192         unified = cg_all_unified();
2193         if (unified < 0)
2194                 return unified;
2195         if (unified > 0) {
2196                 _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
2197                 const char *c;
2198
2199                 /* In the unified hierarchy we can read the supported
2200                  * and accessible controllers from a the top-level
2201                  * cgroup attribute */
2202
2203                 r = cg_get_root_path(&root);
2204                 if (r < 0)
2205                         return r;
2206
2207                 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path);
2208                 if (r < 0)
2209                         return r;
2210
2211                 r = read_one_line_file(path, &controllers);
2212                 if (r < 0)
2213                         return r;
2214
2215                 c = controllers;
2216                 for (;;) {
2217                         _cleanup_free_ char *n = NULL;
2218                         CGroupController v;
2219
2220                         r = extract_first_word(&c, &n, NULL, 0);
2221                         if (r < 0)
2222                                 return r;
2223                         if (r == 0)
2224                                 break;
2225
2226                         v = cgroup_controller_from_string(n);
2227                         if (v < 0)
2228                                 continue;
2229
2230                         mask |= CGROUP_CONTROLLER_TO_MASK(v);
2231                 }
2232
2233                 /* Currently, we support the cpu, memory, io and pids
2234                  * controller in the unified hierarchy, mask
2235                  * everything else off. */
2236                 mask &= CGROUP_MASK_CPU | CGROUP_MASK_MEMORY | CGROUP_MASK_IO | CGROUP_MASK_PIDS;
2237
2238         } else {
2239                 CGroupController c;
2240
2241                 /* In the legacy hierarchy, we check whether which
2242                  * hierarchies are mounted. */
2243
2244                 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2245                         const char *n;
2246
2247                         n = cgroup_controller_to_string(c);
2248                         if (controller_is_accessible(n) >= 0)
2249                                 mask |= CGROUP_CONTROLLER_TO_MASK(c);
2250                 }
2251         }
2252
2253         *ret = mask;
2254         return 0;
2255 }
2256
2257 #if 0 /// UNNEEDED by elogind
2258 int cg_kernel_controllers(Set *controllers) {
2259         _cleanup_fclose_ FILE *f = NULL;
2260         char buf[LINE_MAX];
2261         int r;
2262
2263         assert(controllers);
2264
2265         /* Determines the full list of kernel-known controllers. Might
2266          * include controllers we don't actually support, arbitrary
2267          * named hierarchies and controllers that aren't currently
2268          * accessible (because not mounted). */
2269
2270         f = fopen("/proc/cgroups", "re");
2271         if (!f) {
2272                 if (errno == ENOENT)
2273                         return 0;
2274                 return -errno;
2275         }
2276
2277         /* Ignore the header line */
2278         (void) fgets(buf, sizeof(buf), f);
2279
2280         for (;;) {
2281                 char *controller;
2282                 int enabled = 0;
2283
2284                 errno = 0;
2285                 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
2286
2287                         if (feof(f))
2288                                 break;
2289
2290                         if (ferror(f) && errno > 0)
2291                                 return -errno;
2292
2293                         return -EBADMSG;
2294                 }
2295
2296                 if (!enabled) {
2297                         free(controller);
2298                         continue;
2299                 }
2300
2301                 if (!cg_controller_is_valid(controller)) {
2302                         free(controller);
2303                         return -EBADMSG;
2304                 }
2305
2306                 r = set_consume(controllers, controller);
2307                 if (r < 0)
2308                         return r;
2309         }
2310
2311         return 0;
2312 }
2313 #endif // 0
2314
2315 static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN;
2316
2317 static int cg_update_unified(void) {
2318
2319         struct statfs fs;
2320
2321         /* Checks if we support the unified hierarchy. Returns an
2322          * error when the cgroup hierarchies aren't mounted yet or we
2323          * have any other trouble determining if the unified hierarchy
2324          * is supported. */
2325
2326         if (unified_cache >= CGROUP_UNIFIED_NONE)
2327                 return 0;
2328
2329         if (statfs("/sys/fs/cgroup/", &fs) < 0)
2330                 return -errno;
2331
2332 #if 0 /// UNNEEDED by elogind
2333         if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC))
2334                 unified_cache = CGROUP_UNIFIED_ALL;
2335         else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) {
2336 #else
2337         /* elogind can not support the unified hierarchy as a controller,
2338          * so always assume a classical hierarchy.
2339          * If, and only *if*, someone really wants to substitute systemd-login
2340          * in an environment managed by systemd with elogind, we might have to
2341          * add such a support. */
2342         if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) {
2343 #endif // 0
2344                 if (statfs("/sys/fs/cgroup/systemd/", &fs) < 0)
2345                         return -errno;
2346
2347                 unified_cache = F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC) ?
2348                         CGROUP_UNIFIED_SYSTEMD : CGROUP_UNIFIED_NONE;
2349         } else
2350                 return -ENOMEDIUM;
2351
2352         return 0;
2353 }
2354
2355 int cg_unified(const char *controller) {
2356
2357         int r;
2358
2359         r = cg_update_unified();
2360         if (r < 0)
2361                 return r;
2362
2363         if (streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER))
2364                 return unified_cache >= CGROUP_UNIFIED_SYSTEMD;
2365         else
2366                 return unified_cache >= CGROUP_UNIFIED_ALL;
2367 }
2368
2369 int cg_all_unified(void) {
2370
2371         return cg_unified(NULL);
2372 }
2373
2374 #if 0 /// UNNEEDED by elogind
2375 void cg_unified_flush(void) {
2376         unified_cache = CGROUP_UNIFIED_UNKNOWN;
2377 }
2378
2379 int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
2380         _cleanup_free_ char *fs = NULL;
2381         CGroupController c;
2382         int r, unified;
2383
2384         assert(p);
2385
2386         if (supported == 0)
2387                 return 0;
2388
2389         unified = cg_all_unified();
2390         if (unified < 0)
2391                 return unified;
2392         if (!unified) /* on the legacy hiearchy there's no joining of controllers defined */
2393                 return 0;
2394
2395         r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, p, "cgroup.subtree_control", &fs);
2396         if (r < 0)
2397                 return r;
2398
2399         for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2400                 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2401                 const char *n;
2402
2403                 if (!(supported & bit))
2404                         continue;
2405
2406                 n = cgroup_controller_to_string(c);
2407                 {
2408                         char s[1 + strlen(n) + 1];
2409
2410                         s[0] = mask & bit ? '+' : '-';
2411                         strcpy(s + 1, n);
2412
2413                         r = write_string_file(fs, s, 0);
2414                         if (r < 0)
2415                                 log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs);
2416                 }
2417         }
2418
2419         return 0;
2420 }
2421
2422 bool cg_is_unified_wanted(void) {
2423         static thread_local int wanted = -1;
2424         int r, unified;
2425
2426         /* If the hierarchy is already mounted, then follow whatever
2427          * was chosen for it. */
2428         unified = cg_all_unified();
2429         if (unified >= 0)
2430                 return unified;
2431
2432         /* Otherwise, let's see what the kernel command line has to
2433          * say. Since checking that is expensive, let's cache the
2434          * result. */
2435         if (wanted >= 0)
2436                 return wanted;
2437
2438         r = get_proc_cmdline_key("systemd.unified_cgroup_hierarchy", NULL);
2439         if (r > 0)
2440                 return (wanted = true);
2441         else {
2442                 _cleanup_free_ char *value = NULL;
2443
2444                 r = get_proc_cmdline_key("systemd.unified_cgroup_hierarchy=", &value);
2445                 if (r < 0)
2446                         return false;
2447                 if (r == 0)
2448                         return (wanted = false);
2449
2450                 return (wanted = parse_boolean(value) > 0);
2451         }
2452 }
2453
2454 bool cg_is_legacy_wanted(void) {
2455         return !cg_is_unified_wanted();
2456 }
2457 #else
2458 bool cg_is_legacy_wanted(void) {
2459         return true;
2460
2461 bool cg_is_unified_systemd_controller_wanted(void) {
2462         static thread_local int wanted = -1;
2463         int r, unified;
2464
2465         /* If the unified hierarchy is requested in full, no need to
2466          * bother with this. */
2467         if (cg_is_unified_wanted())
2468                 return 0;
2469
2470         /* If the hierarchy is already mounted, then follow whatever
2471          * was chosen for it. */
2472         unified = cg_unified(SYSTEMD_CGROUP_CONTROLLER);
2473         if (unified >= 0)
2474                 return unified;
2475
2476         /* Otherwise, let's see what the kernel command line has to
2477          * say. Since checking that is expensive, let's cache the
2478          * result. */
2479         if (wanted >= 0)
2480                 return wanted;
2481
2482         r = get_proc_cmdline_key("systemd.legacy_systemd_cgroup_controller", NULL);
2483         if (r > 0) {
2484         if (r > 0)
2485                 wanted = false;
2486         } else {
2487         else {
2488                 _cleanup_free_ char *value = NULL;
2489
2490                 r = get_proc_cmdline_key("systemd.legacy_systemd_cgroup_controller=", &value);
2491                 if (r < 0)
2492                         return true;
2493
2494                 if (r == 0)
2495                         wanted = true;
2496                 else
2497                         wanted = parse_boolean(value) <= 0;
2498         }
2499
2500         return wanted;
2501 }
2502
2503 bool cg_is_legacy_systemd_controller_wanted(void) {
2504         return cg_is_legacy_wanted() && !cg_is_unified_systemd_controller_wanted();
2505 }
2506 #endif // 0
2507
2508 #if 0 /// UNNEEDED by elogind
2509 int cg_weight_parse(const char *s, uint64_t *ret) {
2510         uint64_t u;
2511         int r;
2512
2513         if (isempty(s)) {
2514                 *ret = CGROUP_WEIGHT_INVALID;
2515                 return 0;
2516         }
2517
2518         r = safe_atou64(s, &u);
2519         if (r < 0)
2520                 return r;
2521
2522         if (u < CGROUP_WEIGHT_MIN || u > CGROUP_WEIGHT_MAX)
2523                 return -ERANGE;
2524
2525         *ret = u;
2526         return 0;
2527 }
2528
2529 const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX] = {
2530         [CGROUP_IO_RBPS_MAX]    = CGROUP_LIMIT_MAX,
2531         [CGROUP_IO_WBPS_MAX]    = CGROUP_LIMIT_MAX,
2532         [CGROUP_IO_RIOPS_MAX]   = CGROUP_LIMIT_MAX,
2533         [CGROUP_IO_WIOPS_MAX]   = CGROUP_LIMIT_MAX,
2534 };
2535
2536 static const char* const cgroup_io_limit_type_table[_CGROUP_IO_LIMIT_TYPE_MAX] = {
2537         [CGROUP_IO_RBPS_MAX]    = "IOReadBandwidthMax",
2538         [CGROUP_IO_WBPS_MAX]    = "IOWriteBandwidthMax",
2539         [CGROUP_IO_RIOPS_MAX]   = "IOReadIOPSMax",
2540         [CGROUP_IO_WIOPS_MAX]   = "IOWriteIOPSMax",
2541 };
2542
2543 DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type, CGroupIOLimitType);
2544
2545 int cg_cpu_shares_parse(const char *s, uint64_t *ret) {
2546         uint64_t u;
2547         int r;
2548
2549         if (isempty(s)) {
2550                 *ret = CGROUP_CPU_SHARES_INVALID;
2551                 return 0;
2552         }
2553
2554         r = safe_atou64(s, &u);
2555         if (r < 0)
2556                 return r;
2557
2558         if (u < CGROUP_CPU_SHARES_MIN || u > CGROUP_CPU_SHARES_MAX)
2559                 return -ERANGE;
2560
2561         *ret = u;
2562         return 0;
2563 }
2564
2565 int cg_blkio_weight_parse(const char *s, uint64_t *ret) {
2566         uint64_t u;
2567         int r;
2568
2569         if (isempty(s)) {
2570                 *ret = CGROUP_BLKIO_WEIGHT_INVALID;
2571                 return 0;
2572         }
2573
2574         r = safe_atou64(s, &u);
2575         if (r < 0)
2576                 return r;
2577
2578         if (u < CGROUP_BLKIO_WEIGHT_MIN || u > CGROUP_BLKIO_WEIGHT_MAX)
2579                 return -ERANGE;
2580
2581         *ret = u;
2582         return 0;
2583 }
2584 #endif // 0
2585
2586 bool is_cgroup_fs(const struct statfs *s) {
2587         return is_fs_type(s, CGROUP_SUPER_MAGIC) ||
2588                is_fs_type(s, CGROUP2_SUPER_MAGIC);
2589 }
2590
2591 bool fd_is_cgroup_fs(int fd) {
2592         struct statfs s;
2593
2594         if (fstatfs(fd, &s) < 0)
2595                 return -errno;
2596
2597         return is_cgroup_fs(&s);
2598 }
2599
2600 static const char *cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = {
2601         [CGROUP_CONTROLLER_CPU] = "cpu",
2602         [CGROUP_CONTROLLER_CPUACCT] = "cpuacct",
2603         [CGROUP_CONTROLLER_IO] = "io",
2604         [CGROUP_CONTROLLER_BLKIO] = "blkio",
2605         [CGROUP_CONTROLLER_MEMORY] = "memory",
2606         [CGROUP_CONTROLLER_DEVICES] = "devices",
2607         [CGROUP_CONTROLLER_PIDS] = "pids",
2608 };
2609
2610 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController);