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