summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svr4pkg/pkgrm/main.c
blob: 39a0846704932baef2b1f616f8d456312024800c (plain)
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
/*
 * 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) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */


/*
 * System includes
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <locale.h>
#include <libintl.h>
#include <pkgstrct.h>
#include <pkgdev.h>
#include <pkginfo.h>
#include <pkglocs.h>
#include <pkglib.h>
#include <assert.h>

/*
 * libinstzones includes
 */

#include <instzones_api.h>

/*
 * consolidation pkg command library includes
 */

#include <pkglib.h>

/*
 * local pkg command library includes
 */

#include "install.h"
#include "libinst.h"
#include "libadm.h"
#include "messages.h"

/*
 * pkgrm local includes
 */

#include "quit.h"

/*
 * exported global variables
 */

/* these globals are set by ckreturn and used by quit.c */

int	admnflag = 0;	/* != 0 if any pkg op admin setting failure (4) */
int	doreboot = 0;	/* != 0 if reboot required after installation */
int	failflag = 0;	/* != 0 if fatal error has occurred (1) */
int	intrflag = 0;	/* != 0 if user selected quit (3) */
int	ireboot = 0;	/* != 0 if immediate reboot required */
int	nullflag = 0;	/* != 0 if admin interaction required (5) */
int	warnflag = 0;	/* != 0 if non-fatal error has occurred (2) */

/* imported by quit.c */
int	npkgs = 0;	/* the number of packages yet to be installed */

/* imported by presvr4.c */
int	started = 0;
char	*tmpdir = NULL;	/* location to place temporary files */

/* imported by various (many) */
struct admin	adm;	/* holds info about installation admin */
struct pkgdev	pkgdev;	/* holds info about the installation device */

/*
 * internal global variables
 */

static char	*admnfile = NULL;	/* file to use for installation admin */
static char	*pkginst = NULL;	/* current pkg/src instance 2 process */
static char	*vfstab_file = NULL;
static char	*zoneTempDir = (char *)NULL;

/* set by ckreturn() */

static int	interrupted = 0;	/* last pkg op was quit (1,2,3,4,5) */

static int	nointeract = 0;		/* non-zero - no user interaction */
static int	pkgrmremote = 0;	/* remove pkg objs stored remotely  */
static int	pkgverbose = 0;		/* non-zero if verbose mode selected */

/*
 * Assume the package complies with the standards as regards user
 * interaction during procedure scripts.
 */

static int	old_pkg = 0;
static int	old_symlinks = 0;
static int	no_map_client = 0;

/* Set by -O nozones: do not process any zones */

static boolean_t	noZones = B_FALSE;

/* Set by -O zonelist=<names...>: process only named zones */

static boolean_t	usedZoneList = B_FALSE;

/* Set by -O debug: debug output is enabled? */

static boolean_t	debugFlag = B_FALSE;

/*
 * imported (external) functions
 */

/* check.c */

extern int	preremove_verify(char **a_pkgList, zoneList_t a_zlst,
			char *a_zoneTempDir);
/* quit.c */

extern void	quitSetZonelist(zoneList_t a_zlst);

/*
 * imported (external) variables
 */

extern char	*pkgdir;

/* printable string - if string is null results in ??? */

#define	PSTR(STR) (((STR) == (char *)NULL) ? "???" : (STR))

#define	MAX_FDS	20

#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"
#endif

/*
 * forward declarations
 */

static void		ckreturn(int retcode);
static void		create_zone_adminfile(char **r_zoneAdminFile,
				char *a_zoneTempDir, char *a_admnfile);
static void		create_zone_tempdir(char **r_zoneTempDir,
				char *a_tmpdir);
static int		doRemove(int a_nodelete, char *a_altBinDir,
				int a_longestPkg, char *a_adminFile,
				char *a_zoneAdminFile, zoneList_t zlst);
static int		pkgRemove(int a_nodelete, char *a_altBinDir,
				char *a_adminFile);
static int		pkgZoneCheckRemove(char *a_zoneName, char *a_altBinDir,
				char *a_adminFile, char *a_stdoutPath,
				zone_state_t a_zoneState, boolean_t tmpzone);
static int		pkgZoneRemove(char *a_zoneName, int a_nodelete,
				char *a_altBinDir, char *a_adminFile,
				zone_state_t a_zoneState, boolean_t tmpzone);
static void		resetreturn();
static void		usage(void);
static boolean_t	check_applicability(char *a_packageDir,
				char *a_pkgInst, char *a_rootPath,
				CAF_T a_flags);
static boolean_t	check_packages(char **a_pkgList, char *a_packageDir);
static boolean_t	remove_packages(char **a_pkgList, int a_nodelete,
				int a_longestPkg, int a_repeat,
				char *a_altBinDir, char *a_pkgdir,
				char *a_spoolDir, boolean_t a_noZones);
static boolean_t	remove_packages_from_spool_directory(char **a_pkgList,
				int a_nodelete, int a_longestPkg, int a_repeat,
				char *a_altBinDir);
static boolean_t	remove_packages_in_global_no_zones(char **a_pkgList,
				int a_nodelete, int a_longestPkg, int a_repeat,
				char *a_altBinDir);
static boolean_t	remove_packages_in_global_with_zones(char **a_pkgList,
				int a_nodelete, int a_longestPkg, int a_repeat,
				char *a_altBinDir, char *a_pkgdir,
				zoneList_t a_zlst);
static boolean_t	remove_packages_in_nonglobal_zone(char **a_pkgList,
				int a_nodelete, int a_longestPkg, int a_repeat,
				char *a_altBinDir, char *a_pkgdir);
static boolean_t	shall_we_continue(char *a_pkgInst, int a_npkgs);

/*
 * *****************************************************************************
 * global external (public) functions
 * *****************************************************************************
 */

/*
 * Name:	main
 * Description:	main entry point for pkgrm
 * Returns:	int
 *   0        Successful completion
 *   1        Fatal error.
 *   2        Warning.
 *   3        Interruption.
 *   4        Administration.
 *   5        Administration. Interaction is required. Do not use pkgrm -n.
 *  10       Reboot after removal of all packages.
 *  20       Reboot after removal of this package.
 */

