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