summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/os/sig.c
blob: 453b1f22d4ed871cc0963ad4c4a9948680940c6f (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
/*
 * 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 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
 */

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

#include <sys/param.h>
#include <sys/types.h>
#include <sys/bitmap.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/poll_impl.h> /* only needed for kludge in sigwaiting_send() */
#include <sys/signal.h>
#include <sys/siginfo.h>
#include <sys/fault.h>
#include <sys/ucontext.h>
#include <sys/procfs.h>
#include <sys/wait.h>
#include <sys/class.h>
#include <sys/mman.h>
#include <sys/procset.h>
#include <sys/kmem.h>
#include <sys/cpuvar.h>
#include <sys/prsystm.h>
#include <sys/debug.h>
#include <vm/as.h>
#include <sys/bitmap.h>
#include <c2/audit.h>
#include <sys/core.h>
#include <sys/schedctl.h>
#include <sys/contract/process_impl.h>
#include <sys/cyclic.h>
#include <sys/dtrace.h>
#include <sys/sdt.h>
#include <sys/signalfd.h>

const k_sigset_t nullsmask = {0, 0, 0};

const k_sigset_t fillset =	/* MUST be contiguous */
	{FILLSET0, FILLSET1, FILLSET2};

const k_sigset_t cantmask =
	{CANTMASK0, CANTMASK1, CANTMASK2};

const k_sigset_t cantreset =
	{(sigmask(SIGILL)|sigmask(SIGTRAP)|sigmask(SIGPWR)), 0, 0};

const k_sigset_t ignoredefault =
	{(sigmask(SIGCONT)|sigmask(SIGCLD)|sigmask(SIGPWR)
	|sigmask(SIGWINCH)|sigmask(SIGURG)|sigmask(SIGWAITING)),
	(sigmask(SIGLWP)|sigmask(SIGCANCEL)|sigmask(SIGFREEZE)
	|sigmask(SIGTHAW)|sigmask(SIGXRES)|sigmask(SIGJVM1)
	|sigmask(SIGJVM2)|sigmask(SIGINFO)), 0};

const k_sigset_t stopdefault =
	{(sigmask(SIGSTOP)|sigmask(SIGTSTP)|sigmask(SIGTTOU)|sigmask(SIGTTIN)),
	0, 0};

const k_sigset_t coredefault =
	{(sigmask(SIGQUIT)|sigmask(SIGILL)|sigmask(SIGTRAP)|sigmask(SIGIOT)
	|sigmask(SIGEMT)|sigmask(SIGFPE)|sigmask(SIGBUS)|sigmask(SIGSEGV)
	|sigmask(SIGSYS)|sigmask(SIGXCPU)|sigmask(SIGXFSZ)), 0, 0};

const k_sigset_t holdvfork =
	{(sigmask(SIGTTOU)|sigmask(SIGTTIN)|sigmask(SIGTSTP)), 0, 0};

static	int	isjobstop(int);
static	void	post_sigcld(proc_t *, sigqueue_t *);


/*
 * signalfd helper function which is set when the signalfd driver loads.
 */
void (*sigfd_exit_helper)();

/*
 * Internal variables for counting number of user thread stop requests posted.
 * They may not be accurate at some special situation such as that a virtually
 * stopped thread starts to run.
 */
static int num_utstop;
/*
 * Internal variables for broadcasting an event when all thread stop requests
 * are processed.
 */
static kcondvar_t utstop_cv;

static kmutex_t thread_stop_lock;
void del_one_utstop(void);

/*
 * Send the specified signal to the specified process.
 */
void
psignal(proc_t *p, int sig)
{
	mutex_enter(&p->p_lock);
	sigtoproc(p, NULL, sig);
	mutex_exit(&p->p_lock);
}

/*
 * Send the specified signal to the specified thread.
 */
void
tsignal(kthread_t *t, int sig)
{
	proc_t *p = ttoproc(t);

	mutex_enter(&p->p_lock);
	sigtoproc(p, t, sig);
	mutex_exit(&p->p_lock);
}

int
signal_is_blocked(kthread_t *t, int sig)
{
	return (sigismember(&t->t_hold, sig) ||
	    (schedctl_sigblock(t) && !sigismember(&cantmask, sig)));
}

/*
 * Return true if the signal can safely be discarded on generation.
 * That is, if there is no need for the signal on the receiving end.
 * The answer is true if the process is a zombie or
 * if all of these conditions are true:
 *	the signal is being ignored
 *	the process is single-threaded
 *	the signal is not being traced by /proc
 * 	the signal is not blocked by the process
 *	the signal is not being accepted via sigwait()
 */
static int
sig_discardable(proc_t *p, int sig)
{
	kthread_t *t = p->p_tlist;

	return (t == NULL ||		/* if zombie or ... */
	    (sigismember(&p->p_ignore, sig) &&	/* signal is ignored */
	    t->t_forw == t &&			/* and single-threaded */
	    !tracing(p, sig) &&			/* and no /proc tracing */
	    !signal_is_blocked(t, sig) &&	/* and signal not blocked */
	    !sigismember(&t->t_sigwait, sig)));	/* and not being accepted */
}

/*
 * Return true if this thread is going to eat this signal soon.
 * Note that, if the signal is SIGKILL, we force stopped threads to be
 * set running (to make SIGKILL be a sure kill), but only if the process
 * is not currently locked by /proc (the P_PR_LOCK flag).  Code in /proc
 * relies on the fact that a process will not change shape while P_PR_LOCK
 * is set (it drops and reacquires p->p_lock while leaving P_PR_LOCK set).
 * We wish that we could simply call prbarrier() below, in sigtoproc(), to
 * ensure that the process is not locked by /proc, but prbarrier() drops
 * and reacquires p->p_lock and dropping p->p_lock here would be damaging.
 */
int
eat_signal(kthread_t *t, int sig)
{
	int rval = 0;
	ASSERT(THREAD_LOCK_HELD(t));

	/*
	 * Do not do anything if the target thread has the signal blocked.
	 */
	if (!signal_is_blocked(t, sig)) {
		t->t_sig_check = 1;	/* have thread do an issig */
		if (ISWAKEABLE(t) || ISWAITING(t)) {
			setrun_locked(t);
			rval = 1;
		} else if (t->t_state == TS_STOPPED && sig == SIGKILL &&
		    !(ttoproc(t)->p_proc_flag & P_PR_LOCK)) {
			ttoproc(t)->p_stopsig = 0;
			t->t_dtrace_stop = 0;
			t->t_schedflag |= TS_XSTART | TS_PSTART;
			setrun_locked(t);
		} else if (t != curthread && t->t_state == TS_ONPROC) {
			aston(t);	/* make it do issig promptly */
			if (t->t_cpu != CPU)
				poke_cpu(t->t_cpu->cpu_id);
			rval = 1;
		} else if (t->t_state == TS_RUN) {
			rval = 1;
		}
	}

	return (rval);
}

/*
 * Post a signal.
 * If a non-null thread pointer is passed, then post the signal
 * to the thread/lwp, otherwise post the signal to the process.
 */
void
sigtoproc(proc_t *p, kthread_t *t, int sig)
{
	kthread_t *tt;
	int ext = !(curproc->p_flag & SSYS) &&
	    (curproc->p_ct_process != p->p_ct_process);

	ASSERT(MUTEX_HELD(&p->p_lock));

	/* System processes don't get signals */
	if (sig <= 0 || sig >= NSIG || (p->p_flag & SSYS))
		return;

	/*
	 * Regardless of origin or directedness,
	 * SIGKILL kills all lwps in the process immediately
	 * and jobcontrol signals affect all lwps in the process.
	 */
	if (sig == SIGKILL) {
		p->p_flag |= SKILLED | (ext ? SEXTKILLED : 0);
		t = NULL;
	} else if (sig == SIGCONT) {
		/*
		 * The SSCONT flag will remain set until a stopping
		 * signal comes in (below).  This is harmless.
		 */
		p->p_flag |= SSCONT;
		sigdelq(p, NULL, SIGSTOP);
		sigdelq(p, NULL, SIGTSTP);
		sigdelq(p, NULL, SIGTTOU);
		sigdelq(p, NULL, SIGTTIN);
		sigdiffset(&p->p_sig, &stopdefault);
		sigdiffset(&p->p_extsig, &stopdefault);
		p->p_stopsig = 0;
		if ((tt = p->p_tlist) != NULL) {
			do {
				sigdelq(p, tt, SIGSTOP);
				sigdelq(p, tt, SIGTSTP);
				sigdelq(p, tt, SIGTTOU);
				sigdelq(p, tt, SIGTTIN);
				sigdiffset(&tt->t_sig, &stopdefault);
				sigdiffset(&tt->t_extsig, &stopdefault);
			} while ((tt = tt->t_forw) != p->p_tlist);
		}
		if ((tt = p->p_tlist) != NULL) {
			do {
				thread_lock(tt);
				if (tt->t_state == TS_STOPPED &&
				    tt->t_whystop == PR_JOBCONTROL) {
					tt->t_schedflag |= TS_XSTART;
					setrun_locked(tt);
				}
				thread_unlock(tt);
			} while ((tt = tt->t_forw) != p->p_tlist);
		}
	} else if (sigismember(&stopdefault, sig)) {
		/*
		 * This test has a race condition which we can't fix:
		 * By the time the stopping signal is received by
		 * the target process/thread, the signal handler
		 * and/or the detached state might have changed.
		 */
		if (PTOU(p)->u_signal[sig-1] == SIG_DFL &&
		    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned))
			p->p_flag &= ~SSCONT;
		sigdelq(p, NULL, SIGCONT);
		sigdelset(&p->p_sig, SIGCONT);
		sigdelset(&p->p_extsig, SIGCONT);
		if ((tt = p->p_tlist) != NULL) {
			do {
				sigdelq(p, tt, SIGCONT);
				sigdelset(&tt->t_sig, SIGCONT);
				sigdelset(&tt->t_extsig, SIGCONT);
			} while ((tt = tt->t_forw) != p->p_tlist);
		}
	}

	if (sig_discardable(p, sig)) {
		DTRACE_PROC3(signal__discard, kthread_t *, p->p_tlist,
		    proc_t *, p, int, sig);
		return;
	}

	if (t != NULL) {
		/*
		 * This is a directed signal, wake up the lwp.
		 */
		sigaddset(&t->t_sig, sig);
		if (ext)
			sigaddset(&t->t_extsig, sig);
		thread_lock(t);
		(void) eat_signal(t, sig);
		thread_unlock(t);
		DTRACE_PROC2(signal__send, kthread_t *, t, int, sig);
		if (p->p_sigfd != NULL && ((sigfd_proc_state_t *)
		    (p->p_sigfd))->sigfd_pollwake_cb != NULL)
			(*((sigfd_proc_state_t *)(p->p_sigfd))->
			    sigfd_pollwake_cb)(p, sig);

	} else if ((tt = p->p_tlist) != NULL) {
		/*
		 * Make sure that some lwp that already exists
		 * in the process fields the signal soon.
		 * Wake up an interruptibly sleeping lwp if necessary.
		 * For SIGKILL make all of the lwps see the signal;
		 * This is needed to guarantee a sure kill for processes
		 * with a mix of realtime and non-realtime threads.
		 */
		int su = 0;

		sigaddset(&p->p_sig, sig);
		if (ext)
			sigaddset(&p->p_extsig, sig);
		do {
			thread_lock(tt);
			if (eat_signal(tt, sig) && sig != SIGKILL) {
				thread_unlock(tt);
				break;
			}
			if (SUSPENDED(tt))
				su++;
			thread_unlock(tt);
		} while ((tt = tt->t_forw) != p->p_tlist);
		/*
		 * If the process is deadlocked, make somebody run and die.
		 */
		if (sig == SIGKILL && p->p_stat != SIDL &&
		    p->p_lwprcnt == 0 && p->p_lwpcnt == su &&
		    !(p->p_proc_flag & P_PR_LOCK)) {
			thread_lock(tt);
			p->p_lwprcnt++;
			tt->t_schedflag |= TS_CSTART;
			setrun_locked(tt);
			thread_unlock(tt);
		}

		DTRACE_PROC2(signal__send, kthread_t *, tt, int, sig);
		if (p->p_sigfd != NULL && ((sigfd_proc_state_t *)
		    (p->p_sigfd))->sigfd_pollwake_cb != NULL)
			(*((sigfd_proc_state_t *)(p->p_sigfd))->
			    sigfd_pollwake_cb)(p, sig);
	}
}

