This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
fs.c
2573 lines (2264 loc) · 57.9 KB
/
fs.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
/*
*
* Copyright © 2013 Serge Hallyn
* Author: Serge Hallyn <[email protected]>
*
* based on cgroup.c from
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sched.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <fcntl.h>
#include <sys/param.h>
#include <stdbool.h>
#include <dirent.h>
#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/string.h>
#include <nih/io.h>
#include <nih/option.h>
#include <nih/main.h>
#include <nih/logging.h>
#include <nih/error.h>
#include <nih/hash.h>
#include <nih-dbus/dbus_connection.h>
#include <nih-dbus/dbus_proxy.h>
#include "frontend.h" // for keys_return_type
#include "fs.h" // for #defines
/* defines relating to the release agent */
#define AGENT LIBEXECDIR "/cgmanager/cgm-release-agent"
#define AGENT_LINK_PATH "/run/cgmanager/agents"
/* Define pivot_root() if missing from the C library */
#ifndef HAVE_PIVOT_ROOT
static int pivot_root(const char * new_root, const char * put_old)
{
#ifdef __NR_pivot_root
return syscall(__NR_pivot_root, new_root, put_old);
#else
errno = ENOSYS;
return -1;
#endif
}
#else
extern int pivot_root(const char * new_root, const char * put_old);
#endif
char *all_controllers;
struct controller_mounts {
char *controller;
char *options;
char *path;
char *src;
char *agent;
struct controller_mounts *comounted;
bool premounted;
bool visited;
bool skip;
bool unified;
};
static struct controller_mounts *all_mounts;
static int num_controllers;
/*
* the controller_mnts is an array of the mounts to export as the controller
* list. If freezer and devices are comounted, then they will form one
* entry "freezer,devices"
*/
static int num_controller_mnts;
static char **controller_mnts;
static char *base_path;
bool file_exists(const char *path)
{
struct stat sb;
if (stat(path, &sb) < 0)
return false;
return true;
}
bool dir_exists(const char *path)
{
struct stat sb;
if (stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode))
return false;
return true;
}
char *allow_autoremove_premounted;
int autoremove_premounted_set_release_agent = FALSE;
bool premounted_should_allow_autoremove(const char *controller)
{
nih_local char *allowed = NULL;
char *ctrl;
if (allow_autoremove_premounted == NULL)
return false;
if (strcmp(allow_autoremove_premounted, "all") == 0)
return true;
allowed = NIH_MUST( nih_strdup(NULL, allow_autoremove_premounted) );
nih_assert(controller != NULL);
for (ctrl = strtok(allowed, ","); ctrl != NULL;
ctrl = strtok(NULL, ","))
if (strcmp(controller, ctrl) == 0)
return true;
return false;
}
/*
* Where do we want to mount the controllers? We used to mount
* them under a tmpfs under /sys/fs/cgroup, for all to share. Now
* we want to have our socket there. So how about /run/cgmanager/fs?
* TODO read this from configuration file too
* TODO do we want to create these in a tmpfs?
*/
bool setup_base_run_path(void)
{
base_path = strdup("/run/cgmanager/fs");
if (!base_path) {
nih_fatal("%s: out of memory opening base path", __func__);
return false;
}
if (mkdir("/run", 0755) < 0 && errno != EEXIST) {
nih_fatal("%s: failed to create /run", __func__);
return false;
}
if (mkdir("/run/cgmanager", 0755) < 0 && errno != EEXIST) {
nih_fatal("%s: failed to create /run/cgmanager", __func__);
return false;
}
if (mkdir("/run/cgmanager/fs", 0755) < 0 && errno != EEXIST) {
nih_fatal("%s: failed to create /run/cgmanager/fs", __func__);
return false;
}
if (mkdir(AGENT_LINK_PATH, 0755) < 0 && errno != EEXIST) {
nih_fatal("%s: failed to create %s", __func__, AGENT_LINK_PATH);
return false;
}
return true;
}
static void set_clone_children(const char *path)
{
nih_local char *p = NULL;
FILE *f;
p = NIH_MUST( nih_sprintf(NULL, "%s/cgroup.clone_children", path) );
f = fopen(p, "w");
if (!f) {
nih_fatal("%s: Failed to set clone_children", __func__);
return;
}
fprintf(f, "1\n");
fclose(f);
}
static void set_use_hierarchy(const char *path)
{
nih_local char *p = NULL;
FILE *f;
p = NIH_MUST( nih_sprintf(NULL, "%s/memory.use_hierarchy", path) );
f = fopen(p, "w");
if (!f) {
nih_fatal("%s: Failed to set memory.use_hierarchy", __func__);
return;
}
fprintf(f, "1\n");
fclose(f);
}
static void zero_out(struct controller_mounts *c)
{
memset(c, 0, sizeof(*c));
}
/*
* Look for a controller_mounts struct for controller @c.
* If found, set @found to true and return its index in all_mounts.
* If not found, set @found to false and return index of the first
* location in all_mounts whose controller is > @c, i.e. where it
* should be inserted.
*/
static int find_controller_in_mounts(const char *c, bool *found)
{
int cmp, low = 0, mid = low, high = num_controllers-1;
*found = false;
if (!num_controllers)
return 0;
while (low <= high) {
if (high == low)
break;
if (high == low+1) {
cmp = strcmp(c, all_mounts[low].controller);
if (cmp <= 0) {
mid = low;
break;
}
cmp = strcmp(c, all_mounts[high].controller);
if (cmp > 0) {
mid = high + 1;
if (mid >= num_controllers)
mid = num_controllers-1;
break;
}
mid = high;
break;
}
mid = low + (high-low)/2;
cmp = strcmp(c, all_mounts[mid].controller);
if (cmp == 0)
break;
if (cmp < 0)
high = mid;
else
low = mid;
}
cmp = strcmp(c, all_mounts[mid].controller);
if (cmp == 0)
*found = true;
if (cmp > 0)
mid++;
return mid;
}
static bool fill_in_controller(struct controller_mounts *m, char *controller,
char *src)
{
nih_local char *dest = NULL;
dest = NIH_MUST( nih_sprintf(NULL, "%s/%s", base_path, src) );
m->controller = NIH_MUST( strdup(controller) );
m->options = NULL;
m->path = NIH_MUST( strdup(dest) );
m->src = NIH_MUST( strdup(src) );
nih_info(_("Arranged to mount %s onto %s"), m->controller, m->path);
return true;
}
static bool save_mount_subsys(char *s)
{
struct controller_mounts *tmp;
char *src, *controller;
int i, insert_pt, ret;
size_t len = strlen(s);
bool found;
if (len > MAXPATHLEN) {
nih_fatal("bad controller type: %s", s);
return false;
}
if ((controller = strchr(s, '='))) {
/* this is something like 'name=systemd' */
src = alloca(len+7);
/* so for controller we want 'systemd' */
controller++;
/* and for source we want "none,name=systemd" */
ret = snprintf(src, len+6, "none,%s", s);
if (ret < 0 || ret >= len+6) {
nih_fatal("saving mount subsys for %s", s);
ret = -1;
goto out;
}
} else {
controller = s;
src = s;
}
insert_pt = find_controller_in_mounts(controller, &found);
if (found)
return true;
tmp = realloc(all_mounts, (num_controllers+1) * sizeof(*all_mounts));
if (!tmp) {
nih_fatal("Out of memory mounting controllers");
goto out;
}
all_mounts = tmp;
for (i = num_controllers; i > insert_pt; i--)
all_mounts[i] = all_mounts[i-1];
zero_out(&all_mounts[insert_pt]);
if (!fill_in_controller(&all_mounts[insert_pt], controller, src)) {
ret = -1;
goto out;
}
num_controllers++;
return true;
out:
return false;
}
static bool set_release_agent(struct controller_mounts *m)
{
FILE *f;
nih_local char *path = NULL;
if (m->premounted &&
!(premounted_should_allow_autoremove(m->controller)
&& autoremove_premounted_set_release_agent)) {
nih_info("%s was pre-mounted, not setting a release agent",
m->controller);
return true;
}
path = NIH_MUST( nih_sprintf(NULL, "%s/release_agent", m->path) );
if (m->premounted) {
char buf[128];
size_t read;
bool was_set = false;
f = fopen(path, "r");
if (f == NULL) {
nih_error("failed to open %s for reading", path);
return false;
}
while (!was_set && (read = fread(buf, 1, sizeof(buf), f)) > 0) {
size_t ctr;
for (ctr = 0; !was_set && ctr < read; ctr++)
if (!isspace(buf[ctr]))
was_set = true;
}
fclose(f);
if (was_set) {
nih_info("%s was pre-mounted and already has a release agent, not setting our one",
m->controller);
return true;
}
}
if ((f = fopen(path, "w")) == NULL) {
nih_error("failed to open %s for writing", path);
return false;
}
if (fprintf(f, "%s\n", m->agent) < 0) {
nih_error("failed to set release agent for %s",
m->controller);
fclose(f);
return false;
}
if (fclose(f) != 0) {
nih_error("failed to set release agent for %s",
m->controller);
return false;
}
return true;
}
static bool do_mount_unified(char *src, char *dest)
{
if (mount(src, dest, "cgroup2", 0, NULL) == 0)
return true;
if (errno != ENODEV)
return false;
return mount(src, dest, "cgroup", 0, "__DEVEL__sane_behavior") == 0;
}
static bool do_mount_subsys(int i)
{
char *src, *dest, *controller;
struct controller_mounts *m = &all_mounts[i];
int ret;
dest = m->path;
controller = m->controller;
src = m->src;
if (m->skip) {
nih_info("Skipping mount of %s as requested\n", src);
return true;
}
if (mkdir(dest, 0755) < 0 && errno != EEXIST) {
nih_fatal("Failed to create %s: %s", dest, strerror(errno));
return false;
}
if (m->unified) {
if (!do_mount_unified(m->controller, dest)) {
nih_error("Failed mounting %s: %s\n", m->controller,
strerror(errno));
return false;
}
return true;
}
if (m->premounted)
ret = mount(src, dest, "cgroup", 0, m->options);
else
ret = mount(src, dest, "cgroup", 0, src);
if (ret < 0) {
if (!m->premounted) {
nih_debug("Failed mounting %s onto %s: %s", src, dest, strerror(errno));
free(m->path);
m->path = NULL;
return true;
}
nih_fatal("Failed mounting %s onto %s: %s", src, dest, strerror(errno));
if (m->premounted)
nih_fatal("options was %s\n", m->options);
return false;
}
nih_info(_("Mounted %s onto %s"), controller, dest);
if (strcmp(controller, "cpuset") == 0) {
set_clone_children(dest); // TODO make this optional?
nih_info(_("set clone_children"));
} else if (strcmp(controller, "memory") == 0) {
set_use_hierarchy(dest); // TODO make this optional?
nih_info(_("set memory.use_hierarchy"));
}
if (!set_release_agent(m)) {
nih_error("failed to set release agent for %s",
m->controller);
return false;
}
return true;
}
const char *controllers[] = { "blkio", "cpuset", "cpu", "cpuacct", "debug",
"devices", "freezer", "hugetlb", "memory", "net_cls",
"net_prio", "perf_event", "pids", NULL };
static bool is_kernel_controller(const char *c)
{
int i = 0;
while (controllers[i]) {
if (strcmp(controllers[i++], c) == 0)
return true;
}
return false;
}
static bool process_mounted_subsystem(char *options)
{
char *tok;
nih_local char *cp_opts = NULL, *cp_opts_split = NULL;
int i;
bool found;
tok = strtok(options, ",");
while (tok) {
if (strncmp(tok, "name=", 5) == 0) {
i = find_controller_in_mounts(tok+5, &found);
if (found) {
if (all_mounts[i].unified)
return true;
goto next;
}
if (!save_mount_subsys(tok))
return false;
i = find_controller_in_mounts(tok+5, &found);
if (!found)
return false;
all_mounts[i].premounted = true;
if (!cp_opts)
cp_opts = NIH_MUST( nih_strdup(NULL, tok) );
else
NIH_MUST( nih_strcat_sprintf(&cp_opts, NULL, ",%s", tok) );
} else if (is_kernel_controller(tok)) {
i = find_controller_in_mounts(tok, &found);
if (found) {
if (all_mounts[i].unified)
return true;
goto next;
}
if (!save_mount_subsys(tok))
return false;
i = find_controller_in_mounts(tok, &found);
if (!found)
return false;
all_mounts[i].premounted = true;
if (!cp_opts)
cp_opts = NIH_MUST( nih_strdup(NULL, tok) );
else
NIH_MUST( nih_strcat_sprintf(&cp_opts, NULL, ",%s", tok) );
}
next:
tok = strtok(NULL, ",");
}
if (!cp_opts)
return true;
cp_opts_split = NIH_MUST( nih_strdup(NULL, cp_opts) );
tok = strtok(cp_opts_split, ",");
while (tok) {
if (strncmp(tok, "name=", 5) == 0)
i = find_controller_in_mounts(tok+5, &found);
else
i = find_controller_in_mounts(tok, &found);
if (!found)
return false;
all_mounts[i].options = strdup(cp_opts);
if (!all_mounts[i].options)
return false;
tok = strtok(NULL, ",");
}
return true;
}
/*
* parse /proc/self/mounts looking for already-mounted subsystems
*/
static bool collect_premounted_subsystems(void)
{
char line[1024], *p1, *p2, *p3, *p4;
FILE *f = fopen("/proc/self/mounts", "r");
if (!f)
return false;
while (fgets(line, 1024, f)) {
p1 = strchr(line, ' ');
if (!p1)
goto bad;
*p1 = '\0';
p2 = strchr(++p1, ' ');
if (!p2)
goto bad;
*p2 = '\0';
p2++;
p3 = strchr(p2, ' ');
if (!p3)
goto bad;
*p3 = '\0';
if (strcmp(p2, "cgroup") != 0)
continue;
p4 = strchr(++p3, ' ');
if (!p4)
goto bad;
*p4 = '\0';
if (!process_mounted_subsystem(p3))
goto bad;
}
fclose(f);
return true;
bad:
fclose(f);
return false;
}
static bool collate_premounted_subsystems(void)
{
int i;
for (i = 0; i < num_controllers; i++) {
nih_local char *opts = NULL;
char *tok;
struct controller_mounts *first = NULL, *last = NULL;
int j;
bool found;
first = &all_mounts[i];
if (!first->premounted)
continue;
if (first->unified)
continue;
if (first->comounted) // already linked
continue;
if (!first->options)
continue;
opts = NIH_MUST( nih_strdup(NULL, first->options) );
tok = strtok(opts, ",");
while (tok) {
if (strncmp(tok, "name=", 5) == 0)
j = find_controller_in_mounts(tok+5, &found);
else
j = find_controller_in_mounts(tok, &found);
if (!found)
return false;
if (!last) {
last = &all_mounts[j];
first->comounted = last;
} else {
last->comounted = &all_mounts[j];
last = last->comounted;
}
tok = strtok(NULL, ",");
}
}
return true;
}
static bool collect_kernel_subsystems(void)
{
FILE *cgf;
char line[400];
bool bret = false;
bool is_io_unified = is_unified_controller("io");
if ((cgf = fopen("/proc/cgroups", "r")) == NULL) {
nih_fatal ("Error opening /proc/cgroups: %s", strerror(errno));
return false;
}
while (fgets(line, 400, cgf)) {
char *p;
if (line[0] == '#')
continue;
p = strchr(line, '\t');
if (!p)
continue;
*p = '\0';
// TODO: How stable is /proc/cgroups interface?
// Check the 'enabled' column
p = strrchr(p+1, '\t');
if (!p)
continue;
if (*(p+1) != '1')
continue;
/*
* if unified "io" controller is enabled then "blkio"
* v1 controller is not available for use
*/
if (strcmp(line, "blkio") == 0 && is_io_unified)
continue;
if (!save_mount_subsys(line)) {
nih_fatal("Error storing subsystem %s", line);
goto out;
}
}
bret = true;
out:
fclose(cgf);
return bret;
}
static void prune_from_string(char *list, char *c)
{
char *f;
size_t len;
char *origlist = list;
if (strncmp(c, "none,", 5) == 0)
c += 5;
len = strlen(c);
again:
if (!list)
return;
f = strstr(list, c);
if (!f)
return;
if (f > origlist && *(f-1) != ',') {
list = f+len;
goto again;
}
if (*(f+len) != ',' && *(f+len) != '\0') {
list = f+len;
goto again;
}
/* now we know for sure that [f-1,f+len+1] == ",f," */
if (f[len])
memmove(f, f+len+1, strlen(f+len+1)+1);
else {
if (f > origlist)
*(f-1) = '\0';
else
*f = '\0';
}
goto again;
}
static char *skip_none(char *src)
{
if (strncmp(src, "none,", 5) == 0)
return src+5;
return src;
}
/*
* Build the list of controllers which we return as the result of
* ListControllers
*/
static void build_controller_mntlist(void)
{
int i;
struct controller_mounts *m, *m2;
num_controller_mnts = 0;
for (i = 0; i < num_controllers; i++) {
char *srclist;
m = &all_mounts[i];
if (m->visited || m->skip)
continue;
controller_mnts = realloc(controller_mnts,
(num_controller_mnts+1)*(sizeof(char *)));
if (!controller_mnts) {
nih_fatal("Out of memory building mntlist");
exit(1);
}
srclist = NIH_MUST( nih_strdup(NULL, skip_none(m->src)) );
m->visited = true;
m2 = m->comounted;
while (m2 && m2 != m) {
/*
* XXX Should we use m2->controller or m2->options here?
* options would include "none,". does controller include
* the 'name=' part of systemd? If not do we need ot add it
* by hande?
*/
NIH_MUST( nih_strcat_sprintf(&srclist, NULL, ",%s",
skip_none(m2->src)) );
m2->visited = true;
m2 = m2->comounted;
}
controller_mnts[num_controller_mnts] = srclist;
num_controller_mnts++;
}
}
static void print_debug_controller_info(void)
{
int i;
struct controller_mounts *m;
nih_debug("all unique controllers: %s", all_controllers);
for (i = 0; i < num_controllers; i++) {
m = &all_mounts[i];
nih_debug("%d: controller %s", i, m->controller);
nih_debug(" src %s path %s options %s",
m->src, m->path ? m->path : "(none)", m->options ? m->options : "(none)");
nih_debug(" agent: %s", m->agent ? m->agent : "(none)");
nih_debug(" skipped: %s", m->skip ? "yes" : "no");
nih_debug(" premounted: %s comounted: %s",
m->premounted ? "yes" : "no",
m->comounted ? m->comounted->controller : "(none)");
nih_debug(" unified: %s", m->unified ? "yes" : "no");
}
}
void do_list_controllers(void *parent, char ***output)
{
int i;
nih_assert(output);
*output = NIH_MUST( nih_alloc(parent, (num_controller_mnts+1) * sizeof(char *)) );
(*output)[num_controller_mnts] = NULL;
/* XXX
* This will actually not be right.
* if we have freezer,devices co-mounted, we'll have two separate
* entries for the two.
* So TODO - figure out what we want in that case, and provide it
*/
for (i = 0; i < num_controller_mnts; i++)
(*output)[i] = NIH_MUST( nih_strdup(parent, controller_mnts[i]) );
}
void do_prune_comounts(char *controllers)
{
char *p1 = controllers, *p2;
int i;
bool found;
struct controller_mounts *m1;
while (p1) {
p2 = strchr(p1, ',');
if (!p2)
return;
*p2 = '\0';
i = find_controller_in_mounts(p1, &found);
if (!found) {
*p2 = ',';
p1 = p2+1;
continue;
}
m1 = all_mounts[i].comounted;
while (m1 && m1 != &all_mounts[i]) {
prune_from_string(p2+1, m1->src);
m1 = m1->comounted;
}
*p2 = ',';
p1 = p2+1;
}
}
/*
* If we are passed 'cpu', do nothing and return true.
* If we are passed 'cpu,cpuacct,devices', and all three are comounted,
* set controllers to 'cpu' and return true.
* If we are passed 'cpu,cpuacct,devices', and all three are not comounted,
* return false.
*/
bool prune_verify_comounts(char *controllers)
{
char *comma;
do_prune_comounts(controllers);
comma = strchr(controllers, ',');
if (!comma)
return true;
if (*(comma+1) != '\0')
return false;
*comma = '\0';
return true;
}
/*
* @list is a comma-separated list of words.
* Return true if @word is in @list.
*/
static bool word_in_list(char *word, char *list)
{
char *p = list;
size_t wlen;
if (!list)
return false;
wlen = strlen(word);
while (p && *p) {
size_t len;
char *pe = strchr(p, ',');
len = pe ? pe - p : strlen(p);
if (len == wlen && strncmp(word, p, len) == 0)
return true;
if (pe)
pe++;
p = pe;
}
return false;
}
static void build_all_controllers(char *skip_mounts)
{
int i;
char *c;
for (i = 0; i < num_controllers; i++) {
c = all_mounts[i].src;
if (strncmp(c, "none,", 5) == 0)
c += 5;
if (word_in_list(c, skip_mounts)) {
all_mounts[i].skip = true;
continue;
}
if (!all_controllers)
all_controllers = NIH_MUST( nih_strdup(NULL, c) );
else
NIH_MUST( nih_strcat_sprintf(&all_controllers, NULL, ",%s", c) );
}
do_prune_comounts(all_controllers);
}
/*
* Check whether the unified hierarchy is available
*/
static bool unified_hierarchy_present(void)
{
FILE *f;
char *line = NULL;
size_t len = 0;
bool ret = false;
if ((f = fopen("/proc/self/cgroup", "r")) == NULL)
return false;
while (getline(&line, &len, f) != -1) {
if (strncmp(line, "0:", 2) == 0) {
ret = true;
break;
}
}
fclose(f);
free(line);
return ret;
}
/*
* Mount a transient instance of the unified hierarchy, so that
* we can pin the controllers currently enabled there
*/
static bool mount_transient_unified(void)
{
if (mkdir(UNIFIED_DIR, 0755) < 0 && errno != EEXIST)
return false;
if (!do_mount_unified("cgroup", UNIFIED_DIR)) {
nih_error("Error mounting unified: %s\n", strerror(errno));
return false;
}
return true;
}
static void mark_unified_controllers_comounted(void)
{
int i;
struct controller_mounts *first = NULL, *last = NULL;
for (i = 0; i < num_controllers; i++) {
if (!all_mounts[i].unified)
continue;
if (!first) {
first = last = &all_mounts[i];
continue;
}
last->comounted = &all_mounts[i];
last = &all_mounts[i];
}
if (last && last != first)
last->comounted = first;
}
static bool record_unified_controllers(char *ctrl_list)
{
struct controller_mounts *tmp;
int i, insert_pt;
bool found;
char *tok;
tok = strtok(ctrl_list, " ");
while (tok) {
insert_pt = find_controller_in_mounts(tok, &found);
if (found) {
/*
* Something in the program flow is not right.
* We must not know what's actually going on.
*/
nih_error("Impossible: found duplicate in unified (%s)", tok);
return false;
}
tmp = realloc(all_mounts, (num_controllers+1) * sizeof(*all_mounts));
if (!tmp) {
nih_fatal("Out of memory mounting controllers");
return false;
}
all_mounts = tmp;
for (i = num_controllers; i > insert_pt; i--)
all_mounts[i] = all_mounts[i-1];
zero_out(&all_mounts[insert_pt]);
if (!fill_in_controller(&all_mounts[insert_pt], tok, tok))
return false;
all_mounts[insert_pt].unified = true;
num_controllers++;
tok = strtok(NULL, " ");
}
return true;