-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathcgfsng.c
4489 lines (3664 loc) · 125 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 <libgen.h>
#include <poll.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <unistd.h>
#include "cgroup.h"
#include "af_unix.h"
#include "caps.h"
#include "cgroup2_devices.h"
#include "cgroup_utils.h"
#include "commands.h"
#include "commands_utils.h"
#include "conf.h"
#include "error_utils.h"
#include "log.h"
#include "macro.h"
#include "mainloop.h"
#include "memory_utils.h"
#include "mount_utils.h"
#include "open_utils.h"
#include "storage/storage.h"
#include "string_utils.h"
#include "syscall_wrappers.h"
#include "utils.h"
#if !HAVE_STRLCPY
#include "strlcpy.h"
#endif
#if !HAVE_STRLCAT
#include "strlcat.h"
#endif
#if HAVE_DBUS
#include <dbus/dbus.h>
#endif
lxc_log_define(cgfsng, cgroup);
/*
* 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 cg_list_add(void ***list)
{
int idx = 0;
void **p;
if (*list)
for (; (*list)[idx]; idx++)
;
p = realloc(*list, (idx + 2) * sizeof(void **));
if (!p)
return ret_errno(ENOMEM);
p[idx + 1] = NULL;
*list = p;
return idx;
}
/* 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)
{
if (!list)
return false;
for (int i = 0; list[i]; i++)
if (strequal(list[i], entry))
return true;
return false;
}
/* Given a handler's cgroup data, return the struct hierarchy for the controller
* @c, or NULL if there is none.
*/
static struct hierarchy *get_hierarchy(const struct cgroup_ops *ops, const char *controller)
{
if (!ops->hierarchies)
return log_trace_errno(NULL, errno, "There are no useable cgroup controllers");
for (int 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;
}
/*
* Handle controllers with significant implementation changes
* from cgroup to cgroup2.
*/
if (pure_unified_layout(ops)) {
if (strequal(controller, "devices")) {
if (device_utility_controller(ops->unified))
return ops->unified;
break;
} else if (strequal(controller, "freezer")) {
if (freezer_utility_controller(ops->unified))
return ops->unified;
break;
}
}
if (string_in_list(ops->hierarchies[i]->controllers, controller))
return ops->hierarchies[i];
}
if (controller)
INFO("There is no useable %s controller", controller);
else
WARN("There is no empty unified cgroup hierarchy");
return ret_set_errno(NULL, ENOENT);
}
int prepare_cgroup_fd(const struct cgroup_ops *ops, struct cgroup_fd *fd, bool limit)
{
int dfd;
const struct hierarchy *h;
h = get_hierarchy(ops, fd->controller);
if (!h)
return ret_errno(ENOENT);
/*
* The client requested that the controller must be in a specific
* cgroup version.
*/
if (fd->type != 0 && (cgroupfs_type_magic_t)fd->type != h->fs_type)
return ret_errno(EINVAL);
if (limit)
dfd = h->dfd_con;
else
dfd = h->dfd_lim;
if (dfd < 0)
return ret_errno(EBADF);
fd->layout = ops->cgroup_layout;
fd->type = h->fs_type;
if (fd->type == UNIFIED_HIERARCHY)
fd->utilities = h->utilities;
fd->fd = dfd;
return 0;
}
/* Create cpumask from cpulist aka turn:
*
* 0,2-3
*
* into bit array
*
* 1 0 1 1
*/
static int lxc_cpumask(char *buf, __u32 **bitarr, __u32 *last_set_bit)
{
__do_free __u32 *arr_u32 = NULL;
__u32 cur_last_set_bit = 0, nbits = 256;
__u32 nr_u32;
char *token;
nr_u32 = BITS_TO_LONGS(nbits);
arr_u32 = zalloc(nr_u32 * sizeof(__u32));
if (!arr_u32)
return ret_errno(ENOMEM);
lxc_iterate_parts(token, buf, ",") {
__u32 last_bit, first_bit;
char *range;
errno = 0;
first_bit = strtoul(token, NULL, 0);
last_bit = first_bit;
range = strchr(token, '-');
if (range)
last_bit = strtoul(range + 1, NULL, 0);
if (!(first_bit <= last_bit))
return ret_errno(EINVAL);
if (last_bit >= nbits) {
__u32 add_bits = last_bit - nbits + 32;
__u32 new_nr_u32;
__u32 *p;
new_nr_u32 = BITS_TO_LONGS(nbits + add_bits);
p = realloc(arr_u32, new_nr_u32 * sizeof(uint32_t));
if (!p)
return ret_errno(ENOMEM);
arr_u32 = move_ptr(p);
memset(arr_u32 + nr_u32, 0,
(new_nr_u32 - nr_u32) * sizeof(uint32_t));
nbits += add_bits;
}
while (first_bit <= last_bit)
set_bit(first_bit++, arr_u32);
if (last_bit > cur_last_set_bit)
cur_last_set_bit = last_bit;
}
*last_set_bit = cur_last_set_bit;
*bitarr = move_ptr(arr_u32);
return 0;
}
static int lxc_cpumask_update(char *buf, __u32 *bitarr, __u32 last_set_bit,
bool clear)
{
bool flipped = false;
char *token;
lxc_iterate_parts(token, buf, ",") {
__u32 last_bit, first_bit;
char *range;
errno = 0;
first_bit = strtoul(token, NULL, 0);
last_bit = first_bit;
range = strchr(token, '-');
if (range)
last_bit = strtoul(range + 1, NULL, 0);
if (!(first_bit <= last_bit)) {
WARN("The cup range seems to be inverted: %u-%u", first_bit, last_bit);
continue;
}
if (last_bit > last_set_bit)
continue;
while (first_bit <= last_bit) {
if (clear && is_set(first_bit, bitarr)) {
flipped = true;
clear_bit(first_bit, bitarr);
} else if (!clear && !is_set(first_bit, bitarr)) {
flipped = true;
set_bit(first_bit, bitarr);
}
first_bit++;
}
}
if (flipped)
return 1;
return 0;
}
/* Turn cpumask into simple, comma-separated cpulist. */
static char *lxc_cpumask_to_cpulist(__u32 *bitarr, __u32 last_set_bit)
{
__do_free_string_list char **cpulist = NULL;
char numstr[INTTYPE_TO_STRLEN(__u32)] = {0};
int ret;
for (__u32 bit = 0; bit <= last_set_bit; bit++) {
if (!is_set(bit, bitarr))
continue;
ret = strnprintf(numstr, sizeof(numstr), "%u", bit);
if (ret < 0)
return NULL;
ret = lxc_append_string(&cpulist, numstr);
if (ret < 0)
return ret_set_errno(NULL, ENOMEM);
}
if (!cpulist)
return ret_set_errno(NULL, ENOMEM);
return lxc_string_join(",", (const char **)cpulist, false);
}
static inline bool is_unified_hierarchy(const struct hierarchy *h)
{
return h->fs_type == UNIFIED_HIERARCHY;
}
/* Return true if the controller @entry is found in the null-terminated list of
* hierarchies @hlist.
*/
static bool controller_available(struct hierarchy **hlist, char *entry)
{
if (!hlist)
return false;
for (int i = 0; hlist[i]; i++)
if (string_in_list(hlist[i]->controllers, entry))
return true;
return false;
}
static bool controllers_available(struct cgroup_ops *ops)
{
struct hierarchy **hlist;
if (!ops->cgroup_use)
return true;
hlist = ops->hierarchies;
for (char **cur = ops->cgroup_use; cur && *cur; cur++)
if (!controller_available(hlist, *cur))
return log_error(false, "The %s controller found", *cur);
return true;
}
static char **list_new(void)
{
__do_free_string_list char **list = NULL;
int idx;
idx = cg_list_add((void ***)&list);
if (idx < 0)
return NULL;
list[idx] = NULL;
return move_ptr(list);
}
static int list_add_string(char ***list, char *entry)
{
__do_free char *dup = NULL;
int idx;
dup = strdup(entry);
if (!dup)
return ret_errno(ENOMEM);
idx = cg_list_add((void ***)list);
if (idx < 0)
return idx;
(*list)[idx] = move_ptr(dup);
return 0;
}
static char **list_add_controllers(char *controllers)
{
__do_free_string_list char **list = NULL;
char *it;
lxc_iterate_parts(it, controllers, ", \t\n") {
int ret;
ret = list_add_string(&list, it);
if (ret < 0)
return NULL;
}
return move_ptr(list);
}
static char **unified_controllers(int dfd, const char *file)
{
__do_free char *buf = NULL;
buf = read_file_at(dfd, file, PROTECT_OPEN, 0);
if (!buf)
return NULL;
return list_add_controllers(buf);
}
static bool skip_hierarchy(const struct cgroup_ops *ops, char **controllers)
{
if (!ops->cgroup_use)
return false;
for (char **cur_ctrl = controllers; cur_ctrl && *cur_ctrl; cur_ctrl++) {
bool found = false;
for (char **cur_use = ops->cgroup_use; cur_use && *cur_use; cur_use++) {
if (!strequal(*cur_use, *cur_ctrl))
continue;
found = true;
break;
}
if (found)
continue;
return true;
}
return false;
}
static int cgroup_hierarchy_add(struct cgroup_ops *ops, int dfd_mnt, char *mnt,
int dfd_base, char *base_cgroup,
char **controllers, cgroupfs_type_magic_t fs_type)
{
__do_free struct hierarchy *new = NULL;
int idx;
if (abspath(base_cgroup))
return syserror_set(-EINVAL, "Container base path must be relative to controller mount");
new = zalloc(sizeof(*new));
if (!new)
return ret_errno(ENOMEM);
new->dfd_con = -EBADF;
new->dfd_lim = -EBADF;
new->dfd_mon = -EBADF;
new->fs_type = fs_type;
new->controllers = controllers;
new->at_mnt = mnt;
new->at_base = base_cgroup;
new->dfd_mnt = dfd_mnt;
new->dfd_base = dfd_base;
TRACE("Adding cgroup hierarchy mounted at %s and base cgroup %s",
mnt, maybe_empty(base_cgroup));
for (char *const *it = new->controllers; it && *it; it++)
TRACE("The hierarchy contains the %s controller", *it);
idx = cg_list_add((void ***)&ops->hierarchies);
if (idx < 0)
return ret_errno(idx);
if (fs_type == UNIFIED_HIERARCHY)
ops->unified = new;
(ops->hierarchies)[idx] = move_ptr(new);
return 0;
}
static int cgroup_tree_remove(struct hierarchy **hierarchies, const char *path_prune)
{
if (!path_prune || !hierarchies)
return 0;
for (int i = 0; hierarchies[i]; i++) {
struct hierarchy *h = hierarchies[i];
int ret;
ret = cgroup_tree_prune(h->dfd_base, path_prune);
if (ret < 0)
SYSWARN("Failed to destroy %d(%s)", h->dfd_base, path_prune);
else
TRACE("Removed cgroup tree %d(%s)", h->dfd_base, path_prune);
free_equal(h->path_lim, h->path_con);
}
return 0;
}
struct generic_userns_exec_data {
struct hierarchy **hierarchies;
const char *path_prune;
struct lxc_conf *conf;
uid_t origuid; /* target uid in parent namespace */
char *path;
};
static int cgroup_tree_remove_wrapper(void *data)
{
struct generic_userns_exec_data *arg = data;
uid_t nsuid = (arg->conf->root_nsuid_map != NULL) ? 0 : arg->conf->init_uid;
gid_t nsgid = (arg->conf->root_nsgid_map != NULL) ? 0 : arg->conf->init_gid;
int ret;
if (!lxc_drop_groups() && errno != EPERM)
return log_error_errno(-1, errno, "Failed to setgroups(0, NULL)");
ret = setresgid(nsgid, nsgid, nsgid);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to setresgid(%d, %d, %d)",
(int)nsgid, (int)nsgid, (int)nsgid);
ret = setresuid(nsuid, nsuid, nsuid);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to setresuid(%d, %d, %d)",
(int)nsuid, (int)nsuid, (int)nsuid);
return cgroup_tree_remove(arg->hierarchies, arg->path_prune);
}
__cgfsng_ops static void cgfsng_payload_destroy(struct cgroup_ops *ops,
struct lxc_handler *handler)
{
int ret;
if (!ops) {
ERROR("Called with uninitialized cgroup operations");
return;
}
if (!ops->hierarchies)
return;
if (!handler) {
ERROR("Called with uninitialized handler");
return;
}
if (!handler->conf) {
ERROR("Called with uninitialized conf");
return;
}
if (!ops->container_limit_cgroup) {
WARN("Uninitialized limit cgroup");
return;
}
ret = bpf_program_cgroup_detach(handler->cgroup_ops->cgroup2_devices);
if (ret < 0)
WARN("Failed to detach bpf program from cgroup");
/*
* Only do the user namespace dance if we have too. If the container's
* monitor is root we can assume that it is privileged enough to remove
* the cgroups it created when the container started.
*/
if (container_uses_namespace(handler, CLONE_NEWUSER) && !handler->am_root) {
struct generic_userns_exec_data wrap = {
.conf = handler->conf,
.path_prune = ops->container_limit_cgroup,
.hierarchies = ops->hierarchies,
.origuid = 0,
};
ret = userns_exec_full(handler->conf, cgroup_tree_remove_wrapper,
&wrap, "cgroup_tree_remove_wrapper");
} else {
ret = cgroup_tree_remove(ops->hierarchies, ops->container_limit_cgroup);
}
if (ret < 0)
SYSWARN("Failed to destroy cgroups");
}
#define __ISOL_CPUS "/sys/devices/system/cpu/isolated"
#define __OFFLINE_CPUS "/sys/devices/system/cpu/offline"
static bool cpuset1_cpus_initialize(int dfd_parent, int dfd_child,
bool am_initialized)
{
__do_free char *cpulist = NULL, *isolcpus = NULL,
*offlinecpus = NULL, *posscpus = NULL;
__do_free __u32 *possmask = NULL;
int ret;
__u32 poss_last_set_bit = 0;
posscpus = read_file_at(dfd_parent, "cpuset.cpus", PROTECT_OPEN, 0);
if (!posscpus)
return log_error_errno(false, errno, "Failed to read file %d/cpuset.cpus", dfd_parent);
if (file_exists(__ISOL_CPUS)) {
isolcpus = read_file_at(-EBADF, __ISOL_CPUS, PROTECT_OPEN, 0);
if (!isolcpus)
return log_error_errno(false, errno, "Failed to read file \"%s\"", __ISOL_CPUS);
if (!isdigit(isolcpus[0]))
free_disarm(isolcpus);
} else {
TRACE("The path \""__ISOL_CPUS"\" to read isolated cpus from does not exist");
}
if (file_exists(__OFFLINE_CPUS)) {
offlinecpus = read_file_at(-EBADF, __OFFLINE_CPUS, PROTECT_OPEN, 0);
if (!offlinecpus)
return log_error_errno(false, errno, "Failed to read file \"%s\"", __OFFLINE_CPUS);
if (!isdigit(offlinecpus[0]))
free_disarm(offlinecpus);
} else {
TRACE("The path \""__OFFLINE_CPUS"\" to read offline cpus from does not exist");
}
if (!isolcpus && !offlinecpus) {
cpulist = move_ptr(posscpus);
goto copy_parent;
}
ret = lxc_cpumask(posscpus, &possmask, &poss_last_set_bit);
if (ret)
return log_error_errno(false, errno, "Failed to create cpumask for possible cpus");
if (isolcpus)
ret = lxc_cpumask_update(isolcpus, possmask, poss_last_set_bit, true);
if (offlinecpus)
ret |= lxc_cpumask_update(offlinecpus, possmask, poss_last_set_bit, true);
if (!ret) {
cpulist = lxc_cpumask_to_cpulist(possmask, poss_last_set_bit);
TRACE("No isolated or offline cpus present in cpuset");
} else {
cpulist = move_ptr(posscpus);
TRACE("Removed isolated or offline cpus from cpuset");
}
if (!cpulist)
return log_error_errno(false, errno, "Failed to create cpu list");
copy_parent:
if (!am_initialized) {
ret = lxc_writeat(dfd_child, "cpuset.cpus", cpulist, strlen(cpulist));
if (ret < 0)
return log_error_errno(false, errno, "Failed to write cpu list to \"%d/cpuset.cpus\"", dfd_child);
TRACE("Copied cpu settings of parent cgroup");
}
return true;
}
static bool cpuset1_initialize(int dfd_base, int dfd_next)
{
char mems[PATH_MAX];
ssize_t bytes;
char v;
/* Determine whether the base cgroup has cpuset inheritance turned on. */
bytes = lxc_readat(dfd_base, "cgroup.clone_children", &v, 1);
if (bytes < 0)
return syserror_ret(false, "Failed to read file %d(cgroup.clone_children)", dfd_base);
/* Initialize cpuset.cpus removing any isolated and offline cpus. */
if (!cpuset1_cpus_initialize(dfd_base, dfd_next, v == '1'))
return syserror_ret(false, "Failed to initialize cpuset.cpus");
/* Read cpuset.mems from parent... */
bytes = lxc_readat(dfd_base, "cpuset.mems", mems, sizeof(mems));
if (bytes < 0)
return syserror_ret(false, "Failed to read file %d(cpuset.mems)", dfd_base);
/* and copy to first cgroup in the tree... */
bytes = lxc_writeat(dfd_next, "cpuset.mems", mems, bytes);
if (bytes < 0)
return syserror_ret(false, "Failed to write %d(cpuset.mems)", dfd_next);
/* and finally turn on cpuset inheritance. */
bytes = lxc_writeat(dfd_next, "cgroup.clone_children", "1", 1);
if (bytes < 0)
return syserror_ret(false, "Failed to write %d(cgroup.clone_children)", dfd_next);
return log_trace(true, "Initialized cpuset in the legacy hierarchy");
}
static int __cgroup_tree_create(int dfd_base, const char *path, mode_t mode,
bool cpuset_v1, bool eexist_ignore)
{
__do_close int dfd_final = -EBADF;
int dfd_cur = dfd_base;
int ret = 0;
size_t len;
char *cur;
char buf[PATH_MAX];
if (is_empty_string(path))
return ret_errno(EINVAL);
len = strlcpy(buf, path, sizeof(buf));
if (len >= sizeof(buf))
return ret_errno(E2BIG);
lxc_iterate_parts(cur, buf, "/") {
/*
* Even though we vetted the paths when we parsed the config
* we're paranoid here and check that the path is neither
* absolute nor walks upwards.
*/
if (abspath(cur))
return syserror_set(-EINVAL, "No absolute paths allowed");
if (strnequal(cur, "..", STRLITERALLEN("..")))
return syserror_set(-EINVAL, "No upward walking paths allowed");
ret = mkdirat(dfd_cur, cur, mode);
if (ret < 0) {
if (errno != EEXIST)
return syserror("Failed to create %d(%s)", dfd_cur, cur);
ret = -EEXIST;
}
TRACE("%s %d(%s) cgroup", !ret ? "Created" : "Reusing", dfd_cur, cur);
dfd_final = open_at(dfd_cur, cur, PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_BENEATH, 0);
if (dfd_final < 0)
return syserror("Fail to open%s directory %d(%s)",
!ret ? " newly created" : "", dfd_base, cur);
if (dfd_cur != dfd_base)
close(dfd_cur);
else if (cpuset_v1 && !cpuset1_initialize(dfd_base, dfd_final))
return syserror_set(-EINVAL, "Failed to initialize cpuset controller in the legacy hierarchy");
/*
* Leave dfd_final pointing to the last fd we opened so
* it will be automatically zapped if we return early.
*/
dfd_cur = dfd_final;
TRACE("Opened%s cgroup %s as %d", !ret ? " newly created" : "", cur, dfd_cur);
}
/* The final cgroup must be succesfully creatd by us. */
if (ret) {
if (ret != -EEXIST || !eexist_ignore)
return syswarn_set(ret, "Creating the final cgroup %d(%s) failed", dfd_base, path);
}
return move_fd(dfd_final);
}
static bool cgroup_tree_create(struct cgroup_ops *ops, struct lxc_conf *conf,
struct hierarchy *h, const char *cgroup_limit_dir,
const char *cgroup_leaf, bool payload)
{
__do_close int fd_limit = -EBADF, fd_final = -EBADF;
bool cpuset_v1 = false;
/*
* The legacy cpuset controller needs massaging in case inheriting
* settings from its immediate ancestor cgroup hasn't been turned on.
*/
cpuset_v1 = !is_unified_hierarchy(h) && string_in_list(h->controllers, "cpuset");
if (payload && cgroup_leaf) {
/* With isolation both parts need to not already exist. */
fd_limit = __cgroup_tree_create(h->dfd_base, cgroup_limit_dir, 0755, cpuset_v1, false);
if (fd_limit < 0)
return syswarn_ret(false, "Failed to create limiting cgroup %d(%s)", h->dfd_base, cgroup_limit_dir);
h->path_lim = make_cgroup_path(h, h->at_base, cgroup_limit_dir, NULL);
h->dfd_lim = move_fd(fd_limit);
TRACE("Created limit cgroup %d->%d(%s)",
h->dfd_lim, h->dfd_base, cgroup_limit_dir);
/*
* With isolation the devices legacy cgroup needs to be
* iinitialized early, as it typically contains an 'a' (all)
* line, which is not possible once a subdirectory has been
* created.
*/
if (string_in_list(h->controllers, "devices") &&
!ops->setup_limits_legacy(ops, conf, true))
return log_warn(false, "Failed to setup legacy device limits");
/*
* If we use a separate limit cgroup, the leaf cgroup, i.e. the
* cgroup the container actually resides in, is below fd_limit.
*/
fd_final = __cgroup_tree_create(h->dfd_lim, cgroup_leaf, 0755, cpuset_v1, false);
if (fd_final < 0) {
/* Ensure we don't leave any garbage behind. */
if (cgroup_tree_prune(h->dfd_base, cgroup_limit_dir))
SYSWARN("Failed to destroy %d(%s)", h->dfd_base, cgroup_limit_dir);
else
TRACE("Removed cgroup tree %d(%s)", h->dfd_base, cgroup_limit_dir);
return syswarn_ret(false, "Failed to create %s cgroup %d(%s)", payload ? "payload" : "monitor", h->dfd_base, cgroup_limit_dir);
}
h->dfd_con = move_fd(fd_final);
h->path_con = must_make_path(h->path_lim, cgroup_leaf, NULL);
} else {
fd_final = __cgroup_tree_create(h->dfd_base, cgroup_limit_dir, 0755, cpuset_v1, false);
if (fd_final < 0)
return syswarn_ret(false, "Failed to create %s cgroup %d(%s)", payload ? "payload" : "monitor", h->dfd_base, cgroup_limit_dir);
if (payload) {
h->dfd_con = move_fd(fd_final);
h->dfd_lim = h->dfd_con;
h->path_con = make_cgroup_path(h, h->at_base, cgroup_limit_dir, NULL);
h->path_lim = h->path_con;
} else {
h->dfd_mon = move_fd(fd_final);
}
}
return true;
}
static void cgroup_tree_prune_leaf(struct hierarchy *h, const char *path_prune,
bool payload)
{
bool prune = true;
if (payload) {
/* Check whether we actually created the cgroup to prune. */
if (h->dfd_lim < 0)
prune = false;
free_equal(h->path_con, h->path_lim);
close_equal(h->dfd_con, h->dfd_lim);
} else {
/* Check whether we actually created the cgroup to prune. */
if (h->dfd_mon < 0)
prune = false;
close_prot_errno_disarm(h->dfd_mon);
}
/* We didn't create this cgroup. */
if (!prune)
return;
if (cgroup_tree_prune(h->dfd_base, path_prune))
SYSWARN("Failed to destroy %d(%s)", h->dfd_base, path_prune);
else
TRACE("Removed cgroup tree %d(%s)", h->dfd_base, path_prune);
}
__cgfsng_ops static void cgfsng_monitor_destroy(struct cgroup_ops *ops,
struct lxc_handler *handler)
{
int len;
char pidstr[INTTYPE_TO_STRLEN(pid_t)];
const struct lxc_conf *conf;
if (!ops) {
ERROR("Called with uninitialized cgroup operations");
return;
}
if (!ops->hierarchies)
return;
if (!handler) {
ERROR("Called with uninitialized handler");
return;
}
if (!handler->conf) {
ERROR("Called with uninitialized conf");
return;
}
conf = handler->conf;
if (!ops->monitor_cgroup) {
WARN("Uninitialized monitor cgroup");
return;
}
len = strnprintf(pidstr, sizeof(pidstr), "%d", handler->monitor_pid);
if (len < 0)
return;
for (int i = 0; ops->hierarchies[i]; i++) {
__do_close int fd_pivot = -EBADF;
__do_free char *pivot_path = NULL;
struct hierarchy *h = ops->hierarchies[i];
bool cpuset_v1 = false;
int ret;
/* Monitor might have died before we entered the cgroup. */
if (handler->monitor_pid <= 0) {
WARN("No valid monitor process found while destroying cgroups");
goto cgroup_prune_tree;
}
if (conf->cgroup_meta.monitor_pivot_dir)
pivot_path = must_make_path(conf->cgroup_meta.monitor_pivot_dir, CGROUP_PIVOT, NULL);
else if (conf->cgroup_meta.dir)
pivot_path = must_make_path(conf->cgroup_meta.dir, CGROUP_PIVOT, NULL);
else
pivot_path = must_make_path(CGROUP_PIVOT, NULL);
cpuset_v1 = !is_unified_hierarchy(h) && string_in_list(h->controllers, "cpuset");
fd_pivot = __cgroup_tree_create(h->dfd_base, pivot_path, 0755, cpuset_v1, true);
if (fd_pivot < 0) {
SYSWARN("Failed to create pivot cgroup %d(%s)", h->dfd_base, pivot_path);
continue;
}
ret = lxc_writeat(fd_pivot, "cgroup.procs", pidstr, len);
if (ret != 0) {
SYSWARN("Failed to move monitor %s to \"%s\"", pidstr, pivot_path);
continue;
}
cgroup_prune_tree:
ret = cgroup_tree_prune(h->dfd_base, ops->monitor_cgroup);
if (ret < 0)
SYSWARN("Failed to destroy %d(%s)", h->dfd_base, ops->monitor_cgroup);
else
TRACE("Removed cgroup tree %d(%s)", h->dfd_base, ops->monitor_cgroup);
}
}
/*
* Check we have no lxc.cgroup.dir, and that lxc.cgroup.dir.limit_prefix is a
* proper prefix directory of lxc.cgroup.dir.payload.
*
* Returns the prefix length if it is set, otherwise zero on success.
*/
static bool check_cgroup_dir_config(struct lxc_conf *conf)
{
const char *monitor_dir = conf->cgroup_meta.monitor_dir,
*container_dir = conf->cgroup_meta.container_dir,
*namespace_dir = conf->cgroup_meta.namespace_dir;
/* none of the new options are set, all is fine */
if (!monitor_dir && !container_dir && !namespace_dir)
return true;
/* some are set, make sure lxc.cgroup.dir is not also set*/
if (conf->cgroup_meta.dir)
return log_error_errno(false, EINVAL,
"lxc.cgroup.dir conflicts with lxc.cgroup.dir.payload/monitor");
/* make sure both monitor and payload are set */
if (!monitor_dir || !container_dir)
return log_error_errno(false, EINVAL,
"lxc.cgroup.dir.payload and lxc.cgroup.dir.monitor must both be set");
/* namespace_dir may be empty */
return true;
}
#define SYSTEMD_SCOPE_FAILED 2
#define SYSTEMD_SCOPE_UNSUPP 1
#define SYSTEMD_SCOPE_SUCCESS 0
#if HAVE_DBUS
#define DESTINATION "org.freedesktop.systemd1"
#define PATH "/org/freedesktop/systemd1"
#define INTERFACE "org.freedesktop.systemd1.Manager"
static bool dbus_threads_initialized = false;
static void _dbus_connection_free(DBusConnection **conn) {
if (*conn) {
dbus_connection_unref(*conn);
*conn = NULL;
}
}
static void _dbus_message_free(DBusMessage **message)
{
if (*message) {
dbus_message_unref(*message);
*message = NULL;
}
}
static bool systemd_cgroup_scope_ready(DBusConnection *connection, const char *scope_name)
{
__attribute__((__cleanup__(_dbus_message_free))) DBusMessage* message = NULL;
DBusMessageIter iter;
char *unit, *result;
dbus_connection_read_write(connection, 0);
message = dbus_connection_pop_message(connection);
if (!message)
return log_debug(false, "Dbus error...");
if (!dbus_message_is_signal(message, INTERFACE, "JobRemoved"))
return false;