static int
isjobstop(int sig)
{
	proc_t *p = ttoproc(curthread);

	ASSERT(MUTEX_HELD(&p->p_lock));

	if (PTOU(curproc)->u_signal[sig-1] == SIG_DFL &&
	    sigismember(&stopdefault, sig)) {
		/*
		 * If SIGCONT has been posted since we promoted this signal
		 * from pending to current, then don't do a jobcontrol stop.
		 */
		if (!(p->p_flag & SSCONT) &&
		    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned) &&
		    curthread != p->p_agenttp) {
			sigqueue_t *sqp;

			stop(PR_JOBCONTROL, sig);
			mutex_exit(&p->p_lock);
			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
			mutex_enter(&pidlock);
			/*
			 * Only the first lwp to continue notifies the parent.
			 */
			if (p->p_pidflag & CLDCONT)
				siginfofree(sqp);
			else {
				p->p_pidflag |= CLDCONT;
				p->p_wcode = CLD_CONTINUED;
				p->p_wdata = SIGCONT;
				sigcld(p, sqp);
			}
			mutex_exit(&pidlock);
			mutex_enter(&p->p_lock);
		}
		return (1);
	}
	return (0);
}

/*
 * Returns true if the current process has a signal to process, and
 * the signal is not held.  The signal to process is put in p_cursig.
 * This is asked at least once each time a process enters the system
 * (though this can usually be done without actually calling issig by
 * checking the pending signal masks).  A signal does not do anything
 * directly to a process; it sets a flag that asks the process to do
 * something to itself.
 *
 * The "why" argument indicates the allowable side-effects of the call:
 *
 * FORREAL:  Extract the next pending signal from p_sig into p_cursig;
 * stop the process if a stop has been requested or if a traced signal
 * is pending.
 *
 * JUSTLOOKING:  Don't stop the process, just indicate whether or not
 * a signal might be pending (FORREAL is needed to tell for sure).
 *
 * XXX: Changes to the logic in these routines should be propagated
 * to lm_sigispending().  See bug 1201594.
 */

static int issig_forreal(void);
static int issig_justlooking(void);

int
issig(int why)
{
	ASSERT(why == FORREAL || why == JUSTLOOKING);

	return ((why == FORREAL)? issig_forreal() : issig_justlooking());
}


static int
issig_justlooking(void)
{
	kthread_t *t = curthread;
	klwp_t *lwp = ttolwp(t);
	proc_t *p = ttoproc(t);
	k_sigset_t set;

	/*
	 * This function answers the question:
	 * "Is there any reason to call issig_forreal()?"
	 *
	 * We have to answer the question w/o grabbing any locks
	 * because we are (most likely) being called after we
	 * put ourselves on the sleep queue.
	 */

	if (t->t_dtrace_stop | t->t_dtrace_sig)
		return (1);

	/*
	 * Another piece of complexity in this process.  When single-stepping a
	 * process, we don't want an intervening signal or TP_PAUSE request to
	 * suspend the current thread.  Otherwise, the controlling process will
	 * hang beacuse we will be stopped with TS_PSTART set in t_schedflag.
	 * We will trigger any remaining signals when we re-enter the kernel on
	 * the single step trap.
	 */
	if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP)
		return (0);

	if ((lwp->lwp_asleep && MUSTRETURN(p, t)) ||
	    (p->p_flag & (SEXITLWPS|SKILLED)) ||
	    (lwp->lwp_nostop == 0 &&
	    (p->p_stopsig | (p->p_flag & (SHOLDFORK1|SHOLDWATCH)) |
	    (t->t_proc_flag &
	    (TP_PRSTOP|TP_HOLDLWP|TP_CHKPT|TP_PAUSE)))) ||
	    lwp->lwp_cursig)
		return (1);

	if (p->p_flag & SVFWAIT)
		return (0);
	set = p->p_sig;
	sigorset(&set, &t->t_sig);
	if (schedctl_sigblock(t))	/* all blockable signals blocked */
		sigandset(&set, &cantmask);
	else
		sigdiffset(&set, &t->t_hold);
	if (p->p_flag & SVFORK)
		sigdiffset(&set, &holdvfork);

	if (!sigisempty(&set)) {
		int sig;

		for (sig = 1; sig < NSIG; sig++) {
			if (sigismember(&set, sig) &&
			    (tracing(p, sig) ||
			    sigismember(&t->t_sigwait, sig) ||
			    !sigismember(&p->p_ignore, sig))) {
				/*
				 * Don't promote a signal that will stop
				 * the process when lwp_nostop is set.
				 */
				if (!lwp->lwp_nostop ||
				    PTOU(p)->u_signal[sig-1] != SIG_DFL ||
				    !sigismember(&stopdefault, sig))
					return (1);
			}
		}
	}

	return (0);
}

static int
issig_forreal(void)
{
	int sig = 0, ext = 0;
	kthread_t *t = curthread;
	klwp_t *lwp = ttolwp(t);
	proc_t *p = ttoproc(t);
	int toproc = 0;
	int sigcld_found = 0;
	int nostop_break = 0;

	ASSERT(t->t_state == TS_ONPROC);

	mutex_enter(&p->p_lock);
	schedctl_finish_sigblock(t);

	if (t->t_dtrace_stop | t->t_dtrace_sig) {
		if (t->t_dtrace_stop) {
			/*
			 * If DTrace's "stop" action has been invoked on us,
			 * set TP_PRSTOP.
			 */
			t->t_proc_flag |= TP_PRSTOP;
		}

		if (t->t_dtrace_sig != 0) {
			k_siginfo_t info;

			/*
			 * Post the signal generated as the result of
			 * DTrace's "raise" action as a normal signal before
			 * the full-fledged signal checking begins.
			 */
			bzero(&info, sizeof (info));
			info.si_signo = t->t_dtrace_sig;
			info.si_code = SI_DTRACE;

			sigaddq(p, NULL, &info, KM_NOSLEEP);

			t->t_dtrace_sig = 0;
		}
	}

	for (;;) {
		if (p->p_flag & (SEXITLWPS|SKILLED)) {
			lwp->lwp_cursig = sig = SIGKILL;
			lwp->lwp_extsig = ext = (p->p_flag & SEXTKILLED) != 0;
			t->t_sig_check = 1;
			break;
		}

		/*
		 * Another piece of complexity in this process.  When
		 * single-stepping a process, we don't want an intervening
		 * signal or TP_PAUSE request to suspend the current thread.
		 * Otherwise, the controlling process will hang beacuse we will
		 * be stopped with TS_PSTART set in t_schedflag.  We will
		 * trigger any remaining signals when we re-enter the kernel on
		 * the single step trap.
		 */
		if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP) {
			sig = 0;
			break;
		}

		/*
		 * Hold the lwp here for watchpoint manipulation.
		 */
		if ((t->t_proc_flag & TP_PAUSE) && !lwp->lwp_nostop) {
			stop(PR_SUSPENDED, SUSPEND_PAUSE);
			continue;
		}

		if (lwp->lwp_asleep && MUSTRETURN(p, t)) {
			if ((sig = lwp->lwp_cursig) != 0) {
				/*
				 * Make sure we call ISSIG() in post_syscall()
				 * to re-validate this current signal.
				 */
				t->t_sig_check = 1;
			}
			break;
		}

		/*
		 * If the request is PR_CHECKPOINT, ignore the rest of signals
		 * or requests.  Honor other stop requests or signals later.
		 * Go back to top of loop here to check if an exit or hold
		 * event has occurred while stopped.
		 */
		if ((t->t_proc_flag & TP_CHKPT) && !lwp->lwp_nostop) {
			stop(PR_CHECKPOINT, 0);
			continue;
		}

		/*
		 * Honor SHOLDFORK1, SHOLDWATCH, and TP_HOLDLWP before dealing
		 * with signals or /proc.  Another lwp is executing fork1(),
		 * or is undergoing watchpoint activity (remapping a page),
		 * or is executing lwp_suspend() on this lwp.
		 * Again, go back to top of loop to check if an exit
		 * or hold event has occurred while stopped.
		 */
		if (((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
		    (t->t_proc_flag & TP_HOLDLWP)) && !lwp->lwp_nostop) {
			stop(PR_SUSPENDED, SUSPEND_NORMAL);
			continue;
		}

		/*
		 * Honor requested stop before dealing with the
		 * current signal; a debugger may change it.
		 * Do not want to go back to loop here since this is a special
		 * stop that means: make incremental progress before the next
		 * stop. The danger is that returning to top of loop would most
		 * likely drop the thread right back here to stop soon after it
		 * was continued, violating the incremental progress request.
		 */
		if ((t->t_proc_flag & TP_PRSTOP) && !lwp->lwp_nostop)
			stop(PR_REQUESTED, 0);

		/*
		 * If a debugger wants us to take a signal it will have
		 * left it in lwp->lwp_cursig.  If lwp_cursig has been cleared
		 * or if it's being ignored, we continue on looking for another
		 * signal.  Otherwise we return the specified signal, provided
		 * it's not a signal that causes a job control stop.
		 *
		 * When stopped on PR_JOBCONTROL, there is no current
		 * signal; we cancel lwp->lwp_cursig temporarily before
		 * calling isjobstop().  The current signal may be reset
		 * by a debugger while we are stopped in isjobstop().
		 *
		 * If the current thread is accepting the signal
		 * (via sigwait(), sigwaitinfo(), or sigtimedwait()),
		 * we allow the signal to be accepted, even if it is
		 * being ignored, and without causing a job control stop.
		 */
		if ((sig = lwp->lwp_cursig) != 0) {
			ext = lwp->lwp_extsig;
			lwp->lwp_cursig = 0;
			lwp->lwp_extsig = 0;
			if (sigismember(&t->t_sigwait, sig) ||
			    (!sigismember(&p->p_ignore, sig) &&
			    !isjobstop(sig))) {
				if (p->p_flag & (SEXITLWPS|SKILLED)) {
					sig = SIGKILL;
					ext = (p->p_flag & SEXTKILLED) != 0;
				}
				lwp->lwp_cursig = (uchar_t)sig;
				lwp->lwp_extsig = (uchar_t)ext;
				break;
			}
			/*
			 * The signal is being ignored or it caused a
			 * job-control stop.  If another current signal
			 * has not been established, return the current
			 * siginfo, if any, to the memory manager.
			 */
			if (lwp->lwp_cursig == 0 && lwp->lwp_curinfo != NULL) {
				siginfofree(lwp->lwp_curinfo);
				lwp->lwp_curinfo = NULL;
			}
			/*
			 * Loop around again in case we were stopped
			 * on a job control signal and a /proc stop
			 * request was posted or another current signal
			 * was established while we were stopped.
			 */
			continue;
		}

		if (p->p_stopsig && !lwp->lwp_nostop &&
		    curthread != p->p_agenttp) {
			/*
			 * Some lwp in the process has already stopped
			 * showing PR_JOBCONTROL.  This is a stop in
			 * sympathy with the other lwp, even if this
			 * lwp is blocking the stopping signal.
			 */
			stop(PR_JOBCONTROL, p->p_stopsig);
			continue;
		}

		/*
		 * Loop on the pending signals until we find a
		 * non-held signal that is traced or not ignored.
		 * First check the signals pending for the lwp,
		 * then the signals pending for the process as a whole.
		 */
		for (;;) {
			if ((sig = fsig(&t->t_sig, t)) != 0) {
				toproc = 0;
				if (tracing(p, sig) ||
				    sigismember(&t->t_sigwait, sig) ||
				    !sigismember(&p->p_ignore, sig)) {
					if (sigismember(&t->t_extsig, sig))
						ext = 1;
					break;
				}
				sigdelset(&t->t_sig, sig);
				sigdelset(&t->t_extsig, sig);
				sigdelq(p, t, sig);
			} else if ((sig = fsig(&p->p_sig, t)) != 0) {
				if (sig == SIGCLD)
					sigcld_found = 1;
				toproc = 1;
				if (tracing(p, sig) ||
				    sigismember(&t->t_sigwait, sig) ||
				    !sigismember(&p->p_ignore, sig)) {
					if (sigismember(&p->p_extsig, sig))
						ext = 1;
					break;
				}
				sigdelset(&p->p_sig, sig);
				sigdelset(&p->p_extsig, sig);
				sigdelq(p, NULL, sig);
			} else {
				/* no signal was found */
				break;
			}
		}

		if (sig == 0) {	/* no signal was found */
			if (p->p_flag & (SEXITLWPS|SKILLED)) {
				lwp->lwp_cursig = SIGKILL;
				sig = SIGKILL;
				ext = (p->p_flag & SEXTKILLED) != 0;
			}
			break;
		}

		/*
		 * If we have been informed not to stop (i.e., we are being
		 * called from within a network operation), then don't promote
		 * the signal at this time, just return the signal number.
		 * We will call issig() again later when it is safe.
		 *
		 * fsig() does not return a jobcontrol stopping signal
		 * with a default action of stopping the process if
		 * lwp_nostop is set, so we won't be causing a bogus
		 * EINTR by this action.  (Such a signal is eaten by
		 * isjobstop() when we loop around to do final checks.)
		 */
		if (lwp->lwp_nostop) {
			nostop_break = 1;
			break;
		}

		/*
		 * Promote the signal from pending to current.
		 *
		 * Note that sigdeq() will set lwp->lwp_curinfo to NULL
		 * if no siginfo_t exists for this signal.
		 */
		lwp->lwp_cursig = (uchar_t)sig;
		lwp->lwp_extsig = (uchar_t)ext;
		t->t_sig_check = 1;	/* so post_syscall will see signal */
		ASSERT(lwp->lwp_curinfo == NULL);
		sigdeq(p, toproc ? NULL : t, sig, &lwp->lwp_curinfo);

		if (tracing(p, sig))
			stop(PR_SIGNALLED, sig);

		/*
		 * Loop around to check for requested stop before
		 * performing the usual current-signal actions.
		 */
	}

	mutex_exit(&p->p_lock);

	/*
	 * If SIGCLD was dequeued from the process's signal queue,
	 * search for other pending SIGCLD's from the list of children.
	 */
	if (sigcld_found)
		sigcld_repost();

	if (sig != 0)
		(void) undo_watch_step(NULL);

	/*
	 * If we have been blocked since the p_lock was dropped off
	 * above, then this promoted signal might have been handled
	 * already when we were on the way back from sleep queue, so
	 * just ignore it.
	 * If we have been informed not to stop, just return the signal
	 * number. Also see comments above.
	 */
	if (!nostop_break) {
		sig = lwp->lwp_cursig;
	}

	return (sig != 0);
}

