|
root / base / usr / src / lib / libsysevent
libsysevent Plain Text 4140 lines 98.8 KB
   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
#
# 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 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

include ../Makefile.lib

# Hammerhead: amd64-only
SUBDIRS = $(MACH64)

# conditional assignments
all :		TARGET= all
install :	TARGET= install
clean :	TARGET= clean
clobber :	TARGET= clobber
test :		TARGET= test

# definitions for install_h target
HDRS=		libsysevent.h libsysevent_impl.h
ROOTHDRDIR=	$(ROOT)/usr/include
ROOTHDRS=	$(HDRS:%=$(ROOTHDRDIR)/%)
CHECKHDRS=	$(HDRS:%.h=%.check)

.KEEP_STATE:

all install clean clobber: $(SUBDIRS)

# install rule for install_h target

$(ROOTHDRDIR)/%: %
	$(INS.file)

install_h: $(ROOTHDRS)

check: $(CHECKHDRS)

$(SUBDIRS):	FRC
	@cd $@; pwd; $(MAKE) $(TARGET)

FRC:

#
# 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 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Copyright (c) 2018, Joyent, Inc.

LIBRARY=	libsysevent.a

VERS=		.1

OBJECTS=	libsysevent.o		\
		libevchannel.o

# include library definitions
include ../../Makefile.lib
include ../../Makefile.rootfs

LIBS =		$(DYNLIB)

CPPFLAGS +=	-D_REENTRANT -I..
CFLAGS +=	$(CCVERBOSE)
LDLIBS +=	-lnvpair -lc

# not linted
SMATCH=off

.KEEP_STATE:

all : $(LIBS)

# include library targets
include ../../Makefile.targ

pics/%.o:	../%.c
	$(COMPILE.c) -o $@ $<
	$(POST_PROCESS_O)
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

include ../Makefile.com
include ../../Makefile.lib.64

.KEEP_STATE:

all: $(LIBS)

install: all $(ROOTLIBS64) $(ROOTLINKS64) $(ROOTCOMPATLINKS64)
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <door.h>
#include <unistd.h>
#include <stddef.h>
#include <stdlib.h>
#include <strings.h>
#include <pthread.h>
#include <atomic.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/varargs.h>
#include <sys/sysevent.h>
#include <sys/sysevent_impl.h>

#include "libsysevent.h"
#include "libsysevent_impl.h"

/*
 * The functions below deal with the General Purpose Event Handling framework
 *
 * sysevent_evc_bind	    - create/bind application to named channel
 * sysevent_evc_unbind	    - unbind from previously bound/created channel
 * sysevent_evc_subscribe   - subscribe to existing event channel
 * sysevent_evc_unsubscribe - unsubscribe from existing event channel
 * sysevent_evc_publish	    - generate a system event via an event channel
 * sysevent_evc_control	    - various channel based control operation
 */

static void kill_door_servers(evchan_subscr_t *);

#define	misaligned(p)	((uintptr_t)(p) & 3)	/* 4-byte alignment required */

static pthread_key_t nrkey = PTHREAD_ONCE_KEY_NP;

/*
 * If the current thread is a door server thread servicing a door created
 * for us in sysevent_evc_xsubscribe, then an attempt to unsubscribe from
 * within door invocation context on the same channel will deadlock in the
 * kernel waiting for our own invocation to complete.  Such calls are
 * forbidden, and we abort if they are encountered (better than hanging
 * unkillably).
 *
 * We'd like to offer this detection to subscriptions established with
 * sysevent_evc_subscribe, but we don't have control over the door service
 * threads in that case.  Perhaps the fix is to always use door_xcreate
 * even for sysevent_evc_subscribe?
 */
static boolean_t
will_deadlock(evchan_t *scp)
{
	evchan_subscr_t *subp = pthread_getspecific(nrkey);
	evchan_impl_hdl_t *hdl = EVCHAN_IMPL_HNDL(scp);

	return (subp != NULL && subp->ev_subhead == hdl ? B_TRUE : B_FALSE);
}

/*
 * Check syntax of a channel name
 */
static int
sysevent_is_chan_name(const char *str)
{
	for (; *str != '\0'; str++) {
		if (!EVCH_ISCHANCHAR(*str))
			return (0);
	}

	return (1);
}

/*
 * Check for printable characters
 */
static int
strisprint(const char *s)
{
	for (; *s != '\0'; s++) {
		if (*s < ' ' || *s > '~')
			return (0);
	}

	return (1);
}

/*
 * sysevent_evc_bind - Create/bind application to named channel
 */
int
sysevent_evc_bind(const char *channel, evchan_t **scpp, uint32_t flags)
{
	int chanlen;
	evchan_t *scp;
	sev_bind_args_t uargs;
	int ec;

	if (scpp == NULL || misaligned(scpp)) {
		return (errno = EINVAL);
	}

	/* Provide useful value in error case */
	*scpp = NULL;

	if (channel == NULL ||
	    (chanlen = strlen(channel) + 1) > MAX_CHNAME_LEN) {
		return (errno = EINVAL);
	}

	/* Check channel syntax */
	if (!sysevent_is_chan_name(channel)) {
		return (errno = EINVAL);
	}

	if (flags & ~EVCH_B_FLAGS) {
		return (errno = EINVAL);
	}

	scp = calloc(1, sizeof (evchan_impl_hdl_t));
	if (scp == NULL) {
		return (errno = ENOMEM);
	}

	/*
	 * Enable sysevent driver.  Fallback if the device link doesn't exist;
	 * this situation can arise if a channel is bound early in system
	 * startup, prior to devfsadm(8) being invoked.
	 */
	EV_FD(scp) = open(DEVSYSEVENT, O_RDWR);
	if (EV_FD(scp) == -1) {
		if (errno != ENOENT) {
			ec = errno == EACCES ? EPERM : errno;
			free(scp);
			return (errno = ec);
		}

		EV_FD(scp) = open(DEVICESYSEVENT, O_RDWR);
		if (EV_FD(scp) == -1) {
			ec = errno == EACCES ? EPERM : errno;
			free(scp);
			return (errno = ec);
		}
	}

	/*
	 * Force to close the fd's when process is doing exec.
	 * The driver will then release stale binding handles.
	 * The driver will release also the associated subscriptions
	 * if EVCH_SUB_KEEP flag was not set.
	 */
	(void) fcntl(EV_FD(scp), F_SETFD, FD_CLOEXEC);

	uargs.chan_name.name = (uintptr_t)channel;
	uargs.chan_name.len = chanlen;
	uargs.flags = flags;

	if (ioctl(EV_FD(scp), SEV_CHAN_OPEN, &uargs) != 0) {
		ec = errno;
		(void) close(EV_FD(scp));
		free(scp);
		return (errno = ec);
	}

	/* Needed to detect a fork() */
	EV_PID(scp) = getpid();
	(void) mutex_init(EV_LOCK(scp), USYNC_THREAD, NULL);

	*scpp = scp;

	return (0);
}

/*
 * sysevent_evc_unbind - Unbind from previously bound/created channel
 */
int
sysevent_evc_unbind(evchan_t *scp)
{
	sev_unsubscribe_args_t uargs;
	evchan_subscr_t *subp;
	int errcp;

	if (scp == NULL || misaligned(scp))
		return (errno = EINVAL);

	if (will_deadlock(scp))
		return (errno = EDEADLK);

	(void) mutex_lock(EV_LOCK(scp));

	/*
	 * Unsubscribe, if we are in the process which did the bind.
	 */
	if (EV_PID(scp) == getpid()) {
		uargs.sid.name = (uintptr_t)NULL;
		uargs.sid.len = 0;
		/*
		 * The unsubscribe ioctl will block until all door upcalls have
		 * drained.
		 */
		if (ioctl(EV_FD(scp), SEV_UNSUBSCRIBE, (intptr_t)&uargs) != 0) {
			errcp = errno;
			(void) mutex_unlock(EV_LOCK(scp));
			return (errno = errcp);
		}
	}

	while ((subp =  EV_SUB_NEXT(scp)) != NULL) {
		EV_SUB_NEXT(scp) = subp->evsub_next;

		/* If door_xcreate was applied we can clean up */
		if (subp->evsub_attr)
			kill_door_servers(subp);

		if (door_revoke(subp->evsub_door_desc) != 0 && errno == EPERM)
			(void) close(subp->evsub_door_desc);

		free(subp->evsub_sid);
		free(subp);
	}

	(void) mutex_unlock(EV_LOCK(scp));

	/*
	 * The close of the driver will do the unsubscribe if a) it is the last
	 * close and b) we are in a child which inherited subscriptions.
	 */
	(void) close(EV_FD(scp));
	(void) mutex_destroy(EV_LOCK(scp));
	free(scp);

	return (0);
}

/*
 * sysevent_evc_publish - Generate a system event via an event channel
 */
int
sysevent_evc_publish(evchan_t *scp, const char *class,
    const char *subclass, const char *vendor,
    const char *pub_name, nvlist_t *attr_list,
    uint32_t flags)
{
	sysevent_t *ev;
	sev_publish_args_t uargs;
	int rc;
	int ec;

	if (scp == NULL || misaligned(scp)) {
		return (errno = EINVAL);
	}

	/* No inheritance of binding handles via fork() */
	if (EV_PID(scp) != getpid()) {
		return (errno = EINVAL);
	}

	ev = sysevent_alloc_event((char *)class, (char *)subclass,
	    (char *)vendor, (char *)pub_name, attr_list);
	if (ev == NULL) {
		return (errno);
	}

	uargs.ev.name = (uintptr_t)ev;
	uargs.ev.len = SE_SIZE(ev);
	uargs.flags = flags;

	(void) mutex_lock(EV_LOCK(scp));

	rc = ioctl(EV_FD(scp), SEV_PUBLISH, (intptr_t)&uargs);
	ec = errno;

	(void) mutex_unlock(EV_LOCK(scp));

	sysevent_free(ev);

	if (rc != 0) {
		return (ec);
	}
	return (0);
}

/*
 * Generic callback which catches events from the kernel and calls
 * subscribers call back routine.
 *
 * Kernel guarantees that door_upcalls are disabled when unsubscription
 * was issued that's why cookie points always to a valid evchan_subscr_t *.
 *
 * Furthermore it's not necessary to lock subp because the sysevent
 * framework guarantees no unsubscription until door_return.
 */
/*ARGSUSED3*/
static void
door_upcall(void *cookie, char *args, size_t alen,
    door_desc_t *ddp, uint_t ndid)
{
	evchan_subscr_t *subp = EVCHAN_SUBSCR(cookie);
	int rval = 0;

	/*
	 * If we've been invoked simply to kill the thread then
	 * exit now.
	 */
	if (subp->evsub_state == EVCHAN_SUB_STATE_CLOSING)
		pthread_exit(NULL);

	if (args == NULL || alen <= (size_t)0) {
		/* Skip callback execution */
		rval = EINVAL;
	} else {
		rval = subp->evsub_func((sysevent_t *)(void *)args,
		    subp->evsub_cookie);
	}

	/*
	 * Fill in return values for door_return
	 */
	alen = sizeof (rval);
	bcopy(&rval, args, alen);

	(void) door_return(args, alen, NULL, 0);
}

static pthread_once_t xsub_thrattr_once = PTHREAD_ONCE_INIT;
static pthread_attr_t xsub_thrattr;

static void
xsub_thrattr_init(void)
{
	(void) pthread_attr_init(&xsub_thrattr);
	(void) pthread_attr_setdetachstate(&xsub_thrattr,
	    PTHREAD_CREATE_DETACHED);
	(void) pthread_attr_setscope(&xsub_thrattr, PTHREAD_SCOPE_SYSTEM);
}

/*
 * Our door server create function is only called during initial
 * door_xcreate since we specify DOOR_NO_DEPLETION_CB.
 */
int
xsub_door_server_create(door_info_t *dip, void *(*startf)(void *),
    void *startfarg, void *cookie)
{
	evchan_subscr_t *subp = EVCHAN_SUBSCR(cookie);
	struct sysevent_subattr_impl *xsa = subp->evsub_attr;
	pthread_attr_t *thrattr;
	sigset_t oset;
	int err;

	if (subp->evsub_state == EVCHAN_SUB_STATE_CLOSING)
		return (0);	/* shouldn't happen, but just in case */

	/*
	 * If sysevent_evc_xsubscribe was called electing to use a
	 * different door server create function then let it take it
	 * from here.
	 */
	if (xsa->xs_thrcreate) {
		return (xsa->xs_thrcreate(dip, startf, startfarg,
		    xsa->xs_thrcreate_cookie));
	}

	if (xsa->xs_thrattr == NULL) {
		(void) pthread_once(&xsub_thrattr_once, xsub_thrattr_init);
		thrattr = &xsub_thrattr;
	} else {
		thrattr = xsa->xs_thrattr;
	}

	(void) pthread_sigmask(SIG_SETMASK, &xsa->xs_sigmask, &oset);
	err = pthread_create(NULL, thrattr, startf, startfarg);
	(void) pthread_sigmask(SIG_SETMASK, &oset, NULL);

	return (err == 0 ? 1 : -1);
}

