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