]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/cgroups/cgfsng.c
cgroups: implement "driver" and "driver_version"
[mirror_lxc.git] / src / lxc / cgroups / cgfsng.c
1 /*
2 * lxc: linux Container library
3 *
4 * Copyright © 2016 Canonical Ltd.
5 *
6 * Authors:
7 * Serge Hallyn <serge.hallyn@ubuntu.com>
8 * Christian Brauner <christian.brauner@ubuntu.com>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /*
26 * cgfs-ng.c: this is a new, simplified implementation of a filesystem
27 * cgroup backend. The original cgfs.c was designed to be as flexible
28 * as possible. It would try to find cgroup filesystems no matter where
29 * or how you had them mounted, and deduce the most usable mount for
30 * each controller.
31 *
32 * This new implementation assumes that cgroup filesystems are mounted
33 * under /sys/fs/cgroup/clist where clist is either the controller, or
34 * a comman-separated list of controllers.
35 */
36
37 #include "config.h"
38
39 #include <ctype.h>
40 #include <dirent.h>
41 #include <errno.h>
42 #include <grp.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <linux/kdev_t.h>
49 #include <linux/types.h>
50 #include <sys/types.h>
51
52 #include "caps.h"
53 #include "cgroup.h"
54 #include "cgroup_utils.h"
55 #include "commands.h"
56 #include "conf.h"
57 #include "log.h"
58 #include "storage/storage.h"
59 #include "utils.h"
60
61 lxc_log_define(lxc_cgfsng, lxc);
62
63 static struct cgroup_ops cgfsng_ops;
64
65 /* A descriptor for a mounted hierarchy
66 *
67 * @controllers
68 * - legacy hierarchy
69 * Either NULL, or a null-terminated list of all the co-mounted controllers.
70 * - unified hierarchy
71 * Either NULL, or a null-terminated list of all enabled controllers.
72 *
73 * @mountpoint
74 * - The mountpoint we will use.
75 * - legacy hierarchy
76 * It will be either /sys/fs/cgroup/controller or
77 * /sys/fs/cgroup/controllerlist.
78 * - unified hierarchy
79 * It will either be /sys/fs/cgroup or /sys/fs/cgroup/<mountpoint-name>
80 * depending on whether this is a hybrid cgroup layout (mix of legacy and
81 * unified hierarchies) or a pure unified cgroup layout.
82 *
83 * @base_cgroup
84 * - The cgroup under which the container cgroup path
85 * is created. This will be either the caller's cgroup (if not root), or
86 * init's cgroup (if root).
87 *
88 * @fullcgpath
89 * - The full path to the containers cgroup.
90 *
91 * @version
92 * - legacy hierarchy
93 * If the hierarchy is a legacy hierarchy this will be set to
94 * CGROUP_SUPER_MAGIC.
95 * - unified hierarchy
96 * If the hierarchy is a legacy hierarchy this will be set to
97 * CGROUP2_SUPER_MAGIC.
98 */
99 struct hierarchy {
100 char **controllers;
101 char *mountpoint;
102 char *base_cgroup;
103 char *fullcgpath;
104 int version;
105 };
106
107 /* The cgroup data which is attached to the lxc_handler.
108 *
109 * @cgroup_pattern
110 * - A copy of lxc.cgroup.pattern.
111 *
112 * @container_cgroup
113 * - If not null, the cgroup which was created for the container. For each
114 * hierarchy, it is created under the @hierarchy->base_cgroup directory.
115 * Relative to the base_cgroup it is the same for all hierarchies.
116 *
117 * @name
118 * - The name of the container.
119 *
120 * @cgroup_meta
121 * - A copy of the container's cgroup information. This overrides
122 * @cgroup_pattern.
123 *
124 * @cgroup_layout
125 * - What cgroup layout the container is running with.
126 * - CGROUP_LAYOUT_UNKNOWN
127 * The cgroup layout could not be determined. This should be treated as an
128 * error condition.
129 * - CGROUP_LAYOUT_LEGACY
130 * The container is running with all controllers mounted into legacy cgroup
131 * hierarchies.
132 * - CGROUP_LAYOUT_HYBRID
133 * The container is running with at least one controller mounted into a
134 * legacy cgroup hierarchy and a mountpoint for the unified hierarchy. The
135 * unified hierarchy can be empty (no controllers enabled) or non-empty
136 * (controllers enabled).
137 * - CGROUP_LAYOUT_UNIFIED
138 * The container is running on a pure unified cgroup hierarchy. The unified
139 * hierarchy can be empty (no controllers enabled) or non-empty (controllers
140 * enabled).
141 */
142 struct cgfsng_handler_data {
143 char *cgroup_pattern;
144 char *container_cgroup; /* cgroup we created for the container */
145 char *name; /* container name */
146 /* per-container cgroup information */
147 struct lxc_cgroup cgroup_meta;
148 cgroup_layout_t cgroup_layout;
149 };
150
151 /* @hierarchies
152 * - A NULL-terminated array of struct hierarchy, one per legacy hierarchy. No
153 * duplicates. First sufficient, writeable mounted hierarchy wins.
154 */
155 struct hierarchy **hierarchies;
156 /* Pointer to the unified hierarchy in the null terminated list @hierarchies.
157 * This is merely a convenience for hybrid cgroup layouts to easily retrieve the
158 * unified hierarchy without iterating throught @hierarchies.
159 */
160 struct hierarchy *unified;
161 /*
162 * @cgroup_layout
163 * - What cgroup layout the container is running with.
164 * - CGROUP_LAYOUT_UNKNOWN
165 * The cgroup layout could not be determined. This should be treated as an
166 * error condition.
167 * - CGROUP_LAYOUT_LEGACY
168 * The container is running with all controllers mounted into legacy cgroup
169 * hierarchies.
170 * - CGROUP_LAYOUT_HYBRID
171 * The container is running with at least one controller mounted into a
172 * legacy cgroup hierarchy and a mountpoint for the unified hierarchy. The
173 * unified hierarchy can be empty (no controllers enabled) or non-empty
174 * (controllers enabled).
175 * - CGROUP_LAYOUT_UNIFIED
176 * The container is running on a pure unified cgroup hierarchy. The unified
177 * hierarchy can be empty (no controllers enabled) or non-empty (controllers
178 * enabled).
179 */
180 cgroup_layout_t cgroup_layout;
181 /* What controllers is the container supposed to use. */
182 char *cgroup_use;
183
184 /* @lxc_cgfsng_debug
185 * - Whether to print debug info to stdout for the cgfsng driver.
186 */
187 static bool lxc_cgfsng_debug;
188
189 #define CGFSNG_DEBUG(format, ...) \
190 do { \
191 if (lxc_cgfsng_debug) \
192 printf("cgfsng: " format, ##__VA_ARGS__); \
193 } while (0)
194
195 static void free_string_list(char **clist)
196 {
197 int i;
198
199 if (!clist)
200 return;
201
202 for (i = 0; clist[i]; i++)
203 free(clist[i]);
204
205 free(clist);
206 }
207
208 /* Allocate a pointer, do not fail. */
209 static void *must_alloc(size_t sz)
210 {
211 return must_realloc(NULL, sz);
212 }
213
214 /* Given a pointer to a null-terminated array of pointers, realloc to add one
215 * entry, and point the new entry to NULL. Do not fail. Return the index to the
216 * second-to-last entry - that is, the one which is now available for use
217 * (keeping the list null-terminated).
218 */
219 static int append_null_to_list(void ***list)
220 {
221 int newentry = 0;
222
223 if (*list)
224 for (; (*list)[newentry]; newentry++)
225 ;
226
227 *list = must_realloc(*list, (newentry + 2) * sizeof(void **));
228 (*list)[newentry + 1] = NULL;
229 return newentry;
230 }
231
232 /* Given a null-terminated array of strings, check whether @entry is one of the
233 * strings.
234 */
235 static bool string_in_list(char **list, const char *entry)
236 {
237 int i;
238
239 if (!list)
240 return false;
241
242 for (i = 0; list[i]; i++)
243 if (strcmp(list[i], entry) == 0)
244 return true;
245
246 return false;
247 }
248
249 /* Return a copy of @entry prepending "name=", i.e. turn "systemd" into
250 * "name=systemd". Do not fail.
251 */
252 static char *cg_legacy_must_prefix_named(char *entry)
253 {
254 size_t len;
255 char *prefixed;
256
257 len = strlen(entry);
258 prefixed = must_alloc(len + 6);
259
260 memcpy(prefixed, "name=", sizeof("name="));
261 memcpy(prefixed + sizeof("name="), entry, len);
262 prefixed[len + 5] = '\0';
263 return prefixed;
264 }
265
266 /* Append an entry to the clist. Do not fail. @clist must be NULL the first time
267 * we are called.
268 *
269 * We also handle named subsystems here. Any controller which is not a kernel
270 * subsystem, we prefix "name=". Any which is both a kernel and named subsystem,
271 * we refuse to use because we're not sure which we have here.
272 * (TODO: We could work around this in some cases by just remounting to be
273 * unambiguous, or by comparing mountpoint contents with current cgroup.)
274 *
275 * The last entry will always be NULL.
276 */
277 static void must_append_controller(char **klist, char **nlist, char ***clist,
278 char *entry)
279 {
280 int newentry;
281 char *copy;
282
283 if (string_in_list(klist, entry) && string_in_list(nlist, entry)) {
284 ERROR("Refusing to use ambiguous controller \"%s\"", entry);
285 ERROR("It is both a named and kernel subsystem");
286 return;
287 }
288
289 newentry = append_null_to_list((void ***)clist);
290
291 if (strncmp(entry, "name=", 5) == 0)
292 copy = must_copy_string(entry);
293 else if (string_in_list(klist, entry))
294 copy = must_copy_string(entry);
295 else
296 copy = cg_legacy_must_prefix_named(entry);
297
298 (*clist)[newentry] = copy;
299 }
300
301 static void free_handler_data(struct cgfsng_handler_data *d)
302 {
303 free(d->cgroup_pattern);
304 free(d->container_cgroup);
305 free(d->name);
306 if (d->cgroup_meta.dir)
307 free(d->cgroup_meta.dir);
308 if (d->cgroup_meta.controllers)
309 free(d->cgroup_meta.controllers);
310 free(d);
311 }
312
313 /* Given a handler's cgroup data, return the struct hierarchy for the controller
314 * @c, or NULL if there is none.
315 */
316 struct hierarchy *get_hierarchy(const char *c)
317 {
318 int i;
319
320 if (!hierarchies)
321 return NULL;
322
323 for (i = 0; hierarchies[i]; i++) {
324 if (!c) {
325 /* This is the empty unified hierarchy. */
326 if (hierarchies[i]->controllers &&
327 !hierarchies[i]->controllers[0])
328 return hierarchies[i];
329
330 return NULL;
331 }
332
333 if (string_in_list(hierarchies[i]->controllers, c))
334 return hierarchies[i];
335 }
336
337 return NULL;
338 }
339
340 #define BATCH_SIZE 50
341 static void batch_realloc(char **mem, size_t oldlen, size_t newlen)
342 {
343 int newbatches = (newlen / BATCH_SIZE) + 1;
344 int oldbatches = (oldlen / BATCH_SIZE) + 1;
345
346 if (!*mem || newbatches > oldbatches) {
347 *mem = must_realloc(*mem, newbatches * BATCH_SIZE);
348 }
349 }
350
351 static void append_line(char **dest, size_t oldlen, char *new, size_t newlen)
352 {
353 size_t full = oldlen + newlen;
354
355 batch_realloc(dest, oldlen, full + 1);
356
357 memcpy(*dest + oldlen, new, newlen + 1);
358 }
359
360 /* Slurp in a whole file */
361 static char *read_file(const char *fnam)
362 {
363 FILE *f;
364 char *line = NULL, *buf = NULL;
365 size_t len = 0, fulllen = 0;
366 int linelen;
367
368 f = fopen(fnam, "r");
369 if (!f)
370 return NULL;
371 while ((linelen = getline(&line, &len, f)) != -1) {
372 append_line(&buf, fulllen, line, linelen);
373 fulllen += linelen;
374 }
375 fclose(f);
376 free(line);
377 return buf;
378 }
379
380 /* Taken over modified from the kernel sources. */
381 #define NBITS 32 /* bits in uint32_t */
382 #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
383 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, NBITS)
384
385 static void set_bit(unsigned bit, uint32_t *bitarr)
386 {
387 bitarr[bit / NBITS] |= (1 << (bit % NBITS));
388 }
389
390 static void clear_bit(unsigned bit, uint32_t *bitarr)
391 {
392 bitarr[bit / NBITS] &= ~(1 << (bit % NBITS));
393 }
394
395 static bool is_set(unsigned bit, uint32_t *bitarr)
396 {
397 return (bitarr[bit / NBITS] & (1 << (bit % NBITS))) != 0;
398 }
399
400 /* Create cpumask from cpulist aka turn:
401 *
402 * 0,2-3
403 *
404 * into bit array
405 *
406 * 1 0 1 1
407 */
408 static uint32_t *lxc_cpumask(char *buf, size_t nbits)
409 {
410 char *token;
411 size_t arrlen;
412 uint32_t *bitarr;
413 char *saveptr = NULL;
414
415 arrlen = BITS_TO_LONGS(nbits);
416 bitarr = calloc(arrlen, sizeof(uint32_t));
417 if (!bitarr)
418 return NULL;
419
420 for (; (token = strtok_r(buf, ",", &saveptr)); buf = NULL) {
421 errno = 0;
422 unsigned end, start;
423 char *range;
424
425 start = strtoul(token, NULL, 0);
426 end = start;
427 range = strchr(token, '-');
428 if (range)
429 end = strtoul(range + 1, NULL, 0);
430
431 if (!(start <= end)) {
432 free(bitarr);
433 return NULL;
434 }
435
436 if (end >= nbits) {
437 free(bitarr);
438 return NULL;
439 }
440
441 while (start <= end)
442 set_bit(start++, bitarr);
443 }
444
445 return bitarr;
446 }
447
448 /* Turn cpumask into simple, comma-separated cpulist. */
449 static char *lxc_cpumask_to_cpulist(uint32_t *bitarr, size_t nbits)
450 {
451 int ret;
452 size_t i;
453 char **cpulist = NULL;
454 char numstr[LXC_NUMSTRLEN64] = {0};
455
456 for (i = 0; i <= nbits; i++) {
457 if (!is_set(i, bitarr))
458 continue;
459
460 ret = snprintf(numstr, LXC_NUMSTRLEN64, "%zu", i);
461 if (ret < 0 || (size_t)ret >= LXC_NUMSTRLEN64) {
462 lxc_free_array((void **)cpulist, free);
463 return NULL;
464 }
465
466 ret = lxc_append_string(&cpulist, numstr);
467 if (ret < 0) {
468 lxc_free_array((void **)cpulist, free);
469 return NULL;
470 }
471 }
472
473 if (!cpulist)
474 return NULL;
475
476 return lxc_string_join(",", (const char **)cpulist, false);
477 }
478
479 static ssize_t get_max_cpus(char *cpulist)
480 {
481 char *c1, *c2;
482 char *maxcpus = cpulist;
483 size_t cpus = 0;
484
485 c1 = strrchr(maxcpus, ',');
486 if (c1)
487 c1++;
488
489 c2 = strrchr(maxcpus, '-');
490 if (c2)
491 c2++;
492
493 if (!c1 && !c2)
494 c1 = maxcpus;
495 else if (c1 > c2)
496 c2 = c1;
497 else if (c1 < c2)
498 c1 = c2;
499 else if (!c1 && c2)
500 c1 = c2;
501
502 errno = 0;
503 cpus = strtoul(c1, NULL, 0);
504 if (errno != 0)
505 return -1;
506
507 return cpus;
508 }
509
510 #define __ISOL_CPUS "/sys/devices/system/cpu/isolated"
511 static bool cg_legacy_filter_and_set_cpus(char *path, bool am_initialized)
512 {
513 int ret;
514 ssize_t i;
515 char *lastslash, *fpath, oldv;
516 ssize_t maxisol = 0, maxposs = 0;
517 char *cpulist = NULL, *isolcpus = NULL, *posscpus = NULL;
518 uint32_t *isolmask = NULL, *possmask = NULL;
519 bool bret = false, flipped_bit = false;
520
521 lastslash = strrchr(path, '/');
522 if (!lastslash) {
523 ERROR("Failed to detect \"/\" in \"%s\"", path);
524 return bret;
525 }
526 oldv = *lastslash;
527 *lastslash = '\0';
528 fpath = must_make_path(path, "cpuset.cpus", NULL);
529 posscpus = read_file(fpath);
530 if (!posscpus) {
531 SYSERROR("Failed to read file \"%s\"", fpath);
532 goto on_error;
533 }
534
535 /* Get maximum number of cpus found in possible cpuset. */
536 maxposs = get_max_cpus(posscpus);
537 if (maxposs < 0)
538 goto on_error;
539
540 if (!file_exists(__ISOL_CPUS)) {
541 /* This system doesn't expose isolated cpus. */
542 DEBUG("The path \""__ISOL_CPUS"\" to read isolated cpus from does not exist");
543 cpulist = posscpus;
544 /* No isolated cpus but we weren't already initialized by
545 * someone. We should simply copy the parents cpuset.cpus
546 * values.
547 */
548 if (!am_initialized) {
549 DEBUG("Copying cpu settings of parent cgroup");
550 goto copy_parent;
551 }
552 /* No isolated cpus but we were already initialized by someone.
553 * Nothing more to do for us.
554 */
555 goto on_success;
556 }
557
558 isolcpus = read_file(__ISOL_CPUS);
559 if (!isolcpus) {
560 SYSERROR("Failed to read file \""__ISOL_CPUS"\"");
561 goto on_error;
562 }
563 if (!isdigit(isolcpus[0])) {
564 TRACE("No isolated cpus detected");
565 cpulist = posscpus;
566 /* No isolated cpus but we weren't already initialized by
567 * someone. We should simply copy the parents cpuset.cpus
568 * values.
569 */
570 if (!am_initialized) {
571 DEBUG("Copying cpu settings of parent cgroup");
572 goto copy_parent;
573 }
574 /* No isolated cpus but we were already initialized by someone.
575 * Nothing more to do for us.
576 */
577 goto on_success;
578 }
579
580 /* Get maximum number of cpus found in isolated cpuset. */
581 maxisol = get_max_cpus(isolcpus);
582 if (maxisol < 0)
583 goto on_error;
584
585 if (maxposs < maxisol)
586 maxposs = maxisol;
587 maxposs++;
588
589 possmask = lxc_cpumask(posscpus, maxposs);
590 if (!possmask) {
591 ERROR("Failed to create cpumask for possible cpus");
592 goto on_error;
593 }
594
595 isolmask = lxc_cpumask(isolcpus, maxposs);
596 if (!isolmask) {
597 ERROR("Failed to create cpumask for isolated cpus");
598 goto on_error;
599 }
600
601 for (i = 0; i <= maxposs; i++) {
602 if (!is_set(i, isolmask) || !is_set(i, possmask))
603 continue;
604
605 flipped_bit = true;
606 clear_bit(i, possmask);
607 }
608
609 if (!flipped_bit) {
610 DEBUG("No isolated cpus present in cpuset");
611 goto on_success;
612 }
613 DEBUG("Removed isolated cpus from cpuset");
614
615 cpulist = lxc_cpumask_to_cpulist(possmask, maxposs);
616 if (!cpulist) {
617 ERROR("Failed to create cpu list");
618 goto on_error;
619 }
620
621 copy_parent:
622 *lastslash = oldv;
623 free(fpath);
624 fpath = must_make_path(path, "cpuset.cpus", NULL);
625 ret = lxc_write_to_file(fpath, cpulist, strlen(cpulist), false);
626 if (ret < 0) {
627 SYSERROR("Failed to write cpu list to \"%s\"", fpath);
628 goto on_error;
629 }
630
631 on_success:
632 bret = true;
633
634 on_error:
635 free(fpath);
636
637 free(isolcpus);
638 free(isolmask);
639
640 if (posscpus != cpulist)
641 free(posscpus);
642 free(possmask);
643
644 free(cpulist);
645 return bret;
646 }
647
648 /* Copy contents of parent(@path)/@file to @path/@file */
649 static bool copy_parent_file(char *path, char *file)
650 {
651 int ret;
652 char *fpath, *lastslash, oldv;
653 int len = 0;
654 char *value = NULL;
655
656 lastslash = strrchr(path, '/');
657 if (!lastslash) {
658 ERROR("Failed to detect \"/\" in \"%s\"", path);
659 return false;
660 }
661 oldv = *lastslash;
662 *lastslash = '\0';
663 fpath = must_make_path(path, file, NULL);
664 len = lxc_read_from_file(fpath, NULL, 0);
665 if (len <= 0)
666 goto on_error;
667
668 value = must_alloc(len + 1);
669 ret = lxc_read_from_file(fpath, value, len);
670 if (ret != len)
671 goto on_error;
672 free(fpath);
673
674 *lastslash = oldv;
675 fpath = must_make_path(path, file, NULL);
676 ret = lxc_write_to_file(fpath, value, len, false);
677 if (ret < 0)
678 SYSERROR("Failed to write \"%s\" to file \"%s\"", value, fpath);
679 free(fpath);
680 free(value);
681 return ret >= 0;
682
683 on_error:
684 SYSERROR("Failed to read file \"%s\"", fpath);
685 free(fpath);
686 free(value);
687 return false;
688 }
689
690 /* Initialize the cpuset hierarchy in first directory of @gname and set
691 * cgroup.clone_children so that children inherit settings. Since the
692 * h->base_path is populated by init or ourselves, we know it is already
693 * initialized.
694 */
695 static bool cg_legacy_handle_cpuset_hierarchy(struct hierarchy *h, char *cgname)
696 {
697 int ret;
698 char v;
699 char *cgpath, *clonechildrenpath, *slash;
700
701 if (!string_in_list(h->controllers, "cpuset"))
702 return true;
703
704 if (*cgname == '/')
705 cgname++;
706 slash = strchr(cgname, '/');
707 if (slash)
708 *slash = '\0';
709
710 cgpath = must_make_path(h->mountpoint, h->base_cgroup, cgname, NULL);
711 if (slash)
712 *slash = '/';
713
714 ret = mkdir(cgpath, 0755);
715 if (ret < 0) {
716 if (errno != EEXIST) {
717 SYSERROR("Failed to create directory \"%s\"", cgpath);
718 free(cgpath);
719 return false;
720 }
721 }
722
723 clonechildrenpath =
724 must_make_path(cgpath, "cgroup.clone_children", NULL);
725 /* unified hierarchy doesn't have clone_children */
726 if (!file_exists(clonechildrenpath)) {
727 free(clonechildrenpath);
728 free(cgpath);
729 return true;
730 }
731
732 ret = lxc_read_from_file(clonechildrenpath, &v, 1);
733 if (ret < 0) {
734 SYSERROR("Failed to read file \"%s\"", clonechildrenpath);
735 free(clonechildrenpath);
736 free(cgpath);
737 return false;
738 }
739
740 /* Make sure any isolated cpus are removed from cpuset.cpus. */
741 if (!cg_legacy_filter_and_set_cpus(cgpath, v == '1')) {
742 SYSERROR("Failed to remove isolated cpus");
743 free(clonechildrenpath);
744 free(cgpath);
745 return false;
746 }
747
748 /* Already set for us by someone else. */
749 if (v == '1') {
750 DEBUG("\"cgroup.clone_children\" was already set to \"1\"");
751 free(clonechildrenpath);
752 free(cgpath);
753 return true;
754 }
755
756 /* copy parent's settings */
757 if (!copy_parent_file(cgpath, "cpuset.mems")) {
758 SYSERROR("Failed to copy \"cpuset.mems\" settings");
759 free(cgpath);
760 free(clonechildrenpath);
761 return false;
762 }
763 free(cgpath);
764
765 ret = lxc_write_to_file(clonechildrenpath, "1", 1, false);
766 if (ret < 0) {
767 /* Set clone_children so children inherit our settings */
768 SYSERROR("Failed to write 1 to \"%s\"", clonechildrenpath);
769 free(clonechildrenpath);
770 return false;
771 }
772 free(clonechildrenpath);
773 return true;
774 }
775
776 /* Given two null-terminated lists of strings, return true if any string is in
777 * both.
778 */
779 static bool controller_lists_intersect(char **l1, char **l2)
780 {
781 int i;
782
783 if (!l1 || !l2)
784 return false;
785
786 for (i = 0; l1[i]; i++) {
787 if (string_in_list(l2, l1[i]))
788 return true;
789 }
790
791 return false;
792 }
793
794 /* For a null-terminated list of controllers @clist, return true if any of those
795 * controllers is already listed the null-terminated list of hierarchies @hlist.
796 * Realistically, if one is present, all must be present.
797 */
798 static bool controller_list_is_dup(struct hierarchy **hlist, char **clist)
799 {
800 int i;
801
802 if (!hlist)
803 return false;
804
805 for (i = 0; hlist[i]; i++)
806 if (controller_lists_intersect(hlist[i]->controllers, clist))
807 return true;
808
809 return false;
810 }
811
812 /* Return true if the controller @entry is found in the null-terminated list of
813 * hierarchies @hlist.
814 */
815 static bool controller_found(struct hierarchy **hlist, char *entry)
816 {
817 int i;
818
819 if (!hlist)
820 return false;
821
822 for (i = 0; hlist[i]; i++)
823 if (string_in_list(hlist[i]->controllers, entry))
824 return true;
825
826 return false;
827 }
828
829 /* Return true if all of the controllers which we require have been found. The
830 * required list is freezer and anything in lxc.cgroup.use.
831 */
832 static bool all_controllers_found(void)
833 {
834 char *p;
835 char *saveptr = NULL;
836 struct hierarchy **hlist = hierarchies;
837
838 if (!controller_found(hlist, "freezer")) {
839 CGFSNG_DEBUG("No freezer controller mountpoint found\n");
840 return false;
841 }
842
843 if (!cgroup_use)
844 return true;
845
846 for (p = strtok_r(cgroup_use, ",", &saveptr); p;
847 p = strtok_r(NULL, ",", &saveptr)) {
848 if (!controller_found(hlist, p)) {
849 CGFSNG_DEBUG("No %s controller mountpoint found\n", p);
850 return false;
851 }
852 }
853
854 return true;
855 }
856
857 /* Get the controllers from a mountinfo line There are other ways we could get
858 * this info. For lxcfs, field 3 is /cgroup/controller-list. For cgroupfs, we
859 * could parse the mount options. But we simply assume that the mountpoint must
860 * be /sys/fs/cgroup/controller-list
861 */
862 static char **cg_hybrid_get_controllers(char **klist, char **nlist, char *line,
863 int type)
864 {
865 /* The fourth field is /sys/fs/cgroup/comma-delimited-controller-list
866 * for legacy hierarchies.
867 */
868 int i;
869 char *dup, *p2, *tok;
870 char *p = line, *saveptr = NULL, *sep = ",";
871 char **aret = NULL;
872
873 for (i = 0; i < 4; i++) {
874 p = strchr(p, ' ');
875 if (!p)
876 return NULL;
877 p++;
878 }
879
880 /* Note, if we change how mountinfo works, then our caller will need to
881 * verify /sys/fs/cgroup/ in this field.
882 */
883 if (strncmp(p, "/sys/fs/cgroup/", 15) != 0) {
884 CGFSNG_DEBUG("Found hierarchy not under /sys/fs/cgroup: \"%s\"\n", p);
885 return NULL;
886 }
887
888 p += 15;
889 p2 = strchr(p, ' ');
890 if (!p2) {
891 CGFSNG_DEBUG("Corrupt mountinfo\n");
892 return NULL;
893 }
894 *p2 = '\0';
895
896 if (type == CGROUP_SUPER_MAGIC) {
897 /* strdup() here for v1 hierarchies. Otherwise strtok_r() will
898 * destroy mountpoints such as "/sys/fs/cgroup/cpu,cpuacct".
899 */
900 dup = strdup(p);
901 if (!dup)
902 return NULL;
903
904 for (tok = strtok_r(dup, sep, &saveptr); tok;
905 tok = strtok_r(NULL, sep, &saveptr))
906 must_append_controller(klist, nlist, &aret, tok);
907
908 free(dup);
909 }
910 *p2 = ' ';
911
912 return aret;
913 }
914
915 static char **cg_unified_make_empty_controller(void)
916 {
917 int newentry;
918 char **aret = NULL;
919
920 newentry = append_null_to_list((void ***)&aret);
921 aret[newentry] = NULL;
922 return aret;
923 }
924
925 static char **cg_unified_get_controllers(const char *file)
926 {
927 char *buf, *tok;
928 char *saveptr = NULL, *sep = " \t\n";
929 char **aret = NULL;
930
931 buf = read_file(file);
932 if (!buf)
933 return NULL;
934
935 for (tok = strtok_r(buf, sep, &saveptr); tok;
936 tok = strtok_r(NULL, sep, &saveptr)) {
937 int newentry;
938 char *copy;
939
940 newentry = append_null_to_list((void ***)&aret);
941 copy = must_copy_string(tok);
942 aret[newentry] = copy;
943 }
944
945 free(buf);
946 return aret;
947 }
948
949 static struct hierarchy *add_hierarchy(char **clist, char *mountpoint,
950 char *base_cgroup, int type)
951 {
952 struct hierarchy *new;
953 int newentry;
954
955 new = must_alloc(sizeof(*new));
956 new->controllers = clist;
957 new->mountpoint = mountpoint;
958 new->base_cgroup = base_cgroup;
959 new->fullcgpath = NULL;
960 new->version = type;
961
962 newentry = append_null_to_list((void ***)&hierarchies);
963 hierarchies[newentry] = new;
964 return new;
965 }
966
967 /* Get a copy of the mountpoint from @line, which is a line from
968 * /proc/self/mountinfo.
969 */
970 static char *cg_hybrid_get_mountpoint(char *line)
971 {
972 int i;
973 size_t len;
974 char *p2;
975 char *p = line, *sret = NULL;
976
977 for (i = 0; i < 4; i++) {
978 p = strchr(p, ' ');
979 if (!p)
980 return NULL;
981 p++;
982 }
983
984 if (strncmp(p, "/sys/fs/cgroup/", 15) != 0)
985 return NULL;
986
987 p2 = strchr(p + 15, ' ');
988 if (!p2)
989 return NULL;
990 *p2 = '\0';
991
992 len = strlen(p);
993 sret = must_alloc(len + 1);
994 memcpy(sret, p, len);
995 sret[len] = '\0';
996 return sret;
997 }
998
999 /* Given a multi-line string, return a null-terminated copy of the current line. */
1000 static char *copy_to_eol(char *p)
1001 {
1002 char *p2 = strchr(p, '\n'), *sret;
1003 size_t len;
1004
1005 if (!p2)
1006 return NULL;
1007
1008 len = p2 - p;
1009 sret = must_alloc(len + 1);
1010 memcpy(sret, p, len);
1011 sret[len] = '\0';
1012 return sret;
1013 }
1014
1015 /* cgline: pointer to character after the first ':' in a line in a \n-terminated
1016 * /proc/self/cgroup file. Check whether controller c is present.
1017 */
1018 static bool controller_in_clist(char *cgline, char *c)
1019 {
1020 char *tok, *saveptr = NULL, *eol, *tmp;
1021 size_t len;
1022
1023 eol = strchr(cgline, ':');
1024 if (!eol)
1025 return false;
1026
1027 len = eol - cgline;
1028 tmp = alloca(len + 1);
1029 memcpy(tmp, cgline, len);
1030 tmp[len] = '\0';
1031
1032 for (tok = strtok_r(tmp, ",", &saveptr); tok;
1033 tok = strtok_r(NULL, ",", &saveptr)) {
1034 if (strcmp(tok, c) == 0)
1035 return true;
1036 }
1037
1038 return false;
1039 }
1040
1041 /* @basecginfo is a copy of /proc/$$/cgroup. Return the current cgroup for
1042 * @controller.
1043 */
1044 static char *cg_hybrid_get_current_cgroup(char *basecginfo, char *controller,
1045 int type)
1046 {
1047 char *p = basecginfo;
1048
1049 for (;;) {
1050 bool is_cgv2_base_cgroup = false;
1051
1052 /* cgroup v2 entry in "/proc/<pid>/cgroup": "0::/some/path" */
1053 if ((type == CGROUP2_SUPER_MAGIC) && (*p == '0'))
1054 is_cgv2_base_cgroup = true;
1055
1056 p = strchr(p, ':');
1057 if (!p)
1058 return NULL;
1059 p++;
1060
1061 if (is_cgv2_base_cgroup || (controller && controller_in_clist(p, controller))) {
1062 p = strchr(p, ':');
1063 if (!p)
1064 return NULL;
1065 p++;
1066 return copy_to_eol(p);
1067 }
1068
1069 p = strchr(p, '\n');
1070 if (!p)
1071 return NULL;
1072 p++;
1073 }
1074 }
1075
1076 static void must_append_string(char ***list, char *entry)
1077 {
1078 int newentry;
1079 char *copy;
1080
1081 newentry = append_null_to_list((void ***)list);
1082 copy = must_copy_string(entry);
1083 (*list)[newentry] = copy;
1084 }
1085
1086 static int get_existing_subsystems(char ***klist, char ***nlist)
1087 {
1088 FILE *f;
1089 char *line = NULL;
1090 size_t len = 0;
1091
1092 f = fopen("/proc/self/cgroup", "r");
1093 if (!f)
1094 return -1;
1095
1096 while (getline(&line, &len, f) != -1) {
1097 char *p, *p2, *tok, *saveptr = NULL;
1098 p = strchr(line, ':');
1099 if (!p)
1100 continue;
1101 p++;
1102 p2 = strchr(p, ':');
1103 if (!p2)
1104 continue;
1105 *p2 = '\0';
1106
1107 /* If the kernel has cgroup v2 support, then /proc/self/cgroup
1108 * contains an entry of the form:
1109 *
1110 * 0::/some/path
1111 *
1112 * In this case we use "cgroup2" as controller name.
1113 */
1114 if ((p2 - p) == 0) {
1115 must_append_string(klist, "cgroup2");
1116 continue;
1117 }
1118
1119 for (tok = strtok_r(p, ",", &saveptr); tok;
1120 tok = strtok_r(NULL, ",", &saveptr)) {
1121 if (strncmp(tok, "name=", 5) == 0)
1122 must_append_string(nlist, tok);
1123 else
1124 must_append_string(klist, tok);
1125 }
1126 }
1127
1128 free(line);
1129 fclose(f);
1130 return 0;
1131 }
1132
1133 static void trim(char *s)
1134 {
1135 size_t len;
1136
1137 len = strlen(s);
1138 while ((len > 1) && (s[len - 1] == '\n'))
1139 s[--len] = '\0';
1140 }
1141
1142 static void lxc_cgfsng_print_handler_data(const struct cgfsng_handler_data *d)
1143 {
1144 printf("Cgroup information:\n");
1145 printf(" container name: %s\n", d->name ? d->name : "(null)");
1146 printf(" lxc.cgroup.use: %s\n", cgroup_use ? cgroup_use : "(null)");
1147 printf(" lxc.cgroup.pattern: %s\n",
1148 d->cgroup_pattern ? d->cgroup_pattern : "(null)");
1149 printf(" lxc.cgroup.dir: %s\n",
1150 d->cgroup_meta.dir ? d->cgroup_meta.dir : "(null)");
1151 printf(" cgroup: %s\n",
1152 d->container_cgroup ? d->container_cgroup : "(null)");
1153 }
1154
1155 static void lxc_cgfsng_print_hierarchies()
1156 {
1157 int i;
1158 struct hierarchy **it;
1159
1160 if (!hierarchies) {
1161 printf(" No hierarchies found\n");
1162 return;
1163 }
1164
1165 printf(" Hierarchies:\n");
1166 for (i = 0, it = hierarchies; it && *it; it++, i++) {
1167 int j;
1168 char **cit;
1169
1170 printf(" %d: base_cgroup: %s\n", i, (*it)->base_cgroup ? (*it)->base_cgroup : "(null)");
1171 printf(" mountpoint: %s\n", (*it)->mountpoint ? (*it)->mountpoint : "(null)");
1172 printf(" controllers:\n");
1173 for (j = 0, cit = (*it)->controllers; cit && *cit; cit++, j++)
1174 printf(" %d: %s\n", j, *cit);
1175 }
1176 }
1177
1178 static void lxc_cgfsng_print_basecg_debuginfo(char *basecginfo, char **klist,
1179 char **nlist)
1180 {
1181 int k;
1182 char **it;
1183
1184 printf("basecginfo is:\n");
1185 printf("%s\n", basecginfo);
1186
1187 for (k = 0, it = klist; it && *it; it++, k++)
1188 printf("kernel subsystem %d: %s\n", k, *it);
1189
1190 for (k = 0, it = nlist; it && *it; it++, k++)
1191 printf("named subsystem %d: %s\n", k, *it);
1192 }
1193
1194 static void lxc_cgfsng_print_debuginfo(const struct cgfsng_handler_data *d)
1195 {
1196 lxc_cgfsng_print_handler_data(d);
1197 lxc_cgfsng_print_hierarchies();
1198 }
1199
1200 /* At startup, parse_hierarchies finds all the info we need about cgroup
1201 * mountpoints and current cgroups, and stores it in @d.
1202 */
1203 static bool cg_hybrid_init(void)
1204 {
1205 int ret;
1206 char *basecginfo;
1207 bool will_escape;
1208 FILE *f;
1209 size_t len = 0;
1210 char *line = NULL;
1211 char **klist = NULL, **nlist = NULL;
1212
1213 /* Root spawned containers escape the current cgroup, so use init's
1214 * cgroups as our base in that case.
1215 */
1216 will_escape = (geteuid() == 0);
1217 if (will_escape)
1218 basecginfo = read_file("/proc/1/cgroup");
1219 else
1220 basecginfo = read_file("/proc/self/cgroup");
1221 if (!basecginfo)
1222 return false;
1223
1224 ret = get_existing_subsystems(&klist, &nlist);
1225 if (ret < 0) {
1226 CGFSNG_DEBUG("Failed to retrieve available legacy cgroup controllers\n");
1227 free(basecginfo);
1228 return false;
1229 }
1230
1231 f = fopen("/proc/self/mountinfo", "r");
1232 if (!f) {
1233 CGFSNG_DEBUG("Failed to open \"/proc/self/mountinfo\"\n");
1234 free(basecginfo);
1235 return false;
1236 }
1237
1238 if (lxc_cgfsng_debug)
1239 lxc_cgfsng_print_basecg_debuginfo(basecginfo, klist, nlist);
1240
1241 while (getline(&line, &len, f) != -1) {
1242 int type;
1243 bool writeable;
1244 struct hierarchy *new;
1245 char *base_cgroup = NULL, *mountpoint = NULL;
1246 char **controller_list = NULL;
1247
1248 type = get_cgroup_version(line);
1249 if (type == 0)
1250 continue;
1251
1252 if (type == CGROUP2_SUPER_MAGIC && unified)
1253 continue;
1254
1255 if (cgroup_layout == CGROUP_LAYOUT_UNKNOWN) {
1256 if (type == CGROUP2_SUPER_MAGIC)
1257 cgroup_layout = CGROUP_LAYOUT_UNIFIED;
1258 else if (type == CGROUP_SUPER_MAGIC)
1259 cgroup_layout = CGROUP_LAYOUT_LEGACY;
1260 } else if (cgroup_layout == CGROUP_LAYOUT_UNIFIED) {
1261 if (type == CGROUP_SUPER_MAGIC)
1262 cgroup_layout = CGROUP_LAYOUT_HYBRID;
1263 } else if (cgroup_layout == CGROUP_LAYOUT_LEGACY) {
1264 if (type == CGROUP2_SUPER_MAGIC)
1265 cgroup_layout = CGROUP_LAYOUT_HYBRID;
1266 }
1267
1268 controller_list = cg_hybrid_get_controllers(klist, nlist, line, type);
1269 if (!controller_list && type == CGROUP_SUPER_MAGIC)
1270 continue;
1271
1272 if (type == CGROUP_SUPER_MAGIC)
1273 if (controller_list_is_dup(hierarchies, controller_list))
1274 goto next;
1275
1276 mountpoint = cg_hybrid_get_mountpoint(line);
1277 if (!mountpoint) {
1278 CGFSNG_DEBUG("Failed parsing mountpoint from \"%s\"\n", line);
1279 goto next;
1280 }
1281
1282 if (type == CGROUP_SUPER_MAGIC)
1283 base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, controller_list[0], CGROUP_SUPER_MAGIC);
1284 else
1285 base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, NULL, CGROUP2_SUPER_MAGIC);
1286 if (!base_cgroup) {
1287 CGFSNG_DEBUG("Failed to find current cgroup\n");
1288 goto next;
1289 }
1290
1291 trim(base_cgroup);
1292 prune_init_scope(base_cgroup);
1293 if (type == CGROUP2_SUPER_MAGIC)
1294 writeable = test_writeable_v2(mountpoint, base_cgroup);
1295 else
1296 writeable = test_writeable_v1(mountpoint, base_cgroup);
1297 if (!writeable)
1298 goto next;
1299
1300 if (type == CGROUP2_SUPER_MAGIC) {
1301 char *cgv2_ctrl_path;
1302
1303 cgv2_ctrl_path = must_make_path(mountpoint, base_cgroup,
1304 "cgroup.controllers",
1305 NULL);
1306
1307 controller_list = cg_unified_get_controllers(cgv2_ctrl_path);
1308 free(cgv2_ctrl_path);
1309 if (!controller_list)
1310 controller_list = cg_unified_make_empty_controller();
1311 }
1312
1313 new = add_hierarchy(controller_list, mountpoint, base_cgroup, type);
1314 if (type == CGROUP2_SUPER_MAGIC && !unified)
1315 unified = new;
1316
1317 continue;
1318
1319 next:
1320 free_string_list(controller_list);
1321 free(mountpoint);
1322 free(base_cgroup);
1323 }
1324
1325 free_string_list(klist);
1326 free_string_list(nlist);
1327
1328 free(basecginfo);
1329
1330 fclose(f);
1331 free(line);
1332
1333 if (lxc_cgfsng_debug) {
1334 printf("Writable cgroup hierarchies:\n");
1335 lxc_cgfsng_print_hierarchies();
1336 }
1337
1338 /* verify that all controllers in cgroup.use and all crucial
1339 * controllers are accounted for
1340 */
1341 if (!all_controllers_found())
1342 return false;
1343
1344 return true;
1345 }
1346
1347 static int cg_is_pure_unified(void)
1348 {
1349
1350 int ret;
1351 struct statfs fs;
1352
1353 ret = statfs("/sys/fs/cgroup", &fs);
1354 if (ret < 0)
1355 return -ENOMEDIUM;
1356
1357 if (is_fs_type(&fs, CGROUP2_SUPER_MAGIC))
1358 return CGROUP2_SUPER_MAGIC;
1359
1360 return 0;
1361 }
1362
1363 /* Get current cgroup from /proc/self/cgroup for the cgroupfs v2 hierarchy. */
1364 static char *cg_unified_get_current_cgroup(void)
1365 {
1366 char *basecginfo, *base_cgroup;
1367 bool will_escape;
1368 char *copy = NULL;
1369
1370 will_escape = (geteuid() == 0);
1371 if (will_escape)
1372 basecginfo = read_file("/proc/1/cgroup");
1373 else
1374 basecginfo = read_file("/proc/self/cgroup");
1375 if (!basecginfo)
1376 return NULL;
1377
1378 base_cgroup = strstr(basecginfo, "0::/");
1379 if (!base_cgroup)
1380 goto cleanup_on_err;
1381
1382 base_cgroup = base_cgroup + 3;
1383 copy = copy_to_eol(base_cgroup);
1384 if (!copy)
1385 goto cleanup_on_err;
1386
1387 cleanup_on_err:
1388 free(basecginfo);
1389 if (copy)
1390 trim(copy);
1391
1392 return copy;
1393 }
1394
1395 static int cg_unified_init(void)
1396 {
1397 int ret;
1398 char *mountpoint, *subtree_path;
1399 char **delegatable;
1400 char *base_cgroup = NULL;
1401
1402 ret = cg_is_pure_unified();
1403 if (ret == -ENOMEDIUM)
1404 return -ENOMEDIUM;
1405
1406 if (ret != CGROUP2_SUPER_MAGIC)
1407 return 0;
1408
1409 base_cgroup = cg_unified_get_current_cgroup();
1410 if (!base_cgroup)
1411 return -EINVAL;
1412 prune_init_scope(base_cgroup);
1413
1414 /* We assume that we have already been given controllers to delegate
1415 * further down the hierarchy. If not it is up to the user to delegate
1416 * them to us.
1417 */
1418 mountpoint = must_copy_string("/sys/fs/cgroup");
1419 subtree_path = must_make_path(mountpoint, base_cgroup,
1420 "cgroup.subtree_control", NULL);
1421 delegatable = cg_unified_get_controllers(subtree_path);
1422 free(subtree_path);
1423 if (!delegatable)
1424 delegatable = cg_unified_make_empty_controller();
1425 if (!delegatable[0])
1426 CGFSNG_DEBUG("No controllers are enabled for delegation\n");
1427
1428 /* TODO: If the user requested specific controllers via lxc.cgroup.use
1429 * we should verify here. The reason I'm not doing it right is that I'm
1430 * not convinced that lxc.cgroup.use will be the future since it is a
1431 * global property. I much rather have an option that lets you request
1432 * controllers per container.
1433 */
1434
1435 add_hierarchy(delegatable, mountpoint, base_cgroup, CGROUP2_SUPER_MAGIC);
1436 unified = hierarchies[0];
1437
1438 cgroup_layout = CGROUP_LAYOUT_UNIFIED;
1439 return CGROUP2_SUPER_MAGIC;
1440 }
1441
1442 static bool cg_init(void)
1443 {
1444 int ret;
1445 const char *tmp;
1446
1447 errno = 0;
1448 tmp = lxc_global_config_value("lxc.cgroup.use");
1449 if (!cgroup_use && errno != 0) { /* lxc.cgroup.use can be NULL */
1450 CGFSNG_DEBUG("Failed to retrieve list of cgroups to use\n");
1451 return false;
1452 }
1453 cgroup_use = must_copy_string(tmp);
1454
1455 ret = cg_unified_init();
1456 if (ret < 0)
1457 return false;
1458
1459 if (ret == CGROUP2_SUPER_MAGIC)
1460 return true;
1461
1462 return cg_hybrid_init();
1463 }
1464
1465 static void *cgfsng_init(struct lxc_handler *handler)
1466 {
1467 const char *cgroup_pattern;
1468 struct cgfsng_handler_data *d;
1469
1470 d = must_alloc(sizeof(*d));
1471 memset(d, 0, sizeof(*d));
1472
1473 /* copy container name */
1474 d->name = must_copy_string(handler->name);
1475
1476 /* copy per-container cgroup information */
1477 d->cgroup_meta.dir = NULL;
1478 d->cgroup_meta.controllers = NULL;
1479 if (handler->conf) {
1480 d->cgroup_meta.dir = must_copy_string(handler->conf->cgroup_meta.dir);
1481 d->cgroup_meta.controllers = must_copy_string(handler->conf->cgroup_meta.controllers);
1482 }
1483
1484 /* copy system-wide cgroup information */
1485 cgroup_pattern = lxc_global_config_value("lxc.cgroup.pattern");
1486 if (!cgroup_pattern) {
1487 /* lxc.cgroup.pattern is only NULL on error. */
1488 ERROR("Failed to retrieve cgroup pattern");
1489 goto out_free;
1490 }
1491 d->cgroup_pattern = must_copy_string(cgroup_pattern);
1492
1493 d->cgroup_layout = cgroup_layout;
1494 if (d->cgroup_layout == CGROUP_LAYOUT_LEGACY)
1495 TRACE("Running with legacy cgroup layout");
1496 else if (d->cgroup_layout == CGROUP_LAYOUT_HYBRID)
1497 TRACE("Running with hybrid cgroup layout");
1498 else if (d->cgroup_layout == CGROUP_LAYOUT_UNIFIED)
1499 TRACE("Running with unified cgroup layout");
1500 else
1501 WARN("Running with unknown cgroup layout");
1502
1503 if (lxc_cgfsng_debug)
1504 lxc_cgfsng_print_debuginfo(d);
1505
1506 return d;
1507
1508 out_free:
1509 free_handler_data(d);
1510 return NULL;
1511 }
1512
1513 static int recursive_destroy(char *dirname)
1514 {
1515 int ret;
1516 struct dirent *direntp;
1517 DIR *dir;
1518 int r = 0;
1519
1520 dir = opendir(dirname);
1521 if (!dir)
1522 return -1;
1523
1524 while ((direntp = readdir(dir))) {
1525 char *pathname;
1526 struct stat mystat;
1527
1528 if (!strcmp(direntp->d_name, ".") ||
1529 !strcmp(direntp->d_name, ".."))
1530 continue;
1531
1532 pathname = must_make_path(dirname, direntp->d_name, NULL);
1533
1534 ret = lstat(pathname, &mystat);
1535 if (ret < 0) {
1536 if (!r)
1537 WARN("Failed to stat \"%s\"", pathname);
1538 r = -1;
1539 goto next;
1540 }
1541
1542 if (!S_ISDIR(mystat.st_mode))
1543 goto next;
1544
1545 ret = recursive_destroy(pathname);
1546 if (ret < 0)
1547 r = -1;
1548 next:
1549 free(pathname);
1550 }
1551
1552 ret = rmdir(dirname);
1553 if (ret < 0) {
1554 if (!r)
1555 WARN("%s - Failed to delete \"%s\"", strerror(errno), dirname);
1556 r = -1;
1557 }
1558
1559 ret = closedir(dir);
1560 if (ret < 0) {
1561 if (!r)
1562 WARN("%s - Failed to delete \"%s\"", strerror(errno), dirname);
1563 r = -1;
1564 }
1565
1566 return r;
1567 }
1568
1569 static int cgroup_rmdir(char *container_cgroup)
1570 {
1571 int i;
1572
1573 if (!container_cgroup || !hierarchies)
1574 return 0;
1575
1576 for (i = 0; hierarchies[i]; i++) {
1577 int ret;
1578 struct hierarchy *h = hierarchies[i];
1579
1580 if (!h->fullcgpath)
1581 continue;
1582
1583 ret = recursive_destroy(h->fullcgpath);
1584 if (ret < 0)
1585 WARN("Failed to destroy \"%s\"", h->fullcgpath);
1586
1587 free(h->fullcgpath);
1588 h->fullcgpath = NULL;
1589 }
1590
1591 return 0;
1592 }
1593
1594 struct generic_userns_exec_data {
1595 struct cgfsng_handler_data *d;
1596 struct lxc_conf *conf;
1597 uid_t origuid; /* target uid in parent namespace */
1598 char *path;
1599 };
1600
1601 static int cgroup_rmdir_wrapper(void *data)
1602 {
1603 int ret;
1604 struct generic_userns_exec_data *arg = data;
1605 uid_t nsuid = (arg->conf->root_nsuid_map != NULL) ? 0 : arg->conf->init_uid;
1606 gid_t nsgid = (arg->conf->root_nsgid_map != NULL) ? 0 : arg->conf->init_gid;
1607
1608 ret = setresgid(nsgid, nsgid, nsgid);
1609 if (ret < 0) {
1610 SYSERROR("Failed to setresgid(%d, %d, %d)", (int)nsgid,
1611 (int)nsgid, (int)nsgid);
1612 return -1;
1613 }
1614
1615 ret = setresuid(nsuid, nsuid, nsuid);
1616 if (ret < 0) {
1617 SYSERROR("Failed to setresuid(%d, %d, %d)", (int)nsuid,
1618 (int)nsuid, (int)nsuid);
1619 return -1;
1620 }
1621
1622 ret = setgroups(0, NULL);
1623 if (ret < 0 && errno != EPERM) {
1624 SYSERROR("Failed to setgroups(0, NULL)");
1625 return -1;
1626 }
1627
1628 return cgroup_rmdir(arg->d->container_cgroup);
1629 }
1630
1631 static void cgfsng_destroy(void *hdata, struct lxc_conf *conf)
1632 {
1633 int ret;
1634 struct cgfsng_handler_data *d = hdata;
1635 struct generic_userns_exec_data wrap;
1636
1637 if (!d)
1638 return;
1639
1640 wrap.origuid = 0;
1641 wrap.d = hdata;
1642 wrap.conf = conf;
1643
1644 if (conf && !lxc_list_empty(&conf->id_map))
1645 ret = userns_exec_1(conf, cgroup_rmdir_wrapper, &wrap,
1646 "cgroup_rmdir_wrapper");
1647 else
1648 ret = cgroup_rmdir(d->container_cgroup);
1649 if (ret < 0) {
1650 WARN("Failed to destroy cgroups");
1651 return;
1652 }
1653
1654 free_handler_data(d);
1655 }
1656
1657 struct cgroup_ops *cgfsng_ops_init(void)
1658 {
1659 if (getenv("LXC_DEBUG_CGFSNG"))
1660 lxc_cgfsng_debug = true;
1661
1662 if (!cg_init())
1663 return NULL;
1664
1665 return &cgfsng_ops;
1666 }
1667
1668 static bool cg_unified_create_cgroup(struct hierarchy *h, char *cgname)
1669 {
1670 size_t i, parts_len;
1671 char **it;
1672 size_t full_len = 0;
1673 char *add_controllers = NULL, *cgroup = NULL;
1674 char **parts = NULL;
1675 bool bret = false;
1676
1677 if (h->version != CGROUP2_SUPER_MAGIC)
1678 return true;
1679
1680 if (!h->controllers)
1681 return true;
1682
1683 /* For now we simply enable all controllers that we have detected by
1684 * creating a string like "+memory +pids +cpu +io".
1685 * TODO: In the near future we might want to support "-<controller>"
1686 * etc. but whether supporting semantics like this make sense will need
1687 * some thinking.
1688 */
1689 for (it = h->controllers; it && *it; it++) {
1690 full_len += strlen(*it) + 2;
1691 add_controllers = must_realloc(add_controllers, full_len + 1);
1692 if (h->controllers[0] == *it)
1693 add_controllers[0] = '\0';
1694 strcat(add_controllers, "+");
1695 strcat(add_controllers, *it);
1696 if ((it + 1) && *(it + 1))
1697 strcat(add_controllers, " ");
1698 }
1699
1700 parts = lxc_string_split(cgname, '/');
1701 if (!parts)
1702 goto on_error;
1703 parts_len = lxc_array_len((void **)parts);
1704 if (parts_len > 0)
1705 parts_len--;
1706
1707 cgroup = must_make_path(h->mountpoint, h->base_cgroup, NULL);
1708 for (i = 0; i < parts_len; i++) {
1709 int ret;
1710 char *target;
1711
1712 cgroup = must_append_path(cgroup, parts[i], NULL);
1713 target = must_make_path(cgroup, "cgroup.subtree_control", NULL);
1714 ret = lxc_write_to_file(target, add_controllers, full_len, false);
1715 free(target);
1716 if (ret < 0) {
1717 SYSERROR("Could not enable \"%s\" controllers in the "
1718 "unified cgroup \"%s\"", add_controllers, cgroup);
1719 goto on_error;
1720 }
1721 }
1722
1723 bret = true;
1724
1725 on_error:
1726 lxc_free_array((void **)parts, free);
1727 free(add_controllers);
1728 free(cgroup);
1729 return bret;
1730 }
1731
1732 static bool create_path_for_hierarchy(struct hierarchy *h, char *cgname)
1733 {
1734 int ret;
1735
1736 h->fullcgpath = must_make_path(h->mountpoint, h->base_cgroup, cgname, NULL);
1737 if (dir_exists(h->fullcgpath)) {
1738 ERROR("The cgroup \"%s\" already existed", h->fullcgpath);
1739 return false;
1740 }
1741
1742 if (!cg_legacy_handle_cpuset_hierarchy(h, cgname)) {
1743 ERROR("Failed to handle legacy cpuset controller");
1744 return false;
1745 }
1746
1747 ret = mkdir_p(h->fullcgpath, 0755);
1748 if (ret < 0) {
1749 ERROR("Failed to create cgroup \"%s\"", h->fullcgpath);
1750 return false;
1751 }
1752
1753 return cg_unified_create_cgroup(h, cgname);
1754 }
1755
1756 static void remove_path_for_hierarchy(struct hierarchy *h, char *cgname)
1757 {
1758 int ret;
1759
1760 ret = rmdir(h->fullcgpath);
1761 if (ret < 0)
1762 SYSERROR("Failed to rmdir(\"%s\") from failed creation attempt", h->fullcgpath);
1763
1764 free(h->fullcgpath);
1765 h->fullcgpath = NULL;
1766 }
1767
1768 /* Try to create the same cgroup in all hierarchies. Start with cgroup_pattern;
1769 * next cgroup_pattern-1, -2, ..., -999.
1770 */
1771 static inline bool cgfsng_create(void *hdata)
1772 {
1773 int i;
1774 size_t len;
1775 char *container_cgroup, *offset, *tmp;
1776 int idx = 0;
1777 struct cgfsng_handler_data *d = hdata;
1778
1779 if (!d)
1780 return false;
1781
1782 if (d->container_cgroup) {
1783 WARN("cgfsng_create called a second time");
1784 return false;
1785 }
1786
1787 if (d->cgroup_meta.dir)
1788 tmp = lxc_string_join("/", (const char *[]){d->cgroup_meta.dir, d->name, NULL}, false);
1789 else
1790 tmp = lxc_string_replace("%n", d->name, d->cgroup_pattern);
1791 if (!tmp) {
1792 ERROR("Failed expanding cgroup name pattern");
1793 return false;
1794 }
1795 len = strlen(tmp) + 5; /* leave room for -NNN\0 */
1796 container_cgroup = must_alloc(len);
1797 strcpy(container_cgroup, tmp);
1798 free(tmp);
1799 offset = container_cgroup + len - 5;
1800
1801 again:
1802 if (idx == 1000) {
1803 ERROR("Too many conflicting cgroup names");
1804 goto out_free;
1805 }
1806
1807 if (idx) {
1808 int ret;
1809
1810 ret = snprintf(offset, 5, "-%d", idx);
1811 if (ret < 0 || (size_t)ret >= 5) {
1812 FILE *f = fopen("/dev/null", "w");
1813 if (f) {
1814 fprintf(f, "Workaround for GCC7 bug: "
1815 "https://gcc.gnu.org/bugzilla/"
1816 "show_bug.cgi?id=78969");
1817 fclose(f);
1818 }
1819 }
1820 }
1821
1822 for (i = 0; hierarchies[i]; i++) {
1823 if (!create_path_for_hierarchy(hierarchies[i], container_cgroup)) {
1824 int j;
1825 ERROR("Failed to create cgroup \"%s\"", hierarchies[i]->fullcgpath);
1826 free(hierarchies[i]->fullcgpath);
1827 hierarchies[i]->fullcgpath = NULL;
1828 for (j = 0; j < i; j++)
1829 remove_path_for_hierarchy(hierarchies[j], container_cgroup);
1830 idx++;
1831 goto again;
1832 }
1833 }
1834
1835 d->container_cgroup = container_cgroup;
1836
1837 return true;
1838
1839 out_free:
1840 free(container_cgroup);
1841
1842 return false;
1843 }
1844
1845 static bool cgfsng_enter(void *hdata, pid_t pid)
1846 {
1847 int i, len;
1848 char pidstr[25];
1849
1850 len = snprintf(pidstr, 25, "%d", pid);
1851 if (len < 0 || len >= 25)
1852 return false;
1853
1854 for (i = 0; hierarchies[i]; i++) {
1855 int ret;
1856 char *fullpath;
1857
1858 fullpath = must_make_path(hierarchies[i]->fullcgpath,
1859 "cgroup.procs", NULL);
1860 ret = lxc_write_to_file(fullpath, pidstr, len, false);
1861 if (ret != 0) {
1862 SYSERROR("Failed to enter cgroup \"%s\"", fullpath);
1863 free(fullpath);
1864 return false;
1865 }
1866 free(fullpath);
1867 }
1868
1869 return true;
1870 }
1871
1872 static int chowmod(char *path, uid_t chown_uid, gid_t chown_gid,
1873 mode_t chmod_mode)
1874 {
1875 int ret;
1876
1877 ret = chown(path, chown_uid, chown_gid);
1878 if (ret < 0) {
1879 WARN("%s - Failed to chown(%s, %d, %d)", strerror(errno), path,
1880 (int)chown_uid, (int)chown_gid);
1881 return -1;
1882 }
1883
1884 ret = chmod(path, chmod_mode);
1885 if (ret < 0) {
1886 WARN("%s - Failed to chmod(%s, %d)", strerror(errno), path,
1887 (int)chmod_mode);
1888 return -1;
1889 }
1890
1891 return 0;
1892 }
1893
1894 /* chgrp the container cgroups to container group. We leave
1895 * the container owner as cgroup owner. So we must make the
1896 * directories 775 so that the container can create sub-cgroups.
1897 *
1898 * Also chown the tasks and cgroup.procs files. Those may not
1899 * exist depending on kernel version.
1900 */
1901 static int chown_cgroup_wrapper(void *data)
1902 {
1903 int i, ret;
1904 uid_t destuid;
1905 struct generic_userns_exec_data *arg = data;
1906 uid_t nsuid = (arg->conf->root_nsuid_map != NULL) ? 0 : arg->conf->init_uid;
1907 gid_t nsgid = (arg->conf->root_nsgid_map != NULL) ? 0 : arg->conf->init_gid;
1908
1909 ret = setresgid(nsgid, nsgid, nsgid);
1910 if (ret < 0) {
1911 SYSERROR("Failed to setresgid(%d, %d, %d)",
1912 (int)nsgid, (int)nsgid, (int)nsgid);
1913 return -1;
1914 }
1915
1916 ret = setresuid(nsuid, nsuid, nsuid);
1917 if (ret < 0) {
1918 SYSERROR("Failed to setresuid(%d, %d, %d)",
1919 (int)nsuid, (int)nsuid, (int)nsuid);
1920 return -1;
1921 }
1922
1923 ret = setgroups(0, NULL);
1924 if (ret < 0 && errno != EPERM) {
1925 SYSERROR("Failed to setgroups(0, NULL)");
1926 return -1;
1927 }
1928
1929 destuid = get_ns_uid(arg->origuid);
1930
1931 for (i = 0; hierarchies[i]; i++) {
1932 char *fullpath;
1933 char *path = hierarchies[i]->fullcgpath;
1934
1935 ret = chowmod(path, destuid, nsgid, 0775);
1936 if (ret < 0)
1937 return -1;
1938
1939 /* Failures to chown() these are inconvenient but not
1940 * detrimental We leave these owned by the container launcher,
1941 * so that container root can write to the files to attach. We
1942 * chmod() them 664 so that container systemd can write to the
1943 * files (which systemd in wily insists on doing).
1944 */
1945
1946 if (hierarchies[i]->version == CGROUP_SUPER_MAGIC) {
1947 fullpath = must_make_path(path, "tasks", NULL);
1948 (void)chowmod(fullpath, destuid, nsgid, 0664);
1949 free(fullpath);
1950 }
1951
1952 fullpath = must_make_path(path, "cgroup.procs", NULL);
1953 (void)chowmod(fullpath, destuid, 0, 0664);
1954 free(fullpath);
1955
1956 if (hierarchies[i]->version != CGROUP2_SUPER_MAGIC)
1957 continue;
1958
1959 fullpath = must_make_path(path, "cgroup.subtree_control", NULL);
1960 (void)chowmod(fullpath, destuid, nsgid, 0664);
1961 free(fullpath);
1962
1963 fullpath = must_make_path(path, "cgroup.threads", NULL);
1964 (void)chowmod(fullpath, destuid, nsgid, 0664);
1965 free(fullpath);
1966 }
1967
1968 return 0;
1969 }
1970
1971 static bool cgfsng_chown(void *hdata, struct lxc_conf *conf)
1972 {
1973 struct cgfsng_handler_data *d = hdata;
1974 struct generic_userns_exec_data wrap;
1975
1976 if (!d)
1977 return false;
1978
1979 if (lxc_list_empty(&conf->id_map))
1980 return true;
1981
1982 wrap.origuid = geteuid();
1983 wrap.path = NULL;
1984 wrap.d = d;
1985 wrap.conf = conf;
1986
1987 if (userns_exec_1(conf, chown_cgroup_wrapper, &wrap,
1988 "chown_cgroup_wrapper") < 0) {
1989 ERROR("Error requesting cgroup chown in new user namespace");
1990 return false;
1991 }
1992
1993 return true;
1994 }
1995
1996 /* cgroup-full:* is done, no need to create subdirs */
1997 static bool cg_mount_needs_subdirs(int type)
1998 {
1999 if (type >= LXC_AUTO_CGROUP_FULL_RO)
2000 return false;
2001
2002 return true;
2003 }
2004
2005 /* After $rootfs/sys/fs/container/controller/the/cg/path has been created,
2006 * remount controller ro if needed and bindmount the cgroupfs onto
2007 * controll/the/cg/path.
2008 */
2009 static int cg_legacy_mount_controllers(int type, struct hierarchy *h,
2010 char *controllerpath, char *cgpath,
2011 const char *container_cgroup)
2012 {
2013 int ret, remount_flags;
2014 char *sourcepath;
2015 int flags = MS_BIND;
2016
2017 if (type == LXC_AUTO_CGROUP_RO || type == LXC_AUTO_CGROUP_MIXED) {
2018 ret = mount(controllerpath, controllerpath, "cgroup", MS_BIND, NULL);
2019 if (ret < 0) {
2020 SYSERROR("Failed to bind mount \"%s\" onto \"%s\"",
2021 controllerpath, controllerpath);
2022 return -1;
2023 }
2024
2025 remount_flags = add_required_remount_flags(controllerpath,
2026 controllerpath,
2027 flags | MS_REMOUNT);
2028 ret = mount(controllerpath, controllerpath, "cgroup",
2029 MS_REMOUNT | MS_BIND | MS_RDONLY, NULL);
2030 if (ret < 0) {
2031 SYSERROR("Failed to remount \"%s\" ro", controllerpath);
2032 return -1;
2033 }
2034
2035 INFO("Remounted %s read-only", controllerpath);
2036 }
2037
2038 sourcepath = must_make_path(h->mountpoint, h->base_cgroup,
2039 container_cgroup, NULL);
2040 if (type == LXC_AUTO_CGROUP_RO)
2041 flags |= MS_RDONLY;
2042
2043 ret = mount(sourcepath, cgpath, "cgroup", flags, NULL);
2044 if (ret < 0) {
2045 SYSERROR("Failed to mount \"%s\" onto \"%s\"", h->controllers[0], cgpath);
2046 free(sourcepath);
2047 return -1;
2048 }
2049 INFO("Mounted \"%s\" onto \"%s\"", h->controllers[0], cgpath);
2050
2051 if (flags & MS_RDONLY) {
2052 remount_flags = add_required_remount_flags(sourcepath, cgpath,
2053 flags | MS_REMOUNT);
2054 ret = mount(sourcepath, cgpath, "cgroup", remount_flags, NULL);
2055 if (ret < 0) {
2056 SYSERROR("Failed to remount \"%s\" ro", cgpath);
2057 free(sourcepath);
2058 return -1;
2059 }
2060 INFO("Remounted %s read-only", cgpath);
2061 }
2062
2063 free(sourcepath);
2064 INFO("Completed second stage cgroup automounts for \"%s\"", cgpath);
2065 return 0;
2066 }
2067
2068 /* __cg_mount_direct
2069 *
2070 * Mount cgroup hierarchies directly without using bind-mounts. The main
2071 * uses-cases are mounting cgroup hierarchies in cgroup namespaces and mounting
2072 * cgroups for the LXC_AUTO_CGROUP_FULL option.
2073 */
2074 static int __cg_mount_direct(int type, struct hierarchy *h,
2075 const char *controllerpath)
2076 {
2077 int ret;
2078 char *controllers = NULL;
2079 char *fstype = "cgroup2";
2080 unsigned long flags = 0;
2081
2082 flags |= MS_NOSUID;
2083 flags |= MS_NOEXEC;
2084 flags |= MS_NODEV;
2085 flags |= MS_RELATIME;
2086
2087 if (type == LXC_AUTO_CGROUP_RO || type == LXC_AUTO_CGROUP_FULL_RO)
2088 flags |= MS_RDONLY;
2089
2090 if (h->version != CGROUP2_SUPER_MAGIC) {
2091 controllers = lxc_string_join(",", (const char **)h->controllers, false);
2092 if (!controllers)
2093 return -ENOMEM;
2094 fstype = "cgroup";
2095 }
2096
2097 ret = mount("cgroup", controllerpath, fstype, flags, controllers);
2098 free(controllers);
2099 if (ret < 0) {
2100 SYSERROR("Failed to mount \"%s\" with cgroup filesystem type %s", controllerpath, fstype);
2101 return -1;
2102 }
2103
2104 DEBUG("Mounted \"%s\" with cgroup filesystem type %s", controllerpath, fstype);
2105 return 0;
2106 }
2107
2108 static inline int cg_mount_in_cgroup_namespace(int type, struct hierarchy *h,
2109 const char *controllerpath)
2110 {
2111 return __cg_mount_direct(type, h, controllerpath);
2112 }
2113
2114 static inline int cg_mount_cgroup_full(int type, struct hierarchy *h,
2115 const char *controllerpath)
2116 {
2117 if (type < LXC_AUTO_CGROUP_FULL_RO || type > LXC_AUTO_CGROUP_FULL_MIXED)
2118 return 0;
2119
2120 return __cg_mount_direct(type, h, controllerpath);
2121 }
2122
2123 static bool cgfsng_mount(void *hdata, const char *root, int type)
2124 {
2125 int i, ret;
2126 char *tmpfspath = NULL;
2127 bool has_cgns = false, retval = false, wants_force_mount = false;
2128 struct lxc_handler *handler = hdata;
2129 struct cgfsng_handler_data *d = handler->cgroup_data;
2130
2131 if ((type & LXC_AUTO_CGROUP_MASK) == 0)
2132 return true;
2133
2134 if (type & LXC_AUTO_CGROUP_FORCE) {
2135 type &= ~LXC_AUTO_CGROUP_FORCE;
2136 wants_force_mount = true;
2137 }
2138
2139 if (!wants_force_mount){
2140 if (!lxc_list_empty(&handler->conf->keepcaps))
2141 wants_force_mount = !in_caplist(CAP_SYS_ADMIN, &handler->conf->keepcaps);
2142 else
2143 wants_force_mount = in_caplist(CAP_SYS_ADMIN, &handler->conf->caps);
2144 }
2145
2146 has_cgns = cgns_supported();
2147 if (has_cgns && !wants_force_mount)
2148 return true;
2149
2150 if (type == LXC_AUTO_CGROUP_NOSPEC)
2151 type = LXC_AUTO_CGROUP_MIXED;
2152 else if (type == LXC_AUTO_CGROUP_FULL_NOSPEC)
2153 type = LXC_AUTO_CGROUP_FULL_MIXED;
2154
2155 /* Mount tmpfs */
2156 tmpfspath = must_make_path(root, "/sys/fs/cgroup", NULL);
2157 ret = safe_mount(NULL, tmpfspath, "tmpfs",
2158 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME,
2159 "size=10240k,mode=755", root);
2160 if (ret < 0)
2161 goto on_error;
2162
2163 for (i = 0; hierarchies[i]; i++) {
2164 char *controllerpath, *path2;
2165 struct hierarchy *h = hierarchies[i];
2166 char *controller = strrchr(h->mountpoint, '/');
2167
2168 if (!controller)
2169 continue;
2170 controller++;
2171
2172 controllerpath = must_make_path(tmpfspath, controller, NULL);
2173 if (dir_exists(controllerpath)) {
2174 free(controllerpath);
2175 continue;
2176 }
2177
2178 ret = mkdir(controllerpath, 0755);
2179 if (ret < 0) {
2180 SYSERROR("Error creating cgroup path: %s", controllerpath);
2181 free(controllerpath);
2182 goto on_error;
2183 }
2184
2185 if (has_cgns && wants_force_mount) {
2186 /* If cgroup namespaces are supported but the container
2187 * will not have CAP_SYS_ADMIN after it has started we
2188 * need to mount the cgroups manually.
2189 */
2190 ret = cg_mount_in_cgroup_namespace(type, h, controllerpath);
2191 free(controllerpath);
2192 if (ret < 0)
2193 goto on_error;
2194
2195 continue;
2196 }
2197
2198 ret = cg_mount_cgroup_full(type, h, controllerpath);
2199 if (ret < 0) {
2200 free(controllerpath);
2201 goto on_error;
2202 }
2203
2204 if (!cg_mount_needs_subdirs(type)) {
2205 free(controllerpath);
2206 continue;
2207 }
2208
2209 path2 = must_make_path(controllerpath, h->base_cgroup,
2210 d->container_cgroup, NULL);
2211 ret = mkdir_p(path2, 0755);
2212 if (ret < 0) {
2213 free(controllerpath);
2214 free(path2);
2215 goto on_error;
2216 }
2217
2218 ret = cg_legacy_mount_controllers(type, h, controllerpath,
2219 path2, d->container_cgroup);
2220 free(controllerpath);
2221 free(path2);
2222 if (ret < 0)
2223 goto on_error;
2224 }
2225 retval = true;
2226
2227 on_error:
2228 free(tmpfspath);
2229 return retval;
2230 }
2231
2232 static int recursive_count_nrtasks(char *dirname)
2233 {
2234 struct dirent *direntp;
2235 DIR *dir;
2236 int count = 0, ret;
2237 char *path;
2238
2239 dir = opendir(dirname);
2240 if (!dir)
2241 return 0;
2242
2243 while ((direntp = readdir(dir))) {
2244 struct stat mystat;
2245
2246 if (!direntp)
2247 break;
2248
2249 if (!strcmp(direntp->d_name, ".") ||
2250 !strcmp(direntp->d_name, ".."))
2251 continue;
2252
2253 path = must_make_path(dirname, direntp->d_name, NULL);
2254
2255 if (lstat(path, &mystat))
2256 goto next;
2257
2258 if (!S_ISDIR(mystat.st_mode))
2259 goto next;
2260
2261 count += recursive_count_nrtasks(path);
2262 next:
2263 free(path);
2264 }
2265
2266 path = must_make_path(dirname, "cgroup.procs", NULL);
2267 ret = lxc_count_file_lines(path);
2268 if (ret != -1)
2269 count += ret;
2270 free(path);
2271
2272 (void)closedir(dir);
2273
2274 return count;
2275 }
2276
2277 static int cgfsng_nrtasks(void *hdata)
2278 {
2279 int count;
2280 char *path;
2281 struct cgfsng_handler_data *d = hdata;
2282
2283 if (!d || !d->container_cgroup || !hierarchies)
2284 return -1;
2285
2286 path = must_make_path(hierarchies[0]->fullcgpath, NULL);
2287 count = recursive_count_nrtasks(path);
2288 free(path);
2289 return count;
2290 }
2291
2292 /* Only root needs to escape to the cgroup of its init. */
2293 static bool cgfsng_escape()
2294 {
2295 int i;
2296
2297 if (geteuid())
2298 return true;
2299
2300 for (i = 0; hierarchies[i]; i++) {
2301 int ret;
2302 char *fullpath;
2303
2304 fullpath = must_make_path(hierarchies[i]->mountpoint,
2305 hierarchies[i]->base_cgroup,
2306 "cgroup.procs", NULL);
2307 ret = lxc_write_to_file(fullpath, "0", 2, false);
2308 if (ret != 0) {
2309 SYSERROR("Failed to escape to cgroup \"%s\"", fullpath);
2310 free(fullpath);
2311 return false;
2312 }
2313 free(fullpath);
2314 }
2315
2316 return true;
2317 }
2318
2319 static int cgfsng_num_hierarchies(void)
2320 {
2321 int i;
2322
2323 for (i = 0; hierarchies[i]; i++)
2324 ;
2325
2326 return i;
2327 }
2328
2329 static bool cgfsng_get_hierarchies(int n, char ***out)
2330 {
2331 int i;
2332
2333 /* sanity check n */
2334 for (i = 0; i < n; i++)
2335 if (!hierarchies[i])
2336 return false;
2337
2338 *out = hierarchies[i]->controllers;
2339
2340 return true;
2341 }
2342
2343 #define THAWED "THAWED"
2344 #define THAWED_LEN (strlen(THAWED))
2345
2346 /* TODO: If the unified cgroup hierarchy grows a freezer controller this needs
2347 * to be adapted.
2348 */
2349 static bool cgfsng_unfreeze(void *hdata)
2350 {
2351 int ret;
2352 char *fullpath;
2353 struct hierarchy *h;
2354
2355 h = get_hierarchy("freezer");
2356 if (!h)
2357 return false;
2358
2359 fullpath = must_make_path(h->fullcgpath, "freezer.state", NULL);
2360 ret = lxc_write_to_file(fullpath, THAWED, THAWED_LEN, false);
2361 free(fullpath);
2362 if (ret < 0)
2363 return false;
2364
2365 return true;
2366 }
2367
2368 static const char *cgfsng_get_cgroup(void *hdata, const char *subsystem)
2369 {
2370 struct hierarchy *h;
2371
2372 h = get_hierarchy(subsystem);
2373 if (!h)
2374 return NULL;
2375
2376 return h->fullcgpath ? h->fullcgpath + strlen(h->mountpoint) : NULL;
2377 }
2378
2379 /* Given a cgroup path returned from lxc_cmd_get_cgroup_path, build a full path,
2380 * which must be freed by the caller.
2381 */
2382 static inline char *build_full_cgpath_from_monitorpath(struct hierarchy *h,
2383 const char *inpath,
2384 const char *filename)
2385 {
2386 return must_make_path(h->mountpoint, inpath, filename, NULL);
2387 }
2388
2389 /* Technically, we're always at a delegation boundary here (This is especially
2390 * true when cgroup namespaces are available.). The reasoning is that in order
2391 * for us to have been able to start a container in the first place the root
2392 * cgroup must have been a leaf node. Now, either the container's init system
2393 * has populated the cgroup and kept it as a leaf node or it has created
2394 * subtrees. In the former case we will simply attach to the leaf node we
2395 * created when we started the container in the latter case we create our own
2396 * cgroup for the attaching process.
2397 */
2398 static int __cg_unified_attach(const struct hierarchy *h, const char *name,
2399 const char *lxcpath, const char *pidstr,
2400 size_t pidstr_len, const char *controller)
2401 {
2402 int ret;
2403 size_t len;
2404 int fret = -1, idx = 0;
2405 char *base_path = NULL, *container_cgroup = NULL, *full_path = NULL;
2406
2407 container_cgroup = lxc_cmd_get_cgroup_path(name, lxcpath, controller);
2408 /* not running */
2409 if (!container_cgroup)
2410 return 0;
2411
2412 base_path = must_make_path(h->mountpoint, container_cgroup, NULL);
2413 full_path = must_make_path(base_path, "cgroup.procs", NULL);
2414 /* cgroup is populated */
2415 ret = lxc_write_to_file(full_path, pidstr, pidstr_len, false);
2416 if (ret < 0 && errno != EBUSY)
2417 goto on_error;
2418
2419 if (ret == 0)
2420 goto on_success;
2421
2422 free(full_path);
2423
2424 len = strlen(base_path) + sizeof("/lxc-1000") - 1 +
2425 sizeof("/cgroup-procs") - 1;
2426 full_path = must_alloc(len + 1);
2427 do {
2428 if (idx)
2429 ret = snprintf(full_path, len + 1, "%s/lxc-%d",
2430 base_path, idx);
2431 else
2432 ret = snprintf(full_path, len + 1, "%s/lxc", base_path);
2433 if (ret < 0 || (size_t)ret >= len + 1)
2434 goto on_error;
2435
2436 ret = mkdir_p(full_path, 0755);
2437 if (ret < 0 && errno != EEXIST)
2438 goto on_error;
2439
2440 strcat(full_path, "/cgroup.procs");
2441 ret = lxc_write_to_file(full_path, pidstr, len, false);
2442 if (ret == 0)
2443 goto on_success;
2444
2445 /* this is a non-leaf node */
2446 if (errno != EBUSY)
2447 goto on_error;
2448
2449 } while (++idx > 0 && idx < 1000);
2450
2451 on_success:
2452 if (idx < 1000)
2453 fret = 0;
2454
2455 on_error:
2456 free(base_path);
2457 free(container_cgroup);
2458 free(full_path);
2459
2460 return fret;
2461 }
2462
2463 static bool cgfsng_attach(const char *name, const char *lxcpath, pid_t pid)
2464 {
2465 int i, len, ret;
2466 char pidstr[25];
2467
2468 len = snprintf(pidstr, 25, "%d", pid);
2469 if (len < 0 || len >= 25)
2470 return false;
2471
2472 for (i = 0; hierarchies[i]; i++) {
2473 char *path;
2474 char *fullpath = NULL;
2475 struct hierarchy *h = hierarchies[i];
2476
2477 if (h->version == CGROUP2_SUPER_MAGIC) {
2478 ret = __cg_unified_attach(h, name, lxcpath, pidstr, len,
2479 h->controllers[0]);
2480 if (ret < 0)
2481 return false;
2482
2483 continue;
2484 }
2485
2486 path = lxc_cmd_get_cgroup_path(name, lxcpath, h->controllers[0]);
2487 /* not running */
2488 if (!path)
2489 continue;
2490
2491 fullpath = build_full_cgpath_from_monitorpath(h, path, "cgroup.procs");
2492 ret = lxc_write_to_file(fullpath, pidstr, len, false);
2493 if (ret < 0) {
2494 SYSERROR("Failed to attach %d to %s", (int)pid, fullpath);
2495 free(fullpath);
2496 return false;
2497 }
2498 free(fullpath);
2499 }
2500
2501 return true;
2502 }
2503
2504 /* Called externally (i.e. from 'lxc-cgroup') to query cgroup limits. Here we
2505 * don't have a cgroup_data set up, so we ask the running container through the
2506 * commands API for the cgroup path.
2507 */
2508 static int cgfsng_get(const char *filename, char *value, size_t len,
2509 const char *name, const char *lxcpath)
2510 {
2511 int ret = -1;
2512 size_t controller_len;
2513 char *controller, *p, *path;
2514 struct hierarchy *h;
2515
2516 controller_len = strlen(filename);
2517 controller = alloca(controller_len + 1);
2518 strcpy(controller, filename);
2519 p = strchr(controller, '.');
2520 if (p)
2521 *p = '\0';
2522
2523 path = lxc_cmd_get_cgroup_path(name, lxcpath, controller);
2524 /* not running */
2525 if (!path)
2526 return -1;
2527
2528 h = get_hierarchy(controller);
2529 if (h) {
2530 char *fullpath;
2531
2532 fullpath = build_full_cgpath_from_monitorpath(h, path, filename);
2533 ret = lxc_read_from_file(fullpath, value, len);
2534 free(fullpath);
2535 }
2536 free(path);
2537
2538 return ret;
2539 }
2540
2541 /* Called externally (i.e. from 'lxc-cgroup') to set new cgroup limits. Here we
2542 * don't have a cgroup_data set up, so we ask the running container through the
2543 * commands API for the cgroup path.
2544 */
2545 static int cgfsng_set(const char *filename, const char *value, const char *name,
2546 const char *lxcpath)
2547 {
2548 int ret = -1;
2549 size_t controller_len;
2550 char *controller, *p, *path;
2551 struct hierarchy *h;
2552
2553 controller_len = strlen(filename);
2554 controller = alloca(controller_len + 1);
2555 strcpy(controller, filename);
2556 p = strchr(controller, '.');
2557 if (p)
2558 *p = '\0';
2559
2560 path = lxc_cmd_get_cgroup_path(name, lxcpath, controller);
2561 /* not running */
2562 if (!path)
2563 return -1;
2564
2565 h = get_hierarchy(controller);
2566 if (h) {
2567 char *fullpath;
2568
2569 fullpath = build_full_cgpath_from_monitorpath(h, path, filename);
2570 ret = lxc_write_to_file(fullpath, value, strlen(value), false);
2571 free(fullpath);
2572 }
2573 free(path);
2574
2575 return ret;
2576 }
2577
2578 /* take devices cgroup line
2579 * /dev/foo rwx
2580 * and convert it to a valid
2581 * type major:minor mode
2582 * line. Return <0 on error. Dest is a preallocated buffer long enough to hold
2583 * the output.
2584 */
2585 static int convert_devpath(const char *invalue, char *dest)
2586 {
2587 int n_parts;
2588 char *p, *path, type;
2589 unsigned long minor, major;
2590 struct stat sb;
2591 int ret = -EINVAL;
2592 char *mode = NULL;
2593
2594 path = must_copy_string(invalue);
2595
2596 /* Read path followed by mode. Ignore any trailing text.
2597 * A ' # comment' would be legal. Technically other text is not
2598 * legal, we could check for that if we cared to.
2599 */
2600 for (n_parts = 1, p = path; *p && n_parts < 3; p++) {
2601 if (*p != ' ')
2602 continue;
2603 *p = '\0';
2604
2605 if (n_parts != 1)
2606 break;
2607 p++;
2608 n_parts++;
2609
2610 while (*p == ' ')
2611 p++;
2612
2613 mode = p;
2614
2615 if (*p == '\0')
2616 goto out;
2617 }
2618
2619 if (n_parts == 1)
2620 goto out;
2621
2622 ret = stat(path, &sb);
2623 if (ret < 0)
2624 goto out;
2625
2626 mode_t m = sb.st_mode & S_IFMT;
2627 switch (m) {
2628 case S_IFBLK:
2629 type = 'b';
2630 break;
2631 case S_IFCHR:
2632 type = 'c';
2633 break;
2634 default:
2635 ERROR("Unsupported device type %i for \"%s\"", m, path);
2636 ret = -EINVAL;
2637 goto out;
2638 }
2639
2640 major = MAJOR(sb.st_rdev);
2641 minor = MINOR(sb.st_rdev);
2642 ret = snprintf(dest, 50, "%c %lu:%lu %s", type, major, minor, mode);
2643 if (ret < 0 || ret >= 50) {
2644 ERROR("Error on configuration value \"%c %lu:%lu %s\" (max 50 "
2645 "chars)", type, major, minor, mode);
2646 ret = -ENAMETOOLONG;
2647 goto out;
2648 }
2649 ret = 0;
2650
2651 out:
2652 free(path);
2653 return ret;
2654 }
2655
2656 /* Called from setup_limits - here we have the container's cgroup_data because
2657 * we created the cgroups.
2658 */
2659 static int cg_legacy_set_data(const char *filename, const char *value,
2660 struct cgfsng_handler_data *d)
2661 {
2662 size_t len;
2663 char *fullpath, *p;
2664 /* "b|c <2^64-1>:<2^64-1> r|w|m" = 47 chars max */
2665 char converted_value[50];
2666 struct hierarchy *h;
2667 int ret = 0;
2668 char *controller = NULL;
2669
2670 len = strlen(filename);
2671 controller = alloca(len + 1);
2672 strcpy(controller, filename);
2673 p = strchr(controller, '.');
2674 if (p)
2675 *p = '\0';
2676
2677 if (strcmp("devices.allow", filename) == 0 && value[0] == '/') {
2678 ret = convert_devpath(value, converted_value);
2679 if (ret < 0)
2680 return ret;
2681 value = converted_value;
2682 }
2683
2684 h = get_hierarchy(controller);
2685 if (!h) {
2686 ERROR("Failed to setup limits for the \"%s\" controller. "
2687 "The controller seems to be unused by \"cgfsng\" cgroup "
2688 "driver or not enabled on the cgroup hierarchy",
2689 controller);
2690 errno = ENOENT;
2691 return -ENOENT;
2692 }
2693
2694 fullpath = must_make_path(h->fullcgpath, filename, NULL);
2695 ret = lxc_write_to_file(fullpath, value, strlen(value), false);
2696 free(fullpath);
2697 return ret;
2698 }
2699
2700 static bool __cg_legacy_setup_limits(void *hdata,
2701 struct lxc_list *cgroup_settings,
2702 bool do_devices)
2703 {
2704 struct lxc_list *iterator, *next, *sorted_cgroup_settings;
2705 struct lxc_cgroup *cg;
2706 struct cgfsng_handler_data *d = hdata;
2707 bool ret = false;
2708
2709 if (lxc_list_empty(cgroup_settings))
2710 return true;
2711
2712 sorted_cgroup_settings = sort_cgroup_settings(cgroup_settings);
2713 if (!sorted_cgroup_settings)
2714 return false;
2715
2716 lxc_list_for_each(iterator, sorted_cgroup_settings) {
2717 cg = iterator->elem;
2718
2719 if (do_devices == !strncmp("devices", cg->subsystem, 7)) {
2720 if (cg_legacy_set_data(cg->subsystem, cg->value, d)) {
2721 if (do_devices && (errno == EACCES || errno == EPERM)) {
2722 WARN("Failed to set \"%s\" to \"%s\"",
2723 cg->subsystem, cg->value);
2724 continue;
2725 }
2726 WARN("Failed to set \"%s\" to \"%s\"",
2727 cg->subsystem, cg->value);
2728 goto out;
2729 }
2730 DEBUG("Set controller \"%s\" set to \"%s\"",
2731 cg->subsystem, cg->value);
2732 }
2733 }
2734
2735 ret = true;
2736 INFO("Limits for the legacy cgroup hierarchies have been setup");
2737 out:
2738 lxc_list_for_each_safe(iterator, sorted_cgroup_settings, next) {
2739 lxc_list_del(iterator);
2740 free(iterator);
2741 }
2742 free(sorted_cgroup_settings);
2743 return ret;
2744 }
2745
2746 static bool __cg_unified_setup_limits(void *hdata,
2747 struct lxc_list *cgroup_settings)
2748 {
2749 struct lxc_list *iterator;
2750 struct hierarchy *h = unified;
2751
2752 if (lxc_list_empty(cgroup_settings))
2753 return true;
2754
2755 if (!h)
2756 return false;
2757
2758 lxc_list_for_each(iterator, cgroup_settings) {
2759 int ret;
2760 char *fullpath;
2761 struct lxc_cgroup *cg = iterator->elem;
2762
2763 fullpath = must_make_path(h->fullcgpath, cg->subsystem, NULL);
2764 ret = lxc_write_to_file(fullpath, cg->value, strlen(cg->value), false);
2765 free(fullpath);
2766 if (ret < 0) {
2767 SYSERROR("Failed to set \"%s\" to \"%s\"",
2768 cg->subsystem, cg->value);
2769 return false;
2770 }
2771 TRACE("Set \"%s\" to \"%s\"", cg->subsystem, cg->value);
2772 }
2773
2774 INFO("Limits for the unified cgroup hierarchy have been setup");
2775 return true;
2776 }
2777
2778 static bool cgfsng_setup_limits(void *hdata, struct lxc_conf *conf,
2779 bool do_devices)
2780 {
2781 bool bret;
2782
2783 bret = __cg_legacy_setup_limits(hdata, &conf->cgroup, do_devices);
2784 if (!bret)
2785 return false;
2786
2787 return __cg_unified_setup_limits(hdata, &conf->cgroup2);
2788 }
2789
2790 static struct cgroup_ops cgfsng_ops = {
2791 .init = cgfsng_init,
2792 .destroy = cgfsng_destroy,
2793 .create = cgfsng_create,
2794 .enter = cgfsng_enter,
2795 .escape = cgfsng_escape,
2796 .num_hierarchies = cgfsng_num_hierarchies,
2797 .get_hierarchies = cgfsng_get_hierarchies,
2798 .get_cgroup = cgfsng_get_cgroup,
2799 .get = cgfsng_get,
2800 .set = cgfsng_set,
2801 .unfreeze = cgfsng_unfreeze,
2802 .setup_limits = cgfsng_setup_limits,
2803 .driver = "cgfsng",
2804 .version = "1.0.0",
2805 .attach = cgfsng_attach,
2806 .chown = cgfsng_chown,
2807 .mount_cgroup = cgfsng_mount,
2808 .nrtasks = cgfsng_nrtasks,
2809
2810 /* unsupported */
2811 .create_legacy = NULL,
2812 };