void
xsub_door_server_setup(void *cookie)
{
	evchan_subscr_t *subp = EVCHAN_SUBSCR(cookie);
	struct sysevent_subattr_impl *xsa = subp->evsub_attr;

	if (xsa->xs_thrsetup == NULL) {
		(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
		(void) pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
	}

	(void) pthread_setspecific(nrkey, (void *)subp);

	if (xsa->xs_thrsetup)
		xsa->xs_thrsetup(xsa->xs_thrsetup_cookie);
}

/*
 * Cause private door server threads to exit.  We have already performed the
 * unsubscribe ioctl which stops new invocations and waits until all
 * existing invocations are complete.  So all server threads should be
 * blocked in door_return.  The door has not yet been revoked.  We will
 * invoke repeatedly after setting the evsub_state to be noticed on
 * wakeup; each invocation will result in the death of one server thread.
 *
 * You'd think it would be easier to kill these threads, such as through
 * pthread_cancel.  Unfortunately door_return is not a cancellation point,
 * and if you do cancel a thread blocked in door_return the EINTR check in
 * the door_return assembly logic causes us to loop with EINTR forever!
 */
static void
kill_door_servers(evchan_subscr_t *subp)
{
	door_arg_t da;

	bzero(&da, sizeof (da));
	subp->evsub_state = EVCHAN_SUB_STATE_CLOSING;
	membar_producer();

	(void) door_call(subp->evsub_door_desc, &da);
}

static int
sysevent_evc_subscribe_cmn(evchan_t *scp, const char *sid, const char *class,
    int (*event_handler)(sysevent_t *ev, void *cookie),
    void *cookie, uint32_t flags, struct sysevent_subattr_impl *xsa)
{
	evchan_subscr_t *subp;
	int upcall_door;
	sev_subscribe_args_t uargs;
	uint32_t sid_len;
	uint32_t class_len;
	int ec;

	if (scp == NULL || misaligned(scp) || sid == NULL || class == NULL) {
		return (errno = EINVAL);
	}

	/* No inheritance of binding handles via fork() */
	if (EV_PID(scp) != getpid()) {
		return (errno = EINVAL);
	}

	if ((sid_len = strlen(sid) + 1) > MAX_SUBID_LEN || sid_len == 1 ||
	    (class_len = strlen(class) + 1) > MAX_CLASS_LEN) {
		return (errno = EINVAL);
	}

	/* Check for printable characters */
	if (!strisprint(sid)) {
		return (errno = EINVAL);
	}

	if (event_handler == NULL) {
		return (errno = EINVAL);
	}

	if (pthread_key_create_once_np(&nrkey, NULL) != 0)
		return (errno);	/* ENOMEM or EAGAIN */

	/* Create subscriber data */
	if ((subp = calloc(1, sizeof (evchan_subscr_t))) == NULL) {
		return (errno);
	}

	if ((subp->evsub_sid = strdup(sid)) == NULL) {
		ec = errno;
		free(subp);
		return (ec);
	}

	/*
	 * EC_ALL string will not be copied to kernel - NULL is assumed
	 */
	if (strcmp(class, EC_ALL) == 0) {
		class = NULL;
		class_len = 0;
	}

	/*
	 * Fill this in now for the xsub_door_server_setup dance
	 */
	subp->ev_subhead = EVCHAN_IMPL_HNDL(scp);
	subp->evsub_state = EVCHAN_SUB_STATE_ACTIVE;

	if (xsa == NULL) {
		upcall_door = door_create(door_upcall, (void *)subp,
		    DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
	} else {
		subp->evsub_attr = xsa;

		/*
		 * Create a private door with exactly one thread to
		 * service the callbacks (the GPEC kernel implementation
		 * serializes deliveries for each subscriber id).
		 */
		upcall_door = door_xcreate(door_upcall, (void *)subp,
		    DOOR_REFUSE_DESC | DOOR_NO_CANCEL | DOOR_NO_DEPLETION_CB,
		    xsub_door_server_create, xsub_door_server_setup,
		    (void *)subp, 1);
	}

	if (upcall_door == -1) {
		ec = errno;
		free(subp->evsub_sid);
		free(subp);
		return (ec);
	}

	/* Complete subscriber information */
	subp->evsub_door_desc = upcall_door;
	subp->evsub_func = event_handler;
	subp->evsub_cookie = cookie;

	(void) mutex_lock(EV_LOCK(scp));

	uargs.sid.name = (uintptr_t)sid;
	uargs.sid.len = sid_len;
	uargs.class_info.name = (uintptr_t)class;
	uargs.class_info.len = class_len;
	uargs.door_desc = subp->evsub_door_desc;
	uargs.flags = flags;
	if (ioctl(EV_FD(scp), SEV_SUBSCRIBE, (intptr_t)&uargs) != 0) {
		ec = errno;
		(void) mutex_unlock(EV_LOCK(scp));
		if (xsa)
			kill_door_servers(subp);
		(void) door_revoke(upcall_door);
		free(subp->evsub_sid);
		free(subp);
		return (ec);
	}

	/* Attach to subscriber list */
	subp->evsub_next = EV_SUB_NEXT(scp);
	EV_SUB_NEXT(scp) = subp;

	(void) mutex_unlock(EV_LOCK(scp));

	return (0);
}

/*
 * sysevent_evc_subscribe - subscribe to an existing event channel
 * using a non-private door (which will create as many server threads
 * as the apparent maximum concurrency requirements suggest).
 */
int
sysevent_evc_subscribe(evchan_t *scp, const char *sid, const char *class,
    int (*event_handler)(sysevent_t *ev, void *cookie),
    void *cookie, uint32_t flags)
{
	return (sysevent_evc_subscribe_cmn(scp, sid, class, event_handler,
	    cookie, flags, NULL));
}

static void
subattr_dfltinit(struct sysevent_subattr_impl *xsa)
{
	(void) sigfillset(&xsa->xs_sigmask);
	(void) sigdelset(&xsa->xs_sigmask, SIGABRT);
}

static struct sysevent_subattr_impl dfltsa;
pthread_once_t dfltsa_inited = PTHREAD_ONCE_INIT;

static void
init_dfltsa(void)
{
	subattr_dfltinit(&dfltsa);
}

/*
 * sysevent_evc_subscribe - subscribe to an existing event channel
 * using a private door with control over thread creation.
 */
int
sysevent_evc_xsubscribe(evchan_t *scp, const char *sid, const char *class,
    int (*event_handler)(sysevent_t *ev, void *cookie),
    void *cookie, uint32_t flags, sysevent_subattr_t *attr)
{
	struct sysevent_subattr_impl *xsa;

	if (attr != NULL) {
		xsa = (struct sysevent_subattr_impl *)attr;
	} else {
		xsa = &dfltsa;
		(void) pthread_once(&dfltsa_inited, init_dfltsa);
	}

	return (sysevent_evc_subscribe_cmn(scp, sid, class, event_handler,
	    cookie, flags, xsa));
}

sysevent_subattr_t *
sysevent_subattr_alloc(void)
{
	struct sysevent_subattr_impl *xsa = calloc(1, sizeof (*xsa));

	if (xsa != NULL)
		subattr_dfltinit(xsa);

	return (xsa != NULL ? (sysevent_subattr_t *)xsa : NULL);
}

void
sysevent_subattr_free(sysevent_subattr_t *attr)
{
	struct sysevent_subattr_impl *xsa =
	    (struct sysevent_subattr_impl *)attr;

	free(xsa);
}

void
sysevent_subattr_thrcreate(sysevent_subattr_t *attr,
    door_xcreate_server_func_t *thrcreate, void *cookie)
{
	struct sysevent_subattr_impl *xsa =
	    (struct sysevent_subattr_impl *)attr;

	xsa->xs_thrcreate = thrcreate;
	xsa->xs_thrcreate_cookie = cookie;
}

void
sysevent_subattr_thrsetup(sysevent_subattr_t *attr,
    door_xcreate_thrsetup_func_t *thrsetup, void *cookie)
{
	struct sysevent_subattr_impl *xsa =
	    (struct sysevent_subattr_impl *)attr;

	xsa->xs_thrsetup = thrsetup;
	xsa->xs_thrsetup_cookie = cookie;
}

void
sysevent_subattr_sigmask(sysevent_subattr_t *attr, sigset_t *set)
{
	struct sysevent_subattr_impl *xsa =
	    (struct sysevent_subattr_impl *)attr;

	if (set) {
		xsa->xs_sigmask = *set;
	} else {
		(void) sigfillset(&xsa->xs_sigmask);
		(void) sigdelset(&xsa->xs_sigmask, SIGABRT);
	}
}

void
sysevent_subattr_thrattr(sysevent_subattr_t *attr, pthread_attr_t *thrattr)
{
	struct sysevent_subattr_impl *xsa =
	    (struct sysevent_subattr_impl *)attr;

	xsa->xs_thrattr = thrattr;
}

/*
 * sysevent_evc_unsubscribe - Unsubscribe from an existing event channel
 */
int
sysevent_evc_unsubscribe(evchan_t *scp, const char *sid)
{
	int all_subscribers = 0;
	sev_unsubscribe_args_t uargs;
	evchan_subscr_t *subp, *prevsubp, *tofree;
	int errcp;
	int rc;

	if (scp == NULL || misaligned(scp))
		return (errno = EINVAL);

	if (sid == NULL || strlen(sid) == 0 ||
	    (strlen(sid) >= MAX_SUBID_LEN))
		return (errno = EINVAL);

	/* No inheritance of binding handles via fork() */
	if (EV_PID(scp) != getpid())
		return (errno = EINVAL);

	if (strcmp(sid, EVCH_ALLSUB) == 0) {
		all_subscribers++;
		/* Indicates all subscriber id's for this channel */
		uargs.sid.name = (uintptr_t)NULL;
		uargs.sid.len = 0;
	} else {
		uargs.sid.name = (uintptr_t)sid;
		uargs.sid.len = strlen(sid) + 1;
	}

	if (will_deadlock(scp))
		return (errno = EDEADLK);

	(void) mutex_lock(EV_LOCK(scp));

	/*
	 * The unsubscribe ioctl will block until all door upcalls have drained.
	 */
	rc = ioctl(EV_FD(scp), SEV_UNSUBSCRIBE, (intptr_t)&uargs);

	if (rc != 0) {
		errcp = errno;
		(void) mutex_unlock(EV_LOCK(scp));
		return (errno = errcp); /* EFAULT, ENXIO, EINVAL possible */
	}


	/*
	 * Search for the matching subscriber.  If EVCH_ALLSUB was specified
	 * then the ioctl above will have returned 0 even if there are
	 * no subscriptions, so the initial EV_SUB_NEXT can be NULL.
	 */
	prevsubp = NULL;
	subp =  EV_SUB_NEXT(scp);
	while (subp != NULL) {
		if (all_subscribers || strcmp(subp->evsub_sid, sid) == 0) {
			if (prevsubp == NULL) {
				EV_SUB_NEXT(scp) = subp->evsub_next;
			} else {
				prevsubp->evsub_next = subp->evsub_next;
			}

			tofree = subp;
			subp = subp->evsub_next;

			/* If door_xcreate was applied we can clean up */
			if (tofree->evsub_attr)
				kill_door_servers(tofree);

			(void) door_revoke(tofree->evsub_door_desc);
			free(tofree->evsub_sid);
			free(tofree);

			/* Freed single subscriber already? */
			if (all_subscribers == 0)
				break;
		} else {
			prevsubp = subp;
			subp = subp->evsub_next;
		}
	}

	(void) mutex_unlock(EV_LOCK(scp));

	return (0);
}

/*
 * sysevent_evc_control - Various channel based control operation
 */
int
sysevent_evc_control(evchan_t *scp, int cmd, /* arg */ ...)
{
	va_list ap;
	uint32_t *chlenp;
	sev_control_args_t uargs;
	int rc = 0;

	if (scp == NULL || misaligned(scp)) {
		return (errno = EINVAL);
	}

	/* No inheritance of binding handles via fork() */
	if (EV_PID(scp) != getpid()) {
		return (errno = EINVAL);
	}

	va_start(ap, cmd);

	uargs.cmd = cmd;

	(void) mutex_lock(EV_LOCK(scp));

	switch (cmd) {
	case EVCH_GET_CHAN_LEN:
	case EVCH_GET_CHAN_LEN_MAX:
		chlenp = va_arg(ap, uint32_t *);
		if (chlenp == NULL || misaligned(chlenp)) {
			rc = EINVAL;
			break;
		}
		rc = ioctl(EV_FD(scp), SEV_CHAN_CONTROL, (intptr_t)&uargs);
		*chlenp = uargs.value;
		break;

	case EVCH_SET_CHAN_LEN:
		/* Range change will be handled in framework */
		uargs.value = va_arg(ap, uint32_t);
		rc = ioctl(EV_FD(scp), SEV_CHAN_CONTROL, (intptr_t)&uargs);
		break;

	default:
		rc = EINVAL;
	}

	(void) mutex_unlock(EV_LOCK(scp));

	if (rc == -1) {
		rc = errno;
	}

	va_end(ap);

	return (errno = rc);
}

int
sysevent_evc_setpropnvl(evchan_t *scp, nvlist_t *nvl)
{
	sev_propnvl_args_t uargs;
	char *buf = NULL;
	size_t nvlsz = 0;
	int rc;

	if (scp == NULL || misaligned(scp))
		return (errno = EINVAL);

	if (nvl != NULL &&
	    nvlist_pack(nvl, &buf, &nvlsz, NV_ENCODE_NATIVE, 0) != 0)
		return (errno);

	uargs.packednvl.name = (uint64_t)(uintptr_t)buf;
	uargs.packednvl.len = (uint32_t)nvlsz;

	rc = ioctl(EV_FD(scp), SEV_SETPROPNVL, (intptr_t)&uargs);

	if (buf)
		free(buf);

	return (rc);
}

int
sysevent_evc_getpropnvl(evchan_t *scp, nvlist_t **nvlp)
{
	sev_propnvl_args_t uargs;
	char buf[1024], *bufp = buf;	/* stack buffer */
	size_t sz = sizeof (buf);
	char *buf2 = NULL;		/* allocated if stack buf too small */
	int64_t expgen = -1;
	int rc;

	if (scp == NULL || misaligned(scp) || nvlp == NULL)
		return (errno = EINVAL);

	*nvlp = NULL;

again:
	uargs.packednvl.name = (uint64_t)(uintptr_t)bufp;
	uargs.packednvl.len = (uint32_t)sz;

	rc = ioctl(EV_FD(scp), SEV_GETPROPNVL, (intptr_t)&uargs);

	if (rc == E2BIG)
		return (errno = E2BIG);	/* driver refuses to copyout */

	/*
	 * If the packed nvlist is too big for the buffer size we offered
	 * then the ioctl returns EOVERFLOW and indicates in the 'len'
	 * the size required for the current property nvlist generation
	 * (itself returned in the generation member).
	 */
	if (rc == EOVERFLOW &&
	    (buf2 == NULL || uargs.generation != expgen)) {
		if (buf2 != NULL)
			free(buf2);

		if ((sz = uargs.packednvl.len) > 1024 * 1024)
			return (E2BIG);

		bufp = buf2 = malloc(sz);

		if (buf2 == NULL)
			return (errno = ENOMEM);

		expgen = uargs.generation;
		goto again;
	}

	/*
	 * The chan prop nvlist can be absent, in which case the ioctl
	 * returns success and uargs.packednvl.len of 0;  we have already
	 * set *nvlp to NULL.  Otherwise we must unpack the nvl.
	 */
	if (rc == 0 && uargs.packednvl.len != 0 &&
	    nvlist_unpack(bufp, uargs.packednvl.len, nvlp, 0) != 0)
		rc = EINVAL;

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

	return (rc ? errno = rc : 0);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <door.h>
#include <unistd.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <synch.h>
#include <pthread.h>
#include <signal.h>
#include <thread.h>
#include <libnvpair.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/modctl.h>
#include <sys/mnttab.h>
#include <sys/sysevent.h>
#include <sys/sysevent_impl.h>

#include "libsysevent.h"
#include "libsysevent_impl.h"

/*
 * libsysevent - The system event framework library
 *
 *		This library provides routines to help with marshalling
 *		and unmarshalling of data contained in a sysevent event
 *		buffer.
 */

#define	SE_ENCODE_METHOD	NV_ENCODE_NATIVE

#define	dprint	if (libsysevent_debug) (void) printf
static int libsysevent_debug = 0;

static sysevent_t *se_unpack(sysevent_t *);
static int cleanup_id(sysevent_handle_t *shp, uint32_t id, int type);

/*
 * The following routines allow system event publication to the sysevent
 * framework.
 */

/*
 * sysevent_alloc - allocate a sysevent buffer
 */
static sysevent_t *
sysevent_alloc(char *class, int class_sz, char *subclass, int subclass_sz,
    char *pub, int pub_sz, nvlist_t *attr_list)
{
	int payload_sz;
	int aligned_class_sz, aligned_subclass_sz, aligned_pub_sz;
	size_t nvlist_sz = 0;
	char *attr;
	uint64_t attr_offset;
	sysevent_t *ev;

	if (attr_list != NULL) {
		if (nvlist_size(attr_list, &nvlist_sz, SE_ENCODE_METHOD)
		    != 0) {
			return (NULL);
		}
	}

	/*
	 * Calculate and reserve space for the class, subclass and
	 * publisher strings in the event buffer
	 */

	/* String sizes must be 64-bit aligned in the event buffer */
	aligned_class_sz = SE_ALIGN(class_sz);
	aligned_subclass_sz = SE_ALIGN(subclass_sz);
	aligned_pub_sz = SE_ALIGN(pub_sz);

	payload_sz = (aligned_class_sz - sizeof (uint64_t)) +
	    (aligned_subclass_sz - sizeof (uint64_t)) +
	    (aligned_pub_sz - sizeof (uint64_t)) - sizeof (uint64_t) +
	    nvlist_sz;

	/*
	 * Allocate event buffer plus additional payload overhead.
	 */
	ev = calloc(1, sizeof (sysevent_impl_t) + payload_sz);
	if (ev == NULL) {
		return (NULL);
	}

	/* Initialize the event buffer data */
	SE_VERSION(ev) = SYS_EVENT_VERSION;
	(void) bcopy(class, SE_CLASS_NAME(ev), class_sz);

	SE_SUBCLASS_OFF(ev) = SE_ALIGN(offsetof(sysevent_impl_t, se_class_name))
		+ aligned_class_sz;
	(void) bcopy(subclass, SE_SUBCLASS_NAME(ev), subclass_sz);

	SE_PUB_OFF(ev) = SE_SUBCLASS_OFF(ev) + aligned_subclass_sz;
	(void) bcopy(pub, SE_PUB_NAME(ev), pub_sz);

	SE_PAYLOAD_SZ(ev) = payload_sz;
	SE_ATTR_PTR(ev) = (uint64_t)0;

	/* Check for attribute list */
	if (attr_list == NULL) {
		return (ev);
	}

	/* Copy attribute data to contiguous memory */
	SE_FLAG(ev) = SE_PACKED_BUF;
	attr_offset = SE_ATTR_OFF(ev);
	attr = (char *)((caddr_t)ev + attr_offset);
	if (nvlist_pack(attr_list, &attr, &nvlist_sz, SE_ENCODE_METHOD,
	    0) != 0) {
		free(ev);
		return (NULL);
	}

	return (ev);
}

/*
 * sysevent_post_event - generate a system event via the sysevent framework
 */
int
sysevent_post_event(char *class, char *subclass, char *vendor, char *pub_name,
    nvlist_t *attr_list, sysevent_id_t *eid)
{
	int error;
	sysevent_t *ev;

	ev = sysevent_alloc_event(class, subclass, vendor, pub_name, attr_list);
	if (ev == NULL) {
		return (-1);
	}

	error = modctl(MODEVENTS, (uintptr_t)MODEVENTS_POST_EVENT,
	    (uintptr_t)ev, (uintptr_t)SE_SIZE(ev), (uintptr_t)eid, 0);

	sysevent_free(ev);

	if (error) {
		errno = EIO;
		return (-1);
	}

	return (0);
}

/*
 * The following routines are used to free or duplicate a
 * sysevent event buffer.
 */

/*
 * sysevent_dup - Allocate and copy an event buffer
 *	Copies both packed and unpacked to unpacked sysevent.
 */
sysevent_t *
sysevent_dup(sysevent_t *ev)
{
	nvlist_t *nvl, *cnvl = NULL;
	uint64_t attr_offset;
	sysevent_t *copy;

	if (SE_FLAG(ev) == SE_PACKED_BUF)
		return (se_unpack(ev));

	/* Copy event header information */
	attr_offset = SE_ATTR_OFF(ev);
	copy = calloc(1, attr_offset);
	if (copy == NULL)
		return (NULL);
	bcopy(ev, copy, attr_offset);

	nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev);
	if (nvl && nvlist_dup(nvl, &cnvl, 0) != 0) {
		free(copy);
		return (NULL);
	}

	SE_ATTR_PTR(copy) = (uintptr_t)cnvl;
	SE_FLAG(copy) = 0;	/* unpacked */
	return (copy);
}

/*
 * sysevent_free - Free memory allocated for an event buffer
 */
void
sysevent_free(sysevent_t *ev)
{
	nvlist_t *attr_list = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev);

	nvlist_free(attr_list);
	free(ev);
}

/*
 * The following routines are used to extract attribute data from a sysevent
 * handle.
 */

/*
 * sysevent_get_attr_list - allocate and return an attribute associated with
 *			the given sysevent buffer.
 */
int
sysevent_get_attr_list(sysevent_t *ev, nvlist_t **nvlist)
{
	int error;
	caddr_t attr;
	size_t attr_len;
	uint64_t attr_offset;
	nvlist_t *nvl;

	*nvlist = NULL;

	/* Duplicate attribute for an unpacked sysevent buffer */
	if (SE_FLAG(ev) != SE_PACKED_BUF) {
		nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev);
		if (nvl == NULL) {
			return (0);
		}
		if ((error = nvlist_dup(nvl, nvlist, 0)) != 0) {
			if (error == ENOMEM) {
				errno = error;
			} else {
				errno = EINVAL;
			}
			return (-1);
		}
		return (0);
	}

	attr_offset = SE_ATTR_OFF(ev);
	if (SE_SIZE(ev) == attr_offset) {
		return (0);
	}

	/* unpack nvlist */
	attr = (caddr_t)ev + attr_offset;
	attr_len = SE_SIZE(ev) - attr_offset;
	if ((error = nvlist_unpack(attr, attr_len, nvlist, 0)) != 0) {
		if (error == ENOMEM) {
			errno = error;
		} else {
			errno = EINVAL;
		}
		return (-1);
	}

	return (0);
}

