File: fs.c

package info (click to toggle)
cgmanager 0.33-2+deb8u2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,108 kB
  • ctags: 1,376
  • sloc: ansic: 19,320; sh: 12,053; xml: 182; makefile: 180
file content (1887 lines) | stat: -rw-r--r-- 44,422 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
/*
 *
 * Copyright © 2013 Serge Hallyn
 * Author: Serge Hallyn <serge.hallyn@ubuntu.com>
 *
 * 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 <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

/* defines relating to the release agent */
#define AGENT SBINDIR "/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;
};

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;
}

/*
 * 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 (mount("cgmfs", "/run/cgmanager/fs", "tmpfs", 0, "size=100000,mode=0755") < 0) {
		nih_fatal("%s: failed to mount tmpfs onto /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 = strdup(controller);
	if (!m->controller) {
		nih_fatal("Out of memory mounting controllers");
		return false;
	}
	m->options = NULL;
	m->path = strdup(dest);
	m->src = strdup(src);
	if (!m->path ||
			!m->src) {
		nih_fatal("Out of memory mounting controllers");
		return false;
	}
	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) {
		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 ((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_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 (mkdir(dest, 0755) < 0 && errno != EEXIST) {
		nih_fatal("Failed to create %s: %s", dest, strerror(errno));
		return false;
	}
	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", "memory", "net_cls", "net_prio",
			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) // jinkeys, multiple mounts already
				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) // jinkeys, multiple mounts already
				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->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;

	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 (!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
		*(f-1) = '\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)
			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("    premounted: %s comounted: %s",
			m->premounted ? "yes" : "no",
			m->comounted ? m->comounted->controller : "(none)");
	}
}

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;
	}
}

static void build_all_controllers(void)
{
	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 (!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);
}

int collect_subsystems(char *extra_mounts)
{
	/* first collect all already-mounted subsystems */
	if (!collect_premounted_subsystems())
		return -1;

	/* handle the requested extra-mounts, which are not in /proc/cgroups */
	if (extra_mounts) {
		char *e;
		for (e = strtok(extra_mounts, ","); e; e = strtok(NULL, ",")) {
			if (!save_mount_subsys(e)) {
				nih_fatal("Error loading subsystem \"%s\"", e);
				return -1;
			}
		}
	}

	if (!collect_kernel_subsystems())
		return -1;

	if (!collate_premounted_subsystems())
		return -1;

	build_all_controllers();

	build_controller_mntlist();
	print_debug_controller_info();

	return 0;
}

#define NEWROOT "/run/cgmanager/root"

static int do_pivot(void) {
	int oldroot = -1, newroot = -1;

	oldroot = open("/", O_DIRECTORY | O_RDONLY);
	if (oldroot < 0) {
		nih_fatal("%s: Error opening old-/ for fchdir", __func__);
		return -1;
	}
	newroot = open(NEWROOT, O_DIRECTORY | O_RDONLY);
	if (newroot < 0) {
		nih_fatal("%s: Error opening new-/ for fchdir", __func__);
		goto fail;
	}

	/* change into new root fs */
	if (fchdir(newroot)) {
		nih_fatal("%s: can't chdir to new rootfs '%s'", __func__, NEWROOT);
		goto fail;
	}

	/* pivot_root into our new root fs */
	if (pivot_root(".", ".")) {
		nih_fatal("%s: pivot_root syscall failed: %s",
				__func__, strerror(errno));
		goto fail;
	}

	/*
	 * at this point the old-root is mounted on top of our new-root
	 * To unmounted it we must not be chdir'd into it, so escape back
	 * to old-root
	 */
	if (fchdir(oldroot) < 0) {
		nih_fatal("%s: Error entering oldroot", __func__);
		goto fail;
	}
	if (umount2(".", MNT_DETACH) < 0) {
		nih_fatal("%s: Error detaching old root", __func__);
		goto fail;
	}

	if (fchdir(newroot) < 0) {
		nih_fatal("%s: Error re-entering newroot", __func__);
		goto fail;
	}

	close(oldroot);
	close(newroot);

	return 0;

fail:
	if (oldroot != -1)
		close(oldroot);
	if (newroot != -1)
		close(newroot);
	return -1;
}