int
main(int argc, char **argv)
{
	char			**category = NULL;
	char			*altBinDir = (char *)NULL;
	char			*catg_arg = NULL;
	char			*p;
	char			*prog_full_name = NULL;
	char			*spoolDir = 0;
	int			c;
	int			longestPkg = 0;
	int			n;
	int			nodelete = 0;	/* dont rm files/run scripts */
	int			pkgLgth = 0;
	int			repeat;
	struct sigaction	nact;
	struct sigaction	oact;

	/* initialize locale environment */

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	/* initialize program name */

	prog_full_name = argv[0];
	(void) set_prog_name(argv[0]);

	/* tell spmi zones interface how to access package output functions */

	z_set_output_functions(echo, echoDebug, progerr);

	/* tell quit which ckreturn function to call */

	quitSetCkreturnFunc(&ckreturn);

	/* Read PKG_INSTALL_ROOT from the environment, if it's there. */

	if (!set_inst_root(getenv("PKG_INSTALL_ROOT"))) {
		progerr(ERR_ROOT_SET);
		exit(1);
	}

	if (z_running_in_global_zone() && !enable_local_fs()) {
		progerr(ERR_CANNOT_ENABLE_LOCAL_FS);
	}

	pkgserversetmode(DEFAULTMODE);

	/*
	 * ********************************************************************
	 * parse command line options
	 * ********************************************************************
	 */

	while ((c = getopt(argc, argv, "?Aa:b:FMnO:R:s:V:vY:Z")) != EOF) {
		switch (c) {
		/*
		 * Public interface: Allow admin to remove objects
		 * from a service area via a reference client.
		 * Remove the package files from the client's file
		 * system, absolutely. If a file is shared with other
		 * packages, the default behavior is to not remove
		 * the file from the client's file system.
		 */
		case 'A':
		    pkgrmremote++;
		    break;

		/*
		 * Public interface: Use the installation
		 * administration file, admin, in place of the
		 * default admin file. pkgrm first looks in the
		 * current working directory for the administration
		 * file.  If the specified administration file is not
		 * in the current working directory, pkgrm looks in
		 * the /var/sadm/install/admin directory for the
		 * administra- tion file.
		 */
		case 'a':
		    admnfile = flex_device(optarg, 0);
		    break;

		/*
		 * Not a public interface:  location where package executables
		 * can be found - default is /usr/sadm/install/bin.
		 */
		case 'b':
			if (!path_valid(optarg)) {
				progerr(ERR_PATH, optarg);
				quit(1);
			}
			if (isdir(optarg) != 0) {
				p = strerror(errno);
				progerr(ERR_CANNOT_USE_DIR, optarg, p);
				quit(1);
			}
			altBinDir = optarg;
			break;

		/*
		 * Not a public interface: pass -F option to
		 * pkgremove which suppresses the removal of any
		 * files and any class action scripts, and suppresses
		 * the running of any class action scripts.  The
		 * package files remain but the package looks like it
		 * is not installed. This is mainly for use by the
		 * upgrade process.
		 */
		case 'F':
		    nodelete++;
		    break;

		/*
		 * Public interface: Instruct pkgrm not to use the
		 * $root_path/etc/vfstab file for determining the
		 * client's mount points. This option assumes the
		 * mount points are correct on the server and it
		 * behaves consistently with Solaris 2.5 and earlier
		 * releases.
		 */
		case 'M':
		    no_map_client = 1;
		    break;

		/*
		 * Public interface: package removal occurs in
		 * non-interactive mode.  Suppress output of the list of
		 * removed files. The default mode is interactive.
		 */
		case 'n':
		    nointeract++;
		    (void) echoSetFlag(B_FALSE);
		    break;

		/*
		 * Not a public interface: the -O option allows the behavior
		 * of the package tools to be modified. Recognized options:
		 * -> debug
		 * ---> enable debugging output
		 * -> nozones
		 * ---> act as though in global zone with no non-global zones
		 * -> enable-hollow-package-support
		 * --> Enable hollow package support. When specified, for any
		 * --> package that has SUNW_PKG_HOLLOW=true:
		 * --> Do not calculate and verify package size against target
		 * --> Do not run any package procedure or class action scripts
		 * --> Do not create or remove any target directories
		 * --> Do not perform any script locking
		 * --> Do not install or uninstall any components of any package
		 * --> Do not output any status or database update messages
		 * -> zonelist="<names...>"
		 * ---> add package to space-separated list of zones only
		 */

		case 'O':
			for (p = strtok(optarg, ","); p != (char *)NULL;
				p = strtok(NULL, ",")) {

				if (strcmp(p, "nozones") == 0) {
					noZones = B_TRUE;
					continue;
				}

				if (strcmp(p,
					"enable-hollow-package-support") == 0) {
					set_depend_pkginfo_DB(B_TRUE);
					continue;
				}

				if (strcmp(p, "debug") == 0) {
					/* set debug flag/enable debug output */
					debugFlag = B_TRUE;
					(void) echoDebugSetFlag(debugFlag);

					/* debug info on arguments to pkgadd */
					for (n = 0; n < argc && argv[n]; n++) {
						echoDebug(DBG_ARG, n, argv[n]);
					}

					continue;
				}

				if (strncmp(p, "zonelist=", 9) == 0) {
					if (z_set_zone_spec(p + 9) == -1)
						quit(1);
					usedZoneList = B_TRUE;
					continue;
				}

				/* -O option not recognized - issue warning */

				progerr(ERR_INVALID_O_OPTION, p);
				continue;
			}
			break;

		/*
		 * Public interface: defines the full path name of a
		 * directory to use as the root_path.  All files,
		 * including package system information files, are
		 * relocated to a directory tree starting in the
		 * specified root_path.
		 */
		case 'R':
		    if (!set_inst_root(optarg)) {
			    progerr(ERR_ROOT_CMD);
			    exit(1);
		    }
		    break;

		/*
		 * Public interface: remove the specified package(s)
		 * from the directory spool.  The default directory
		 * for spooled packages is /var/sadm/pkg.
		 */
		case 's':
		    spoolDir = flex_device(optarg, 1);
		    break;

		/*
		 * Public interface: Allow admin to establish the client
		 * filesystem using a vfstab-like file of stable format.
		 */
		case 'V':
		    vfstab_file = flex_device(optarg, 2);
		    no_map_client = 0;
		    break;

		/*
		 * Public interface: trace all of the scripts that
		 * get executed by pkgrm, located in the
		 * pkginst/install directory. This option is used for
		 * debugging the procedural and non- procedural
		 * scripts.
		 */
		case 'v':
		    pkgverbose++;
		    break;

		/*
		 * Public interface: remove packages based on the
		 * CATEGORY variable from the installed/spooled
		 * pkginfo file
		 */
		case 'Y':
		    catg_arg = strdup(optarg);

		    if ((category = get_categories(catg_arg)) == NULL) {
			    progerr(ERR_CAT_INV, catg_arg);
			    exit(1);
		    } else if (is_not_valid_category(category,
				    get_prog_name())) {
			    progerr(ERR_CAT_SYS);
			    exit(1);
		    } else if (is_not_valid_length(category)) {
			    progerr(ERR_CAT_LNGTH);
			    exit(1);
		    }

		    break;

		/*
		 * unrecognized option
		 */
		default:
		    usage();
		    /* NOTREACHED */
		}
	}

	/*
	 * ********************************************************************
	 * validate command line options
	 * ********************************************************************
	 */

	/* set "debug echo" flag according to setting of "-O debug" option */

	(void) echoDebugSetFlag(debugFlag);

	/* output entry debugging information */

	if (z_running_in_global_zone()) {
		echoDebug(DBG_ENTRY_IN_GZ, prog_full_name);
	} else {
		echoDebug(DBG_ENTRY_IN_LZ, prog_full_name, getzoneid(),
			z_get_zonename());
	}

	/* -s cannot be used with several */

	if (spoolDir != (char *)NULL) {
		if (admnfile != (char *)NULL) {
			progerr(ERR_SPOOLDIR_AND_ADMNFILE);
			usage();
			/* NOTREACHED */
		}

		if (pkgrmremote != 0) {
			progerr(ERR_SPOOLDIR_AND_PKGRMREMOTE);
			usage();
			/* NOTREACHED */
		}

		if (pkgverbose != 0) {
			progerr(ERR_SPOOLDIR_AND_PKGVERBOSE);
			usage();
			/* NOTREACHED */
		}

		if (is_an_inst_root() != 0) {
			progerr(ERR_SPOOLDIR_AND_INST_ROOT);
			usage();
			/* NOTREACHED */
		}
	}

	/* -V cannot be used with -A */

	if (no_map_client && pkgrmremote) {
		progerr(ERR_V_USED_AND_PKGRMREMOTE);
		usage();
		/* NOTREACHED */
	}

	/* -n used without pkg names or category */

	if (nointeract && (optind == argc) && (catg_arg == NULL)) {
		progerr(ERR_BAD_N_PKGRM);
		usage();
		/* NOTREACHED */
	}

	/* Error if specified zone list isn't valid on target */
	if (usedZoneList && z_verify_zone_spec() == -1)
		usage();

	/*
	 * hook SIGINT and SIGHUP interrupts into quit.c's trap handler
	 */

	/* hold SIGINT/SIGHUP interrupts */

	(void) sighold(SIGHUP);
	(void) sighold(SIGINT);

	/* connect quit.c:trap() to SIGINT */

	nact.sa_handler = quitGetTrapHandler();
	nact.sa_flags = SA_RESTART;
	(void) sigemptyset(&nact.sa_mask);

	(void) sigaction(SIGINT, &nact, &oact);

	/* connect quit.c:trap() to SIGHUP */

	nact.sa_handler = quitGetTrapHandler();
	nact.sa_flags = SA_RESTART;
	(void) sigemptyset(&nact.sa_mask);

	(void) sigaction(SIGHUP, &nact, &oact);

	/* release hold on signals */

	(void) sigrelse(SIGHUP);
	(void) sigrelse(SIGINT);

	/* establish temporary directory to use */

	tmpdir = getenv("TMPDIR");
	if (tmpdir == NULL) {
		tmpdir = P_tmpdir;
	}

	echoDebug(DBG_PKGRM_TMPDIR, tmpdir);

	/* initialize path parameters */

	set_PKGpaths(get_inst_root());

	/*
	 * initialize installation admin parameters - if removing from a spool
	 * directory then the admin file is ignore.
	 */

	if (spoolDir == NULL) {
		echoDebug(DBG_PKGRM_ADMINFILE, admnfile ? admnfile : "");
		setadminFile(admnfile);
	}

	/*
	 * if running in the global zone, and non-global zones exist, then
	 * enable hollow package support so that any packages that are marked
	 * SUNW_PKG_HOLLOW=true will be correctly removed in non-global zones
	 * when removed directly in the global zone by the global zone admin.
	 */

	if (is_depend_pkginfo_DB()) {
		echoDebug(DBG_PKGRM_HOLLOW_ENABLED);
	} else if ((z_running_in_global_zone() == B_TRUE) &&
		(z_non_global_zones_exist() == B_TRUE)) {
		echoDebug(DBG_PKGRM_ENABLING_HOLLOW);
		set_depend_pkginfo_DB(B_TRUE);
	}

	/*
	 * See if user wants this to be handled as an old style pkg.
	 * NOTE : the ``exception_pkg()'' stuff is to be used only
	 * through on495. This function comes out for on1095. See
	 * PSARC 1993-546. -- JST
	 */
	if (getenv("NONABI_SCRIPTS") != NULL) {
		old_pkg = 1;
	}

	/*
	 * See if the user wants to process symlinks consistent with
	 * the old behavior.
	 */

	if (getenv("PKG_NONABI_SYMLINKS") != NULL) {
		old_symlinks = 1;
	}

	if (devtype((spoolDir ? spoolDir : get_PKGLOC()), &pkgdev) ||
	    pkgdev.dirname == NULL) {
		progerr(ERR_BAD_DEVICE, spoolDir ? spoolDir : get_PKGLOC());
		quit(1);
		/* NOTREACHED */
	}

	pkgdir = pkgdev.dirname;
	repeat = ((optind >= argc) && pkgdev.mount);

	/*
	 * error if there are packages on the command line and a category
	 * was specified
	 */

	if (optind < argc && catg_arg != NULL) {
		progerr(ERR_PKGS_AND_CAT_PKGRM);
		usage();
		/* NOTREACHED */
	}

	/*
	 * ********************************************************************
	 * main package processing "loop"
	 * ********************************************************************
	 */

	for (;;) {
		boolean_t	b;
		char		**pkglist;	/* points to array of pkgs */

		/*
		 * mount the spool device if required
		 */

		if (pkgdev.mount) {
			if (n = pkgmount(&pkgdev, NULL, 0, 0, 1)) {
				quit(n);
				/* NOTREACHED */
			}
		}

		if (chdir(pkgdev.dirname)) {
			progerr(ERR_CHDIR, pkgdev.dirname);
			quit(1);
			/* NOTREACHED */
		}

		/*
		 * spool device mounted/available - get the list of the
		 * packages to remove
		 */

		n = pkgGetPackageList(&pkglist, argv, optind,
			catg_arg, category, &pkgdev);

		switch (n) {
			case -1:	/* no packages found */
				echoDebug(DBG_PKGLIST_RM_NONFOUND,
					PSTR(pkgdev.dirname));
				progerr(ERR_NOPKGS, pkgdev.dirname);
				quit(1);
				/* NOTREACHED */

			case 0:		/* packages found */
				break;

			default:	/* "quit" error */
				echoDebug(DBG_PKGLIST_RM_ERROR,
					pkgdev.dirname, n);
				quit(n);
				/* NOTREACHED */
		}

		/*
		 * count the number of packages to remove
		 * NOTE: npkgs is a global variable that is referenced by quit.c
		 * when error messages are generated - it is referenced directly
		 * by the other functions called below...
		 */

		for (npkgs = 0; pkglist[npkgs] != (char *)NULL; /* void */) {
			pkgLgth = strlen(pkglist[npkgs]);
			if (pkgLgth > longestPkg) {
				longestPkg = pkgLgth;
			}
			echoDebug(DBG_PKG_SELECTED, npkgs, pkglist[npkgs]);
			npkgs++;
		}

		/* output number of packages to be removed */

		echoDebug(DBG_NUM_PKGS_TO_REMOVE, npkgs, longestPkg);

		/*
		 * package list generated - remove packages
		 */

		b = remove_packages(pkglist, nodelete, longestPkg, repeat,
			altBinDir, pkgdev.dirname, spoolDir, noZones);

		/*
		 * unmount the spool directory if necessary
		 */

		if (pkgdev.mount) {
			(void) chdir("/");
			if (pkgumount(&pkgdev)) {
				progerr(ERR_PKGUNMOUNT, pkgdev.bdevice);
				quit(99);
				/* NOTREACHED */

			}
		}

		/*
		 * continue with next sequence of packages if continue set
		 */

		if (b == B_TRUE) {
			continue;
		}

		/*
		 * not continuing - quit with 0 exit code
		 */

		quit(0);
		/* NOTREACHED */
#ifdef lint
		return (0);
#endif	/* lint */
	}
}

/*
 * *****************************************************************************
 * static internal (private) functions
 * *****************************************************************************
 */