/*
 * Return true if the process is currently stopped showing PR_JOBCONTROL.
 * This is true only if all of the process's lwp's are so stopped.
 * If this is asked by one of the lwps in the process, exclude that lwp.
 */
int
jobstopped(proc_t *p)
{
	kthread_t *t;

	ASSERT(MUTEX_HELD(&p->p_lock));

	if ((t = p->p_tlist) == NULL)
		return (0);

	do {
		thread_lock(t);
		/* ignore current, zombie and suspended lwps in the test */
		if (!(t == curthread || t->t_state == TS_ZOMB ||
		    SUSPENDED(t)) &&
		    (t->t_state != TS_STOPPED ||
		    t->t_whystop != PR_JOBCONTROL)) {
			thread_unlock(t);
			return (0);
		}
		thread_unlock(t);
	} while ((t = t->t_forw) != p->p_tlist);

	return (1);
}

/*
 * Put ourself (curthread) into the stopped state and notify tracers.
 */
void
stop(int why, int what)
{
	kthread_t	*t = curthread;
	proc_t		*p = ttoproc(t);
	klwp_t		*lwp = ttolwp(t);
	kthread_t	*tx;
	lwpent_t	*lep;
	int		procstop;
	int		flags = TS_ALLSTART;
	hrtime_t	stoptime;

	/*
	 * Can't stop a system process.
	 */
	if (p == NULL || lwp == NULL || (p->p_flag & SSYS) || p->p_as == &kas)
		return;

	ASSERT(MUTEX_HELD(&p->p_lock));

	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
		/*
		 * Don't stop an lwp with SIGKILL pending.
		 * Don't stop if the process or lwp is exiting.
		 */
		if (lwp->lwp_cursig == SIGKILL ||
		    sigismember(&t->t_sig, SIGKILL) ||
		    sigismember(&p->p_sig, SIGKILL) ||
		    (t->t_proc_flag & TP_LWPEXIT) ||
		    (p->p_flag & (SEXITLWPS|SKILLED))) {
			p->p_stopsig = 0;
			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
			return;
		}
	}

	/*
	 * Make sure we don't deadlock on a recursive call to prstop().
	 * prstop() sets the lwp_nostop flag.
	 */
	if (lwp->lwp_nostop)
		return;

	/*
	 * Make sure the lwp is in an orderly state for inspection
	 * by a debugger through /proc or for dumping via core().
	 */
	schedctl_finish_sigblock(t);
	t->t_proc_flag |= TP_STOPPING;	/* must set before dropping p_lock */
	mutex_exit(&p->p_lock);
	stoptime = gethrtime();
	prstop(why, what);
	(void) undo_watch_step(NULL);
	mutex_enter(&p->p_lock);
	ASSERT(t->t_state == TS_ONPROC);

	switch (why) {
	case PR_CHECKPOINT:
		/*
		 * The situation may have changed since we dropped
		 * and reacquired p->p_lock. Double-check now
		 * whether we should stop or not.
		 */
		if (!(t->t_proc_flag & TP_CHKPT)) {
			t->t_proc_flag &= ~TP_STOPPING;
			return;
		}
		t->t_proc_flag &= ~TP_CHKPT;
		flags &= ~TS_RESUME;
		break;

	case PR_JOBCONTROL:
		ASSERT(what == SIGSTOP || what == SIGTSTP ||
		    what == SIGTTIN || what == SIGTTOU);
		flags &= ~TS_XSTART;
		break;

	case PR_SUSPENDED:
		ASSERT(what == SUSPEND_NORMAL || what == SUSPEND_PAUSE);
		/*
		 * The situation may have changed since we dropped
		 * and reacquired p->p_lock.  Double-check now
		 * whether we should stop or not.
		 */
		if (what == SUSPEND_PAUSE) {
			if (!(t->t_proc_flag & TP_PAUSE)) {
				t->t_proc_flag &= ~TP_STOPPING;
				return;
			}
			flags &= ~TS_UNPAUSE;
		} else {
			if (!((t->t_proc_flag & TP_HOLDLWP) ||
			    (p->p_flag & (SHOLDFORK|SHOLDFORK1|SHOLDWATCH)))) {
				t->t_proc_flag &= ~TP_STOPPING;
				return;
			}
			/*
			 * If SHOLDFORK is in effect and we are stopping
			 * while asleep (not at the top of the stack),
			 * we return now to allow the hold to take effect
			 * when we reach the top of the kernel stack.
			 */
			if (lwp->lwp_asleep && (p->p_flag & SHOLDFORK)) {
				t->t_proc_flag &= ~TP_STOPPING;
				return;
			}
			flags &= ~TS_CSTART;
		}
		break;

	default:	/* /proc stop */
		flags &= ~TS_PSTART;
		/*
		 * Do synchronous stop unless the async-stop flag is set.
		 * If why is PR_REQUESTED and t->t_dtrace_stop flag is set,
		 * then no debugger is present and we also do synchronous stop.
		 */
		if ((why != PR_REQUESTED || t->t_dtrace_stop) &&
		    !(p->p_proc_flag & P_PR_ASYNC)) {
			int notify;

			for (tx = t->t_forw; tx != t; tx = tx->t_forw) {
				notify = 0;
				thread_lock(tx);
				if (ISTOPPED(tx) ||
				    (tx->t_proc_flag & TP_PRSTOP)) {
					thread_unlock(tx);
					continue;
				}
				tx->t_proc_flag |= TP_PRSTOP;
				tx->t_sig_check = 1;
				if (tx->t_state == TS_SLEEP &&
				    (tx->t_flag & T_WAKEABLE)) {
					/*
					 * Don't actually wake it up if it's
					 * in one of the lwp_*() syscalls.
					 * Mark it virtually stopped and
					 * notify /proc waiters (below).
					 */
					if (tx->t_wchan0 == NULL)
						setrun_locked(tx);
					else {
						tx->t_proc_flag |= TP_PRVSTOP;
						tx->t_stoptime = stoptime;
						notify = 1;
					}
				}

				/* Move waiting thread to run queue */
				if (ISWAITING(tx))
					setrun_locked(tx);

				/*
				 * force the thread into the kernel
				 * if it is not already there.
				 */
				if (tx->t_state == TS_ONPROC &&
				    tx->t_cpu != CPU)
					poke_cpu(tx->t_cpu->cpu_id);
				thread_unlock(tx);
				lep = p->p_lwpdir[tx->t_dslot].ld_entry;
				if (notify && lep->le_trace)
					prnotify(lep->le_trace);
			}
			/*
			 * We do this just in case one of the threads we asked
			 * to stop is in holdlwps() (called from cfork()) or
			 * lwp_suspend().
			 */
			cv_broadcast(&p->p_holdlwps);
		}
		break;
	}

	t->t_stoptime = stoptime;

	if (why == PR_JOBCONTROL || (why == PR_SUSPENDED && p->p_stopsig)) {
		/*
		 * Determine if the whole process is jobstopped.
		 */
		if (jobstopped(p)) {
			sigqueue_t *sqp;
			int sig;

			if ((sig = p->p_stopsig) == 0)
				p->p_stopsig = (uchar_t)(sig = what);
			mutex_exit(&p->p_lock);
			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
			mutex_enter(&pidlock);
			/*
			 * The last lwp to stop notifies the parent.
			 * Turn off the CLDCONT flag now so the first
			 * lwp to continue knows what to do.
			 */
			p->p_pidflag &= ~CLDCONT;
			p->p_wcode = CLD_STOPPED;
			p->p_wdata = sig;
			sigcld(p, sqp);
			/*
			 * Grab p->p_lock before releasing pidlock so the
			 * parent and the child don't have a race condition.
			 */
			mutex_enter(&p->p_lock);
			mutex_exit(&pidlock);
			p->p_stopsig = 0;
		} else if (why == PR_JOBCONTROL && p->p_stopsig == 0) {
			/*
			 * Set p->p_stopsig and wake up sleeping lwps
			 * so they will stop in sympathy with this lwp.
			 */
			p->p_stopsig = (uchar_t)what;
			pokelwps(p);
			/*
			 * We do this just in case one of the threads we asked
			 * to stop is in holdlwps() (called from cfork()) or
			 * lwp_suspend().
			 */
			cv_broadcast(&p->p_holdlwps);
		}
	}

	if (why != PR_JOBCONTROL && why != PR_CHECKPOINT) {
		/*
		 * Do process-level notification when all lwps are
		 * either stopped on events of interest to /proc
		 * or are stopped showing PR_SUSPENDED or are zombies.
		 */
		procstop = 1;
		for (tx = t->t_forw; procstop && tx != t; tx = tx->t_forw) {
			if (VSTOPPED(tx))
				continue;
			thread_lock(tx);
			switch (tx->t_state) {
			case TS_ZOMB:
				break;
			case TS_STOPPED:
				/* neither ISTOPPED nor SUSPENDED? */
				if ((tx->t_schedflag &
				    (TS_CSTART | TS_UNPAUSE | TS_PSTART)) ==
				    (TS_CSTART | TS_UNPAUSE | TS_PSTART))
					procstop = 0;
				break;
			case TS_SLEEP:
				/* not paused for watchpoints? */
				if (!(tx->t_flag & T_WAKEABLE) ||
				    tx->t_wchan0 == NULL ||
				    !(tx->t_proc_flag & TP_PAUSE))
					procstop = 0;
				break;
			default:
				procstop = 0;
				break;
			}
			thread_unlock(tx);
		}
		if (procstop) {
			/* there must not be any remapped watched pages now */
			ASSERT(p->p_mapcnt == 0);
			if (p->p_proc_flag & P_PR_PTRACE) {
				/* ptrace() compatibility */
				mutex_exit(&p->p_lock);
				mutex_enter(&pidlock);
				p->p_wcode = CLD_TRAPPED;
				p->p_wdata = (why == PR_SIGNALLED)?
				    what : SIGTRAP;
				cv_broadcast(&p->p_parent->p_cv);
				/*
				 * Grab p->p_lock before releasing pidlock so
				 * parent and child don't have a race condition.
				 */
				mutex_enter(&p->p_lock);
				mutex_exit(&pidlock);
			}
			if (p->p_trace)			/* /proc */
				prnotify(p->p_trace);
			cv_broadcast(&pr_pid_cv[p->p_slot]); /* pauselwps() */
			cv_broadcast(&p->p_holdlwps);	/* holdwatch() */
		}
		if (why != PR_SUSPENDED) {
			lep = p->p_lwpdir[t->t_dslot].ld_entry;
			if (lep->le_trace)		/* /proc */
				prnotify(lep->le_trace);
			/*
			 * Special notification for creation of the agent lwp.
			 */
			if (t == p->p_agenttp &&
			    (t->t_proc_flag & TP_PRSTOP) &&
			    p->p_trace)
				prnotify(p->p_trace);
			/*
			 * The situation may have changed since we dropped
			 * and reacquired p->p_lock. Double-check now
			 * whether we should stop or not.
			 */
			if (!(t->t_proc_flag & TP_STOPPING)) {
				if (t->t_proc_flag & TP_PRSTOP)
					t->t_proc_flag |= TP_STOPPING;
			}
			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
			prnostep(lwp);
		}
	}

	if (why == PR_SUSPENDED) {

		/*
		 * We always broadcast in the case of SUSPEND_PAUSE.  This is
		 * because checks for TP_PAUSE take precedence over checks for
		 * SHOLDWATCH.  If a thread is trying to stop because of
		 * SUSPEND_PAUSE and tries to do a holdwatch(), it will be
		 * waiting for the rest of the threads to enter a stopped state.
		 * If we are stopping for a SUSPEND_PAUSE, we may be the last
		 * lwp and not know it, so broadcast just in case.
		 */
		if (what == SUSPEND_PAUSE ||
		    --p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP))
			cv_broadcast(&p->p_holdlwps);

	}

	/*
	 * Need to do this here (rather than after the thread is officially
	 * stopped) because we can't call mutex_enter from a stopped thread.
	 */
	if (why == PR_CHECKPOINT)
		del_one_utstop();

	thread_lock(t);
	ASSERT((t->t_schedflag & TS_ALLSTART) == 0);
	t->t_schedflag |= flags;
	t->t_whystop = (short)why;
	t->t_whatstop = (short)what;
	CL_STOP(t, why, what);
	(void) new_mstate(t, LMS_STOPPED);
	thread_stop(t);			/* set stop state and drop lock */

	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
		/*
		 * We may have gotten a SIGKILL or a SIGCONT when
		 * we released p->p_lock; make one last check.
		 * Also check for a /proc run-on-last-close.
		 */
		if (sigismember(&t->t_sig, SIGKILL) ||
		    sigismember(&p->p_sig, SIGKILL) ||
		    (t->t_proc_flag & TP_LWPEXIT) ||
		    (p->p_flag & (SEXITLWPS|SKILLED))) {
			p->p_stopsig = 0;
			thread_lock(t);
			t->t_schedflag |= TS_XSTART | TS_PSTART;
			setrun_locked(t);
			thread_unlock_nopreempt(t);
		} else if (why == PR_JOBCONTROL) {
			if (p->p_flag & SSCONT) {
				/*
				 * This resulted from a SIGCONT posted
				 * while we were not holding p->p_lock.
				 */
				p->p_stopsig = 0;
				thread_lock(t);
				t->t_schedflag |= TS_XSTART;
				setrun_locked(t);
				thread_unlock_nopreempt(t);
			}
		} else if (!(t->t_proc_flag & TP_STOPPING)) {
			/*
			 * This resulted from a /proc run-on-last-close.
			 */
			thread_lock(t);
			t->t_schedflag |= TS_PSTART;
			setrun_locked(t);
			thread_unlock_nopreempt(t);
		}
	}

	t->t_proc_flag &= ~TP_STOPPING;
	mutex_exit(&p->p_lock);

	swtch();
	setallwatch();	/* reestablish any watchpoints set while stopped */
	mutex_enter(&p->p_lock);
	prbarrier(p);	/* barrier against /proc locking */
}

