summaryrefslogtreecommitdiff
path: root/usr/src/cmd/init/init.c
blob: e1955733490ffc917830e34d6f1b737b97034d23 (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
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
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
/*
 * 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 2021 OmniOS Community Edition (OmniOSce) Association.
 * Copyright 2020 Oxide Computer Company
 * Copyright (c) 2013 Gary Mills
 *
 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2020 Joyent, Inc.
 */

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

/*
 * University Copyright- Copyright (c) 1982, 1986, 1988
 * The Regents of the University of California
 * All Rights Reserved
 *
 * University Acknowledgment- Portions of this document are derived from
 * software developed by the University of California, Berkeley, and its
 * contributors.
 */

/*
 * init(8) is the general process spawning program.  Its primary job is to
 * start and restart svc.startd for smf(7).  For backwards-compatibility it also
 * spawns and respawns processes according to /etc/inittab and the current
 * run-level.  It reads /etc/default/inittab for general configuration.
 *
 * To change run-levels the system administrator runs init from the command
 * line with a level name.  init signals svc.startd via libscf and directs the
 * zone's init (pid 1 in the global zone) what to do by sending it a signal;
 * these signal numbers are commonly refered to in the code as 'states'.  Valid
 * run-levels are [sS0123456].  Additionally, init can be given directives
 * [qQabc], which indicate actions to be taken pertaining to /etc/inittab.
 *
 * When init processes inittab entries, it finds processes that are to be
 * spawned at various run-levels.  inittab contains the set of the levels for
 * which each inittab entry is valid.
 *
 * State File and Restartability
 *   Premature exit by init(8) is handled as a special case by the kernel:
 *   init(8) will be immediately re-executed, retaining its original PID.  (PID
 *   1 in the global zone.)  To track the processes it has previously spawned,
 *   as well as other mutable state, init(8) regularly updates a state file
 *   such that its subsequent invocations have knowledge of its various
 *   dependent processes and duties.
 *
 * Process Contracts
 *   We start svc.startd(8) in a contract and transfer inherited contracts when
 *   restarting it.  Everything else is started using the legacy contract
 *   template, and the created contracts are abandoned when they become empty.
 *
 * utmpx Entry Handling
 *   Because init(8) no longer governs the startup process, its knowledge of
 *   when utmpx becomes writable is indirect.  However, spawned processes
 *   expect to be constructed with valid utmpx entries.  As a result, attempts
 *   to write normal entries will be retried until successful.
 *
 * Maintenance Mode
 *   In certain failure scenarios, init(8) will enter a maintenance mode, in
 *   which it invokes sulogin(8) to allow the operator an opportunity to
 *   repair the system.  Normally, this operation is performed as a
 *   fork(2)-exec(2)-waitpid(3C) sequence with the parent waiting for repair or
 *   diagnosis to be completed.  In the cases that fork(2) requests themselves
 *   fail, init(8) will directly execute sulogin(8), and allow the kernel to
 *   restart init(8) on exit from the operator session.
 *
 *   One scenario where init(8) enters its maintenance mode is when
 *   svc.startd(8) begins to fail rapidly, defined as when the average time
 *   between recent failures drops below a given threshold.
 */

#include <sys/contract/process.h>
#include <sys/ctfs.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/stropts.h>
#include <sys/systeminfo.h>
#include <sys/time.h>
#include <sys/termios.h>
#include <sys/tty.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/bootbanner.h>

#include <bsm/adt_event.h>
#include <bsm/libbsm.h>
#include <security/pam_appl.h>

#include <assert.h>
#include <ctype.h>
#include <definit.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <libcontract.h>
#include <libcontract_priv.h>
#include <libintl.h>
#include <libscf.h>
#include <libscf_priv.h>
#include <poll.h>
#include <procfs.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <syslog.h>
#include <time.h>
#include <ulimit.h>
#include <unistd.h>
#include <utmpx.h>
#include <wait.h>
#include <zone.h>
#include <ucontext.h>

#undef	sleep

#define	fioctl(p, sptr, cmd)	ioctl(fileno(p), sptr, cmd)
#define	min(a, b)		(((a) < (b)) ? (a) : (b))

#define	TRUE	1
#define	FALSE	0
#define	FAILURE	-1

#define	UT_USER_SZ	32	/* Size of a utmpx ut_user field */
#define	UT_LINE_SZ	32	/* Size of a utmpx ut_line field */

#define	CHECK_SVC	SCF_INSTANCE_FS_MINIMAL

/*
 * SLEEPTIME	The number of seconds "init" sleeps between wakeups if
 *		nothing else requires this "init" wakeup.
 */
#define	SLEEPTIME	(5 * 60)

/*
 * MAXCMDL	The maximum length of a command string in inittab.
 */
#define	MAXCMDL	512

/*
 * EXEC		The length of the prefix string added to all comamnds
 *		found in inittab.
 */
#define	EXEC	(sizeof ("exec ") - 1)

/*
 * TWARN	The amount of time between warning signal, SIGTERM,
 *		and the fatal kill signal, SIGKILL.
 */
#define	TWARN	5

#define	id_eq(x, y)	((x[0] == y[0] && x[1] == y[1] && x[2] == y[2] &&\
			x[3] == y[3]) ? TRUE : FALSE)

/*
 * The kernel's default umask is 022 these days; since some processes inherit
 * their umask from init, init will set it from CMASK in /etc/default/init.
 * init gets the default umask from the kernel, it sets it to 022 whenever
 * it wants to create a file and reverts to CMASK afterwards.
 */

static int cmask;

/*
 * The following definitions, concluding with the 'lvls' array, provide a
 * common mapping between level-name (like 'S'), signal number (state),
 * run-level mask, and specific properties associated with a run-level.
 * This array should be accessed using the routines lvlname_to_state(),
 * lvlname_to_mask(), state_to_mask(), and state_to_flags().
 */

/*
 * Correspondence of signals to init actions.
 */
#define	LVLQ		SIGHUP
#define	LVL0		SIGINT
#define	LVL1		SIGQUIT
#define	LVL2		SIGILL
#define	LVL3		SIGTRAP
#define	LVL4		SIGIOT
#define	LVL5		SIGEMT
#define	LVL6		SIGFPE
#define	SINGLE_USER	SIGBUS
#define	LVLa		SIGSEGV
#define	LVLb		SIGSYS
#define	LVLc		SIGPIPE

/*
 * Bit Mask for each level.  Used to determine legal levels.
 */
#define	MASK0	0x0001
#define	MASK1	0x0002
#define	MASK2	0x0004
#define	MASK3	0x0008
#define	MASK4	0x0010
#define	MASK5	0x0020
#define	MASK6	0x0040
#define	MASKSU	0x0080
#define	MASKa	0x0100
#define	MASKb	0x0200
#define	MASKc	0x0400

#define	MASK_NUMERIC (MASK0 | MASK1 | MASK2 | MASK3 | MASK4 | MASK5 | MASK6)
#define	MASK_abc (MASKa | MASKb | MASKc)

/*
 * Flags to indicate properties of various states.
 */
#define	LSEL_RUNLEVEL	0x0001	/* runlevels you can transition to */

typedef struct lvl {
	int	lvl_state;
	int	lvl_mask;
	char	lvl_name;
	int	lvl_flags;
} lvl_t;

static lvl_t lvls[] = {
	{ LVLQ,		0,	'Q', 0					},
	{ LVLQ,		0,	'q', 0					},
	{ LVL0,		MASK0,	'0', LSEL_RUNLEVEL			},
	{ LVL1,		MASK1,	'1', LSEL_RUNLEVEL			},
	{ LVL2,		MASK2,	'2', LSEL_RUNLEVEL			},
	{ LVL3,		MASK3,	'3', LSEL_RUNLEVEL			},
	{ LVL4,		MASK4,	'4', LSEL_RUNLEVEL			},
	{ LVL5,		MASK5,	'5', LSEL_RUNLEVEL			},
	{ LVL6,		MASK6,	'6', LSEL_RUNLEVEL			},
	{ SINGLE_USER,	MASKSU, 'S', LSEL_RUNLEVEL			},
	{ SINGLE_USER,	MASKSU, 's', LSEL_RUNLEVEL			},
	{ LVLa,		MASKa,	'a', 0					},
	{ LVLb,		MASKb,	'b', 0					},
	{ LVLc,		MASKc,	'c', 0					}
};

#define	LVL_NELEMS (sizeof (lvls) / sizeof (lvl_t))

/*
 * Legal action field values.
 */
#define	OFF		0	/* Kill process if on, else ignore */
#define	RESPAWN		1	/* Continuously restart process when it dies */
#define	ONDEMAND	RESPAWN	/* Respawn for a, b, c type processes */
#define	ONCE		2	/* Start process, do not respawn when dead */
#define	WAIT		3	/* Perform once and wait to complete */
#define	BOOT		4	/* Start at boot time only */
#define	BOOTWAIT	5	/* Start at boot time and wait to complete */
#define	POWERFAIL	6	/* Start on powerfail */
#define	POWERWAIT	7	/* Start and wait for complete on powerfail */
#define	INITDEFAULT	8	/* Default level "init" should start at */
#define	SYSINIT		9	/* Actions performed before init speaks */

#define	M_OFF		0001
#define	M_RESPAWN	0002
#define	M_ONDEMAND	M_RESPAWN
#define	M_ONCE		0004
#define	M_WAIT		0010
#define	M_BOOT		0020
#define	M_BOOTWAIT	0040
#define	M_PF		0100
#define	M_PWAIT		0200
#define	M_INITDEFAULT	0400
#define	M_SYSINIT	01000

/* States for the inittab parser in getcmd(). */
#define	ID	1
#define	LEVELS	2
#define	ACTION	3
#define	COMMAND	4
#define	COMMENT	5

/*
 * inittab entry id constants
 */
#define	INITTAB_ENTRY_ID_SIZE 4
#define	INITTAB_ENTRY_ID_STR_FORMAT "%.4s"	/* if INITTAB_ENTRY_ID_SIZE */
						/* changes, this should */
						/* change accordingly */

/*
 * Init can be in any of three main states, "normal" mode where it is
 * processing entries for the lines file in a normal fashion, "boot" mode,
 * where it is only interested in the boot actions, and "powerfail" mode,
 * where it is only interested in powerfail related actions. The following
 * masks declare the legal actions for each mode.
 */
#define	NORMAL_MODES	(M_OFF | M_RESPAWN | M_ONCE | M_WAIT)
#define	BOOT_MODES	(M_BOOT | M_BOOTWAIT)
#define	PF_MODES	(M_PF | M_PWAIT)

struct PROC_TABLE {
	char	p_id[INITTAB_ENTRY_ID_SIZE];	/* Four letter unique id of */
						/* process */
	pid_t	p_pid;		/* Process id */
	short	p_count;	/* How many respawns of this command in */
				/*   the current series */
	long	p_time;		/* Start time for a series of respawns */
	short	p_flags;
	short	p_exit;		/* Exit status of a process which died */
};

/*
 * Flags for the "p_flags" word of a PROC_TABLE entry:
 *
 *	OCCUPIED	This slot in init's proc table is in use.
 *
 *	LIVING		Process is alive.
 *
 *	NOCLEANUP	efork() is not allowed to cleanup this entry even
 *			if process is dead.
 *
 *	NAMED		This process has a name, i.e. came from inittab.
 *
 *	DEMANDREQUEST	Process started by a "telinit [abc]" command.  Processes
 *			formed this way are respawnable and immune to level
 *			changes as long as their entry exists in inittab.
 *
 *	TOUCHED		Flag used by remv() to determine whether it has looked
 *			at an entry while checking for processes to be killed.
 *
 *	WARNED		Flag used by remv() to mark processes that have been
 *			sent the SIGTERM signal.  If they don't die in 5
 *			seconds, they are sent the SIGKILL signal.
 *
 *	KILLED		Flag used by remv() to mark procs that have been sent
 *			the SIGTERM and SIGKILL signals.
 *
 *	PF_MASK		Bitwise or of legal flags, for sanity checking.
 */
#define	OCCUPIED	01
#define	LIVING		02
#define	NOCLEANUP	04
#define	NAMED		010
#define	DEMANDREQUEST	020
#define	TOUCHED		040
#define	WARNED		0100
#define	KILLED		0200
#define	PF_MASK		0377

/*
 * Respawn limits for processes that are to be respawned:
 *
 *	SPAWN_INTERVAL	The number of seconds over which "init" will try to
 *			respawn a process SPAWN_LIMIT times before it gets mad.
 *
 *	SPAWN_LIMIT	The number of respawns "init" will attempt in
 *			SPAWN_INTERVAL seconds before it generates an
 *			error message and inhibits further tries for
 *			INHIBIT seconds.
 *
 *	INHIBIT		The number of seconds "init" ignores an entry it had
 *			trouble spawning unless a "telinit Q" is received.
 */

#define	SPAWN_INTERVAL	(2*60)
#define	SPAWN_LIMIT	10
#define	INHIBIT		(5*60)

/*
 * The maximum number of decimal digits for an id_t.  (ceil(log10 (max_id)))
 */
#define	ID_MAX_STR_LEN	10

#define	NULLPROC	((struct PROC_TABLE *)(0))
#define	NO_ROOM		((struct PROC_TABLE *)(FAILURE))

struct CMD_LINE {
	char c_id[INITTAB_ENTRY_ID_SIZE];	/* Four letter unique id of */
						/* process to be affected by */
						/* action */
	short c_levels;	/* Mask of legal levels for process */
	short c_action;	/* Mask for type of action required */
	char *c_command; /* Pointer to init command */
};

struct	pidrec {
	int	pd_type;	/* Command type */
	pid_t	pd_pid;		/* pid to add or remove */
};

/*
 * pd_type's
 */
#define	ADDPID	1
#define	REMPID	2

static struct	pidlist {
	pid_t	pl_pid;		/* pid to watch for */
	int	pl_dflag;	/* Flag indicating SIGCLD from this pid */
	short	pl_exit;	/* Exit status of proc */
	struct	pidlist	*pl_next; /* Next in list */
} *Plhead, *Plfree;

/*
 * The following structure contains a set of modes for /dev/syscon
 * and should match the default contents of /etc/ioctl.syscon.
 */
static struct termios	dflt_termios = {
	.c_iflag = BRKINT|ICRNL|IXON|IMAXBEL,
	.c_oflag = OPOST|ONLCR|TAB3,
	.c_cflag = CS8|CREAD|B9600,
	.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN,
	.c_cc = { CINTR, CQUIT, CERASE, CKILL, CEOF, 0, 0, 0,
	    CSTART, CSTOP, CSWTCH, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT,
	    CSTATUS, CERASE2, 0
	}
};

static struct termios	stored_syscon_termios;
static int		write_ioctl = 0;	/* Rewrite /etc/ioctl.syscon */

static union WAKEUP {
	struct WAKEFLAGS {
		unsigned w_usersignal : 1;	/* User sent signal to "init" */
		unsigned w_childdeath : 1;	/* An "init" child died */
		unsigned w_powerhit : 1;	/* OS experienced powerfail */
	}	w_flags;
	int w_mask;
} wakeup;


struct init_state {
	int			ist_runlevel;
	int			ist_num_proc;
	int			ist_utmpx_ok;
	struct PROC_TABLE	ist_proc_table[1];
};

#define	cur_state	(g_state->ist_runlevel)
#define	num_proc	(g_state->ist_num_proc)
#define	proc_table	(g_state->ist_proc_table)
#define	utmpx_ok	(g_state->ist_utmpx_ok)

/* Contract cookies. */
#define	ORDINARY_COOKIE		0
#define	STARTD_COOKIE		1


#ifndef NDEBUG
#define	bad_error(func, err)	{					\
	(void) fprintf(stderr, "%s:%d: %s() failed with unexpected "	\
	    "error %d.  Aborting.\n", __FILE__, __LINE__, (func), (err)); \
	abort();							\
}
#else
#define	bad_error(func, err)	abort()
#endif


/*
 * Useful file and device names.
 */
static char *CONSOLE	  = "/dev/console";	/* Real system console */
static char *INITPIPE_DIR = "/var/run";
static char *INITPIPE	  = "/var/run/initpipe";

#define	INIT_STATE_DIR "/etc/svc/volatile"
static const char * const init_state_file = INIT_STATE_DIR "/init.state";
static const char * const init_next_state_file =
	INIT_STATE_DIR "/init-next.state";

static const int init_num_proc = 20;	/* Initial size of process table. */

static char *UTMPX	 = UTMPX_FILE;		/* Snapshot record file */
static char *WTMPX	 = WTMPX_FILE;		/* Long term record file */
static char *INITTAB	 = "/etc/inittab";	/* Script file for "init" */
static char *SYSTTY	 = "/dev/systty";	/* System Console */
static char *SYSCON	 = "/dev/syscon";	/* Virtual System console */
static char *IOCTLSYSCON = "/etc/ioctl.syscon";	/* Last syscon modes */
static char *ENVFILE	 = DEFINIT_DEFAULT_FILE; /* Default env. */
static char *SU	= "/etc/sulogin";	/* Super-user program for single user */
static char *SH	= "/sbin/sh";		/* Standard shell */