/*
 * sysevent_attr_name - Get name of attribute
 */
char *
sysevent_attr_name(sysevent_attr_t *attr)
{
	if (attr == NULL) {
		errno = EINVAL;
		return (NULL);
	}
	return (nvpair_name((nvpair_t *)attr));
}

/*
 * sysevent_attr_value - Get attribute value data and type
 */
int
sysevent_attr_value(sysevent_attr_t *attr, sysevent_value_t *se_value)
{
	nvpair_t *nvp = attr;

	if (nvp == NULL)
		return (EINVAL);

	/* Convert DATA_TYPE_* to SE_DATA_TYPE_* */
	switch (nvpair_type(nvp)) {
	case DATA_TYPE_BYTE:
		se_value->value_type = SE_DATA_TYPE_BYTE;
		(void) nvpair_value_byte(nvp, &se_value->value.sv_byte);
		break;
	case DATA_TYPE_INT16:
		se_value->value_type = SE_DATA_TYPE_INT16;
		(void) nvpair_value_int16(nvp, &se_value->value.sv_int16);
		break;
	case DATA_TYPE_UINT16:
		se_value->value_type = SE_DATA_TYPE_UINT16;
		(void) nvpair_value_uint16(nvp, &se_value->value.sv_uint16);
		break;
	case DATA_TYPE_INT32:
		se_value->value_type = SE_DATA_TYPE_INT32;
		(void) nvpair_value_int32(nvp, &se_value->value.sv_int32);
		break;
	case DATA_TYPE_UINT32:
		se_value->value_type = SE_DATA_TYPE_UINT32;
		(void) nvpair_value_uint32(nvp, &se_value->value.sv_uint32);
		break;
	case DATA_TYPE_INT64:
		se_value->value_type = SE_DATA_TYPE_INT64;
		(void) nvpair_value_int64(nvp, &se_value->value.sv_int64);
		break;
	case DATA_TYPE_UINT64:
		se_value->value_type = SE_DATA_TYPE_UINT64;
		(void) nvpair_value_uint64(nvp, &se_value->value.sv_uint64);
		break;
	case DATA_TYPE_STRING:
		se_value->value_type = SE_DATA_TYPE_STRING;
		(void) nvpair_value_string(nvp, &se_value->value.sv_string);
		break;
	case DATA_TYPE_BYTE_ARRAY:
		se_value->value_type = SE_DATA_TYPE_BYTES;
		(void) nvpair_value_byte_array(nvp,
		    &se_value->value.sv_bytes.data,
		    (uint_t *)&se_value->value.sv_bytes.size);
		break;
	case DATA_TYPE_HRTIME:
		se_value->value_type = SE_DATA_TYPE_TIME;
		(void) nvpair_value_hrtime(nvp, &se_value->value.sv_time);
		break;
	default:
		return (ENOTSUP);
	}
	return (0);
}

/*
 * sysevent_attr_next - Get next attribute in event attribute list
 */
sysevent_attr_t *
sysevent_attr_next(sysevent_t *ev, sysevent_attr_t *attr)
{
	nvlist_t *nvl;
	nvpair_t *nvp = attr;

	/* all user visible sysevent_t's are unpacked */
	assert(SE_FLAG(ev) != SE_PACKED_BUF);

	if (SE_ATTR_PTR(ev) == (uint64_t)0) {
		return (NULL);
	}

	nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev);
	return (nvlist_next_nvpair(nvl, nvp));
}

/*
 * sysevent_lookup_attr - Lookup attribute by name and datatype.
 */
int
sysevent_lookup_attr(sysevent_t *ev, char *name, int datatype,
    sysevent_value_t *se_value)
{
	nvpair_t *nvp;
	nvlist_t *nvl;

	assert(SE_FLAG(ev) != SE_PACKED_BUF);

	if (SE_ATTR_PTR(ev) == (uint64_t)0) {
		return (ENOENT);
	}

	/*
	 * sysevent matches on both name and datatype
	 * nvlist_look mataches name only. So we walk
	 * nvlist manually here.
	 */
	nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev);
	nvp = nvlist_next_nvpair(nvl, NULL);
	while (nvp) {
		if ((strcmp(name, nvpair_name(nvp)) == 0) &&
		    (sysevent_attr_value(nvp, se_value) == 0) &&
		    (se_value->value_type == datatype))
			return (0);
		nvp = nvlist_next_nvpair(nvl, nvp);
	}
	return (ENOENT);
}

/* Routines to extract event header information */

/*
 * sysevent_get_class - Get class id
 */
int
sysevent_get_class(sysevent_t *ev)
{
	return (SE_CLASS(ev));
}

/*
 * sysevent_get_subclass - Get subclass id
 */
int
sysevent_get_subclass(sysevent_t *ev)
{
	return (SE_SUBCLASS(ev));
}

/*
 * sysevent_get_class_name - Get class name string
 */
char *
sysevent_get_class_name(sysevent_t *ev)
{
	return (SE_CLASS_NAME(ev));
}

typedef enum {
	PUB_VEND,
	PUB_KEYWD,
	PUB_NAME,
	PUB_PID
} se_pub_id_t;

/*
 * sysevent_get_pub - Get publisher name string
 */
char *
sysevent_get_pub(sysevent_t *ev)
{
	return (SE_PUB_NAME(ev));
}

/*
 * Get the requested string pointed by the token.
 *
 * Return NULL if not found or for insufficient memory.
 */
static char *
parse_pub_id(sysevent_t *ev, se_pub_id_t token)
{
	int i;
	char *pub_id, *pub_element, *str, *next;

	next = pub_id = strdup(sysevent_get_pub(ev));
	for (i = 0; i <= token; ++i) {
		str = strtok_r(next, ":", &next);
		if (str == NULL) {
			free(pub_id);
			return (NULL);
		}
	}

	pub_element = strdup(str);
	free(pub_id);
	return (pub_element);
}

/*
 * Return a pointer to the string following the token
 *
 * Note: This is a dedicated function for parsing
 * publisher strings and not for general purpose.
 */
static const char *
pub_idx(const char *pstr, int token)
{
	int i;

	for (i = 1; i <= token; i++) {
		if ((pstr = index(pstr, ':')) == NULL)
			return (NULL);
		pstr++;
	}

	/* String might be empty */
	if (pstr) {
		if (*pstr == '\0' || *pstr == ':')
			return (NULL);
	}
	return (pstr);
}

char *
sysevent_get_vendor_name(sysevent_t *ev)
{
	return (parse_pub_id(ev, PUB_VEND));
}

char *
sysevent_get_pub_name(sysevent_t *ev)
{
	return (parse_pub_id(ev, PUB_NAME));
}

/*
 * Provide the pid encoded in the publisher string
 * w/o allocating any resouces.
 */
void
sysevent_get_pid(sysevent_t *ev, pid_t *pid)
{
	const char *part_str;
	const char *pub_str = sysevent_get_pub(ev);

	*pid = (pid_t)SE_KERN_PID;

	part_str = pub_idx(pub_str, PUB_KEYWD);
	if (part_str != NULL && strstr(part_str, SE_KERN_PUB) != NULL)
		return;

	if ((part_str = pub_idx(pub_str, PUB_PID)) == NULL)
		return;

	*pid = (pid_t)atoi(part_str);
}

/*
 * sysevent_get_subclass_name - Get subclass name string
 */
char *
sysevent_get_subclass_name(sysevent_t *ev)
{
	return (SE_SUBCLASS_NAME(ev));
}

/*
 * sysevent_get_seq - Get event sequence id
 */
uint64_t
sysevent_get_seq(sysevent_t *ev)
{
	return (SE_SEQ(ev));
}

/*
 * sysevent_get_time - Get event timestamp
 */
void
sysevent_get_time(sysevent_t *ev, hrtime_t *etime)
{
	*etime = SE_TIME(ev);
}

/*
 * sysevent_get_size - Get event buffer size
 */
size_t
sysevent_get_size(sysevent_t *ev)
{
	return ((size_t)SE_SIZE(ev));
}

/*
 * The following routines are used by devfsadm_mod.c to propagate event
 * buffers to devfsadmd.  These routines will serve as the basis for
 * event channel publication and subscription.
 */

/*
 * sysevent_alloc_event -
 *	allocate a sysevent buffer for sending through an established event
 *	channel.
 */
sysevent_t *
sysevent_alloc_event(char *class, char *subclass, char *vendor, char *pub_name,
    nvlist_t *attr_list)
{
	int class_sz, subclass_sz, pub_sz;
	char *pub_id;
	sysevent_t *ev;

	if ((class == NULL) || (subclass == NULL) || (vendor == NULL) ||
	    (pub_name == NULL)) {
		errno = EINVAL;
		return (NULL);
	}

	class_sz = strlen(class) + 1;
	subclass_sz = strlen(subclass) + 1;
	if ((class_sz > MAX_CLASS_LEN) ||
	    (subclass_sz > MAX_SUBCLASS_LEN)) {
		errno = EINVAL;
		return (NULL);
	}

	/*
	 * Calculate the publisher size plus string seperators and maximum
	 * pid characters
	 */
	pub_sz = strlen(vendor) + sizeof (SE_USR_PUB) + strlen(pub_name) + 14;
	if (pub_sz > MAX_PUB_LEN) {
		errno = EINVAL;
		return (NULL);
	}
	pub_id = malloc(pub_sz);
	if (pub_id == NULL) {
		errno = ENOMEM;
		return (NULL);
	}
	if (snprintf(pub_id, pub_sz, "%s:%s%s:%d", vendor, SE_USR_PUB,
	    pub_name, (int)getpid()) >= pub_sz) {
		free(pub_id);
		errno = EINVAL;
		return (NULL);
	}
	pub_sz = strlen(pub_id) + 1;

	ev = sysevent_alloc(class, class_sz, subclass, subclass_sz,
	    pub_id, pub_sz, attr_list);
	free(pub_id);
	if (ev == NULL) {
		errno = ENOMEM;
		return (NULL);
	}

	return (ev);
}

