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
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 (c) 2017 Peter Tribble.
*/
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Program: pkgcond
*
* Function: Implements the package command suite public utility pkgcond(8)
*
* Usage: pkgcond [-nv] [-O debug] condition [ argument ]
*
* command options:
* -n - negate results of condition test
* -v - verbose output of condition testing
*
* <condition> may be any one of:
* can_add_driver [path]
* can_remove_driver [path]
* can_update_driver [path]
* is_alternative_root [path]
* is_boot_environment [path]
* is_diskless_client [path]
* is_global_zone [path]
* is_mounted_miniroot [path]
* is_netinstall_image [path]
* is_nonglobal_zone [path]
* is_path_writable path
* is_running_system [path]
* is_what [path]
* is_whole_root_nonglobal_zone [path]
*
* <option(s)> are specific to the condition used
*
* Input: depends on command
*
* Output: depends on command
*
* Exit status: If the -n option is not specified:
* == 0 - the specified condition is true (or exists).
* == 1 - the specified condition is false (or does not exist).
* == 2 - command line usage errors (including bad keywords)
* == 3 - command failed to perform the test due to a fatal error
*
* If the -n option is specified:
* == 0 - the specified condition is false (or does not exist).
* == 1 - the specified condition is true (or exists).
* == 2 - command line usage errors (including bad keywords)
* == 3 - command failed to perform the test due to a fatal error
*/
#include <stdio.h>
#include <sys/mnttab.h>
#include <sys/mntent.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <locale.h>
#include <errno.h>
#include <sys/param.h>
#include <assert.h>
#include <instzones_api.h>
#include <pkglib.h>
#include <install.h>
#include <libinst.h>
#include <libadm.h>
#include <messages.h>
#include "pkgcond.h"
#include "pkgcond_msgs.h"
/* Should be defined by cc -D */
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
/* commands to execute */
#define LS_CMD "/usr/bin/ls"
/*
* type definition and "types" for testPath()
*/
typedef enum {
TEST_EXISTS = 0x01,
TEST_NOT_EXISTS = 0x02,
TEST_IS_DIRECTORY = 0x04,
TEST_IS_FILE = 0x08,
TEST_NOT_DIRECTORY = 0x10,
TEST_NOT_FILE = 0x20,
TEST_IS_SYMBOLIC_LINK = 0x40,
TEST_NOT_SYMBOLIC_LINK = 0x80,
TEST_GLOBAL_TOKEN_IN_FILE = 0x100
} TEST_TYPES;
/* holds file system info */
struct fsi_t {
char *fsi_mntOptions;
char *fsi_fsType;
char *fsi_mntPoint;
};
typedef struct fsi_t FSI_T;
/* holds parsed global data */
struct globalData_t {
/* initial install: PKG_INIT_INSTALL=true */
boolean_t gd_initialInstall;
/* global zone install: SUNW_PKG_INSTALL_ZONENAME=global */
boolean_t gd_globalZoneInstall;
/* non-global zone install: SUNW_PKG_INSTALL_ZONENAME!=global */
boolean_t gd_nonglobalZoneInstall;
/* non-global zone is in a mounted state */
boolean_t inMountedState;
/* sorted list of all mounted file systems */
FSI_T *gd_fileSystemConfig;
/* number of mounted file systems in list */
long gd_fileSystemConfigLen;
/* current zone name */
char *gd_zoneName;
/* SUNW_PKGCOND_GLOBAL_DATA:parentZone:zoneName */
char *gd_parentZoneName;
/* SUNW_PKGCOND_GLOBAL_DATA:parentZone:zoneType */
char *gd_parentZoneType;
/* root path to target: PKG_INSTALL_ROOT */
char *gd_installRoot;
/* SUNW_PKGCOND_GLOBAL_DATA:currentZone:zoneName */
char *gd_currentZoneName;
/* SUNW_PKGCOND_GLOBAL_DATA:currentZone:zoneType */
char *gd_currentZoneType;
/* path provided on command line */
char *gd_cmdline_path;
};
typedef struct globalData_t GLOBALDATA_T;
/* holds subcommands and their definitions */
struct cmd_t {
char *c_name;
char *c_args;
int (*c_func)(int argc, char **argv, GLOBALDATA_T *a_gdt);
};
typedef struct cmd_t CMD_T;
/* Command function prototypes */
static int cmd_can_add_driver(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_can_remove_driver(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_can_update_driver(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_is_alternative_root(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_is_boot_environment(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_is_diskless_client(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_is_global_zone(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_is_mounted_miniroot(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_is_netinstall_image(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_is_nonglobal_zone(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_is_path_writable(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_is_running_system(int argc, char **argv,
GLOBALDATA_T *a_gdt);
static int cmd_is_what(int argc, char **argv,
GLOBALDATA_T *a_gdt);
/* Utility function Prototypes */
static boolean_t getNegateResults(void);
static boolean_t recursionCheck(int *r_recursion, char *a_function);
static int adjustResults(int a_result);
static int calculateFileSystemConfig(GLOBALDATA_T *a_gdt);
static int getRootPath(char **r_rootPath);
static int getZoneName(char **r_zoneName);
static int mountOptionPresent(char *a_mntOptions, char *a_opt);
static int parseGlobalData(char *a_envVar, GLOBALDATA_T **a_gdt);
static int resolvePath(char **r_path);
static int setRootPath(char *a_path, char *a_envVar,
boolean_t a_mustExist);
static int testPath(TEST_TYPES a_tt, char *format, ...);
static int usage(char *a_format, ...);
static int findToken(char *path, char *token);
static char *getMountOption(char **p);
static void dumpGlobalData(GLOBALDATA_T *a_gdt);
static void removeLeadingWhitespace(char **a_str);
static void setNegateResults(boolean_t setting);
static void setVerbose(boolean_t);
static void sortedInsert(FSI_T **r_list, long *a_listSize,
char *a_mntPoint, char *a_fsType, char *a_mntOptions);
static void setCmdLinePath(char **a_path, char **args,
int num_args);
/* local static data */
static boolean_t _negateResults = B_FALSE;
static char *_rootPath = "/";
/* define subcommand data structure */
static CMD_T cmds[] = {
{ "can_add_driver", " [path]",
cmd_can_add_driver },
{ "can_remove_driver", " [path]",
cmd_can_remove_driver },
{ "can_update_driver", " [path]",
cmd_can_update_driver },
{ "is_alternative_root", " [path]",
cmd_is_alternative_root },
{ "is_boot_environment", " [path]",
cmd_is_boot_environment },
{ "is_diskless_client", " [path]",
cmd_is_diskless_client },
{ "is_global_zone", " [path]",
cmd_is_global_zone },
{ "is_mounted_miniroot", " [path]",
cmd_is_mounted_miniroot },
{ "is_netinstall_image", " [path]",
cmd_is_netinstall_image },
{ "is_nonglobal_zone", " [path]",
cmd_is_nonglobal_zone },
{ "is_path_writable", " path",
cmd_is_path_writable },
{ "is_running_system", " [path]",
cmd_is_running_system },
{ "is_what", " [path]",
cmd_is_what },
/* last one must be all NULLs */
{ NULL, NULL, NULL }
};
/*
* *****************************************************************************
* main
* *****************************************************************************
*/
/*
* Name: main
* Description: main processing loop for pkgcond *
* Return: 0 - condition is satisfied (true)
* 1 - condition is not satisfied (false)
* 2 - command line usage errors
* 3 - failure to determine condition
*/
int
main(int argc, char **argv)
{
GLOBALDATA_T *gdt = NULL;
char **newargv;
char *p;
int cur_cmd;
int i;
int newargc;
/* make standard output non-buffered */
setbuf(stdout, NULL);
/* set the default text domain for messaging */
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
/* remember command name */
set_prog_name(argv[0]);
/* tell spmi zones interface how to access package output functions */
z_set_output_functions(echo, echoDebug, progerr);
/* set verbose mode if appropriate environment variable is set */
if (getenv(ENV_VAR_VERBOSE)) {
/* same as -v */
setVerbose(B_TRUE);
}
/* set debug mode if appropriate environment variable is set */
if (getenv(ENV_VAR_DEBUG)) {
/* same as -O debug */
/* set sml tracing (sml.c) */
smlSetVerbose(B_TRUE);
/* set log and echo (interactive) message tracing */
setVerbose(B_TRUE);
/* enable echoDebug debugging messages */
echoDebugSetFlag(B_TRUE);
}
/* generate usage if no options or arguments specified */
if (argc <= 1) {
(void) usage(MSG_NO_ARGUMENTS_SPECIFIED);
return (R_USAGE);
}
/*
* process any arguments that can appear before the subcommand
*/
while ((i = getopt(argc, argv, ":O:vn?")) != EOF) {
switch (i) {
/*
* Not a public interface: the -O option allows the behavior
* of the package tools to be modified. Recognized options:
* -> debug
* ---> enable debugging output
*/
case 'O':
for (p = strtok(optarg, ","); p != NULL;
p = strtok(NULL, ",")) {
/* debug - enable all tracing */
if (strcmp(p, "debug") == 0) {
/* set sml tracing */
smlSetVerbose(B_TRUE);
/* set log/echo tracing */
setVerbose(B_TRUE);
/* enable debugging messages */
echoDebugSetFlag(B_TRUE);
continue;
}
progerr(ERR_INVALID_O_OPTION, p);
return (adjustResults(R_USAGE));
}
break;
/*
* Public interface: enable verbose (debug) output.
*/
case 'v': /* verbose mode enabled */
/* set command tracing only */
setVerbose(B_TRUE);
break;
/*
* Public interface: negate output results.
*/
case 'n':
setNegateResults(B_TRUE);
break;
/*
* unrecognized option
*/
case '?':
default:
(void) usage(MSG_INVALID_OPTION_SPECIFIED, optopt);
return (R_USAGE);
}
}
/*
* done processing options that can preceed subcommand
*/
/* error if no subcommand specified */
if ((argc-optind) <= 0) {
(void) usage(MSG_NO_ARGUMENTS_SPECIFIED);
return (R_USAGE);
}
/* parse global data if environment variable set */
if (parseGlobalData(PKGCOND_GLOBAL_VARIABLE, &gdt) != R_SUCCESS) {
log_msg(LOG_MSG_ERR, ERR_CANNOT_USE_GLOBAL_DATA,
PKGCOND_GLOBAL_VARIABLE);
return (R_ERROR);
}
if (setRootPath(gdt->gd_installRoot,
(strcmp(gdt->gd_installRoot, "/") == 0) ? NULL :
ENV_VAR_SET, B_TRUE) != R_SUCCESS) {
log_msg(LOG_MSG_ERR, ERR_CANNOT_SET_ROOT_PATH,
ENV_VAR_PKGROOT);
return (R_ERROR);
}
/* set path provided on the command line */
setCmdLinePath(&(gdt->gd_cmdline_path), argv, argc);
echoDebug(DBG_CMDLINE_PATH,
gdt->gd_cmdline_path == NULL ? "" : gdt->gd_cmdline_path);
/* determine how file systems are layered in this zone */
if (calculateFileSystemConfig(gdt) != R_SUCCESS) {
log_msg(LOG_MSG_ERR, ERR_CANNOT_CALC_FS_CONFIG);
return (R_ERROR);
}
/* dump global data read in (only if debugging) */
dumpGlobalData(gdt);
/* search for specified subcommand and execute if found */
for (cur_cmd = 0; cmds[cur_cmd].c_name != NULL; cur_cmd++) {
if (ci_streq(argv[optind], cmds[cur_cmd].c_name)) {
int result;
/* make subcommand the first option */
newargc = argc - optind;
newargv = argv + optind;
opterr = optind = 1; optopt = 0;
/* call subcommand with its own argc/argv */
result = cmds[cur_cmd].c_func(newargc, newargv, gdt);
/* process result code and exit */
result = adjustResults(result);
log_msg(LOG_MSG_DEBUG, DBG_RESULTS, result);
return (result);
}
}
/* subcommand not found - output error message and exit with error */
log_msg(LOG_MSG_ERR, ERR_BAD_SUB, argv[optind]);
(void) usage(MSG_UNRECOGNIZED_CONDITION_SPECIFIED);
return (R_USAGE);
}
/*
* *****************************************************************************
* command implementation functions
* *****************************************************************************
*/
/*
* Name: cmd_is_diskless_client
* Description: determine if target is a diskless client
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* IMPLEMENTATION:
* - must not be initial installation to the install root
* - must not be installation of a zone
* - must not be a whole root non-global zone
* - must not be a non-global zone
* - must not be a mounted mini-root
* - must not be a netinstall image
* - must not be a boot environment
* - The package "SUNWdclnt" must be installed at "/"
* - The root path must not be "/"
* - The path "/export/exec/Solaris_\*\/usr" must exist at "/"
* - The directory "$ROOTDIR/../templates" must exist
*/
static int
cmd_is_diskless_client(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
char cmd[MAXPATHLEN+1];
int c;
int r;
int rc;
static char *cmdName = "is_diskless_client";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
/*
* a diskless client cannot be any of the following
*/
/* cannot be non-global zone */
r = cmd_is_nonglobal_zone(argc, argv, a_gdt);
/* cannot be mounted miniroot */
if (r != R_SUCCESS) {
r = cmd_is_mounted_miniroot(argc, argv, a_gdt);
}
/* cannot be a netinstall image */
if (r != R_SUCCESS) {
r = cmd_is_netinstall_image(argc, argv, a_gdt);
}
/* cannot be a boot environment */
if (r != R_SUCCESS) {
r = cmd_is_boot_environment(argc, argv, a_gdt);
}
/* no need to guard against recursion any more */
recursion--;
/* return failure if any of the preceeding are true */
switch (r) {
case R_SUCCESS:
return (R_FAILURE);
case R_FAILURE:
break;
case R_USAGE:
case R_ERROR:
default:
return (r);
}
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* SUNWdclnt must be installed */
if (pkgTestInstalled("SUNWdclnt", "/") != B_TRUE) {
log_msg(LOG_MSG_DEBUG, DBG_IDLC_PKG_NOT_INSTALLED,
rootPath, "SUNWdclnt", "/");
return (R_FAILURE);
}
/* - $ROOTDIR must not be "/" */
if (strcmp(rootPath, "/") == 0) {
log_msg(LOG_MSG_DEBUG, DBG_IDLC_ROOTPATH_BAD, rootPath, "/");
return (R_FAILURE);
}
/* - zone name must be global */
if (strcmp(a_gdt->gd_zoneName, GLOBAL_ZONENAME) != 0) {
log_msg(LOG_MSG_DEBUG, DBG_IDLC_ZONE_BAD, rootPath,
GLOBAL_ZONENAME);
return (R_FAILURE);
}
/*
* /export/exec/Solaris_"*"/usr must exist;
* create ls command to test:
* /usr/bin/ls /export/exec/Solaris_"*"/usr
*/
(void) snprintf(cmd, sizeof (cmd), "%s %s >/dev/null 2>&1",
LS_CMD, "/export/exec/Solaris_*/usr");
/* execute command */
rc = system(cmd);
/* return error if ls returns something other than "0" */
if (rc != 0) {
log_msg(LOG_MSG_DEBUG, DBG_IDLC_PATH_MISSING,
rootPath, "/export/exec/Solaris_*/usr");
return (R_FAILURE);
}
/*
* /usr must be empty on a diskless client:
* create ls command to test:
* /usr/bin/ls -d1 $ROOTDIR/usr/\*
*/
(void) snprintf(cmd, sizeof (cmd), "%s %s %s/%s >/dev/null 2>&1",
LS_CMD, "-1d", rootPath, "usr/*");
/* execute command */
rc = system(cmd);
/* return error if ls returns "0" */
if (rc == 0) {
log_msg(LOG_MSG_DEBUG, DBG_IDLC_USR_IS_NOT_EMPTY,
rootPath);
return (R_FAILURE);
}
/* there must be a templates directory at ${ROOTPATH}/../templates */
r = testPath(TEST_EXISTS|TEST_IS_DIRECTORY,
"%s/%s", rootPath, "../templates");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_IDLC_NO_TEMPLATES_PATH,
rootPath, rootPath, "../templates");
return (R_FAILURE);
}
/* must not be initial installation to the install root */
if ((a_gdt->gd_initialInstall == B_TRUE) &&
(strcmp(a_gdt->gd_installRoot, rootPath) == 0)) {
/* initial install: install root cannot be diskless client */
log_msg(LOG_MSG_DEBUG, DBG_IDLC_INITIAL_INSTALL, rootPath);
return (R_FAILURE);
}
/* must not be installation of a zone */
if ((a_gdt->gd_globalZoneInstall == B_TRUE) ||
(a_gdt->gd_nonglobalZoneInstall == B_TRUE)) {
/* initial zone install: no path can be diskless client */
log_msg(LOG_MSG_DEBUG, DBG_IDLC_ZONE_INSTALL, rootPath);
return (R_FAILURE);
}
/* the path is a diskless client */
log_msg(LOG_MSG_DEBUG, DBG_IDLC_PATH_IS_DISKLESS_CLIENT, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_is_global_zone
* Description: determine if target is a global zone
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* IMPLEMENTATION:
* - must not be initial installation to the install root
* - must not be installation of a non-global zone
* - must not be a non-global zone
* - must not be a mounted mini-root
* - must not be a netinstall image
* - must not be a diskless client
* - if $ROOTDIR is "/":
* -- if zone name is "GLOBAL", then is a global zone;
* -- else not a global zone.
* - $ROOTDIR/etc/zones must exist and be a directory
* - $ROOTDIR/.tmp_proto must not exist
* - $ROOTDIR/var must exist and must not be a symbolic link
*/
static int
cmd_is_global_zone(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
int c;
int r;
static char *cmdName = "is_global_zone";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
/*
* a global zone cannot be any of the following
*/
/* cannot be a non-global zone */
r = cmd_is_nonglobal_zone(argc, argv, a_gdt);
/* cannot be a mounted miniroot */
if (r != R_SUCCESS) {
r = cmd_is_mounted_miniroot(argc, argv, a_gdt);
}
/* cannot be a netinstall image */
if (r != R_SUCCESS) {
r = cmd_is_netinstall_image(argc, argv, a_gdt);
}
/* cannot be a diskless client */
if (r != R_SUCCESS) {
r = cmd_is_diskless_client(argc, argv, a_gdt);
}
/* no need to guard against recursion any more */
recursion--;
/* return failure if any of the preceeding are true */
switch (r) {
case R_SUCCESS:
return (R_FAILURE);
case R_FAILURE:
break;
case R_USAGE:
case R_ERROR:
default:
return (r);
}
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* must not be initial installation to the install root */
if ((a_gdt->gd_initialInstall == B_TRUE) &&
(strcmp(a_gdt->gd_installRoot, rootPath) == 0)) {
/* initial install: install root cannot be global zone */
log_msg(LOG_MSG_DEBUG, DBG_ISGZ_INITIAL_INSTALL, rootPath);
return (R_FAILURE);
}
/* must not be installation of a non-global zone */
if (a_gdt->gd_nonglobalZoneInstall == B_TRUE) {
/* initial nonglobal zone install: no path can be global zone */
log_msg(LOG_MSG_DEBUG, DBG_ISGZ_NGZ_ZONE_INSTALL, rootPath);
return (R_FAILURE);
}
/* handle if global zone installation to the install root */
if ((a_gdt->gd_globalZoneInstall == B_TRUE) &&
(strcmp(a_gdt->gd_installRoot, rootPath) == 0)) {
/* the path is a global zone */
log_msg(LOG_MSG_DEBUG, DBG_ISGZ_PATH_IS_GLOBAL_ZONE,
rootPath);
return (R_SUCCESS);
}
/* true if current root is "/" and zone name is GLOBAL_ZONENAME */
if (strcmp(rootPath, "/") == 0) {
if (strcmp(a_gdt->gd_zoneName, GLOBAL_ZONENAME) == 0) {
/* the path is a global zone */
log_msg(LOG_MSG_DEBUG, DBG_ISGZ_PATH_IS_GLOBAL_ZONE,
rootPath);
return (R_SUCCESS);
}
/* inside a non-global zone */
log_msg(LOG_MSG_DEBUG, DBG_ISGZ_ZONENAME_ISNT_GLOBAL,
rootPath, a_gdt->gd_zoneName);
return (R_FAILURE);
}
/*
* current root is not "/" - see if target looks like a global zone
*
* - rootpath is not "/"
* - and $ROOTDIR/etc/zones exists
* - and $ROOTDIR/.tmp_proto does not exist
* - and $ROOTDIR/var is not a symbolic link
*/
/* not global zone if /etc/zones does not exist */
r = testPath(TEST_EXISTS|TEST_IS_DIRECTORY,
"%s/%s", rootPath, "/etc/zones");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_ISGZ_PATH_ISNT_DIRECTORY,
rootPath, "/etc/zones");
return (R_FAILURE);
}
/* .tmp_proto must not exist */
r = testPath(TEST_NOT_EXISTS,
"%s/%s", rootPath, ".tmp_proto");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_ISGZ_PATH_EXISTS,
rootPath, "/.tmp_proto");
return (R_FAILURE);
}
/* /var must not be a symbolic link */
r = testPath(TEST_EXISTS|TEST_NOT_SYMBOLIC_LINK,
"%s/%s", rootPath, "/var");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_ISGZ_PATH_IS_SYMLINK,
rootPath, "/var");
return (R_FAILURE);
}
/* the path is a global zone */
log_msg(LOG_MSG_DEBUG, DBG_ISGZ_PATH_IS_GLOBAL_ZONE, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_is_netinstall_image
* Description: determine if target is a net install image
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* IMPLEMENTATION:
* - must not be initial installation to the install root
* - must not be installation of a zone
* - must not be a global zone
* - must not be a mounted mini-root
* - zone name must be "global"
* - $ROOTDIR/.tmp_proto must exist and must be a directory
* - $ROOTDIR/var must exist and must be a symbolic link
* - $ROOTDIR/tmp/kernel must exist and must be a directory
* - $ROOTDIR/.tmp_proto/kernel must exist and must be a symbolic link
*/
static int
cmd_is_netinstall_image(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
int c;
int r;
static char *cmdName = "is_netinstall_image";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
/* a netinstall image cannot be a global zone */
r = cmd_is_global_zone(argc, argv, a_gdt);
/* no need to guard against recursion any more */
recursion--;
switch (r) {
case R_SUCCESS:
return (R_FAILURE);
case R_FAILURE:
break;
case R_USAGE:
case R_ERROR:
default:
return (r);
}
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* current zone name must be "global" */
if (strcmp(a_gdt->gd_zoneName, GLOBAL_ZONENAME) != 0) {
log_msg(LOG_MSG_DEBUG, DBG_INIM_BAD_CURRENT_ZONE,
rootPath, GLOBAL_ZONENAME);
return (R_FAILURE);
}
/* cannot be a mounted_miniroot */
if (cmd_is_mounted_miniroot(argc, argv, a_gdt) == R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_IMRT_PATH_IS_MOUNTED_MINIROOT,
rootPath);
return (R_FAILURE);
}
/* $ROOTDIR/.tmp_proto exists */
r = testPath(TEST_EXISTS|TEST_IS_DIRECTORY,
"%s/%s", rootPath, ".tmp_proto");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_INIM_PATH_ISNT_DIRECTORY,
rootPath, "/.tmp_proto");
return (R_FAILURE);
}
/* $ROOTDIR/var is a symbolic link */
r = testPath(TEST_IS_SYMBOLIC_LINK,
"%s/%s", rootPath, "/var");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_INIM_PATH_ISNT_SYMLINK,
rootPath, "/var");
return (R_FAILURE);
}
/* $ROOTDIR/tmp/kernel does exist */
r = testPath(TEST_EXISTS|TEST_IS_DIRECTORY,
"%s/%s", rootPath, "/tmp/kernel");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_INIM_PATH_ISNT_DIRECTORY,
rootPath, "/tmp/kernel");
return (R_FAILURE);
}
/* $ROOTDIR/.tmp_proto/kernel is a symbolic link */
r = testPath(TEST_IS_SYMBOLIC_LINK,
"%s/%s", rootPath, "/.tmp_proto/kernel");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_INIM_PATH_ISNT_SYMLINK,
rootPath, "/.tmp_proto/kernel");
return (R_FAILURE);
}
/* must not be initial installation to the install root */
if ((a_gdt->gd_initialInstall == B_TRUE) &&
(strcmp(a_gdt->gd_installRoot, rootPath) == 0)) {
/* initial install: install root cannot be netinstall image */
log_msg(LOG_MSG_DEBUG, DBG_INIM_INITIAL_INSTALL, rootPath);
return (R_FAILURE);
}
/* must not be installation of a zone */
if ((a_gdt->gd_globalZoneInstall == B_TRUE) ||
(a_gdt->gd_nonglobalZoneInstall == B_TRUE)) {
/* initial zone install: no path can be netinstall image */
log_msg(LOG_MSG_DEBUG, DBG_INIM_ZONE_INSTALL, rootPath);
return (R_FAILURE);
}
/* target is a netinstall image */
log_msg(LOG_MSG_DEBUG, DBG_INIM_PATH_IS_NETINSTALL_IMAGE, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_is_mounted_miniroot
* Description: determine if target is a mounted miniroot image
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* IMPLEMENTATION:
* - must not be initial installation to the install root
* - must not be installation of a zone
* - zone name must be "global"
* - $ROOTDIR/tmp/kernel must exist and must be a symbolic link
* - $ROOTDIR/tmp/root/kernel must exist and must be a directory
*/
static int
cmd_is_mounted_miniroot(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
int c;
int r;
static char *cmdName = "is_mounted_miniroot";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
recursion--;
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* current zone name must be "global" */
if (strcmp(a_gdt->gd_zoneName, GLOBAL_ZONENAME) != 0) {
log_msg(LOG_MSG_DEBUG, DBG_IMRT_BAD_CURRENT_ZONE,
rootPath, GLOBAL_ZONENAME);
return (R_FAILURE);
}
/* $ROOTDIR/tmp/kernel is a symbolic link */
r = testPath(TEST_IS_SYMBOLIC_LINK,
"%s/%s", rootPath, "/tmp/kernel");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_IMRT_PATH_ISNT_SYMLINK,
rootPath, "/tmp/kernel");
return (R_FAILURE);
}
/* $ROOTDIR/tmp/root/kernel is a directory */
r = testPath(TEST_EXISTS|TEST_IS_DIRECTORY,
"%s/%s", rootPath, "/tmp/root/kernel");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_IMRT_PATH_ISNT_DIRECTORY,
rootPath, "/tmp/root/kernel");
return (R_FAILURE);
}
/* must not be initial installation to the install root */
if ((a_gdt->gd_initialInstall == B_TRUE) &&
(strcmp(a_gdt->gd_installRoot, rootPath) == 0)) {
/* initial install: install root cannot be mounted miniroot */
log_msg(LOG_MSG_DEBUG, DBG_IMRT_INITIAL_INSTALL, rootPath);
return (R_FAILURE);
}
/* must not be installation of a zone */
if ((a_gdt->gd_globalZoneInstall == B_TRUE) ||
(a_gdt->gd_nonglobalZoneInstall == B_TRUE)) {
/* initial zone install: no path can be mounted miniroot */
log_msg(LOG_MSG_DEBUG, DBG_IMRT_ZONE_INSTALL, rootPath);
return (R_FAILURE);
}
/* target is a mounted miniroot */
log_msg(LOG_MSG_DEBUG, DBG_IMRT_PATH_IS_MOUNTED_MINIROOT, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_is_nonglobal_zone
* Description: determine if target is a global zone
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* - must not be initial installation to the install root
* - must not be installation of a global zone
* - success if installation of a non-global zone
*/
static int
cmd_is_nonglobal_zone(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
int c;
int r;
static char *cmdName = "is_nonglobal_zone";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
recursion--;
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* handle if non-global zone installation to the install root */
if ((a_gdt->gd_nonglobalZoneInstall == B_TRUE) &&
(strcmp(a_gdt->gd_installRoot, rootPath) == 0)) {
log_msg(LOG_MSG_DEBUG, DBG_NGZN_INSTALL_ZONENAME_IS_NGZ,
rootPath, a_gdt->gd_zoneName);
return (R_SUCCESS);
}
/* must not be initial installation to the install root */
if ((a_gdt->gd_initialInstall == B_TRUE) &&
(strcmp(a_gdt->gd_installRoot, rootPath) == 0)) {
/* initial install: install root cannot be non-global zone */
log_msg(LOG_MSG_DEBUG, DBG_NGZN_INITIAL_INSTALL, rootPath);
return (R_FAILURE);
}
/* must not be installation of a global zone */
if ((a_gdt->gd_globalZoneInstall == B_TRUE) ||
(a_gdt->gd_nonglobalZoneInstall == B_TRUE)) {
/* initial global zone install: no path can be nonglobal zone */
log_msg(LOG_MSG_DEBUG, DBG_NGZN_GLOBAL_ZONE_INSTALL, rootPath);
return (R_FAILURE);
}
/*
* *********************************************************************
* if root directory is "/" then the only thing that needs to be done is
* to test the zone name directly - if the zone name is "global" then
* the target is not a non-global zone; otherwise if the zone name is
* not "global" then the target IS a non-global zone.
* *********************************************************************
*/
if (strcmp(rootPath, "/") == 0) {
/* target is current running root */
if (strcmp(a_gdt->gd_zoneName, GLOBAL_ZONENAME) == 0) {
/* in the global zone */
log_msg(LOG_MSG_DEBUG, DBG_NGZN_ZONENAME_ISNT_NGZ,
rootPath, a_gdt->gd_zoneName);
return (R_FAILURE);
}
/* in a non-global zone */
log_msg(LOG_MSG_DEBUG, DBG_NGZN_ZONENAME_IS_NGZ,
rootPath, a_gdt->gd_zoneName);
return (R_SUCCESS);
}
/*
* $ROOTDIR/etc/zones/index must exist in a global zone. It also
* exists in a non-global zone after s10u4 but we can't check that
* since it is undeterministic for all releases so we only check
* for the global zone here.
*/
r = testPath(TEST_EXISTS, "%s/%s", rootPath, "/etc/zones/index");
if (r == R_SUCCESS) {
/* See if "global" exists in .../etc/zones/index */
if (testPath(TEST_GLOBAL_TOKEN_IN_FILE, "%s/%s", rootPath,
"/etc/zones/index") != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_NGZN_ZONENAME_ISNT_NGZ,
rootPath, GLOBAL_ZONENAME);
return (R_FAILURE);
}
}
/*
* *********************************************************************
* If the root directory is "/" then you can use only the zone
* name to determine if the zone is non-global or not since the
* package is being installed or removed to the current "zone".
*
* Since the root directory being tested is not "/" then you have to
* look into the target to try and infer zone type using means other
* than the zone name only.
* *********************************************************************
*/
/* reject if any items found that cannot be in a non-global zone */
/* .tmp_proto must not exist */
r = testPath(TEST_NOT_EXISTS, "%s/%s", rootPath, ".tmp_proto");
if (r != R_SUCCESS) {
/* $R/.tmp_proto cannot exist in a non-global zone */
log_msg(LOG_MSG_DEBUG, DBG_NGZN_PATH_EXISTS,
rootPath, "/.tmp_proto");
return (R_FAILURE);
}
/* /var must not be a symbolic link */
r = testPath(TEST_EXISTS|TEST_NOT_SYMBOLIC_LINK,
"%s/%s", rootPath, "/var");
if (r != R_SUCCESS) {
/* $R/var cannot be a symbolic link in a non-global zone */
log_msg(LOG_MSG_DEBUG, DBG_NGZN_PATH_DOES_NOT_EXIST,
rootPath, "/var");
return (R_FAILURE);
}
/* $ROOTDIR/tmp/root/kernel must not exist */
r = testPath(TEST_NOT_EXISTS,
"%s/%s", rootPath, "/tmp/root/kernel");
if (r != R_SUCCESS) {
/* $R/tmp/root/kernel cannot exist in a non-global zone */
log_msg(LOG_MSG_DEBUG, DBG_NGZN_PATH_EXISTS,
rootPath, "/tmp/root/kernel");
return (R_FAILURE);
}
/*
* *********************************************************************
* no items exist in $ROOTDIR that identify something other than
* a non-global zone.
*
* if in global zone no more tests possible: is a non-global zone
* *********************************************************************
*/
if (strcmp(a_gdt->gd_zoneName, GLOBAL_ZONENAME) == 0) {
/* in the global zone */
log_msg(LOG_MSG_DEBUG, DBG_NGZN_IN_GZ_IS_NONGLOBAL_ZONE,
rootPath);
return (R_SUCCESS);
}
/*
* *********************************************************************
* In non-global zone: interrogate zone name and type.
*
* The parent zone is the zone that the "pkgadd" or "pkgrm" command was
* run in. The child zone is the zone that the "pkginstall" or
* "pkgremove" command was run in.
* *********************************************************************
*/
/*
* If parent zone name and current zone name defined, and
* both zone names are the same, since pkgcond is running
* inside of a non-global zone, this is how the scratch
* zone is implemented, so target is a non-global zone
*/
if ((a_gdt->gd_parentZoneName != NULL) &&
(a_gdt->gd_currentZoneName != NULL) &&
(strcmp(a_gdt->gd_parentZoneName,
a_gdt->gd_currentZoneName) == 0)) {
/* parent and current zone name identical: non-gz */
log_msg(LOG_MSG_DEBUG, DBG_NGZN_PARENT_CHILD_SAMEZONE,
rootPath, a_gdt->gd_parentZoneName);
return (R_SUCCESS);
}
/*
* In non-global zone if zone specific read only FS's exist
* or it is in a mounted state.
*/
if (a_gdt->inMountedState) {
log_msg(LOG_MSG_DEBUG, DBG_NGZN_IS_NONGLOBAL_ZONE, rootPath);
return (R_SUCCESS);
}
/*
* the parent and current zone name are not the same;
* interrogate the zone types: the parent must be global
* and the current must be non-global, which would be set
* when a package command is run in the global zone that in
* turn runs a package command within the non-global zone.
*/
/* if defined, parent zone type must be "global" */
if ((a_gdt->gd_parentZoneType != NULL) &&
(strcmp(a_gdt->gd_parentZoneType, "nonglobal") == 0)) {
log_msg(LOG_MSG_DEBUG, DBG_NGZN_BAD_PARENT_ZONETYPE,
rootPath, "nonglobal");
return (R_FAILURE);
}
/* if defined, current zone type must be "nonglobal" */
if ((a_gdt->gd_currentZoneType != NULL) &&
(strcmp(a_gdt->gd_currentZoneType, GLOBAL_ZONENAME) == 0)) {
log_msg(LOG_MSG_DEBUG, DBG_NGZN_BAD_CURRENT_ZONETYPE,
rootPath, GLOBAL_ZONENAME);
return (R_FAILURE);
}
/*
* *********************************************************************
* no other tests possible: target is a non-global zone
* *********************************************************************
*/
log_msg(LOG_MSG_DEBUG, DBG_NGZN_IS_NONGLOBAL_ZONE, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_is_running_system
* Description: determine if target is a global zone
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* IMPLEMENTATION:
* - must not be initial installation to the install root
* - must not be installation of a zone
* - must not be a diskless client
* - $ROOTDIR must be "/"
* - zone name must be "global"
*/
static int
cmd_is_running_system(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
int c;
int r;
static char *cmdName = "is_running_system";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
/* a running system cannot be a diskless client */
r = cmd_is_diskless_client(argc, argv, a_gdt);
/* no need to guard against recursion any more */
recursion--;
switch (r) {
case R_SUCCESS:
return (R_FAILURE);
case R_FAILURE:
break;
case R_USAGE:
case R_ERROR:
default:
return (r);
}
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* if root path is "/" then check zone name */
if (strcmp(rootPath, "/") != 0) {
log_msg(LOG_MSG_DEBUG, DBG_IRST_ROOTPATH_BAD, rootPath, "/");
return (R_FAILURE);
}
/* zone name must be global */
if (strcmp(a_gdt->gd_zoneName, GLOBAL_ZONENAME) != 0) {
log_msg(LOG_MSG_DEBUG, DBG_IRST_ZONE_BAD, rootPath,
GLOBAL_ZONENAME);
return (R_FAILURE);
}
/* must not be initial installation to the install root */
if ((a_gdt->gd_initialInstall == B_TRUE) &&
(strcmp(a_gdt->gd_installRoot, rootPath) == 0)) {
/* initial install: install root cannot be the running system */
log_msg(LOG_MSG_DEBUG, DBG_IRST_INITIAL_INSTALL, rootPath);
return (R_FAILURE);
}
/* must not be installation of a zone */
if ((a_gdt->gd_globalZoneInstall == B_TRUE) ||
(a_gdt->gd_nonglobalZoneInstall == B_TRUE)) {
/* initial zone install: no path can be running system */
log_msg(LOG_MSG_DEBUG, DBG_IRST_ZONE_INSTALL, rootPath);
return (R_FAILURE);
}
/* target is a running system */
log_msg(LOG_MSG_DEBUG, DBG_IRST_PATH_IS_RUNNING_SYSTEM, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_can_add_driver
* Description: determine if target is a global zone
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* Implementation:
* A driver can be added to the system if the components of a Solaris
* instance capable of loading drivers is present and it is not the
* currently running system.
*/
static int
cmd_can_add_driver(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
int c;
int r;
static char *cmdName = "can_add_driver";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
/* see if this is the current running system */
r = cmd_is_running_system(argc, argv, a_gdt);
/* cannot be a diskless client */
if (r != R_SUCCESS) {
r = cmd_is_diskless_client(argc, argv, a_gdt);
}
/* no need to guard against recursion any more */
recursion--;
switch (r) {
case R_SUCCESS:
/* is a running system */
return (R_FAILURE);
case R_FAILURE:
/* not a running syste */
break;
case R_USAGE:
case R_ERROR:
default:
/* cannot determine if is a running system */
return (r);
}
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* /etc must exist and must not be a symbolic link */
r = testPath(TEST_EXISTS|TEST_NOT_SYMBOLIC_LINK,
"%s/%s", rootPath, "/etc");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_ADDV_PATH_IS_SYMLINK,
rootPath, "/etc");
return (R_FAILURE);
}
/* /platform must exist and must not be a symbolic link */
r = testPath(TEST_EXISTS|TEST_NOT_SYMBOLIC_LINK,
"%s/%s", rootPath, "/platform");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_ADDV_PATH_IS_SYMLINK,
rootPath, "/platform");
return (R_FAILURE);
}
/* /kernel must exist and must not be a symbolic link */
r = testPath(TEST_EXISTS|TEST_NOT_SYMBOLIC_LINK,
"%s/%s", rootPath, "/kernel");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_ADDV_PATH_IS_SYMLINK,
rootPath, "/kernel");
return (R_FAILURE);
}
/* can add a driver */
log_msg(LOG_MSG_DEBUG, DBG_ADDV_YES, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_can_update_driver
* Description: determine if target is a global zone
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* Implementation:
* A driver can be added to the system if the components of a Solaris
* instance capable of loading drivers is present and it is not the
* currently running system.
*/
static int
cmd_can_update_driver(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
int c;
int r;
static char *cmdName = "can_update_driver";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
/* see if this is the current running system */
r = cmd_is_running_system(argc, argv, a_gdt);
/* cannot be a diskless client */
if (r != R_SUCCESS) {
r = cmd_is_diskless_client(argc, argv, a_gdt);
}
/* no need to guard against recursion any more */
recursion--;
switch (r) {
case R_SUCCESS:
/* is a running system */
return (R_FAILURE);
case R_FAILURE:
/* not a running syste */
break;
case R_USAGE:
case R_ERROR:
default:
/* cannot determine if is a running system */
return (r);
}
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* /etc must exist and must not be a symbolic link */
r = testPath(TEST_EXISTS|TEST_NOT_SYMBOLIC_LINK,
"%s/%s", rootPath, "/etc");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_UPDV_PATH_IS_SYMLINK,
rootPath, "/etc");
return (R_FAILURE);
}
/* /platform must exist and must not be a symbolic link */
r = testPath(TEST_EXISTS|TEST_NOT_SYMBOLIC_LINK,
"%s/%s", rootPath, "/platform");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_UPDV_PATH_IS_SYMLINK,
rootPath, "/platform");
return (R_FAILURE);
}
/* /kernel must exist and must not be a symbolic link */
r = testPath(TEST_EXISTS|TEST_NOT_SYMBOLIC_LINK,
"%s/%s", rootPath, "/kernel");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_UPDV_PATH_IS_SYMLINK,
rootPath, "/kernel");
return (R_FAILURE);
}
/* can update driver */
log_msg(LOG_MSG_DEBUG, DBG_UPDV_YES, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_can_remove_driver
* Description: determine if target is a global zone
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* Implementation:
* A driver can be added to the system if the components of a Solaris
* instance capable of loading drivers is present and it is not the
* currently running system.
*/
static int
cmd_can_remove_driver(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
int c;
int r;
static char *cmdName = "can_remove_driver";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
/* see if this is the current running system */
r = cmd_is_running_system(argc, argv, a_gdt);
/* cannot be a diskless client */
if (r != R_SUCCESS) {
r = cmd_is_diskless_client(argc, argv, a_gdt);
}
/* no need to guard against recursion any more */
recursion--;
switch (r) {
case R_SUCCESS:
/* is a running system */
return (R_FAILURE);
case R_FAILURE:
/* not a running syste */
break;
case R_USAGE:
case R_ERROR:
default:
/* cannot determine if is a running system */
return (r);
}
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* /etc must exist and must not be a symbolic link */
r = testPath(TEST_EXISTS|TEST_NOT_SYMBOLIC_LINK,
"%s/%s", rootPath, "/etc");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_RMDV_PATH_IS_SYMLINK,
rootPath, "/etc");
return (R_FAILURE);
}
/* /platform must exist and must not be a symbolic link */
r = testPath(TEST_EXISTS|TEST_NOT_SYMBOLIC_LINK,
"%s/%s", rootPath, "/platform");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_RMDV_PATH_IS_SYMLINK,
rootPath, "/platform");
return (R_FAILURE);
}
/* /kernel must exist and must not be a symbolic link */
r = testPath(TEST_EXISTS|TEST_NOT_SYMBOLIC_LINK,
"%s/%s", rootPath, "/kernel");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_RMDV_PATH_IS_SYMLINK,
rootPath, "/kernel");
return (R_FAILURE);
}
/* can remove driver */
log_msg(LOG_MSG_DEBUG, DBG_RMDV_YES, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_is_path_writable
* Description: determine if target path is writable
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* IMPLEMENTATION:
* - path must be found in the file systems configured
* - mount options must not include "read only"
*/
static int
cmd_is_path_writable(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
FSI_T *list;
char *rootPath = NULL;
int c;
int n;
int nn;
int r;
long listSize;
long rootPathLen;
static char *cmdName = "is_path_writable";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
recursion--;
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc != 1) {
(void) usage(ERR_REQUIRED_ROOTPATH_MISSING, cmdName);
return (R_USAGE);
}
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* search file system conf for this path */
rootPathLen = strlen(rootPath);
list = a_gdt->gd_fileSystemConfig;
listSize = a_gdt->gd_fileSystemConfigLen;
for (nn = 0, n = 0; n < listSize; n++) {
long mplen = strlen(list[n].fsi_mntPoint);
if (rootPathLen < mplen) {
/* root path is longer than target, ignore */
continue;
}
if (strncmp(rootPath, list[n].fsi_mntPoint, mplen) == 0) {
/* remember last partial match */
nn = n;
}
}
log_msg(LOG_MSG_DEBUG, DBG_PWRT_INFO,
rootPath, list[nn].fsi_mntPoint, list[nn].fsi_fsType,
list[nn].fsi_mntOptions);
/*
* need to determine if the mount point is writeable:
*/
/* see if the file system is mounted with the "read only" option */
r = mountOptionPresent(list[nn].fsi_mntOptions, MNTOPT_RO);
if (r == R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_PWRT_READONLY,
rootPath, list[nn].fsi_mntOptions);
return (R_FAILURE);
}
/* target path is writable */
log_msg(LOG_MSG_DEBUG, DBG_PWRT_IS, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_is_alternative_root
* Description: determine if target is an alternative root
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* Implementation:
* - success if an initial installation to the install root
* (an initial install to $PKG_INSTALL_ROOT means that $PKG_INSTALL_ROOT
* points to an alternative root that is under construction)
* - must not be installation of a zone
* - must not be a boot environment
* - must not be a diskless client
* - must not be a mounted miniroot
* - must not be a netinstall image
* - must not be a nonglobal zone
* - must not be a running system
* - $ROOTDIR must not be "/"
* - $ROOTDIR/var must exist
*/
static int
cmd_is_alternative_root(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
int c;
int r;
static char *cmdName = "is_alternative_root";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
/*
* an alternative root cannot be any of the following
*/
/* cannot be a boot_environment */
r = cmd_is_boot_environment(argc, argv, a_gdt);
/* cannot be a diskless_client */
if (r != R_SUCCESS) {
r = cmd_is_diskless_client(argc, argv, a_gdt);
}
/* cannot be a mounted_miniroot */
if (r != R_SUCCESS) {
r = cmd_is_mounted_miniroot(argc, argv, a_gdt);
}
/* cannot be a netinstall_image */
if (r != R_SUCCESS) {
r = cmd_is_netinstall_image(argc, argv, a_gdt);
}
/* cannot be a nonglobal_zone */
if (r != R_SUCCESS) {
r = cmd_is_nonglobal_zone(argc, argv, a_gdt);
}
/* cannot be a running_system */
if (r != R_SUCCESS) {
r = cmd_is_running_system(argc, argv, a_gdt);
}
/* no need to guard against recursion any more */
recursion--;
/* return failure if any of the preceeding are true */
switch (r) {
case R_SUCCESS:
return (R_FAILURE);
case R_FAILURE:
break;
case R_USAGE:
case R_ERROR:
default:
return (r);
}
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* return success if initial installation */
if ((a_gdt->gd_initialInstall == B_TRUE) &&
(strcmp(a_gdt->gd_installRoot, rootPath) == 0)) {
log_msg(LOG_MSG_DEBUG, DBG_IALR_INITIAL_INSTALL, rootPath);
return (R_SUCCESS);
}
/* root path must not be "/" */
if (strcmp(rootPath, "/") == 0) {
log_msg(LOG_MSG_DEBUG, DBG_IALR_BAD_ROOTPATH, rootPath, "/");
return (R_FAILURE);
}
/* /var must exist */
r = testPath(TEST_EXISTS,
"%s/%s", rootPath, "/var");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_IALR_PATH_DOES_NOT_EXIST,
rootPath, "/var");
return (R_FAILURE);
}
/* must not be installation of a zone */
if ((a_gdt->gd_globalZoneInstall == B_TRUE) ||
(a_gdt->gd_nonglobalZoneInstall == B_TRUE)) {
/* initial zone install: no path can be alternative root */
log_msg(LOG_MSG_DEBUG, DBG_IALR_ZONE_INSTALL, rootPath);
return (R_FAILURE);
}
/* target is an alternative root */
log_msg(LOG_MSG_DEBUG, DBG_IALR_IS, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_is_boot_environment
* Description: determine if target is an alternative, inactive boot environment
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
* IMPLEMENTATION:
* - must not be initial installation to the install root
* - must not be installation of a zone
* - must not be a diskless client
* - must not be a netinstall image
* - must not be a mounted miniroot
* - $ROOTDIR must not be "/"
* - $ROOTDIR/etc/lutab must exist
* - $ROOTDIR/etc/lu must exist and must be a directory
*/
static int
cmd_is_boot_environment(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
int c;
int r;
static char *cmdName = "is_boot_environment";
static int recursion = 0;
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* prevent recursion */
if (recursionCheck(&recursion, cmdName) == B_FALSE) {
/*
* a boot environment cannot be any of the following
*/
/* cannot be a diskless client */
r = cmd_is_diskless_client(argc, argv, a_gdt);
/* cannot be a netinstall_image */
if (r != R_SUCCESS) {
r = cmd_is_netinstall_image(argc, argv, a_gdt);
}
/* cannot be a mounted_miniroot */
if (r != R_SUCCESS) {
r = cmd_is_mounted_miniroot(argc, argv, a_gdt);
}
/* no need to guard against recursion any more */
recursion--;
/* return failure if any of the preceeding are true */
switch (r) {
case R_SUCCESS:
return (R_FAILURE);
case R_FAILURE:
break;
case R_USAGE:
case R_ERROR:
default:
return (r);
}
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* root path must not be "/" */
if (strcmp(rootPath, "/") == 0) {
log_msg(LOG_MSG_DEBUG, DBG_BENV_BAD_ROOTPATH, rootPath, "/");
return (R_FAILURE);
}
/* zone name must be global */
if (strcmp(a_gdt->gd_zoneName, GLOBAL_ZONENAME) != 0) {
log_msg(LOG_MSG_DEBUG, DBG_BENV_BAD_ZONE, rootPath,
GLOBAL_ZONENAME);
return (R_FAILURE);
}
/* $ROOTDIR/etc/lutab must exist */
r = testPath(TEST_EXISTS, "%s/%s", rootPath, "/etc/lutab");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_BENV_NO_ETCLUTAB, rootPath,
"/etc/lutab");
return (R_FAILURE);
}
/* $ROOTDIR/etc/lu must exist */
r = testPath(TEST_EXISTS|TEST_IS_DIRECTORY,
"%s/%s", rootPath, "/etc/lu");
if (r != R_SUCCESS) {
log_msg(LOG_MSG_DEBUG, DBG_BENV_NO_ETCLU, rootPath, "/etc/lu");
return (R_FAILURE);
}
/* must not be initial installation */
if ((a_gdt->gd_initialInstall == B_TRUE) &&
(strcmp(a_gdt->gd_installRoot, rootPath) == 0)) {
log_msg(LOG_MSG_DEBUG, DBG_BENV_INITIAL_INSTALL, rootPath);
return (R_FAILURE);
}
/* must not be installation of a zone */
if ((a_gdt->gd_globalZoneInstall == B_TRUE) ||
(a_gdt->gd_nonglobalZoneInstall == B_TRUE)) {
/* initial zone install: no path can be boot environment */
log_msg(LOG_MSG_DEBUG, DBG_BENV_ZONE_INSTALL, rootPath);
return (R_FAILURE);
}
/* target is a boot environment */
log_msg(LOG_MSG_DEBUG, DBG_BENV_IS, rootPath);
return (R_SUCCESS);
}
/*
* Name: cmd_is_what
* Description: determine what the target is
* Scope: public
* Arguments: argc,argv:
* - optional path to target to test
* Returns: int
* == 0 - success
* != 0 - failure
*/
static int
cmd_is_what(int argc, char **argv, GLOBALDATA_T *a_gdt)
{
char *rootPath = NULL;
int c;
int cur_cmd;
int r;
static char *cmdName = "is_what";
/* process any command line options */
while ((c = getopt(argc, argv, ":")) != EOF) {
switch (c) {
case '\0': /* prevent end-of-loop not reached warning */
break;
case '?':
default:
(void) usage(MSG_IS_INVALID_OPTION, optopt, cmdName);
return (R_USAGE);
}
}
/* normalize argc/argv */
argc -= optind;
argv += optind;
/* error if more than one argument */
if (argc > 1) {
log_msg(LOG_MSG_ERR, ERR_UNRECOGNIZED_OPTION, argv[1]);
(void) usage(MSG_IS_INVALID_OPTION, argv[1]);
return (R_USAGE);
}
/* process root path if first argument present */
if (argc == 1) {
if (setRootPath(argv[0], "argv[0]", B_TRUE) != R_SUCCESS) {
return (R_ERROR);
}
}
/* get current root path */
r = getRootPath(&rootPath);
if (r != R_SUCCESS) {
return (r);
}
/*
* construct the command line for all of the packages
*/
argc = 0;
argv[argc++] = strdup(get_prog_name());
argv[argc++] = strdup(rootPath);
/* start of command debugging information */
echoDebug(DBG_ROOTPATH_IS, rootPath);
/* search for specified subcommand and execute if found */
for (cur_cmd = 0; cmds[cur_cmd].c_name != NULL; cur_cmd++) {
int result;
/* do not recursively call this function */
if (cmds[cur_cmd].c_func == cmd_is_what) {
continue;
}
/* call subcommand with its own argc/argv */
result = cmds[cur_cmd].c_func(argc, argv, a_gdt);
/* process result code and exit */
result = adjustResults(result);
log_msg(LOG_MSG_INFO, MSG_IS_WHAT_RESULT,
cmds[cur_cmd].c_name, result);
}
return (R_SUCCESS);
}
/*
* *****************************************************************************
* utility support functions
* *****************************************************************************
*/
/*
* Name: getMountOption
* Description: return next mount option in a string
* Arguments: p - pointer to string containing mount options
* Output: none
* Returns: char * - pointer to next option in string "p"
* Side Effects: advances input "p" and inserts \0 in place of the
* option separator found.
*/
static char *
getMountOption(char **p)
{
char *cp = *p;
char *retstr;
/* advance past all white space */
while (*cp && isspace(*cp))
cp++;
/* remember start of next option */
retstr = cp;
/* advance to end of string or option separator */
while (*cp && *cp != ',')
cp++;
/* replace separator with '\0' if not at end of string */
if (*cp) {
*cp = '\0';
cp++;
}
/* reset caller's pointer and return pointer to option */
*p = cp;
return (retstr);
}
/*
* Name: mountOptionPresent
* Description: determine if specified mount option is present in list
* of mount point options
* Arguments: a_mntOptions - pointer to string containing list of mount
* point options to search
* a_opt - pointer to string containing option to search for
* Output: none
* Returns: R_SUCCESS - option is present in list of mount point options
* R_FAILURE - options is not present
* R_ERROR - unable to determine if option is present or not
*/
static int
mountOptionPresent(char *a_mntOptions, char *a_opt)
{
char tmpopts[MNT_LINE_MAX];
char *f, *opts = tmpopts;
/* return false if no mount options present */
if ((a_opt == NULL) || (*a_opt == '\0')) {
return (R_FAILURE);
}
/* return not present if no list of options to search */
if (a_mntOptions == NULL) {
return (R_FAILURE);
}
/* return not present if list of options to search is empty */
if (*a_mntOptions == '\0') {
return (R_FAILURE);
}
/* make local copy of option list to search */
(void) strcpy(opts, a_mntOptions);
/* scan each option looking for the specified option */
f = getMountOption(&opts);
for (; *f; f = getMountOption(&opts)) {
/* return success if option matches target */
if (strncmp(a_opt, f, strlen(a_opt)) == 0) {
return (R_SUCCESS);
}
}
/* option not found */
return (R_FAILURE);
}
/*
* Name: sortedInsert
* Description: perform an alphabetical sorted insert into a list
* Arguments: r_list - pointer to list to insert next entry into
* a_listSize - pointer to current list size
* a_mntPoint - mount point to insert (is sort key)
* a_fsType - file system type for mount point
* a_mntOptions - file syste mount options for mount point
* Output: None
* Returns: None
*/
static void
sortedInsert(FSI_T **r_list, long *a_listSize, char *a_mntPoint,
char *a_fsType, char *a_mntOptions)
{
int listSize;
FSI_T *list;
int n;
/* entry assertions */
assert(a_listSize != (long *)NULL);
assert(a_mntPoint != NULL);
assert(a_fsType != NULL);
assert(a_mntOptions != NULL);
/* entry debugging info */
echoDebug(DBG_SINS_ENTRY, a_mntPoint, a_fsType, a_mntOptions);
/* localize references to the list and list size */
listSize = *a_listSize;
list = *r_list;
/*
* if list empty insert this entry as the first one in the list
*/
if (listSize == 0) {
/* allocate new entry for list */
listSize++;
list = (FSI_T *)realloc(list, sizeof (FSI_T)*(listSize+1));
/* first entry is data passed to this function */
list[0].fsi_mntPoint = strdup(a_mntPoint);
list[0].fsi_fsType = strdup(a_fsType);
list[0].fsi_mntOptions = strdup(a_mntOptions);
/* second entry is all NULL - end of entry marker */
list[1].fsi_mntPoint = NULL;
list[1].fsi_fsType = NULL;
list[1].fsi_mntOptions = NULL;
/* restore list and list size references to caller */
*a_listSize = listSize;
*r_list = list;
return;
}
/*
* list not empty - scan looking for largest match
*/
for (n = 0; n < listSize; n++) {
int c;
/* compare target with current list entry */
c = strcmp(list[n].fsi_mntPoint, a_mntPoint);
if (c == 0) {
char *me;
long len;
/* entry already in list -- merge entries */
len = strlen(list[n].fsi_mntOptions) +
strlen(a_mntOptions) + 2;
me = (char *)calloc(1, len);
/* merge two mount options lists into one */
(void) strlcat(me, list[n].fsi_mntOptions, len);
(void) strlcat(me, ",", len);
(void) strlcat(me, a_mntOptions, len);
/* free old list, replace with merged one */
free(list[n].fsi_mntOptions);
list[n].fsi_mntOptions = me;
echoDebug(DBG_SORTEDINS_SKIPPED,
n, list[n].fsi_mntPoint, a_fsType,
list[n].fsi_fsType, a_mntOptions,
list[n].fsi_mntOptions);
continue;
} else if (c < 0) {
/* entry before this one - skip */
continue;
}
/*
* entry after this one - insert new entry
*/
/* allocate one more entry and make space for new entry */
listSize++;
list = (FSI_T *)realloc(list,
sizeof (FSI_T)*(listSize+1));
(void) memmove(&(list[n+1]), &(list[n]),
sizeof (FSI_T)*(listSize-n));
/* insert this entry into list */
list[n].fsi_mntPoint = strdup(a_mntPoint);
list[n].fsi_fsType = strdup(a_fsType);
list[n].fsi_mntOptions = strdup(a_mntOptions);
/* restore list and list size references to caller */
*a_listSize = listSize;
*r_list = list;
return;
}
/*
* all entries are before this one - append to end of list
*/
/* allocate new entry at end of list */
listSize++;
list = (FSI_T *)realloc(list, sizeof (FSI_T)*(listSize+1));
/* append this entry to the end of the list */
list[listSize-1].fsi_mntPoint = strdup(a_mntPoint);
list[listSize-1].fsi_fsType = strdup(a_fsType);
list[listSize-1].fsi_mntOptions = strdup(a_mntOptions);
/* restore list and list size references to caller */
*a_listSize = listSize;
*r_list = list;
}
/*
* Name: calculateFileSystemConfig
* Description: generate sorted list of all mounted file systems
* Arguments: a_gdt - global data structure to place sorted entries into
* Output: None
* Returns: R_SUCCESS - successfully generated mounted file systems list
* R_FAILURE - options is not present
* R_ERROR - unable to determine if option is present or not
*/
static int
calculateFileSystemConfig(GLOBALDATA_T *a_gdt)
{
FILE *fp;
struct mnttab mntbuf;
FSI_T *list;
long listSize;
/* entry assetions */
assert(a_gdt != (GLOBALDATA_T *)NULL);
/* allocate a list that has one termination entry */
list = (FSI_T *)calloc(1, sizeof (FSI_T));
list[0].fsi_mntPoint = NULL;
list[0].fsi_fsType = NULL;
list[0].fsi_mntOptions = NULL;
listSize = 0;
/* open the mount table for reading */
fp = fopen(MNTTAB, "r");
if (fp == (FILE *)NULL) {
return (R_ERROR);
}
/* debugging info */
echoDebug(DBG_CALCSCFG_MOUNTED);
/* go through all the specials looking for the device */
while (getmntent(fp, &mntbuf) == 0) {
if (mntbuf.mnt_mountp[0] == '/') {
sortedInsert(&list, &listSize,
strdup(mntbuf.mnt_mountp),
strdup(mntbuf.mnt_fstype),
strdup(mntbuf.mnt_mntopts ?
mntbuf.mnt_mntopts : ""));
}
/*
* Set flag if we are in a non-global zone and it is in
* the mounted state.
*/
if (strcmp(mntbuf.mnt_mountp, "/a") == 0 &&
strcmp(mntbuf.mnt_special, "/a") == 0 &&
strcmp(mntbuf.mnt_fstype, "lofs") == 0) {
a_gdt->inMountedState = B_TRUE;
}
}
/* close mount table file */
(void) fclose(fp);
/* store list pointers in global data structure */
a_gdt->gd_fileSystemConfig = list;
a_gdt->gd_fileSystemConfigLen = listSize;
return (R_SUCCESS);
}
/*
* Name: adjustResults
* Description: adjust output result code before existing
* Arguments: a_result - result code to adjust
* Returns: int - adjusted result code
*/
static int
adjustResults(int a_result)
{
boolean_t negate = getNegateResults();
int realResult;
/* adjust code as appropriate */
switch (a_result) {
case R_SUCCESS: /* condition satisfied */
realResult = ((negate == B_TRUE) ? 1 : 0);
break;
case R_FAILURE: /* condition not satisfied */
realResult = ((negate == B_TRUE) ? 0 : 1);
break;
case R_USAGE: /* usage errors */
realResult = 2;
break;
case R_ERROR: /* condition could not be determined */
default:
realResult = 3;
break;
}
/* debugging output */
log_msg(LOG_MSG_DEBUG, DBG_ADJUST_RESULTS, a_result, negate,
realResult);
/* return results */
return (realResult);
}
/*
* Name: setCmdLinePath
* Description: set global command line path
* Arguments: path - path to set from the command line
* args - command line args
* num_args - number of command line args
* Returns: R_SUCCESS - root path successfully set
* R_FAILURE - root path could not be set
* R_ERROR - fatal error attempting to set root path
*/
static void
setCmdLinePath(char **path, char **args, int num_args)
{
char rp[PATH_MAX] = { '\0' };
struct stat statbuf;
if (*path != NULL) {
return;
}
/*
* If a path "pkgcond is_global_zone [path]" is provided on the
* command line it must be the last argument.
*/
if (realpath(args[num_args - 1], rp) != NULL) {
if (stat(rp, &statbuf) == 0) {
/* make sure the target is a directory */
if ((statbuf.st_mode & S_IFDIR)) {
*path = strdup(rp);
} else {
*path = NULL;
}
}
}
}
/*
* Name: setRootPath
* Description: set global root path returned by getRootPath
* Arguments: a_path - root path to set
* a_mustExist - B_TRUE if path must exist (else error)
* - B_FALSE if path may not exist
* Returns: R_SUCCESS - root path successfully set
* R_FAILURE - root path could not be set
* R_ERROR - fatal error attempting to set root path
*/
static int
setRootPath(char *a_path, char *a_envVar, boolean_t a_mustExist)
{
char rp[PATH_MAX] = { '\0' };
struct stat statbuf;
/* if no data then issue warning and return success */
if ((a_path == NULL) || (*a_path == '\0')) {
echoDebug(DBG_NO_DEFAULT_ROOT_PATH_SET);
return (R_SUCCESS);
}
/* path present - resolve to absolute path */
if (realpath(a_path, rp) == NULL) {
if (a_mustExist == B_TRUE) {
/* must exist ... error */
log_msg(LOG_MSG_ERR, ERR_DEFAULT_ROOT_INVALID,
a_path, strerror(errno));
return (R_ERROR);
} else {
/* may not exist - use path as specified */
(void) strcpy(rp, a_path);
}
}
/* debugging output */
echoDebug(DBG_DEFAULT_ROOT_PATH_SET, rp, a_envVar ? a_envVar : "");
/* validate path existence if it must exist */
if (a_mustExist == B_TRUE) {
/* get node status */
if (stat(rp, &statbuf) != 0) {
log_msg(LOG_MSG_ERR, ERR_DEFAULT_ROOT_INVALID,
rp, strerror(errno));
return (R_ERROR);
}
/* make sure the target is a directory */
if (!(statbuf.st_mode & S_IFDIR)) {
log_msg(LOG_MSG_ERR, ERR_DEFAULT_ROOT_NOT_DIR, rp);
return (R_ERROR);
}
}
/* target exists and is a directory - set */
echoDebug(DBG_SET_ROOT_PATH_TO, rp);
/* store copy of resolved root path */
_rootPath = strdup(rp);
/* success! */
return (R_SUCCESS);
}
/*
* Name: testPath
* Description: determine if a path meets the specified conditions
* Arguments: a_tt - conditions to test path against
* a_format - format to use to generate path
* arguments following a_format - as needed for a_format
* Returns: R_SUCCESS - the path meets all of the specified conditions
* R_FAILURE - the path does not meet all of the conditions
* R_ERROR - error attempting to test path
*/
/*PRINTFLIKE2*/
static int
testPath(TEST_TYPES a_tt, char *a_format, ...)
{
char *mbPath; /* copy for the path to be returned */
char bfr[1];
int r;
size_t vres = 0;
struct stat statbuf;
va_list ap;
int fd;
/* entry assertions */
assert(a_format != NULL);
assert(*a_format != '\0');
/* determine size of the message in bytes */
va_start(ap, a_format);
vres = vsnprintf(bfr, 1, a_format, ap);
va_end(ap);
assert(vres > 0);
/* allocate storage to hold the message */
mbPath = (char *)calloc(1, vres+2);
assert(mbPath != NULL);
/* generate the results of the printf conversion */
va_start(ap, a_format);
vres = vsnprintf(mbPath, vres+1, a_format, ap);
va_end(ap);
assert(vres > 0);
echoDebug(DBG_TEST_PATH, mbPath, (unsigned long)a_tt);
/*
* When a path given to open(2) contains symbolic links, the
* open system call first resolves all symbolic links and then
* opens that final "resolved" path. As a result, it is not
* possible to check the result of an fstat(2) against the
* file descriptor returned by open(2) for S_IFLNK (a symbolic
* link) since all symbolic links are resolved before the
* target is opened.
*
* When testing the target as being (or not being) a symbolic
* link, first use lstat(2) against the target to determine
* whether or not the specified target itself is (or is not) a
* symbolic link.
*/
if (a_tt & (TEST_IS_SYMBOLIC_LINK|TEST_NOT_SYMBOLIC_LINK)) {
/*
* testing target is/is not a symbolic link; use lstat
* to determine the status of the target itself rather
* than what the target might finally address.
*/
if (lstat(mbPath, &statbuf) != 0) {
echoDebug(DBG_CANNOT_LSTAT_PATH, mbPath,
strerror(errno));
free(mbPath);
return (R_FAILURE);
}
/* Is the target required to be a symbolic link? */
if (a_tt & TEST_IS_SYMBOLIC_LINK) {
/* target must be a symbolic link */
if (!(statbuf.st_mode & S_IFLNK)) {
/* failure: target is not a symbolic link */
echoDebug(DBG_IS_NOT_A_SYMLINK, mbPath);
free(mbPath);
return (R_FAILURE);
}
/* success: target is a symbolic link */
echoDebug(DBG_SYMLINK_IS, mbPath);
}
/* Is the target required to not be a symbolic link? */
if (a_tt & TEST_NOT_SYMBOLIC_LINK) {
/* target must not be a symbolic link */
if (statbuf.st_mode & S_IFLNK) {
/* failure: target is a symbolic link */
echoDebug(DBG_IS_A_SYMLINK, mbPath);
free(mbPath);
return (R_FAILURE);
}
/* success: target is not a symbolic link */
echoDebug(DBG_SYMLINK_NOT, mbPath);
}
/*
* if only testing is/is not a symbolic link, then
* no need to open the target: return success.
*/
if (!(a_tt &
(~(TEST_IS_SYMBOLIC_LINK|TEST_NOT_SYMBOLIC_LINK)))) {
free(mbPath);
return (R_SUCCESS);
}
}
/* resolve path and remove any whitespace */
r = resolvePath(&mbPath);
if (r != R_SUCCESS) {
echoDebug(DBG_TEST_PATH_NO_RESOLVE, mbPath);
free(mbPath);
if (a_tt & TEST_NOT_EXISTS) {
return (R_SUCCESS);
}
return (r);
}
echoDebug(DBG_TEST_PATH_RESOLVE, mbPath);
/* open the file - this is the basic existence test */
fd = open(mbPath, O_RDONLY|O_LARGEFILE, 0);
/* existence test failed if file cannot be opened */
if (fd < 0) {
/*
* target could not be opened - if testing for non-existence,
* return success, otherwise return failure
*/
if (a_tt & TEST_NOT_EXISTS) {
echoDebug(DBG_CANNOT_ACCESS_PATH_OK, mbPath);
free(mbPath);
return (R_SUCCESS);
}
echoDebug(DBG_CANNOT_ACCESS_PATH_BUT_SHOULD,
mbPath, strerror(errno));
free(mbPath);
return (R_FAILURE);
}
/*
* target successfully opened - if testing for non-existence,
* return failure, otherwise continue with specified tests
*/
if (a_tt & TEST_NOT_EXISTS) {
/* testing for non-existence: return failure */
echoDebug(DBG_TEST_EXISTS_SHOULD_NOT, mbPath);
free(mbPath);
(void) close(fd);
return (R_FAILURE);
}
/* get the file status */
r = fstat(fd, &statbuf);
if (r != 0) {
echoDebug(DBG_PATH_DOES_NOT_EXIST, mbPath, strerror(errno));
(void) close(fd);
free(mbPath);
return (R_FAILURE);
}
/* required to be a directory? */
if (a_tt & TEST_IS_DIRECTORY) {
if (!(statbuf.st_mode & S_IFDIR)) {
/* is not a directory */
echoDebug(DBG_IS_NOT_A_DIRECTORY, mbPath);
free(mbPath);
return (R_FAILURE);
}
/* a directory */
echoDebug(DBG_DIRECTORY_IS, mbPath);
}
/* required to not be a directory? */
if (a_tt & TEST_NOT_DIRECTORY) {
if (statbuf.st_mode & S_IFDIR) {
/* is a directory */
echoDebug(DBG_IS_A_DIRECTORY, mbPath);
free(mbPath);
return (R_FAILURE);
}
/* not a directory */
echoDebug(DBG_DIRECTORY_NOT, mbPath);
}
/* required to be a file? */
if (a_tt & TEST_IS_FILE) {
if (!(statbuf.st_mode & S_IFREG)) {
/* is not a regular file */
echoDebug(DBG_IS_NOT_A_FILE, mbPath);
free(mbPath);
return (R_FAILURE);
}
/* a regular file */
echoDebug(DBG_FILE_IS, mbPath);
}
/* required to not be a file? */
if (a_tt & TEST_NOT_FILE) {
if (statbuf.st_mode & S_IFREG) {
/* is a regular file */
echoDebug(DBG_IS_A_FILE, mbPath);
free(mbPath);
return (R_FAILURE);
}
/* not a regular file */
echoDebug(DBG_FILE_NOT, mbPath);
}
/*
* Find token (global) in file pointed to by mbPath.
* token is only compared to first word in mbPath.
*/
if (a_tt & TEST_GLOBAL_TOKEN_IN_FILE) {
if (!(statbuf.st_mode & S_IFREG)) {
/* is not a regular file */
echoDebug(DBG_IS_NOT_A_FILE, mbPath);
free(mbPath);
return (R_FAILURE);
}
/* If global exists then we're not in a non-global zone */
if (findToken(mbPath, GLOBAL_ZONENAME) == R_SUCCESS) {
echoDebug(DBG_TOKEN__EXISTS, GLOBAL_ZONENAME, mbPath);
free(mbPath);
return (R_FAILURE);
}
}
(void) close(fd);
/* success! */
echoDebug(DBG_TESTPATH_OK, mbPath);
/* free up temp storage used to hold path to test */
free(mbPath);
return (R_SUCCESS);
}
/*
* Name: findToken
* Description: Find first token in file.
* Arguments:
* path - file to search for token
* token - string to search for
* Returns:
* R_SUCCESS - the token exists
* R_FAILURE - the token does not exist
* R_ERROR - fatal error attempting to find token
*/
static int
findToken(char *path, char *token)
{
FILE *fp;
char *cp;
char line[MAXPATHLEN];
if (path == NULL || token == NULL) {
return (R_ERROR);
}
if ((fp = fopen(path, "r")) == NULL) {
return (R_ERROR);
}
while (fgets(line, sizeof (line), fp) != NULL) {
for (cp = line; *cp && isspace(*cp); cp++)
;
/* skip comments */
if (*cp == '#') {
continue;
}
if (pkgstrContainsToken(cp, token, ":")) {
(void) fclose(fp);
return (R_SUCCESS);
}
}
(void) fclose(fp);
return (R_FAILURE);
}
/*
* Name: resolvePath
* Description: fully resolve a path to an absolute real path
* Arguments: r_path - pointer to pointer to malloc()ed storage containing
* the path to resolve - this path may be reallocated
* as necessary to hold the fully resolved path
* Output: r_path - is realloc()ed as necessary
* Returns: R_SUCCESS - the path is fully resolved
* R_FAILURE - the path could not be resolved
* R_ERROR - fatal error attempting to resolve path
*/
static int
resolvePath(char **r_path)
{
int i;
char resolvedPath[MAXPATHLEN+1] = {'\0'};
size_t mbPathlen; /* length of multi-byte path */
size_t wcPathlen; /* length of wide-character path */
wchar_t *wcPath; /* wide-character version of the path */
wchar_t *wptr; /* scratch pointer */
/* entry assertions */
assert(r_path != NULL);
/* return error if the path is completely empty */
if (**r_path == '\0') {
return (R_FAILURE);
}
/* remove all leading whitespace */
removeLeadingWhitespace(r_path);
/*
* convert to real path: an absolute pathname that names the same file,
* whose resolution does not involve ".", "..", or symbolic links.
*/
if (realpath(*r_path, resolvedPath) != NULL) {
free(*r_path);
*r_path = strdup(resolvedPath);
}
/*
* convert the multi-byte version of the path to a
* wide-character rendering, for doing our figuring.
*/
mbPathlen = strlen(*r_path);
if ((wcPath = (wchar_t *)
calloc(1, sizeof (wchar_t)*(mbPathlen+1))) == NULL) {
return (R_FAILURE);
}
/*LINTED*/
if ((wcPathlen = mbstowcs(wcPath, *r_path, mbPathlen)) == -1) {
free(wcPath);
return (R_FAILURE);
}
/*
* remove duplicate slashes first ("//../" -> "/")
*/
for (wptr = wcPath, i = 0; i < wcPathlen; i++) {
*wptr++ = wcPath[i];
if (wcPath[i] == '/') {
i++;
while (wcPath[i] == '/') {
i++;
}
i--;
}
}
*wptr = '\0';
/*
* now convert back to the multi-byte format.
*/
/*LINTED*/
if (wcstombs(*r_path, wcPath, mbPathlen) == -1) {
free(wcPath);
return (R_FAILURE);
}
/* at this point have a path */
/* free up temporary storage */
free(wcPath);
return (R_SUCCESS);
}
/*
* Name: removeLeadingWhitespace
* Synopsis: Remove leading whitespace from string
* Description: Remove all leading whitespace characters from a string
* Arguments: a_str - [RO, *RW] - (char **)
* Pointer to handle to string (in allocated storage) to
* remove all leading whitespace from
* Returns: void
* The input string is modified as follows:
* == NULL:
* - input string was NULL
* - input string is all whitespace
* != NULL:
* - copy of input string with leading
* whitespace removed
* CAUTION: The input string must be allocated space (via malloc() or
* strdup()) - it must not be a static or inline character string
* NOTE: The input string a_str will be freed with 'free'
* if it is all whitespace, or if it contains any leading
* whitespace characters
* NOTE: Any string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the string is no longer needed.
* Errors: If the string cannot be created, the process exits
*/
static void
removeLeadingWhitespace(char **a_str)
{
char *o_str;
/* entry assertions */
assert(a_str != (char **)NULL);
/* if string is null, just return */
if (*a_str == NULL) {
return;
}
o_str = *a_str;
/* if string is empty, deallocate and return NULL */
if (*o_str == '\0') {
/* free string */
free(*a_str);
*a_str = NULL;
return;
}
/* if first character is not a space, just return */
if (!isspace(*o_str)) {
return;
}
/* advance past all space characters */
while ((*o_str != '\0') && (isspace(*o_str))) {
o_str++;
}
/* if string was all space characters, deallocate and return NULL */
if (*o_str == '\0') {
/* free string */
free(*a_str);
*a_str = NULL;
return;
}
/* have non-space/null byte, return dup, deallocate original */
o_str = strdup(o_str);
free(*a_str);
*a_str = o_str;
}
/*
* Name: getZoneName
* Description: get the name of the zone this process is running in
* Arguments: r_zoneName - pointer to pointer to receive zone name
* Output: r_zoneName - a pointer to malloc()ed storage containing
* the zone name this process is running in is stored
* in the location pointed to by r_zoneName
* Returns: R_SUCCESS - the zone name is successfully returned
* R_FAILURE - the zone name is not successfully returned
* R_ERROR - error attempting to get the zone name
*/
static int
getZoneName(char **r_zoneName)
{
static char zoneName[ZONENAME_MAX] = { '\0' };
/* if zone name not already present, retrieve and cache name */
if (zoneName[0] == '\0') {
if (getzonenamebyid(getzoneid(), zoneName,
sizeof (zoneName)) < 0) {
log_msg(LOG_MSG_ERR, ERR_CANNOT_GET_ZONENAME);
return (R_ERROR);
}
}
/* return cached zone name */
*r_zoneName = zoneName;
return (R_SUCCESS);
}
/*
* Name: getRootPath
* Description: get the root path being tested by this process
* Arguments: r_rootPath - pointer to pointer to receive root path
* Output: r_rootPath - a pointer to malloc()ed storage containing
* the root path name this process is testing
* Returns: R_SUCCESS - the root path is successfully returned
* R_FAILURE - the root path is not successfully returned
* R_ERROR - error attempting to get the root path
*/
static int
getRootPath(char **r_rootPath)
{
*r_rootPath = _rootPath;
return (R_SUCCESS);
}
/*
* Name: setVerbose
* Description: Turns on verbose output
* Scope: public
* Arguments: verbose = B_TRUE indicates verbose mode
* Returns: none
*/
static void
setVerbose(boolean_t setting)
{
/* set log verbose messages */
log_set_verbose(setting);
/* set interactive messages */
echoSetFlag(setting);
}
/*
* Name: negate_results
* Description: control negation of results
* Scope: public
* Arguments: setting
* == B_TRUE indicates negated results mode
* == B_FALSE indicates non-negated results mode
* Returns: none
*/
static void
setNegateResults(boolean_t setting)
{
log_msg(LOG_MSG_DEBUG, DBG_SET_NEGATE_RESULTS,
_negateResults, setting);
_negateResults = setting;
}
/*
* Name: getNegateResults
* Description: Returns whether or not to results are negated
* Scope: public
* Arguments: none
* Returns: B_TRUE - results are negated
* B_FALSE - results are not negated
*/
static boolean_t
getNegateResults(void)
{
return (_negateResults);
}
/*
* Name: usage
* Description: output usage string
* Arguments: a_format - format to use to generate message
* arguments following a_format - as needed for a_format
* Output: Outputs the usage string to stderr.
* Returns: R_ERROR
*/
static int
usage(char *a_format, ...)
{
int cur_cmd;
char cmdlst[LINE_MAX+1] = { '\0' };
char *message;
char bfr[1];
char *p = get_prog_name();
size_t vres = 0;
va_list ap;
/* entry assertions */
assert(a_format != NULL);
assert(*a_format != '\0');
/* determine size of the message in bytes */
va_start(ap, a_format);
/* LINTED warning: variable format specifier to vsnprintf(); */
vres = vsnprintf(bfr, 1, a_format, ap);
va_end(ap);
assert(vres > 0);
/* allocate storage to hold the message */
message = (char *)calloc(1, vres+2);
assert(message != NULL);
/* generate the results of the printf conversion */
va_start(ap, a_format);
/* LINTED warning: variable format specifier to vsnprintf(); */
vres = vsnprintf(message, vres+1, a_format, ap);
va_end(ap);
assert(vres > 0);
/* generate list of all defined conditions */
for (cur_cmd = 0; cmds[cur_cmd].c_name != NULL; cur_cmd++) {
(void) strlcat(cmdlst, "\t", sizeof (cmdlst));
(void) strlcat(cmdlst, cmds[cur_cmd].c_name, sizeof (cmdlst));
if (cmds[cur_cmd].c_args != NULL) {
(void) strlcat(cmdlst, cmds[cur_cmd].c_args,
sizeof (cmdlst));
}
(void) strlcat(cmdlst, "\n", sizeof (cmdlst));
}
/* output usage with conditions */
log_msg(LOG_MSG_INFO, MSG_USAGE, message, p ? p : "pkgcond", cmdlst);
return (R_ERROR);
}
/*
* Name: parseGlobalData
* Description: parse environment global data and store in global data structure
* Arguments: a_envVar - pointer to string representing the name of the
* environment variable to get and parse
* r_gdt - pointer to pointer to global data structure to fill in
* using the parsed data from a_envVar
* Output: none
* Returns: R_SUCCESS - the global data is successfully parsed
* R_FAILURE - problem parsing global data
* R_ERROR - fatal error attempting to parse global data
*/
static int
parseGlobalData(char *a_envVar, GLOBALDATA_T **r_gdt)
{
int r;
char *a;
SML_TAG *tag;
SML_TAG *ntag;
assert(r_gdt != (GLOBALDATA_T **)NULL);
/*
* allocate space for global data structure if needed
*/
if (*r_gdt == (GLOBALDATA_T *)NULL) {
*r_gdt = (GLOBALDATA_T *)calloc(1, sizeof (GLOBALDATA_T));
}
/*
* get initial installation indication:
* If the initial install variable is set to "true", then an initial
* installation of Solaris is underway. When this condition is true:
* - if the path being checked is the package install root, then
* the path is considered to be an 'alternative root' which is
* currently being installed.
* - if the path being checked is not the package install root, then
* the path needs to be further analyzed to determine what it may
* be referring to.
*/
a = getenv(ENV_VAR_INITIAL_INSTALL);
if ((a != NULL) && (strcasecmp(a, "true") == 0)) {
(*r_gdt)->gd_initialInstall = B_TRUE;
}
/* get current zone name */
r = getZoneName(&(*r_gdt)->gd_zoneName);
if (r != R_SUCCESS) {
(*r_gdt)->gd_zoneName = "";
}
/*
* get zone installation status:
* - If the package install zone name is not set, then an installation
* of a global zone, or of a non-global zone, is not underway.
* - If the package install zone name is set to "global", then an
* installation of a global zone is underway. In this case, no path
* can be a netinstall image, diskless client, mounted miniroot,
* non-global zone, the current running system, alternative root,
* or alternative boot environment.
* - If the package install zone name is set to a value other than
* "global", then an installation of a non-global zone with that name
* is underway. In this case, no path can be a netinstall image,
* diskless client, mounted miniroot, global zone, the current
* running system, alternative root, or alternative boot environment.
*/
a = getenv(ENV_VAR_PKGZONENAME);
if ((a == NULL) || (*a == '\0')) {
/* not installing a zone */
(*r_gdt)->gd_globalZoneInstall = B_FALSE;
(*r_gdt)->gd_nonglobalZoneInstall = B_FALSE;
} else if (strcmp(a, GLOBAL_ZONENAME) == 0) {
/* installing a global zone */
(*r_gdt)->gd_globalZoneInstall = B_TRUE;
(*r_gdt)->gd_nonglobalZoneInstall = B_FALSE;
(*r_gdt)->gd_zoneName = a;
} else {
/* installing a non-global zone by that name */
(*r_gdt)->gd_globalZoneInstall = B_FALSE;
(*r_gdt)->gd_nonglobalZoneInstall = B_TRUE;
(*r_gdt)->gd_zoneName = a;
}
/*
* get package install root.
*/
a = getenv(ENV_VAR_PKGROOT);
if ((a != NULL) && (*a != '\0')) {
(*r_gdt)->gd_installRoot = a;
} else {
(*r_gdt)->gd_installRoot = "/";
}
/* get the global data environment variable */
a = getenv(a_envVar);
/* if no data then issue warning and return success */
if ((a == NULL) || (*a_envVar == '\0')) {
log_msg(LOG_MSG_DEBUG, DBG_NO_GLOBAL_DATA_AVAILABLE, a_envVar);
return (R_SUCCESS);
}
/* data present - parse into SML structure */
log_msg(LOG_MSG_DEBUG, DBG_PARSE_GLOBAL, a);
r = smlConvertStringToTag(&tag, a);
if (r != R_SUCCESS) {
log_msg(LOG_MSG_ERR, ERR_CANNOT_PARSE_GLOBAL_DATA, a);
return (R_FAILURE);
}
smlDbgPrintTag(tag, DBG_PARSED_ENVIRONMENT, a_envVar);
/* fill in global data structure */
/* find the environment condition information structure */
ntag = smlGetTagByName(tag, 0, TAG_COND_TOPLEVEL);
if (ntag == SML_TAG__NULL) {
log_msg(LOG_MSG_WRN, WRN_PARSED_DATA_MISSING,
TAG_COND_TOPLEVEL);
return (R_FAILURE);
}
/*
* data found - extract what we know about
*/
/* parent zone name */
a = smlGetParamByTag(ntag, 0, TAG_COND_PARENT_ZONE, TAG_COND_ZONE_NAME);
(*r_gdt)->gd_parentZoneName = a;
/* parent zone type */
a = smlGetParamByTag(ntag, 0, TAG_COND_PARENT_ZONE, TAG_COND_ZONE_TYPE);
(*r_gdt)->gd_parentZoneType = a;
/* current zone name */
a = smlGetParamByTag(ntag, 0, TAG_COND_CURRENT_ZONE,
TAG_COND_ZONE_NAME);
(*r_gdt)->gd_currentZoneName = a;
/* current zone type */
a = smlGetParamByTag(ntag, 0, TAG_COND_CURRENT_ZONE,
TAG_COND_ZONE_TYPE);
(*r_gdt)->gd_currentZoneType = a;
return (R_SUCCESS);
}
/*
* Name: dumpGlobalData
* Description: dump global data structure using echoDebug
* Arguments: a_gdt - pointer to global data structure to dump
* Outputs: echoDebug is called to output global data strucutre information
* Returns: void
*/
static void
dumpGlobalData(GLOBALDATA_T *a_gdt)
{
/* entry assertions */
assert(a_gdt != (GLOBALDATA_T *)NULL);
/* debugging enabled, dump the global data structure */
echoDebug(DBG_DUMP_GLOBAL_ENTRY);
echoDebug(DBG_DUMP_GLOBAL_PARENT_ZONE,
a_gdt->gd_parentZoneName ? a_gdt->gd_parentZoneName : "",
a_gdt->gd_parentZoneType ? a_gdt->gd_parentZoneType : "");
echoDebug(DBG_DUMP_GLOBAL_CURRENT_ZONE,
a_gdt->gd_currentZoneName ? a_gdt->gd_currentZoneName : "",
a_gdt->gd_currentZoneType ? a_gdt->gd_currentZoneType : "");
}
/*
* Name: recursionCheck
* Description: prevent recursive calling of functions
* Arguments: r_recursion - pointer to int recursion counter
* a_function - pointer to name of function
* Returns: B_TRUE - function is recursively called
* B_FALSE - function not recursively called
*/
static boolean_t
recursionCheck(int *r_recursion, char *a_function)
{
/* prevent recursion */
(*r_recursion)++;
if (*r_recursion > 1) {
echoDebug(DBG_RECURSION, a_function, *r_recursion);
(*r_recursion)--;
return (B_TRUE);
}
echoDebug(DBG_NO_RECURSION, a_function);
return (B_FALSE);
}
/*
* Name: quit
* Description: cleanup and exit
* Arguments: a_retcode - the code to use to determine final exit status;
* if this is NOT "99" and if a "ckreturnFunc" is
* set, then that function is called with a_retcode
* to set the final exit status.
* Valid values are:
* 0 - success
* 1 - package operation failed (fatal error)
* 2 - non-fatal error (warning)
* 3 - user selected quit (operation interrupted)
* 4 - admin settings prevented operation
* 5 - interaction required and -n (non-interactive) specified
* "10" is added to indicate "immediate reboot required"
* "20" is be added to indicate "reboot after install required"
* 99 - do not interpret the code - just exit "99"
* Returns: <<this function does not return - calls exit()>>
* NOTE: This is needed because libinst functions can call "quit(99)"
* to force an error exit.
*/
void
quit(int a_retcode)
{
/* process return code if not quit(99) */
if (a_retcode == 99) {
exit(0x7f); /* processing error (127) */
}
exit(R_FAILURE);
}
|