/*
 * Default Path.  /sbin is included in path only during sysinit phase
 */
#define	DEF_PATH	"PATH=/usr/sbin:/usr/bin"
#define	INIT_PATH	"PATH=/sbin:/usr/sbin:/usr/bin"

static int	prior_state;
static int	prev_state;	/* State "init" was in last time it woke */
static int	new_state;	/* State user wants "init" to go to. */
static int	lvlq_received;	/* Explicit request to examine state */
static int	op_modes = BOOT_MODES; /* Current state of "init" */
static int	Gchild = 0;	/* Flag to indicate "godchild" died, set in */
				/*   childeath() and cleared in cleanaux() */
static int	Pfd = -1;	/* fd to receive pids thru */
static unsigned int	spawncnt, pausecnt;
static int	rsflag;		/* Set if a respawn has taken place */
static volatile int time_up;	/* Flag set to TRUE by the alarm interrupt */
				/* routine each time an alarm interrupt */
				/* takes place. */
static int	sflg = 0;	/* Set if we were booted -s to single user */
static int	rflg = 0;	/* Set if booted -r, reconfigure devices */
static int	bflg = 0;	/* Set if booted -b, don't run rc scripts */
static pid_t	init_pid;	/* PID of "one true" init for current zone */

static struct init_state *g_state = NULL;
static size_t	g_state_sz;
static int	booting = 1;	/* Set while we're booting. */

/*
 * Array for default global environment.
 */
#define	MAXENVENT	24	/* Max number of default env variables + 1 */
				/* init can use three itself, so this leaves */
				/* 20 for the administrator in ENVFILE. */
static char	*glob_envp[MAXENVENT];	/* Array of environment strings */
static int	glob_envn;		/* Number of environment strings */


static struct pollfd	poll_fds[1];
static int		poll_nfds = 0;	/* poll_fds is uninitialized */

/*
 * Contracts constants
 */
#define	SVC_INIT_PREFIX "init:/"
#define	SVC_AUX_SIZE (INITTAB_ENTRY_ID_SIZE + 1)
#define	SVC_FMRI_SIZE (sizeof (SVC_INIT_PREFIX) + INITTAB_ENTRY_ID_SIZE)

static int	legacy_tmpl = -1;	/* fd for legacy contract template */
static int	startd_tmpl = -1;	/* fd for svc.startd's template */
static char	startd_svc_aux[SVC_AUX_SIZE];

static char	startd_cline[256] = "";	/* svc.startd's command line */
static int	do_restart_startd = 1;	/* Whether to restart svc.startd. */
static char	*smf_options = NULL;	/* Options to give to startd. */
static int	smf_debug = 0;		/* Messages for debugging smf(7) */
static time_t	init_boot_time;		/* Substitute for kernel boot time. */

#define	NSTARTD_FAILURE_TIMES	3		/* trigger after 3 failures */
#define	STARTD_FAILURE_RATE_NS	5000000000LL	/* 1 failure/5 seconds */
#define	STARTD_THROTTLE_RETRY	60	/* space failure retry after 60 secs */
#define	ROOT_MIN_FREE		524288	/* 512KB min. space needed in root */

static hrtime_t	startd_failure_time[NSTARTD_FAILURE_TIMES];
static uint_t	startd_failure_index;


static char	*prog_name(char *);
static int	state_to_mask(int);
static int	lvlname_to_mask(char, int *);
static void	lscf_set_runlevel(char);
static int	state_to_flags(int);
static char	state_to_name(int);
static int	lvlname_to_state(char);
static int	getcmd(struct CMD_LINE *, char *);
static int	realcon();
static int	spawn_processes();
static int	get_ioctl_syscon();
static int	account(short, struct PROC_TABLE *, char *);
static void	alarmclk();
static void	childeath(int);
static void	cleanaux();
static void	clearent(pid_t, short);
static void	console(boolean_t, char *, ...);
static void	init_signals(void);
static void	setup_pipe();
static void	killproc(pid_t);
static void	init_env();
static void	boot_init();
static void	powerfail();
static void	remv();
static void	write_ioctl_syscon();
static void	spawn(struct PROC_TABLE *, struct CMD_LINE *);
static void	setimer(int);
static void	siglvl(int, siginfo_t *, ucontext_t *);
static void	sigpoll(int);
static void	enter_maintenance(void);
static void	timer(int);
static void	userinit(int, char **);
static void	notify_pam_dead(struct utmpx *);
static long	waitproc(struct PROC_TABLE *);
static struct PROC_TABLE *efork(int, struct PROC_TABLE *, int);
static struct PROC_TABLE *findpslot(struct CMD_LINE *);
static void	increase_proc_table_size();
static void	st_init();
static void	st_write();
static void	contracts_init();
static void	contract_event(struct pollfd *);
static int	startd_run(const char *, int, ctid_t);
static void	startd_record_failure();
static int	startd_failure_rate_critical();
static char	*audit_boot_msg();
static int	audit_put_record(int, int, char *);
static void	update_boot_archive(int new_state);
static void	init_bootbanner_print(const char *, uint_t);

int
main(int argc, char *argv[])
{
	int	chg_lvl_flag = FALSE, print_banner = FALSE;
	int	may_need_audit = 1;
	int	c;
	char	*msg;

	/* Get a timestamp for use as boot time, if needed. */
	(void) time(&init_boot_time);

	/* Get the default umask */
	cmask = umask(022);
	(void) umask(cmask);

	/* Parse the arguments to init. Check for single user */
	opterr = 0;
	while ((c = getopt(argc, argv, "brsm:")) != EOF) {
		switch (c) {
		case 'b':
			rflg = 0;
			bflg = 1;
			if (!sflg)
				sflg++;
			break;
		case 'r':
			bflg = 0;
			rflg++;
			break;
		case 's':
			if (!bflg)
				sflg++;
			break;
		case 'm':
			smf_options = optarg;
			smf_debug = (strstr(smf_options, "debug") != NULL);
			break;
		}
	}

	/*
	 * Determine if we are the main init, or a user invoked init, whose job
	 * it is to inform init to change levels or perform some other action.
	 */
	if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
	    sizeof (init_pid)) != sizeof (init_pid)) {
		(void) fprintf(stderr, "could not get pid for init\n");
		return (1);
	}

	/*
	 * If this PID is not the same as the "true" init for the zone, then we
	 * must be in 'user' mode.
	 */
	if (getpid() != init_pid) {
		userinit(argc, argv);
	}

	if (getzoneid() != GLOBAL_ZONEID) {
		print_banner = TRUE;
	}

	/*
	 * Initialize state (and set "booting").
	 */
	st_init();

	if (booting && print_banner) {
		/*
		 * We want to print the boot banner as soon as
		 * possible.  In the global zone, the kernel does it,
		 * but we do not have that luxury in non-global zones,
		 * so we will print it here.
		 */
#ifdef	LEGACY_BANNER
		struct utsname un;
		char buf[BUFSIZ];
		const char *bits;
		int r;

		(void) uname(&un);
		if ((r = sysinfo(SI_ADDRESS_WIDTH, buf, sizeof (buf))) > 0 &&
		    r < sizeof (buf)) {
			bits = buf;
		} else {
			bits = "64";
		}

		console(B_FALSE,
		    "\n\n%s Release %s Version %s %s-bit\r\n",
		    un.sysname, un.release, un.version, bits);
		console(B_FALSE, "Copyright 2010-2020 Joyent, Inc.\r\n");
#else
		bootbanner_print(init_bootbanner_print, 0);
#endif
	}

	/*
	 * Get the ioctl settings for /dev/syscon from /etc/ioctl.syscon
	 * so that it can be brought up in the state it was in when the
	 * system went down; or set to defaults if ioctl.syscon isn't
	 * valid.
	 *
	 * This needs to be done even if we're restarting so reset_modes()
	 * will work in case we need to go down to single user mode.
	 */
	write_ioctl = get_ioctl_syscon();

	/*
	 * Set up all signals to be caught or ignored as appropriate.
	 */
	init_signals();

	/* Load glob_envp from ENVFILE. */
	init_env();

	contracts_init();

	if (!booting) {
		/* cur_state should have been read in. */

		op_modes = NORMAL_MODES;

		/* Rewrite the ioctl file if it was bad. */
		if (write_ioctl)
			write_ioctl_syscon();
	} else {
		/*
		 * It's fine to boot up with state as zero, because
		 * startd will later tell us the real state.
		 */
		cur_state = 0;
		op_modes = BOOT_MODES;

		boot_init();
	}

	prev_state = prior_state = cur_state;

	setup_pipe();

	/*
	 * Here is the beginning of the main process loop.
	 */
	for (;;) {
		if (lvlq_received) {
			setup_pipe();
			lvlq_received = B_FALSE;
		}

		/*
		 * Clean up any accounting records for dead "godchildren".
		 */
		if (Gchild)
			cleanaux();

		/*
		 * If in "normal" mode, check all living processes and initiate
		 * kill sequence on those that should not be there anymore.
		 */
		if (op_modes == NORMAL_MODES && cur_state != LVLa &&
		    cur_state != LVLb && cur_state != LVLc)
			remv();

		/*
		 * If a change in run levels is the reason we awoke, now do
		 * the accounting to report the change in the utmp file.
		 * Also report the change on the system console.
		 */
		if (chg_lvl_flag) {
			chg_lvl_flag = FALSE;

			if (state_to_flags(cur_state) & LSEL_RUNLEVEL) {
				char rl = state_to_name(cur_state);

				if (rl != -1)
					lscf_set_runlevel(rl);
			}

			may_need_audit = 1;
		}

		/*
		 * Scan the inittab file and spawn and respawn processes that
		 * should be alive in the current state. If inittab does not
		 * exist default to  single user mode.
		 */
		if (spawn_processes() == FAILURE) {
			prior_state = prev_state;
			cur_state = SINGLE_USER;
		}

		/* If any respawns occurred, take note. */
		if (rsflag) {
			rsflag = 0;
			spawncnt++;
		}

		/*
		 * If a powerfail signal was received during the last
		 * sequence, set mode to powerfail.  When spawn_processes() is
		 * entered the first thing it does is to check "powerhit".  If
		 * it is in PF_MODES then it clears "powerhit" and does
		 * a powerfail sequence.  If it is not in PF_MODES, then it
		 * puts itself in PF_MODES and then clears "powerhit".  Should
		 * "powerhit" get set again while spawn_processes() is working
		 * on a powerfail sequence, the following code  will see that
		 * spawn_processes() tries to execute the powerfail sequence
		 * again.  This guarantees that the powerfail sequence will be
		 * successfully completed before further processing takes
		 * place.
		 */
		if (wakeup.w_flags.w_powerhit) {
			op_modes = PF_MODES;
			/*
			 * Make sure that cur_state != prev_state so that
			 * ONCE and WAIT types work.
			 */
			prev_state = 0;
		} else if (op_modes != NORMAL_MODES) {
			/*
			 * If spawn_processes() was not just called while in
			 * normal mode, we set the mode to normal and it will
			 * be called again to check normal modes.  If we have
			 * just finished a powerfail sequence with prev_state
			 * equal to zero, we set prev_state equal to cur_state
			 * before the next pass through.
			 */
			if (op_modes == PF_MODES)
				prev_state = cur_state;
			op_modes = NORMAL_MODES;
		} else if (cur_state == LVLa || cur_state == LVLb ||
		    cur_state == LVLc) {
			/*
			 * If it was a change of levels that awakened us and the
			 * new level is one of the demand levels then reset
			 * cur_state to the previous state and do another scan
			 * to take care of the usual respawn actions.
			 */
			cur_state = prior_state;
			prior_state = prev_state;
			prev_state = cur_state;
		} else {
			prev_state = cur_state;

			if (wakeup.w_mask == 0) {
				int ret;

				if (may_need_audit && (cur_state == LVL3)) {
					msg = audit_boot_msg();

					may_need_audit = 0;
					(void) audit_put_record(ADT_SUCCESS,
					    ADT_SUCCESS, msg);
					free(msg);
				}

				/*
				 * "init" is finished with all actions for
				 * the current wakeup.
				 */
				ret = poll(poll_fds, poll_nfds,
				    SLEEPTIME * MILLISEC);
				pausecnt++;
				if (ret > 0)
					contract_event(&poll_fds[0]);
				else if (ret < 0 && errno != EINTR)
					console(B_TRUE, "poll() error: %s\n",
					    strerror(errno));
			}

			if (wakeup.w_flags.w_usersignal) {
				/*
				 * Install the new level.  This could be a real
				 * change in levels  or a telinit [Q|a|b|c] or
				 * just a telinit to the same level at which
				 * we are running.
				 */
				if (new_state != cur_state) {
					if (new_state == LVLa ||
					    new_state == LVLb ||
					    new_state == LVLc) {
						prev_state = prior_state;
						prior_state = cur_state;
						cur_state = new_state;
					} else {
						prev_state = cur_state;
						if (cur_state >= 0)
							prior_state = cur_state;
						cur_state = new_state;
						chg_lvl_flag = TRUE;
					}
				}

				new_state = 0;
			}

			if (wakeup.w_flags.w_powerhit)
				op_modes = PF_MODES;

			/*
			 * Clear all wakeup reasons.
			 */
			wakeup.w_mask = 0;
		}
	}

	/*NOTREACHED*/
}

static void
init_bootbanner_print(const char *line, uint_t num)
{
	const char *pfx = (num == 0) ? "\n\n" : "";

	console(B_FALSE, "%s%s\r\n", pfx, line);
}

static void
update_boot_archive(int new_state)
{
	if (new_state != LVL0 && new_state != LVL5 && new_state != LVL6)
		return;

	if (getzoneid() != GLOBAL_ZONEID)
		return;

	(void) system("/sbin/bootadm -ea update_all");
}

/*
 * void enter_maintenance()
 *   A simple invocation of sulogin(8), with no baggage, in the case that we
 *   are unable to activate svc.startd(8).  We fork; the child runs sulogin;
 *   we wait for it to exit.
 */
static void
enter_maintenance()
{
	struct PROC_TABLE	*su_process;

	console(B_FALSE, "Requesting maintenance mode\n"
	    "(See /lib/svc/share/README for additional information.)\n");
	(void) sighold(SIGCLD);
	while ((su_process = efork(M_OFF, NULLPROC, NOCLEANUP)) == NO_ROOM)
		(void) pause();
	(void) sigrelse(SIGCLD);
	if (su_process == NULLPROC) {
		int fd;

		(void) fclose(stdin);
		(void) fclose(stdout);
		(void) fclose(stderr);
		closefrom(0);

		fd = open(SYSCON, O_RDWR | O_NOCTTY);
		if (fd >= 0) {
			(void) dup2(fd, 1);
			(void) dup2(fd, 2);
		} else {
			/*
			 * Need to issue an error message somewhere.
			 */
			syslog(LOG_CRIT, "init[%d]: cannot open %s; %s\n",
			    getpid(), SYSCON, strerror(errno));
		}

		/*
		 * Execute the "su" program.
		 */
		(void) execle(SU, SU, "-", (char *)0, glob_envp);
		console(B_TRUE, "execle of %s failed: %s\n", SU,
		    strerror(errno));
		timer(5);
		exit(1);
	}

	/*
	 * If we are the parent, wait around for the child to die
	 * or for "init" to be signaled to change levels.
	 */
	while (waitproc(su_process) == FAILURE) {
		/*
		 * All other reasons for waking are ignored when in
		 * single-user mode.  The only child we are interested
		 * in is being waited for explicitly by waitproc().
		 */
		wakeup.w_mask = 0;
	}
}

/*
 * remv() scans through "proc_table" and performs cleanup.  If
 * there is a process in the table, which shouldn't be here at
 * the current run level, then remv() kills the process.
 */