/*
 * se_unpack - unpack nvlist to a searchable list.
 *	If already unpacked, will do a dup.
 */
static sysevent_t *
se_unpack(sysevent_t *ev)
{
	caddr_t attr;
	size_t attr_len;
	nvlist_t *attrp = NULL;
	uint64_t attr_offset;
	sysevent_t *copy;

	assert(SE_FLAG(ev) == SE_PACKED_BUF);

	/* Copy event header information */
	attr_offset = SE_ATTR_OFF(ev);
	copy = calloc(1, attr_offset);
	if (copy == NULL)
		return (NULL);
	bcopy(ev, copy, attr_offset);
	SE_FLAG(copy) = 0;	/* unpacked */

	/* unpack nvlist */
	attr = (caddr_t)ev + attr_offset;
	attr_len = SE_SIZE(ev) - attr_offset;
	if (attr_len == 0) {
		return (copy);
	}
	if (nvlist_unpack(attr, attr_len, &attrp, 0) != 0) {
		free(copy);
		return (NULL);
	}

	SE_ATTR_PTR(copy) = (uintptr_t)attrp;
	return (copy);
}

/*
 * se_print - Prints elements in an event buffer
 */
void
se_print(FILE *fp, sysevent_t *ev)
{
	char *vendor, *pub;
	pid_t pid;
	hrtime_t hrt;
	nvlist_t *attr_list = NULL;

	(void) sysevent_get_time(ev, &hrt);
	(void) fprintf(fp, "received sysevent id = 0X%llx:%llx\n",
	    hrt, (longlong_t)sysevent_get_seq(ev));
	(void) fprintf(fp, "\tclass = %s\n", sysevent_get_class_name(ev));
	(void) fprintf(fp, "\tsubclass = %s\n", sysevent_get_subclass_name(ev));
	if ((vendor =  sysevent_get_vendor_name(ev)) != NULL) {
		(void) fprintf(fp, "\tvendor = %s\n", vendor);
		free(vendor);
	}
	if ((pub = sysevent_get_pub_name(ev)) != NULL) {
		sysevent_get_pid(ev, &pid);
		(void) fprintf(fp, "\tpublisher = %s:%d\n", pub, (int)pid);
		free(pub);
	}

	if (sysevent_get_attr_list(ev, &attr_list) == 0 && attr_list != NULL) {
		nvlist_print(fp, attr_list);
		nvlist_free(attr_list);
	}
}

/*
 * The following routines are provided to support establishment and use
 * of sysevent channels.  A sysevent channel is established between
 * publishers and subscribers of sysevents for an agreed upon channel name.
 * These routines currently support sysevent channels between user-level
 * applications running on the same system.
 *
 * Sysevent channels may be created by a single publisher or subscriber process.
 * Once established, up to MAX_SUBSRCIBERS subscribers may subscribe interest in
 * receiving sysevent notifications on the named channel.  At present, only
 * one publisher is allowed per sysevent channel.
 *
 * The registration information for each channel is kept in the kernel.  A
 * kernel-based registration was chosen for persistence and reliability reasons.
 * If either a publisher or a subscriber exits for any reason, the channel
 * properties are maintained until all publishers and subscribers have exited.
 * Additionally, an in-kernel registration allows the API to be extended to
 * include kernel subscribers as well as userland subscribers in the future.
 *
 * To insure fast lookup of subscriptions, a cached copy of the registration
 * is kept and maintained for the publisher process.  Updates are made
 * everytime a change is made in the kernel.  Changes to the registration are
 * expected to be infrequent.
 *
 * Channel communication between publisher and subscriber processes is
 * implemented primarily via doors.  Each publisher creates a door for
 * registration notifications and each subscriber creates a door for event
 * delivery.
 *
 * Most of these routines are used by syseventd(8), the sysevent publisher
 * for the syseventd channel.  Processes wishing to receive sysevent
 * notifications from syseventd may use a set of public
 * APIs designed to subscribe to syseventd sysevents.  The subscription
 * APIs are implemented in accordance with PSARC/2001/076.
 *
 */

/*
 * Door handlers for the channel subscribers
 */

/*
 * subscriber_event_handler - generic event handling wrapper for subscribers
 *			This handler is used to process incoming sysevent
 *			notifications from channel publishers.
 *			It is created as a seperate thread in each subscriber
 *			process per subscription.
 */
static void *
subscriber_event_handler(void *arg)
{
	sysevent_handle_t *shp = arg;
	subscriber_priv_t *sub_info;
	sysevent_queue_t *evqp;

	sub_info = (subscriber_priv_t *)SH_PRIV_DATA(shp);

	/* See hack alert in sysevent_bind_subscriber_cmn */
	if (sub_info->sp_handler_tid == 0)
		sub_info->sp_handler_tid = thr_self();

	(void) mutex_lock(&sub_info->sp_qlock);
	for (;;) {
		while (sub_info->sp_evq_head == NULL && SH_BOUND(shp)) {
			(void) cond_wait(&sub_info->sp_cv, &sub_info->sp_qlock);
		}
		evqp = sub_info->sp_evq_head;
		while (evqp) {
			(void) mutex_unlock(&sub_info->sp_qlock);
			(void) sub_info->sp_func(evqp->sq_ev);
			(void) mutex_lock(&sub_info->sp_qlock);
			sub_info->sp_evq_head = sub_info->sp_evq_head->sq_next;
			free(evqp->sq_ev);
			free(evqp);
			evqp = sub_info->sp_evq_head;
		}
		if (!SH_BOUND(shp)) {
			(void) mutex_unlock(&sub_info->sp_qlock);
			return (NULL);
		}
	}

	/* NOTREACHED */
}

/*
 * Data structure used to communicate event subscription cache updates
 * to publishers via a registration door
 */
struct reg_args {
	uint32_t ra_sub_id;
	uint32_t ra_op;
	uint64_t ra_buf_ptr;
};


/*
 * event_deliver_service - generic event delivery service routine.  This routine
 *		is called in response to a door call to post an event.
 *
 */
/*ARGSUSED*/
static void
event_deliver_service(void *cookie, char *args, size_t alen,
    door_desc_t *ddp, uint_t ndid)
{
	int	ret = 0;
	subscriber_priv_t *sub_info;
	sysevent_handle_t *shp;
	sysevent_queue_t *new_eq;

	if (args == NULL || alen < sizeof (uint32_t)) {
		ret = EINVAL;
		goto return_from_door;
	}

	/* Publisher checking on subscriber */
	if (alen == sizeof (uint32_t)) {
		ret = 0;
		goto return_from_door;
	}

	shp = (sysevent_handle_t *)cookie;
	if (shp == NULL) {
		ret = EBADF;
		goto return_from_door;
	}

	/*
	 * Mustn't block if we are trying to update the registration with
	 * the publisher
	 */
	if (mutex_trylock(SH_LOCK(shp)) != 0) {
		ret = EAGAIN;
		goto return_from_door;
	}

	if (!SH_BOUND(shp)) {
		ret = EBADF;
		(void) mutex_unlock(SH_LOCK(shp));
		goto return_from_door;
	}

	sub_info = (subscriber_priv_t *)SH_PRIV_DATA(shp);
	if (sub_info == NULL) {
		ret = EBADF;
		(void) mutex_unlock(SH_LOCK(shp));
		goto return_from_door;
	}

	new_eq = (sysevent_queue_t *)calloc(1,
	    sizeof (sysevent_queue_t));
	if (new_eq == NULL) {
		ret = EAGAIN;
		(void) mutex_unlock(SH_LOCK(shp));
		goto return_from_door;
	}

	/*
	 * Allocate and copy the event buffer into the subscriber's
	 * address space
	 */
	new_eq->sq_ev = calloc(1, alen);
	if (new_eq->sq_ev == NULL) {
		free(new_eq);
		ret = EAGAIN;
		(void) mutex_unlock(SH_LOCK(shp));
		goto return_from_door;
	}
	(void) bcopy(args, new_eq->sq_ev, alen);

	(void) mutex_lock(&sub_info->sp_qlock);
	if (sub_info->sp_evq_head == NULL) {
		sub_info->sp_evq_head = new_eq;
	} else {
		sub_info->sp_evq_tail->sq_next = new_eq;
	}
	sub_info->sp_evq_tail = new_eq;

	(void) cond_signal(&sub_info->sp_cv);
	(void) mutex_unlock(&sub_info->sp_qlock);
	(void) mutex_unlock(SH_LOCK(shp));

return_from_door:
	(void) door_return((void *)&ret, sizeof (ret), NULL, 0);
	(void) door_return(NULL, 0, NULL, 0);
}

/*
 * Sysevent subscription information is maintained in the kernel.  Updates
 * to the in-kernel registration database is expected to be infrequent and
 * offers consistency for publishers and subscribers that may come and go
 * for a given channel.
 *
 * To expedite registration lookups by publishers, a cached copy of the
 * kernel registration database is kept per-channel.  Caches are invalidated
 * and refreshed upon state changes to the in-kernel registration database.
 *
 * To prevent stale subscriber data, publishers may remove subsriber
 * registrations from the in-kernel registration database in the event
 * that a particular subscribing process is unresponsive.
 *
 * The following routines provide a mechanism to update publisher and subscriber
 * information for a specified channel.
 */

/*
 * clnt_deliver_event - Deliver an event through the consumer's event
 *			delivery door
 *
 * Returns -1 if message not delivered. With errno set to cause of error.
 * Returns 0 for success with the results returned in posting buffer.
 */
static int
clnt_deliver_event(int service_door, void *data, size_t datalen,
    void *result, size_t rlen)
{
	int error = 0;
	door_arg_t door_arg;

	door_arg.rbuf = result;
	door_arg.rsize = rlen;
	door_arg.data_ptr = data;
	door_arg.data_size = datalen;
	door_arg.desc_ptr = NULL;
	door_arg.desc_num = 0;

	/*
	 * Make door call
	 */
	while ((error = door_call(service_door, &door_arg)) != 0) {
		if (errno == EAGAIN || errno == EINTR) {
			continue;
		} else {
			error = errno;
			break;
		}
	}

	return (error);
}

static int
update_publisher_cache(subscriber_priv_t *sub_info, int update_op,
    uint32_t sub_id, size_t datasz, uchar_t *data)
{
	int pub_fd;
	uint32_t result = 0;
	struct reg_args *rargs;

	rargs = (struct reg_args *)calloc(1, sizeof (struct reg_args) +
	    datasz);
	if (rargs == NULL) {
		errno = ENOMEM;
		return (-1);
	}

	rargs->ra_sub_id = sub_id;
	rargs->ra_op = update_op;
	bcopy(data, (char *)&rargs->ra_buf_ptr, datasz);

	pub_fd = open(sub_info->sp_door_name, O_RDONLY);
	(void) clnt_deliver_event(pub_fd, (void *)rargs,
	    sizeof (struct reg_args) + datasz, &result, sizeof (result));
	(void) close(pub_fd);

	free(rargs);
	if (result != 0) {
		errno = result;
		return (-1);
	}

	return (0);
}


/*
 * update_kernel_registration - update the in-kernel registration for the
 * given channel.
 */
static int
update_kernel_registration(sysevent_handle_t *shp, int update_type,
    int update_op, uint32_t *sub_id, size_t datasz, uchar_t *data)
{
	int error;
	char *channel_name = SH_CHANNEL_NAME(shp);
	se_pubsub_t udata;

	udata.ps_channel_name_len = strlen(channel_name) + 1;
	udata.ps_op = update_op;
	udata.ps_type = update_type;
	udata.ps_buflen = datasz;
	udata.ps_id = *sub_id;

	if ((error = modctl(MODEVENTS, (uintptr_t)MODEVENTS_REGISTER_EVENT,
	    (uintptr_t)channel_name, (uintptr_t)data, (uintptr_t)&udata, 0))
	    != 0) {
		return (error);
	}

	*sub_id = udata.ps_id;

	return (error);
}

/*
 * get_kernel_registration - get the current subscriber registration for
 * the given channel
 */
static nvlist_t *
get_kernel_registration(char *channel_name, uint32_t class_id)
{
	char *nvlbuf;
	nvlist_t *nvl;
	se_pubsub_t udata;

	nvlbuf = calloc(1, MAX_SUBSCRIPTION_SZ);
	if (nvlbuf == NULL) {
		return (NULL);
	}

	udata.ps_buflen = MAX_SUBSCRIPTION_SZ;
	udata.ps_channel_name_len = strlen(channel_name) + 1;
	udata.ps_id = class_id;
	udata.ps_op = SE_GET_REGISTRATION;
	udata.ps_type = PUBLISHER;

	if (modctl(MODEVENTS, (uintptr_t)MODEVENTS_REGISTER_EVENT,
	    (uintptr_t)channel_name, (uintptr_t)nvlbuf, (uintptr_t)&udata, 0)
	    != 0) {

		/* Need a bigger buffer to hold channel registration */
		if (errno == EAGAIN) {
			free(nvlbuf);
			nvlbuf = calloc(1, udata.ps_buflen);
			if (nvlbuf == NULL)
				return (NULL);

			/* Try again */
			if (modctl(MODEVENTS,
			    (uintptr_t)MODEVENTS_REGISTER_EVENT,
			    (uintptr_t)channel_name, (uintptr_t)nvlbuf,
			    (uintptr_t)&udata, 0) != 0) {
				free(nvlbuf);
				return (NULL);
			}
		} else {
			free(nvlbuf);
			return (NULL);
		}
	}

	if (nvlist_unpack(nvlbuf, udata.ps_buflen, &nvl, 0) != 0) {
		free(nvlbuf);
		return (NULL);
	}
	free(nvlbuf);

	return (nvl);
}

/*
 * The following routines provide a mechanism for publishers to maintain
 * subscriber information.
 */

static void
dealloc_subscribers(sysevent_handle_t *shp)
{
	int i;
	subscriber_data_t *sub;

	for (i = 1; i <= MAX_SUBSCRIBERS; ++i) {
		sub = SH_SUBSCRIBER(shp, i);
		if (sub != NULL) {
			free(sub->sd_door_name);
			free(sub);
		}
		SH_SUBSCRIBER(shp, i) = NULL;
	}
}