/*
 * Name:	doRemove
 * Description:	Remove a package from the global zone, and optionally from one
 *		or more non-global zones.
 * Arguments:	a_nodelete: should the files and scripts remain installed?
 *			- if != 0 pass -F flag to pkgremove - suppress
 *			the removal of any files and any class action scripts
 *			and suppress the running of any class action scripts.
 *			The package files remain but the package looks like it
 *			is not installed. This is mainly for use by upgrade.
 *			- if == 0 do not pass -F flag to pkgremove - all
 *			files and class action scripts are removed, and any
 *			appropriate class action scripts are run.
 *		a_altBinDir - pointer to string representing location of the
 *			pkgremove executable to run. If not NULL, then pass
 *			the path specified to the -b option to pkgremove.
 *		a_longestPkg - length of the longest package "name" (for
 *			output format alignment)
 *		a_adminFile - pointer to string representing the admin
 *			file to pass to pkgremove when removing a package from
 *			the global zone only. Typically the admin file used for
 *			the global zone is the admin file passed in by the user.
 *			If this is == NULL no admin file is given to pkgremove.
 *		a_zoneAdminFile - pointer to string representing the admin
 *			file to pass to pkgremove when removing the package
 *			from a non-global zone only. Typically the admin file
 *			used for non-global zones supresses all checks since
 *			the dependency checking is done for all zones first
 *			before proceeding.
 *			A zoneAdminFile MUST be specified if a_zlst != NULL.
 *			A zoneAdminFile must NOT be specified if a_zlst == NULL.
 *		a_zlst - list of zones to process; NULL if no zones to process.
 * Returns:	int	(see ckreturn() function for details)
 *		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" will be added to indicate "immediate reboot required"
 *		"20" will be added to indicate "reboot after install required"
 */

static int
doRemove(int a_nodelete, char *a_altBinDir, int a_longestPkg, char *a_adminFile,
	char *a_zoneAdminFile, zoneList_t a_zlst)
{
	boolean_t	b;
	char		*zoneName;
	char		ans[MAX_INPUT];
	int		n;
	int		zoneIndex;
	int		zonesSkipped;
	struct pkginfo	*pinfo = (struct pkginfo *)NULL;
	zone_state_t	zst;

	/* entry assertions */

	if (a_zlst != (zoneList_t)NULL) {
		/* zone list specified - zone admin file required */
		assert(a_zoneAdminFile != (char *)NULL);
		assert(*a_zoneAdminFile != '\0');
	} else {
		/* no zone list specified - no zone admin file needed */
		assert(a_zoneAdminFile == (char *)NULL);
	}

	/* NOTE: required 'pkgdir' set to spool directory or NULL */
	b = pkginfoIsPkgInstalled(&pinfo, pkginst);
	if (b == B_FALSE) {
		progerr(ERR_NO_SUCH_INSTANCE, pkginst);
		pkginfoFree(&pinfo);
		return (2);
	}

	/* entry debugging info */

	echoDebug(DBG_DOREMOVE_ENTRY);
	echoDebug(DBG_DOREMOVE_ARGS, PSTR(pinfo->pkginst), PSTR(pinfo->name),
		PSTR(pinfo->arch), PSTR(pinfo->version), PSTR(pinfo->basedir),
		PSTR(pinfo->catg), pinfo->status);

	if (!nointeract) {
		char	fmt1[100];

		/* create format based on max pkg name length */

		(void) snprintf(fmt1, sizeof (fmt1), "   %%-%d.%ds  %%s",
				a_longestPkg, a_longestPkg);

		if (pinfo->status == PI_SPOOLED) {
			echo(INFO_SPOOLED);
		} else {
			if (getuid()) {
				progerr(ERR_NOT_ROOT, get_prog_name());
				exit(1);
			}
			echo(INFO_INSTALL);
		}

		echo(fmt1, pinfo->pkginst, pinfo->name);

		if (pinfo->arch || pinfo->version) {
			char	fmt2[100];

			/* create format based on max pkg name length */

			(void) snprintf(fmt2, sizeof (fmt2), "   %%%d.%ds  ",
					a_longestPkg, a_longestPkg);

			/* LINTED variable format specifier to fprintf() */
			(void) fprintf(stderr, fmt2, "");

			if (pinfo->arch) {
				(void) fprintf(stderr, "(%s) ", pinfo->arch);
			}

			if (pinfo->version) {
				(void) fprintf(stderr, "%s", pinfo->version);
			}

			(void) fprintf(stderr, "\n");
		}

		n = ckyorn(ans, NULL, NULL, NULL, ASK_CONFIRM);
		if (n != 0) {
			quit(n);
			/* NOTREACHED */
		}

		if (strchr("yY", *ans) == NULL) {
			pkginfoFree(&pinfo);
			return (0);
		}
	}

	if (pinfo->status == PI_SPOOLED) {
		/* removal from a directory */
		echo(INFO_RMSPOOL, pkginst);
		pkginfoFree(&pinfo);
		return (rrmdir(pkginst));
	}

	/* exit if not root */

	if (getuid()) {
		progerr(ERR_NOT_ROOT, get_prog_name());
		exit(1);
	}

	pkginfoFree(&pinfo);

	zonesSkipped = 0;

	if (interrupted != 0) {
		echo(MSG_DOREMOVE_INTERRUPTED_B4_Z, pkginst);
		echoDebug(MSG_DOREMOVE_INTERRUPTED_B4_Z, pkginst);
		return (n);
	}

	echoDebug(DBG_REMOVE_FLAG_VALUES, "before pkgZoneRemove",
		admnflag, doreboot, failflag, interrupted,
		intrflag, ireboot, nullflag, warnflag);

	for (zoneIndex = 0;
	    a_zlst != NULL &&
	    (zoneName = z_zlist_get_zonename(a_zlst, zoneIndex)) != NULL;
	    zoneIndex++) {

		/* skip the zone if it is NOT running */

		zst = z_zlist_get_current_state(a_zlst, zoneIndex);
		if (zst != ZONE_STATE_RUNNING && zst != ZONE_STATE_MOUNTED) {
			zonesSkipped++;
			echoDebug(DBG_SKIPPING_ZONE, zoneName);
			continue;
		}

		echo(MSG_REMOVE_PKG_FROM_ZONE, pkginst, zoneName);
		echoDebug(DBG_REMOVE_PKG_FROM_ZONE, pkginst, zoneName);

		/*
		 * remove package from zone; use the zone admin file which
		 * suppresses all checks.
		 */

		n = pkgZoneRemove(z_zlist_get_scratch(a_zlst, zoneIndex),
			a_nodelete, a_altBinDir, a_zoneAdminFile,
			zst, B_FALSE);

		/* set success/fail condition variables */

		ckreturn(n);

		echoDebug(DBG_REMOVE_FLAG_VALUES, "after pkgZoneRemove",
			admnflag, doreboot, failflag, interrupted, intrflag,
			ireboot, nullflag, warnflag);
	}

	if (zonesSkipped > 0) {
		echoDebug(DBG_ZONES_SKIPPED, zonesSkipped);

		for (zoneIndex = 0;
			(zoneName = z_zlist_get_zonename(a_zlst, zoneIndex)) !=
				(char *)NULL; zoneIndex++) {

			/* skip the zone if it IS running */

			zst = z_zlist_get_current_state(a_zlst, zoneIndex);
			if (zst == ZONE_STATE_RUNNING ||
			    zst == ZONE_STATE_MOUNTED) {
				zonesSkipped++;
				echoDebug(DBG_SKIPPING_ZONE_BOOT, zoneName);
				continue;
			}

			/* skip the zone if it is NOT bootable */

			if (z_zlist_is_zone_runnable(a_zlst,
						zoneIndex) == B_FALSE) {
				echo(MSG_SKIPPING_ZONE_NOT_RUNNABLE, zoneName);
				echoDebug(DBG_SKIPPING_ZONE_NOT_RUNNABLE,
					zoneName);
				continue;
			}

			/* mount up the zone */

			echo(MSG_BOOTING_ZONE, zoneName);
			echoDebug(DBG_BOOTING_ZONE, zoneName);

			b = z_zlist_change_zone_state(a_zlst, zoneIndex,
				ZONE_STATE_MOUNTED);
			if (b == B_FALSE) {
				progerr(ERR_CANNOT_BOOT_ZONE, zoneName);
				/* set fatal error return condition */
				ckreturn(1);
				continue;
			}

			echo(MSG_REMOVE_PKG_FROM_ZONE, pkginst, zoneName);

			/*
			 * remove package from zone; use the zone admin file
			 * which suppresses all checks.
			 */

			n = pkgZoneRemove(z_zlist_get_scratch(a_zlst,
				zoneIndex), a_nodelete, a_altBinDir,
				a_zoneAdminFile, ZONE_STATE_MOUNTED, B_TRUE);

			/* set success/fail condition variables */

			ckreturn(n);

			echoDebug(DBG_REMOVE_FLAG_VALUES, "after pkgZoneRemove",
				admnflag, doreboot, failflag, interrupted,
				intrflag, ireboot, nullflag, warnflag);

			/* restore original state of zone */

			echo(MSG_RESTORE_ZONE_STATE, zoneName);
			echoDebug(DBG_RESTORE_ZONE_STATE, zoneName);

			b = z_zlist_restore_zone_state(a_zlst, zoneIndex);
		}
	}

	/*
	 * Process global zone if it was either the only possible
	 * target (no list of zones specified) or it appears in the list
	 */
	if (a_zlst == NULL || z_on_zone_spec(GLOBAL_ZONENAME)) {
		/* reset interrupted flag before calling pkgremove */
		interrupted = 0;	/* last action was NOT quit */

		/*
		 * call pkgremove for this package for the global zone;
		 * use the admin file passed in by the user via -a.
		 */
		n = pkgRemove(a_nodelete, a_altBinDir, a_adminFile);

		/* set success/fail condition variables */
		ckreturn(n);
	}

	return (n);
}

/*
 *  function to clear out any exisiting error return conditions that may have
 *  been set by previous calls to ckreturn()
 */
static void
resetreturn()
{
	admnflag = 0;	/* != 0 if any pkg op admin setting failure (4) */
	doreboot = 0;	/* != 0 if reboot required after installation (>= 10) */
	failflag = 0;	/* != 0 if fatal error has occurred (1) */
	intrflag = 0;	/* != 0 if user selected quit (3) */
	ireboot = 0;	/* != 0 if immediate reboot required (>= 20) */
	nullflag = 0;	/* != 0 if admin interaction required (5) */
	warnflag = 0;	/* != 0 if non-fatal error has occurred (2) */
	interrupted = 0;	/* last pkg op was quit (1,2,3,4,5) */
}

/*
 *  function which checks the indicated return value
 *  and indicates disposition of installation
 */