static int pivot_into_new_root(void) {
	int i, ret;
	char *createdirs[] = {NEWROOT "/run/cgmanager", NEWROOT "/run/cgmanager/fs", NULL};
	char path[100];

	/* Mount tmpfs for new root */
	if (mkdir(NEWROOT, 0755) < 0 && errno != EEXIST) {
		nih_fatal("%s: Failed to create directory for new root\n", __func__);
		return -1;
	}
	ret = mount("/", NEWROOT, NULL, MS_BIND, 0);
	if (ret < 0) {
		nih_fatal("%s: Failed to bind-mount / for new root", __func__);
		return -1;
	}

	ret = snprintf(path, 100, NEWROOT "/proc");
	if (ret < 0 || ret > 100)
		return -1;
	if (mount("/proc", path, NULL, MS_REC|MS_MOVE, 0) < 0) {
		nih_fatal("%s: failed to move /proc into new root: %s",
			__func__, strerror(errno));
		return -1;
	}

	ret = snprintf(path, 100, NEWROOT "/run");
	if (ret < 0 || ret > 100)
		return -1;
	ret = mount("/run", path, NULL, MS_BIND, 0);
	if (ret < 0) {
		nih_fatal("%s: failed to move /run into new root: %s",
			__func__, strerror(errno));
		return -1;
	}

	for (i = 0; createdirs[i]; i++) {
		if (mkdir(createdirs[i], 0755) < 0 && errno != EEXIST) {
			nih_fatal("%s: failed to create %s : %s\n", __func__,
				createdirs[i], strerror(errno));
			return -1;
		}
	}

	ret = snprintf(path, 100, NEWROOT "/run/cgmanager/fs");
	if (ret < 0 || ret > 100)
		return -1;
	if (mount("/run/cgmanager/fs", path, NULL, MS_REC|MS_MOVE, 0) < 0) {
		nih_fatal("%s: failed to move /run/cgmanager/fs into new root: %s",
			__func__, strerror(errno));
		return -1;
	}

	/* Pivot into new root */
	if (do_pivot() < 0)
		return -1;

	return 0;
}

/**
 * Mount the cgroup filesystems and record the information.
 * This should take configuration data from /etc.  For now,
 * Just mount all controllers, separately just as cgroup-lite
 * does, and set the use_hierarchy and clone_children options.
 *
 * Things which should go into configuration file:
 * . which controllers to mount
 * . which controllers to co-mount
 * . any mount options (per-controller)
 * . values for sane_behavior, use_hierarchy, and clone_children
 */
int setup_cgroup_mounts(void)
{
	int i;

	if (unshare(CLONE_NEWNS) < 0) {
		nih_fatal("Failed to unshare a private mount ns: %s", strerror(errno));
		return 0;
	}

	if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, 0) < 0) {
		nih_warn("Failed to re-mount / private");
		return -1;
	}

	for (i=0; i<num_controllers; i++) {
		if (!do_mount_subsys(i)) {
			nih_fatal("Failed mounting cgroups");
			return -1;
		}
	}

	/* Now pivot into a new root */
	if (pivot_into_new_root() < 0) {
		nih_fatal("Failed pivoting into new root");
		return -1;
	}

	return 0;
}

/*
 * In the old release agent support, the release agent is not told the
 * controller in which the cgroup was freed.  Therefore we need to have a
 * different binary for each mounted controller.  We will create these under
 * /run/cgmanager/agents/ as symlinks to /sbin/cgm-release-agent, i.e.
 * /run/cgmanager/agents/cgm-release-agent.freezer.
 */
bool create_agent_symlinks(void)
{
	struct stat statbuf;
	char buf[MAXPATHLEN];
	int i, ret, plen;

	ret = stat(AGENT, &statbuf);
	if (ret < 0) {
		nih_error("release agent not found");
		return false;
	}

	plen = snprintf(buf, MAXPATHLEN, "%s/", AGENT_LINK_PATH);
	if (plen < 0 || plen >= MAXPATHLEN) {
		nih_error("memory error");
		return false;
	}

	for (i=0; i<num_controllers; i++) {
		if (all_mounts[i].premounted)
			continue;

		ret = snprintf(buf+plen, MAXPATHLEN-plen, "cgm-release-agent.%s",
				all_mounts[i].controller);
		if (ret < 0 || ret >= MAXPATHLEN) {
			nih_error("path names too long");
			return false;
		}
		nih_info("buf is %s", buf);
		if (!file_exists(buf)) {
			if (symlink(AGENT, buf) < 0) {
				nih_error("failed to create release agent for %s",
					all_mounts[i].controller);
				return false;
			}
		}
		if ((all_mounts[i].agent = strdup(buf)) == NULL) {
			nih_error("out of memory");
			return false;
		}
	}

	return true;
}

