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
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This module contains functions used to bring up and tear down the
* Virtual Platform: [un]mounting file-systems, [un]plumbing network
* interfaces, [un]configuring devices, establishing resource controls,
* and creating/destroying the zone in the kernel. These actions, on
* the way up, ready the zone; on the way down, they halt the zone.
* See the much longer block comment at the beginning of zoneadmd.c
* for a bigger picture of how the whole program functions.
*
* This module also has primary responsibility for the layout of "scratch
* zones." These are mounted, but inactive, zones that are used during
* operating system upgrade and potentially other administrative action. The
* scratch zone environment is similar to the miniroot environment. The zone's
* actual root is mounted read-write on /a, and the standard paths (/usr,
* /sbin, /lib) all lead to read-only copies of the running system's binaries.
* This allows the administrative tools to manipulate the zone using "-R /a"
* without relying on any binaries in the zone itself.
*
* If the scratch zone is on an alternate root (Live Upgrade [LU] boot
* environment), then we must resolve the lofs mounts used there to uncover
* writable (unshared) resources. Shared resources, though, are always
* read-only. In addition, if the "same" zone with a different root path is
* currently running, then "/b" inside the zone points to the running zone's
* root. This allows LU to synchronize configuration files during the upgrade
* process.
*
* To construct this environment, this module creates a tmpfs mount on
* $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as
* described above is constructed on the fly. The zone is then created using
* $ZONEPATH/lu as the root.
*
* Note that scratch zones are inactive. The zone's bits are not running and
* likely cannot be run correctly until upgrade is done. Init is not running
* there, nor is SMF. Because of this, the "mounted" state of a scratch zone
* is not a part of the usual halt/ready/boot state machine.
*/
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sockio.h>
#include <sys/stropts.h>
#include <sys/conf.h>
#include <inet/tcp.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <net/route.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <rctl.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <wait.h>
#include <limits.h>
#include <libgen.h>
#include <libzfs.h>
#include <zone.h>
#include <assert.h>
#include <sys/mntio.h>
#include <sys/mnttab.h>
#include <sys/fs/autofs.h> /* for _autofssys() */
#include <sys/fs/lofs_info.h>
#include <sys/fs/zfs.h>
#include <pool.h>
#include <sys/pool.h>
#include <libzonecfg.h>
#include "zoneadmd.h"
#define V4_ADDR_LEN 32
#define V6_ADDR_LEN 128
/* 0755 is the default directory mode. */
#define DEFAULT_DIR_MODE \
(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
#define IPD_DEFAULT_OPTS \
MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
#define DFSTYPES "/etc/dfs/fstypes"
/*
* A list of directories which should be created.
*/
struct dir_info {
char *dir_name;
mode_t dir_mode;
};
/*
* The pathnames below are relative to the zonepath
*/
static struct dir_info dev_dirs[] = {
{ "/dev", 0755 },
{ "/dev/dsk", 0755 },
{ "/dev/fd", 0555 },
{ "/dev/pts", 0755 },
{ "/dev/rdsk", 0755 },
{ "/dev/rmt", 0755 },
{ "/dev/sad", 0755 },
{ "/dev/swap", 0755 },
{ "/dev/term", 0755 },
};
/*
* A list of devices which should be symlinked to /dev/zconsole.
*/
struct symlink_info {
char *sl_source;
char *sl_target;
};
/*
* The "source" paths are relative to the zonepath
*/
static struct symlink_info dev_symlinks[] = {
{ "/dev/stderr", "./fd/2" },
{ "/dev/stdin", "./fd/0" },
{ "/dev/stdout", "./fd/1" },
{ "/dev/dtremote", "/dev/null" },
{ "/dev/console", "zconsole" },
{ "/dev/syscon", "zconsole" },
{ "/dev/sysmsg", "zconsole" },
{ "/dev/systty", "zconsole" },
{ "/dev/msglog", "zconsole" },
};
/* for routing socket */
static int rts_seqno = 0;
/* mangled zone name when mounting in an alternate root environment */
static char kernzone[ZONENAME_MAX];
/* array of cached mount entries for resolve_lofs */
static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
/* from libsocket, not in any header file */
extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
/*
* An optimization for build_mnttable: reallocate (and potentially copy the
* data) only once every N times through the loop.
*/
#define MNTTAB_HUNK 32
/*
* Private autofs system call
*/
extern int _autofssys(int, void *);
static int
autofs_cleanup(zoneid_t zoneid)
{
/*
* Ask autofs to unmount all trigger nodes in the given zone.
*/
return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
}
static void
free_mnttable(struct mnttab *mnt_array, uint_t nelem)
{
uint_t i;
if (mnt_array == NULL)
return;
for (i = 0; i < nelem; i++) {
free(mnt_array[i].mnt_mountp);
free(mnt_array[i].mnt_fstype);
free(mnt_array[i].mnt_special);
free(mnt_array[i].mnt_mntopts);
assert(mnt_array[i].mnt_time == NULL);
}
free(mnt_array);
}
/*
* Build the mount table for the zone rooted at "zroot", storing the resulting
* array of struct mnttabs in "mnt_arrayp" and the number of elements in the
* array in "nelemp".
*/
static int
build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
struct mnttab **mnt_arrayp, uint_t *nelemp)
{
struct mnttab mnt;
struct mnttab *mnts;
struct mnttab *mnp;
uint_t nmnt;
rewind(mnttab);
resetmnttab(mnttab);
nmnt = 0;
mnts = NULL;
while (getmntent(mnttab, &mnt) == 0) {
struct mnttab *tmp_array;
if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
continue;
if (nmnt % MNTTAB_HUNK == 0) {
tmp_array = realloc(mnts,
(nmnt + MNTTAB_HUNK) * sizeof (*mnts));
if (tmp_array == NULL) {
free_mnttable(mnts, nmnt);
return (-1);
}
mnts = tmp_array;
}
mnp = &mnts[nmnt++];
/*
* Zero out any fields we're not using.
*/
(void) memset(mnp, 0, sizeof (*mnp));
if (mnt.mnt_special != NULL)
mnp->mnt_special = strdup(mnt.mnt_special);
if (mnt.mnt_mntopts != NULL)
mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
mnp->mnt_mountp = strdup(mnt.mnt_mountp);
mnp->mnt_fstype = strdup(mnt.mnt_fstype);
if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
(mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
free_mnttable(mnts, nmnt);
return (-1);
}
}
*mnt_arrayp = mnts;
*nelemp = nmnt;
return (0);
}
/*
* This is an optimization. The resolve_lofs function is used quite frequently
* to manipulate file paths, and on a machine with a large number of zones,
* there will be a huge number of mounted file systems. Thus, we trigger a
* reread of the list of mount points
*/
static void
lofs_discard_mnttab(void)
{
free_mnttable(resolve_lofs_mnts,
resolve_lofs_mnt_max - resolve_lofs_mnts);
resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
}
static int
lofs_read_mnttab(zlog_t *zlogp)
{
FILE *mnttab;
uint_t nmnts;
if ((mnttab = fopen(MNTTAB, "r")) == NULL)
return (-1);
if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
&nmnts) == -1) {
(void) fclose(mnttab);
return (-1);
}
(void) fclose(mnttab);
resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
return (0);
}
/*
* This function loops over potential loopback mounts and symlinks in a given
* path and resolves them all down to an absolute path.
*/
static void
resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
{
int len, arlen;
const char *altroot;
char tmppath[MAXPATHLEN];
boolean_t outside_altroot;
if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
return;
tmppath[len] = '\0';
(void) strlcpy(path, tmppath, sizeof (tmppath));
/* This happens once per zoneadmd operation. */
if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
return;
altroot = zonecfg_get_root();
arlen = strlen(altroot);
outside_altroot = B_FALSE;
for (;;) {
struct mnttab *mnp;
for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
mnp++) {
if (mnp->mnt_fstype == NULL ||
mnp->mnt_mountp == NULL ||
mnp->mnt_special == NULL ||
strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
continue;
len = strlen(mnp->mnt_mountp);
if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
(path[len] == '/' || path[len] == '\0'))
break;
}
if (mnp >= resolve_lofs_mnt_max)
break;
if (outside_altroot) {
char *cp;
int olen = sizeof (MNTOPT_RO) - 1;
/*
* If we run into a read-only mount outside of the
* alternate root environment, then the user doesn't
* want this path to be made read-write.
*/
if (mnp->mnt_mntopts != NULL &&
(cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
NULL &&
(cp == mnp->mnt_mntopts || cp[-1] == ',') &&
(cp[olen] == '\0' || cp[olen] == ',')) {
break;
}
} else if (arlen > 0 &&
(strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
(mnp->mnt_special[arlen] != '\0' &&
mnp->mnt_special[arlen] != '/'))) {
outside_altroot = B_TRUE;
}
/* use temporary buffer because new path might be longer */
(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
mnp->mnt_special, path + len);
if ((len = resolvepath(tmppath, path, pathlen)) == -1)
break;
path[len] = '\0';
}
}
/*
* For a regular mount, check if a replacement lofs mount is needed because the
* referenced device is already mounted somewhere.
*/
static int
check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
{
struct mnttab *mnp;
zone_fsopt_t *optptr, *onext;
/* This happens once per zoneadmd operation. */
if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
return (-1);
/*
* If this special node isn't already in use, then it's ours alone;
* no need to worry about conflicting mounts.
*/
for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
mnp++) {
if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
break;
}
if (mnp >= resolve_lofs_mnt_max)
return (0);
/*
* Convert this duplicate mount into a lofs mount.
*/
(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
sizeof (fsptr->zone_fs_special));
(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
sizeof (fsptr->zone_fs_type));
fsptr->zone_fs_raw[0] = '\0';
/*
* Discard all but one of the original options and set that to be the
* same set of options used for inherit package directory resources.
*/
optptr = fsptr->zone_fs_options;
if (optptr == NULL) {
optptr = malloc(sizeof (*optptr));
if (optptr == NULL) {
zerror(zlogp, B_TRUE, "cannot mount %s",
fsptr->zone_fs_dir);
return (-1);
}
} else {
while ((onext = optptr->zone_fsopt_next) != NULL) {
optptr->zone_fsopt_next = onext->zone_fsopt_next;
free(onext);
}
}
(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
optptr->zone_fsopt_next = NULL;
fsptr->zone_fs_options = optptr;
return (0);
}
static int
make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode)
{
char path[MAXPATHLEN];
struct stat st;
if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
sizeof (path)) {
zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
subdir);
return (-1);
}
if (lstat(path, &st) == 0) {
/*
* We don't check the file mode since presumably the zone
* administrator may have had good reason to change the mode,
* and we don't need to second guess him.
*/
if (!S_ISDIR(st.st_mode)) {
zerror(zlogp, B_FALSE, "%s is not a directory", path);
return (-1);
}
} else if (mkdirp(path, mode) != 0) {
if (errno == EROFS)
zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
"a read-only file system in this local zone.\nMake "
"sure %s exists in the global zone.", path, subdir);
else
zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
return (-1);
}
return (0);
}
/*
* Make /dev and various directories underneath it.
*/
static int
make_dev_dirs(zlog_t *zlogp, const char *zonepath)
{
int i;
for (i = 0; i < sizeof (dev_dirs) / sizeof (struct dir_info); i++) {
if (make_one_dir(zlogp, zonepath, dev_dirs[i].dir_name,
dev_dirs[i].dir_mode) != 0)
return (-1);
}
return (0);
}
/*
* Make various sym-links underneath /dev.
*/
static int
make_dev_links(zlog_t *zlogp, char *zonepath)
{
int i;
for (i = 0; i < sizeof (dev_symlinks) / sizeof (struct symlink_info);
i++) {
char dev[MAXPATHLEN];
struct stat st;
(void) snprintf(dev, sizeof (dev), "%s%s", zonepath,
dev_symlinks[i].sl_source);
if (lstat(dev, &st) == 0) {
/*
* Try not to call unlink(2) on directories, since that
* makes UFS unhappy.
*/
if (S_ISDIR(st.st_mode)) {
zerror(zlogp, B_FALSE, "symlink path %s is a "
"directory", dev_symlinks[i].sl_source);
return (-1);
}
(void) unlink(dev);
}
if (symlink(dev_symlinks[i].sl_target, dev) != 0) {
zerror(zlogp, B_TRUE, "could not setup %s->%s symlink",
dev_symlinks[i].sl_source,
dev_symlinks[i].sl_target);
return (-1);
}
}
return (0);
}
/*
* Create various directories and sym-links under /dev.
*/
static int
create_dev_files(zlog_t *zlogp)
{
char zonepath[MAXPATHLEN];
if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
zerror(zlogp, B_TRUE, "unable to determine zone root");
return (-1);
}
if (zonecfg_in_alt_root())
resolve_lofs(zlogp, zonepath, sizeof (zonepath));
if (make_dev_dirs(zlogp, zonepath) != 0)
return (-1);
if (make_dev_links(zlogp, zonepath) != 0)
return (-1);
return (0);
}
static void
free_remote_fstypes(char **types)
{
uint_t i;
if (types == NULL)
return;
for (i = 0; types[i] != NULL; i++)
free(types[i]);
free(types);
}
static char **
get_remote_fstypes(zlog_t *zlogp)
{
char **types = NULL;
FILE *fp;
char buf[MAXPATHLEN];
char fstype[MAXPATHLEN];
uint_t lines = 0;
uint_t i;
if ((fp = fopen(DFSTYPES, "r")) == NULL) {
zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
return (NULL);
}
/*
* Count the number of lines
*/
while (fgets(buf, sizeof (buf), fp) != NULL)
lines++;
if (lines == 0) /* didn't read anything; empty file */
goto out;
rewind(fp);
/*
* Allocate enough space for a NULL-terminated array.
*/
types = calloc(lines + 1, sizeof (char *));
if (types == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
goto out;
}
i = 0;
while (fgets(buf, sizeof (buf), fp) != NULL) {
/* LINTED - fstype is big enough to hold buf */
if (sscanf(buf, "%s", fstype) == 0) {
zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
free_remote_fstypes(types);
types = NULL;
goto out;
}
types[i] = strdup(fstype);
if (types[i] == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
free_remote_fstypes(types);
types = NULL;
goto out;
}
i++;
}
out:
(void) fclose(fp);
return (types);
}
static boolean_t
is_remote_fstype(const char *fstype, char *const *remote_fstypes)
{
uint_t i;
if (remote_fstypes == NULL)
return (B_FALSE);
for (i = 0; remote_fstypes[i] != NULL; i++) {
if (strcmp(remote_fstypes[i], fstype) == 0)
return (B_TRUE);
}
return (B_FALSE);
}
/*
* This converts a zone root path (normally of the form .../root) to a Live
* Upgrade scratch zone root (of the form .../lu).
*/
static void
root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
{
if (!isresolved && zonecfg_in_alt_root())
resolve_lofs(zlogp, zroot, zrootlen);
(void) strcpy(strrchr(zroot, '/') + 1, "lu");
}
/*
* The general strategy for unmounting filesystems is as follows:
*
* - Remote filesystems may be dead, and attempting to contact them as
* part of a regular unmount may hang forever; we want to always try to
* forcibly unmount such filesystems and only fall back to regular
* unmounts if the filesystem doesn't support forced unmounts.
*
* - We don't want to unnecessarily corrupt metadata on local
* filesystems (ie UFS), so we want to start off with graceful unmounts,
* and only escalate to doing forced unmounts if we get stuck.
*
* We start off walking backwards through the mount table. This doesn't
* give us strict ordering but ensures that we try to unmount submounts
* first. We thus limit the number of failed umount2(2) calls.
*
* The mechanism for determining if we're stuck is to count the number
* of failed unmounts each iteration through the mount table. This
* gives us an upper bound on the number of filesystems which remain
* mounted (autofs trigger nodes are dealt with separately). If at the
* end of one unmount+autofs_cleanup cycle we still have the same number
* of mounts that we started out with, we're stuck and try a forced
* unmount. If that fails (filesystem doesn't support forced unmounts)
* then we bail and are unable to teardown the zone. If it succeeds,
* we're no longer stuck so we continue with our policy of trying
* graceful mounts first.
*
* Zone must be down (ie, no processes or threads active).
*/
static int
unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
{
int error = 0;
FILE *mnttab;
struct mnttab *mnts;
uint_t nmnt;
char zroot[MAXPATHLEN + 1];
size_t zrootlen;
uint_t oldcount = UINT_MAX;
boolean_t stuck = B_FALSE;
char **remote_fstypes = NULL;
if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
zerror(zlogp, B_FALSE, "unable to determine zone root");
return (-1);
}
if (unmount_cmd)
root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
(void) strcat(zroot, "/");
zrootlen = strlen(zroot);
if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
return (-1);
}
/*
* Use our hacky mntfs ioctl so we see everything, even mounts with
* MS_NOMNTTAB.
*/
if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
error++;
goto out;
}
/*
* Build the list of remote fstypes so we know which ones we
* should forcibly unmount.
*/
remote_fstypes = get_remote_fstypes(zlogp);
for (; /* ever */; ) {
uint_t newcount = 0;
boolean_t unmounted;
struct mnttab *mnp;
char *path;
uint_t i;
mnts = NULL;
nmnt = 0;
/*
* MNTTAB gives us a way to walk through mounted
* filesystems; we need to be able to walk them in
* reverse order, so we build a list of all mounted
* filesystems.
*/
if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
&nmnt) != 0) {
error++;
goto out;
}
for (i = 0; i < nmnt; i++) {
mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
path = mnp->mnt_mountp;
unmounted = B_FALSE;
/*
* Try forced unmount first for remote filesystems.
*
* Not all remote filesystems support forced unmounts,
* so if this fails (ENOTSUP) we'll continue on
* and try a regular unmount.
*/
if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
if (umount2(path, MS_FORCE) == 0)
unmounted = B_TRUE;
}
/*
* Try forced unmount if we're stuck.
*/
if (stuck) {
if (umount2(path, MS_FORCE) == 0) {
unmounted = B_TRUE;
stuck = B_FALSE;
} else {
/*
* The first failure indicates a
* mount we won't be able to get
* rid of automatically, so we
* bail.
*/
error++;
zerror(zlogp, B_FALSE,
"unable to unmount '%s'", path);
free_mnttable(mnts, nmnt);
goto out;
}
}
/*
* Try regular unmounts for everything else.
*/
if (!unmounted && umount2(path, 0) != 0)
newcount++;
}
free_mnttable(mnts, nmnt);
if (newcount == 0)
break;
if (newcount >= oldcount) {
/*
* Last round didn't unmount anything; we're stuck and
* should start trying forced unmounts.
*/
stuck = B_TRUE;
}
oldcount = newcount;
/*
* Autofs doesn't let you unmount its trigger nodes from
* userland so we have to tell the kernel to cleanup for us.
*/
if (autofs_cleanup(zoneid) != 0) {
zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
error++;
goto out;
}
}
out:
free_remote_fstypes(remote_fstypes);
(void) fclose(mnttab);
return (error ? -1 : 0);
}
static int
fs_compare(const void *m1, const void *m2)
{
struct zone_fstab *i = (struct zone_fstab *)m1;
struct zone_fstab *j = (struct zone_fstab *)m2;
return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
}
/*
* Fork and exec (and wait for) the mentioned binary with the provided
* arguments. Returns (-1) if something went wrong with fork(2) or exec(2),
* returns the exit status otherwise.
*
* If we were unable to exec the provided pathname (for whatever
* reason), we return the special token ZEXIT_EXEC. The current value
* of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
* consumers of this function; any future consumers must make sure this
* remains the case.
*/
static int
forkexec(zlog_t *zlogp, const char *path, char *const argv[])
{
pid_t child_pid;
int child_status = 0;
/*
* Do not let another thread localize a message while we are forking.
*/
(void) mutex_lock(&msglock);
child_pid = fork();
(void) mutex_unlock(&msglock);
if (child_pid == -1) {
zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
return (-1);
} else if (child_pid == 0) {
closefrom(0);
(void) execv(path, argv);
/*
* Since we are in the child, there is no point calling zerror()
* since there is nobody waiting to consume it. So exit with a
* special code that the parent will recognize and call zerror()
* accordingly.
*/
_exit(ZEXIT_EXEC);
} else {
(void) waitpid(child_pid, &child_status, 0);
}
if (WIFSIGNALED(child_status)) {
zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
"signal %d", path, WTERMSIG(child_status));
return (-1);
}
assert(WIFEXITED(child_status));
if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
zerror(zlogp, B_FALSE, "failed to exec %s", path);
return (-1);
}
return (WEXITSTATUS(child_status));
}
static int
dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
{
char cmdbuf[MAXPATHLEN];
char *argv[4];
int status;
/*
* We could alternatively have called /usr/sbin/fsck -F <fstype>, but
* that would cost us an extra fork/exec without buying us anything.
*/
if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
> sizeof (cmdbuf)) {
zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
return (-1);
}
argv[0] = "fsck";
argv[1] = "-m";
argv[2] = (char *)rawdev;
argv[3] = NULL;
status = forkexec(zlogp, cmdbuf, argv);
if (status == 0 || status == -1)
return (status);
zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
"run fsck manually", rawdev, status);
return (-1);
}
static int
domount(zlog_t *zlogp, const char *fstype, const char *opts,
const char *special, const char *directory)
{
char cmdbuf[MAXPATHLEN];
char *argv[6];
int status;
/*
* We could alternatively have called /usr/sbin/mount -F <fstype>, but
* that would cost us an extra fork/exec without buying us anything.
*/
if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
> sizeof (cmdbuf)) {
zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
return (-1);
}
argv[0] = "mount";
if (opts[0] == '\0') {
argv[1] = (char *)special;
argv[2] = (char *)directory;
argv[3] = NULL;
} else {
argv[1] = "-o";
argv[2] = (char *)opts;
argv[3] = (char *)special;
argv[4] = (char *)directory;
argv[5] = NULL;
}
status = forkexec(zlogp, cmdbuf, argv);
if (status == 0 || status == -1)
return (status);
if (opts[0] == '\0')
zerror(zlogp, B_FALSE, "\"%s %s %s\" "
"failed with exit code %d",
cmdbuf, special, directory, status);
else
zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
"failed with exit code %d",
cmdbuf, opts, special, directory, status);
return (-1);
}
/*
* Make sure if a given path exists, it is not a sym-link, and is a directory.
*/
static int
check_path(zlog_t *zlogp, const char *path)
{
struct stat statbuf;
char respath[MAXPATHLEN];
int res;
if (lstat(path, &statbuf) != 0) {
if (errno == ENOENT)
return (0);
zerror(zlogp, B_TRUE, "can't stat %s", path);
return (-1);
}
if (S_ISLNK(statbuf.st_mode)) {
zerror(zlogp, B_FALSE, "%s is a symlink", path);
return (-1);
}
if (!S_ISDIR(statbuf.st_mode)) {
zerror(zlogp, B_FALSE, "%s is not a directory", path);
return (-1);
}
if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
return (-1);
}
respath[res] = '\0';
if (strcmp(path, respath) != 0) {
/*
* We don't like ".."s and "."s throwing us off
*/
zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
return (-1);
}
return (0);
}
/*
* Check every component of rootpath/relpath. If any component fails (ie,
* exists but isn't the canonical path to a directory), it is returned in
* badpath, which is assumed to be at least of size MAXPATHLEN.
*
* Relpath must begin with '/'.
*/
static boolean_t
valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *relpath)
{
char abspath[MAXPATHLEN], *slashp;
/*
* Make sure abspath has at least one '/' after its rootpath
* component, and ends with '/'.
*/
if (snprintf(abspath, sizeof (abspath), "%s%s/", rootpath, relpath) >
sizeof (abspath)) {
zerror(zlogp, B_FALSE, "pathname %s%s is too long", rootpath,
relpath);
return (B_FALSE);
}
slashp = &abspath[strlen(rootpath)];
assert(*slashp == '/');
do {
*slashp = '\0';
if (check_path(zlogp, abspath) != 0)
return (B_FALSE);
*slashp = '/';
slashp++;
} while ((slashp = strchr(slashp, '/')) != NULL);
return (B_TRUE);
}
static int
mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath)
{
char path[MAXPATHLEN];
char specpath[MAXPATHLEN];
char optstr[MAX_MNTOPT_STR];
zone_fsopt_t *optptr;
if (!valid_mount_path(zlogp, rootpath, fsptr->zone_fs_dir)) {
zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
rootpath, fsptr->zone_fs_dir);
return (-1);
}
if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
DEFAULT_DIR_MODE) != 0)
return (-1);
(void) snprintf(path, sizeof (path), "%s%s", rootpath,
fsptr->zone_fs_dir);
if (strlen(fsptr->zone_fs_special) == 0) {
/*
* A zero-length special is how we distinguish IPDs from
* general-purpose FSs. Make sure it mounts from a place that
* can be seen via the alternate zone's root.
*/
if (snprintf(specpath, sizeof (specpath), "%s%s",
zonecfg_get_root(), fsptr->zone_fs_dir) >=
sizeof (specpath)) {
zerror(zlogp, B_FALSE, "cannot mount %s: path too "
"long in alternate root", fsptr->zone_fs_dir);
return (-1);
}
if (zonecfg_in_alt_root())
resolve_lofs(zlogp, specpath, sizeof (specpath));
if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
specpath, path) != 0) {
zerror(zlogp, B_TRUE, "failed to loopback mount %s",
specpath);
return (-1);
}
return (0);
}
/*
* In general the strategy here is to do just as much verification as
* necessary to avoid crashing or otherwise doing something bad; if the
* administrator initiated the operation via zoneadm(1m), he'll get
* auto-verification which will let him know what's wrong. If he
* modifies the zone configuration of a running zone and doesn't attempt
* to verify that it's OK we won't crash but won't bother trying to be
* too helpful either. zoneadm verify is only a couple keystrokes away.
*/
if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
"invalid file-system type %s", fsptr->zone_fs_special,
fsptr->zone_fs_dir, fsptr->zone_fs_type);
return (-1);
}
/*
* If we're looking at an alternate root environment, then construct
* read-only loopback mounts as necessary. For all lofs mounts, make
* sure that the 'special' entry points inside the alternate root. (We
* don't do this with other mounts, as devfs isn't in the alternate
* root, and we need to assume the device environment is roughly the
* same.)
*/
if (zonecfg_in_alt_root()) {
struct stat64 st;
if (stat64(fsptr->zone_fs_special, &st) != -1 &&
S_ISBLK(st.st_mode) &&
check_lofs_needed(zlogp, fsptr) == -1)
return (-1);
if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
if (snprintf(specpath, sizeof (specpath), "%s%s",
zonecfg_get_root(), fsptr->zone_fs_special) >=
sizeof (specpath)) {
zerror(zlogp, B_FALSE, "cannot mount %s: path "
"too long in alternate root",
fsptr->zone_fs_special);
return (-1);
}
resolve_lofs(zlogp, specpath, sizeof (specpath));
(void) strlcpy(fsptr->zone_fs_special, specpath,
sizeof (fsptr->zone_fs_special));
}
}
/*
* Run 'fsck -m' if there's a device to fsck.
*/
if (fsptr->zone_fs_raw[0] != '\0' &&
dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0)
return (-1);
/*
* Build up mount option string.
*/
optstr[0] = '\0';
if (fsptr->zone_fs_options != NULL) {
(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
sizeof (optstr));
for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
optptr != NULL; optptr = optptr->zone_fsopt_next) {
(void) strlcat(optstr, ",", sizeof (optstr));
(void) strlcat(optstr, optptr->zone_fsopt_opt,
sizeof (optstr));
}
}
return (domount(zlogp, fsptr->zone_fs_type, optstr,
fsptr->zone_fs_special, path));
}
static void
free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
{
uint_t i;
if (fsarray == NULL)
return;
for (i = 0; i < nelem; i++)
zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
free(fsarray);
}
/*
* This function constructs the miniroot-like "scratch zone" environment. If
* it returns B_FALSE, then the error has already been logged.
*/
static boolean_t
build_mounted(zlog_t *zlogp, char *rootpath, size_t rootlen,
const char *zonepath)
{
char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
char luroot[MAXPATHLEN];
const char **cpp;
static const char *mkdirs[] = {
"/system", "/system/contract", "/proc", "/dev", "/tmp",
"/a", NULL
};
static const char *localdirs[] = {
"/etc", "/var", NULL
};
static const char *loopdirs[] = {
"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
"/usr", NULL
};
static const char *tmpdirs[] = {
"/tmp", "/var/run", NULL
};
FILE *fp;
struct stat st;
char *altstr;
uuid_t uuid;
/*
* Construct a small Solaris environment, including the zone root
* mounted on '/a' inside that environment.
*/
resolve_lofs(zlogp, rootpath, rootlen);
(void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath);
resolve_lofs(zlogp, luroot, sizeof (luroot));
(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
(void) symlink("./usr/bin", tmp);
/*
* These are mostly special mount points; not handled here. (See
* zone_mount_early.)
*/
for (cpp = mkdirs; *cpp != NULL; cpp++) {
(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
if (mkdir(tmp, 0755) != 0) {
zerror(zlogp, B_TRUE, "cannot create %s", tmp);
return (B_FALSE);
}
}
/*
* These are mounted read-write from the zone undergoing upgrade. We
* must be careful not to 'leak' things from the main system into the
* zone, and this accomplishes that goal.
*/
for (cpp = localdirs; *cpp != NULL; cpp++) {
(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
(void) snprintf(fromdir, sizeof (fromdir), "%s%s", rootpath,
*cpp);
if (mkdir(tmp, 0755) != 0) {
zerror(zlogp, B_TRUE, "cannot create %s", tmp);
return (B_FALSE);
}
if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) != 0) {
zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
*cpp);
return (B_FALSE);
}
}
/*
* These are things mounted read-only from the running system because
* they contain binaries that must match system.
*/
for (cpp = loopdirs; *cpp != NULL; cpp++) {
(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
if (mkdir(tmp, 0755) != 0) {
if (errno != EEXIST) {
zerror(zlogp, B_TRUE, "cannot create %s", tmp);
return (B_FALSE);
}
if (lstat(tmp, &st) != 0) {
zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
return (B_FALSE);
}
/*
* Ignore any non-directories encountered. These are
* things that have been converted into symlinks
* (/etc/fs and /etc/lib) and no longer need a lofs
* fixup.
*/
if (!S_ISDIR(st.st_mode))
continue;
}
if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
tmp) != 0) {
zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
*cpp);
return (B_FALSE);
}
}
/*
* These are things with tmpfs mounted inside.
*/
for (cpp = tmpdirs; *cpp != NULL; cpp++) {
(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
zerror(zlogp, B_TRUE, "cannot create %s", tmp);
return (B_FALSE);
}
if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
return (B_FALSE);
}
}
/*
* This is here to support lucopy. If there's an instance of this same
* zone on the current running system, then we mount its root up as
* read-only inside the scratch zone.
*/
(void) zonecfg_get_uuid(zone_name, uuid);
altstr = strdup(zonecfg_get_root());
if (altstr == NULL) {
zerror(zlogp, B_TRUE, "out of memory");
return (B_FALSE);
}
zonecfg_set_root("");
(void) strlcpy(tmp, zone_name, sizeof (tmp));
(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
strcmp(fromdir, rootpath) != 0) {
(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
if (mkdir(tmp, 0755) != 0) {
zerror(zlogp, B_TRUE, "cannot create %s", tmp);
return (B_FALSE);
}
if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
tmp) != 0) {
zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
fromdir);
return (B_FALSE);
}
}
zonecfg_set_root(altstr);
free(altstr);
if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
zerror(zlogp, B_TRUE, "cannot open zone mapfile");
return (B_FALSE);
}
(void) ftruncate(fileno(fp), 0);
if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
}
zonecfg_close_scratch(fp);
(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
return (B_FALSE);
(void) strlcpy(rootpath, tmp, rootlen);
return (B_TRUE);
}
static int
mount_filesystems(zlog_t *zlogp, boolean_t mount_cmd)
{
char rootpath[MAXPATHLEN];
char zonepath[MAXPATHLEN];
int num_fs = 0, i;
struct zone_fstab fstab, *fs_ptr = NULL, *tmp_ptr;
struct zone_fstab *fsp;
zone_dochandle_t handle = NULL;
zone_state_t zstate;
if (zone_get_state(zone_name, &zstate) != Z_OK ||
(zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
zerror(zlogp, B_FALSE,
"zone must be in '%s' or '%s' state to mount file-systems",
zone_state_str(ZONE_STATE_READY),
zone_state_str(ZONE_STATE_MOUNTED));
goto bad;
}
if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
zerror(zlogp, B_TRUE, "unable to determine zone path");
goto bad;
}
if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
zerror(zlogp, B_TRUE, "unable to determine zone root");
goto bad;
}
if ((handle = zonecfg_init_handle()) == NULL) {
zerror(zlogp, B_TRUE,
"could not get zone configuration handle");
goto bad;
}
if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
zonecfg_setfsent(handle) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
goto bad;
}
/*
* /dev in the zone is loopback'd from the external /dev repository,
* in order to provide a largely read-only semantic. But because
* processes in the zone need to be able to chown, chmod, etc. zone
* /dev files, we can't use a 'ro' lofs mount. Instead we use a
* special mode just for zones, "zonedevfs".
*
* In the future we should front /dev with a full-fledged filesystem.
*/
num_fs++;
if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
num_fs--;
goto bad;
}
fs_ptr = tmp_ptr;
fsp = &fs_ptr[num_fs - 1];
/*
* Note that mount_one will prepend the alternate root to
* zone_fs_special and do the necessary resolution, so all that is
* needed here is to strip the root added by zone_get_zonepath.
*/
(void) strlcpy(fsp->zone_fs_dir, "/dev", sizeof (fsp->zone_fs_dir));
(void) snprintf(fsp->zone_fs_special, sizeof (fsp->zone_fs_special),
"%s/dev", zonepath + strlen(zonecfg_get_root()));
fsp->zone_fs_raw[0] = '\0';
(void) strlcpy(fsp->zone_fs_type, MNTTYPE_LOFS,
sizeof (fsp->zone_fs_type));
fsp->zone_fs_options = NULL;
if (zonecfg_add_fs_option(fsp, MNTOPT_LOFS_ZONEDEVFS) != Z_OK) {
zerror(zlogp, B_FALSE, "error adding property");
goto bad;
}
/*
* Iterate through the rest of the filesystems, first the IPDs, then
* the general FSs. Sort them all, then mount them in sorted order.
* This is to make sure the higher level directories (e.g., /usr)
* get mounted before any beneath them (e.g., /usr/local).
*/
if (zonecfg_setipdent(handle) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
goto bad;
}
while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
num_fs++;
if ((tmp_ptr = realloc(fs_ptr,
num_fs * sizeof (*tmp_ptr))) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
num_fs--;
(void) zonecfg_endipdent(handle);
goto bad;
}
fs_ptr = tmp_ptr;
fsp = &fs_ptr[num_fs - 1];
/*
* IPDs logically only have a mount point; all other properties
* are implied.
*/
(void) strlcpy(fsp->zone_fs_dir,
fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
fsp->zone_fs_special[0] = '\0';
fsp->zone_fs_raw[0] = '\0';
fsp->zone_fs_type[0] = '\0';
fsp->zone_fs_options = NULL;
}
(void) zonecfg_endipdent(handle);
if (zonecfg_setfsent(handle) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
goto bad;
}
while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
/*
* ZFS filesystems will not be accessible under an alternate
* root, since the pool will not be known. Ignore them in this
* case.
*/
if (mount_cmd && strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
continue;
num_fs++;
if ((tmp_ptr = realloc(fs_ptr,
num_fs * sizeof (*tmp_ptr))) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
num_fs--;
(void) zonecfg_endfsent(handle);
goto bad;
}
fs_ptr = tmp_ptr;
fsp = &fs_ptr[num_fs - 1];
(void) strlcpy(fsp->zone_fs_dir,
fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
(void) strlcpy(fsp->zone_fs_special, fstab.zone_fs_special,
sizeof (fsp->zone_fs_special));
(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
sizeof (fsp->zone_fs_raw));
(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
sizeof (fsp->zone_fs_type));
fsp->zone_fs_options = fstab.zone_fs_options;
}
(void) zonecfg_endfsent(handle);
zonecfg_fini_handle(handle);
handle = NULL;
/*
* If we're mounting a zone for administration, then we need to set up
* the "/a" environment inside the zone so that the commands that run
* in there have access to both the running system's utilities and the
* to-be-modified zone's files.
*/
if (mount_cmd &&
!build_mounted(zlogp, rootpath, sizeof (rootpath), zonepath))
goto bad;
qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
for (i = 0; i < num_fs; i++) {
if (mount_cmd && strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
size_t slen = strlen(rootpath) - 2;
/* /dev is special and always goes at the top */
rootpath[slen] = '\0';
if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
goto bad;
rootpath[slen] = '/';
continue;
}
if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
goto bad;
}
free_fs_data(fs_ptr, num_fs);
/*
* Everything looks fine.
*/
return (0);
bad:
if (handle != NULL)
zonecfg_fini_handle(handle);
free_fs_data(fs_ptr, num_fs);
return (-1);
}
/* caller makes sure neither parameter is NULL */
static int
addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
{
int prefixlen;
prefixlen = atoi(prefixstr);
if (prefixlen < 0 || prefixlen > maxprefixlen)
return (1);
while (prefixlen > 0) {
if (prefixlen >= 8) {
*maskstr++ = 0xFF;
prefixlen -= 8;
continue;
}
*maskstr |= 1 << (8 - prefixlen);
prefixlen--;
}
return (0);
}
/*
* Tear down all interfaces belonging to the given zone. This should
* be called with the zone in a state other than "running", so that
* interfaces can't be assigned to the zone after this returns.
*
* If anything goes wrong, log an error message and return an error.
*/
static int
unconfigure_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
{
struct lifnum lifn;
struct lifconf lifc;
struct lifreq *lifrp, lifrl;
int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
int num_ifs, s, i, ret_code = 0;
uint_t bufsize;
char *buf = NULL;
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
zerror(zlogp, B_TRUE, "could not get socket");
ret_code = -1;
goto bad;
}
lifn.lifn_family = AF_UNSPEC;
lifn.lifn_flags = (int)lifc_flags;
if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
zerror(zlogp, B_TRUE,
"could not determine number of interfaces");
ret_code = -1;
goto bad;
}
num_ifs = lifn.lifn_count;
bufsize = num_ifs * sizeof (struct lifreq);
if ((buf = malloc(bufsize)) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
ret_code = -1;
goto bad;
}
lifc.lifc_family = AF_UNSPEC;
lifc.lifc_flags = (int)lifc_flags;
lifc.lifc_len = bufsize;
lifc.lifc_buf = buf;
if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
zerror(zlogp, B_TRUE, "could not get configured interfaces");
ret_code = -1;
goto bad;
}
lifrp = lifc.lifc_req;
for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
(void) close(s);
if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
0) {
zerror(zlogp, B_TRUE, "%s: could not get socket",
lifrl.lifr_name);
ret_code = -1;
continue;
}
(void) memset(&lifrl, 0, sizeof (lifrl));
(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
sizeof (lifrl.lifr_name));
if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
zerror(zlogp, B_TRUE,
"%s: could not determine zone interface belongs to",
lifrl.lifr_name);
ret_code = -1;
continue;
}
if (lifrl.lifr_zoneid == zone_id) {
if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
zerror(zlogp, B_TRUE,
"%s: could not remove interface",
lifrl.lifr_name);
ret_code = -1;
continue;
}
}
}
bad:
if (s > 0)
(void) close(s);
if (buf)
free(buf);
return (ret_code);
}
static union sockunion {
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_dl sdl;
struct sockaddr_in6 sin6;
} so_dst, so_ifp;
static struct {
struct rt_msghdr hdr;
char space[512];
} rtmsg;
static int
salen(struct sockaddr *sa)
{
switch (sa->sa_family) {
case AF_INET:
return (sizeof (struct sockaddr_in));
case AF_LINK:
return (sizeof (struct sockaddr_dl));
case AF_INET6:
return (sizeof (struct sockaddr_in6));
default:
return (sizeof (struct sockaddr));
}
}
#define ROUNDUP_LONG(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
/*
* Look up which zone is using a given IP address. The address in question
* is expected to have been stuffed into the structure to which lifr points
* via a previous SIOCGLIFADDR ioctl().
*
* This is done using black router socket magic.
*
* Return the name of the zone on success or NULL on failure.
*
* This is a lot of code for a simple task; a new ioctl request to take care
* of this might be a useful RFE.
*/
static char *
who_is_using(zlog_t *zlogp, struct lifreq *lifr)
{
static char answer[ZONENAME_MAX];
pid_t pid;
int s, rlen, l, i;
char *cp = rtmsg.space;
struct sockaddr_dl *ifp = NULL;
struct sockaddr *sa;
char save_if_name[LIFNAMSIZ];
answer[0] = '\0';
pid = getpid();
if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
zerror(zlogp, B_TRUE, "could not get routing socket");
return (NULL);
}
if (lifr->lifr_addr.ss_family == AF_INET) {
struct sockaddr_in *sin4;
so_dst.sa.sa_family = AF_INET;
sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
so_dst.sin.sin_addr = sin4->sin_addr;
} else {
struct sockaddr_in6 *sin6;
so_dst.sa.sa_family = AF_INET6;
sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
so_dst.sin6.sin6_addr = sin6->sin6_addr;
}
so_ifp.sa.sa_family = AF_LINK;
(void) memset(&rtmsg, 0, sizeof (rtmsg));
rtmsg.hdr.rtm_type = RTM_GET;
rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
rtmsg.hdr.rtm_version = RTM_VERSION;
rtmsg.hdr.rtm_seq = ++rts_seqno;
rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
l = ROUNDUP_LONG(salen(&so_dst.sa));
(void) memmove(cp, &(so_dst), l);
cp += l;
l = ROUNDUP_LONG(salen(&so_ifp.sa));
(void) memmove(cp, &(so_ifp), l);
cp += l;
rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
if ((rlen = write(s, &rtmsg, l)) < 0) {
zerror(zlogp, B_TRUE, "writing to routing socket");
return (NULL);
} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
zerror(zlogp, B_TRUE,
"write to routing socket got only %d for len\n", rlen);
return (NULL);
}
do {
l = read(s, &rtmsg, sizeof (rtmsg));
} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
rtmsg.hdr.rtm_pid != pid));
if (l < 0) {
zerror(zlogp, B_TRUE, "reading from routing socket");
return (NULL);
}
if (rtmsg.hdr.rtm_version != RTM_VERSION) {
zerror(zlogp, B_FALSE,
"routing message version %d not understood",
rtmsg.hdr.rtm_version);
return (NULL);
}
if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
zerror(zlogp, B_FALSE, "message length mismatch, "
"expected %d bytes, returned %d bytes",
rtmsg.hdr.rtm_msglen, l);
return (NULL);
}
if (rtmsg.hdr.rtm_errno != 0) {
errno = rtmsg.hdr.rtm_errno;
zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
return (NULL);
}
if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
zerror(zlogp, B_FALSE, "interface not found");
return (NULL);
}
cp = ((char *)(&rtmsg.hdr + 1));
for (i = 1; i != 0; i <<= 1) {
/* LINTED E_BAD_PTR_CAST_ALIGN */
sa = (struct sockaddr *)cp;
if (i != RTA_IFP) {
if ((i & rtmsg.hdr.rtm_addrs) != 0)
cp += ROUNDUP_LONG(salen(sa));
continue;
}
if (sa->sa_family == AF_LINK &&
((struct sockaddr_dl *)sa)->sdl_nlen != 0)
ifp = (struct sockaddr_dl *)sa;
break;
}
if (ifp == NULL) {
zerror(zlogp, B_FALSE, "interface could not be determined");
return (NULL);
}
/*
* We need to set the I/F name to what we got above, then do the
* appropriate ioctl to get its zone name. But lifr->lifr_name is
* used by the calling function to do a REMOVEIF, so if we leave the
* "good" zone's I/F name in place, *that* I/F will be removed instead
* of the bad one. So we save the old (bad) I/F name before over-
* writing it and doing the ioctl, then restore it after the ioctl.
*/
(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
lifr->lifr_name[ifp->sdl_nlen] = '\0';
i = ioctl(s, SIOCGLIFZONE, lifr);
(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
if (i < 0) {
zerror(zlogp, B_TRUE,
"%s: could not determine the zone interface belongs to",
lifr->lifr_name);
return (NULL);
}
if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
(void) snprintf(answer, sizeof (answer), "%d",
lifr->lifr_zoneid);
if (strlen(answer) > 0)
return (answer);
return (NULL);
}
typedef struct mcast_rtmsg_s {
struct rt_msghdr m_rtm;
union {
struct {
struct sockaddr_in m_dst;
struct sockaddr_in m_gw;
struct sockaddr_in m_netmask;
} m_v4;
struct {
struct sockaddr_in6 m_dst;
struct sockaddr_in6 m_gw;
struct sockaddr_in6 m_netmask;
} m_v6;
} m_u;
} mcast_rtmsg_t;
#define m_dst4 m_u.m_v4.m_dst
#define m_dst6 m_u.m_v6.m_dst
#define m_gw4 m_u.m_v4.m_gw
#define m_gw6 m_u.m_v6.m_gw
#define m_netmask4 m_u.m_v4.m_netmask
#define m_netmask6 m_u.m_v6.m_netmask
/*
* Configures a single interface: a new virtual interface is added, based on
* the physical interface nwiftabptr->zone_nwif_physical, with the address
* specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that
* the "address" can be an IPv6 address (with a /prefixlength required), an
* IPv4 address (with a /prefixlength optional), or a name; for the latter,
* an IPv4 name-to-address resolution will be attempted.
*
* A default interface route for multicast is created on the first IPv4 and
* IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively.
* This should really be done in the init scripts if we ever allow zones to
* modify the routing tables.
*
* If anything goes wrong, we log an detailed error message, attempt to tear
* down whatever we set up and return an error.
*/
static int
configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp,
boolean_t *mcast_rt_v6_setp)
{
struct lifreq lifr;
struct sockaddr_in netmask4;
struct sockaddr_in6 netmask6;
struct in_addr in4;
struct in6_addr in6;
sa_family_t af;
char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
mcast_rtmsg_t mcast_rtmsg;
int s;
int rs;
int rlen;
boolean_t got_netmask = B_FALSE;
char addrstr4[INET_ADDRSTRLEN];
int res;
res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
if (res != Z_OK) {
zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
nwiftabptr->zone_nwif_address);
return (-1);
}
af = lifr.lifr_addr.ss_family;
if (af == AF_INET)
in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
else
in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr;
if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
zerror(zlogp, B_TRUE, "could not get socket");
return (-1);
}
(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
sizeof (lifr.lifr_name));
if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE, "%s: could not add interface",
lifr.lifr_name);
(void) close(s);
return (-1);
}
if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE,
"%s: could not set IP address to %s",
lifr.lifr_name, nwiftabptr->zone_nwif_address);
goto bad;
}
/* Preserve literal IPv4 address for later potential printing. */
if (af == AF_INET)
(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
lifr.lifr_zoneid = zone_id;
if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE, "%s: could not place interface into zone",
lifr.lifr_name);
goto bad;
}
if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
got_netmask = B_TRUE; /* default setting will be correct */
} else {
if (af == AF_INET) {
/*
* The IPv4 netmask can be determined either
* directly if a prefix length was supplied with
* the address or via the netmasks database. Not
* being able to determine it is a common failure,
* but it often is not fatal to operation of the
* interface. In that case, a warning will be
* printed after the rest of the interface's
* parameters have been configured.
*/
(void) memset(&netmask4, 0, sizeof (netmask4));
if (slashp != NULL) {
if (addr2netmask(slashp + 1, V4_ADDR_LEN,
(uchar_t *)&netmask4.sin_addr) != 0) {
*slashp = '/';
zerror(zlogp, B_FALSE,
"%s: invalid prefix length in %s",
lifr.lifr_name,
nwiftabptr->zone_nwif_address);
goto bad;
}
got_netmask = B_TRUE;
} else if (getnetmaskbyaddr(in4,
&netmask4.sin_addr) == 0) {
got_netmask = B_TRUE;
}
if (got_netmask) {
netmask4.sin_family = af;
(void) memcpy(&lifr.lifr_addr, &netmask4,
sizeof (netmask4));
}
} else {
(void) memset(&netmask6, 0, sizeof (netmask6));
if (addr2netmask(slashp + 1, V6_ADDR_LEN,
(uchar_t *)&netmask6.sin6_addr) != 0) {
*slashp = '/';
zerror(zlogp, B_FALSE,
"%s: invalid prefix length in %s",
lifr.lifr_name,
nwiftabptr->zone_nwif_address);
goto bad;
}
got_netmask = B_TRUE;
netmask6.sin6_family = af;
(void) memcpy(&lifr.lifr_addr, &netmask6,
sizeof (netmask6));
}
if (got_netmask &&
ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE, "%s: could not set netmask",
lifr.lifr_name);
goto bad;
}
/*
* This doesn't set the broadcast address at all. Rather, it
* gets, then sets the interface's address, relying on the fact
* that resetting the address will reset the broadcast address.
*/
if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE, "%s: could not get address",
lifr.lifr_name);
goto bad;
}
if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE,
"%s: could not reset broadcast address",
lifr.lifr_name);
goto bad;
}
}
if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE, "%s: could not get flags",
lifr.lifr_name);
goto bad;
}
lifr.lifr_flags |= IFF_UP;
if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
int save_errno = errno;
char *zone_using;
/*
* If we failed with something other than EADDRNOTAVAIL,
* then skip to the end. Otherwise, look up our address,
* then call a function to determine which zone is already
* using that address.
*/
if (errno != EADDRNOTAVAIL) {
zerror(zlogp, B_TRUE,
"%s: could not bring interface up", lifr.lifr_name);
goto bad;
}
if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
zerror(zlogp, B_TRUE, "%s: could not get address",
lifr.lifr_name);
goto bad;
}
zone_using = who_is_using(zlogp, &lifr);
errno = save_errno;
if (zone_using == NULL)
zerror(zlogp, B_TRUE,
"%s: could not bring interface up", lifr.lifr_name);
else
zerror(zlogp, B_TRUE, "%s: could not bring interface "
"up: address in use by zone '%s'", lifr.lifr_name,
zone_using);
goto bad;
}
if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET &&
mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) ||
(af == AF_INET6 &&
mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) {
rs = socket(PF_ROUTE, SOCK_RAW, 0);
if (rs < 0) {
zerror(zlogp, B_TRUE, "%s: could not create "
"routing socket", lifr.lifr_name);
goto bad;
}
(void) shutdown(rs, 0);
(void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t));
mcast_rtmsg.m_rtm.rtm_msglen = sizeof (struct rt_msghdr) +
3 * (af == AF_INET ? sizeof (struct sockaddr_in) :
sizeof (struct sockaddr_in6));
mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION;
mcast_rtmsg.m_rtm.rtm_type = RTM_ADD;
mcast_rtmsg.m_rtm.rtm_flags = RTF_UP;
mcast_rtmsg.m_rtm.rtm_addrs =
RTA_DST | RTA_GATEWAY | RTA_NETMASK;
mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno;
if (af == AF_INET) {
mcast_rtmsg.m_dst4.sin_family = AF_INET;
mcast_rtmsg.m_dst4.sin_addr.s_addr =
htonl(INADDR_UNSPEC_GROUP);
mcast_rtmsg.m_gw4.sin_family = AF_INET;
mcast_rtmsg.m_gw4.sin_addr = in4;
mcast_rtmsg.m_netmask4.sin_family = AF_INET;
mcast_rtmsg.m_netmask4.sin_addr.s_addr =
htonl(IN_CLASSD_NET);
} else {
mcast_rtmsg.m_dst6.sin6_family = AF_INET6;
mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU;
mcast_rtmsg.m_gw6.sin6_family = AF_INET6;
mcast_rtmsg.m_gw6.sin6_addr = in6;
mcast_rtmsg.m_netmask6.sin6_family = AF_INET6;
mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU;
}
rlen = write(rs, (char *)&mcast_rtmsg,
mcast_rtmsg.m_rtm.rtm_msglen);
if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) {
if (rlen < 0) {
zerror(zlogp, B_TRUE, "%s: could not set "
"default interface for multicast",
lifr.lifr_name);
} else {
zerror(zlogp, B_FALSE, "%s: write to routing "
"socket returned %d", lifr.lifr_name, rlen);
}
(void) close(rs);
goto bad;
}
if (af == AF_INET) {
*mcast_rt_v4_setp = B_TRUE;
} else {
*mcast_rt_v6_setp = B_TRUE;
}
(void) close(rs);
}
if (!got_netmask) {
/*
* A common, but often non-fatal problem, is that the system
* cannot find the netmask for an interface address. This is
* often caused by it being only in /etc/inet/netmasks, but
* /etc/nsswitch.conf says to use NIS or NIS+ and it's not
* in that. This doesn't show up at boot because the netmask
* is obtained from /etc/inet/netmasks when no network
* interfaces are up, but isn't consulted when NIS/NIS+ is
* available. We warn the user here that something like this
* has happened and we're just running with a default and
* possible incorrect netmask.
*/
char buffer[INET6_ADDRSTRLEN];
void *addr;
if (af == AF_INET)
addr = &((struct sockaddr_in *)
(&lifr.lifr_addr))->sin_addr;
else
addr = &((struct sockaddr_in6 *)
(&lifr.lifr_addr))->sin6_addr;
/* Find out what netmask interface is going to be using */
if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL)
goto bad;
zerror(zlogp, B_FALSE,
"WARNING: %s: no matching subnet found in netmasks(4) for "
"%s; using default of %s.",
lifr.lifr_name, addrstr4, buffer);
}
(void) close(s);
return (Z_OK);
bad:
(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
(void) close(s);
return (-1);
}
/*
* Sets up network interfaces based on information from the zone configuration.
* An IPv4 loopback interface is set up "for free", modeling the global system.
* If any of the configuration interfaces were IPv6, then an IPv6 loopback
* address is set up as well.
*
* If anything goes wrong, we log a general error message, attempt to tear down
* whatever we set up, and return an error.
*/
static int
configure_network_interfaces(zlog_t *zlogp)
{
zone_dochandle_t handle;
struct zone_nwiftab nwiftab, loopback_iftab;
boolean_t saw_v6 = B_FALSE;
boolean_t mcast_rt_v4_set = B_FALSE;
boolean_t mcast_rt_v6_set = B_FALSE;
zoneid_t zoneid;
if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
zerror(zlogp, B_TRUE, "unable to get zoneid");
return (-1);
}
if ((handle = zonecfg_init_handle()) == NULL) {
zerror(zlogp, B_TRUE, "getting zone configuration handle");
return (-1);
}
if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
zonecfg_fini_handle(handle);
return (-1);
}
if (zonecfg_setnwifent(handle) == Z_OK) {
for (;;) {
struct in6_addr in6;
if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
break;
if (configure_one_interface(zlogp, zoneid,
&nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) !=
Z_OK) {
(void) zonecfg_endnwifent(handle);
zonecfg_fini_handle(handle);
return (-1);
}
if (inet_pton(AF_INET6, nwiftab.zone_nwif_address,
&in6) == 1)
saw_v6 = B_TRUE;
}
(void) zonecfg_endnwifent(handle);
}
zonecfg_fini_handle(handle);
(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
sizeof (loopback_iftab.zone_nwif_physical));
(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
sizeof (loopback_iftab.zone_nwif_address));
if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL)
!= Z_OK) {
return (-1);
}
if (saw_v6) {
(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
sizeof (loopback_iftab.zone_nwif_address));
if (configure_one_interface(zlogp, zoneid,
&loopback_iftab, NULL, NULL) != Z_OK) {
return (-1);
}
}
return (0);
}
static int
tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
{
int fd;
struct strioctl ioc;
tcp_ioc_abort_conn_t conn;
int error;
conn.ac_local = *local;
conn.ac_remote = *remote;
conn.ac_start = TCPS_SYN_SENT;
conn.ac_end = TCPS_TIME_WAIT;
conn.ac_zoneid = zoneid;
ioc.ic_cmd = TCP_IOC_ABORT_CONN;
ioc.ic_timout = -1; /* infinite timeout */
ioc.ic_len = sizeof (conn);
ioc.ic_dp = (char *)&conn;
if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
return (-1);
}
error = ioctl(fd, I_STR, &ioc);
(void) close(fd);
if (error == 0 || errno == ENOENT) /* ENOENT is not an error */
return (0);
return (-1);
}
static int
tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
{
struct sockaddr_storage l, r;
struct sockaddr_in *local, *remote;
struct sockaddr_in6 *local6, *remote6;
int error;
/*
* Abort IPv4 connections.
*/
bzero(&l, sizeof (*local));
local = (struct sockaddr_in *)&l;
local->sin_family = AF_INET;
local->sin_addr.s_addr = INADDR_ANY;
local->sin_port = 0;
bzero(&r, sizeof (*remote));
remote = (struct sockaddr_in *)&r;
remote->sin_family = AF_INET;
remote->sin_addr.s_addr = INADDR_ANY;
remote->sin_port = 0;
if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
return (error);
/*
* Abort IPv6 connections.
*/
bzero(&l, sizeof (*local6));
local6 = (struct sockaddr_in6 *)&l;
local6->sin6_family = AF_INET6;
local6->sin6_port = 0;
local6->sin6_addr = in6addr_any;
bzero(&r, sizeof (*remote6));
remote6 = (struct sockaddr_in6 *)&r;
remote6->sin6_family = AF_INET6;
remote6->sin6_port = 0;
remote6->sin6_addr = in6addr_any;
if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
return (error);
return (0);
}
static int
devfsadm_call(zlog_t *zlogp, const char *arg)
{
char *argv[4];
int status;
argv[0] = DEVFSADM;
argv[1] = (char *)arg;
argv[2] = zone_name;
argv[3] = NULL;
status = forkexec(zlogp, DEVFSADM_PATH, argv);
if (status == 0 || status == -1)
return (status);
zerror(zlogp, B_FALSE, "%s call (%s %s %s) unexpectedly returned %d",
DEVFSADM, DEVFSADM_PATH, arg, zone_name, status);
return (-1);
}
static int
devfsadm_register(zlog_t *zlogp)
{
/*
* Ready the zone's devices.
*/
return (devfsadm_call(zlogp, "-z"));
}
static int
devfsadm_unregister(zlog_t *zlogp)
{
return (devfsadm_call(zlogp, "-Z"));
}
static int
get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
{
nvlist_t *nvl = NULL;
char *nvl_packed = NULL;
size_t nvl_size = 0;
nvlist_t **nvlv = NULL;
int rctlcount = 0;
int error = -1;
zone_dochandle_t handle;
struct zone_rctltab rctltab;
rctlblk_t *rctlblk = NULL;
*bufp = NULL;
*bufsizep = 0;
if ((handle = zonecfg_init_handle()) == NULL) {
zerror(zlogp, B_TRUE, "getting zone configuration handle");
return (-1);
}
if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
zonecfg_fini_handle(handle);
return (-1);
}
rctltab.zone_rctl_valptr = NULL;
if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
goto out;
}
if (zonecfg_setrctlent(handle) != Z_OK) {
zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
goto out;
}
if ((rctlblk = malloc(rctlblk_size())) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
goto out;
}
while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
struct zone_rctlvaltab *rctlval;
uint_t i, count;
const char *name = rctltab.zone_rctl_name;
/* zoneadm should have already warned about unknown rctls. */
if (!zonecfg_is_rctl(name)) {
zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
rctltab.zone_rctl_valptr = NULL;
continue;
}
count = 0;
for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
rctlval = rctlval->zone_rctlval_next) {
count++;
}
if (count == 0) { /* ignore */
continue; /* Nothing to free */
}
if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
goto out;
i = 0;
for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
rctlval = rctlval->zone_rctlval_next, i++) {
if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
zerror(zlogp, B_TRUE, "%s failed",
"nvlist_alloc");
goto out;
}
if (zonecfg_construct_rctlblk(rctlval, rctlblk)
!= Z_OK) {
zerror(zlogp, B_FALSE, "invalid rctl value: "
"(priv=%s,limit=%s,action=%s)",
rctlval->zone_rctlval_priv,
rctlval->zone_rctlval_limit,
rctlval->zone_rctlval_action);
goto out;
}
if (!zonecfg_valid_rctl(name, rctlblk)) {
zerror(zlogp, B_FALSE,
"(priv=%s,limit=%s,action=%s) is not a "
"valid value for rctl '%s'",
rctlval->zone_rctlval_priv,
rctlval->zone_rctlval_limit,
rctlval->zone_rctlval_action,
name);
goto out;
}
if (nvlist_add_uint64(nvlv[i], "privilege",
rctlblk_get_privilege(rctlblk)) != 0) {
zerror(zlogp, B_FALSE, "%s failed",
"nvlist_add_uint64");
goto out;
}
if (nvlist_add_uint64(nvlv[i], "limit",
rctlblk_get_value(rctlblk)) != 0) {
zerror(zlogp, B_FALSE, "%s failed",
"nvlist_add_uint64");
goto out;
}
if (nvlist_add_uint64(nvlv[i], "action",
(uint_t)rctlblk_get_local_action(rctlblk, NULL))
!= 0) {
zerror(zlogp, B_FALSE, "%s failed",
"nvlist_add_uint64");
goto out;
}
}
zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
rctltab.zone_rctl_valptr = NULL;
if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
!= 0) {
zerror(zlogp, B_FALSE, "%s failed",
"nvlist_add_nvlist_array");
goto out;
}
for (i = 0; i < count; i++)
nvlist_free(nvlv[i]);
free(nvlv);
nvlv = NULL;
rctlcount++;
}
(void) zonecfg_endrctlent(handle);
if (rctlcount == 0) {
error = 0;
goto out;
}
if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
!= 0) {
zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
goto out;
}
error = 0;
*bufp = nvl_packed;
*bufsizep = nvl_size;
out:
free(rctlblk);
zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
if (error && nvl_packed != NULL)
free(nvl_packed);
if (nvl != NULL)
nvlist_free(nvl);
if (nvlv != NULL)
free(nvlv);
if (handle != NULL)
zonecfg_fini_handle(handle);
return (error);
}
static int
get_zone_pool(zlog_t *zlogp, char *poolbuf, size_t bufsz)
{
zone_dochandle_t handle;
int error;
if ((handle = zonecfg_init_handle()) == NULL) {
zerror(zlogp, B_TRUE, "getting zone configuration handle");
return (-1);
}
if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
zonecfg_fini_handle(handle);
return (-1);
}
error = zonecfg_get_pool(handle, poolbuf, bufsz);
zonecfg_fini_handle(handle);
return (error);
}
static int
get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
{
zone_dochandle_t handle;
struct zone_dstab dstab;
size_t total, offset, len;
int error = -1;
char *str;
*bufp = NULL;
*bufsizep = 0;
if ((handle = zonecfg_init_handle()) == NULL) {
zerror(zlogp, B_TRUE, "getting zone configuration handle");
return (-1);
}
if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
zonecfg_fini_handle(handle);
return (-1);
}
if (zonecfg_setdsent(handle) != Z_OK) {
zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
goto out;
}
total = 0;
while (zonecfg_getdsent(handle, &dstab) == Z_OK)
total += strlen(dstab.zone_dataset_name) + 1;
(void) zonecfg_enddsent(handle);
if (total == 0) {
error = 0;
goto out;
}
if ((str = malloc(total)) == NULL) {
zerror(zlogp, B_TRUE, "memory allocation failed");
goto out;
}
if (zonecfg_setdsent(handle) != Z_OK) {
zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
goto out;
}
offset = 0;
while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
len = strlen(dstab.zone_dataset_name);
(void) strlcpy(str + offset, dstab.zone_dataset_name,
sizeof (dstab.zone_dataset_name) - offset);
offset += len;
if (offset != total - 1)
str[offset++] = ',';
}
(void) zonecfg_enddsent(handle);
error = 0;
*bufp = str;
*bufsizep = total;
out:
if (error != 0 && str != NULL)
free(str);
if (handle != NULL)
zonecfg_fini_handle(handle);
return (error);
}
/* ARGSUSED */
static void
zfs_error_handler(const char *fmt, va_list ap)
{
/*
* Do nothing - we interpret the failures from each libzfs call below.
*/
}
static int
validate_datasets(zlog_t *zlogp)
{
zone_dochandle_t handle;
struct zone_dstab dstab;
zfs_handle_t *zhp;
if ((handle = zonecfg_init_handle()) == NULL) {
zerror(zlogp, B_TRUE, "getting zone configuration handle");
return (-1);
}
if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
zonecfg_fini_handle(handle);
return (-1);
}
if (zonecfg_setdsent(handle) != Z_OK) {
zerror(zlogp, B_FALSE, "invalid configuration");
zonecfg_fini_handle(handle);
return (-1);
}
zfs_set_error_handler(zfs_error_handler);
/*
* libzfs opens /dev/zfs during its .init routine.
* zoneadmd automatically closes these files when it daemonizes,
* so we cheat by re-calling the init routine.
*/
zfs_init();
while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
if ((zhp = zfs_open(dstab.zone_dataset_name,
ZFS_TYPE_FILESYSTEM)) == NULL) {
zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
dstab.zone_dataset_name);
zonecfg_fini_handle(handle);
return (-1);
}
/*
* Automatically set the 'zoned' property. We check the value
* first because we'll get EPERM if it is already set.
*/
if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
zfs_prop_set(zhp, ZFS_PROP_ZONED, "on") != 0) {
zerror(zlogp, B_FALSE, "cannot set 'zoned' "
"property for ZFS dataset '%s'\n",
dstab.zone_dataset_name);
zonecfg_fini_handle(handle);
zfs_close(zhp);
return (-1);
}
zfs_close(zhp);
}
(void) zonecfg_enddsent(handle);
zonecfg_fini_handle(handle);
return (0);
}
static int
bind_to_pool(zlog_t *zlogp, zoneid_t zoneid)
{
pool_conf_t *poolconf;
pool_t *pool;
char poolname[MAXPATHLEN];
int status;
int error;
/*
* Find the pool mentioned in the zone configuration, and bind to it.
*/
error = get_zone_pool(zlogp, poolname, sizeof (poolname));
if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
/*
* The property is not set on the zone, so the pool
* should be bound to the default pool. But that's
* already done by the kernel, so we can just return.
*/
return (0);
}
if (error != Z_OK) {
/*
* Not an error, even though it shouldn't be happening.
*/
zerror(zlogp, B_FALSE,
"WARNING: unable to retrieve default pool.");
return (0);
}
/*
* Don't do anything if pools aren't enabled.
*/
if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
zerror(zlogp, B_FALSE, "WARNING: pools facility not active; "
"zone will not be bound to pool '%s'.", poolname);
return (0);
}
/*
* Try to provide a sane error message if the requested pool doesn't
* exist.
*/
if ((poolconf = pool_conf_alloc()) == NULL) {
zerror(zlogp, B_FALSE, "%s failed", "pool_conf_alloc");
return (-1);
}
if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
PO_SUCCESS) {
zerror(zlogp, B_FALSE, "%s failed", "pool_conf_open");
pool_conf_free(poolconf);
return (-1);
}
pool = pool_get_pool(poolconf, poolname);
(void) pool_conf_close(poolconf);
pool_conf_free(poolconf);
if (pool == NULL) {
zerror(zlogp, B_FALSE, "WARNING: pool '%s' not found; "
"using default pool.", poolname);
return (0);
}
/*
* Bind the zone to the pool.
*/
if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
zerror(zlogp, B_FALSE, "WARNING: unable to bind to pool '%s'; "
"using default pool.", poolname);
}
return (0);
}
int
prtmount(const char *fs, void *x) {
zerror((zlog_t *)x, B_FALSE, " %s", fs);
return (0);
}
/*
* Look for zones running on the main system that are using this root (or any
* subdirectory of it). Return B_TRUE and print an error if a conflicting zone
* is found or if we can't tell.
*/
static boolean_t
duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
{
zoneid_t *zids = NULL;
uint_t nzids = 0;
boolean_t retv;
int rlen, zlen;
char zroot[MAXPATHLEN];
char zonename[ZONENAME_MAX];
for (;;) {
nzids += 10;
zids = malloc(nzids * sizeof (*zids));
if (zids == NULL) {
zerror(zlogp, B_TRUE, "unable to allocate memory");
return (B_TRUE);
}
if (zone_list(zids, &nzids) == 0)
break;
free(zids);
}
retv = B_FALSE;
rlen = strlen(rootpath);
while (nzids > 0) {
/*
* Ignore errors; they just mean that the zone has disappeared
* while we were busy.
*/
if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
sizeof (zroot)) == -1)
continue;
zlen = strlen(zroot);
if (zlen > rlen)
zlen = rlen;
if (strncmp(rootpath, zroot, zlen) == 0 &&
(zroot[zlen] == '\0' || zroot[zlen] == '/') &&
(rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
if (getzonenamebyid(zids[nzids], zonename,
sizeof (zonename)) == -1)
(void) snprintf(zonename, sizeof (zonename),
"id %d", (int)zids[nzids]);
zerror(zlogp, B_FALSE,
"zone root %s already in use by zone %s",
rootpath, zonename);
retv = B_TRUE;
break;
}
}
free(zids);
return (retv);
}
/*
* Search for loopback mounts that use this same source node (same device and
* inode). Return B_TRUE if there is one or if we can't tell.
*/
static boolean_t
duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
{
struct stat64 rst, zst;
struct mnttab *mnp;
if (stat64(rootpath, &rst) == -1) {
zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
return (B_TRUE);
}
if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
return (B_TRUE);
for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
if (mnp->mnt_fstype == NULL ||
strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
continue;
/* We're looking at a loopback mount. Stat it. */
if (mnp->mnt_special != NULL &&
stat64(mnp->mnt_special, &zst) != -1 &&
rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
zerror(zlogp, B_FALSE,
"zone root %s is reachable through %s",
rootpath, mnp->mnt_mountp);
return (B_TRUE);
}
}
return (B_FALSE);
}
zoneid_t
vplat_create(zlog_t *zlogp, boolean_t mount_cmd)
{
zoneid_t rval = -1;
priv_set_t *privs;
char rootpath[MAXPATHLEN];
char *rctlbuf = NULL;
size_t rctlbufsz = 0;
char *zfsbuf = NULL;
size_t zfsbufsz = 0;
zoneid_t zoneid = -1;
int xerr;
char *kzone;
FILE *fp = NULL;
if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
zerror(zlogp, B_TRUE, "unable to determine zone root");
return (-1);
}
if (zonecfg_in_alt_root())
resolve_lofs(zlogp, rootpath, sizeof (rootpath));
if ((privs = priv_allocset()) == NULL) {
zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
return (-1);
}
priv_emptyset(privs);
if (zonecfg_get_privset(privs) != Z_OK) {
zerror(zlogp, B_TRUE, "Failed to initialize privileges");
goto error;
}
if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
zerror(zlogp, B_FALSE, "Unable to get list of rctls");
goto error;
}
if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
goto error;
}
kzone = zone_name;
/*
* We must do this scan twice. First, we look for zones running on the
* main system that are using this root (or any subdirectory of it).
* Next, we reduce to the shortest path and search for loopback mounts
* that use this same source node (same device and inode).
*/
if (duplicate_zone_root(zlogp, rootpath))
goto error;
if (duplicate_reachable_path(zlogp, rootpath))
goto error;
if (mount_cmd) {
root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
/*
* Forge up a special root for this zone. When a zone is
* mounted, we can't let the zone have its own root because the
* tools that will be used in this "scratch zone" need access
* to both the zone's resources and the running machine's
* executables.
*
* Note that the mkdir here also catches read-only filesystems.
*/
if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
goto error;
}
if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
goto error;
}
if (zonecfg_in_alt_root()) {
/*
* If we are mounting up a zone in an alternate root partition,
* then we have some additional work to do before starting the
* zone. First, resolve the root path down so that we're not
* fooled by duplicates. Then forge up an internal name for
* the zone.
*/
if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
zerror(zlogp, B_TRUE, "cannot open mapfile");
goto error;
}
if (zonecfg_lock_scratch(fp) != 0) {
zerror(zlogp, B_TRUE, "cannot lock mapfile");
goto error;
}
if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
NULL, 0) == 0) {
zerror(zlogp, B_FALSE, "scratch zone already running");
goto error;
}
/* This is the preferred name */
(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
zone_name);
srandom(getpid());
while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
0) == 0) {
/* This is just an arbitrary name; note "." usage */
(void) snprintf(kernzone, sizeof (kernzone),
"SUNWlu.%08lX%08lX", random(), random());
}
kzone = kernzone;
}
xerr = 0;
if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
rctlbufsz, zfsbuf, zfsbufsz, &xerr)) == -1) {
if (xerr == ZE_AREMOUNTS) {
if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
zerror(zlogp, B_FALSE,
"An unknown file-system is mounted on "
"a subdirectory of %s", rootpath);
} else {
zerror(zlogp, B_FALSE,
"These file-systems are mounted on "
"subdirectories of %s:", rootpath);
(void) zonecfg_find_mounts(rootpath,
prtmount, zlogp);
}
} else if (xerr == ZE_CHROOTED) {
zerror(zlogp, B_FALSE, "%s: "
"cannot create a zone from a chrooted "
"environment", "zone_create");
} else {
zerror(zlogp, B_TRUE, "%s failed", "zone_create");
}
goto error;
}
if (zonecfg_in_alt_root() &&
zonecfg_add_scratch(fp, zone_name, kernzone,
zonecfg_get_root()) == -1) {
zerror(zlogp, B_TRUE, "cannot add mapfile entry");
goto error;
}
/*
* The following is a warning, not an error, and is not performed when
* merely mounting a zone for administrative use.
*/
if (!mount_cmd && bind_to_pool(zlogp, zoneid) != 0)
zerror(zlogp, B_FALSE, "WARNING: unable to bind zone to "
"requested pool; using default pool.");
rval = zoneid;
zoneid = -1;
error:
if (zoneid != -1)
(void) zone_destroy(zoneid);
if (rctlbuf != NULL)
free(rctlbuf);
priv_freeset(privs);
if (fp != NULL)
zonecfg_close_scratch(fp);
lofs_discard_mnttab();
return (rval);
}
int
vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd)
{
if (!mount_cmd && validate_datasets(zlogp) != 0) {
lofs_discard_mnttab();
return (-1);
}
if (create_dev_files(zlogp) != 0 ||
mount_filesystems(zlogp, mount_cmd) != 0) {
lofs_discard_mnttab();
return (-1);
}
if (!mount_cmd && (devfsadm_register(zlogp) != 0 ||
configure_network_interfaces(zlogp) != 0)) {
lofs_discard_mnttab();
return (-1);
}
lofs_discard_mnttab();
return (0);
}
static int
lu_root_teardown(zlog_t *zlogp)
{
char zroot[MAXPATHLEN];
if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
zerror(zlogp, B_FALSE, "unable to determine zone root");
return (-1);
}
root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
/*
* At this point, the processes are gone, the filesystems (save the
* root) are unmounted, and the zone is on death row. But there may
* still be creds floating about in the system that reference the
* zone_t, and which pin down zone_rootvp causing this call to fail
* with EBUSY. Thus, we try for a little while before just giving up.
* (How I wish this were not true, and umount2 just did the right
* thing, or tmpfs supported MS_FORCE This is a gross hack.)
*/
if (umount2(zroot, MS_FORCE) != 0) {
if (errno == ENOTSUP && umount2(zroot, 0) == 0)
goto unmounted;
if (errno == EBUSY) {
int tries = 10;
while (--tries >= 0) {
(void) sleep(1);
if (umount2(zroot, 0) == 0)
goto unmounted;
if (errno != EBUSY)
break;
}
}
zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
return (-1);
}
unmounted:
/*
* Only zones in an alternate root environment have scratch zone
* entries.
*/
if (zonecfg_in_alt_root()) {
FILE *fp;
int retv;
if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
zerror(zlogp, B_TRUE, "cannot open mapfile");
return (-1);
}
retv = -1;
if (zonecfg_lock_scratch(fp) != 0)
zerror(zlogp, B_TRUE, "cannot lock mapfile");
else if (zonecfg_delete_scratch(fp, kernzone) != 0)
zerror(zlogp, B_TRUE, "cannot delete map entry");
else
retv = 0;
zonecfg_close_scratch(fp);
return (retv);
} else {
return (0);
}
}
int
vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd)
{
char *kzone;
zoneid_t zoneid;
kzone = zone_name;
if (zonecfg_in_alt_root()) {
FILE *fp;
if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
zerror(zlogp, B_TRUE, "unable to open map file");
goto error;
}
if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
kernzone, sizeof (kernzone)) != 0) {
zerror(zlogp, B_FALSE, "unable to find scratch zone");
zonecfg_close_scratch(fp);
goto error;
}
zonecfg_close_scratch(fp);
kzone = kernzone;
}
if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
if (!bringup_failure_recovery)
zerror(zlogp, B_TRUE, "unable to get zoneid");
if (unmount_cmd)
(void) lu_root_teardown(zlogp);
goto error;
}
if (zone_shutdown(zoneid) != 0) {
zerror(zlogp, B_TRUE, "unable to shutdown zone");
goto error;
}
if (!unmount_cmd && devfsadm_unregister(zlogp) != 0)
goto error;
if (!unmount_cmd &&
unconfigure_network_interfaces(zlogp, zoneid) != 0) {
zerror(zlogp, B_FALSE,
"unable to unconfigure network interfaces in zone");
goto error;
}
if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
zerror(zlogp, B_TRUE, "unable to abort TCP connections");
goto error;
}
if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
zerror(zlogp, B_FALSE,
"unable to unmount file systems in zone");
goto error;
}
if (zone_destroy(zoneid) != 0) {
zerror(zlogp, B_TRUE, "unable to destroy zone");
goto error;
}
/*
* Special teardown for alternate boot environments: remove the tmpfs
* root for the zone and then remove it from the map file.
*/
if (unmount_cmd && lu_root_teardown(zlogp) != 0)
goto error;
if (!unmount_cmd)
destroy_console_slave();
lofs_discard_mnttab();
return (0);
error:
lofs_discard_mnttab();
return (-1);
}
|