static void
ckreturn(int retcode)
{
	/*
	 * entry debugging info
	 */

	echoDebug(DBG_PKGRM_CKRETURN, retcode, PSTR(pkginst));

	switch (retcode) {
	    case  0:		/* successful */
	    case 10:
	    case 20:
		break; /* empty case */

	    case  1:		/* package operation failed (fatal error) */
	    case 11:
	    case 21:
		failflag++;
		interrupted++;
		break;

	    case  2:		/* non-fatal error (warning) */
	    case 12:
	    case 22:
		warnflag++;
		interrupted++;
		break;

	    case  3:		/* user selected quit; operation interrupted */
	    case 13:
	    case 23:
		intrflag++;
		interrupted++;
		break;

	    case  4:		/* admin settings prevented operation */
	    case 14:
	    case 24:
		admnflag++;
		interrupted++;
		break;

	    case  5:		/* administration: interaction req (no -n) */
	    case 15:
	    case 25:
		nullflag++;
		interrupted++;
		break;

	    default:
		failflag++;
		interrupted++;
		return;
	}

	if (retcode >= 20) {
		ireboot++;
	} else if (retcode >= 10) {
		doreboot++;
	}
}

static int
pkgZoneCheckRemove(char *a_zoneName, char *a_altBinDir, char *a_adminFile,
	char *a_stdoutPath, zone_state_t a_zoneState, boolean_t tmpzone)
{
	char	*arg[MAXARGS];
	char	*p;
	char	adminfd_path[PATH_MAX];
	char	path[PATH_MAX];
	int	fds[MAX_FDS];
	int	maxfds;
	int	n;
	int	nargs;

	/* entry assertions */

	assert(a_zoneName != (char *)NULL);
	assert(*a_zoneName != '\0');

	/* entry debugging info */

	echoDebug(DBG_PKGZONECHECKREMOVE_ENTRY);
	echoDebug(DBG_PKGZONECHECKREMOVE_ARGS, a_zoneName, PSTR(pkginst),
		PSTR(pkgdev.dirname), PSTR(a_adminFile), PSTR(a_stdoutPath));

	/* generate path to pkgremove */

	(void) snprintf(path, sizeof (path), "%s/pkgremove",
		a_altBinDir == (char *)NULL ? PKGBIN : a_altBinDir);

	/* start at first file descriptor */

	maxfds = 0;

	/*
	 * generate argument list for call to pkgremove
	 */

	/* start at argument 0 */

	nargs = 0;

	/* first argument is path to executable */

	arg[nargs++] = strdup(path);

	/* second argument is always: pass -O debug to pkgremove: debug mode */

	if (debugFlag == B_TRUE) {
		arg[nargs++] = "-O";
		arg[nargs++] = "debug";
	}

	/* pkgrm -b dir: pass -b to pkgremove */

	if (a_altBinDir != (char *)NULL) {
		arg[nargs++] = "-b";
		arg[nargs++] = a_altBinDir;
	}

	/*
	 * NONABI_SCRIPTS defined: pass -o to pkgremove; refers to a
	 * pkg requiring operator interaction during a procedure script
	 * (common before on1093)
	 */

	if (old_pkg) {
		arg[nargs++] = "-o";
	}

	/*
	 * PKG_NONABI_SYMLINKS defined: pass -y to pkgremove; process
	 * symlinks consistent with old behavior
	 */

	if (old_symlinks) {
		arg[nargs++] = "-y";
	}

	/* pkgrm -M: pass -M to pkgremove: don't mount client file systems */

	arg[nargs++] = "-M";

	/* pkgrm -A: pass -A to pkgremove */

	if (pkgrmremote) {
		arg[nargs++] = "-A";
	}

	/* pkgrm -v: pass -v to pkgremove: never trace scripts */

	/* pass "-O enable-hollow-package-support" */

	if (is_depend_pkginfo_DB()) {
		arg[nargs++] = "-O";
		arg[nargs++] = "enable-hollow-package-support";
	}

	/* pass -n to pkgremove: always in noninteractive mode */

	arg[nargs++] = "-n";

	/* pkgrm -a admin: pass -a admin to pkgremove: admin file */

	if (a_adminFile) {
		int fd;
		fd = openLocal(a_adminFile, O_RDONLY, tmpdir);
		if (fd < 0) {
			progerr(ERR_CANNOT_COPY_LOCAL, a_adminFile,
				errno, strerror(errno));
			return (1);
		}
		(void) snprintf(adminfd_path, sizeof (adminfd_path),
			"/proc/self/fd/%d", fd);
		fds[maxfds++] = fd;
		arg[nargs++] = "-a";
		arg[nargs++] = strdup(adminfd_path);
	}

	/*
	 * pkgadd -R root: pass -R /a to pkgremove in mounted zone
	 */
	if (a_zoneState == ZONE_STATE_MOUNTED) {
		arg[nargs++] = "-R";
		arg[nargs++] = "/a";
	}

	/* pkgrm -F: pass -F to pkgremove: always update DB only */

	arg[nargs++] = "-F";

	/* pass "-O preremovecheck" */

	arg[nargs++] = "-O";
	arg[nargs++] = "preremovecheck";

	/* add "-O addzonename" */

	arg[nargs++] = "-O";
	arg[nargs++] = "addzonename";

	/*
	 * add parent zone info/type
	 */

	p = z_get_zonename();
	if ((p != NULL) && (*p != '\0')) {
			char	zn[MAXPATHLEN];
			(void) snprintf(zn, sizeof (zn),
				"parent-zone-name=%s", p);
			arg[nargs++] = "-O";
			arg[nargs++] = strdup(zn);
	}

	/* current zone type */

	arg[nargs++] = "-O";
	if (z_running_in_global_zone() == B_TRUE) {
			char	zn[MAXPATHLEN];
			(void) snprintf(zn, sizeof (zn),
				"parent-zone-type=%s",
				TAG_VALUE_GLOBAL_ZONE);
			arg[nargs++] = strdup(zn);
	} else {
			char	zn[MAXPATHLEN];
			(void) snprintf(zn, sizeof (zn),
				"parent-zone-type=%s",
				TAG_VALUE_NONGLOBAL_ZONE);
			arg[nargs++] = strdup(zn);
	}

	/* Add arguments how to start the pkgserv */

	arg[nargs++] = "-O";
	arg[nargs++] = pkgmodeargument(tmpzone ? RUN_ONCE : pkgservergetmode());

	/* pass -N to pkgremove: program name to report */

	arg[nargs++] = "-N";
	arg[nargs++] = get_prog_name();

	/* add package instance name */

	arg[nargs++] = pkginst;

	/* terminate argument list */

	arg[nargs++] = NULL;

	/* execute pkgremove command */

	if (debugFlag == B_TRUE) {
		echoDebug(DBG_ZONE_EXEC_ENTER, a_zoneName, arg[0]);
		for (n = 0; arg[n]; n++) {
			echoDebug(DBG_ARG, n, arg[n]);
		}
	}

	/* terminate file descriptor list */

	fds[maxfds] = -1;

	/* exec command in zone */

	n = z_zone_exec(a_zoneName, path, arg, a_stdoutPath, (char *)NULL, fds);

	echoDebug(DBG_ZONE_EXEC_EXIT, a_zoneName, arg[0], n,
			PSTR(a_stdoutPath));

	/*
	 * close any files that were opened for use by the
	 * /proc/self/fd interface so they could be passed to programs
	 * via the z_zone_exec() interface
	 */

	for (; maxfds > 0; maxfds--) {
		(void) close(fds[maxfds-1]);
	}

	/* return results of pkgremove in zone execution */

	return (n);
}

static int
pkgZoneRemove(char *a_zoneName, int a_nodelete, char *a_altBinDir,
	char *a_adminFile, zone_state_t a_zoneState, boolean_t tmpzone)
{
	char	*arg[MAXARGS];
	char	*p;
	char	adminfd_path[PATH_MAX];
	char	path[PATH_MAX];
	int	fds[MAX_FDS];
	int	maxfds;
	int	n;
	int	nargs;

	/* entry assertions */

	assert(a_zoneName != (char *)NULL);
	assert(*a_zoneName != '\0');

	/* entry debugging info */

	echoDebug(DBG_PKGZONEREMOVE_ENTRY);
	echoDebug(DBG_PKGZONEREMOVE_ARGS, a_zoneName, PSTR(pkginst),
		PSTR(pkgdev.dirname), a_nodelete, PSTR(a_adminFile));

	/* generate path to pkgremove */

	(void) snprintf(path, sizeof (path), "%s/pkgremove",
		a_altBinDir == (char *)NULL ? PKGBIN : a_altBinDir);

	/* start at first file descriptor */

	maxfds = 0;

	/*
	 * generate argument list for call to pkgremove
	 */

	/* start at argument 0 */

	nargs = 0;

	/* first argument is path to executable */

	arg[nargs++] = strdup(path);

	/* second argument is always: pass -O debug to pkgremove: debug mode */

	if (debugFlag == B_TRUE) {
		arg[nargs++] = "-O";
		arg[nargs++] = "debug";
	}

	/* pkgrm -b dir: pass -b to pkgremove */

	if (a_altBinDir != (char *)NULL) {
		arg[nargs++] = "-b";
		arg[nargs++] = a_altBinDir;
	}

	/*
	 * NONABI_SCRIPTS defined: pass -o to pkgremove; refers to a
	 * pkg requiring operator interaction during a procedure script
	 * (common before on1093)
	 */

	if (old_pkg) {
		arg[nargs++] = "-o";
	}

	/*
	 * PKG_NONABI_SYMLINKS defined: pass -y to pkgremove; process
	 * symlinks consistent with old behavior
	 */

	if (old_symlinks) {
		arg[nargs++] = "-y";
	}

	/* pkgrm -M: pass -M to pkgremove: don't mount client file systems */

	arg[nargs++] = "-M";

	/* pkgrm -A: pass -A to pkgremove */

	if (pkgrmremote) {
		arg[nargs++] = "-A";
	}

	/* pkgrm -v: pass -v to pkgremove: trace scripts */

	if (pkgverbose) {
		arg[nargs++] = "-v";
	}

	/* pass "-O enable-hollow-package-support" */

	if (is_depend_pkginfo_DB()) {
		arg[nargs++] = "-O";
		arg[nargs++] = "enable-hollow-package-support";
	}

	/* pkgrm -n: pass -n to pkgremove: noninteractive mode */

	if (nointeract) {
		arg[nargs++] = "-n";
	}

	/* pkgrm -a admin: pass -a admin to pkgremove: admin file */

	if (a_adminFile) {
		int fd;
		fd = openLocal(a_adminFile, O_RDONLY, tmpdir);
		if (fd < 0) {
			progerr(ERR_CANNOT_COPY_LOCAL, a_adminFile,
				errno, strerror(errno));
			return (1);
		}
		(void) snprintf(adminfd_path, sizeof (adminfd_path),
			"/proc/self/fd/%d", fd);
		fds[maxfds++] = fd;
		arg[nargs++] = "-a";
		arg[nargs++] = adminfd_path;
	}

	/*
	 * pkgadd -R root: pass -R /a to pkgremove in mounted zone
	 */
	if (a_zoneState == ZONE_STATE_MOUNTED) {
		arg[nargs++] = "-R";
		arg[nargs++] = "/a";
	}

	/* pkgrm -F: pass -F to pkgremove: update DB only */

	if (a_nodelete) {
		arg[nargs++] = "-F";
	}

	/* add "-O addzonename" */

	arg[nargs++] = "-O";
	arg[nargs++] = "addzonename";

	/*
	 * add parent zone info/type
	 */

	p = z_get_zonename();
	if ((p != NULL) && (*p != '\0')) {
			char	zn[MAXPATHLEN];
			(void) snprintf(zn, sizeof (zn),
				"parent-zone-name=%s", p);
			arg[nargs++] = "-O";
			arg[nargs++] = strdup(zn);
	}

	/* current zone type */

	arg[nargs++] = "-O";
	if (z_running_in_global_zone() == B_TRUE) {
			char	zn[MAXPATHLEN];
			(void) snprintf(zn, sizeof (zn),
				"parent-zone-type=%s",
				TAG_VALUE_GLOBAL_ZONE);
			arg[nargs++] = strdup(zn);
	} else {
			char	zn[MAXPATHLEN];
			(void) snprintf(zn, sizeof (zn),
				"parent-zone-type=%s",
				TAG_VALUE_NONGLOBAL_ZONE);
			arg[nargs++] = strdup(zn);
	}

	/* Add arguments how to start the pkgserv */

	arg[nargs++] = "-O";
	arg[nargs++] = pkgmodeargument(tmpzone ? RUN_ONCE : pkgservergetmode());

	/* pass -N to pkgremove: program name to report */

	arg[nargs++] = "-N";
	arg[nargs++] = get_prog_name();

	/* add package instance name */

	arg[nargs++] = pkginst;

	/* terminate argument list */

	arg[nargs++] = NULL;

	/* execute pkgremove command */

	if (debugFlag == B_TRUE) {
		echoDebug(DBG_ZONE_EXEC_ENTER, a_zoneName, arg[0]);
		for (n = 0; arg[n]; n++) {
			echoDebug(DBG_ARG, n, arg[n]);
		}
	}

	/* terminate file descriptor list */

	fds[maxfds] = -1;

	/* exec command in zone */

	n = z_zone_exec(a_zoneName, path, arg, (char *)NULL, (char *)NULL, fds);

	/*
	 * close any files that were opened for use by the
	 * /proc/self/fd interface so they could be passed to programs
	 * via the z_zone_exec() interface
	 */

	for (; maxfds > 0; maxfds--) {
		(void) close(fds[maxfds-1]);
	}

	return (n);
}