static inline void drop_newlines(char *s)
{
	int l;

	for (l=strlen(s); l>0 && s[l-1] == '\n'; l--)
		s[l-1] = '\0';
}

/*
 * The user will pass in 'cpuset' or 'systemd'.  /proc/self/cgroup will
 * show 'cpuset:' or 'name=systemd:'.  We have to account for that.
 */
static bool is_same_controller(const char *cmp, const char *cnt)
{
	if (strcmp(cmp, cnt) == 0)
		return true;
	if (strncmp(cmp, "name=", 5) != 0)
		return false;
	if (strcmp(cmp+5, cnt) == 0)
		return true;
	return false;
}

/*
 * pid_cgroup: return the cgroup of @pid for @controller.
 * retv must be a (at least) MAXPATHLEN size buffer into
 * which the answer will be copied.
 */
static inline char *pid_cgroup(pid_t pid, const char *controller, char *retv)
{
	FILE *f;
	char path[100];
	char *line = NULL, *cgroup = NULL;
	size_t len = 0;

	sprintf(path, "/proc/%d/cgroup", pid);
	if ((f = fopen(path, "r")) == NULL) {
		nih_error("could not open cgroup file for %d", pid);
		return NULL;
	}
	while (getline(&line, &len, f) != -1) {
		char *c1, *c2;
		char *token, *saveptr = NULL;
		if ((c1 = strchr(line, ':')) == NULL)
			continue;
		if ((c2 = strchr(++c1, ':')) == NULL)
			continue;
		*c2 = '\0';
		for (; (token = strtok_r(c1, ",", &saveptr)); c1 = NULL) {
			if (!is_same_controller(token, controller))
				continue;
			if (strlen(c2+1) + 1 > MAXPATHLEN) {
				nih_error("cgroup name too long");
				goto found;
			}
			strncpy(retv, c2+1, strlen(c2+1)+1);
			drop_newlines(retv);
			cgroup = retv;
			goto found;
		}
	}
found:
	fclose(f);
	free(line);
	return cgroup;
}

/*
 * Given a open file * to /proc/pid/{u,g}id_map, and an id
 * valid in the caller's namespace, return the id mapped into
 * pid's namespace.
 * Returns the mapped id, or -1 on error.
 */
unsigned int
convert_id_to_ns(FILE *idfile, unsigned int in_id)
{
	unsigned int nsuid,   // base id for a range in the idfile's namespace
		     hostuid, // base id for a range in the caller's namespace
		     count;   // number of ids in this range
	char line[400];
	int ret;

	fseek(idfile, 0L, SEEK_SET);
	while (fgets(line, 400, idfile)) {
		ret = sscanf(line, "%u %u %u\n", &nsuid, &hostuid, &count);
		if (ret != 3)
			continue;
		if (hostuid + count < hostuid || nsuid + count < nsuid) {
			/*
			 * uids wrapped around - unexpected as this is a procfile,
			 * so just bail.
			 */
			nih_error("pid wrapparound at entry %u %u %u in %s",
				nsuid, hostuid, count, line);
			return -1;
		}
		if (hostuid <= in_id && hostuid+count > in_id) {
			/*
			 * now since hostuid <= in_id < hostuid+count, and
			 * hostuid+count and nsuid+count do not wrap around,
			 * we know that nsuid+(in_id-hostuid) which must be
			 * less that nsuid+(count) must not wrap around
			 */
			return (in_id - hostuid) + nsuid;
		}
	}

	// no answer found
	return -1;
}

/*
 * Given host @uid, return the uid to which it maps in
 * @pid's user namespace, or -1 if none.
 */
bool hostuid_to_ns(uid_t uid, pid_t pid, uid_t *answer)
{
	FILE *f;
	char line[400];

	sprintf(line, "/proc/%d/uid_map", pid);
	if ((f = fopen(line, "r")) == NULL) {
		return false;
	}

	*answer = convert_id_to_ns(f, uid);
	fclose(f);

	if (*answer == -1)
		return false;
	return true;
}

