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