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
|
#
# 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.
#
.KEEP_STATE:
PROG= prctl
OBJS= prctl.o utils.o
include ../Makefile.cmd
include ../Makefile.cmd.64
CFLAGS += $(CCVERBOSE)
CERRWARN += -Wno-parentheses
CERRWARN += $(CNOWARN_UNINIT)
# not linted
SMATCH=off
LDLIBS += -lproc -lproject
# Hammerhead: ROOTBINLINK removed — ROOTBIN64=ROOTBIN (path flattening),
# so ../../bin symlink from /usr/bin would be circular.
.KEEP_STATE:
all: $(PROG)
install: all $(ROOTPROG)
$(PROG): $(OBJS)
$(LINK.c) $(OBJS) -o $@ $(LDLIBS)
$(POST_PROCESS)
clean:
$(RM) $(OBJS)
include ../Makefile.targ
/*
* 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) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2015, Joyent, Inc.
*/
#include <unistd.h>
#include <rctl.h>
#include <libproc.h>
#include <stdio.h>
#include <libintl.h>
#include <locale.h>
#include <string.h>
#include <signal.h>
#include <strings.h>
#include <ctype.h>
#include <project.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/varargs.h>
#include <priv.h>
#include <zone.h>
#include "utils.h"
/* Valid user actions */
#define ACTION_DISABLE 0x01
#define ACTION_ENABLE 0x02
#define ACTION_SET 0x04
#define ACTION_REPLACE 0x08
#define ACTION_DELETE 0x10
#define PRCTL_VALUE_WIDTH 4
/* Maximum string length for deferred errors */
#define GLOBAL_ERR_SZ 1024
/* allow important process values to be passed together easily */
typedef struct pr_info_handle {
struct ps_prochandle *pr;
pid_t pid;
psinfo_t psinfo;
taskid_t taskid;
projid_t projid;
char *projname;
zoneid_t zoneid;
char *zonename;
} pr_info_handle_t;
/* Structures for list of resource controls */
typedef struct prctl_value {
rctlblk_t *rblk;
struct prctl_value *next;
} prctl_value_t;
typedef struct prctl_list {
char *name;
rctl_qty_t *usage;
prctl_value_t *val_list;
struct prctl_list *next;
} prctl_list_t;
static volatile int interrupt;
static prctl_list_t *global_rctl_list_head = NULL;
static prctl_list_t *global_rctl_list_tail = NULL;
static char global_error[GLOBAL_ERR_SZ];
/* global variables that contain commmand line option info */
static int arg_operation = 0;
static int arg_force = 0;
/* String and type from -i */
static rctl_entity_t arg_entity_type = RCENTITY_PROCESS;
static char *arg_entity_string = NULL;
/* -n argument */
static char *arg_name = NULL;
static rctl_entity_t arg_name_entity = 0;
/* -t argument value */
static int arg_priv = 0;
/* -v argument string */
static char *arg_valuestring = NULL;
/* global flags of rctl name passed to -n */
static int arg_global_flags = 0;
static rctl_qty_t arg_global_max;
/* appropriate scaling variables determined by rctl unit type */
scale_t *arg_scale;
static char *arg_unit = NULL;
/* -v argument string converted to uint64_t */
static uint64_t arg_value = 0;
/* if -v argument is scaled value, points to "K", "M", "G", ... */
static char *arg_modifier = NULL;
/* -e/-d argument string */
static char *arg_action_string = NULL;
/* Set to RCTL_LOCAL_SIGNAL|DENY based on arg_action_string */
static int arg_action = 0;
/* if -e/-d arg is signal=XXX, set to signal number of XXX */
static int arg_signal = 0;
/* -p arg if -p is specified */
static int arg_pid = -1;
static char *arg_pid_string = NULL;
/* Set to 1 if -P is specified */
static int arg_parseable_mode = 0;
/* interupt handler */
static void intr(int);
static int get_rctls(struct ps_prochandle *);
static int store_rctls(const char *rctlname, void *walk_data);
static prctl_value_t *store_value_entry(rctlblk_t *rblk, prctl_list_t *list);
static prctl_list_t *store_list_entry(const char *name);
static void free_lists();
static int change_action(rctlblk_t *blk);
static int prctl_setrctl(struct ps_prochandle *Pr, const char *name,
rctlblk_t *, rctlblk_t *, uint_t);
static int match_rctl(struct ps_prochandle *Pr, rctlblk_t **rctl, char *name,
char *valuestringin, int valuein, rctl_priv_t privin,
int pidin);
static int match_rctl_blk(rctlblk_t *rctl, char *valuestringin,
uint64_t valuein,
rctl_priv_t privin, int pidin);
static pid_t regrab_process(pid_t pid, pr_info_handle_t *p, int, int *gret);
static pid_t grab_process_by_id(char *idname, rctl_entity_t type,
pr_info_handle_t *p, int, int *gret);
static int grab_process(pr_info_handle_t *p, int *gret);
static void release_process(struct ps_prochandle *Pr);
static void preserve_error(char *format, ...);
static void print_rctls(pr_info_handle_t *p);
static void print_priv(rctl_priv_t local_priv, char *format);
static void print_local_action(int action, int *signalp, char *format);
static const char USAGE[] = ""
"usage:\n"
" Report resource control values and actions:\n"
" prctl [-P] [-t [basic | privileged | system]\n"
" [-n name] [-i process | task | project | zone] id ...\n"
" -P space delimited output\n"
" -t privilege level of rctl values to get\n"
" -n name of resource control values to get\n"
" -i idtype of operand list\n"
" Manipulate resource control values:\n"
" prctl [-t [basic | privileged | system]\n"
" -n name [-srx] [-v value] [-p pid ] [-e | -d action]\n"
" [-i process | task | project | zone] id ...\n"
" -t privilege level of rctl value to set/replace/delete/modify\n"
" -n name of resource control to set/replace/delete/modify\n"
" -s set new resource control value\n"
" -r replace first rctl value of matching privilege\n"
" -x delete first rctl value of matching privilege, value, and \n"
" recipient pid\n"
" -v value of rctl to set/replace/delete/modify\n"
" -p recipient pid of rctl to set/replace/delete/modify\n"
" -e enable action of first rctl value of matching privilege,\n"
" value, and recipient pid\n"
" -d disable action of first rctl value of matching privilege,\n"
" value, and recipient pid\n"
" -i idtype of operand list\n";
static void
usage()
{
(void) fprintf(stderr, gettext(USAGE));
exit(2);
}
int
main(int argc, char **argv)
{
int flags;
int opt, errflg = 0;
rctlblk_t *rctlblkA = NULL;
rctlblk_t *rctlblkB = NULL;
rctlblk_t *tmp = NULL;
pid_t pid;
char *target_id;
int search_type;
int signal;
int localaction;
int printed = 0;
int gret;
char *end;
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
(void) setpname(argv[0]);
while ((opt = getopt(argc, argv, "sPp:Fd:e:i:n:rt:v:x")) != EOF) {
switch (opt) {
case 'F': /* force grabbing (no O_EXCL) */
arg_force = PGRAB_FORCE;
break;
case 'i': /* id type for arguments */
arg_entity_string = optarg;
if (strcmp(optarg, "process") == 0 ||
strcmp(optarg, "pid") == 0)
arg_entity_type = RCENTITY_PROCESS;
else if (strcmp(optarg, "project") == 0 ||
strcmp(optarg, "projid") == 0)
arg_entity_type = RCENTITY_PROJECT;
else if (strcmp(optarg, "task") == 0 ||
strcmp(optarg, "taskid") == 0)
arg_entity_type = RCENTITY_TASK;
else if (strcmp(optarg, "zone") == 0 ||
strcmp(optarg, "zoneid") == 0)
arg_entity_type = RCENTITY_ZONE;
else {
warn(gettext("unknown idtype %s"), optarg);
errflg = 1;
}
break;
case 'd':
arg_action_string = optarg;
arg_operation |= ACTION_DISABLE;
break;
case 'e':
arg_action_string = optarg;
arg_operation |= ACTION_ENABLE;
break;
case 'n': /* name of rctl */
arg_name = optarg;
if (strncmp(optarg, "process.",
strlen("process.")) == 0)
arg_name_entity = RCENTITY_PROCESS;
else if (strncmp(optarg, "project.",
strlen("project.")) == 0)
arg_name_entity = RCENTITY_PROJECT;
else if (strncmp(optarg, "task.",
strlen("task.")) == 0)
arg_name_entity = RCENTITY_TASK;
else if (strncmp(optarg, "zone.",
strlen("zone.")) == 0)
arg_name_entity = RCENTITY_ZONE;
break;
case 'r':
arg_operation |= ACTION_REPLACE;
break;
case 't': /* rctl type */
if (strcmp(optarg, "basic") == 0)
arg_priv = RCPRIV_BASIC;
else if (strcmp(optarg, "privileged") == 0)
arg_priv = RCPRIV_PRIVILEGED;
else if (strcmp(optarg, "priv") == 0)
arg_priv = RCPRIV_PRIVILEGED;
else if (strcmp(optarg, "system") == 0)
arg_priv = RCPRIV_SYSTEM;
else {
warn(gettext("unknown privilege %s"), optarg);
errflg = 1;
}
break;
case 'v': /* value */
arg_valuestring = optarg;
break;
case 's':
arg_operation |= ACTION_SET;
break;
case 'x': /* delete */
arg_operation |= ACTION_DELETE;
break;
case 'p':
errno = 0;
/* Stick with -1 if arg is "-" */
if (strcmp("-", optarg) == 0)
break;
arg_pid_string = optarg;
arg_pid = strtoul(optarg, &end, 10);
if (errno || *end != '\0' || end == optarg) {
warn(gettext("invalid pid %s"), optarg);
errflg = 1;
break;
}
break;
case 'P':
arg_parseable_mode = 1;
break;
default:
warn(gettext("unknown option"));
errflg = 1;
break;
}
}
argc -= optind;
argv += optind;
if (argc < 1) {
warn(gettext("no arguments specified"));
errflg = 1;
goto done_parse;
}
/* if -v is specified without -r, -x, -d, or -e, -s is implied */
if (arg_valuestring &&
(!(arg_operation & (ACTION_REPLACE | ACTION_DELETE |
ACTION_DISABLE | ACTION_ENABLE)))) {
arg_operation |= ACTION_SET;
}
/* operations require -n */
if (arg_operation && (arg_name == NULL)) {
warn(gettext("-n is required with -s, -r, -x, -e, or -d"));
errflg = 1;
goto done_parse;
}
/* enable and disable are exclusive */
if ((arg_operation & ACTION_ENABLE) &&
(arg_operation & ACTION_DISABLE)) {
warn(gettext("options -d and -e are exclusive"));
errflg = 1;
goto done_parse;
}
/* -s, -r, and -x are exclusive */
flags = arg_operation &
(ACTION_REPLACE | ACTION_SET | ACTION_DELETE);
if (flags & (flags - 1)) {
warn(gettext("options -s, -r, and -x are exclusive"));
errflg = 1;
goto done_parse;
}
/* -e or -d makes no sense with -x */
if ((arg_operation & ACTION_DELETE) &
(arg_operation & (ACTION_ENABLE | ACTION_DISABLE))) {
warn(gettext("options -e or -d not allowed with -x"));
errflg = 1;
goto done_parse;
}
/* if -r is specified -v must be as well */
if ((arg_operation & ACTION_REPLACE) && (!arg_valuestring)) {
warn(gettext("option -r requires use of option -v"));
errflg = 1;
goto done_parse;
}
/* if -s is specified -v must be as well */
if ((arg_operation & ACTION_SET) && (!arg_valuestring)) {
warn(gettext("option -s requires use of option -v"));
errflg = 1;
goto done_parse;
}
/* Specifying a recipient pid on a non-basic rctl makes no sense */
if (arg_pid != -1 && arg_priv > RCPRIV_BASIC) {
warn(gettext("option -p not allowed on non-basic rctl"));
errflg = 1;
goto done_parse;
}
/* Specifying a recipient pid on a privileged rctl makes no sense */
if (arg_pid != -1 &&
arg_priv == RCPRIV_PRIVILEGED) {
warn(gettext("option -p not allowed with privileged rctl"));
errflg = 1;
goto done_parse;
}
if (arg_operation) {
/* do additional checks if there is an operation */
if (arg_parseable_mode == 1) {
warn(gettext("-P not valid when manipulating "
"resource control values"));
errflg = 1;
goto done_parse;
}
/* get rctl global flags to determine if actions are valid */
if ((rctlblkA = calloc(1, rctlblk_size())) == NULL) {
warn(gettext("malloc failed: %s"),
strerror(errno));
errflg = 1;
goto done_parse;
}
if ((rctlblkB = calloc(1, rctlblk_size())) == NULL) {
warn(gettext("malloc failed: %s"),
strerror(errno));
errflg = 1;
goto done_parse;
}
/* get system rctl to get global flags and max value */
if (getrctl(arg_name, NULL, rctlblkA, RCTL_FIRST)) {
warn(gettext("failed to get resource control "
"for %s: %s"), arg_name, strerror(errno));
errflg = 1;
goto done_parse;
}
while (getrctl(arg_name, rctlblkA, rctlblkB, RCTL_NEXT) == 0) {
/* allow user interrupt */
if (interrupt) {
errflg = 1;
goto done_parse;
}
tmp = rctlblkB;
rctlblkB = rctlblkA;
rctlblkA = tmp;
if (rctlblk_get_privilege(rctlblkA) ==
RCPRIV_SYSTEM) {
break;
}
}
if (rctlblk_get_privilege(rctlblkA) != RCPRIV_SYSTEM) {
warn(gettext("failed to get system resource control "
"for %s: %s"), arg_name, strerror(errno));
errflg = 1;
goto done_parse;
}
/* figure out the correct scale and unit for this rctl */
arg_global_flags = rctlblk_get_global_flags(rctlblkA);
arg_global_max = rctlblk_get_value(rctlblkA);
if (arg_global_flags & RCTL_GLOBAL_BYTES) {
arg_unit = SCALED_UNIT_BYTES;
arg_scale = scale_binary;
} else if (arg_global_flags & RCTL_GLOBAL_SECONDS) {
arg_unit = SCALED_UNIT_SECONDS;
arg_scale = scale_metric;
} else {
arg_unit = SCALED_UNIT_NONE;
arg_scale = scale_metric;
}
/* parse -v value string */
if (arg_valuestring) {
if (scaledtouint64(arg_valuestring,
&arg_value, NULL, &arg_modifier, NULL,
arg_scale, arg_unit,
SCALED_ALL_FLAGS)) {
warn(gettext("invalid -v value %s"),
arg_valuestring);
errflg = 1;
goto done_parse;
}
if (arg_value > arg_global_max) {
warn(gettext("-v value %s exceeds system "
"limit for resource control: %s"),
arg_valuestring, arg_name);
errflg = 1;
goto done_parse;
}
}
/* parse action */
if (arg_action_string) {
char *sigchr;
char *iter;
if ((strcmp(arg_action_string, "signal") == 0) ||
(strcmp(arg_action_string, "sig") == 0)) {
if (arg_operation & ACTION_ENABLE) {
warn(gettext(
"signal name or number must be "
"specified with -e"));
errflg = 1;
goto done_parse;
}
arg_action = RCTL_LOCAL_SIGNAL;
arg_signal = -1;
} else if ((strncmp(arg_action_string,
"signal=", strlen("signal=")) == 0) ||
(strncmp(arg_action_string,
"sig=", strlen("sig=")) == 0)) {
arg_action = RCTL_LOCAL_SIGNAL;
sigchr = strrchr(arg_action_string, '=');
sigchr++;
iter = sigchr;
while (*iter) {
*iter = toupper(*iter);
iter++;
}
if (strncmp("SIG", sigchr, 3) == 0)
sigchr += 3;
if (str2sig(sigchr, &arg_signal) != 0) {
warn(gettext("signal invalid"));
errflg = 1;
goto done_parse;
}
} else if (strcmp(arg_action_string, "deny") == 0) {
arg_action = RCTL_LOCAL_DENY;
} else if (strcmp(arg_action_string, "all") == 0) {
if (arg_operation & ACTION_ENABLE) {
warn(gettext(
"cannot use action 'all' with -e"));
errflg = 1;
goto done_parse;
}
arg_action = RCTL_LOCAL_DENY |
RCTL_LOCAL_SIGNAL;
arg_signal = -1;
goto done_parse;
} else {
warn(gettext("action invalid"));
errflg = 1;
goto done_parse;
}
}
/* cannot manipulate system rctls */
if (arg_priv == RCPRIV_SYSTEM) {
warn(gettext("cannot modify system values"));
errflg = 1;
goto done_parse;
}
/* validate that the privilege is allowed */
if ((arg_priv == RCPRIV_BASIC) &&
(arg_global_flags & RCTL_GLOBAL_NOBASIC)) {
warn(gettext("basic values not allowed on rctl %s"),
arg_name);
errflg = 1;
goto done_parse;
}
/* validate that actions are appropriate for given rctl */
if ((arg_operation & ACTION_ENABLE) &&
(arg_action & RCTL_LOCAL_DENY) &&
(arg_global_flags & RCTL_GLOBAL_DENY_NEVER)) {
warn(gettext("unable to enable deny on rctl with "
"global flag 'no-deny'"));
errflg = 1;
goto done_parse;
}
if ((arg_operation & ACTION_DISABLE) &&
(arg_action & RCTL_LOCAL_DENY) &&
(arg_global_flags & RCTL_GLOBAL_DENY_ALWAYS)) {
warn(gettext("unable to disable deny on rctl with "
"global flag 'deny'"));
errflg = 1;
goto done_parse;
}
if ((arg_operation & ACTION_ENABLE) &&
(arg_action & RCTL_LOCAL_SIGNAL) &&
(arg_global_flags & RCTL_GLOBAL_SIGNAL_NEVER)) {
warn(gettext("unable to enable signal on rctl with "
"global flag 'no-signal'"));
errflg = 1;
goto done_parse;
}
/* now set defaults for options not supplied */
/*
* default privilege to basic if this is a seting an rctl
* operation
*/
if (arg_operation & ACTION_SET) {
if (arg_priv == 0) {
arg_priv = RCPRIV_BASIC;
}
}
/*
* -p is required when set a basic task,
* project or zone rctl
*/
if ((arg_pid == -1) &&
(arg_priv == RCPRIV_BASIC) &&
(arg_entity_type != RCENTITY_PROCESS) &&
(arg_operation & ACTION_SET) &&
(arg_name) &&
(arg_name_entity == RCENTITY_TASK ||
arg_name_entity == RCENTITY_PROJECT ||
arg_name_entity == RCENTITY_ZONE)) {
warn(gettext("-p pid required when setting or "
"replacing task or project rctl"));
errflg = 1;
goto done_parse;
}
} else {
/* validate for list mode */
/* -p is not valid in list mode */
if (arg_pid != -1) {
warn(gettext("-p pid requires -s, -r, -x, -e, or -d"));
errflg = 1;
goto done_parse;
}
}
/* getting/setting process rctl on task or project is error */
if ((arg_name && (arg_name_entity == RCENTITY_PROCESS)) &&
((arg_entity_type == RCENTITY_TASK) ||
(arg_entity_type == RCENTITY_PROJECT))) {
warn(gettext("cannot get/set process rctl on task "
"or project"));
errflg = 1;
goto done_parse;
}
/* getting/setting task rctl on project is error */
if ((arg_name && (arg_name_entity == RCENTITY_TASK)) &&
(arg_entity_type == RCENTITY_PROJECT)) {
warn(gettext("cannot get/set task rctl on project"));
errflg = 1;
goto done_parse;
}
done_parse:
/* free any rctlblk's that we may have allocated */
if (rctlblkA) {
free(rctlblkA);
rctlblkA = NULL;
}
if (rctlblkB) {
free(rctlblkB);
rctlblkB = NULL;
}
if (errflg)
usage();
/* catch signals from terminal */
if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
(void) sigset(SIGHUP, intr);
if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
(void) sigset(SIGINT, intr);
if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
(void) sigset(SIGQUIT, intr);
(void) sigset(SIGTERM, intr);
while (--argc >= 0 && !interrupt) {
pr_info_handle_t p;
char *arg = *argv++;
int intarg;
char *end;
errflg = 0;
gret = 0;
/* Store int version of arg */
errno = 0;
intarg = strtoul(arg, &end, 10);
if (errno || *end != '\0' || end == arg) {
intarg = -1;
}
/*
* -p defaults to arg if basic and collective rctl
* and -i process is specified
*/
if ((arg_pid == -1) &&
(arg_priv == RCPRIV_BASIC) &&
(arg_entity_type == RCENTITY_PROCESS) &&
(arg_name) &&
(arg_name_entity == RCENTITY_TASK ||
arg_name_entity == RCENTITY_PROJECT)) {
arg_pid_string = arg;
errno = 0;
arg_pid = intarg;
}
/* Specifying a recipient pid and -i pid is redundent */
if (arg_pid != -1 && arg_entity_type == RCENTITY_PROCESS &&
arg_pid != intarg) {
warn(gettext("option -p pid must match -i process"));
errflg = 1;
continue;
}
/* use recipient pid if we have one */
if (arg_pid_string != NULL) {
target_id = arg_pid_string;
search_type = RCENTITY_PROCESS;
} else {
target_id = arg;
search_type = arg_entity_type;
}
(void) fflush(stdout); /* process-at-a-time */
if (arg_operation != 0) {
if ((pid = grab_process_by_id(target_id,
search_type, &p, arg_priv, &gret)) < 0) {
/*
* Mark that an error occurred so that the
* return value can be set, but continue
* on with other processes
*/
errflg = 1;
continue;
}
/*
* At this point, the victim process is held.
* Do not call any Pgrab-unsafe functions until
* the process is released via release_process().
*/
errflg = get_rctls(p.pr);
if (arg_operation & ACTION_DELETE) {
/* match by privilege, value, and pid */
if (match_rctl(p.pr, &rctlblkA, arg_name,
arg_valuestring, arg_value, arg_priv,
arg_pid) != 0 || rctlblkA == NULL) {
if (interrupt)
goto out;
preserve_error(gettext("no matching "
"resource control found for "
"deletion"));
errflg = 1;
goto out;
}
/*
* grab correct process. This is neccessary
* if the recipient pid does not match the
* one we grabbed
*/
pid = regrab_process(
rctlblk_get_recipient_pid(rctlblkA),
&p, arg_priv, &gret);
if (pid < 0) {
errflg = 1;
goto out;
}
if (prctl_setrctl(p.pr, arg_name, NULL,
rctlblkA, RCTL_DELETE) != 0) {
errflg = 1;
goto out;
}
} else if (arg_operation & ACTION_SET) {
/* match by privilege, value, and pid */
if (match_rctl(p.pr, &rctlblkA, arg_name,
arg_valuestring, arg_value, arg_priv,
arg_pid) == 0) {
if (interrupt)
goto out;
preserve_error(gettext("resource "
"control already exists"));
errflg = 1;
goto out;
}
rctlblkB = calloc(1, rctlblk_size());
if (rctlblkB == NULL) {
preserve_error(gettext(
"malloc failed"), strerror(errno));
errflg = 1;
goto out;
}
rctlblk_set_value(rctlblkB, arg_value);
rctlblk_set_privilege(rctlblkB, arg_priv);
if (change_action(rctlblkB)) {
errflg = 1;
goto out;
}
if (prctl_setrctl(p.pr, arg_name, NULL,
rctlblkB, RCTL_INSERT) != 0) {
errflg = 1;
goto out;
}
} else if (arg_operation & ACTION_REPLACE) {
/*
* match rctl for deletion by privilege and
* pid only
*/
if (match_rctl(p.pr, &rctlblkA, arg_name,
NULL, 0, arg_priv,
arg_pid) != 0 || rctlblkA == NULL) {
if (interrupt)
goto out;
preserve_error(gettext("no matching "
"resource control to replace"));
errflg = 1;
goto out;
}
/*
* grab correct process. This is neccessary
* if the recipient pid does not match the
* one we grabbed
*/
pid = regrab_process(
rctlblk_get_recipient_pid(rctlblkA),
&p, arg_priv, &gret);
if (pid < 0) {
errflg = 1;
goto out;
}
pid = rctlblk_get_recipient_pid(rctlblkA);
/*
* match by privilege, value and pid to
* check if new rctl already exists
*/
if (match_rctl(p.pr, &rctlblkB, arg_name,
arg_valuestring, arg_value, arg_priv,
pid) < 0) {
if (interrupt)
goto out;
preserve_error(gettext(
"Internal Error"));
errflg = 1;
goto out;
}
/*
* If rctl already exists, and it does not
* match the one that we will delete, than
* the replace will fail.
*/
if (rctlblkB != NULL &&
arg_value != rctlblk_get_value(rctlblkA)) {
preserve_error(gettext("replacement "
"resource control already "
"exists"));
errflg = 1;
goto out;
}
/* create new rctl */
rctlblkB = calloc(1, rctlblk_size());
if (rctlblkB == NULL) {
preserve_error(gettext(
"malloc failed"), strerror(errno));
errflg = 1;
goto out;
}
localaction =
rctlblk_get_local_action(rctlblkA, &signal);
rctlblk_set_local_action(rctlblkB, localaction,
signal);
rctlblk_set_value(rctlblkB, arg_value);
rctlblk_set_privilege(rctlblkB,
rctlblk_get_privilege(rctlblkA));
if (change_action(rctlblkB)) {
errflg = 1;
goto out;
}
/* do replacement */
if (prctl_setrctl(p.pr, arg_name, rctlblkA,
rctlblkB, RCTL_REPLACE) != 0) {
errflg = 1;
goto out;
}
} else if (arg_operation &
(ACTION_ENABLE | ACTION_DISABLE)) {
rctlblkB = calloc(1, rctlblk_size());
if (rctlblkB == NULL) {
preserve_error(gettext(
"malloc failed"), strerror(errno));
errflg = 1;
goto out;
}
/* match by privilege, value, and pid */
if (match_rctl(p.pr, &rctlblkA, arg_name,
arg_valuestring, arg_value, arg_priv,
arg_pid) != 0) {
if (interrupt)
goto out;
/* if no match, just set new rctl */
if (arg_priv == 0)
arg_priv = RCPRIV_BASIC;
if ((arg_priv == RCPRIV_BASIC) &&
(arg_entity_type !=
RCENTITY_PROCESS) &&
(arg_pid_string == NULL)) {
preserve_error(gettext(
"-p required when setting "
"basic rctls"));
errflg = 1;
goto out;
}
rctlblk_set_value(rctlblkB,
arg_value);
rctlblk_set_privilege(
rctlblkB, arg_priv);
if (change_action(rctlblkB)) {
errflg = 1;
goto out;
}
if (prctl_setrctl(p.pr,
arg_name, NULL, rctlblkB,
RCTL_INSERT) != 0) {
errflg = 1;
goto out;
}
goto out;
}
if (rctlblkA == NULL) {
preserve_error(gettext("no matching "
"resource control found"));
errflg = 1;
goto out;
}
/*
* grab correct process. This is neccessary
* if the recipient pid does not match the
* one we grabbed
*/
pid = regrab_process(
rctlblk_get_recipient_pid(rctlblkA),
&p, arg_priv, &gret);
if (pid < 0) {
errflg = 1;
goto out;
}
localaction =
rctlblk_get_local_action(rctlblkA,
&signal);
rctlblk_set_local_action(rctlblkB, localaction,
signal);
rctlblk_set_privilege(rctlblkB,
rctlblk_get_privilege(rctlblkA));
rctlblk_set_value(rctlblkB,
rctlblk_get_value(rctlblkA));
if (change_action(rctlblkB)) {
errflg = 1;
goto out;
}
if (prctl_setrctl(p.pr, arg_name, rctlblkA,
rctlblkB, RCTL_REPLACE) != 0) {
errflg = 1;
goto out;
}
}
out:
release_process(p.pr);
if (rctlblkA)
free(rctlblkA);
if (rctlblkB)
free(rctlblkB);
/* Print any errors that occurred */
if (errflg && *global_error != '\0') {
proc_unctrl_psinfo(&(p.psinfo));
(void) fprintf(stderr, "%d:\t%.70s\n",
(int)p.pid, p.psinfo.pr_psargs);
warn("%s\n", global_error);
break;
}
} else {
struct project projent;
char buf[PROJECT_BUFSZ];
char zonename[ZONENAME_MAX];
/*
* Hack to allow the user to specify a system
* process.
*/
gret = G_SYS;
pid = grab_process_by_id(
target_id, search_type, &p, RCPRIV_BASIC, &gret);
/*
* Print system process if user chose specifically
* to inspect a system process.
*/
if (arg_entity_type == RCENTITY_PROCESS &&
pid < 0 &&
gret == G_SYS) {
/*
* Add blank lines between output for
* operands.
*/
if (printed) {
(void) fprintf(stdout, "\n");
}
proc_unctrl_psinfo(&(p.psinfo));
(void) printf(
"process: %d: %s [ system process ]\n",
(int)p.pid, p.psinfo.pr_psargs);
printed = 1;
continue;
} else if (pid < 0) {
/*
* Mark that an error occurred so that the
* return value can be set, but continue
* on with other processes
*/
errflg = 1;
continue;
}
errflg = get_rctls(p.pr);
release_process(p.pr);
/* handle user interrupt of getting rctls */
if (interrupt)
break;
/* add blank lines between output for operands */
if (printed) {
(void) fprintf(stdout, "\n");
}
/* First print any errors */
if (errflg) {
warn("%s\n", global_error);
free_lists();
break;
}
if (getprojbyid(p.projid, &projent, buf,
sizeof (buf))) {
p.projname = projent.pj_name;
} else {
p.projname = "";
}
if (getzonenamebyid(p.zoneid, zonename,
sizeof (zonename)) > 0) {
p.zonename = zonename;
} else {
p.zonename = "";
}
print_rctls(&p);
printed = 1;
/* Free the resource control lists */
free_lists();
}
}
if (interrupt)
errflg = 1;
/*
* return error if one occurred
*/
return (errflg);
}
static void
intr(int sig)
{
interrupt = sig;
}
/*
* get_rctls(struct ps_prochandle *, const char *)
*
* If controlname is given, store only controls for that named
* resource. If controlname is NULL, store all controls for all
* resources.
*
* This function is Pgrab-safe.
*/
static int
get_rctls(struct ps_prochandle *Pr)
{
int ret = 0;
if (arg_name == NULL) {
if (rctl_walk(store_rctls, Pr) != 0)
ret = 1;
} else {
ret = store_rctls(arg_name, Pr);
}
return (ret);
}
/*
* store_rctls(const char *, void *)
*
* Store resource controls for the given name in a linked list.
* Honor the user's options, and store only the ones they are
* interested in. If priv is not 0, show only controls that match
* the given privilege.
*
* This function is Pgrab-safe
*/
static int
store_rctls(const char *rctlname, void *walk_data)
{
struct ps_prochandle *Pr = walk_data;
rctlblk_t *rblk2, *rblk_tmp, *rblk1 = NULL;
prctl_list_t *list = NULL;
rctl_priv_t rblk_priv;
rctl_entity_t rblk_entity;
if (((rblk1 = calloc(1, rctlblk_size())) == NULL) ||
((rblk2 = calloc(1, rctlblk_size())) == NULL)) {
if (rblk1 != NULL)
free(rblk1);
preserve_error(gettext("malloc failed: %s"),
strerror(errno));
return (1);
}
if (pr_getrctl(Pr, rctlname, NULL, rblk1, RCTL_FIRST)) {
preserve_error(gettext("failed to get resource control "
"for %s: %s"), rctlname, strerror(errno));
free(rblk1);
free(rblk2);
return (1);
}
/* Store control if it matches privilege and enity type criteria */
rblk_priv = rctlblk_get_privilege(rblk1);
rblk_entity = 0;
if (strncmp(rctlname, "process.",
strlen("process.")) == 0)
rblk_entity = RCENTITY_PROCESS;
else if (strncmp(rctlname, "project.",
strlen("project.")) == 0)
rblk_entity = RCENTITY_PROJECT;
else if (strncmp(rctlname, "task.",
strlen("task.")) == 0)
rblk_entity = RCENTITY_TASK;
else if (strncmp(rctlname, "zone.",
strlen("zone.")) == 0)
rblk_entity = RCENTITY_ZONE;
if (((arg_priv == 0) || (rblk_priv == arg_priv)) &&
((arg_name == NULL) ||
strncmp(rctlname, arg_name, strlen(arg_name)) == 0) &&
(arg_entity_string == NULL || rblk_entity >= arg_entity_type)) {
/* Once we know we have some controls, store the name */
if ((list = store_list_entry(rctlname)) == NULL) {
free(rblk1);
free(rblk2);
return (1);
}
if (store_value_entry(rblk1, list) == NULL) {
free(rblk1);
free(rblk2);
return (1);
}
}
while (pr_getrctl(Pr, rctlname, rblk1, rblk2, RCTL_NEXT) == 0) {
/*
* in case this is stuck for some reason, allow manual
* interrupt
*/
if (interrupt) {
free(rblk1);
free(rblk2);
return (1);
}
rblk_priv = rctlblk_get_privilege(rblk2);
/*
* Store control if it matches privilege and entity type
* criteria
*/
if (((arg_priv == 0) || (rblk_priv == arg_priv)) &&
((arg_name == NULL) ||
strncmp(rctlname, arg_name, strlen(arg_name)) == 0) &&
(arg_entity_string == NULL ||
rblk_entity == arg_entity_type)) {
/* May not have created the list yet. */
if (list == NULL) {
if ((list = store_list_entry(rctlname))
== NULL) {
free(rblk1);
free(rblk2);
return (1);
}
}
if (store_value_entry(rblk2, list) == NULL) {
free(rblk1);
free(rblk2);
return (1);
}
}
rblk_tmp = rblk1;
rblk1 = rblk2;
rblk2 = rblk_tmp;
}
/*
* Get the current usage for the resource control if it matched the
* privilege and entity type criteria.
*/
if (list != NULL) {
if (pr_getrctl(Pr, rctlname, NULL, rblk2, RCTL_USAGE) == 0) {
list->usage = (rctl_qty_t *)malloc(sizeof (rctl_qty_t));
if (list->usage == NULL) {
preserve_error(gettext("malloc failed: %s"),
strerror(errno));
free(rblk1);
free(rblk2);
return (1);
}
*list->usage = rctlblk_get_value(rblk2);
} else {
list->usage = NULL;
if (errno != ENOTSUP) {
preserve_error(gettext("failed to get "
"resource control usage for %s: %s"),
rctlname, strerror(errno));
free(rblk1);
free(rblk2);
return (1);
}
}
}
free(rblk1);
free(rblk2);
return (0);
}
/*
* store_value_entry(rctlblk_t *, prctl_list_t *)
*
* Store an rblk for a given resource control into the global list.
*
* This function is Pgrab-safe.
*/
prctl_value_t *
store_value_entry(rctlblk_t *rblk, prctl_list_t *list)
{
prctl_value_t *e = calloc(1, sizeof (prctl_value_t));
rctlblk_t *store_blk = calloc(1, rctlblk_size());
prctl_value_t *iter = list->val_list;
if (e == NULL || store_blk == NULL) {
preserve_error(gettext("malloc failed %s"),
strerror(errno));
if (e != NULL)
free(e);
if (store_blk != NULL)
free(store_blk);
return (NULL);
}
if (iter == NULL)
list->val_list = e;
else {
while (iter->next != NULL) {
iter = iter->next;
}
iter->next = e;
}
bcopy(rblk, store_blk, rctlblk_size());
e->rblk = store_blk;
e->next = NULL;
return (e);
}
/*
* store_list_entry(const char *)
*
* Store a new resource control value in the global list. No checking
* for duplicates done.
*
* This function is Pgrab-safe.
*/
prctl_list_t *
store_list_entry(const char *name)
{
prctl_list_t *e = calloc(1, sizeof (prctl_list_t));
if (e == NULL) {
preserve_error(gettext("malloc failed %s"),
strerror(errno));
return (NULL);
}
if ((e->name = calloc(1, strlen(name) + 1)) == NULL) {
preserve_error(gettext("malloc failed %s"),
strerror(errno));
free(e);
return (NULL);
}
(void) strcpy(e->name, name);
e->val_list = NULL;
if (global_rctl_list_head == NULL) {
global_rctl_list_head = e;
global_rctl_list_tail = e;
} else {
global_rctl_list_tail->next = e;
global_rctl_list_tail = e;
}
e->next = NULL;
return (e);
}
/*
* free_lists()
*
* Free all resource control blocks and values from the global lists.
*
* This function is Pgrab-safe.
*/
void
free_lists()
{
prctl_list_t *new_list, *old_list = global_rctl_list_head;
prctl_value_t *old_val, *new_val;
while (old_list != NULL) {
old_val = old_list->val_list;
while (old_val != NULL) {
free(old_val->rblk);
new_val = old_val->next;
free(old_val);
old_val = new_val;
}
free(old_list->name);
free(old_list->usage);
new_list = old_list->next;
free(old_list);
old_list = new_list;
}
global_rctl_list_head = NULL;
global_rctl_list_tail = NULL;
}
void
print_heading()
{
/* print headings */
(void) fprintf(stdout, "%-8s%-16s%-9s%-7s%-28s%10s\n",
"NAME", "PRIVILEGE", "VALUE",
"FLAG", "ACTION", "RECIPIENT");
}
/*
* print_rctls()
*
* Print all resource controls from the global list that was
* previously populated by store_rctls.
*/
void
print_rctls(pr_info_handle_t *p)
{
prctl_list_t *iter_list = global_rctl_list_head;
prctl_value_t *iter_val;
rctl_qty_t rblk_value;
rctl_priv_t rblk_priv;
uint_t local_action;
int signal, local_flags, global_flags;
pid_t pid;
char rctl_valuestring[SCALED_STRLEN];
char *unit = NULL;
scale_t *scale;
char *string;
int doneheading = 0;
if (iter_list == NULL)
return;
while (iter_list != NULL) {
if (doneheading == 0 &&
arg_entity_type == RCENTITY_PROCESS) {
proc_unctrl_psinfo(&(p->psinfo));
doneheading = 1;
(void) fprintf(stdout,
"process: %d: %.70s\n", (int)p->pid,
p->psinfo.pr_psargs);
if (!arg_parseable_mode)
print_heading();
}
if (doneheading == 0 &&
arg_entity_type == RCENTITY_TASK) {
doneheading = 1;
(void) fprintf(stdout, "task: %d\n", (int)p->taskid);
if (!arg_parseable_mode)
print_heading();
}
if (doneheading == 0 &&
arg_entity_type == RCENTITY_PROJECT) {
if (!arg_parseable_mode && doneheading)
(void) fprintf(stdout, "\n");
doneheading = 1;
(void) fprintf(stdout,
"project: %d: %.70s\n", (int)p->projid,
p->projname);
if (!arg_parseable_mode)
print_heading();
}
if (doneheading == 0 &&
arg_entity_type == RCENTITY_ZONE) {
doneheading = 1;
(void) fprintf(stdout,
"zone: %d: %.70s\n", (int)p->zoneid,
p->zonename);
if (!arg_parseable_mode)
print_heading();
}
/* only print name once in normal output */
if (!arg_parseable_mode)
(void) fprintf(stdout, "%s\n", iter_list->name);
iter_val = iter_list->val_list;
/* if for some reason there are no values, skip */
if (iter_val == 0)
continue;
/* get the global flags the first rctl only */
global_flags = rctlblk_get_global_flags(iter_val->rblk);
if (global_flags & RCTL_GLOBAL_BYTES) {
unit = SCALED_UNIT_BYTES;
scale = scale_binary;
} else if (global_flags & RCTL_GLOBAL_SECONDS) {
unit = SCALED_UNIT_SECONDS;
scale = scale_metric;
} else {
unit = SCALED_UNIT_NONE;
scale = scale_metric;
}
/* print the current usage for the rctl if available */
if (iter_list->usage != NULL) {
rblk_value = *(iter_list->usage);
if (!arg_parseable_mode) {
(void) uint64toscaled(rblk_value, 4, "E",
rctl_valuestring, NULL, NULL,
scale, NULL, 0);
(void) fprintf(stdout, "%8s%-16s%5s%-4s\n",
"", "usage", rctl_valuestring, unit);
} else {
(void) fprintf(stdout, "%s %s %llu - - -\n",
iter_list->name, "usage", rblk_value);
}
}
/* iterate over an print all control values */
while (iter_val != NULL) {
/* print name or empty name field */
if (!arg_parseable_mode)
(void) fprintf(stdout, "%8s", "");
else
(void) fprintf(stdout, "%s ", iter_list->name);
rblk_priv = rctlblk_get_privilege(iter_val->rblk);
if (!arg_parseable_mode)
print_priv(rblk_priv, "%-16s");
else
print_priv(rblk_priv, "%s ");
rblk_value = rctlblk_get_value(iter_val->rblk);
if (arg_parseable_mode) {
(void) fprintf(stdout, "%llu ", rblk_value);
} else {
(void) uint64toscaled(rblk_value, 4, "E",
rctl_valuestring, NULL, NULL,
scale, NULL, 0);
(void) fprintf(stdout, "%5s",
rctl_valuestring);
(void) fprintf(stdout, "%-4s", unit);
}
local_flags = rctlblk_get_local_flags(iter_val->rblk);
if (local_flags & RCTL_LOCAL_MAXIMAL) {
if (global_flags & RCTL_GLOBAL_INFINITE) {
string = "inf";
} else {
string = "max";
}
} else {
string = "-";
}
if (arg_parseable_mode)
(void) fprintf(stdout, "%s ", string);
else
(void) fprintf(stdout, "%4s%3s",
string, "");
local_action = rctlblk_get_local_action(iter_val->rblk,
&signal);
if (arg_parseable_mode)
print_local_action(local_action, &signal,
"%s ");
else
print_local_action(local_action, &signal,
"%-28s");
pid = rctlblk_get_recipient_pid(iter_val->rblk);
if (arg_parseable_mode) {
if (pid < 0) {
(void) fprintf(stdout, "%s\n", "-");
} else {
(void) fprintf(stdout, "%d\n",
(int)pid);
}
} else {
if (pid < 0) {
(void) fprintf(stdout, "%10s\n", "-");
} else {
(void) fprintf(stdout, "%10d\n",
(int)pid);
}
}
iter_val = iter_val->next;
}
iter_list = iter_list->next;
}
}
/*
*
* match_rctl
*
* find the first rctl with matching name, value, priv, and recipient pid
*/
int
match_rctl(struct ps_prochandle *Pr, rctlblk_t **rctl, char *name,
char *valuestringin, int valuein, rctl_priv_t privin, int pidin)
{
rctlblk_t *next;
rctlblk_t *last;
rctlblk_t *tmp;
*rctl = NULL;
next = calloc(1, rctlblk_size());
last = calloc(1, rctlblk_size());
if ((last == NULL) || (next == NULL)) {
preserve_error(gettext("malloc failed"), strerror(errno));
return (-1);
}
/*
* For this resource name, now iterate through all
* the controls, looking for a match to the
* user-specified input.
*/
if (pr_getrctl(Pr, name, NULL, next, RCTL_FIRST)) {
preserve_error(gettext("failed to get resource control "
"for %s: %s"), name, strerror(errno));
return (-1);
}
if (match_rctl_blk(next, valuestringin, valuein, privin, pidin) == 1) {
free(last);
*rctl = next;
return (0);
}
tmp = next;
next = last;
last = tmp;
while (pr_getrctl(Pr, name, last, next, RCTL_NEXT) == 0) {
/* allow user interrupt */
if (interrupt)
break;
if (match_rctl_blk(next, valuestringin, valuein, privin, pidin)
== 1) {
free(last);
*rctl = next;
return (0);
}
tmp = next;
next = last;
last = tmp;
}
free(next);
free(last);
return (1);
}
/*
* int match_rctl_blk(rctlblk_t *, char *, uint64, rctl_priv_t, int pid)
*
* Input
* Must supply a valid rctl, value, privilege, and pid to match on.
* If valuestring is NULL, then valuestring and valuein will not be used
* If privilege type is 0 it will not be used.
* If pid is -1 it will not be used.
*
* Return values
* Returns 1 if a matching rctl given matches the parameters specified, and
* 0 if they do not.
*
* This function is Pgrab-safe.
*/
int
match_rctl_blk(rctlblk_t *rctl, char *valuestringin,
uint64_t valuein, rctl_priv_t privin, int pidin)
{
rctl_qty_t value;
rctl_priv_t priv;
pid_t pid;
int valuematch = 1;
int privmatch = 1;
int pidmatch = 1;
value = rctlblk_get_value(rctl);
priv = rctlblk_get_privilege(rctl);
pid = rctlblk_get_recipient_pid(rctl);
if (valuestringin) {
if (arg_modifier == NULL) {
valuematch = (valuein == value);
} else {
valuematch = scaledequint64(valuestringin, value,
PRCTL_VALUE_WIDTH,
arg_scale, arg_unit,
SCALED_ALL_FLAGS);
}
}
if (privin != 0) {
privmatch = (privin == priv);
}
if (pidin != -1) {
pidmatch = (pidin == pid);
}
return (valuematch && privmatch && pidmatch);
}
static int
change_action(rctlblk_t *blk)
{
int signal = 0;
int action;
action = rctlblk_get_local_action(blk, &signal);
if (arg_operation & ACTION_ENABLE) {
if (arg_action & RCTL_LOCAL_SIGNAL) {
signal = arg_signal;
}
action = action | arg_action;
/* add local action */
rctlblk_set_local_action(blk, action, signal);
} else if (arg_operation & ACTION_DISABLE) {
/*
* if deleting signal and signal number is specified,
* then signal number must match
*/
if ((arg_action & RCTL_LOCAL_SIGNAL) &&
(arg_signal != -1)) {
if (arg_signal != signal) {
preserve_error(gettext("signal name or number "
"does not match existing action"));
return (-1);
}
}
/* remove local action */
action = action & (~arg_action);
rctlblk_set_local_action(blk, RCTL_LOCAL_NOACTION, 0);
rctlblk_set_local_action(blk, action, signal);
}
/* enable deny if it must be enabled */
if (arg_global_flags & RCTL_GLOBAL_DENY_ALWAYS) {
rctlblk_set_local_action(blk, RCTL_LOCAL_DENY | action,
signal);
}
return (0);
}
/*
* prctl_setrctl
*
* Input
* This function expects that input has been validated. In the
* case of a replace operation, both old_rblk and new_rblk must
* be valid resource controls. If a resource control is being
* created, only new_rblk must be supplied. If a resource control
* is being deleted, only new_rblk must be supplied.
*
* If the privilege is a priviliged type, at this time, the process
* tries to take on superuser privileges.
*/
int
prctl_setrctl(struct ps_prochandle *Pr, const char *name,
rctlblk_t *old_rblk, rctlblk_t *new_rblk, uint_t flags)
{
int ret = 0;
rctl_priv_t rblk_priv;
psinfo_t psinfo;
zoneid_t oldzoneid = GLOBAL_ZONEID;
prpriv_t *old_prpriv = NULL, *new_prpriv = NULL;
priv_set_t *eset, *pset;
boolean_t relinquish_failed = B_FALSE;
rblk_priv = rctlblk_get_privilege(new_rblk);
if (rblk_priv == RCPRIV_SYSTEM) {
preserve_error(gettext("cannot modify system values"));
return (1);
}
if (rblk_priv == RCPRIV_PRIVILEGED) {
new_prpriv = proc_get_priv(Pstatus(Pr)->pr_pid);
if (new_prpriv == NULL) {
preserve_error(gettext("cannot get process privileges "
"for pid %d: %s"), Pstatus(Pr)->pr_pid,
strerror(errno));
return (1);
}
/*
* We only have to change the process privileges if it doesn't
* already have PRIV_SYS_RESOURCE. In addition, we want to make
* sure that we don't leave a process with elevated privileges,
* so we make sure the process dies if we exit unexpectedly.
*/
eset = (priv_set_t *)
&new_prpriv->pr_sets[new_prpriv->pr_setsize *
priv_getsetbyname(PRIV_EFFECTIVE)];
pset = (priv_set_t *)
&new_prpriv->pr_sets[new_prpriv->pr_setsize *
priv_getsetbyname(PRIV_PERMITTED)];
if (!priv_ismember(eset, PRIV_SYS_RESOURCE)) {
/* Keep track of original privileges */
old_prpriv = proc_get_priv(Pstatus(Pr)->pr_pid);
if (old_prpriv == NULL) {
preserve_error(gettext("cannot get process "
"privileges for pid %d: %s"),
Pstatus(Pr)->pr_pid, strerror(errno));
proc_free_priv(new_prpriv);
return (1);
}
(void) priv_addset(eset, PRIV_SYS_RESOURCE);
(void) priv_addset(pset, PRIV_SYS_RESOURCE);
if (Psetflags(Pr, PR_KLC) != 0 ||
Psetpriv(Pr, new_prpriv) != 0) {
preserve_error(gettext("cannot set process "
"privileges for pid %d: %s"),
Pstatus(Pr)->pr_pid, strerror(errno));
(void) Punsetflags(Pr, PR_KLC);
proc_free_priv(new_prpriv);
proc_free_priv(old_prpriv);
return (1);
}
}
/*
* If this is a zone.* rctl, it requires more than
* PRIV_SYS_RESOURCE: it wants the process to have global-zone
* credentials. We temporarily grant non-global zone processes
* these credentials, and make sure the process dies if we exit
* unexpectedly.
*/
if (arg_name &&
arg_name_entity == RCENTITY_ZONE &&
getzoneid() == GLOBAL_ZONEID &&
proc_get_psinfo(Pstatus(Pr)->pr_pid, &psinfo) == 0 &&
(oldzoneid = psinfo.pr_zoneid) != GLOBAL_ZONEID) {
/*
* We need to give this process superuser
* ("super-zone") privileges.
*
* Must never return without setting this back!
*/
if (Psetflags(Pr, PR_KLC) != 0 ||
Psetzoneid(Pr, GLOBAL_ZONEID) < 0) {
preserve_error(gettext(
"cannot set global-zone "
"privileges for pid %d: %s"),
Pstatus(Pr)->pr_pid, strerror(errno));
/*
* We couldn't set the zoneid to begin with, so
* there's no point in warning the user about
* trying to un-set it.
*/
oldzoneid = GLOBAL_ZONEID;
ret = 1;
goto bail;
}
}
}
/* Now, actually populate the rctlblk in the kernel */
if (flags == RCTL_REPLACE) {
/*
* Replace should be a delete followed by an insert. This
* allows us to replace rctl value blocks which match in
* privilege and value, but have updated actions, etc.
* setrctl() doesn't allow a direct replace, but we
* should do the right thing for the user in the command.
*/
if (pr_setrctl(Pr, name, NULL,
old_rblk, RCTL_DELETE)) {
preserve_error(gettext("failed to delete resource "
"control %s for pid %d: %s"), name,
Pstatus(Pr)->pr_pid, strerror(errno));
ret = 1;
goto bail;
}
if (pr_setrctl(Pr, name, NULL,
new_rblk, RCTL_INSERT)) {
preserve_error(gettext("failed to insert resource "
"control %s for pid %d: %s"), name,
Pstatus(Pr)->pr_pid, strerror(errno));
ret = 1;
goto bail;
}
} else if (flags == RCTL_INSERT) {
if (pr_setrctl(Pr, name, NULL,
new_rblk, RCTL_INSERT)) {
preserve_error(gettext("failed to create resource "
"control %s for pid %d: %s"), name,
Pstatus(Pr)->pr_pid, strerror(errno));
ret = 1;
goto bail;
}
} else if (flags == RCTL_DELETE) {
if (pr_setrctl(Pr, name, NULL,
new_rblk, RCTL_DELETE)) {
preserve_error(gettext("failed to delete resource "
"control %s for pid %d: %s"), name,
Pstatus(Pr)->pr_pid, strerror(errno));
ret = 1;
goto bail;
}
}
bail:
if (oldzoneid != GLOBAL_ZONEID) {
if (Psetzoneid(Pr, oldzoneid) != 0)
relinquish_failed = B_TRUE;
}
if (old_prpriv != NULL) {
if (Psetpriv(Pr, old_prpriv) != 0)
relinquish_failed = B_TRUE;
proc_free_priv(old_prpriv);
}
if (relinquish_failed) {
/*
* If this failed, we can't leave a process hanging
* around with elevated privileges, so we'll have to
* release the process from libproc, knowing that it
* will be killed (since we set PR_KLC).
*/
Pdestroy_agent(Pr);
preserve_error(gettext("cannot relinquish privileges "
"for pid %d. The process was killed."),
Pstatus(Pr)->pr_pid);
} else {
if (Punsetflags(Pr, PR_KLC) != 0)
preserve_error(gettext("cannot relinquish privileges "
"for pid %d. The process was killed."),
Pstatus(Pr)->pr_pid);
}
if (new_prpriv != NULL)
proc_free_priv(new_prpriv);
return (ret);
}
void
print_priv(rctl_priv_t local_priv, char *format)
{
char pstring[11];
switch (local_priv) {
case RCPRIV_BASIC:
(void) strcpy(pstring, "basic");
break;
case RCPRIV_PRIVILEGED:
(void) strcpy(pstring, "privileged");
break;
case RCPRIV_SYSTEM:
(void) strcpy(pstring, "system");
break;
default:
(void) sprintf(pstring, "%d", local_priv);
break;
}
/* LINTED */
(void) fprintf(stdout, format, pstring);
}
void
print_local_action(int action, int *signalp, char *format)
{
char sig[SIG2STR_MAX];
char sigstring[SIG2STR_MAX + 7];
char astring[5 + SIG2STR_MAX + 7];
int set = 0;
astring[0] = '\0';
if (action == RCTL_LOCAL_NOACTION) {
(void) strcat(astring, "none");
set++;
}
if (action & RCTL_LOCAL_DENY) {
(void) strcat(astring, "deny");
set++;
}
if ((action & RCTL_LOCAL_DENY) &&
(action & RCTL_LOCAL_SIGNAL)) {
(void) strcat(astring, ",");
}
if (action & RCTL_LOCAL_SIGNAL) {
if (sig2str(*signalp, sig))
(void) snprintf(sigstring, sizeof (astring),
"signal=%d", *signalp);
else
(void) snprintf(sigstring, sizeof (astring),
"signal=%s", sig);
(void) strcat(astring, sigstring);
set++;
}
if (set)
/* LINTED */
(void) fprintf(stdout, format, astring);
else
/* LINTED */
(void) fprintf(stdout, format, action);
}
/*
* This function is used to grab the process matching the recipient pid
*/
pid_t
regrab_process(pid_t pid, pr_info_handle_t *p, int priv, int *gret)
{
char pidstring[24];
gret = 0;
if (pid == -1)
return (p->pid);
if (p->pid == pid)
return (p->pid);
release_process(p->pr);
(void) memset(p, 0, sizeof (*p));
(void) snprintf(pidstring, 24, "%d", pid);
return (grab_process_by_id(
pidstring, RCENTITY_PROCESS, p, priv, gret));
}
/*
* int grab_process_by_id(char *, rctl_entity_t, pr_info_handle_t *, int, int *)
*
* Input
* Supply a non-NULL string containing:
* - logical project/zone name or project/zone number if type is
* RCENTITY_PROJECT or RCENTITY_ZONE
* - task number if type is RCENTITY_TYPE
* - a pid if type is RCENTITY_PID
* Also supply an un-allocated prochandle, and an allocated info_handle.
* This function assumes that the type is set.
* If priv is not RCPRIV_BASIC, the grabbed process is required to have
* PRIV_SYS_RESOURCE in it's limit set.
*
* Return Values
* Returns 0 on success and 1 on failure. If there is a process
* running under the specified id, success is returned, and
* Pr is pointed to the process. Success will be returned and Pr
* set to NULL if the matching process is our own.
* If success is returned, psinfo will be valid, and pid will
* be the process number. The process will also be held at the
* end, so release_process should be used by the caller.
*
* This function assumes that signals are caught already so that libproc
* can be safely used.
*
* Return Values
* pid - Process found and grabbed
* -1 - Error
*/
pid_t
grab_process_by_id(char *idname, rctl_entity_t type, pr_info_handle_t *p,
int priv, int *gret)
{
char prbuf[PROJECT_BUFSZ];
projid_t projid;
taskid_t taskid;
zoneid_t zoneid;
zoneid_t zone_self;
struct project proj;
DIR *dirp;
struct dirent *dentp;
int found = 0;
int pid_self;
int ret;
int gret_in;
int intidname;
char *end;
prpriv_t *prpriv;
priv_set_t *prset;
gret_in = *gret;
/* get our pid se we do not try to operate on self */
pid_self = getpid();
/* Store integer version of id */
intidname = strtoul(idname, &end, 10);
if (errno || *end != '\0' || end == idname) {
intidname = -1;
}
/*
* get our zoneid so we don't try to operate on a project in
* another zone
*/
zone_self = getzoneid();
if (idname == NULL || strcmp(idname, "") == 0) {
warn(gettext("id name cannot be nuint64\n"));
return (-1);
}
/*
* Set up zoneid, projid or taskid, as appropriate, so that comparisons
* can be done later with the input.
*/
if (type == RCENTITY_ZONE) {
if (zone_get_id(idname, &zoneid) != 0) {
warn(gettext("%s: unknown zone\n"), idname);
return (-1);
}
} else if (type == RCENTITY_PROJECT) {
if (getprojbyname(idname, &proj, prbuf, PROJECT_BUFSZ)
== NULL) {
if (getprojbyid(intidname, &proj, prbuf,
PROJECT_BUFSZ) == NULL) {
warn(gettext("%s: cannot find project\n"),
idname);
return (-1);
}
}
projid = proj.pj_projid;
} else if (type == RCENTITY_TASK) {
taskid = (taskid_t)atol(idname);
}
/*
* Projects and tasks need to search through /proc for
* a parent process.
*/
if (type == RCENTITY_ZONE || type == RCENTITY_PROJECT ||
type == RCENTITY_TASK) {
if ((dirp = opendir("/proc")) == NULL) {
warn(gettext("%s: cannot open /proc directory\n"),
idname);
return (-1);
}
/*
* Look through all processes in /proc. For each process,
* check if the pr_projid in their psinfo matches the
* specified id.
*/
while (dentp = readdir(dirp)) {
p->pid = atoi(dentp->d_name);
/* Skip self */
if (p->pid == pid_self)
continue;
if (proc_get_psinfo(p->pid, &(p->psinfo)) != 0)
continue;
/* Skip process if it is not what we are looking for */
if (type == RCENTITY_ZONE &&
(p->psinfo).pr_zoneid != zoneid) {
continue;
} else if (type == RCENTITY_PROJECT &&
((p->psinfo).pr_projid != projid ||
(p->psinfo).pr_zoneid != zone_self)) {
continue;
} else if (type == RCENTITY_TASK &&
(p->psinfo).pr_taskid != taskid) {
continue;
}
/* attempt to grab process */
if (grab_process(p, gret) != 0)
continue;
/*
* Re-confirm that this process is still running as
* part of the specified project or task. If it
* doesn't match, release the process and return an
* error. This should only be done if the Pr struct is
* not NULL.
*/
if (type == RCENTITY_PROJECT) {
if (pr_getprojid(p->pr) != projid ||
pr_getzoneid(p->pr) != zone_self) {
release_process(p->pr);
continue;
}
} else if (type == RCENTITY_TASK) {
if (pr_gettaskid(p->pr) != taskid) {
release_process(p->pr);
continue;
}
} else if (type == RCENTITY_ZONE) {
if (pr_getzoneid(p->pr) != zoneid) {
release_process(p->pr);
continue;
}
}
/*
* If we are setting a privileged resource control,
* verify that process has PRIV_SYS_RESOURCE in it's
* limit set. If it does not, then we will not be
* able to give this process the privilege it needs
* to set the resource control.
*/
if (priv != RCPRIV_BASIC) {
prpriv = proc_get_priv(p->pid);
if (prpriv == NULL) {
release_process(p->pr);
continue;
}
prset = (priv_set_t *)
&prpriv->pr_sets[prpriv->pr_setsize *
priv_getsetbyname(PRIV_LIMIT)];
if (!priv_ismember(prset, PRIV_SYS_RESOURCE)) {
proc_free_priv(prpriv);
release_process(p->pr);
continue;
}
proc_free_priv(prpriv);
}
found = 1;
p->taskid = pr_gettaskid(p->pr);
p->projid = pr_getprojid(p->pr);
p->zoneid = pr_getzoneid(p->pr);
break;
}
(void) closedir(dirp);
if (found == 0) {
warn(gettext("%s: No controllable process found in "
"task, project, or zone.\n"), idname);
return (-1);
}
return (p->pid);
} else if (type == RCENTITY_PROCESS) {
/* fail if self */
if (p->pid == pid_self) {
warn(gettext("%s: cannot control self"), idname);
return (-1);
}
/*
* Process types need to be set up with the correct pid
* and psinfo structure.
*/
if ((p->pid = proc_arg_psinfo(idname, PR_ARG_PIDS,
&(p->psinfo), gret)) == -1) {
warn(gettext("%s: cannot examine: %s"), idname,
Pgrab_error(*gret));
return (-1);
}
/* grab process */
ret = grab_process(p, gret);
if (ret == 1) {
/* Don't print error if G_SYS is allowed */
if (gret_in == G_SYS && *gret == G_SYS) {
return (-1);
} else {
warn(gettext("%s: cannot control: %s"), idname,
Pgrab_error(*gret));
return (-1);
}
} else if (ret == 2) {
ret = errno;
warn(gettext("%s: cannot control: %s"), idname,
strerror(ret));
return (-1);
}
p->taskid = pr_gettaskid(p->pr);
p->projid = pr_getprojid(p->pr);
p->zoneid = pr_getzoneid(p->pr);
return (p->pid);
} else {
warn(gettext("%s: unknown resource entity type %d\n"), idname,
type);
return (-1);
}
}
/*
* Do the work required to manipulate a process through libproc.
* If grab_process() returns no errors (0), then release_process()
* must eventually be called.
*
* Return values:
* 0 Successful creation of agent thread
* 1 Error grabbing
* 2 Error creating agent
*/
int
grab_process(pr_info_handle_t *p, int *gret)
{
if ((p->pr = Pgrab(p->pid, arg_force, gret)) != NULL) {
if (Psetflags(p->pr, PR_RLC) != 0) {
Prelease(p->pr, 0);
return (1);
}
if (Pcreate_agent(p->pr) == 0) {
return (0);
} else {
Prelease(p->pr, 0);
return (2);
}
} else {
return (1);
}
}
/*
* Release the specified process. This destroys the agent
* and releases the process. If the process is NULL, nothing
* is done. This function should only be called if grab_process()
* has previously been called and returned success.
*
* This function is Pgrab-safe.
*/
void
release_process(struct ps_prochandle *Pr)
{
if (Pr == NULL)
return;
Pdestroy_agent(Pr);
Prelease(Pr, 0);
}
/*
* preserve_error(char *, ...)
*
* preserve_error() should be called rather than warn() by any
* function that is called while the victim process is held by Pgrab.
* It will save the error until the process has been un-controlled
* and output is reasonable again.
*
* Note that multiple errors are not stored. Any error in these
* sections should be critical and return immediately.
*
* This function is Pgrab-safe.
*
* Since this function may copy untrusted command line arguments to
* global_error, security practices require that global_error never be
* printed directly. Use printf("%s\n", global_error) or equivalent.
*/
/*PRINTFLIKE1*/
void
preserve_error(char *format, ...)
{
va_list alist;
va_start(alist, format);
/*
* GLOBAL_ERR_SZ is pretty big. If the error is longer
* than that, just truncate it, rather than chance missing
* the error altogether.
*/
(void) vsnprintf(global_error, GLOBAL_ERR_SZ-1, format, alist);
va_end(alist);
}
/*
* 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) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <sys/param.h>
#include <libintl.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <strings.h>
#include <sys/types.h>
#include <limits.h>
#include "utils.h"
static char PNAME_FMT[] = "%s: ";
static char ERRNO_FMT[] = ": %s\n";
static char EOL_FMT[] = "\n";
static char *pname;
char *
setpname(char *arg0)
{
char *p = strrchr(arg0, '/');
if (p == NULL)
p = arg0;
else
p++;
pname = p;
return (pname);
}
/*PRINTFLIKE1*/
void
warn(const char *format, ...)
{
int err = errno;
va_list alist;
if (pname != NULL)
(void) fprintf(stderr, gettext(PNAME_FMT), pname);
va_start(alist, format);
(void) vfprintf(stderr, format, alist);
va_end(alist);
if (strchr(format, '\n') == NULL)
if (err)
(void) fprintf(stderr,
gettext(ERRNO_FMT), strerror(err));
else
(void) fprintf(stderr, gettext(EOL_FMT));
}
static char *__metric_modifiers[] = { "K", "M", "G", "T", "P", "E", NULL };
static uint64_t __metric_scales[] = {
1000LLU,
1000LLU * 1000,
1000LLU * 1000 * 1000,
1000LLU * 1000 * 1000 * 1000,
1000LLU * 1000 * 1000 * 1000 * 1000,
1000LLU * 1000 * 1000 * 1000 * 1000 * 1000
};
static scale_t __metric_scale = { __metric_modifiers, __metric_scales };
static char *__binary_modifiers[] = {"K", "M", "G", "T", "P", "E", NULL};
static uint64_t __binary_scales[] = {
1024LLU,
1024LLU * 1024,
1024LLU * 1024 * 1024,
1024LLU * 1024 * 1024 * 1024,
1024LLU * 1024 * 1024 * 1024 * 1024,
1024LLU * 1024 * 1024 * 1024 * 1024 * 1024
};
static scale_t __binary_scale = { __binary_modifiers, __binary_scales };
scale_t *scale_metric = &__metric_scale;
scale_t *scale_binary = &__binary_scale;
int
scaledtouint64(char *scaledin,
uint64_t *uint64out,
int *widthout, char **modifierout, char **unitout,
scale_t *scale, char *unit, int flags) {
double result;
double value;
int index = 0;
uint64_t multiplier = 1;
char string[SCALED_STRLEN];
char *endptr;
int cmp;
int hasmodifier = 0;
char **modifiers = scale->modifers;
uint64_t *scales = scale->scales;
if (modifierout)
*modifierout = NULL;
if (unitout)
*unitout = NULL;
/*
* first check for hex value, which cannot be scaled, as
* hex letters cannot be disserned from modifier or unit letters
*/
if ((strncmp("0x", scaledin, 2) == 0) ||
(strncmp("0X", scaledin, 2) == 0)) {
/* unit cannot be required on hex values */
if ((unit && *unit != '\0') &&
!(flags & SCALED_UNIT_OPTIONAL_FLAG))
return (SCALED_INVALID_UNIT);
errno = 0;
*uint64out = strtoull(scaledin, &endptr, 16);
if (errno) {
if (errno == ERANGE)
return (SCALED_OVERFLOW);
else
return (SCALED_INVALID_NUMBER);
}
if (*endptr != '\0')
return (SCALED_INVALID_NUMBER);
/* compute width of decimal equivalent */
if (widthout) {
(void) snprintf(
string, SCALED_STRLEN, "%llu", *uint64out);
*widthout = strlen(string);
}
return (0);
}
/* scan out numeric value */
errno = 0;
value = strtod(scaledin, &endptr);
if (errno) {
if (errno == ERANGE)
return (SCALED_OVERFLOW);
else
return (SCALED_INVALID_NUMBER);
}
if (endptr == scaledin)
return (SCALED_INVALID_NUMBER);
/* no negative values */
if (strchr(scaledin, '-'))
return (SCALED_INVALID_NUMBER);
if (value < 0.0)
return (SCALED_INVALID_NUMBER);
/* compute width of number string */
if (widthout)
*widthout = (int)(endptr - scaledin);
/* check possible modifier */
if (*endptr != '\0') {
index = 0;
while (modifiers[index] != NULL) {
if (flags & SCALED_MODIFIER_CASE_INSENSITIVE_FLAG)
cmp = strncasecmp(modifiers[index], endptr,
strlen(modifiers[index]));
else
cmp = strncmp(modifiers[index], endptr,
strlen(modifiers[index]));
if (cmp == 0) {
if (modifierout)
*modifierout = modifiers[index];
endptr += strlen(modifiers[index]);
multiplier = scales[index];
result = value * multiplier;
if (result > UINT64_MAX)
return (SCALED_OVERFLOW);
*uint64out = (uint64_t)result;
hasmodifier = 1;
break;
}
index++;
}
}
/* if there is no modifier, value must be an integer */
if (!hasmodifier) {
errno = 0;
*uint64out = strtoull(scaledin, &endptr, 0);
if (errno) {
if (errno == ERANGE)
return (SCALED_OVERFLOW);
else
return (SCALED_INVALID_NUMBER);
}
if (endptr == scaledin)
return (SCALED_INVALID_NUMBER);
}
/* if unit is present when no unit is allowed, fail */
if ((unit == NULL || *unit == '\0') && (*endptr != '\0'))
return (SCALED_INVALID_UNIT);
/* check for missing unit when unit is required */
if ((unit && *unit != '\0') &&
!(flags & SCALED_UNIT_OPTIONAL_FLAG) &&
(*endptr == '\0'))
return (SCALED_INVALID_UNIT);
/* validate unit */
if (unit && *unit != '\0') {
/* allow for missing unit if it is optional */
if ((flags & SCALED_UNIT_OPTIONAL_FLAG) &&
(*endptr == '\0'))
return (0);
if (flags & SCALED_UNIT_CASE_INSENSITIVE_FLAG)
cmp = strncasecmp(unit, endptr, strlen(unit));
else
cmp = strncmp(unit, endptr, strlen(unit));
if (cmp != 0)
return (SCALED_INVALID_UNIT);
if (*(endptr + strlen(unit)) != '\0')
return (SCALED_INVALID_UNIT);
if (unitout)
*unitout = unit;
}
return (0);
}
int
uint64toscaled(uint64_t uint64in, int widthin, char *maxmodifierin,
char *scaledout, int *widthout, char **modifierout,
scale_t *scale, char *unit, int flags) {
int index = 0;
int count;
int width;
int decimals = 0;
char string[SCALED_STRLEN];
double value;
char **modifiers = scale->modifers;
uint64_t *scales = scale->scales;
/* don't scale if there is no reason to */
if (uint64in < scales[0] || maxmodifierin == NULL) {
if (flags & SCALED_PAD_WIDTH_FLAG)
width = widthin;
else
width = 0;
(void) snprintf(string, SCALED_STRLEN, "%%%dllu", width);
/* LINTED */
count = snprintf(scaledout, SCALED_STRLEN, string, uint64in);
if (unit && *unit != '\0')
(void) strcat(scaledout, unit);
if (widthout)
*widthout = count;
if (modifierout)
*modifierout = NULL;
return (0);
}
for (index = 0; modifiers[index + 1] != NULL; index++) {
if (uint64in >= scales[index] &&
uint64in < scales[index + 1])
break;
if ((strncmp(modifiers[index], maxmodifierin,
strlen(modifiers[index])) == 0) &&
(strlen(modifiers[index]) == strlen(maxmodifierin)))
break;
}
value = ((double)(uint64in)) / scales[index];
if (modifierout)
*modifierout = modifiers[index];
count = snprintf(string, SCALED_STRLEN, "%0.0lf", value);
while (count < widthin) {
decimals++;
(void) snprintf(string, SCALED_STRLEN, "%%0.%dlf", decimals);
/* LINTED */
count = snprintf(scaledout, SCALED_STRLEN, string, value);
/* reduce decimal places if we've overshot the desired width */
if (count > widthin) {
decimals--;
break;
}
}
if (flags & SCALED_PAD_WIDTH_FLAG)
width = widthin;
else
width = 0;
(void) snprintf(string, SCALED_STRLEN, "%%%d.%dlf", width, decimals);
/* LINTED */
count = snprintf(scaledout, SCALED_STRLEN, string, value);
(void) strcat(scaledout, modifiers[index]);
if (unit && *unit != '\0')
(void) strcat(scaledout, unit);
if (widthout)
*widthout = count;
return (0);
}
int
scaledtoscaled(char *scaledin, int widthin, char *maxmodifierin,
char *scaledout, int *widthout, char **modifierout,
scale_t *scale, char *unit, int flags) {
int ret;
uint64_t val;
ret = scaledtouint64(scaledin, &val, NULL, NULL, NULL,
scale, unit, flags);
if (ret)
return (ret);
ret = uint64toscaled(val, widthin, maxmodifierin,
scaledout, widthout, modifierout,
scale, unit, flags);
return (ret);
}
int
scaledeqscaled(char *scaled1, char *scaled2,
scale_t *scale, char *unit, int flags) {
int ret;
uint64_t uint64;
char *modifier1;
char *modifier2;
char *modifier = NULL;
int i;
int width;
int width1;
int width2;
char scaledA[SCALED_STRLEN];
char scaledB[SCALED_STRLEN];
char **modifiers = scale->modifers;
/*
* remove padding flag, so strings to compare will not have
* whitespace
*/
flags = flags & (~SCALED_PAD_WIDTH_FLAG);
/* determine each number's width and modifier */
ret = scaledtouint64(scaled1, &uint64, &width1, &modifier1, NULL,
scale, unit, flags);
if (ret)
return (0);
ret = scaledtouint64(scaled2, &uint64, &width2, &modifier2, NULL,
scale, unit, flags);
if (ret)
return (0);
/*
* determine the width and modifier to use for comparison.
* Use widest width and smallest modifier.
* Rescale to new width and modifier
*/
if (modifier1 == NULL || modifier2 == NULL)
modifier = NULL;
else {
for (i = 0; modifiers[i] != NULL; i++) {
if (strcmp(modifier1, modifiers[i]) == 0) {
modifier = modifiers[i];
break;
}
if (strcmp(modifier2, modifiers[i]) == 0) {
modifier = modifiers[i];
break;
}
}
}
width = 0;
if (width1 > width)
width = width1;
if (width2 > width)
width = width2;
/*
* Convert first number to width and modifier.
* This is done for the following reasons:
* 1. In case first number is hecadecimal. This will convert
* it to decimal
* 2. In case the first number has < the minimum number of
* columns.
* 3. The first number is missing an optional unit string.
* 4. Fix casing of modifier and unit.
*/
ret = scaledtoscaled(scaled1, width, modifier,
scaledA, NULL, NULL, scale, unit, flags);
if (ret)
return (0);
/* convert second number to width and modifier matching first number */
ret = scaledtoscaled(scaled2, width, modifier,
scaledB, NULL, NULL, scale, unit, flags);
if (ret)
return (0);
/* numbers are equal if strings match */
return ((strncmp(scaledA, scaledB, strlen(scaledA)) == 0) &&
(strlen(scaledA) == strlen(scaledB)));
}
int
scaledequint64(char *scaled, uint64_t uint64, int minwidth,
scale_t *scale, char *unit, int flags) {
int ret;
uint64_t tmpuint64;
char *modifier;
int width;
char scaledA[SCALED_STRLEN];
char scaledB[SCALED_STRLEN];
/* determine for number's width and modifier */
ret = scaledtouint64(scaled, &tmpuint64, &width, &modifier, NULL,
scale, unit, flags);
if (ret)
return (0);
if (width < minwidth)
width = minwidth;
/*
* Convert first number to width and modifier.
* This is done for the following reasons:
* 1. In case first number is hecadecimal. This will convert
* it to decimal
* 2. In case the first number has < the minimum number of
* columns.
* 3. The first number is missing an optional unit string.
* 4. Fix casing of modifier and unit.
*/
ret = scaledtoscaled(scaled, width, modifier,
scaledA, NULL, NULL, scale, unit, flags);
if (ret)
return (0);
/* convert second number to width and modifier matching first number */
ret = uint64toscaled(uint64, width, modifier,
scaledB, NULL, NULL, scale, unit, flags);
if (ret)
return (0);
/* numbers are equal if strings match */
return ((strncmp(scaledA, scaledB, strlen(scaledA)) == 0) &&
(strlen(scaledA) == strlen(scaledB)));
}
/*
* 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) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#ifndef _UTILS_H
#define _UTILS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/types.h>
extern void warn(const char *, ...);
extern char *setpname(char *);
/*
* scale_t
*
* Used to describe string modifiers and integer scales.
* modifiers: NULL terminated array of modifier strings, such as
* { "K", "M", NULL }, for strings like "100KB" or "100MB"
* scales: array of scales for each modifer string, such as
* { 1000, 1000000 }
*/
typedef struct scale_struct {
char **modifers;
uint64_t *scales;
} scale_t;
/*
* pointers to standard scales.
*/
extern scale_t *scale_binary;
extern scale_t *scale_metric;
#define SCALED_MODIFIER_CASE_INSENSITIVE_FLAG 0x01
#define SCALED_UNIT_CASE_INSENSITIVE_FLAG 0x02
#define SCALED_UNIT_OPTIONAL_FLAG 0x04
#define SCALED_PAD_WIDTH_FLAG 0x08
#define SCALED_ALL_FLAGS 0x0F
/*
* 20 characters for UINT64_MAX, 1 character for modifer, 1 character for
* unit, 1 character for NULL, 1 extra.
*/
#define SCALED_STRLEN (24)
#define SCALED_INVALID_MODIFIER 1
#define SCALED_INVALID_UNIT 2
#define SCALED_INVALID_NUMBER 3
#define SCALED_OVERFLOW 4
#define SCALED_UNIT_BYTES "B"
#define SCALED_UNIT_SECONDS "s"
#define SCALED_UNIT_NONE ""
/*
* scaledtouint64
*
* converts a string in one of the forms:
* "[decimal number]][modifier][unit]"
* "[integer number][unit]"
*
* to a uint64. As seen from the two forms, If no modifier is present,
* the number must be an integer.
*
* Inputs:
*
* scaledin: input string containing number string
* scale: pointer to scale_t to describe scaling modifiers and scales
* unit: expected unit string, such as "B", for the number "100MB"
* flags: one of:
* SCALED_MODIFIER_CASE_INSENSITIVE_FLAG
* SCALED_UNIT_CASE_INSENSITIVE_FLAG
* SCALED_UNIT_OPTIONAL_FLAG
* which are pretty self explainatory.
* Outputs:
*
* return value: 0 on success, on errors:
* SCALED_INVALID_NUMBER - string contains no valid number
* SCALED_INVALID_MODIFIER - string has unknown modifier
* SCALED_INVALID_UNIT - string has unknown or missing unit
* SCALED_OVERFLOW - number exceeds MAX_UINT64
*
* uint64out: uint64_t value of input string
* widthout: width of number (not including modifier and unit)
* in the input string. "10.0MB" has a width of 4.
* modiferout: pointer to the string in the modifiers array which
* was found in the input string. If no modifer was
* found, this well be set to NULL;
* unitout: If unit string was present in the input string, this
* will be set to point to unit, otherwise NULL.
*/
int scaledtouint64(char *scaledin, uint64_t *uint64out,
int *widthout, char **modifierout, char **unitout,
scale_t *scale, char *unit, int flags);
/*
* uint64toscaled
*
* converts a uint64 to a string in one of the forms:
* "[decimal number]][modifier][unit]"
* "[integer number][unit]"
* (no modifier means number will be an integer)
*
* Inputs:
*
* uint64in: input number to convert to scaled string
* widthin: character width of desired string, not including modifier
* and unit. Eg: 1.00MB has a width of 4 for the "1.00".
* unit.
* maxmodifier: The maximium scaling to use. For instance, to limit the
* scaling to megabytes (no GB or higher), use "M"
* scale: pointer to scale_t to describe modifiers and scales
* unit: unit string, such as "B", for the number "100MB"
* flags: one of:
* SCALED_PAD_WIDTH_FLAG
* If the length of the scaled string is less than
* widthin, pad to the left with spaces.
* Outputs:
*
* return value: 0 on success, no error conditions.
* scaledout: Pointer to a string buffer to fill with the scaled string.
* widthout: Used to return the actual character length of the produced
* string, not including modifier and unit.
* modifierout: pointer to modifier used in scaled string.
*/
int uint64toscaled(uint64_t uint64in, int widthin, char *maxmodifier,
char *scaledout, int *widthout, char **modifierout,
scale_t *scale, char *unit, int flags);
/*
* scaledtoscaled
*
* Used to rescale a string from/to the following forms:
* "[decimal number]][modifier][unit]"
* "[integer number][unit]"
*
* This is used ensure the desired width and letter casing.
*
* As seen from the two forms, If no modifier is present,
* the number must be an integer.
*
* Inputs:
* scaledin: input string containing number string
* widthin: character width of desired string, not including modifier
* and unit. Eg: 1.00MB has a width of 4 for the "1.00".
* unit.
* maxmodifier: The maximium scaling to use. For instance, to limit the
* scaling to megabytes (no GB or higher), use "M"
* scale: pointer to scale_t to describe modifiers and scales
* unit: unit string, such as "B", for the number "100MB"
* flags: one of:
* SCALED_PAD_WIDTH_FLAG
* If the length of the scaled string is less than
* widthin, pad to the left with spaces.
* SCALED_MODIFIER_CASE_INSENSITIVE_FLAG
* SCALED_UNIT_CASE_INSENSITIVE_FLAG
* SCALED_UNIT_OPTIONAL_FLAG
* which are pretty self explainatory.
*
* Outputs:
*
* return value: 0 on success, on errors:
* SCALED_INVALID_NUMBER - string contains no valid number
* SCALED_INVALID_MODIFIER - string has unknown modifier
* SCALED_INVALID_UNIT - string has unknown or missing unit
* SCALED_OVERFLOW - number exceeds MAX_UINT64
*
* scaledout: Pointer to a string buffer to fill with the scaled string.
* widthout: width of number (not including modifier and unit)
* in the input string. "10.0MB" has a width of 4.
* modiferout: pointer to the string in the modifiers array which
* was found in the input string. If no modifer was
* found, this well be set to NULL;
*/
int scaledtoscaled(char *scaledin, int widthin, char *maxmodifier,
char *scaledout, int *widthout, char ** modifierout,
scale_t *scale, char *unit, int flags);
/*
* scaledeqscaled
*
* Determine if two scaled strings are equivalent. Flags are same as
* scaledtouint64.
*/
int scaledeqscaled(char *scale1, char *scale2,
scale_t *scale, char *unit, int flags);
/*
* scaledequint64
*
* Determine if a scaled number is equal to an uint64. The uint64 is scaled
* to the same scale and width as the scaled strings. If the resultant string
* is equal, then the numbers are considered equal.
*
* minwidth: minimum number width to scale string and number to for
* comparision.
* flags are same as scaledtouint64.
*/
int scaledequint64(char *scaled, uint64_t uint64, int minwidth,
scale_t *scale, char *unit, int flags);
#ifdef __cplusplus
}
#endif
#endif /* _UTILS_H */
|