/*ARGSUSED*/
static int
alloc_subscriber(sysevent_handle_t *shp, uint32_t sub_id, int oflag)
{
	subscriber_data_t *sub;
	char door_name[MAXPATHLEN];

	if (SH_SUBSCRIBER(shp, sub_id) != NULL) {
		return (0);
	}

	/* Allocate and initialize the subscriber data */
	sub = (subscriber_data_t *)calloc(1,
	    sizeof (subscriber_data_t));
	if (sub == NULL) {
		return (-1);
	}
	if (snprintf(door_name, MAXPATHLEN, "%s/%d",
	    SH_CHANNEL_PATH(shp), sub_id) >= MAXPATHLEN) {
		free(sub);
		return (-1);
	}

	sub->sd_flag = ACTIVE;
	sub->sd_door_name = strdup(door_name);
	if (sub->sd_door_name == NULL) {
		free(sub);
		return (-1);
	}

	SH_SUBSCRIBER(shp, sub_id) = sub;
	return (0);

}

/*
 * The following routines are used to update and maintain the registration cache
 * for a particular sysevent channel.
 */

static uint32_t
hash_func(const char *s)
{
	uint32_t result = 0;
	uint_t g;

	while (*s != '\0') {
		result <<= 4;
		result += (uint32_t)*s++;
		g = result & 0xf0000000;
		if (g != 0) {
			result ^= g >> 24;
			result ^= g;
		}
	}

	return (result);
}

subclass_lst_t *
cache_find_subclass(class_lst_t *c_list, char *subclass)
{
	subclass_lst_t *sc_list;

	if (c_list == NULL)
		return (NULL);

	sc_list = c_list->cl_subclass_list;

	while (sc_list != NULL) {
		if (strcmp(sc_list->sl_name, subclass) == 0) {
			return (sc_list);
		}
		sc_list = sc_list->sl_next;
	}

	return (NULL);
}


static class_lst_t *
cache_find_class(sysevent_handle_t *shp, char *class)
{
	int index;
	class_lst_t *c_list;
	class_lst_t **class_hash = SH_CLASS_HASH(shp);

	if (strcmp(class, EC_ALL) == 0) {
		return (class_hash[0]);
	}

	index = CLASS_HASH(class);
	c_list = class_hash[index];
	while (c_list != NULL) {
		if (strcmp(class, c_list->cl_name) == 0) {
			break;
		}
		c_list = c_list->cl_next;
	}

	return (c_list);
}

static int
cache_insert_subclass(class_lst_t *c_list, char **subclass_names,
    int subclass_num, uint32_t sub_id)
{
	int i;
	subclass_lst_t *sc_list;

	for (i = 0; i < subclass_num; ++i) {
		if ((sc_list = cache_find_subclass(c_list, subclass_names[i]))
		    != NULL) {
			sc_list->sl_num[sub_id] = 1;
		} else {
			sc_list = (subclass_lst_t *)calloc(1,
			    sizeof (subclass_lst_t));
			if (sc_list == NULL)
				return (-1);

			sc_list->sl_name = strdup(subclass_names[i]);
			if (sc_list->sl_name == NULL) {
				free(sc_list);
				return (-1);
			}

			sc_list->sl_num[sub_id] = 1;
			sc_list->sl_next = c_list->cl_subclass_list;
			c_list->cl_subclass_list = sc_list;
		}
	}

	return (0);
}

static int
cache_insert_class(sysevent_handle_t *shp, char *class,
    char **subclass_names, int subclass_num, uint32_t sub_id)
{
	class_lst_t *c_list;

	if (strcmp(class, EC_ALL) == 0) {
		char *subclass_all = EC_SUB_ALL;

		(void) cache_insert_subclass(SH_CLASS_HASH(shp)[0],
		    (char **)&subclass_all, 1, sub_id);
		return (0);
	}

	/* New class, add to the registration cache */
	if ((c_list = cache_find_class(shp, class)) == NULL) {

		c_list = (class_lst_t *)calloc(1, sizeof (class_lst_t));
		if (c_list == NULL) {
			return (1);
		}
		c_list->cl_name = strdup(class);
		if (c_list->cl_name == NULL) {
			free(c_list);
			return (1);
		}

		c_list->cl_subclass_list = (subclass_lst_t *)
		    calloc(1, sizeof (subclass_lst_t));
		if (c_list->cl_subclass_list == NULL) {
			free(c_list->cl_name);
			free(c_list);
			return (1);
		}
		c_list->cl_subclass_list->sl_name = strdup(EC_SUB_ALL);
		if (c_list->cl_subclass_list->sl_name == NULL) {
			free(c_list->cl_subclass_list);
			free(c_list->cl_name);
			free(c_list);
			return (1);
		}
		c_list->cl_next = SH_CLASS_HASH(shp)[CLASS_HASH(class)];
		SH_CLASS_HASH(shp)[CLASS_HASH(class)] = c_list;

	}

	/* Update the subclass list */
	if (cache_insert_subclass(c_list, subclass_names, subclass_num,
	    sub_id) != 0)
		return (1);

	return (0);
}

static void
cache_remove_all_class(sysevent_handle_t *shp, uint32_t sub_id)
{
	int i;
	class_lst_t *c_list;
	subclass_lst_t *sc_list;

	for (i = 0; i < CLASS_HASH_SZ + 1; ++i) {
		c_list = SH_CLASS_HASH(shp)[i];
		while (c_list != NULL) {
			sc_list = c_list->cl_subclass_list;
			while (sc_list != NULL) {
				sc_list->sl_num[sub_id] = 0;
				sc_list = sc_list->sl_next;
			}
			c_list = c_list->cl_next;
		}
	}
}

static void
cache_remove_class(sysevent_handle_t *shp, char *class, uint32_t sub_id)
{
	class_lst_t *c_list;
	subclass_lst_t *sc_list;

	if (strcmp(class, EC_ALL) == 0) {
		cache_remove_all_class(shp, sub_id);
		return;
	}

	if ((c_list = cache_find_class(shp, class)) == NULL) {
		return;
	}

	sc_list = c_list->cl_subclass_list;
	while (sc_list != NULL) {
		sc_list->sl_num[sub_id] = 0;
		sc_list = sc_list->sl_next;
	}
}

static void
free_cached_registration(sysevent_handle_t *shp)
{
	int i;
	class_lst_t *clist, *next_clist;
	subclass_lst_t *sc_list, *next_sc;

	for (i = 0; i < CLASS_HASH_SZ + 1; i++) {
		clist = SH_CLASS_HASH(shp)[i];
		while (clist != NULL) {
			sc_list = clist->cl_subclass_list;
			while (sc_list != NULL) {
				free(sc_list->sl_name);
				next_sc = sc_list->sl_next;
				free(sc_list);
				sc_list = next_sc;
			}
			free(clist->cl_name);
			next_clist = clist->cl_next;
			free(clist);
			clist = next_clist;
		}
		SH_CLASS_HASH(shp)[i] = NULL;
	}
}

static int
create_cached_registration(sysevent_handle_t *shp,
    class_lst_t **class_hash)
{
	int i, j, new_class;
	char *class_name;
	uint_t num_elem;
	uchar_t *subscribers;
	nvlist_t *nvl;
	nvpair_t *nvpair;
	class_lst_t *clist;
	subclass_lst_t *sc_list;

	for (i = 0; i < CLASS_HASH_SZ + 1; ++i) {

		if ((nvl = get_kernel_registration(SH_CHANNEL_NAME(shp), i))
		    == NULL) {
			if (errno == ENOENT) {
				class_hash[i] = NULL;
				continue;
			} else {
				goto create_failed;
			}
		}


		nvpair = NULL;
		if ((nvpair = nvlist_next_nvpair(nvl, nvpair)) == NULL) {
			goto create_failed;
		}

		new_class = 1;
		while (new_class) {
			/* Extract the class name from the nvpair */
			if (nvpair_value_string(nvpair, &class_name) != 0) {
				goto create_failed;
			}
			clist = (class_lst_t *)
			    calloc(1, sizeof (class_lst_t));
			if (clist == NULL) {
				goto create_failed;
			}

			clist->cl_name = strdup(class_name);
			if (clist->cl_name == NULL) {
				free(clist);
				goto create_failed;
			}

			/*
			 * Extract the subclass name and registration
			 * from the nvpair
			 */
			if ((nvpair = nvlist_next_nvpair(nvl, nvpair))
			    == NULL) {
				free(clist->cl_name);
				free(clist);
				goto create_failed;
			}

			clist->cl_next = class_hash[i];
			class_hash[i] = clist;

			for (;;) {

				sc_list = (subclass_lst_t *)calloc(1,
				    sizeof (subclass_lst_t));
				if (sc_list == NULL) {
					goto create_failed;
				}

				sc_list->sl_next = clist->cl_subclass_list;
				clist->cl_subclass_list = sc_list;

				sc_list->sl_name = strdup(nvpair_name(nvpair));
				if (sc_list->sl_name == NULL) {
					goto create_failed;
				}

				if (nvpair_value_byte_array(nvpair,
				    &subscribers, &num_elem) != 0) {
					goto create_failed;
				}
				bcopy(subscribers, (uchar_t *)sc_list->sl_num,
				    MAX_SUBSCRIBERS + 1);

				for (j = 1; j <= MAX_SUBSCRIBERS; ++j) {
					if (sc_list->sl_num[j] == 0)
						continue;

					if (alloc_subscriber(shp, j, 1) != 0) {
						goto create_failed;
					}
				}

				/*
				 * Check next nvpair - either subclass or
				 * class
				 */
				if ((nvpair = nvlist_next_nvpair(nvl, nvpair))
				    == NULL) {
					new_class = 0;
					break;
				} else if (strcmp(nvpair_name(nvpair),
				    CLASS_NAME) == 0) {
					break;
				}
			}
		}
		nvlist_free(nvl);
	}
	return (0);

create_failed:
	dealloc_subscribers(shp);
	free_cached_registration(shp);
	nvlist_free(nvl);
	return (-1);

}

/*
 * cache_update_service - generic event publisher service routine.  This routine
 *		is called in response to a registration cache update.
 *
 */
/*ARGSUSED*/
static void
cache_update_service(void *cookie, char *args, size_t alen,
    door_desc_t *ddp, uint_t ndid)
{
	int ret = 0;
	uint_t num_elem;
	char *class, **event_list;
	size_t datalen;
	uint32_t sub_id;
	nvlist_t *nvl;
	nvpair_t *nvpair = NULL;
	struct reg_args *rargs;
	sysevent_handle_t *shp;
	subscriber_data_t *sub;

	if (alen < sizeof (struct reg_args) || cookie == NULL) {
		ret = EINVAL;
		goto return_from_door;
	}

	/* LINTED: E_BAD_PTR_CAST_ALIGN */
	rargs = (struct reg_args *)args;
	shp = (sysevent_handle_t *)cookie;

	datalen = alen - sizeof (struct reg_args);
	sub_id = rargs->ra_sub_id;

	(void) mutex_lock(SH_LOCK(shp));

	switch (rargs->ra_op) {
	case SE_UNREGISTER:
		class = (char *)&rargs->ra_buf_ptr;
		cache_remove_class(shp, (char *)class,
		    sub_id);
		break;
	case SE_UNBIND_REGISTRATION:

		sub = SH_SUBSCRIBER(shp, sub_id);
		if (sub == NULL)
			break;

		free(sub->sd_door_name);
		free(sub);
		cache_remove_class(shp, EC_ALL, sub_id);
		SH_SUBSCRIBER(shp, sub_id) = NULL;

		break;
	case SE_BIND_REGISTRATION:

		/* New subscriber */
		if (alloc_subscriber(shp, sub_id, 0) != 0) {
			ret = ENOMEM;
			break;
		}
		break;
	case SE_REGISTER:

		if (SH_SUBSCRIBER(shp, sub_id) == NULL) {
			ret = EINVAL;
			break;
		}
		/* Get new registration data */
		if (nvlist_unpack((char *)&rargs->ra_buf_ptr, datalen,
		    &nvl, 0) != 0) {
			ret =  EFAULT;
			break;
		}
		if ((nvpair = nvlist_next_nvpair(nvl, nvpair)) == NULL) {
			nvlist_free(nvl);
			ret = EFAULT;
			break;
		}
		if (nvpair_value_string_array(nvpair, &event_list, &num_elem)
		    != 0) {
			nvlist_free(nvl);
			ret =  EFAULT;
			break;
		}
		class = nvpair_name(nvpair);

		ret = cache_insert_class(shp, class,
		    event_list, num_elem, sub_id);
		if (ret != 0) {
			cache_remove_class(shp, class, sub_id);
			nvlist_free(nvl);
			ret =  EFAULT;
			break;
		}

		nvlist_free(nvl);

		break;
	case SE_CLEANUP:
		/* Cleanup stale subscribers */
		sysevent_cleanup_subscribers(shp);
		break;
	default:
		ret =  EINVAL;
	}

	(void) mutex_unlock(SH_LOCK(shp));

return_from_door:
	(void) door_return((void *)&ret, sizeof (ret), NULL, 0);
	(void) door_return(NULL, 0, NULL, 0);
}

/*
 * sysevent_send_event -
 * Send an event via the communication channel associated with the sysevent
 * handle.  Event notifications are broadcast to all subscribers based upon
 * the event class and subclass.  The handle must have been previously
 * allocated and bound by
 * sysevent_open_channel() and sysevent_bind_publisher()
 */
int
sysevent_send_event(sysevent_handle_t *shp, sysevent_t *ev)
{
	int i, error, sub_fd, result = 0;
	int deliver_error = 0;
	int subscribers_sent = 0;
	int want_resend, resend_cnt = 0;
	char *event_class, *event_subclass;
	uchar_t *all_class_subscribers, *all_subclass_subscribers;
	uchar_t *subclass_subscribers;
	subscriber_data_t *sub;
	subclass_lst_t *sc_lst;

	/* Check for proper registration */
	event_class = sysevent_get_class_name(ev);
	event_subclass = sysevent_get_subclass_name(ev);

	(void) mutex_lock(SH_LOCK(shp));

send_event:

	want_resend = 0;
	if (!SH_BOUND(shp)) {
		(void) mutex_unlock(SH_LOCK(shp));
		errno = EINVAL;
		return (-1);
	}

	/* Find all subscribers for this event class/subclass */
	sc_lst = cache_find_subclass(
	    cache_find_class(shp, EC_ALL), EC_SUB_ALL);
	all_class_subscribers = sc_lst->sl_num;

	sc_lst = cache_find_subclass(
	    cache_find_class(shp, event_class), EC_SUB_ALL);
	if (sc_lst)
		all_subclass_subscribers = sc_lst->sl_num;
	else
		all_subclass_subscribers = NULL;

	sc_lst = cache_find_subclass(
	    cache_find_class(shp, event_class), event_subclass);
	if (sc_lst)
		subclass_subscribers = sc_lst->sl_num;
	else
		subclass_subscribers = NULL;

	/* Send event buffer to all valid subscribers */
	for (i = 1; i <= MAX_SUBSCRIBERS; ++i) {
		if ((all_class_subscribers[i] |
		    (all_subclass_subscribers && all_subclass_subscribers[i]) |
		    (subclass_subscribers && subclass_subscribers[i])) == 0)
			continue;

		sub = SH_SUBSCRIBER(shp, i);
		assert(sub != NULL);

		/* Check for active subscriber */
		if (!(sub->sd_flag & ACTIVE)) {
			dprint("sysevent_send_event: subscriber %d inactive\n",
			    i);
			continue;
		}

		/* Process only resend requests */
		if (resend_cnt > 0 && !(sub->sd_flag & SEND_AGAIN)) {
			continue;
		}

		if ((sub_fd = open(sub->sd_door_name, O_RDONLY)) == -1) {
			dprint("sysevent_send_event: Failed to open "
			    "%s: %s\n", sub->sd_door_name, strerror(errno));
			continue;
		}
		result = 0;
		error = clnt_deliver_event(sub_fd, ev,
		    sysevent_get_size(ev), &result, sizeof (result));

		(void) close(sub_fd);

		/* Successful door call */
		if (error == 0) {
			switch (result) {
			/* Subscriber requested EAGAIN */
			case EAGAIN:
				if (resend_cnt > SE_MAX_RETRY_LIMIT) {
					deliver_error = 1;
				} else {
					want_resend = 1;
					dprint("sysevent_send_event: resend "
					    "requested for %d\n", i);
					sub->sd_flag |= SEND_AGAIN;
				}
				break;
			/* Bad sysevent handle for subscriber */
			case EBADF:
			case EINVAL:
				dprint("sysevent_send_event: Bad sysevent "
				    "handle for %s", sub->sd_door_name);
				sub->sd_flag = 0;
				deliver_error = 1;
				break;
			/* Successful delivery */
			default:
				sub->sd_flag &= ~SEND_AGAIN;
				++subscribers_sent;
			}
		} else {
			dprint("sysevent_send_event: Failed door call "
			    "to %s: %s: %d\n", sub->sd_door_name,
			    strerror(errno), result);
			sub->sd_flag = 0;
			deliver_error = 1;
		}
	}

	if (want_resend) {
		resend_cnt++;
		goto send_event;
	}

	if (deliver_error) {
		sysevent_cleanup_subscribers(shp);
		(void) mutex_unlock(SH_LOCK(shp));
		errno = EFAULT;
		return (-1);
	}

	(void) mutex_unlock(SH_LOCK(shp));

	if (subscribers_sent == 0) {
		dprint("sysevent_send_event: No subscribers for %s:%s\n",
		    event_class, event_subclass);
		errno = ENOENT;
		return (-1);
	}

	return (0);
}