/*
 * Name:	pkgRemove
 * Description:	Invoke pkgremove in the current zone to perform a remove
 *		of a single package from the current zone or standalone system
 * Arguments:	a_nodelete: should the files and scripts remain installed?
 *			- if != 0 pass -F flag to pkgremove - suppress
 *			the removal of any files and any class action scripts
 *			and suppress the running of any class action scripts.
 *			The package files remain but the package looks like it
 *			is not installed. This is mainly for use by upgrade.
 *			- if == 0 do not pass -F flag to pkgremove - all
 *			files and class action scripts are removed, and any
 *			appropriate class action scripts are run.
 *		a_altBinDir - pointer to string representing location of the
 *			pkgremove executable to run. If not NULL, then pass
 *			the path specified to the -b option to pkgremove.
 *		a_adminFile - pointer to string representing the admin
 *			file to pass to pkgremove when removing the package.
 *			If this is == NULL no admin file is given to pkgremove.
 * Returns:	int	(see ckreturn() function for details)
 *		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" will be added to indicate "immediate reboot required"
 *		"20" will be added to indicate "reboot after install required"
 */

static int
pkgRemove(int a_nodelete, char *a_altBinDir, char *a_adminFile)
{
	char	*arg[MAXARGS];
	char	*p;
	char	path[PATH_MAX];
	int	n;
	int	nargs;

	/* entry debugging info */

	echoDebug(DBG_PKGREMOVE_ENTRY);
	echoDebug(DBG_PKGREMOVE_ARGS, PSTR(pkginst), PSTR(pkgdev.dirname),
		a_nodelete, PSTR(a_adminFile));

	(void) snprintf(path, sizeof (path), "%s/pkgremove",
		a_altBinDir == (char *)NULL ? PKGBIN : a_altBinDir);

	nargs = 0;

	/* first argument is path to executable */

	arg[nargs++] = strdup(path);

	/* second argument is always: pass -O debug to pkgremove: debug mode */

	if (debugFlag == B_TRUE) {
		arg[nargs++] = "-O";
		arg[nargs++] = "debug";
	}

	/* Add arguments how to start the pkgserv */

	arg[nargs++] = "-O";
	arg[nargs++] = pkgmodeargument(pkgservergetmode());

	/* pkgrm -b dir: pass -b to pkgremove */

	if (a_altBinDir != (char *)NULL) {
		arg[nargs++] = "-b";
		arg[nargs++] = a_altBinDir;
	}

	/*
	 * NONABI_SCRIPTS defined: pass -o to pkgremove; refers to a
	 * pkg requiring operator interaction during a procedure script
	 * (common before on1093)
	 */

	if (old_pkg) {
		arg[nargs++] = "-o";
	}

	/*
	 * PKG_NONABI_SYMLINKS defined: pass -y to pkgremove; process
	 * symlinks consistent with old behavior
	 */

	if (old_symlinks) {
		arg[nargs++] = "-y";
	}

	/* pkgrm -M: pass -M to pkgrm: dont mount client file systems */

	if (no_map_client) {
		arg[nargs++] = "-M";
	}

	/* pkgrm -A: pass -A to pkgrm */

	if (pkgrmremote) {
		arg[nargs++] = "-A";
	}

	/* pkgrm -v: pass -v to pkgremove: trace scripts */

	if (pkgverbose) {
		arg[nargs++] = "-v";
	}

	/* pkgrm -n: pass -n to pkgremove: noninteractive mode */

	if (nointeract) {
		arg[nargs++] = "-n";
	}

	/* pkgrm -a admin: pass -a admin to pkgremove: admin file */

	if (a_adminFile) {
		arg[nargs++] = "-a";
		arg[nargs++] = strdup(a_adminFile);
	}

	/* pkgrm -V vfstab: pass -V vfstab to pkgremove: alternate vfstab */

	if (vfstab_file) {
		arg[nargs++] = "-V";
		arg[nargs++] = vfstab_file;
	}

	/* pkgrm -R root: pass -R root to pkgremove: alternative root */

	if (is_an_inst_root()) {
		arg[nargs++] = "-R";
		arg[nargs++] = get_inst_root();
	}

	/* pkgrm -F: pass -F to pkgremove: update DB only */

	if (a_nodelete) {
		arg[nargs++] = "-F";
	}

	/*
	 * add parent zone info/type
	 */

	p = z_get_zonename();
	if ((p != NULL) && (*p != '\0')) {
			char	zn[MAXPATHLEN];
			(void) snprintf(zn, sizeof (zn),
				"parent-zone-name=%s", p);
			arg[nargs++] = "-O";
			arg[nargs++] = strdup(zn);
	}

	/* current zone type */

	arg[nargs++] = "-O";
	if (z_running_in_global_zone() == B_TRUE) {
			char	zn[MAXPATHLEN];
			(void) snprintf(zn, sizeof (zn),
				"parent-zone-type=%s",
				TAG_VALUE_GLOBAL_ZONE);
			arg[nargs++] = strdup(zn);
	} else {
			char	zn[MAXPATHLEN];
			(void) snprintf(zn, sizeof (zn),
				"parent-zone-type=%s",
				TAG_VALUE_NONGLOBAL_ZONE);
			arg[nargs++] = strdup(zn);
	}

	/* pass -N to pkgremove: program name to report */

	arg[nargs++] = "-N";
	arg[nargs++] = get_prog_name();

	/* add package instance name */

	arg[nargs++] = pkginst;

	/* terminate argument list */

	arg[nargs++] = NULL;

	/*
	 * run the appropriate pkgremove command in the specified zone
	 */

	if (debugFlag == B_TRUE) {
		echoDebug(DBG_ZONE_EXEC_ENTER, "global", arg[0]);
		for (n = 0; arg[n]; n++) {
			echoDebug(DBG_ARG, n, arg[n]);
		}
	}

	/* execute pkgremove command */

	n = pkgexecv(NULL, NULL, NULL, NULL, arg);

	/* return results of pkgrm in this zone */

	return (n);
}

static void
usage(void)
{
	char	*prog = get_prog_name();

	(void) fprintf(stderr, ERR_USAGE_PKGRM, prog, prog);
	exit(1);
}

/*
 * Name:	remove_packages_in_global_with_zones
 * Description:	Remove packages from the global zone and from non-global zones
 *		when run from the global zone and when non-global zones are
 *		present.
 * Arguments:	a_pkgList - pointer to array of strings, each string specifying
 *			the name of one package to be removed.
 *		a_nodelete: should the files and scripts remain installed?
 *			- if != 0 pass -F flag to pkgremove - suppress
 *			the removal of any files and any class action scripts
 *			and suppress the running of any class action scripts.
 *			The package files remain but the package looks like it
 *			is not installed. This is mainly for use by upgrade.
 *			- if == 0 do not pass -F flag to pkgremove - all
 *			files and class action scripts are removed, and any
 *			appropriate class action scripts are run.
 *		a_longestPkg - length of the longest package "name" (for
 *			output format alignment)
 *		a_repeat - are there more packages avialable in "optind"
 *			- B_TRUE - process packages from optind
 *			- B_FALSE - do not process packages from optind
 *		a_altBinDir - pointer to string representing location of the
 *			pkgremove executable to run. If not NULL, then pass
 *			the path specified to the -b option to pkgremove.
 *		a_pkgdir - pointer to string representing the directory
 *			where the packages to be removed are located.
 *		a_zlst - list of zones to process; NULL if no zones to process.
 * Returns:	int	(see ckreturn() function for details)
 *		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" will be added to indicate "immediate reboot required"
 *		"20" will be added to indicate "reboot after install required"
 */

