/
cgfsng.c
2016 lines (1720 loc) · 46.4 KB
/
cgfsng.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
*
* Copyright © 2016 Canonical Ltd.
*
* Authors:
* Serge Hallyn <serge.hallyn@ubuntu.com>
*
* 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
*/
/*
* cgfs-ng.c: this is a new, simplified implementation of a filesystem
* cgroup backend. The original cgfs.c was designed to be as flexible
* as possible. It would try to find cgroup filesystems no matter where
* or how you had them mounted, and deduce the most usable mount for
* each controller. It also was not designed for unprivileged use, as
* that was reserved for cgmanager.
*
* This new implementation assumes that cgroup filesystems are mounted
* under /sys/fs/cgroup/clist where clist is either the controller, or
* a comman-separated list of controllers.
*/
#include "config.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <grp.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include "bdev.h"
#include "cgroup.h"
#include "commands.h"
#include "log.h"
#include "utils.h"
lxc_log_define(lxc_cgfsng, lxc);
static struct cgroup_ops cgfsng_ops;
/*
* A descriptor for a mounted hierarchy
* @controllers: either NULL, or a null-terminated list of all
* the co-mounted controllers
* @mountpoint: the mountpoint we will use. It will be either
* /sys/fs/cgroup/controller or /sys/fs/cgroup/controllerlist
* @base_cgroup: the cgroup under which the container cgroup path
is created. This will be either the caller's cgroup (if not
root), or init's cgroup (if root).
*/
struct hierarchy {
char **controllers;
char *mountpoint;
char *base_cgroup;
char *fullcgpath;
};
/*
* The cgroup data which is attached to the lxc_handler.
* @cgroup_pattern - a copy of the lxc.cgroup.pattern
* @container_cgroup - if not null, the cgroup which was created for
* the container. For each hierarchy, it is created under the
* @hierarchy->base_cgroup directory. Relative to the base_cgroup
* it is the same for all hierarchies.
* @name - the container name
*/
struct cgfsng_handler_data {
char *cgroup_pattern;
char *container_cgroup; // cgroup we created for the container
char *name; // container name
};
/*
* @hierarchies - a NULL-terminated array of struct hierarchy, one per
* hierarchy. No duplicates. First sufficient, writeable mounted
* hierarchy wins
*/
struct hierarchy **hierarchies;
/*
* @cgroup_use - a copy of the lxc.cgroup.use
*/
char *cgroup_use;
/*
* @lxc_cgfsng_debug - whether to print debug info to stdout for the cgfsng
* driver
*/
static bool lxc_cgfsng_debug;
static void free_string_list(char **clist)
{
if (clist) {
int i;
for (i = 0; clist[i]; i++)
free(clist[i]);
free(clist);
}
}
/* Re-alllocate a pointer, do not fail */
static void *must_realloc(void *orig, size_t sz)
{
void *ret;
do {
ret = realloc(orig, sz);
} while (!ret);
return ret;
}
/* Allocate a pointer, do not fail */
static void *must_alloc(size_t sz)
{
return must_realloc(NULL, sz);
}
/* return copy of string @entry; do not fail. */
static char *must_copy_string(const char *entry)
{
char *ret;
if (!entry)
return NULL;
do {
ret = strdup(entry);
} while (!ret);
return ret;
}
/*
* This is a special case - return a copy of @entry
* prepending 'name='. I.e. turn systemd into name=systemd.
* Do not fail.
*/
static char *must_prefix_named(char *entry)
{
char *ret;
size_t len = strlen(entry);
ret = must_alloc(len + 6);
snprintf(ret, len + 6, "name=%s", entry);
return ret;
}
/*
* Given a pointer to a null-terminated array of pointers, realloc to
* add one entry, and point the new entry to NULL. Do not fail. Return
* the index to the second-to-last entry - that is, the one which is
* now available for use (keeping the list null-terminated).
*/
static int append_null_to_list(void ***list)
{
int newentry = 0;
if (*list)
for (; (*list)[newentry]; newentry++);
*list = must_realloc(*list, (newentry + 2) * sizeof(void **));
(*list)[newentry + 1] = NULL;
return newentry;
}
/*
* Given a null-terminated array of strings, check whether @entry
* is one of the strings
*/
static bool string_in_list(char **list, const char *entry)
{
int i;
if (!list)
return false;
for (i = 0; list[i]; i++)
if (strcmp(list[i], entry) == 0)
return true;
return false;
}
/*
* append an entry to the clist. Do not fail.
* *clist must be NULL the first time we are called.
*
* We also handle named subsystems here. Any controller which is not a
* kernel subsystem, we prefix 'name='. Any which is both a kernel and
* named subsystem, we refuse to use because we're not sure which we
* have here. (TODO - we could work around this in some cases by just
* remounting to be unambiguous, or by comparing mountpoint contents
* with current cgroup)
*
* The last entry will always be NULL.
*/
static void must_append_controller(char **klist, char **nlist, char ***clist, char *entry)
{
int newentry;
char *copy;
if (string_in_list(klist, entry) && string_in_list(nlist, entry)) {
ERROR("Refusing to use ambiguous controller '%s'", entry);
ERROR("It is both a named and kernel subsystem");
return;
}
newentry = append_null_to_list((void ***)clist);
if (strncmp(entry, "name=", 5) == 0)
copy = must_copy_string(entry);
else if (string_in_list(klist, entry))
copy = must_copy_string(entry);
else
copy = must_prefix_named(entry);
(*clist)[newentry] = copy;
}
static void free_handler_data(struct cgfsng_handler_data *d)
{
free(d->cgroup_pattern);
free(d->container_cgroup);
free(d->name);
free(d);
}
/*
* Given a handler's cgroup data, return the struct hierarchy for the
* controller @c, or NULL if there is none.
*/
struct hierarchy *get_hierarchy(const char *c)
{
int i;
if (!hierarchies)
return NULL;
for (i = 0; hierarchies[i]; i++) {
if (string_in_list(hierarchies[i]->controllers, c))
return hierarchies[i];
}
return NULL;
}
static char *must_make_path(const char *first, ...) __attribute__((sentinel));
#define BATCH_SIZE 50
static void batch_realloc(char **mem, size_t oldlen, size_t newlen)
{
int newbatches = (newlen / BATCH_SIZE) + 1;
int oldbatches = (oldlen / BATCH_SIZE) + 1;
if (!*mem || newbatches > oldbatches) {
*mem = must_realloc(*mem, newbatches * BATCH_SIZE);
}
}
static void append_line(char **dest, size_t oldlen, char *new, size_t newlen)
{
size_t full = oldlen + newlen;
batch_realloc(dest, oldlen, full + 1);
memcpy(*dest + oldlen, new, newlen + 1);
}
/* Slurp in a whole file */
static char *read_file(char *fnam)
{
FILE *f;
char *line = NULL, *buf = NULL;
size_t len = 0, fulllen = 0;
int linelen;
f = fopen(fnam, "r");
if (!f)
return NULL;
while ((linelen = getline(&line, &len, f)) != -1) {
append_line(&buf, fulllen, line, linelen);
fulllen += linelen;
}
fclose(f);
free(line);
return buf;
}
/* Taken over modified from the kernel sources. */
#define NBITS 32 /* bits in uint32_t */
#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, NBITS)
static void set_bit(unsigned bit, uint32_t *bitarr)
{
bitarr[bit / NBITS] |= (1 << (bit % NBITS));
}
static void clear_bit(unsigned bit, uint32_t *bitarr)
{
bitarr[bit / NBITS] &= ~(1 << (bit % NBITS));
}
static bool is_set(unsigned bit, uint32_t *bitarr)
{
return (bitarr[bit / NBITS] & (1 << (bit % NBITS))) != 0;
}
/* Create cpumask from cpulist aka turn:
*
* 0,2-3
*
* into bit array
*
* 1 0 1 1
*/
static uint32_t *lxc_cpumask(char *buf, size_t nbits)
{
char *token;
char *saveptr = NULL;
size_t arrlen = BITS_TO_LONGS(nbits);
uint32_t *bitarr = calloc(arrlen, sizeof(uint32_t));
if (!bitarr)
return NULL;
for (; (token = strtok_r(buf, ",", &saveptr)); buf = NULL) {
errno = 0;
unsigned start = strtoul(token, NULL, 0);
unsigned end = start;
char *range = strchr(token, '-');
if (range)
end = strtoul(range + 1, NULL, 0);
if (!(start <= end)) {
free(bitarr);
return NULL;
}
if (end >= nbits) {
free(bitarr);
return NULL;
}
while (start <= end)
set_bit(start++, bitarr);
}
return bitarr;
}
/* Turn cpumask into simple, comma-separated cpulist. */
static char *lxc_cpumask_to_cpulist(uint32_t *bitarr, size_t nbits)
{
size_t i;
int ret;
char numstr[LXC_NUMSTRLEN64] = {0};
char **cpulist = NULL;
for (i = 0; i <= nbits; i++) {
if (is_set(i, bitarr)) {
ret = snprintf(numstr, LXC_NUMSTRLEN64, "%zu", i);
if (ret < 0 || (size_t)ret >= LXC_NUMSTRLEN64) {
lxc_free_array((void **)cpulist, free);
return NULL;
}
if (lxc_append_string(&cpulist, numstr) < 0) {
lxc_free_array((void **)cpulist, free);
return NULL;
}
}
}
return lxc_string_join(",", (const char **)cpulist, false);
}
static ssize_t get_max_cpus(char *cpulist)
{
char *c1, *c2;
char *maxcpus = cpulist;
size_t cpus = 0;
c1 = strrchr(maxcpus, ',');
if (c1)
c1++;
c2 = strrchr(maxcpus, '-');
if (c2)
c2++;
if (!c1 && !c2)
c1 = maxcpus;
else if (c1 > c2)
c2 = c1;
else if (c1 < c2)
c1 = c2;
else if (!c1 && c2) // The reverse case is obvs. not needed.
c1 = c2;
/* If the above logic is correct, c1 should always hold a valid string
* here.
*/
errno = 0;
cpus = strtoul(c1, NULL, 0);
if (errno != 0)
return -1;
return cpus;
}
#define __ISOL_CPUS "/sys/devices/system/cpu/isolated"
static bool filter_and_set_cpus(char *path, bool am_initialized)
{
char *lastslash, *fpath, oldv;
int ret;
ssize_t i;
ssize_t maxposs = 0, maxisol = 0;
char *cpulist = NULL, *posscpus = NULL, *isolcpus = NULL;
uint32_t *possmask = NULL, *isolmask = NULL;
bool bret = false, flipped_bit = false;
lastslash = strrchr(path, '/');
if (!lastslash) { // bug... this shouldn't be possible
ERROR("Invalid path: %s.", path);
return bret;
}
oldv = *lastslash;
*lastslash = '\0';
fpath = must_make_path(path, "cpuset.cpus", NULL);
posscpus = read_file(fpath);
if (!posscpus) {
SYSERROR("Could not read file: %s.\n", fpath);
goto on_error;
}
/* Get maximum number of cpus found in possible cpuset. */
maxposs = get_max_cpus(posscpus);
if (maxposs < 0)
goto on_error;
if (!file_exists(__ISOL_CPUS)) {
/* This system doesn't expose isolated cpus. */
DEBUG("Path: "__ISOL_CPUS" to read isolated cpus from does not exist.\n");
cpulist = posscpus;
/* No isolated cpus but we weren't already initialized by
* someone. We should simply copy the parents cpuset.cpus
* values.
*/
if (!am_initialized) {
DEBUG("Copying cpuset of parent cgroup.");
goto copy_parent;
}
/* No isolated cpus but we were already initialized by someone.
* Nothing more to do for us.
*/
goto on_success;
}
isolcpus = read_file(__ISOL_CPUS);
if (!isolcpus) {
SYSERROR("Could not read file "__ISOL_CPUS);
goto on_error;
}
if (!isdigit(isolcpus[0])) {
DEBUG("No isolated cpus detected.");
cpulist = posscpus;
/* No isolated cpus but we weren't already initialized by
* someone. We should simply copy the parents cpuset.cpus
* values.
*/
if (!am_initialized) {
DEBUG("Copying cpuset of parent cgroup.");
goto copy_parent;
}
/* No isolated cpus but we were already initialized by someone.
* Nothing more to do for us.
*/
goto on_success;
}
/* Get maximum number of cpus found in isolated cpuset. */
maxisol = get_max_cpus(isolcpus);
if (maxisol < 0)
goto on_error;
if (maxposs < maxisol)
maxposs = maxisol;
maxposs++;
possmask = lxc_cpumask(posscpus, maxposs);
if (!possmask) {
ERROR("Could not create cpumask for all possible cpus.\n");
goto on_error;
}
isolmask = lxc_cpumask(isolcpus, maxposs);
if (!isolmask) {
ERROR("Could not create cpumask for all isolated cpus.\n");
goto on_error;
}
for (i = 0; i <= maxposs; i++) {
if (is_set(i, isolmask) && is_set(i, possmask)) {
flipped_bit = true;
clear_bit(i, possmask);
}
}
if (!flipped_bit) {
DEBUG("No isolated cpus present in cpuset.");
goto on_success;
}
DEBUG("Removed isolated cpus from cpuset.");
cpulist = lxc_cpumask_to_cpulist(possmask, maxposs);
if (!cpulist) {
ERROR("Could not create cpu list.\n");
goto on_error;
}
copy_parent:
*lastslash = oldv;
fpath = must_make_path(path, "cpuset.cpus", NULL);
ret = lxc_write_to_file(fpath, cpulist, strlen(cpulist), false);
if (ret < 0) {
SYSERROR("Could not write cpu list to: %s.\n", fpath);
goto on_error;
}
on_success:
bret = true;
on_error:
free(fpath);
free(isolcpus);
free(isolmask);
if (posscpus != cpulist)
free(posscpus);
free(possmask);
free(cpulist);
return bret;
}
/* Copy contents of parent(@path)/@file to @path/@file */
static bool copy_parent_file(char *path, char *file)
{
char *lastslash, *value = NULL, *fpath, oldv;
int len = 0;
int ret;
lastslash = strrchr(path, '/');
if (!lastslash) { // bug... this shouldn't be possible
ERROR("cgfsng:copy_parent_file: bad path %s", path);
return false;
}
oldv = *lastslash;
*lastslash = '\0';
fpath = must_make_path(path, file, NULL);
len = lxc_read_from_file(fpath, NULL, 0);
if (len <= 0)
goto bad;
value = must_alloc(len + 1);
if (lxc_read_from_file(fpath, value, len) != len)
goto bad;
free(fpath);
*lastslash = oldv;
fpath = must_make_path(path, file, NULL);
ret = lxc_write_to_file(fpath, value, len, false);
if (ret < 0)
SYSERROR("Unable to write %s to %s", value, fpath);
free(fpath);
free(value);
return ret >= 0;
bad:
SYSERROR("Error reading '%s'", fpath);
free(fpath);
free(value);
return false;
}
/*
* Initialize the cpuset hierarchy in first directory of @gname and
* set cgroup.clone_children so that children inherit settings.
* Since the h->base_path is populated by init or ourselves, we know
* it is already initialized.
*/
static bool handle_cpuset_hierarchy(struct hierarchy *h, char *cgname)
{
char *cgpath, *clonechildrenpath, v, *slash;
if (!string_in_list(h->controllers, "cpuset"))
return true;
if (*cgname == '/')
cgname++;
slash = strchr(cgname, '/');
if (slash)
*slash = '\0';
cgpath = must_make_path(h->mountpoint, h->base_cgroup, cgname, NULL);
if (slash)
*slash = '/';
if (mkdir(cgpath, 0755) < 0 && errno != EEXIST) {
SYSERROR("Failed to create '%s'", cgpath);
free(cgpath);
return false;
}
clonechildrenpath = must_make_path(cgpath, "cgroup.clone_children", NULL);
if (!file_exists(clonechildrenpath)) { /* unified hierarchy doesn't have clone_children */
free(clonechildrenpath);
free(cgpath);
return true;
}
if (lxc_read_from_file(clonechildrenpath, &v, 1) < 0) {
SYSERROR("Failed to read '%s'", clonechildrenpath);
free(clonechildrenpath);
free(cgpath);
return false;
}
/* Make sure any isolated cpus are removed from cpuset.cpus. */
if (!filter_and_set_cpus(cgpath, v == '1')) {
SYSERROR("Failed to remove isolated cpus.");
free(clonechildrenpath);
free(cgpath);
return false;
}
if (v == '1') { /* already set for us by someone else */
DEBUG("\"cgroup.clone_children\" was already set to \"1\".");
free(clonechildrenpath);
free(cgpath);
return true;
}
/* copy parent's settings */
if (!copy_parent_file(cgpath, "cpuset.mems")) {
SYSERROR("Failed to copy \"cpuset.mems\" settings.");
free(cgpath);
free(clonechildrenpath);
return false;
}
free(cgpath);
if (lxc_write_to_file(clonechildrenpath, "1", 1, false) < 0) {
/* Set clone_children so children inherit our settings */
SYSERROR("Failed to write 1 to %s", clonechildrenpath);
free(clonechildrenpath);
return false;
}
free(clonechildrenpath);
return true;
}
/*
* Given two null-terminated lists of strings, return true if any string
* is in both.
*/
static bool controller_lists_intersect(char **l1, char **l2)
{
int i;
if (!l1 || !l2)
return false;
for (i = 0; l1[i]; i++) {
if (string_in_list(l2, l1[i]))
return true;
}
return false;
}
/*
* For a null-terminated list of controllers @clist, return true if any of
* those controllers is already listed the null-terminated list of
* hierarchies @hlist. Realistically, if one is present, all must be present.
*/
static bool controller_list_is_dup(struct hierarchy **hlist, char **clist)
{
int i;
if (!hlist)
return false;
for (i = 0; hlist[i]; i++)
if (controller_lists_intersect(hlist[i]->controllers, clist))
return true;
return false;
}
/*
* Return true if the controller @entry is found in the null-terminated
* list of hierarchies @hlist
*/
static bool controller_found(struct hierarchy **hlist, char *entry)
{
int i;
if (!hlist)
return false;
for (i = 0; hlist[i]; i++)
if (string_in_list(hlist[i]->controllers, entry))
return true;
return false;
}
/*
* Return true if all of the controllers which we require have been found.
* The required list is freezer and anything in * lxc.cgroup.use.
*/
static bool all_controllers_found(void)
{
char *p, *saveptr = NULL;
struct hierarchy ** hlist = hierarchies;
if (!controller_found(hlist, "freezer")) {
ERROR("no freezer controller mountpoint found");
return false;
}
if (!cgroup_use)
return true;
for (p = strtok_r(cgroup_use, ",", &saveptr); p;
p = strtok_r(NULL, ",", &saveptr)) {
if (!controller_found(hlist, p)) {
ERROR("no %s controller mountpoint found", p);
return false;
}
}
return true;
}
/* Return true if the fs type is fuse.lxcfs */
static bool is_lxcfs(const char *line)
{
char *p = strstr(line, " - ");
if (!p)
return false;
return strncmp(p, " - fuse.lxcfs ", 14) == 0;
}
/*
* Get the controllers from a mountinfo line
* There are other ways we could get this info. For lxcfs, field 3
* is /cgroup/controller-list. For cgroupfs, we could parse the mount
* options. But we simply assume that the mountpoint must be
* /sys/fs/cgroup/controller-list
*/
static char **get_controllers(char **klist, char **nlist, char *line)
{
// the fourth field is /sys/fs/cgroup/comma-delimited-controller-list
int i;
char *p = line, *p2, *tok, *saveptr = NULL;
char **aret = NULL;
for (i = 0; i < 4; i++) {
p = strchr(p, ' ');
if (!p)
return NULL;
p++;
}
if (!p)
return NULL;
/* note - if we change how mountinfo works, then our caller
* will need to verify /sys/fs/cgroup/ in this field */
if (strncmp(p, "/sys/fs/cgroup/", 15) != 0) {
INFO("cgfsng: found hierarchy not under /sys/fs/cgroup: \"%s\"", p);
return NULL;
}
p += 15;
p2 = strchr(p, ' ');
if (!p2) {
ERROR("corrupt mountinfo");
return NULL;
}
*p2 = '\0';
for (tok = strtok_r(p, ",", &saveptr); tok;
tok = strtok_r(NULL, ",", &saveptr)) {
must_append_controller(klist, nlist, &aret, tok);
}
return aret;
}
/* return true if the fstype is cgroup */
static bool is_cgroupfs(char *line)
{
char *p = strstr(line, " - ");
if (!p)
return false;
return strncmp(p, " - cgroup ", 10) == 0;
}
/* Add a controller to our list of hierarchies */
static void add_controller(char **clist, char *mountpoint, char *base_cgroup)
{
struct hierarchy *new;
int newentry;
new = must_alloc(sizeof(*new));
new->controllers = clist;
new->mountpoint = mountpoint;
new->base_cgroup = base_cgroup;
new->fullcgpath = NULL;
newentry = append_null_to_list((void ***)&hierarchies);
hierarchies[newentry] = new;
}
/*
* Get a copy of the mountpoint from @line, which is a line from
* /proc/self/mountinfo
*/
static char *get_mountpoint(char *line)
{
int i;
char *p = line, *sret;
size_t len;
for (i = 0; i < 4; i++) {
p = strchr(p, ' ');
if (!p)
return NULL;
p++;
}
/* we've already stuck a \0 after the mountpoint */
len = strlen(p);
sret = must_alloc(len + 1);
memcpy(sret, p, len);
sret[len] = '\0';
return sret;
}
/*
* Given a multi-line string, return a null-terminated copy of the
* current line.
*/
static char *copy_to_eol(char *p)
{
char *p2 = strchr(p, '\n'), *sret;
size_t len;
if (!p2)
return NULL;
len = p2 - p;
sret = must_alloc(len + 1);
memcpy(sret, p, len);
sret[len] = '\0';
return sret;
}
/*
* cgline: pointer to character after the first ':' in a line in a
* \n-terminated /proc/self/cgroup file. Check whether * controller c is
* present.
*/
static bool controller_in_clist(char *cgline, char *c)
{
char *tok, *saveptr = NULL, *eol, *tmp;
size_t len;
eol = strchr(cgline, ':');
if (!eol)
return false;
len = eol - cgline;
tmp = alloca(len + 1);
memcpy(tmp, cgline, len);
tmp[len] = '\0';
for (tok = strtok_r(tmp, ",", &saveptr); tok;
tok = strtok_r(NULL, ",", &saveptr)) {
if (strcmp(tok, c) == 0)
return true;
}
return false;
}
/*
* @basecginfo is a copy of /proc/$$/cgroup. Return the current
* cgroup for @controller
*/
static char *get_current_cgroup(char *basecginfo, char *controller)
{
char *p = basecginfo;
while (1) {
p = strchr(p, ':');
if (!p)
return NULL;
p++;
if (controller_in_clist(p, controller)) {
p = strchr(p, ':');
if (!p)
return NULL;
p++;
return copy_to_eol(p);
}
p = strchr(p, '\n');
if (!p)
return NULL;
p++;
}
}
/*
* Given a hierarchy @mountpoint and base @path, verify that we can create
* directories underneath it.
*/
static bool test_writeable(char *mountpoint, char *path)
{
char *fullpath = must_make_path(mountpoint, path, NULL);
int ret;
ret = access(fullpath, W_OK);
free(fullpath);
return ret == 0;
}
static void must_append_string(char ***list, char *entry)
{
int newentry = append_null_to_list((void ***)list);
char *copy;
copy = must_copy_string(entry);
(*list)[newentry] = copy;
}
static void get_existing_subsystems(char ***klist, char ***nlist)
{
FILE *f;
char *line = NULL;
size_t len = 0;
if ((f = fopen("/proc/self/cgroup", "r")) == NULL)
return;
while (getline(&line, &len, f) != -1) {
char *p, *p2, *tok, *saveptr = NULL;
p = strchr(line, ':');
if (!p)
continue;
p++;
p2 = strchr(p, ':');
if (!p2)
continue;
*p2 = '\0';
/* If we have a mixture between cgroup v1 and cgroup v2
* hierarchies, then /proc/self/cgroup contains entries of the
* form:
*
* 0::/some/path
*
* We need to skip those.
*/
if ((p2 - p) == 0)
continue;
for (tok = strtok_r(p, ",", &saveptr); tok;
tok = strtok_r(NULL, ",", &saveptr)) {
if (strncmp(tok, "name=", 5) == 0)
must_append_string(nlist, tok);
else
must_append_string(klist, tok);
}
}
free(line);
fclose(f);
}
static void trim(char *s)
{
size_t len = strlen(s);
while ((len > 1) && (s[len - 1] == '\n'))
s[--len] = '\0';
}