static void
remv()
{
	struct PROC_TABLE	*process;
	struct CMD_LINE		cmd;
	char			cmd_string[MAXCMDL];
	int			change_level;

	change_level = (cur_state != prev_state ? TRUE : FALSE);

	/*
	 * Clear the TOUCHED flag on all entries so that when we have
	 * finished scanning inittab, we will be able to tell if we
	 * have any processes for which there is no entry in inittab.
	 */
	for (process = proc_table;
	    (process < proc_table + num_proc); process++) {
		process->p_flags &= ~TOUCHED;
	}

	/*
	 * Scan all inittab entries.
	 */
	while (getcmd(&cmd, &cmd_string[0]) == TRUE) {
		/* Scan for process which goes with this entry in inittab. */
		for (process = proc_table;
		    (process < proc_table + num_proc); process++) {
			if ((process->p_flags & OCCUPIED) == 0 ||
			    !id_eq(process->p_id, cmd.c_id))
				continue;

			/*
			 * This slot contains the process we are looking for.
			 */

			/*
			 * Is the cur_state SINGLE_USER or is this process
			 * marked as "off" or was this proc started by some
			 * mechanism other than LVL{a|b|c} and the current level
			 * does not support this process?
			 */
			if (cur_state == SINGLE_USER ||
			    cmd.c_action == M_OFF ||
			    ((cmd.c_levels & state_to_mask(cur_state)) == 0 &&
			    (process->p_flags & DEMANDREQUEST) == 0)) {
				if (process->p_flags & LIVING) {
					/*
					 * Touch this entry so we know we have
					 * treated it.  Note that procs which
					 * are already dead at this point and
					 * should not be restarted are left
					 * untouched.  This causes their slot to
					 * be freed later after dead accounting
					 * is done.
					 */
					process->p_flags |= TOUCHED;

					if ((process->p_flags & KILLED) == 0) {
						if (change_level) {
							process->p_flags
							    |= WARNED;
							(void) kill(
							    process->p_pid,
							    SIGTERM);
						} else {
							/*
							 * Fork a killing proc
							 * so "init" can
							 * continue without
							 * having to pause for
							 * TWARN seconds.
							 */
							killproc(
							    process->p_pid);
						}
						process->p_flags |= KILLED;
					}
				}
			} else {
				/*
				 * Process can exist at current level.  If it is
				 * still alive or a DEMANDREQUEST we touch it so
				 * it will be left alone.  Otherwise we leave it
				 * untouched so it will be accounted for and
				 * cleaned up later in remv().  Dead
				 * DEMANDREQUESTs will be accounted but not
				 * freed.
				 */
				if (process->p_flags &
				    (LIVING|NOCLEANUP|DEMANDREQUEST))
					process->p_flags |= TOUCHED;
			}

			break;
		}
	}

	st_write();

	/*
	 * If this was a change of levels call, scan through the
	 * process table for processes that were warned to die.  If any
	 * are found that haven't left yet, sleep for TWARN seconds and
	 * then send final terminations to any that haven't died yet.
	 */
	if (change_level) {

		/*
		 * Set the alarm for TWARN seconds on the assumption
		 * that there will be some that need to be waited for.
		 * This won't harm anything except we are guaranteed to
		 * wakeup in TWARN seconds whether we need to or not.
		 */
		setimer(TWARN);

		/*
		 * Scan for processes which should be dying.  We hope they
		 * will die without having to be sent a SIGKILL signal.
		 */
		for (process = proc_table;
		    (process < proc_table + num_proc); process++) {
			/*
			 * If this process should die, hasn't yet, and the
			 * TWARN time hasn't expired yet, wait for process
			 * to die or for timer to expire.
			 */
			while (time_up == FALSE &&
			    (process->p_flags & (WARNED|LIVING|OCCUPIED)) ==
			    (WARNED|LIVING|OCCUPIED))
				(void) pause();

			if (time_up == TRUE)
				break;
		}

		/*
		 * If we reached the end of the table without the timer
		 * expiring, then there are no procs which will have to be
		 * sent the SIGKILL signal.  If the timer has expired, then
		 * it is necessary to scan the table again and send signals
		 * to all processes which aren't going away nicely.
		 */
		if (time_up == TRUE) {
			for (process = proc_table;
			    (process < proc_table + num_proc); process++) {
				if ((process->p_flags &
				    (WARNED|LIVING|OCCUPIED)) ==
				    (WARNED|LIVING|OCCUPIED))
					(void) kill(process->p_pid, SIGKILL);
			}
		}
		setimer(0);
	}

	/*
	 * Rescan the proc_table for two kinds of entry, those marked LIVING,
	 * NAMED, which don't have an entry in inittab (haven't been TOUCHED
	 * by the above scanning), and haven't been sent kill signals, and
	 * those entries marked not LIVING, NAMED.  The former procs are killed.
	 * The latter have DEAD_PROCESS accounting done and the slot cleared.
	 */
	for (process = proc_table;
	    (process < proc_table + num_proc); process++) {
		if ((process->p_flags & (LIVING|NAMED|TOUCHED|KILLED|OCCUPIED))
		    == (LIVING|NAMED|OCCUPIED)) {
			killproc(process->p_pid);
			process->p_flags |= KILLED;
		} else if ((process->p_flags & (LIVING|NAMED|OCCUPIED)) ==
		    (NAMED|OCCUPIED)) {
			(void) account(DEAD_PROCESS, process, NULL);
			/*
			 * If this named proc hasn't been TOUCHED, then free the
			 * space. It has either died of it's own accord, but
			 * isn't respawnable or it was killed because it
			 * shouldn't exist at this level.
			 */
			if ((process->p_flags & TOUCHED) == 0)
				process->p_flags = 0;
		}
	}

	st_write();
}

/*
 * Extract the svc.startd command line and whether to restart it from its
 * inittab entry.
 */
/*ARGSUSED*/
static void
process_startd_line(struct CMD_LINE *cmd, char *cmd_string)
{
	size_t sz;

	/* Save the command line. */
	if (sflg || rflg) {
		/* Also append -r or -s. */
		(void) strlcpy(startd_cline, cmd_string, sizeof (startd_cline));
		(void) strlcat(startd_cline, " -", sizeof (startd_cline));
		if (sflg)
			sz = strlcat(startd_cline, "s", sizeof (startd_cline));
		if (rflg)
			sz = strlcat(startd_cline, "r", sizeof (startd_cline));
	} else {
		sz = strlcpy(startd_cline, cmd_string, sizeof (startd_cline));
	}

	if (sz >= sizeof (startd_cline)) {
		console(B_TRUE,
		    "svc.startd command line too long.  Ignoring.\n");
		startd_cline[0] = '\0';
		return;
	}
}

/*
 * spawn_processes() scans inittab for entries which should be run at this
 * mode.  Processes which should be running but are not, are started.
 */
static int
spawn_processes()
{
	struct PROC_TABLE		*pp;
	struct CMD_LINE			cmd;
	char				cmd_string[MAXCMDL];
	short				lvl_mask;
	int				status;

	/*
	 * First check the "powerhit" flag.  If it is set, make sure the modes
	 * are PF_MODES and clear the "powerhit" flag.  Avoid the possible race
	 * on the "powerhit" flag by disallowing a new powerfail interrupt
	 * between the test of the powerhit flag and the clearing of it.
	 */
	if (wakeup.w_flags.w_powerhit) {
		wakeup.w_flags.w_powerhit = 0;
		op_modes = PF_MODES;
	}
	lvl_mask = state_to_mask(cur_state);

	/*
	 * Scan through all the entries in inittab.
	 */
	while ((status = getcmd(&cmd, &cmd_string[0])) == TRUE) {
		if (id_eq(cmd.c_id, "smf")) {
			process_startd_line(&cmd, cmd_string);
			continue;
		}

retry_for_proc_slot:

		/*
		 * Find out if there is a process slot for this entry already.
		 */
		if ((pp = findpslot(&cmd)) == NULLPROC) {
			/*
			 * we've run out of proc table entries
			 * increase proc_table.
			 */
			increase_proc_table_size();

			/*
			 * Retry now as we have an empty proc slot.
			 * In case increase_proc_table_size() fails,
			 * we will keep retrying.
			 */
			goto retry_for_proc_slot;
		}

		/*
		 * If there is an entry, and it is marked as DEMANDREQUEST,
		 * one of the levels a, b, or c is in its levels mask, and
		 * the action field is ONDEMAND and ONDEMAND is a permissable
		 * mode, and the process is dead, then respawn it.
		 */
		if (((pp->p_flags & (LIVING|DEMANDREQUEST)) == DEMANDREQUEST) &&
		    (cmd.c_levels & MASK_abc) &&
		    (cmd.c_action & op_modes) == M_ONDEMAND) {
			spawn(pp, &cmd);
			continue;
		}

		/*
		 * If the action is not an action we are interested in,
		 * skip the entry.
		 */
		if ((cmd.c_action & op_modes) == 0 || pp->p_flags & LIVING ||
		    (cmd.c_levels & lvl_mask) == 0)
			continue;

		/*
		 * If the modes are the normal modes (ONCE, WAIT, RESPAWN, OFF,
		 * ONDEMAND) and the action field is either OFF or the action
		 * field is ONCE or WAIT and the current level is the same as
		 * the last level, then skip this entry.  ONCE and WAIT only
		 * get run when the level changes.
		 */
		if (op_modes == NORMAL_MODES &&
		    (cmd.c_action == M_OFF ||
		    (cmd.c_action & (M_ONCE|M_WAIT)) &&
		    cur_state == prev_state))
			continue;

		/*
		 * At this point we are interested in performing the action for
		 * this entry.  Actions fall into two categories, spinning off
		 * a process and not waiting, and spinning off a process and
		 * waiting for it to die.  If the action is ONCE, RESPAWN,
		 * ONDEMAND, POWERFAIL, or BOOT we don't wait for the process
		 * to die, for all other actions we do wait.
		 */
		if (cmd.c_action & (M_ONCE | M_RESPAWN | M_PF | M_BOOT)) {
			spawn(pp, &cmd);

		} else {
			spawn(pp, &cmd);
			while (waitproc(pp) == FAILURE)
				;
			(void) account(DEAD_PROCESS, pp, NULL);
			pp->p_flags = 0;
		}
	}
	return (status);
}

/*
 * spawn() spawns a shell, inserts the information about the process
 * process into the proc_table, and does the startup accounting.
 */
static void
spawn(struct PROC_TABLE *process, struct CMD_LINE *cmd)
{
	int		i;
	int		modes, maxfiles;
	time_t		now;
	struct PROC_TABLE tmproc, *oprocess;

	/*
	 * The modes to be sent to efork() are 0 unless we are
	 * spawning a LVLa, LVLb, or LVLc entry or we will be
	 * waiting for the death of the child before continuing.
	 */
	modes = NAMED;
	if (process->p_flags & DEMANDREQUEST || cur_state == LVLa ||
	    cur_state == LVLb || cur_state == LVLc)
		modes |= DEMANDREQUEST;
	if ((cmd->c_action & (M_SYSINIT | M_WAIT | M_BOOTWAIT | M_PWAIT)) != 0)
		modes |= NOCLEANUP;

	/*
	 * If this is a respawnable process, check the threshold
	 * information to avoid excessive respawns.
	 */
	if (cmd->c_action & M_RESPAWN) {
		/*
		 * Add NOCLEANUP to all respawnable commands so that the
		 * information about the frequency of respawns isn't lost.
		 */
		modes |= NOCLEANUP;
		(void) time(&now);

		/*
		 * If no time is assigned, then this is the first time
		 * this command is being processed in this series.  Assign
		 * the current time.
		 */
		if (process->p_time == 0L)
			process->p_time = now;

		if (process->p_count++ == SPAWN_LIMIT) {

			if ((now - process->p_time) < SPAWN_INTERVAL) {
				/*
				 * Process is respawning too rapidly.  Print
				 * message and refuse to respawn it for now.
				 */
				console(B_TRUE, "Command is respawning too "
				    "rapidly. Check for possible errors.\n"
				    "id:%4s \"%s\"\n",
				    &cmd->c_id[0], &cmd->c_command[EXEC]);
				return;
			}
			process->p_time = now;
			process->p_count = 0;

		} else if (process->p_count > SPAWN_LIMIT) {
			/*
			 * If process has been respawning too rapidly and
			 * the inhibit time limit hasn't expired yet, we
			 * refuse to respawn.
			 */
			if (now - process->p_time < SPAWN_INTERVAL + INHIBIT)
				return;
			process->p_time = now;
			process->p_count = 0;
		}
		rsflag = TRUE;
	}

	/*
	 * Spawn a child process to execute this command.
	 */
	(void) sighold(SIGCLD);
	oprocess = process;
	while ((process = efork(cmd->c_action, oprocess, modes)) == NO_ROOM)
		(void) pause();

	if (process == NULLPROC) {

		/*
		 * We are the child.  We must make sure we get a different
		 * file pointer for our references to utmpx.  Otherwise our
		 * seeks and reads will compete with those of the parent.
		 */
		endutxent();

		/*
		 * Perform the accounting for the beginning of a process.
		 * Note that all processes are initially "INIT_PROCESS"es.
		 */
		tmproc.p_id[0] = cmd->c_id[0];
		tmproc.p_id[1] = cmd->c_id[1];
		tmproc.p_id[2] = cmd->c_id[2];
		tmproc.p_id[3] = cmd->c_id[3];
		tmproc.p_pid = getpid();
		tmproc.p_exit = 0;
		(void) account(INIT_PROCESS, &tmproc,
		    prog_name(&cmd->c_command[EXEC]));
		maxfiles = ulimit(UL_GDESLIM, 0);
		for (i = 0; i < maxfiles; i++)
			(void) fcntl(i, F_SETFD, FD_CLOEXEC);

		/*
		 * Now exec a shell with the -c option and the command
		 * from inittab.
		 */
		(void) execle(SH, "INITSH", "-c", cmd->c_command, (char *)0,
		    glob_envp);
		console(B_TRUE, "Command\n\"%s\"\n failed to execute.  errno "
		    "= %d (exec of shell failed)\n", cmd->c_command, errno);

		/*
		 * Don't come back so quickly that "init" doesn't have a
		 * chance to finish putting this child in "proc_table".
		 */
		timer(20);
		exit(1);

	}

	/*
	 * We are the parent.  Insert the necessary
	 * information in the proc_table.
	 */
	process->p_id[0] = cmd->c_id[0];
	process->p_id[1] = cmd->c_id[1];
	process->p_id[2] = cmd->c_id[2];
	process->p_id[3] = cmd->c_id[3];

	st_write();

	(void) sigrelse(SIGCLD);
}

/*
 * findpslot() finds the old slot in the process table for the
 * command with the same id, or it finds an empty slot.
 */
static struct PROC_TABLE *
findpslot(struct CMD_LINE *cmd)
{
	struct PROC_TABLE	*process;
	struct PROC_TABLE	*empty = NULLPROC;

	for (process = proc_table;
	    (process < proc_table + num_proc); process++) {
		if (process->p_flags & OCCUPIED &&
		    id_eq(process->p_id, cmd->c_id))
			break;

		/*
		 * If the entry is totally empty and "empty" is still 0,
		 * remember where this hole is and make sure the slot is
		 * zeroed out.
		 */
		if (empty == NULLPROC && (process->p_flags & OCCUPIED) == 0) {
			empty = process;
			process->p_id[0] = '\0';
			process->p_id[1] = '\0';
			process->p_id[2] = '\0';
			process->p_id[3] = '\0';
			process->p_pid = 0;
			process->p_time = 0L;
			process->p_count = 0;
			process->p_flags = 0;
			process->p_exit = 0;
		}
	}

	/*
	 * If there is no entry for this slot, then there should be an
	 * empty slot.  If there is no empty slot, then we've run out
	 * of proc_table space.  If the latter is true, empty will be
	 * NULL and the caller will have to complain.
	 */
	if (process == (proc_table + num_proc))
		process = empty;

	return (process);
}

/*
 * getcmd() parses lines from inittab.  Each time it finds a command line
 * it will return TRUE as well as fill the passed CMD_LINE structure and
 * the shell command string.  When the end of inittab is reached, FALSE
 * is returned inittab is automatically opened if it is not currently open
 * and is closed when the end of the file is reached.
 */
static FILE *fp_inittab = NULL;