static boolean_t
remove_packages_in_global_with_zones(char **a_pkgList, int a_nodelete,
	int a_longestPkg, int a_repeat, char *a_altBinDir, char *a_pkgdir,
	zoneList_t a_zlst)
{
static	char		*zoneAdminFile = (char *)NULL;

	boolean_t	b;
	char		*zoneName;
	char		*scratchName;
	char		preremovecheckPath[PATH_MAX+1];
	int		i;
	int		n;
	int		savenpkgs = npkgs;
	int		zoneIndex;
	int		zonesSkipped;
	zone_state_t	zst;

	/* entry assertions */

	assert(a_zlst != (zoneList_t)NULL);
	assert(a_pkgList != (char **)NULL);
	assert(a_longestPkg > 0);
	assert(a_pkgdir != (char *)NULL);
	assert(*a_pkgdir != '\0');

	/* entry debugging info */

	echoDebug(DBG_PKGREMPKGSGZWNGZ_ENTRY);
	echoDebug(DBG_PKGREMPKGSGZWNGZ_ARGS, a_nodelete, a_longestPkg,
		a_repeat, PSTR(a_altBinDir), PSTR(a_pkgdir));

	/* check all packages */

	if (check_packages(a_pkgList, a_pkgdir) != B_TRUE) {
		quit(1);
	}

	/* create temporary directory for use by zone operations */

	create_zone_tempdir(&zoneTempDir, tmpdir);

	/* create hands off settings admin file for use in a non-global zone */

	create_zone_adminfile(&zoneAdminFile, zoneTempDir, admnfile);

	/*
	 * all of the packages (as listed in the package list) are
	 * removed one at a time from all non-global zones and then
	 * from the global zone.
	 */

	for (i = 0; (pkginst = a_pkgList[i]) != NULL; i++) {
		/* reset interrupted flag before calling pkgremove */

		interrupted = 0;	/* last action was NOT quit */

		/* skip package if it is "in the global zone only" */

		if (pkgIsPkgInGzOnly(get_inst_root(), pkginst) == B_TRUE) {
			continue;
		}

		/*
		 * if operation failed in global zone do not propagate to
		 * non-global zones
		 */

		zonesSkipped = 0;

		if (interrupted != 0) {
			echo(MSG_DOREMOVE_INTERRUPTED, pkginst);
			echoDebug(DBG_DOREMOVE_INTERRUPTED, pkginst);
			break;
		}

		echoDebug(DBG_REMOVE_FLAG_VALUES, "before loop",
			admnflag, doreboot, failflag, interrupted,
			intrflag, ireboot, nullflag, warnflag);

		for (zoneIndex = 0;
			(zoneName = z_zlist_get_zonename(a_zlst, zoneIndex)) !=
				(char *)NULL; zoneIndex++) {

			/* skip the zone if it is NOT running */

			zst = z_zlist_get_current_state(a_zlst, zoneIndex);
			if (zst != ZONE_STATE_RUNNING &&
			    zst != ZONE_STATE_MOUNTED) {
				zonesSkipped++;
				echoDebug(DBG_SKIPPING_ZONE, zoneName);
				continue;
			}

			echo(MSG_CHECKREMOVE_PKG_IN_ZONE, pkginst, zoneName);
			echoDebug(DBG_CHECKREMOVE_PKG_IN_ZONE, pkginst,
				zoneName);

			scratchName = z_zlist_get_scratch(a_zlst, zoneIndex);

			(void) snprintf(preremovecheckPath,
				sizeof (preremovecheckPath),
				"%s/%s.%s.preremovecheck.txt",
				zoneTempDir, pkginst, scratchName);

			/*
			 * dependency check this package this zone; use the
			 * user supplied admin file so that the appropriate
			 * level of dependency checking is (or is not) done.
			 */

			n = pkgZoneCheckRemove(scratchName, a_altBinDir,
				admnfile, preremovecheckPath,
				zst, B_FALSE);

			/* set success/fail condition variables */

			ckreturn(n);

			echoDebug(DBG_REMOVE_FLAG_VALUES,
				"after pkgzonecheckremove",
				admnflag, doreboot, failflag, interrupted,
				intrflag, ireboot, nullflag, warnflag);
		}

		if (zonesSkipped == 0) {
			continue;
		}

		echoDebug(DBG_ZONES_SKIPPED, zonesSkipped);

		for (zoneIndex = 0;
			(zoneName = z_zlist_get_zonename(a_zlst, zoneIndex)) !=
				(char *)NULL; zoneIndex++) {

			/* skip the zone if it IS running */

			zst = z_zlist_get_current_state(a_zlst, zoneIndex);
			if (zst == ZONE_STATE_RUNNING ||
			    zst == ZONE_STATE_MOUNTED) {
				zonesSkipped++;
				echoDebug(DBG_SKIPPING_ZONE_BOOT, zoneName);
				continue;
			}

			/* skip the zone if it is NOT bootable */

			if (z_zlist_is_zone_runnable(a_zlst,
						zoneIndex) == B_FALSE) {
				echo(MSG_SKIPPING_ZONE_NOT_RUNNABLE, zoneName);
				echoDebug(DBG_SKIPPING_ZONE_NOT_RUNNABLE,
					zoneName);
				continue;
			}

			/* mount up the zone */

			echo(MSG_BOOTING_ZONE, zoneName);
			echoDebug(DBG_BOOTING_ZONE, zoneName);

			b = z_zlist_change_zone_state(a_zlst, zoneIndex,
				ZONE_STATE_MOUNTED);
			if (b == B_FALSE) {
				progerr(ERR_CANNOT_BOOT_ZONE, zoneName);
				/* set fatal error return condition */
				ckreturn(1);
				continue;
			}

			echo(MSG_CHECKREMOVE_PKG_IN_ZONE, pkginst, zoneName);
			echoDebug(DBG_CHECKREMOVE_PKG_IN_ZONE, pkginst,
					zoneName);

			scratchName = z_zlist_get_scratch(a_zlst, zoneIndex);

			(void) snprintf(preremovecheckPath,
				sizeof (preremovecheckPath),
				"%s/%s.%s.preremovecheck.txt",
				zoneTempDir, pkginst, scratchName);

			/*
			 * dependency check this package this zone; use the
			 * user supplied admin file so that the appropriate
			 * level of dependency checking is (or is not) done.
			 */

			n = pkgZoneCheckRemove(scratchName, a_altBinDir,
				admnfile, preremovecheckPath,
				ZONE_STATE_MOUNTED, B_TRUE);

			/* set success/fail condition variables */

			ckreturn(n);

			echoDebug(DBG_REMOVE_FLAG_VALUES,
				"after pkgzonecheckremove",
				admnflag, doreboot, failflag, interrupted,
				intrflag, ireboot, nullflag, warnflag);

			/* restore original state of zone */

			echo(MSG_RESTORE_ZONE_STATE, zoneName);
			echoDebug(DBG_RESTORE_ZONE_STATE, zoneName);

			b = z_zlist_restore_zone_state(a_zlst, zoneIndex);
		}
		npkgs--;
	}

	/*
	 * look at all pre-remove check files
	 */

	i = preremove_verify(a_pkgList, a_zlst, zoneTempDir);
	if (i != 0) {
		quit(i);
	}

	npkgs = savenpkgs;

	/*
	 * reset all error return condition variables that may have been
	 * set during package removal dependency checking so that they
	 * do not reflect on the success/failure of the actual package
	 * removal operations
	 */

	resetreturn();

	/*
	 * all of the packages (as listed in the package list) are
	 * removed one at a time.
	 */

	interrupted = 0;
	for (i = 0; (pkginst = a_pkgList[i]) != NULL; i++) {
		boolean_t	in_gz_only;
		started = 0;

		if (shall_we_continue(pkginst, npkgs) == B_FALSE) {
			continue;
		}

		in_gz_only = pkgIsPkgInGzOnly(get_inst_root(), pkginst);

		/* reset interrupted flag before calling pkgremove */

		interrupted = 0;

		/*
		 * pkgrm invoked from within the global zone and there are
		 * non-global zones configured:
		 * Remove the package from the global zone.
		 * If not removing the package from the global zone only,
		 * then remove the package from the list of zones specified.
		 */

		if (in_gz_only) {
			/* global zone only */
			n = doRemove(a_nodelete, a_altBinDir, a_longestPkg,
				admnfile, (char *)NULL, (zoneList_t)NULL);
		} else {
			/* global zone and non-global zones */
			n = doRemove(a_nodelete, a_altBinDir, a_longestPkg,
				zoneAdminFile, zoneAdminFile, a_zlst);
		}

		/* set success/fail condition variables */

		ckreturn(n);

		npkgs--;
	}

	/*
	 * all packages in the package list have been removed.
	 * Continue with removal if:
	 * -- immediate reboot is NOT required
	 * -- there are more packages to remove
	 * else return do NOT continue.
	 */

	if ((ireboot == 0) && (a_repeat != 0)) {
		return (B_TRUE);
	}

	/* return 'dont continue' */

	return (B_FALSE);
}

/*
 * Name:	remove_packages_in_nonglobal_zone
 * Description:	Remove packages in a non-global zone when run from a
 *		non-global zone.
 * Arguments:	a_pkgList - pointer to array of strings, each string specifying
 *			the name of one package to be removed.
 *		a_nodelete: should the files and scripts remain installed?
 *			- if != 0 pass -F flag to pkgremove - suppress
 *			the removal of any files and any class action scripts
 *			and suppress the running of any class action scripts.
 *			The package files remain but the package looks like it
 *			is not installed. This is mainly for use by upgrade.
 *			- if == 0 do not pass -F flag to pkgremove - all
 *			files and class action scripts are removed, and any
 *			appropriate class action scripts are run.
 *		a_longestPkg - length of the longest package "name" (for
 *			output format alignment)
 *		a_repeat - are there more packages avialable in "optind"
 *			- B_TRUE - process packages from optind
 *			- B_FALSE - do not process packages from optind
 *		a_altBinDir - pointer to string representing location of the
 *			pkgremove executable to run. If not NULL, then pass
 *			the path specified to the -b option to pkgremove.
 *		a_pkgdir - pointer to string representing the directory
 *			where the packages to be removed are located.
 * Returns:	int	(see ckreturn() function for details)
 *		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" will be added to indicate "immediate reboot required"
 *		"20" will be added to indicate "reboot after install required"
 */

static boolean_t
remove_packages_in_nonglobal_zone(char **a_pkgList, int a_nodelete,
	int a_longestPkg, int a_repeat, char *a_altBinDir, char *a_pkgdir)
{
static	char		*zoneAdminFile = (char *)NULL;

	int		n;
	int		i;

	/* entry assertions */

	assert(a_pkgList != (char **)NULL);
	assert(a_longestPkg > 0);
	assert(a_pkgdir != (char *)NULL);
	assert(*a_pkgdir != '\0');

	/* entry debugging info */

	echoDebug(DBG_PKGREMPKGSNGZ_ENTRY);
	echoDebug(DBG_PKGREMPKGSNGZ_ARGS, a_nodelete, a_longestPkg,
		a_repeat, PSTR(a_altBinDir), PSTR(a_pkgdir));

	/* check all package */

	if (check_packages(a_pkgList, a_pkgdir) != B_TRUE) {
		quit(1);
	}

	/* create temporary directory for use by zone operations */

	create_zone_tempdir(&zoneTempDir, tmpdir);

	/* create hands off settings admin file for use in a non-global zone */

	create_zone_adminfile(&zoneAdminFile, zoneTempDir, admnfile);

	/*
	 * all of the packages (as listed in the package list) are
	 * removed one at a time.
	 */

	interrupted = 0;
	for (i = 0; (pkginst = a_pkgList[i]) != NULL; i++) {
		started = 0;

		if (shall_we_continue(pkginst, npkgs) == B_FALSE) {
			continue;
		}

		interrupted = 0;

		/*
		 * pkgrm invoked from within a non-global zone: remove
		 * the package from the current zone only - no non-global
		 * zones are possible.
		 */

		n = doRemove(a_nodelete, a_altBinDir, a_longestPkg,
			admnfile, (char *)NULL, (zoneList_t)NULL);

		/* set success/fail condition variables */

		ckreturn(n);

		npkgs--;
	}

	/*
	 * all packages in the package list have been removed.
	 * Continue with removal if:
	 * -- immediate reboot is NOT required
	 * -- there are more packages to remove
	 * else return do NOT continue.
	 */

	if ((ireboot == 0) && (a_repeat != 0)) {
		return (B_TRUE);
	}

	/* return 'dont continue' */

	return (B_FALSE);
}