/* Interface for resetting user thread stop count. */
void
utstop_init(void)
{
	mutex_enter(&thread_stop_lock);
	num_utstop = 0;
	mutex_exit(&thread_stop_lock);
}

/* Interface for registering a user thread stop request. */
void
add_one_utstop(void)
{
	mutex_enter(&thread_stop_lock);
	num_utstop++;
	mutex_exit(&thread_stop_lock);
}

/* Interface for cancelling a user thread stop request */
void
del_one_utstop(void)
{
	mutex_enter(&thread_stop_lock);
	num_utstop--;
	if (num_utstop == 0)
		cv_broadcast(&utstop_cv);
	mutex_exit(&thread_stop_lock);
}

/* Interface to wait for all user threads to be stopped */
void
utstop_timedwait(clock_t ticks)
{
	mutex_enter(&thread_stop_lock);
	if (num_utstop > 0)
		(void) cv_reltimedwait(&utstop_cv, &thread_stop_lock, ticks,
		    TR_CLOCK_TICK);
	mutex_exit(&thread_stop_lock);
}

/*
 * Perform the action specified by the current signal.
 * The usual sequence is:
 * 	if (issig())
 * 		psig();
 * The signal bit has already been cleared by issig(),
 * the current signal number has been stored in lwp_cursig,
 * and the current siginfo is now referenced by lwp_curinfo.
 */
void
psig(void)
{
	kthread_t *t = curthread;
	proc_t *p = ttoproc(t);
	klwp_t *lwp = ttolwp(t);
	void (*func)();
	int sig, rc, code, ext;
	pid_t pid = -1;
	id_t ctid = 0;
	zoneid_t zoneid = -1;
	sigqueue_t *sqp = NULL;
	uint32_t auditing = AU_AUDITING();

	mutex_enter(&p->p_lock);
	schedctl_finish_sigblock(t);
	code = CLD_KILLED;

	if (p->p_flag & SEXITLWPS) {
		lwp_exit();
		return;			/* not reached */
	}
	sig = lwp->lwp_cursig;
	ext = lwp->lwp_extsig;

	ASSERT(sig < NSIG);

	/*
	 * Re-check lwp_cursig after we acquire p_lock.  Since p_lock was
	 * dropped between issig() and psig(), a debugger may have cleared
	 * lwp_cursig via /proc in the intervening window.
	 */
	if (sig == 0) {
		if (lwp->lwp_curinfo) {
			siginfofree(lwp->lwp_curinfo);
			lwp->lwp_curinfo = NULL;
		}
		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
			t->t_flag &= ~T_TOMASK;
			t->t_hold = lwp->lwp_sigoldmask;
		}
		mutex_exit(&p->p_lock);
		return;
	}
	func = PTOU(curproc)->u_signal[sig-1];

	/*
	 * The signal disposition could have changed since we promoted
	 * this signal from pending to current (we dropped p->p_lock).
	 * This can happen only in a multi-threaded process.
	 */
	if (sigismember(&p->p_ignore, sig) ||
	    (func == SIG_DFL && sigismember(&stopdefault, sig))) {
		lwp->lwp_cursig = 0;
		lwp->lwp_extsig = 0;
		if (lwp->lwp_curinfo) {
			siginfofree(lwp->lwp_curinfo);
			lwp->lwp_curinfo = NULL;
		}
		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
			t->t_flag &= ~T_TOMASK;
			t->t_hold = lwp->lwp_sigoldmask;
		}
		mutex_exit(&p->p_lock);
		return;
	}

	/*
	 * We check lwp_curinfo first since pr_setsig can actually
	 * stuff a sigqueue_t there for SIGKILL.
	 */
	if (lwp->lwp_curinfo) {
		sqp = lwp->lwp_curinfo;
	} else if (sig == SIGKILL && p->p_killsqp) {
		sqp = p->p_killsqp;
	}

	if (sqp != NULL) {
		if (SI_FROMUSER(&sqp->sq_info)) {
			pid = sqp->sq_info.si_pid;
			ctid = sqp->sq_info.si_ctid;
			zoneid = sqp->sq_info.si_zoneid;
		}
		/*
		 * If we have a sigqueue_t, its sq_external value
		 * trumps the lwp_extsig value.  It is theoretically
		 * possible to make lwp_extsig reflect reality, but it
		 * would unnecessarily complicate things elsewhere.
		 */
		ext = sqp->sq_external;
	}

	if (func == SIG_DFL) {
		mutex_exit(&p->p_lock);
		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
		    NULL, void (*)(void), func);
	} else {
		k_siginfo_t *sip = NULL;

		/*
		 * If DTrace user-land tracing is active, give DTrace a
		 * chance to defer the signal until after tracing is
		 * complete.
		 */
		if (t->t_dtrace_on && dtrace_safe_defer_signal()) {
			mutex_exit(&p->p_lock);
			return;
		}

		/*
		 * save siginfo pointer here, in case the
		 * the signal's reset bit is on
		 *
		 * The presence of a current signal prevents paging
		 * from succeeding over a network.  We copy the current
		 * signal information to the side and cancel the current
		 * signal so that sendsig() will succeed.
		 */
		if (sigismember(&p->p_siginfo, sig)) {
			sip = &lwp->lwp_siginfo;
			if (sqp) {
				bcopy(&sqp->sq_info, sip, sizeof (*sip));
				/*
				 * If we were interrupted out of a system call
				 * due to pthread_cancel(), inform libc.
				 */
				if (sig == SIGCANCEL &&
				    sip->si_code == SI_LWP &&
				    t->t_sysnum != 0)
					schedctl_cancel_eintr();
			} else if (sig == SIGPROF && sip->si_signo == SIGPROF &&
			    t->t_rprof != NULL && t->t_rprof->rp_anystate) {
				/* EMPTY */;
			} else {
				bzero(sip, sizeof (*sip));
				sip->si_signo = sig;
				sip->si_code = SI_NOINFO;
			}
		}

		if (t->t_flag & T_TOMASK)
			t->t_flag &= ~T_TOMASK;
		else
			lwp->lwp_sigoldmask = t->t_hold;
		sigorset(&t->t_hold, &PTOU(curproc)->u_sigmask[sig-1]);
		if (!sigismember(&PTOU(curproc)->u_signodefer, sig))
			sigaddset(&t->t_hold, sig);
		if (sigismember(&PTOU(curproc)->u_sigresethand, sig))
			setsigact(sig, SIG_DFL, &nullsmask, 0);

		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
		    sip, void (*)(void), func);

		lwp->lwp_cursig = 0;
		lwp->lwp_extsig = 0;
		if (lwp->lwp_curinfo) {
			/* p->p_killsqp is freed by freeproc */
			siginfofree(lwp->lwp_curinfo);
			lwp->lwp_curinfo = NULL;
		}
		mutex_exit(&p->p_lock);
		lwp->lwp_ru.nsignals++;

		if (p->p_model == DATAMODEL_NATIVE)
			rc = sendsig(sig, sip, func);
