-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathcgfsng.c
1110 lines (897 loc) · 26.1 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
/* SPDX-License-Identifier: LGPL-2.1+ */
/*
* 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.
*
* This new implementation assumes that cgroup filesystems are mounted
* under /sys/fs/cgroup/clist where clist is either the controller, or
* a comma-separated list of controllers.
*/
#include "config.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <grp.h>
#include <linux/kdev_t.h>
#include <linux/types.h>
#include <poll.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <unistd.h>
#include "../macro.h"
#include "../memory_utils.h"
#include "../utils.h"
#include "cgroup.h"
#include "cgroup2_devices.h"
#include "cgroup_utils.h"
/* 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;
}
/* Return a copy of @entry prepending "name=", i.e. turn "systemd" into
* "name=systemd". Do not fail.
*/
static char *cg_legacy_must_prefix_named(char *entry)
{
size_t len;
char *prefixed;
len = strlen(entry);
prefixed = must_realloc(NULL, len + 6);
memcpy(prefixed, "name=", STRLITERALLEN("name="));
memcpy(prefixed + STRLITERALLEN("name="), entry, len);
prefixed[len + 5] = '\0';
return prefixed;
}
/* 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))
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 = cg_legacy_must_prefix_named(entry);
(*clist)[newentry] = copy;
}
/* Given a handler's cgroup data, return the struct hierarchy for the controller
* @c, or NULL if there is none.
*/
static struct hierarchy *cgfsng_get_hierarchy(struct cgroup_ops *ops,
const char *controller)
{
int i;
errno = ENOENT;
if (!ops->hierarchies)
return NULL;
for (i = 0; ops->hierarchies[i]; i++) {
if (!controller) {
/* This is the empty unified hierarchy. */
if (ops->hierarchies[i]->controllers &&
!ops->hierarchies[i]->controllers[0])
return ops->hierarchies[i];
continue;
} else if (pure_unified_layout(ops) &&
strcmp(controller, "devices") == 0) {
if (ops->unified->bpf_device_controller)
return ops->unified;
break;
}
if (string_in_list(ops->hierarchies[i]->controllers, controller))
return ops->hierarchies[i];
}
return NULL;
}
/* 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;
}
/* 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 **cg_hybrid_get_controllers(char **klist, char **nlist, char *line,
int type, char **controllers)
{
/* The fourth field is /sys/fs/cgroup/comma-delimited-controller-list
* for legacy hierarchies.
*/
int i;
char *p2, *tok;
char *p = line, *sep = ",";
char **aret = NULL;
for (i = 0; i < 4; i++) {
p = strchr(p, ' ');
if (!p)
return NULL;
p++;
}
/* Note, if we change how mountinfo works, then our caller will need to
* verify /sys/fs/cgroup/ in this field.
*/
if (strncmp(p, DEFAULT_CGROUP_MOUNTPOINT "/", 15) != 0)
return NULL;
p += 15;
p2 = strchr(p, ' ');
if (!p2)
return NULL;
*p2 = '\0';
if (type == CGROUP_SUPER_MAGIC) {
__do_free char *dup = NULL;
/* strdup() here for v1 hierarchies. Otherwise
* lxc_iterate_parts() will destroy mountpoints such as
* "/sys/fs/cgroup/cpu,cpuacct".
*/
dup = must_copy_string(p);
if (!dup)
return NULL;
lxc_iterate_parts (tok, dup, sep)
must_append_controller(klist, nlist, &aret, tok);
*controllers = move_ptr(dup);
}
*p2 = ' ';
return aret;
}
static char **cg_unified_make_empty_controller(void)
{
int newentry;
char **aret = NULL;
newentry = append_null_to_list((void ***)&aret);
aret[newentry] = NULL;
return aret;
}
static char **cg_unified_get_controllers(const char *file)
{
__do_free char *buf = NULL;
char *sep = " \t\n";
char **aret = NULL;
char *tok;
buf = read_file(file);
if (!buf)
return NULL;
lxc_iterate_parts(tok, buf, sep) {
int newentry;
char *copy;
newentry = append_null_to_list((void ***)&aret);
copy = must_copy_string(tok);
aret[newentry] = copy;
}
return aret;
}
static struct hierarchy *add_hierarchy(struct hierarchy ***h, char **clist, char *mountpoint,
char *base_path, int type)
{
struct hierarchy *new;
int newentry;
new = zalloc(sizeof(*new));
new->controllers = clist;
new->mountpoint = mountpoint;
new->base_path = base_path;
new->version = type;
newentry = append_null_to_list((void ***)h);
(*h)[newentry] = new;
return new;
}
/* Get a copy of the mountpoint from @line, which is a line from
* /proc/self/mountinfo.
*/
static char *cg_hybrid_get_mountpoint(char *line)
{
int i;
size_t len;
char *p2;
char *p = line, *sret = NULL;
for (i = 0; i < 4; i++) {
p = strchr(p, ' ');
if (!p)
return NULL;
p++;
}
if (strncmp(p, DEFAULT_CGROUP_MOUNTPOINT "/", 15) != 0)
return NULL;
p2 = strchr(p + 15, ' ');
if (!p2)
return NULL;
*p2 = '\0';
len = strlen(p);
sret = must_realloc(NULL, len + 1);
memcpy(sret, p, len);
sret[len] = '\0';
return sret;
}
static void must_append_string(char ***list, char *entry)
{
int newentry;
char *copy;
newentry = append_null_to_list((void ***)list);
copy = must_copy_string(entry);
(*list)[newentry] = copy;
}
static int get_existing_subsystems(char ***klist, char ***nlist)
{
__do_free char *line = NULL;
__do_fclose FILE *f = NULL;
size_t len = 0;
f = fopen("/proc/self/cgroup", "re");
if (!f)
return -1;
while (getline(&line, &len, f) != -1) {
char *p, *p2, *tok;
p = strchr(line, ':');
if (!p)
continue;
p++;
p2 = strchr(p, ':');
if (!p2)
continue;
*p2 = '\0';
/* If the kernel has cgroup v2 support, then /proc/self/cgroup
* contains an entry of the form:
*
* 0::/some/path
*
* In this case we use "cgroup2" as controller name.
*/
if ((p2 - p) == 0) {
must_append_string(klist, "cgroup2");
continue;
}
lxc_iterate_parts(tok, p, ",") {
if (strncmp(tok, "name=", 5) == 0)
must_append_string(nlist, tok);
else
must_append_string(klist, tok);
}
}
return 0;
}
static void trim(char *s)
{
size_t len;
len = strlen(s);
while ((len > 1) && (s[len - 1] == '\n'))
s[--len] = '\0';
}
/* __cg_mount_direct
*
* Mount cgroup hierarchies directly without using bind-mounts. The main
* uses-cases are mounting cgroup hierarchies in cgroup namespaces and mounting
* cgroups for the LXC_AUTO_CGROUP_FULL option.
*/
static int __cg_mount_direct(struct hierarchy *h, const char *controllerpath)
{
__do_free char *controllers = NULL;
char *fstype = "cgroup2";
unsigned long flags = 0;
int ret;
flags |= MS_NOSUID;
flags |= MS_NOEXEC;
flags |= MS_NODEV;
flags |= MS_RELATIME;
if (h->version != CGROUP2_SUPER_MAGIC) {
controllers = lxc_string_join(",", (const char **)h->controllers, false);
if (!controllers)
return -ENOMEM;
fstype = "cgroup";
}
ret = mount("cgroup", controllerpath, fstype, flags, controllers);
if (ret < 0)
return -1;
return 0;
}
static inline int cg_mount_cgroup_full(struct hierarchy *h,
const char *controllerpath)
{
return __cg_mount_direct(h, controllerpath);
}
static bool cgfsng_mount(struct cgroup_ops *ops, const char *root)
{
__do_free char *cgroup_root = NULL;
int ret;
bool retval = false;
if (!ops)
return ret_set_errno(false, ENOENT);
if (!ops->hierarchies)
return true;
cgroup_root = must_make_path(root, DEFAULT_CGROUP_MOUNTPOINT, NULL);
if (ops->cgroup_layout == CGROUP_LAYOUT_UNIFIED)
return cg_mount_cgroup_full(ops->unified, cgroup_root) == 0;
/* mount tmpfs */
ret = safe_mount(NULL, cgroup_root, "tmpfs",
MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME,
"size=10240k,mode=755", root);
if (ret < 0)
goto on_error;
for (int i = 0; ops->hierarchies[i]; i++) {
__do_free char *controllerpath = NULL;
struct hierarchy *h = ops->hierarchies[i];
char *controller = strrchr(h->mountpoint, '/');
if (!controller)
continue;
controller++;
controllerpath = must_make_path(cgroup_root, controller, NULL);
if (dir_exists(controllerpath))
continue;
ret = mkdir(controllerpath, 0755);
if (ret < 0)
log_error_errno(goto on_error, errno,
"Error creating cgroup path: %s",
controllerpath);
ret = cg_mount_cgroup_full(h, controllerpath);
if (ret < 0)
goto on_error;
}
retval = true;
on_error:
return retval;
}
static int cgfsng_num_hierarchies(struct cgroup_ops *ops)
{
int i = 0;
if (!ops)
return ret_set_errno(-1, ENOENT);
if (!ops->hierarchies)
return 0;
for (; ops->hierarchies[i]; i++)
;
return i;
}
static bool cgfsng_get_hierarchies(struct cgroup_ops *ops, int n, char ***out)
{
int i;
if (!ops)
return ret_set_errno(false, ENOENT);
if (!ops->hierarchies)
return false;
/* sanity check n */
for (i = 0; i < n; i++)
if (!ops->hierarchies[i])
return ret_set_errno(false, ENOENT);
*out = ops->hierarchies[i]->controllers;
return true;
}
static bool cgfsng_get(struct cgroup_ops *ops, const char *controller,
const char *cgroup, const char *file, char **value)
{
__do_free char *path = NULL;
struct hierarchy *h;
h = ops->get_hierarchy(ops, controller);
if (!h)
return false;
path = must_make_path_relative(cgroup, file, NULL);
*value = readat_file(h->fd, path);
return *value != NULL;
}
static int cgfsng_get_memory(struct cgroup_ops *ops, const char *cgroup,
const char *file, char **value)
{
__do_free char *path = NULL;
struct hierarchy *h;
int cgroup2_root_fd, layout, ret;
h = ops->get_hierarchy(ops, "memory");
if (!h)
return -1;
if (!is_unified_hierarchy(h)) {
if (strcmp(file, "memory.max") == 0)
file = "memory.limit_in_bytes";
else if (strcmp(file, "memory.swap.max") == 0)
file = "memory.memsw.limit_in_bytes";
else if (strcmp(file, "memory.swap.current") == 0)
file = "memory.memsw.usage_in_bytes";
else if (strcmp(file, "memory.current") == 0)
file = "memory.usage_in_bytes";
layout = CGROUP_SUPER_MAGIC;
cgroup2_root_fd = -EBADF;
} else {
layout = CGROUP2_SUPER_MAGIC;
cgroup2_root_fd = ops->cgroup2_root_fd;
}
path = must_make_path_relative(cgroup, NULL);
ret = cgroup_walkup_to_root(cgroup2_root_fd, h->fd, path, file, value);
if (ret < 0)
return ret;
if (ret == 1) {
*value = strdup("");
if (!*value)
return -ENOMEM;
}
return layout;
}
static int cgfsng_get_memory_stats_fd(struct cgroup_ops *ops, const char *cgroup)
{
__do_free char *path = NULL;
struct hierarchy *h;
h = ops->get_hierarchy(ops, "memory");
if (!h)
return -1;
path = must_make_path_relative(cgroup, "memory.stat", NULL);
return openat(h->fd, path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
}
static int cgfsng_get_memory_current(struct cgroup_ops *ops, const char *cgroup,
char **value)
{
return cgfsng_get_memory(ops, cgroup, "memory.current", value);
}
static int cgfsng_get_memory_swap_current(struct cgroup_ops *ops,
const char *cgroup, char **value)
{
return cgfsng_get_memory(ops, cgroup, "memory.swap.current", value);
}
static int cgfsng_get_memory_max(struct cgroup_ops *ops, const char *cgroup,
char **value)
{
return cgfsng_get_memory(ops, cgroup, "memory.max", value);
}
static int cgfsng_get_memory_swappiness(struct cgroup_ops *ops, const char *cgroup,
char **value)
{
return cgfsng_get_memory(ops, cgroup, "memory.swappiness", value);
}
static int cgfsng_get_memory_swap_max(struct cgroup_ops *ops,
const char *cgroup, char **value)
{
return cgfsng_get_memory(ops, cgroup, "memory.swap.max", value);
}
static int cgfsng_get_memory_slabinfo_fd(struct cgroup_ops *ops, const char *cgroup)
{
__do_free char *path = NULL;
struct hierarchy *h;
h = ops->get_hierarchy(ops, "memory");
if (!h)
return -1;
if (faccessat(h->fd, "memory.kmem.slabinfo", F_OK, 0))
return -1;
path = must_make_path_relative(cgroup, "memory.kmem.slabinfo", NULL);
return openat(h->fd, path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
}
static bool cgfsng_can_use_swap(struct cgroup_ops *ops)
{
bool has_swap = false;
struct hierarchy *h;
h = ops->get_hierarchy(ops, "memory");
if (!h)
return false;
if (is_unified_hierarchy(h)) {
if (faccessat(h->fd, "memory.swap.max", F_OK, 0))
return false;
if (faccessat(h->fd, "memory.swap.current", F_OK, 0))
return false;
has_swap = true;
} else {
if (faccessat(h->fd, "memory.memsw.limit_in_bytes", F_OK, 0))
return false;
if (faccessat(h->fd, "memory.memsw.usage_in_bytes", F_OK, 0))
return false;
has_swap = true;
}
return has_swap;
}
static int cgfsng_get_memory_stats(struct cgroup_ops *ops, const char *cgroup,
char **value)
{
return cgfsng_get_memory(ops, cgroup, "memory.stat", value);
}
static char *readat_cpuset(int cgroup_fd)
{
__do_free char *val = NULL;
val = readat_file(cgroup_fd, "cpuset.cpus");
if (val && strcmp(val, "") != 0)
return move_ptr(val);
free_disarm(val);
val = readat_file(cgroup_fd, "cpuset.cpus.effective");
if (val && strcmp(val, "") != 0)
return move_ptr(val);
return NULL;
}
static char *readat_cpuset_mems(int cgroup_fd)
{
__do_free char *val = NULL;
val = readat_file(cgroup_fd, "cpuset.mems");
if (val && strcmp(val, "") != 0)
return move_ptr(val);
free_disarm(val);
val = readat_file(cgroup_fd, "cpuset.mems.effective");
if (val && strcmp(val, "") != 0)
return move_ptr(val);
return NULL;
}
static int cgfsng_get_cpuset_cpus(struct cgroup_ops *ops, const char *cgroup,
char **value)
{
__do_close int cgroup_fd = -EBADF;
__do_free char *path = NULL;
char *v;
struct hierarchy *h;
int ret;
h = ops->get_hierarchy(ops, "cpuset");
if (!h)
return -1;
if (!is_unified_hierarchy(h))
ret = CGROUP_SUPER_MAGIC;
else
ret = CGROUP2_SUPER_MAGIC;
*value = NULL;
path = must_make_path_relative(cgroup, NULL);
cgroup_fd = openat_safe(h->fd, path);
if (cgroup_fd < 0)
return -1;
v = readat_cpuset(cgroup_fd);
if (v) {
*value = v;
return ret;
}
/*
* cpuset.cpus and cpuset.cpus.effective are empty so we need to look
* the nearest ancestor with a non-empty cpuset.cpus{.effective} file.
*/
for (;;) {
int fd;
fd = openat_safe(cgroup_fd, "../");
if (fd < 0 || !is_cgroup_fd(fd))
return -1;
close_prot_errno_replace(cgroup_fd, fd);
v = readat_cpuset(fd);
if (v) {
*value = v;
return ret;
}
}
return -1;
}
static int cgfsng_get_cpuset_mems(struct cgroup_ops *ops, const char *cgroup,
char **value)
{
__do_close int cgroup_fd = -EBADF;
__do_free char *path = NULL;
char *v;
struct hierarchy *h;
int ret;
h = ops->get_hierarchy(ops, "cpuset");
if (!h)
return -1;
if (!is_unified_hierarchy(h))
ret = CGROUP_SUPER_MAGIC;
else
ret = CGROUP2_SUPER_MAGIC;
*value = NULL;
path = must_make_path_relative(cgroup, NULL);
cgroup_fd = openat_safe(h->fd, path);
if (cgroup_fd < 0)
return -1;
v = readat_cpuset_mems(cgroup_fd);
if (v) {
*value = v;
return ret;
}
/*
* cpuset.cpus and cpuset.cpus.effective are empty so we need to look
* the nearest ancestor with a non-empty cpuset.cpus{.effective} file.
*/
for (;;) {
int fd;
fd = openat_safe(cgroup_fd, "../");
if (fd < 0 || !is_cgroup_fd(fd))
return -1;
close_prot_errno_replace(cgroup_fd, fd);
v = readat_cpuset_mems(fd);
if (v) {
*value = v;
return ret;
}
}
return -1;
}
static int cgfsng_get_io(struct cgroup_ops *ops, const char *cgroup,
const char *file, char **value)
{
__do_free char *path = NULL;
struct hierarchy *h;
int ret;
h = ops->get_hierarchy(ops, "blkio");
if (!h)
return -1;
if (!is_unified_hierarchy(h))
ret = CGROUP_SUPER_MAGIC;
else
ret = CGROUP2_SUPER_MAGIC;
path = must_make_path_relative(cgroup, file, NULL);
*value = readat_file(h->fd, path);
if (!*value) {
if (errno == ENOENT)
errno = EOPNOTSUPP;
return ret_errno(errno);
}
return ret;
}
static int cgfsng_get_io_service_bytes(struct cgroup_ops *ops,
const char *cgroup, char **value)
{
return cgfsng_get_io(ops, cgroup, "blkio.io_service_bytes_recursive", value);
}
static int cgfsng_get_io_service_time(struct cgroup_ops *ops,
const char *cgroup, char **value)
{
return cgfsng_get_io(ops, cgroup, "blkio.io_service_time_recursive", value);
}
static int cgfsng_get_io_serviced(struct cgroup_ops *ops, const char *cgroup,
char **value)
{
return cgfsng_get_io(ops, cgroup, "blkio.io_serviced_recursive", value);
}
static int cgfsng_get_io_merged(struct cgroup_ops *ops, const char *cgroup,
char **value)
{
return cgfsng_get_io(ops, cgroup, "blkio.io_merged_recursive", value);
}
static int cgfsng_get_io_wait_time(struct cgroup_ops *ops, const char *cgroup,
char **value)
{
return cgfsng_get_io(ops, cgroup, "blkio.io_wait_time_recursive", value);
}
static bool cgfsng_can_use_cpuview(struct cgroup_ops *ops)
{
struct hierarchy *cpu, *cpuacct;
if (pure_unified_layout(ops))
return true;
cpu = ops->get_hierarchy(ops, "cpu");
if (!cpu || is_unified_hierarchy(cpu))
return false;
cpuacct = ops->get_hierarchy(ops, "cpuacct");
if (!cpuacct || is_unified_hierarchy(cpuacct))
return false;
return true;
}
/* At startup, parse_hierarchies finds all the info we need about cgroup
* mountpoints and current cgroups, and stores it in @d.
*/
static int cg_hybrid_init(struct cgroup_ops *ops)
{
__do_free char *basecginfo = NULL;
__do_free char *line = NULL;
__do_free void *fopen_cache = NULL;
__do_fclose FILE *f = NULL;
int ret;
size_t len = 0;
char **klist = NULL, **nlist = NULL;
/* Root spawned containers escape the current cgroup, so use init's
* cgroups as our base in that case.
*/
basecginfo = read_file("/proc/1/cgroup");
if (!basecginfo)
return ret_set_errno(-1, ENOMEM);
ret = get_existing_subsystems(&klist, &nlist);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to retrieve available legacy cgroup controllers");
f = fopen_cached("/proc/self/mountinfo", "re", &fopen_cache);
if (!f)
return log_error_errno(-1, errno, "Failed to open \"/proc/self/mountinfo\"");
while (getline(&line, &len, f) != -1) {
int type;
struct hierarchy *new;
char *base_cgroup = NULL, *mountpoint = NULL;
char **controller_list = NULL;
__do_free char *controllers = NULL;
type = get_cgroup_version(line);
if (type == 0)
continue;
if (type == CGROUP2_SUPER_MAGIC && ops->unified)
continue;
if (ops->cgroup_layout == CGROUP_LAYOUT_UNKNOWN) {
if (type == CGROUP2_SUPER_MAGIC)
ops->cgroup_layout = CGROUP_LAYOUT_UNIFIED;
else if (type == CGROUP_SUPER_MAGIC)
ops->cgroup_layout = CGROUP_LAYOUT_LEGACY;
} else if (ops->cgroup_layout == CGROUP_LAYOUT_UNIFIED) {
if (type == CGROUP_SUPER_MAGIC)
ops->cgroup_layout = CGROUP_LAYOUT_HYBRID;
} else if (ops->cgroup_layout == CGROUP_LAYOUT_LEGACY) {
if (type == CGROUP2_SUPER_MAGIC)
ops->cgroup_layout = CGROUP_LAYOUT_HYBRID;
}
controller_list = cg_hybrid_get_controllers(klist, nlist, line,
type, &controllers);
if (!controller_list && type == CGROUP_SUPER_MAGIC)
continue;
if (type == CGROUP_SUPER_MAGIC)
if (controller_list_is_dup(ops->hierarchies, controller_list))
ret_set_errno(goto next, EEXIST);
mountpoint = cg_hybrid_get_mountpoint(line);
if (!mountpoint)
log_error_errno(goto next, EINVAL, "Failed parsing mountpoint from \"%s\"", line);
if (type == CGROUP_SUPER_MAGIC)
base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, controller_list[0], CGROUP_SUPER_MAGIC);
else
base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, NULL, CGROUP2_SUPER_MAGIC);
if (!base_cgroup)
log_error_errno(goto next, EINVAL, "Failed to find current cgroup %s", mountpoint);
trim(base_cgroup);
prune_init_scope(base_cgroup);
if (type == CGROUP2_SUPER_MAGIC) {
char *cgv2_ctrl_path;
cgv2_ctrl_path = must_make_path(mountpoint, base_cgroup,
"cgroup.controllers",
NULL);
controller_list = cg_unified_get_controllers(cgv2_ctrl_path);
free(cgv2_ctrl_path);
if (!controller_list)
controller_list = cg_unified_make_empty_controller();
}
new = add_hierarchy(&ops->hierarchies, controller_list, mountpoint, base_cgroup, type);
new->__controllers = move_ptr(controllers);
if (type == CGROUP2_SUPER_MAGIC && !ops->unified)
ops->unified = new;
continue;
next:
free_string_list(controller_list);
free(mountpoint);
free(base_cgroup);
}
free_string_list(klist);
free_string_list(nlist);
return 0;
}
static int cg_unified_init(struct cgroup_ops *ops)
{
__do_free char *subtree_path = NULL;
int ret;
char *mountpoint;
char **delegatable;
struct hierarchy *new;