/*
 * Name:	remove_packages_in_global_no_zones
 * Description:	Remove packages from the global zone only when run in the
 *		global zone and no non-global zones are installed.
 * Arguments:	a_pkgList - pointer to array of strings, each string specifying
 *			the name of one package to be removed.
 *		a_nodelete: should the files and scripts remain installed?
 *			- if != 0 pass -F flag to pkgremove - suppress
 *			the removal of any files and any class action scripts
 *			and suppress the running of any class action scripts.
 *			The package files remain but the package looks like it
 *			is not installed. This is mainly for use by upgrade.
 *			- if == 0 do not pass -F flag to pkgremove - all
 *			files and class action scripts are removed, and any
 *			appropriate class action scripts are run.
 *		a_longestPkg - length of the longest package "name" (for
 *			output format alignment)
 *		a_repeat - are there more packages avialable in "optind"
 *			- B_TRUE - process packages from optind
 *			- B_FALSE - do not process packages from optind
 *		a_altBinDir - pointer to string representing location of the
 *			pkgremove executable to run. If not NULL, then pass
 *			the path specified to the -b option to pkgremove.
 * Returns:	int	(see ckreturn() function for details)
 *		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" will be added to indicate "immediate reboot required"
 *		"20" will be added to indicate "reboot after install required"
 */

static boolean_t
remove_packages_in_global_no_zones(char **a_pkgList, int a_nodelete,
	int a_longestPkg, int a_repeat, char *a_altBinDir)
{
	int	n;
	int	i;

	/* entry assertions */

	assert(a_pkgList != (char **)NULL);
	assert(a_longestPkg > 0);

	/* entry debugging info */

	echoDebug(DBG_PKGREMPKGSGZNNGZ_ENTRY);
	echoDebug(DBG_PKGREMPKGSGZNNGZ_ARGS, a_nodelete, a_longestPkg,
		a_repeat, PSTR(a_altBinDir));

	/*
	 * all of the packages (as listed in the package list) are
	 * removed one at a time.
	 */

	interrupted = 0;
	for (i = 0; (pkginst = a_pkgList[i]) != NULL; i++) {
		started = 0;

		if (shall_we_continue(pkginst, npkgs) == B_FALSE) {
			continue;
		}

		interrupted = 0;

		/*
		 * pkgrm invoked from within the global zone and there are
		 * NO non-global zones configured:
		 * Remove the package from the global zone only.
		 */

		n = doRemove(a_nodelete, a_altBinDir, a_longestPkg,
				admnfile, (char *)NULL, (zoneList_t)NULL);

		/* set success/fail condition variables */

		ckreturn(n);

		npkgs--;
	}

	/*
	 * all packages in the package list have been removed.
	 * Continue with removal if:
	 * -- immediate reboot is NOT required
	 * -- there are more packages to remove
	 * else return do NOT continue.
	 */

	if ((ireboot == 0) && (a_repeat != 0)) {
		return (B_TRUE);
	}

	/* return 'dont continue' */

	return (B_FALSE);
}

/*
 * Name:	remove_packages_from_spool_directory
 * Description:	Remove packages from a spool directory only.
 * Arguments:	a_pkgList - pointer to array of strings, each string specifying
 *			the name of one package to be removed.
 *		a_nodelete: should the files and scripts remain installed?
 *			- if != 0 pass -F flag to pkgremove - suppress
 *			the removal of any files and any class action scripts
 *			and suppress the running of any class action scripts.
 *			The package files remain but the package looks like it
 *			is not installed. This is mainly for use by upgrade.
 *			- if == 0 do not pass -F flag to pkgremove - all
 *			files and class action scripts are removed, and any
 *			appropriate class action scripts are run.
 *		a_longestPkg - length of the longest package "name" (for
 *			output format alignment)
 *		a_repeat - are there more packages avialable in "optind"
 *			- B_TRUE - process packages from optind
 *			- B_FALSE - do not process packages from optind
 *		a_altBinDir - pointer to string representing location of the
 *			pkgremove executable to run. If not NULL, then pass
 *			the path specified to the -b option to pkgremove.
 * Returns:	int	(see ckreturn() function for details)
 *		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" will be added to indicate "immediate reboot required"
 *		"20" will be added to indicate "reboot after install required"
 */

static boolean_t
remove_packages_from_spool_directory(char **a_pkgList, int a_nodelete,
	int a_longestPkg, int a_repeat, char *a_altBinDir)
{
	int	n;
	int	i;

	/* entry assertions */

	assert(a_pkgList != (char **)NULL);
	assert(a_longestPkg > 0);

	/*
	 * all of the packages (as listed in the package list) are
	 * removed one at a time.
	 */

	interrupted = 0;
	for (i = 0; (pkginst = a_pkgList[i]) != NULL; i++) {
		started = 0;

		if (shall_we_continue(pkginst, npkgs) == B_FALSE) {
			continue;
		}

		interrupted = 0;

		/*
		 * pkgrm invoked from any type of zone BUT the target
		 * to be removed is a local spool directory: remove the
		 * packages from the spool directory only.
		 */

		n = doRemove(a_nodelete, a_altBinDir, a_longestPkg,
			admnfile, (char *)NULL, (zoneList_t)NULL);

		/* set success/fail condition variables */

		ckreturn(n);

		npkgs--;
	}

	/*
	 * all packages in the package list have been removed.
	 * Continue with removal if:
	 * -- immediate reboot is NOT required
	 * -- there are more packages to remove
	 * else return do NOT continue.
	 */

	if ((ireboot == 0) && (a_repeat != 0)) {
		return (B_TRUE);
	}

	/* return 'dont continue' */

	return (B_FALSE);
}

/*
 * Name:	remove_packages
 * Description:	Remove packages from the global zone, and optionally from one
 *		or more non-global zones, or from a specified spool directory.
 * Arguments:	a_pkgList - pointer to array of strings, each string specifying
 *			the name of one package to be removed.
 *		a_nodelete: should the files and scripts remain installed?
 *			- if != 0 pass -F flag to pkgremove - suppress
 *			the removal of any files and any class action scripts
 *			and suppress the running of any class action scripts.
 *			The package files remain but the package looks like it
 *			is not installed. This is mainly for use by upgrade.
 *			- if == 0 do not pass -F flag to pkgremove - all
 *			files and class action scripts are removed, and any
 *			appropriate class action scripts are run.
 *		a_longestPkg - length of the longest package "name" (for
 *			output format alignment)
 *		a_repeat - are there more packages avialable in "optind"
 *			- B_TRUE - process packages from optind
 *			- B_FALSE - do not process packages from optind
 *		a_altBinDir - pointer to string representing location of the
 *			pkgremove executable to run. If not NULL, then pass
 *			the path specified to the -b option to pkgremove.
 *		a_pkgdir - pointer to string representing the directory
 *			where the packages to be removed are located.
 *		a_spoolDir - pointer to string specifying spool directory
 *			to remove packages from. If != NULL then all zones
 *			processing is bypassed and the packages are removed
 *			from the specified spool directory only.
 *		a_noZones - if non-global zones are configured, should the
 *			packages be removed from the non-global zones?
 *			- B_TRUE - do NOT remove packages from non-global zones
 *			- B_FALSE - remove packages from non-global zones
 * Returns:	int	(see ckreturn() function for details)
 *		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" will be added to indicate "immediate reboot required"
 *		"20" will be added to indicate "reboot after install required"
 */

static boolean_t
remove_packages(char **a_pkgList, int a_nodelete, int a_longestPkg,
	int a_repeat, char *a_altBinDir, char *a_pkgdir, char *a_spoolDir,
	boolean_t a_noZones)
{
	zoneList_t	zlst;
	boolean_t	b;

	/* entry assertions */

	assert(a_pkgList != (char **)NULL);

	echoDebug(DBG_REMOVEPKGS_ENTRY);
	echoDebug(DBG_REMOVEPKGS_ARGS, npkgs, a_nodelete, a_longestPkg,
		a_repeat, PSTR(a_pkgdir), PSTR(a_spoolDir));

	/*
	 * if removing from spool directory, bypass all zones checks
	 */

	if (a_spoolDir != (char *)NULL) {
		/* in non-global zone */

		echoDebug(DBG_REMOVE_PKGS_FROM_SPOOL, a_spoolDir);

		b = remove_packages_from_spool_directory(a_pkgList, a_nodelete,
			a_longestPkg, a_repeat, a_altBinDir);

		return (B_FALSE);
	}

	/* exit if not root */

	if (getuid()) {
		progerr(ERR_NOT_ROOT, get_prog_name());
		exit(1);
	}

	/*
	 * if running in the global zone AND one or more non-global
	 * zones exist, add packages in a 'zones aware' manner, else
	 * add packages in the standard 'non-zones aware' manner.
	 */

	if ((a_noZones == B_FALSE) && (z_running_in_global_zone() == B_FALSE)) {
		/* in non-global zone */

		echoDebug(DBG_IN_LZ);

		b = z_lock_this_zone(ZLOCKS_PKG_ADMIN);
		if (b != B_TRUE) {
			progerr(ERR_CANNOT_LOCK_THIS_ZONE);
			/* set fatal error return condition */
			ckreturn(1);
			return (B_FALSE);
		}

		b = remove_packages_in_nonglobal_zone(a_pkgList, a_nodelete,
			a_longestPkg, a_repeat, a_altBinDir, a_pkgdir);

		(void) z_unlock_this_zone(ZLOCKS_ALL);

		return (B_FALSE);
	}

	/* running in the global zone */

	b = z_non_global_zones_exist();
	if ((a_noZones == B_FALSE) && (b == B_TRUE)) {

		echoDebug(DBG_IN_GZ_WITH_LZ);

		/* get a list of all non-global zones */
		zlst = z_get_nonglobal_zone_list();
		if (zlst == (zoneList_t)NULL) {
			progerr(ERR_CANNOT_GET_ZONE_LIST);
			quit(1);
		}

		/* need to lock all of the zones */

		quitSetZonelist(zlst);
		b = z_lock_zones(zlst, ZLOCKS_PKG_ADMIN);
		if (b == B_FALSE) {
			z_free_zone_list(zlst);
			progerr(ERR_CANNOT_LOCK_ZONES);
			/* set fatal error return condition */
			ckreturn(1);
			return (B_FALSE);
		}

		/* add packages to all zones */

		b = remove_packages_in_global_with_zones(a_pkgList, a_nodelete,
			a_longestPkg, a_repeat, a_altBinDir, a_pkgdir, zlst);

		/* unlock all zones */

		(void) z_unlock_zones(zlst, ZLOCKS_ALL);
		quitSetZonelist((zoneList_t)NULL);

		/* free list of all non-global zones */

		z_free_zone_list(zlst);

		return (B_FALSE);
	}

	/* in global zone no non-global zones */

	echoDebug(DBG_IN_GZ_NO_LZ);

	b = z_lock_this_zone(ZLOCKS_PKG_ADMIN);
	if (b != B_TRUE) {
		progerr(ERR_CANNOT_LOCK_THIS_ZONE);
		/* set fatal error return condition */
		ckreturn(1);
		return (B_FALSE);
	}

	b = remove_packages_in_global_no_zones(a_pkgList, a_nodelete,
			a_longestPkg, a_repeat, a_altBinDir);

	(void) z_unlock_this_zone(ZLOCKS_ALL);

	return (B_FALSE);
}