#ifdef _SYSCALL32_IMPL
		else
			rc = sendsig32(sig, sip, func);
#endif	/* _SYSCALL32_IMPL */
		if (rc)
			return;
		sig = lwp->lwp_cursig = SIGSEGV;
		ext = 0;	/* lwp_extsig was set above */
		pid = -1;
		ctid = 0;
	}

	if (sigismember(&coredefault, sig)) {
		/*
		 * Terminate all LWPs but don't discard them.
		 * If another lwp beat us to the punch by calling exit(),
		 * evaporate now.
		 */
		proc_is_exiting(p);
		if (exitlwps(1) != 0) {
			mutex_enter(&p->p_lock);
			lwp_exit();
		}
		/* if we got a SIGKILL from anywhere, no core dump */
		if (p->p_flag & SKILLED) {
			sig = SIGKILL;
			ext = (p->p_flag & SEXTKILLED) != 0;
		} else {
			if (auditing)		/* audit core dump */
				audit_core_start(sig);
			if (core(sig, ext) == 0)
				code = CLD_DUMPED;
			if (auditing)		/* audit core dump */
				audit_core_finish(code);
		}
	}

	/*
	 * Generate a contract event once if the process is killed
	 * by a signal.
	 */
	if (ext) {
		proc_is_exiting(p);
		if (exitlwps(0) != 0) {
			mutex_enter(&p->p_lock);
			lwp_exit();
		}
		contract_process_sig(p->p_ct_process, p, sig, pid, ctid,
		    zoneid);
	}

	exit(code, sig);
}

/*
 * Find next unheld signal in ssp for thread t.
 */
int
fsig(k_sigset_t *ssp, kthread_t *t)
{
	proc_t *p = ttoproc(t);
	user_t *up = PTOU(p);
	int i;
	k_sigset_t temp;

	ASSERT(MUTEX_HELD(&p->p_lock));

	/*
	 * Don't promote any signals for the parent of a vfork()d
	 * child that hasn't yet released the parent's memory.
	 */
	if (p->p_flag & SVFWAIT)
		return (0);

	temp = *ssp;
	sigdiffset(&temp, &t->t_hold);

	/*
	 * Don't promote stopping signals (except SIGSTOP) for a child
	 * of vfork() that hasn't yet released the parent's memory.
	 */
	if (p->p_flag & SVFORK)
		sigdiffset(&temp, &holdvfork);

	/*
	 * Don't promote a signal that will stop
	 * the process when lwp_nostop is set.
	 */
	if (ttolwp(t)->lwp_nostop) {
		sigdelset(&temp, SIGSTOP);
		if (!p->p_pgidp->pid_pgorphaned) {
			if (up->u_signal[SIGTSTP-1] == SIG_DFL)
				sigdelset(&temp, SIGTSTP);
			if (up->u_signal[SIGTTIN-1] == SIG_DFL)
				sigdelset(&temp, SIGTTIN);
			if (up->u_signal[SIGTTOU-1] == SIG_DFL)
				sigdelset(&temp, SIGTTOU);
		}
	}

	/*
	 * Choose SIGKILL and SIGPROF before all other pending signals.
	 * The rest are promoted in signal number order.
	 */
	if (sigismember(&temp, SIGKILL))
		return (SIGKILL);
	if (sigismember(&temp, SIGPROF))
		return (SIGPROF);

	for (i = 0; i < sizeof (temp) / sizeof (temp.__sigbits[0]); i++) {
		if (temp.__sigbits[i])
			return ((i * NBBY * sizeof (temp.__sigbits[0])) +
			    lowbit(temp.__sigbits[i]));
	}

	return (0);
}

void
setsigact(int sig, void (*disp)(), const k_sigset_t *mask, int flags)
{
	proc_t *p = ttoproc(curthread);
	kthread_t *t;

	ASSERT(MUTEX_HELD(&p->p_lock));

	PTOU(curproc)->u_signal[sig - 1] = disp;

	/*
	 * Honor the SA_SIGINFO flag if the signal is being caught.
	 * Force the SA_SIGINFO flag if the signal is not being caught.
	 * This is necessary to make sigqueue() and sigwaitinfo() work
	 * properly together when the signal is set to default or is
	 * being temporarily ignored.
	 */
	if ((flags & SA_SIGINFO) || disp == SIG_DFL || disp == SIG_IGN)
		sigaddset(&p->p_siginfo, sig);
	else
		sigdelset(&p->p_siginfo, sig);

	if (disp != SIG_DFL && disp != SIG_IGN) {
		sigdelset(&p->p_ignore, sig);
		PTOU(curproc)->u_sigmask[sig - 1] = *mask;
		if (!sigismember(&cantreset, sig)) {
			if (flags & SA_RESETHAND)
				sigaddset(&PTOU(curproc)->u_sigresethand, sig);
			else
				sigdelset(&PTOU(curproc)->u_sigresethand, sig);
		}
		if (flags & SA_NODEFER)
			sigaddset(&PTOU(curproc)->u_signodefer, sig);
		else
			sigdelset(&PTOU(curproc)->u_signodefer, sig);
		if (flags & SA_RESTART)
			sigaddset(&PTOU(curproc)->u_sigrestart, sig);
		else
			sigdelset(&PTOU(curproc)->u_sigrestart, sig);
		if (flags & SA_ONSTACK)
			sigaddset(&PTOU(curproc)->u_sigonstack, sig);
		else
			sigdelset(&PTOU(curproc)->u_sigonstack, sig);
	} else if (disp == SIG_IGN ||
	    (disp == SIG_DFL && sigismember(&ignoredefault, sig))) {
		/*
		 * Setting the signal action to SIG_IGN results in the
		 * discarding of all pending signals of that signal number.
		 * Setting the signal action to SIG_DFL does the same *only*
		 * if the signal's default behavior is to be ignored.
		 */
		sigaddset(&p->p_ignore, sig);
		sigdelset(&p->p_sig, sig);
		sigdelset(&p->p_extsig, sig);
		sigdelq(p, NULL, sig);
		t = p->p_tlist;
		do {
			sigdelset(&t->t_sig, sig);
			sigdelset(&t->t_extsig, sig);
			sigdelq(p, t, sig);
		} while ((t = t->t_forw) != p->p_tlist);
	} else {
		/*
		 * The signal action is being set to SIG_DFL and the default
		 * behavior is to do something: make sure it is not ignored.
		 */
		sigdelset(&p->p_ignore, sig);
	}

	if (sig == SIGCLD) {
		if (flags & SA_NOCLDWAIT)
			p->p_flag |= SNOWAIT;
		else
			p->p_flag &= ~SNOWAIT;

		if (flags & SA_NOCLDSTOP)
			p->p_flag &= ~SJCTL;
		else
			p->p_flag |= SJCTL;

		if ((p->p_flag & SNOWAIT) || disp == SIG_IGN) {
			proc_t *cp, *tp;

			mutex_exit(&p->p_lock);
			mutex_enter(&pidlock);
			for (cp = p->p_child; cp != NULL; cp = tp) {
				tp = cp->p_sibling;
				if (cp->p_stat == SZOMB &&
				    !(cp->p_pidflag & CLDWAITPID))
					freeproc(cp);
			}
			mutex_exit(&pidlock);
			mutex_enter(&p->p_lock);
		}
	}
}

/*
 * Set all signal actions not already set to SIG_DFL or SIG_IGN to SIG_DFL.
 * Called from exec_common() for a process undergoing execve()
 * and from cfork() for a newly-created child of vfork().
 * In the vfork() case, 'p' is not the current process.
 * In both cases, there is only one thread in the process.
 */
void
sigdefault(proc_t *p)
{
	kthread_t *t = p->p_tlist;
	struct user *up = PTOU(p);
	int sig;

	ASSERT(MUTEX_HELD(&p->p_lock));

	for (sig = 1; sig < NSIG; sig++) {
		if (up->u_signal[sig - 1] != SIG_DFL &&
		    up->u_signal[sig - 1] != SIG_IGN) {
			up->u_signal[sig - 1] = SIG_DFL;
			sigemptyset(&up->u_sigmask[sig - 1]);
			if (sigismember(&ignoredefault, sig)) {
				sigdelq(p, NULL, sig);
				sigdelq(p, t, sig);
			}
			if (sig == SIGCLD)
				p->p_flag &= ~(SNOWAIT|SJCTL);
		}
	}
	sigorset(&p->p_ignore, &ignoredefault);
	sigfillset(&p->p_siginfo);
	sigdiffset(&p->p_siginfo, &cantmask);
	sigdiffset(&p->p_sig, &ignoredefault);
	sigdiffset(&p->p_extsig, &ignoredefault);
	sigdiffset(&t->t_sig, &ignoredefault);
	sigdiffset(&t->t_extsig, &ignoredefault);
}

void
sigcld(proc_t *cp, sigqueue_t *sqp)
{
	proc_t *pp = cp->p_parent;

	ASSERT(MUTEX_HELD(&pidlock));

	switch (cp->p_wcode) {
	case CLD_EXITED:
	case CLD_DUMPED:
	case CLD_KILLED:
		ASSERT(cp->p_stat == SZOMB);
		/*
		 * The broadcast on p_srwchan_cv is a kludge to
		 * wakeup a possible thread in uadmin(A_SHUTDOWN).
		 */
		cv_broadcast(&cp->p_srwchan_cv);

		/*
		 * Add to newstate list of the parent
		 */
		add_ns(pp, cp);

		cv_broadcast(&pp->p_cv);
		if ((pp->p_flag & SNOWAIT) ||
		    PTOU(pp)->u_signal[SIGCLD - 1] == SIG_IGN) {
			if (!(cp->p_pidflag & CLDWAITPID))
				freeproc(cp);
		} else if (!(cp->p_pidflag & CLDNOSIGCHLD)) {
			post_sigcld(cp, sqp);
			sqp = NULL;
		}
		break;

	case CLD_STOPPED:
	case CLD_CONTINUED:
		cv_broadcast(&pp->p_cv);
		if (pp->p_flag & SJCTL) {
			post_sigcld(cp, sqp);
			sqp = NULL;
		}
		break;
	}

	if (sqp)
		siginfofree(sqp);
}

/*
 * Common code called from sigcld() and from
 * waitid() and issig_forreal() via sigcld_repost().
 * Give the parent process a SIGCLD if it does not have one pending,
 * else mark the child process so a SIGCLD can be posted later.
 */
static void
post_sigcld(proc_t *cp, sigqueue_t *sqp)
{
	proc_t *pp = cp->p_parent;
	k_siginfo_t info;

	ASSERT(MUTEX_HELD(&pidlock));
	mutex_enter(&pp->p_lock);

	/*
	 * If a SIGCLD is pending, then just mark the child process
	 * so that its SIGCLD will be posted later, when the first
	 * SIGCLD is taken off the queue or when the parent is ready
	 * to receive it or accept it, if ever.
	 */
	if (sigismember(&pp->p_sig, SIGCLD)) {
		cp->p_pidflag |= CLDPEND;
	} else {
		cp->p_pidflag &= ~CLDPEND;
		if (sqp == NULL) {
			/*
			 * This can only happen when the parent is init.
			 * (See call to sigcld(q, NULL) in exit().)
			 * Use KM_NOSLEEP to avoid deadlock.
			 */
			ASSERT(pp == proc_init);
			winfo(cp, &info, 0);
			sigaddq(pp, NULL, &info, KM_NOSLEEP);
		} else {
			winfo(cp, &sqp->sq_info, 0);
			sigaddqa(pp, NULL, sqp);
			sqp = NULL;
		}
	}

	mutex_exit(&pp->p_lock);

	if (sqp)
		siginfofree(sqp);
}