static int
getcmd(struct CMD_LINE *cmd, char *shcmd)
{
	char	*ptr;
	int	c, lastc, state;
	char	*ptr1;
	int	answer, i, proceed;
	struct	stat	sbuf;
	static char *actions[] = {
		"off", "respawn", "ondemand", "once", "wait", "boot",
		"bootwait", "powerfail", "powerwait", "initdefault",
		"sysinit",
	};
	static short act_masks[] = {
		M_OFF, M_RESPAWN, M_ONDEMAND, M_ONCE, M_WAIT, M_BOOT,
		M_BOOTWAIT, M_PF, M_PWAIT, M_INITDEFAULT, M_SYSINIT,
	};
	/*
	 * Only these actions will be allowed for entries which
	 * are specified for single-user mode.
	 */
	short su_acts = M_INITDEFAULT | M_PF | M_PWAIT | M_WAIT;

	if (fp_inittab == NULL) {
		/*
		 * Before attempting to open inittab we stat it to make
		 * sure it currently exists and is not empty.  We try
		 * several times because someone may have temporarily
		 * unlinked or truncated the file.
		 */
		for (i = 0; i < 3; i++) {
			if (stat(INITTAB, &sbuf) == -1) {
				if (i == 2) {
					console(B_TRUE,
					    "Cannot stat %s, errno: %d\n",
					    INITTAB, errno);
					return (FAILURE);
				} else {
					timer(3);
				}
			} else if (sbuf.st_size < 10) {
				if (i == 2) {
					console(B_TRUE,
					    "%s truncated or corrupted\n",
					    INITTAB);
					return (FAILURE);
				} else {
					timer(3);
				}
			} else {
				break;
			}
		}

		/*
		 * If unable to open inittab, print error message and
		 * return FAILURE to caller.
		 */
		if ((fp_inittab = fopen(INITTAB, "r")) == NULL) {
			console(B_TRUE, "Cannot open %s errno: %d\n", INITTAB,
			    errno);
			return (FAILURE);
		}
	}

	/*
	 * Keep getting commands from inittab until you find a
	 * good one or run out of file.
	 */
	for (answer = FALSE; answer == FALSE; ) {
		/*
		 * Zero out the cmd itself before trying next line.
		 */
		bzero(cmd, sizeof (struct CMD_LINE));

		/*
		 * Read in lines of inittab, parsing at colons, until a line is
		 * read in which doesn't end with a backslash.  Do not start if
		 * the first character read is an EOF.  Note that this means
		 * that lines which don't end in a newline are still processed,
		 * since the "for" will terminate normally once started,
		 * regardless of whether line terminates with a newline or EOF.
		 */
		state = FAILURE;
		if ((c = fgetc(fp_inittab)) == EOF) {
			answer = FALSE;
			(void) fclose(fp_inittab);
			fp_inittab = NULL;
			break;
		}

		for (proceed = TRUE, ptr = shcmd, state = ID, lastc = '\0';
		    proceed && c != EOF;
		    lastc = c, c = fgetc(fp_inittab)) {
			/* If we're not in the FAILURE state and haven't */
			/* yet reached the shell command field, process	 */
			/* the line, otherwise just look for a real end	 */
			/* of line.					 */
			if (state != FAILURE && state != COMMAND) {
			/*
			 * Squeeze out spaces and tabs.
			 */
			if (c == ' ' || c == '\t')
				continue;

			/*
			 * Ignore characters in a comment, except for the \n.
			 */
			if (state == COMMENT) {
				if (c == '\n') {
					lastc = ' ';
					break;
				} else {
					continue;
				}
			}

			/*
			 * Detect comments (lines whose first non-whitespace
			 * character is '#') by checking that we're at the
			 * beginning of a line, have seen a '#', and haven't
			 * yet accumulated any characters.
			 */
			if (state == ID && c == '#' && ptr == shcmd) {
				state = COMMENT;
				continue;
			}

			/*
			 * If the character is a ':', then check the
			 * previous field for correctness and advance
			 * to the next field.
			 */
			if (c == ':') {
				switch (state) {

				case ID :
				/*
				 * Check to see that there are only
				 * 1 to 4 characters for the id.
				 */
				if ((i = ptr - shcmd) < 1 || i > 4) {
					state = FAILURE;
				} else {
					bcopy(shcmd, &cmd->c_id[0], i);
					ptr = shcmd;
					state = LEVELS;
				}
				break;

				case LEVELS :
				/*
				 * Build a mask for all the levels for
				 * which this command will be legal.
				 */
				for (cmd->c_levels = 0, ptr1 = shcmd;
				    ptr1 < ptr; ptr1++) {
					int mask;
					if (lvlname_to_mask(*ptr1,
					    &mask) == -1) {
						state = FAILURE;
						break;
					}
					cmd->c_levels |= mask;
				}
				if (state != FAILURE) {
					state = ACTION;
					ptr = shcmd;	/* Reset the buffer */
				}
				break;

				case ACTION :
				/*
				 * Null terminate the string in shcmd buffer and
				 * then try to match against legal actions.  If
				 * the field is of length 0, then the default of
				 * "RESPAWN" is used if the id is numeric,
				 * otherwise the default is "OFF".
				 */
				if (ptr == shcmd) {
					if (isdigit(cmd->c_id[0]) &&
					    (cmd->c_id[1] == '\0' ||
					    isdigit(cmd->c_id[1])) &&
					    (cmd->c_id[2] == '\0' ||
					    isdigit(cmd->c_id[2])) &&
					    (cmd->c_id[3] == '\0' ||
					    isdigit(cmd->c_id[3])))
						cmd->c_action = M_RESPAWN;
					else
						cmd->c_action = M_OFF;
				} else {
					for (cmd->c_action = 0, i = 0,
					    *ptr = '\0';
					    i <
					    sizeof (actions)/sizeof (char *);
					    i++) {
					if (strcmp(shcmd, actions[i]) == 0) {
						if ((cmd->c_levels & MASKSU) &&
						    !(act_masks[i] & su_acts))
							cmd->c_action = 0;
						else
							cmd->c_action =
							    act_masks[i];
						break;
					}
					}
				}

				/*
				 * If the action didn't match any legal action,
				 * set state to FAILURE.
				 */
				if (cmd->c_action == 0) {
					state = FAILURE;
				} else {
					state = COMMAND;
					(void) strcpy(shcmd, "exec ");
				}
				ptr = shcmd + EXEC;
				break;
				}
				continue;
			}
		}

		/* If the character is a '\n', then this is the end of a */
		/* line.  If the '\n' wasn't preceded by a backslash, */
		/* it is also the end of an inittab command.  If it was */
		/* preceded by a backslash then the next line is a */
		/* continuation.  Note that the continuation '\n' falls */
		/* through and is treated like other characters and is */
		/* stored in the shell command line. */
		if (c == '\n' && lastc != '\\') {
			proceed = FALSE;
			*ptr = '\0';
			break;
		}

		/* For all other characters just stuff them into the */
		/* command as long as there aren't too many of them. */
		/* Make sure there is room for a terminating '\0' also. */
		if (ptr >= shcmd + MAXCMDL - 1)
			state = FAILURE;
		else
			*ptr++ = (char)c;

		/* If the character we just stored was a quoted	*/
		/* backslash, then change "c" to '\0', so that this	*/
		/* backslash will not cause a subsequent '\n' to appear */
		/* quoted.  In otherwords '\' '\' '\n' is the real end */
		/* of a command, while '\' '\n' is a continuation. */
		if (c == '\\' && lastc == '\\')
			c = '\0';
		}

		/*
		 * Make sure all the fields are properly specified
		 * for a good command line.
		 */
		if (state == COMMAND) {
			answer = TRUE;
			cmd->c_command = shcmd;

			/*
			 * If no default level was supplied, insert
			 * all numerical levels.
			 */
			if (cmd->c_levels == 0)
				cmd->c_levels = MASK_NUMERIC;

			/*
			 * If no action has been supplied, declare this
			 * entry to be OFF.
			 */
			if (cmd->c_action == 0)
				cmd->c_action = M_OFF;

			/*
			 * If no shell command has been supplied, make sure
			 * there is a null string in the command field.
			 */
			if (ptr == shcmd + EXEC)
				*shcmd = '\0';
		} else
			answer = FALSE;

		/*
		 * If we have reached the end of inittab, then close it
		 * and quit trying to find a good command line.
		 */
		if (c == EOF) {
			(void) fclose(fp_inittab);
			fp_inittab = NULL;
			break;
		}
	}
	return (answer);
}

/*
 * lvlname_to_state(): convert the character name of a state to its level
 * (its corresponding signal number).
 */
static int
lvlname_to_state(char name)
{
	int i;
	for (i = 0; i < LVL_NELEMS; i++) {
		if (lvls[i].lvl_name == name)
			return (lvls[i].lvl_state);
	}
	return (-1);
}

/*
 * state_to_name(): convert the level to the character name.
 */
static char
state_to_name(int state)
{
	int i;
	for (i = 0; i < LVL_NELEMS; i++) {
		if (lvls[i].lvl_state == state)
			return (lvls[i].lvl_name);
	}
	return (-1);
}

/*
 * state_to_mask(): return the mask corresponding to a signal number
 */
static int
state_to_mask(int state)
{
	int i;
	for (i = 0; i < LVL_NELEMS; i++) {
		if (lvls[i].lvl_state == state)
			return (lvls[i].lvl_mask);
	}
	return (0);	/* return 0, since that represents an empty mask */
}

/*
 * lvlname_to_mask(): return the mask corresponding to a levels character name
 */
static int
lvlname_to_mask(char name, int *mask)
{
	int i;
	for (i = 0; i < LVL_NELEMS; i++) {
		if (lvls[i].lvl_name == name) {
			*mask = lvls[i].lvl_mask;
			return (0);
		}
	}
	return (-1);
}

/*
 * state_to_flags(): return the flags corresponding to a runlevel.  These
 * indicate properties of that runlevel.
 */
static int
state_to_flags(int state)
{
	int i;
	for (i = 0; i < LVL_NELEMS; i++) {
		if (lvls[i].lvl_state == state)
			return (lvls[i].lvl_flags);
	}
	return (0);
}

/*
 * killproc() creates a child which kills the process specified by pid.
 */
void
killproc(pid_t pid)
{
	struct PROC_TABLE	*process;

	(void) sighold(SIGCLD);
	while ((process = efork(M_OFF, NULLPROC, 0)) == NO_ROOM)
		(void) pause();
	(void) sigrelse(SIGCLD);

	if (process == NULLPROC) {
		/*
		 * efork() sets all signal handlers to the default, so reset
		 * the ALRM handler to make timer() work as expected.
		 */
		(void) sigset(SIGALRM, alarmclk);

		/*
		 * We are the child.  Try to terminate the process nicely
		 * first using SIGTERM and if it refuses to die in TWARN
		 * seconds kill it with SIGKILL.
		 */
		(void) kill(pid, SIGTERM);
		(void) timer(TWARN);
		(void) kill(pid, SIGKILL);
		(void) exit(0);
	}
}

/*
 * Set up the default environment for all procs to be forked from init.
 * Read the values from the /etc/default/init file, except for PATH.  If
 * there is not enough room in the environment array, the environment
 * lines that don't fit are discarded and a message is written to the console.
 */
void
init_env()
{
	void		*dstate;
	const char	*tokp;

	glob_envp[0] = malloc((unsigned)(strlen(DEF_PATH)+2));
	(void) strcpy(glob_envp[0], DEF_PATH);
	glob_envn = 1;

	if (rflg) {
		glob_envp[1] =
		    malloc((unsigned)(strlen("_DVFS_RECONFIG=YES")+2));
		(void) strcpy(glob_envp[1], "_DVFS_RECONFIG=YES");
		++glob_envn;
	} else if (bflg == 1) {
		glob_envp[1] =
		    malloc((unsigned)(strlen("RB_NOBOOTRC=YES")+2));
		(void) strcpy(glob_envp[1], "RB_NOBOOTRC=YES");
		++glob_envn;
	}

	if (definit_open(ENVFILE, &dstate) != 0) {
		console(B_TRUE,
		    "Cannot open %s. Environment not initialized.\n",
		    ENVFILE);
		return;
	}

	while ((tokp = definit_token(dstate)) != NULL &&
	    glob_envn < MAXENVENT - 2) {

		if (strncmp(tokp, "CMASK=", sizeof ("CMASK=") - 1) == 0) {
			long t;

			/* We know there's an = */
			t = strtol(strchr(tokp, '=') + 1, NULL, 8);

			/* Sanity */
			if (t >= DEFINIT_MIN_UMASK && t <= DEFINIT_MAX_UMASK)
				cmask = (int)t;
			(void) umask(cmask);
			continue;
		}
		glob_envp[glob_envn] = strdup(tokp);
		if (glob_envp[glob_envn] == NULL) {
			console(B_TRUE, "Out of memory building environment, "
			    "truncated.\n");
			break;
		}
		if (++glob_envn >= MAXENVENT - 1) {
			console(B_TRUE, "Too many variables in %s; "
			    "environment not fully initialized.\n", ENVFILE);
			break;
		}
	}

	/*
	 * Append a null pointer to the environment array to mark its end.
	 */
	glob_envp[glob_envn] = NULL;

	definit_close(dstate);
}

/*
 * boot_init(): Do initialization things that should be done at boot.
 */
void
boot_init()
{
	int i;
	struct PROC_TABLE *process, *oprocess;
	struct CMD_LINE	cmd;
	char	line[MAXCMDL];
	char	svc_aux[SVC_AUX_SIZE];
	char	init_svc_fmri[SVC_FMRI_SIZE];
	char *old_path;
	int maxfiles;

	/* Use INIT_PATH for sysinit cmds */
	old_path = glob_envp[0];
	glob_envp[0] = malloc((unsigned)(strlen(INIT_PATH)+2));
	(void) strcpy(glob_envp[0], INIT_PATH);

	/*
	 * Scan inittab(5) and process the special svc.startd entry, initdefault
	 * and sysinit entries.
	 */
	while (getcmd(&cmd, &line[0]) == TRUE) {
		if (startd_tmpl >= 0 && id_eq(cmd.c_id, "smf")) {
			process_startd_line(&cmd, line);
			(void) snprintf(startd_svc_aux, SVC_AUX_SIZE,
			    INITTAB_ENTRY_ID_STR_FORMAT, cmd.c_id);
		} else if (cmd.c_action == M_INITDEFAULT) {
			/*
			 * initdefault is no longer meaningful, as the SMF
			 * milestone controls what (legacy) run level we
			 * boot to.
			 */
			console(B_TRUE,
			    "Ignoring legacy \"initdefault\" entry.\n");
		} else if (cmd.c_action == M_SYSINIT) {
			/*
			 * Execute the "sysinit" entry and wait for it to
			 * complete.  No bookkeeping is performed on these
			 * entries because we avoid writing to the file system
			 * until after there has been an chance to check it.
			 */
			if (process = findpslot(&cmd)) {
				(void) sighold(SIGCLD);
				(void) snprintf(svc_aux, SVC_AUX_SIZE,
				    INITTAB_ENTRY_ID_STR_FORMAT, cmd.c_id);
				(void) snprintf(init_svc_fmri, SVC_FMRI_SIZE,
				    SVC_INIT_PREFIX INITTAB_ENTRY_ID_STR_FORMAT,
				    cmd.c_id);
				if (legacy_tmpl >= 0) {
					(void) ct_pr_tmpl_set_svc_fmri(
					    legacy_tmpl, init_svc_fmri);
					(void) ct_pr_tmpl_set_svc_aux(
					    legacy_tmpl, svc_aux);
				}

				for (oprocess = process;
				    (process = efork(M_OFF, oprocess,
				    (NAMED|NOCLEANUP))) == NO_ROOM;
				    /* CSTYLED */)
					;
				(void) sigrelse(SIGCLD);

				if (process == NULLPROC) {
					maxfiles = ulimit(UL_GDESLIM, 0);

					for (i = 0; i < maxfiles; i++)
						(void) fcntl(i, F_SETFD,
						    FD_CLOEXEC);
					(void) execle(SH, "INITSH", "-c",
					    cmd.c_command,
					    (char *)0, glob_envp);
					console(B_TRUE,
"Command\n\"%s\"\n failed to execute.  errno = %d (exec of shell failed)\n",
					    cmd.c_command, errno);
					exit(1);
				} else
					while (waitproc(process) == FAILURE)
						;
				process->p_flags = 0;
				st_write();
			}
		}
	}

	/* Restore the path. */
	free(glob_envp[0]);
	glob_envp[0] = old_path;

	/*
	 * This will enable st_write() to complain about init_state_file.
	 */
	booting = 0;

	/*
	 * If the /etc/ioctl.syscon didn't exist or had invalid contents write
	 * out a correct version.
	 */
	if (write_ioctl)
		write_ioctl_syscon();

	/*
	 * Start svc.startd(8), which does most of the work.
	 */
	if (startd_cline[0] != '\0' && startd_tmpl >= 0) {
		/* Start svc.startd. */
		if (startd_run(startd_cline, startd_tmpl, 0) == -1)
			cur_state = SINGLE_USER;
	} else {
		console(B_TRUE, "Absent svc.startd entry or bad "
		    "contract template.  Not starting svc.startd.\n");
		enter_maintenance();
	}
}

/*
 * init_signals(): Initialize all signals to either be caught or ignored.
 */
void
init_signals(void)
{
	struct sigaction act;
	int i;

	/*
	 * Start by ignoring all signals, then selectively re-enable some.
	 * The SIG_IGN disposition will only affect asynchronous signals:
	 * any signal that we trigger synchronously that doesn't end up
	 * being handled by siglvl() will be forcibly delivered by the kernel.
	 */
	for (i = SIGHUP; i <= SIGRTMAX; i++)
		(void) sigset(i, SIG_IGN);

	/*
	 * Handle all level-changing signals using siglvl() and set sa_mask so
	 * that all level-changing signals are blocked while in siglvl().
	 */
	act.sa_handler = siglvl;
	act.sa_flags = SA_SIGINFO;
	(void) sigemptyset(&act.sa_mask);

	(void) sigaddset(&act.sa_mask, LVLQ);
	(void) sigaddset(&act.sa_mask, LVL0);
	(void) sigaddset(&act.sa_mask, LVL1);
	(void) sigaddset(&act.sa_mask, LVL2);
	(void) sigaddset(&act.sa_mask, LVL3);
	(void) sigaddset(&act.sa_mask, LVL4);
	(void) sigaddset(&act.sa_mask, LVL5);
	(void) sigaddset(&act.sa_mask, LVL6);
	(void) sigaddset(&act.sa_mask, SINGLE_USER);
	(void) sigaddset(&act.sa_mask, LVLa);
	(void) sigaddset(&act.sa_mask, LVLb);
	(void) sigaddset(&act.sa_mask, LVLc);

	(void) sigaction(LVLQ, &act, NULL);
	(void) sigaction(LVL0, &act, NULL);
	(void) sigaction(LVL1, &act, NULL);
	(void) sigaction(LVL2, &act, NULL);
	(void) sigaction(LVL3, &act, NULL);
	(void) sigaction(LVL4, &act, NULL);
	(void) sigaction(LVL5, &act, NULL);
	(void) sigaction(LVL6, &act, NULL);
	(void) sigaction(SINGLE_USER, &act, NULL);
	(void) sigaction(LVLa, &act, NULL);
	(void) sigaction(LVLb, &act, NULL);
	(void) sigaction(LVLc, &act, NULL);

	(void) sigset(SIGALRM, alarmclk);
	alarmclk();

	(void) sigset(SIGCLD, childeath);
	(void) sigset(SIGPWR, powerfail);
}

