]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/cgroup.c
Automatic mounting: write lxc.mount.auto in write_config
[mirror_lxc.git] / src / lxc / cgroup.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 */
23#define _GNU_SOURCE
24#include <stdio.h>
25#undef _GNU_SOURCE
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"
881450bb 42#include "config.h"
ae5c8b8e 43#include "commands.h"
b98f7d6e
SH
44#include "list.h"
45#include "conf.h"
33ad9f1a 46#include "utils.h"
740d1928 47#include "bdev.h"
025ed0f3 48#include "lxclock.h"
36eb9bde 49
36eb9bde 50#include <lxc/log.h>
00b3c2e2
CLG
51#include <lxc/cgroup.h>
52#include <lxc/start.h>
36eb9bde 53
edaf8b1b
SG
54#if IS_BIONIC
55#include <../include/lxcmntent.h>
56#else
57#include <mntent.h>
58#endif
59
120ce443
SG
60#ifndef HAVE_GETLINE
61#ifdef HAVE_FGETLN
62#include <../include/getline.h>
63#endif
64#endif
65
36eb9bde 66lxc_log_define(lxc_cgroup, lxc);
576f946d 67
33ad9f1a
CS
68static struct cgroup_process_info *lxc_cgroup_process_info_getx(const char *proc_pid_cgroup_str, struct cgroup_meta_data *meta);
69static char **subsystems_from_mount_options(const char *mount_options, char **kernel_list);
70static void lxc_cgroup_mount_point_free(struct cgroup_mount_point *mp);
71static void lxc_cgroup_hierarchy_free(struct cgroup_hierarchy *h);
72static bool is_valid_cgroup(const char *name);
73static int create_or_remove_cgroup(bool remove, struct cgroup_mount_point *mp, const char *path);
74static int create_cgroup(struct cgroup_mount_point *mp, const char *path);
75static int remove_cgroup(struct cgroup_mount_point *mp, const char *path);
76static char *cgroup_to_absolute_path(struct cgroup_mount_point *mp, const char *path, const char *suffix);
77static struct cgroup_process_info *find_info_for_subsystem(struct cgroup_process_info *info, const char *subsystem);
78static int do_cgroup_get(const char *cgroup_path, const char *sub_filename, char *value, size_t len);
79static int do_cgroup_set(const char *cgroup_path, const char *sub_filename, const char *value);
80static bool cgroup_devices_has_allow_or_deny(struct lxc_handler *h, char *v, bool for_allow);
81static int do_setup_cgroup(struct lxc_handler *h, struct lxc_list *cgroup_settings, bool do_devices);
82static int cgroup_recursive_task_count(const char *cgroup_path);
83static int count_lines(const char *fn);
84static int handle_clone_children(struct cgroup_mount_point *mp, char *cgroup_path);
85
86struct cgroup_meta_data *lxc_cgroup_load_meta()
87{
88 const char *cgroup_use = NULL;
89 char **cgroup_use_list = NULL;
90 struct cgroup_meta_data *md = NULL;
91 int saved_errno;
92
93 errno = 0;
94 cgroup_use = lxc_global_config_value("cgroup.use");
95 if (!cgroup_use && errno != 0)
96 return NULL;
97 if (cgroup_use) {
98 cgroup_use_list = lxc_string_split_and_trim(cgroup_use, ',');
99 if (!cgroup_use_list)
100 return NULL;
101 }
576f946d 102
33ad9f1a
CS
103 md = lxc_cgroup_load_meta2((const char **)cgroup_use_list);
104 saved_errno = errno;
105 lxc_free_array((void **)cgroup_use_list, free);
106 errno = saved_errno;
107 return md;
108}
fd37327f 109
b653309a
SH
110/* Step 1: determine all kernel subsystems */
111static bool find_cgroup_subsystems(char ***kernel_subsystems)
1d39a065 112{
b653309a
SH
113 FILE *proc_cgroups;
114 bool bret = false;
33ad9f1a
CS
115 char *line = NULL;
116 size_t sz = 0;
b653309a
SH
117 size_t kernel_subsystems_count = 0;
118 size_t kernel_subsystems_capacity = 0;
119 int r;
1d39a065 120
025ed0f3 121 process_lock();
33ad9f1a 122 proc_cgroups = fopen_cloexec("/proc/cgroups", "r");
025ed0f3 123 process_unlock();
33ad9f1a 124 if (!proc_cgroups)
b653309a 125 return false;
1d39a065 126
33ad9f1a
CS
127 while (getline(&line, &sz, proc_cgroups) != -1) {
128 char *tab1;
129 char *tab2;
130 int hierarchy_number;
1d39a065 131
33ad9f1a
CS
132 if (line[0] == '#')
133 continue;
134 if (!line[0])
135 continue;
1d39a065 136
33ad9f1a
CS
137 tab1 = strchr(line, '\t');
138 if (!tab1)
8900b9eb 139 continue;
33ad9f1a
CS
140 *tab1++ = '\0';
141 tab2 = strchr(tab1, '\t');
142 if (!tab2)
143 continue;
144 *tab2 = '\0';
fd37327f 145
33ad9f1a
CS
146 tab2 = NULL;
147 hierarchy_number = strtoul(tab1, &tab2, 10);
148 if (!tab2 || *tab2)
149 continue;
150 (void)hierarchy_number;
151
b653309a 152 r = lxc_grow_array((void ***)kernel_subsystems, &kernel_subsystems_capacity, kernel_subsystems_count + 1, 12);
33ad9f1a 153 if (r < 0)
b653309a
SH
154 goto out;
155 (*kernel_subsystems)[kernel_subsystems_count] = strdup(line);
156 if (!(*kernel_subsystems)[kernel_subsystems_count])
157 goto out;
33ad9f1a 158 kernel_subsystems_count++;
bcbd102c 159 }
b653309a 160 bret = true;
0d9f8e18 161
b653309a 162out:
025ed0f3 163 process_lock();
33ad9f1a 164 fclose(proc_cgroups);
025ed0f3 165 process_unlock();
0ccf7c2a 166 free(line);
b653309a
SH
167 return bret;
168}
169
170/* Step 2: determine all hierarchies (by reading /proc/self/cgroup),
171 * since mount points don't specify hierarchy number and
172 * /proc/cgroups does not contain named hierarchies
173 */
174static bool find_cgroup_hierarchies(struct cgroup_meta_data *meta_data,
175 bool all_kernel_subsystems, bool all_named_subsystems,
176 const char **subsystem_whitelist)
177{
178 FILE *proc_self_cgroup;
179 char *line = NULL;
180 size_t sz = 0;
181 int r;
182 bool bret = false;
183 size_t hierarchy_capacity = 0;
ef6e34ee 184
025ed0f3 185 process_lock();
33ad9f1a
CS
186 proc_self_cgroup = fopen_cloexec("/proc/self/cgroup", "r");
187 /* if for some reason (because of setns() and pid namespace for example),
188 * /proc/self is not valid, we try /proc/1/cgroup... */
189 if (!proc_self_cgroup)
190 proc_self_cgroup = fopen_cloexec("/proc/1/cgroup", "r");
025ed0f3 191 process_unlock();
33ad9f1a 192 if (!proc_self_cgroup)
b653309a 193 return false;
33ad9f1a
CS
194
195 while (getline(&line, &sz, proc_self_cgroup) != -1) {
196 /* file format: hierarchy:subsystems:group,
197 * we only extract hierarchy and subsystems
198 * here */
199 char *colon1;
200 char *colon2;
201 int hierarchy_number;
202 struct cgroup_hierarchy *h = NULL;
203 char **p;
204
205 if (!line[0])
206 continue;
ad08bbb7 207
33ad9f1a
CS
208 colon1 = strchr(line, ':');
209 if (!colon1)
8900b9eb 210 continue;
33ad9f1a
CS
211 *colon1++ = '\0';
212 colon2 = strchr(colon1, ':');
213 if (!colon2)
214 continue;
215 *colon2 = '\0';
ad08bbb7 216
33ad9f1a
CS
217 colon2 = NULL;
218 hierarchy_number = strtoul(line, &colon2, 10);
219 if (!colon2 || *colon2)
220 continue;
576f946d 221
33ad9f1a
CS
222 if (hierarchy_number > meta_data->maximum_hierarchy) {
223 /* lxc_grow_array will never shrink, so even if we find a lower
224 * hierarchy number here, the array will never be smaller
225 */
226 r = lxc_grow_array((void ***)&meta_data->hierarchies, &hierarchy_capacity, hierarchy_number + 1, 12);
227 if (r < 0)
b653309a 228 goto out;
5193cc3d 229
33ad9f1a
CS
230 meta_data->maximum_hierarchy = hierarchy_number;
231 }
fd37327f 232
33ad9f1a
CS
233 /* this shouldn't happen, we had this already */
234 if (meta_data->hierarchies[hierarchy_number])
b653309a 235 goto out;
33ad9f1a
CS
236
237 h = calloc(1, sizeof(struct cgroup_hierarchy));
238 if (!h)
b653309a 239 goto out;
33ad9f1a
CS
240
241 meta_data->hierarchies[hierarchy_number] = h;
242
243 h->index = hierarchy_number;
244 h->subsystems = lxc_string_split_and_trim(colon1, ',');
245 if (!h->subsystems)
b653309a 246 goto out;
33ad9f1a
CS
247 /* see if this hierarchy should be considered */
248 if (!all_kernel_subsystems || !all_named_subsystems) {
249 for (p = h->subsystems; *p; p++) {
250 if (!strncmp(*p, "name=", 5)) {
251 if (all_named_subsystems || (subsystem_whitelist && lxc_string_in_array(*p, subsystem_whitelist))) {
252 h->used = true;
253 break;
254 }
255 } else {
256 if (all_kernel_subsystems || (subsystem_whitelist && lxc_string_in_array(*p, subsystem_whitelist))) {
257 h->used = true;
258 break;
259 }
260 }
261 }
262 } else {
263 /* we want all hierarchy anyway */
264 h->used = true;
ae5c8b8e 265 }
ae5c8b8e 266 }
b653309a 267 bret = true;
0b9c21ab 268
b653309a 269out:
025ed0f3 270 process_lock();
33ad9f1a 271 fclose(proc_self_cgroup);
025ed0f3 272 process_unlock();
0ccf7c2a 273 free(line);
b653309a
SH
274 return bret;
275}
276
277/* Step 3: determine all mount points of each hierarchy */
278static bool find_hierarchy_mountpts( struct cgroup_meta_data *meta_data, char **kernel_subsystems)
279{
280 bool bret = false;
281 FILE *proc_self_mountinfo;
282 char *line = NULL;
283 size_t sz = 0;
284 char **tokens = NULL;
285 size_t mount_point_count = 0;
286 size_t mount_point_capacity = 0;
287 size_t token_capacity = 0;
288 int r;
289
025ed0f3 290 process_lock();
33ad9f1a
CS
291 proc_self_mountinfo = fopen_cloexec("/proc/self/mountinfo", "r");
292 /* if for some reason (because of setns() and pid namespace for example),
293 * /proc/self is not valid, we try /proc/1/cgroup... */
294 if (!proc_self_mountinfo)
295 proc_self_mountinfo = fopen_cloexec("/proc/1/mountinfo", "r");
025ed0f3 296 process_unlock();
33ad9f1a 297 if (!proc_self_mountinfo)
b653309a 298 return false;
33ad9f1a
CS
299
300 while (getline(&line, &sz, proc_self_mountinfo) != -1) {
178938fe 301 char *token, *line_tok, *saveptr = NULL;
33ad9f1a
CS
302 size_t i, j, k;
303 struct cgroup_mount_point *mount_point;
304 struct cgroup_hierarchy *h;
305 char **subsystems;
306
307 if (line[0] && line[strlen(line) - 1] == '\n')
308 line[strlen(line) - 1] = '\0';
309
178938fe 310 for (i = 0, line_tok = line; (token = strtok_r(line_tok, " ", &saveptr)); line_tok = NULL) {
33ad9f1a
CS
311 r = lxc_grow_array((void ***)&tokens, &token_capacity, i + 1, 64);
312 if (r < 0)
b653309a 313 goto out;
33ad9f1a
CS
314 tokens[i++] = token;
315 }
b98f7d6e 316
33ad9f1a
CS
317 /* layout of /proc/self/mountinfo:
318 * 0: id
319 * 1: parent id
320 * 2: device major:minor
321 * 3: mount prefix
8900b9eb 322 * 4: mount point
33ad9f1a
CS
323 * 5: per-mount options
324 * [optional X]: additional data
325 * X+7: "-"
326 * X+8: type
327 * X+9: source
328 * X+10: per-superblock options
329 */
330 for (j = 6; j < i && tokens[j]; j++)
331 if (!strcmp(tokens[j], "-"))
332 break;
fd4f5a56 333
33ad9f1a
CS
334 /* could not find separator */
335 if (j >= i || !tokens[j])
336 continue;
337 /* there should be exactly three fields after
338 * the separator
339 */
340 if (i != j + 4)
341 continue;
fd4f5a56 342
33ad9f1a
CS
343 /* not a cgroup filesystem */
344 if (strcmp(tokens[j + 1], "cgroup") != 0)
345 continue;
b98f7d6e 346
33ad9f1a
CS
347 subsystems = subsystems_from_mount_options(tokens[j + 3], kernel_subsystems);
348 if (!subsystems)
b653309a 349 goto out;
33ad9f1a
CS
350
351 h = NULL;
352 for (k = 1; k <= meta_data->maximum_hierarchy; k++) {
353 if (meta_data->hierarchies[k] &&
354 meta_data->hierarchies[k]->subsystems[0] &&
355 lxc_string_in_array(meta_data->hierarchies[k]->subsystems[0], (const char **)subsystems)) {
356 /* TODO: we could also check if the lists really match completely,
357 * just to have an additional sanity check */
358 h = meta_data->hierarchies[k];
b98f7d6e 359 break;
33ad9f1a 360 }
b98f7d6e 361 }
33ad9f1a
CS
362 lxc_free_array((void **)subsystems, free);
363
364 r = lxc_grow_array((void ***)&meta_data->mount_points, &mount_point_capacity, mount_point_count + 1, 12);
365 if (r < 0)
b653309a 366 goto out;
33ad9f1a
CS
367
368 /* create mount point object */
369 mount_point = calloc(1, sizeof(*mount_point));
370 if (!mount_point)
b653309a 371 goto out;
33ad9f1a
CS
372
373 meta_data->mount_points[mount_point_count++] = mount_point;
374
375 mount_point->hierarchy = h;
376 mount_point->mount_point = strdup(tokens[4]);
377 mount_point->mount_prefix = strdup(tokens[3]);
378 if (!mount_point->mount_point || !mount_point->mount_prefix)
b653309a 379 goto out;
33ad9f1a
CS
380 mount_point->read_only = !lxc_string_in_list("rw", tokens[5], ',');
381
382 if (!strcmp(mount_point->mount_prefix, "/")) {
383 if (mount_point->read_only) {
384 if (!h->ro_absolute_mount_point)
385 h->ro_absolute_mount_point = mount_point;
386 } else {
387 if (!h->rw_absolute_mount_point)
388 h->rw_absolute_mount_point = mount_point;
389 }
b98f7d6e 390 }
ae5c8b8e 391
33ad9f1a
CS
392 k = lxc_array_len((void **)h->all_mount_points);
393 r = lxc_grow_array((void ***)&h->all_mount_points, &h->all_mount_point_capacity, k + 1, 4);
394 if (r < 0)
b653309a 395 goto out;
33ad9f1a 396 h->all_mount_points[k] = mount_point;
fd4f5a56 397 }
b653309a
SH
398 bret = true;
399
400out:
401 process_lock();
402 fclose(proc_self_mountinfo);
403 process_unlock();
404 free(tokens);
2cdafc54 405 free(line);
b653309a
SH
406 return bret;
407}
408
409struct cgroup_meta_data *lxc_cgroup_load_meta2(const char **subsystem_whitelist)
410{
411 bool all_kernel_subsystems = true;
412 bool all_named_subsystems = false;
413 struct cgroup_meta_data *meta_data = NULL;
414 char **kernel_subsystems = NULL;
415 int saved_errno = 0;
416
417 /* if the subsystem whitelist is not specified, include all
418 * hierarchies that contain kernel subsystems by default but
419 * no hierarchies that only contain named subsystems
420 *
421 * if it is specified, the specifier @all will select all
422 * hierarchies, @kernel will select all hierarchies with
423 * kernel subsystems and @named will select all named
424 * hierarchies
425 */
426 all_kernel_subsystems = subsystem_whitelist ?
427 (lxc_string_in_array("@kernel", subsystem_whitelist) || lxc_string_in_array("@all", subsystem_whitelist)) :
428 true;
429 all_named_subsystems = subsystem_whitelist ?
430 (lxc_string_in_array("@named", subsystem_whitelist) || lxc_string_in_array("@all", subsystem_whitelist)) :
431 false;
432
433 meta_data = calloc(1, sizeof(struct cgroup_meta_data));
434 if (!meta_data)
435 return NULL;
436 meta_data->ref = 1;
437
438 if (!find_cgroup_subsystems(&kernel_subsystems))
439 goto out_error;
440
441 if (!find_cgroup_hierarchies(meta_data, all_kernel_subsystems,
442 all_named_subsystems, subsystem_whitelist))
443 goto out_error;
444
445 if (!find_hierarchy_mountpts(meta_data, kernel_subsystems))
446 goto out_error;
fd4f5a56 447
33ad9f1a
CS
448 /* oops, we couldn't find anything */
449 if (!meta_data->hierarchies || !meta_data->mount_points) {
450 errno = EINVAL;
451 goto out_error;
ae5c8b8e 452 }
fd4f5a56 453
3a0abb3a 454 lxc_free_array((void **)kernel_subsystems, free);
33ad9f1a
CS
455 return meta_data;
456
457out_error:
458 saved_errno = errno;
33ad9f1a
CS
459 lxc_free_array((void **)kernel_subsystems, free);
460 lxc_cgroup_put_meta(meta_data);
461 errno = saved_errno;
462 return NULL;
fd4f5a56
DL
463}
464
33ad9f1a 465struct cgroup_meta_data *lxc_cgroup_get_meta(struct cgroup_meta_data *meta_data)
e14f67a7 466{
33ad9f1a
CS
467 meta_data->ref++;
468 return meta_data;
469}
e14f67a7 470
33ad9f1a
CS
471struct cgroup_meta_data *lxc_cgroup_put_meta(struct cgroup_meta_data *meta_data)
472{
473 size_t i;
474 if (!meta_data)
475 return NULL;
476 if (--meta_data->ref > 0)
477 return meta_data;
478 lxc_free_array((void **)meta_data->mount_points, (lxc_free_fn)lxc_cgroup_mount_point_free);
479 if (meta_data->hierarchies) {
480 for (i = 0; i <= meta_data->maximum_hierarchy; i++)
481 lxc_cgroup_hierarchy_free(meta_data->hierarchies[i]);
e14f67a7 482 }
33ad9f1a 483 free(meta_data->hierarchies);
178938fe 484 free(meta_data);
33ad9f1a 485 return NULL;
e14f67a7
U
486}
487
33ad9f1a 488struct cgroup_hierarchy *lxc_cgroup_find_hierarchy(struct cgroup_meta_data *meta_data, const char *subsystem)
e14f67a7 489{
33ad9f1a
CS
490 size_t i;
491 for (i = 0; i <= meta_data->maximum_hierarchy; i++) {
492 struct cgroup_hierarchy *h = meta_data->hierarchies[i];
493 if (h && lxc_string_in_array(subsystem, (const char **)h->subsystems))
494 return h;
e14f67a7 495 }
e14f67a7
U
496 return NULL;
497}
498
33ad9f1a 499struct cgroup_mount_point *lxc_cgroup_find_mount_point(struct cgroup_hierarchy *hierarchy, const char *group, bool should_be_writable)
b98f7d6e 500{
33ad9f1a
CS
501 struct cgroup_mount_point **mps;
502 struct cgroup_mount_point *current_result = NULL;
503 ssize_t quality = -1;
b98f7d6e 504
33ad9f1a
CS
505 /* trivial case */
506 if (hierarchy->rw_absolute_mount_point)
507 return hierarchy->rw_absolute_mount_point;
508 if (!should_be_writable && hierarchy->ro_absolute_mount_point)
509 return hierarchy->ro_absolute_mount_point;
b98f7d6e 510
33ad9f1a
CS
511 for (mps = hierarchy->all_mount_points; mps && *mps; mps++) {
512 struct cgroup_mount_point *mp = *mps;
513 size_t prefix_len = mp->mount_prefix ? strlen(mp->mount_prefix) : 0;
b98f7d6e 514
33ad9f1a
CS
515 if (prefix_len == 1 && mp->mount_prefix[0] == '/')
516 prefix_len = 0;
b98f7d6e 517
33ad9f1a
CS
518 if (should_be_writable && mp->read_only)
519 continue;
520
521 if (!prefix_len ||
522 (strncmp(group, mp->mount_prefix, prefix_len) == 0 &&
523 (group[prefix_len] == '\0' || group[prefix_len] == '/'))) {
524 /* search for the best quality match, i.e. the match with the
525 * shortest prefix where this group is still contained
526 */
527 if (quality == -1 || prefix_len < quality) {
528 current_result = mp;
529 quality = prefix_len;
530 }
b98f7d6e
SH
531 }
532 }
533
33ad9f1a
CS
534 if (!current_result)
535 errno = ENOENT;
536 return current_result;
b98f7d6e
SH
537}
538
33ad9f1a 539char *lxc_cgroup_find_abs_path(const char *subsystem, const char *group, bool should_be_writable, const char *suffix)
b98f7d6e 540{
33ad9f1a
CS
541 struct cgroup_meta_data *meta_data;
542 struct cgroup_hierarchy *h;
543 struct cgroup_mount_point *mp;
544 char *result;
545 int saved_errno;
546
547 meta_data = lxc_cgroup_load_meta();
548 if (!meta_data)
549 return NULL;
b98f7d6e 550
33ad9f1a
CS
551 h = lxc_cgroup_find_hierarchy(meta_data, subsystem);
552 if (!h)
553 goto out_error;
b98f7d6e 554
33ad9f1a
CS
555 mp = lxc_cgroup_find_mount_point(h, group, should_be_writable);
556 if (!mp)
557 goto out_error;
b98f7d6e 558
33ad9f1a
CS
559 result = cgroup_to_absolute_path(mp, group, suffix);
560 if (!result)
561 goto out_error;
b98f7d6e 562
33ad9f1a
CS
563 lxc_cgroup_put_meta(meta_data);
564 return result;
b98f7d6e 565
33ad9f1a
CS
566out_error:
567 saved_errno = errno;
568 lxc_cgroup_put_meta(meta_data);
569 errno = saved_errno;
570 return NULL;
b98f7d6e
SH
571}
572
33ad9f1a 573struct cgroup_process_info *lxc_cgroup_process_info_get(pid_t pid, struct cgroup_meta_data *meta)
fd4f5a56 574{
33ad9f1a
CS
575 char pid_buf[32];
576 snprintf(pid_buf, 32, "/proc/%lu/cgroup", (unsigned long)pid);
577 return lxc_cgroup_process_info_getx(pid_buf, meta);
c8f7c563
CS
578}
579
33ad9f1a 580struct cgroup_process_info *lxc_cgroup_process_info_get_init(struct cgroup_meta_data *meta)
c8f7c563 581{
33ad9f1a
CS
582 return lxc_cgroup_process_info_get(1, meta);
583}
b98f7d6e 584
33ad9f1a
CS
585struct cgroup_process_info *lxc_cgroup_process_info_get_self(struct cgroup_meta_data *meta)
586{
587 struct cgroup_process_info *i;
588 i = lxc_cgroup_process_info_getx("/proc/self/cgroup", meta);
589 if (!i)
590 i = lxc_cgroup_process_info_get(getpid(), meta);
591 return i;
592}
ae5c8b8e 593
692ba18f
SH
594/*
595 * If a controller has ns cgroup mounted, then in that cgroup the handler->pid
596 * is already in a new cgroup named after the pid. 'mnt' is passed in as
597 * the full current cgroup. Say that is /sys/fs/cgroup/lxc/2975 and the container
598 * name is c1. . We want to rename the cgroup directory to /sys/fs/cgroup/lxc/c1,
599 * and return the string /sys/fs/cgroup/lxc/c1.
600 */
cea0552e 601static char *cgroup_rename_nsgroup(const char *mountpath, const char *oldname, pid_t pid, const char *name)
692ba18f
SH
602{
603 char *dir, *fulloldpath;
604 char *newname, *fullnewpath;
cea0552e 605 int len, newlen, ret;
692ba18f
SH
606
607 /*
608 * if cgroup is mounted at /cgroup and task is in cgroup /ab/, pid 2375 and
609 * name is c1,
610 * dir: /ab
611 * fulloldpath = /cgroup/ab/2375
612 * fullnewpath = /cgroup/ab/c1
613 * newname = /ab/c1
614 */
615 dir = alloca(strlen(oldname) + 1);
616 strcpy(dir, oldname);
617
cea0552e
SH
618 len = strlen(oldname) + strlen(mountpath) + 22;
619 fulloldpath = alloca(len);
620 ret = snprintf(fulloldpath, len, "%s/%s/%ld", mountpath, oldname, (unsigned long)pid);
621 if (ret < 0 || ret >= len)
622 return NULL;
692ba18f
SH
623
624 len = strlen(dir) + strlen(name) + 2;
625 newname = malloc(len);
626 if (!newname) {
627 SYSERROR("Out of memory");
628 return NULL;
629 }
cea0552e
SH
630 ret = snprintf(newname, len, "%s/%s", dir, name);
631 if (ret < 0 || ret >= len) {
632 free(newname);
633 return NULL;
634 }
692ba18f 635
cea0552e
SH
636 newlen = strlen(mountpath) + len + 2;
637 fullnewpath = alloca(newlen);
638 ret = snprintf(fullnewpath, newlen, "%s/%s", mountpath, newname);
639 if (ret < 0 || ret >= newlen) {
640 free(newname);
641 return NULL;
642 }
692ba18f
SH
643
644 if (access(fullnewpath, F_OK) == 0) {
645 if (rmdir(fullnewpath) != 0) {
646 SYSERROR("container cgroup %s already exists.", fullnewpath);
647 free(newname);
648 return NULL;
649 }
650 }
651 if (rename(fulloldpath, fullnewpath)) {
652 SYSERROR("failed to rename cgroup %s->%s", fulloldpath, fullnewpath);
653 free(newname);
654 return NULL;
655 }
656
657 DEBUG("'%s' renamed to '%s'", oldname, newname);
658
659 return newname;
660}
661
33ad9f1a 662/* create a new cgroup */
47d8fb3b 663extern struct cgroup_process_info *lxc_cgroup_create(const char *name, const char *path_pattern, struct cgroup_meta_data *meta_data, const char *sub_pattern)
33ad9f1a 664{
001b026e 665 char **cgroup_path_components = NULL;
33ad9f1a
CS
666 char **p = NULL;
667 char *path_so_far = NULL;
668 char **new_cgroup_paths = NULL;
669 char **new_cgroup_paths_sub = NULL;
670 struct cgroup_mount_point *mp;
671 struct cgroup_hierarchy *h;
672 struct cgroup_process_info *base_info = NULL;
673 struct cgroup_process_info *info_ptr;
674 int saved_errno;
675 int r;
676 unsigned suffix = 0;
677 bool had_sub_pattern = false;
678 size_t i;
ae5c8b8e 679
33ad9f1a
CS
680 if (!is_valid_cgroup(name)) {
681 ERROR("Invalid cgroup name: '%s'", name);
682 errno = EINVAL;
683 return NULL;
ae5c8b8e
SH
684 }
685
33ad9f1a
CS
686 if (!strstr(path_pattern, "%n")) {
687 ERROR("Invalid cgroup path pattern: '%s'; contains no %%n for specifying container name", path_pattern);
688 errno = EINVAL;
689 return NULL;
690 }
fd37327f 691
33ad9f1a
CS
692 /* we will modify the result of this operation directly,
693 * so we don't have to copy the data structure
694 */
695 base_info = (path_pattern[0] == '/') ?
696 lxc_cgroup_process_info_get_init(meta_data) :
697 lxc_cgroup_process_info_get_self(meta_data);
698 if (!base_info)
699 return NULL;
c8f7c563 700
33ad9f1a
CS
701 new_cgroup_paths = calloc(meta_data->maximum_hierarchy + 1, sizeof(char *));
702 if (!new_cgroup_paths)
703 goto out_initial_error;
704
705 new_cgroup_paths_sub = calloc(meta_data->maximum_hierarchy + 1, sizeof(char *));
706 if (!new_cgroup_paths_sub)
707 goto out_initial_error;
708
709 /* find mount points we can use */
710 for (info_ptr = base_info; info_ptr; info_ptr = info_ptr->next) {
711 h = info_ptr->hierarchy;
712 mp = lxc_cgroup_find_mount_point(h, info_ptr->cgroup_path, true);
713 if (!mp) {
714 ERROR("Could not find writable mount point for cgroup hierarchy %d while trying to create cgroup.", h->index);
715 goto out_initial_error;
716 }
717 info_ptr->designated_mount_point = mp;
460a1cf0 718
692ba18f
SH
719 if (lxc_string_in_array("ns", (const char **)h->subsystems))
720 continue;
33ad9f1a
CS
721 if (handle_clone_children(mp, info_ptr->cgroup_path) < 0) {
722 ERROR("Could not set clone_children to 1 for cpuset hierarchy in parent cgroup.");
723 goto out_initial_error;
724 }
725 }
b98f7d6e 726
33ad9f1a
CS
727 /* normalize the path */
728 cgroup_path_components = lxc_normalize_path(path_pattern);
729 if (!cgroup_path_components)
730 goto out_initial_error;
731
732 /* go through the path components to see if we can create them */
733 for (p = cgroup_path_components; *p || (sub_pattern && !had_sub_pattern); p++) {
734 /* we only want to create the same component with -1, -2, etc.
735 * if the component contains the container name itself, otherwise
736 * it's not an error if it already exists
737 */
738 char *p_eff = *p ? *p : (char *)sub_pattern;
739 bool contains_name = strstr(p_eff, "%n");
740 char *current_component = NULL;
741 char *current_subpath = NULL;
742 char *current_entire_path = NULL;
743 char *parts[3];
744 size_t j = 0;
745 i = 0;
746
747 /* if we are processing the subpattern, we want to make sure
748 * loop is ended the next time around
749 */
750 if (!*p) {
751 had_sub_pattern = true;
752 p--;
753 }
b98f7d6e 754
33ad9f1a
CS
755 goto find_name_on_this_level;
756
757 cleanup_name_on_this_level:
758 /* This is reached if we found a name clash.
759 * In that case, remove the cgroup from all previous hierarchies
760 */
761 for (j = 0, info_ptr = base_info; j < i && info_ptr; info_ptr = info_ptr->next, j++) {
762 r = remove_cgroup(info_ptr->designated_mount_point, info_ptr->created_paths[info_ptr->created_paths_count - 1]);
763 if (r < 0)
764 WARN("could not clean up cgroup we created when trying to create container");
765 free(info_ptr->created_paths[info_ptr->created_paths_count - 1]);
766 info_ptr->created_paths[--info_ptr->created_paths_count] = NULL;
767 }
768 if (current_component != current_subpath)
769 free(current_subpath);
770 if (current_component != p_eff)
771 free(current_component);
772 current_component = current_subpath = NULL;
773 /* try again with another suffix */
774 ++suffix;
775
776 find_name_on_this_level:
777 /* determine name of the path component we should create */
778 if (contains_name && suffix > 0) {
779 char *buf = calloc(strlen(name) + 32, 1);
780 if (!buf)
781 goto out_initial_error;
782 snprintf(buf, strlen(name) + 32, "%s-%u", name, suffix);
783 current_component = lxc_string_replace("%n", buf, p_eff);
784 free(buf);
785 } else {
786 current_component = contains_name ? lxc_string_replace("%n", name, p_eff) : p_eff;
787 }
788 parts[0] = path_so_far;
789 parts[1] = current_component;
790 parts[2] = NULL;
791 current_subpath = path_so_far ? lxc_string_join("/", (const char **)parts, false) : current_component;
792
793 /* Now go through each hierarchy and try to create the
794 * corresponding cgroup
795 */
796 for (i = 0, info_ptr = base_info; info_ptr; info_ptr = info_ptr->next, i++) {
797 char *parts2[3];
692ba18f
SH
798
799 if (lxc_string_in_array("ns", (const char **)info_ptr->hierarchy->subsystems))
800 continue;
33ad9f1a
CS
801 current_entire_path = NULL;
802
803 parts2[0] = !strcmp(info_ptr->cgroup_path, "/") ? "" : info_ptr->cgroup_path;
804 parts2[1] = current_subpath;
805 parts2[2] = NULL;
806 current_entire_path = lxc_string_join("/", (const char **)parts2, false);
807
808 if (!*p) {
809 /* we are processing the subpath, so only update that one */
810 free(new_cgroup_paths_sub[i]);
811 new_cgroup_paths_sub[i] = strdup(current_entire_path);
812 if (!new_cgroup_paths_sub[i])
813 goto cleanup_from_error;
814 } else {
815 /* remember which path was used on this controller */
816 free(new_cgroup_paths[i]);
817 new_cgroup_paths[i] = strdup(current_entire_path);
818 if (!new_cgroup_paths[i])
819 goto cleanup_from_error;
820 }
fd4f5a56 821
33ad9f1a
CS
822 r = create_cgroup(info_ptr->designated_mount_point, current_entire_path);
823 if (r < 0 && errno == EEXIST && contains_name) {
824 /* name clash => try new name with new suffix */
825 free(current_entire_path);
826 current_entire_path = NULL;
827 goto cleanup_name_on_this_level;
828 } else if (r < 0 && errno != EEXIST) {
829 SYSERROR("Could not create cgroup %s", current_entire_path);
830 goto cleanup_from_error;
831 } else if (r == 0) {
832 /* successfully created */
833 r = lxc_grow_array((void ***)&info_ptr->created_paths, &info_ptr->created_paths_capacity, info_ptr->created_paths_count + 1, 8);
834 if (r < 0)
835 goto cleanup_from_error;
836 info_ptr->created_paths[info_ptr->created_paths_count++] = current_entire_path;
837 } else {
838 /* if we didn't create the cgroup, then we have to make sure that
839 * further cgroups will be created properly
840 */
841 if (handle_clone_children(mp, info_ptr->cgroup_path) < 0) {
842 ERROR("Could not set clone_children to 1 for cpuset hierarchy in pre-existing cgroup.");
843 goto cleanup_from_error;
844 }
845
846 /* already existed but path component of pattern didn't contain '%n',
847 * so this is not an error; but then we don't need current_entire_path
848 * anymore...
849 */
850 free(current_entire_path);
851 current_entire_path = NULL;
852 }
853 }
fd4f5a56 854
33ad9f1a
CS
855 /* save path so far */
856 free(path_so_far);
857 path_so_far = strdup(current_subpath);
858 if (!path_so_far)
859 goto cleanup_from_error;
860
861 /* cleanup */
862 if (current_component != current_subpath)
863 free(current_subpath);
864 if (current_component != p_eff)
865 free(current_component);
866 current_component = current_subpath = NULL;
867 continue;
868
869 cleanup_from_error:
870 /* called if an error occured in the loop, so we
871 * do some additional cleanup here
872 */
873 saved_errno = errno;
874 if (current_component != current_subpath)
875 free(current_subpath);
876 if (current_component != p_eff)
877 free(current_component);
878 free(current_entire_path);
879 errno = saved_errno;
880 goto out_initial_error;
fd4f5a56
DL
881 }
882
33ad9f1a
CS
883 /* we're done, now update the paths */
884 for (i = 0, info_ptr = base_info; info_ptr; info_ptr = info_ptr->next, i++) {
47d8fb3b
CS
885 /* ignore legacy 'ns' subsystem here, lxc_cgroup_create_legacy
886 * will take care of it
887 * Since we do a continue in above loop, new_cgroup_paths[i] is
888 * unset anyway, as is new_cgroup_paths_sub[i]
692ba18f 889 */
47d8fb3b
CS
890 if (lxc_string_in_array("ns", (const char **)info_ptr->hierarchy->subsystems))
891 continue;
892 free(info_ptr->cgroup_path);
893 info_ptr->cgroup_path = new_cgroup_paths[i];
894 info_ptr->cgroup_path_sub = new_cgroup_paths_sub[i];
fd4f5a56 895 }
33ad9f1a
CS
896 /* don't use lxc_free_array since we used the array members
897 * to store them in our result...
898 */
899 free(new_cgroup_paths);
900 free(new_cgroup_paths_sub);
901 free(path_so_far);
902 lxc_free_array((void **)cgroup_path_components, free);
903 return base_info;
904
905out_initial_error:
906 saved_errno = errno;
907 free(path_so_far);
908 lxc_cgroup_process_info_free_and_remove(base_info);
909 lxc_free_array((void **)new_cgroup_paths, free);
910 lxc_free_array((void **)new_cgroup_paths_sub, free);
911 lxc_free_array((void **)cgroup_path_components, free);
912 errno = saved_errno;
913 return NULL;
c8f7c563
CS
914}
915
47d8fb3b
CS
916int lxc_cgroup_create_legacy(struct cgroup_process_info *base_info, const char *name, pid_t pid)
917{
918 struct cgroup_process_info *info_ptr;
919 int r;
920
921 for (info_ptr = base_info; info_ptr; info_ptr = info_ptr->next) {
922 if (!lxc_string_in_array("ns", (const char **)info_ptr->hierarchy->subsystems))
923 continue;
924 /*
925 * For any path which has ns cgroup mounted, handler->pid is already
926 * moved into a container called '%d % (handler->pid)'. Rename it to
927 * the cgroup name and record that.
928 */
929 char *tmp = cgroup_rename_nsgroup((const char *)info_ptr->designated_mount_point->mount_point,
930 info_ptr->cgroup_path, pid, name);
931 if (!tmp)
932 return -1;
933 free(info_ptr->cgroup_path);
934 info_ptr->cgroup_path = tmp;
935 r = lxc_grow_array((void ***)&info_ptr->created_paths, &info_ptr->created_paths_capacity, info_ptr->created_paths_count + 1, 8);
936 if (r < 0)
937 return -1;
938 tmp = strdup(tmp);
939 if (!tmp)
940 return -1;
941 info_ptr->created_paths[info_ptr->created_paths_count++] = tmp;
942 }
943 return 0;
944}
945
33ad9f1a
CS
946/* get the cgroup membership of a given container */
947struct cgroup_process_info *lxc_cgroup_get_container_info(const char *name, const char *lxcpath, struct cgroup_meta_data *meta_data)
c8f7c563 948{
33ad9f1a
CS
949 struct cgroup_process_info *result = NULL;
950 int saved_errno = 0;
951 size_t i;
952 struct cgroup_process_info **cptr = &result;
953 struct cgroup_process_info *entry = NULL;
954 char *path = NULL;
955
956 for (i = 0; i <= meta_data->maximum_hierarchy; i++) {
957 struct cgroup_hierarchy *h = meta_data->hierarchies[i];
958 if (!h || !h->used)
959 continue;
c8f7c563 960
33ad9f1a
CS
961 /* use the command interface to look for the cgroup */
962 path = lxc_cmd_get_cgroup_path(name, lxcpath, h->subsystems[0]);
963 if (!path)
964 goto out_error;
965
966 entry = calloc(1, sizeof(struct cgroup_process_info));
967 if (!entry)
968 goto out_error;
969 entry->meta_ref = lxc_cgroup_get_meta(meta_data);
970 entry->hierarchy = h;
971 entry->cgroup_path = path;
972 path = NULL;
973
974 /* it is not an error if we don't find anything here,
975 * it is up to the caller to decide what to do in that
976 * case */
977 entry->designated_mount_point = lxc_cgroup_find_mount_point(h, entry->cgroup_path, true);
978
979 *cptr = entry;
980 cptr = &entry->next;
981 entry = NULL;
c8f7c563
CS
982 }
983
33ad9f1a
CS
984 return result;
985out_error:
986 saved_errno = errno;
987 free(path);
988 lxc_cgroup_process_info_free(result);
989 lxc_cgroup_process_info_free(entry);
990 errno = saved_errno;
991 return NULL;
fd4f5a56
DL
992}
993
33ad9f1a
CS
994/* move a processs to the cgroups specified by the membership */
995int lxc_cgroup_enter(struct cgroup_process_info *info, pid_t pid, bool enter_sub)
4f17323e 996{
33ad9f1a
CS
997 char pid_buf[32];
998 char *cgroup_tasks_fn;
999 int r;
1000 struct cgroup_process_info *info_ptr;
1001
1002 snprintf(pid_buf, 32, "%lu", (unsigned long)pid);
1003 for (info_ptr = info; info_ptr; info_ptr = info_ptr->next) {
1004 char *cgroup_path = (enter_sub && info_ptr->cgroup_path_sub) ?
1005 info_ptr->cgroup_path_sub :
1006 info_ptr->cgroup_path;
1007
1008 if (!info_ptr->designated_mount_point) {
1009 info_ptr->designated_mount_point = lxc_cgroup_find_mount_point(info_ptr->hierarchy, cgroup_path, true);
1010 if (!info_ptr->designated_mount_point) {
1011 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);
1012 return -1;
1013 }
1014 }
4f17323e 1015
33ad9f1a
CS
1016 cgroup_tasks_fn = cgroup_to_absolute_path(info_ptr->designated_mount_point, cgroup_path, "/tasks");
1017 if (!cgroup_tasks_fn) {
1018 SYSERROR("Could not add pid %lu to cgroup %s: internal error", (unsigned long)pid, cgroup_path);
1019 return -1;
1020 }
4f17323e 1021
33ad9f1a
CS
1022 r = lxc_write_to_file(cgroup_tasks_fn, pid_buf, strlen(pid_buf), false);
1023 if (r < 0) {
1024 SYSERROR("Could not add pid %lu to cgroup %s: internal error", (unsigned long)pid, cgroup_path);
1025 return -1;
1026 }
4f17323e
CS
1027 }
1028
33ad9f1a 1029 return 0;
4f17323e
CS
1030}
1031
33ad9f1a
CS
1032/* free process membership information */
1033void lxc_cgroup_process_info_free(struct cgroup_process_info *info)
fc7de561 1034{
33ad9f1a
CS
1035 struct cgroup_process_info *next;
1036 if (!info)
b98f7d6e 1037 return;
33ad9f1a
CS
1038 next = info->next;
1039 lxc_cgroup_put_meta(info->meta_ref);
1040 free(info->cgroup_path);
1041 free(info->cgroup_path_sub);
1042 lxc_free_array((void **)info->created_paths, free);
1043 free(info);
1044 lxc_cgroup_process_info_free(next);
fc7de561
SH
1045}
1046
33ad9f1a
CS
1047/* free process membership information and remove cgroups that were created */
1048void lxc_cgroup_process_info_free_and_remove(struct cgroup_process_info *info)
b98f7d6e 1049{
33ad9f1a
CS
1050 struct cgroup_process_info *next;
1051 char **pp;
1052 if (!info)
1053 return;
1054 next = info->next;
1055 for (pp = info->created_paths; pp && *pp; pp++);
1056 for ((void)(pp && --pp); info->created_paths && pp >= info->created_paths; --pp) {
1057 struct cgroup_mount_point *mp = info->designated_mount_point;
1058 if (!mp)
1059 mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, true);
1060 if (mp)
1061 /* ignore return value here, perhaps we created the
1062 * '/lxc' cgroup in this container but another container
1063 * is still running (for example)
1064 */
1065 (void)remove_cgroup(mp, *pp);
1066 free(*pp);
b98f7d6e 1067 }
33ad9f1a
CS
1068 free(info->created_paths);
1069 lxc_cgroup_put_meta(info->meta_ref);
1070 free(info->cgroup_path);
1071 free(info->cgroup_path_sub);
1072 free(info);
9431aa65 1073 lxc_cgroup_process_info_free_and_remove(next);
33ad9f1a 1074}
b98f7d6e 1075
33ad9f1a
CS
1076char *lxc_cgroup_get_hierarchy_path_handler(const char *subsystem, struct lxc_handler *handler)
1077{
1078 struct cgroup_process_info *info = find_info_for_subsystem(handler->cgroup, subsystem);
1079 if (!info)
1080 return NULL;
1081 return info->cgroup_path;
b98f7d6e
SH
1082}
1083
33ad9f1a 1084char *lxc_cgroup_get_hierarchy_path(const char *subsystem, const char *name, const char *lxcpath)
b98f7d6e 1085{
33ad9f1a 1086 return lxc_cmd_get_cgroup_path(name, lxcpath, subsystem);
b98f7d6e
SH
1087}
1088
33ad9f1a 1089char *lxc_cgroup_get_hierarchy_abs_path_handler(const char *subsystem, struct lxc_handler *handler)
b98f7d6e 1090{
33ad9f1a
CS
1091 struct cgroup_mount_point *mp = NULL;
1092 struct cgroup_process_info *info = find_info_for_subsystem(handler->cgroup, subsystem);
1093 if (!info)
1094 return NULL;
1095 if (info->designated_mount_point) {
8900b9eb 1096 mp = info->designated_mount_point;
33ad9f1a
CS
1097 } else {
1098 mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, true);
1099 if (!mp)
1100 return NULL;
b98f7d6e 1101 }
33ad9f1a 1102 return cgroup_to_absolute_path(mp, info->cgroup_path, NULL);
b98f7d6e 1103}
55c76589 1104
33ad9f1a 1105char *lxc_cgroup_get_hierarchy_abs_path(const char *subsystem, const char *name, const char *lxcpath)
9a93d992 1106{
33ad9f1a
CS
1107 struct cgroup_meta_data *meta;
1108 struct cgroup_process_info *base_info, *info;
1109 struct cgroup_mount_point *mp;
1110 char *result = NULL;
33ad9f1a
CS
1111
1112 meta = lxc_cgroup_load_meta();
1113 if (!meta)
9a93d992 1114 return NULL;
33ad9f1a
CS
1115 base_info = lxc_cgroup_get_container_info(name, lxcpath, meta);
1116 if (!base_info)
178938fe 1117 goto out1;
33ad9f1a
CS
1118 info = find_info_for_subsystem(base_info, subsystem);
1119 if (!info)
178938fe 1120 goto out2;
33ad9f1a 1121 if (info->designated_mount_point) {
8900b9eb 1122 mp = info->designated_mount_point;
33ad9f1a
CS
1123 } else {
1124 mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, true);
1125 if (!mp)
178938fe 1126 goto out3;
33ad9f1a
CS
1127 }
1128 result = cgroup_to_absolute_path(mp, info->cgroup_path, NULL);
178938fe 1129out3:
178938fe 1130out2:
33ad9f1a 1131 lxc_cgroup_process_info_free(base_info);
178938fe 1132out1:
33ad9f1a 1133 lxc_cgroup_put_meta(meta);
33ad9f1a
CS
1134 return result;
1135}
9a93d992 1136
33ad9f1a
CS
1137int lxc_cgroup_set_handler(const char *filename, const char *value, struct lxc_handler *handler)
1138{
1139 char *subsystem = NULL, *p, *path;
1140 int ret = -1;
9a93d992 1141
33ad9f1a
CS
1142 subsystem = alloca(strlen(filename) + 1);
1143 strcpy(subsystem, filename);
1144 if ((p = index(subsystem, '.')) != NULL)
1145 *p = '\0';
9a93d992 1146
33ad9f1a
CS
1147 path = lxc_cgroup_get_hierarchy_abs_path_handler(subsystem, handler);
1148 if (path) {
1149 ret = do_cgroup_set(path, filename, value);
1150 free(path);
9a93d992 1151 }
33ad9f1a
CS
1152 return ret;
1153}
9a93d992 1154
33ad9f1a
CS
1155int lxc_cgroup_get_handler(const char *filename, char *value, size_t len, struct lxc_handler *handler)
1156{
1157 char *subsystem = NULL, *p, *path;
1158 int ret = -1;
1159
1160 subsystem = alloca(strlen(filename) + 1);
1161 strcpy(subsystem, filename);
1162 if ((p = index(subsystem, '.')) != NULL)
1163 *p = '\0';
1164
1165 path = lxc_cgroup_get_hierarchy_abs_path_handler(subsystem, handler);
1166 if (path) {
1167 ret = do_cgroup_get(path, filename, value, len);
1168 free(path);
1169 }
9a93d992
SH
1170 return ret;
1171}
1172
33ad9f1a 1173int lxc_cgroup_set(const char *filename, const char *value, const char *name, const char *lxcpath)
9a93d992 1174{
33ad9f1a
CS
1175 char *subsystem = NULL, *p, *path;
1176 int ret = -1;
9a93d992 1177
33ad9f1a
CS
1178 subsystem = alloca(strlen(filename) + 1);
1179 strcpy(subsystem, filename);
1180 if ((p = index(subsystem, '.')) != NULL)
1181 *p = '\0';
9a93d992 1182
33ad9f1a
CS
1183 path = lxc_cgroup_get_hierarchy_abs_path(subsystem, name, lxcpath);
1184 if (path) {
1185 ret = do_cgroup_set(path, filename, value);
1186 free(path);
1187 }
b98f7d6e 1188 return ret;
9a93d992
SH
1189}
1190
33ad9f1a 1191int lxc_cgroup_get(const char *filename, char *value, size_t len, const char *name, const char *lxcpath)
9a93d992 1192{
33ad9f1a
CS
1193 char *subsystem = NULL, *p, *path;
1194 int ret = -1;
1195
1196 subsystem = alloca(strlen(filename) + 1);
1197 strcpy(subsystem, filename);
1198 if ((p = index(subsystem, '.')) != NULL)
1199 *p = '\0';
1200
1201 path = lxc_cgroup_get_hierarchy_abs_path(subsystem, name, lxcpath);
1202 if (path) {
1203 ret = do_cgroup_get(path, filename, value, len);
1204 free(path);
9a93d992 1205 }
33ad9f1a 1206 return ret;
9a93d992
SH
1207}
1208
33ad9f1a
CS
1209/*
1210 * lxc_cgroup_path_get: Get the absolute pathname for a cgroup
1211 * file for a running container.
1212 *
1213 * @filename : the file of interest (e.g. "freezer.state") or
1214 * the subsystem name (e.g. "freezer") in which case
1215 * the directory where the cgroup may be modified
1216 * will be returned
1217 * @name : name of container to connect to
1218 * @lxcpath : the lxcpath in which the container is running
8900b9eb 1219 *
33ad9f1a
CS
1220 * This is the exported function, which determines cgpath from the
1221 * lxc-start of the @name container running in @lxcpath.
1222 *
1223 * Returns path on success, NULL on error. The caller must free()
1224 * the returned path.
1225 */
1226char *lxc_cgroup_path_get(const char *filename, const char *name,
1227 const char *lxcpath)
9a93d992 1228{
33ad9f1a 1229 char *subsystem = NULL, *longer_file = NULL, *p, *group, *path;
9a93d992 1230
33ad9f1a
CS
1231 subsystem = alloca(strlen(filename) + 1);
1232 strcpy(subsystem, filename);
1233 if ((p = index(subsystem, '.')) != NULL) {
1234 *p = '\0';
1235 longer_file = alloca(strlen(filename) + 2);
1236 longer_file[0] = '/';
1237 strcpy(longer_file + 1, filename);
b98f7d6e
SH
1238 }
1239
33ad9f1a
CS
1240 group = lxc_cgroup_get_hierarchy_path(subsystem, name, lxcpath);
1241 if (!group)
1242 return NULL;
b98f7d6e 1243
33ad9f1a
CS
1244 path = lxc_cgroup_find_abs_path(subsystem, group, true, *p ? longer_file : NULL);
1245 free(group);
1246 return path;
9a93d992
SH
1247}
1248
33ad9f1a
CS
1249int lxc_setup_cgroup_without_devices(struct lxc_handler *h, struct lxc_list *cgroup_settings)
1250{
1251 return do_setup_cgroup(h, cgroup_settings, false);
1252}
b98f7d6e 1253
33ad9f1a 1254int lxc_setup_cgroup_devices(struct lxc_handler *h, struct lxc_list *cgroup_settings)
460a1cf0 1255{
33ad9f1a
CS
1256 return do_setup_cgroup(h, cgroup_settings, true);
1257}
fd37327f 1258
aae1f3c4
CS
1259int lxc_setup_mount_cgroup(const char *root, struct cgroup_process_info *base_info)
1260{
1261 size_t bufsz = strlen(root) + sizeof("/sys/fs/cgroup");
1262 char *path = NULL;
1263 char **parts = NULL;
1264 char *dirname = NULL;
1265 char *abs_path = NULL;
1266 char *abs_path2 = NULL;
1267 struct cgroup_process_info *info;
1268 int r, saved_errno = 0;
1269
1270 path = calloc(1, bufsz);
1271 if (!path)
1272 return -1;
1273 snprintf(path, bufsz, "%s/sys/fs/cgroup", root);
1274 r = mount("cgroup_root", path, "tmpfs", MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_RELATIME, "size=10240k,mode=755");
1275 if (r < 0) {
1276 SYSERROR("could not mount tmpfs to /sys/fs/cgroup in the container");
1277 return -1;
1278 }
1279
1280 /* now mount all the hierarchies we care about */
1281 for (info = base_info; info; info = info->next) {
1282 size_t subsystem_count, i;
1283 struct cgroup_mount_point *mp = info->designated_mount_point;
1284 if (!mp)
1285 mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, true);
1286 if (!mp) {
1287 SYSERROR("could not find original mount point for cgroup hierarchy while trying to mount cgroup filesystem");
1288 goto out_error;
1289 }
1290
1291 subsystem_count = lxc_array_len((void **)info->hierarchy->subsystems);
1292 parts = calloc(subsystem_count + 1, sizeof(char *));
1293 if (!parts)
1294 goto out_error;
1295
1296 for (i = 0; i < subsystem_count; i++) {
1297 if (!strncmp(info->hierarchy->subsystems[i], "name=", 5))
1298 parts[i] = info->hierarchy->subsystems[i] + 5;
1299 else
1300 parts[i] = info->hierarchy->subsystems[i];
1301 }
1302 dirname = lxc_string_join(",", (const char **)parts, false);
1303 if (!dirname)
1304 goto out_error;
1305
1306 /* create subsystem directory */
1307 abs_path = lxc_append_paths(path, dirname);
1308 if (!abs_path)
1309 goto out_error;
1310 r = mkdir_p(abs_path, 0755);
1311 if (r < 0 && errno != EEXIST) {
1312 SYSERROR("could not create cgroup subsystem directory /sys/fs/cgroup/%s", dirname);
1313 goto out_error;
1314 }
1315
1316 /* create path for container's cgroup */
1317 abs_path2 = lxc_append_paths(abs_path, info->cgroup_path);
1318 if (!abs_path2)
1319 goto out_error;
1320 r = mkdir_p(abs_path2, 0755);
1321 if (r < 0 && errno != EEXIST) {
1322 SYSERROR("could not create cgroup directory /sys/fs/cgroup/%s%s", dirname, info->cgroup_path);
1323 goto out_error;
1324 }
1325
1326 free(abs_path);
1327 abs_path = NULL;
1328
1329 /* bind-mount container's cgroup to that directory */
1330 abs_path = cgroup_to_absolute_path(mp, info->cgroup_path, NULL);
1331 if (!abs_path)
1332 goto out_error;
1333 r = mount(abs_path, abs_path2, "none", MS_BIND, 0);
1334 if (r < 0) {
1335 SYSERROR("error bind-mounting %s to %s", abs_path, abs_path2);
1336 goto out_error;
1337 }
1338
1339 free(abs_path);
1340 free(abs_path2);
1341 abs_path = NULL;
1342 abs_path2 = NULL;
1343
1344 /* add symlinks for every single subsystem */
1345 if (subsystem_count > 1) {
1346 for (i = 0; i < subsystem_count; i++) {
1347 abs_path = lxc_append_paths(path, parts[i]);
1348 if (!abs_path)
1349 goto out_error;
1350 r = symlink(dirname, abs_path);
1351 if (r < 0)
1352 WARN("could not create symlink %s -> %s in /sys/fs/cgroup of container", parts[i], dirname);
1353 free(abs_path);
1354 abs_path = NULL;
1355 }
1356 }
1357 free(dirname);
1358 free(parts);
1359 dirname = NULL;
1360 parts = NULL;
1361 }
1362
1363 /* try to remount the tmpfs readonly, since the container shouldn't
1364 * change anything (this will also make sure that trying to create
1365 * new cgroups outside the allowed area fails with an error instead
1366 * of simply causing this to create directories in the tmpfs itself)
1367 */
1368 mount(NULL, path, NULL, MS_REMOUNT|MS_RDONLY, NULL);
1369
1370 free(path);
1371
1372 return 0;
1373
1374out_error:
1375 saved_errno = errno;
1376 free(path);
1377 free(dirname);
1378 free(parts);
1379 free(abs_path);
1380 free(abs_path2);
1381 errno = saved_errno;
1382 return -1;
1383}
1384
33ad9f1a
CS
1385int lxc_cgroup_nrtasks_handler(struct lxc_handler *handler)
1386{
1387 struct cgroup_process_info *info = handler->cgroup;
1388 struct cgroup_mount_point *mp = NULL;
1389 char *abs_path = NULL;
1390 int ret;
460a1cf0 1391
33ad9f1a
CS
1392 if (!info) {
1393 errno = ENOENT;
1394 return -1;
b98f7d6e 1395 }
c8f7c563 1396
33ad9f1a 1397 if (info->designated_mount_point) {
8900b9eb 1398 mp = info->designated_mount_point;
33ad9f1a
CS
1399 } else {
1400 mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, false);
1401 if (!mp)
1402 return -1;
c8f7c563
CS
1403 }
1404
33ad9f1a
CS
1405 abs_path = cgroup_to_absolute_path(mp, info->cgroup_path, NULL);
1406 if (!abs_path)
1407 return -1;
1408
1409 ret = cgroup_recursive_task_count(abs_path);
1410 free(abs_path);
1411 return ret;
c8f7c563
CS
1412}
1413
33ad9f1a 1414struct cgroup_process_info *lxc_cgroup_process_info_getx(const char *proc_pid_cgroup_str, struct cgroup_meta_data *meta)
d08ba6ec 1415{
33ad9f1a
CS
1416 struct cgroup_process_info *result = NULL;
1417 FILE *proc_pid_cgroup = NULL;
1418 char *line = NULL;
1419 size_t sz = 0;
1420 int saved_errno = 0;
1421 struct cgroup_process_info **cptr = &result;
1422 struct cgroup_process_info *entry = NULL;
1423
025ed0f3 1424 process_lock();
33ad9f1a 1425 proc_pid_cgroup = fopen_cloexec(proc_pid_cgroup_str, "r");
025ed0f3 1426 process_unlock();
33ad9f1a 1427 if (!proc_pid_cgroup)
b98f7d6e 1428 return NULL;
1ac470c0 1429
33ad9f1a
CS
1430 while (getline(&line, &sz, proc_pid_cgroup) != -1) {
1431 /* file format: hierarchy:subsystems:group */
1432 char *colon1;
1433 char *colon2;
1434 char *endptr;
1435 int hierarchy_number;
1436 struct cgroup_hierarchy *h = NULL;
fd4f5a56 1437
33ad9f1a 1438 if (!line[0])
ae5c8b8e 1439 continue;
b98f7d6e 1440
33ad9f1a
CS
1441 if (line[strlen(line) - 1] == '\n')
1442 line[strlen(line) - 1] = '\0';
1443
1444 colon1 = strchr(line, ':');
1445 if (!colon1)
8900b9eb 1446 continue;
33ad9f1a
CS
1447 *colon1++ = '\0';
1448 colon2 = strchr(colon1, ':');
1449 if (!colon2)
ae5c8b8e 1450 continue;
33ad9f1a 1451 *colon2++ = '\0';
e4659536 1452
33ad9f1a
CS
1453 endptr = NULL;
1454 hierarchy_number = strtoul(line, &endptr, 10);
1455 if (!endptr || *endptr)
9a93d992 1456 continue;
9a93d992 1457
33ad9f1a
CS
1458 if (hierarchy_number > meta->maximum_hierarchy) {
1459 /* we encountered a hierarchy we didn't have before,
1460 * so probably somebody remounted some stuff in the
1461 * mean time...
1462 */
1463 errno = EAGAIN;
1464 goto out_error;
b98f7d6e 1465 }
33ad9f1a
CS
1466
1467 h = meta->hierarchies[hierarchy_number];
1468 if (!h) {
1469 /* we encountered a hierarchy that was thought to be
1470 * dead before, so probably somebody remounted some
1471 * stuff in the mean time...
1472 */
1473 errno = EAGAIN;
1474 goto out_error;
b98f7d6e 1475 }
33ad9f1a
CS
1476
1477 /* we are told that we should ignore this hierarchy */
1478 if (!h->used)
b98f7d6e 1479 continue;
5193cc3d 1480
33ad9f1a
CS
1481 entry = calloc(1, sizeof(struct cgroup_process_info));
1482 if (!entry)
1483 goto out_error;
fd4f5a56 1484
33ad9f1a
CS
1485 entry->meta_ref = lxc_cgroup_get_meta(meta);
1486 entry->hierarchy = h;
1487 entry->cgroup_path = strdup(colon2);
1488 if (!entry->cgroup_path)
1489 goto out_error;
d08ba6ec 1490
33ad9f1a
CS
1491 *cptr = entry;
1492 cptr = &entry->next;
1493 entry = NULL;
b98f7d6e 1494 }
b98f7d6e 1495
025ed0f3 1496 process_lock();
33ad9f1a 1497 fclose(proc_pid_cgroup);
025ed0f3 1498 process_unlock();
33ad9f1a
CS
1499 free(line);
1500 return result;
1501
1502out_error:
1503 saved_errno = errno;
025ed0f3 1504 process_lock();
33ad9f1a
CS
1505 if (proc_pid_cgroup)
1506 fclose(proc_pid_cgroup);
025ed0f3 1507 process_unlock();
33ad9f1a
CS
1508 lxc_cgroup_process_info_free(result);
1509 lxc_cgroup_process_info_free(entry);
1510 free(line);
1511 errno = saved_errno;
ae5c8b8e 1512 return NULL;
36b86299
DL
1513}
1514
33ad9f1a 1515char **subsystems_from_mount_options(const char *mount_options, char **kernel_list)
36b86299 1516{
33ad9f1a
CS
1517 char *token, *str, *saveptr = NULL;
1518 char **result = NULL;
1519 size_t result_capacity = 0;
8900b9eb 1520 size_t result_count = 0;
33ad9f1a
CS
1521 int saved_errno;
1522 int r;
ef342abb 1523
33ad9f1a
CS
1524 str = alloca(strlen(mount_options)+1);
1525 strcpy(str, mount_options);
1526 for (; (token = strtok_r(str, ",", &saveptr)); str = NULL) {
1527 /* we have a subsystem if it's either in the list of
1528 * subsystems provided by the kernel OR if it starts
1529 * with name= for named hierarchies
1530 */
1531 if (!strncmp(token, "name=", 5) || lxc_string_in_array(token, (const char **)kernel_list)) {
1532 r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 12);
1533 if (r < 0)
1534 goto out_free;
1535 result[result_count + 1] = NULL;
1536 result[result_count] = strdup(token);
1537 if (!result[result_count])
1538 goto out_free;
1539 result_count++;
1540 }
ae5c8b8e 1541 }
f0e64b8b 1542
33ad9f1a
CS
1543 return result;
1544
1545out_free:
1546 saved_errno = errno;
1547 lxc_free_array((void**)result, free);
1548 errno = saved_errno;
1549 return NULL;
b98f7d6e
SH
1550}
1551
33ad9f1a 1552void lxc_cgroup_mount_point_free(struct cgroup_mount_point *mp)
b98f7d6e 1553{
33ad9f1a
CS
1554 if (!mp)
1555 return;
1556 free(mp->mount_point);
1557 free(mp->mount_prefix);
1558 free(mp);
bcbd102c
SH
1559}
1560
33ad9f1a 1561void lxc_cgroup_hierarchy_free(struct cgroup_hierarchy *h)
341a9bd8 1562{
33ad9f1a
CS
1563 if (!h)
1564 return;
1565 lxc_free_array((void **)h->subsystems, free);
1566 free(h);
1567}
341a9bd8 1568
33ad9f1a
CS
1569bool is_valid_cgroup(const char *name)
1570{
1571 const char *p;
1572 for (p = name; *p; p++) {
1573 if (*p < 32 || *p == 127 || *p == '/')
1574 return false;
341a9bd8 1575 }
33ad9f1a
CS
1576 return strcmp(name, ".") != 0 && strcmp(name, "..") != 0;
1577}
341a9bd8 1578
33ad9f1a
CS
1579int create_or_remove_cgroup(bool do_remove, struct cgroup_mount_point *mp, const char *path)
1580{
1581 int r, saved_errno = 0;
1582 char *buf = cgroup_to_absolute_path(mp, path, NULL);
1583 if (!buf)
1584 return -1;
341a9bd8 1585
33ad9f1a
CS
1586 /* create or remove directory */
1587 r = do_remove ?
1588 rmdir(buf) :
1589 mkdir(buf, 0777);
1590 saved_errno = errno;
1591 free(buf);
1592 errno = saved_errno;
1593 return r;
341a9bd8 1594}
bcbd102c 1595
33ad9f1a 1596int create_cgroup(struct cgroup_mount_point *mp, const char *path)
a6ddef61 1597{
33ad9f1a 1598 return create_or_remove_cgroup(false, mp, path);
a6ddef61
MN
1599}
1600
33ad9f1a 1601int remove_cgroup(struct cgroup_mount_point *mp, const char *path)
576f946d 1602{
33ad9f1a
CS
1603 return create_or_remove_cgroup(true, mp, path);
1604}
576f946d 1605
33ad9f1a
CS
1606char *cgroup_to_absolute_path(struct cgroup_mount_point *mp, const char *path, const char *suffix)
1607{
1608 /* first we have to make sure we subtract the mount point's prefix */
1609 char *prefix = mp->mount_prefix;
1610 char *buf;
1611 ssize_t len, rv;
1612
1613 /* we want to make sure only absolute paths to cgroups are passed to us */
1614 if (path[0] != '/') {
1615 errno = EINVAL;
1616 return NULL;
1617 }
b98f7d6e 1618
33ad9f1a
CS
1619 if (prefix && !strcmp(prefix, "/"))
1620 prefix = NULL;
b98f7d6e 1621
33ad9f1a
CS
1622 /* prefix doesn't match */
1623 if (prefix && strncmp(prefix, path, strlen(prefix)) != 0) {
1624 errno = EINVAL;
1625 return NULL;
1626 }
1627 /* if prefix is /foo and path is /foobar */
1628 if (prefix && path[strlen(prefix)] != '/' && path[strlen(prefix)] != '\0') {
1629 errno = EINVAL;
1630 return NULL;
1631 }
b98f7d6e 1632
33ad9f1a
CS
1633 /* remove prefix from path */
1634 path += prefix ? strlen(prefix) : 0;
b98f7d6e 1635
33ad9f1a
CS
1636 len = strlen(mp->mount_point) + strlen(path) + (suffix ? strlen(suffix) : 0);
1637 buf = calloc(len + 1, 1);
1638 rv = snprintf(buf, len + 1, "%s%s%s", mp->mount_point, path, suffix ? suffix : "");
8900b9eb 1639 if (rv > len) {
33ad9f1a
CS
1640 free(buf);
1641 errno = ENOMEM;
8900b9eb 1642 return NULL;
8b92dc3a 1643 }
576f946d 1644
33ad9f1a 1645 return buf;
e0f888d9 1646}
283678ed 1647
33ad9f1a 1648struct cgroup_process_info *find_info_for_subsystem(struct cgroup_process_info *info, const char *subsystem)
283678ed 1649{
33ad9f1a
CS
1650 struct cgroup_process_info *info_ptr;
1651 for (info_ptr = info; info_ptr; info_ptr = info_ptr->next) {
1652 struct cgroup_hierarchy *h = info_ptr->hierarchy;
1653 if (lxc_string_in_array(subsystem, (const char **)h->subsystems))
1654 return info_ptr;
b98f7d6e 1655 }
33ad9f1a
CS
1656 errno = ENOENT;
1657 return NULL;
1658}
283678ed 1659
33ad9f1a
CS
1660int do_cgroup_get(const char *cgroup_path, const char *sub_filename, char *value, size_t len)
1661{
1662 const char *parts[3] = {
1663 cgroup_path,
1664 sub_filename,
1665 NULL
1666 };
1667 char *filename;
1668 int ret, saved_errno;
1669
1670 filename = lxc_string_join("/", parts, false);
1671 if (!filename)
1672 return -1;
1673
1674 ret = lxc_read_from_file(filename, value, len);
1675 saved_errno = errno;
1676 free(filename);
1677 errno = saved_errno;
1678 return ret;
283678ed 1679}
b113383b 1680
33ad9f1a 1681int do_cgroup_set(const char *cgroup_path, const char *sub_filename, const char *value)
b113383b 1682{
33ad9f1a
CS
1683 const char *parts[3] = {
1684 cgroup_path,
1685 sub_filename,
1686 NULL
1687 };
1688 char *filename;
1689 int ret, saved_errno;
b113383b 1690
33ad9f1a
CS
1691 filename = lxc_string_join("/", parts, false);
1692 if (!filename)
1693 return -1;
b113383b 1694
33ad9f1a
CS
1695 ret = lxc_write_to_file(filename, value, strlen(value), false);
1696 saved_errno = errno;
1697 free(filename);
1698 errno = saved_errno;
1699 return ret;
b98f7d6e
SH
1700}
1701
33ad9f1a 1702int do_setup_cgroup(struct lxc_handler *h, struct lxc_list *cgroup_settings, bool do_devices)
b98f7d6e
SH
1703{
1704 struct lxc_list *iterator;
1705 struct lxc_cgroup *cg;
1706 int ret = -1;
1707
33ad9f1a 1708 if (lxc_list_empty(cgroup_settings))
b98f7d6e
SH
1709 return 0;
1710
33ad9f1a 1711 lxc_list_for_each(iterator, cgroup_settings) {
b98f7d6e
SH
1712 cg = iterator->elem;
1713
33ad9f1a 1714 if (do_devices == !strncmp("devices", cg->subsystem, 7)) {
b98f7d6e 1715 if (strcmp(cg->subsystem, "devices.deny") == 0 &&
33ad9f1a 1716 cgroup_devices_has_allow_or_deny(h, cg->value, false))
b98f7d6e
SH
1717 continue;
1718 if (strcmp(cg->subsystem, "devices.allow") == 0 &&
33ad9f1a 1719 cgroup_devices_has_allow_or_deny(h, cg->value, true))
b98f7d6e 1720 continue;
33ad9f1a 1721 if (lxc_cgroup_set_handler(cg->subsystem, cg->value, h)) {
b98f7d6e
SH
1722 ERROR("Error setting %s to %s for %s\n",
1723 cg->subsystem, cg->value, h->name);
1724 goto out;
1725 }
b113383b 1726 }
b98f7d6e
SH
1727
1728 DEBUG("cgroup '%s' set to '%s'", cg->subsystem, cg->value);
b113383b
SH
1729 }
1730
b98f7d6e
SH
1731 ret = 0;
1732 INFO("cgroup has been setup");
1733out:
b113383b
SH
1734 return ret;
1735}
b98f7d6e 1736
33ad9f1a
CS
1737bool cgroup_devices_has_allow_or_deny(struct lxc_handler *h, char *v, bool for_allow)
1738{
1739 char *path;
1740 FILE *devices_list;
8900b9eb 1741 char *line = NULL;
33ad9f1a
CS
1742 size_t sz = 0;
1743 bool ret = !for_allow;
1744 const char *parts[3] = {
1745 NULL,
1746 "devices.list",
1747 NULL
1748 };
1749
1750 // XXX FIXME if users could use something other than 'lxc.devices.deny = a'.
1751 // not sure they ever do, but they *could*
1752 // right now, I'm assuming they do NOT
1753 if (!for_allow && strcmp(v, "a") != 0 && strcmp(v, "a *:* rwm") != 0)
1754 return false;
1755
1756 parts[0] = (const char *)lxc_cgroup_get_hierarchy_abs_path_handler("devices", h);
1757 if (!parts[0])
1758 return false;
1759 path = lxc_string_join("/", parts, false);
1760 if (!path) {
1761 free((void *)parts[0]);
1762 return false;
1763 }
1764
025ed0f3 1765 process_lock();
33ad9f1a 1766 devices_list = fopen_cloexec(path, "r");
025ed0f3 1767 process_unlock();
33ad9f1a
CS
1768 if (!devices_list) {
1769 free(path);
1770 return false;
1771 }
1772
1773 while (getline(&line, &sz, devices_list) != -1) {
1774 size_t len = strlen(line);
1775 if (len > 0 && line[len-1] == '\n')
1776 line[len-1] = '\0';
1777 if (strcmp(line, "a *:* rwm") == 0) {
1778 ret = for_allow;
1779 goto out;
1780 } else if (for_allow && strcmp(line, v) == 0) {
1781 ret = true;
8900b9eb 1782 goto out;
33ad9f1a
CS
1783 }
1784 }
1785
1786out:
025ed0f3 1787 process_lock();
33ad9f1a 1788 fclose(devices_list);
025ed0f3 1789 process_unlock();
33ad9f1a
CS
1790 free(line);
1791 free(path);
1792 return ret;
1793}
1794
1795int cgroup_recursive_task_count(const char *cgroup_path)
b98f7d6e 1796{
33ad9f1a
CS
1797 DIR *d;
1798 struct dirent *dent_buf;
1799 struct dirent *dent;
8900b9eb 1800 ssize_t name_max;
33ad9f1a
CS
1801 int n = 0, r;
1802
1803 /* see man readdir_r(3) */
1804 name_max = pathconf(cgroup_path, _PC_NAME_MAX);
1805 if (name_max <= 0)
1806 name_max = 255;
1807 dent_buf = malloc(offsetof(struct dirent, d_name) + name_max + 1);
1808 if (!dent_buf)
1809 return -1;
1810
025ed0f3 1811 process_lock();
33ad9f1a 1812 d = opendir(cgroup_path);
025ed0f3 1813 process_unlock();
33ad9f1a
CS
1814 if (!d)
1815 return 0;
1816
1817 while (readdir_r(d, dent_buf, &dent) == 0 && dent) {
1818 const char *parts[3] = {
1819 cgroup_path,
1820 dent->d_name,
1821 NULL
1822 };
1823 char *sub_path;
1824 struct stat st;
1825
1826 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1827 continue;
1828 sub_path = lxc_string_join("/", parts, false);
1829 if (!sub_path) {
025ed0f3 1830 process_lock();
33ad9f1a 1831 closedir(d);
025ed0f3 1832 process_unlock();
33ad9f1a
CS
1833 free(dent_buf);
1834 return -1;
1835 }
1836 r = stat(sub_path, &st);
1837 if (r < 0) {
025ed0f3 1838 process_lock();
33ad9f1a 1839 closedir(d);
025ed0f3 1840 process_unlock();
33ad9f1a
CS
1841 free(dent_buf);
1842 free(sub_path);
1843 return -1;
1844 }
1845 if (S_ISDIR(st.st_mode)) {
1846 r = cgroup_recursive_task_count(sub_path);
1847 if (r >= 0)
1848 n += r;
1849 } else if (!strcmp(dent->d_name, "tasks")) {
1850 r = count_lines(sub_path);
1851 if (r >= 0)
1852 n += r;
1853 }
1854 free(sub_path);
1855 }
025ed0f3 1856 process_lock();
33ad9f1a 1857 closedir(d);
025ed0f3 1858 process_unlock();
33ad9f1a
CS
1859 free(dent_buf);
1860
1861 return n;
1862}
1863
8900b9eb 1864int count_lines(const char *fn)
33ad9f1a
CS
1865{
1866 FILE *f;
1867 char *line = NULL;
1868 size_t sz = 0;
1869 int n = 0;
1870
025ed0f3 1871 process_lock();
33ad9f1a 1872 f = fopen_cloexec(fn, "r");
025ed0f3 1873 process_unlock();
33ad9f1a
CS
1874 if (!f)
1875 return -1;
1876
1877 while (getline(&line, &sz, f) != -1) {
1878 n++;
1879 }
1880 free(line);
025ed0f3 1881 process_lock();
33ad9f1a 1882 fclose(f);
025ed0f3 1883 process_unlock();
33ad9f1a 1884 return n;
b98f7d6e
SH
1885}
1886
33ad9f1a 1887int handle_clone_children(struct cgroup_mount_point *mp, char *cgroup_path)
b98f7d6e 1888{
33ad9f1a
CS
1889 int r, saved_errno = 0;
1890 /* if this is a cpuset hierarchy, we have to set cgroup.clone_children in
1891 * the base cgroup, otherwise containers will start with an empty cpuset.mems
1892 * and cpuset.cpus and then
1893 */
1894 if (lxc_string_in_array("cpuset", (const char **)mp->hierarchy->subsystems)) {
1895 char *cc_path = cgroup_to_absolute_path(mp, cgroup_path, "/cgroup.clone_children");
1896 if (!cc_path)
1897 return -1;
1898 r = lxc_write_to_file(cc_path, "1", 1, false);
1899 saved_errno = errno;
1900 free(cc_path);
1901 errno = saved_errno;
1902 return r < 0 ? -1 : 0;
1903 }
1904 return 0;
b98f7d6e 1905}