]>
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 | ||
690 | /* | |
691 | * Initialize the cpuset hierarchy in first directory of @gname and | |
692 | * set cgroup.clone_children so that children inherit settings. | |
693 | * Since the h->base_path is populated by init or ourselves, we know | |
694 | * it is already initialized. | |
695 | */ | |
a3926f6a | 696 | static bool cg_legacy_handle_cpuset_hierarchy(struct hierarchy *h, char *cgname) |
e3a3fecf SH |
697 | { |
698 | char *cgpath, *clonechildrenpath, v, *slash; | |
699 | ||
700 | if (!string_in_list(h->controllers, "cpuset")) | |
701 | return true; | |
702 | ||
703 | if (*cgname == '/') | |
704 | cgname++; | |
705 | slash = strchr(cgname, '/'); | |
706 | if (slash) | |
707 | *slash = '\0'; | |
708 | ||
709 | cgpath = must_make_path(h->mountpoint, h->base_cgroup, cgname, NULL); | |
710 | if (slash) | |
711 | *slash = '/'; | |
712 | if (mkdir(cgpath, 0755) < 0 && errno != EEXIST) { | |
713 | SYSERROR("Failed to create '%s'", cgpath); | |
714 | free(cgpath); | |
715 | return false; | |
716 | } | |
6f9584d8 | 717 | |
e3a3fecf | 718 | clonechildrenpath = must_make_path(cgpath, "cgroup.clone_children", NULL); |
6328fd9c CB |
719 | /* unified hierarchy doesn't have clone_children */ |
720 | if (!file_exists(clonechildrenpath)) { | |
e3a3fecf SH |
721 | free(clonechildrenpath); |
722 | free(cgpath); | |
723 | return true; | |
724 | } | |
725 | if (lxc_read_from_file(clonechildrenpath, &v, 1) < 0) { | |
726 | SYSERROR("Failed to read '%s'", clonechildrenpath); | |
727 | free(clonechildrenpath); | |
728 | free(cgpath); | |
729 | return false; | |
730 | } | |
731 | ||
a54694f8 | 732 | /* Make sure any isolated cpus are removed from cpuset.cpus. */ |
a3926f6a | 733 | if (!cg_legacy_filter_and_set_cpus(cgpath, v == '1')) { |
6f9584d8 CB |
734 | SYSERROR("Failed to remove isolated cpus."); |
735 | free(clonechildrenpath); | |
736 | free(cgpath); | |
a54694f8 | 737 | return false; |
6f9584d8 | 738 | } |
a54694f8 | 739 | |
e3a3fecf | 740 | if (v == '1') { /* already set for us by someone else */ |
6f9584d8 | 741 | DEBUG("\"cgroup.clone_children\" was already set to \"1\"."); |
e3a3fecf SH |
742 | free(clonechildrenpath); |
743 | free(cgpath); | |
744 | return true; | |
745 | } | |
746 | ||
747 | /* copy parent's settings */ | |
a54694f8 | 748 | if (!copy_parent_file(cgpath, "cpuset.mems")) { |
6f9584d8 | 749 | SYSERROR("Failed to copy \"cpuset.mems\" settings."); |
e3a3fecf SH |
750 | free(cgpath); |
751 | free(clonechildrenpath); | |
752 | return false; | |
753 | } | |
754 | free(cgpath); | |
755 | ||
756 | if (lxc_write_to_file(clonechildrenpath, "1", 1, false) < 0) { | |
757 | /* Set clone_children so children inherit our settings */ | |
758 | SYSERROR("Failed to write 1 to %s", clonechildrenpath); | |
759 | free(clonechildrenpath); | |
760 | return false; | |
761 | } | |
762 | free(clonechildrenpath); | |
763 | return true; | |
764 | } | |
765 | ||
ccb4cabe SH |
766 | /* |
767 | * Given two null-terminated lists of strings, return true if any string | |
768 | * is in both. | |
769 | */ | |
770 | static bool controller_lists_intersect(char **l1, char **l2) | |
771 | { | |
772 | int i; | |
773 | ||
774 | if (!l1 || !l2) | |
775 | return false; | |
776 | ||
777 | for (i = 0; l1[i]; i++) { | |
778 | if (string_in_list(l2, l1[i])) | |
779 | return true; | |
780 | } | |
781 | return false; | |
782 | } | |
783 | ||
784 | /* | |
785 | * For a null-terminated list of controllers @clist, return true if any of | |
786 | * those controllers is already listed the null-terminated list of | |
787 | * hierarchies @hlist. Realistically, if one is present, all must be present. | |
788 | */ | |
789 | static bool controller_list_is_dup(struct hierarchy **hlist, char **clist) | |
790 | { | |
791 | int i; | |
792 | ||
793 | if (!hlist) | |
794 | return false; | |
795 | for (i = 0; hlist[i]; i++) | |
796 | if (controller_lists_intersect(hlist[i]->controllers, clist)) | |
797 | return true; | |
798 | return false; | |
799 | ||
800 | } | |
801 | ||
802 | /* | |
803 | * Return true if the controller @entry is found in the null-terminated | |
804 | * list of hierarchies @hlist | |
805 | */ | |
806 | static bool controller_found(struct hierarchy **hlist, char *entry) | |
807 | { | |
808 | int i; | |
d6337a5f | 809 | |
ccb4cabe SH |
810 | if (!hlist) |
811 | return false; | |
812 | ||
813 | for (i = 0; hlist[i]; i++) | |
814 | if (string_in_list(hlist[i]->controllers, entry)) | |
815 | return true; | |
d6337a5f | 816 | |
ccb4cabe SH |
817 | return false; |
818 | } | |
819 | ||
820 | /* | |
c30b61c3 SH |
821 | * Return true if all of the controllers which we require have been found. |
822 | * The required list is freezer and anything in * lxc.cgroup.use. | |
ccb4cabe | 823 | */ |
457ca9aa | 824 | static bool all_controllers_found(void) |
ccb4cabe SH |
825 | { |
826 | char *p, *saveptr = NULL; | |
457ca9aa | 827 | struct hierarchy ** hlist = hierarchies; |
ccb4cabe | 828 | |
ccb4cabe | 829 | if (!controller_found(hlist, "freezer")) { |
65d78313 | 830 | CGFSNG_DEBUG("No freezer controller mountpoint found\n"); |
ccb4cabe SH |
831 | return false; |
832 | } | |
833 | ||
457ca9aa | 834 | if (!cgroup_use) |
ccb4cabe | 835 | return true; |
c2712f64 | 836 | |
457ca9aa | 837 | for (p = strtok_r(cgroup_use, ",", &saveptr); p; |
ccb4cabe SH |
838 | p = strtok_r(NULL, ",", &saveptr)) { |
839 | if (!controller_found(hlist, p)) { | |
65d78313 | 840 | CGFSNG_DEBUG("No %s controller mountpoint found\n", p); |
ccb4cabe SH |
841 | return false; |
842 | } | |
843 | } | |
c2712f64 | 844 | |
ccb4cabe SH |
845 | return true; |
846 | } | |
847 | ||
ccb4cabe SH |
848 | /* |
849 | * Get the controllers from a mountinfo line | |
850 | * There are other ways we could get this info. For lxcfs, field 3 | |
851 | * is /cgroup/controller-list. For cgroupfs, we could parse the mount | |
852 | * options. But we simply assume that the mountpoint must be | |
853 | * /sys/fs/cgroup/controller-list | |
854 | */ | |
a3926f6a CB |
855 | static char **cg_hybrid_get_controllers(char **klist, char **nlist, char *line, |
856 | int type) | |
ccb4cabe | 857 | { |
6328fd9c | 858 | /* the fourth field is /sys/fs/cgroup/comma-delimited-controller-list */ |
ccb4cabe | 859 | int i; |
411ac6d8 | 860 | char *dup, *p2, *tok; |
d6337a5f | 861 | char *p = line, *saveptr = NULL, *sep = ","; |
411ac6d8 | 862 | char **aret = NULL; |
6328fd9c | 863 | |
ccb4cabe | 864 | for (i = 0; i < 4; i++) { |
235f1815 | 865 | p = strchr(p, ' '); |
ccb4cabe SH |
866 | if (!p) |
867 | return NULL; | |
868 | p++; | |
869 | } | |
a55f31bd | 870 | |
ccb4cabe SH |
871 | /* note - if we change how mountinfo works, then our caller |
872 | * will need to verify /sys/fs/cgroup/ in this field */ | |
c2712f64 | 873 | if (strncmp(p, "/sys/fs/cgroup/", 15)) { |
65d78313 | 874 | CGFSNG_DEBUG("Found hierarchy not under /sys/fs/cgroup: \"%s\"\n", p); |
ccb4cabe | 875 | return NULL; |
5059aae9 | 876 | } |
d6337a5f | 877 | |
ccb4cabe | 878 | p += 15; |
235f1815 | 879 | p2 = strchr(p, ' '); |
ccb4cabe | 880 | if (!p2) { |
65d78313 | 881 | CGFSNG_DEBUG("Corrupt mountinfo\n"); |
ccb4cabe SH |
882 | return NULL; |
883 | } | |
884 | *p2 = '\0'; | |
6328fd9c | 885 | |
d6337a5f CB |
886 | if (type == CGROUP_SUPER_MAGIC) { |
887 | /* strdup() here for v1 hierarchies. Otherwise strtok_r() will | |
888 | * destroy mountpoints such as "/sys/fs/cgroup/cpu,cpuacct". | |
889 | */ | |
890 | dup = strdup(p); | |
891 | if (!dup) | |
892 | return NULL; | |
893 | ||
894 | for (tok = strtok_r(dup, sep, &saveptr); tok; | |
895 | tok = strtok_r(NULL, sep, &saveptr)) | |
896 | must_append_controller(klist, nlist, &aret, tok); | |
897 | ||
898 | free(dup); | |
411ac6d8 | 899 | } |
d6337a5f CB |
900 | *p2 = ' '; |
901 | return aret; | |
902 | } | |
411ac6d8 | 903 | |
d6337a5f CB |
904 | static char **cg_unified_make_empty_controller(void) |
905 | { | |
906 | int newentry; | |
907 | char **aret = NULL; | |
908 | ||
909 | newentry = append_null_to_list((void ***)&aret); | |
910 | aret[newentry] = NULL; | |
911 | return aret; | |
912 | } | |
913 | ||
914 | static char **cg_unified_get_controllers(const char *file) | |
915 | { | |
916 | char *buf, *tok; | |
917 | char *saveptr = NULL, *sep = " \t\n"; | |
918 | char **aret = NULL; | |
919 | ||
920 | buf = read_file(file); | |
921 | if (!buf) | |
411ac6d8 | 922 | return NULL; |
6328fd9c | 923 | |
d6337a5f CB |
924 | for (tok = strtok_r(buf, sep, &saveptr); tok; |
925 | tok = strtok_r(NULL, sep, &saveptr)) { | |
926 | int newentry; | |
927 | char *copy; | |
928 | ||
929 | newentry = append_null_to_list((void ***)&aret); | |
930 | copy = must_copy_string(tok); | |
931 | aret[newentry] = copy; | |
ccb4cabe SH |
932 | } |
933 | ||
d6337a5f | 934 | free(buf); |
ccb4cabe SH |
935 | return aret; |
936 | } | |
937 | ||
d6337a5f CB |
938 | static struct hierarchy *add_hierarchy(char **clist, char *mountpoint, |
939 | char *base_cgroup, int type) | |
ccb4cabe SH |
940 | { |
941 | struct hierarchy *new; | |
942 | int newentry; | |
943 | ||
944 | new = must_alloc(sizeof(*new)); | |
945 | new->controllers = clist; | |
946 | new->mountpoint = mountpoint; | |
947 | new->base_cgroup = base_cgroup; | |
948 | new->fullcgpath = NULL; | |
d6337a5f | 949 | new->version = type; |
6328fd9c | 950 | |
457ca9aa SH |
951 | newentry = append_null_to_list((void ***)&hierarchies); |
952 | hierarchies[newentry] = new; | |
d6337a5f | 953 | return new; |
ccb4cabe SH |
954 | } |
955 | ||
956 | /* | |
957 | * Get a copy of the mountpoint from @line, which is a line from | |
958 | * /proc/self/mountinfo | |
959 | */ | |
a3926f6a | 960 | static char *cg_hybrid_get_mountpoint(char *line) |
ccb4cabe SH |
961 | { |
962 | int i; | |
d6337a5f | 963 | char *p2; |
ccb4cabe | 964 | size_t len; |
d6337a5f CB |
965 | char *p = line; |
966 | char *sret = NULL; | |
ccb4cabe SH |
967 | |
968 | for (i = 0; i < 4; i++) { | |
235f1815 | 969 | p = strchr(p, ' '); |
ccb4cabe SH |
970 | if (!p) |
971 | return NULL; | |
972 | p++; | |
973 | } | |
d6337a5f CB |
974 | |
975 | if (strncmp(p, "/sys/fs/cgroup/", 15)) | |
976 | return NULL; | |
977 | ||
978 | p2 = strchr(p + 15, ' '); | |
979 | if (!p2) | |
980 | return NULL; | |
981 | *p2 = '\0'; | |
982 | ||
ccb4cabe SH |
983 | len = strlen(p); |
984 | sret = must_alloc(len + 1); | |
985 | memcpy(sret, p, len); | |
986 | sret[len] = '\0'; | |
987 | return sret; | |
988 | } | |
989 | ||
990 | /* | |
991 | * Given a multi-line string, return a null-terminated copy of the | |
992 | * current line. | |
993 | */ | |
994 | static char *copy_to_eol(char *p) | |
995 | { | |
235f1815 | 996 | char *p2 = strchr(p, '\n'), *sret; |
ccb4cabe SH |
997 | size_t len; |
998 | ||
999 | if (!p2) | |
1000 | return NULL; | |
1001 | ||
1002 | len = p2 - p; | |
1003 | sret = must_alloc(len + 1); | |
1004 | memcpy(sret, p, len); | |
1005 | sret[len] = '\0'; | |
1006 | return sret; | |
1007 | } | |
1008 | ||
1009 | /* | |
1010 | * cgline: pointer to character after the first ':' in a line in a | |
1011 | * \n-terminated /proc/self/cgroup file. Check whether * controller c is | |
1012 | * present. | |
1013 | */ | |
1014 | static bool controller_in_clist(char *cgline, char *c) | |
1015 | { | |
1016 | char *tok, *saveptr = NULL, *eol, *tmp; | |
1017 | size_t len; | |
1018 | ||
235f1815 | 1019 | eol = strchr(cgline, ':'); |
ccb4cabe SH |
1020 | if (!eol) |
1021 | return false; | |
1022 | ||
1023 | len = eol - cgline; | |
1024 | tmp = alloca(len + 1); | |
1025 | memcpy(tmp, cgline, len); | |
1026 | tmp[len] = '\0'; | |
1027 | ||
1028 | for (tok = strtok_r(tmp, ",", &saveptr); tok; | |
d6337a5f | 1029 | tok = strtok_r(NULL, ",", &saveptr)) { |
ccb4cabe SH |
1030 | if (strcmp(tok, c) == 0) |
1031 | return true; | |
1032 | } | |
d6337a5f | 1033 | |
ccb4cabe SH |
1034 | return false; |
1035 | } | |
1036 | ||
1037 | /* | |
1038 | * @basecginfo is a copy of /proc/$$/cgroup. Return the current | |
1039 | * cgroup for @controller | |
1040 | */ | |
a3926f6a | 1041 | static char *cg_hybrid_get_current_cgroup(char *basecginfo, char *controller, int type) |
ccb4cabe SH |
1042 | { |
1043 | char *p = basecginfo; | |
6328fd9c | 1044 | |
d6337a5f CB |
1045 | for (;;) { |
1046 | bool is_cgv2_base_cgroup = false; | |
1047 | ||
6328fd9c | 1048 | /* cgroup v2 entry in "/proc/<pid>/cgroup": "0::/some/path" */ |
d6337a5f CB |
1049 | if ((type == CGROUP2_SUPER_MAGIC) && (*p == '0')) |
1050 | is_cgv2_base_cgroup = true; | |
ccb4cabe | 1051 | |
235f1815 | 1052 | p = strchr(p, ':'); |
ccb4cabe SH |
1053 | if (!p) |
1054 | return NULL; | |
1055 | p++; | |
d6337a5f CB |
1056 | |
1057 | if (is_cgv2_base_cgroup || (controller && controller_in_clist(p, controller))) { | |
235f1815 | 1058 | p = strchr(p, ':'); |
ccb4cabe SH |
1059 | if (!p) |
1060 | return NULL; | |
1061 | p++; | |
1062 | return copy_to_eol(p); | |
1063 | } | |
1064 | ||
235f1815 | 1065 | p = strchr(p, '\n'); |
ccb4cabe SH |
1066 | if (!p) |
1067 | return NULL; | |
1068 | p++; | |
1069 | } | |
1070 | } | |
1071 | ||
ccb4cabe SH |
1072 | static void must_append_string(char ***list, char *entry) |
1073 | { | |
1074 | int newentry = append_null_to_list((void ***)list); | |
1075 | char *copy; | |
1076 | ||
1077 | copy = must_copy_string(entry); | |
1078 | (*list)[newentry] = copy; | |
1079 | } | |
1080 | ||
d6337a5f | 1081 | static int get_existing_subsystems(char ***klist, char ***nlist) |
ccb4cabe SH |
1082 | { |
1083 | FILE *f; | |
1084 | char *line = NULL; | |
1085 | size_t len = 0; | |
1086 | ||
d6337a5f CB |
1087 | f = fopen("/proc/self/cgroup", "r"); |
1088 | if (!f) | |
1089 | return -1; | |
1090 | ||
ccb4cabe SH |
1091 | while (getline(&line, &len, f) != -1) { |
1092 | char *p, *p2, *tok, *saveptr = NULL; | |
235f1815 | 1093 | p = strchr(line, ':'); |
ccb4cabe SH |
1094 | if (!p) |
1095 | continue; | |
1096 | p++; | |
235f1815 | 1097 | p2 = strchr(p, ':'); |
ccb4cabe SH |
1098 | if (!p2) |
1099 | continue; | |
1100 | *p2 = '\0'; | |
ff8d6ee9 | 1101 | |
6328fd9c CB |
1102 | /* If the kernel has cgroup v2 support, then /proc/self/cgroup |
1103 | * contains an entry of the form: | |
ff8d6ee9 CB |
1104 | * |
1105 | * 0::/some/path | |
1106 | * | |
6328fd9c | 1107 | * In this case we use "cgroup2" as controller name. |
ff8d6ee9 | 1108 | */ |
6328fd9c CB |
1109 | if ((p2 - p) == 0) { |
1110 | must_append_string(klist, "cgroup2"); | |
ff8d6ee9 | 1111 | continue; |
6328fd9c | 1112 | } |
ff8d6ee9 | 1113 | |
ccb4cabe | 1114 | for (tok = strtok_r(p, ",", &saveptr); tok; |
d6337a5f | 1115 | tok = strtok_r(NULL, ",", &saveptr)) { |
ccb4cabe SH |
1116 | if (strncmp(tok, "name=", 5) == 0) |
1117 | must_append_string(nlist, tok); | |
1118 | else | |
1119 | must_append_string(klist, tok); | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | free(line); | |
1124 | fclose(f); | |
d6337a5f | 1125 | return 0; |
ccb4cabe SH |
1126 | } |
1127 | ||
1128 | static void trim(char *s) | |
1129 | { | |
1130 | size_t len = strlen(s); | |
2c28d76b | 1131 | while ((len > 1) && (s[len - 1] == '\n')) |
ccb4cabe SH |
1132 | s[--len] = '\0'; |
1133 | } | |
1134 | ||
e4aeecf5 CB |
1135 | static void lxc_cgfsng_print_handler_data(const struct cgfsng_handler_data *d) |
1136 | { | |
1137 | printf("Cgroup information:\n"); | |
1138 | printf(" container name: %s\n", d->name ? d->name : "(null)"); | |
1139 | printf(" lxc.cgroup.use: %s\n", cgroup_use ? cgroup_use : "(null)"); | |
43654d34 CB |
1140 | printf(" lxc.cgroup.pattern: %s\n", |
1141 | d->cgroup_pattern ? d->cgroup_pattern : "(null)"); | |
1142 | printf(" lxc.cgroup.dir: %s\n", | |
1143 | d->cgroup_meta.dir ? d->cgroup_meta.dir : "(null)"); | |
1144 | printf(" cgroup: %s\n", | |
1145 | d->container_cgroup ? d->container_cgroup : "(null)"); | |
e4aeecf5 CB |
1146 | } |
1147 | ||
1148 | static void lxc_cgfsng_print_hierarchies() | |
ccb4cabe | 1149 | { |
a7b0cc4c | 1150 | struct hierarchy **it; |
ccb4cabe | 1151 | int i; |
41c33dbe | 1152 | |
457ca9aa | 1153 | if (!hierarchies) { |
c2712f64 | 1154 | printf(" No hierarchies found\n"); |
ccb4cabe SH |
1155 | return; |
1156 | } | |
e4aeecf5 | 1157 | printf(" Hierarchies:\n"); |
a7b0cc4c CB |
1158 | for (i = 0, it = hierarchies; it && *it; it++, i++) { |
1159 | char **cit; | |
ccb4cabe | 1160 | int j; |
c2712f64 CB |
1161 | printf(" %d: base_cgroup: %s\n", i, (*it)->base_cgroup ? (*it)->base_cgroup : "(null)"); |
1162 | printf(" mountpoint: %s\n", (*it)->mountpoint ? (*it)->mountpoint : "(null)"); | |
e4aeecf5 | 1163 | printf(" controllers:\n"); |
a7b0cc4c | 1164 | for (j = 0, cit = (*it)->controllers; cit && *cit; cit++, j++) |
e4aeecf5 | 1165 | printf(" %d: %s\n", j, *cit); |
ccb4cabe SH |
1166 | } |
1167 | } | |
41c33dbe | 1168 | |
a3926f6a CB |
1169 | static void lxc_cgfsng_print_basecg_debuginfo(char *basecginfo, char **klist, |
1170 | char **nlist) | |
41c33dbe SH |
1171 | { |
1172 | int k; | |
a7b0cc4c | 1173 | char **it; |
41c33dbe | 1174 | |
a7b0cc4c CB |
1175 | printf("basecginfo is:\n"); |
1176 | printf("%s\n", basecginfo); | |
41c33dbe | 1177 | |
a7b0cc4c CB |
1178 | for (k = 0, it = klist; it && *it; it++, k++) |
1179 | printf("kernel subsystem %d: %s\n", k, *it); | |
1180 | for (k = 0, it = nlist; it && *it; it++, k++) | |
1181 | printf("named subsystem %d: %s\n", k, *it); | |
41c33dbe | 1182 | } |
ccb4cabe | 1183 | |
e4aeecf5 CB |
1184 | static void lxc_cgfsng_print_debuginfo(const struct cgfsng_handler_data *d) |
1185 | { | |
1186 | lxc_cgfsng_print_handler_data(d); | |
1187 | lxc_cgfsng_print_hierarchies(); | |
1188 | } | |
1189 | ||
ccb4cabe SH |
1190 | /* |
1191 | * At startup, parse_hierarchies finds all the info we need about | |
1192 | * cgroup mountpoints and current cgroups, and stores it in @d. | |
1193 | */ | |
a3926f6a | 1194 | static bool cg_hybrid_init(void) |
ccb4cabe | 1195 | { |
d6337a5f CB |
1196 | int ret; |
1197 | char *basecginfo; | |
1198 | bool will_escape; | |
ccb4cabe | 1199 | FILE *f; |
ccb4cabe | 1200 | size_t len = 0; |
d6337a5f CB |
1201 | char *line = NULL; |
1202 | char **klist = NULL, **nlist = NULL; | |
ccb4cabe | 1203 | |
d30ec4cb SH |
1204 | /* |
1205 | * Root spawned containers escape the current cgroup, so use init's | |
1206 | * cgroups as our base in that case. | |
1207 | */ | |
d6337a5f CB |
1208 | will_escape = (geteuid() == 0); |
1209 | if (will_escape) | |
ccb4cabe | 1210 | basecginfo = read_file("/proc/1/cgroup"); |
d6337a5f CB |
1211 | else |
1212 | basecginfo = read_file("/proc/self/cgroup"); | |
ccb4cabe SH |
1213 | if (!basecginfo) |
1214 | return false; | |
1215 | ||
d6337a5f CB |
1216 | ret = get_existing_subsystems(&klist, &nlist); |
1217 | if (ret < 0) { | |
1218 | CGFSNG_DEBUG("Failed to retrieve available cgroup v1 controllers\n"); | |
1219 | free(basecginfo); | |
ccb4cabe SH |
1220 | return false; |
1221 | } | |
1222 | ||
d6337a5f CB |
1223 | f = fopen("/proc/self/mountinfo", "r"); |
1224 | if (!f) { | |
1225 | CGFSNG_DEBUG("Failed to open \"/proc/self/mountinfo\"\n"); | |
bd01b7d5 | 1226 | free(basecginfo); |
d6337a5f CB |
1227 | return false; |
1228 | } | |
41c33dbe | 1229 | |
e4aeecf5 CB |
1230 | if (lxc_cgfsng_debug) |
1231 | lxc_cgfsng_print_basecg_debuginfo(basecginfo, klist, nlist); | |
ccb4cabe | 1232 | |
ccb4cabe | 1233 | while (getline(&line, &len, f) != -1) { |
49ff3958 | 1234 | int type; |
d6337a5f CB |
1235 | bool writeable; |
1236 | struct hierarchy *new; | |
1237 | char *mountpoint = NULL, *base_cgroup = NULL; | |
1238 | char **controller_list = NULL; | |
ccb4cabe | 1239 | |
49ff3958 | 1240 | type = get_cgroup_version(line); |
d6337a5f | 1241 | if (type == 0) |
ccb4cabe SH |
1242 | continue; |
1243 | ||
d6337a5f | 1244 | if (type == CGROUP2_SUPER_MAGIC && unified) |
ccb4cabe SH |
1245 | continue; |
1246 | ||
d6337a5f CB |
1247 | if (cgroup_layout == CGROUP_LAYOUT_UNKNOWN) { |
1248 | if (type == CGROUP2_SUPER_MAGIC) | |
1249 | cgroup_layout = CGROUP_LAYOUT_UNIFIED; | |
1250 | else if (type == CGROUP_SUPER_MAGIC) | |
1251 | cgroup_layout = CGROUP_LAYOUT_LEGACY; | |
1252 | } else if (cgroup_layout == CGROUP_LAYOUT_UNIFIED) { | |
1253 | if (type == CGROUP_SUPER_MAGIC) | |
1254 | cgroup_layout = CGROUP_LAYOUT_HYBRID; | |
1255 | } else if (cgroup_layout == CGROUP_LAYOUT_LEGACY) { | |
1256 | if (type == CGROUP2_SUPER_MAGIC) | |
1257 | cgroup_layout = CGROUP_LAYOUT_HYBRID; | |
ccb4cabe SH |
1258 | } |
1259 | ||
a3926f6a | 1260 | controller_list = cg_hybrid_get_controllers(klist, nlist, line, type); |
d6337a5f CB |
1261 | if (!controller_list && type == CGROUP_SUPER_MAGIC) |
1262 | continue; | |
1263 | ||
1264 | if (type == CGROUP_SUPER_MAGIC) | |
1265 | if (controller_list_is_dup(hierarchies, controller_list)) | |
1266 | goto next; | |
1267 | ||
a3926f6a | 1268 | mountpoint = cg_hybrid_get_mountpoint(line); |
ccb4cabe | 1269 | if (!mountpoint) { |
65d78313 | 1270 | CGFSNG_DEBUG("Failed parsing mountpoint from \"%s\"\n", line); |
d6337a5f | 1271 | goto next; |
ccb4cabe SH |
1272 | } |
1273 | ||
d6337a5f | 1274 | if (type == CGROUP_SUPER_MAGIC) |
a3926f6a | 1275 | base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, controller_list[0], CGROUP_SUPER_MAGIC); |
d6337a5f | 1276 | else |
a3926f6a | 1277 | base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, NULL, CGROUP2_SUPER_MAGIC); |
ccb4cabe | 1278 | if (!base_cgroup) { |
d6337a5f CB |
1279 | CGFSNG_DEBUG("Failed to find current cgroup\n"); |
1280 | goto next; | |
ccb4cabe | 1281 | } |
6328fd9c | 1282 | |
ccb4cabe SH |
1283 | trim(base_cgroup); |
1284 | prune_init_scope(base_cgroup); | |
d6337a5f | 1285 | if (type == CGROUP2_SUPER_MAGIC) |
6328fd9c CB |
1286 | writeable = test_writeable_v2(mountpoint, base_cgroup); |
1287 | else | |
1288 | writeable = test_writeable_v1(mountpoint, base_cgroup); | |
d6337a5f CB |
1289 | if (!writeable) |
1290 | goto next; | |
1291 | ||
1292 | if (type == CGROUP2_SUPER_MAGIC) { | |
1293 | char *cgv2_ctrl_path; | |
1294 | ||
1295 | cgv2_ctrl_path = must_make_path(mountpoint, base_cgroup, | |
1296 | "cgroup.controllers", | |
1297 | NULL); | |
1298 | ||
1299 | controller_list = cg_unified_get_controllers(cgv2_ctrl_path); | |
1300 | free(cgv2_ctrl_path); | |
1301 | if (!controller_list) | |
1302 | controller_list = cg_unified_make_empty_controller(); | |
ccb4cabe | 1303 | } |
d6337a5f CB |
1304 | new = add_hierarchy(controller_list, mountpoint, base_cgroup, type); |
1305 | if (type == CGROUP2_SUPER_MAGIC && !unified) | |
1306 | unified = new; | |
1307 | ||
1308 | continue; | |
1309 | ||
1310 | next: | |
1311 | free_string_list(controller_list); | |
1312 | free(mountpoint); | |
1313 | free(base_cgroup); | |
ccb4cabe SH |
1314 | } |
1315 | ||
1316 | free_string_list(klist); | |
1317 | free_string_list(nlist); | |
1318 | ||
1319 | free(basecginfo); | |
1320 | ||
1321 | fclose(f); | |
1322 | free(line); | |
1323 | ||
e4aeecf5 CB |
1324 | if (lxc_cgfsng_debug) { |
1325 | printf("writeable subsystems:\n"); | |
1326 | lxc_cgfsng_print_hierarchies(); | |
1327 | } | |
1328 | ||
ccb4cabe SH |
1329 | /* verify that all controllers in cgroup.use and all crucial |
1330 | * controllers are accounted for | |
1331 | */ | |
c2712f64 | 1332 | if (!all_controllers_found()) |
ccb4cabe SH |
1333 | return false; |
1334 | ||
1335 | return true; | |
1336 | } | |
1337 | ||
d6337a5f CB |
1338 | static int cg_is_pure_unified(void) { |
1339 | ||
1340 | int ret; | |
1341 | struct statfs fs; | |
1342 | ||
1343 | ret = statfs("/sys/fs/cgroup", &fs); | |
1344 | if (ret < 0) | |
1345 | return -ENOMEDIUM; | |
1346 | ||
1347 | if (is_fs_type(&fs, CGROUP2_SUPER_MAGIC)) | |
1348 | return CGROUP2_SUPER_MAGIC; | |
1349 | ||
1350 | return 0; | |
1351 | } | |
1352 | ||
1353 | /* Get current cgroup from /proc/self/cgroup for the cgroupfs v2 hierarchy. */ | |
a3926f6a | 1354 | static char *cg_unified_get_current_cgroup(void) |
457ca9aa | 1355 | { |
d6337a5f CB |
1356 | char *basecginfo; |
1357 | char *base_cgroup; | |
1358 | bool will_escape; | |
1359 | char *copy = NULL; | |
1360 | ||
1361 | will_escape = (geteuid() == 0); | |
1362 | if (will_escape) | |
1363 | basecginfo = read_file("/proc/1/cgroup"); | |
1364 | else | |
1365 | basecginfo = read_file("/proc/self/cgroup"); | |
1366 | if (!basecginfo) | |
1367 | return NULL; | |
1368 | ||
1369 | base_cgroup = strstr(basecginfo, "0::/"); | |
1370 | if (!base_cgroup) | |
1371 | goto cleanup_on_err; | |
1372 | ||
1373 | base_cgroup = base_cgroup + 3; | |
1374 | copy = copy_to_eol(base_cgroup); | |
1375 | if (!copy) | |
1376 | goto cleanup_on_err; | |
1377 | ||
1378 | cleanup_on_err: | |
1379 | free(basecginfo); | |
1380 | if (copy) | |
1381 | trim(copy); | |
1382 | ||
1383 | return copy; | |
1384 | } | |
1385 | ||
a3926f6a | 1386 | static int cg_unified_init(void) |
d6337a5f CB |
1387 | { |
1388 | int ret; | |
1389 | char *mountpoint, *subtree_path; | |
1390 | char **delegatable; | |
1391 | char *base_cgroup = NULL; | |
1392 | ||
1393 | ret = cg_is_pure_unified(); | |
1394 | if (ret == -ENOMEDIUM) | |
1395 | return -ENOMEDIUM; | |
1396 | ||
1397 | if (ret != CGROUP2_SUPER_MAGIC) | |
1398 | return 0; | |
1399 | ||
a3926f6a | 1400 | base_cgroup = cg_unified_get_current_cgroup(); |
d6337a5f CB |
1401 | if (!base_cgroup) |
1402 | return -EINVAL; | |
1403 | prune_init_scope(base_cgroup); | |
1404 | ||
1405 | /* We assume that we have already been given controllers to delegate | |
1406 | * further down the hierarchy. If not it is up to the user to delegate | |
1407 | * them to us. | |
1408 | */ | |
1409 | mountpoint = must_copy_string("/sys/fs/cgroup"); | |
1410 | subtree_path = must_make_path(mountpoint, base_cgroup, | |
1411 | "cgroup.subtree_control", NULL); | |
1412 | delegatable = cg_unified_get_controllers(subtree_path); | |
1413 | free(subtree_path); | |
1414 | if (!delegatable) | |
1415 | delegatable = cg_unified_make_empty_controller(); | |
1416 | if (!delegatable[0]) | |
1417 | CGFSNG_DEBUG("No controllers are enabled for delegation\n"); | |
1418 | ||
1419 | /* TODO: If the user requested specific controllers via lxc.cgroup.use | |
1420 | * we should verify here. The reason I'm not doing it right is that I'm | |
1421 | * not convinced that lxc.cgroup.use will be the future since it is a | |
1422 | * global property. I much rather have an option that lets you request | |
1423 | * controllers per container. | |
1424 | */ | |
1425 | ||
1426 | add_hierarchy(delegatable, mountpoint, base_cgroup, CGROUP2_SUPER_MAGIC); | |
1427 | unified = hierarchies[0]; | |
1428 | ||
1429 | cgroup_layout = CGROUP_LAYOUT_UNIFIED; | |
1430 | return CGROUP2_SUPER_MAGIC; | |
1431 | } | |
1432 | ||
1433 | static bool cg_init(void) | |
1434 | { | |
1435 | int ret; | |
457ca9aa | 1436 | const char *tmp; |
d6337a5f | 1437 | |
457ca9aa SH |
1438 | errno = 0; |
1439 | tmp = lxc_global_config_value("lxc.cgroup.use"); | |
1a0e70ac | 1440 | if (!cgroup_use && errno != 0) { /* lxc.cgroup.use can be NULL */ |
65d78313 | 1441 | CGFSNG_DEBUG("Failed to retrieve list of cgroups to use\n"); |
457ca9aa SH |
1442 | return false; |
1443 | } | |
1444 | cgroup_use = must_copy_string(tmp); | |
1445 | ||
a3926f6a | 1446 | ret = cg_unified_init(); |
d6337a5f CB |
1447 | if (ret < 0) |
1448 | return false; | |
1449 | ||
1450 | if (ret == CGROUP2_SUPER_MAGIC) | |
1451 | return true; | |
1452 | ||
a3926f6a | 1453 | return cg_hybrid_init(); |
457ca9aa SH |
1454 | } |
1455 | ||
43654d34 | 1456 | static void *cgfsng_init(struct lxc_handler *handler) |
ccb4cabe | 1457 | { |
457ca9aa | 1458 | const char *cgroup_pattern; |
43654d34 | 1459 | struct cgfsng_handler_data *d; |
ccb4cabe SH |
1460 | |
1461 | d = must_alloc(sizeof(*d)); | |
1462 | memset(d, 0, sizeof(*d)); | |
1463 | ||
43654d34 CB |
1464 | /* copy container name */ |
1465 | d->name = must_copy_string(handler->name); | |
1466 | ||
1467 | /* copy per-container cgroup information */ | |
ae5e6c08 CB |
1468 | d->cgroup_meta.dir = NULL; |
1469 | d->cgroup_meta.controllers = NULL; | |
9b5396f9 CB |
1470 | if (handler->conf) { |
1471 | d->cgroup_meta.dir = must_copy_string(handler->conf->cgroup_meta.dir); | |
1472 | d->cgroup_meta.controllers = must_copy_string(handler->conf->cgroup_meta.controllers); | |
1473 | } | |
ccb4cabe | 1474 | |
43654d34 | 1475 | /* copy system-wide cgroup information */ |
ccb4cabe | 1476 | cgroup_pattern = lxc_global_config_value("lxc.cgroup.pattern"); |
43654d34 CB |
1477 | if (!cgroup_pattern) { |
1478 | /* lxc.cgroup.pattern is only NULL on error. */ | |
ccb4cabe SH |
1479 | ERROR("Error getting cgroup pattern"); |
1480 | goto out_free; | |
1481 | } | |
1482 | d->cgroup_pattern = must_copy_string(cgroup_pattern); | |
1483 | ||
d6337a5f CB |
1484 | d->cgroup_layout = cgroup_layout; |
1485 | if (d->cgroup_layout == CGROUP_LAYOUT_LEGACY) | |
1486 | TRACE("Running with legacy cgroup layout"); | |
1487 | else if (d->cgroup_layout == CGROUP_LAYOUT_HYBRID) | |
1488 | TRACE("Running with hybrid cgroup layout"); | |
1489 | else if (d->cgroup_layout == CGROUP_LAYOUT_UNIFIED) | |
1490 | TRACE("Running with unified cgroup layout"); | |
1491 | else | |
1492 | WARN("Running with unknown cgroup layout"); | |
1493 | ||
e4aeecf5 CB |
1494 | if (lxc_cgfsng_debug) |
1495 | lxc_cgfsng_print_debuginfo(d); | |
ccb4cabe SH |
1496 | |
1497 | return d; | |
1498 | ||
1499 | out_free: | |
1500 | free_handler_data(d); | |
1501 | return NULL; | |
1502 | } | |
1503 | ||
bd8ef4e4 | 1504 | static int recursive_destroy(char *dirname) |
ccb4cabe | 1505 | { |
a17f8b3f | 1506 | int ret; |
74f96976 | 1507 | struct dirent *direntp; |
ccb4cabe SH |
1508 | DIR *dir; |
1509 | int r = 0; | |
1510 | ||
1511 | dir = opendir(dirname); | |
1512 | if (!dir) | |
1513 | return -1; | |
1514 | ||
74f96976 | 1515 | while ((direntp = readdir(dir))) { |
ccb4cabe | 1516 | char *pathname; |
a17f8b3f | 1517 | struct stat mystat; |
ccb4cabe | 1518 | |
ccb4cabe SH |
1519 | if (!strcmp(direntp->d_name, ".") || |
1520 | !strcmp(direntp->d_name, "..")) | |
1521 | continue; | |
1522 | ||
1523 | pathname = must_make_path(dirname, direntp->d_name, NULL); | |
1524 | ||
a17f8b3f CB |
1525 | ret = lstat(pathname, &mystat); |
1526 | if (ret < 0) { | |
ccb4cabe | 1527 | if (!r) |
a17f8b3f | 1528 | WARN("Failed to stat %s", pathname); |
ccb4cabe SH |
1529 | r = -1; |
1530 | goto next; | |
1531 | } | |
1532 | ||
1533 | if (!S_ISDIR(mystat.st_mode)) | |
1534 | goto next; | |
a17f8b3f | 1535 | |
bd8ef4e4 | 1536 | ret = recursive_destroy(pathname); |
a17f8b3f | 1537 | if (ret < 0) |
ccb4cabe | 1538 | r = -1; |
bd8ef4e4 | 1539 | next: |
ccb4cabe SH |
1540 | free(pathname); |
1541 | } | |
1542 | ||
a17f8b3f CB |
1543 | ret = rmdir(dirname); |
1544 | if (ret < 0) { | |
ccb4cabe | 1545 | if (!r) |
bd8ef4e4 CB |
1546 | WARN("%s - Failed to delete \"%s\"", strerror(errno), |
1547 | dirname); | |
ccb4cabe SH |
1548 | r = -1; |
1549 | } | |
1550 | ||
a17f8b3f CB |
1551 | ret = closedir(dir); |
1552 | if (ret < 0) { | |
ccb4cabe | 1553 | if (!r) |
bd8ef4e4 CB |
1554 | WARN("%s - Failed to delete \"%s\"", strerror(errno), |
1555 | dirname); | |
ccb4cabe SH |
1556 | r = -1; |
1557 | } | |
a17f8b3f | 1558 | |
ccb4cabe SH |
1559 | return r; |
1560 | } | |
1561 | ||
bd8ef4e4 CB |
1562 | static int cgroup_rmdir(char *container_cgroup) |
1563 | { | |
1564 | int i; | |
1565 | ||
1566 | if (!container_cgroup || !hierarchies) | |
1567 | return 0; | |
1568 | ||
1569 | for (i = 0; hierarchies[i]; i++) { | |
1570 | int ret; | |
1571 | struct hierarchy *h = hierarchies[i]; | |
1572 | ||
1573 | if (!h->fullcgpath) | |
1574 | continue; | |
1575 | ||
1576 | ret = recursive_destroy(h->fullcgpath); | |
1577 | if (ret < 0) | |
1578 | WARN("Failed to destroy \"%s\"", h->fullcgpath); | |
1579 | ||
1580 | free(h->fullcgpath); | |
1581 | h->fullcgpath = NULL; | |
1582 | } | |
1583 | ||
1584 | return 0; | |
1585 | } | |
1586 | ||
4160c3a0 CB |
1587 | struct generic_userns_exec_data { |
1588 | struct cgfsng_handler_data *d; | |
1589 | struct lxc_conf *conf; | |
1590 | uid_t origuid; /* target uid in parent namespace */ | |
1591 | char *path; | |
1592 | }; | |
1593 | ||
bd8ef4e4 | 1594 | static int cgroup_rmdir_wrapper(void *data) |
ccb4cabe | 1595 | { |
6efacf80 | 1596 | int ret; |
4160c3a0 CB |
1597 | struct generic_userns_exec_data *arg = data; |
1598 | uid_t nsuid = (arg->conf->root_nsuid_map != NULL) ? 0 : arg->conf->init_uid; | |
1599 | gid_t nsgid = (arg->conf->root_nsgid_map != NULL) ? 0 : arg->conf->init_gid; | |
ccb4cabe | 1600 | |
6efacf80 CB |
1601 | ret = setresgid(nsgid, nsgid, nsgid); |
1602 | if (ret < 0) { | |
1603 | SYSERROR("Failed to setresgid(%d, %d, %d)", (int)nsgid, | |
1604 | (int)nsgid, (int)nsgid); | |
1605 | return -1; | |
1606 | } | |
1607 | ||
1608 | ret = setresuid(nsuid, nsuid, nsuid); | |
1609 | if (ret < 0) { | |
1610 | SYSERROR("Failed to setresuid(%d, %d, %d)", (int)nsuid, | |
1611 | (int)nsuid, (int)nsuid); | |
1612 | return -1; | |
1613 | } | |
1614 | ||
1615 | ret = setgroups(0, NULL); | |
1616 | if (ret < 0 && errno != EPERM) { | |
1617 | SYSERROR("Failed to setgroups(0, NULL)"); | |
1618 | return -1; | |
1619 | } | |
ccb4cabe | 1620 | |
bd8ef4e4 | 1621 | return cgroup_rmdir(arg->d->container_cgroup); |
ccb4cabe SH |
1622 | } |
1623 | ||
bd8ef4e4 | 1624 | static void cgfsng_destroy(void *hdata, struct lxc_conf *conf) |
ccb4cabe | 1625 | { |
bd8ef4e4 CB |
1626 | int ret; |
1627 | struct cgfsng_handler_data *d = hdata; | |
4160c3a0 CB |
1628 | struct generic_userns_exec_data wrap; |
1629 | ||
bd8ef4e4 CB |
1630 | if (!d) |
1631 | return; | |
1632 | ||
4160c3a0 | 1633 | wrap.origuid = 0; |
bd8ef4e4 | 1634 | wrap.d = hdata; |
4160c3a0 CB |
1635 | wrap.conf = conf; |
1636 | ||
ccb4cabe | 1637 | if (conf && !lxc_list_empty(&conf->id_map)) |
bd8ef4e4 CB |
1638 | ret = userns_exec_1(conf, cgroup_rmdir_wrapper, &wrap, |
1639 | "cgroup_rmdir_wrapper"); | |
ccb4cabe | 1640 | else |
bd8ef4e4 CB |
1641 | ret = cgroup_rmdir(d->container_cgroup); |
1642 | if (ret < 0) { | |
1643 | WARN("Failed to destroy cgroups"); | |
ccb4cabe | 1644 | return; |
ccb4cabe SH |
1645 | } |
1646 | ||
1647 | free_handler_data(d); | |
1648 | } | |
1649 | ||
1650 | struct cgroup_ops *cgfsng_ops_init(void) | |
1651 | { | |
e4aeecf5 CB |
1652 | if (getenv("LXC_DEBUG_CGFSNG")) |
1653 | lxc_cgfsng_debug = true; | |
1654 | ||
d6337a5f | 1655 | if (!cg_init()) |
457ca9aa | 1656 | return NULL; |
e4aeecf5 | 1657 | |
ccb4cabe SH |
1658 | return &cgfsng_ops; |
1659 | } | |
1660 | ||
a3926f6a | 1661 | static bool cg_unified_create_cgroup(struct hierarchy *h, char *cgname) |
0c3deb94 CB |
1662 | { |
1663 | char **it; | |
1664 | size_t i, parts_len; | |
1665 | size_t full_len = 0; | |
1666 | char *add_controllers = NULL, *cgroup = NULL; | |
1667 | char **parts = NULL; | |
1668 | bool bret = false; | |
1669 | ||
1670 | if (h->version != CGROUP2_SUPER_MAGIC) | |
1671 | return true; | |
1672 | ||
1673 | if (!h->controllers) | |
1674 | return true; | |
1675 | ||
1676 | /* For now we simply enable all controllers that we have detected by | |
1677 | * creating a string like "+memory +pids +cpu +io". | |
1678 | * TODO: In the near future we might want to support "-<controller>" | |
1679 | * etc. but whether supporting semantics like this make sense will need | |
1680 | * some thinking. | |
1681 | */ | |
1682 | for (it = h->controllers; it && *it; it++) { | |
1683 | full_len += strlen(*it) + 2; | |
1684 | add_controllers = must_realloc(add_controllers, full_len + 1); | |
1685 | if (h->controllers[0] == *it) | |
1686 | add_controllers[0] = '\0'; | |
1687 | strcat(add_controllers, "+"); | |
1688 | strcat(add_controllers, *it); | |
1689 | if ((it + 1) && *(it + 1)) | |
1690 | strcat(add_controllers, " "); | |
1691 | } | |
1692 | ||
1693 | parts = lxc_string_split(cgname, '/'); | |
1694 | if (!parts) | |
1695 | goto on_error; | |
1696 | parts_len = lxc_array_len((void **)parts); | |
1697 | if (parts_len > 0) | |
1698 | parts_len--; | |
1699 | ||
1700 | cgroup = must_make_path(h->mountpoint, h->base_cgroup, NULL); | |
1701 | for (i = 0; i < parts_len; i++) { | |
1702 | int ret; | |
1703 | char *target; | |
1704 | ||
1705 | cgroup = must_append_path(cgroup, parts[i], NULL); | |
1706 | target = must_make_path(cgroup, "cgroup.subtree_control", NULL); | |
1707 | ret = lxc_write_to_file(target, add_controllers, full_len, false); | |
1708 | free(target); | |
1709 | if (ret < 0) { | |
1710 | SYSERROR("Could not enable \"%s\" controllers in the " | |
1711 | "unified cgroup \"%s\"", add_controllers, cgroup); | |
1712 | goto on_error; | |
1713 | } | |
1714 | } | |
1715 | ||
1716 | bret = true; | |
1717 | ||
1718 | on_error: | |
1719 | lxc_free_array((void **)parts, free); | |
1720 | free(add_controllers); | |
1721 | free(cgroup); | |
1722 | return bret; | |
1723 | } | |
1724 | ||
ccb4cabe SH |
1725 | static bool create_path_for_hierarchy(struct hierarchy *h, char *cgname) |
1726 | { | |
0c3deb94 CB |
1727 | int ret; |
1728 | ||
e3a3fecf | 1729 | h->fullcgpath = must_make_path(h->mountpoint, h->base_cgroup, cgname, NULL); |
1a0e70ac | 1730 | if (dir_exists(h->fullcgpath)) { /* it must not already exist */ |
0c3deb94 | 1731 | ERROR("cgroup \"%s\" already existed", h->fullcgpath); |
d8da679e | 1732 | return false; |
6f9584d8 | 1733 | } |
0c3deb94 | 1734 | |
a3926f6a | 1735 | if (!cg_legacy_handle_cpuset_hierarchy(h, cgname)) { |
0c3deb94 CB |
1736 | ERROR("Failed to handle cgroupfs v1 cpuset controller"); |
1737 | return false; | |
1738 | } | |
1739 | ||
1740 | ret = mkdir_p(h->fullcgpath, 0755); | |
1741 | if (ret < 0) { | |
1742 | ERROR("Failed to create cgroup \"%s\"", h->fullcgpath); | |
e3a3fecf | 1743 | return false; |
6f9584d8 | 1744 | } |
0c3deb94 | 1745 | |
a3926f6a | 1746 | return cg_unified_create_cgroup(h, cgname); |
ccb4cabe SH |
1747 | } |
1748 | ||
1749 | static void remove_path_for_hierarchy(struct hierarchy *h, char *cgname) | |
1750 | { | |
1751 | if (rmdir(h->fullcgpath) < 0) | |
1752 | SYSERROR("Failed to clean up cgroup %s from failed creation attempt", h->fullcgpath); | |
1753 | free(h->fullcgpath); | |
1754 | h->fullcgpath = NULL; | |
1755 | } | |
1756 | ||
1757 | /* | |
d30ec4cb | 1758 | * Try to create the same cgroup in all hierarchies. |
ccb4cabe SH |
1759 | * Start with cgroup_pattern; next cgroup_pattern-1, -2, ..., -999 |
1760 | */ | |
1761 | static inline bool cgfsng_create(void *hdata) | |
1762 | { | |
bb30b52a | 1763 | int i; |
ccb4cabe | 1764 | size_t len; |
0c3deb94 | 1765 | char *container_cgroup, *offset, *tmp; |
7d531e9b CB |
1766 | int idx = 0; |
1767 | struct cgfsng_handler_data *d = hdata; | |
ccb4cabe SH |
1768 | |
1769 | if (!d) | |
1770 | return false; | |
43654d34 | 1771 | |
ccb4cabe SH |
1772 | if (d->container_cgroup) { |
1773 | WARN("cgfsng_create called a second time"); | |
1774 | return false; | |
1775 | } | |
1776 | ||
43654d34 | 1777 | if (d->cgroup_meta.dir) |
7d531e9b | 1778 | tmp = lxc_string_join("/", (const char *[]){d->cgroup_meta.dir, d->name, NULL}, false); |
43654d34 CB |
1779 | else |
1780 | tmp = lxc_string_replace("%n", d->name, d->cgroup_pattern); | |
ccb4cabe SH |
1781 | if (!tmp) { |
1782 | ERROR("Failed expanding cgroup name pattern"); | |
1783 | return false; | |
1784 | } | |
1a0e70ac | 1785 | len = strlen(tmp) + 5; /* leave room for -NNN\0 */ |
0c3deb94 CB |
1786 | container_cgroup = must_alloc(len); |
1787 | strcpy(container_cgroup, tmp); | |
ccb4cabe | 1788 | free(tmp); |
0c3deb94 | 1789 | offset = container_cgroup + len - 5; |
ccb4cabe SH |
1790 | |
1791 | again: | |
95adfe93 SH |
1792 | if (idx == 1000) { |
1793 | ERROR("Too many conflicting cgroup names"); | |
ccb4cabe | 1794 | goto out_free; |
95adfe93 | 1795 | } |
66b66624 | 1796 | if (idx) { |
bb30b52a CB |
1797 | int ret; |
1798 | ||
66b66624 CB |
1799 | ret = snprintf(offset, 5, "-%d", idx); |
1800 | if (ret < 0 || (size_t)ret >= 5) { | |
1801 | FILE *f = fopen("/dev/null", "w"); | |
97ebced3 | 1802 | if (f) { |
66b66624 CB |
1803 | fprintf(f, "Workaround for GCC7 bug: " |
1804 | "https://gcc.gnu.org/bugzilla/" | |
1805 | "show_bug.cgi?id=78969"); | |
1806 | fclose(f); | |
1807 | } | |
1808 | } | |
1809 | } | |
457ca9aa | 1810 | for (i = 0; hierarchies[i]; i++) { |
0c3deb94 | 1811 | if (!create_path_for_hierarchy(hierarchies[i], container_cgroup)) { |
ccb4cabe | 1812 | int j; |
1a0e70ac | 1813 | ERROR("Failed to create \"%s\"", hierarchies[i]->fullcgpath); |
457ca9aa SH |
1814 | free(hierarchies[i]->fullcgpath); |
1815 | hierarchies[i]->fullcgpath = NULL; | |
ccb4cabe | 1816 | for (j = 0; j < i; j++) |
0c3deb94 | 1817 | remove_path_for_hierarchy(hierarchies[j], container_cgroup); |
ccb4cabe SH |
1818 | idx++; |
1819 | goto again; | |
1820 | } | |
1821 | } | |
1822 | /* Done */ | |
0c3deb94 | 1823 | d->container_cgroup = container_cgroup; |
ccb4cabe SH |
1824 | return true; |
1825 | ||
1826 | out_free: | |
0c3deb94 | 1827 | free(container_cgroup); |
ccb4cabe SH |
1828 | return false; |
1829 | } | |
1830 | ||
ccb4cabe SH |
1831 | static bool cgfsng_enter(void *hdata, pid_t pid) |
1832 | { | |
ccb4cabe SH |
1833 | char pidstr[25]; |
1834 | int i, len; | |
1835 | ||
1836 | len = snprintf(pidstr, 25, "%d", pid); | |
1837 | if (len < 0 || len > 25) | |
1838 | return false; | |
1839 | ||
457ca9aa SH |
1840 | for (i = 0; hierarchies[i]; i++) { |
1841 | char *fullpath = must_make_path(hierarchies[i]->fullcgpath, | |
ccb4cabe SH |
1842 | "cgroup.procs", NULL); |
1843 | if (lxc_write_to_file(fullpath, pidstr, len, false) != 0) { | |
d3b00a8f | 1844 | SYSERROR("Failed to enter %s", fullpath); |
ccb4cabe SH |
1845 | free(fullpath); |
1846 | return false; | |
1847 | } | |
1848 | free(fullpath); | |
1849 | } | |
1850 | ||
1851 | return true; | |
1852 | } | |
1853 | ||
6efacf80 CB |
1854 | static int chowmod(char *path, uid_t chown_uid, gid_t chown_gid, |
1855 | mode_t chmod_mode) | |
1856 | { | |
1857 | int ret; | |
1858 | ||
1859 | ret = chown(path, chown_uid, chown_gid); | |
1860 | if (ret < 0) { | |
1861 | WARN("%s - Failed to chown(%s, %d, %d)", strerror(errno), path, | |
1862 | (int)chown_uid, (int)chown_gid); | |
1863 | return -1; | |
1864 | } | |
1865 | ||
1866 | ret = chmod(path, chmod_mode); | |
1867 | if (ret < 0) { | |
1868 | WARN("%s - Failed to chmod(%s, %d)", strerror(errno), path, | |
1869 | (int)chmod_mode); | |
1870 | return -1; | |
1871 | } | |
1872 | ||
1873 | return 0; | |
1874 | } | |
1875 | ||
1876 | /* chgrp the container cgroups to container group. We leave | |
c0888dfe SH |
1877 | * the container owner as cgroup owner. So we must make the |
1878 | * directories 775 so that the container can create sub-cgroups. | |
43647298 SH |
1879 | * |
1880 | * Also chown the tasks and cgroup.procs files. Those may not | |
1881 | * exist depending on kernel version. | |
c0888dfe | 1882 | */ |
ccb4cabe SH |
1883 | static int chown_cgroup_wrapper(void *data) |
1884 | { | |
6efacf80 | 1885 | int i, ret; |
4160c3a0 CB |
1886 | uid_t destuid; |
1887 | struct generic_userns_exec_data *arg = data; | |
1888 | uid_t nsuid = (arg->conf->root_nsuid_map != NULL) ? 0 : arg->conf->init_uid; | |
1889 | gid_t nsgid = (arg->conf->root_nsgid_map != NULL) ? 0 : arg->conf->init_gid; | |
ccb4cabe | 1890 | |
6efacf80 CB |
1891 | ret = setresgid(nsgid, nsgid, nsgid); |
1892 | if (ret < 0) { | |
1893 | SYSERROR("Failed to setresgid(%d, %d, %d)", | |
1894 | (int)nsgid, (int)nsgid, (int)nsgid); | |
1895 | return -1; | |
1896 | } | |
1897 | ||
1898 | ret = setresuid(nsuid, nsuid, nsuid); | |
1899 | if (ret < 0) { | |
1900 | SYSERROR("Failed to setresuid(%d, %d, %d)", | |
1901 | (int)nsuid, (int)nsuid, (int)nsuid); | |
1902 | return -1; | |
1903 | } | |
1904 | ||
1905 | ret = setgroups(0, NULL); | |
1906 | if (ret < 0 && errno != EPERM) { | |
1907 | SYSERROR("Failed to setgroups(0, NULL)"); | |
1908 | return -1; | |
1909 | } | |
ccb4cabe SH |
1910 | |
1911 | destuid = get_ns_uid(arg->origuid); | |
1912 | ||
457ca9aa | 1913 | for (i = 0; hierarchies[i]; i++) { |
6efacf80 CB |
1914 | char *fullpath; |
1915 | char *path = hierarchies[i]->fullcgpath; | |
43647298 | 1916 | |
63e42fee | 1917 | ret = chowmod(path, destuid, nsgid, 0775); |
6efacf80 | 1918 | if (ret < 0) |
ccb4cabe | 1919 | return -1; |
c0888dfe | 1920 | |
6efacf80 CB |
1921 | /* Failures to chown() these are inconvenient but not |
1922 | * detrimental We leave these owned by the container launcher, | |
1923 | * so that container root can write to the files to attach. We | |
1924 | * chmod() them 664 so that container systemd can write to the | |
1925 | * files (which systemd in wily insists on doing). | |
ab8f5424 | 1926 | */ |
6efacf80 CB |
1927 | |
1928 | if (hierarchies[i]->version == CGROUP_SUPER_MAGIC) { | |
1929 | fullpath = must_make_path(path, "tasks", NULL); | |
1930 | (void)chowmod(fullpath, destuid, nsgid, 0664); | |
1931 | free(fullpath); | |
1932 | } | |
43647298 SH |
1933 | |
1934 | fullpath = must_make_path(path, "cgroup.procs", NULL); | |
6efacf80 | 1935 | (void)chowmod(fullpath, destuid, 0, 0664); |
ccb4cabe | 1936 | free(fullpath); |
0e17357c | 1937 | |
d6337a5f | 1938 | if (hierarchies[i]->version != CGROUP2_SUPER_MAGIC) |
0e17357c CB |
1939 | continue; |
1940 | ||
1941 | fullpath = must_make_path(path, "cgroup.subtree_control", NULL); | |
6efacf80 | 1942 | (void)chowmod(fullpath, destuid, nsgid, 0664); |
0e17357c CB |
1943 | free(fullpath); |
1944 | ||
1945 | fullpath = must_make_path(path, "cgroup.threads", NULL); | |
6efacf80 | 1946 | (void)chowmod(fullpath, destuid, nsgid, 0664); |
0e17357c | 1947 | free(fullpath); |
ccb4cabe SH |
1948 | } |
1949 | ||
1950 | return 0; | |
1951 | } | |
1952 | ||
058c1cb6 | 1953 | static bool cgfsng_chown(void *hdata, struct lxc_conf *conf) |
ccb4cabe SH |
1954 | { |
1955 | struct cgfsng_handler_data *d = hdata; | |
4160c3a0 | 1956 | struct generic_userns_exec_data wrap; |
ccb4cabe SH |
1957 | |
1958 | if (!d) | |
1959 | return false; | |
1960 | ||
1961 | if (lxc_list_empty(&conf->id_map)) | |
1962 | return true; | |
1963 | ||
ccb4cabe | 1964 | wrap.origuid = geteuid(); |
4160c3a0 CB |
1965 | wrap.path = NULL; |
1966 | wrap.d = d; | |
1967 | wrap.conf = conf; | |
ccb4cabe | 1968 | |
c9b7c33e CB |
1969 | if (userns_exec_1(conf, chown_cgroup_wrapper, &wrap, |
1970 | "chown_cgroup_wrapper") < 0) { | |
ccb4cabe SH |
1971 | ERROR("Error requesting cgroup chown in new namespace"); |
1972 | return false; | |
1973 | } | |
1974 | ||
1975 | return true; | |
1976 | } | |
1977 | ||
8aa1044f SH |
1978 | /* |
1979 | * We've safe-mounted a tmpfs as parent, so we don't need to protect against | |
1980 | * symlinks any more - just use mount | |
1981 | */ | |
1982 | ||
1983 | /* mount cgroup-full if requested */ | |
1984 | static int mount_cgroup_full(int type, struct hierarchy *h, char *dest, | |
a3926f6a | 1985 | char *container_cgroup) |
8aa1044f SH |
1986 | { |
1987 | if (type < LXC_AUTO_CGROUP_FULL_RO || type > LXC_AUTO_CGROUP_FULL_MIXED) | |
1988 | return 0; | |
1989 | if (mount(h->mountpoint, dest, "cgroup", MS_BIND, NULL) < 0) { | |
1990 | SYSERROR("Error bind-mounting %s cgroup onto %s", h->mountpoint, | |
1991 | dest); | |
1992 | return -1; | |
1993 | } | |
1994 | if (type != LXC_AUTO_CGROUP_FULL_RW) { | |
5b6f9369 SH |
1995 | unsigned long flags = MS_BIND | MS_NOSUID | MS_NOEXEC | MS_NODEV | |
1996 | MS_REMOUNT | MS_RDONLY; | |
1997 | if (mount(NULL, dest, "cgroup", flags, NULL) < 0) { | |
8aa1044f SH |
1998 | SYSERROR("Error remounting %s readonly", dest); |
1999 | return -1; | |
2000 | } | |
2001 | } | |
2002 | ||
2003 | INFO("Bind mounted %s onto %s", h->mountpoint, dest); | |
2004 | if (type != LXC_AUTO_CGROUP_FULL_MIXED) | |
2005 | return 0; | |
2006 | ||
2007 | /* mount just the container path rw */ | |
2008 | char *source = must_make_path(h->mountpoint, h->base_cgroup, container_cgroup, NULL); | |
5b6f9369 | 2009 | char *rwpath = must_make_path(dest, h->base_cgroup, container_cgroup, NULL); |
8aa1044f | 2010 | if (mount(source, rwpath, "cgroup", MS_BIND, NULL) < 0) |
13277ec4 | 2011 | WARN("Failed to mount %s read-write: %s", rwpath, |
2012 | strerror(errno)); | |
8aa1044f SH |
2013 | INFO("Made %s read-write", rwpath); |
2014 | free(rwpath); | |
2015 | free(source); | |
2016 | return 0; | |
2017 | } | |
2018 | ||
2019 | /* cgroup-full:* is done, no need to create subdirs */ | |
2020 | static bool cg_mount_needs_subdirs(int type) | |
2021 | { | |
2022 | if (type >= LXC_AUTO_CGROUP_FULL_RO) | |
2023 | return false; | |
a3926f6a | 2024 | |
8aa1044f SH |
2025 | return true; |
2026 | } | |
2027 | ||
886cac86 CB |
2028 | /* After $rootfs/sys/fs/container/controller/the/cg/path has been created, |
2029 | * remount controller ro if needed and bindmount the cgroupfs onto | |
2030 | * controll/the/cg/path. | |
8aa1044f | 2031 | */ |
a3926f6a CB |
2032 | static int do_secondstage_mounts_if_needed(int type, struct hierarchy *h, |
2033 | char *controllerpath, char *cgpath, | |
2034 | const char *container_cgroup) | |
8aa1044f | 2035 | { |
5285689c | 2036 | int ret, remount_flags; |
886cac86 CB |
2037 | char *sourcepath; |
2038 | int flags = MS_BIND; | |
2039 | ||
8aa1044f | 2040 | if (type == LXC_AUTO_CGROUP_RO || type == LXC_AUTO_CGROUP_MIXED) { |
886cac86 CB |
2041 | ret = mount(controllerpath, controllerpath, "cgroup", MS_BIND, NULL); |
2042 | if (ret < 0) { | |
2043 | SYSERROR("Failed to bind mount \"%s\" onto \"%s\"", | |
2044 | controllerpath, controllerpath); | |
8aa1044f SH |
2045 | return -1; |
2046 | } | |
886cac86 | 2047 | |
5285689c CB |
2048 | remount_flags = add_required_remount_flags(controllerpath, |
2049 | controllerpath, | |
2050 | flags | MS_REMOUNT); | |
886cac86 CB |
2051 | ret = mount(controllerpath, controllerpath, "cgroup", |
2052 | MS_REMOUNT | MS_BIND | MS_RDONLY, NULL); | |
2053 | if (ret < 0) { | |
2054 | SYSERROR("Failed to remount \"%s\" ro", controllerpath); | |
8aa1044f SH |
2055 | return -1; |
2056 | } | |
886cac86 | 2057 | |
8aa1044f SH |
2058 | INFO("Remounted %s read-only", controllerpath); |
2059 | } | |
886cac86 CB |
2060 | |
2061 | sourcepath = must_make_path(h->mountpoint, h->base_cgroup, | |
2062 | container_cgroup, NULL); | |
8aa1044f SH |
2063 | if (type == LXC_AUTO_CGROUP_RO) |
2064 | flags |= MS_RDONLY; | |
886cac86 CB |
2065 | |
2066 | ret = mount(sourcepath, cgpath, "cgroup", flags, NULL); | |
2067 | if (ret < 0) { | |
2068 | SYSERROR("Failed to mount \"%s\" onto \"%s\"", h->controllers[0], cgpath); | |
8aa1044f | 2069 | free(sourcepath); |
8aa1044f SH |
2070 | return -1; |
2071 | } | |
886cac86 | 2072 | INFO("Mounted \"%s\" onto \"%s\"", h->controllers[0], cgpath); |
f8c40ffa L |
2073 | |
2074 | if (flags & MS_RDONLY) { | |
5285689c CB |
2075 | remount_flags = add_required_remount_flags(sourcepath, cgpath, |
2076 | flags | MS_REMOUNT); | |
2077 | ret = mount(sourcepath, cgpath, "cgroup", remount_flags, NULL); | |
886cac86 CB |
2078 | if (ret < 0) { |
2079 | SYSERROR("Failed to remount \"%s\" ro", cgpath); | |
f8c40ffa | 2080 | free(sourcepath); |
f8c40ffa L |
2081 | return -1; |
2082 | } | |
5285689c | 2083 | INFO("Remounted %s read-only", cgpath); |
f8c40ffa L |
2084 | } |
2085 | ||
8aa1044f | 2086 | free(sourcepath); |
886cac86 | 2087 | INFO("Completed second stage cgroup automounts for \"%s\"", cgpath); |
8aa1044f SH |
2088 | return 0; |
2089 | } | |
2090 | ||
5285689c CB |
2091 | static int cg_mount_in_cgroup_namespace(int type, struct hierarchy *h, |
2092 | const char *controllerpath) | |
b635e92d CB |
2093 | { |
2094 | int ret; | |
2095 | char *controllers = NULL; | |
a760603e CB |
2096 | char *fstype = "cgroup2"; |
2097 | unsigned long flags = 0; | |
b635e92d | 2098 | |
a760603e CB |
2099 | flags |= MS_NOSUID; |
2100 | flags |= MS_NOEXEC; | |
2101 | flags |= MS_NODEV; | |
2102 | flags |= MS_RELATIME; | |
2103 | ||
2104 | if (type == LXC_AUTO_CGROUP_RO || type == LXC_AUTO_CGROUP_FULL_RO) | |
2105 | flags |= MS_RDONLY; | |
2106 | ||
d6337a5f | 2107 | if (h->version != CGROUP2_SUPER_MAGIC) { |
a760603e CB |
2108 | controllers = lxc_string_join(",", (const char **)h->controllers, false); |
2109 | if (!controllers) | |
2110 | return -ENOMEM; | |
2111 | fstype = "cgroup"; | |
b635e92d CB |
2112 | } |
2113 | ||
a760603e | 2114 | ret = mount("cgroup", controllerpath, fstype, flags, controllers); |
b635e92d CB |
2115 | free(controllers); |
2116 | if (ret < 0) { | |
a760603e | 2117 | SYSERROR("Failed to mount %s with cgroup filesystem type %s", controllerpath, fstype); |
b635e92d CB |
2118 | return -1; |
2119 | } | |
2120 | ||
a760603e | 2121 | DEBUG("Mounted %s with cgroup filesystem type %s", controllerpath, fstype); |
b635e92d CB |
2122 | return 0; |
2123 | } | |
2124 | ||
ccb4cabe SH |
2125 | static bool cgfsng_mount(void *hdata, const char *root, int type) |
2126 | { | |
3f69fb12 | 2127 | int i, ret; |
8aa1044f SH |
2128 | char *tmpfspath = NULL; |
2129 | bool retval = false; | |
b635e92d CB |
2130 | struct lxc_handler *handler = hdata; |
2131 | struct cgfsng_handler_data *d = handler->cgroup_data; | |
3f69fb12 | 2132 | bool has_cgns = false, wants_force_mount = false; |
8aa1044f SH |
2133 | |
2134 | if ((type & LXC_AUTO_CGROUP_MASK) == 0) | |
2135 | return true; | |
2136 | ||
3f69fb12 SY |
2137 | if (type & LXC_AUTO_CGROUP_FORCE) { |
2138 | type &= ~LXC_AUTO_CGROUP_FORCE; | |
2139 | wants_force_mount = true; | |
2140 | } | |
b635e92d | 2141 | |
3f69fb12 SY |
2142 | if (!wants_force_mount){ |
2143 | if (!lxc_list_empty(&handler->conf->keepcaps)) | |
2144 | wants_force_mount = !in_caplist(CAP_SYS_ADMIN, &handler->conf->keepcaps); | |
2145 | else | |
2146 | wants_force_mount = in_caplist(CAP_SYS_ADMIN, &handler->conf->caps); | |
2147 | } | |
8aa1044f | 2148 | |
3f69fb12 SY |
2149 | has_cgns = cgns_supported(); |
2150 | if (has_cgns && !wants_force_mount) | |
2151 | return true; | |
8aa1044f SH |
2152 | |
2153 | if (type == LXC_AUTO_CGROUP_NOSPEC) | |
2154 | type = LXC_AUTO_CGROUP_MIXED; | |
2155 | else if (type == LXC_AUTO_CGROUP_FULL_NOSPEC) | |
2156 | type = LXC_AUTO_CGROUP_FULL_MIXED; | |
2157 | ||
2158 | /* Mount tmpfs */ | |
3f69fb12 SY |
2159 | tmpfspath = must_make_path(root, "/sys/fs/cgroup", NULL); |
2160 | ret = safe_mount("cgroup_root", tmpfspath, "tmpfs", | |
2161 | MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, | |
2162 | "size=10240k,mode=755", root); | |
2163 | if (ret < 0) | |
2164 | goto on_error; | |
8aa1044f | 2165 | |
457ca9aa | 2166 | for (i = 0; hierarchies[i]; i++) { |
8aa1044f | 2167 | char *controllerpath, *path2; |
457ca9aa | 2168 | struct hierarchy *h = hierarchies[i]; |
8aa1044f | 2169 | char *controller = strrchr(h->mountpoint, '/'); |
8aa1044f SH |
2170 | |
2171 | if (!controller) | |
2172 | continue; | |
2173 | controller++; | |
2174 | controllerpath = must_make_path(tmpfspath, controller, NULL); | |
2175 | if (dir_exists(controllerpath)) { | |
2176 | free(controllerpath); | |
2177 | continue; | |
2178 | } | |
3f69fb12 SY |
2179 | ret = mkdir(controllerpath, 0755); |
2180 | if (ret < 0) { | |
8aa1044f SH |
2181 | SYSERROR("Error creating cgroup path: %s", controllerpath); |
2182 | free(controllerpath); | |
3f69fb12 | 2183 | goto on_error; |
8aa1044f | 2184 | } |
b635e92d | 2185 | |
3f69fb12 | 2186 | if (has_cgns && wants_force_mount) { |
b635e92d CB |
2187 | /* If cgroup namespaces are supported but the container |
2188 | * will not have CAP_SYS_ADMIN after it has started we | |
2189 | * need to mount the cgroups manually. | |
2190 | */ | |
3f69fb12 | 2191 | ret = cg_mount_in_cgroup_namespace(type, h, controllerpath); |
b635e92d | 2192 | free(controllerpath); |
3f69fb12 SY |
2193 | if (ret < 0) |
2194 | goto on_error; | |
2195 | ||
b635e92d CB |
2196 | continue; |
2197 | } | |
2198 | ||
3f69fb12 SY |
2199 | ret = mount_cgroup_full(type, h, controllerpath, d->container_cgroup); |
2200 | if (ret < 0) { | |
8aa1044f | 2201 | free(controllerpath); |
3f69fb12 | 2202 | goto on_error; |
8aa1044f | 2203 | } |
3f69fb12 | 2204 | |
8aa1044f SH |
2205 | if (!cg_mount_needs_subdirs(type)) { |
2206 | free(controllerpath); | |
2207 | continue; | |
2208 | } | |
3f69fb12 SY |
2209 | |
2210 | path2 = must_make_path(controllerpath, h->base_cgroup, | |
2211 | d->container_cgroup, NULL); | |
2212 | ret = mkdir_p(path2, 0755); | |
2213 | if (ret < 0) { | |
8aa1044f | 2214 | free(controllerpath); |
8e0c6620 | 2215 | free(path2); |
3f69fb12 | 2216 | goto on_error; |
8aa1044f | 2217 | } |
2f62fb00 | 2218 | |
3f69fb12 SY |
2219 | ret = do_secondstage_mounts_if_needed( |
2220 | type, h, controllerpath, path2, d->container_cgroup); | |
8aa1044f SH |
2221 | free(controllerpath); |
2222 | free(path2); | |
3f69fb12 SY |
2223 | if (ret < 0) |
2224 | goto on_error; | |
8aa1044f SH |
2225 | } |
2226 | retval = true; | |
2227 | ||
3f69fb12 | 2228 | on_error: |
8aa1044f SH |
2229 | free(tmpfspath); |
2230 | return retval; | |
ccb4cabe SH |
2231 | } |
2232 | ||
2233 | static int recursive_count_nrtasks(char *dirname) | |
2234 | { | |
74f96976 | 2235 | struct dirent *direntp; |
ccb4cabe SH |
2236 | DIR *dir; |
2237 | int count = 0, ret; | |
2238 | char *path; | |
2239 | ||
2240 | dir = opendir(dirname); | |
2241 | if (!dir) | |
2242 | return 0; | |
2243 | ||
74f96976 | 2244 | while ((direntp = readdir(dir))) { |
ccb4cabe SH |
2245 | struct stat mystat; |
2246 | ||
2247 | if (!direntp) | |
2248 | break; | |
2249 | ||
2250 | if (!strcmp(direntp->d_name, ".") || | |
2251 | !strcmp(direntp->d_name, "..")) | |
2252 | continue; | |
2253 | ||
2254 | path = must_make_path(dirname, direntp->d_name, NULL); | |
2255 | ||
2256 | if (lstat(path, &mystat)) | |
2257 | goto next; | |
2258 | ||
2259 | if (!S_ISDIR(mystat.st_mode)) | |
2260 | goto next; | |
2261 | ||
2262 | count += recursive_count_nrtasks(path); | |
2263 | next: | |
2264 | free(path); | |
2265 | } | |
2266 | ||
2267 | path = must_make_path(dirname, "cgroup.procs", NULL); | |
2268 | ret = lxc_count_file_lines(path); | |
2269 | if (ret != -1) | |
2270 | count += ret; | |
2271 | free(path); | |
2272 | ||
2273 | (void) closedir(dir); | |
2274 | ||
2275 | return count; | |
2276 | } | |
2277 | ||
2278 | static int cgfsng_nrtasks(void *hdata) { | |
2279 | struct cgfsng_handler_data *d = hdata; | |
2280 | char *path; | |
2281 | int count; | |
2282 | ||
457ca9aa | 2283 | if (!d || !d->container_cgroup || !hierarchies) |
ccb4cabe | 2284 | return -1; |
a3926f6a | 2285 | |
457ca9aa | 2286 | path = must_make_path(hierarchies[0]->fullcgpath, NULL); |
ccb4cabe SH |
2287 | count = recursive_count_nrtasks(path); |
2288 | free(path); | |
2289 | return count; | |
2290 | } | |
2291 | ||
2292 | /* Only root needs to escape to the cgroup of its init */ | |
7103fe6f | 2293 | static bool cgfsng_escape() |
ccb4cabe | 2294 | { |
ccb4cabe SH |
2295 | int i; |
2296 | ||
2297 | if (geteuid()) | |
2298 | return true; | |
2299 | ||
457ca9aa SH |
2300 | for (i = 0; hierarchies[i]; i++) { |
2301 | char *fullpath = must_make_path(hierarchies[i]->mountpoint, | |
2302 | hierarchies[i]->base_cgroup, | |
ccb4cabe SH |
2303 | "cgroup.procs", NULL); |
2304 | if (lxc_write_to_file(fullpath, "0", 2, false) != 0) { | |
d3b00a8f | 2305 | SYSERROR("Failed to escape to %s", fullpath); |
ccb4cabe | 2306 | free(fullpath); |
6df334d1 | 2307 | return false; |
ccb4cabe SH |
2308 | } |
2309 | free(fullpath); | |
2310 | } | |
2311 | ||
6df334d1 | 2312 | return true; |
ccb4cabe SH |
2313 | } |
2314 | ||
36662416 TA |
2315 | static int cgfsng_num_hierarchies(void) |
2316 | { | |
2317 | int i; | |
2318 | ||
2319 | for (i = 0; hierarchies[i]; i++) | |
2320 | ; | |
2321 | ||
2322 | return i; | |
2323 | } | |
2324 | ||
2325 | static bool cgfsng_get_hierarchies(int n, char ***out) | |
2326 | { | |
2327 | int i; | |
2328 | ||
2329 | /* sanity check n */ | |
6b38e644 | 2330 | for (i = 0; i < n; i++) |
36662416 TA |
2331 | if (!hierarchies[i]) |
2332 | return false; | |
36662416 TA |
2333 | |
2334 | *out = hierarchies[i]->controllers; | |
2335 | ||
2336 | return true; | |
2337 | } | |
2338 | ||
ccb4cabe SH |
2339 | #define THAWED "THAWED" |
2340 | #define THAWED_LEN (strlen(THAWED)) | |
2341 | ||
d6337a5f CB |
2342 | /* TODO: If the unified cgroup hierarchy grows a freezer controller this needs |
2343 | * to be adapted. | |
2344 | */ | |
ccb4cabe SH |
2345 | static bool cgfsng_unfreeze(void *hdata) |
2346 | { | |
d6337a5f | 2347 | int ret; |
ccb4cabe | 2348 | char *fullpath; |
d6337a5f | 2349 | struct hierarchy *h; |
ccb4cabe | 2350 | |
d6337a5f | 2351 | h = get_hierarchy("freezer"); |
457ca9aa | 2352 | if (!h) |
ccb4cabe | 2353 | return false; |
d6337a5f | 2354 | |
ccb4cabe | 2355 | fullpath = must_make_path(h->fullcgpath, "freezer.state", NULL); |
d6337a5f | 2356 | ret = lxc_write_to_file(fullpath, THAWED, THAWED_LEN, false); |
ccb4cabe | 2357 | free(fullpath); |
d6337a5f CB |
2358 | if (ret < 0) |
2359 | return false; | |
2360 | ||
ccb4cabe SH |
2361 | return true; |
2362 | } | |
2363 | ||
2364 | static const char *cgfsng_get_cgroup(void *hdata, const char *subsystem) | |
2365 | { | |
d6337a5f CB |
2366 | struct hierarchy *h; |
2367 | ||
2368 | h = get_hierarchy(subsystem); | |
ccb4cabe SH |
2369 | if (!h) |
2370 | return NULL; | |
2371 | ||
371f834d SH |
2372 | return h->fullcgpath ? h->fullcgpath + strlen(h->mountpoint) : NULL; |
2373 | } | |
2374 | ||
2375 | /* | |
2376 | * Given a cgroup path returned from lxc_cmd_get_cgroup_path, build a | |
2377 | * full path, which must be freed by the caller. | |
2378 | */ | |
2379 | static char *build_full_cgpath_from_monitorpath(struct hierarchy *h, | |
2380 | const char *inpath, | |
2381 | const char *filename) | |
2382 | { | |
371f834d | 2383 | return must_make_path(h->mountpoint, inpath, filename, NULL); |
ccb4cabe SH |
2384 | } |
2385 | ||
c2aed66d CB |
2386 | /* Technically, we're always at a delegation boundary here. (This is especially |
2387 | * true when cgroup namespaces are available.) The reasoning is that in order | |
2388 | * for us to have been able to start a container in the first place the root | |
2389 | * cgroup must have been a leaf node. Now, either the container's init system | |
2390 | * has populated the cgroup and kept it as a leaf node or it has created | |
2391 | * subtrees. In the former case we will simply attach to the leaf node we | |
2392 | * created when we started the container in the latter case we create our own | |
2393 | * cgroup for the attaching process. | |
2394 | */ | |
a3926f6a CB |
2395 | static int __cg_unified_attach(const struct hierarchy *h, const char *name, |
2396 | const char *lxcpath, const char *pidstr, | |
2397 | size_t pidstr_len, const char *controller) | |
c2aed66d CB |
2398 | { |
2399 | int ret; | |
2400 | size_t len; | |
2401 | int fret = -1, idx = 0; | |
2402 | char *base_path = NULL, *container_cgroup = NULL, *full_path = NULL; | |
2403 | ||
2404 | container_cgroup = lxc_cmd_get_cgroup_path(name, lxcpath, controller); | |
2405 | /* not running */ | |
2406 | if (!container_cgroup) | |
2407 | return 0; | |
2408 | ||
2409 | base_path = must_make_path(h->mountpoint, container_cgroup, NULL); | |
2410 | full_path = must_make_path(base_path, "cgroup.procs", NULL); | |
2411 | /* cgroup is populated */ | |
2412 | ret = lxc_write_to_file(full_path, pidstr, pidstr_len, false); | |
2413 | if (ret < 0 && errno != EBUSY) | |
2414 | goto on_error; | |
2415 | ||
2416 | if (ret == 0) | |
2417 | goto on_success; | |
2418 | ||
2419 | free(full_path); | |
2420 | ||
2421 | len = strlen(base_path) + sizeof("/lxc-1000") - 1 + | |
2422 | sizeof("/cgroup-procs") - 1; | |
2423 | full_path = must_alloc(len + 1); | |
2424 | do { | |
2425 | if (idx) | |
2426 | ret = snprintf(full_path, len + 1, "%s/lxc-%d", | |
2427 | base_path, idx); | |
2428 | else | |
2429 | ret = snprintf(full_path, len + 1, "%s/lxc", base_path); | |
2430 | if (ret < 0 || (size_t)ret >= len + 1) | |
2431 | goto on_error; | |
2432 | ||
2433 | ret = mkdir_p(full_path, 0755); | |
2434 | if (ret < 0 && errno != EEXIST) | |
2435 | goto on_error; | |
2436 | ||
2437 | strcat(full_path, "/cgroup.procs"); | |
2438 | ret = lxc_write_to_file(full_path, pidstr, len, false); | |
2439 | if (ret == 0) | |
2440 | goto on_success; | |
2441 | ||
2442 | /* this is a non-leaf node */ | |
2443 | if (errno != EBUSY) | |
2444 | goto on_error; | |
2445 | ||
2446 | } while (++idx > 0 && idx < 1000); | |
2447 | ||
2448 | on_success: | |
2449 | if (idx < 1000) | |
2450 | fret = 0; | |
2451 | ||
2452 | on_error: | |
2453 | free(base_path); | |
2454 | free(container_cgroup); | |
2455 | free(full_path); | |
2456 | ||
2457 | return fret; | |
2458 | } | |
2459 | ||
ccb4cabe SH |
2460 | static bool cgfsng_attach(const char *name, const char *lxcpath, pid_t pid) |
2461 | { | |
c2aed66d | 2462 | int i, len, ret; |
ccb4cabe | 2463 | char pidstr[25]; |
ccb4cabe SH |
2464 | |
2465 | len = snprintf(pidstr, 25, "%d", pid); | |
2466 | if (len < 0 || len > 25) | |
2467 | return false; | |
2468 | ||
457ca9aa | 2469 | for (i = 0; hierarchies[i]; i++) { |
c2aed66d CB |
2470 | char *path; |
2471 | char *fullpath = NULL; | |
457ca9aa | 2472 | struct hierarchy *h = hierarchies[i]; |
ccb4cabe | 2473 | |
c2aed66d | 2474 | if (h->version == CGROUP2_SUPER_MAGIC) { |
a3926f6a CB |
2475 | ret = __cg_unified_attach(h, name, lxcpath, pidstr, len, |
2476 | h->controllers[0]); | |
c2aed66d CB |
2477 | if (ret < 0) |
2478 | return false; | |
2479 | ||
2480 | continue; | |
2481 | } | |
2482 | ||
ccb4cabe | 2483 | path = lxc_cmd_get_cgroup_path(name, lxcpath, h->controllers[0]); |
c2aed66d CB |
2484 | /* not running */ |
2485 | if (!path) | |
ccb4cabe SH |
2486 | continue; |
2487 | ||
371f834d | 2488 | fullpath = build_full_cgpath_from_monitorpath(h, path, "cgroup.procs"); |
c2aed66d CB |
2489 | ret = lxc_write_to_file(fullpath, pidstr, len, false); |
2490 | if (ret < 0) { | |
ccb4cabe SH |
2491 | SYSERROR("Failed to attach %d to %s", (int)pid, fullpath); |
2492 | free(fullpath); | |
ccb4cabe SH |
2493 | return false; |
2494 | } | |
ccb4cabe SH |
2495 | free(fullpath); |
2496 | } | |
2497 | ||
ccb4cabe SH |
2498 | return true; |
2499 | } | |
2500 | ||
2501 | /* | |
2502 | * Called externally (i.e. from 'lxc-cgroup') to query cgroup limits. | |
2503 | * Here we don't have a cgroup_data set up, so we ask the running | |
2504 | * container through the commands API for the cgroup path | |
2505 | */ | |
0069cc61 CB |
2506 | static int cgfsng_get(const char *filename, char *value, size_t len, |
2507 | const char *name, const char *lxcpath) | |
ccb4cabe | 2508 | { |
ccb4cabe | 2509 | int ret = -1; |
0069cc61 CB |
2510 | size_t controller_len; |
2511 | char *controller, *p, *path; | |
2512 | struct hierarchy *h; | |
ccb4cabe | 2513 | |
0069cc61 CB |
2514 | controller_len = strlen(filename); |
2515 | controller = alloca(controller_len + 1); | |
2516 | strcpy(controller, filename); | |
2517 | p = strchr(controller, '.'); | |
2518 | if (p) | |
ccb4cabe SH |
2519 | *p = '\0'; |
2520 | ||
0069cc61 CB |
2521 | path = lxc_cmd_get_cgroup_path(name, lxcpath, controller); |
2522 | /* not running */ | |
2523 | if (!path) | |
ccb4cabe SH |
2524 | return -1; |
2525 | ||
0069cc61 | 2526 | h = get_hierarchy(controller); |
ccb4cabe | 2527 | if (h) { |
0069cc61 CB |
2528 | char *fullpath; |
2529 | ||
2530 | fullpath = build_full_cgpath_from_monitorpath(h, path, filename); | |
ccb4cabe SH |
2531 | ret = lxc_read_from_file(fullpath, value, len); |
2532 | free(fullpath); | |
2533 | } | |
ccb4cabe SH |
2534 | free(path); |
2535 | ||
2536 | return ret; | |
2537 | } | |
2538 | ||
2539 | /* | |
2540 | * Called externally (i.e. from 'lxc-cgroup') to set new cgroup limits. | |
2541 | * Here we don't have a cgroup_data set up, so we ask the running | |
2542 | * container through the commands API for the cgroup path | |
2543 | */ | |
87777968 CB |
2544 | static int cgfsng_set(const char *filename, const char *value, const char *name, |
2545 | const char *lxcpath) | |
ccb4cabe | 2546 | { |
ccb4cabe | 2547 | int ret = -1; |
87777968 CB |
2548 | size_t controller_len; |
2549 | char *controller, *p, *path; | |
2550 | struct hierarchy *h; | |
ccb4cabe | 2551 | |
87777968 CB |
2552 | controller_len = strlen(filename); |
2553 | controller = alloca(controller_len + 1); | |
2554 | strcpy(controller, filename); | |
2555 | p = strchr(controller, '.'); | |
2556 | if (p) | |
ccb4cabe SH |
2557 | *p = '\0'; |
2558 | ||
87777968 CB |
2559 | path = lxc_cmd_get_cgroup_path(name, lxcpath, controller); |
2560 | /* not running */ | |
2561 | if (!path) | |
ccb4cabe SH |
2562 | return -1; |
2563 | ||
87777968 | 2564 | h = get_hierarchy(controller); |
ccb4cabe | 2565 | if (h) { |
87777968 CB |
2566 | char *fullpath; |
2567 | ||
2568 | fullpath = build_full_cgpath_from_monitorpath(h, path, filename); | |
ccb4cabe SH |
2569 | ret = lxc_write_to_file(fullpath, value, strlen(value), false); |
2570 | free(fullpath); | |
2571 | } | |
ccb4cabe SH |
2572 | free(path); |
2573 | ||
2574 | return ret; | |
2575 | } | |
2576 | ||
72add155 SH |
2577 | /* |
2578 | * take devices cgroup line | |
2579 | * /dev/foo rwx | |
2580 | * and convert it to a valid | |
2581 | * type major:minor mode | |
2582 | * line. Return <0 on error. Dest is a preallocated buffer | |
2583 | * long enough to hold the output. | |
2584 | */ | |
2585 | static int convert_devpath(const char *invalue, char *dest) | |
2586 | { | |
2a06d041 CB |
2587 | int n_parts; |
2588 | char *p, *path, type; | |
72add155 SH |
2589 | struct stat sb; |
2590 | unsigned long minor, major; | |
2a06d041 CB |
2591 | int ret = -EINVAL; |
2592 | char *mode = NULL; | |
72add155 SH |
2593 | |
2594 | path = must_copy_string(invalue); | |
2595 | ||
2596 | /* | |
2597 | * read path followed by mode; ignore any trailing text. | |
2598 | * A ' # comment' would be legal. Technically other text | |
2599 | * is not legal, we could check for that if we cared to | |
2600 | */ | |
2601 | for (n_parts = 1, p = path; *p && n_parts < 3; p++) { | |
2c2d6c49 SH |
2602 | if (*p != ' ') |
2603 | continue; | |
2604 | *p = '\0'; | |
2605 | if (n_parts != 1) | |
2606 | break; | |
2607 | p++; | |
2608 | n_parts++; | |
2609 | while (*p == ' ') | |
2610 | p++; | |
2611 | mode = p; | |
2612 | if (*p == '\0') | |
2613 | goto out; | |
72add155 | 2614 | } |
2c2d6c49 SH |
2615 | |
2616 | if (n_parts == 1) | |
72add155 | 2617 | goto out; |
72add155 SH |
2618 | |
2619 | ret = stat(path, &sb); | |
2620 | if (ret < 0) | |
2621 | goto out; | |
2622 | ||
72add155 SH |
2623 | mode_t m = sb.st_mode & S_IFMT; |
2624 | switch (m) { | |
2625 | case S_IFBLK: | |
2626 | type = 'b'; | |
2627 | break; | |
2628 | case S_IFCHR: | |
2629 | type = 'c'; | |
2630 | break; | |
2c2d6c49 | 2631 | default: |
72add155 SH |
2632 | ERROR("Unsupported device type %i for %s", m, path); |
2633 | ret = -EINVAL; | |
2634 | goto out; | |
2635 | } | |
2c2d6c49 SH |
2636 | |
2637 | major = MAJOR(sb.st_rdev); | |
2638 | minor = MINOR(sb.st_rdev); | |
2639 | ret = snprintf(dest, 50, "%c %lu:%lu %s", type, major, minor, mode); | |
72add155 | 2640 | if (ret < 0 || ret >= 50) { |
2a06d041 CB |
2641 | ERROR("Error on configuration value \"%c %lu:%lu %s\" (max 50 " |
2642 | "chars)", type, major, minor, mode); | |
72add155 SH |
2643 | ret = -ENAMETOOLONG; |
2644 | goto out; | |
2645 | } | |
2646 | ret = 0; | |
2647 | ||
2648 | out: | |
2649 | free(path); | |
2650 | return ret; | |
2651 | } | |
2652 | ||
ccb4cabe SH |
2653 | /* |
2654 | * Called from setup_limits - here we have the container's cgroup_data because | |
2655 | * we created the cgroups | |
2656 | */ | |
a3926f6a CB |
2657 | static int cg_legacy_set_data(const char *filename, const char *value, |
2658 | struct cgfsng_handler_data *d) | |
ccb4cabe | 2659 | { |
b3646d7e | 2660 | char *fullpath, *p; |
ab1a6cac | 2661 | size_t len; |
1a0e70ac CB |
2662 | /* "b|c <2^64-1>:<2^64-1> r|w|m" = 47 chars max */ |
2663 | char converted_value[50]; | |
b3646d7e CB |
2664 | struct hierarchy *h; |
2665 | int ret = 0; | |
2666 | char *controller = NULL; | |
ccb4cabe | 2667 | |
ab1a6cac CB |
2668 | len = strlen(filename); |
2669 | controller = alloca(len + 1); | |
b3646d7e | 2670 | strcpy(controller, filename); |
ab1a6cac CB |
2671 | p = strchr(controller, '.'); |
2672 | if (p) | |
ccb4cabe SH |
2673 | *p = '\0'; |
2674 | ||
c8bf519d | 2675 | if (strcmp("devices.allow", filename) == 0 && value[0] == '/') { |
72add155 SH |
2676 | ret = convert_devpath(value, converted_value); |
2677 | if (ret < 0) | |
c8bf519d | 2678 | return ret; |
72add155 | 2679 | value = converted_value; |
c8bf519d | 2680 | } |
2681 | ||
b3646d7e CB |
2682 | h = get_hierarchy(controller); |
2683 | if (!h) { | |
2684 | ERROR("Failed to setup limits for the \"%s\" controller. " | |
2685 | "The controller seems to be unused by \"cgfsng\" cgroup " | |
2686 | "driver or not enabled on the cgroup hierarchy", | |
2687 | controller); | |
d1953b26 | 2688 | errno = ENOENT; |
ab1a6cac | 2689 | return -ENOENT; |
ccb4cabe | 2690 | } |
b3646d7e CB |
2691 | |
2692 | fullpath = must_make_path(h->fullcgpath, filename, NULL); | |
2693 | ret = lxc_write_to_file(fullpath, value, strlen(value), false); | |
2694 | free(fullpath); | |
ccb4cabe SH |
2695 | return ret; |
2696 | } | |
2697 | ||
a3926f6a CB |
2698 | static bool __cg_legacy_setup_limits(void *hdata, |
2699 | struct lxc_list *cgroup_settings, | |
2700 | bool do_devices) | |
ccb4cabe SH |
2701 | { |
2702 | struct cgfsng_handler_data *d = hdata; | |
2703 | struct lxc_list *iterator, *sorted_cgroup_settings, *next; | |
2704 | struct lxc_cgroup *cg; | |
ccb4cabe SH |
2705 | bool ret = false; |
2706 | ||
2707 | if (lxc_list_empty(cgroup_settings)) | |
2708 | return true; | |
2709 | ||
2710 | sorted_cgroup_settings = sort_cgroup_settings(cgroup_settings); | |
6b38e644 | 2711 | if (!sorted_cgroup_settings) |
ccb4cabe | 2712 | return false; |
ccb4cabe | 2713 | |
ccb4cabe SH |
2714 | lxc_list_for_each(iterator, sorted_cgroup_settings) { |
2715 | cg = iterator->elem; | |
2716 | ||
2717 | if (do_devices == !strncmp("devices", cg->subsystem, 7)) { | |
a3926f6a | 2718 | if (cg_legacy_set_data(cg->subsystem, cg->value, d)) { |
ccb4cabe SH |
2719 | if (do_devices && (errno == EACCES || errno == EPERM)) { |
2720 | WARN("Error setting %s to %s for %s", | |
2721 | cg->subsystem, cg->value, d->name); | |
2722 | continue; | |
2723 | } | |
2724 | SYSERROR("Error setting %s to %s for %s", | |
2725 | cg->subsystem, cg->value, d->name); | |
2726 | goto out; | |
2727 | } | |
6a628f4a | 2728 | DEBUG("cgroup '%s' set to '%s'", cg->subsystem, cg->value); |
ccb4cabe | 2729 | } |
ccb4cabe SH |
2730 | } |
2731 | ||
2732 | ret = true; | |
6b38e644 | 2733 | INFO("Limits for the legacy cgroup hierarchies have been setup"); |
ccb4cabe | 2734 | out: |
ccb4cabe SH |
2735 | lxc_list_for_each_safe(iterator, sorted_cgroup_settings, next) { |
2736 | lxc_list_del(iterator); | |
2737 | free(iterator); | |
2738 | } | |
2739 | free(sorted_cgroup_settings); | |
2740 | return ret; | |
2741 | } | |
2742 | ||
a3926f6a CB |
2743 | static bool __cg_unified_setup_limits(void *hdata, |
2744 | struct lxc_list *cgroup_settings) | |
6b38e644 CB |
2745 | { |
2746 | struct lxc_list *iterator; | |
2747 | struct hierarchy *h = unified; | |
2748 | ||
2749 | if (lxc_list_empty(cgroup_settings)) | |
2750 | return true; | |
2751 | ||
2752 | if (!h) | |
2753 | return false; | |
2754 | ||
2755 | lxc_list_for_each(iterator, cgroup_settings) { | |
2756 | int ret; | |
2757 | char *fullpath; | |
2758 | struct lxc_cgroup *cg = iterator->elem; | |
2759 | ||
2760 | fullpath = must_make_path(h->fullcgpath, cg->subsystem, NULL); | |
2761 | ret = lxc_write_to_file(fullpath, cg->value, strlen(cg->value), false); | |
2762 | free(fullpath); | |
2763 | if (ret < 0) { | |
2764 | SYSERROR("Failed to set \"%s\" to \"%s\"", cg->subsystem, cg->value); | |
2765 | return false; | |
2766 | } | |
2767 | TRACE("Set \"%s\" to \"%s\"", cg->subsystem, cg->value); | |
2768 | } | |
2769 | ||
2770 | INFO("Limits for the unified cgroup hierarchy have been setup"); | |
2771 | return true; | |
2772 | } | |
2773 | ||
2774 | static bool cgfsng_setup_limits(void *hdata, struct lxc_conf *conf, | |
2775 | bool do_devices) | |
2776 | { | |
2777 | bool bret; | |
2778 | ||
a3926f6a | 2779 | bret = __cg_legacy_setup_limits(hdata, &conf->cgroup, do_devices); |
6b38e644 CB |
2780 | if (!bret) |
2781 | return false; | |
2782 | ||
a3926f6a | 2783 | return __cg_unified_setup_limits(hdata, &conf->cgroup2); |
6b38e644 CB |
2784 | } |
2785 | ||
ccb4cabe SH |
2786 | static struct cgroup_ops cgfsng_ops = { |
2787 | .init = cgfsng_init, | |
2788 | .destroy = cgfsng_destroy, | |
2789 | .create = cgfsng_create, | |
2790 | .enter = cgfsng_enter, | |
ccb4cabe | 2791 | .escape = cgfsng_escape, |
36662416 TA |
2792 | .num_hierarchies = cgfsng_num_hierarchies, |
2793 | .get_hierarchies = cgfsng_get_hierarchies, | |
ccb4cabe SH |
2794 | .get_cgroup = cgfsng_get_cgroup, |
2795 | .get = cgfsng_get, | |
2796 | .set = cgfsng_set, | |
2797 | .unfreeze = cgfsng_unfreeze, | |
2798 | .setup_limits = cgfsng_setup_limits, | |
2799 | .name = "cgroupfs-ng", | |
2800 | .attach = cgfsng_attach, | |
058c1cb6 | 2801 | .chown = cgfsng_chown, |
ccb4cabe SH |
2802 | .mount_cgroup = cgfsng_mount, |
2803 | .nrtasks = cgfsng_nrtasks, | |
2804 | .driver = CGFSNG, | |
2805 | ||
2806 | /* unsupported */ | |
2807 | .create_legacy = NULL, | |
2808 | }; |