/*
 * Set up pipe for "godchildren". If the file exists and is a pipe just open
 * it. Else, if the file system is r/w create it.  Otherwise, defer its
 * creation and open until after /var/run has been mounted.  This function is
 * only called on startup and when explicitly requested via LVLQ.
 */
void
setup_pipe()
{
	struct stat stat_buf;
	struct statvfs statvfs_buf;
	struct sigaction act;

	/*
	 * Always close the previous pipe descriptor as the mounted filesystems
	 * may have changed.
	 */
	if (Pfd >= 0)
		(void) close(Pfd);

	if ((stat(INITPIPE, &stat_buf) == 0) &&
	    ((stat_buf.st_mode & (S_IFMT|S_IRUSR)) == (S_IFIFO|S_IRUSR)))
		Pfd = open(INITPIPE, O_RDWR | O_NDELAY);
	else
		if ((statvfs(INITPIPE_DIR, &statvfs_buf) == 0) &&
		    ((statvfs_buf.f_flag & ST_RDONLY) == 0)) {
			(void) unlink(INITPIPE);
			(void) mknod(INITPIPE, S_IFIFO | 0600, 0);
			Pfd = open(INITPIPE, O_RDWR | O_NDELAY);
		}

	if (Pfd >= 0) {
		(void) ioctl(Pfd, I_SETSIG, S_INPUT);
		/*
		 * Read pipe in message discard mode.
		 */
		(void) ioctl(Pfd, I_SRDOPT, RMSGD);

		act.sa_handler = sigpoll;
		act.sa_flags = 0;
		(void) sigemptyset(&act.sa_mask);
		(void) sigaddset(&act.sa_mask, SIGCLD);
		(void) sigaction(SIGPOLL, &act, NULL);
	}
}

/*
 * siglvl - handle an asynchronous signal from init(8) telling us that we
 * should change the current run level.  We set new_state accordingly.
 */
void
siglvl(int sig, siginfo_t *sip, ucontext_t *ucp)
{
	struct PROC_TABLE *process;
	struct sigaction act;

	/*
	 * If the signal was from the kernel (rather than init(8)) then init
	 * itself tripped the signal.  That is, we might have a bug and tripped
	 * a real SIGSEGV instead of receiving it as an alias for SIGLVLa.  In
	 * such a case we reset the disposition to SIG_DFL, block all signals
	 * in uc_mask but the current one, and return to the interrupted ucp
	 * to effect an appropriate death.  The kernel will then restart us.
	 *
	 * The one exception to SI_FROMKERNEL() is SIGFPE (a.k.a. LVL6), which
	 * the kernel can send us when it wants to effect an orderly reboot.
	 * For this case we must also verify si_code is zero, rather than a
	 * code such as FPE_INTDIV which a bug might have triggered.
	 */
	if (sip != NULL && SI_FROMKERNEL(sip) &&
	    (sig != SIGFPE || sip->si_code == 0)) {

		(void) sigemptyset(&act.sa_mask);
		act.sa_handler = SIG_DFL;
		act.sa_flags = 0;
		(void) sigaction(sig, &act, NULL);

		(void) sigfillset(&ucp->uc_sigmask);
		(void) sigdelset(&ucp->uc_sigmask, sig);
		ucp->uc_flags |= UC_SIGMASK;

		(void) setcontext(ucp);
	}

	/*
	 * If the signal received is a LVLQ signal, do not really
	 * change levels, just restate the current level.  If the
	 * signal is not a LVLQ, set the new level to the signal
	 * received.
	 */
	if (sig == LVLQ) {
		new_state = cur_state;
		lvlq_received = B_TRUE;
	} else {
		new_state = sig;
	}

	/*
	 * Clear all times and repeat counts in the process table
	 * since either the level is changing or the user has editted
	 * the inittab file and wants us to look at it again.
	 * If the user has fixed a typo, we don't want residual timing
	 * data preventing the fixed command line from executing.
	 */
	for (process = proc_table;
	    (process < proc_table + num_proc); process++) {
		process->p_time = 0L;
		process->p_count = 0;
	}

	/*
	 * Set the flag to indicate that a "user signal" was received.
	 */
	wakeup.w_flags.w_usersignal = 1;
}


/*
 * alarmclk
 */
static void
alarmclk()
{
	time_up = TRUE;
}

/*
 * childeath_single():
 *
 * This used to be the SIGCLD handler and it was set with signal()
 * (as opposed to sigset()).  When a child exited we'd come to the
 * handler, wait for the child, and reenable the handler with
 * signal() just before returning.  The implementation of signal()
 * checks with waitid() for waitable children and sends a SIGCLD
 * if there are some.  If children are exiting faster than the
 * handler can run we keep sending signals and the handler never
 * gets to return and eventually the stack runs out and init dies.
 * To prevent that we set the handler with sigset() so the handler
 * doesn't need to be reset, and in childeath() (see below) we
 * call childeath_single() as long as there are children to be
 * waited for.  If a child exits while init is in the handler a
 * SIGCLD will be pending and delivered on return from the handler.
 * If the child was already waited for the handler will have nothing
 * to do and return, otherwise the child will be waited for.
 */
static void
childeath_single(pid_t pid, int status)
{
	struct PROC_TABLE	*process;
	struct pidlist		*pp;

	/*
	 * Scan the process table to see if we are interested in this process.
	 */
	for (process = proc_table;
	    (process < proc_table + num_proc); process++) {
		if ((process->p_flags & (LIVING|OCCUPIED)) ==
		    (LIVING|OCCUPIED) && process->p_pid == pid) {

			/*
			 * Mark this process as having died and store the exit
			 * status.  Also set the wakeup flag for a dead child
			 * and break out of the loop.
			 */
			process->p_flags &= ~LIVING;
			process->p_exit = (short)status;
			wakeup.w_flags.w_childdeath = 1;

			return;
		}
	}

	/*
	 * No process was found above, look through auxiliary list.
	 */
	(void) sighold(SIGPOLL);
	pp = Plhead;
	while (pp) {
		if (pid > pp->pl_pid) {
			/*
			 * Keep on looking.
			 */
			pp = pp->pl_next;
			continue;
		} else if (pid < pp->pl_pid) {
			/*
			 * Not in the list.
			 */
			break;
		} else {
			/*
			 * This is a dead "godchild".
			 */
			pp->pl_dflag = 1;
			pp->pl_exit = (short)status;
			wakeup.w_flags.w_childdeath = 1;
			Gchild = 1;	/* Notice to call cleanaux(). */
			break;
		}
	}

	(void) sigrelse(SIGPOLL);
}

/* ARGSUSED */
static void
childeath(int signo)
{
	pid_t pid;
	int status;

	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
		childeath_single(pid, status);
}

static void
powerfail()
{
	(void) nice(-19);
	wakeup.w_flags.w_powerhit = 1;
}

/*
 * efork() forks a child and the parent inserts the process in its table
 * of processes that are directly a result of forks that it has performed.
 * The child just changes the "global" with the process id for this process
 * to it's new value.
 * If efork() is called with a pointer into the proc_table it uses that slot,
 * otherwise it searches for a free slot.  Regardless of how it was called,
 * it returns the pointer to the proc_table entry
 *
 * The SIGCLD signal is blocked (held) before calling efork()
 * and is unblocked (released) after efork() returns.
 *
 * Ideally, this should be rewritten to use modern signal semantics.
 */
static struct PROC_TABLE *
efork(int action, struct PROC_TABLE *process, int modes)
{
	pid_t	childpid;
	struct PROC_TABLE *proc;
	int		i;
	/*
	 * Freshen up the proc_table, removing any entries for dead processes
	 * that don't have NOCLEANUP set.  Perform the necessary accounting.
	 */
	for (proc = proc_table; (proc < proc_table + num_proc); proc++) {
		if ((proc->p_flags & (OCCUPIED|LIVING|NOCLEANUP)) ==
		    (OCCUPIED)) {
			/*
			 * Is this a named process?
			 * If so, do the necessary bookkeeping.
			 */
			if (proc->p_flags & NAMED)
				(void) account(DEAD_PROCESS, proc, NULL);

			/*
			 * Free this entry for new usage.
			 */
			proc->p_flags = 0;
		}
	}

	while ((childpid = fork()) == FAILURE) {
		/*
		 * Shorten the alarm timer in case someone else's child dies
		 * and free up a slot in the process table.
		 */
		setimer(5);

		/*
		 * Wait for some children to die.  Since efork()
		 * is always called with SIGCLD blocked, unblock
		 * it here so that child death signals can come in.
		 */
		(void) sigrelse(SIGCLD);
		(void) pause();
		(void) sighold(SIGCLD);
		setimer(0);
	}

	if (childpid != 0) {

		if (process == NULLPROC) {
			/*
			 * No proc table pointer specified so search
			 * for a free slot.
			 */
			for (process = proc_table;  process->p_flags != 0 &&
			    (process < proc_table + num_proc); process++)
					;

			if (process == (proc_table + num_proc)) {
				int old_proc_table_size = num_proc;

				/* Increase the process table size */
				increase_proc_table_size();
				if (old_proc_table_size == num_proc) {
					/* didn't grow: memory failure */
					return (NO_ROOM);
				} else {
					process =
					    proc_table + old_proc_table_size;
				}
			}

			process->p_time = 0L;
			process->p_count = 0;
		}
		process->p_id[0] = '\0';
		process->p_id[1] = '\0';
		process->p_id[2] = '\0';
		process->p_id[3] = '\0';
		process->p_pid = childpid;
		process->p_flags = (LIVING | OCCUPIED | modes);
		process->p_exit = 0;

		st_write();
	} else {
		if ((action & (M_WAIT | M_BOOTWAIT)) == 0)
			(void) setpgrp();

		process = NULLPROC;

		/*
		 * Reset all signals to the system defaults.
		 */
		for (i = SIGHUP; i <= SIGRTMAX; i++)
			(void) sigset(i, SIG_DFL);

		/*
		 * POSIX B.2.2.2 advises that init should set SIGTTOU,
		 * SIGTTIN, and SIGTSTP to SIG_IGN.
		 *
		 * Make sure that SIGXCPU and SIGXFSZ also remain ignored,
		 * for backward compatibility.
		 */
		(void) sigset(SIGTTIN, SIG_IGN);
		(void) sigset(SIGTTOU, SIG_IGN);
		(void) sigset(SIGTSTP, SIG_IGN);
		(void) sigset(SIGXCPU, SIG_IGN);
		(void) sigset(SIGXFSZ, SIG_IGN);
	}
	return (process);
}


/*
 * waitproc() waits for a specified process to die.  For this function to
 * work, the specified process must already in the proc_table.  waitproc()
 * returns the exit status of the specified process when it dies.
 */
static long
waitproc(struct PROC_TABLE *process)
{
	int		answer;
	sigset_t	oldmask, newmask, zeromask;

	(void) sigemptyset(&zeromask);
	(void) sigemptyset(&newmask);

	(void) sigaddset(&newmask, SIGCLD);

	/* Block SIGCLD and save the current signal mask */
	if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
		perror("SIG_BLOCK error");

	/*
	 * Wait around until the process dies.
	 */
	if (process->p_flags & LIVING)
		(void) sigsuspend(&zeromask);

	/* Reset signal mask to unblock SIGCLD */
	if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
		perror("SIG_SETMASK error");

	if (process->p_flags & LIVING)
		return (FAILURE);

	/*
	 * Make sure to only return 16 bits so that answer will always
	 * be positive whenever the process of interest really died.
	 */
	answer = (process->p_exit & 0xffff);

	/*
	 * Free the slot in the proc_table.
	 */
	process->p_flags = 0;
	return (answer);
}

/*
 * notify_pam_dead(): calls into the PAM framework to close the given session.
 */
static void
notify_pam_dead(struct utmpx *up)
{
	pam_handle_t *pamh;
	char user[sizeof (up->ut_user) + 1];
	char ttyn[sizeof (up->ut_line) + 1];
	char host[sizeof (up->ut_host) + 1];

	/*
	 * PAM does not take care of updating utmpx/wtmpx.
	 */
	(void) snprintf(user, sizeof (user), "%s", up->ut_user);
	(void) snprintf(ttyn, sizeof (ttyn), "%s", up->ut_line);
	(void) snprintf(host, sizeof (host), "%s", up->ut_host);

	if (pam_start("init", user, NULL, &pamh) == PAM_SUCCESS)  {
		(void) pam_set_item(pamh, PAM_TTY, ttyn);
		(void) pam_set_item(pamh, PAM_RHOST, host);
		(void) pam_close_session(pamh, 0);
		(void) pam_end(pamh, PAM_SUCCESS);
	}
}

/*
 * Check you can access utmpx (As / may be read-only and
 * /var may not be mounted yet).
 */
static int
access_utmpx(void)
{
	do {
		utmpx_ok = (access(UTMPX, R_OK|W_OK) == 0);
	} while (!utmpx_ok && errno == EINTR);

	return (utmpx_ok);
}

/*
 * account() updates entries in utmpx and appends new entries to the end of
 * wtmpx (assuming they exist).  The program argument indicates the name of
 * program if INIT_PROCESS, otherwise should be NULL.
 *
 * account() only blocks for INIT_PROCESS requests.
 *
 * Returns non-zero if write failed.
 */
static int
account(short state, struct PROC_TABLE *process, char *program)
{
	struct utmpx utmpbuf, *u, *oldu;
	int tmplen;
	char fail_buf[UT_LINE_SZ];
	sigset_t block, unblock;

	if (!utmpx_ok && !access_utmpx()) {
		return (-1);
	}

	/*
	 * Set up the prototype for the utmp structure we want to write.
	 */
	u = &utmpbuf;
	(void) memset(u, 0, sizeof (struct utmpx));

	/*
	 * Fill in the various fields of the utmp structure.
	 */
	u->ut_id[0] = process->p_id[0];
	u->ut_id[1] = process->p_id[1];
	u->ut_id[2] = process->p_id[2];
	u->ut_id[3] = process->p_id[3];
	u->ut_pid = process->p_pid;

	/*
	 * Fill the "ut_exit" structure.
	 */
	u->ut_exit.e_termination = WTERMSIG(process->p_exit);
	u->ut_exit.e_exit = WEXITSTATUS(process->p_exit);
	u->ut_type = state;

	(void) time(&u->ut_tv.tv_sec);

	/*
	 * Block signals for utmp update.
	 */
	(void) sigfillset(&block);
	(void) sigprocmask(SIG_BLOCK, &block, &unblock);

	/*
	 * See if there already is such an entry in the "utmpx" file.
	 */
	setutxent();	/* Start at beginning of utmpx file. */

	if ((oldu = getutxid(u)) != NULL) {
		/*
		 * Copy in the old "user", "line" and "host" fields
		 * to our new structure.
		 */
		bcopy(oldu->ut_user, u->ut_user, sizeof (u->ut_user));
		bcopy(oldu->ut_line, u->ut_line, sizeof (u->ut_line));
		bcopy(oldu->ut_host, u->ut_host, sizeof (u->ut_host));
		u->ut_syslen = (tmplen = strlen(u->ut_host)) ?
		    min(tmplen + 1, sizeof (u->ut_host)) : 0;

		if (oldu->ut_type == USER_PROCESS && state == DEAD_PROCESS) {
			notify_pam_dead(oldu);
		}
	}

	/*
	 * Perform special accounting. Insert the special string into the
	 * ut_line array. For INIT_PROCESSes put in the name of the
	 * program in the "ut_user" field.
	 */
	switch (state) {
	case INIT_PROCESS:
		(void) strncpy(u->ut_user, program, sizeof (u->ut_user));
		(void) strcpy(fail_buf, "INIT_PROCESS");
		break;

	default:
		(void) strlcpy(fail_buf, u->ut_id, sizeof (u->ut_id) + 1);
		break;
	}

	/*
	 * Write out the updated entry to utmpx file.
	 */
	if (pututxline(u) == NULL) {
		console(B_TRUE, "Failed write of utmpx entry: \"%s\": %s\n",
		    fail_buf, strerror(errno));
		endutxent();
		(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
		return (-1);
	}

	/*
	 * If we're able to write to utmpx, then attempt to add to the
	 * end of the wtmpx file.
	 */
	updwtmpx(WTMPX, u);

	endutxent();

	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);

	return (0);
}

static void
clearent(pid_t pid, short status)
{
	struct utmpx *up;
	sigset_t block, unblock;

	/*
	 * Block signals for utmp update.
	 */
	(void) sigfillset(&block);
	(void) sigprocmask(SIG_BLOCK, &block, &unblock);

	/*
	 * No error checking for now.
	 */

	setutxent();
	while (up = getutxent()) {
		if (up->ut_pid == pid) {
			if (up->ut_type == DEAD_PROCESS) {
				/*
				 * Cleaned up elsewhere.
				 */
				continue;
			}

			notify_pam_dead(up);

			up->ut_type = DEAD_PROCESS;
			up->ut_exit.e_termination = WTERMSIG(status);
			up->ut_exit.e_exit = WEXITSTATUS(status);
			(void) time(&up->ut_tv.tv_sec);

			(void) pututxline(up);
			/*
			 * Now attempt to add to the end of the
			 * wtmp and wtmpx files.  Do not create
			 * if they don't already exist.
			 */
			updwtmpx(WTMPX, up);

			break;
		}
	}

	endutxent();
	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
}

