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