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
|
#
# 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.
#
#ident "%Z%%M% %I% %E% SMI"
# Hammerhead: amd64-only
SUBDIRS = $(MACH64)
include ../Makefile.subdirs
#
# 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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
# Copyright 2024 Oxide Computer Co.
#
.KEEP_STATE:
.SUFFIXES:
SRCS += fmdump.c nvlrender.c asru.c error.c fault.c scheme.c info.c
OBJS = $(SRCS:%.c=%.o)
LINTFILES = $(SRCS:%.c=%.ln)
PROG = fmdump
ROOTPROG = $(ROOTUSRSBIN)/$(PROG)
$(NOT_RELEASE_BUILD)CPPFLAGS += -DDEBUG
CPPFLAGS += -I. -I../common -I../../include
CFLAGS += $(CTF_FLAGS) $(CCVERBOSE)
CFLAGS64 += $(CTF_FLAGS_64) $(CCVERBOSE)
LDLIBS += -L$(ROOT)/usr/lib/fm -lfmd_log -lnvpair -ltopo -lfmd_msg
LDFLAGS += -R/usr/lib/fm
LINTFLAGS += -mnu
CERRWARN += -Wno-parentheses
CERRWARN += $(CNOWARN_UNINIT)
CSTD = $(CSTD_GNU99)
.NOTPARALLEL:
.PARALLEL: $(OBJS) $(LINTFILES)
all: $(PROG)
$(PROG): $(OBJS)
$(LINK.c) $(OBJS) -o $@ $(LDLIBS)
$(CTFMERGE) -L VERSION -o $@ $(OBJS)
$(POST_PROCESS)
%.o: ../common/%.c
$(COMPILE.c) $<
$(CTFCONVERT_O)
%.o: %.c
$(COMPILE.c) $<
$(CTFCONVERT_O)
clean:
$(RM) $(OBJS) $(LINTFILES)
clobber: clean
$(RM) $(PROG)
%.ln: ../common/%.c
$(LINT.c) -c $<
%.ln: %.c
$(LINT.c) -c $<
lint: $(LINTFILES)
$(LINT) $(LINTFLAGS) $(LINTFILES)
install_h:
install: all $(ROOTPROG)
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
# Copyright 2025 Hammerhead Project
#
# Hammerhead: Include Makefile.cmd first to load Makefile.master
# (defines COMPILE.c, COMPILE64.c needed by Makefile.com's pattern rules)
include $(SRC)/cmd/Makefile.cmd
include ../Makefile.com
include $(SRC)/cmd/Makefile.cmd.64
install: all $(ROOTPROG64)
/*
* 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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, Joyent, Inc. All rights reserved.
*/
#include <fmdump.h>
#include <strings.h>
#include <stdio.h>
#include <time.h>
/*ARGSUSED*/
static int
asru_short(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
char buf[32];
fmdump_printf(fp, "%-20s %-32s\n",
fmdump_date(buf, sizeof (buf), rp), rp->rec_class);
return (0);
}
/*ARGSUSED*/
static int
asru_verb1(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
char *uuid = "-";
boolean_t f = 0, u = 0;
char buf[32], state[32];
(void) nvlist_lookup_string(rp->rec_nvl, FM_RSRC_ASRU_UUID, &uuid);
(void) nvlist_lookup_boolean_value(rp->rec_nvl,
FM_RSRC_ASRU_FAULTY, &f);
(void) nvlist_lookup_boolean_value(rp->rec_nvl,
FM_RSRC_ASRU_UNUSABLE, &u);
state[0] = '\0';
if (f)
(void) strcat(state, ",faulty");
if (u)
(void) strcat(state, ",unusable");
if (!f && !u)
(void) strcat(state, ",ok");
fmdump_printf(fp, "%-20s %-36s %s\n",
fmdump_date(buf, sizeof (buf), rp), uuid, state + 1);
return (0);
}
/*ARGSUSED*/
static int
asru_verb23_cmn(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp,
nvlist_prtctl_t pctl)
{
char *uuid = "-";
boolean_t f = 0, u = 0;
char buf[32], state[32];
(void) nvlist_lookup_string(rp->rec_nvl, FM_RSRC_ASRU_UUID, &uuid);
(void) nvlist_lookup_boolean_value(rp->rec_nvl,
FM_RSRC_ASRU_FAULTY, &f);
(void) nvlist_lookup_boolean_value(rp->rec_nvl,
FM_RSRC_ASRU_UNUSABLE, &u);
state[0] = '\0';
if (f)
(void) strcat(state, ",faulty");
if (u)
(void) strcat(state, ",unusable");
if (!f && !u)
(void) strcat(state, ",ok");
fmdump_printf(fp, "%-20s.%9.9llu %-36s %s\n",
fmdump_year(buf, sizeof (buf), rp), rp->rec_nsec, uuid, state + 1);
if (pctl)
nvlist_prt(rp->rec_nvl, pctl);
else
nvlist_print(fp, rp->rec_nvl);
fmdump_printf(fp, "\n");
return (0);
}
static int
asru_verb2(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
return (asru_verb23_cmn(lp, rp, fp, NULL));
}
static int
asru_pretty(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
nvlist_prtctl_t pctl;
int rc;
if ((pctl = nvlist_prtctl_alloc()) != NULL) {
nvlist_prtctl_setdest(pctl, fp);
nvlist_prtctlop_nvlist(pctl, fmdump_render_nvlist, NULL);
}
rc = asru_verb23_cmn(lp, rp, fp, pctl);
nvlist_prtctl_free(pctl);
return (rc);
}
const fmdump_ops_t fmdump_asru_ops = {
"asru", {
{
"TIME CLASS",
(fmd_log_rec_f *)asru_short
}, {
"TIME UUID STATE",
(fmd_log_rec_f *)asru_verb1
}, {
"TIME UUID STATE",
(fmd_log_rec_f *)asru_verb2
}, {
"TIME UUID STATE",
(fmd_log_rec_f *)asru_pretty
}, {
NULL, NULL
}, {
NULL,
(fmd_log_rec_f *)fmdump_print_json
} }
};
/*
* 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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, Joyent, Inc. All rights reserved.
*/
#include <fmdump.h>
#include <stdio.h>
#include <time.h>
/*ARGSUSED*/
static int
err_short(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
char buf[32];
fmdump_printf(fp, "%-20s %-32s\n",
fmdump_date(buf, sizeof (buf), rp), rp->rec_class);
return (0);
}
/*ARGSUSED*/
static int
err_verb1(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
uint64_t ena = 0;
char buf[32];
(void) nvlist_lookup_uint64(rp->rec_nvl, FM_EREPORT_ENA, &ena);
fmdump_printf(fp, "%-20s %-37s 0x%016llx\n",
fmdump_date(buf, sizeof (buf), rp), rp->rec_class, ena);
return (0);
}
/*ARGSUSED*/
static int
err_verb23_cmn(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp,
nvlist_prtctl_t pctl)
{
char buf[32];
fmdump_printf(fp, "%-20s.%9.9llu %s\n",
fmdump_year(buf, sizeof (buf), rp), rp->rec_nsec, rp->rec_class);
if (pctl)
nvlist_prt(rp->rec_nvl, pctl);
else
nvlist_print(fp, rp->rec_nvl);
fmdump_printf(fp, "\n");
return (0);
}
static int
err_verb2(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
return (err_verb23_cmn(lp, rp, fp, NULL));
}
static int
err_pretty(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
nvlist_prtctl_t pctl;
int rc;
if ((pctl = nvlist_prtctl_alloc()) != NULL) {
nvlist_prtctl_setdest(pctl, fp);
nvlist_prtctlop_nvlist(pctl, fmdump_render_nvlist, NULL);
}
rc = err_verb23_cmn(lp, rp, fp, pctl);
nvlist_prtctl_free(pctl);
return (rc);
}
const fmdump_ops_t fmdump_err_ops = {
"error", {
{
"TIME CLASS",
(fmd_log_rec_f *)err_short
}, {
"TIME CLASS ENA",
(fmd_log_rec_f *)err_verb1
}, {
"TIME CLASS",
(fmd_log_rec_f *)err_verb2
}, {
"TIME CLASS",
(fmd_log_rec_f *)err_pretty
}, {
NULL, NULL
}, {
NULL,
(fmd_log_rec_f *)fmdump_print_json
} }
};
/*
* 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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, Joyent, Inc.
*/
#include <fmdump.h>
#include <stdio.h>
#include <strings.h>
/*ARGSUSED*/
static int
flt_short(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
char buf[32], str[32];
char *class = NULL, *uuid = "-", *code = "-";
static const struct {
const char *class;
const char *tag;
} tags[] = {
{ FM_LIST_SUSPECT_CLASS, "Diagnosed" },
{ FM_LIST_REPAIRED_CLASS, "Repaired" },
{ FM_LIST_RESOLVED_CLASS, "Resolved" },
{ FM_LIST_UPDATED_CLASS, "Updated" },
{ FM_LIST_ISOLATED_CLASS, "Isolated" },
};
(void) nvlist_lookup_string(rp->rec_nvl, FM_SUSPECT_UUID, &uuid);
(void) nvlist_lookup_string(rp->rec_nvl, FM_SUSPECT_DIAG_CODE, &code);
(void) nvlist_lookup_string(rp->rec_nvl, FM_CLASS, &class);
if (class != NULL) {
int i;
for (i = 0; i < sizeof (tags) / sizeof (tags[0]); i++) {
if (strcmp(class, tags[i].class) == 0) {
(void) snprintf(str, sizeof (str), "%s %s",
code, tags[i].tag);
code = str;
break;
}
}
}
fmdump_printf(fp, "%-20s %-32s %s\n",
fmdump_date(buf, sizeof (buf), rp), uuid, code);
return (0);
}
static int
flt_verb1(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
uint_t i, size = 0;
nvlist_t **nva;
uint8_t *ba;
(void) flt_short(lp, rp, fp);
(void) nvlist_lookup_uint32(rp->rec_nvl, FM_SUSPECT_FAULT_SZ, &size);
if (size != 0) {
(void) nvlist_lookup_nvlist_array(rp->rec_nvl,
FM_SUSPECT_FAULT_LIST, &nva, &size);
(void) nvlist_lookup_uint8_array(rp->rec_nvl,
FM_SUSPECT_FAULT_STATUS, &ba, &size);
}
for (i = 0; i < size; i++) {
char *class = NULL, *rname = NULL, *aname = NULL, *fname = NULL;
char *loc = NULL;
nvlist_t *fru, *asru, *rsrc;
uint8_t pct = 0;
(void) nvlist_lookup_uint8(nva[i], FM_FAULT_CERTAINTY, &pct);
(void) nvlist_lookup_string(nva[i], FM_CLASS, &class);
if (nvlist_lookup_nvlist(nva[i], FM_FAULT_FRU, &fru) == 0)
fname = fmdump_nvl2str(fru);
if (nvlist_lookup_nvlist(nva[i], FM_FAULT_ASRU, &asru) == 0)
aname = fmdump_nvl2str(asru);
if (nvlist_lookup_nvlist(nva[i], FM_FAULT_RESOURCE, &rsrc) == 0)
rname = fmdump_nvl2str(rsrc);
if (nvlist_lookup_string(nva[i], FM_FAULT_LOCATION, &loc)
== 0) {
if (fname && strncmp(fname, FM_FMRI_LEGACY_HC_PREFIX,
sizeof (FM_FMRI_LEGACY_HC_PREFIX)) == 0)
loc = fname + sizeof (FM_FMRI_LEGACY_HC_PREFIX);
}
fmdump_printf(fp, " %3u%% %s",
pct, class ? class : "-");
if (ba[i] & FM_SUSPECT_FAULTY)
fmdump_printf(fp, "\n\n");
else if (ba[i] & FM_SUSPECT_NOT_PRESENT)
fmdump_printf(fp, "\tRemoved\n\n");
else if (ba[i] & FM_SUSPECT_REPLACED)
fmdump_printf(fp, "\tReplaced\n\n");
else if (ba[i] & FM_SUSPECT_REPAIRED)
fmdump_printf(fp, "\tRepair Attempted\n\n");
else if (ba[i] & FM_SUSPECT_ACQUITTED)
fmdump_printf(fp, "\tAcquitted\n\n");
else
fmdump_printf(fp, "\n\n");
fmdump_printf(fp, " Problem in: %s\n",
rname ? rname : "-");
fmdump_printf(fp, " Affects: %s\n",
aname ? aname : "-");
fmdump_printf(fp, " FRU: %s\n",
fname ? fname : "-");
fmdump_printf(fp, " Location: %s\n\n",
loc ? loc : "-");
free(fname);
free(aname);
free(rname);
}
return (0);
}
static int
flt_verb23_cmn(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp,
nvlist_prtctl_t pctl)
{
const struct fmdump_fmt *efp = &fmdump_err_ops.do_formats[FMDUMP_VERB1];
const struct fmdump_fmt *ffp = &fmdump_flt_ops.do_formats[FMDUMP_VERB2];
uint_t i;
char buf[32], str[32];
char *class = NULL, *uuid = "-", *code = "-";
(void) nvlist_lookup_string(rp->rec_nvl, FM_SUSPECT_UUID, &uuid);
(void) nvlist_lookup_string(rp->rec_nvl, FM_SUSPECT_DIAG_CODE, &code);
(void) nvlist_lookup_string(rp->rec_nvl, FM_CLASS, &class);
if (class != NULL && strcmp(class, FM_LIST_REPAIRED_CLASS) == 0) {
(void) snprintf(str, sizeof (str), "%s %s", code, "Repaired");
code = str;
}
if (class != NULL && strcmp(class, FM_LIST_RESOLVED_CLASS) == 0) {
(void) snprintf(str, sizeof (str), "%s %s", code, "Resolved");
code = str;
}
if (class != NULL && strcmp(class, FM_LIST_UPDATED_CLASS) == 0) {
(void) snprintf(str, sizeof (str), "%s %s", code, "Updated");
code = str;
}
fmdump_printf(fp, "%s\n", ffp->do_hdr);
fmdump_printf(fp, "%-20s.%9.9llu %-32s %s\n",
fmdump_year(buf, sizeof (buf), rp), rp->rec_nsec, uuid, code);
if (rp->rec_nrefs != 0)
fmdump_printf(fp, "\n %s\n", efp->do_hdr);
for (i = 0; i < rp->rec_nrefs; i++) {
fmdump_printf(fp, " ");
(void) efp->do_func(lp, &rp->rec_xrefs[i], fp);
}
fmdump_printf(fp, "\n");
if (pctl)
nvlist_prt(rp->rec_nvl, pctl);
else
nvlist_print(fp, rp->rec_nvl);
fmdump_printf(fp, "\n");
return (0);
}
static int
flt_verb2(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
return (flt_verb23_cmn(lp, rp, fp, NULL));
}
static int
flt_pretty(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
nvlist_prtctl_t pctl;
int rc;
if ((pctl = nvlist_prtctl_alloc()) != NULL) {
nvlist_prtctl_setdest(pctl, fp);
nvlist_prtctlop_nvlist(pctl, fmdump_render_nvlist, NULL);
}
rc = flt_verb23_cmn(lp, rp, fp, pctl);
nvlist_prtctl_free(pctl);
return (rc);
}
/*
* There is a lack of uniformity in how the various entries in our diagnosis
* are terminated. Some end with one newline, others with two. This makes the
* output of fmdump -m look a bit ugly. Therefore we postprocess the message
* before printing it, removing consecutive occurences of newlines.
*/
static void
postprocess_msg(char *msg)
{
int i = 0, j = 0;
char *buf;
if ((buf = malloc(strlen(msg) + 1)) == NULL)
return;
buf[j++] = msg[i++];
for (i = 1; i < strlen(msg); i++) {
if (!(msg[i] == '\n' && msg[i - 1] == '\n'))
buf[j++] = msg[i];
}
buf[j] = '\0';
(void) strncpy(msg, buf, j+1);
free(buf);
}
/*ARGSUSED*/
static int
flt_msg(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
char *msg, *uuid = "-", *code = "-";
if ((msg = fmd_msg_gettext_nv(g_msg, NULL, rp->rec_nvl)) == NULL) {
(void) nvlist_lookup_string(rp->rec_nvl, FM_SUSPECT_UUID,
&uuid);
(void) nvlist_lookup_string(rp->rec_nvl, FM_SUSPECT_DIAG_CODE,
&code);
(void) fprintf(stderr, "%s: failed to format message for "
"diagcode %s, event %s: %s\n\n", g_pname, code, uuid,
strerror(errno));
g_errs++;
} else {
postprocess_msg(msg);
fmdump_printf(fp, "%s\n", msg);
free(msg);
}
return (0);
}
const fmdump_ops_t fmdump_flt_ops = {
"fault", {
{
"TIME UUID SUNW-MSG-ID "
"EVENT",
(fmd_log_rec_f *)flt_short
}, {
"TIME UUID SUNW-MSG-ID "
"EVENT",
(fmd_log_rec_f *)flt_verb1
}, {
"TIME UUID"
" SUNW-MSG-ID",
(fmd_log_rec_f *)flt_verb2
}, {
"TIME UUID"
" SUNW-MSG-ID",
(fmd_log_rec_f *)flt_pretty
}, {
NULL,
(fmd_log_rec_f *)flt_msg
}, {
NULL,
(fmd_log_rec_f *)fmdump_print_json
} }
};
/*
* 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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2012 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 2013, Joyent, Inc. All rights reserved.
* Copyright 2024 Oxide Computer Co.
*/
#include <alloca.h>
#include <unistd.h>
#include <limits.h>
#include <strings.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include <regex.h>
#include <dirent.h>
#include <pthread.h>
#include <fmdump.h>
#define FMDUMP_EXIT_SUCCESS 0
#define FMDUMP_EXIT_FATAL 1
#define FMDUMP_EXIT_USAGE 2
#define FMDUMP_EXIT_ERROR 3
const char *g_pname;
ulong_t g_errs;
ulong_t g_recs;
char *g_root;
struct topo_hdl *g_thp;
fmd_msg_hdl_t *g_msg;
/*PRINTFLIKE2*/
void
fmdump_printf(FILE *fp, const char *format, ...)
{
va_list ap;
va_start(ap, format);
if (vfprintf(fp, format, ap) < 0) {
(void) fprintf(stderr, "%s: failed to print record: %s\n",
g_pname, strerror(errno));
g_errs++;
}
va_end(ap);
}
void
fmdump_vwarn(const char *format, va_list ap)
{
int err = errno;
(void) fprintf(stderr, "%s: warning: ", g_pname);
(void) vfprintf(stderr, format, ap);
if (strchr(format, '\n') == NULL)
(void) fprintf(stderr, ": %s\n", strerror(err));
g_errs++;
}
/*PRINTFLIKE1*/
void
fmdump_warn(const char *format, ...)
{
va_list ap;
va_start(ap, format);
fmdump_vwarn(format, ap);
va_end(ap);
}
static void
fmdump_exit(int err, int exitcode, const char *format, va_list ap)
{
(void) fprintf(stderr, "%s: ", g_pname);
(void) vfprintf(stderr, format, ap);
if (strchr(format, '\n') == NULL)
(void) fprintf(stderr, ": %s\n", strerror(err));
exit(exitcode);
}
/*PRINTFLIKE1*/
static void
fmdump_fatal(const char *format, ...)
{
int err = errno;
va_list ap;
va_start(ap, format);
fmdump_exit(err, FMDUMP_EXIT_FATAL, format, ap);
va_end(ap);
}
/*PRINTFLIKE1*/
static void
fmdump_usage(const char *format, ...)
{
int err = errno;
va_list ap;
va_start(ap, format);
fmdump_exit(err, FMDUMP_EXIT_USAGE, format, ap);
va_end(ap);
}
char *
fmdump_date(char *buf, size_t len, const fmd_log_record_t *rp)
{
if (rp->rec_sec > LONG_MAX) {
fmdump_warn("record time is too large for 32-bit utility\n");
(void) snprintf(buf, len, "0x%llx", rp->rec_sec);
} else {
time_t tod = (time_t)rp->rec_sec;
time_t now = time(NULL);
if (tod > now+60 ||
tod < now - 6L*30L*24L*60L*60L) { /* 6 months ago */
(void) strftime(buf, len, "%b %d %Y %T",
localtime(&tod));
} else {
size_t sz;
sz = strftime(buf, len, "%b %d %T", localtime(&tod));
(void) snprintf(buf + sz, len - sz, ".%4.4llu",
rp->rec_nsec / (NANOSEC / 10000));
}
}
return (buf);
}
char *
fmdump_year(char *buf, size_t len, const fmd_log_record_t *rp)
{
#ifdef _ILP32
if (rp->rec_sec > LONG_MAX) {
fmdump_warn("record time is too large for 32-bit utility\n");
(void) snprintf(buf, len, "0x%llx", rp->rec_sec);
} else {
#endif
time_t tod = (time_t)rp->rec_sec;
(void) strftime(buf, len, "%b %d %Y %T", localtime(&tod));
#ifdef _ILP32
}
#endif
return (buf);
}
/* BEGIN CSTYLED */
static const char *synopsis =
"Usage: %s [[-e | -i | -I | -u] | -A ] [-f] [-aHmvVp] [-c class] [-R root]\n"
"\t [-t time] [-T time] [-u uuid] [-n name[.name]*[=value]]\n"
"\t [-N name[.name]*[=value][;name[.name]*[=value]]*] "
"[file]...\n "
"Log selection: [-e | -i | -I] or one [file]; default is the fault log\n"
"\t-e display error log content\n"
"\t-i display infolog content\n"
"\t-I display the high-value-infolog content\n"
"\t-R set root directory for pathname expansions\n "
"Command behaviour:\n"
"\t-A Aggregate specified [file]s or, if no [file], all known logs\n"
"\t-H display the log's header attributes instead of contents\n"
"\t-f follow growth of log file by waiting for additional data\n "
"Output options:\n"
"\t-j Used with -V: emit JSON-formatted output\n"
"\t-m display human-readable messages (only for fault logs)\n"
"\t-p Used with -V: apply some output prettification\n"
"\t-v set verbose mode: display additional event detail\n"
"\t-V set very verbose mode: display complete event contents\n "
"Selection filters:\n"
"\t-a select all events, including normally silent events\n"
"\t-c select events that match the specified class\n"
"\t-n select events containing named nvpair (with matching value)\n"
"\t-N select events matching multiple property names (or nvpairs)\n"
"\t-t select events that occurred after the specified time\n"
"\t-T select events that occurred before the specified time\n"
"\t-u select events that match the specified diagnosis uuid\n";
/* END CSTYLED */
static int
usage(FILE *fp)
{
(void) fprintf(fp, synopsis, g_pname);
return (FMDUMP_EXIT_USAGE);
}
/*ARGSUSED*/
static int
error(fmd_log_t *lp, void *private)
{
fmdump_warn("skipping record: %s\n",
fmd_log_errmsg(lp, fmd_log_errno(lp)));
return (0);
}
/*
* Yet another disgusting argument parsing function (TM). We attempt to parse
* a time argument in a variety of strptime(3C) formats, in which case it is
* interpreted as a local time and is converted to a timeval using mktime(3C).
* If those formats fail, we look to see if the time is a decimal integer
* followed by one of our magic suffixes, in which case the time is interpreted
* as a time delta *before* the current time-of-day (i.e. "1h" = "1 hour ago").
*/
static struct timeval *
gettimeopt(const char *arg)
{
const struct {
const char *name;
hrtime_t mul;
} suffix[] = {
{ "ns", NANOSEC / NANOSEC },
{ "nsec", NANOSEC / NANOSEC },
{ "us", NANOSEC / MICROSEC },
{ "usec", NANOSEC / MICROSEC },
{ "ms", NANOSEC / MILLISEC },
{ "msec", NANOSEC / MILLISEC },
{ "s", NANOSEC / SEC },
{ "sec", NANOSEC / SEC },
{ "m", NANOSEC * (hrtime_t)60 },
{ "min", NANOSEC * (hrtime_t)60 },
{ "h", NANOSEC * (hrtime_t)(60 * 60) },
{ "hour", NANOSEC * (hrtime_t)(60 * 60) },
{ "d", NANOSEC * (hrtime_t)(24 * 60 * 60) },
{ "day", NANOSEC * (hrtime_t)(24 * 60 * 60) },
{ NULL }
};
struct timeval *tvp = malloc(sizeof (struct timeval));
struct timeval tod;
struct tm tm;
char *p;
if (tvp == NULL)
fmdump_fatal("failed to allocate memory");
if (gettimeofday(&tod, NULL) != 0)
fmdump_fatal("failed to get tod");
/*
* First try a variety of strptime() calls. If these all fail, we'll
* try parsing an integer followed by one of our suffix[] strings.
*/
if ((p = strptime(arg, "%m/%d/%Y %H:%M:%S", &tm)) == NULL &&
(p = strptime(arg, "%m/%d/%y %H:%M:%S", &tm)) == NULL &&
(p = strptime(arg, "%m/%d/%Y %H:%M", &tm)) == NULL &&
(p = strptime(arg, "%m/%d/%y %H:%M", &tm)) == NULL &&
(p = strptime(arg, "%m/%d/%Y", &tm)) == NULL &&
(p = strptime(arg, "%m/%d/%y", &tm)) == NULL &&
(p = strptime(arg, "%Y-%m-%dT%H:%M:%S", &tm)) == NULL &&
(p = strptime(arg, "%y-%m-%dT%H:%M:%S", &tm)) == NULL &&
(p = strptime(arg, "%Y-%m-%dT%H:%M", &tm)) == NULL &&
(p = strptime(arg, "%y-%m-%dT%H:%M", &tm)) == NULL &&
(p = strptime(arg, "%Y-%m-%d", &tm)) == NULL &&
(p = strptime(arg, "%y-%m-%d", &tm)) == NULL &&
(p = strptime(arg, "%d%b%Y %H:%M:%S", &tm)) == NULL &&
(p = strptime(arg, "%d%b%y %H:%M:%S", &tm)) == NULL &&
(p = strptime(arg, "%d%b%Y %H:%M", &tm)) == NULL &&
(p = strptime(arg, "%d%b%y %H:%M", &tm)) == NULL &&
(p = strptime(arg, "%d%b%Y", &tm)) == NULL &&
(p = strptime(arg, "%d%b%y", &tm)) == NULL &&
(p = strptime(arg, "%b %d %H:%M:%S", &tm)) == NULL &&
(p = strptime(arg, "%b %d %H:%M:%S", &tm)) == NULL &&
(p = strptime(arg, "%H:%M:%S", &tm)) == NULL &&
(p = strptime(arg, "%H:%M", &tm)) == NULL) {
hrtime_t nsec;
int i;
errno = 0;
nsec = strtol(arg, (char **)&p, 10);
if (errno != 0 || nsec == 0 || p == arg || *p == '\0')
fmdump_usage("illegal time format -- %s\n", arg);
for (i = 0; suffix[i].name != NULL; i++) {
if (strcasecmp(suffix[i].name, p) == 0) {
nsec *= suffix[i].mul;
break;
}
}
if (suffix[i].name == NULL)
fmdump_usage("illegal time format -- %s\n", arg);
tvp->tv_sec = nsec / NANOSEC;
tvp->tv_usec = (nsec % NANOSEC) / (NANOSEC / MICROSEC);
if (tvp->tv_sec > tod.tv_sec)
fmdump_usage("time delta precedes UTC time origin "
"-- %s\n", arg);
tvp->tv_sec = tod.tv_sec - tvp->tv_sec;
} else if (*p == '\0' || *p == '.') {
/*
* If tm_year is zero, we matched [%b %d] %H:%M[:%S]; use
* the result of localtime(&tod.tv_sec) to fill in the rest.
*/
if (tm.tm_year == 0) {
int h = tm.tm_hour;
int m = tm.tm_min;
int s = tm.tm_sec;
int b = tm.tm_mon;
int d = tm.tm_mday;
bcopy(localtime(&tod.tv_sec), &tm, sizeof (tm));
tm.tm_isdst = 0; /* see strptime(3C) and below */
if (d > 0) {
tm.tm_mon = b;
tm.tm_mday = d;
}
tm.tm_hour = h;
tm.tm_min = m;
tm.tm_sec = s;
}
errno = 0;
tvp->tv_sec = mktime(&tm);
tvp->tv_usec = 0;
if (tvp->tv_sec == -1L && errno != 0)
fmdump_fatal("failed to compose time %s", arg);
/*
* If our mktime() set tm_isdst, adjust the result for DST by
* subtracting the offset between the main and alternate zones.
*/
if (tm.tm_isdst)
tvp->tv_sec -= timezone - altzone;
if (p[0] == '.') {
arg = p;
errno = 0;
tvp->tv_usec =
(suseconds_t)(strtod(arg, &p) * (double)MICROSEC);
if (errno != 0 || p == arg || *p != '\0')
fmdump_usage("illegal time suffix -- .%s\n",
arg);
}
} else {
fmdump_usage("unexpected suffix after time %s -- %s\n", arg, p);
}
return (tvp);
}
/*
* If the -u option is specified in combination with the -e option, we iterate
* over each record in the fault log with a matching UUID finding xrefs to the
* error log, and then use this function to iterate over every xref'd record.
*/
int
xref_iter(fmd_log_t *lp, const fmd_log_record_t *rp, void *arg)
{
const fmd_log_record_t *xrp = rp->rec_xrefs;
fmdump_arg_t *dap = arg;
int i, rv = 0;
for (i = 0; rv == 0 && i < rp->rec_nrefs; i++, xrp++) {
if (fmd_log_filter(lp, dap->da_fc, dap->da_fv, xrp))
rv = dap->da_fmt->do_func(lp, xrp, dap->da_fp);
}
return (rv);
}
int
xoff_iter(fmd_log_t *lp, const fmd_log_record_t *rp, void *arg)
{
fmdump_lyr_t *dyp = arg;
fmdump_printf(dyp->dy_fp, "%16llx ", (u_longlong_t)rp->rec_off);
return (dyp->dy_func(lp, rp, dyp->dy_arg));
}
/*
* Initialize fmd_log_filter_nvarg_t from -n name=value argument string.
*/
static fmd_log_filter_nvarg_t *
setupnamevalue(char *namevalue)
{
fmd_log_filter_nvarg_t *argt;
char *value;
regex_t *value_regex = NULL;
char errstr[128];
int rv;
if ((value = strchr(namevalue, '=')) == NULL) {
value_regex = NULL;
} else {
*value++ = '\0'; /* separate name and value string */
/*
* Skip white space before value to facilitate direct
* cut/paste from previous fmdump output.
*/
while (isspace(*value))
value++;
if ((value_regex = malloc(sizeof (regex_t))) == NULL)
fmdump_fatal("failed to allocate memory");
/* compile regular expression for possible string match */
if ((rv = regcomp(value_regex, value,
REG_NOSUB|REG_NEWLINE)) != 0) {
(void) regerror(rv, value_regex, errstr,
sizeof (errstr));
free(value_regex);
fmdump_usage("unexpected regular expression in "
"%s: %s\n", value, errstr);
}
}
if ((argt = calloc(1, sizeof (fmd_log_filter_nvarg_t))) == NULL)
fmdump_fatal("failed to allocate memory");
argt->nvarg_name = namevalue; /* now just name */
argt->nvarg_value = value;
argt->nvarg_value_regex = value_regex;
return (argt);
}
/*
* As for setupnamevalue() above, create our chain of filter arguments for -N
* [name[=value][;name[=value]]*. This would be simple except for the problems
* of escaping something in a string. To accommodate the use of the ; within
* the chain, we allow it to be escaped. One might imagine that the backslash
* character should be used to escape it, but that opens Pandora's box because
* the value portion of each entry (if present) is allowed to be a regex. The
* treatment of backslashes within regexes is not something we want to replicate
* here, which would be necessary if we wanted to allow escaping the ; with a
* backslash. Specifically, consider how we treat the sequence of characters
* '\\;x' (two backslash characters followed by a semicolon and then some other
* character x). In the name portion of the entry, this would be a backslash
* followed by an escaped semicolon, so that we would treat this as '\;' and
* include x and subsequent characters in this entry. In the value portion (if
* present), we would have to treat it as a pair of backslashes followed by the
* terminating ; and the next entry would begin with 'x'... except that we
* might be inside [] where the backslash is not special, and so on.
*
* Let's not do that. Instead, we allow the user to 'escape' the ; by repeating
* it, and we interpret that before any regex interpretation is done. Therefore
* *every* pair of consecutive semicolons, regardless of where it appears, is
* replaced by a literal semicolon. This allows the semicolon to appear any
* number of times in either the name or, if present, the value, including as
* part of a regex (see regexp(7)), simply by doubling it. A non-doubled
* semicolon always terminates the entry. This now creates one more problem:
* whether to treat ';;;' as a literal semicolon followed by the entry
* terminator, or the entry terminator followed by a literal semicolon to start
* the next entry. Here we have to cheat a little: it's clear from the FMD PRM
* (especially chapter 10 as well as the schema for module properties, buffers,
* statistics, and other entities) that the event member namespace is intended
* to exclude both the semicolon and whitespace. A value, or a regex intended
* to match values, might well include anything. Therefore, a semicolon at the
* beginning of an entry is unlikely to be useful, while one at the end of an
* entry may well be intentional. We'll allow either or both when unambiguous,
* but a sequence containing an odd number of consecutive ';' characters will be
* interpreted as half that number of literal semicolons (rounded down) followed
* by the terminator. If the user wishes to begin an event property name with a
* semicolon, it needs to be the first property in the chain. Chains with
* multiple properties whose names begin with a literal semicolon are not
* supported. Again, this almost certainly can never matter as no event should
* ever have a property whose name contains a semicolon.
*
* We choose the semicolon because the comma is very likely to be present in
* some property values on which the user may want to filter, especially the
* name of device paths. The semicolon may itself appear in values, especially
* if the property is a URI, though it is likely much less common. We have to
* pick something. If this proves unwieldy or insufficiently expressive, it
* will need to be replaced by a full-on logical expression parser with
* first-class support for internal quoting, escaping, and regexes. One might
* be better off dumping JSON and importing it into a SQL database if that level
* of complexity is required.
*/
static fmd_log_filter_nvarg_t *
setupnamevalue_multi(char *chainstr)
{
fmd_log_filter_nvarg_t *argchain = NULL;
size_t rem = strlen(chainstr) + 1;
fmd_log_filter_nvarg_t *argt;
/*
* Here, rem holds the number of characters remaining that we are
* permitted to examine, including the terminating NUL. If the first
* entry begins with a single semicolon, it is considered empty and
* ignored. Similarly, a trailing semicolon is optional and ignored if
* present. We won't create empty filter entries for any input.
*/
for (char *nv = chainstr; rem > 0; ++chainstr, --rem) {
switch (*chainstr) {
case ';':
ASSERT(rem > 1);
/*
* Check for double-semicolon. If found,
* de-duplicate it and advance past, then continue the
* loop: we can't be done yet.
*/
if (chainstr[1] == ';') {
ASSERT(rem > 2);
--rem;
(void) memmove(chainstr, chainstr + 1, rem);
break;
}
*chainstr = '\0';
/*FALLTHROUGH*/
case '\0':
if (chainstr != nv) {
argt = setupnamevalue(nv);
argt->nvarg_next = argchain;
argchain = argt;
}
nv = chainstr + 1;
/*FALLTHROUGH*/
default:
ASSERT(rem > 0);
}
}
return (argchain);
}
/*
* If the -a option is not present, filter out fault records that correspond
* to events that the producer requested not be messaged for administrators.
*/
/*ARGSUSED*/
int
log_filter_silent(fmd_log_t *lp, const fmd_log_record_t *rp, void *arg)
{
int opt_A = (arg != NULL);
boolean_t msg;
char *class;
/*
* If -A was used then apply this filter only to events of list class
*/
if (opt_A) {
if (nvlist_lookup_string(rp->rec_nvl, FM_CLASS, &class) != 0 ||
strncmp(class, FM_LIST_EVENT ".",
sizeof (FM_LIST_EVENT)) != 0)
return (1);
}
return (nvlist_lookup_boolean_value(rp->rec_nvl,
FM_SUSPECT_MESSAGE, &msg) != 0 || msg != 0);
}
struct loglink {
char *path;
long suffix;
struct loglink *next;
};
static void
addlink(struct loglink **llp, char *dirname, char *logname, long suffix)
{
struct loglink *newp;
size_t len;
char *str;
newp = malloc(sizeof (struct loglink));
len = strlen(dirname) + strlen(logname) + 2;
str = malloc(len);
if (newp == NULL || str == NULL)
fmdump_fatal("failed to allocate memory");
(void) snprintf(str, len, "%s/%s", dirname, logname);
newp->path = str;
newp->suffix = suffix;
while (*llp != NULL && suffix < (*llp)->suffix)
llp = &(*llp)->next;
newp->next = *llp;
*llp = newp;
}
/*
* Find and return all the rotated logs.
*/
static struct loglink *
get_rotated_logs(char *logpath)
{
char dirname[PATH_MAX], *logname, *endptr;
DIR *dirp;
struct dirent *dp;
long len, suffix;
struct loglink *head = NULL;
(void) strlcpy(dirname, logpath, sizeof (dirname));
logname = strrchr(dirname, '/');
*logname++ = '\0';
len = strlen(logname);
if ((dirp = opendir(dirname)) == NULL) {
fmdump_warn("failed to opendir `%s'", dirname);
g_errs++;
return (NULL);
}
while ((dp = readdir(dirp)) != NULL) {
/*
* Search the log directory for logs named "<logname>.0",
* "<logname>.1", etc and add to the link in the
* reverse numeric order.
*/
if (strlen(dp->d_name) < len + 2 ||
strncmp(dp->d_name, logname, len) != 0 ||
dp->d_name[len] != '.')
continue;
/*
* "*.0-" file normally should not be seen. It may
* exist when user manually run 'fmadm rotate'.
* In such case, we put it at the end of the list so
* it'll be dumped after all the rotated logs, before
* the current one.
*/
if (strcmp(dp->d_name + len + 1, "0-") == 0)
addlink(&head, dirname, dp->d_name, -1);
else if ((suffix = strtol(dp->d_name + len + 1,
&endptr, 10)) >= 0 && *endptr == '\0')
addlink(&head, dirname, dp->d_name, suffix);
}
(void) closedir(dirp);
return (head);
}
/*
* Aggregate log files. If ifiles is not NULL then one or more files
* were listed on the command line, and we will merge just those files.
* Otherwise we will merge all known log file types, and include the
* rotated logs for each type (you can suppress the inclusion of
* some logtypes through use of FMDUMP_AGGREGATE_IGNORE in the process
* environment, setting it to a comma-separated list of log labels and/or
* log filenames to ignore).
*
* We will not attempt to perform a chronological sort across all log records
* of all files. Indeed, we won't even sort individual log files -
* we will not re-order events differently to how they appeared in their
* original log file. This is because log files are already inherently
* ordered by the order in which fmd receives and processes events.
* So we determine the output order by comparing the "next" record
* off the top of each log file.
*
* We will construct a number of log record source "pipelines". As above,
* the next record to render in the overall output is that from the
* pipeline with the oldest event.
*
* For the case that input logfiles were listed on the command line, each
* pipeline will process exactly one of those logfiles. Distinct pipelines
* may process logfiles of the same "type" - eg if two "error" logs and
* one "fault" logs are specified then there'll be two pipelines producing
* events from "error" logs.
*
* If we are merging all known log types then we will construct exactly
* one pipeline for each known log type - one for error, one for fault, etc.
* Each pipeline will process first the rotated logs of that type and then
* move on to the current log of that type.
*
* The output from all pipelines flows into a serializer which selects
* the next record once all pipelines have asserted their output state.
* The output state of a pipeline is one of:
*
* - record available: the next record from this pipeline is available
* for comparison and consumption
*
* - done: this pipeline will produce no more records
*
* - polling: this pipeline is polling for new records and will
* make them available as output if/when any are observed
*
* - processing: output state will be updated shortly
*
* A pipeline iterates over each file queued to it using fmd_log_xiter.
* We do this in a separate thread for each pipeline. The callback on
* each iteration must update the serializer to let it know that
* a new record is available. In the serializer thread we decide whether
* we have all records expected have arrived and it is time to choose
* the next output record.
*/
/*
* A pipeline descriptor. The pl_cv condition variable is used together
* with pl_lock for initial synchronisation, and thereafter with the
* lock for the serializer for pausing and continuing this pipeline.
*/
struct fmdump_pipeline {
pthread_mutex_t pl_lock; /* used only in pipeline startup */
int pl_started; /* sync with main thread on startup */
pthread_t pl_thr; /* our processing thread */
pthread_cond_t pl_cv; /* see above */
struct loglink *pl_rotated; /* rotated logs to process first */
char *pl_logpath; /* target path to process */
char *pl_processing; /* path currently being processed */
struct fmdump_srlzer *pl_srlzer; /* link to serializer */
int pl_srlzeridx; /* serializer index for this pipeline */
const fmdump_ops_t *pl_ops; /* ops for the log type we're given */
int pl_fmt; /* FMDUMP_{SHORT,VERB1,VERB2,PRETTY} */
boolean_t pl_follow; /* go into poll mode at log end */
fmdump_arg_t pl_arg; /* arguments */
};
enum fmdump_pipestate {
FMDUMP_PIPE_PROCESSING = 0x1000,
FMDUMP_PIPE_RECORDAVAIL,
FMDUMP_PIPE_POLLING,
FMDUMP_PIPE_DONE
};
/*
* Each pipeline has an associated output slot in the serializer. This
* must be updated with the serializer locked. After update evaluate
* whether there are enough slots decided that we should select a
* record to output.
*/
struct fmdump_srlzer_slot {
enum fmdump_pipestate ss_state;
uint64_t ss_sec;
uint64_t ss_nsec;
};
/*
* All pipelines are linked to a single serializer. The serializer
* structure must be updated under the ds_lock; this mutex is also
* paired with the pl_cv of individual pipelines (one mutex, many condvars)
* in pausing and continuing individual pipelines.
*/
struct fmdump_srlzer {
struct fmdump_pipeline *ds_pipearr; /* pipeline array */
pthread_mutex_t ds_lock; /* see above */
uint32_t ds_pipecnt; /* number of pipelines */
uint32_t ds_pollcnt; /* pipelines in poll mode */
uint32_t ds_nrecordavail; /* pipelines with a record */
uint32_t ds_ndone; /* completed pipelines */
struct fmdump_srlzer_slot *ds_slot; /* slot array */
};
/*
* All known log types. When aggregation is requested an no file list
* is provided we will process the logs identified here (if lt_enabled
* is true and not over-ridden by environment settings). We also
* use this in determining the appropriate ops structure for each distinct
* label.
*/
static struct fmdump_logtype {
const char *lt_label; /* label from log header */
boolean_t lt_enabled; /* include in merge? */
const char *lt_logname; /* var/fm/fmd/%s */
const fmdump_ops_t *lt_ops;
} logtypes[] = {
{
"error",
B_TRUE,
"errlog",
&fmdump_err_ops
},
{
"fault",
B_TRUE,
"fltlog",
&fmdump_flt_ops
},
{
"info",
B_TRUE,
"infolog",
&fmdump_info_ops
},
{
"info",
B_TRUE,
"infolog_hival",
&fmdump_info_ops
},
{
"asru",
B_FALSE, /* not included unless in file list */
NULL,
&fmdump_asru_ops /* but we need ops when it is */
}
};
/*
* Disable logtypes per environment setting. Does not apply when a list
* of logs is provided on the command line.
*/
static void
do_disables(void)
{
char *env = getenv("FMDUMP_AGGREGATE_IGNORE");
char *dup, *start, *tofree;
int i;
if (env == NULL)
return;
tofree = dup = strdup(env);
while (dup != NULL) {
start = strsep(&dup, ",");
for (i = 0; i < sizeof (logtypes) / sizeof (logtypes[0]); i++) {
if (logtypes[i].lt_logname == NULL)
continue;
if (strcmp(start, logtypes[i].lt_label) == 0 ||
strcmp(start, logtypes[i].lt_logname) == 0) {
logtypes[i].lt_enabled = B_FALSE;
}
}
}
free(tofree);
}
static void
srlzer_enter(struct fmdump_pipeline *pl)
{
struct fmdump_srlzer *srlzer = pl->pl_srlzer;
(void) pthread_mutex_lock(&srlzer->ds_lock);
}
static void
srlzer_exit(struct fmdump_pipeline *pl)
{
struct fmdump_srlzer *srlzer = pl->pl_srlzer;
ASSERT(MUTEX_HELD(&srlzer->ds_lock));
(void) pthread_mutex_unlock(&srlzer->ds_lock);
}
static struct fmdump_pipeline *
srlzer_choose(struct fmdump_srlzer *srlzer)
{
struct fmdump_srlzer_slot *slot, *oldest;
int oldestidx = -1;
int first = 1;
int i;
ASSERT(MUTEX_HELD(&srlzer->ds_lock));
for (i = 0, slot = &srlzer->ds_slot[0]; i < srlzer->ds_pipecnt;
i++, slot++) {
if (slot->ss_state != FMDUMP_PIPE_RECORDAVAIL)
continue;
if (first) {
oldest = slot;
oldestidx = i;
first = 0;
continue;
}
if (slot->ss_sec < oldest->ss_sec ||
slot->ss_sec == oldest->ss_sec &&
slot->ss_nsec < oldest->ss_nsec) {
oldest = slot;
oldestidx = i;
}
}
return (oldestidx >= 0 ? &srlzer->ds_pipearr[oldestidx] : NULL);
}
static void
pipeline_stall(struct fmdump_pipeline *pl)
{
struct fmdump_srlzer *srlzer = pl->pl_srlzer;
ASSERT(MUTEX_HELD(&srlzer->ds_lock));
(void) pthread_cond_wait(&pl->pl_cv, &srlzer->ds_lock);
}
static void
pipeline_continue(struct fmdump_pipeline *pl)
{
struct fmdump_srlzer *srlzer = pl->pl_srlzer;
ASSERT(MUTEX_HELD(&srlzer->ds_lock));
(void) pthread_cond_signal(&srlzer->ds_pipearr[pl->pl_srlzeridx].pl_cv);
}
/*
* Called on each pipeline record iteration to make a new record
* available for input to the serializer. Returns 0 to indicate that
* the caller must stall the pipeline, or 1 to indicate that the
* caller should go ahead and render their record. If this record
* addition fills the serializer then choose a pipeline that must
* render output.
*/
static int
pipeline_output(struct fmdump_pipeline *pl, const fmd_log_record_t *rp)
{
struct fmdump_srlzer *srlzer = pl->pl_srlzer;
struct fmdump_srlzer_slot *slot;
struct fmdump_pipeline *wpl;
int thisidx = pl->pl_srlzeridx;
ASSERT(MUTEX_HELD(&srlzer->ds_lock));
slot = &srlzer->ds_slot[thisidx];
slot->ss_state = FMDUMP_PIPE_RECORDAVAIL;
slot->ss_sec = rp->rec_sec;
slot->ss_nsec = rp->rec_nsec;
srlzer->ds_nrecordavail++;
/*
* Once all pipelines are polling we just render in arrival order.
*/
if (srlzer->ds_pollcnt == srlzer->ds_pipecnt)
return (1);
/*
* If not all pipelines have asserted an output yet then the
* caller must block.
*/
if (srlzer->ds_nrecordavail + srlzer->ds_ndone +
srlzer->ds_pollcnt < srlzer->ds_pipecnt)
return (0);
/*
* Right so it's time to turn the crank by choosing which of the
* filled line of slots should produce output. If it is the slot
* for our caller then return their index to them, otherwise return
* -1 to the caller to make them block and cv_signal the winner.
*/
wpl = srlzer_choose(srlzer);
ASSERT(wpl != NULL);
if (wpl == pl)
return (1);
/* Wake the oldest, and return 0 to put the caller to sleep */
pipeline_continue(wpl);
return (0);
}
static void
pipeline_mark_consumed(struct fmdump_pipeline *pl)
{
struct fmdump_srlzer *srlzer = pl->pl_srlzer;
ASSERT(MUTEX_HELD(&srlzer->ds_lock));
srlzer->ds_slot[pl->pl_srlzeridx].ss_state = FMDUMP_PIPE_PROCESSING;
srlzer->ds_nrecordavail--;
}
static void
pipeline_done(struct fmdump_pipeline *pl)
{
struct fmdump_srlzer *srlzer = pl->pl_srlzer;
struct fmdump_pipeline *wpl;
srlzer_enter(pl);
srlzer->ds_slot[pl->pl_srlzeridx].ss_state = FMDUMP_PIPE_DONE;
srlzer->ds_ndone++;
wpl = srlzer_choose(srlzer);
if (wpl != NULL)
pipeline_continue(wpl);
srlzer_exit(pl);
}
static void
pipeline_pollmode(struct fmdump_pipeline *pl)
{
struct fmdump_srlzer *srlzer = pl->pl_srlzer;
struct fmdump_pipeline *wpl;
if (srlzer->ds_slot[pl->pl_srlzeridx].ss_state == FMDUMP_PIPE_POLLING)
return;
srlzer_enter(pl);
srlzer->ds_slot[pl->pl_srlzeridx].ss_state = FMDUMP_PIPE_POLLING;
if (++srlzer->ds_pollcnt + srlzer->ds_nrecordavail ==
srlzer->ds_pipecnt && (wpl = srlzer_choose(srlzer)) != NULL)
pipeline_continue(wpl);
srlzer_exit(pl);
}
static int
pipeline_err(fmd_log_t *lp, void *arg)
{
struct fmdump_pipeline *pl = (struct fmdump_pipeline *)arg;
fmdump_warn("skipping record in %s: %s\n", pl->pl_processing,
fmd_log_errmsg(lp, fmd_log_errno(lp)));
g_errs++;
return (0);
}
static int
pipeline_cb(fmd_log_t *lp, const fmd_log_record_t *rp, void *arg)
{
struct fmdump_pipeline *pl = (struct fmdump_pipeline *)arg;
int rc;
fmd_log_rec_f *func = pl->pl_arg.da_fmt->do_func;
srlzer_enter(pl);
if (!pipeline_output(pl, rp))
pipeline_stall(pl);
rc = func(lp, rp, pl->pl_arg.da_fp);
pipeline_mark_consumed(pl);
srlzer_exit(pl);
return (rc);
}
static void
pipeline_process(struct fmdump_pipeline *pl, char *logpath, boolean_t follow)
{
fmd_log_header_t log;
fmd_log_t *lp;
int err;
int i;
pl->pl_processing = logpath;
if ((lp = fmd_log_open(FMD_LOG_VERSION, logpath, &err)) == NULL) {
fmdump_warn("failed to open %s: %s\n",
logpath, fmd_log_errmsg(NULL, err));
g_errs++;
return;
}
fmd_log_header(lp, &log);
for (i = 0; i < sizeof (logtypes) / sizeof (logtypes[0]); i++) {
if (strcmp(log.log_label, logtypes[i].lt_label) == 0) {
pl->pl_ops = logtypes[i].lt_ops;
pl->pl_arg.da_fmt =
&pl->pl_ops->do_formats[pl->pl_fmt];
break;
}
}
if (pl->pl_ops == NULL) {
fmdump_warn("unknown log type %s for %s\n",
log.log_label, logpath);
g_errs++;
return;
}
do {
if (fmd_log_xiter(lp, FMD_LOG_XITER_REFS, pl->pl_arg.da_fc,
pl->pl_arg.da_fv, pipeline_cb, pipeline_err, (void *)pl,
NULL) != 0) {
fmdump_warn("failed to dump %s: %s\n",
logpath, fmd_log_errmsg(lp, fmd_log_errno(lp)));
g_errs++;
fmd_log_close(lp);
return;
}
if (follow) {
pipeline_pollmode(pl);
(void) sleep(1);
}
} while (follow);
fmd_log_close(lp);
}
static void *
pipeline_thr(void *arg)
{
struct fmdump_pipeline *pl = (struct fmdump_pipeline *)arg;
struct loglink *ll;
(void) pthread_mutex_lock(&pl->pl_lock);
pl->pl_started = 1;
(void) pthread_mutex_unlock(&pl->pl_lock);
(void) pthread_cond_signal(&pl->pl_cv);
for (ll = pl->pl_rotated; ll != NULL; ll = ll->next)
pipeline_process(pl, ll->path, B_FALSE);
pipeline_process(pl, pl->pl_logpath, pl->pl_follow);
pipeline_done(pl);
return (NULL);
}
static int
aggregate(char **ifiles, int n_ifiles, int opt_f,
fmd_log_filter_t *fv, uint_t fc,
int opt_v, int opt_V, int opt_p, int opt_j)
{
struct fmdump_pipeline *pipeline, *pl;
struct fmdump_srlzer srlzer;
uint32_t npipe;
int fmt;
int i;
if (ifiles != NULL) {
npipe = n_ifiles;
pipeline = calloc(npipe, sizeof (struct fmdump_pipeline));
if (!pipeline)
fmdump_fatal("failed to allocate memory");
for (i = 0; i < n_ifiles; i++)
pipeline[i].pl_logpath = ifiles[i];
} else {
pipeline = calloc(sizeof (logtypes) / sizeof (logtypes[0]),
sizeof (struct fmdump_pipeline));
if (!pipeline)
fmdump_fatal("failed to allocate memory");
do_disables();
npipe = 0;
for (i = 0; i < sizeof (logtypes) / sizeof (logtypes[0]); i++) {
struct fmdump_logtype *ltp = &logtypes[i];
char *logpath;
if (ltp->lt_enabled == B_FALSE)
continue;
if ((logpath = malloc(PATH_MAX)) == NULL)
fmdump_fatal("failed to allocate memory");
(void) snprintf(logpath, PATH_MAX,
"%s/var/fm/fmd/%s",
g_root ? g_root : "", ltp->lt_logname);
pipeline[npipe].pl_rotated =
get_rotated_logs(logpath);
pipeline[npipe++].pl_logpath = logpath;
}
}
if (opt_V)
fmt = opt_p ? FMDUMP_PRETTY : opt_j ? FMDUMP_JSON :
FMDUMP_VERB2;
else if (opt_v)
fmt = FMDUMP_VERB1;
else
fmt = FMDUMP_SHORT;
bzero(&srlzer, sizeof (srlzer));
srlzer.ds_pipearr = pipeline;
srlzer.ds_pipecnt = npipe;
srlzer.ds_slot = calloc(npipe, sizeof (struct fmdump_srlzer_slot));
if (!srlzer.ds_slot)
fmdump_fatal("failed to allocate memory");
(void) pthread_mutex_init(&srlzer.ds_lock, NULL);
for (i = 0, pl = &pipeline[0]; i < npipe; i++, pl++) {
(void) pthread_mutex_init(&pl->pl_lock, NULL);
(void) pthread_cond_init(&pl->pl_cv, NULL);
srlzer.ds_slot[i].ss_state = FMDUMP_PIPE_PROCESSING;
pl->pl_srlzer = &srlzer;
pl->pl_srlzeridx = i;
pl->pl_follow = opt_f ? B_TRUE : B_FALSE;
pl->pl_fmt = fmt;
pl->pl_arg.da_fv = fv;
pl->pl_arg.da_fc = fc;
pl->pl_arg.da_fp = stdout;
(void) pthread_mutex_lock(&pl->pl_lock);
if (pthread_create(&pl->pl_thr, NULL,
pipeline_thr, (void *)pl) != 0)
fmdump_fatal("pthread_create for pipeline %d failed",
i);
}
for (i = 0, pl = &pipeline[0]; i < npipe; i++, pl++) {
while (!pl->pl_started)
(void) pthread_cond_wait(&pl->pl_cv, &pl->pl_lock);
(void) pthread_mutex_unlock(&pl->pl_lock);
}
for (i = 0, pl = &pipeline[0]; i < npipe; i++, pl++)
(void) pthread_join(pl->pl_thr, NULL);
if (ifiles == NULL) {
for (i = 0; i < npipe; i++)
free(pipeline[i].pl_logpath);
}
free(srlzer.ds_slot);
free(pipeline);
return (FMDUMP_EXIT_SUCCESS);
}
static void
cleanup(char **ifiles, int n_ifiles)
{
int i;
if (ifiles == NULL)
return;
for (i = 0; i < n_ifiles; i++) {
if (ifiles[i] != NULL) {
free(ifiles[i]);
ifiles[i] = NULL;
}
}
free(ifiles);
}
int
main(int argc, char *argv[])
{
int opt_a = 0, opt_e = 0, opt_f = 0, opt_H = 0, opt_m = 0, opt_p = 0;
int opt_u = 0, opt_v = 0, opt_V = 0, opt_j = 0;
int opt_i = 0, opt_I = 0;
int opt_A = 0;
char **ifiles = NULL;
char *ifile = NULL;
int n_ifiles;
int ifileidx = 0;
int iflags = 0;
fmdump_arg_t arg;
fmdump_lyr_t lyr;
const fmdump_ops_t *ops;
fmd_log_filter_t *filtv;
uint_t filtc;
fmd_log_filter_t *errfv, *fltfv, *allfv;
uint_t errfc = 0, fltfc = 0, allfc = 0;
fmd_log_header_t log;
fmd_log_rec_f *func;
void *farg;
fmd_log_t *lp;
int c, err;
off64_t off = 0;
ulong_t recs;
struct loglink *rotated_logs = NULL, *llp;
g_pname = argv[0];
errfv = alloca(sizeof (fmd_log_filter_t) * argc);
fltfv = alloca(sizeof (fmd_log_filter_t) * argc);
allfv = alloca(sizeof (fmd_log_filter_t) * argc);
while (optind < argc) {
while ((c = getopt(argc, argv,
"Aac:efHiIjmN:n:O:pR:t:T:u:vV")) != EOF) {
switch (c) {
case 'A':
opt_A++;
break;
case 'a':
opt_a++;
break;
case 'c':
errfv[errfc].filt_func = fmd_log_filter_class;
errfv[errfc].filt_arg = optarg;
allfv[allfc++] = errfv[errfc++];
break;
case 'e':
if (opt_i)
return (usage(stderr));
opt_e++;
break;
case 'f':
opt_f++;
break;
case 'H':
opt_H++;
break;
case 'i':
if (opt_e || opt_I)
return (usage(stderr));
opt_i++;
break;
case 'I':
if (opt_e || opt_i)
return (usage(stderr));
opt_I++;
break;
case 'j':
if (opt_p)
return (usage(stderr));
opt_j++;
break;
case 'm':
opt_m++;
break;
case 'N':
fltfv[fltfc].filt_func =
fmd_log_filter_nv_multi;
fltfv[fltfc].filt_arg =
setupnamevalue_multi(optarg);
allfv[allfc++] = fltfv[fltfc++];
break;
case 'n':
fltfv[fltfc].filt_func = fmd_log_filter_nv;
fltfv[fltfc].filt_arg = setupnamevalue(optarg);
allfv[allfc++] = fltfv[fltfc++];
break;
case 'O': {
char *p;
errno = 0;
off = strtoull(optarg, &p, 16);
if (errno != 0 || p == optarg || *p != '\0') {
fmdump_usage(
"illegal offset format -- %s\n",
optarg);
}
iflags |= FMD_LOG_XITER_OFFS;
break;
}
case 'p':
if (opt_j)
return (usage(stderr));
opt_p++;
break;
case 'R':
g_root = optarg;
break;
case 't':
errfv[errfc].filt_func = fmd_log_filter_after;
errfv[errfc].filt_arg = gettimeopt(optarg);
allfv[allfc++] = errfv[errfc++];
break;
case 'T':
errfv[errfc].filt_func = fmd_log_filter_before;
errfv[errfc].filt_arg = gettimeopt(optarg);
allfv[allfc++] = errfv[errfc++];
break;
case 'u':
fltfv[fltfc].filt_func = fmd_log_filter_uuid;
fltfv[fltfc].filt_arg = optarg;
allfv[allfc++] = fltfv[fltfc++];
opt_u++;
opt_a++; /* -u implies -a */
break;
case 'v':
opt_v++;
break;
case 'V':
opt_V++;
break;
default:
return (usage(stderr));
}
}
if (opt_A && (opt_e || opt_i || opt_I || opt_m || opt_u))
fmdump_usage("-A excludes all of "
"-e, -i, -I, -m and -u\n");
if (optind < argc) {
char *dest;
if (ifiles == NULL) {
n_ifiles = argc - optind;
ifiles = calloc(n_ifiles, sizeof (char *));
if (ifiles == NULL) {
fmdump_fatal(
"failed to allocate memory for "
"%d input file%s", n_ifiles,
n_ifiles > 1 ? "s" : "");
}
}
if (ifileidx > 0 && !opt_A)
fmdump_usage("illegal argument -- %s\n",
argv[optind]);
ASSERT(ifileidx < n_ifiles);
if ((dest = malloc(PATH_MAX)) == NULL)
fmdump_fatal("failed to allocate memory");
(void) strlcpy(dest, argv[optind++], PATH_MAX);
ifiles[ifileidx++] = dest;
}
}
/*
* It's possible that file arguments were interleaved with options and
* option arguments, in which case we allocated space for more file
* arguments that we actually got. Adjust as required so that we don't
* reference invalid entries.
*/
n_ifiles = ifileidx;
if (opt_A) {
int rc;
if (!opt_a) {
fltfv[fltfc].filt_func = log_filter_silent;
fltfv[fltfc].filt_arg = (void *)1;
allfv[allfc++] = fltfv[fltfc++];
}
rc = aggregate(ifiles, n_ifiles, opt_f,
allfv, allfc,
opt_v, opt_V, opt_p, opt_j);
cleanup(ifiles, n_ifiles);
return (rc);
} else {
if (ifiles == NULL) {
if ((ifile = calloc(1, PATH_MAX)) == NULL)
fmdump_fatal("failed to allocate memory");
} else {
ifile = ifiles[0];
}
}
if (*ifile == '\0') {
const char *pfx, *sfx;
if (opt_u || (!opt_e && !opt_i && !opt_I)) {
pfx = "flt";
sfx = "";
} else {
if (opt_e) {
pfx = "err";
sfx = "";
} else {
pfx = "info";
sfx = opt_I ? "_hival" : "";
}
}
(void) snprintf(ifile, PATH_MAX, "%s/var/fm/fmd/%slog%s",
g_root ? g_root : "", pfx, sfx);
/*
* logadm may rotate the logs. When no input file is specified,
* we try to dump all the rotated logs as well in the right
* order.
*/
if (!opt_H && off == 0)
rotated_logs = get_rotated_logs(ifile);
} else if (g_root != NULL) {
fmdump_usage("-R option is not appropriate "
"when file operand is present\n");
}
if ((g_msg = fmd_msg_init(g_root, FMD_MSG_VERSION)) == NULL)
fmdump_fatal("failed to initialize libfmd_msg");
if ((lp = fmd_log_open(FMD_LOG_VERSION, ifile, &err)) == NULL) {
fmdump_fatal("failed to open %s: %s\n", ifile,
fmd_log_errmsg(NULL, err));
}
if (opt_H) {
fmd_log_header(lp, &log);
(void) printf("EXD_CREATOR = %s\n", log.log_creator);
(void) printf("EXD_HOSTNAME = %s\n", log.log_hostname);
(void) printf("EXD_FMA_LABEL = %s\n", log.log_label);
(void) printf("EXD_FMA_VERSION = %s\n", log.log_version);
(void) printf("EXD_FMA_OSREL = %s\n", log.log_osrelease);
(void) printf("EXD_FMA_OSVER = %s\n", log.log_osversion);
(void) printf("EXD_FMA_PLAT = %s\n", log.log_platform);
(void) printf("EXD_FMA_UUID = %s\n", log.log_uuid);
return (FMDUMP_EXIT_SUCCESS);
}
if (off != 0 && fmd_log_seek(lp, off) != 0) {
fmdump_fatal("failed to seek %s: %s\n", ifile,
fmd_log_errmsg(lp, fmd_log_errno(lp)));
}
if (opt_e && opt_u)
ops = &fmdump_err_ops;
else if (strcmp(fmd_log_label(lp), fmdump_flt_ops.do_label) == 0)
ops = &fmdump_flt_ops;
else if (strcmp(fmd_log_label(lp), fmdump_asru_ops.do_label) == 0)
ops = &fmdump_asru_ops;
else if (strcmp(fmd_log_label(lp), fmdump_info_ops.do_label) == 0)
ops = &fmdump_info_ops;
else
ops = &fmdump_err_ops;
if (!opt_a && ops == &fmdump_flt_ops) {
fltfv[fltfc].filt_func = log_filter_silent;
fltfv[fltfc].filt_arg = NULL;
allfv[allfc++] = fltfv[fltfc++];
}
if (opt_V) {
arg.da_fmt =
&ops->do_formats[opt_p ? FMDUMP_PRETTY :
opt_j ? FMDUMP_JSON : FMDUMP_VERB2];
iflags |= FMD_LOG_XITER_REFS;
} else if (opt_v) {
arg.da_fmt = &ops->do_formats[FMDUMP_VERB1];
} else if (opt_m) {
arg.da_fmt = &ops->do_formats[FMDUMP_MSG];
} else
arg.da_fmt = &ops->do_formats[FMDUMP_SHORT];
if (opt_m && arg.da_fmt->do_func == NULL) {
fmdump_usage("-m mode is not supported for "
"log of type %s: %s\n", fmd_log_label(lp), ifile);
}
arg.da_fv = errfv;
arg.da_fc = errfc;
arg.da_fp = stdout;
if (iflags & FMD_LOG_XITER_OFFS)
fmdump_printf(arg.da_fp, "%16s ", "OFFSET");
if (arg.da_fmt->do_hdr && !(opt_V && ops == &fmdump_flt_ops))
fmdump_printf(arg.da_fp, "%s\n", arg.da_fmt->do_hdr);
if (opt_e && opt_u) {
iflags |= FMD_LOG_XITER_REFS;
func = xref_iter;
farg = &arg;
filtc = fltfc;
filtv = fltfv;
} else {
func = arg.da_fmt->do_func;
farg = arg.da_fp;
filtc = allfc;
filtv = allfv;
}
if (iflags & FMD_LOG_XITER_OFFS) {
lyr.dy_func = func;
lyr.dy_arg = farg;
lyr.dy_fp = arg.da_fp;
func = xoff_iter;
farg = &lyr;
}
for (llp = rotated_logs; llp != NULL; llp = llp->next) {
fmd_log_t *rlp;
if ((rlp = fmd_log_open(FMD_LOG_VERSION, llp->path, &err))
== NULL) {
fmdump_warn("failed to open %s: %s\n",
llp->path, fmd_log_errmsg(NULL, err));
g_errs++;
continue;
}
recs = 0;
if (fmd_log_xiter(rlp, iflags, filtc, filtv,
func, error, farg, &recs) != 0) {
fmdump_warn("failed to dump %s: %s\n", llp->path,
fmd_log_errmsg(rlp, fmd_log_errno(rlp)));
g_errs++;
}
g_recs += recs;
fmd_log_close(rlp);
}
do {
recs = 0;
if (fmd_log_xiter(lp, iflags, filtc, filtv,
func, error, farg, &recs) != 0) {
fmdump_warn("failed to dump %s: %s\n", ifile,
fmd_log_errmsg(lp, fmd_log_errno(lp)));
g_errs++;
}
g_recs += recs;
if (opt_f)
(void) sleep(1);
} while (opt_f);
if (!opt_f && g_recs == 0 && isatty(STDOUT_FILENO))
fmdump_warn("%s is empty\n", ifile);
if (g_thp != NULL)
topo_close(g_thp);
fmd_log_close(lp);
fmd_msg_fini(g_msg);
if (ifiles == NULL)
free(ifile);
else
cleanup(ifiles, n_ifiles);
return (g_errs ? FMDUMP_EXIT_ERROR : FMDUMP_EXIT_SUCCESS);
}
/*
* 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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, Joyent, Inc. All rights reserved.
*/
#ifndef _FMDUMP_H
#define _FMDUMP_H
#ifdef __cplusplus
extern "C" {
#endif
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <synch.h>
#include <sys/types.h>
#include <sys/fm/protocol.h>
#include <fm/fmd_log.h>
#include <fm/fmd_msg.h>
#include <fm/libtopo.h>
#ifdef DEBUG
#define ASSERT(x) (assert(x))
#else
#define ASSERT(x)
#endif
enum {
FMDUMP_SHORT,
FMDUMP_VERB1,
FMDUMP_VERB2,
FMDUMP_PRETTY,
FMDUMP_MSG,
FMDUMP_JSON,
FMDUMP_NFMTS
};
typedef struct fmdump_ops {
const char *do_label;
struct fmdump_fmt {
const char *do_hdr;
fmd_log_rec_f *do_func;
} do_formats[FMDUMP_NFMTS];
} fmdump_ops_t;
typedef struct fmdump_arg {
const struct fmdump_fmt *da_fmt;
fmd_log_filter_t *da_fv;
uint_t da_fc;
FILE *da_fp;
} fmdump_arg_t;
typedef struct fmdump_lyr {
fmd_log_rec_f *dy_func;
void *dy_arg;
FILE *dy_fp;
} fmdump_lyr_t;
extern const fmdump_ops_t fmdump_err_ops;
extern const fmdump_ops_t fmdump_flt_ops;
extern const fmdump_ops_t fmdump_asru_ops;
extern const fmdump_ops_t fmdump_info_ops;
extern const char *g_pname;
extern ulong_t g_errs;
extern ulong_t g_recs;
extern char *g_root;
extern struct topo_hdl *g_thp;
extern fmd_msg_hdl_t *g_msg;
extern void fmdump_printf(FILE *, const char *, ...);
extern void fmdump_warn(const char *, ...);
extern void fmdump_vwarn(const char *, va_list);
extern char *fmdump_date(char *, size_t, const fmd_log_record_t *);
extern char *fmdump_year(char *, size_t, const fmd_log_record_t *);
extern char *fmdump_nvl2str(nvlist_t *nvl);
extern int fmdump_render_nvlist(nvlist_prtctl_t, void *, nvlist_t *,
const char *, nvlist_t *);
extern int fmdump_print_json(fmd_log_t *, const fmd_log_record_t *, FILE *);
#ifdef __cplusplus
}
#endif
#endif /* _FMDUMP_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) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, Joyent, Inc. All rights reserved.
*/
#include <fmdump.h>
#include <stdio.h>
#include <time.h>
/*ARGSUSED*/
static int
info_short(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
char buf[32];
fmdump_printf(fp, "%-20s %-32s\n",
fmdump_date(buf, sizeof (buf), rp), rp->rec_class);
return (0);
}
/*ARGSUSED*/
static int
info_verb1(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
char *uuid = "(absent)";
char buf[32];
(void) nvlist_lookup_string(rp->rec_nvl, FM_IREPORT_UUID, &uuid);
fmdump_printf(fp, "%-20s %-36s %s\n",
fmdump_date(buf, sizeof (buf), rp), uuid, rp->rec_class);
return (0);
}
/*ARGSUSED*/
static int
info_verb23_cmn(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp,
nvlist_prtctl_t pctl)
{
char buf[32];
char *uuid = "(absent)";
(void) nvlist_lookup_string(rp->rec_nvl, FM_IREPORT_UUID, &uuid);
fmdump_printf(fp, "%-20s.%9.9llu %s\n",
fmdump_year(buf, sizeof (buf), rp), rp->rec_nsec, uuid);
if (pctl)
nvlist_prt(rp->rec_nvl, pctl);
else
nvlist_print(fp, rp->rec_nvl);
fmdump_printf(fp, "\n");
return (0);
}
static int
info_verb2(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
return (info_verb23_cmn(lp, rp, fp, NULL));
}
static int
info_pretty(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
nvlist_prtctl_t pctl;
int rc;
if ((pctl = nvlist_prtctl_alloc()) != NULL) {
nvlist_prtctl_setdest(pctl, fp);
nvlist_prtctlop_nvlist(pctl, fmdump_render_nvlist, NULL);
}
rc = info_verb23_cmn(lp, rp, fp, pctl);
nvlist_prtctl_free(pctl);
return (rc);
}
const fmdump_ops_t fmdump_info_ops = {
"info", {
{
"TIME CLASS",
(fmd_log_rec_f *)info_short
}, {
"TIME UUID CLASS",
(fmd_log_rec_f *)info_verb1
}, {
"TIME UUID",
(fmd_log_rec_f *)info_verb2
}, {
"TIME UUID",
(fmd_log_rec_f *)info_pretty
}, {
NULL, NULL
}, {
NULL,
(fmd_log_rec_f *)fmdump_print_json
} }
};
/*
* 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) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, Joyent, Inc. All rights reserved.
*/
/*
* Rendering functions for nvlist_prt that are of use to all types
* of log.
*/
#include <fmdump.h>
#include <stdio.h>
#include <strings.h>
extern topo_hdl_t *fmd_fmri_topo_hold(int);
/*
* Can be appointed to be called for dumping all nvlist members of
* an nvlist we ask to print with nvlist_prt. Return 0 if the
* nvlist is not recognized as an fmri, and default formatting
* will be applied; otherwise format as an fmri string and return 1.
*/
/*ARGSUSED*/
int
fmdump_render_nvlist(nvlist_prtctl_t pctl, void *private, nvlist_t *nvl,
const char *name, nvlist_t *fmri)
{
topo_hdl_t *thp = fmd_fmri_topo_hold(TOPO_VERSION);
FILE *fp = nvlist_prtctl_getdest(pctl);
char *class, *fmristr = NULL;
uint8_t version;
int err;
if (nvlist_lookup_string(fmri, FM_FMRI_SCHEME, &class) != 0 ||
nvlist_lookup_uint8(fmri, FM_VERSION, &version) != 0)
return (0);
/*
* Instead of hardcoding known FMRI classes here we'll try
* topo_fmri_nvl2str which should fail gracefully for invalid
* schemes (ie an nvlist that just happens to have the expected
* class and version members but that isn't an FMRI).
*/
if (topo_fmri_nvl2str(thp, fmri, &fmristr, &err) != 0 ||
fmristr == NULL)
return (0);
nvlist_prtctl_doindent(pctl, 1);
nvlist_prtctl_dofmt(pctl, NVLIST_FMT_MEMBER_NAME, name);
(void) fprintf(fp, "%s", fmristr);
topo_hdl_strfree(thp, fmristr);
return (1);
}
/*
* Thin wrapper around libnvpair's inbuilt JSON routine. Simply dumps the
* entire log record nvlist without any reformatting.
*/
/*ARGSUSED*/
int
fmdump_print_json(fmd_log_t *lp, const fmd_log_record_t *rp, FILE *fp)
{
if (nvlist_print_json(fp, rp->rec_nvl) != 0 || fprintf(fp, "\n") < 0 ||
fflush(fp) != 0)
return (-1);
return (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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/systeminfo.h>
#include <limits.h>
#include <strings.h>
#include <stddef.h>
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
#include <fmdump.h>
/*
* fmdump loadable scheme support
*
* This file provides a pared-down implementation of fmd's fmd_fmri.c and
* fmd_scheme.c and must be kept in sync with the set of service routines
* required by scheme plug-ins. At some point if other utilities want to
* use this we can refactor it into a more general library. (Note: fmd
* cannot use such a library because it has its own internal locking, etc.)
* As schemes are needed, we dlopen() them and cache a list of them which we
* can search later. We also use the list as a negative cache: if we fail to
* load a scheme, we add an entry with sch_dlp = NULL and sch_err recording
* the errno to be returned to the caller.
*/
typedef struct fmd_scheme_ops {
int (*sop_init)(void);
void (*sop_fini)(void);
ssize_t (*sop_nvl2str)(nvlist_t *, char *, size_t);
} fmd_scheme_ops_t;
typedef struct fmd_scheme_opd {
const char *opd_name; /* symbol name of scheme function */
size_t opd_off; /* offset within fmd_scheme_ops_t */
} fmd_scheme_opd_t;
typedef struct fmd_scheme {
struct fmd_scheme *sch_next; /* next scheme on list of schemes */
char *sch_name; /* name of this scheme (fmri prefix) */
void *sch_dlp; /* libdl shared library handle */
int sch_err; /* if negative entry, errno to return */
fmd_scheme_ops_t sch_ops; /* scheme function pointers */
} fmd_scheme_t;
static fmd_scheme_t *sch_list; /* list of cached schemes */
static long
fmd_scheme_notsup(void)
{
errno = ENOTSUP;
return (-1);
}
static void
fmd_scheme_vnop(void)
{
}
static int
fmd_scheme_nop(void)
{
return (0);
}
/*
* Default values for the scheme ops. If a scheme function is not defined in
* the module, then this operation is implemented using the default function.
*/
static const fmd_scheme_ops_t _fmd_scheme_default_ops = {
(int (*)())fmd_scheme_nop, /* sop_init */
(void (*)())fmd_scheme_vnop, /* sop_fini */
(ssize_t (*)())fmd_scheme_notsup, /* sop_nvl2str */
};
/*
* Scheme ops descriptions. These names and offsets are used by the function
* fmd_scheme_rtld_init(), defined below, to load up a fmd_scheme_ops_t.
*/
static const fmd_scheme_opd_t _fmd_scheme_ops[] = {
{ "fmd_fmri_init", offsetof(fmd_scheme_ops_t, sop_init) },
{ "fmd_fmri_fini", offsetof(fmd_scheme_ops_t, sop_fini) },
{ "fmd_fmri_nvl2str", offsetof(fmd_scheme_ops_t, sop_nvl2str) },
{ NULL, 0 }
};
static fmd_scheme_t *
fmd_scheme_create(const char *name)
{
fmd_scheme_t *sp;
if ((sp = malloc(sizeof (fmd_scheme_t))) == NULL ||
(sp->sch_name = strdup(name)) == NULL) {
free(sp);
return (NULL);
}
sp->sch_next = sch_list;
sp->sch_dlp = NULL;
sp->sch_err = 0;
sp->sch_ops = _fmd_scheme_default_ops;
sch_list = sp;
return (sp);
}
static int
fmd_scheme_rtld_init(fmd_scheme_t *sp)
{
const fmd_scheme_opd_t *opd;
void *p;
for (opd = _fmd_scheme_ops; opd->opd_name != NULL; opd++) {
if ((p = dlsym(sp->sch_dlp, opd->opd_name)) != NULL)
*(void **)((uintptr_t)&sp->sch_ops + opd->opd_off) = p;
}
return (sp->sch_ops.sop_init());
}
static fmd_scheme_t *
fmd_scheme_lookup(const char *dir, const char *name)
{
fmd_scheme_t *sp;
char path[PATH_MAX];
for (sp = sch_list; sp != NULL; sp = sp->sch_next) {
if (strcmp(name, sp->sch_name) == 0)
return (sp);
}
if ((sp = fmd_scheme_create(name)) == NULL)
return (NULL); /* errno is set for us */
(void) snprintf(path, sizeof (path), "%s%s/%s.so",
g_root ? g_root : "", dir, name);
if (access(path, F_OK) != 0) {
sp->sch_err = errno;
return (sp);
}
if ((sp->sch_dlp = dlopen(path, RTLD_LOCAL | RTLD_NOW)) == NULL) {
sp->sch_err = ELIBACC;
return (sp);
}
if (fmd_scheme_rtld_init(sp) != 0) {
sp->sch_err = errno;
(void) dlclose(sp->sch_dlp);
sp->sch_dlp = NULL;
}
return (sp);
}
char *
fmdump_nvl2str(nvlist_t *nvl)
{
fmd_scheme_t *sp;
char c, *name, *s = NULL;
ssize_t len;
if (nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &name) != 0) {
fmdump_warn("fmri does not contain required '%s' nvpair\n",
FM_FMRI_SCHEME);
return (NULL);
}
if ((sp = fmd_scheme_lookup("/usr/lib/fm/fmd/schemes", name)) == NULL ||
sp->sch_dlp == NULL || sp->sch_err != 0) {
const char *msg =
sp->sch_err == ELIBACC ? dlerror() : strerror(sp->sch_err);
fmdump_warn("cannot init '%s' scheme library to "
"format fmri: %s\n", name, msg ? msg : "unknown error");
return (NULL);
}
if ((len = sp->sch_ops.sop_nvl2str(nvl, &c, sizeof (c))) == -1 ||
(s = malloc(len + 1)) == NULL ||
sp->sch_ops.sop_nvl2str(nvl, s, len + 1) == -1) {
fmdump_warn("cannot format fmri using scheme '%s'", name);
free(s);
return (NULL);
}
return (s);
}
void *
fmd_fmri_alloc(size_t size)
{
return (malloc(size));
}
void *
fmd_fmri_zalloc(size_t size)
{
void *data;
if ((data = malloc(size)) != NULL)
bzero(data, size);
return (data);
}
/*ARGSUSED*/
void
fmd_fmri_free(void *data, size_t size)
{
free(data);
}
int
fmd_fmri_error(int err)
{
errno = err;
return (-1);
}
char *
fmd_fmri_strescape(const char *s)
{
return (strdup(s));
}
char *
fmd_fmri_strdup(const char *s)
{
return (strdup(s));
}
void
fmd_fmri_strfree(char *s)
{
free(s);
}
const char *
fmd_fmri_get_rootdir(void)
{
return (g_root ? g_root : "");
}
const char *
fmd_fmri_get_platform(void)
{
static char platform[MAXNAMELEN];
if (platform[0] == '\0')
(void) sysinfo(SI_PLATFORM, platform, sizeof (platform));
return (platform);
}
uint64_t
fmd_fmri_get_drgen(void)
{
return (0);
}
int
fmd_fmri_set_errno(int err)
{
errno = err;
return (-1);
}
void
fmd_fmri_warn(const char *format, ...)
{
va_list ap;
va_start(ap, format);
fmdump_vwarn(format, ap);
va_end(ap);
}
struct topo_hdl *
fmd_fmri_topo_hold(int version)
{
int err;
if (version != TOPO_VERSION)
return (NULL);
if (g_thp == NULL) {
if ((g_thp = topo_open(TOPO_VERSION, "/", &err)) == NULL) {
(void) fprintf(stderr, "topo_open failed: %s\n",
topo_strerror(err));
exit(1);
}
}
return (g_thp);
}
/*ARGSUSED*/
void
fmd_fmri_topo_rele(struct topo_hdl *thp)
{
/* nothing to do */
}
|