/*
 * pid may access path if the uids are the same, or if
 * path's uid is mapped into the userns and pid is root
 * there, or if the gids are the same and path has mode
 * in group rights, or if path has mode in other rights.
 *
 * uid and gid are passed in to avoid recomputation.  uid
 * and gid are the host uids, not mapped into the ns.
 *
 * TODO should we use acls
 * TODO should we be checking for x access over each directory along the path
 */
bool may_access(pid_t pid, uid_t uid, gid_t gid, const char *path, int mode)
{
	struct stat sb;
	int ret;
	uid_t nsruid, nsvuid;

	ret = stat(path, &sb);
	if (ret < 0) {
		nih_error("Could not look up %s\n", path);
		return false;
	}

	// TODO should we check capabilities in case of (host) root?
	if (uid == 0)
		return true;

	/*
	 * If victim is mapped into requestor's uid namespace, and
	 * requestor is root there, then that suffices.
	 */
	if (hostuid_to_ns(sb.st_uid, pid, &nsvuid) &&
			hostuid_to_ns(uid, pid, &nsruid) && nsruid == 0)
		return true;

	if (uid == sb.st_uid) {
		if (mode == O_RDONLY && sb.st_mode & S_IRUSR)
			return true;
		if (mode == O_RDWR && ((sb.st_mode & (S_IRUSR|S_IWUSR)) == (S_IRUSR|S_IWUSR)))
			return true;
		if (mode == O_WRONLY && sb.st_mode & S_IWUSR)
			return true;
		return false;
	}
	if (gid == sb.st_gid) {
		if (mode == O_RDONLY && sb.st_mode & S_IRGRP)
			return true;
		if (mode == O_RDWR && ((sb.st_mode & (S_IRGRP|S_IWGRP)) == (S_IRGRP|S_IWGRP)))
			return true;
		if (mode == O_WRONLY && sb.st_mode & S_IWGRP)
			return true;
		return false;
	}

	if (mode == O_RDONLY && sb.st_mode & S_IROTH)
		return true;
	if (mode == O_RDWR && ((sb.st_mode & (S_IROTH|S_IWOTH)) == (S_IROTH|S_IWOTH)))
		return true;
	if (mode == O_WRONLY && sb.st_mode & S_IWOTH)
		return true;
	return false;
}

const char *get_controller_path(const char *controller)
{
	int i;
	bool found;

	i = find_controller_in_mounts(controller, &found);
	if (!found) {
		if (strncmp(controller, "name=", 5) == 0) {
			i = find_controller_in_mounts(controller+5, &found);
			if (found)
				return all_mounts[i].path;
		}
		return NULL;
	}
	return all_mounts[i].path;
}

int get_path_depth(const char *p)
{
	int depth = 0;

	if (!p)
		return 0;
	while (*p) {
		while (*p && *p == '/')
			p++;
		if (*p)
			depth++;
		while (*p && *p != '/')
			p++;
	}
	return depth;
}

/*
 * Calculate a full path to the cgroup being requested.
 * @pid is the process making the request
 * @controller is the mounted controller under which we will look.
 * @cgroup is the cgroup which @pid is asking about.  If @cgroup is
 * @path is the path in which to return the full cgroup path.
 *    "a/b", then we concatenate "/cgroup/for/pid" with "a/b"
 *    If @cgroup is "/a/b", then we use "/a/b"
 * @depth, if not null, will contain the depth of the tasks's
 * current cgroup plus the proposed new cgroup.
 */
bool compute_pid_cgroup(pid_t pid, const char *controller, const char *cgroup,
		char *path, int *depth)
{
	int ret;
	char requestor_cgpath[MAXPATHLEN], fullpath[MAXPATHLEN];
	/*
	 * cg contains the the requestor's current cgroup, to prepend to
	 * the requested cgroup - or "" if requesting an absolute path
	 */
	char *cg = "";
	const char *cont_path;
	bool abspath = false;

	if (!cgroup) {
		nih_error("%s: BUG: called with NULL cgroup\n", __func__);
		return false;
	}

	if (cgroup[0] != '/') {
		cg = pid_cgroup(pid, controller, requestor_cgpath);
		if (!cg) {
			nih_error("Found no cgroup entry for pid %lu controller %s\n",
				(unsigned long)pid, controller);
			return false;
		}
	} else
		abspath = true;

	if (depth)
		*depth = get_path_depth(cg) + get_path_depth(cgroup);

	if ((cont_path = get_controller_path(controller)) == NULL) {
		nih_error("Controller %s not mounted", controller);
		return false;
	}

	/* append the requested cgroup */
	ret = snprintf(fullpath, MAXPATHLEN, "%s/%s%s%s", cont_path,
			abspath ? "" : cg, abspath ? "" : "/",
			cgroup ? cgroup : "");
	if (ret < 0 || ret >= MAXPATHLEN) {
		nih_error("Path name too long: %s/%s/%s", cont_path, cg, cgroup);
		return false;
	}

	/* Make sure client isn't passing us a bunch of bogus '../'s to
	 * try to read host files */
	if (!realpath(fullpath, path)) {
		nih_error("Invalid path %s (%s)", fullpath, strerror(errno));
		return false;
	}
	if (strncmp(path, cont_path, strlen(cont_path)) != 0) {
		nih_error("invalid cgroup path '%s' for pid %d", cgroup, pid);
		return false;
	}

	return true;
}