/*
 * Search for a child that has a pending SIGCLD for us, the parent.
 * The queue of SIGCLD signals is implied by the list of children.
 * We post the SIGCLD signals one at a time so they don't get lost.
 * When one is dequeued, another is enqueued, until there are no more.
 */
void
sigcld_repost()
{
	proc_t *pp = curproc;
	proc_t *cp;
	sigqueue_t *sqp;

	sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
	mutex_enter(&pidlock);
	for (cp = pp->p_child; cp; cp = cp->p_sibling) {
		if (cp->p_pidflag & CLDPEND) {
			post_sigcld(cp, sqp);
			mutex_exit(&pidlock);
			return;
		}
	}
	mutex_exit(&pidlock);
	kmem_free(sqp, sizeof (sigqueue_t));
}

/*
 * count number of sigqueue send by sigaddqa()
 */
void
sigqsend(int cmd, proc_t *p, kthread_t *t, sigqueue_t *sigqp)
{
	sigqhdr_t *sqh;

	sqh = (sigqhdr_t *)sigqp->sq_backptr;
	ASSERT(sqh);

	mutex_enter(&sqh->sqb_lock);
	sqh->sqb_sent++;
	mutex_exit(&sqh->sqb_lock);

	if (cmd == SN_SEND)
		sigaddqa(p, t, sigqp);
	else
		siginfofree(sigqp);
}

int
sigsendproc(proc_t *p, sigsend_t *pv)
{
	struct cred *cr;
	proc_t *myprocp = curproc;

	ASSERT(MUTEX_HELD(&pidlock));

	if (p->p_pid == 1 && pv->sig && sigismember(&cantmask, pv->sig))
		return (EPERM);

	cr = CRED();

	if (pv->checkperm == 0 ||
	    (pv->sig == SIGCONT && p->p_sessp == myprocp->p_sessp) ||
	    prochasprocperm(p, myprocp, cr)) {
		pv->perm++;
		if (pv->sig) {
			/* Make sure we should be setting si_pid and friends */
			ASSERT(pv->sicode <= 0);
			if (SI_CANQUEUE(pv->sicode)) {
				sigqueue_t *sqp;

				mutex_enter(&myprocp->p_lock);
				sqp = sigqalloc(myprocp->p_sigqhdr);
				mutex_exit(&myprocp->p_lock);
				if (sqp == NULL)
					return (EAGAIN);
				sqp->sq_info.si_signo = pv->sig;
				sqp->sq_info.si_code = pv->sicode;
				sqp->sq_info.si_pid = myprocp->p_pid;
				sqp->sq_info.si_ctid = PRCTID(myprocp);
				sqp->sq_info.si_zoneid = getzoneid();
				sqp->sq_info.si_uid = crgetruid(cr);
				sqp->sq_info.si_value = pv->value;
				mutex_enter(&p->p_lock);
				sigqsend(SN_SEND, p, NULL, sqp);
				mutex_exit(&p->p_lock);
			} else {
				k_siginfo_t info;
				bzero(&info, sizeof (info));
				info.si_signo = pv->sig;
				info.si_code = pv->sicode;
				info.si_pid = myprocp->p_pid;
				info.si_ctid = PRCTID(myprocp);
				info.si_zoneid = getzoneid();
				info.si_uid = crgetruid(cr);
				mutex_enter(&p->p_lock);
				/*
				 * XXX: Should be KM_SLEEP but
				 * we have to avoid deadlock.
				 */
				sigaddq(p, NULL, &info, KM_NOSLEEP);
				mutex_exit(&p->p_lock);
			}
		}
	}

	return (0);
}

int
sigsendset(procset_t *psp, sigsend_t *pv)
{
	int error;

	error = dotoprocs(psp, sigsendproc, (char *)pv);
	if (error == 0 && pv->perm == 0)
		return (EPERM);

	return (error);
}

/*
 * Dequeue a queued siginfo structure.
 * If a non-null thread pointer is passed then dequeue from
 * the thread queue, otherwise dequeue from the process queue.
 */
void
sigdeq(proc_t *p, kthread_t *t, int sig, sigqueue_t **qpp)
{
	sigqueue_t **psqp, *sqp;

	ASSERT(MUTEX_HELD(&p->p_lock));

	*qpp = NULL;

	if (t != NULL) {
		sigdelset(&t->t_sig, sig);
		sigdelset(&t->t_extsig, sig);
		psqp = &t->t_sigqueue;
	} else {
		sigdelset(&p->p_sig, sig);
		sigdelset(&p->p_extsig, sig);
		psqp = &p->p_sigqueue;
	}

	for (;;) {
		if ((sqp = *psqp) == NULL)
			return;
		if (sqp->sq_info.si_signo == sig)
			break;
		else
			psqp = &sqp->sq_next;
	}
	*qpp = sqp;
	*psqp = sqp->sq_next;
	for (sqp = *psqp; sqp; sqp = sqp->sq_next) {
		if (sqp->sq_info.si_signo == sig) {
			if (t != (kthread_t *)NULL) {
				sigaddset(&t->t_sig, sig);
				t->t_sig_check = 1;
			} else {
				sigaddset(&p->p_sig, sig);
				set_proc_ast(p);
			}
			break;
		}
	}
}

/*
 * Delete a queued SIGCLD siginfo structure matching the k_siginfo_t argument.
 */
void
sigcld_delete(k_siginfo_t *ip)
{
	proc_t *p = curproc;
	int another_sigcld = 0;
	sigqueue_t **psqp, *sqp;

	ASSERT(ip->si_signo == SIGCLD);

	mutex_enter(&p->p_lock);

	if (!sigismember(&p->p_sig, SIGCLD)) {
		mutex_exit(&p->p_lock);
		return;
	}

	psqp = &p->p_sigqueue;
	for (;;) {
		if ((sqp = *psqp) == NULL) {
			mutex_exit(&p->p_lock);
			return;
		}
		if (sqp->sq_info.si_signo == SIGCLD) {
			if (sqp->sq_info.si_pid == ip->si_pid &&
			    sqp->sq_info.si_code == ip->si_code &&
			    sqp->sq_info.si_status == ip->si_status)
				break;
			another_sigcld = 1;
		}
		psqp = &sqp->sq_next;
	}
	*psqp = sqp->sq_next;

	siginfofree(sqp);

	for (sqp = *psqp; !another_sigcld && sqp; sqp = sqp->sq_next) {
		if (sqp->sq_info.si_signo == SIGCLD)
			another_sigcld = 1;
	}

	if (!another_sigcld) {
		sigdelset(&p->p_sig, SIGCLD);
		sigdelset(&p->p_extsig, SIGCLD);
	}

	mutex_exit(&p->p_lock);
}

/*
 * Delete queued siginfo structures.
 * If a non-null thread pointer is passed then delete from
 * the thread queue, otherwise delete from the process queue.
 */
void
sigdelq(proc_t *p, kthread_t *t, int sig)
{
	sigqueue_t **psqp, *sqp;

	/*
	 * We must be holding p->p_lock unless the process is
	 * being reaped or has failed to get started on fork.
	 */
	ASSERT(MUTEX_HELD(&p->p_lock) ||
	    p->p_stat == SIDL || p->p_stat == SZOMB);

	if (t != (kthread_t *)NULL)
		psqp = &t->t_sigqueue;
	else
		psqp = &p->p_sigqueue;

	while (*psqp) {
		sqp = *psqp;
		if (sig == 0 || sqp->sq_info.si_signo == sig) {
			*psqp = sqp->sq_next;
			siginfofree(sqp);
		} else
			psqp = &sqp->sq_next;
	}
}

/*
 * Insert a siginfo structure into a queue.
 * If a non-null thread pointer is passed then add to the thread queue,
 * otherwise add to the process queue.
 *
 * The function sigaddqins() is called with sigqueue already allocated.
 * It is called from sigaddqa() and sigaddq() below.
 *
 * The value of si_code implicitly indicates whether sigp is to be
 * explicitly queued, or to be queued to depth one.
 */