/*
 * prog_name() searches for the word or unix path name and
 * returns a pointer to the last element of the pathname.
 */
static char *
prog_name(char *string)
{
	char	*ptr, *ptr2;
	static char word[UT_USER_SZ + 1];

	/*
	 * Search for the first word skipping leading spaces and tabs.
	 */
	while (*string == ' ' || *string == '\t')
		string++;

	/*
	 * If the first non-space non-tab character is not one allowed in
	 * a word, return a pointer to a null string, otherwise parse the
	 * pathname.
	 */
	if (*string != '.' && *string != '/' && *string != '_' &&
	    (*string < 'a' || *string > 'z') &&
	    (*string < 'A' || * string > 'Z') &&
	    (*string < '0' || *string > '9'))
		return ("");

	/*
	 * Parse the pathname looking forward for '/', ' ', '\t', '\n' or
	 * '\0'.  Each time a '/' is found, move "ptr" to one past the
	 * '/', thus when a ' ', '\t', '\n', or '\0' is found, "ptr" will
	 * point to the last element of the pathname.
	 */
	for (ptr = string; *string != ' ' && *string != '\t' &&
	    *string != '\n' && *string != '\0'; string++) {
		if (*string == '/')
			ptr = string+1;
	}

	/*
	 * Copy out up to the size of the "ut_user" array into "word",
	 * null terminate it and return a pointer to it.
	 */
	for (ptr2 = &word[0]; ptr2 < &word[UT_USER_SZ] &&
	    ptr < string; /* CSTYLED */)
		*ptr2++ = *ptr++;

	*ptr2 = '\0';
	return (&word[0]);
}


/*
 * realcon() returns a nonzero value if there is a character device
 * associated with SYSCON that has the same device number as CONSOLE.
 */
static int
realcon()
{
	struct stat sconbuf, conbuf;

	if (stat(SYSCON, &sconbuf) != -1 &&
	    stat(CONSOLE, &conbuf) != -1 &&
	    S_ISCHR(sconbuf.st_mode) &&
	    S_ISCHR(conbuf.st_mode) &&
	    sconbuf.st_rdev == conbuf.st_rdev) {
		return (1);
	} else {
		return (0);
	}
}


/*
 * get_ioctl_syscon() retrieves the SYSCON settings from the IOCTLSYSCON file.
 * Returns true if the IOCTLSYSCON file needs to be written (with
 * write_ioctl_syscon() below)
 */
static int
get_ioctl_syscon()
{
	FILE	*fp;
	unsigned int	iflags, oflags, cflags, lflags, ldisc, cc[18];
	int		i, valid_format = 0;

	/*
	 * Read in the previous modes for SYSCON from IOCTLSYSCON.
	 */
	if ((fp = fopen(IOCTLSYSCON, "r")) == NULL) {
		stored_syscon_termios = dflt_termios;
		console(B_TRUE,
		    "warning:%s does not exist, default settings assumed\n",
		    IOCTLSYSCON);
	} else {

		i = fscanf(fp,
	    "%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x",
		    &iflags, &oflags, &cflags, &lflags,
		    &cc[0], &cc[1], &cc[2], &cc[3], &cc[4], &cc[5], &cc[6],
		    &cc[7], &cc[8], &cc[9], &cc[10], &cc[11], &cc[12], &cc[13],
		    &cc[14], &cc[15], &cc[16], &cc[17]);

		if (i == 22) {
			stored_syscon_termios.c_iflag = iflags;
			stored_syscon_termios.c_oflag = oflags;
			stored_syscon_termios.c_cflag = cflags;
			stored_syscon_termios.c_lflag = lflags;
			for (i = 0; i < 18; i++)
				stored_syscon_termios.c_cc[i] = (char)cc[i];
			valid_format = 1;
		} else if (i == 13) {
		rewind(fp);
		i = fscanf(fp, "%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x",
		    &iflags, &oflags, &cflags, &lflags, &ldisc, &cc[0], &cc[1],
		    &cc[2], &cc[3], &cc[4], &cc[5], &cc[6], &cc[7]);

		/*
		 * If the file is formatted properly, use the values to
		 * initialize the console terminal condition.
		 */
		stored_syscon_termios.c_iflag = (ushort_t)iflags;
		stored_syscon_termios.c_oflag = (ushort_t)oflags;
		stored_syscon_termios.c_cflag = (ushort_t)cflags;
		stored_syscon_termios.c_lflag = (ushort_t)lflags;
		for (i = 0; i < 8; i++)
			stored_syscon_termios.c_cc[i] = (char)cc[i];
		valid_format = 1;
		}
		(void) fclose(fp);

		/* If the file is badly formatted, use the default settings. */
		if (!valid_format)
			stored_syscon_termios = dflt_termios;
	}

	/* If the file had a bad format, rewrite it later. */
	return (!valid_format);
}


static void
write_ioctl_syscon()
{
	FILE *fp;
	int i;

	(void) unlink(SYSCON);
	(void) link(SYSTTY, SYSCON);
	(void) umask(022);
	fp = fopen(IOCTLSYSCON, "w");

	(void) fprintf(fp, "%x:%x:%x:%x:0", stored_syscon_termios.c_iflag,
	    stored_syscon_termios.c_oflag, stored_syscon_termios.c_cflag,
	    stored_syscon_termios.c_lflag);
	for (i = 0; i < 8; ++i)
		(void) fprintf(fp, ":%x", stored_syscon_termios.c_cc[i]);
	(void) putc('\n', fp);

	(void) fflush(fp);
	(void) fsync(fileno(fp));
	(void) fclose(fp);
	(void) umask(cmask);
}


/*
 * void console(boolean_t, char *, ...)
 *   Outputs the requested message to the system console.  Note that the number
 *   of arguments passed to console() should be determined by the print format.
 *
 *   The "prefix" parameter indicates whether or not "INIT: " should precede the
 *   message.
 *
 *   To make sure we write to the console in a sane fashion, we use the modes
 *   we keep in stored_syscon_termios (which we read out of /etc/ioctl.syscon).
 *   Afterwards we restore whatever modes were already there.
 */
/* PRINTFLIKE2 */
static void
console(boolean_t prefix, char *format, ...)
{
	char	outbuf[BUFSIZ];
	va_list	args;
	int fd, getret;
	struct termios old_syscon_termios;
	FILE *f;

	/*
	 * We open SYSCON anew each time in case it has changed (see
	 * userinit()).
	 */
	if ((fd = open(SYSCON, O_RDWR | O_NOCTTY)) < 0 ||
	    (f = fdopen(fd, "r+")) == NULL) {
		if (prefix)
			syslog(LOG_WARNING, "INIT: ");
		va_start(args, format);
		vsyslog(LOG_WARNING, format, args);
		va_end(args);
		if (fd >= 0)
			(void) close(fd);
		return;
	}
	setbuf(f, &outbuf[0]);

	getret = tcgetattr(fd, &old_syscon_termios);
	old_syscon_termios.c_cflag &= ~HUPCL;
	if (realcon())
		/* Don't overwrite cflag of real console. */
		stored_syscon_termios.c_cflag = old_syscon_termios.c_cflag;

	stored_syscon_termios.c_cflag &= ~HUPCL;

	(void) tcsetattr(fd, TCSANOW, &stored_syscon_termios);

	if (prefix)
		(void) fprintf(f, "\nINIT: ");
	va_start(args, format);
	(void) vfprintf(f, format, args);
	va_end(args);

	if (getret == 0)
		(void) tcsetattr(fd, TCSADRAIN, &old_syscon_termios);

	(void) fclose(f);
}

/*
 * timer() is a substitute for sleep() which uses alarm() and pause().
 */
static void
timer(int waitime)
{
	setimer(waitime);
	while (time_up == FALSE)
		(void) pause();
}

static void
setimer(int timelimit)
{
	alarmclk();
	(void) alarm(timelimit);
	time_up = (timelimit ? FALSE : TRUE);
}

/*
 * Fails with
 *   ENOMEM - out of memory
 *   ECONNABORTED - repository connection broken
 *   EPERM - permission denied
 *   EACCES - backend access denied
 *   EROFS - backend readonly
 */
static int
get_or_add_startd(scf_instance_t *inst)
{
	scf_handle_t *h;
	scf_scope_t *scope = NULL;
	scf_service_t *svc = NULL;
	int ret = 0;

	h = scf_instance_handle(inst);

	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == 0)
		return (0);

	switch (scf_error()) {
	case SCF_ERROR_CONNECTION_BROKEN:
		return (ECONNABORTED);

	case SCF_ERROR_NOT_FOUND:
		break;

	case SCF_ERROR_HANDLE_MISMATCH:
	case SCF_ERROR_INVALID_ARGUMENT:
	case SCF_ERROR_CONSTRAINT_VIOLATED:
	default:
		bad_error("scf_handle_decode_fmri", scf_error());
	}

	/* Make sure we're right, since we're adding piece-by-piece. */
	assert(strcmp(SCF_SERVICE_STARTD,
	    "svc:/system/svc/restarter:default") == 0);

	if ((scope = scf_scope_create(h)) == NULL ||
	    (svc = scf_service_create(h)) == NULL) {
		ret = ENOMEM;
		goto out;
	}

get_scope:
	if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, scope) != 0) {
		switch (scf_error()) {
		case SCF_ERROR_CONNECTION_BROKEN:
			ret = ECONNABORTED;
			goto out;

		case SCF_ERROR_NOT_FOUND:
			(void) fputs(gettext(
			    "smf(7) repository missing local scope.\n"),
			    stderr);
			exit(1);
			/* NOTREACHED */

		case SCF_ERROR_HANDLE_MISMATCH:
		case SCF_ERROR_INVALID_ARGUMENT:
		default:
			bad_error("scf_handle_get_scope", scf_error());
		}
	}

get_svc:
	if (scf_scope_get_service(scope, "system/svc/restarter", svc) != 0) {
		switch (scf_error()) {
		case SCF_ERROR_CONNECTION_BROKEN:
			ret = ECONNABORTED;
			goto out;

		case SCF_ERROR_DELETED:
			goto get_scope;

		case SCF_ERROR_NOT_FOUND:
			break;

		case SCF_ERROR_HANDLE_MISMATCH:
		case SCF_ERROR_INVALID_ARGUMENT:
		case SCF_ERROR_NOT_SET:
		default:
			bad_error("scf_scope_get_service", scf_error());
		}

add_svc:
		if (scf_scope_add_service(scope, "system/svc/restarter", svc) !=
		    0) {
			switch (scf_error()) {
			case SCF_ERROR_CONNECTION_BROKEN:
				ret = ECONNABORTED;
				goto out;

			case SCF_ERROR_EXISTS:
				goto get_svc;

			case SCF_ERROR_PERMISSION_DENIED:
				ret = EPERM;
				goto out;

			case SCF_ERROR_BACKEND_ACCESS:
				ret = EACCES;
				goto out;

			case SCF_ERROR_BACKEND_READONLY:
				ret = EROFS;
				goto out;

			case SCF_ERROR_HANDLE_MISMATCH:
			case SCF_ERROR_INVALID_ARGUMENT:
			case SCF_ERROR_NOT_SET:
			default:
				bad_error("scf_scope_add_service", scf_error());
			}
		}
	}

get_inst:
	if (scf_service_get_instance(svc, "default", inst) != 0) {
		switch (scf_error()) {
		case SCF_ERROR_CONNECTION_BROKEN:
			ret = ECONNABORTED;
			goto out;

		case SCF_ERROR_DELETED:
			goto add_svc;

		case SCF_ERROR_NOT_FOUND:
			break;

		case SCF_ERROR_HANDLE_MISMATCH:
		case SCF_ERROR_INVALID_ARGUMENT:
		case SCF_ERROR_NOT_SET:
		default:
			bad_error("scf_service_get_instance", scf_error());
		}

		if (scf_service_add_instance(svc, "default", inst) !=
		    0) {
			switch (scf_error()) {
			case SCF_ERROR_CONNECTION_BROKEN:
				ret = ECONNABORTED;
				goto out;

			case SCF_ERROR_DELETED:
				goto add_svc;

			case SCF_ERROR_EXISTS:
				goto get_inst;

			case SCF_ERROR_PERMISSION_DENIED:
				ret = EPERM;
				goto out;

			case SCF_ERROR_BACKEND_ACCESS:
				ret = EACCES;
				goto out;

			case SCF_ERROR_BACKEND_READONLY:
				ret = EROFS;
				goto out;

			case SCF_ERROR_HANDLE_MISMATCH:
			case SCF_ERROR_INVALID_ARGUMENT:
			case SCF_ERROR_NOT_SET:
			default:
				bad_error("scf_service_add_instance",
				    scf_error());
			}
		}
	}

	ret = 0;

out:
	scf_service_destroy(svc);
	scf_scope_destroy(scope);
	return (ret);
}

/*
 * Fails with
 *   ECONNABORTED - repository connection broken
 *   ECANCELED - the transaction's property group was deleted
 */
static int
transaction_add_set(scf_transaction_t *tx, scf_transaction_entry_t *ent,
    const char *pname, scf_type_t type)
{
change_type:
	if (scf_transaction_property_change_type(tx, ent, pname, type) == 0)
		return (0);

	switch (scf_error()) {
	case SCF_ERROR_CONNECTION_BROKEN:
		return (ECONNABORTED);

	case SCF_ERROR_DELETED:
		return (ECANCELED);

	case SCF_ERROR_NOT_FOUND:
		goto new;

	case SCF_ERROR_HANDLE_MISMATCH:
	case SCF_ERROR_INVALID_ARGUMENT:
	case SCF_ERROR_NOT_BOUND:
	case SCF_ERROR_NOT_SET:
	default:
		bad_error("scf_transaction_property_change_type", scf_error());
	}

new:
	if (scf_transaction_property_new(tx, ent, pname, type) == 0)
		return (0);

	switch (scf_error()) {
	case SCF_ERROR_CONNECTION_BROKEN:
		return (ECONNABORTED);

	case SCF_ERROR_DELETED:
		return (ECANCELED);

	case SCF_ERROR_EXISTS:
		goto change_type;

	case SCF_ERROR_HANDLE_MISMATCH:
	case SCF_ERROR_INVALID_ARGUMENT:
	case SCF_ERROR_NOT_BOUND:
	case SCF_ERROR_NOT_SET:
	default:
		bad_error("scf_transaction_property_new", scf_error());
		/* NOTREACHED */
	}
}

static void
scferr(void)
{
	switch (scf_error()) {
	case SCF_ERROR_NO_MEMORY:
		console(B_TRUE, gettext("Out of memory.\n"));
		break;

	case SCF_ERROR_CONNECTION_BROKEN:
		console(B_TRUE, gettext(
		    "Connection to smf(7) repository server broken.\n"));
		break;

	case SCF_ERROR_NO_RESOURCES:
		console(B_TRUE, gettext(
		    "smf(7) repository server is out of memory.\n"));
		break;

	case SCF_ERROR_PERMISSION_DENIED:
		console(B_TRUE, gettext("Insufficient privileges.\n"));
		break;

	default:
		console(B_TRUE, gettext("libscf error: %s\n"),
		    scf_strerror(scf_error()));
	}
}