/*
 * file_read_string:
 *
 * @parent: parent which will be given a reference to the returned string
 * (to allow the returned value to be freed automatically when @parent is
 * freed).
 * @path: Full path to file to read.
 *
 * Read specified file and return its contents.
 *
 * Note: this is not a general purpose I/O function.  It is specifically
 * written for virtual filesystems (cgroupfs) where we do know that the
 * file ends in \0, and do not know that stat.st_size is reliable.
 *
 * Returns: newly-allocated contents of file @path, or NULL on
 * insufficient memory.
 */
char *file_read_string(void *parent, const char *path)
{
	int ret, fd = open(path, O_RDONLY);
	char *string = NULL;
	off_t sz = 0;
	if (fd < 0) {
		nih_error("Error opening %s: %s", path, strerror(errno));
		return NULL;
	}

	while (1) {
		char *n;
		sz += 1024;
		if (!(n = nih_realloc(string, parent, sz))) {
			if (string)
				nih_free(string);
			string = NULL;
			goto out;
		}
		string = n;
		memset(string+sz-1024, 0, 1024);
		ret = read(fd, string+sz-1024, 1024);
		if (ret < 0) {
			nih_free(string);
			string = NULL;
			goto out;
		}
		if (ret < 1024)
			break;
	}
out:
	close(fd);
	if (string && *string)
		drop_newlines(string);
	return string;
}

static int find_ordered_pid(int32_t *pids, int32_t pid, int nrpids)
{
	int low = 0, mid = low, high = nrpids-1;

	if (!nrpids)
		return 0;

	while (low <= high) {
		if (high == low)
			break;
		if (high == low+1) {
			if (pid <= pids[low]) {
				mid = low;
				break;
			}
			if (pid > pids[high]) {
				mid = high + 1;
				if (mid >= nrpids)
					mid = nrpids-1;
				break;
			}
			mid = high;
			break;
		}
		mid = low + (high-low)/2;
		if (pid == pids[mid])
			break;
		if (pid < pids[mid])
			high = mid;
		else
			low = mid;
	}

	if (pid > pids[mid])
		mid++;

	return mid;
}

static bool insert_ordered_pid(int32_t *pids, int32_t pid, int nrpids)
{
	int i, j;

	/* TODO Switch this to binary */
	i = find_ordered_pid(pids, pid, nrpids);
	if (i < nrpids && pids[i] == pid)
		return false;
	for (j = nrpids; j > i; j--)
		pids[j] = pids[j-1];
	pids[i] = pid;
	return true;
}

/*
 * file_read_pids:
 *
 * @parent: parent which will be given a reference to the returned string
 * (to allow the returned value to be freed automatically when @parent is
 * freed).
 * @path: Full path to file to read.
 * @pids: pointer to an array of ints in which pids will be placed.
 * This should start as NULL, but on repeated calls will be re-used.
 * @alloced_pids: size of @pids allocated so far
 * @nrpids: number of pids in @pids which are already in use
 *
 * Read specified file and return the pids it contains.  The file is
 * expected to contain only a set of newline-separated int32_ts.
 *
 * Returns: 0 on success, -1 on memory error, -2 on failure to open the
 * cgroup's tasks file.
 * @pids will be ordered
 */
