/
cgroup.c
1591 lines (1393 loc) · 43.3 KB
/
cgroup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <daniel.lezcano at free.fr>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/inotify.h>
#include <netinet/in.h>
#include <net/if.h>
#include "error.h"
#include "config.h"
#include "commands.h"
#include "list.h"
#include "conf.h"
#include "utils.h"
#include <lxc/log.h>
#include <lxc/cgroup.h>
#include <lxc/start.h>
#if IS_BIONIC
#include <../include/lxcmntent.h>
#else
#include <mntent.h>
#endif
#ifndef HAVE_GETLINE
#ifdef HAVE_FGETLN
#include <../include/getline.h>
#endif
#endif
lxc_log_define(lxc_cgroup, lxc);
static struct cgroup_process_info *lxc_cgroup_process_info_getx(const char *proc_pid_cgroup_str, struct cgroup_meta_data *meta);
static char **subsystems_from_mount_options(const char *mount_options, char **kernel_list);
static void lxc_cgroup_mount_point_free(struct cgroup_mount_point *mp);
static void lxc_cgroup_hierarchy_free(struct cgroup_hierarchy *h);
static bool is_valid_cgroup(const char *name);
static int create_or_remove_cgroup(bool remove, struct cgroup_mount_point *mp, const char *path);
static int create_cgroup(struct cgroup_mount_point *mp, const char *path);
static int remove_cgroup(struct cgroup_mount_point *mp, const char *path);
static char *cgroup_to_absolute_path(struct cgroup_mount_point *mp, const char *path, const char *suffix);
static struct cgroup_process_info *find_info_for_subsystem(struct cgroup_process_info *info, const char *subsystem);
static int do_cgroup_get(const char *cgroup_path, const char *sub_filename, char *value, size_t len);
static int do_cgroup_set(const char *cgroup_path, const char *sub_filename, const char *value);
static bool cgroup_devices_has_allow_or_deny(struct lxc_handler *h, char *v, bool for_allow);
static int do_setup_cgroup(struct lxc_handler *h, struct lxc_list *cgroup_settings, bool do_devices);
static int cgroup_recursive_task_count(const char *cgroup_path);
static int count_lines(const char *fn);
static int handle_clone_children(struct cgroup_mount_point *mp, char *cgroup_path);
struct cgroup_meta_data *lxc_cgroup_load_meta()
{
const char *cgroup_use = NULL;
char **cgroup_use_list = NULL;
struct cgroup_meta_data *md = NULL;
int saved_errno;
errno = 0;
cgroup_use = lxc_global_config_value("cgroup.use");
if (!cgroup_use && errno != 0)
return NULL;
if (cgroup_use) {
cgroup_use_list = lxc_string_split_and_trim(cgroup_use, ',');
if (!cgroup_use_list)
return NULL;
}
md = lxc_cgroup_load_meta2((const char **)cgroup_use_list);
saved_errno = errno;
lxc_free_array((void **)cgroup_use_list, free);
errno = saved_errno;
return md;
}
struct cgroup_meta_data *lxc_cgroup_load_meta2(const char **subsystem_whitelist)
{
FILE *proc_cgroups = NULL;
FILE *proc_self_cgroup = NULL;
FILE *proc_self_mountinfo = NULL;
bool all_kernel_subsystems = true;
bool all_named_subsystems = false;
struct cgroup_meta_data *meta_data = NULL;
char **kernel_subsystems = NULL;
size_t kernel_subsystems_count = 0;
size_t kernel_subsystems_capacity = 0;
size_t hierarchy_capacity = 0;
size_t mount_point_capacity = 0;
size_t mount_point_count = 0;
char **tokens = NULL;
size_t token_capacity = 0;
char *line = NULL;
size_t sz = 0;
int r, saved_errno = 0;
/* if the subsystem whitelist is not specified, include all
* hierarchies that contain kernel subsystems by default but
* no hierarchies that only contain named subsystems
*
* if it is specified, the specifier @all will select all
* hierarchies, @kernel will select all hierarchies with
* kernel subsystems and @named will select all named
* hierarchies
*/
all_kernel_subsystems = subsystem_whitelist ?
(lxc_string_in_array("@kernel", subsystem_whitelist) || lxc_string_in_array("@all", subsystem_whitelist)) :
true;
all_named_subsystems = subsystem_whitelist ?
(lxc_string_in_array("@named", subsystem_whitelist) || lxc_string_in_array("@all", subsystem_whitelist)) :
false;
meta_data = calloc(1, sizeof(struct cgroup_meta_data));
if (!meta_data)
return NULL;
meta_data->ref = 1;
/* Step 1: determine all kernel subsystems */
proc_cgroups = fopen_cloexec("/proc/cgroups", "r");
if (!proc_cgroups)
goto out_error;
while (getline(&line, &sz, proc_cgroups) != -1) {
char *tab1;
char *tab2;
int hierarchy_number;
if (line[0] == '#')
continue;
if (!line[0])
continue;
tab1 = strchr(line, '\t');
if (!tab1)
continue;
*tab1++ = '\0';
tab2 = strchr(tab1, '\t');
if (!tab2)
continue;
*tab2 = '\0';
tab2 = NULL;
hierarchy_number = strtoul(tab1, &tab2, 10);
if (!tab2 || *tab2)
continue;
(void)hierarchy_number;
r = lxc_grow_array((void ***)&kernel_subsystems, &kernel_subsystems_capacity, kernel_subsystems_count + 1, 12);
if (r < 0)
goto out_error;
kernel_subsystems[kernel_subsystems_count] = strdup(line);
if (!kernel_subsystems[kernel_subsystems_count])
goto out_error;
kernel_subsystems_count++;
}
fclose(proc_cgroups);
proc_cgroups = NULL;
/* Step 2: determine all hierarchies (by reading /proc/self/cgroup),
* since mount points don't specify hierarchy number and
* /proc/cgroups does not contain named hierarchies
*/
proc_self_cgroup = fopen_cloexec("/proc/self/cgroup", "r");
/* if for some reason (because of setns() and pid namespace for example),
* /proc/self is not valid, we try /proc/1/cgroup... */
if (!proc_self_cgroup)
proc_self_cgroup = fopen_cloexec("/proc/1/cgroup", "r");
if (!proc_self_cgroup)
goto out_error;
while (getline(&line, &sz, proc_self_cgroup) != -1) {
/* file format: hierarchy:subsystems:group,
* we only extract hierarchy and subsystems
* here */
char *colon1;
char *colon2;
int hierarchy_number;
struct cgroup_hierarchy *h = NULL;
char **p;
if (!line[0])
continue;
colon1 = strchr(line, ':');
if (!colon1)
continue;
*colon1++ = '\0';
colon2 = strchr(colon1, ':');
if (!colon2)
continue;
*colon2 = '\0';
colon2 = NULL;
hierarchy_number = strtoul(line, &colon2, 10);
if (!colon2 || *colon2)
continue;
if (hierarchy_number > meta_data->maximum_hierarchy) {
/* lxc_grow_array will never shrink, so even if we find a lower
* hierarchy number here, the array will never be smaller
*/
r = lxc_grow_array((void ***)&meta_data->hierarchies, &hierarchy_capacity, hierarchy_number + 1, 12);
if (r < 0)
goto out_error;
meta_data->maximum_hierarchy = hierarchy_number;
}
/* this shouldn't happen, we had this already */
if (meta_data->hierarchies[hierarchy_number])
goto out_error;
h = calloc(1, sizeof(struct cgroup_hierarchy));
if (!h)
goto out_error;
meta_data->hierarchies[hierarchy_number] = h;
h->index = hierarchy_number;
h->subsystems = lxc_string_split_and_trim(colon1, ',');
if (!h->subsystems)
goto out_error;
/* see if this hierarchy should be considered */
if (!all_kernel_subsystems || !all_named_subsystems) {
for (p = h->subsystems; *p; p++) {
if (!strncmp(*p, "name=", 5)) {
if (all_named_subsystems || (subsystem_whitelist && lxc_string_in_array(*p, subsystem_whitelist))) {
h->used = true;
break;
}
} else {
if (all_kernel_subsystems || (subsystem_whitelist && lxc_string_in_array(*p, subsystem_whitelist))) {
h->used = true;
break;
}
}
}
} else {
/* we want all hierarchy anyway */
h->used = true;
}
}
fclose(proc_self_cgroup);
proc_self_cgroup = NULL;
/* Step 3: determine all mount points of each hierarchy */
proc_self_mountinfo = fopen_cloexec("/proc/self/mountinfo", "r");
/* if for some reason (because of setns() and pid namespace for example),
* /proc/self is not valid, we try /proc/1/cgroup... */
if (!proc_self_mountinfo)
proc_self_mountinfo = fopen_cloexec("/proc/1/mountinfo", "r");
if (!proc_self_mountinfo)
goto out_error;
while (getline(&line, &sz, proc_self_mountinfo) != -1) {
char *token, *saveptr = NULL;
size_t i, j, k;
struct cgroup_mount_point *mount_point;
struct cgroup_hierarchy *h;
char **subsystems;
if (line[0] && line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = '\0';
for (i = 0; (token = strtok_r(line, " ", &saveptr)); line = NULL) {
r = lxc_grow_array((void ***)&tokens, &token_capacity, i + 1, 64);
if (r < 0)
goto out_error;
tokens[i++] = token;
}
/* layout of /proc/self/mountinfo:
* 0: id
* 1: parent id
* 2: device major:minor
* 3: mount prefix
* 4: mount point
* 5: per-mount options
* [optional X]: additional data
* X+7: "-"
* X+8: type
* X+9: source
* X+10: per-superblock options
*/
for (j = 6; j < i && tokens[j]; j++)
if (!strcmp(tokens[j], "-"))
break;
/* could not find separator */
if (j >= i || !tokens[j])
continue;
/* there should be exactly three fields after
* the separator
*/
if (i != j + 4)
continue;
/* not a cgroup filesystem */
if (strcmp(tokens[j + 1], "cgroup") != 0)
continue;
subsystems = subsystems_from_mount_options(tokens[j + 3], kernel_subsystems);
if (!subsystems)
goto out_error;
h = NULL;
for (k = 1; k <= meta_data->maximum_hierarchy; k++) {
if (meta_data->hierarchies[k] &&
meta_data->hierarchies[k]->subsystems[0] &&
lxc_string_in_array(meta_data->hierarchies[k]->subsystems[0], (const char **)subsystems)) {
/* TODO: we could also check if the lists really match completely,
* just to have an additional sanity check */
h = meta_data->hierarchies[k];
break;
}
}
lxc_free_array((void **)subsystems, free);
r = lxc_grow_array((void ***)&meta_data->mount_points, &mount_point_capacity, mount_point_count + 1, 12);
if (r < 0)
goto out_error;
/* create mount point object */
mount_point = calloc(1, sizeof(*mount_point));
if (!mount_point)
goto out_error;
meta_data->mount_points[mount_point_count++] = mount_point;
mount_point->hierarchy = h;
mount_point->mount_point = strdup(tokens[4]);
mount_point->mount_prefix = strdup(tokens[3]);
if (!mount_point->mount_point || !mount_point->mount_prefix)
goto out_error;
mount_point->read_only = !lxc_string_in_list("rw", tokens[5], ',');
if (!strcmp(mount_point->mount_prefix, "/")) {
if (mount_point->read_only) {
if (!h->ro_absolute_mount_point)
h->ro_absolute_mount_point = mount_point;
} else {
if (!h->rw_absolute_mount_point)
h->rw_absolute_mount_point = mount_point;
}
}
k = lxc_array_len((void **)h->all_mount_points);
r = lxc_grow_array((void ***)&h->all_mount_points, &h->all_mount_point_capacity, k + 1, 4);
if (r < 0)
goto out_error;
h->all_mount_points[k] = mount_point;
}
/* oops, we couldn't find anything */
if (!meta_data->hierarchies || !meta_data->mount_points) {
errno = EINVAL;
goto out_error;
}
return meta_data;
out_error:
saved_errno = errno;
if (proc_cgroups)
fclose(proc_cgroups);
if (proc_self_cgroup)
fclose(proc_self_cgroup);
if (proc_self_mountinfo)
fclose(proc_self_mountinfo);
free(line);
free(tokens);
lxc_free_array((void **)kernel_subsystems, free);
lxc_cgroup_put_meta(meta_data);
errno = saved_errno;
return NULL;
}
struct cgroup_meta_data *lxc_cgroup_get_meta(struct cgroup_meta_data *meta_data)
{
meta_data->ref++;
return meta_data;
}
struct cgroup_meta_data *lxc_cgroup_put_meta(struct cgroup_meta_data *meta_data)
{
size_t i;
if (!meta_data)
return NULL;
if (--meta_data->ref > 0)
return meta_data;
lxc_free_array((void **)meta_data->mount_points, (lxc_free_fn)lxc_cgroup_mount_point_free);
if (meta_data->hierarchies) {
for (i = 0; i <= meta_data->maximum_hierarchy; i++)
lxc_cgroup_hierarchy_free(meta_data->hierarchies[i]);
}
free(meta_data->hierarchies);
return NULL;
}
struct cgroup_hierarchy *lxc_cgroup_find_hierarchy(struct cgroup_meta_data *meta_data, const char *subsystem)
{
size_t i;
for (i = 0; i <= meta_data->maximum_hierarchy; i++) {
struct cgroup_hierarchy *h = meta_data->hierarchies[i];
if (h && lxc_string_in_array(subsystem, (const char **)h->subsystems))
return h;
}
return NULL;
}
struct cgroup_mount_point *lxc_cgroup_find_mount_point(struct cgroup_hierarchy *hierarchy, const char *group, bool should_be_writable)
{
struct cgroup_mount_point **mps;
struct cgroup_mount_point *current_result = NULL;
ssize_t quality = -1;
/* trivial case */
if (hierarchy->rw_absolute_mount_point)
return hierarchy->rw_absolute_mount_point;
if (!should_be_writable && hierarchy->ro_absolute_mount_point)
return hierarchy->ro_absolute_mount_point;
for (mps = hierarchy->all_mount_points; mps && *mps; mps++) {
struct cgroup_mount_point *mp = *mps;
size_t prefix_len = mp->mount_prefix ? strlen(mp->mount_prefix) : 0;
if (prefix_len == 1 && mp->mount_prefix[0] == '/')
prefix_len = 0;
if (should_be_writable && mp->read_only)
continue;
if (!prefix_len ||
(strncmp(group, mp->mount_prefix, prefix_len) == 0 &&
(group[prefix_len] == '\0' || group[prefix_len] == '/'))) {
/* search for the best quality match, i.e. the match with the
* shortest prefix where this group is still contained
*/
if (quality == -1 || prefix_len < quality) {
current_result = mp;
quality = prefix_len;
}
}
}
if (!current_result)
errno = ENOENT;
return current_result;
}
char *lxc_cgroup_find_abs_path(const char *subsystem, const char *group, bool should_be_writable, const char *suffix)
{
struct cgroup_meta_data *meta_data;
struct cgroup_hierarchy *h;
struct cgroup_mount_point *mp;
char *result;
int saved_errno;
meta_data = lxc_cgroup_load_meta();
if (!meta_data)
return NULL;
h = lxc_cgroup_find_hierarchy(meta_data, subsystem);
if (!h)
goto out_error;
mp = lxc_cgroup_find_mount_point(h, group, should_be_writable);
if (!mp)
goto out_error;
result = cgroup_to_absolute_path(mp, group, suffix);
if (!result)
goto out_error;
lxc_cgroup_put_meta(meta_data);
return result;
out_error:
saved_errno = errno;
lxc_cgroup_put_meta(meta_data);
errno = saved_errno;
return NULL;
}
struct cgroup_process_info *lxc_cgroup_process_info_get(pid_t pid, struct cgroup_meta_data *meta)
{
char pid_buf[32];
snprintf(pid_buf, 32, "/proc/%lu/cgroup", (unsigned long)pid);
return lxc_cgroup_process_info_getx(pid_buf, meta);
}
struct cgroup_process_info *lxc_cgroup_process_info_get_init(struct cgroup_meta_data *meta)
{
return lxc_cgroup_process_info_get(1, meta);
}
struct cgroup_process_info *lxc_cgroup_process_info_get_self(struct cgroup_meta_data *meta)
{
struct cgroup_process_info *i;
i = lxc_cgroup_process_info_getx("/proc/self/cgroup", meta);
if (!i)
i = lxc_cgroup_process_info_get(getpid(), meta);
return i;
}
/* create a new cgroup */
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)
{
char **cgroup_path_components = NULL;
char **p = NULL;
char *path_so_far = NULL;
char **new_cgroup_paths = NULL;
char **new_cgroup_paths_sub = NULL;
struct cgroup_mount_point *mp;
struct cgroup_hierarchy *h;
struct cgroup_process_info *base_info = NULL;
struct cgroup_process_info *info_ptr;
int saved_errno;
int r;
unsigned suffix = 0;
bool had_sub_pattern = false;
size_t i;
if (!is_valid_cgroup(name)) {
ERROR("Invalid cgroup name: '%s'", name);
errno = EINVAL;
return NULL;
}
if (!strstr(path_pattern, "%n")) {
ERROR("Invalid cgroup path pattern: '%s'; contains no %%n for specifying container name", path_pattern);
errno = EINVAL;
return NULL;
}
/* we will modify the result of this operation directly,
* so we don't have to copy the data structure
*/
base_info = (path_pattern[0] == '/') ?
lxc_cgroup_process_info_get_init(meta_data) :
lxc_cgroup_process_info_get_self(meta_data);
if (!base_info)
return NULL;
new_cgroup_paths = calloc(meta_data->maximum_hierarchy + 1, sizeof(char *));
if (!new_cgroup_paths)
goto out_initial_error;
new_cgroup_paths_sub = calloc(meta_data->maximum_hierarchy + 1, sizeof(char *));
if (!new_cgroup_paths_sub)
goto out_initial_error;
/* find mount points we can use */
for (info_ptr = base_info; info_ptr; info_ptr = info_ptr->next) {
h = info_ptr->hierarchy;
mp = lxc_cgroup_find_mount_point(h, info_ptr->cgroup_path, true);
if (!mp) {
ERROR("Could not find writable mount point for cgroup hierarchy %d while trying to create cgroup.", h->index);
goto out_initial_error;
}
info_ptr->designated_mount_point = mp;
if (handle_clone_children(mp, info_ptr->cgroup_path) < 0) {
ERROR("Could not set clone_children to 1 for cpuset hierarchy in parent cgroup.");
goto out_initial_error;
}
}
/* normalize the path */
cgroup_path_components = lxc_normalize_path(path_pattern);
if (!cgroup_path_components)
goto out_initial_error;
/* go through the path components to see if we can create them */
for (p = cgroup_path_components; *p || (sub_pattern && !had_sub_pattern); p++) {
/* we only want to create the same component with -1, -2, etc.
* if the component contains the container name itself, otherwise
* it's not an error if it already exists
*/
char *p_eff = *p ? *p : (char *)sub_pattern;
bool contains_name = strstr(p_eff, "%n");
char *current_component = NULL;
char *current_subpath = NULL;
char *current_entire_path = NULL;
char *parts[3];
size_t j = 0;
i = 0;
/* if we are processing the subpattern, we want to make sure
* loop is ended the next time around
*/
if (!*p) {
had_sub_pattern = true;
p--;
}
goto find_name_on_this_level;
cleanup_name_on_this_level:
/* This is reached if we found a name clash.
* In that case, remove the cgroup from all previous hierarchies
*/
for (j = 0, info_ptr = base_info; j < i && info_ptr; info_ptr = info_ptr->next, j++) {
r = remove_cgroup(info_ptr->designated_mount_point, info_ptr->created_paths[info_ptr->created_paths_count - 1]);
if (r < 0)
WARN("could not clean up cgroup we created when trying to create container");
free(info_ptr->created_paths[info_ptr->created_paths_count - 1]);
info_ptr->created_paths[--info_ptr->created_paths_count] = NULL;
}
if (current_component != current_subpath)
free(current_subpath);
if (current_component != p_eff)
free(current_component);
current_component = current_subpath = NULL;
/* try again with another suffix */
++suffix;
find_name_on_this_level:
/* determine name of the path component we should create */
if (contains_name && suffix > 0) {
char *buf = calloc(strlen(name) + 32, 1);
if (!buf)
goto out_initial_error;
snprintf(buf, strlen(name) + 32, "%s-%u", name, suffix);
current_component = lxc_string_replace("%n", buf, p_eff);
free(buf);
} else {
current_component = contains_name ? lxc_string_replace("%n", name, p_eff) : p_eff;
}
parts[0] = path_so_far;
parts[1] = current_component;
parts[2] = NULL;
current_subpath = path_so_far ? lxc_string_join("/", (const char **)parts, false) : current_component;
/* Now go through each hierarchy and try to create the
* corresponding cgroup
*/
for (i = 0, info_ptr = base_info; info_ptr; info_ptr = info_ptr->next, i++) {
char *parts2[3];
current_entire_path = NULL;
parts2[0] = !strcmp(info_ptr->cgroup_path, "/") ? "" : info_ptr->cgroup_path;
parts2[1] = current_subpath;
parts2[2] = NULL;
current_entire_path = lxc_string_join("/", (const char **)parts2, false);
if (!*p) {
/* we are processing the subpath, so only update that one */
free(new_cgroup_paths_sub[i]);
new_cgroup_paths_sub[i] = strdup(current_entire_path);
if (!new_cgroup_paths_sub[i])
goto cleanup_from_error;
} else {
/* remember which path was used on this controller */
free(new_cgroup_paths[i]);
new_cgroup_paths[i] = strdup(current_entire_path);
if (!new_cgroup_paths[i])
goto cleanup_from_error;
}
r = create_cgroup(info_ptr->designated_mount_point, current_entire_path);
if (r < 0 && errno == EEXIST && contains_name) {
/* name clash => try new name with new suffix */
free(current_entire_path);
current_entire_path = NULL;
goto cleanup_name_on_this_level;
} else if (r < 0 && errno != EEXIST) {
SYSERROR("Could not create cgroup %s", current_entire_path);
goto cleanup_from_error;
} else if (r == 0) {
/* successfully created */
r = lxc_grow_array((void ***)&info_ptr->created_paths, &info_ptr->created_paths_capacity, info_ptr->created_paths_count + 1, 8);
if (r < 0)
goto cleanup_from_error;
info_ptr->created_paths[info_ptr->created_paths_count++] = current_entire_path;
} else {
/* if we didn't create the cgroup, then we have to make sure that
* further cgroups will be created properly
*/
if (handle_clone_children(mp, info_ptr->cgroup_path) < 0) {
ERROR("Could not set clone_children to 1 for cpuset hierarchy in pre-existing cgroup.");
goto cleanup_from_error;
}
/* already existed but path component of pattern didn't contain '%n',
* so this is not an error; but then we don't need current_entire_path
* anymore...
*/
free(current_entire_path);
current_entire_path = NULL;
}
}
/* save path so far */
free(path_so_far);
path_so_far = strdup(current_subpath);
if (!path_so_far)
goto cleanup_from_error;
/* cleanup */
if (current_component != current_subpath)
free(current_subpath);
if (current_component != p_eff)
free(current_component);
current_component = current_subpath = NULL;
continue;
cleanup_from_error:
/* called if an error occured in the loop, so we
* do some additional cleanup here
*/
saved_errno = errno;
if (current_component != current_subpath)
free(current_subpath);
if (current_component != p_eff)
free(current_component);
free(current_entire_path);
errno = saved_errno;
goto out_initial_error;
}
/* we're done, now update the paths */
for (i = 0, info_ptr = base_info; info_ptr; info_ptr = info_ptr->next, i++) {
free(info_ptr->cgroup_path);
info_ptr->cgroup_path = new_cgroup_paths[i];
info_ptr->cgroup_path_sub = new_cgroup_paths_sub[i];
}
/* don't use lxc_free_array since we used the array members
* to store them in our result...
*/
free(new_cgroup_paths);
free(new_cgroup_paths_sub);
free(path_so_far);
lxc_free_array((void **)cgroup_path_components, free);
return base_info;
out_initial_error:
saved_errno = errno;
free(path_so_far);
lxc_cgroup_process_info_free_and_remove(base_info);
lxc_free_array((void **)new_cgroup_paths, free);
lxc_free_array((void **)new_cgroup_paths_sub, free);
lxc_free_array((void **)cgroup_path_components, free);
errno = saved_errno;
return NULL;
}
/* get the cgroup membership of a given container */
struct cgroup_process_info *lxc_cgroup_get_container_info(const char *name, const char *lxcpath, struct cgroup_meta_data *meta_data)
{
struct cgroup_process_info *result = NULL;
int saved_errno = 0;
size_t i;
struct cgroup_process_info **cptr = &result;
struct cgroup_process_info *entry = NULL;
char *path = NULL;
for (i = 0; i <= meta_data->maximum_hierarchy; i++) {
struct cgroup_hierarchy *h = meta_data->hierarchies[i];
if (!h || !h->used)
continue;
/* use the command interface to look for the cgroup */
path = lxc_cmd_get_cgroup_path(name, lxcpath, h->subsystems[0]);
if (!path)
goto out_error;
entry = calloc(1, sizeof(struct cgroup_process_info));
if (!entry)
goto out_error;
entry->meta_ref = lxc_cgroup_get_meta(meta_data);
entry->hierarchy = h;
entry->cgroup_path = path;
path = NULL;
/* it is not an error if we don't find anything here,
* it is up to the caller to decide what to do in that
* case */
entry->designated_mount_point = lxc_cgroup_find_mount_point(h, entry->cgroup_path, true);
*cptr = entry;
cptr = &entry->next;
entry = NULL;
}
return result;
out_error:
saved_errno = errno;
free(path);
lxc_cgroup_process_info_free(result);
lxc_cgroup_process_info_free(entry);
errno = saved_errno;
return NULL;
}
/* move a processs to the cgroups specified by the membership */
int lxc_cgroup_enter(struct cgroup_process_info *info, pid_t pid, bool enter_sub)
{
char pid_buf[32];
char *cgroup_tasks_fn;
int r;
struct cgroup_process_info *info_ptr;
snprintf(pid_buf, 32, "%lu", (unsigned long)pid);
for (info_ptr = info; info_ptr; info_ptr = info_ptr->next) {
char *cgroup_path = (enter_sub && info_ptr->cgroup_path_sub) ?
info_ptr->cgroup_path_sub :
info_ptr->cgroup_path;
if (!info_ptr->designated_mount_point) {
info_ptr->designated_mount_point = lxc_cgroup_find_mount_point(info_ptr->hierarchy, cgroup_path, true);
if (!info_ptr->designated_mount_point) {
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);
return -1;
}
}
cgroup_tasks_fn = cgroup_to_absolute_path(info_ptr->designated_mount_point, cgroup_path, "/tasks");
if (!cgroup_tasks_fn) {
SYSERROR("Could not add pid %lu to cgroup %s: internal error", (unsigned long)pid, cgroup_path);
return -1;
}
r = lxc_write_to_file(cgroup_tasks_fn, pid_buf, strlen(pid_buf), false);
if (r < 0) {
SYSERROR("Could not add pid %lu to cgroup %s: internal error", (unsigned long)pid, cgroup_path);
return -1;
}
}
return 0;
}
/* free process membership information */
void lxc_cgroup_process_info_free(struct cgroup_process_info *info)
{
struct cgroup_process_info *next;
if (!info)
return;
next = info->next;
lxc_cgroup_put_meta(info->meta_ref);
free(info->cgroup_path);
free(info->cgroup_path_sub);
lxc_free_array((void **)info->created_paths, free);
free(info);
lxc_cgroup_process_info_free(next);
}
/* free process membership information and remove cgroups that were created */
void lxc_cgroup_process_info_free_and_remove(struct cgroup_process_info *info)
{
struct cgroup_process_info *next;
char **pp;
if (!info)
return;
next = info->next;
for (pp = info->created_paths; pp && *pp; pp++);
for ((void)(pp && --pp); info->created_paths && pp >= info->created_paths; --pp) {
struct cgroup_mount_point *mp = info->designated_mount_point;
if (!mp)
mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, true);
if (mp)
/* ignore return value here, perhaps we created the
* '/lxc' cgroup in this container but another container
* is still running (for example)
*/
(void)remove_cgroup(mp, *pp);
free(*pp);
}
free(info->created_paths);
lxc_cgroup_put_meta(info->meta_ref);
free(info->cgroup_path);
free(info->cgroup_path_sub);
free(info);
lxc_cgroup_process_info_free_and_remove(next);
}
char *lxc_cgroup_get_hierarchy_path_handler(const char *subsystem, struct lxc_handler *handler)
{
struct cgroup_process_info *info = find_info_for_subsystem(handler->cgroup, subsystem);
if (!info)
return NULL;
return info->cgroup_path;
}
char *lxc_cgroup_get_hierarchy_path(const char *subsystem, const char *name, const char *lxcpath)
{
return lxc_cmd_get_cgroup_path(name, lxcpath, subsystem);
}
char *lxc_cgroup_get_hierarchy_abs_path_handler(const char *subsystem, struct lxc_handler *handler)
{
struct cgroup_mount_point *mp = NULL;
struct cgroup_process_info *info = find_info_for_subsystem(handler->cgroup, subsystem);
if (!info)
return NULL;
if (info->designated_mount_point) {
mp = info->designated_mount_point;
} else {
mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, true);
if (!mp)
return NULL;
}
return cgroup_to_absolute_path(mp, info->cgroup_path, NULL);
}
char *lxc_cgroup_get_hierarchy_abs_path(const char *subsystem, const char *name, const char *lxcpath)
{
struct cgroup_meta_data *meta;
struct cgroup_process_info *base_info, *info;
struct cgroup_mount_point *mp;
char *result = NULL;
int saved_errno;
meta = lxc_cgroup_load_meta();
if (!meta)
return NULL;
base_info = lxc_cgroup_get_container_info(name, lxcpath, meta);
if (!base_info)
return NULL;
info = find_info_for_subsystem(base_info, subsystem);
if (!info)
return NULL;
if (info->designated_mount_point) {
mp = info->designated_mount_point;
} else {
mp = lxc_cgroup_find_mount_point(info->hierarchy, info->cgroup_path, true);
if (!mp)
return NULL;
}
result = cgroup_to_absolute_path(mp, info->cgroup_path, NULL);
saved_errno = errno;
lxc_cgroup_process_info_free(base_info);
lxc_cgroup_put_meta(meta);
errno = saved_errno;
return result;
}
int lxc_cgroup_set_handler(const char *filename, const char *value, struct lxc_handler *handler)
{
char *subsystem = NULL, *p, *path;
int ret = -1;
subsystem = alloca(strlen(filename) + 1);
strcpy(subsystem, filename);
if ((p = index(subsystem, '.')) != NULL)
*p = '\0';
path = lxc_cgroup_get_hierarchy_abs_path_handler(subsystem, handler);
if (path) {
ret = do_cgroup_set(path, filename, value);
free(path);
}
return ret;
}
int lxc_cgroup_get_handler(const char *filename, char *value, size_t len, struct lxc_handler *handler)
{
char *subsystem = NULL, *p, *path;
int ret = -1;
subsystem = alloca(strlen(filename) + 1);
strcpy(subsystem, filename);
if ((p = index(subsystem, '.')) != NULL)
*p = '\0';
path = lxc_cgroup_get_hierarchy_abs_path_handler(subsystem, handler);
if (path) {