static void
lscf_set_runlevel(char rl)
{
	scf_handle_t *h;
	scf_instance_t *inst = NULL;
	scf_propertygroup_t *pg = NULL;
	scf_transaction_t *tx = NULL;
	scf_transaction_entry_t *ent = NULL;
	scf_value_t *val = NULL;
	char buf[2];
	int r;

	h = scf_handle_create(SCF_VERSION);
	if (h == NULL) {
		scferr();
		return;
	}

	if (scf_handle_bind(h) != 0) {
		switch (scf_error()) {
		case SCF_ERROR_NO_SERVER:
			console(B_TRUE,
			    gettext("smf(7) repository server not running.\n"));
			goto bail;

		default:
			scferr();
			goto bail;
		}
	}

	if ((inst = scf_instance_create(h)) == NULL ||
	    (pg = scf_pg_create(h)) == NULL ||
	    (val = scf_value_create(h)) == NULL ||
	    (tx = scf_transaction_create(h)) == NULL ||
	    (ent = scf_entry_create(h)) == NULL) {
		scferr();
		goto bail;
	}

get_inst:
	r = get_or_add_startd(inst);
	switch (r) {
	case 0:
		break;

	case ENOMEM:
	case ECONNABORTED:
	case EPERM:
	case EACCES:
	case EROFS:
		scferr();
		goto bail;
	default:
		bad_error("get_or_add_startd", r);
	}

get_pg:
	if (scf_instance_get_pg(inst, SCF_PG_OPTIONS_OVR, pg) != 0) {
		switch (scf_error()) {
		case SCF_ERROR_CONNECTION_BROKEN:
			scferr();
			goto bail;

		case SCF_ERROR_DELETED:
			goto get_inst;

		case SCF_ERROR_NOT_FOUND:
			break;

		case SCF_ERROR_HANDLE_MISMATCH:
		case SCF_ERROR_INVALID_ARGUMENT:
		case SCF_ERROR_NOT_SET:
		default:
			bad_error("scf_instance_get_pg", scf_error());
		}

add_pg:
		if (scf_instance_add_pg(inst, SCF_PG_OPTIONS_OVR,
		    SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS, pg) !=
		    0) {
			switch (scf_error()) {
			case SCF_ERROR_CONNECTION_BROKEN:
			case SCF_ERROR_PERMISSION_DENIED:
			case SCF_ERROR_BACKEND_ACCESS:
				scferr();
				goto bail;

			case SCF_ERROR_DELETED:
				goto get_inst;

			case SCF_ERROR_EXISTS:
				goto get_pg;

			case SCF_ERROR_HANDLE_MISMATCH:
			case SCF_ERROR_INVALID_ARGUMENT:
			case SCF_ERROR_NOT_SET:
			default:
				bad_error("scf_instance_add_pg", scf_error());
			}
		}
	}

	buf[0] = rl;
	buf[1] = '\0';
	r = scf_value_set_astring(val, buf);
	assert(r == 0);

	for (;;) {
		if (scf_transaction_start(tx, pg) != 0) {
			switch (scf_error()) {
			case SCF_ERROR_CONNECTION_BROKEN:
			case SCF_ERROR_PERMISSION_DENIED:
			case SCF_ERROR_BACKEND_ACCESS:
				scferr();
				goto bail;

			case SCF_ERROR_DELETED:
				goto add_pg;

			case SCF_ERROR_HANDLE_MISMATCH:
			case SCF_ERROR_NOT_BOUND:
			case SCF_ERROR_IN_USE:
			case SCF_ERROR_NOT_SET:
			default:
				bad_error("scf_transaction_start", scf_error());
			}
		}

		r = transaction_add_set(tx, ent, "runlevel", SCF_TYPE_ASTRING);
		switch (r) {
		case 0:
			break;

		case ECONNABORTED:
			scferr();
			goto bail;

		case ECANCELED:
			scf_transaction_reset(tx);
			goto add_pg;

		default:
			bad_error("transaction_add_set", r);
		}

		r = scf_entry_add_value(ent, val);
		assert(r == 0);

		r = scf_transaction_commit(tx);
		if (r == 1)
			break;

		if (r != 0) {
			switch (scf_error()) {
			case SCF_ERROR_CONNECTION_BROKEN:
			case SCF_ERROR_PERMISSION_DENIED:
			case SCF_ERROR_BACKEND_ACCESS:
			case SCF_ERROR_BACKEND_READONLY:
				scferr();
				goto bail;

			case SCF_ERROR_DELETED:
				scf_transaction_reset(tx);
				goto add_pg;

			case SCF_ERROR_INVALID_ARGUMENT:
			case SCF_ERROR_NOT_BOUND:
			case SCF_ERROR_NOT_SET:
			default:
				bad_error("scf_transaction_commit",
				    scf_error());
			}
		}

		scf_transaction_reset(tx);
		(void) scf_pg_update(pg);
	}

bail:
	scf_transaction_destroy(tx);
	scf_entry_destroy(ent);
	scf_value_destroy(val);
	scf_pg_destroy(pg);
	scf_instance_destroy(inst);

	(void) scf_handle_unbind(h);
	scf_handle_destroy(h);
}

/*
 * Attempt to confirm that svc.startd is ready to accept a user-initiated
 * run-level change. startd is not ready until it has started its
 * _scf_notify_wait thread to watch for events from svc.configd. This is
 * inherently racy. To workaround this, we check the status of a file that
 * startd will create once it has started the _scf_notify_wait thread.
 * If we don't see this file after one minute, then charge ahead.
 */
static void
verify_startd_ready()
{
	struct stat64 buf;
	int i;

	for (i = 0; i < 60; i++) {
		if (stat64("/etc/svc/volatile/startd.ready", &buf) == 0)
			return;
		sleep(1);
	}
	console(B_TRUE, "verify startd timeout\n");
}

/*
 * Function to handle requests from users to main init running as process 1.
 */
static void
userinit(int argc, char **argv)
{
	FILE	*fp;
	char	*ln;
	int	init_signal;
	struct stat	sconbuf, conbuf;
	const char *usage_msg = "Usage: init [0123456SsQqabc]\n";

	/*
	 * We are a user invoked init.  Is there an argument and is it
	 * a single character?  If not, print usage message and quit.
	 */
	if (argc != 2 || argv[1][1] != '\0') {
		(void) fprintf(stderr, usage_msg);
		exit(0);
	}

	if ((init_signal = lvlname_to_state((char)argv[1][0])) == -1) {
		(void) fprintf(stderr, usage_msg);
		(void) audit_put_record(ADT_FAILURE, ADT_FAIL_VALUE_BAD_CMD,
		    argv[1]);
		exit(1);
	}

	if (init_signal == SINGLE_USER) {
		/*
		 * Make sure this process is talking to a legal tty line
		 * and that /dev/syscon is linked to this line.
		 */
		ln = ttyname(0);	/* Get the name of tty */
		if (ln == NULL) {
			(void) fprintf(stderr,
			    "Standard input not a tty line\n");
			(void) audit_put_record(ADT_FAILURE,
			    ADT_FAIL_VALUE_BAD_TTY, argv[1]);
			exit(1);
		}

		if ((stat(ln, &sconbuf) != -1) &&
		    (stat(SYSCON, &conbuf) == -1 ||
		    sconbuf.st_rdev != conbuf.st_rdev)) {
			/*
			 * /dev/syscon needs to change.
			 * Unlink /dev/syscon and relink it to the current line.
			 */
			if (lstat(SYSCON, &conbuf) != -1 &&
			    unlink(SYSCON) == FAILURE) {
				perror("Can't unlink /dev/syscon");
				(void) fprintf(stderr,
				    "Run command on the system console.\n");
				(void) audit_put_record(ADT_FAILURE,
				    ADT_FAIL_VALUE_PROGRAM, argv[1]);
				exit(1);
			}
			if (symlink(ln, SYSCON) == FAILURE) {
				(void) fprintf(stderr,
				    "Can't symlink /dev/syscon to %s: %s", ln,
				    strerror(errno));

				/* Try to leave a syscon */
				(void) link(SYSTTY, SYSCON);
				(void) audit_put_record(ADT_FAILURE,
				    ADT_FAIL_VALUE_PROGRAM, argv[1]);
				exit(1);
			}

			/*
			 * Try to leave a message on system console saying where
			 * /dev/syscon is currently connected.
			 */
			if ((fp = fopen(SYSTTY, "r+")) != NULL) {
				(void) fprintf(fp,
				    "\n****	SYSCON CHANGED TO %s	****\n",
				    ln);
				(void) fclose(fp);
			}
		}
	}

	update_boot_archive(init_signal);

	(void) audit_put_record(ADT_SUCCESS, ADT_SUCCESS, argv[1]);

	/*
	 * Before we tell init to start a run-level change, we need to be
	 * sure svc.startd is ready to accept that.
	 */
	verify_startd_ready();

	/*
	 * Signal init; init will take care of telling svc.startd.
	 */
	if (kill(init_pid, init_signal) == FAILURE) {
		(void) fprintf(stderr, "Must be super-user\n");
		(void) audit_put_record(ADT_FAILURE,
		    ADT_FAIL_VALUE_AUTH, argv[1]);
		exit(1);
	}

	exit(0);
}


#define	DELTA	25	/* Number of pidlist elements to allocate at a time */

/* ARGSUSED */
void
sigpoll(int n)
{
	struct pidrec prec;
	struct pidrec *p = &prec;
	struct pidlist *plp;
	struct pidlist *tp, *savetp;
	int i;

	if (Pfd < 0) {
		return;
	}

	for (;;) {
		/*
		 * Important Note: Either read will really fail (in which case
		 * return is all we can do) or will get EAGAIN (Pfd was opened
		 * O_NDELAY), in which case we also want to return.
		 * Always return from here!
		 */
		if (read(Pfd, p, sizeof (struct pidrec)) !=
						sizeof (struct pidrec)) {
			return;
		}
		switch (p->pd_type) {

		case ADDPID:
			/*
			 * New "godchild", add to list.
			 */
			if (Plfree == NULL) {
				plp = (struct pidlist *)calloc(DELTA,
				    sizeof (struct pidlist));
				if (plp == NULL) {
					/* Can't save pid */
					break;
				}
				/*
				 * Point at 2nd record allocated, we'll use plp.
				 */
				tp = plp + 1;
				/*
				 * Link them into a chain.
				 */
				Plfree = tp;
				for (i = 0; i < DELTA - 2; i++) {
					tp->pl_next = tp + 1;
					tp++;
				}
			} else {
				plp = Plfree;
				Plfree = plp->pl_next;
			}
			plp->pl_pid = p->pd_pid;
			plp->pl_dflag = 0;
			plp->pl_next = NULL;
			/*
			 * Note - pid list is kept in increasing order of pids.
			 */
			if (Plhead == NULL) {
				Plhead = plp;
				/* Back up to read next record */
				break;
			} else {
				savetp = tp = Plhead;
				while (tp) {
					if (plp->pl_pid > tp->pl_pid) {
						savetp = tp;
						tp = tp->pl_next;
						continue;
					} else if (plp->pl_pid < tp->pl_pid) {
						if (tp == Plhead) {
							plp->pl_next = Plhead;
							Plhead = plp;
						} else {
							plp->pl_next =
							    savetp->pl_next;
							savetp->pl_next = plp;
						}
						break;
					} else {
						/* Already in list! */
						plp->pl_next = Plfree;
						Plfree = plp;
						break;
					}
				}
				if (tp == NULL) {
					/* Add to end of list */
					savetp->pl_next = plp;
				}
			}
			/* Back up to read next record. */
			break;

		case REMPID:
			/*
			 * This one was handled by someone else,
			 * purge it from the list.
			 */
			if (Plhead == NULL) {
				/* Back up to read next record. */
				break;
			}
			savetp = tp = Plhead;
			while (tp) {
				if (p->pd_pid > tp->pl_pid) {
					/* Keep on looking. */
					savetp = tp;
					tp = tp->pl_next;
					continue;
				} else if (p->pd_pid < tp->pl_pid) {
					/* Not in list. */
					break;
				} else {
					/* Found it. */
					if (tp == Plhead)
						Plhead = tp->pl_next;
					else
						savetp->pl_next = tp->pl_next;
					tp->pl_next = Plfree;
					Plfree = tp;
					break;
				}
			}
			/* Back up to read next record. */
			break;
		default:
			console(B_TRUE, "Bad message on initpipe\n");
			break;
		}
	}
}


static void
cleanaux()
{
	struct pidlist *savep, *p;
	pid_t	pid;
	short	status;

	(void) sighold(SIGCLD);
	Gchild = 0;	/* Note - Safe to do this here since no SIGCLDs */
	(void) sighold(SIGPOLL);
	savep = p = Plhead;
	while (p) {
		if (p->pl_dflag) {
			/*
			 * Found an entry to delete,
			 * remove it from list first.
			 */
			pid = p->pl_pid;
			status = p->pl_exit;
			if (p == Plhead) {
				Plhead = p->pl_next;
				p->pl_next = Plfree;
				Plfree = p;
				savep = p = Plhead;
			} else {
				savep->pl_next = p->pl_next;
				p->pl_next = Plfree;
				Plfree = p;
				p = savep->pl_next;
			}
			clearent(pid, status);
			continue;
		}
		savep = p;
		p = p->pl_next;
	}
	(void) sigrelse(SIGPOLL);
	(void) sigrelse(SIGCLD);
}


/*
 * /etc/inittab has more entries and we have run out of room in the proc_table
 * array. Double the size of proc_table to accomodate the extra entries.
 */
static void
increase_proc_table_size()
{
	sigset_t block, unblock;
	void *ptr;
	size_t delta = num_proc * sizeof (struct PROC_TABLE);


	/*
	 * Block signals for realloc.
	 */
	(void) sigfillset(&block);
	(void) sigprocmask(SIG_BLOCK, &block, &unblock);


	/*
	 * On failure we just return because callers of this function check
	 * for failure.
	 */
	do
		ptr = realloc(g_state, g_state_sz + delta);
	while (ptr == NULL && errno == EAGAIN)
		;

	if (ptr != NULL) {
		/* ensure that the new part is initialized to zero */
		bzero((caddr_t)ptr + g_state_sz, delta);

		g_state = ptr;
		g_state_sz += delta;
		num_proc <<= 1;
	}


	/* unblock our signals before returning */
	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
}



/*
 * Sanity check g_state.
 */
static int
st_sane()
{
	int i;
	struct PROC_TABLE *ptp;


	/* Note: cur_state is encoded as a signal number */
	if (cur_state < 1 || cur_state == 9 || cur_state > 13)
		return (0);

	/* Check num_proc */
	if (g_state_sz != sizeof (struct init_state) + (num_proc - 1) *
	    sizeof (struct PROC_TABLE))
		return (0);

	/* Check proc_table */
	for (i = 0, ptp = proc_table; i < num_proc; ++i, ++ptp) {
		/* skip unoccupied entries */
		if (!(ptp->p_flags & OCCUPIED))
			continue;

		/* p_flags has no bits outside of PF_MASK */
		if (ptp->p_flags & ~(PF_MASK))
			return (0);

		/* 5 <= pid <= MAXPID */
		if (ptp->p_pid < 5 || ptp->p_pid > MAXPID)
			return (0);

		/* p_count >= 0 */
		if (ptp->p_count < 0)
			return (0);

		/* p_time >= 0 */
		if (ptp->p_time < 0)
			return (0);
	}

	return (1);
}

/*
 * Initialize our state.
 *
 * If the system just booted, then init_state_file, which is located on an
 * everpresent tmpfs filesystem, should not exist.
 *
 * If we were restarted, then init_state_file should exist, in
 * which case we'll read it in, sanity check it, and use it.
 *
 * Note: You can't call console() until proc_table is ready.
 */
void
st_init()
{
	struct stat stb;
	int ret, st_fd, insane = 0;
	size_t to_be_read;
	char *ptr;


	booting = 1;

	do {
		/*
		 * If we can exclusively create the file, then we're the
		 * initial invocation of init(8).
		 */
		st_fd = open(init_state_file, O_RDWR | O_CREAT | O_EXCL,
		    S_IRUSR | S_IWUSR);
	} while (st_fd == -1 && errno == EINTR);
	if (st_fd != -1)
		goto new_state;

	booting = 0;

	do {
		st_fd = open(init_state_file, O_RDWR, S_IRUSR | S_IWUSR);
	} while (st_fd == -1 && errno == EINTR);
	if (st_fd == -1)
		goto new_state;

	/* Get the size of the file. */
	do
		ret = fstat(st_fd, &stb);
	while (ret == -1 && errno == EINTR)
		;
	if (ret == -1)
		goto new_state;

	do
		g_state = malloc(stb.st_size);
	while (g_state == NULL && errno == EAGAIN)
		;
	if (g_state == NULL)
		goto new_state;

	to_be_read = stb.st_size;
	ptr = (char *)g_state;
	while (to_be_read > 0) {
		ssize_t read_ret;

		read_ret = read(st_fd, ptr, to_be_read);
		if (read_ret < 0) {
			if (errno == EINTR)
				continue;

			goto new_state;
		}

		to_be_read -= read_ret;
		ptr += read_ret;
	}

	(void) close(st_fd);

	g_state_sz = stb.st_size;

	if (st_sane()) {
		console(B_TRUE, "Restarting.\n");
		return;
	}

	insane = 1;

new_state:
	if (st_fd >= 0)
		(void) close(st_fd);
	else
		(void) unlink(init_state_file);

	if (g_state != NULL)
		free(g_state);

	/* Something went wrong, so allocate new state. */
	g_state_sz = sizeof (struct init_state) +
	    ((init_num_proc - 1) * sizeof (struct PROC_TABLE));
	do
		g_state = calloc(1, g_state_sz);
	while (g_state == NULL && errno == EAGAIN)
		;
	if (g_state == NULL) {
		/* Fatal error! */
		exit(errno);
	}

	g_state->ist_runlevel = -1;
	num_proc = init_num_proc;

	if (!booting) {
		console(B_TRUE, "Restarting.\n");

		/* Overwrite the bad state file. */
		st_write();

		if (!insane) {
			console(B_TRUE,
			    "Error accessing persistent state file `%s'.  "
			    "Ignored.\n", init_state_file);
		} else {
			console(B_TRUE,
			    "Persistent state file `%s' is invalid and was "
			    "ignored.\n", init_state_file);
		}
	}
}

/*
 * Write g_state out to the state file.
 */