int file_read_pids(void *parent, const char *path, int32_t **pids,
			int *alloced_pids, int *nrpids)
{
	int pid;
	FILE *fin = fopen(path, "r");

	if (!fin) {
		nih_error("Error opening %s: %s", path, strerror(errno));
		return -2;
	}

	while (fscanf(fin, "%d", &pid) == 1) {
		if (*nrpids + 1 >= *alloced_pids) {
			int32_t *tmp;
			*alloced_pids += 256;
			tmp = nih_realloc(*pids, parent,
					  *alloced_pids*sizeof(int32_t));
			if (!tmp) {
				nih_error("Out of memory getting pid list");
				fclose(fin);
				return -1;
			}
			*pids = tmp;
			memset(&(tmp[*nrpids]), 0, 256);
		}
		if (insert_ordered_pid(*pids, (int32_t) pid, *nrpids))
			(*nrpids)++;
	}
	fclose(fin);
	return 0;
}

/*
 * get_pid_creds: get the real uid and gid of @pid from
 * /proc/$$/status
 * (XXX should we use euid here?)
 */
void get_pid_creds(pid_t pid, uid_t *uid, gid_t *gid)
{
	char line[400];
	uid_t u;
	gid_t g;
	FILE *f;

	*uid = -1;
	*gid = -1;
	sprintf(line, "/proc/%d/status", pid);
	if ((f = fopen(line, "r")) == NULL) {
		nih_error("Error opening %s: %s", line, strerror(errno));
		return;
	}
	while (fgets(line, 400, f)) {
		if (strncmp(line, "Uid:", 4) == 0) {
			if (sscanf(line+4, "%u", &u) != 1) {
				nih_error("bad uid line for pid %u", pid);
				fclose(f);
				return;
			}
			*uid = u;
		} else if (strncmp(line, "Gid:", 4) == 0) {
			if (sscanf(line+4, "%u", &g) != 1) {
				nih_error("bad gid line for pid %u", pid);
				fclose(f);
				return;
			}
			*gid = g;
		}
	}
	fclose(f);
}

/*
 * Given a directory path, chown it to a userid.
 * We will chown $path and try to chown $path/tasks and $path/procs.
 * if @all_children is true, then chown all files under @path.  (This
 * is for the case where the caller had the rights to mkdir the path.
 * In that case he gets to write to all files - the kernel will ensure
 * hierarhical limits)
 *
 * Return true so long as we could chown the directory itself.
 */
bool chown_cgroup_path(const char *path, uid_t uid, gid_t gid, bool all_children)
{
	int len;

	nih_assert (path);
	len = strlen(path);
	if (chown(path, uid, gid) < 0)
		return false;

	if (all_children) {
		// chown all the files in the directory
		struct dirent dirent, *direntp;
		DIR *d;

		if (len >= MAXPATHLEN)
			return true;

		d = opendir(path);
		if (!d)
			goto out;

		while (readdir_r(d, &dirent, &direntp) == 0 && direntp) {
			nih_local char *fpath = NULL;
			if (!strcmp(direntp->d_name, ".") || !strcmp(direntp->d_name, ".."))
				continue;
			fpath = NIH_MUST( nih_sprintf(NULL, "%s/%s", path, direntp->d_name) );
			if (chown(fpath, uid, gid) < 0)
				nih_error("Failed to chown file %s to %u:%u",
					fpath, uid, gid);
		}
		closedir(d);
	} else {
		// chown only the tasks and procs files
		nih_local char *fpath = NULL;
		fpath = NIH_MUST( nih_sprintf(NULL, "%s/cgroup.procs", path) );
		if (chown(fpath, uid, gid) < 0)
			nih_error("Failed to chown procs file %s", fpath);
		sprintf(fpath+len, "/tasks");
		if (chown(fpath, uid, gid) < 0)
			nih_error("Failed to chown tasks file %s", fpath);
	}

out:
	return true;
}

/*
 * Given a directory path, chmod it.
 *
 * Caller has already checked for permission
 *
 * Return true so long as we could chown the directory itself.
 */
bool chmod_cgroup_path(const char *path, int mode)
{
	nih_assert (path);
	if (chmod(path, mode) < 0) {
		nih_error("Failed to chown tasks file %s", path);
		return false;
	}

	return true;
}

/*
 * TODO - make this more baroque to allow ranges etc
 */
static char *set_value_blacklist[] = { "tasks",
	"release-agent",
	"cgroup.procs",
	"notify-on-release"
};
static size_t blacklist_len = sizeof(set_value_blacklist)/sizeof(char *);

