-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
cgfs.c
2360 lines (2073 loc) · 62.5 KB
/
cgfs.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
*/
#include "config.h"
#include <stdio.h>
#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 <sys/mount.h>
#include <netinet/in.h>
#include <net/if.h>
#include "error.h"
#include "commands.h"
#include "list.h"
#include "conf.h"
#include "utils.h"
#include "bdev.h"
#include "log.h"
#include "cgroup.h"
#include "start.h"
#include "state.h"
#if IS_BIONIC
#include <../include/lxcmntent.h>
#else
#include <mntent.h>
#endif
struct cgroup_hierarchy;
struct cgroup_meta_data;
struct cgroup_mount_point;
/*
* cgroup_meta_data: the metadata about the cgroup infrastructure on this
* host
*/
struct cgroup_meta_data {
ptrdiff_t ref; /* simple refcount */
struct cgroup_hierarchy **hierarchies;
struct cgroup_mount_point **mount_points;
int maximum_hierarchy;
};
/*
* cgroup_hierarchy: describes a single cgroup hierarchy
* (may have multiple mount points)
*/
struct cgroup_hierarchy {
int index;
bool used; /* false if the hierarchy should be ignored by lxc */
char **subsystems;
struct cgroup_mount_point *rw_absolute_mount_point;
struct cgroup_mount_point *ro_absolute_mount_point;
struct cgroup_mount_point **all_mount_points;
size_t all_mount_point_capacity;
};
/*
* cgroup_mount_point: a mount point to where a hierarchy
* is mounted to
*/
struct cgroup_mount_point {
struct cgroup_hierarchy *hierarchy;
char *mount_point;
char *mount_prefix;
bool read_only;
bool need_cpuset_init;
};
/*
* cgroup_process_info: describes the membership of a
* process to the different cgroup
* hierarchies
*
* Note this is the per-process info tracked by the cgfs_ops.
* This is not used with cgmanager.
*/
struct cgroup_process_info {
struct cgroup_process_info *next;
struct cgroup_meta_data *meta_ref;
struct cgroup_hierarchy *hierarchy;
char *cgroup_path;
char *cgroup_path_sub;
char **created_paths;
size_t created_paths_capacity;
size_t created_paths_count;
struct cgroup_mount_point *designated_mount_point;
};
struct cgfs_data {
char *name;
const char *cgroup_pattern;
struct cgroup_meta_data *meta;
struct cgroup_process_info *info;
};
lxc_log_define(lxc_cgfs, 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_cgroup(struct cgroup_mount_point *mp, const char *path);
static int remove_cgroup(struct cgroup_mount_point *mp, const char *path, bool recurse);
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 cgfs_data *d, char *v, bool for_allow);
static int do_setup_cgroup_limits(struct cgfs_data *d, 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_cgroup_settings(struct cgroup_mount_point *mp, char *cgroup_path);
static bool init_cpuset_if_needed(struct cgroup_mount_point *mp, const char *path);
static struct cgroup_meta_data *lxc_cgroup_load_meta2(const char **subsystem_whitelist);
static struct cgroup_meta_data *lxc_cgroup_get_meta(struct cgroup_meta_data *meta_data);
static struct cgroup_meta_data *lxc_cgroup_put_meta(struct cgroup_meta_data *meta_data);
/* free process membership information */
static void lxc_cgroup_process_info_free(struct cgroup_process_info *info);
static void lxc_cgroup_process_info_free_and_remove(struct cgroup_process_info *info);
static struct cgroup_ops cgfs_ops;
static int cgroup_rmdir(char *dirname)
{
struct dirent dirent, *direntp;
int saved_errno = 0;
DIR *dir;
int ret, failed=0;
char pathname[MAXPATHLEN];
dir = opendir(dirname);
if (!dir) {
ERROR("%s: failed to open %s", __func__, dirname);
return -1;
}
while (!readdir_r(dir, &dirent, &direntp)) {
struct stat mystat;
int rc;
if (!direntp)
break;
if (!strcmp(direntp->d_name, ".") ||
!strcmp(direntp->d_name, ".."))
continue;
rc = snprintf(pathname, MAXPATHLEN, "%s/%s", dirname, direntp->d_name);
if (rc < 0 || rc >= MAXPATHLEN) {
ERROR("pathname too long");
failed=1;
if (!saved_errno)
saved_errno = -ENOMEM;
continue;
}
ret = lstat(pathname, &mystat);
if (ret) {
SYSERROR("%s: failed to stat %s", __func__, pathname);
failed=1;
if (!saved_errno)
saved_errno = errno;
continue;
}
if (S_ISDIR(mystat.st_mode)) {
if (cgroup_rmdir(pathname) < 0) {
if (!saved_errno)
saved_errno = errno;
failed=1;
}
}
}
if (rmdir(dirname) < 0) {
SYSERROR("%s: failed to delete %s", __func__, dirname);
if (!saved_errno)
saved_errno = errno;
failed=1;
}
ret = closedir(dir);
if (ret) {
SYSERROR("%s: failed to close directory %s", __func__, dirname);
if (!saved_errno)
saved_errno = errno;
failed=1;
}
errno = saved_errno;
return failed ? -1 : 0;
}
static 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("lxc.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;
}
/* Step 1: determine all kernel subsystems */
static bool find_cgroup_subsystems(char ***kernel_subsystems)
{
FILE *proc_cgroups;
bool bret = false;
char *line = NULL;
size_t sz = 0;
size_t kernel_subsystems_count = 0;
size_t kernel_subsystems_capacity = 0;
int r;
proc_cgroups = fopen_cloexec("/proc/cgroups", "r");
if (!proc_cgroups)
return false;
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;
(*kernel_subsystems)[kernel_subsystems_count] = strdup(line);
if (!(*kernel_subsystems)[kernel_subsystems_count])
goto out;
kernel_subsystems_count++;
}
bret = true;
out:
fclose(proc_cgroups);
free(line);
return bret;
}
/* 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
*/
static bool find_cgroup_hierarchies(struct cgroup_meta_data *meta_data,
bool all_kernel_subsystems, bool all_named_subsystems,
const char **subsystem_whitelist)
{
FILE *proc_self_cgroup;
char *line = NULL;
size_t sz = 0;
int r;
bool bret = false;
size_t hierarchy_capacity = 0;
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)
return false;
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;
meta_data->maximum_hierarchy = hierarchy_number;
}
/* this shouldn't happen, we had this already */
if (meta_data->hierarchies[hierarchy_number])
goto out;
h = calloc(1, sizeof(struct cgroup_hierarchy));
if (!h)
goto out;
meta_data->hierarchies[hierarchy_number] = h;
h->index = hierarchy_number;
h->subsystems = lxc_string_split_and_trim(colon1, ',');
if (!h->subsystems)
goto out;
/* 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;
}
}
bret = true;
out:
fclose(proc_self_cgroup);
free(line);
return bret;
}
/* Step 3: determine all mount points of each hierarchy */
static bool find_hierarchy_mountpts( struct cgroup_meta_data *meta_data, char **kernel_subsystems)
{
bool bret = false;
FILE *proc_self_mountinfo;
char *line = NULL;
size_t sz = 0;
char **tokens = NULL;
size_t mount_point_count = 0;
size_t mount_point_capacity = 0;
size_t token_capacity = 0;
int r;
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)
return false;
while (getline(&line, &sz, proc_self_mountinfo) != -1) {
char *token, *line_tok, *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, line_tok = line; (token = strtok_r(line_tok, " ", &saveptr)); line_tok = NULL) {
r = lxc_grow_array((void ***)&tokens, &token_capacity, i + 1, 64);
if (r < 0)
goto out;
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;
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;
/* create mount point object */
mount_point = calloc(1, sizeof(*mount_point));
if (!mount_point)
goto out;
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;
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;
h->all_mount_points[k] = mount_point;
}
bret = true;
out:
fclose(proc_self_mountinfo);
free(tokens);
free(line);
return bret;
}
static struct cgroup_meta_data *lxc_cgroup_load_meta2(const char **subsystem_whitelist)
{
bool all_kernel_subsystems = true;
bool all_named_subsystems = false;
struct cgroup_meta_data *meta_data = NULL;
char **kernel_subsystems = NULL;
int 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;
if (!find_cgroup_subsystems(&kernel_subsystems))
goto out_error;
if (!find_cgroup_hierarchies(meta_data, all_kernel_subsystems,
all_named_subsystems, subsystem_whitelist))
goto out_error;
if (!find_hierarchy_mountpts(meta_data, kernel_subsystems))
goto out_error;
/* oops, we couldn't find anything */
if (!meta_data->hierarchies || !meta_data->mount_points) {
errno = EINVAL;
goto out_error;
}
lxc_free_array((void **)kernel_subsystems, free);
return meta_data;
out_error:
saved_errno = errno;
lxc_free_array((void **)kernel_subsystems, free);
lxc_cgroup_put_meta(meta_data);
errno = saved_errno;
return NULL;
}
static struct cgroup_meta_data *lxc_cgroup_get_meta(struct cgroup_meta_data *meta_data)
{
meta_data->ref++;
return meta_data;
}
static 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);
free(meta_data);
return NULL;
}
static 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;
}
static 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;
}
static 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;
}
static 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);
}
static struct cgroup_process_info *lxc_cgroup_process_info_get_init(struct cgroup_meta_data *meta)
{
return lxc_cgroup_process_info_get(1, meta);
}
static 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;
}
/*
* If a controller has ns cgroup mounted, then in that cgroup the handler->pid
* is already in a new cgroup named after the pid. 'mnt' is passed in as
* the full current cgroup. Say that is /sys/fs/cgroup/lxc/2975 and the container
* name is c1. . We want to rename the cgroup directory to /sys/fs/cgroup/lxc/c1,
* and return the string /sys/fs/cgroup/lxc/c1.
*/
static char *cgroup_rename_nsgroup(const char *mountpath, const char *oldname, pid_t pid, const char *name)
{
char *dir, *fulloldpath;
char *newname, *fullnewpath;
int len, newlen, ret;
/*
* if cgroup is mounted at /cgroup and task is in cgroup /ab/, pid 2375 and
* name is c1,
* dir: /ab
* fulloldpath = /cgroup/ab/2375
* fullnewpath = /cgroup/ab/c1
* newname = /ab/c1
*/
dir = alloca(strlen(oldname) + 1);
strcpy(dir, oldname);
len = strlen(oldname) + strlen(mountpath) + 22;
fulloldpath = alloca(len);
ret = snprintf(fulloldpath, len, "%s/%s/%ld", mountpath, oldname, (unsigned long)pid);
if (ret < 0 || ret >= len)
return NULL;
len = strlen(dir) + strlen(name) + 2;
newname = malloc(len);
if (!newname) {
SYSERROR("Out of memory");
return NULL;
}
ret = snprintf(newname, len, "%s/%s", dir, name);
if (ret < 0 || ret >= len) {
free(newname);
return NULL;
}
newlen = strlen(mountpath) + len + 2;
fullnewpath = alloca(newlen);
ret = snprintf(fullnewpath, newlen, "%s/%s", mountpath, newname);
if (ret < 0 || ret >= newlen) {
free(newname);
return NULL;
}
if (access(fullnewpath, F_OK) == 0) {
if (rmdir(fullnewpath) != 0) {
SYSERROR("container cgroup %s already exists.", fullnewpath);
free(newname);
return NULL;
}
}
if (rename(fulloldpath, fullnewpath)) {
SYSERROR("failed to rename cgroup %s->%s", fulloldpath, fullnewpath);
free(newname);
return NULL;
}
DEBUG("'%s' renamed to '%s'", oldname, newname);
return newname;
}
/* create a new cgroup */
static struct cgroup_process_info *lxc_cgroupfs_create(const char *name, const char *path_pattern, struct cgroup_meta_data *meta_data, const char *sub_pattern)
{
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 (lxc_string_in_array("ns", (const char **)h->subsystems))
continue;
if (handle_cgroup_settings(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], false);
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];
if (lxc_string_in_array("ns", (const char **)info_ptr->hierarchy->subsystems))
continue;
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;
if (!init_cpuset_if_needed(info_ptr->designated_mount_point, current_entire_path)) {
ERROR("Failed to initialize cpuset in new '%s'.", current_entire_path);
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_cgroup_settings(info_ptr->designated_mount_point, info_ptr->cgroup_path) < 0) {
ERROR("Could not set clone_children to 1 for cpuset hierarchy in pre-existing cgroup.");
goto cleanup_from_error;
}
if (!init_cpuset_if_needed(info_ptr->designated_mount_point, info_ptr->cgroup_path)) {
ERROR("Failed to initialize cpuset in pre-existing '%s'.", info_ptr->cgroup_path);
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);