void
st_write()
{
	static int complained = 0;

	int st_fd;
	char *cp;
	size_t sz;
	ssize_t ret;


	do {
		st_fd = open(init_next_state_file,
		    O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
	} while (st_fd < 0 && errno == EINTR);
	if (st_fd < 0)
		goto err;

	cp = (char *)g_state;
	sz = g_state_sz;
	while (sz > 0) {
		ret = write(st_fd, cp, sz);
		if (ret < 0) {
			if (errno == EINTR)
				continue;

			goto err;
		}

		sz -= ret;
		cp += ret;
	}

	(void) close(st_fd);
	st_fd = -1;
	if (rename(init_next_state_file, init_state_file)) {
		(void) unlink(init_next_state_file);
		goto err;
	}
	complained = 0;

	return;

err:
	if (st_fd >= 0)
		(void) close(st_fd);

	if (!booting && !complained) {
		/*
		 * Only complain after the filesystem should have come up.
		 * And only do it once so we don't loop between console()
		 * & efork().
		 */
		complained = 1;
		if (st_fd)
			console(B_TRUE, "Couldn't write persistent state "
			    "file `%s'.\n", init_state_file);
		else
			console(B_TRUE, "Couldn't move persistent state "
			    "file `%s' to `%s'.\n", init_next_state_file,
			    init_state_file);
	}
}

/*
 * Create a contract with these parameters.
 */
static int
contract_make_template(uint_t info, uint_t critical, uint_t fatal,
    uint64_t cookie)
{
	int fd, err;

	char *ioctl_tset_emsg =
	    "Couldn't set \"%s\" contract template parameter: %s.\n";

	do
		fd = open64(CTFS_ROOT "/process/template", O_RDWR);
	while (fd < 0 && errno == EINTR)
		;
	if (fd < 0) {
		console(B_TRUE, "Couldn't create process template: %s.\n",
		    strerror(errno));
		return (-1);
	}

	if (err = ct_pr_tmpl_set_param(fd, CT_PR_INHERIT | CT_PR_REGENT))
		console(B_TRUE, "Contract set template inherit, regent "
		    "failed: %s.\n", strerror(err));

	/*
	 * These errors result in a misconfigured template, which is better
	 * than no template at all, so warn but don't abort.
	 */
	if (err = ct_tmpl_set_informative(fd, info))
		console(B_TRUE, ioctl_tset_emsg, "informative", strerror(err));

	if (err = ct_tmpl_set_critical(fd, critical))
		console(B_TRUE, ioctl_tset_emsg, "critical", strerror(err));

	if (err = ct_pr_tmpl_set_fatal(fd, fatal))
		console(B_TRUE, ioctl_tset_emsg, "fatal", strerror(err));

	if (err = ct_tmpl_set_cookie(fd, cookie))
		console(B_TRUE, ioctl_tset_emsg, "cookie", strerror(err));

	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);

	return (fd);
}

/*
 * Create the templates and open an event file descriptor.  We use dup2(2) to
 * get these descriptors away from the stdin/stdout/stderr group.
 */
static void
contracts_init()
{
	int err, fd;

	/*
	 * Create & configure a legacy template.  We only want empty events so
	 * we know when to abandon them.
	 */
	legacy_tmpl = contract_make_template(0, CT_PR_EV_EMPTY, CT_PR_EV_HWERR,
	    ORDINARY_COOKIE);
	if (legacy_tmpl >= 0) {
		err = ct_tmpl_activate(legacy_tmpl);
		if (err != 0) {
			(void) close(legacy_tmpl);
			legacy_tmpl = -1;
			console(B_TRUE,
			    "Couldn't activate legacy template (%s); "
			    "legacy services will be in init's contract.\n",
			    strerror(err));
		}
	} else
		console(B_TRUE,
		    "Legacy services will be in init's contract.\n");

	if (dup2(legacy_tmpl, 255) == -1) {
		console(B_TRUE, "Could not duplicate legacy template: %s.\n",
		    strerror(errno));
	} else {
		(void) close(legacy_tmpl);
		legacy_tmpl = 255;
	}

	(void) fcntl(legacy_tmpl, F_SETFD, FD_CLOEXEC);

	startd_tmpl = contract_make_template(0, CT_PR_EV_EMPTY,
	    CT_PR_EV_HWERR | CT_PR_EV_SIGNAL | CT_PR_EV_CORE, STARTD_COOKIE);

	if (dup2(startd_tmpl, 254) == -1) {
		console(B_TRUE, "Could not duplicate startd template: %s.\n",
		    strerror(errno));
	} else {
		(void) close(startd_tmpl);
		startd_tmpl = 254;
	}

	(void) fcntl(startd_tmpl, F_SETFD, FD_CLOEXEC);

	if (legacy_tmpl < 0 && startd_tmpl < 0) {
		/* The creation errors have already been reported. */
		console(B_TRUE,
		    "Ignoring contract events.  Core smf(7) services will not "
		    "be restarted.\n");
		return;
	}

	/*
	 * Open an event endpoint.
	 */
	do
		fd = open64(CTFS_ROOT "/process/pbundle", O_RDONLY);
	while (fd < 0 && errno == EINTR)
		;
	if (fd < 0) {
		console(B_TRUE,
		    "Couldn't open process pbundle: %s.  Core smf(7) services "
		    "will not be restarted.\n", strerror(errno));
		return;
	}

	if (dup2(fd, 253) == -1) {
		console(B_TRUE, "Could not duplicate process bundle: %s.\n",
		    strerror(errno));
	} else {
		(void) close(fd);
		fd = 253;
	}

	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);

	/* Reset in case we've been restarted. */
	(void) ct_event_reset(fd);

	poll_fds[0].fd = fd;
	poll_fds[0].events = POLLIN;
	poll_nfds = 1;
}

static int
contract_getfile(ctid_t id, const char *name, int oflag)
{
	int fd;

	do
		fd = contract_open(id, "process", name, oflag);
	while (fd < 0 && errno == EINTR)
		;

	if (fd < 0)
		console(B_TRUE, "Couldn't open %s for contract %ld: %s.\n",
		    name, id, strerror(errno));

	return (fd);
}

static int
contract_cookie(ctid_t id, uint64_t *cp)
{
	int fd, err;
	ct_stathdl_t sh;

	fd = contract_getfile(id, "status", O_RDONLY);
	if (fd < 0)
		return (-1);

	err = ct_status_read(fd, CTD_COMMON, &sh);
	if (err != 0) {
		console(B_TRUE, "Couldn't read status of contract %ld: %s.\n",
		    id, strerror(err));
		(void) close(fd);
		return (-1);
	}

	(void) close(fd);

	*cp = ct_status_get_cookie(sh);

	ct_status_free(sh);
	return (0);
}

static void
contract_ack(ct_evthdl_t e)
{
	int fd;

	if (ct_event_get_flags(e) & CTE_INFO)
		return;

	fd = contract_getfile(ct_event_get_ctid(e), "ctl", O_WRONLY);
	if (fd < 0)
		return;

	(void) ct_ctl_ack(fd, ct_event_get_evid(e));
	(void) close(fd);
}

/*
 * Process a contract event.
 */
static void
contract_event(struct pollfd *poll)
{
	ct_evthdl_t e;
	int err;
	ctid_t ctid;

	if (!(poll->revents & POLLIN)) {
		if (poll->revents & POLLERR)
			console(B_TRUE,
			    "Unknown poll error on my process contract "
			    "pbundle.\n");
		return;
	}

	err = ct_event_read(poll->fd, &e);
	if (err != 0) {
		console(B_TRUE, "Error retrieving contract event: %s.\n",
		    strerror(err));
		return;
	}

	ctid = ct_event_get_ctid(e);

	if (ct_event_get_type(e) == CT_PR_EV_EMPTY) {
		uint64_t cookie;
		int ret, abandon = 1;

		/* If it's svc.startd, restart it.  Else, abandon. */
		ret = contract_cookie(ctid, &cookie);

		if (ret == 0) {
			if (cookie == STARTD_COOKIE &&
			    do_restart_startd) {
				console(B_TRUE, "Restarting svc.startd.\n");

				/*
				 * Account for the failure.  If the failure rate
				 * exceeds a threshold, then drop to maintenance
				 * mode.
				 */
				startd_record_failure();
				if (startd_failure_rate_critical())
					enter_maintenance();

				if (startd_tmpl < 0)
					console(B_TRUE,
					    "Restarting svc.startd in "
					    "improper contract (bad "
					    "template).\n");

				(void) startd_run(startd_cline, startd_tmpl,
				    ctid);

				abandon = 0;
			}
		}

		if (abandon && (err = contract_abandon_id(ctid))) {
			console(B_TRUE, "Couldn't abandon contract %ld: %s.\n",
			    ctid, strerror(err));
		}

		/*
		 * No need to acknowledge the event since either way the
		 * originating contract should be abandoned.
		 */
	} else {
		console(B_TRUE,
		    "Received contract event of unexpected type %d from "
		    "contract %ld.\n", ct_event_get_type(e), ctid);

		if ((ct_event_get_flags(e) & (CTE_INFO | CTE_ACK)) == 0)
			/* Allow unexpected critical events to be released. */
			contract_ack(e);
	}

	ct_event_free(e);
}

/*
 * svc.startd(8) Management
 */

/*
 * (Re)start svc.startd(8).  old_ctid should be the contract ID of the old
 * contract, or 0 if we're starting it for the first time.  If wait is true
 * we'll wait for and return the exit value of the child.
 */
static int
startd_run(const char *cline, int tmpl, ctid_t old_ctid)
{
	int err, i, ret, did_activate;
	pid_t pid;
	struct stat sb;

	if (cline[0] == '\0')
		return (-1);

	/*
	 * Don't restart startd if the system is rebooting or shutting down.
	 */
	do {
		ret = stat("/etc/svc/volatile/resetting", &sb);
	} while (ret == -1 && errno == EINTR);

	if (ret == 0) {
		if (smf_debug)
			console(B_TRUE, "Quiescing for reboot.\n");
		(void) pause();
		return (-1);
	}

	err = ct_pr_tmpl_set_transfer(tmpl, old_ctid);
	if (err == EINVAL) {
		console(B_TRUE, "Remake startd_tmpl; reattempt transfer.\n");
		tmpl = startd_tmpl = contract_make_template(0, CT_PR_EV_EMPTY,
		    CT_PR_EV_HWERR, STARTD_COOKIE);

		err = ct_pr_tmpl_set_transfer(tmpl, old_ctid);
	}
	if (err != 0) {
		console(B_TRUE,
		    "Couldn't set transfer parameter of contract template: "
		    "%s.\n", strerror(err));
	}

	if ((err = ct_pr_tmpl_set_svc_fmri(startd_tmpl,
	    SCF_SERVICE_STARTD)) != 0)
		console(B_TRUE,
		    "Can not set svc_fmri in contract template: %s\n",
		    strerror(err));
	if ((err = ct_pr_tmpl_set_svc_aux(startd_tmpl,
	    startd_svc_aux)) != 0)
		console(B_TRUE,
		    "Can not set svc_aux in contract template: %s\n",
		    strerror(err));
	did_activate = !(ct_tmpl_activate(tmpl));
	if (!did_activate)
		console(B_TRUE,
		    "Template activation failed; not starting \"%s\" in "
		    "proper contract.\n", cline);

	/* Hold SIGCLD so we can wait if necessary. */
	(void) sighold(SIGCLD);

	while ((pid = fork()) < 0) {
		if (errno == EPERM) {
			console(B_TRUE, "Insufficient permission to fork.\n");

			/* Now that's a doozy. */
			exit(1);
		}

		console(B_TRUE,
		    "fork() for svc.startd failed: %s.  Will retry in 1 "
		    "second...\n", strerror(errno));

		(void) sleep(1);

		/* Eventually give up? */
	}

	if (pid == 0) {
		/* child */
		struct statvfs64 sbuf;

		/*
		 * svc.configd needs some space (a few hundred KB) in / for its
		 * database. One common cause for startd failure is when
		 * configd dies because / is full. We don't want to go into the
		 * fast restart loop (startd_failure_rate_critical) and enter
		 * maintenance so we check for this case and slow down the
		 * failure rate so as to keep retrying in the hope space will
		 * free up.
		 */
		if (statvfs64("/", &sbuf) != -1 &&
		    (sbuf.f_bsize * sbuf.f_bfree) < ROOT_MIN_FREE) {
			syslog(LOG_ERR, "Insufficent space (%ld) in / to "
			    "start svc.startd.\n",
			    (long)(sbuf.f_bsize * sbuf.f_bfree));
			console(B_TRUE, "Insufficent space (%ld) in / to "
			    "start svc.startd.\n",
			    (long)(sbuf.f_bsize * sbuf.f_bfree));
			sleep(STARTD_THROTTLE_RETRY);
			exit(1);
		}

		/* See the comment in efork() */
		for (i = SIGHUP; i <= SIGRTMAX; ++i) {
			if (i == SIGTTOU || i == SIGTTIN || i == SIGTSTP)
				(void) sigset(i, SIG_IGN);
			else
				(void) sigset(i, SIG_DFL);
		}

		if (smf_options != NULL) {
			/* Put smf_options in the environment. */
			glob_envp[glob_envn] =
			    malloc(sizeof ("SMF_OPTIONS=") - 1 +
			    strlen(smf_options) + 1);

			if (glob_envp[glob_envn] != NULL) {
				/* LINTED */
				(void) sprintf(glob_envp[glob_envn],
				    "SMF_OPTIONS=%s", smf_options);
				glob_envp[glob_envn+1] = NULL;
			} else {
				console(B_TRUE,
				    "Could not set SMF_OPTIONS (%s).\n",
				    strerror(errno));
			}
		}

		if (smf_debug)
			console(B_TRUE, "Executing svc.startd\n");

		(void) execle(SH, "INITSH", "-c", cline, NULL, glob_envp);

		console(B_TRUE, "Could not exec \"%s\" (%s).\n", SH,
		    strerror(errno));

		exit(1);
	}

	/* parent */

	if (did_activate) {
		if (legacy_tmpl < 0 || ct_tmpl_activate(legacy_tmpl) != 0)
			(void) ct_tmpl_clear(tmpl);
	}

	/* Clear the old_ctid reference so the kernel can reclaim it. */
	if (old_ctid != 0)
		(void) ct_pr_tmpl_set_transfer(tmpl, 0);

	(void) sigrelse(SIGCLD);

	return (0);
}

/*
 * void startd_record_failure(void)
 *   Place the current time in our circular array of svc.startd failures.
 */
void
startd_record_failure()
{
	int index = startd_failure_index++ % NSTARTD_FAILURE_TIMES;

	startd_failure_time[index] = gethrtime();
}

/*
 * int startd_failure_rate_critical(void)
 *   Return true if the average failure interval is less than the permitted
 *   interval.  Implicit success if insufficient measurements for an average
 *   exist.
 */
int
startd_failure_rate_critical()
{
	int n = startd_failure_index;
	hrtime_t avg_ns = 0;

	if (startd_failure_index < NSTARTD_FAILURE_TIMES)
		return (0);

	avg_ns =
	    (startd_failure_time[(n - 1) % NSTARTD_FAILURE_TIMES] -
	    startd_failure_time[n % NSTARTD_FAILURE_TIMES]) /
	    NSTARTD_FAILURE_TIMES;

	return (avg_ns < STARTD_FAILURE_RATE_NS);
}

/*
 * returns string that must be free'd
 */

static char
*audit_boot_msg()
{
	char		*b, *p;
	char		desc[] = "booted";
	zoneid_t	zid = getzoneid();

	b = malloc(sizeof (desc) + MAXNAMELEN + 3);
	if (b == NULL)
		return (b);

	p = b;
	p += strlcpy(p, desc, sizeof (desc));
	if (zid != GLOBAL_ZONEID) {
		p += strlcpy(p, ": ", 3);
		(void) getzonenamebyid(zid, p, MAXNAMELEN);
	}
	return (b);
}

/*
 * Generate AUE_init_solaris audit record.  Return 1 if
 * auditing is enabled in case the caller cares.
 *
 * In the case of userint() or a local zone invocation of
 * one_true_init, the process initially contains the audit
 * characteristics of the process that invoked init.  The first pass
 * through here uses those characteristics then for the case of
 * one_true_init in a local zone, clears them so subsequent system
 * state changes won't be attributed to the person who booted the
 * zone.
 */
static int
audit_put_record(int pass_fail, int status, char *msg)
{
	adt_session_data_t	*ah;
	adt_event_data_t	*event;

	if (!adt_audit_enabled())
		return (0);

	/*
	 * the PROC_DATA picks up the context to tell whether this is
	 * an attributed record (auid = -2 is unattributed)
	 */
	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA)) {
		console(B_TRUE, "audit failure:  %s\n", strerror(errno));
		return (1);
	}
	event = adt_alloc_event(ah, ADT_init_solaris);
	if (event == NULL) {
		console(B_TRUE, "audit failure:  %s\n", strerror(errno));
		(void) adt_end_session(ah);
		return (1);
	}
	event->adt_init_solaris.info = msg;	/* NULL is ok here */

	if (adt_put_event(event, pass_fail, status)) {
		console(B_TRUE, "audit failure:  %s\n", strerror(errno));
		(void) adt_end_session(ah);
		return (1);
	}
	adt_free_event(event);

	(void) adt_end_session(ah);

	return (1);
}