]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/cgfs.c
log: Drop trailing \n from log messages
[mirror_lxc.git] / src / lxc / cgfs.c
CommitLineData
576f946d 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
576f946d 8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
576f946d 22 */
d06245b8
NC
23#include "config.h"
24
576f946d 25#include <stdio.h>
576f946d 26#include <stdlib.h>
27#include <errno.h>
576f946d 28#include <unistd.h>
29#include <string.h>
341a9bd8 30#include <dirent.h>
576f946d 31#include <fcntl.h>
b98f7d6e 32#include <ctype.h>
576f946d 33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/param.h>
36#include <sys/inotify.h>
aae1f3c4 37#include <sys/mount.h>
576f946d 38#include <netinet/in.h>
39#include <net/if.h>
40
e2bcd7db 41#include "error.h"
ae5c8b8e 42#include "commands.h"
b98f7d6e
SH
43#include "list.h"
44#include "conf.h"
33ad9f1a 45#include "utils.h"
740d1928 46#include "bdev.h"
f2363e38
ÇO
47#include "log.h"
48#include "cgroup.h"
49#include "start.h"
484ed030 50#include "state.h"
36eb9bde 51
edaf8b1b
SG
52#if IS_BIONIC
53#include <../include/lxcmntent.h>
54#else
55#include <mntent.h>
56#endif
57
4fb3cba5
DE
58struct cgroup_hierarchy;
59struct cgroup_meta_data;
60struct cgroup_mount_point;
61
62/*
63 * cgroup_meta_data: the metadata about the cgroup infrastructure on this
64 * host
65 */
66struct cgroup_meta_data {
67 ptrdiff_t ref; /* simple refcount */
68 struct cgroup_hierarchy **hierarchies;
69 struct cgroup_mount_point **mount_points;
70 int maximum_hierarchy;
71};
72
73/*
74 * cgroup_hierarchy: describes a single cgroup hierarchy
75 * (may have multiple mount points)
76 */
77struct cgroup_hierarchy {
78 int index;
79 bool used; /* false if the hierarchy should be ignored by lxc */
80 char **subsystems;
81 struct cgroup_mount_point *rw_absolute_mount_point;
82 struct cgroup_mount_point *ro_absolute_mount_point;
83 struct cgroup_mount_point **all_mount_points;
84 size_t all_mount_point_capacity;
85};
86
87/*
88 * cgroup_mount_point: a mount point to where a hierarchy
89 * is mounted to
90 */
91struct cgroup_mount_point {
92 struct cgroup_hierarchy *hierarchy;
93 char *mount_point;
94 char *mount_prefix;
95 bool read_only;
96 bool need_cpuset_init;
97};
98
99/*
100 * cgroup_process_info: describes the membership of a
101 * process to the different cgroup
102 * hierarchies
103 *
104 * Note this is the per-process info tracked by the cgfs_ops.
105 * This is not used with cgmanager.
106 */
107struct cgroup_process_info {
108 struct cgroup_process_info *next;
109 struct cgroup_meta_data *meta_ref;
110 struct cgroup_hierarchy *hierarchy;
111 char *cgroup_path;
112 char *cgroup_path_sub;
113 char **created_paths;
114 size_t created_paths_capacity;
115 size_t created_paths_count;
116 struct cgroup_mount_point *designated_mount_point;
117};
118
119struct cgfs_data {
120 char *name;
121 const char *cgroup_pattern;
122 struct cgroup_meta_data *meta;
123 struct cgroup_process_info *info;
124};
125
126lxc_log_define(lxc_cgfs, lxc);
576f946d 127
33ad9f1a
CS
128static struct cgroup_process_info *lxc_cgroup_process_info_getx(const char *proc_pid_cgroup_str, struct cgroup_meta_data *meta);
129static char **subsystems_from_mount_options(const char *mount_options, char **kernel_list);
130static void lxc_cgroup_mount_point_free(struct cgroup_mount_point *mp);
131static void lxc_cgroup_hierarchy_free(struct cgroup_hierarchy *h);
132static bool is_valid_cgroup(const char *name);
33ad9f1a 133static int create_cgroup(struct cgroup_mount_point *mp, const char *path);
603c64c2 134static int remove_cgroup(struct cgroup_mount_point *mp, const char *path, bool recurse);
33ad9f1a
CS
135static char *cgroup_to_absolute_path(struct cgroup_mount_point *mp, const char *path, const char *suffix);
136static struct cgroup_process_info *find_info_for_subsystem(struct cgroup_process_info *info, const char *subsystem);
137static int do_cgroup_get(const char *cgroup_path, const char *sub_filename, char *value, size_t len);
138static int do_cgroup_set(const char *cgroup_path, const char *sub_filename, const char *value);
4fb3cba5
DE
139static bool cgroup_devices_has_allow_or_deny(struct cgfs_data *d, char *v, bool for_allow);
140static int do_setup_cgroup_limits(struct cgfs_data *d, struct lxc_list *cgroup_settings, bool do_devices);
33ad9f1a
CS
141static int cgroup_recursive_task_count(const char *cgroup_path);
142static int count_lines(const char *fn);
1ea59ad2 143static int handle_cgroup_settings(struct cgroup_mount_point *mp, char *cgroup_path);
d703c2b1 144static bool init_cpuset_if_needed(struct cgroup_mount_point *mp, const char *path);
33ad9f1a 145
4fb3cba5
DE
146static struct cgroup_meta_data *lxc_cgroup_load_meta2(const char **subsystem_whitelist);
147static struct cgroup_meta_data *lxc_cgroup_get_meta(struct cgroup_meta_data *meta_data);
148static struct cgroup_meta_data *lxc_cgroup_put_meta(struct cgroup_meta_data *meta_data);
149
150/* free process membership information */
151static void lxc_cgroup_process_info_free(struct cgroup_process_info *info);
152static void lxc_cgroup_process_info_free_and_remove(struct cgroup_process_info *info);
153
d4ef7c50 154static struct cgroup_ops cgfs_ops;
d4ef7c50 155
603c64c2
SH
156static int cgroup_rmdir(char *dirname)
157{
158 struct dirent dirent, *direntp;
159 int saved_errno = 0;
160 DIR *dir;
161 int ret, failed=0;
162 char pathname[MAXPATHLEN];
163
164 dir = opendir(dirname);
165 if (!dir) {
166 ERROR("%s: failed to open %s", __func__, dirname);
167 return -1;
168 }
169
170 while (!readdir_r(dir, &dirent, &direntp)) {
171 struct stat mystat;
172 int rc;
173
174 if (!direntp)
175 break;
176
177 if (!strcmp(direntp->d_name, ".") ||
178 !strcmp(direntp->d_name, ".."))
179 continue;
180
181 rc = snprintf(pathname, MAXPATHLEN, "%s/%s", dirname, direntp->d_name);
182 if (rc < 0 || rc >= MAXPATHLEN) {
183 ERROR("pathname too long");
184 failed=1;
185 if (!saved_errno)
186 saved_errno = -ENOMEM;
187 continue;
188 }
189 ret = lstat(pathname, &mystat);
190 if (ret) {
191 SYSERROR("%s: failed to stat %s", __func__, pathname);
192 failed=1;
193 if (!saved_errno)
194 saved_errno = errno;
195 continue;
196 }
197 if (S_ISDIR(mystat.st_mode)) {
198 if (cgroup_rmdir(pathname) < 0) {
199 if (!saved_errno)
200 saved_errno = errno;
201 failed=1;
202 }
203 }
204 }
205
206 if (rmdir(dirname) < 0) {
207 SYSERROR("%s: failed to delete %s", __func__, dirname);
208 if (!saved_errno)
209 saved_errno = errno;
210 failed=1;
211 }
212
213 ret = closedir(dir);
214 if (ret) {
215 SYSERROR("%s: failed to close directory %s", __func__, dirname);
216 if (!saved_errno)
217 saved_errno = errno;
218 failed=1;
219 }
220
221 errno = saved_errno;
222 return failed ? -1 : 0;
223}
224
4fb3cba5 225static struct cgroup_meta_data *lxc_cgroup_load_meta()
33ad9f1a
CS
226{
227 const char *cgroup_use = NULL;
228 char **cgroup_use_list = NULL;
229 struct cgroup_meta_data *md = NULL;
230 int saved_errno;
231
232 errno = 0;
593e8478 233 cgroup_use = lxc_global_config_value("lxc.cgroup.use");
33ad9f1a
CS
234 if (!cgroup_use && errno != 0)
235 return NULL;
236 if (cgroup_use) {
237 cgroup_use_list = lxc_string_split_and_trim(cgroup_use, ',');
238 if (!cgroup_use_list)
239 return NULL;
240 }
576f946d 241
33ad9f1a
CS
242 md = lxc_cgroup_load_meta2((const char **)cgroup_use_list);
243 saved_errno = errno;
244 lxc_free_array((void **)cgroup_use_list, free);
245 errno = saved_errno;
246 return md;
247}
fd37327f 248
b653309a 249/* Step 1: determine all kernel subsystems */
4fb3cba5 250static bool find_cgroup_subsystems(char ***kernel_subsystems)
1d39a065 251{
b653309a
SH
252 FILE *proc_cgroups;
253 bool bret = false;
33ad9f1a
CS
254 char *line = NULL;
255 size_t sz = 0;
b653309a
SH
256 size_t kernel_subsystems_count = 0;
257 size_t kernel_subsystems_capacity = 0;
258 int r;
1d39a065 259
33ad9f1a
CS
260 proc_cgroups = fopen_cloexec("/proc/cgroups", "r");
261 if (!proc_cgroups)
b653309a 262 return false;
1d39a065 263
33ad9f1a
CS
264 while (getline(&line, &sz, proc_cgroups) != -1) {
265 char *tab1;
266 char *tab2;
267 int hierarchy_number;
1d39a065 268
33ad9f1a
CS
269 if (line[0] == '#')
270 continue;
271 if (!line[0])
272 continue;
1d39a065 273
33ad9f1a
CS
274 tab1 = strchr(line, '\t');
275 if (!tab1)
8900b9eb 276 continue;
33ad9f1a
CS
277 *tab1++ = '\0';
278 tab2 = strchr(tab1, '\t');
279 if (!tab2)
280 continue;
281 *tab2 = '\0';
fd37327f 282
33ad9f1a
CS
283 tab2 = NULL;
284 hierarchy_number = strtoul(tab1, &tab2, 10);
285 if (!tab2 || *tab2)
286 continue;
287 (void)hierarchy_number;
288
b653309a 289 r = lxc_grow_array((void ***)kernel_subsystems, &kernel_subsystems_capacity, kernel_subsystems_count + 1, 12);
33ad9f1a 290 if (r < 0)
b653309a
SH
291 goto out;
292 (*kernel_subsystems)[kernel_subsystems_count] = strdup(line);
293 if (!(*kernel_subsystems)[kernel_subsystems_count])
294 goto out;
33ad9f1a 295 kernel_subsystems_count++;
bcbd102c 296 }
b653309a 297 bret = true;
0d9f8e18 298
b653309a 299out:
33ad9f1a 300 fclose(proc_cgroups);
0ccf7c2a 301 free(line);
b653309a
SH
302 return bret;
303}
304
305/* Step 2: determine all hierarchies (by reading /proc/self/cgroup),
306 * since mount points don't specify hierarchy number and
307 * /proc/cgroups does not contain named hierarchies
308 */
309static bool find_cgroup_hierarchies(struct cgroup_meta_data *meta_data,
310 bool all_kernel_subsystems, bool all_named_subsystems,
311 const char **subsystem_whitelist)
312{
313 FILE *proc_self_cgroup;
314 char *line = NULL;
315 size_t sz = 0;
316 int r;
317 bool bret = false;
318 size_t hierarchy_capacity = 0;
ef6e34ee 319
33ad9f1a
CS
320 proc_self_cgroup = fopen_cloexec("/proc/self/cgroup", "r");
321 /* if for some reason (because of setns() and pid namespace for example),
322 * /proc/self is not valid, we try /proc/1/cgroup... */
323 if (!proc_self_cgroup)
324 proc_self_cgroup = fopen_cloexec("/proc/1/cgroup", "r");
325 if (!proc_self_cgroup)
b653309a 326 return false;
33ad9f1a
CS
327
328 while (getline(&line, &sz, proc_self_cgroup) != -1) {
329 /* file format: hierarchy:subsystems:group,
330 * we only extract hierarchy and subsystems
331 * here */
332 char *colon1;
333 char *colon2;
334 int hierarchy_number;
335 struct cgroup_hierarchy *h = NULL;
336 char **p;
337
338 if (!line[0])
339 continue;
ad08bbb7 340
33ad9f1a
CS
341 colon1 = strchr(line, ':');
342 if (!colon1)
8900b9eb 343 continue;
33ad9f1a
CS
344 *colon1++ = '\0';
345 colon2 = strchr(colon1, ':');
346 if (!colon2)
347 continue;
348 *colon2 = '\0';
ad08bbb7 349
33ad9f1a
CS
350 colon2 = NULL;
351 hierarchy_number = strtoul(line, &colon2, 10);
352 if (!colon2 || *colon2)
353 continue;
576f946d 354
33ad9f1a
CS
355 if (hierarchy_number > meta_data->maximum_hierarchy) {
356 /* lxc_grow_array will never shrink, so even if we find a lower
357 * hierarchy number here, the array will never be smaller
358 */
359 r = lxc_grow_array((void ***)&meta_data->hierarchies, &hierarchy_capacity, hierarchy_number + 1, 12);
360 if (r < 0)
b653309a 361 goto out;
5193cc3d 362
33ad9f1a
CS
363 meta_data->maximum_hierarchy = hierarchy_number;
364 }
fd37327f 365
33ad9f1a
CS
366 /* this shouldn't happen, we had this already */
367 if (meta_data->hierarchies[hierarchy_number])
b653309a 368 goto out;
33ad9f1a
CS
369
370 h = calloc(1, sizeof(struct cgroup_hierarchy));
371 if (!h)
b653309a 372 goto out;
33ad9f1a
CS
373
374 meta_data->hierarchies[hierarchy_number] = h;
375
376 h->index = hierarchy_number;
377 h->subsystems = lxc_string_split_and_trim(colon1, ',');
378 if (!h->subsystems)
b653309a 379 goto out;
33ad9f1a
CS
380 /* see if this hierarchy should be considered */
381 if (!all_kernel_subsystems || !all_named_subsystems) {
382 for (p = h->subsystems; *p; p++) {
383 if (!strncmp(*p, "name=", 5)) {
384 if (all_named_subsystems || (subsystem_whitelist && lxc_string_in_array(*p, subsystem_whitelist))) {
385 h->used = true;
386 break;
387 }
388 } else {
389 if (all_kernel_subsystems || (subsystem_whitelist && lxc_string_in_array(*p, subsystem_whitelist))) {
390 h->used = true;
391 break;
392 }
393 }
394 }
395 } else {
396 /* we want all hierarchy anyway */
397 h->used = true;
ae5c8b8e 398 }
ae5c8b8e 399 }
b653309a 400 bret = true;
0b9c21ab 401
b653309a 402out:
33ad9f1a 403 fclose(proc_self_cgroup);
0ccf7c2a 404 free(line);
b653309a
SH
405 return bret;
406}
407
408/* Step 3: determine all mount points of each hierarchy */
409static bool find_hierarchy_mountpts( struct cgroup_meta_data *meta_data, char **kernel_subsystems)
410{
411 bool bret = false;
412 FILE *proc_self_mountinfo;
413 char *line = NULL;
414 size_t sz = 0;
415 char **tokens = NULL;
416 size_t mount_point_count = 0;
417 size_t mount_point_capacity = 0;
418 size_t token_capacity = 0;
419 int r;
420
33ad9f1a
CS
421 proc_self_mountinfo = fopen_cloexec("/proc/self/mountinfo", "r");
422 /* if for some reason (because of setns() and pid namespace for example),
423 * /proc/self is not valid, we try /proc/1/cgroup... */
424 if (!proc_self_mountinfo)
425 proc_self_mountinfo = fopen_cloexec("/proc/1/mountinfo", "r");
426 if (!proc_self_mountinfo)
b653309a 427 return false;
33ad9f1a
CS
428
429 while (getline(&line, &sz, proc_self_mountinfo) != -1) {
178938fe 430 char *token, *line_tok, *saveptr = NULL;
33ad9f1a
CS
431 size_t i, j, k;
432 struct cgroup_mount_point *mount_point;
433 struct cgroup_hierarchy *h;
434 char **subsystems;
435
436 if (line[0] && line[strlen(line) - 1] == '\n')
437 line[strlen(line) - 1] = '\0';
438
178938fe 439 for (i = 0, line_tok = line; (token = strtok_r(line_tok, " ", &saveptr)); line_tok = NULL) {
33ad9f1a
CS
440 r = lxc_grow_array((void ***)&tokens, &token_capacity, i + 1, 64);
441 if (r < 0)
b653309a 442 goto out;
33ad9f1a
CS
443 tokens[i++] = token;
444 }
b98f7d6e 445
33ad9f1a
CS
446 /* layout of /proc/self/mountinfo:
447 * 0: id
448 * 1: parent id
449 * 2: device major:minor
450 * 3: mount prefix
8900b9eb 451 * 4: mount point
33ad9f1a
CS
452 * 5: per-mount options
453 * [optional X]: additional data
454 * X+7: "-"
455 * X+8: type
456 * X+9: source
457 * X+10: per-superblock options
458 */
459 for (j = 6; j < i && tokens[j]; j++)
460 if (!strcmp(tokens[j], "-"))
461 break;
fd4f5a56 462
33ad9f1a
CS
463 /* could not find separator */
464 if (j >= i || !tokens[j])
465 continue;
466 /* there should be exactly three fields after
467 * the separator
468 */
469 if (i != j + 4)
470 continue;
fd4f5a56 471
33ad9f1a
CS
472 /* not a cgroup filesystem */
473 if (strcmp(tokens[j + 1], "cgroup") != 0)
474 continue;
b98f7d6e 475
33ad9f1a
CS
476 subsystems = subsystems_from_mount_options(tokens[j + 3], kernel_subsystems);
477 if (!subsystems)
b653309a 478 goto out;
33ad9f1a
CS
479
480 h = NULL;
481 for (k = 1; k <= meta_data->maximum_hierarchy; k++) {
482 if (meta_data->hierarchies[k] &&
483 meta_data->hierarchies[k]->subsystems[0] &&
484 lxc_string_in_array(meta_data->hierarchies[k]->subsystems[0], (const char **)subsystems)) {
485 /* TODO: we could also check if the lists really match completely,
486 * just to have an additional sanity check */
487 h = meta_data->hierarchies[k];
b98f7d6e 488 break;
33ad9f1a 489 }
b98f7d6e 490 }
33ad9f1a
CS
491 lxc_free_array((void **)subsystems, free);
492
493 r = lxc_grow_array((void ***)&meta_data->mount_points, &mount_point_capacity, mount_point_count + 1, 12);
494 if (r < 0)
b653309a 495 goto out;
33ad9f1a
CS
496
497 /* create mount point object */
498 mount_point = calloc(1, sizeof(*mount_point));
499 if (!mount_point)
b653309a 500 goto out;
33ad9f1a
CS
501
502 meta_data->mount_points[mount_point_count++] = mount_point;
503
504 mount_point->hierarchy = h;
505 mount_point->mount_point = strdup(tokens[4]);
506 mount_point->mount_prefix = strdup(tokens[3]);
507 if (!mount_point->mount_point || !mount_point->mount_prefix)
b653309a 508 goto out;
33ad9f1a
CS
509 mount_point->read_only = !lxc_string_in_list("rw", tokens[5], ',');
510
511 if (!strcmp(mount_point->mount_prefix, "/")) {
512 if (mount_point->read_only) {
513 if (!h->ro_absolute_mount_point)
514 h->ro_absolute_mount_point = mount_point;
515 } else {
516 if (!h->rw_absolute_mount_point)
517 h->rw_absolute_mount_point = mount_point;
518 }
b98f7d6e 519 }
ae5c8b8e 520
33ad9f1a
CS
521 k = lxc_array_len((void **)h->all_mount_points);
522 r = lxc_grow_array((void ***)&h->all_mount_points, &h->all_mount_point_capacity, k + 1, 4);
523 if (r < 0)
b653309a 524 goto out;
33ad9f1a 525 h->all_mount_points[k] = mount_point;
fd4f5a56 526 }
b653309a
SH
527 bret = true;
528
529out:
b653309a 530 fclose(proc_self_mountinfo);
b653309a 531 free(tokens);
2cdafc54 532 free(line);
b653309a
SH
533 return bret;
534}
535
4fb3cba5 536static struct cgroup_meta_data *lxc_cgroup_load_meta2(const char **subsystem_whitelist)
b653309a
SH
537{
538 bool all_kernel_subsystems = true;
539 bool all_named_subsystems = false;
540 struct cgroup_meta_data *meta_data = NULL;
541 char **kernel_subsystems = NULL;
542 int saved_errno = 0;
543
544 /* if the subsystem whitelist is not specified, include all
545 * hierarchies that contain kernel subsystems by default but
546 * no hierarchies that only contain named subsystems
547 *
548 * if it is specified, the specifier @all will select all
549 * hierarchies, @kernel will select all hierarchies with
550 * kernel subsystems and @named will select all named
551 * hierarchies
552 */
553 all_kernel_subsystems = subsystem_whitelist ?
554 (lxc_string_in_array("@kernel", subsystem_whitelist) || lxc_string_in_array("@all", subsystem_whitelist)) :
555 true;
556 all_named_subsystems = subsystem_whitelist ?
557 (lxc_string_in_array("@named", subsystem_whitelist) || lxc_string_in_array("@all", subsystem_whitelist)) :
558 false;
559
560 meta_data = calloc(1, sizeof(struct cgroup_meta_data));
561 if (!meta_data)
562 return NULL;
563 meta_data->ref = 1;
564
565 if (!find_cgroup_subsystems(&kernel_subsystems))
566 goto out_error;
567
568 if (!find_cgroup_hierarchies(meta_data, all_kernel_subsystems,
569 all_named_subsystems, subsystem_whitelist))
570 goto out_error;
571
572 if (!find_hierarchy_mountpts(meta_data, kernel_subsystems))
573 goto out_error;
fd4f5a56 574
33ad9f1a
CS
575 /* oops, we couldn't find anything */
576 if (!meta_data->hierarchies || !meta_data->mount_points) {
577 errno = EINVAL;
578 goto out_error;
ae5c8b8e 579 }
fd4f5a56 580
3a0abb3a 581 lxc_free_array((void **)kernel_subsystems, free);
33ad9f1a
CS
582 return meta_data;
583
584out_error:
585 saved_errno = errno;
33ad9f1a
CS
586 lxc_free_array((void **)kernel_subsystems, free);
587 lxc_cgroup_put_meta(meta_data);
588 errno = saved_errno;
589 return NULL;
fd4f5a56
DL
590}
591
4fb3cba5 592static struct cgroup_meta_data *lxc_cgroup_get_meta(struct cgroup_meta_data *meta_data)
e14f67a7 593{
33ad9f1a
CS
594 meta_data->ref++;
595 return meta_data;
596}
e14f67a7 597
4fb3cba5 598static struct cgroup_meta_data *lxc_cgroup_put_meta(struct cgroup_meta_data *meta_data)
33ad9f1a
CS
599{
600 size_t i;
601 if (!meta_data)
602 return NULL;
603 if (--meta_data->ref > 0)
604 return meta_data;
605 lxc_free_array((void **)meta_data->mount_points, (lxc_free_fn)lxc_cgroup_mount_point_free);
606 if (meta_data->hierarchies) {
607 for (i = 0; i <= meta_data->maximum_hierarchy; i++)
608 lxc_cgroup_hierarchy_free(meta_data->hierarchies[i]);
e14f67a7 609 }
33ad9f1a 610 free(meta_data->hierarchies);
178938fe 611 free(meta_data);
33ad9f1a 612 return NULL;
e14f67a7
U
613}
614
4fb3cba5 615static struct cgroup_hierarchy *lxc_cgroup_find_hierarchy(struct cgroup_meta_data *meta_data, const char *subsystem)
e14f67a7 616{
33ad9f1a
CS
617 size_t i;
618 for (i = 0; i <= meta_data->maximum_hierarchy; i++) {
619 struct cgroup_hierarchy *h = meta_data->hierarchies[i];
620 if (h && lxc_string_in_array(subsystem, (const char **)h->subsystems))
621 return h;
e14f67a7 622 }
e14f67a7
U
623 return NULL;
624}
625
4fb3cba5 626static struct cgroup_mount_point *lxc_cgroup_find_mount_point(struct cgroup_hierarchy *hierarchy, const char *group, bool should_be_writable)
b98f7d6e 627{
33ad9f1a
CS
628 struct cgroup_mount_point **mps;
629 struct cgroup_mount_point *current_result = NULL;
630 ssize_t quality = -1;
b98f7d6e 631
33ad9f1a
CS
632 /* trivial case */
633 if (hierarchy->rw_absolute_mount_point)
634 return hierarchy->rw_absolute_mount_point;
635 if (!should_be_writable && hierarchy->ro_absolute_mount_point)
636 return hierarchy->ro_absolute_mount_point;
b98f7d6e 637
33ad9f1a
CS
638 for (mps = hierarchy->all_mount_points; mps && *mps; mps++) {
639 struct cgroup_mount_point *mp = *mps;
640 size_t prefix_len = mp->mount_prefix ? strlen(mp->mount_prefix) : 0;
b98f7d6e 641
33ad9f1a
CS
642 if (prefix_len == 1 && mp->mount_prefix[0] == '/')
643 prefix_len = 0;
b98f7d6e 644
33ad9f1a
CS
645 if (should_be_writable && mp->read_only)
646 continue;
647
648 if (!prefix_len ||
649 (strncmp(group, mp->mount_prefix, prefix_len) == 0 &&
650 (group[prefix_len] == '\0' || group[prefix_len] == '/'))) {
651 /* search for the best quality match, i.e. the match with the
652 * shortest prefix where this group is still contained
653 */
654 if (quality == -1 || prefix_len < quality) {
655 current_result = mp;
656 quality = prefix_len;
657 }
b98f7d6e
SH
658 }
659 }
660
33ad9f1a
CS
661 if (!current_result)
662 errno = ENOENT;
663 return current_result;
b98f7d6e
SH
664}
665
4fb3cba5 666static char *lxc_cgroup_find_abs_path(const char *subsystem, const char *group, bool should_be_writable, const char *suffix)
b98f7d6e 667{
33ad9f1a
CS
668 struct cgroup_meta_data *meta_data;
669 struct cgroup_hierarchy *h;
670 struct cgroup_mount_point *mp;
671 char *result;
672 int saved_errno;
673
674 meta_data = lxc_cgroup_load_meta();
675 if (!meta_data)
676 return NULL;
b98f7d6e 677
33ad9f1a
CS
678 h = lxc_cgroup_find_hierarchy(meta_data, subsystem);
679 if (!h)
680 goto out_error;
b98f7d6e 681
33ad9f1a
CS
682 mp = lxc_cgroup_find_mount_point(h, group, should_be_writable);
683 if (!mp)
684 goto out_error;
b98f7d6e 685
33ad9f1a
CS
686 result = cgroup_to_absolute_path(mp, group, suffix);
687 if (!result)
688 goto out_error;
b98f7d6e 689
33ad9f1a
CS
690 lxc_cgroup_put_meta(meta_data);
691 return result;
b98f7d6e 692
33ad9f1a
CS
693out_error:
694 saved_errno = errno;
695 lxc_cgroup_put_meta(meta_data);
696 errno = saved_errno;
697 return NULL;
b98f7d6e
SH
698}
699
4fb3cba5 700static struct cgroup_process_info *lxc_cgroup_process_info_get(pid_t pid, struct cgroup_meta_data *meta)
fd4f5a56 701{
33ad9f1a
CS
702 char pid_buf[32];
703 snprintf(pid_buf, 32, "/proc/%lu/cgroup", (unsigned long)pid);
704 return lxc_cgroup_process_info_getx(pid_buf, meta);
c8f7c563
CS
705}
706
4fb3cba5 707static struct cgroup_process_info *lxc_cgroup_process_info_get_init(struct cgroup_meta_data *meta)
c8f7c563 708{
33ad9f1a
CS
709 return lxc_cgroup_process_info_get(1, meta);
710}
b98f7d6e 711
4fb3cba5 712static struct cgroup_process_info *lxc_cgroup_process_info_get_self(struct cgroup_meta_data *meta)
33ad9f1a
CS
713{
714 struct cgroup_process_info *i;
715 i = lxc_cgroup_process_info_getx("/proc/self/cgroup", meta);
716 if (!i)
717 i = lxc_cgroup_process_info_get(getpid(), meta);
718 return i;
719}
ae5c8b8e 720
692ba18f
SH
721/*
722 * If a controller has ns cgroup mounted, then in that cgroup the handler->pid
723 * is already in a new cgroup named after the pid. 'mnt' is passed in as
724 * the full current cgroup. Say that is /sys/fs/cgroup/lxc/2975 and the container
725 * name is c1. . We want to rename the cgroup directory to /sys/fs/cgroup/lxc/c1,
726 * and return the string /sys/fs/cgroup/lxc/c1.
727 */
cea0552e 728static char *cgroup_rename_nsgroup(const char *mountpath, const char *oldname, pid_t pid, const char *name)
692ba18f
SH
729{
730 char *dir, *fulloldpath;
731 char *newname, *fullnewpath;
cea0552e 732 int len, newlen, ret;
692ba18f
SH
733
734 /*
735 * if cgroup is mounted at /cgroup and task is in cgroup /ab/, pid 2375 and
736 * name is c1,
737 * dir: /ab
738 * fulloldpath = /cgroup/ab/2375
739 * fullnewpath = /cgroup/ab/c1
740 * newname = /ab/c1
741 */
742 dir = alloca(strlen(oldname) + 1);
743 strcpy(dir, oldname);
744
cea0552e
SH
745 len = strlen(oldname) + strlen(mountpath) + 22;
746 fulloldpath = alloca(len);
747 ret = snprintf(fulloldpath, len, "%s/%s/%ld", mountpath, oldname, (unsigned long)pid);
748 if (ret < 0 || ret >= len)
749 return NULL;
692ba18f
SH
750
751 len = strlen(dir) + strlen(name) + 2;
752 newname = malloc(len);
753 if (!newname) {
754 SYSERROR("Out of memory");
755 return NULL;
756 }
cea0552e
SH
757 ret = snprintf(newname, len, "%s/%s", dir, name);
758 if (ret < 0 || ret >= len) {
759 free(newname);
760 return NULL;
761 }
692ba18f 762
cea0552e
SH
763 newlen = strlen(mountpath) + len + 2;
764 fullnewpath = alloca(newlen);
765 ret = snprintf(fullnewpath, newlen, "%s/%s", mountpath, newname);
766 if (ret < 0 || ret >= newlen) {
767 free(newname);
768 return NULL;
769 }
692ba18f
SH
770
771 if (access(fullnewpath, F_OK) == 0) {
772 if (rmdir(fullnewpath) != 0) {
773 SYSERROR("container cgroup %s already exists.", fullnewpath);
774 free(newname);
775 return NULL;
776 }
777 }
778 if (rename(fulloldpath, fullnewpath)) {
779 SYSERROR("failed to rename cgroup %s->%s", fulloldpath, fullnewpath);
780 free(newname);
781 return NULL;
782 }
783
784 DEBUG("'%s' renamed to '%s'", oldname, newname);
785
786 return newname;
787}
788
33ad9f1a 789/* create a new cgroup */
4fb3cba5 790static struct cgroup_process_info *lxc_cgroupfs_create(const char *name, const char *path_pattern, struct cgroup_meta_data *meta_data, const char *sub_pattern)
33ad9f1a 791{
001b026e 792 char **cgroup_path_components = NULL;
33ad9f1a
CS
793 char **p = NULL;
794 char *path_so_far = NULL;
795 char **new_cgroup_paths = NULL;
796 char **new_cgroup_paths_sub = NULL;
797 struct cgroup_mount_point *mp;
798 struct cgroup_hierarchy *h;
799 struct cgroup_process_info *base_info = NULL;
800 struct cgroup_process_info *info_ptr;
801 int saved_errno;
802 int r;
803 unsigned suffix = 0;
804 bool had_sub_pattern = false;
805 size_t i;
ae5c8b8e 806
33ad9f1a
CS
807 if (!is_valid_cgroup(name)) {
808 ERROR("Invalid cgroup name: '%s'", name);
809 errno = EINVAL;
810 return NULL;
ae5c8b8e
SH
811 }
812
33ad9f1a
CS
813 if (!strstr(path_pattern, "%n")) {
814 ERROR("Invalid cgroup path pattern: '%s'; contains no %%n for specifying container name", path_pattern);
815 errno = EINVAL;
816 return NULL;
817 }
fd37327f 818
33ad9f1a
CS
819 /* we will modify the result of this operation directly,
820 * so we don't have to copy the data structure
821 */
822 base_info = (path_pattern[0] == '/') ?
823 lxc_cgroup_process_info_get_init(meta_data) :
824 lxc_cgroup_process_info_get_self(meta_data);
825 if (!base_info)
826 return NULL;
c8f7c563 827
33ad9f1a
CS
828 new_cgroup_paths = calloc(meta_data->maximum_hierarchy + 1, sizeof(char *));
829 if (!new_cgroup_paths)
830 goto out_initial_error;
831
832 new_cgroup_paths_sub = calloc(meta_data->maximum_hierarchy + 1, sizeof(char *));
833 if (!new_cgroup_paths_sub)
834 goto out_initial_error;
835
836 /* find mount points we can use */
837 for (info_ptr = base_info; info_ptr; info_ptr = info_ptr->next) {
838 h = info_ptr->hierarchy;
839 mp = lxc_cgroup_find_mount_point(h, info_ptr->cgroup_path, true);
840 if (!mp) {
841 ERROR("Could not find writable mount point for cgroup hierarchy %d while trying to create cgroup.", h->index);
842 goto out_initial_error;
843 }
844 info_ptr->designated_mount_point = mp;
460a1cf0 845
692ba18f
SH
846 if (lxc_string_in_array("ns", (const char **)h->subsystems))
847 continue;
2edb53c7
SH
848 if (handle_cgroup_settings(mp, info_ptr->cgroup_path) < 0) {
849 ERROR("Could not set clone_children to 1 for cpuset hierarchy in parent cgroup.");
33ad9f1a 850 goto out_initial_error;
2edb53c7 851 }
33ad9f1a 852 }
b98f7d6e 853
33ad9f1a
CS
854 /* normalize the path */
855 cgroup_path_components = lxc_normalize_path(path_pattern);
856 if (!cgroup_path_components)
857 goto out_initial_error;
858
859 /* go through the path components to see if we can create them */
860 for (p = cgroup_path_components; *p || (sub_pattern && !had_sub_pattern); p++) {
861 /* we only want to create the same component with -1, -2, etc.
862 * if the component contains the container name itself, otherwise
863 * it's not an error if it already exists
864 */
865 char *p_eff = *p ? *p : (char *)sub_pattern;
866 bool contains_name = strstr(p_eff, "%n");
867 char *current_component = NULL;
868 char *current_subpath = NULL;
869 char *current_entire_path = NULL;
870 char *parts[3];
871 size_t j = 0;
872 i = 0;
873
874 /* if we are processing the subpattern, we want to make sure
875 * loop is ended the next time around
876 */
877 if (!*p) {
878 had_sub_pattern = true;
879 p--;
880 }
b98f7d6e 881
33ad9f1a 882 goto find_name_on_this_level;
4fb3cba5 883
33ad9f1a
CS
884 cleanup_name_on_this_level:
885 /* This is reached if we found a name clash.
886 * In that case, remove the cgroup from all previous hierarchies
887 */
888 for (j = 0, info_ptr = base_info; j < i && info_ptr; info_ptr = info_ptr->next, j++) {
603c64c2 889 r = remove_cgroup(info_ptr->designated_mount_point, info_ptr->created_paths[info_ptr->created_paths_count - 1], false);
33ad9f1a
CS
890 if (r < 0)
891 WARN("could not clean up cgroup we created when trying to create container");
892 free(info_ptr->created_paths[info_ptr->created_paths_count - 1]);
893 info_ptr->created_paths[--info_ptr->created_paths_count] = NULL;
894 }
895 if (current_component != current_subpath)
896 free(current_subpath);
897 if (current_component != p_eff)
898 free(current_component);
899 current_component = current_subpath = NULL;
900 /* try again with another suffix */
901 ++suffix;
4fb3cba5 902
33ad9f1a
CS
903 find_name_on_this_level:
904 /* determine name of the path component we should create */
905 if (contains_name && suffix > 0) {
906 char *buf = calloc(strlen(name) + 32, 1);
907 if (!buf)
908 goto out_initial_error;
909 snprintf(buf, strlen(name) + 32, "%s-%u", name, suffix);
910 current_component = lxc_string_replace("%n", buf, p_eff);
911 free(buf);
912 } else {
913 current_component = contains_name ? lxc_string_replace("%n", name, p_eff) : p_eff;
914 }
915 parts[0] = path_so_far;
916 parts[1] = current_component;
917 parts[2] = NULL;
918 current_subpath = path_so_far ? lxc_string_join("/", (const char **)parts, false) : current_component;
919
920 /* Now go through each hierarchy and try to create the
921 * corresponding cgroup
922 */
923 for (i = 0, info_ptr = base_info; info_ptr; info_ptr = info_ptr->next, i++) {
924 char *parts2[3];
692ba18f
SH
925
926 if (lxc_string_in_array("ns", (const char **)info_ptr->hierarchy->subsystems))
927 continue;
33ad9f1a
CS
928 current_entire_path = NULL;
929
930 parts2[0] = !strcmp(info_ptr->cgroup_path, "/") ? "" : info_ptr->cgroup_path;
931 parts2[1] = current_subpath;
932 parts2[2] = NULL;
933 current_entire_path = lxc_string_join("/", (const char **)parts2, false);
934
935 if (!*p) {
936 /* we are processing the subpath, so only update that one */
937 free(new_cgroup_paths_sub[i]);
938 new_cgroup_paths_sub[i] = strdup(current_entire_path);
939 if (!new_cgroup_paths_sub[i])
940 goto cleanup_from_error;
941 } else {
942 /* remember which path was used on this controller */
943 free(new_cgroup_paths[i]);
944 new_cgroup_paths[i] = strdup(current_entire_path);
945 if (!new_cgroup_paths[i])
946 goto cleanup_from_error;
947 }
fd4f5a56 948
33ad9f1a
CS
949 r = create_cgroup(info_ptr->designated_mount_point, current_entire_path);
950 if (r < 0 && errno == EEXIST && contains_name) {
951 /* name clash => try new name with new suffix */
952 free(current_entire_path);
953 current_entire_path = NULL;
954 goto cleanup_name_on_this_level;
955 } else if (r < 0 && errno != EEXIST) {
956 SYSERROR("Could not create cgroup %s", current_entire_path);
957 goto cleanup_from_error;
958 } else if (r == 0) {
959 /* successfully created */
960 r = lxc_grow_array((void ***)&info_ptr->created_paths, &info_ptr->created_paths_capacity, info_ptr->created_paths_count + 1, 8);
961 if (r < 0)
962 goto cleanup_from_error;
d703c2b1
RV
963 if (!init_cpuset_if_needed(info_ptr->designated_mount_point, current_entire_path)) {
964 ERROR("Failed to initialize cpuset in new '%s'.", current_entire_path);
965 goto cleanup_from_error;
966 }
33ad9f1a
CS
967 info_ptr->created_paths[info_ptr->created_paths_count++] = current_entire_path;
968 } else {
969 /* if we didn't create the cgroup, then we have to make sure that
970 * further cgroups will be created properly
971 */
d703c2b1 972 if (handle_cgroup_settings(info_ptr->designated_mount_point, info_ptr->cgroup_path) < 0) {
f6ac3b9e 973 ERROR("Could not set clone_children to 1 for cpuset hierarchy in pre-existing cgroup.");
33ad9f1a 974 goto cleanup_from_error;
f6ac3b9e 975 }
d703c2b1
RV
976 if (!init_cpuset_if_needed(info_ptr->designated_mount_point, info_ptr->cgroup_path)) {
977 ERROR("Failed to initialize cpuset in pre-existing '%s'.", info_ptr->cgroup_path);
978 goto cleanup_from_error;
979 }
33ad9f1a
CS
980
981 /* already existed but path component of pattern didn't contain '%n',
982 * so this is not an error; but then we don't need current_entire_path
983 * anymore...
984 */
985 free(current_entire_path);
986 current_entire_path = NULL;
987 }
988 }
fd4f5a56 989
33ad9f1a
CS
990 /* save path so far */
991 free(path_so_far);
992 path_so_far = strdup(current_subpath);
993 if (!path_so_far)
994 goto cleanup_from_error;
995
996 /* cleanup */
997 if (current_component != current_subpath)
998 free(current_subpath);
999 if (current_component != p_eff)
1000 free(current_component);
1001 current_component = current_subpath = NULL;
1002 continue;
4fb3cba5 1003
33ad9f1a
CS
1004 cleanup_from_error:
1005 /* called if an error occured in the loop, so we
1006 * do some additional cleanup here
1007 */
1008 saved_errno = errno;
1009 if (current_component != current_subpath)
1010 free(current_subpath);
1011 if (current_component != p_eff)
1012 free(current_component);
1013 free(current_entire_path);
1014 errno = saved_errno;
1015 goto out_initial_error;
fd4f5a56
DL
1016 }
1017
33ad9f1a
CS
1018 /* we're done, now update the paths */
1019 for (i = 0, info_ptr = base_info; info_ptr; info_ptr = info_ptr->next, i++) {
47d8fb3b
CS
1020 /* ignore legacy 'ns' subsystem here, lxc_cgroup_create_legacy
1021 * will take care of it
1022 * Since we do a continue in above loop, new_cgroup_paths[i] is
1023 * unset anyway, as is new_cgroup_paths_sub[i]
692ba18f 1024 */
47d8fb3b
CS
1025 if (lxc_string_in_array("ns", (const char **)info_ptr->hierarchy->subsystems))
1026 continue;
1027 free(info_ptr->cgroup_path);
1028 info_ptr->cgroup_path = new_cgroup_paths[i];
1029 info_ptr->cgroup_path_sub = new_cgroup_paths_sub[i];
fd4f5a56 1030 }
33ad9f1a
CS
1031 /* don't use lxc_free_array since we used the array members
1032 * to store them in our result...
1033 */
1034 free(new_cgroup_paths);
1035 free(new_cgroup_paths_sub);
1036 free(path_so_far);
1037 lxc_free_array((void **)cgroup_path_components, free);
1038 return base_info;
1039
1040out_initial_error:
1041 saved_errno = errno;
1042 free(path_so_far);
1043 lxc_cgroup_process_info_free_and_remove(base_info);
1044 lxc_free_array((void **)new_cgroup_paths, free);
1045 lxc_free_array((void **)new_cgroup_paths_sub, free);
1046 lxc_free_array((void **)cgroup_path_components, free);
1047 errno = saved_errno;
1048 return NULL;
c8f7c563
CS
1049}
1050
4fb3cba5 1051static int lxc_cgroup_create_legacy(struct cgroup_process_info *base_info, const char *name, pid_t pid)
47d8fb3b
CS
1052{
1053 struct cgroup_process_info *info_ptr;
1054 int r;
1055
1056 for (info_ptr = base_info; info_ptr; info_ptr = info_ptr->next) {
1057 if (!lxc_string_in_array("ns", (const char **)info_ptr->hierarchy->subsystems))
1058 continue;
1059 /*
1060 * For any path which has ns cgroup mounted, handler->pid is already
1061 * moved into a container called '%d % (handler->pid)'. Rename it to
1062 * the cgroup name and record that.
1063 */
1064 char *tmp = cgroup_rename_nsgroup((const char *)info_ptr->designated_mount_point->mount_point,
1065 info_ptr->cgroup_path, pid, name);
1066 if (!tmp)
1067 return -1;
1068 free(info_ptr->cgroup_path);
1069 info_ptr->cgroup_path = tmp;
1070 r = lxc_grow_array((void ***)&info_ptr->created_paths, &info_ptr->created_paths_capacity, info_ptr->created_paths_count + 1, 8);
1071 if (r < 0)
1072 return -1;
1073 tmp = strdup(tmp);
1074 if (!tmp)
1075 return -1;
1076 info_ptr->created_paths[info_ptr->created_paths_count++] = tmp;
1077 }
1078 return 0;
1079}
1080
33ad9f1a 1081/* get the cgroup membership of a given container */
4fb3cba5 1082static struct cgroup_process_info *lxc_cgroup_get_container_info(const char *name, const char *lxcpath, struct cgroup_meta_data *meta_data)
c8f7c563 1083{
33ad9f1a
CS
1084 struct cgroup_process_info *result = NULL;
1085 int saved_errno = 0;
1086 size_t i;
1087 struct cgroup_process_info **cptr = &result;
1088 struct cgroup_process_info *entry = NULL;
1089 char *path = NULL;
1090
1091 for (i = 0; i <= meta_data->maximum_hierarchy; i++) {
1092 struct cgroup_hierarchy *h = meta_data->hierarchies[i];
1093 if (!h || !h->used)
1094 continue;
c8f7c563 1095
33ad9f1a
CS
1096 /* use the command interface to look for the cgroup */
1097 path = lxc_cmd_get_cgroup_path(name, lxcpath, h->subsystems[0]);
1098 if (!path)
1099 goto out_error;
1100
1101 entry = calloc(1, sizeof(struct cgroup_process_info));
1102 if (!entry)
1103 goto out_error;
1104 entry->meta_ref = lxc_cgroup_get_meta(meta_data);
1105 entry->hierarchy = h;
1106 entry->cgroup_path = path;
1107 path = NULL;
1108
1109 /* it is not an error if we don't find anything here,
1110 * it is up to the caller to decide what to do in that
1111 * case */
1112 entry->designated_mount_point = lxc_cgroup_find_mount_point(h, entry->cgroup_path, true);
1113
1114 *cptr = entry;
1115 cptr = &entry->next;
1116 entry = NULL;
c8f7c563
CS
1117 }
1118
33ad9f1a
CS
1119 return result;
1120out_error:
1121 saved_errno = errno;
1122 free(path);
1123 lxc_cgroup_process_info_free(result);
1124 lxc_cgroup_process_info_free(entry);
1125 errno = saved_errno;
1126 return NULL;
fd4f5a56
DL
1127}
1128
33ad9f1a 1129/* move a processs to the cgroups specified by the membership */
4fb3cba5 1130static int lxc_cgroupfs_enter(struct cgroup_process_info *info, pid_t pid, bool enter_sub)
4f17323e 1131{
33ad9f1a
CS
1132 char pid_buf[32];
1133 char *cgroup_tasks_fn;
1134 int r;
1135 struct cgroup_process_info *info_ptr;
1136
1137 snprintf(pid_buf, 32, "%lu", (unsigned long)pid);
1138 for (info_ptr = info; info_ptr; info_ptr = info_ptr->next) {
1139 char *cgroup_path = (enter_sub && info_ptr->cgroup_path_sub) ?
1140 info_ptr->cgroup_path_sub :
1141 info_ptr->cgroup_path;
1142
1143 if (!info_ptr->designated_mount_point) {
1144 info_ptr->designated_mount_point = lxc_cgroup_find_mount_point(info_ptr->hierarchy, cgroup_path, true);
1145 if (!info_ptr->designated_mount_point) {
1146 SYSERROR("Could not add pid %lu to cgroup %s: internal error (couldn't find any writable mountpoint to cgroup filesystem)", (unsigned long)pid, cgroup_path);
1147 return -1;
1148 }
1149 }
4f17323e 1150
33ad9f1a
CS
1151 cgroup_tasks_fn = cgroup_to_absolute_path(info_ptr->designated_mount_point, cgroup_path, "/tasks");
1152 if (!cgroup_tasks_fn) {
1153 SYSERROR("Could not add pid %lu to cgroup %s: internal error", (unsigned long)pid, cgroup_path);
1154 return -1;
1155 }
4f17323e 1156
33ad9f1a 1157 r = lxc_write_to_file(cgroup_tasks_fn, pid_buf, strlen(pid_buf), false);
5903da82 1158 free(cgroup_tasks_fn);
33ad9f1a
CS
1159 if (r < 0) {
1160 SYSERROR("Could not add pid %lu to cgroup %s: internal error", (unsigned long)pid, cgroup_path);
1161 return -1;
1162 }
4f17323e
CS
1163 }
1164
33ad9f1a 1165 return 0;
4f17323e
CS
1166}
1167
33ad9f1a
CS
1168/* free process membership information */
1169void lxc_cgroup_process_info_free(struct cgroup_process_info *info)
fc7de561 1170{
33ad9f1a
CS
1171 struct cgroup_process_info *next;
1172 if (!info)
b98f7d6e 1173 return;
33ad9f1a
CS
1174 next = info->next;
1175 lxc_cgroup_put_meta(info->meta_ref);
1176 free(info->cgroup_path);
1177 free(info->cgroup_path_sub);
1178 lxc_free_array((void **)info->created_paths, free);
1179 free(info);
1180 lxc_cgroup_process_info_free(next);
fc7de561
SH
1181}
1182
33ad9f1a
CS
1183/* free process membership information and remove cgroups that were created */
1184void lxc_cgroup_process_info_free_and_remove(struct cgroup_process_info *info)
b98f7d6e 1185{
33ad9f1a
CS
1186 struct cgroup_process_info *next;
1187 char **pp;
1188 if (!info)
1189 return;
1190 next = info->next;
603c64c2 1191 {
33ad9f1a
CS
1192 struct cgroup_mount_point *mp = info->designated_mount_point;
1193 if (!mp)
1194 mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, true);
1195 if (mp)
1196 /* ignore return value here, perhaps we created the
1197 * '/lxc' cgroup in this container but another container
1198 * is still running (for example)
1199 */
603c64c2
SH
1200 (void)remove_cgroup(mp, info->cgroup_path, true);
1201 }
1202 for (pp = info->created_paths; pp && *pp; pp++);
1203 for ((void)(pp && --pp); info->created_paths && pp >= info->created_paths; --pp) {
33ad9f1a 1204 free(*pp);
b98f7d6e 1205 }
33ad9f1a
CS
1206 free(info->created_paths);
1207 lxc_cgroup_put_meta(info->meta_ref);
1208 free(info->cgroup_path);
1209 free(info->cgroup_path_sub);
1210 free(info);
9431aa65 1211 lxc_cgroup_process_info_free_and_remove(next);
33ad9f1a 1212}
b98f7d6e 1213
4fb3cba5 1214static char *lxc_cgroup_get_hierarchy_path_data(const char *subsystem, struct cgfs_data *d)
33ad9f1a 1215{
d4ef7c50
SH
1216 struct cgroup_process_info *info = d->info;
1217 info = find_info_for_subsystem(info, subsystem);
33ad9f1a
CS
1218 if (!info)
1219 return NULL;
1220 return info->cgroup_path;
b98f7d6e
SH
1221}
1222
4fb3cba5 1223static char *lxc_cgroup_get_hierarchy_abs_path_data(const char *subsystem, struct cgfs_data *d)
b98f7d6e 1224{
d4ef7c50 1225 struct cgroup_process_info *info = d->info;
33ad9f1a 1226 struct cgroup_mount_point *mp = NULL;
d4ef7c50
SH
1227
1228 info = find_info_for_subsystem(info, subsystem);
33ad9f1a
CS
1229 if (!info)
1230 return NULL;
1231 if (info->designated_mount_point) {
8900b9eb 1232 mp = info->designated_mount_point;
33ad9f1a
CS
1233 } else {
1234 mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, true);
1235 if (!mp)
1236 return NULL;
b98f7d6e 1237 }
33ad9f1a 1238 return cgroup_to_absolute_path(mp, info->cgroup_path, NULL);
b98f7d6e 1239}
55c76589 1240
4fb3cba5 1241static char *lxc_cgroup_get_hierarchy_abs_path(const char *subsystem, const char *name, const char *lxcpath)
9a93d992 1242{
33ad9f1a
CS
1243 struct cgroup_meta_data *meta;
1244 struct cgroup_process_info *base_info, *info;
1245 struct cgroup_mount_point *mp;
1246 char *result = NULL;
33ad9f1a
CS
1247
1248 meta = lxc_cgroup_load_meta();
1249 if (!meta)
9a93d992 1250 return NULL;
33ad9f1a
CS
1251 base_info = lxc_cgroup_get_container_info(name, lxcpath, meta);
1252 if (!base_info)
178938fe 1253 goto out1;
33ad9f1a
CS
1254 info = find_info_for_subsystem(base_info, subsystem);
1255 if (!info)
178938fe 1256 goto out2;
33ad9f1a 1257 if (info->designated_mount_point) {
8900b9eb 1258 mp = info->designated_mount_point;
33ad9f1a
CS
1259 } else {
1260 mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, true);
1261 if (!mp)
178938fe 1262 goto out3;
33ad9f1a
CS
1263 }
1264 result = cgroup_to_absolute_path(mp, info->cgroup_path, NULL);
178938fe 1265out3:
178938fe 1266out2:
33ad9f1a 1267 lxc_cgroup_process_info_free(base_info);
178938fe 1268out1:
33ad9f1a 1269 lxc_cgroup_put_meta(meta);
33ad9f1a
CS
1270 return result;
1271}
9a93d992 1272
4fb3cba5 1273static int lxc_cgroup_set_data(const char *filename, const char *value, struct cgfs_data *d)
33ad9f1a
CS
1274{
1275 char *subsystem = NULL, *p, *path;
1276 int ret = -1;
9a93d992 1277
33ad9f1a
CS
1278 subsystem = alloca(strlen(filename) + 1);
1279 strcpy(subsystem, filename);
1280 if ((p = index(subsystem, '.')) != NULL)
1281 *p = '\0';
9a93d992 1282
4fb3cba5 1283 path = lxc_cgroup_get_hierarchy_abs_path_data(subsystem, d);
33ad9f1a
CS
1284 if (path) {
1285 ret = do_cgroup_set(path, filename, value);
1286 free(path);
9a93d992 1287 }
33ad9f1a
CS
1288 return ret;
1289}
9a93d992 1290
4fb3cba5 1291static int lxc_cgroupfs_set(const char *filename, const char *value, const char *name, const char *lxcpath)
9a93d992 1292{
33ad9f1a
CS
1293 char *subsystem = NULL, *p, *path;
1294 int ret = -1;
9a93d992 1295
33ad9f1a
CS
1296 subsystem = alloca(strlen(filename) + 1);
1297 strcpy(subsystem, filename);
1298 if ((p = index(subsystem, '.')) != NULL)
1299 *p = '\0';
9a93d992 1300
33ad9f1a
CS
1301 path = lxc_cgroup_get_hierarchy_abs_path(subsystem, name, lxcpath);
1302 if (path) {
1303 ret = do_cgroup_set(path, filename, value);
1304 free(path);
1305 }
b98f7d6e 1306 return ret;
9a93d992
SH
1307}
1308
4fb3cba5 1309static int lxc_cgroupfs_get(const char *filename, char *value, size_t len, const char *name, const char *lxcpath)
9a93d992 1310{
33ad9f1a
CS
1311 char *subsystem = NULL, *p, *path;
1312 int ret = -1;
1313
1314 subsystem = alloca(strlen(filename) + 1);
1315 strcpy(subsystem, filename);
1316 if ((p = index(subsystem, '.')) != NULL)
1317 *p = '\0';
1318
1319 path = lxc_cgroup_get_hierarchy_abs_path(subsystem, name, lxcpath);
1320 if (path) {
1321 ret = do_cgroup_get(path, filename, value, len);
1322 free(path);
9a93d992 1323 }
33ad9f1a 1324 return ret;
9a93d992
SH
1325}
1326
4fb3cba5 1327static bool cgroupfs_mount_cgroup(void *hdata, const char *root, int type)
aae1f3c4
CS
1328{
1329 size_t bufsz = strlen(root) + sizeof("/sys/fs/cgroup");
1330 char *path = NULL;
1331 char **parts = NULL;
1332 char *dirname = NULL;
1333 char *abs_path = NULL;
1334 char *abs_path2 = NULL;
d4ef7c50
SH
1335 struct cgfs_data *cgfs_d;
1336 struct cgroup_process_info *info, *base_info;
aae1f3c4
CS
1337 int r, saved_errno = 0;
1338
4fb3cba5
DE
1339 cgfs_d = hdata;
1340 if (!cgfs_d)
1341 return false;
d4ef7c50
SH
1342 base_info = cgfs_d->info;
1343
7997d7da
CS
1344 if (type < LXC_AUTO_CGROUP_RO || type > LXC_AUTO_CGROUP_FULL_MIXED) {
1345 ERROR("could not mount cgroups into container: invalid type specified internally");
1346 errno = EINVAL;
c476bdce 1347 return false;
7997d7da
CS
1348 }
1349
aae1f3c4
CS
1350 path = calloc(1, bufsz);
1351 if (!path)
c476bdce 1352 return false;
aae1f3c4
CS
1353 snprintf(path, bufsz, "%s/sys/fs/cgroup", root);
1354 r = mount("cgroup_root", path, "tmpfs", MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_RELATIME, "size=10240k,mode=755");
1355 if (r < 0) {
1356 SYSERROR("could not mount tmpfs to /sys/fs/cgroup in the container");
c476bdce 1357 return false;
aae1f3c4
CS
1358 }
1359
1360 /* now mount all the hierarchies we care about */
1361 for (info = base_info; info; info = info->next) {
1362 size_t subsystem_count, i;
1363 struct cgroup_mount_point *mp = info->designated_mount_point;
1364 if (!mp)
1365 mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, true);
1366 if (!mp) {
1367 SYSERROR("could not find original mount point for cgroup hierarchy while trying to mount cgroup filesystem");
1368 goto out_error;
1369 }
1370
1371 subsystem_count = lxc_array_len((void **)info->hierarchy->subsystems);
1372 parts = calloc(subsystem_count + 1, sizeof(char *));
1373 if (!parts)
1374 goto out_error;
1375
1376 for (i = 0; i < subsystem_count; i++) {
1377 if (!strncmp(info->hierarchy->subsystems[i], "name=", 5))
1378 parts[i] = info->hierarchy->subsystems[i] + 5;
1379 else
1380 parts[i] = info->hierarchy->subsystems[i];
1381 }
1382 dirname = lxc_string_join(",", (const char **)parts, false);
1383 if (!dirname)
1384 goto out_error;
1385
1386 /* create subsystem directory */
1387 abs_path = lxc_append_paths(path, dirname);
1388 if (!abs_path)
1389 goto out_error;
1390 r = mkdir_p(abs_path, 0755);
1391 if (r < 0 && errno != EEXIST) {
1392 SYSERROR("could not create cgroup subsystem directory /sys/fs/cgroup/%s", dirname);
1393 goto out_error;
1394 }
1395
aae1f3c4
CS
1396 abs_path2 = lxc_append_paths(abs_path, info->cgroup_path);
1397 if (!abs_path2)
1398 goto out_error;
aae1f3c4 1399
7997d7da
CS
1400 if (type == LXC_AUTO_CGROUP_FULL_RO || type == LXC_AUTO_CGROUP_FULL_RW || type == LXC_AUTO_CGROUP_FULL_MIXED) {
1401 /* bind-mount the cgroup entire filesystem there */
1402 if (strcmp(mp->mount_prefix, "/") != 0) {
1403 /* FIXME: maybe we should just try to remount the entire hierarchy
1404 * with a regular mount command? may that works? */
1405 ERROR("could not automatically mount cgroup-full to /sys/fs/cgroup/%s: host has no mount point for this cgroup filesystem that has access to the root cgroup", dirname);
1406 goto out_error;
1407 }
1408 r = mount(mp->mount_point, abs_path, "none", MS_BIND, 0);
1409 if (r < 0) {
1410 SYSERROR("error bind-mounting %s to %s", mp->mount_point, abs_path);
1411 goto out_error;
1412 }
1413 /* main cgroup path should be read-only */
1414 if (type == LXC_AUTO_CGROUP_FULL_RO || type == LXC_AUTO_CGROUP_FULL_MIXED) {
1415 r = mount(NULL, abs_path, NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL);
1416 if (r < 0) {
1417 SYSERROR("error re-mounting %s readonly", abs_path);
1418 goto out_error;
1419 }
1420 }
1421 /* own cgroup should be read-write */
1422 if (type == LXC_AUTO_CGROUP_FULL_MIXED) {
1423 r = mount(abs_path2, abs_path2, NULL, MS_BIND, NULL);
1424 if (r < 0) {
1425 SYSERROR("error bind-mounting %s onto itself", abs_path2);
1426 goto out_error;
1427 }
1428 r = mount(NULL, abs_path2, NULL, MS_REMOUNT|MS_BIND, NULL);
1429 if (r < 0) {
1430 SYSERROR("error re-mounting %s readwrite", abs_path2);
1431 goto out_error;
1432 }
1433 }
1434 } else {
1435 /* create path for container's cgroup */
1436 r = mkdir_p(abs_path2, 0755);
1437 if (r < 0 && errno != EEXIST) {
1438 SYSERROR("could not create cgroup directory /sys/fs/cgroup/%s%s", dirname, info->cgroup_path);
1439 goto out_error;
1440 }
aae1f3c4 1441
7997d7da
CS
1442 free(abs_path);
1443 abs_path = NULL;
1444
1445 /* bind-mount container's cgroup to that directory */
1446 abs_path = cgroup_to_absolute_path(mp, info->cgroup_path, NULL);
1447 if (!abs_path)
1448 goto out_error;
1449 r = mount(abs_path, abs_path2, "none", MS_BIND, 0);
1450 if (r < 0) {
1451 SYSERROR("error bind-mounting %s to %s", abs_path, abs_path2);
1452 goto out_error;
1453 }
1454 if (type == LXC_AUTO_CGROUP_RO) {
1455 r = mount(NULL, abs_path2, NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL);
1456 if (r < 0) {
1457 SYSERROR("error re-mounting %s readonly", abs_path2);
1458 goto out_error;
1459 }
1460 }
aae1f3c4
CS
1461 }
1462
1463 free(abs_path);
1464 free(abs_path2);
1465 abs_path = NULL;
1466 abs_path2 = NULL;
1467
1468 /* add symlinks for every single subsystem */
1469 if (subsystem_count > 1) {
1470 for (i = 0; i < subsystem_count; i++) {
1471 abs_path = lxc_append_paths(path, parts[i]);
1472 if (!abs_path)
1473 goto out_error;
1474 r = symlink(dirname, abs_path);
1475 if (r < 0)
1476 WARN("could not create symlink %s -> %s in /sys/fs/cgroup of container", parts[i], dirname);
1477 free(abs_path);
1478 abs_path = NULL;
1479 }
1480 }
1481 free(dirname);
1482 free(parts);
1483 dirname = NULL;
1484 parts = NULL;
1485 }
1486
1487 /* try to remount the tmpfs readonly, since the container shouldn't
1488 * change anything (this will also make sure that trying to create
1489 * new cgroups outside the allowed area fails with an error instead
1490 * of simply causing this to create directories in the tmpfs itself)
1491 */
7997d7da
CS
1492 if (type != LXC_AUTO_CGROUP_RW && type != LXC_AUTO_CGROUP_FULL_RW)
1493 mount(NULL, path, NULL, MS_REMOUNT|MS_RDONLY, NULL);
aae1f3c4
CS
1494
1495 free(path);
1496
c476bdce 1497 return true;
aae1f3c4
CS
1498
1499out_error:
1500 saved_errno = errno;
1501 free(path);
1502 free(dirname);
1503 free(parts);
1504 free(abs_path);
1505 free(abs_path2);
1506 errno = saved_errno;
c476bdce 1507 return false;
aae1f3c4
CS
1508}
1509
4fb3cba5 1510static int cgfs_nrtasks(void *hdata)
33ad9f1a 1511{
4fb3cba5
DE
1512 struct cgfs_data *d = hdata;
1513 struct cgroup_process_info *info;
33ad9f1a
CS
1514 struct cgroup_mount_point *mp = NULL;
1515 char *abs_path = NULL;
1516 int ret;
460a1cf0 1517
4fb3cba5
DE
1518 if (!d) {
1519 errno = ENOENT;
1520 return -1;
1521 }
1522
1523 info = d->info;
33ad9f1a
CS
1524 if (!info) {
1525 errno = ENOENT;
1526 return -1;
b98f7d6e 1527 }
c8f7c563 1528
33ad9f1a 1529 if (info->designated_mount_point) {
8900b9eb 1530 mp = info->designated_mount_point;
33ad9f1a
CS
1531 } else {
1532 mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, false);
1533 if (!mp)
1534 return -1;
c8f7c563
CS
1535 }
1536
33ad9f1a
CS
1537 abs_path = cgroup_to_absolute_path(mp, info->cgroup_path, NULL);
1538 if (!abs_path)
1539 return -1;
1540
1541 ret = cgroup_recursive_task_count(abs_path);
1542 free(abs_path);
1543 return ret;
c8f7c563
CS
1544}
1545
574c4428
QH
1546static struct cgroup_process_info *
1547lxc_cgroup_process_info_getx(const char *proc_pid_cgroup_str,
1548 struct cgroup_meta_data *meta)
d08ba6ec 1549{
33ad9f1a
CS
1550 struct cgroup_process_info *result = NULL;
1551 FILE *proc_pid_cgroup = NULL;
1552 char *line = NULL;
1553 size_t sz = 0;
1554 int saved_errno = 0;
1555 struct cgroup_process_info **cptr = &result;
1556 struct cgroup_process_info *entry = NULL;
1557
1558 proc_pid_cgroup = fopen_cloexec(proc_pid_cgroup_str, "r");
1559 if (!proc_pid_cgroup)
b98f7d6e 1560 return NULL;
1ac470c0 1561
33ad9f1a
CS
1562 while (getline(&line, &sz, proc_pid_cgroup) != -1) {
1563 /* file format: hierarchy:subsystems:group */
1564 char *colon1;
1565 char *colon2;
1566 char *endptr;
1567 int hierarchy_number;
1568 struct cgroup_hierarchy *h = NULL;
fd4f5a56 1569
33ad9f1a 1570 if (!line[0])
ae5c8b8e 1571 continue;
b98f7d6e 1572
33ad9f1a
CS
1573 if (line[strlen(line) - 1] == '\n')
1574 line[strlen(line) - 1] = '\0';
1575
1576 colon1 = strchr(line, ':');
1577 if (!colon1)
8900b9eb 1578 continue;
33ad9f1a
CS
1579 *colon1++ = '\0';
1580 colon2 = strchr(colon1, ':');
1581 if (!colon2)
ae5c8b8e 1582 continue;
33ad9f1a 1583 *colon2++ = '\0';
e4659536 1584
33ad9f1a
CS
1585 endptr = NULL;
1586 hierarchy_number = strtoul(line, &endptr, 10);
1587 if (!endptr || *endptr)
9a93d992 1588 continue;
9a93d992 1589
33ad9f1a
CS
1590 if (hierarchy_number > meta->maximum_hierarchy) {
1591 /* we encountered a hierarchy we didn't have before,
1592 * so probably somebody remounted some stuff in the
1593 * mean time...
1594 */
1595 errno = EAGAIN;
1596 goto out_error;
b98f7d6e 1597 }
33ad9f1a
CS
1598
1599 h = meta->hierarchies[hierarchy_number];
1600 if (!h) {
1601 /* we encountered a hierarchy that was thought to be
1602 * dead before, so probably somebody remounted some
1603 * stuff in the mean time...
1604 */
1605 errno = EAGAIN;
1606 goto out_error;
b98f7d6e 1607 }
33ad9f1a
CS
1608
1609 /* we are told that we should ignore this hierarchy */
1610 if (!h->used)
b98f7d6e 1611 continue;
5193cc3d 1612
33ad9f1a
CS
1613 entry = calloc(1, sizeof(struct cgroup_process_info));
1614 if (!entry)
1615 goto out_error;
fd4f5a56 1616
33ad9f1a
CS
1617 entry->meta_ref = lxc_cgroup_get_meta(meta);
1618 entry->hierarchy = h;
1619 entry->cgroup_path = strdup(colon2);
1620 if (!entry->cgroup_path)
1621 goto out_error;
d08ba6ec 1622
33ad9f1a
CS
1623 *cptr = entry;
1624 cptr = &entry->next;
1625 entry = NULL;
b98f7d6e 1626 }
b98f7d6e 1627
33ad9f1a
CS
1628 fclose(proc_pid_cgroup);
1629 free(line);
1630 return result;
1631
1632out_error:
1633 saved_errno = errno;
1634 if (proc_pid_cgroup)
1635 fclose(proc_pid_cgroup);
1636 lxc_cgroup_process_info_free(result);
1637 lxc_cgroup_process_info_free(entry);
1638 free(line);
1639 errno = saved_errno;
ae5c8b8e 1640 return NULL;
36b86299
DL
1641}
1642
574c4428
QH
1643static char **subsystems_from_mount_options(const char *mount_options,
1644 char **kernel_list)
36b86299 1645{
33ad9f1a
CS
1646 char *token, *str, *saveptr = NULL;
1647 char **result = NULL;
1648 size_t result_capacity = 0;
8900b9eb 1649 size_t result_count = 0;
33ad9f1a
CS
1650 int saved_errno;
1651 int r;
ef342abb 1652
33ad9f1a
CS
1653 str = alloca(strlen(mount_options)+1);
1654 strcpy(str, mount_options);
1655 for (; (token = strtok_r(str, ",", &saveptr)); str = NULL) {
1656 /* we have a subsystem if it's either in the list of
1657 * subsystems provided by the kernel OR if it starts
1658 * with name= for named hierarchies
1659 */
1660 if (!strncmp(token, "name=", 5) || lxc_string_in_array(token, (const char **)kernel_list)) {
1661 r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 12);
1662 if (r < 0)
1663 goto out_free;
1664 result[result_count + 1] = NULL;
1665 result[result_count] = strdup(token);
1666 if (!result[result_count])
1667 goto out_free;
1668 result_count++;
1669 }
ae5c8b8e 1670 }
f0e64b8b 1671
33ad9f1a
CS
1672 return result;
1673
1674out_free:
1675 saved_errno = errno;
1676 lxc_free_array((void**)result, free);
1677 errno = saved_errno;
1678 return NULL;
b98f7d6e
SH
1679}
1680
574c4428 1681static void lxc_cgroup_mount_point_free(struct cgroup_mount_point *mp)
b98f7d6e 1682{
33ad9f1a
CS
1683 if (!mp)
1684 return;
1685 free(mp->mount_point);
1686 free(mp->mount_prefix);
1687 free(mp);
bcbd102c
SH
1688}
1689
574c4428 1690static void lxc_cgroup_hierarchy_free(struct cgroup_hierarchy *h)
341a9bd8 1691{
33ad9f1a
CS
1692 if (!h)
1693 return;
1694 lxc_free_array((void **)h->subsystems, free);
8bfcb981 1695 free(h->all_mount_points);
33ad9f1a
CS
1696 free(h);
1697}
341a9bd8 1698
574c4428 1699static bool is_valid_cgroup(const char *name)
33ad9f1a
CS
1700{
1701 const char *p;
1702 for (p = name; *p; p++) {
28bb9321
QH
1703 /* Use the ASCII printable characters range(32 - 127)
1704 * is reasonable, we kick out 32(SPACE) because it'll
1705 * break legacy lxc-ls
1706 */
1707 if (*p <= 32 || *p >= 127 || *p == '/')
33ad9f1a 1708 return false;
341a9bd8 1709 }
33ad9f1a
CS
1710 return strcmp(name, ".") != 0 && strcmp(name, "..") != 0;
1711}
341a9bd8 1712
574c4428
QH
1713static int create_or_remove_cgroup(bool do_remove,
1714 struct cgroup_mount_point *mp, const char *path, int recurse)
33ad9f1a
CS
1715{
1716 int r, saved_errno = 0;
1717 char *buf = cgroup_to_absolute_path(mp, path, NULL);
1718 if (!buf)
1719 return -1;
341a9bd8 1720
33ad9f1a 1721 /* create or remove directory */
603c64c2
SH
1722 if (do_remove) {
1723 if (recurse)
1724 r = cgroup_rmdir(buf);
1725 else
1726 r = rmdir(buf);
1727 } else
1728 r = mkdir(buf, 0777);
33ad9f1a
CS
1729 saved_errno = errno;
1730 free(buf);
1731 errno = saved_errno;
1732 return r;
341a9bd8 1733}
bcbd102c 1734
574c4428 1735static int create_cgroup(struct cgroup_mount_point *mp, const char *path)
a6ddef61 1736{
603c64c2 1737 return create_or_remove_cgroup(false, mp, path, false);
a6ddef61
MN
1738}
1739
574c4428
QH
1740static int remove_cgroup(struct cgroup_mount_point *mp,
1741 const char *path, bool recurse)
576f946d 1742{
603c64c2 1743 return create_or_remove_cgroup(true, mp, path, recurse);
33ad9f1a 1744}
576f946d 1745
574c4428
QH
1746static char *cgroup_to_absolute_path(struct cgroup_mount_point *mp,
1747 const char *path, const char *suffix)
33ad9f1a
CS
1748{
1749 /* first we have to make sure we subtract the mount point's prefix */
1750 char *prefix = mp->mount_prefix;
1751 char *buf;
1752 ssize_t len, rv;
1753
1754 /* we want to make sure only absolute paths to cgroups are passed to us */
1755 if (path[0] != '/') {
1756 errno = EINVAL;
1757 return NULL;
1758 }
b98f7d6e 1759
33ad9f1a
CS
1760 if (prefix && !strcmp(prefix, "/"))
1761 prefix = NULL;
b98f7d6e 1762
33ad9f1a
CS
1763 /* prefix doesn't match */
1764 if (prefix && strncmp(prefix, path, strlen(prefix)) != 0) {
1765 errno = EINVAL;
1766 return NULL;
1767 }
1768 /* if prefix is /foo and path is /foobar */
1769 if (prefix && path[strlen(prefix)] != '/' && path[strlen(prefix)] != '\0') {
1770 errno = EINVAL;
1771 return NULL;
1772 }
b98f7d6e 1773
33ad9f1a
CS
1774 /* remove prefix from path */
1775 path += prefix ? strlen(prefix) : 0;
b98f7d6e 1776
33ad9f1a
CS
1777 len = strlen(mp->mount_point) + strlen(path) + (suffix ? strlen(suffix) : 0);
1778 buf = calloc(len + 1, 1);
50266dc6
DE
1779 if (!buf)
1780 return NULL;
33ad9f1a 1781 rv = snprintf(buf, len + 1, "%s%s%s", mp->mount_point, path, suffix ? suffix : "");
8900b9eb 1782 if (rv > len) {
33ad9f1a
CS
1783 free(buf);
1784 errno = ENOMEM;
8900b9eb 1785 return NULL;
8b92dc3a 1786 }
576f946d 1787
33ad9f1a 1788 return buf;
e0f888d9 1789}
283678ed 1790
574c4428
QH
1791static struct cgroup_process_info *
1792find_info_for_subsystem(struct cgroup_process_info *info, const char *subsystem)
283678ed 1793{
33ad9f1a
CS
1794 struct cgroup_process_info *info_ptr;
1795 for (info_ptr = info; info_ptr; info_ptr = info_ptr->next) {
1796 struct cgroup_hierarchy *h = info_ptr->hierarchy;
1797 if (lxc_string_in_array(subsystem, (const char **)h->subsystems))
1798 return info_ptr;
b98f7d6e 1799 }
33ad9f1a
CS
1800 errno = ENOENT;
1801 return NULL;
1802}
283678ed 1803
574c4428
QH
1804static int do_cgroup_get(const char *cgroup_path, const char *sub_filename,
1805 char *value, size_t len)
33ad9f1a
CS
1806{
1807 const char *parts[3] = {
1808 cgroup_path,
1809 sub_filename,
1810 NULL
1811 };
1812 char *filename;
1813 int ret, saved_errno;
1814
1815 filename = lxc_string_join("/", parts, false);
1816 if (!filename)
1817 return -1;
1818
1819 ret = lxc_read_from_file(filename, value, len);
1820 saved_errno = errno;
1821 free(filename);
1822 errno = saved_errno;
1823 return ret;
283678ed 1824}
b113383b 1825
574c4428
QH
1826static int do_cgroup_set(const char *cgroup_path, const char *sub_filename,
1827 const char *value)
b113383b 1828{
33ad9f1a
CS
1829 const char *parts[3] = {
1830 cgroup_path,
1831 sub_filename,
1832 NULL
1833 };
1834 char *filename;
1835 int ret, saved_errno;
b113383b 1836
33ad9f1a
CS
1837 filename = lxc_string_join("/", parts, false);
1838 if (!filename)
1839 return -1;
b113383b 1840
33ad9f1a
CS
1841 ret = lxc_write_to_file(filename, value, strlen(value), false);
1842 saved_errno = errno;
1843 free(filename);
1844 errno = saved_errno;
1845 return ret;
b98f7d6e
SH
1846}
1847
4fb3cba5 1848static int do_setup_cgroup_limits(struct cgfs_data *d,
574c4428 1849 struct lxc_list *cgroup_settings, bool do_devices)
b98f7d6e
SH
1850{
1851 struct lxc_list *iterator;
1852 struct lxc_cgroup *cg;
1853 int ret = -1;
1854
33ad9f1a 1855 if (lxc_list_empty(cgroup_settings))
b98f7d6e
SH
1856 return 0;
1857
33ad9f1a 1858 lxc_list_for_each(iterator, cgroup_settings) {
b98f7d6e
SH
1859 cg = iterator->elem;
1860
33ad9f1a 1861 if (do_devices == !strncmp("devices", cg->subsystem, 7)) {
b98f7d6e 1862 if (strcmp(cg->subsystem, "devices.deny") == 0 &&
4fb3cba5 1863 cgroup_devices_has_allow_or_deny(d, cg->value, false))
b98f7d6e
SH
1864 continue;
1865 if (strcmp(cg->subsystem, "devices.allow") == 0 &&
4fb3cba5 1866 cgroup_devices_has_allow_or_deny(d, cg->value, true))
b98f7d6e 1867 continue;
4fb3cba5 1868 if (lxc_cgroup_set_data(cg->subsystem, cg->value, d)) {
959aee9c 1869 ERROR("Error setting %s to %s for %s",
4fb3cba5 1870 cg->subsystem, cg->value, d->name);
b98f7d6e
SH
1871 goto out;
1872 }
b113383b 1873 }
b98f7d6e
SH
1874
1875 DEBUG("cgroup '%s' set to '%s'", cg->subsystem, cg->value);
b113383b
SH
1876 }
1877
b98f7d6e
SH
1878 ret = 0;
1879 INFO("cgroup has been setup");
1880out:
b113383b
SH
1881 return ret;
1882}
b98f7d6e 1883
4fb3cba5 1884static bool cgroup_devices_has_allow_or_deny(struct cgfs_data *d,
574c4428 1885 char *v, bool for_allow)
33ad9f1a
CS
1886{
1887 char *path;
1888 FILE *devices_list;
8900b9eb 1889 char *line = NULL;
33ad9f1a
CS
1890 size_t sz = 0;
1891 bool ret = !for_allow;
1892 const char *parts[3] = {
1893 NULL,
1894 "devices.list",
1895 NULL
1896 };
1897
1898 // XXX FIXME if users could use something other than 'lxc.devices.deny = a'.
1899 // not sure they ever do, but they *could*
1900 // right now, I'm assuming they do NOT
1901 if (!for_allow && strcmp(v, "a") != 0 && strcmp(v, "a *:* rwm") != 0)
1902 return false;
1903
4fb3cba5 1904 parts[0] = (const char *)lxc_cgroup_get_hierarchy_abs_path_data("devices", d);
33ad9f1a
CS
1905 if (!parts[0])
1906 return false;
1907 path = lxc_string_join("/", parts, false);
1908 if (!path) {
1909 free((void *)parts[0]);
1910 return false;
1911 }
1912
1913 devices_list = fopen_cloexec(path, "r");
1914 if (!devices_list) {
1915 free(path);
1916 return false;
1917 }
1918
1919 while (getline(&line, &sz, devices_list) != -1) {
1920 size_t len = strlen(line);
1921 if (len > 0 && line[len-1] == '\n')
1922 line[len-1] = '\0';
1923 if (strcmp(line, "a *:* rwm") == 0) {
1924 ret = for_allow;
1925 goto out;
1926 } else if (for_allow && strcmp(line, v) == 0) {
1927 ret = true;
8900b9eb 1928 goto out;
33ad9f1a
CS
1929 }
1930 }
1931
1932out:
1933 fclose(devices_list);
1934 free(line);
1935 free(path);
1936 return ret;
1937}
1938
574c4428 1939static int cgroup_recursive_task_count(const char *cgroup_path)
b98f7d6e 1940{
33ad9f1a
CS
1941 DIR *d;
1942 struct dirent *dent_buf;
1943 struct dirent *dent;
8900b9eb 1944 ssize_t name_max;
33ad9f1a
CS
1945 int n = 0, r;
1946
1947 /* see man readdir_r(3) */
1948 name_max = pathconf(cgroup_path, _PC_NAME_MAX);
1949 if (name_max <= 0)
1950 name_max = 255;
1951 dent_buf = malloc(offsetof(struct dirent, d_name) + name_max + 1);
1952 if (!dent_buf)
1953 return -1;
1954
1955 d = opendir(cgroup_path);
034ef75d
SH
1956 if (!d) {
1957 free(dent_buf);
33ad9f1a 1958 return 0;
034ef75d 1959 }
33ad9f1a
CS
1960
1961 while (readdir_r(d, dent_buf, &dent) == 0 && dent) {
1962 const char *parts[3] = {
1963 cgroup_path,
1964 dent->d_name,
1965 NULL
1966 };
1967 char *sub_path;
1968 struct stat st;
1969
1970 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1971 continue;
1972 sub_path = lxc_string_join("/", parts, false);
1973 if (!sub_path) {
1974 closedir(d);
1975 free(dent_buf);
1976 return -1;
1977 }
1978 r = stat(sub_path, &st);
1979 if (r < 0) {
1980 closedir(d);
1981 free(dent_buf);
1982 free(sub_path);
1983 return -1;
1984 }
1985 if (S_ISDIR(st.st_mode)) {
1986 r = cgroup_recursive_task_count(sub_path);
1987 if (r >= 0)
1988 n += r;
1989 } else if (!strcmp(dent->d_name, "tasks")) {
1990 r = count_lines(sub_path);
1991 if (r >= 0)
1992 n += r;
1993 }
1994 free(sub_path);
1995 }
1996 closedir(d);
1997 free(dent_buf);
1998
1999 return n;
2000}
2001
574c4428 2002static int count_lines(const char *fn)
33ad9f1a
CS
2003{
2004 FILE *f;
2005 char *line = NULL;
2006 size_t sz = 0;
2007 int n = 0;
2008
2009 f = fopen_cloexec(fn, "r");
2010 if (!f)
2011 return -1;
2012
2013 while (getline(&line, &sz, f) != -1) {
2014 n++;
2015 }
2016 free(line);
2017 fclose(f);
2018 return n;
b98f7d6e
SH
2019}
2020
574c4428
QH
2021static int handle_cgroup_settings(struct cgroup_mount_point *mp,
2022 char *cgroup_path)
b98f7d6e 2023{
33ad9f1a 2024 int r, saved_errno = 0;
7e7243e1 2025 char buf[2];
1ea59ad2 2026
934b1673
SH
2027 mp->need_cpuset_init = false;
2028
1ea59ad2
SH
2029 /* If this is the memory cgroup, we want to enforce hierarchy.
2030 * But don't fail if for some reason we can't.
2031 */
2edb53c7
SH
2032 if (lxc_string_in_array("memory", (const char **)mp->hierarchy->subsystems)) {
2033 char *cc_path = cgroup_to_absolute_path(mp, cgroup_path, "/memory.use_hierarchy");
2034 if (cc_path) {
2035 r = lxc_read_from_file(cc_path, buf, 1);
2036 if (r < 1 || buf[0] != '1') {
2037 r = lxc_write_to_file(cc_path, "1", 1, false);
2038 if (r < 0)
2039 SYSERROR("failed to set memory.use_hiararchy to 1; continuing");
2040 }
1ea59ad2
SH
2041 free(cc_path);
2042 }
2edb53c7 2043 }
1ea59ad2 2044
33ad9f1a
CS
2045 /* if this is a cpuset hierarchy, we have to set cgroup.clone_children in
2046 * the base cgroup, otherwise containers will start with an empty cpuset.mems
2047 * and cpuset.cpus and then
2048 */
2edb53c7
SH
2049 if (lxc_string_in_array("cpuset", (const char **)mp->hierarchy->subsystems)) {
2050 char *cc_path = cgroup_to_absolute_path(mp, cgroup_path, "/cgroup.clone_children");
d703c2b1
RV
2051 struct stat sb;
2052
33ad9f1a 2053 if (!cc_path)
2edb53c7 2054 return -1;
d703c2b1
RV
2055 /* cgroup.clone_children is not available when running under
2056 * older kernel versions; in this case, we'll initialize
2057 * cpuset.cpus and cpuset.mems later, after the new cgroup
2058 * was created
2059 */
2060 if (stat(cc_path, &sb) != 0 && errno == ENOENT) {
934b1673 2061 mp->need_cpuset_init = true;
d703c2b1
RV
2062 free(cc_path);
2063 return 0;
2064 }
7e7243e1
SH
2065 r = lxc_read_from_file(cc_path, buf, 1);
2066 if (r == 1 && buf[0] == '1') {
2067 free(cc_path);
2edb53c7 2068 return 0;
7e7243e1 2069 }
33ad9f1a 2070 r = lxc_write_to_file(cc_path, "1", 1, false);
2edb53c7
SH
2071 saved_errno = errno;
2072 free(cc_path);
2073 errno = saved_errno;
2074 return r < 0 ? -1 : 0;
33ad9f1a
CS
2075 }
2076 return 0;
b98f7d6e 2077}
484ed030 2078
934b1673 2079static int cgroup_read_from_file(const char *fn, char buf[], size_t bufsize)
d703c2b1
RV
2080{
2081 int ret = lxc_read_from_file(fn, buf, bufsize);
2082 if (ret < 0) {
2083 SYSERROR("failed to read %s", fn);
934b1673 2084 return ret;
d703c2b1
RV
2085 }
2086 if (ret == bufsize) {
934b1673
SH
2087 if (bufsize > 0) {
2088 /* obviously this wasn't empty */
2089 buf[bufsize-1] = '\0';
2090 return ret;
2091 }
2092 /* Callers don't do this, but regression/sanity check */
2093 ERROR("%s: was not expecting 0 bufsize", __func__);
2094 return -1;
d703c2b1
RV
2095 }
2096 buf[ret] = '\0';
934b1673 2097 return ret;
d703c2b1
RV
2098}
2099
2100static bool do_init_cpuset_file(struct cgroup_mount_point *mp,
2101 const char *path, const char *name)
2102{
934b1673
SH
2103 char value[1024];
2104 char *childfile, *parentfile = NULL, *tmp;
2105 int ret;
2106 bool ok = false;
2107
d703c2b1
RV
2108 childfile = cgroup_to_absolute_path(mp, path, name);
2109 if (!childfile)
2110 return false;
2111
2112 /* don't overwrite a non-empty value in the file */
934b1673
SH
2113 ret = cgroup_read_from_file(childfile, value, sizeof(value));
2114 if (ret < 0)
2115 goto out;
d703c2b1 2116 if (value[0] != '\0' && value[0] != '\n') {
934b1673
SH
2117 ok = true;
2118 goto out;
d703c2b1
RV
2119 }
2120
2121 /* path to the same name in the parent cgroup */
2122 parentfile = strdup(path);
2123 if (!parentfile)
934b1673
SH
2124 goto out;
2125
d703c2b1 2126 tmp = strrchr(parentfile, '/');
934b1673
SH
2127 if (!tmp)
2128 goto out;
d703c2b1
RV
2129 if (tmp == parentfile)
2130 tmp++; /* keep the '/' at the start */
2131 *tmp = '\0';
2132 tmp = parentfile;
2133 parentfile = cgroup_to_absolute_path(mp, tmp, name);
2134 free(tmp);
934b1673
SH
2135 if (!parentfile)
2136 goto out;
d703c2b1
RV
2137
2138 /* copy from parent to child cgroup */
934b1673
SH
2139 ret = cgroup_read_from_file(parentfile, value, sizeof(value));
2140 if (ret < 0)
2141 goto out;
2142 if (ret == sizeof(value)) {
2143 /* If anyone actually sees this error, we can address it */
2144 ERROR("parent cpuset value too long");
2145 goto out;
d703c2b1
RV
2146 }
2147 ok = (lxc_write_to_file(childfile, value, strlen(value), false) >= 0);
2148 if (!ok)
2149 SYSERROR("failed writing %s", childfile);
b1dad6f6
RV
2150
2151out:
934b1673
SH
2152 if (parentfile)
2153 free(parentfile);
d703c2b1 2154 free(childfile);
d703c2b1
RV
2155 return ok;
2156}
2157
2158static bool init_cpuset_if_needed(struct cgroup_mount_point *mp,
2159 const char *path)
2160{
2161 /* the files we have to handle here are only in cpuset hierarchies */
2162 if (!lxc_string_in_array("cpuset",
2163 (const char **)mp->hierarchy->subsystems))
2164 return true;
2165
b1dad6f6
RV
2166 if (!mp->need_cpuset_init)
2167 return true;
2168
d703c2b1
RV
2169 return (do_init_cpuset_file(mp, path, "/cpuset.cpus") &&
2170 do_init_cpuset_file(mp, path, "/cpuset.mems") );
2171}
2172
4fb3cba5 2173struct cgroup_ops *cgfs_ops_init(void)
484ed030 2174{
4fb3cba5 2175 return &cgfs_ops;
d4ef7c50 2176}
484ed030 2177
4fb3cba5 2178static void *cgfs_init(const char *name)
d4ef7c50 2179{
4fb3cba5 2180 struct cgfs_data *d;
484ed030 2181
4fb3cba5
DE
2182 d = malloc(sizeof(*d));
2183 if (!d)
2184 return NULL;
484ed030 2185
4fb3cba5
DE
2186 memset(d, 0, sizeof(*d));
2187 d->name = strdup(name);
2188 if (!d->name)
2189 goto err1;
2190
2191 /* if we are running as root, use system cgroup pattern, otherwise
2192 * just create a cgroup under the current one. But also fall back to
2193 * that if for some reason reading the configuration fails and no
2194 * default value is available
2195 */
2196 if (geteuid() == 0)
2197 d->cgroup_pattern = lxc_global_config_value("lxc.cgroup.pattern");
2198 if (!d->cgroup_pattern)
2199 d->cgroup_pattern = "%n";
2200
2201 d->meta = lxc_cgroup_load_meta();
2202 if (!d->meta) {
2203 ERROR("cgroupfs failed to detect cgroup metadata");
2204 goto err2;
2205 }
2206 return d;
2207
2208err2:
2209 free(d->name);
2210err1:
2211 free(d);
2212 return NULL;
d4ef7c50 2213}
484ed030 2214
4fb3cba5 2215static void cgfs_destroy(void *hdata)
d4ef7c50 2216{
4fb3cba5
DE
2217 struct cgfs_data *d = hdata;
2218
d4ef7c50
SH
2219 if (!d)
2220 return;
4fb3cba5
DE
2221 if (d->name)
2222 free(d->name);
d4ef7c50
SH
2223 if (d->info)
2224 lxc_cgroup_process_info_free_and_remove(d->info);
2225 if (d->meta)
2226 lxc_cgroup_put_meta(d->meta);
2227 free(d);
d4ef7c50 2228}
484ed030 2229
4fb3cba5 2230static inline bool cgfs_create(void *hdata)
d4ef7c50 2231{
4fb3cba5
DE
2232 struct cgfs_data *d = hdata;
2233 struct cgroup_process_info *i;
2234 struct cgroup_meta_data *md;
484ed030 2235
4fb3cba5 2236 if (!d)
d4ef7c50 2237 return false;
4fb3cba5
DE
2238 md = d->meta;
2239 i = lxc_cgroupfs_create(d->name, d->cgroup_pattern, md, NULL);
d4ef7c50
SH
2240 if (!i)
2241 return false;
2242 d->info = i;
2243 return true;
2244}
484ed030 2245
4fb3cba5 2246static inline bool cgfs_enter(void *hdata, pid_t pid)
d4ef7c50 2247{
4fb3cba5
DE
2248 struct cgfs_data *d = hdata;
2249 struct cgroup_process_info *i;
d4ef7c50 2250 int ret;
4fb3cba5
DE
2251
2252 if (!d)
2253 return false;
2254 i = d->info;
2255 ret = lxc_cgroupfs_enter(i, pid, false);
484ed030 2256
d4ef7c50
SH
2257 return ret == 0;
2258}
2259
4fb3cba5 2260static inline bool cgfs_create_legacy(void *hdata, pid_t pid)
d4ef7c50 2261{
4fb3cba5
DE
2262 struct cgfs_data *d = hdata;
2263 struct cgroup_process_info *i;
2264
2265 if (!d)
2266 return false;
2267 i = d->info;
2268 if (lxc_cgroup_create_legacy(i, d->name, pid) < 0) {
2269 ERROR("failed to create legacy ns cgroups for '%s'", d->name);
d4ef7c50 2270 return false;
484ed030 2271 }
d4ef7c50
SH
2272 return true;
2273}
484ed030 2274
4fb3cba5 2275static const char *cgfs_get_cgroup(void *hdata, const char *subsystem)
d4ef7c50 2276{
4fb3cba5
DE
2277 struct cgfs_data *d = hdata;
2278
2279 if (!d)
2280 return NULL;
2281 return lxc_cgroup_get_hierarchy_path_data(subsystem, d);
484ed030
SH
2282}
2283
4fb3cba5 2284static bool cgfs_unfreeze(void *hdata)
0086f499 2285{
4fb3cba5 2286 struct cgfs_data *d = hdata;
0086f499
SH
2287 char *cgabspath, *cgrelpath;
2288 int ret;
2289
4fb3cba5
DE
2290 if (!d)
2291 return false;
2292
2293 cgrelpath = lxc_cgroup_get_hierarchy_path_data("freezer", d);
0086f499
SH
2294 cgabspath = lxc_cgroup_find_abs_path("freezer", cgrelpath, true, NULL);
2295 if (!cgabspath)
ecfcb3f0 2296 return false;
0086f499
SH
2297
2298 ret = do_cgroup_set(cgabspath, "freezer.state", "THAWED");
2299 free(cgabspath);
ecfcb3f0 2300 return ret == 0;
0086f499
SH
2301}
2302
4fb3cba5
DE
2303static bool cgroupfs_setup_limits(void *hdata, struct lxc_list *cgroup_conf,
2304 bool with_devices)
9daf6f5d 2305{
4fb3cba5
DE
2306 struct cgfs_data *d = hdata;
2307
2308 if (!d)
2309 return false;
2310 return do_setup_cgroup_limits(d, cgroup_conf, with_devices) == 0;
9daf6f5d
SH
2311}
2312
4fb3cba5 2313static bool lxc_cgroupfs_attach(const char *name, const char *lxcpath, pid_t pid)
5d897655
SH
2314{
2315 struct cgroup_meta_data *meta_data;
2316 struct cgroup_process_info *container_info;
2317 int ret;
2318
2319 meta_data = lxc_cgroup_load_meta();
2320 if (!meta_data) {
2321 ERROR("could not move attached process %d to cgroup of container", pid);
2322 return false;
2323 }
2324
2325 container_info = lxc_cgroup_get_container_info(name, lxcpath, meta_data);
2326 lxc_cgroup_put_meta(meta_data);
2327 if (!container_info) {
2328 ERROR("could not move attached process %d to cgroup of container", pid);
2329 return false;
2330 }
2331
2332 ret = lxc_cgroupfs_enter(container_info, pid, false);
2333 lxc_cgroup_process_info_free(container_info);
2334 if (ret < 0) {
2335 ERROR("could not move attached process %d to cgroup of container", pid);
2336 return false;
2337 }
2338 return true;
2339}
2340
d4ef7c50 2341static struct cgroup_ops cgfs_ops = {
d4ef7c50 2342 .init = cgfs_init,
4fb3cba5 2343 .destroy = cgfs_destroy,
d4ef7c50
SH
2344 .create = cgfs_create,
2345 .enter = cgfs_enter,
2346 .create_legacy = cgfs_create_legacy,
2347 .get_cgroup = cgfs_get_cgroup,
2348 .get = lxc_cgroupfs_get,
2349 .set = lxc_cgroupfs_set,
4fb3cba5 2350 .unfreeze = cgfs_unfreeze,
9daf6f5d 2351 .setup_limits = cgroupfs_setup_limits,
d4ef7c50 2352 .name = "cgroupfs",
5d897655 2353 .attach = lxc_cgroupfs_attach,
0996e18a 2354 .chown = NULL,
c476bdce 2355 .mount_cgroup = cgroupfs_mount_cgroup,
4fb3cba5 2356 .nrtasks = cgfs_nrtasks,
d4ef7c50 2357};