/*
 */

static boolean_t
check_packages(char **a_pkgList, char *a_packageDir)
{
	int	savenpkgs = npkgs;
	int	i;
	CAF_T	flags = 0;

	/* set flags for applicability check */

	if (z_running_in_global_zone() == B_TRUE) {
		flags |= CAF_IN_GLOBAL_ZONE;
	}

	/*
	 * for each package to remove, verify that the package is installed
	 * and is removable.
	 */

	for (i = 0; (pkginst = a_pkgList[i]) != NULL; i++) {
		/* check package applicability */
		if (check_applicability(a_packageDir, pkginst, get_inst_root(),
			flags) == B_FALSE) {
			progerr(ERR_PKG_NOT_REMOVABLE, pkginst);
			npkgs = savenpkgs;
			return (B_FALSE);
		}
		npkgs--;
	}

	npkgs = savenpkgs;
	return (B_TRUE);
}

/*
 * - is this package removable from this zone?
 * - does the scope of remove conflict with existing installation
 */

static boolean_t
check_applicability(char *a_packageDir, char *a_pkgInst,
	char *a_rootPath, CAF_T a_flags)
{
	FILE		*pkginfoFP;
	boolean_t	all_zones;	/* pkg is "all zones" only */
	char		pkginfoPath[PATH_MAX];
	char		pkgpath[PATH_MAX];
	int		len;

	/* entry assertions */

	assert(a_packageDir != (char *)NULL);
	assert(*a_packageDir != '\0');
	assert(a_pkgInst != (char *)NULL);
	assert(*a_pkgInst != '\0');

	/* normalize root path */

	if (a_rootPath == (char *)NULL) {
		a_rootPath = "";
	}

	/*
	 * determine if this package is currently installed
	 * if not installed return success - operation will fail
	 * when the removal is attempted
	 */

	if (pkginfoIsPkgInstalled((struct pkginfo **)NULL, a_pkgInst) !=
		B_TRUE) {
		return (B_TRUE);
	}

	/*
	 * calculate paths to various objects
	 */

	len = snprintf(pkgpath, sizeof (pkgpath), "%s/%s", a_packageDir,
			a_pkgInst);
	if (len > sizeof (pkgpath)) {
		progerr(ERR_CREATE_PATH_2, a_packageDir, a_pkgInst);
		return (B_FALSE);
	}

	/* if not installed then just return */

	if (isdir(pkgpath) != 0) {
		progerr(ERR_NO_PKGDIR, pkgpath, a_pkgInst, strerror(errno));
		return (B_TRUE);
	}

	len = snprintf(pkginfoPath, sizeof (pkginfoPath),
			"%s/pkginfo", pkgpath);
	if (len > sizeof (pkgpath)) {
		progerr(ERR_CREATE_PATH_2, pkgpath, "pkginfo");
		return (B_FALSE);
	}

	/*
	 * gather information from this packages pkginfo file
	 */

	pkginfoFP = fopen(pkginfoPath, "r");

	if (pkginfoFP == (FILE *)NULL) {
		progerr(ERR_NO_PKG_INFOFILE, a_pkgInst, pkginfoPath,
							strerror(errno));
		return (B_FALSE);
	}

	/* determine "ALLZONES" setting for this package */

	all_zones = pkginfoParamTruth(pkginfoFP, PKG_ALLZONES_VARIABLE,
			"true", B_FALSE);

	/* close pkginfo file */

	(void) fclose(pkginfoFP);

	/* gather information from the global zone only file */

	/*
	 * verify package applicability based on information gathered;
	 * the package IS currently installed....
	 */

	/* pkg ALLZONES=true & not running in global zone */

	if ((all_zones == B_TRUE) && (!(a_flags & CAF_IN_GLOBAL_ZONE))) {
		progerr(ERR_ALLZONES_AND_IN_LZ_PKGRM, a_pkgInst);
		return (B_FALSE);
	}

	return (B_TRUE);
}

/*
 * Name:	shall_we_continue
 * Description: Called from within a loop that is installing packages,
 *		this function examines various global variables and decides
 *		whether or not to ask an appropriate question, and wait for
 *		and appropriate reply.
 * Arguments:	<<global variables>>
 * Returns:	B_TRUE - continue processing with next package
 *		B_FALSE - do not continue processing with next package
 */

static boolean_t
shall_we_continue(char *a_pkgInst, int a_npkgs)
{
	char	ans[MAX_INPUT];
	int	n;

	/* return FALSE if immediate reboot required */

	if (ireboot) {
		ptext(stderr, MSG_SUSPEND_RM, a_pkgInst);
		return (B_FALSE);
	}

	/* return TRUE if not interrupted */

	if (!interrupted) {
		return (B_TRUE);
	}

	/* output appropriate interrupt message */

	echo(a_npkgs == 1 ? MSG_1MORETODO : MSG_MORETODO, a_npkgs);

	/* if running with no interaction (-n) do not ask question */

	if (nointeract) {
		quit(0);
		/* NOTREACHED */
	}

	/* interaction possible: ask question */

	n = ckyorn(ans, NULL, NULL, NULL, ASK_CONTINUE_RM);
	if (n != 0) {
		quit(n);
		/* NOTREACHED */
	}

	if (strchr("yY", *ans) == NULL) {
		quit(0);
		/* NOTREACHED */
	}
	return (B_TRUE);
}

/*
 * Name:	create_zone_adminfile
 * Description: Given a zone temporary directory and optionally an existing
 *		administration file, generate an administration file that
 *		can be used to perform "non-interactive" operations in a
 *		non-global zone.
 * Arguments:	r_zoneAdminFile - pointer to handle that will contain a
 *			string representing the path to the temporary
 *			administration file created - this must be NULL
 *			before the first call to this function - on
 *			subsequent calls if the pointer is NOT null then
 *			the existing string will NOT be overwritten.
 *		a_zoneTempDir - pointer to string representing the path
 *			to the zone temporary directory to create the
 *			temporary administration file in
 *		a_admnfile - pointer to string representing the path to
 *			an existing "user" administration file - the
 *			administration file created will contain the
 *			settings contained in this file, modified as
 *			appropriate to supress any interaction;
 *			If this is == NULL then the administration file
 *			created will not contain any extra settings
 * Returns:	void
 * 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.
 * NOTE:	On any error this function will call 'quit(1)'
 */

static void
create_zone_adminfile(char **r_zoneAdminFile, char *a_zoneTempDir,
	char *a_admnfile)
{
	boolean_t	b;

	/* entry assertions */

	assert(r_zoneAdminFile != (char **)NULL);
	assert(a_zoneTempDir != (char *)NULL);
	assert(*a_zoneTempDir != '\0');

	/* entry debugging info */

	echoDebug(DBG_CREATE_ZONE_ADMINFILE, a_zoneTempDir, PSTR(a_admnfile));

	/* if temporary name already exists, do not overwrite */

	if (*r_zoneAdminFile != (char *)NULL) {
		return;
	}

	/* create temporary name */

	*r_zoneAdminFile = tempnam(a_zoneTempDir, "zadmn");
	b = z_create_zone_admin_file(*r_zoneAdminFile, a_admnfile);
	if (b == B_FALSE) {
		progerr(ERR_CREATE_TMPADMIN, *r_zoneAdminFile,
			strerror(errno));
		quit(1);
		/* NOTREACHED */
	}

	echoDebug(DBG_CREATED_ZONE_ADMINFILE, *r_zoneAdminFile);
}

/*
 * Name:	create_zone_tempdir
 * Description: Given a system temporary directory, create a "zone" specific
 *		temporary directory and return the path to the directory
 *		created.
 * Arguments:	r_zoneTempDir - pointer to handle that will contain a
 *			string representing the path to the temporary
 *			directory created - this must be NULL before the
 *			first call to this function - on subsequent calls
 *			if the pointer is NOT null then the existing string
 *			will NOT be overwritten.
 *		a_zoneTempDir - pointer to string representing the path
 *			to the system temporary directory to create the
 *			temporary zone directory in
 * Returns:	void
 * 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.
 * NOTE:	On any error this function will call 'quit(1)'
 * NOTE:	This function calls "quitSetZoneTmpdir" on success to
 *		register the directory created with quit() so that the
 *		directory will be automatically deleted on exit.
 */

static void
create_zone_tempdir(char **r_zoneTempDir, char *a_tmpdir)
{
	boolean_t	b;

	/* entry assertions */

	assert(r_zoneTempDir != (char **)NULL);
	assert(a_tmpdir != (char *)NULL);
	assert(*a_tmpdir != '\0');

	/* entry debugging info */

	echoDebug(DBG_CREATE_ZONE_TEMPDIR, a_tmpdir);

	/* if temporary directory already exists, do not overwrite */

	if (*r_zoneTempDir != (char *)NULL) {
		return;
	}

	/* create temporary directory */

	b = setup_temporary_directory(r_zoneTempDir, a_tmpdir, "ztemp");
	if (b == B_FALSE) {
		progerr(ERR_ZONETEMPDIR, a_tmpdir, strerror(errno));
		quit(1);
		/* NOTREACHED */
	}

	/* register with quit() to directory is removed on exit */

	quitSetZoneTmpdir(*r_zoneTempDir);

	/* exit debugging info */

	echoDebug(DBG_CREATED_ZONE_TEMPDIR, *r_zoneTempDir);
}