/*
 * Common routine to establish an event channel through which an event
 * publisher or subscriber may post or receive events.
 */
static sysevent_handle_t *
sysevent_open_channel_common(const char *channel_path)
{
	uint32_t sub_id = 0;
	char *begin_path;
	struct stat chan_stat;
	sysevent_handle_t *shp;


	if (channel_path == NULL || strlen(channel_path) + 1 > MAXPATHLEN) {
		errno = EINVAL;
		return (NULL);
	}

	if (mkdir(channel_path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0) {
		if (errno != EEXIST) {
			errno = EACCES;
			return (NULL);
		}
	}

	/* Check channel file permissions */
	if (stat(channel_path, &chan_stat) != 0) {
		dprint("sysevent_open_channel: Invalid permissions for channel "
		    "%s\n", channel_path);
		errno = EACCES;
		return (NULL);
	} else if (chan_stat.st_uid != getuid() ||
	    !S_ISDIR(chan_stat.st_mode)) {
		dprint("sysevent_open_channel: Invalid "
		    "permissions for channel %s\n: %d:%d:%d", channel_path,
		    (int)chan_stat.st_uid, (int)chan_stat.st_gid,
		    (int)chan_stat.st_mode);

		errno = EACCES;
		return (NULL);
	}

	shp = calloc(1, sizeof (sysevent_impl_hdl_t));
	if (shp == NULL) {
		errno = ENOMEM;
		return (NULL);
	}

	SH_CHANNEL_NAME(shp) = NULL;
	SH_CHANNEL_PATH(shp) = strdup(channel_path);
	if (SH_CHANNEL_PATH(shp) == NULL) {
		free(shp);
		errno = ENOMEM;
		return (NULL);
	}

	/* Extract the channel name */
	begin_path = SH_CHANNEL_PATH(shp);
	while (*begin_path != '\0' &&
	    (begin_path = strpbrk(begin_path, "/")) != NULL) {
		++begin_path;
		SH_CHANNEL_NAME(shp) = begin_path;
	}

	if (update_kernel_registration(shp, 0,
	    SE_OPEN_REGISTRATION, &sub_id, 0, NULL) != 0) {
		dprint("sysevent_open_channel: Failed for channel %s\n",
		    SH_CHANNEL_NAME(shp));
		free(SH_CHANNEL_PATH(shp));
		free(shp);
		errno = EFAULT;
		return (NULL);
	}

	(void) mutex_init(SH_LOCK(shp), USYNC_THREAD, NULL);

	return (shp);
}

/*
 * Establish a sysevent channel for publication and subscription
 */
sysevent_handle_t *
sysevent_open_channel(const char *channel)
{
	int var_run_mounted = 0;
	char full_channel[MAXPATHLEN + 1];
	FILE *fp;
	struct stat chan_stat;
	struct extmnttab m;

	if (channel == NULL) {
		errno = EINVAL;
		return (NULL);
	}

	/*
	 * Check that /var/run is mounted as tmpfs before allowing a channel
	 * to be opened.
	 */
	if ((fp = fopen(MNTTAB, "rF")) == NULL) {
		errno = EACCES;
		return (NULL);
	}

	resetmnttab(fp);

	while (getextmntent(fp, &m, sizeof (struct extmnttab)) == 0) {
		if (strcmp(m.mnt_mountp, "/var/run") == 0 &&
		    strcmp(m.mnt_fstype, "tmpfs") == 0) {
			var_run_mounted = 1;
			break;
		}
	}
	(void) fclose(fp);

	if (!var_run_mounted) {
		errno = EACCES;
		return (NULL);
	}

	if (stat(CHAN_PATH, &chan_stat) < 0) {
		if (mkdir(CHAN_PATH,
		    S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0) {
			dprint("sysevent_open_channel: Unable "
			    "to create channel directory %s:%s\n", CHAN_PATH,
			    strerror(errno));
			if (errno != EEXIST) {
				errno = EACCES;
				return (NULL);
			}
		}
	}

	if (snprintf(full_channel, MAXPATHLEN, "%s/%s", CHAN_PATH, channel) >=
	    MAXPATHLEN) {
		errno = EINVAL;
		return (NULL);
	}

	return (sysevent_open_channel_common(full_channel));
}

/*
 * Establish a sysevent channel for publication and subscription
 * Full path to the channel determined by the caller
 */
sysevent_handle_t *
sysevent_open_channel_alt(const char *channel_path)
{
	return (sysevent_open_channel_common(channel_path));
}

/*
 * sysevent_close_channel - Clean up resources associated with a previously
 *				opened sysevent channel
 */
void
sysevent_close_channel(sysevent_handle_t *shp)
{
	int error = errno;
	uint32_t sub_id = 0;

	if (shp == NULL) {
		return;
	}

	(void) mutex_lock(SH_LOCK(shp));
	if (SH_BOUND(shp)) {
		(void) mutex_unlock(SH_LOCK(shp));
		if (SH_TYPE(shp) == PUBLISHER)
			sysevent_unbind_publisher(shp);
		else if (SH_TYPE(shp) == SUBSCRIBER)
			sysevent_unbind_subscriber(shp);
		(void) mutex_lock(SH_LOCK(shp));
	}

	(void) update_kernel_registration(shp, 0,
	    SE_CLOSE_REGISTRATION, &sub_id, 0, NULL);
	(void) mutex_unlock(SH_LOCK(shp));

	free(SH_CHANNEL_PATH(shp));
	free(shp);
	errno = error;
}

/*
 * sysevent_bind_publisher - Bind an event publisher to an event channel
 */
int
sysevent_bind_publisher(sysevent_handle_t *shp)
{
	int error = 0;
	int fd = -1;
	char door_name[MAXPATHLEN];
	uint32_t pub_id;
	struct stat reg_stat;
	publisher_priv_t *pub;

	if (shp == NULL) {
		errno = EINVAL;
		return (-1);
	}

	(void) mutex_lock(SH_LOCK(shp));
	if (SH_BOUND(shp)) {
		(void) mutex_unlock(SH_LOCK(shp));
		errno = EINVAL;
		return (-1);
	}

	if ((pub = (publisher_priv_t *)calloc(1, sizeof (publisher_priv_t))) ==
	    NULL) {
		(void) mutex_unlock(SH_LOCK(shp));
		errno = ENOMEM;
		return (-1);
	}
	SH_PRIV_DATA(shp) = (void *)pub;

	if (snprintf(door_name, MAXPATHLEN, "%s/%s",
	    SH_CHANNEL_PATH(shp), REG_DOOR) >= MAXPATHLEN) {
		free(pub);
		(void) mutex_unlock(SH_LOCK(shp));
		errno = ENOMEM;
		return (-1);
	}
	if ((SH_DOOR_NAME(shp) = strdup(door_name)) == NULL) {
		free(pub);
		(void) mutex_unlock(SH_LOCK(shp));
		errno = ENOMEM;
		return (-1);
	}

	/* Only one publisher allowed per channel */
	if (stat(SH_DOOR_NAME(shp), &reg_stat) != 0) {
		if (errno != ENOENT) {
			error = EINVAL;
			goto fail;
		}
	}

	/*
	 * Remove door file for robustness.
	 */
	if (unlink(SH_DOOR_NAME(shp)) != 0)
		dprint("sysevent_bind_publisher: Unlink of %s failed.\n",
		    SH_DOOR_NAME(shp));

	/* Open channel registration door */
	fd = open(SH_DOOR_NAME(shp), O_CREAT|O_RDWR,
	    S_IREAD|S_IWRITE);
	if (fd == -1) {
		error = EINVAL;
		goto fail;
	}

	/*
	 * Create the registration service for this publisher.
	 */
	if ((SH_DOOR_DESC(shp) = door_create(cache_update_service,
	    (void *)shp, DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
		dprint("sysevent_bind_publisher: door create failed: "
		    "%s\n", strerror(errno));
		error = EFAULT;
		goto fail;
	}

	(void) fdetach(SH_DOOR_NAME(shp));
	if (fattach(SH_DOOR_DESC(shp), SH_DOOR_NAME(shp)) != 0) {
		dprint("sysevent_bind_publisher: unable to "
		    "bind event channel: fattach: %s\n",
		    SH_DOOR_NAME(shp));
		error = EACCES;
		goto fail;
	}

	/* Bind this publisher in the kernel registration database */
	if (update_kernel_registration(shp, PUBLISHER,
	    SE_BIND_REGISTRATION, &pub_id, 0, NULL) != 0) {
		error = errno;
		goto fail;
	}

	SH_ID(shp) = pub_id;
	SH_BOUND(shp) = 1;
	SH_TYPE(shp) = PUBLISHER;


	/* Create the subscription registration cache */
	if (create_cached_registration(shp, SH_CLASS_HASH(shp)) != 0) {
		(void) update_kernel_registration(shp,
		    PUBLISHER, SE_UNBIND_REGISTRATION, &pub_id, 0, NULL);
		error = EFAULT;
		goto fail;
	}
	(void) close(fd);

	(void) mutex_unlock(SH_LOCK(shp));

	return (0);

fail:
	SH_BOUND(shp) = 0;
	(void) door_revoke(SH_DOOR_DESC(shp));
	(void) fdetach(SH_DOOR_NAME(shp));
	free(SH_DOOR_NAME(shp));
	free(pub);
	(void) close(fd);
	(void) mutex_unlock(SH_LOCK(shp));
	errno = error;
	return (-1);
}

static pthread_once_t xdoor_thrattr_once = PTHREAD_ONCE_INIT;
static pthread_attr_t xdoor_thrattr;

static void
xdoor_thrattr_init(void)
{
	(void) pthread_attr_init(&xdoor_thrattr);
	(void) pthread_attr_setdetachstate(&xdoor_thrattr,
	    PTHREAD_CREATE_DETACHED);
	(void) pthread_attr_setscope(&xdoor_thrattr, PTHREAD_SCOPE_SYSTEM);
}

static int
xdoor_server_create(door_info_t *dip, void *(*startf)(void *),
    void *startfarg, void *cookie)
{
	struct sysevent_subattr_impl *xsa = cookie;
	pthread_attr_t *thrattr;
	sigset_t oset;
	int err;

	if (xsa->xs_thrcreate) {
		return (xsa->xs_thrcreate(dip, startf, startfarg,
		    xsa->xs_thrcreate_cookie));
	}

	if (xsa->xs_thrattr == NULL) {
		(void) pthread_once(&xdoor_thrattr_once, xdoor_thrattr_init);
		thrattr = &xdoor_thrattr;
	} else {
		thrattr = xsa->xs_thrattr;
	}

	(void) pthread_sigmask(SIG_SETMASK, &xsa->xs_sigmask, &oset);
	err = pthread_create(NULL, thrattr, startf, startfarg);
	(void) pthread_sigmask(SIG_SETMASK, &oset, NULL);

	return (err == 0 ? 1 : -1);
}

static void
xdoor_server_setup(void *cookie)
{
	struct sysevent_subattr_impl *xsa = cookie;

	if (xsa->xs_thrsetup) {
		xsa->xs_thrsetup(xsa->xs_thrsetup_cookie);
	} else {
		(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
		(void) pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
	}
}

static int
sysevent_bind_subscriber_cmn(sysevent_handle_t *shp,
    void (*event_handler)(sysevent_t *ev),
    sysevent_subattr_t *subattr)
{
	int fd = -1;
	int error = 0;
	uint32_t sub_id = 0;
	char door_name[MAXPATHLEN];
	subscriber_priv_t *sub_info;
	int created;
	struct sysevent_subattr_impl *xsa =
	    (struct sysevent_subattr_impl *)subattr;

	if (shp == NULL || event_handler == NULL) {
		errno = EINVAL;
		return (-1);
	}

	(void) mutex_lock(SH_LOCK(shp));
	if (SH_BOUND(shp)) {
		errno = EINVAL;
		(void) mutex_unlock(SH_LOCK(shp));
		return (-1);
	}

	if ((sub_info = (subscriber_priv_t *)calloc(1,
	    sizeof (subscriber_priv_t))) == NULL) {
		errno = ENOMEM;
		(void) mutex_unlock(SH_LOCK(shp));
		return (-1);
	}

	if (snprintf(door_name, MAXPATHLEN, "%s/%s",
	    SH_CHANNEL_PATH(shp), REG_DOOR) >= MAXPATHLEN) {
		free(sub_info);
		errno = EINVAL;
		(void) mutex_unlock(SH_LOCK(shp));
		return (-1);
	}

	if ((sub_info->sp_door_name = strdup(door_name)) == NULL) {
		free(sub_info);
		errno = ENOMEM;
		(void) mutex_unlock(SH_LOCK(shp));
		return (-1);
	}
	(void) cond_init(&sub_info->sp_cv, USYNC_THREAD, NULL);
	(void) mutex_init(&sub_info->sp_qlock, USYNC_THREAD, NULL);
	sub_info->sp_func = event_handler;

	/* Update the in-kernel registration */
	if (update_kernel_registration(shp, SUBSCRIBER,
	    SE_BIND_REGISTRATION, &sub_id, 0, NULL) != 0) {
		error = errno;
		goto fail;
	}
	SH_ID(shp) = sub_id;

	if (snprintf(door_name, MAXPATHLEN, "%s/%d",
	    SH_CHANNEL_PATH(shp), sub_id) >= MAXPATHLEN) {
		error = EINVAL;
		goto fail;
	}
	if ((SH_DOOR_NAME(shp) = strdup(door_name)) == NULL) {
		error = ENOMEM;
		goto fail;
	}

	/*
	 * Remove door file for robustness.
	 */
	if (unlink(SH_DOOR_NAME(shp)) != 0)
		dprint("sysevent_bind_subscriber: Unlink of %s failed.\n",
		    SH_DOOR_NAME(shp));

	fd = open(SH_DOOR_NAME(shp), O_CREAT|O_RDWR, S_IREAD|S_IWRITE);
	if (fd == -1) {
		error = EFAULT;
		goto fail;
	}

	/*
	 * Create the sysevent door service for this client.
	 * syseventd will use this door service to propagate
	 * events to the client.
	 */
	if (subattr == NULL) {
		SH_DOOR_DESC(shp) = door_create(event_deliver_service,
		    (void *)shp, DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
	} else {
		SH_DOOR_DESC(shp) = door_xcreate(event_deliver_service,
		    (void *)shp,
		    DOOR_REFUSE_DESC | DOOR_NO_CANCEL | DOOR_NO_DEPLETION_CB,
		    xdoor_server_create, xdoor_server_setup,
		    (void *)subattr, 1);
	}

	if (SH_DOOR_DESC(shp) == -1) {
		dprint("sysevent_bind_subscriber: door create failed: "
		    "%s\n", strerror(errno));
		error = EFAULT;
		goto fail;
	}

	(void) fdetach(SH_DOOR_NAME(shp));
	if (fattach(SH_DOOR_DESC(shp), SH_DOOR_NAME(shp)) != 0) {
		error = EFAULT;
		goto fail;
	}
	(void) close(fd);

	if (update_publisher_cache(sub_info, SE_BIND_REGISTRATION,
	    sub_id, 0, NULL) != 0) {
		error = errno;
		(void) update_kernel_registration(shp, SUBSCRIBER,
		    SE_UNBIND_REGISTRATION, &sub_id, 0, NULL);
		goto fail;
	}

	SH_BOUND(shp) = 1;
	SH_TYPE(shp) = SUBSCRIBER;
	SH_PRIV_DATA(shp) = (void *)sub_info;

	/* Create an event handler thread */
	if (xsa == NULL || xsa->xs_thrcreate == NULL) {
		created = thr_create(NULL, 0, subscriber_event_handler,
		    shp, THR_BOUND, &sub_info->sp_handler_tid) == 0;
	} else {
		/*
		 * A terrible hack.  We will use the extended private
		 * door thread creation function the caller passed in to
		 * create the event handler thread.  That function will
		 * be called with our chosen thread start function and arg
		 * instead of the usual libc-provided ones, but that's ok
		 * as it is required to use them verbatim anyway.  We will
		 * pass a NULL door_info_t pointer to the function - so
		 * callers depending on this hack had better be prepared
		 * for that.  All this allow the caller to rubberstamp
		 * the created thread as it wishes.  But we don't get
		 * the created threadid with this, so we modify the
		 * thread start function to stash it.
		 */

		created = xsa->xs_thrcreate(NULL, subscriber_event_handler,
		    shp, xsa->xs_thrcreate_cookie) == 1;
	}

	if (!created) {
		error = EFAULT;
		goto fail;
	}

	(void) mutex_unlock(SH_LOCK(shp));

	return (0);

fail:
	(void) close(fd);
	(void) door_revoke(SH_DOOR_DESC(shp));
	(void) fdetach(SH_DOOR_NAME(shp));
	(void) cond_destroy(&sub_info->sp_cv);
	(void) mutex_destroy(&sub_info->sp_qlock);
	free(sub_info->sp_door_name);
	free(sub_info);
	if (SH_ID(shp)) {
		(void) update_kernel_registration(shp, SUBSCRIBER,
		    SE_UNBIND_REGISTRATION, &sub_id, 0, NULL);
		SH_ID(shp) = 0;
	}
	if (SH_BOUND(shp)) {
		(void) update_publisher_cache(sub_info, SE_UNBIND_REGISTRATION,
		    sub_id, 0, NULL);
		free(SH_DOOR_NAME(shp));
		SH_BOUND(shp) = 0;
	}
	(void) mutex_unlock(SH_LOCK(shp));

	errno = error;

	return (-1);
}

/*
 * sysevent_bind_subscriber - Bind an event receiver to an event channel
 */
int
sysevent_bind_subscriber(sysevent_handle_t *shp,
    void (*event_handler)(sysevent_t *ev))
{
	return (sysevent_bind_subscriber_cmn(shp, event_handler, NULL));
}

/*
 * sysevent_bind_xsubscriber - Bind a subscriber using door_xcreate with
 * attributes specified.
 */
int
sysevent_bind_xsubscriber(sysevent_handle_t *shp,
    void (*event_handler)(sysevent_t *ev), sysevent_subattr_t *subattr)
{
	return (sysevent_bind_subscriber_cmn(shp, event_handler, subattr));
}

/*
 * sysevent_register_event - register an event class and associated subclasses
 *		for an event subscriber
 */
int
sysevent_register_event(sysevent_handle_t *shp,
    const char *ev_class, const char **ev_subclass,
    int subclass_num)
{
	int error;
	char *event_class = (char *)ev_class;
	char **event_subclass_list = (char **)ev_subclass;
	char *nvlbuf = NULL;
	size_t datalen;
	nvlist_t *nvl;

	(void) mutex_lock(SH_LOCK(shp));
	if (event_class == NULL || event_subclass_list == NULL ||
	    event_subclass_list[0] == NULL || SH_BOUND(shp) != 1 ||
	    subclass_num <= 0) {
		(void) mutex_unlock(SH_LOCK(shp));
		errno = EINVAL;
		return (-1);
	}

	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0) != 0) {
		(void) mutex_unlock(SH_LOCK(shp));
		return (-1);
	}
	if (nvlist_add_string_array(nvl, event_class, event_subclass_list,
	    subclass_num) != 0) {
		nvlist_free(nvl);
		(void) mutex_unlock(SH_LOCK(shp));
		return (-1);
	}
	if (nvlist_pack(nvl, &nvlbuf, &datalen, NV_ENCODE_NATIVE, 0) != 0) {
		nvlist_free(nvl);
		(void) mutex_unlock(SH_LOCK(shp));
		return (-1);
	}
	nvlist_free(nvl);

	/* Store new subscriber in in-kernel registration */
	if (update_kernel_registration(shp, SUBSCRIBER,
	    SE_REGISTER, &SH_ID(shp), datalen, (uchar_t *)nvlbuf)
	    != 0) {
		error = errno;
		free(nvlbuf);
		(void) mutex_unlock(SH_LOCK(shp));
		errno = error;
		return (-1);
	}
	/* Update the publisher's cached registration */
	if (update_publisher_cache(
	    (subscriber_priv_t *)SH_PRIV_DATA(shp), SE_REGISTER,
	    SH_ID(shp), datalen, (uchar_t *)nvlbuf) != 0) {
		error = errno;
		free(nvlbuf);
		(void) mutex_unlock(SH_LOCK(shp));
		errno = error;
		return (-1);
	}

	free(nvlbuf);

	(void) mutex_unlock(SH_LOCK(shp));

	return (0);
}

/*
 * sysevent_unregister_event - Unregister an event class and associated
 *				subclasses for an event subscriber
 */
void
sysevent_unregister_event(sysevent_handle_t *shp, const char *class)
{
	size_t class_sz;

	(void) mutex_lock(SH_LOCK(shp));

	if (!SH_BOUND(shp)) {
		(void) mutex_unlock(SH_LOCK(shp));
		return;
	}

	/* Remove subscriber from in-kernel registration */
	class_sz = strlen(class) + 1;
	(void) update_kernel_registration(shp, SUBSCRIBER,
	    SE_UNREGISTER, &SH_ID(shp), class_sz, (uchar_t *)class);
	/* Update the publisher's cached registration */
	(void) update_publisher_cache(
	    (subscriber_priv_t *)SH_PRIV_DATA(shp), SE_UNREGISTER,
	    SH_ID(shp), class_sz, (uchar_t *)class);

	(void) mutex_unlock(SH_LOCK(shp));
}

static int
cleanup_id(sysevent_handle_t *shp, uint32_t id, int type)
{
	dprint("cleanup_id: Cleaning up %s/%d\n", SH_CHANNEL_NAME(shp), id);

	/* Remove registration from the kernel */
	if (update_kernel_registration(shp, type, SE_CLEANUP, &id,
	    0, NULL) != 0) {
		dprint("cleanup_id: Unable to clean "
		    "up %s/%d\n", SH_CHANNEL_NAME(shp), id);
		return (-1);
	}

	return (0);
}

/*
 * sysevent_cleanup_subscribers: Allows the caller to cleanup resources
 *		allocated to unresponsive subscribers.
 */
void
sysevent_cleanup_subscribers(sysevent_handle_t *shp)
{
	uint32_t ping, result;
	int i, error, sub_fd;
	subscriber_data_t *sub;

	if (!SH_BOUND(shp)) {
		return;
	}

	for (i = 1; i <= MAX_SUBSCRIBERS; ++i) {

		sub = SH_SUBSCRIBER(shp, i);
		if (sub == NULL) {
			continue;
		}

		if ((sub_fd = open(sub->sd_door_name, O_RDONLY)) == -1) {
			continue;
		}
		/* Check for valid and responsive subscriber */
		error = clnt_deliver_event(sub_fd, &ping,
		    sizeof (uint32_t), &result, sizeof (result));
		(void) close(sub_fd);

		/* Only cleanup on EBADF (Invalid door descriptor) */
		if (error != EBADF)
			continue;

		if (cleanup_id(shp, i, SUBSCRIBER) != 0)
			continue;

		cache_remove_class(shp, EC_ALL, i);

		free(sub->sd_door_name);
		free(sub);
		SH_SUBSCRIBER(shp, i) = NULL;
	}

}

/*
 * sysevent_cleanup_publishers: Allows stale publisher handles to be deallocated
 *		as needed.
 */
void
sysevent_cleanup_publishers(sysevent_handle_t *shp)
{
	(void) cleanup_id(shp, 1, PUBLISHER);
}

/*
 * sysevent_unbind_subscriber: Unbind the subscriber from the sysevent channel.
 */
void
sysevent_unbind_subscriber(sysevent_handle_t *shp)
{
	subscriber_priv_t *sub_info;

	if (shp == NULL)
		return;

	(void) mutex_lock(SH_LOCK(shp));
	if (SH_BOUND(shp) == 0) {
		(void) mutex_unlock(SH_LOCK(shp));
		return;
	}

	/* Update the in-kernel registration */
	(void) update_kernel_registration(shp, SUBSCRIBER,
	    SE_UNBIND_REGISTRATION, &SH_ID(shp), 0, NULL);

	/* Update the sysevent channel publisher */
	sub_info = (subscriber_priv_t *)SH_PRIV_DATA(shp);
	(void) update_publisher_cache(sub_info, SE_UNBIND_REGISTRATION,
	    SH_ID(shp), 0, NULL);

	/* Close down event delivery facilities */
	(void) door_revoke(SH_DOOR_DESC(shp));
	(void) fdetach(SH_DOOR_NAME(shp));

	/*
	 * Release resources and wait for pending event delivery to
	 * complete.
	 */
	(void) mutex_lock(&sub_info->sp_qlock);
	SH_BOUND(shp) = 0;
	/* Signal event handler and drain the subscriber's event queue */
	(void) cond_signal(&sub_info->sp_cv);
	(void) mutex_unlock(&sub_info->sp_qlock);
	if (sub_info->sp_handler_tid != 0)
		(void) thr_join(sub_info->sp_handler_tid, NULL, NULL);

	(void) cond_destroy(&sub_info->sp_cv);
	(void) mutex_destroy(&sub_info->sp_qlock);
	free(sub_info->sp_door_name);
	free(sub_info);
	free(SH_DOOR_NAME(shp));
	(void) mutex_unlock(SH_LOCK(shp));
}

/*
 * sysevent_unbind_publisher: Unbind publisher from the sysevent channel.
 */
void
sysevent_unbind_publisher(sysevent_handle_t *shp)
{
	if (shp == NULL)
		return;

	(void) mutex_lock(SH_LOCK(shp));
	if (SH_BOUND(shp) == 0) {
		(void) mutex_unlock(SH_LOCK(shp));
		return;
	}

	/* Close down the registration facilities */
	(void) door_revoke(SH_DOOR_DESC(shp));
	(void) fdetach(SH_DOOR_NAME(shp));

	/* Update the in-kernel registration */
	(void) update_kernel_registration(shp, PUBLISHER,
	    SE_UNBIND_REGISTRATION, &SH_ID(shp), 0, NULL);
	SH_BOUND(shp) = 0;

	/* Free resources associated with bind */
	free_cached_registration(shp);
	dealloc_subscribers(shp);

	free(SH_PRIV_DATA(shp));
	free(SH_DOOR_NAME(shp));
	SH_ID(shp) = 0;
	(void) mutex_unlock(SH_LOCK(shp));
}

/*
 * Evolving APIs to subscribe to syseventd(8) system events.
 */

static sysevent_handle_t *
sysevent_bind_handle_cmn(void (*event_handler)(sysevent_t *ev),
    sysevent_subattr_t *subattr)
{
	sysevent_handle_t *shp;

	if (getuid() != 0) {
		errno = EACCES;
		return (NULL);
	}

	if (event_handler == NULL) {
		errno = EINVAL;
		return (NULL);
	}

	if ((shp = sysevent_open_channel(SYSEVENTD_CHAN)) == NULL) {
		return (NULL);
	}

	if (sysevent_bind_xsubscriber(shp, event_handler, subattr) != 0) {
		/*
		 * Ask syseventd to clean-up any stale subcribers and try to
		 * to bind again
		 */
		if (errno == EBUSY) {
			int pub_fd;
			char door_name[MAXPATHLEN];
			uint32_t result;
			struct reg_args rargs;

			if (snprintf(door_name, MAXPATHLEN, "%s/%s",
			    SH_CHANNEL_PATH(shp), REG_DOOR) >= MAXPATHLEN) {
				sysevent_close_channel(shp);
				errno = EINVAL;
				return (NULL);
			}

			rargs.ra_op = SE_CLEANUP;
			pub_fd = open(door_name, O_RDONLY);
			(void) clnt_deliver_event(pub_fd, (void *)&rargs,
			    sizeof (struct reg_args), &result, sizeof (result));
			(void) close(pub_fd);

			/* Try to bind again */
			if (sysevent_bind_xsubscriber(shp, event_handler,
			    subattr) != 0) {
				sysevent_close_channel(shp);
				return (NULL);
			}
		} else {
			sysevent_close_channel(shp);
			return (NULL);
		}
	}

	return (shp);
}

/*
 * sysevent_bind_handle - Bind application event handler for syseventd
 *		subscription.
 */
sysevent_handle_t *
sysevent_bind_handle(void (*event_handler)(sysevent_t *ev))
{
	return (sysevent_bind_handle_cmn(event_handler, NULL));
}

/*
 * sysevent_bind_xhandle - Bind application event handler for syseventd
 *		subscription, using door_xcreate and attributes as specified.
 */
sysevent_handle_t *
sysevent_bind_xhandle(void (*event_handler)(sysevent_t *ev),
    sysevent_subattr_t *subattr)
{
	return (sysevent_bind_handle_cmn(event_handler, subattr));
}

/*
 * sysevent_unbind_handle - Unbind caller from syseventd subscriptions
 */
void
sysevent_unbind_handle(sysevent_handle_t *shp)
{
	sysevent_unbind_subscriber(shp);
	sysevent_close_channel(shp);
}

/*
 * sysevent_subscribe_event - Subscribe to system event notification from
 *			syseventd(8) for the class and subclasses specified.
 */
int
sysevent_subscribe_event(sysevent_handle_t *shp, const char *event_class,
    const char **event_subclass_list, int num_subclasses)
{
	return (sysevent_register_event(shp, event_class,
	    event_subclass_list, num_subclasses));
}

void
sysevent_unsubscribe_event(sysevent_handle_t *shp, const char *event_class)
{
	sysevent_unregister_event(shp, event_class);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#ifndef	_LIBSYSEVENT_H
#define	_LIBSYSEVENT_H

#include <stdio.h>
#include <thread.h>
#include <stddef.h>
#include <synch.h>
#include <sys/types.h>
#include <sys/sysevent.h>

#ifdef	__cplusplus
extern "C" {
#endif

#define	SYSEVENTD_CHAN	"syseventd_channel"

/* sysevent loadable module ops structure and related defines */
#define	SE_MAX_RETRY_LIMIT	3
#define	SE_RETRY_TIME		1 /* seconds */
#define	SE_NO_RETRY		1
#define	SE_MAJOR_VERSION	0
#define	SE_MINOR_VERSION	0

struct slm_mod_ops {
	int	major_version;
	int	minor_version;
	int	retry_limit;
	int	(*deliver_event)();
};

typedef void *sysevent_handle_t;
typedef void *subscriber_t;

int sysevent_post_event(char *event_class, char *event_subclass, char *vendor,
	char *pub_name, nvlist_t *attr_list, sysevent_id_t *eid);
sysevent_t *sysevent_dup(sysevent_t *ev);
void sysevent_free(sysevent_t *ev);
int sysevent_get_attr_list(sysevent_t *ev, nvlist_t **nvlist);
int sysevent_lookup_attr(sysevent_t *ev, char *name, int datatype,
	sysevent_value_t *se_value);
sysevent_attr_t *sysevent_attr_next(sysevent_t *ev, sysevent_attr_t *attr);
char *sysevent_attr_name(sysevent_attr_t *attr);
int sysevent_attr_value(sysevent_attr_t *attr, sysevent_value_t *se_value);
int sysevent_get_class(sysevent_t *ev);
char *sysevent_get_class_name(sysevent_t *ev);
int sysevent_get_subclass(sysevent_t *ev);
char *sysevent_get_subclass_name(sysevent_t *ev);
char *sysevent_get_pub(sysevent_t *ev);
char *sysevent_get_vendor_name(sysevent_t *ev);
char *sysevent_get_pub_name(sysevent_t *ev);
void sysevent_get_pid(sysevent_t *ev, pid_t *pid);
uint64_t sysevent_get_seq(sysevent_t *ev);
void sysevent_get_time(sysevent_t *ev, hrtime_t *etime);
size_t sysevent_get_size(sysevent_t *ev);

/* syseventd subscriber interfaces */
sysevent_handle_t *sysevent_bind_handle(void (*event_handler)(sysevent_t *ev));
sysevent_handle_t *sysevent_bind_xhandle(void (*event_handler)(sysevent_t *ev),
    sysevent_subattr_t *);
void sysevent_unbind_handle(sysevent_handle_t *sysevent_hdl);
int sysevent_subscribe_event(sysevent_handle_t *sysevent_hdl,
	const char *event_class, const char **event_subclass_list,
	int num_subclasses);
void sysevent_unsubscribe_event(sysevent_handle_t *sysevent_hdl,
	const char *event_class);

/* Subscriber private interfaces */
sysevent_t *sysevent_alloc_event(char *event_class, char *event_subclass,
	char *vendor, char *pub_name, nvlist_t *attr_list);
int sysevent_send_event(sysevent_handle_t *shp, sysevent_t *ev);
sysevent_handle_t *sysevent_open_channel(const char *channel);
sysevent_handle_t *sysevent_open_channel_alt(const char *channel_path);
void sysevent_close_channel(sysevent_handle_t *shp);
int sysevent_bind_subscriber(sysevent_handle_t *shp,
	void (*event_handler)(sysevent_t *ev));
int sysevent_bind_xsubscriber(sysevent_handle_t *shp,
	void (*event_handler)(sysevent_t *ev), sysevent_subattr_t *);
void sysevent_unbind_subscriber(sysevent_handle_t *shp);
int sysevent_bind_publisher(sysevent_handle_t *shp);
void sysevent_unbind_publisher(sysevent_handle_t *shp);
int sysevent_register_event(sysevent_handle_t *shp, const char *event_class,
	const char **event_subclass_list, int num_subclasses);
void sysevent_unregister_event(sysevent_handle_t *shp,
	const char *event_class);
void sysevent_cleanup_subscribers(sysevent_handle_t *shp);
void sysevent_cleanup_publishers(sysevent_handle_t *shp);

/* Debug interfaces */
void se_print(FILE *fp, sysevent_t *);

#ifdef	__cplusplus
}
#endif

#endif	/* _LIBSYSEVENT_H */
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_LIBSYSEVENT_IMPL_H
#define	_LIBSYSEVENT_IMPL_H

#ifdef	__cplusplus
extern "C" {
#endif

/*
 * libsysevent implementation-specific structures
 */

/* sysevent publisher/subscriber handle and related data structures */

#define	CHAN_PATH	"/var/run/sysevent_channels"
#define	REG_DOOR	"reg_door"

/* Subscription size values */
#define	MAX_SUBSCRIPTION_SZ	1024

/* Sysevent Channel Handle */
typedef struct sysevent_impl_handle {
	int		sh_bound;		/* Channel bind status */
	int		sh_type;		/* pub/sub channel binding */
	uint32_t	sh_id;			/* pub/sub within channel */
	int		sh_door_desc;		/* Service door descrip */
	char		*sh_door_name;		/* Service door */
	char		*sh_channel_name;	/* Event Channel name */
	char		*sh_channel_path;	/* Full path to Event Chan */
	void		*sh_priv_data;	/* Pub/Sub private data */
	mutex_t		sh_lock;	/* lock to protect access */
} sysevent_impl_hdl_t;

/* Sysevent queue for subscriber delivery */
typedef struct sysevent_queue {
	struct sysevent_queue	*sq_next;
	sysevent_t		*sq_ev;
} sysevent_queue_t;

/*
 * Subscriber private data stored in the sysevent channel handle
 */
typedef struct subscriber_priv {
	cond_t			sp_cv;		/* cv for event synch */
	mutex_t			sp_qlock;	/* event queue lock */
	char			*sp_door_name;	/* Publisher reg door */
	thread_t		sp_handler_tid; /* delivery handler thread id */
	struct sysevent_queue	*sp_evq_head;   /* event q head */
	struct sysevent_queue	*sp_evq_tail;   /* event q tail */
	void			(*sp_func)(sysevent_t *ev); /* deliver func */
} subscriber_priv_t;

/* Subscriber information stored on the publisher side */
typedef struct subscriber_data {
	int			sd_flag;		/* flag */
	char			*sd_door_name;	/* Client door name */
} subscriber_data_t;

/* Publisher private data stored in the sysevent channel handle */
typedef struct publisher_priv {
	struct class_lst	*pp_class_hash[CLASS_HASH_SZ + 1];
	subscriber_data_t	*pp_subscriber_list[MAX_SUBSCRIBERS + 1];
} publisher_priv_t;

/* Subscriber flag values */
#define	ACTIVE		1	/* Active subscriber */
#define	SEND_AGAIN	2	/* Resend of event requested */

/* Sysevent handle access */
#define	SYSEVENT_IMPL_HNDL(sehp)	((sysevent_impl_hdl_t *)(void *)(sehp))
#define	SH_BOUND(sehp)	(SYSEVENT_IMPL_HNDL(sehp)->sh_bound)
#define	SH_TYPE(sehp)	(SYSEVENT_IMPL_HNDL(sehp)->sh_type)
#define	SH_RESULT(sehp)	(SYSEVENT_IMPL_HNDL(sehp)->sh_result)
#define	SH_ID(sehp)	(SYSEVENT_IMPL_HNDL(sehp)->sh_id)
#define	SH_DOOR_DESC(sehp)	(SYSEVENT_IMPL_HNDL(sehp)->sh_door_desc)
#define	SH_DOOR_NAME(sehp)	(SYSEVENT_IMPL_HNDL(sehp)->sh_door_name)
#define	SH_CHANNEL_NAME(sehp)	(SYSEVENT_IMPL_HNDL(sehp)->sh_channel_name)
#define	SH_CHANNEL_PATH(sehp)	(SYSEVENT_IMPL_HNDL(sehp)->sh_channel_path)
#define	SH_LOCK(sehp)		(&(SYSEVENT_IMPL_HNDL(sehp)->sh_lock))
#define	SH_PRIV_DATA(sehp)	(SYSEVENT_IMPL_HNDL(sehp)->sh_priv_data)

#define	SH_CLASS_HASH(sehp)	(((publisher_priv_t *) \
	SH_PRIV_DATA(sehp))->pp_class_hash)
#define	SH_SUBSCRIBER(sehp, id)	(((publisher_priv_t *) \
	SH_PRIV_DATA(sehp))->pp_subscriber_list[id])

/*
 * GPEC Interface definitions
 */

typedef struct evchan_subscriber evchan_subscr_t;

typedef struct evchan_sub_head {
	evchan_subscr_t *evchan_sub_next;
} evchan_sub_head_t;

/* Event channel handle */
typedef struct evchan_impl_handle {
	pid_t		ev_pid;		/* verify descend via fork() */
	int		ev_fd;		/* descriptor for sev driver */
	mutex_t		ev_lock;	/* lock to protect this structure */
	evchan_sub_head_t ev_sub;	/* anchor of subscriber list */
} evchan_impl_hdl_t;

/* Evchan handle access */
#define	EVCHAN_IMPL_HNDL(evcp)	((evchan_impl_hdl_t *)(void *)(evcp))
#define	EV_PID(evcp)		(EVCHAN_IMPL_HNDL(evcp)->ev_pid)
#define	EV_FD(evcp)		(EVCHAN_IMPL_HNDL(evcp)->ev_fd)
#define	EV_LOCK(evcp)		(&(EVCHAN_IMPL_HNDL(evcp)->ev_lock))
#define	EV_SUB(evcp)		(&(EVCHAN_IMPL_HNDL(evcp)->ev_sub))
#define	EV_SUB_NEXT(evcp)	(EVCHAN_IMPL_HNDL(evcp)->ev_sub.evchan_sub_next)

struct sysevent_subattr_impl {
	door_xcreate_server_func_t *xs_thrcreate;
	void *xs_thrcreate_cookie;
	door_xcreate_thrsetup_func_t *xs_thrsetup;
	void *xs_thrsetup_cookie;
	pthread_attr_t *xs_thrattr;
	sigset_t xs_sigmask;
};

/*
 * Subscriber private data
 */
struct evchan_subscriber {
	evchan_subscr_t *evsub_next;	/* list of subscribers */
	evchan_impl_hdl_t *ev_subhead;	/* link back to channel data */
	int evsub_door_desc;		/* Service door descriptor */
	char *evsub_sid;		/* identifier of subscriber */
	void *evsub_cookie;		/* subscriber cookie */
	int (*evsub_func)(sysevent_t *, void *); /* subscriber event handler */
	struct sysevent_subattr_impl *evsub_attr;
	uint32_t evsub_state;
};

#define	EVCHAN_SUB_STATE_ACTIVE		1
#define	EVCHAN_SUB_STATE_CLOSING	2

/* Access to subscriber data */
#define	EVCHAN_SUBSCR(subp)	((evchan_subscr_t *)(subp))

/* Characters for channel name syntax */
#define	EVCH_ISCHANCHAR(c)	(isalnum(c) || (c) == '.' || (c) == ':' || \
				    (c) == '-' || (c) == '_')

#ifdef	__cplusplus
}
#endif

