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