bool set_value_trusted(const char *path, const char *value)
{
	int len;
	FILE *f;

	nih_assert (path);

	if (!value)
		value = "";

	len = strlen(value);

	if ((f = fopen(path, "w")) == NULL) {
		nih_error("Error opening %s for writing", path);
		return false;
	}
	if (fprintf(f, "%s", value) < 0) {
		nih_error("Error writing %s to %s: %s", value, path,
			  strerror(errno));
		fclose(f);
		return false;
	}
	if (*value && value[len-1] != '\n')
		fprintf(f, "\n");
	if (fclose(f) != 0) {
		nih_error("Error closing %s", path);
		return false;
	}
	return true;
}
bool set_value(const char *path, const char *value)
{
	int i;

	nih_assert (path);

	for (i = 0; i < blacklist_len; i++) {
		const char *p = strrchr(path, '/');
		if (p)
			p++;
		else
			p = path;
		if (strcmp(p, set_value_blacklist[i]) == 0) {
			nih_error("attempted write to %s", set_value_blacklist[i]);
			return false;
		}
	}

	return set_value_trusted(path, value);
}

/*
 * Tiny helper to read the /proc/pid/ns/pid link for a given pid.
 * @pid: the pid whose link name to look up
 */
unsigned long read_pid_ns_link(int pid)
{
	int ret;
	struct stat sb;
	char path[100];
	ret = snprintf(path, 100, "/proc/%d/ns/pid", pid);
	if (ret < 0 || ret >= 100) {
		nih_fatal("Error reading pid ns link");
		exit(1);
	}
	ret = stat(path, &sb);
	return sb.st_ino;
}

/*
 * Tiny helper to read the /proc/pid/ns/user link for a given pid.
 * @pid: the pid whose link name to look up
 */
unsigned long read_user_ns_link(int pid)
{
	int ret;
	struct stat sb;
	char path[100];
	ret = snprintf(path, 100, "/proc/%d/ns/user", pid);
	if (ret < 0 || ret >= 100) {
		nih_fatal("Error reading user ns link");
		exit(1);
	}
	ret = stat(path, &sb);
	return sb.st_ino;
}

bool realpath_escapes(char *path, char *safety)
{
		/* Make sure r doesn't try to escape his cgroup with .. */
	char *tmppath;
	if (!(tmppath = realpath(path, NULL))) {
		nih_error("Invalid path %s", path);
		return true;
	}
	if (strncmp(safety, tmppath, strlen(safety)) != 0) {
		nih_error("Improper requested path %s escapes safety %s",
			   path, safety);
		free(tmppath);
		return true;
	}
	free(tmppath);
	return false;
}

/*
 * move_self_to_root: called by cgmanager at startup to make sure
 * it starts in /
 */
bool move_self_to_root(void)
{
	int i;
	pid_t me = getpid();

	for (i = 0; i < num_controllers; i++) {
		FILE *f;
		nih_local char *path = NULL;

		if (!all_mounts[i].path)
			continue;
		path = NIH_MUST( nih_sprintf(NULL, "%s/tasks", all_mounts[i].path) );
		if ((f = fopen(path, "w")) == NULL)
			return false;
		if (fprintf(f, "%d\n", me) <= 0) {
			fclose(f);
			return false;
		}
		if (fclose(f) != 0)
			return false;
	}
	return true;
}

/*
 * get_directory_children:
 *
 * @parent: parent which will be given a reference to the returned string
 * (to allow the returned value to be freed automatically when @parent is
 * freed).
 * @path: Full path whose child directories to list.
 * output: pointer to which the list of directory names will be stored.
 *
 * Read all child directories under @path.
 *
 * Returns: Number of directories read.  The names will be placed in the
 * null-terminated array @output.
 */
int get_directory_children(void *parent, const char *path, char ***output)
{
	int used = 0, alloced = 5;
	DIR *d;
	struct dirent dirent, *direntp;

	nih_assert(output);
	d = opendir(path);
	if (!d) {
		nih_error("%s: failed to open directory %s: %s",
			__func__, path, strerror(errno));
		return -1;
	}
	*output = NIH_MUST( nih_alloc(parent, alloced * sizeof(char *)) );
	(*output)[0] = NULL;
	while (readdir_r(d, &dirent, &direntp) == 0 && direntp) {
		if (!strcmp(direntp->d_name, ".") || !strcmp(direntp->d_name, ".."))
			continue;
		if (direntp->d_type != DT_DIR)
			continue;
		if (used+1 >= alloced) {
			char **tmp;
			alloced += 5;
			tmp = nih_realloc(*output, parent, alloced * sizeof(char *));
			if (!tmp) {
				nih_free(*output);
				*output = NULL;
				nih_error("%s: Out of memory", __func__);
				closedir(d);
				return -1;
			}
			*output = tmp;
		}
		(*output)[used] = NIH_MUST( nih_strdup(parent, direntp->d_name) );
		(*output)[used+1] = NULL;
		used++;
	}
	closedir(d);
	return used;
}

