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