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
|
/*
* 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
*/
/* Portions Copyright 2005 Cyril Plisko */
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <langinfo.h>
#include <time.h>
#if !defined(DEBUG)
#define NDEBUG 1
#else
#undef NDEBUG
#endif
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <dlfcn.h>
#include <synch.h>
#include <sys/systeminfo.h>
#include <sys/sunddi.h>
#include <libdevinfo.h>
#include <unistd.h>
#include <stdarg.h>
#include <limits.h>
#include <ftw.h>
#include <ctype.h>
#define CFGA_PLUGIN_LIB
#include <config_admin.h>
/* Limit size of sysinfo return */
#define SYSINFO_LENGTH 256
/*
* Attachment point specifier types.
*/
typedef enum {
UNKNOWN_AP,
LOGICAL_LINK_AP,
LOGICAL_DRV_AP,
PHYSICAL_AP,
AP_TYPE
} cfga_ap_types_t;
static char *listopt_array[] = {
#define LISTOPT_CLASS 0
"class",
NULL
};
typedef struct {
int v_min; /* Min acceptable version */
int v_max; /* Max acceptable version */
} vers_req_t;
#define INVALID_VERSION -1
#define VALID_HSL_VERS(v) (((v) >= CFGA_HSL_V1) && \
((v) <= CFGA_HSL_VERS))
/*
* Incomplete definition
*/
struct cfga_vers_ops;
/*
* Structure that contains plugin library information.
*/
typedef struct plugin_lib {
struct plugin_lib *next; /* pointer to next */
mutex_t lock; /* protects refcnt */
int refcnt; /* reference count */
void *handle; /* handle from dlopen */
cfga_err_t (*cfga_change_state_p)();
cfga_err_t (*cfga_private_func_p)();
cfga_err_t (*cfga_test_p)();
cfga_err_t (*cfga_stat_p)();
cfga_err_t (*cfga_list_p)();
cfga_err_t (*cfga_help_p)();
int (*cfga_ap_id_cmp_p)();
cfga_err_t (*cfga_list_ext_p)(); /* For V2 plug-ins only */
int plugin_vers; /* actual plugin version */
struct cfga_vers_ops *vers_ops; /* version dependant routines */
char libpath[MAXPATHLEN]; /* full pathname to lib */
} plugin_lib_t;
static plugin_lib_t plugin_list;
typedef struct lib_cache {
struct lib_cache *lc_next;
plugin_lib_t *lc_libp;
char *lc_ap_id;
char *lc_ap_physical; /* physical ap_id */
char *lc_ap_logical; /* logical ap_id */
} lib_cache_t;
static lib_cache_t *lib_cache;
static mutex_t lib_cache_lock;
/*
* Library locator data struct - used to pass down through the device
* tree walking code.
*/
typedef struct lib_locator {
char ap_base[MAXPATHLEN];
char ap_logical[CFGA_LOG_EXT_LEN];
char ap_physical[CFGA_PHYS_EXT_LEN];
char ap_class[CFGA_CLASS_LEN];
char pathname[MAXPATHLEN];
plugin_lib_t *libp;
cfga_err_t status;
vers_req_t vers_req; /* plug-in version required */
} lib_loc_t;
/*
* linked list of cfga_stat_data structs - used for
* config_list
*/
typedef struct stat_data_list {
struct stat_data_list *next;
cfga_stat_data_t stat_data;
} stat_data_list_t;
/*
* linked list of arrays. Each array represents a bunch
* of list_data_t structures returned by a single call
* to a plugin's cfga_list_ext() routine.
*/
typedef struct array_list {
struct array_list *next;
cfga_list_data_t *array;
int nelem;
} array_list_t;
/*
* encapsulate config_list args to get them through the tree
* walking code
*/
typedef struct list_stat {
const char *opts; /* Hardware specific options */
char **errstr;
cfga_flags_t flags;
int *countp; /* Total number of list and stat structures */
stat_data_list_t *sdl; /* Linked list of stat structures */
array_list_t *al; /* Linked list of arrays of list structures */
vers_req_t use_vers; /* plugin versions to be stat'ed */
char *shp_errstr; /* only for shp plugin */
} list_stat_t;
/*
* Internal operations for libcfgadm which are version dependant
*/
struct cfga_vers_ops {
cfga_err_t (*resolve_lib)(plugin_lib_t *libp);
cfga_err_t (*stat_plugin)(list_stat_t *, lib_loc_t *, char **errstring);
cfga_err_t (*mklog)(di_node_t, di_minor_t, plugin_lib_t *,
lib_loc_t *liblocp);
cfga_err_t (*get_cond)(lib_loc_t *, cfga_cond_t *, char **);
};
/*
* Lock to protect list of libraries
*/
static mutex_t plugin_list_lock;
/*
* Forward declarations
*/
static const char *__config_strerror(cfga_err_t);
static void *config_calloc_check(size_t, size_t, char **);
static cfga_err_t resolve_lib_ref(plugin_lib_t *, lib_loc_t *);
static cfga_err_t config_get_lib(const char *, lib_loc_t *, char **);
static int check_ap(di_node_t, di_minor_t, void *);
static int check_ap_hp(di_node_t, di_hp_t, void *);
static int check_ap_impl(di_node_t, di_minor_t, di_hp_t, void *);
static int check_ap_phys(di_node_t, di_minor_t, void *);
static int check_ap_phys_hp(di_node_t, di_hp_t, void *);
static int check_ap_phys_impl(di_node_t, di_minor_t, di_hp_t, void *);
static cfga_err_t find_ap_common(lib_loc_t *libloc_p, const char *rootpath,
int (*fcn)(di_node_t node, di_minor_t minor, void *arg),
int (*fcn_hp)(di_node_t node, di_hp_t hp, void *arg),
char **errstring);
static plugin_lib_t *lib_in_list(char *);
static cfga_err_t find_lib(di_node_t, di_minor_t, lib_loc_t *);
static cfga_err_t find_lib_hp(di_node_t, di_hp_t, lib_loc_t *);
static cfga_err_t find_lib_impl(char *, lib_loc_t *);
static cfga_err_t load_lib(di_node_t, di_minor_t, lib_loc_t *);
static cfga_err_t load_lib_hp(di_node_t, di_hp_t, lib_loc_t *);
static cfga_err_t load_lib_impl(di_node_t, di_minor_t, di_hp_t, lib_loc_t *);
extern void bcopy(const void *, void *, size_t);
static void config_err(int, int, char **);
static void hold_lib(plugin_lib_t *);
static void rele_lib(plugin_lib_t *);
static cfga_err_t parse_listopt(char *listopts, char **classpp,
char **errstring);
static cfga_err_t list_common(list_stat_t *lstatp, const char *class);
static int do_list_common(di_node_t node, di_minor_t minor, void *arg);
static int do_list_common_hp(di_node_t node, di_hp_t hp, void *arg);
static int do_list_common_impl(di_node_t node, di_minor_t minor,
di_hp_t hp, void *arg);
static cfga_err_t stat_common(int num_ap_ids, char *const *ap_ids,
const char *class, list_stat_t *lstatp);
static cfga_err_t null_resolve(plugin_lib_t *libp);
static cfga_err_t resolve_v1(plugin_lib_t *libp);
static cfga_err_t resolve_v2(plugin_lib_t *libp);
static cfga_err_t mklog_common(di_node_t node, di_minor_t minor,
lib_loc_t *liblocp, size_t len);
static cfga_err_t null_mklog(di_node_t node, di_minor_t minor,
plugin_lib_t *libp, lib_loc_t *liblocp);
static cfga_err_t mklog_v1(di_node_t node, di_minor_t minor,
plugin_lib_t *libp, lib_loc_t *liblocp);
static cfga_err_t mklog_v2(di_node_t node, di_minor_t minor,
plugin_lib_t *libp, lib_loc_t *liblocp);
static cfga_err_t null_stat_plugin(list_stat_t *lstatp, lib_loc_t *libloc_p,
char **errstring);
static cfga_err_t stat_plugin_v2(list_stat_t *lstat, lib_loc_t *libloc_p,
char **errstring);
static cfga_err_t stat_plugin_v1(list_stat_t *lstat, lib_loc_t *libloc_p,
char **errstring);
static cfga_err_t null_get_cond(lib_loc_t *liblocp, cfga_cond_t *condp,
char **errstring);
static cfga_err_t get_cond_v1(lib_loc_t *liblocp, cfga_cond_t *condp,
char **errstring);
static cfga_err_t get_cond_v2(lib_loc_t *liblocp, cfga_cond_t *condp,
char **errstring);
static cfga_err_t realloc_data(cfga_stat_data_t **ap_id_list,
int *nlistp, list_stat_t *lstatp);
static cfga_err_t realloc_data_ext(cfga_list_data_t **ap_id_list,
int *nlistp, list_stat_t *lstatp);
static void stat_to_list(cfga_list_data_t *lp, cfga_stat_data_t *statp);
static void lstat_free(list_stat_t *lstatp);
static cfga_ap_types_t find_arg_type(const char *ap_id);
static int compat_plugin(vers_req_t *reqp, int plugin_vers);
static cfga_err_t check_flags(cfga_flags_t flags, cfga_flags_t mask,
char **errstring);
static cfga_err_t check_apids(int num_ap_ids, char *const *ap_ids,
char **errstring);
static char *get_class(di_minor_t minor);
static cfga_err_t split_apid(char *ap_id, char **dyncompp, char **errstring);
static void append_dyn(char *buf, const char *dyncomp, size_t blen);
static int default_ap_id_cmp(const char *ap_id1, const char *ap_id2);
static void destroy_cache();
/*
* Plugin library search path helpers
*/
#define LIB_PATH_BASE1 "/usr/platform/"
#define LIB_PATH_BASE2 "/usr"
#if defined(__sparcv9)
#define LIB_PATH_MIDDLE "/lib/cfgadm/sparcv9/"
#elif defined(__amd64)
#define LIB_PATH_MIDDLE "/lib/cfgadm/amd64/"
#else
#define LIB_PATH_MIDDLE "/lib/cfgadm/"
#endif
#define LIB_PATH_TAIL ".so.1"
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
/*
* Defined constants
*/
#define DEVICES_DIR "/devices"
#define DOT_DOT_DEVICES "../devices"
#define CFGA_DEV_DIR "/dev/cfg"
#define SLASH "/"
#define S_FREE(x) (((x) != NULL) ? (free(x), (x) = NULL) : (void *)0)
#define GET_DYN(a) (strstr((a), CFGA_DYN_SEP))
#define CFGA_NO_CLASS "none"
/*
* Error strings
*/
#define DI_INIT_FAILED 1
#define ALLOC_FAILED 2
#define INVALID_ARGS 3
static char *
err_strings[] = {
NULL,
"Device library initialize failed",
"Memory allocation failed",
"Invalid argument(s)"
};
static const char err_sep[] = ": ";
/*
* Table of version dependant routines
*/
static struct cfga_vers_ops cfga_vers_ops[CFGA_HSL_VERS + 1] = {
{null_resolve, null_stat_plugin, null_mklog, null_get_cond },
{resolve_v1, stat_plugin_v1, mklog_v1, get_cond_v1 },
{resolve_v2, stat_plugin_v2, mklog_v2, get_cond_v2 }
};
#define VERS_ARRAY_SZ (sizeof (cfga_vers_ops)/sizeof (cfga_vers_ops[0]))
/*
* Public interfaces for libcfgadm, as documented in config_admin.3x
*/
/*
* config_change_state
*/
cfga_err_t
config_change_state(
cfga_cmd_t state_change_cmd,
int num_ap_ids,
char *const *ap_id,
const char *options,
struct cfga_confirm *confp,
struct cfga_msg *msgp,
char **errstring,
cfga_flags_t flags)
{
/*
* for each arg -
* load hs library,
* if force
* call cfga_state_change_func
* return status
* else
* call it's cfga_stat
* check condition
* call cfga_state_change_func
* return status
*/
int i;
lib_loc_t libloc;
plugin_lib_t *libp;
cfga_cond_t cond;
cfga_err_t retval = CFGA_OK;
/* Sanity checks */
if (state_change_cmd == CFGA_CMD_NONE)
return (retval);
if ((state_change_cmd < CFGA_CMD_NONE) ||
(state_change_cmd > CFGA_CMD_UNCONFIGURE))
return (CFGA_INVAL);
if (errstring != NULL) {
*errstring = NULL;
}
if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
!= CFGA_OK) {
return (CFGA_ERROR);
}
if (check_apids(num_ap_ids, ap_id, errstring) != CFGA_OK) {
return (CFGA_ERROR);
}
/*
* operate on each ap_id
*/
for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
libloc.libp = NULL;
if ((retval = config_get_lib(ap_id[i], &libloc, errstring)) !=
CFGA_OK) {
break;
}
libp = libloc.libp;
if ((flags & CFGA_FLAG_FORCE) ||
(state_change_cmd == CFGA_CMD_UNLOAD) ||
(state_change_cmd == CFGA_CMD_DISCONNECT) ||
(state_change_cmd == CFGA_CMD_UNCONFIGURE)) {
errno = 0;
retval = (*libp->cfga_change_state_p)
(state_change_cmd, libloc.ap_physical, options,
confp, msgp, errstring, flags);
} else {
/*
* Need to check condition before proceeding in
* the "configure direction"
*/
if ((retval = libp->vers_ops->get_cond(&libloc, &cond,
errstring)) != CFGA_OK) {
break;
}
if (cond == CFGA_COND_OK || cond == CFGA_COND_UNKNOWN) {
errno = 0;
retval =
(*libp->cfga_change_state_p)(
state_change_cmd,
libloc.ap_physical, options,
confp, msgp, errstring,
flags);
} else {
retval = CFGA_INSUFFICENT_CONDITION;
}
}
rele_lib(libp);
}
return (retval);
}
/*
* config_private_func
*/
cfga_err_t
config_private_func(
const char *function,
int num_ap_ids,
char *const *ap_ids,
const char *options,
struct cfga_confirm *confp,
struct cfga_msg *msgp,
char **errstring,
cfga_flags_t flags)
{
int i;
lib_loc_t libloc;
cfga_err_t retval = CFGA_OK;
if (errstring != NULL) {
*errstring = NULL;
}
if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
!= CFGA_OK) {
return (CFGA_ERROR);
}
if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
return (CFGA_ERROR);
}
/*
* operate on each ap_id
*/
for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
libloc.libp = NULL;
if ((retval = config_get_lib(ap_ids[i], &libloc, errstring)) !=
CFGA_OK) {
return (retval);
}
errno = 0;
retval = (*libloc.libp->cfga_private_func_p)(function,
libloc.ap_physical, options, confp, msgp, errstring,
flags);
rele_lib(libloc.libp);
}
return (retval);
}
/*
* config_test
*/
cfga_err_t
config_test(
int num_ap_ids,
char *const *ap_ids,
const char *options,
struct cfga_msg *msgp,
char **errstring,
cfga_flags_t flags)
{
int i;
lib_loc_t libloc;
cfga_err_t retval = CFGA_OK;
if (errstring != NULL) {
*errstring = NULL;
}
if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
!= CFGA_OK) {
return (CFGA_ERROR);
}
if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
return (CFGA_ERROR);
}
/*
* operate on each ap_id
*/
for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
libloc.libp = NULL;
if ((retval = config_get_lib(ap_ids[i], &libloc, errstring)) !=
CFGA_OK) {
return (retval);
}
errno = 0;
retval = (*libloc.libp->cfga_test_p)(libloc.ap_physical,
options, msgp, errstring, flags);
rele_lib(libloc.libp);
}
return (retval);
}
cfga_err_t
config_stat(
int num_ap_ids,
char *const *ap_ids,
struct cfga_stat_data *buf,
const char *options,
char **errstring)
{
int nstat, n, i;
list_stat_t lstat = {NULL};
cfga_err_t rc = CFGA_OK;
if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
return (CFGA_ERROR);
}
/*
* V1 entry points don't support dynamic attachment points
*/
for (i = 0; i < num_ap_ids; i++) {
if (GET_DYN(ap_ids[i]) != NULL) {
return (CFGA_APID_NOEXIST);
}
}
nstat = n = 0;
lstat.countp = &nstat;
lstat.opts = options;
lstat.errstr = errstring;
lstat.shp_errstr = NULL;
/*
* This is a V1 interface which can use only V1 plugins
*/
lstat.use_vers.v_max = lstat.use_vers.v_min = CFGA_HSL_V1;
rc = stat_common(num_ap_ids, ap_ids, NULL, &lstat);
if (rc == CFGA_OK) {
assert(*lstat.countp == num_ap_ids);
rc = realloc_data(&buf, &n, &lstat);
}
return (rc);
}
/*
* config_list
*/
cfga_err_t
config_list(
struct cfga_stat_data **ap_id_list,
int *nlistp,
const char *options,
char **errstring)
{
int nstat;
list_stat_t lstat = {NULL};
cfga_err_t retval = CFGA_ERROR;
if (errstring != NULL) {
*errstring = NULL;
}
nstat = 0;
lstat.countp = &nstat;
lstat.opts = options;
lstat.errstr = errstring;
lstat.shp_errstr = NULL;
/*
* This is a V1 interface which can use only V1 plugins
*/
lstat.use_vers.v_max = lstat.use_vers.v_min = CFGA_HSL_V1;
*ap_id_list = NULL;
*nlistp = 0;
/*
* V1 interfaces don't support prefiltering, no class
* specified.
*/
retval = list_common(&lstat, NULL);
if (retval == CFGA_OK) {
retval = realloc_data(ap_id_list, nlistp, &lstat);
}
assert((ap_id_list != NULL && *nlistp != 0) ||
(ap_id_list == NULL && *nlistp == 0));
if (retval == CFGA_OK && *nlistp == 0) {
return (CFGA_NOTSUPP);
} else {
return (retval);
}
}
/*
* config_list_ext
*/
cfga_err_t
config_list_ext(
int num_ap_ids,
char *const *ap_ids,
struct cfga_list_data **ap_id_list,
int *nlistp,
const char *options,
const char *listopts,
char **errstring,
cfga_flags_t flags)
{
int nstat, list, prefilter;
list_stat_t lstat = {NULL};
char *class;
cfga_err_t rc = CFGA_ERROR;
*nlistp = 0;
*ap_id_list = NULL;
if (errstring != NULL) {
*errstring = NULL;
}
if (check_flags(flags, CFGA_FLAG_LIST_ALL, errstring) != CFGA_OK) {
return (CFGA_ERROR);
}
class = NULL;
if ((rc = parse_listopt((char *)listopts, &class, errstring))
!= CFGA_OK) {
return (rc);
}
prefilter = (class == NULL) ? 0 : 1;
nstat = 0;
lstat.countp = &nstat;
lstat.opts = options;
lstat.errstr = errstring;
lstat.shp_errstr = NULL;
lstat.flags = flags;
/*
* We support both V1 and V2 plugins through this entry
* point.
*/
lstat.use_vers.v_min = CFGA_HSL_V1;
lstat.use_vers.v_max = CFGA_HSL_V2;
list = 0;
if (num_ap_ids == 0 && ap_ids == NULL) {
/*
* discover and stat all attachment points
*/
list = 1;
rc = list_common(&lstat, class);
} else if (num_ap_ids > 0 && ap_ids != NULL) {
/*
* Stat specified attachment points. With dynamic expansion
* more data may be returned than was specified by user.
*/
rc = stat_common(num_ap_ids, ap_ids, class, &lstat);
} else {
rc = CFGA_ERROR;
}
S_FREE(class);
if (rc != CFGA_OK) {
return (rc);
}
rc = realloc_data_ext(ap_id_list, nlistp, &lstat);
assert((ap_id_list != NULL && *nlistp != 0) ||
(ap_id_list == NULL && *nlistp == 0));
/*
* For the list command notify user if no attachment
* point is found in the system.
*
*/
if (list && rc == CFGA_OK && *nlistp == 0) {
/*
* If attachment points are being prefiltered, absence of data
* does not imply that config. admin. is not
* supported by the system.
*/
if (prefilter) {
/*
* Prefiltering: requested class is absent
*/
return (CFGA_APID_NOEXIST);
} else {
/*
* No attachment points in system
*/
return (CFGA_NOTSUPP);
}
} else {
return (rc);
}
}
/*
* config_unload_libs
*
* Attempts to remove all libs on the plugin list.
*/
void
config_unload_libs()
{
plugin_lib_t *libp, *prev = &plugin_list, *next = NULL;
/* destroy cache entries to remove refcnt agains plugins */
destroy_cache();
(void) mutex_lock(&plugin_list_lock);
for (libp = plugin_list.next; libp != NULL; libp = next) {
next = libp->next;
(void) mutex_lock(&libp->lock);
if (libp->refcnt) {
(void) mutex_unlock(&libp->lock);
prev = libp;
continue;
}
(void) mutex_unlock(&libp->lock);
prev->next = next;
(void) dlclose(libp->handle);
(void) mutex_destroy(&libp->lock);
free(libp);
}
(void) mutex_unlock(&plugin_list_lock);
}
/*
* config_ap_id_cmp
*/
int
config_ap_id_cmp(
const cfga_ap_log_id_t ap1,
const cfga_ap_log_id_t ap2)
{
int ret;
lib_loc_t libloc;
char apstat1[CFGA_PHYS_EXT_LEN];
char apstat2[CFGA_PHYS_EXT_LEN];
char *sep1, *sep2;
/*
* Extract static ap_ids
*/
(void) strlcpy(apstat1, ap1, sizeof (apstat1));
(void) strlcpy(apstat2, ap2, sizeof (apstat2));
sep1 = GET_DYN(apstat1);
sep2 = GET_DYN(apstat2);
if (sep1)
*sep1 = '\0';
if (sep2)
*sep2 = '\0';
/*
* Use the default comparator for static ap_ids
*/
ret = default_ap_id_cmp(apstat1, apstat2);
if (ret)
return (ret);
/*
* static components match. They belong to
* the same static ap_id. Check if both are dynamic
* If not, static < dynamic.
*/
if ((sep1 == NULL) ^ (sep2 == NULL))
return (sep1 ? 1 : -1);
/*
* If both are static, then ap1 = ap2
*/
if (sep1 == NULL)
return (0);
/*
* Both are dynamic and belong to same static ap_id.
* Use the plugin comparator
*/
libloc.libp = NULL;
if (config_get_lib(ap1, &libloc, NULL) != CFGA_OK) {
return (strncmp(sep1, sep2, CFGA_PHYS_EXT_LEN));
}
ret = (*libloc.libp->cfga_ap_id_cmp_p)(ap1, ap2);
rele_lib(libloc.libp);
return (ret);
}
/*
* config_strerror
*/
const char *
config_strerror(cfga_err_t cfgerrnum)
{
const char *ep = NULL;
if ((cfgerrnum < CFGA_OK) || (cfgerrnum > CFGA_ATTR_INVAL))
return (NULL);
ep = __config_strerror(cfgerrnum);
return ((ep != NULL) ? dgettext(TEXT_DOMAIN, ep) : NULL);
}
/*
* config_help
*/
cfga_err_t
config_help(
int num_ap_ids,
char *const *ap_ids,
struct cfga_msg *msgp,
const char *options,
cfga_flags_t flags)
{
int i;
lib_loc_t libloc;
cfga_err_t retval = CFGA_OK;
if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, NULL)
!= CFGA_OK) {
return (CFGA_ERROR);
}
if (num_ap_ids < 0) {
return (CFGA_ERROR);
}
if (num_ap_ids > 0 && ap_ids == NULL) {
return (CFGA_ERROR);
}
/*
* operate on each ap_id
*/
for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
libloc.libp = NULL;
if ((retval = config_get_lib(ap_ids[i], &libloc,
NULL)) != CFGA_OK) {
return (retval);
}
errno = 0;
retval = (*libloc.libp->cfga_help_p)(msgp, options, flags);
rele_lib(libloc.libp);
}
return (retval);
}
/*
* Private support routines for the public interfaces
*/
static const char *
__config_strerror(cfga_err_t cfgerrnum)
{
const char *ep = NULL;
switch (cfgerrnum) {
case CFGA_OK:
ep = "Configuration operation succeeded";
break;
case CFGA_NACK:
ep = "Configuration operation cancelled";
break;
case CFGA_INVAL:
ep = "Configuration operation invalid";
break;
case CFGA_NOTSUPP:
ep = "Configuration administration not supported";
break;
case CFGA_OPNOTSUPP:
ep = "Configuration operation not supported";
break;
case CFGA_PRIV:
ep = "Insufficient privileges";
break;
case CFGA_BUSY:
ep = "Component system is busy, try again";
break;
case CFGA_SYSTEM_BUSY:
ep = "System is busy, try again";
break;
case CFGA_DATA_ERROR:
ep = "Data error";
break;
case CFGA_LIB_ERROR:
ep = "Library error";
break;
case CFGA_NO_LIB:
ep = "No Library found";
break;
case CFGA_INSUFFICENT_CONDITION:
ep = "Insufficient condition";
break;
case CFGA_ERROR:
ep = "Hardware specific failure";
break;
case CFGA_APID_NOEXIST:
ep = "Attachment point not found";
break;
case CFGA_ATTR_INVAL:
ep = "No attachment point with specified attributes found";
break;
default:
ep = NULL;
break;
}
return (ep);
}
/*
* listopts is a string in the getsubopt(3C) style:
* name1=value1,name2=value2,
*/
static cfga_err_t
parse_listopt(char *listopts, char **classpp, char **errstring)
{
char *bufp, *optp, *val = NULL;
cfga_err_t rc = CFGA_ERROR;
*classpp = NULL;
/*
* NULL is a legal value for listopts
*/
if (listopts == NULL) {
return (CFGA_OK);
}
if ((bufp = config_calloc_check(1, strlen(listopts) + 1, errstring))
== NULL) {
return (CFGA_LIB_ERROR);
}
(void) strcpy(bufp, listopts);
optp = bufp; /* getsubopt() modifies its argument */
while (*optp != '\0') {
switch (getsubopt(&optp, listopt_array, &val)) {
case LISTOPT_CLASS:
if (val == NULL || *classpp != NULL) {
rc = CFGA_ERROR;
goto out;
}
if ((*classpp = config_calloc_check(1, strlen(val) + 1,
errstring)) == NULL) {
rc = CFGA_LIB_ERROR;
goto out;
}
(void) strcpy(*classpp, val);
break;
default:
rc = CFGA_ERROR;
goto out;
}
}
rc = CFGA_OK;
/*FALLTHRU*/
out:
S_FREE(bufp);
if (rc != CFGA_OK) {
S_FREE(*classpp);
}
return (rc);
}
/*ARGSUSED*/
static cfga_err_t
null_mklog(
di_node_t node,
di_minor_t minor,
plugin_lib_t *libp,
lib_loc_t *liblocp)
{
return (CFGA_OK);
}
static cfga_err_t
mklog_v1(
di_node_t node,
di_minor_t minor,
plugin_lib_t *libp,
lib_loc_t *liblocp)
{
const size_t len = CFGA_AP_LOG_ID_LEN;
assert(len <= sizeof (liblocp->ap_logical));
if (libp->plugin_vers != CFGA_HSL_V1) {
return (CFGA_LIB_ERROR);
}
return (mklog_common(node, minor, liblocp, len));
}
/*
* Obtain the devlink from a /devices path
*/
static int
get_link(di_devlink_t devlink, void *arg)
{
char *linkp = (char *)arg;
(void) snprintf(linkp, CFGA_LOG_EXT_LEN, "%s",
di_devlink_path(devlink));
return (DI_WALK_TERMINATE);
}
static cfga_err_t
mklog_v2(
di_node_t node,
di_minor_t minor,
plugin_lib_t *libp,
lib_loc_t *liblocp)
{
const size_t len = CFGA_LOG_EXT_LEN;
di_devlink_handle_t hdl;
assert(len <= sizeof (liblocp->ap_logical));
if (libp->plugin_vers != CFGA_HSL_V2) {
return (CFGA_LIB_ERROR);
}
/* open devlink database */
if ((hdl = di_devlink_init(NULL, 0)) == NULL) {
return (CFGA_LIB_ERROR);
}
liblocp->ap_logical[0] = '\0';
(void) di_devlink_walk(hdl, NULL,
liblocp->ap_physical + strlen(DEVICES_DIR),
DI_PRIMARY_LINK, (void *)liblocp->ap_logical, get_link);
(void) di_devlink_fini(&hdl);
if (liblocp->ap_logical[0] != '\0')
return (CFGA_OK);
return (mklog_common(node, minor, liblocp, len));
}
/*
* mklog_common - make a logical name from the driver and instance
*/
static cfga_err_t
mklog_common(
di_node_t node,
di_minor_t minor,
lib_loc_t *libloc_p,
size_t len)
{
int inst;
char *drv, *minor_name;
drv = di_driver_name(node);
inst = di_instance(node);
minor_name = di_minor_name(minor);
errno = 0;
if (drv != NULL && inst != -1 && minor_name != NULL &&
snprintf(libloc_p->ap_logical, len, "%s%d:%s", drv, inst,
minor_name) < len) { /* snprintf returns strlen */
return (CFGA_OK);
}
return (CFGA_LIB_ERROR);
}
/*
* mklog_common - make a logical name from the driver and instance
*/
/*ARGSUSED*/
static cfga_err_t
mklog_hp(
di_node_t node,
di_hp_t hp,
plugin_lib_t *libp,
lib_loc_t *liblocp)
{
const size_t len = CFGA_LOG_EXT_LEN;
int inst;
char *drv, *hp_name;
drv = di_driver_name(node);
inst = di_instance(node);
hp_name = di_hp_name(hp);
errno = 0;
if (drv != NULL && inst != -1 && hp_name != NULL &&
snprintf(liblocp->ap_logical, len, "%s%d:%s", drv, inst,
hp_name) < len) { /* snprintf returns strlen */
return (CFGA_OK);
}
return (CFGA_LIB_ERROR);
}
/*
* resolve_lib_ref - relocate to use plugin lib
*/
static cfga_err_t
resolve_lib_ref(
plugin_lib_t *libp,
lib_loc_t *libloc_p)
{
void *sym;
void *libhdlp = libp->handle;
int plug_vers;
if ((sym = dlsym(libhdlp, "cfga_version")) == NULL) {
/*
* Version symbol not defined, must be the first version
*/
plug_vers = CFGA_HSL_V1;
} else {
plug_vers = *((int *)sym);
}
/*
* Check if plugin version matches request.
*/
if (!compat_plugin(&libloc_p->vers_req, plug_vers)) {
return (CFGA_NO_LIB);
}
/*
* Record the plugin version and setup version dependant routines
*/
assert(plug_vers < VERS_ARRAY_SZ);
libp->plugin_vers = plug_vers;
libp->vers_ops = &cfga_vers_ops[plug_vers];
/* resolve symbols common to all versions */
if ((sym = dlsym(libhdlp, "cfga_change_state")) == NULL) {
perror("dlsym: cfga_change_state");
return (CFGA_LIB_ERROR);
} else
libp->cfga_change_state_p = (cfga_err_t (*)(cfga_cmd_t,
const char *, const char *, struct cfga_confirm *,
struct cfga_msg *, char **, cfga_flags_t)) sym;
if ((sym = dlsym(libhdlp, "cfga_private_func")) == NULL) {
perror("dlsym: cfga_private_func");
return (CFGA_LIB_ERROR);
} else
libp->cfga_private_func_p = (cfga_err_t (*)(const char *,
const char *, const char *, struct cfga_confirm *,
struct cfga_msg *, char **, cfga_flags_t))sym;
if ((sym = dlsym(libhdlp, "cfga_test")) == NULL) {
perror("dlsym: cfga_test");
return (CFGA_LIB_ERROR);
} else
libp->cfga_test_p = (cfga_err_t (*)(const char *, const char *,
struct cfga_msg *, char **, cfga_flags_t))sym;
if ((sym = dlsym(libhdlp, "cfga_help")) == NULL) {
perror("dlsym: cfga_help");
return (CFGA_LIB_ERROR);
} else
libp->cfga_help_p = (cfga_err_t (*)(struct cfga_msg *,
const char *, cfga_flags_t))sym;
if ((sym = dlsym(libhdlp, "cfga_ap_id_cmp")) == NULL) {
libp->cfga_ap_id_cmp_p = default_ap_id_cmp;
} else
libp->cfga_ap_id_cmp_p = (int (*)(const
cfga_ap_log_id_t, const cfga_ap_log_id_t))sym;
/* Resolve version specific symbols */
return (libp->vers_ops->resolve_lib(libp));
}
/*ARGSUSED*/
static cfga_err_t
null_resolve(plugin_lib_t *libp)
{
return (CFGA_OK);
}
static cfga_err_t
resolve_v1(plugin_lib_t *libp)
{
void *sym, *libhdlp = libp->handle;
if (libp->plugin_vers != CFGA_HSL_V1) {
return (CFGA_NO_LIB);
}
if ((sym = dlsym(libhdlp, "cfga_stat")) == NULL) {
perror("dlsym: cfga_stat");
return (CFGA_LIB_ERROR);
} else
libp->cfga_stat_p = (cfga_err_t (*)(const char *,
struct cfga_stat_data *, const char *,
char **))sym;
if ((sym = dlsym(libhdlp, "cfga_list")) == NULL) {
perror("dlsym: cfga_list");
return (CFGA_LIB_ERROR);
} else
libp->cfga_list_p = (cfga_err_t (*)(struct cfga_stat_data **,
int *, const char *, char **))sym;
return (CFGA_OK);
}
static cfga_err_t
resolve_v2(plugin_lib_t *libp)
{
void *sym;
if (libp->plugin_vers != CFGA_HSL_V2) {
return (CFGA_NO_LIB);
}
if ((sym = dlsym(libp->handle, "cfga_list_ext")) == NULL) {
perror("dlsym: cfga_list_ext");
return (CFGA_LIB_ERROR);
} else {
libp->cfga_list_ext_p = (cfga_err_t (*)(const char *,
struct cfga_list_data **, int *, const char *,
const char *, char **, cfga_flags_t))sym;
return (CFGA_OK);
}
}
/*
* config_calloc_check - perform allocation, check result and
* set error string
*/
static void *
config_calloc_check(
size_t nelem,
size_t elsize,
char **errstring)
{
void *p;
p = calloc(nelem, elsize);
if (p == NULL) {
config_err(0, ALLOC_FAILED, errstring);
}
return (p);
}
/*
* config_get_lib - given an ap_id find the library name
* If successful, the plugin library is held.
*/
static cfga_err_t
config_get_lib(
const char *ap_id,
lib_loc_t *lib_loc_p,
char **errstring)
{
char *dyncomp, path[PATH_MAX];
char *apdup;
cfga_ap_types_t type = UNKNOWN_AP;
cfga_err_t ret = CFGA_ERROR;
if (ap_id == NULL) {
config_err(0, INVALID_ARGS, errstring);
return (ret);
}
lib_loc_p->libp = NULL;
if ((apdup = config_calloc_check(1, strlen(ap_id) + 1, errstring))
== NULL) {
return (CFGA_LIB_ERROR);
}
(void) strcpy(apdup, ap_id);
/*
* Separate into base and dynamic components
*/
if ((ret = split_apid(apdup, &dyncomp, errstring)) != CFGA_OK) {
goto out;
}
/*
* No upper limit on version
*/
lib_loc_p->vers_req.v_max = CFGA_HSL_VERS;
if (dyncomp != NULL) {
/*
* We need atleast version 2 of the plug-in library
* interface since the ap_id has a dynamic component.
*/
lib_loc_p->vers_req.v_min = CFGA_HSL_V2;
} else {
lib_loc_p->vers_req.v_min = CFGA_HSL_V1;
}
/*
* If the ap_id is a devlink in CFGA_DEV_DIR, follow link
* to get the physical ap_id.
*/
if ((type = find_arg_type(apdup)) == LOGICAL_LINK_AP) {
(void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
"%s%s", CFGA_DEV_DIR SLASH, apdup);
}
path[sizeof (path) - 1] = '\0';
if (type == LOGICAL_LINK_AP && realpath(lib_loc_p->ap_base, path)
!= NULL) {
(void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
"%s", path);
} else {
(void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
"%s", apdup);
}
/*
* find and load the library
* The base component of the ap_id is used to locate the plug-in
*
* NOTE that PCIE/PCISHPC connectors also have minor nodes &
* dev links created for now.
*/
if ((type = find_arg_type(lib_loc_p->ap_base)) == PHYSICAL_AP) {
/*
* physical ap_id: Use ap_base as root for tree walk
* A link based apid (logical) will resolve to a physical
* ap_id.
*/
ret = find_ap_common(lib_loc_p, lib_loc_p->ap_base,
check_ap_phys, check_ap_phys_hp, errstring);
} else if ((type == LOGICAL_DRV_AP) ||
(type == AP_TYPE && dyncomp == NULL)) {
/*
* logical ap_id or ap_type: Use "/" as root for tree walk
* Note: an aptype cannot have a dynamic component
*/
ret = find_ap_common(lib_loc_p, "/", check_ap,
check_ap_hp, errstring);
} else {
ret = CFGA_APID_NOEXIST;
}
if (ret == CFGA_OK) {
#ifndef NDEBUG
/*
* variables used by assert() only which is disabled
* by defining NDEBUG (see top of this file)
*/
plugin_lib_t *libp;
libp = lib_loc_p->libp;
#endif /* NDEBUG */
assert(strcmp(libp->libpath, lib_loc_p->pathname) == 0);
assert(VALID_HSL_VERS(libp->plugin_vers));
/*
* If a dynamic component was present, v1 plug-ins are not
* acceptable.
*/
assert(dyncomp == NULL || libp->plugin_vers >= CFGA_HSL_V2);
/*
* ap_physical is passed to plugins as their ap_id argument.
* Append dynamic component if any.
*/
append_dyn(lib_loc_p->ap_physical, dyncomp,
sizeof (lib_loc_p->ap_physical));
}
/* cleanup */
lib_loc_p->vers_req.v_min = INVALID_VERSION;
lib_loc_p->vers_req.v_max = INVALID_VERSION;
*lib_loc_p->ap_base = '\0';
/*FALLTHRU*/
out:
S_FREE(apdup);
S_FREE(dyncomp);
if (ret != CFGA_OK) {
lib_loc_p->libp = NULL;
}
assert(ret != CFGA_OK || lib_loc_p->libp != NULL);
return (ret);
}
/* load_lib - load library for non-SHP attachment point node */
static cfga_err_t
load_lib(
di_node_t node,
di_minor_t minor,
lib_loc_t *libloc_p)
{
return (load_lib_impl(node, minor, NULL, libloc_p));
}
/* load_lib_hp - load library for SHP attachment point node */
static cfga_err_t
load_lib_hp(
di_node_t node,
di_hp_t hp,
lib_loc_t *libloc_p)
{
return (load_lib_impl(node, NULL, hp, libloc_p));
}
/*
* load_lib_impl - Given a library pathname, create a entry for it
* in the library list, * if one does not already exist, and read
* lock it to keep it there.
*/
static cfga_err_t
load_lib_impl(
di_node_t node,
di_minor_t minor,
di_hp_t hp,
lib_loc_t *libloc_p)
{
plugin_lib_t *libp, *list_libp;
char *devfs_path;
char *name;
if (minor != DI_MINOR_NIL && hp != DI_HP_NIL)
return (CFGA_LIB_ERROR);
if (minor != DI_MINOR_NIL)
name = di_minor_name(minor);
else
name = di_hp_name(hp);
/*
* lock the library list
*/
(void) mutex_lock(&plugin_list_lock);
/*
* see if lib exist in list, if not, allocate a new one
*/
list_libp = lib_in_list(libloc_p->pathname);
if (list_libp != NULL) {
hold_lib(list_libp);
(void) mutex_unlock(&plugin_list_lock);
/* fill in logical and physical name in libloc_p */
libloc_p->libp = libp = list_libp;
if (minor != DI_MINOR_NIL) {
if (libp->vers_ops->mklog(node, minor, libp, libloc_p)
!= CFGA_OK) {
rele_lib(list_libp);
return (CFGA_LIB_ERROR);
}
} else {
if (mklog_hp(node, hp, libp, libloc_p) != CFGA_OK) {
rele_lib(list_libp);
return (CFGA_LIB_ERROR);
}
}
devfs_path = di_devfs_path(node);
(void) snprintf(libloc_p->ap_physical, MAXPATHLEN, "%s%s:%s",
DEVICES_DIR, devfs_path, name);
di_devfs_path_free(devfs_path);
return (CFGA_OK);
}
/* allocate a new plugin_lib_t structure */
libp = config_calloc_check(1, sizeof (plugin_lib_t), NULL);
if (libp == NULL) {
(void) mutex_unlock(&plugin_list_lock);
return (CFGA_LIB_ERROR);
}
(void) snprintf(libp->libpath, sizeof (libp->libpath), "%s",
libloc_p->pathname);
/*
* ensure that the lib is open and linked in
*/
libp->handle = dlopen(libp->libpath, RTLD_NOW);
if (libp->handle == NULL) {
(void) mutex_unlock(&plugin_list_lock);
free(libp);
return (CFGA_NO_LIB);
}
if (minor != DI_MINOR_NIL) {
if (resolve_lib_ref(libp, libloc_p) != CFGA_OK ||
libp->vers_ops->mklog(node, minor, libp, libloc_p)
!= CFGA_OK) {
(void) mutex_unlock(&plugin_list_lock);
(void) dlclose(libp->handle);
free(libp);
return (CFGA_NO_LIB);
}
} else {
if (resolve_lib_ref(libp, libloc_p) != CFGA_OK ||
mklog_hp(node, hp, libp, libloc_p) != CFGA_OK) {
(void) mutex_unlock(&plugin_list_lock);
(void) dlclose(libp->handle);
free(libp);
return (CFGA_NO_LIB);
}
}
/*
* link in new entry to the end of list
*/
list_libp = &plugin_list;
while (list_libp->next != NULL)
list_libp = list_libp->next;
libp->next = list_libp->next;
list_libp->next = libp;
/* Initialize refcnt to 1 */
libp->refcnt = 1;
(void) mutex_init(&libp->lock, USYNC_THREAD, NULL);
(void) mutex_unlock(&plugin_list_lock);
/*
* record libp and physical node name in the libloc struct
*/
libloc_p->libp = libp;
devfs_path = di_devfs_path(node);
(void) snprintf(libloc_p->ap_physical, MAXPATHLEN, "%s%s:%s",
DEVICES_DIR, devfs_path, name);
di_devfs_path_free(devfs_path);
return (CFGA_OK);
}
#define NUM_LIB_NAMES 2
/*
* find_lib - find library for non-SHP attachment point node
*/
static cfga_err_t
find_lib(
di_node_t node,
di_minor_t minor,
lib_loc_t *libloc_p)
{
char name[NUM_LIB_NAMES][MAXPATHLEN];
char *class = NULL, *drv = NULL;
int i;
/* Make sure pathname and class is null if we fail */
*libloc_p->ap_class = *libloc_p->pathname = '\0';
/*
* Initialize possible library tags.
*/
drv = di_driver_name(node);
class = get_class(minor);
if (drv == NULL || class == NULL) {
return (CFGA_LIB_ERROR);
}
i = 0;
(void) snprintf(&name[i++][0], sizeof (name[0]), "%s", drv);
(void) snprintf(&name[i++][0], sizeof (name[0]), "%s", class);
/*
* Cycle through the array of names to find the library.
*/
for (i = 0; i < NUM_LIB_NAMES; i++) {
/* Attachment points may not have a class (i.e. are generic) */
if (name[i][0] == '\0') {
continue;
}
if (find_lib_impl(name[i], libloc_p) == CFGA_OK)
goto found;
}
return (CFGA_NO_LIB);
found:
/* Record class name (if any) */
(void) snprintf(libloc_p->ap_class, sizeof (libloc_p->ap_class), "%s",
class);
return (CFGA_OK);
}
/*
* find_lib_hp - find library for SHP attachment point
*/
/*ARGSUSED*/
static cfga_err_t
find_lib_hp(
di_node_t node,
di_hp_t hp,
lib_loc_t *libloc_p)
{
char name[MAXPATHLEN];
char *class = NULL;
/* Make sure pathname and class is null if we fail */
*libloc_p->ap_class = *libloc_p->pathname = '\0';
/*
* Initialize possible library tags.
*
* Only support PCI class for now, this will need to be
* changed as other plugins are migrated to SHP plugin.
*/
class = "pci";
#if 0
/*
* No type check for now as PCI is the only class SHP plugin
* supports. In the future we'll need to enable the type check
* and set class accordingly, when non PCI plugins are migrated
* to SHP. In that case we'll probably need to add an additional
* interface between libcfgadm and the plugins, and SHP plugin will
* implement this interface which will translate the bus specific
* strings to standard classes that libcfgadm can recognize, for
* all the buses it supports, e.g. for pci/pcie it will translate
* PCIE_NATIVE_HP_TYPE to string "pci". We'll also need to bump up
* SHP plugin version to 3 to use the new interface.
*/
class = di_hp_type(hp);
if ((strcmp(class, PCIE_NATIVE_HP_TYPE) == 0) ||
(strcmp(class, PCIE_ACPI_HP_TYPE) == 0) ||
(strcmp(class, PCIE_PCI_HP_TYPE) == 0)) {
class = "pci";
} else {
goto fail;
}
#endif
(void) snprintf(&name[0], sizeof (name), "%s", "shp");
if (find_lib_impl(name, libloc_p) == CFGA_OK)
goto found;
fail:
return (CFGA_NO_LIB);
found:
/* Record class name (if any) */
(void) snprintf(libloc_p->ap_class, sizeof (libloc_p->ap_class), "%s",
class);
return (CFGA_OK);
}
/*
* find_lib_impl - Given an attachment point node find it's library
*/
static cfga_err_t
find_lib_impl(
char *name,
lib_loc_t *libloc_p)
{
char lib[MAXPATHLEN];
struct stat lib_stat;
void *dlhandle = NULL;
static char plat_name[SYSINFO_LENGTH];
static char machine_name[SYSINFO_LENGTH];
static char arch_name[SYSINFO_LENGTH];
/*
* Initialize machine name and arch name
*/
if (strncmp("", machine_name, MAXPATHLEN) == 0) {
if (sysinfo(SI_PLATFORM, plat_name, SYSINFO_LENGTH) == -1) {
return (CFGA_ERROR);
}
if (sysinfo(SI_ARCHITECTURE, arch_name, SYSINFO_LENGTH) == -1) {
return (CFGA_ERROR);
}
if (sysinfo(SI_MACHINE, machine_name, SYSINFO_LENGTH) == -1) {
return (CFGA_ERROR);
}
}
/*
* Try path based upon platform name
*/
(void) snprintf(lib, sizeof (lib), "%s%s%s%s%s",
LIB_PATH_BASE1, plat_name, LIB_PATH_MIDDLE,
name, LIB_PATH_TAIL);
if (stat(lib, &lib_stat) == 0) {
/* file exists, is it a lib */
dlhandle = dlopen(lib, RTLD_LAZY);
if (dlhandle != NULL) {
goto found;
}
}
/*
* Try path based upon machine name
*/
(void) snprintf(lib, sizeof (lib), "%s%s%s%s%s",
LIB_PATH_BASE1, machine_name, LIB_PATH_MIDDLE,
name, LIB_PATH_TAIL);
if (stat(lib, &lib_stat) == 0) {
/* file exists, is it a lib */
dlhandle = dlopen(lib, RTLD_LAZY);
if (dlhandle != NULL) {
goto found;
}
}
/*
* Try path based upon arch name
*/
(void) snprintf(lib, sizeof (lib), "%s%s%s%s%s",
LIB_PATH_BASE1, arch_name, LIB_PATH_MIDDLE,
name, LIB_PATH_TAIL);
if (stat(lib, &lib_stat) == 0) {
/* file exists, is it a lib */
dlhandle = dlopen(lib, RTLD_LAZY);
if (dlhandle != NULL) {
goto found;
}
}
/*
* Try generic location
*/
(void) snprintf(lib, sizeof (lib), "%s%s%s%s",
LIB_PATH_BASE2, LIB_PATH_MIDDLE, name, LIB_PATH_TAIL);
if (stat(lib, &lib_stat) == 0) {
/* file exists, is it a lib */
dlhandle = dlopen(lib, RTLD_LAZY);
if (dlhandle != NULL) {
goto found;
}
}
return (CFGA_NO_LIB);
found:
/* we got one! */
(void) snprintf(libloc_p->pathname, sizeof (libloc_p->pathname), "%s",
lib);
(void) dlclose(dlhandle);
return (CFGA_OK);
}
static cfga_err_t
lookup_cache(lib_loc_t *libloc_p)
{
lib_cache_t *entry;
(void) mutex_lock(&lib_cache_lock);
entry = lib_cache;
while (entry) {
if (strcmp(entry->lc_ap_id, libloc_p->ap_base) == 0) {
plugin_lib_t *libp = entry->lc_libp;
libloc_p->libp = libp;
hold_lib(libp);
(void) strcpy(libloc_p->pathname, libp->libpath);
(void) strcpy(libloc_p->ap_physical,
entry->lc_ap_physical);
(void) strcpy(libloc_p->ap_logical,
entry->lc_ap_logical);
(void) mutex_unlock(&lib_cache_lock);
return (CFGA_OK);
}
entry = entry->lc_next;
}
(void) mutex_unlock(&lib_cache_lock);
return (CFGA_ERROR);
}
static void
update_cache(lib_loc_t *libloc_p)
{
lib_cache_t *entry;
entry = config_calloc_check(1, sizeof (lib_cache_t), NULL);
if (entry == NULL)
return;
entry->lc_ap_id = strdup(libloc_p->ap_base);
entry->lc_ap_physical = strdup(libloc_p->ap_physical);
entry->lc_ap_logical = strdup(libloc_p->ap_logical);
if ((entry->lc_ap_id == NULL) || (entry->lc_ap_physical == NULL) ||
(entry->lc_ap_logical == NULL)) {
free(entry->lc_ap_id);
free(entry->lc_ap_physical);
free(entry->lc_ap_logical);
free(entry);
return;
}
(void) mutex_lock(&lib_cache_lock);
entry->lc_libp = libloc_p->libp;
entry->lc_next = lib_cache;
lib_cache = entry;
hold_lib(entry->lc_libp); /* prevent stale cache */
(void) mutex_unlock(&lib_cache_lock);
}
static void
destroy_cache()
{
lib_cache_t *entry, *next;
(void) mutex_lock(&lib_cache_lock);
entry = lib_cache;
while (entry) {
next = entry->lc_next;
rele_lib(entry->lc_libp);
free(entry->lc_ap_id);
free(entry->lc_ap_physical);
free(entry->lc_ap_logical);
free(entry);
entry = next;
}
(void) mutex_unlock(&lib_cache_lock);
}
/*
* find_ap_common - locate a particular attachment point
*/
static cfga_err_t
find_ap_common(
lib_loc_t *libloc_p,
const char *physpath,
int (*fcn)(di_node_t node, di_minor_t minor, void *arg),
int (*fcn_hp)(di_node_t node, di_hp_t hp, void *arg),
char **errstring)
{
di_node_t rnode, wnode;
char *cp, *rpath;
size_t len;
if (lookup_cache(libloc_p) == CFGA_OK)
return (CFGA_OK);
if ((rpath = config_calloc_check(1, strlen(physpath) + 1,
errstring)) == NULL) {
return (CFGA_LIB_ERROR);
}
(void) strcpy(rpath, physpath);
/* Remove devices prefix (if any) */
len = strlen(DEVICES_DIR);
if (strncmp(rpath, DEVICES_DIR SLASH, len + strlen(SLASH)) == 0) {
(void) memmove(rpath, rpath + len,
strlen(rpath + len) + 1);
}
/* Remove dynamic component if any */
if ((cp = GET_DYN(rpath)) != NULL) {
*cp = '\0';
}
/* Remove minor name (if any) */
if ((cp = strrchr(rpath, ':')) != NULL) {
*cp = '\0';
}
/*
* begin walk of device tree
*
* Since we create minor nodes & dev links for both all PCI/PCIE
* connectors, but only create hp nodes for PCIE/PCISHPC connectors
* of the new framework, we should first match with hp nodes. If
* the ap_id refers to a PCIE/PCISHPC connector, we'll be able to
* find it here.
*/
rnode = di_init("/", DINFOSUBTREE | DINFOHP);
if (rnode)
wnode = di_lookup_node(rnode, rpath);
else
wnode = DI_NODE_NIL;
if (wnode == DI_NODE_NIL) {
if (rnode == DI_NODE_NIL) {
S_FREE(rpath);
config_err(errno, DI_INIT_FAILED, errstring);
return (CFGA_LIB_ERROR);
} else {
/*
* di_lookup_node() may fail, either because the
* ap_id does not exist, or because the ap_id refers
* to a legacy PCI slot, thus we'll not able to
* find node using DINFOHP, try to see if we can
* find one using DINFOCACHE.
*/
di_fini(rnode);
goto find_minor;
}
}
libloc_p->libp = NULL;
libloc_p->status = CFGA_APID_NOEXIST;
(void) di_walk_hp(wnode, NULL, DI_HP_CONNECTOR,
libloc_p, fcn_hp);
di_fini(rnode);
/*
* Failed to find a matching hp node, try minor node.
*/
if (libloc_p->libp == NULL) {
find_minor:
rnode = di_init("/", DINFOCACHE);
if (rnode)
wnode = di_lookup_node(rnode, rpath);
else
wnode = DI_NODE_NIL;
if (wnode == DI_NODE_NIL) {
if (rnode == DI_NODE_NIL) {
S_FREE(rpath);
config_err(errno, DI_INIT_FAILED, errstring);
return (CFGA_LIB_ERROR);
} else {
/*
* di_lookup_node() may fail, because the
* ap_id does not exist.
*/
S_FREE(rpath);
di_fini(rnode);
return (CFGA_APID_NOEXIST);
}
}
libloc_p->libp = NULL;
libloc_p->status = CFGA_APID_NOEXIST;
(void) di_walk_minor(wnode, "ddi_ctl:attachment_point",
DI_CHECK_ALIAS|DI_CHECK_INTERNAL_PATH,
libloc_p, fcn);
di_fini(rnode);
}
S_FREE(rpath);
if (libloc_p->libp != NULL) {
update_cache(libloc_p);
return (CFGA_OK);
} else {
return (libloc_p->status);
}
}
/*
* check_ap - called for each non-SHP attachment point found
*/
static int
check_ap(
di_node_t node,
di_minor_t minor,
void *arg)
{
return (check_ap_impl(node, minor, NULL, arg));
}
/*
* check_ap_hp - called for each SHP attachment point found
*/
static int
check_ap_hp(
di_node_t node,
di_hp_t hp,
void *arg)
{
return (check_ap_impl(node, NULL, hp, arg));
}
/*
* check_ap_impl - called for each attachment point found
*
* This is used in cases where a particular attachment point
* or type of attachment point is specified via a logical name or ap_type.
* Not used for physical names or in the list case with no
* ap's specified.
*/
static int
check_ap_impl(
di_node_t node,
di_minor_t minor,
di_hp_t hp,
void *arg)
{
char *cp = NULL;
char aptype[MAXPATHLEN];
char *recep_id = NULL;
char *node_minor;
char *drv_name;
char inst[MAXPATHLEN];
char inst2[MAXPATHLEN];
lib_loc_t *libloc_p;
int comparison_test;
int instance;
cfga_ap_types_t type;
if (minor != DI_MINOR_NIL && hp != DI_HP_NIL)
return (DI_WALK_CONTINUE);
libloc_p = (lib_loc_t *)arg;
(void) snprintf(aptype, sizeof (aptype), "%s", libloc_p->ap_base);
/*
* This routime handles only aptypes and driver based logical apids.
*/
type = find_arg_type(aptype);
if (type == LOGICAL_DRV_AP) {
cp = strchr(aptype, ':');
*cp = '\0';
recep_id = cp+1;
cp--;
while (isdigit(*cp) && cp != aptype)
cp--;
cp++;
(void) snprintf(inst, sizeof (inst), "%s", cp);
*cp = '\0';
} else if (type != AP_TYPE) {
libloc_p->status = CFGA_APID_NOEXIST;
return (DI_WALK_CONTINUE);
}
if (minor != DI_MINOR_NIL)
node_minor = di_minor_name(minor);
else
node_minor = di_hp_name(hp);
drv_name = di_driver_name(node);
instance = di_instance(node);
if (node_minor == NULL || drv_name == NULL || instance == -1) {
libloc_p->status = CFGA_APID_NOEXIST;
return (DI_WALK_CONTINUE);
}
(void) sprintf(inst2, "%d", instance);
/*
* If the base matches driver and instance try and find a lib for it,
* then load it. On any failure we continue the walk.
*
* driver based logical ap_ids are derived from driver name + instance.
* Ap_types are just partial driver names.
*
*/
comparison_test = 0;
if (type == AP_TYPE) {
if (strncmp(aptype, drv_name, strlen(aptype)) == 0) {
comparison_test = 1;
}
} else {
if (strcmp(aptype, drv_name) == 0 &&
strcmp(recep_id, node_minor) == 0 &&
strcmp(inst, inst2) == 0) {
comparison_test = 1;
}
}
if (comparison_test) {
/*
* save the correct type of error so user does not get confused
*/
if (minor != DI_MINOR_NIL) {
if (find_lib(node, minor, libloc_p) != CFGA_OK) {
libloc_p->status = CFGA_NO_LIB;
return (DI_WALK_CONTINUE);
}
if (load_lib(node, minor, libloc_p) != CFGA_OK) {
libloc_p->status = CFGA_LIB_ERROR;
return (DI_WALK_CONTINUE);
}
} else {
if (find_lib_hp(node, hp, libloc_p) != CFGA_OK) {
libloc_p->status = CFGA_NO_LIB;
return (DI_WALK_CONTINUE);
}
if (load_lib_hp(node, hp, libloc_p) != CFGA_OK) {
libloc_p->status = CFGA_LIB_ERROR;
return (DI_WALK_CONTINUE);
}
}
libloc_p->status = CFGA_OK;
return (DI_WALK_TERMINATE);
} else {
libloc_p->status = CFGA_APID_NOEXIST;
return (DI_WALK_CONTINUE);
}
}
/*
* check_ap_phys - called for each non-SHP attachment point found
*/
static int
check_ap_phys(
di_node_t node,
di_minor_t minor,
void *arg)
{
return (check_ap_phys_impl(node, minor, DI_HP_NIL, arg));
}
/*
* check_ap_phys_hp - called for each SHP attachment point found
*/
static int
check_ap_phys_hp(
di_node_t node,
di_hp_t hp,
void *arg)
{
return (check_ap_phys_impl(node, DI_HP_NIL, hp, arg));
}
/*
* check_ap_phys_impl - called for each attachment point found
*
* This is used in cases where a particular attachment point
* is specified via a physical name. If the name matches then
* we try and find and load the library for it.
*/
static int
check_ap_phys_impl(
di_node_t node,
di_minor_t minor,
di_hp_t hp,
void *arg)
{
lib_loc_t *libloc_p;
char phys_name[MAXPATHLEN];
char *devfs_path;
char *minor_name;
if (minor != DI_MINOR_NIL && hp != DI_HP_NIL)
return (DI_WALK_CONTINUE);
libloc_p = (lib_loc_t *)arg;
devfs_path = di_devfs_path(node);
if (minor != DI_MINOR_NIL)
minor_name = di_minor_name(minor);
else
minor_name = di_hp_name(hp);
if (devfs_path == NULL || minor_name == NULL) {
libloc_p->status = CFGA_APID_NOEXIST;
return (DI_WALK_CONTINUE);
}
(void) snprintf(phys_name, sizeof (phys_name), "%s%s:%s",
DEVICES_DIR, devfs_path, minor_name);
di_devfs_path_free(devfs_path);
if (strcmp(phys_name, libloc_p->ap_base) == 0) {
if (minor != DI_MINOR_NIL) {
if (find_lib(node, minor, libloc_p) != CFGA_OK) {
libloc_p->status = CFGA_NO_LIB;
return (DI_WALK_CONTINUE);
}
if (load_lib(node, minor, libloc_p) != CFGA_OK) {
libloc_p->status = CFGA_LIB_ERROR;
return (DI_WALK_CONTINUE);
}
} else {
if (find_lib_hp(node, hp, libloc_p) != CFGA_OK) {
libloc_p->status = CFGA_NO_LIB;
return (DI_WALK_CONTINUE);
}
if (load_lib_hp(node, hp, libloc_p) != CFGA_OK) {
libloc_p->status = CFGA_LIB_ERROR;
return (DI_WALK_CONTINUE);
}
}
libloc_p->status = CFGA_OK;
return (DI_WALK_TERMINATE);
} else {
libloc_p->status = CFGA_APID_NOEXIST;
return (DI_WALK_CONTINUE);
}
}
/*
* lib_in_list
*
* See if library, as specified by the full pathname and controller
* instance number is already represented in the plugin library list.
* If the instance number is -1 it is ignored.
*/
static plugin_lib_t *
lib_in_list(char *libpath)
{
plugin_lib_t *libp = NULL;
for (libp = plugin_list.next; libp != NULL; libp = libp->next) {
if (strncmp(libpath, libp->libpath, MAXPATHLEN) == 0) {
return (libp);
}
}
return (NULL);
}
/*
* Coalesce stat and list data into single array
*/
static cfga_err_t
realloc_data_ext(
cfga_list_data_t **ap_id_list,
int *nlistp,
list_stat_t *lstatp)
{
int i, j;
stat_data_list_t *slp;
cfga_list_data_t *cldp;
array_list_t *alp;
cfga_err_t rc = CFGA_OK;
assert(*lstatp->countp >= 0);
if (*lstatp->countp == 0) {
*ap_id_list = NULL;
*nlistp = 0;
return (CFGA_OK);
}
/*
* allocate the array
*/
if ((cldp = config_calloc_check(*lstatp->countp,
sizeof (cfga_list_data_t), lstatp->errstr)) == NULL) {
rc = CFGA_LIB_ERROR;
goto out;
}
/*
* copy all the stat elements (if any) into the array
*/
slp = lstatp->sdl;
for (i = 0; slp != NULL; i++) {
if (i >= *lstatp->countp) {
rc = CFGA_LIB_ERROR;
goto out;
}
stat_to_list(&cldp[i], &slp->stat_data);
slp = slp->next;
}
/*
* copy all the list elements (if any) into the array
*/
alp = lstatp->al;
for (; alp != NULL; ) {
if (i + alp->nelem > *lstatp->countp) {
rc = CFGA_LIB_ERROR;
goto out;
}
for (j = 0; j < alp->nelem; i++, j++) {
cldp[i] = alp->array[j];
}
alp = alp->next;
}
if (i != *lstatp->countp) {
rc = CFGA_LIB_ERROR;
} else {
rc = CFGA_OK;
}
/*FALLTHRU*/
out:
/* clean up */
lstat_free(lstatp);
if (rc == CFGA_OK) {
*ap_id_list = cldp;
*nlistp = *lstatp->countp;
} else {
S_FREE(cldp);
*ap_id_list = NULL;
*nlistp = 0;
}
return (rc);
}
/*
* The caller of this routine may supply a buffer through
* ap_id_list for returning data. Otherwise, this routine allocates the
* buffer.
*/
static cfga_err_t
realloc_data(cfga_stat_data_t **ap_id_list, int *nlistp, list_stat_t *lstatp)
{
int i;
stat_data_list_t *slp;
cfga_stat_data_t *csdp, *buf;
cfga_err_t rc;
assert(*lstatp->countp >= 0);
if (*lstatp->countp == 0) {
*nlistp = 0;
return (CFGA_OK);
}
/*
* allocate the array if caller does not supply one.
*/
if (*ap_id_list == NULL) {
if ((buf = config_calloc_check(*lstatp->countp,
sizeof (cfga_stat_data_t), lstatp->errstr)) == NULL) {
rc = CFGA_LIB_ERROR;
goto out;
}
} else {
buf = *ap_id_list;
}
/*
* copy the stat elements into the array
*/
csdp = buf;
slp = lstatp->sdl;
for (i = 0; slp != NULL; i++) {
if (i >= *lstatp->countp) {
rc = CFGA_LIB_ERROR;
goto out;
}
*csdp++ = slp->stat_data;
slp = slp->next;
}
rc = CFGA_OK;
out:
if (rc == CFGA_OK) {
*nlistp = *lstatp->countp;
*ap_id_list = buf;
} else {
/*
* Free buffer only if we allocated it.
*/
if (*ap_id_list == NULL) {
free(buf);
}
*nlistp = 0;
}
assert(lstatp->al == NULL);
lstat_free(lstatp);
return (rc);
}
/*
* list_common - walk the device tree and stat all attachment points.
*/
static cfga_err_t
list_common(list_stat_t *lstatp, const char *class)
{
di_node_t rnode;
char nodetype[MAXPATHLEN];
const char *l_class, *l_sep;
/*
* May walk a subset of all attachment points in the device tree if
* a class is specified
*/
if (class != NULL) {
l_sep = ":";
l_class = class;
} else {
l_sep = l_class = "";
}
(void) snprintf(nodetype, sizeof (nodetype), "%s%s%s",
DDI_NT_ATTACHMENT_POINT, l_sep, l_class);
/*
* Walk all hp nodes
*/
if ((rnode = di_init("/", DINFOSUBTREE | DINFOHP)) == DI_NODE_NIL) {
config_err(errno, DI_INIT_FAILED, lstatp->errstr);
return (CFGA_LIB_ERROR);
}
/* No need to filter on class for now */
(void) di_walk_hp(rnode, NULL, DI_HP_CONNECTOR,
lstatp, do_list_common_hp);
di_fini(rnode);
/*
* Walk all minor nodes
* but exclude PCIE/PCIESHPC connectors which have been walked above.
*/
if ((rnode = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
config_err(errno, DI_INIT_FAILED, lstatp->errstr);
return (CFGA_LIB_ERROR);
}
(void) di_walk_minor(rnode, nodetype,
DI_CHECK_ALIAS|DI_CHECK_INTERNAL_PATH, lstatp, do_list_common);
di_fini(rnode);
if (lstatp->shp_errstr != NULL) {
*(lstatp->errstr) = strdup(lstatp->shp_errstr);
free(lstatp->shp_errstr);
lstatp->shp_errstr = NULL;
}
return (CFGA_OK);
}
static void
config_err(int errnum, int err_type, char **errstring)
{
char *p = NULL, *q = NULL;
char *syserr = NULL;
char syserr_num[20];
int len = 0;
/*
* If errstring is null it means user in not interested in getting
* error status. So we don't do all the work
*/
if (errstring == NULL) {
return;
}
if (errnum != 0) {
syserr = strerror(errnum);
if (syserr == NULL) {
(void) sprintf(syserr_num, "errno=%d", errnum);
syserr = syserr_num;
}
} else
syserr = NULL;
q = dgettext(TEXT_DOMAIN, err_strings[err_type]);
len = strlen(q);
if (syserr != NULL) {
len += strlen(err_sep) + strlen(syserr);
}
p = malloc(len + 1);
if (p == NULL) {
*errstring = NULL;
return;
}
(void) strcpy(p, q);
if (syserr != NULL) {
(void) strcat(p, err_sep);
(void) strcat(p, syserr);
}
*errstring = p;
}
/*
* do_list_common - list non-SHP attachment point
*/
static int
do_list_common(
di_node_t node,
di_minor_t minor,
void *arg)
{
di_node_t rnode;
di_hp_t hp;
char *minor_name;
minor_name = di_minor_name(minor);
/*
* since PCIE/PCIHSHPC connectors have both hp nodes and minor nodes
* created for now, we need to specifically exclude these connectors
* during walking minor nodes.
*/
if ((rnode = di_init(di_devfs_path(node), DINFOSUBTREE | DINFOHP))
== DI_NODE_NIL) {
return (DI_WALK_CONTINUE);
}
for (hp = DI_HP_NIL; (hp = di_hp_next(rnode, hp)) != DI_HP_NIL; ) {
if (strcmp(di_hp_name(hp), minor_name) == 0) {
di_fini(rnode);
return (DI_WALK_CONTINUE);
}
}
di_fini(rnode);
return (do_list_common_impl(node, minor, NULL, arg));
}
/*
* do_list_common_hp - list SHP attachment point
*/
static int
do_list_common_hp(
di_node_t node,
di_hp_t hp,
void *arg)
{
return (do_list_common_impl(node, NULL, hp, arg));
}
/*
* do_list_common_impl - Routine to list attachment point as part of
* a config_list opertion. Used by both v1 and v2 interfaces.
* This is somewhat similar to config_get_lib() and its helper routines
* except that the ap_ids are always physical and don't have dynamic
* components.
*/
static int
do_list_common_impl(
di_node_t node,
di_minor_t minor,
di_hp_t hp,
void *arg)
{
lib_loc_t lib_loc;
plugin_lib_t *libp;
list_stat_t *lstatp = NULL;
cfga_err_t ret = CFGA_ERROR;
if (minor != DI_MINOR_NIL && hp != DI_HP_NIL)
return (DI_WALK_CONTINUE);
lstatp = (list_stat_t *)arg;
lib_loc.libp = NULL;
/*
* try and find a lib for this node
*/
if (minor != DI_MINOR_NIL) {
ret = find_lib(node, minor, &lib_loc);
} else {
ret = find_lib_hp(node, hp, &lib_loc);
}
if (ret != CFGA_OK) {
return (DI_WALK_CONTINUE);
}
/*
* Load all plugins. We will check compatibility later in this
* routine.
*/
lib_loc.vers_req.v_min = CFGA_HSL_V1;
lib_loc.vers_req.v_max = CFGA_HSL_VERS;
if (minor != DI_MINOR_NIL) {
ret = load_lib(node, minor, &lib_loc);
} else {
ret = load_lib_hp(node, hp, &lib_loc);
}
if (ret != CFGA_OK) {
return (DI_WALK_CONTINUE);
}
libp = lib_loc.libp;
assert(libp != NULL);
/*
* Note: For list type routines (list all attachment points in
* device tree) we don't pass errstring to the plugin, nor do we
* stop the walk if an error occurs in the plugin.
*/
if (compat_plugin(&lstatp->use_vers, libp->plugin_vers)) {
if (minor != DI_MINOR_NIL) {
(void) libp->vers_ops->stat_plugin(lstatp,
&lib_loc, NULL);
} else {
/*
* If the underlying hotplug daemon is not enabled,
* the SHP attach points will not be shown, this
* could confuse the uesrs. We specifically pass the
* errstring to SHP plugin so that it can set the
* errstring accordingly in this case, giving users
* a hint.
*/
ret = libp->vers_ops->stat_plugin(lstatp,
&lib_loc, lstatp->errstr);
if (ret == CFGA_NOTSUPP && *(lstatp->errstr) != NULL) {
if (lstatp->shp_errstr == NULL) {
lstatp->shp_errstr =
strdup(*(lstatp->errstr));
}
}
if (*(lstatp->errstr) != NULL) {
free(*(lstatp->errstr));
*(lstatp->errstr) = NULL;
}
}
}
rele_lib(libp);
return (DI_WALK_CONTINUE);
}
/*
* stat_common - stat a user specified set of attachment points.
*/
static cfga_err_t
stat_common(
int num_ap_ids,
char *const *ap_ids,
const char *class,
list_stat_t *lstatp)
{
int i;
lib_loc_t libloc;
plugin_lib_t *libp;
cfga_err_t rc = CFGA_OK;
/*
* operate on each ap_id
*/
for (i = 0; i < num_ap_ids; i++) {
libloc.libp = NULL;
if ((rc = config_get_lib(ap_ids[i], &libloc,
lstatp->errstr)) != CFGA_OK) {
break;
}
assert(libloc.libp != NULL);
libp = libloc.libp;
/*
* do pre-filtering if requested
*/
if (class != NULL && strcmp(libloc.ap_class, class)) {
rele_lib(libp);
continue;
}
/*
* Unlike list type routines, while stat'ing specific
* attachment points we pass errstring to the plugins
* and halt if an error occurs in the plugin.
*/
rc = libp->vers_ops->stat_plugin(lstatp, &libloc,
lstatp->errstr);
rele_lib(libp);
if (rc != CFGA_OK) {
break;
}
}
if (rc != CFGA_OK) {
lstat_free(lstatp);
}
return (rc);
}
/*ARGSUSED*/
static cfga_err_t
null_stat_plugin(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
{
return (CFGA_OK);
}
/*
* Pass errstring as a separate argument. Some higher level routines need
* it to be NULL.
*/
static cfga_err_t
stat_plugin_v1(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
{
stat_data_list_t *slp, *slp2 = NULL;
cfga_err_t rc;
/*
* allocate stat data buffer and list element
*/
if ((slp = config_calloc_check(1, sizeof (stat_data_list_t),
errstring)) == NULL) {
return (CFGA_LIB_ERROR);
}
/*
* Do the stat
*/
errno = 0;
if ((rc = (*(libloc_p->libp->cfga_stat_p))(libloc_p->ap_physical,
&slp->stat_data, lstatp->opts, errstring)) != CFGA_OK) {
S_FREE(slp);
return (rc);
}
slp->next = NULL;
/*
* Set up the logical and physical id's.
* For v1 interfaces, the generic library (libcfgadm) creates the
* ap_ids. mklog() is assumed to have been called in
* the caller of this routine.
*/
(void) snprintf(slp->stat_data.ap_log_id, CFGA_AP_LOG_ID_LEN, "%s",
libloc_p->ap_logical);
(void) snprintf(slp->stat_data.ap_phys_id, CFGA_AP_PHYS_ID_LEN, "%s",
libloc_p->ap_physical);
/*
* link it in
*/
if ((slp2 = lstatp->sdl) == NULL) {
lstatp->sdl = slp;
} else {
while (slp2->next != NULL)
slp2 = slp2->next;
slp2->next = slp;
}
/* keep count */
(*lstatp->countp)++;
return (CFGA_OK);
}
static cfga_err_t
stat_plugin_v2(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
{
int i;
array_list_t *alp, *alp2 = NULL;
cfga_err_t rc;
char *class;
/*
* allocate array list
*/
if ((alp = config_calloc_check(1, sizeof (array_list_t),
errstring)) == NULL) {
return (CFGA_LIB_ERROR);
}
alp->array = NULL;
alp->nelem = 0;
/*
* The listopts argument is currently unused. Use NULL
*/
errno = 0;
if ((rc = (*(libloc_p->libp->cfga_list_ext_p))(
libloc_p->ap_physical, &alp->array, &alp->nelem, lstatp->opts, NULL,
errstring, lstatp->flags)) != CFGA_OK || alp->nelem <= 0) {
S_FREE(alp);
return (rc);
}
alp->next = NULL;
/*
* Set up the logical and physical id's if necessary.
* For v2 interfaces, the generic library (libcfgadm) creates the
* ap_ids only if there are no dynamic attachment points and the
* plug-in does not create the name itself. mklog() is
* assumed to have been called in the caller of this routine.
*/
if (alp->nelem == 1) {
char cphys, clog;
clog = (alp->array[0]).ap_log_id[0];
cphys = (alp->array[0]).ap_phys_id[0];
if (clog == '\0') {
(void) snprintf((alp->array[0]).ap_log_id,
sizeof ((alp->array[0]).ap_log_id), "%s",
libloc_p->ap_logical);
}
if (cphys == '\0') {
(void) snprintf((alp->array[0]).ap_phys_id,
sizeof ((alp->array[0]).ap_phys_id), "%s",
libloc_p->ap_physical);
}
}
if (libloc_p->ap_class[0] == '\0') {
class = CFGA_NO_CLASS;
} else {
class = libloc_p->ap_class;
}
/* Fill in the class information for all list elements */
for (i = 0; i < alp->nelem; i++) {
(void) snprintf((alp->array[i]).ap_class,
sizeof ((alp->array[i]).ap_class), "%s", class);
}
/*
* link it in
*/
if ((alp2 = lstatp->al) == NULL) {
lstatp->al = alp;
} else {
while (alp2->next != NULL)
alp2 = alp2->next;
alp2->next = alp;
}
/* keep count */
(*lstatp->countp) += alp->nelem;
return (CFGA_OK);
}
/*
* Check if a plugin version is within requested limits.
*/
static int
compat_plugin(vers_req_t *reqp, int plugin_vers)
{
if (!VALID_HSL_VERS(reqp->v_min) || !VALID_HSL_VERS(reqp->v_max) ||
!VALID_HSL_VERS(plugin_vers)) {
return (0);
}
if (plugin_vers < reqp->v_min || plugin_vers > reqp->v_max) {
return (0);
}
return (1);
}
/*
* find_arg_type - determine if an argument is an ap_id or an ap_type.
* Adapted from cfgadm.c
*/
static cfga_ap_types_t
find_arg_type(const char *ap_id)
{
struct stat sbuf;
cfga_ap_types_t type = UNKNOWN_AP;
char *mkr = NULL;
size_t len;
int size_ap = 0, size_mkr = 0, digit = 0, i = 0;
char *cp, path[MAXPATHLEN], ap_base[MAXPATHLEN];
/*
* sanity checks
*/
if (ap_id == NULL || *ap_id == '\0') {
return (UNKNOWN_AP);
}
/*
* Extract the base component
*/
if ((cp = GET_DYN(ap_id)) != NULL) {
len = cp - ap_id;
} else {
len = strlen(ap_id);
}
if (len >= sizeof (ap_base)) {
return (UNKNOWN_AP);
}
/* Copy only the first "len" chars */
(void) strncpy(ap_base, ap_id, len);
ap_base[len] = '\0';
/*
* If it starts with a slash and is stat-able its a physical.
*/
if (*ap_base == '/' && stat(ap_base, &sbuf) == 0) {
return (PHYSICAL_AP);
}
/*
* Is this a symlink in CFGA_DEV_DIR ?
*/
(void) snprintf(path, sizeof (path), "%s%s",
CFGA_DEV_DIR SLASH, ap_base);
if (lstat(path, &sbuf) == 0 && S_ISLNK(sbuf.st_mode) &&
stat(path, &sbuf) == 0) {
return (LOGICAL_LINK_AP);
}
/*
* Check for ":" which is always present in an ap_id
* but not in an ap_type.
* we need to check that the characters right before the : are digits
* since an ap_id is of the form <name><instance>:<specific ap name>
*/
if ((mkr = strchr(ap_base, ':')) == NULL) {
type = AP_TYPE;
} else {
size_ap = strlen(ap_base);
size_mkr = strlen(mkr);
mkr = ap_base;
digit = 0;
for (i = size_ap - size_mkr - 1; i > 0; i--) {
if ((int)isdigit(mkr[i])) {
digit++;
break;
}
}
if (digit == 0) {
type = AP_TYPE;
} else {
type = LOGICAL_DRV_AP;
}
}
return (type);
}
/*ARGSUSED*/
static cfga_err_t
null_get_cond(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
{
return (CFGA_OK);
}
static cfga_err_t
get_cond_v1(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
{
plugin_lib_t *libp;
cfga_stat_data_t sdbuf;
cfga_err_t rc;
libp = liblocp->libp;
if (libp->plugin_vers != CFGA_HSL_V1) {
return (CFGA_LIB_ERROR);
}
errno = 0;
if ((rc = (*liblocp->libp->cfga_stat_p)(
liblocp->ap_physical, &sdbuf, NULL, errstring))
== CFGA_OK) {
*condp = sdbuf.ap_cond;
} else {
*condp = CFGA_COND_UNKNOWN;
}
return (rc);
}
static cfga_err_t
get_cond_v2(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
{
int nelem;
plugin_lib_t *libp;
cfga_list_data_t *ldbufp;
cfga_err_t rc;
libp = liblocp->libp;
if (libp->plugin_vers != CFGA_HSL_V2) {
return (CFGA_LIB_ERROR);
}
errno = 0;
nelem = 0;
ldbufp = NULL;
if ((rc = (*liblocp->libp->cfga_list_ext_p)(
liblocp->ap_physical, &ldbufp, &nelem, NULL, NULL,
errstring, 0)) == CFGA_OK) {
assert(nelem == 1 && ldbufp != NULL);
*condp = ldbufp->ap_cond;
S_FREE(ldbufp);
} else {
*condp = CFGA_COND_UNKNOWN;
}
return (rc);
}
/* mask represents the flags accepted */
static cfga_err_t
check_flags(cfga_flags_t flags, cfga_flags_t mask, char **errstring)
{
if ((flags & ~mask) != 0) {
config_err(0, INVALID_ARGS, errstring);
return (CFGA_ERROR);
} else {
return (CFGA_OK);
}
}
static cfga_err_t
check_apids(int num_ap_ids, char *const *ap_ids, char **errstring)
{
if (num_ap_ids <= 0 || ap_ids == NULL) {
config_err(0, INVALID_ARGS, errstring);
return (CFGA_ERROR);
} else {
return (CFGA_OK);
}
}
/*
* Returns the class or the empty string if attacment point has
* no class.
*/
static char *
get_class(di_minor_t minor)
{
char *cp, c;
size_t len;
if (minor == DI_MINOR_NIL) {
return (NULL);
}
cp = di_minor_nodetype(minor);
if (cp == NULL) {
return (NULL);
}
len = strlen(DDI_NT_ATTACHMENT_POINT);
if (strncmp(cp, DDI_NT_ATTACHMENT_POINT, len)) {
return (NULL);
}
cp += len;
c = *cp;
if (c != '\0' && c != ':') {
return (NULL);
}
if (c == ':') {
cp++;
}
return (cp);
}
/*
* Transform stat data to list data
*/
static void
stat_to_list(cfga_list_data_t *lp, cfga_stat_data_t *statp)
{
(void) snprintf(lp->ap_log_id, sizeof (lp->ap_log_id), "%s",
statp->ap_log_id);
(void) snprintf(lp->ap_phys_id, sizeof (lp->ap_phys_id), "%s",
statp->ap_phys_id);
(void) snprintf(lp->ap_class, sizeof (lp->ap_class), "%s",
CFGA_NO_CLASS);
lp->ap_r_state = statp->ap_r_state;
lp->ap_o_state = statp->ap_o_state;
lp->ap_cond = statp->ap_cond;
lp->ap_busy = statp->ap_busy;
lp->ap_status_time = statp->ap_status_time;
(void) snprintf(lp->ap_info, sizeof (lp->ap_info), "%s",
statp->ap_info);
(void) snprintf(lp->ap_type, sizeof (lp->ap_type), "%s",
statp->ap_type);
}
static void
lstat_free(list_stat_t *lstatp)
{
stat_data_list_t *slp, *slp2;
array_list_t *ap, *ap2;
slp = lstatp->sdl;
while (slp != NULL) {
slp2 = slp->next;
S_FREE(slp);
slp = slp2;
}
lstatp->sdl = NULL;
ap = lstatp->al;
while (ap != NULL) {
ap2 = ap->next;
S_FREE(ap->array);
S_FREE(ap);
ap = ap2;
}
lstatp->al = NULL;
}
static cfga_err_t
split_apid(char *ap_id, char **dyncompp, char **errstring)
{
char *cp;
*dyncompp = NULL;
if (ap_id == NULL) {
return (CFGA_ERROR);
}
if ((cp = strstr(ap_id, CFGA_DYN_SEP)) == NULL) {
return (CFGA_OK);
}
*cp = '\0';
cp += strlen(CFGA_DYN_SEP);
if ((*dyncompp = config_calloc_check(1, strlen(cp) + 1,
errstring)) == NULL) {
return (CFGA_LIB_ERROR);
}
(void) strcpy(*dyncompp, cp);
return (CFGA_OK);
}
static void
append_dyn(char *buf, const char *dyncomp, size_t blen)
{
if (dyncomp != NULL) {
char *cp = buf + strlen(buf);
size_t len = blen - strlen(buf);
(void) snprintf(cp, len, "%s%s", CFGA_DYN_SEP,
dyncomp);
}
}
/*
* Default implementation of cfga_ap_id_cmp. Works for most cases
* except for long hex number sequences like world-wide-name.
*
* This function compares the ap's in a generic way. It does so by
* determining the place of difference between the 2 aps. If the first
* difference is a digit, it attempts to obtain the numbers and compare them
* Otherwise it just compares the aps as strings
*/
static int
default_ap_id_cmp(const char *ap_id1, const char *ap_id2)
{
int i = 0;
/*
* Search for first different char
*/
while (ap_id1[i] == ap_id2[i] && ap_id1[i] != '\0')
i++;
/*
* If one of the char is a digit, back up to where the
* number started, compare the number.
*/
if (isdigit(ap_id1[i]) || isdigit(ap_id2[i])) {
while ((i > 0) && isdigit(ap_id1[i - 1]))
i--;
if (isdigit(ap_id1[i]) && isdigit(ap_id2[i]))
return (atoi(ap_id1 + i) - atoi(ap_id2 + i));
}
/* One of them isn't a number, compare the char */
return (ap_id1[i] - ap_id2[i]);
}
static void
hold_lib(plugin_lib_t *libp)
{
assert(libp->refcnt >= 0);
(void) mutex_lock(&libp->lock);
libp->refcnt++;
(void) mutex_unlock(&libp->lock);
}
static void
rele_lib(plugin_lib_t *libp)
{
assert(libp->refcnt > 0);
(void) mutex_lock(&libp->lock);
libp->refcnt--;
(void) mutex_unlock(&libp->lock);
}
|