int get_directory_contents(void *parent, const char *path,
	struct keys_return_type ***output)
{
	DIR *d;
	size_t entries = 0;
	struct dirent dirent, *direntp;

	nih_assert(output);
	d = opendir(path);
	if (!d) {
		nih_error("%s: failed to open directory %s: %s",
			__func__, path, strerror(errno));
		return -1;
	}
	*output = NIH_MUST( nih_alloc(parent, (entries + 1) * sizeof(**output)) );
	(*output)[0] = NULL;
	while (readdir_r(d, &dirent, &direntp) == 0 && direntp) {
		struct keys_return_type *tmp;
		struct stat sb;
		struct keys_return_type **r;
		nih_local char *pathname = NULL;

		if (!strcmp(direntp->d_name, ".") || !strcmp(direntp->d_name, ".."))
			continue;
		if (direntp->d_type != DT_REG)
			continue;

		r = NIH_MUST( nih_realloc(*output, parent, (entries + 2) * sizeof(struct keys_return_type *)) );
		*output = r;

		(*output)[entries+1] = NULL;
		(*output)[entries] = tmp = NIH_MUST( nih_new(*output, struct keys_return_type));
		tmp->name = NIH_MUST( nih_strdup(tmp, direntp->d_name) );
		pathname = NIH_MUST( nih_sprintf(NULL, "%s/%s", path, direntp->d_name) );
		if (stat(pathname, &sb) < 0) {
			tmp->uid = tmp->gid = -1;
			tmp->perms = 0;
		} else {
			tmp->uid = sb.st_uid;
			tmp->gid = sb.st_gid;
			tmp->perms = (uint32_t) sb.st_mode;
		}
		entries++;
	}
	closedir(d);
	return entries;
}

void convert_directory_contents(struct keys_return_type **keys, struct ucred r)
{
	int i = 0;
	FILE *uidf, *gidf;
	nih_local char *upath = NULL, *gpath = NULL;

	upath = nih_sprintf(NULL, "/proc/%d/uid_map", r.pid);
	uidf = fopen(upath, "r");
	if (!uidf)
		return;
	gpath = nih_sprintf(NULL, "/proc/%d/gid_map", r.pid);
	gidf = fopen(gpath, "r");
	if (!gidf) {
		fclose(uidf);
		return;
	}

	while (keys[i]) {
		keys[i]->uid = convert_id_to_ns(uidf, keys[i]->uid);
		keys[i]->gid = convert_id_to_ns(gidf, keys[i]->gid);
		i++;
	}
	fclose(uidf);
	fclose(gidf);
}


bool was_premounted(const char *controller)
{
	bool found;
	int i = find_controller_in_mounts(controller, &found);
	if (!found)
		return false;
	return all_mounts[i].premounted;
}

/*
 * Check that (absolute) @path is under @pid's cgroup for @contr
 */
bool path_is_under_taskcg(pid_t pid, const char *contr,const char *path)
{
	char pcgpath[MAXPATHLEN];
	size_t plen;

	// Get p's current cgroup in pcgpath
	if (!compute_pid_cgroup(pid, contr, "", pcgpath, NULL)) {
		nih_error("%s: Could not determine the proxy's cgroup for %s",
				__func__, contr);
		return false;
	}
	plen = strlen(pcgpath);
	// path must start with pcgpath
	if (strncmp(pcgpath, path, plen) != 0)
		return false;
	// If path is equal to pcgpath then that's ok
	if (plen == strlen(path))
		return true;
	/*
	 * if path is longer than pcpgath, then it must be a subdirectory
	 * of pcpgpath. I.e. if pcgpath is /xxx then /xxx/a is ok, /xxx2 is
	 * not.
	 */
	if (path[plen] == '/')
		return true;
	return false;
}