static void
sigaddqins(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
{
	sigqueue_t **psqp;
	int sig = sigqp->sq_info.si_signo;

	sigqp->sq_external = (curproc != &p0) &&
	    (curproc->p_ct_process != p->p_ct_process);

	/*
	 * issig_forreal() doesn't bother dequeueing signals if SKILLED
	 * is set, and even if it did, we would want to avoid situation
	 * (which would be unique to SIGKILL) where one thread dequeued
	 * the sigqueue_t and another executed psig().  So we create a
	 * separate stash for SIGKILL's sigqueue_t.  Because a second
	 * SIGKILL can set SEXTKILLED, we overwrite the existing entry
	 * if (and only if) it was non-extracontractual.
	 */
	if (sig == SIGKILL) {
		if (p->p_killsqp == NULL || !p->p_killsqp->sq_external) {
			if (p->p_killsqp != NULL)
				siginfofree(p->p_killsqp);
			p->p_killsqp = sigqp;
			sigqp->sq_next = NULL;
		} else {
			siginfofree(sigqp);
		}
		return;
	}

	ASSERT(sig >= 1 && sig < NSIG);
	if (t != NULL)	/* directed to a thread */
		psqp = &t->t_sigqueue;
	else 		/* directed to a process */
		psqp = &p->p_sigqueue;
	if (SI_CANQUEUE(sigqp->sq_info.si_code) &&
	    sigismember(&p->p_siginfo, sig)) {
		for (; *psqp != NULL; psqp = &(*psqp)->sq_next)
				;
	} else {
		for (; *psqp != NULL; psqp = &(*psqp)->sq_next) {
			if ((*psqp)->sq_info.si_signo == sig) {
				siginfofree(sigqp);
				return;
			}
		}
	}
	*psqp = sigqp;
	sigqp->sq_next = NULL;
}

/*
 * The function sigaddqa() is called with sigqueue already allocated.
 * If signal is ignored, discard but guarantee KILL and generation semantics.
 * It is called from sigqueue() and other places.
 */
void
sigaddqa(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
{
	int sig = sigqp->sq_info.si_signo;

	ASSERT(MUTEX_HELD(&p->p_lock));
	ASSERT(sig >= 1 && sig < NSIG);

	if (sig_discardable(p, sig))
		siginfofree(sigqp);
	else
		sigaddqins(p, t, sigqp);

	sigtoproc(p, t, sig);
}

/*
 * Allocate the sigqueue_t structure and call sigaddqins().
 */
void
sigaddq(proc_t *p, kthread_t *t, k_siginfo_t *infop, int km_flags)
{
	sigqueue_t *sqp;
	int sig = infop->si_signo;

	ASSERT(MUTEX_HELD(&p->p_lock));
	ASSERT(sig >= 1 && sig < NSIG);

	/*
	 * If the signal will be discarded by sigtoproc() or
	 * if the process isn't requesting siginfo and it isn't
	 * blocking the signal (it *could* change it's mind while
	 * the signal is pending) then don't bother creating one.
	 */
	if (!sig_discardable(p, sig) &&
	    (sigismember(&p->p_siginfo, sig) ||
	    (curproc->p_ct_process != p->p_ct_process) ||
	    (sig == SIGCLD && SI_FROMKERNEL(infop))) &&
	    ((sqp = kmem_alloc(sizeof (sigqueue_t), km_flags)) != NULL)) {
		bcopy(infop, &sqp->sq_info, sizeof (k_siginfo_t));
		sqp->sq_func = NULL;
		sqp->sq_next = NULL;
		sigaddqins(p, t, sqp);
	}
	sigtoproc(p, t, sig);
}

/*
 * Handle stop-on-fault processing for the debugger.  Returns 0
 * if the fault is cleared during the stop, nonzero if it isn't.
 */
int
stop_on_fault(uint_t fault, k_siginfo_t *sip)
{
	proc_t *p = ttoproc(curthread);
	klwp_t *lwp = ttolwp(curthread);

	ASSERT(prismember(&p->p_fltmask, fault));

	/*
	 * Record current fault and siginfo structure so debugger can
	 * find it.
	 */
	mutex_enter(&p->p_lock);
	lwp->lwp_curflt = (uchar_t)fault;
	lwp->lwp_siginfo = *sip;

	stop(PR_FAULTED, fault);

	fault = lwp->lwp_curflt;
	lwp->lwp_curflt = 0;
	mutex_exit(&p->p_lock);
	return (fault);
}

void
sigorset(k_sigset_t *s1, const k_sigset_t *s2)
{
	s1->__sigbits[0] |= s2->__sigbits[0];
	s1->__sigbits[1] |= s2->__sigbits[1];
	s1->__sigbits[2] |= s2->__sigbits[2];
}

void
sigandset(k_sigset_t *s1, const k_sigset_t *s2)
{
	s1->__sigbits[0] &= s2->__sigbits[0];
	s1->__sigbits[1] &= s2->__sigbits[1];
	s1->__sigbits[2] &= s2->__sigbits[2];
}

void
sigdiffset(k_sigset_t *s1, const k_sigset_t *s2)
{
	s1->__sigbits[0] &= ~(s2->__sigbits[0]);
	s1->__sigbits[1] &= ~(s2->__sigbits[1]);
	s1->__sigbits[2] &= ~(s2->__sigbits[2]);
}

/*
 * Return non-zero if curthread->t_sig_check should be set to 1, that is,
 * if there are any signals the thread might take on return from the kernel.
 * If ksigset_t's were a single word, we would do:
 *	return (((p->p_sig | t->t_sig) & ~t->t_hold) & fillset);
 */
int
sigcheck(proc_t *p, kthread_t *t)
{
	sc_shared_t *tdp = t->t_schedctl;

	/*
	 * If signals are blocked via the schedctl interface
	 * then we only check for the unmaskable signals.
	 * The unmaskable signal numbers should all be contained
	 * in __sigbits[0] and we assume this for speed.
	 */
#if (CANTMASK1 == 0 && CANTMASK2 == 0)
	if (tdp != NULL && tdp->sc_sigblock)
		return ((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
		    CANTMASK0);
#else
#error "fix me: CANTMASK1 and CANTMASK2 are not zero"
#endif

/* see uts/common/sys/signal.h for why this must be true */
#if ((MAXSIG > (2 * 32)) && (MAXSIG <= (3 * 32)))
	return (((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
	    ~t->t_hold.__sigbits[0]) |
	    ((p->p_sig.__sigbits[1] | t->t_sig.__sigbits[1]) &
	    ~t->t_hold.__sigbits[1]) |
	    (((p->p_sig.__sigbits[2] | t->t_sig.__sigbits[2]) &
	    ~t->t_hold.__sigbits[2]) & FILLSET2));
#else
#error "fix me: MAXSIG out of bounds"
#endif
}

void
sigintr(k_sigset_t *smask, int intable)
{
	proc_t *p;
	int owned;
	k_sigset_t lmask;		/* local copy of cantmask */
	klwp_t *lwp = ttolwp(curthread);

	/*
	 * Mask out all signals except SIGHUP, SIGINT, SIGQUIT
	 *    and SIGTERM. (Preserving the existing masks).
	 *    This function supports the -intr nfs and ufs mount option.
	 */

	/*
	 * don't do kernel threads
	 */
	if (lwp == NULL)
		return;

	/*
	 * get access to signal mask
	 */
	p = ttoproc(curthread);
	owned = mutex_owned(&p->p_lock);	/* this is filthy */
	if (!owned)
		mutex_enter(&p->p_lock);

	/*
	 * remember the current mask
	 */
	schedctl_finish_sigblock(curthread);
	*smask = curthread->t_hold;

	/*
	 * mask out all signals
	 */
	sigfillset(&curthread->t_hold);

	/*
	 * Unmask the non-maskable signals (e.g., KILL), as long as
	 * they aren't already masked (which could happen at exit).
	 * The first sigdiffset sets lmask to (cantmask & ~curhold).  The
	 * second sets the current hold mask to (~0 & ~lmask), which reduces
	 * to (~cantmask | curhold).
	 */
	lmask = cantmask;
	sigdiffset(&lmask, smask);
	sigdiffset(&curthread->t_hold, &lmask);

	/*
	 * Re-enable HUP, QUIT, and TERM iff they were originally enabled
	 * Re-enable INT if it's originally enabled and the NFS mount option
	 * nointr is not set.
	 */
	if (!sigismember(smask, SIGHUP))
		sigdelset(&curthread->t_hold, SIGHUP);
	if (!sigismember(smask, SIGINT) && intable)
		sigdelset(&curthread->t_hold, SIGINT);
	if (!sigismember(smask, SIGQUIT))
		sigdelset(&curthread->t_hold, SIGQUIT);
	if (!sigismember(smask, SIGTERM))
		sigdelset(&curthread->t_hold, SIGTERM);

	/*
	 * release access to signal mask
	 */
	if (!owned)
		mutex_exit(&p->p_lock);

	/*
	 * Indicate that this lwp is not to be stopped.
	 */
	lwp->lwp_nostop++;

}

void
sigunintr(k_sigset_t *smask)
{
	proc_t *p;
	int owned;
	klwp_t *lwp = ttolwp(curthread);

	/*
	 * Reset previous mask (See sigintr() above)
	 */
	if (lwp != NULL) {
		lwp->lwp_nostop--;	/* restore lwp stoppability */
		p = ttoproc(curthread);
		owned = mutex_owned(&p->p_lock);	/* this is filthy */
		if (!owned)
			mutex_enter(&p->p_lock);
		curthread->t_hold = *smask;
		/* so unmasked signals will be seen */
		curthread->t_sig_check = 1;
		if (!owned)
			mutex_exit(&p->p_lock);
	}
}

void
sigreplace(k_sigset_t *newmask, k_sigset_t *oldmask)
{
	proc_t	*p;
	int owned;
	/*
	 * Save current signal mask in oldmask, then
	 * set it to newmask.
	 */
	if (ttolwp(curthread) != NULL) {
		p = ttoproc(curthread);
		owned = mutex_owned(&p->p_lock);	/* this is filthy */
		if (!owned)
			mutex_enter(&p->p_lock);
		schedctl_finish_sigblock(curthread);
		if (oldmask != NULL)
			*oldmask = curthread->t_hold;
		curthread->t_hold = *newmask;
		curthread->t_sig_check = 1;
		if (!owned)
			mutex_exit(&p->p_lock);
	}
}

/*
 * Return true if the signal number is in range
 * and the signal code specifies signal queueing.
 */
int
sigwillqueue(int sig, int code)
{
	if (sig >= 0 && sig < NSIG) {
		switch (code) {
		case SI_QUEUE:
		case SI_TIMER:
		case SI_ASYNCIO:
		case SI_MESGQ:
			return (1);
		}
	}
	return (0);
}

/*
 * The pre-allocated pool (with _SIGQUEUE_PREALLOC entries) is
 * allocated at the first sigqueue/signotify call.
 */
sigqhdr_t *
sigqhdralloc(size_t size, uint_t maxcount)
{
	size_t i;
	sigqueue_t *sq, *next;
	sigqhdr_t *sqh;

	/*
	 * Before the introduction of process.max-sigqueue-size
	 * _SC_SIGQUEUE_MAX had this static value.
	 */
#define	_SIGQUEUE_PREALLOC	32

	i = (_SIGQUEUE_PREALLOC * size) + sizeof (sigqhdr_t);
	ASSERT(maxcount <= INT_MAX);
	sqh = kmem_alloc(i, KM_SLEEP);
	sqh->sqb_count = maxcount;
	sqh->sqb_maxcount = maxcount;
	sqh->sqb_size = i;
	sqh->sqb_pexited = 0;
	sqh->sqb_sent = 0;
	sqh->sqb_free = sq = (sigqueue_t *)(sqh + 1);
	for (i = _SIGQUEUE_PREALLOC - 1; i != 0; i--) {
		next = (sigqueue_t *)((uintptr_t)sq + size);
		sq->sq_next = next;
		sq = next;
	}
	sq->sq_next = NULL;
	cv_init(&sqh->sqb_cv, NULL, CV_DEFAULT, NULL);
	mutex_init(&sqh->sqb_lock, NULL, MUTEX_DEFAULT, NULL);
	return (sqh);
}

static void sigqrel(sigqueue_t *);

/*
 * Allocate a sigqueue/signotify structure from the per process
 * pre-allocated pool or allocate a new sigqueue/signotify structure
 * if the pre-allocated pool is exhausted.
 */
sigqueue_t *
sigqalloc(sigqhdr_t *sqh)
{
	sigqueue_t *sq = NULL;

	ASSERT(MUTEX_HELD(&curproc->p_lock));

	if (sqh != NULL) {
		mutex_enter(&sqh->sqb_lock);
		if (sqh->sqb_count > 0) {
			sqh->sqb_count--;
			if (sqh->sqb_free == NULL) {
				/*
				 * The pre-allocated pool is exhausted.
				 */
				sq = kmem_alloc(sizeof (sigqueue_t), KM_SLEEP);
				sq->sq_func = NULL;
			} else {
				sq = sqh->sqb_free;
				sq->sq_func = sigqrel;
				sqh->sqb_free = sq->sq_next;
			}
			mutex_exit(&sqh->sqb_lock);
			bzero(&sq->sq_info, sizeof (k_siginfo_t));
			sq->sq_backptr = sqh;
			sq->sq_next = NULL;
			sq->sq_external = 0;
		} else {
			mutex_exit(&sqh->sqb_lock);
		}
	}
	return (sq);
}

/*
 * Return a sigqueue structure back to the pre-allocated pool.
 */
static void
sigqrel(sigqueue_t *sq)
{
	sigqhdr_t *sqh;

	/* make sure that p_lock of the affected process is held */

	sqh = (sigqhdr_t *)sq->sq_backptr;
	mutex_enter(&sqh->sqb_lock);
	if (sqh->sqb_pexited && sqh->sqb_sent == 1) {
		mutex_exit(&sqh->sqb_lock);
		cv_destroy(&sqh->sqb_cv);
		mutex_destroy(&sqh->sqb_lock);
		kmem_free(sqh, sqh->sqb_size);
	} else {
		sqh->sqb_count++;
		sqh->sqb_sent--;
		sq->sq_next = sqh->sqb_free;
		sq->sq_backptr = NULL;
		sqh->sqb_free = sq;
		cv_signal(&sqh->sqb_cv);
		mutex_exit(&sqh->sqb_lock);
	}
}

/*
 * Free up the pre-allocated sigqueue headers of sigqueue pool
 * and signotify pool, if possible.
 * Called only by the owning process during exec() and exit().
 */
void
sigqfree(proc_t *p)
{
	ASSERT(MUTEX_HELD(&p->p_lock));

	if (p->p_sigqhdr != NULL) {	/* sigqueue pool */
		sigqhdrfree(p->p_sigqhdr);
		p->p_sigqhdr = NULL;
	}
	if (p->p_signhdr != NULL) {	/* signotify pool */
		sigqhdrfree(p->p_signhdr);
		p->p_signhdr = NULL;
	}
}

/*
 * Free up the pre-allocated header and sigq pool if possible.
 */
void
sigqhdrfree(sigqhdr_t *sqh)
{
	mutex_enter(&sqh->sqb_lock);
	if (sqh->sqb_sent == 0) {
		mutex_exit(&sqh->sqb_lock);
		cv_destroy(&sqh->sqb_cv);
		mutex_destroy(&sqh->sqb_lock);
		kmem_free(sqh, sqh->sqb_size);
	} else {
		sqh->sqb_pexited = 1;
		mutex_exit(&sqh->sqb_lock);
	}
}

/*
 * Free up a single sigqueue structure.
 * No other code should free a sigqueue directly.
 */
void
siginfofree(sigqueue_t *sqp)
{
	if (sqp != NULL) {
		if (sqp->sq_func != NULL)
			(sqp->sq_func)(sqp);
		else
			kmem_free(sqp, sizeof (sigqueue_t));
	}
}

/*
 * Generate a synchronous signal caused by a hardware
 * condition encountered by an lwp.  Called from trap().
 */
void
trapsig(k_siginfo_t *ip, int restartable)
{
	proc_t *p = ttoproc(curthread);
	int sig = ip->si_signo;
	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);

	ASSERT(sig > 0 && sig < NSIG);

	if (curthread->t_dtrace_on)
		dtrace_safe_synchronous_signal();

	mutex_enter(&p->p_lock);
	schedctl_finish_sigblock(curthread);
	/*
	 * Avoid a possible infinite loop if the lwp is holding the
	 * signal generated by a trap of a restartable instruction or
	 * if the signal so generated is being ignored by the process.
	 */
	if (restartable &&
	    (sigismember(&curthread->t_hold, sig) ||
	    p->p_user.u_signal[sig-1] == SIG_IGN)) {
		sigdelset(&curthread->t_hold, sig);
		p->p_user.u_signal[sig-1] = SIG_DFL;
		sigdelset(&p->p_ignore, sig);
	}
	bcopy(ip, &sqp->sq_info, sizeof (k_siginfo_t));
	sigaddqa(p, curthread, sqp);
	mutex_exit(&p->p_lock);
}

/*
 * Dispatch the real time profiling signal in the traditional way,
 * honoring all of the /proc tracing mechanism built into issig().
 */
static void
realsigprof_slow(int sysnum, int nsysarg, int error)
{
	kthread_t *t = curthread;
	proc_t *p = ttoproc(t);
	klwp_t *lwp = ttolwp(t);
	k_siginfo_t *sip = &lwp->lwp_siginfo;
	void (*func)();

	mutex_enter(&p->p_lock);
	func = PTOU(p)->u_signal[SIGPROF - 1];
	if (p->p_rprof_cyclic == CYCLIC_NONE ||
	    func == SIG_DFL || func == SIG_IGN) {
		bzero(t->t_rprof, sizeof (*t->t_rprof));
		mutex_exit(&p->p_lock);
		return;
	}
	if (sigismember(&t->t_hold, SIGPROF)) {
		mutex_exit(&p->p_lock);
		return;
	}
	sip->si_signo = SIGPROF;
	sip->si_code = PROF_SIG;
	sip->si_errno = error;
	hrt2ts(gethrtime(), &sip->si_tstamp);
	sip->si_syscall = sysnum;
	sip->si_nsysarg = nsysarg;
	sip->si_fault = lwp->lwp_lastfault;
	sip->si_faddr = lwp->lwp_lastfaddr;
	lwp->lwp_lastfault = 0;
	lwp->lwp_lastfaddr = NULL;
	sigtoproc(p, t, SIGPROF);
	mutex_exit(&p->p_lock);
	ASSERT(lwp->lwp_cursig == 0);
	if (issig(FORREAL))
		psig();
	sip->si_signo = 0;
	bzero(t->t_rprof, sizeof (*t->t_rprof));
}

/*
 * We are not tracing the SIGPROF signal, or doing any other unnatural
 * acts, like watchpoints, so dispatch the real time profiling signal
 * directly, bypassing all of the overhead built into issig().
 */
static void
realsigprof_fast(int sysnum, int nsysarg, int error)
{
	kthread_t *t = curthread;
	proc_t *p = ttoproc(t);
	klwp_t *lwp = ttolwp(t);
	k_siginfo_t *sip = &lwp->lwp_siginfo;
	void (*func)();
	int rc;
	int code;

	/*
	 * We don't need to acquire p->p_lock here;
	 * we are manipulating thread-private data.
	 */
	func = PTOU(p)->u_signal[SIGPROF - 1];
	if (p->p_rprof_cyclic == CYCLIC_NONE ||
	    func == SIG_DFL || func == SIG_IGN) {
		bzero(t->t_rprof, sizeof (*t->t_rprof));
		return;
	}
	if (lwp->lwp_cursig != 0 ||
	    lwp->lwp_curinfo != NULL ||
	    sigismember(&t->t_hold, SIGPROF)) {
		return;
	}
	sip->si_signo = SIGPROF;
	sip->si_code = PROF_SIG;
	sip->si_errno = error;
	hrt2ts(gethrtime(), &sip->si_tstamp);
	sip->si_syscall = sysnum;
	sip->si_nsysarg = nsysarg;
	sip->si_fault = lwp->lwp_lastfault;
	sip->si_faddr = lwp->lwp_lastfaddr;
	lwp->lwp_lastfault = 0;
	lwp->lwp_lastfaddr = NULL;
	if (t->t_flag & T_TOMASK)
		t->t_flag &= ~T_TOMASK;
	else
		lwp->lwp_sigoldmask = t->t_hold;
	sigorset(&t->t_hold, &PTOU(p)->u_sigmask[SIGPROF - 1]);
	if (!sigismember(&PTOU(p)->u_signodefer, SIGPROF))
		sigaddset(&t->t_hold, SIGPROF);
	lwp->lwp_extsig = 0;
	lwp->lwp_ru.nsignals++;
	if (p->p_model == DATAMODEL_NATIVE)
		rc = sendsig(SIGPROF, sip, func);
#ifdef _SYSCALL32_IMPL
	else
		rc = sendsig32(SIGPROF, sip, func);
#endif	/* _SYSCALL32_IMPL */
	sip->si_signo = 0;
	bzero(t->t_rprof, sizeof (*t->t_rprof));
	if (rc == 0) {
		/*
		 * sendsig() failed; we must dump core with a SIGSEGV.
		 * See psig().  This code is copied from there.
		 */
		lwp->lwp_cursig = SIGSEGV;
		code = CLD_KILLED;
		proc_is_exiting(p);
		if (exitlwps(1) != 0) {
			mutex_enter(&p->p_lock);
			lwp_exit();
		}
		if (audit_active == C2AUDIT_LOADED)
			audit_core_start(SIGSEGV);
		if (core(SIGSEGV, 0) == 0)
			code = CLD_DUMPED;
		if (audit_active == C2AUDIT_LOADED)
			audit_core_finish(code);
		exit(code, SIGSEGV);
	}
}

/*
 * Arrange for the real time profiling signal to be dispatched.
 */
void
realsigprof(int sysnum, int nsysarg, int error)
{
	kthread_t *t = curthread;
	proc_t *p = ttoproc(t);

	if (t->t_rprof->rp_anystate == 0)
		return;

	schedctl_finish_sigblock(t);

	/* test for any activity that requires p->p_lock */
	if (tracing(p, SIGPROF) || pr_watch_active(p) ||
	    sigismember(&PTOU(p)->u_sigresethand, SIGPROF)) {
		/* do it the classic slow way */
		realsigprof_slow(sysnum, nsysarg, error);
	} else {
		/* do it the cheating-a-little fast way */
		realsigprof_fast(sysnum, nsysarg, error);
	}
}

#ifdef _SYSCALL32_IMPL

/*
 * It's tricky to transmit a sigval between 32-bit and 64-bit
 * process, since in the 64-bit world, a pointer and an integer
 * are different sizes.  Since we're constrained by the standards
 * world not to change the types, and it's unclear how useful it is
 * to send pointers between address spaces this way, we preserve
 * the 'int' interpretation for 32-bit processes interoperating
 * with 64-bit processes.  The full semantics (pointers or integers)
 * are available for N-bit processes interoperating with N-bit
 * processes.
 */
void
siginfo_kto32(const k_siginfo_t *src, siginfo32_t *dest)
{
	bzero(dest, sizeof (*dest));

	/*
	 * The absolute minimum content is si_signo and si_code.
	 */
	dest->si_signo = src->si_signo;
	if ((dest->si_code = src->si_code) == SI_NOINFO)
		return;

	/*
	 * A siginfo generated by user level is structured
	 * differently from one generated by the kernel.
	 */
	if (SI_FROMUSER(src)) {
		dest->si_pid = src->si_pid;
		dest->si_ctid = src->si_ctid;
		dest->si_zoneid = src->si_zoneid;
		dest->si_uid = src->si_uid;
		if (SI_CANQUEUE(src->si_code))
			dest->si_value.sival_int =
			    (int32_t)src->si_value.sival_int;
		return;
	}

	dest->si_errno = src->si_errno;

	switch (src->si_signo) {
	default:
		dest->si_pid = src->si_pid;
		dest->si_ctid = src->si_ctid;
		dest->si_zoneid = src->si_zoneid;
		dest->si_uid = src->si_uid;
		dest->si_value.sival_int = (int32_t)src->si_value.sival_int;
		break;
	case SIGCLD:
		dest->si_pid = src->si_pid;
		dest->si_ctid = src->si_ctid;
		dest->si_zoneid = src->si_zoneid;
		dest->si_status = src->si_status;
		dest->si_stime = src->si_stime;
		dest->si_utime = src->si_utime;
		break;
	case SIGSEGV:
	case SIGBUS:
	case SIGILL:
	case SIGTRAP:
	case SIGFPE:
	case SIGEMT:
		dest->si_addr = (caddr32_t)(uintptr_t)src->si_addr;
		dest->si_trapno = src->si_trapno;
		dest->si_pc = (caddr32_t)(uintptr_t)src->si_pc;
		break;
	case SIGPOLL:
	case SIGXFSZ:
		dest->si_fd = src->si_fd;
		dest->si_band = src->si_band;
		break;
	case SIGPROF:
		dest->si_faddr = (caddr32_t)(uintptr_t)src->si_faddr;
		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
		dest->si_syscall = src->si_syscall;
		dest->si_nsysarg = src->si_nsysarg;
		dest->si_fault = src->si_fault;
		break;
	}
}

void
siginfo_32tok(const siginfo32_t *src, k_siginfo_t *dest)
{
	bzero(dest, sizeof (*dest));

	/*
	 * The absolute minimum content is si_signo and si_code.
	 */
	dest->si_signo = src->si_signo;
	if ((dest->si_code = src->si_code) == SI_NOINFO)
		return;

	/*
	 * A siginfo generated by user level is structured
	 * differently from one generated by the kernel.
	 */
	if (SI_FROMUSER(src)) {
		dest->si_pid = src->si_pid;
		dest->si_ctid = src->si_ctid;
		dest->si_zoneid = src->si_zoneid;
		dest->si_uid = src->si_uid;
		if (SI_CANQUEUE(src->si_code))
			dest->si_value.sival_int =
			    (int)src->si_value.sival_int;
		return;
	}

	dest->si_errno = src->si_errno;

	switch (src->si_signo) {
	default:
		dest->si_pid = src->si_pid;
		dest->si_ctid = src->si_ctid;
		dest->si_zoneid = src->si_zoneid;
		dest->si_uid = src->si_uid;
		dest->si_value.sival_int = (int)src->si_value.sival_int;
		break;
	case SIGCLD:
		dest->si_pid = src->si_pid;
		dest->si_ctid = src->si_ctid;
		dest->si_zoneid = src->si_zoneid;
		dest->si_status = src->si_status;
		dest->si_stime = src->si_stime;
		dest->si_utime = src->si_utime;
		break;
	case SIGSEGV:
	case SIGBUS:
	case SIGILL:
	case SIGTRAP:
	case SIGFPE:
	case SIGEMT:
		dest->si_addr = (void *)(uintptr_t)src->si_addr;
		dest->si_trapno = src->si_trapno;
		dest->si_pc = (void *)(uintptr_t)src->si_pc;
		break;
	case SIGPOLL:
	case SIGXFSZ:
		dest->si_fd = src->si_fd;
		dest->si_band = src->si_band;
		break;
	case SIGPROF:
		dest->si_faddr = (void *)(uintptr_t)src->si_faddr;
		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
		dest->si_syscall = src->si_syscall;
		dest->si_nsysarg = src->si_nsysarg;
		dest->si_fault = src->si_fault;
		break;
	}
}

#endif /* _SYSCALL32_IMPL */