#endif	/* _LIBSYSEVENT_IMPL_H */
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
#

#
# MAPFILE HEADER START
#
# WARNING:  STOP NOW.  DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
#	usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#

$mapfile_version 2

# Due to mistakes made early in the history of this library, there are
# no SUNW_1.2 symbols, but the version is now kept as a placeholder.
# Don't add any symbols to this version.

SYMBOL_VERSION SUNW_1.2 {
    global:
	SUNW_1.2;
} SUNW_1.1;

SYMBOL_VERSION SUNW_1.1 {
    global:
	sysevent_bind_handle;
	sysevent_free;
	sysevent_get_attr_list;
	sysevent_get_class_name;
	sysevent_get_pid;
	sysevent_get_pub_name;
	sysevent_get_seq;
	sysevent_get_size;
	sysevent_get_subclass_name;
	sysevent_get_time;
	sysevent_get_vendor_name;
	sysevent_post_event;
	sysevent_subscribe_event;
	sysevent_unbind_handle;
	sysevent_unsubscribe_event;
};

SYMBOL_VERSION SUNWprivate_1.1 {
    global:
	se_print;
	sysevent_alloc_event;
	sysevent_attr_name;
	sysevent_attr_next;
	sysevent_attr_value;
	sysevent_bind_publisher;
	sysevent_bind_subscriber;
	sysevent_bind_xhandle;
	sysevent_bind_xsubscriber;
	sysevent_cleanup_publishers;
	sysevent_cleanup_subscribers;
	sysevent_close_channel;
	sysevent_dup;
	sysevent_evc_bind;
	sysevent_evc_control;
	sysevent_evc_getpropnvl;
	sysevent_evc_publish;
	sysevent_evc_setpropnvl;
	sysevent_evc_subscribe;
	sysevent_evc_unbind;
	sysevent_evc_unsubscribe;
	sysevent_evc_xsubscribe;
	sysevent_get_class;
	sysevent_get_pub;
	sysevent_get_subclass;
	sysevent_lookup_attr;
	sysevent_open_channel;
	sysevent_open_channel_alt;
	sysevent_register_event;
	sysevent_send_event;
	sysevent_unbind_publisher;
	sysevent_unbind_subscriber;
	sysevent_unregister_event;
	sysevent_subattr_alloc;
	sysevent_subattr_free;
	sysevent_subattr_sigmask;
	sysevent_subattr_thrattr;
	sysevent_subattr_thrcreate;
	sysevent_subattr_thrsetup;
    local:
	*;
};