1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
22413
22414
22415
22416
22417
22418
22419
22420
22421
22422
22423
22424
22425
22426
22427
22428
22429
22430
22431
22432
22433
22434
22435
22436
22437
22438
22439
22440
22441
22442
22443
22444
22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
22461
22462
22463
22464
22465
22466
22467
22468
22469
22470
22471
22472
22473
22474
22475
22476
22477
22478
22479
22480
22481
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
22671
22672
22673
22674
22675
22676
22677
22678
22679
22680
22681
22682
22683
22684
22685
22686
22687
22688
22689
22690
22691
22692
22693
22694
22695
22696
22697
22698
22699
22700
22701
22702
22703
22704
22705
22706
22707
22708
22709
22710
22711
22712
22713
22714
22715
22716
22717
22718
22719
22720
22721
22722
22723
22724
22725
22726
22727
22728
22729
22730
22731
22732
22733
22734
22735
22736
22737
22738
22739
22740
22741
22742
22743
22744
22745
22746
22747
22748
22749
22750
22751
22752
22753
22754
22755
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769
22770
22771
22772
22773
22774
22775
22776
22777
22778
22779
22780
22781
22782
22783
22784
22785
22786
22787
22788
22789
22790
22791
22792
22793
22794
22795
22796
22797
22798
22799
22800
22801
22802
22803
22804
22805
22806
22807
22808
22809
22810
22811
22812
22813
22814
22815
22816
22817
22818
22819
22820
22821
22822
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834
22835
22836
22837
22838
22839
22840
22841
22842
22843
22844
22845
22846
22847
22848
22849
22850
22851
22852
22853
22854
22855
22856
22857
22858
22859
22860
22861
22862
22863
22864
22865
22866
22867
22868
22869
22870
22871
22872
22873
22874
22875
22876
22877
22878
22879
22880
22881
22882
22883
22884
22885
22886
22887
22888
22889
22890
22891
22892
22893
22894
22895
22896
22897
22898
22899
22900
22901
22902
22903
22904
22905
22906
22907
22908
22909
22910
22911
22912
22913
22914
22915
22916
22917
22918
22919
22920
22921
22922
22923
22924
22925
22926
22927
22928
22929
22930
22931
22932
22933
22934
22935
22936
22937
22938
22939
22940
22941
22942
22943
22944
22945
22946
22947
22948
22949
22950
22951
22952
22953
22954
22955
22956
22957
22958
22959
22960
22961
22962
22963
22964
22965
22966
22967
22968
22969
22970
22971
22972
22973
22974
22975
22976
22977
22978
22979
22980
22981
22982
22983
22984
22985
22986
22987
22988
22989
22990
22991
22992
22993
22994
22995
22996
22997
22998
22999
23000
23001
23002
23003
23004
23005
23006
23007
23008
23009
23010
23011
23012
23013
23014
23015
23016
23017
23018
23019
23020
23021
23022
23023
23024
23025
23026
23027
23028
23029
23030
23031
23032
23033
23034
23035
23036
23037
23038
23039
23040
23041
23042
23043
23044
23045
23046
23047
23048
23049
23050
23051
23052
23053
23054
23055
23056
23057
23058
23059
23060
23061
23062
23063
23064
23065
23066
23067
23068
23069
23070
23071
23072
23073
23074
23075
23076
23077
23078
23079
23080
23081
23082
23083
23084
23085
23086
23087
23088
23089
23090
23091
23092
23093
23094
23095
23096
23097
23098
23099
23100
23101
23102
23103
23104
23105
23106
23107
23108
23109
23110
23111
23112
23113
23114
23115
23116
23117
23118
23119
23120
23121
23122
23123
23124
23125
23126
23127
23128
23129
23130
23131
23132
23133
23134
23135
23136
23137
23138
23139
23140
23141
23142
23143
23144
23145
23146
23147
23148
23149
23150
23151
23152
23153
23154
23155
23156
23157
23158
23159
23160
23161
23162
23163
23164
23165
23166
23167
23168
23169
23170
23171
23172
23173
23174
23175
23176
23177
23178
23179
23180
23181
23182
23183
23184
23185
23186
23187
23188
23189
23190
23191
23192
23193
23194
23195
23196
23197
23198
23199
23200
23201
23202
23203
23204
23205
23206
23207
23208
23209
23210
23211
23212
23213
23214
23215
23216
23217
23218
23219
23220
23221
23222
23223
23224
23225
23226
23227
23228
23229
23230
23231
23232
23233
23234
23235
23236
23237
23238
23239
23240
23241
23242
23243
23244
23245
23246
23247
23248
23249
23250
23251
23252
23253
23254
23255
23256
23257
23258
23259
23260
23261
23262
23263
23264
23265
23266
23267
23268
23269
23270
23271
23272
23273
23274
23275
23276
23277
23278
23279
23280
23281
23282
23283
23284
23285
23286
23287
23288
23289
23290
23291
23292
23293
23294
23295
23296
23297
23298
23299
23300
23301
23302
23303
23304
23305
23306
23307
23308
23309
23310
23311
23312
23313
23314
23315
23316
23317
23318
23319
23320
23321
23322
23323
23324
23325
23326
23327
23328
23329
23330
23331
23332
23333
23334
23335
23336
23337
23338
23339
23340
23341
23342
23343
23344
23345
23346
23347
23348
23349
23350
23351
23352
23353
23354
23355
23356
23357
23358
23359
23360
23361
23362
23363
23364
23365
23366
23367
23368
23369
23370
23371
23372
23373
23374
23375
23376
23377
23378
23379
23380
23381
23382
23383
23384
23385
23386
23387
23388
23389
23390
23391
23392
23393
23394
23395
23396
23397
23398
23399
23400
23401
23402
23403
23404
23405
23406
23407
23408
23409
23410
23411
23412
23413
23414
23415
23416
23417
23418
23419
23420
23421
23422
23423
23424
23425
23426
23427
23428
23429
23430
23431
23432
23433
23434
23435
23436
23437
23438
23439
23440
23441
23442
23443
23444
23445
23446
23447
23448
23449
23450
23451
23452
23453
23454
23455
23456
23457
23458
23459
23460
23461
23462
23463
23464
23465
23466
23467
23468
23469
23470
23471
23472
23473
23474
23475
23476
23477
23478
23479
23480
23481
23482
23483
23484
23485
23486
23487
23488
23489
23490
23491
23492
23493
23494
23495
23496
23497
23498
23499
23500
23501
23502
23503
23504
23505
23506
23507
23508
23509
23510
23511
23512
23513
23514
23515
23516
23517
23518
23519
23520
23521
23522
23523
23524
23525
23526
23527
23528
23529
23530
23531
23532
23533
23534
23535
23536
23537
23538
23539
23540
23541
23542
23543
23544
23545
23546
23547
23548
23549
23550
23551
23552
23553
23554
23555
23556
23557
23558
23559
23560
23561
23562
23563
23564
23565
23566
23567
23568
23569
23570
23571
23572
23573
23574
23575
23576
23577
23578
23579
23580
23581
23582
23583
23584
23585
23586
23587
23588
23589
23590
23591
23592
23593
23594
23595
23596
23597
23598
23599
23600
23601
23602
23603
23604
23605
23606
23607
23608
23609
23610
23611
23612
23613
23614
23615
23616
23617
23618
23619
23620
23621
23622
23623
23624
23625
23626
23627
23628
23629
23630
23631
23632
23633
23634
23635
23636
23637
23638
23639
23640
23641
23642
23643
23644
23645
23646
23647
23648
23649
23650
23651
23652
23653
23654
23655
23656
23657
23658
23659
23660
23661
23662
23663
23664
23665
23666
23667
23668
23669
23670
23671
23672
23673
23674
23675
23676
23677
23678
23679
23680
23681
23682
23683
23684
23685
23686
23687
23688
23689
23690
23691
23692
23693
23694
23695
23696
23697
23698
23699
23700
23701
23702
23703
23704
23705
23706
23707
23708
23709
23710
23711
23712
23713
23714
23715
23716
23717
23718
23719
23720
23721
23722
23723
23724
23725
23726
23727
23728
23729
23730
23731
23732
23733
23734
23735
23736
23737
23738
23739
23740
23741
23742
23743
23744
23745
23746
23747
23748
23749
23750
23751
23752
23753
23754
23755
23756
23757
23758
23759
23760
23761
23762
23763
23764
23765
23766
23767
23768
23769
23770
23771
23772
23773
23774
23775
23776
23777
23778
23779
23780
23781
23782
23783
23784
23785
23786
23787
23788
23789
23790
23791
23792
23793
23794
23795
23796
23797
23798
23799
23800
23801
23802
23803
23804
23805
23806
23807
23808
23809
23810
23811
23812
23813
23814
23815
23816
23817
23818
23819
23820
23821
23822
23823
23824
23825
23826
23827
23828
23829
23830
23831
23832
23833
23834
23835
23836
23837
23838
23839
23840
23841
23842
23843
23844
23845
23846
23847
23848
23849
23850
23851
23852
23853
23854
23855
23856
23857
23858
23859
23860
23861
23862
23863
23864
23865
23866
23867
23868
23869
23870
23871
23872
23873
23874
23875
23876
23877
23878
23879
23880
23881
23882
23883
23884
23885
23886
23887
23888
23889
23890
23891
23892
23893
23894
23895
23896
23897
23898
23899
23900
23901
23902
23903
23904
23905
23906
23907
23908
23909
23910
23911
23912
23913
23914
23915
23916
23917
23918
23919
23920
23921
23922
23923
23924
23925
23926
23927
23928
23929
23930
23931
23932
23933
23934
23935
23936
23937
23938
23939
23940
23941
23942
23943
23944
23945
23946
23947
23948
23949
23950
23951
23952
23953
23954
23955
23956
23957
23958
23959
23960
23961
23962
23963
23964
23965
23966
23967
23968
23969
23970
23971
23972
23973
23974
23975
23976
23977
23978
23979
23980
23981
23982
23983
23984
23985
23986
23987
23988
23989
23990
23991
23992
23993
23994
23995
23996
23997
23998
23999
24000
24001
24002
24003
24004
24005
24006
24007
24008
24009
24010
24011
24012
24013
24014
24015
24016
24017
24018
24019
24020
24021
24022
24023
24024
24025
24026
24027
24028
24029
24030
24031
24032
24033
24034
24035
24036
24037
24038
24039
24040
24041
24042
24043
24044
24045
24046
24047
24048
24049
24050
24051
24052
24053
24054
24055
24056
24057
24058
24059
24060
24061
24062
24063
24064
24065
24066
24067
24068
24069
24070
24071
24072
24073
24074
24075
24076
24077
24078
24079
24080
24081
24082
24083
24084
24085
24086
24087
24088
24089
24090
24091
24092
24093
24094
24095
24096
24097
24098
24099
24100
24101
24102
24103
24104
24105
24106
24107
24108
24109
24110
24111
24112
24113
24114
24115
24116
24117
24118
24119
24120
24121
24122
24123
24124
24125
24126
24127
24128
24129
24130
24131
24132
24133
24134
24135
24136
24137
24138
24139
24140
24141
24142
24143
24144
24145
24146
24147
24148
24149
24150
24151
24152
24153
24154
24155
24156
24157
24158
24159
24160
24161
24162
24163
24164
24165
24166
24167
24168
24169
24170
24171
24172
24173
24174
24175
24176
24177
24178
24179
24180
24181
24182
24183
24184
24185
24186
24187
24188
24189
24190
24191
24192
24193
24194
24195
24196
24197
24198
24199
24200
24201
24202
24203
24204
24205
24206
24207
24208
24209
24210
24211
24212
24213
24214
24215
24216
24217
24218
24219
24220
24221
24222
24223
24224
24225
24226
24227
24228
24229
24230
24231
24232
24233
24234
24235
24236
24237
24238
24239
24240
24241
24242
24243
24244
24245
24246
24247
24248
24249
24250
24251
24252
24253
24254
24255
24256
24257
24258
24259
24260
24261
24262
24263
24264
24265
24266
24267
24268
24269
24270
24271
24272
24273
24274
24275
24276
24277
24278
24279
24280
24281
24282
24283
24284
24285
24286
24287
24288
24289
24290
24291
24292
24293
24294
24295
24296
24297
24298
24299
24300
24301
24302
24303
24304
24305
24306
24307
24308
24309
24310
24311
24312
24313
24314
24315
24316
24317
24318
24319
24320
24321
24322
24323
24324
24325
24326
24327
24328
24329
24330
24331
24332
24333
24334
24335
24336
24337
24338
24339
24340
24341
24342
24343
24344
24345
24346
24347
24348
24349
24350
24351
24352
24353
24354
24355
24356
24357
24358
24359
24360
24361
24362
24363
24364
24365
24366
24367
24368
24369
24370
24371
24372
24373
24374
24375
24376
24377
24378
24379
24380
24381
24382
24383
24384
24385
24386
24387
24388
24389
24390
24391
24392
24393
24394
24395
24396
24397
24398
24399
24400
24401
24402
24403
24404
24405
24406
24407
24408
24409
24410
24411
24412
24413
24414
24415
24416
24417
24418
24419
24420
24421
24422
24423
24424
24425
24426
24427
24428
24429
24430
24431
24432
24433
24434
24435
24436
24437
24438
24439
24440
24441
24442
24443
24444
24445
24446
24447
24448
24449
24450
24451
24452
24453
24454
24455
24456
24457
24458
24459
24460
24461
24462
24463
24464
24465
24466
24467
24468
24469
24470
24471
24472
24473
24474
24475
24476
24477
24478
24479
24480
24481
24482
24483
24484
24485
24486
24487
24488
24489
24490
24491
24492
24493
24494
24495
24496
24497
24498
24499
24500
24501
24502
24503
24504
24505
24506
24507
24508
24509
24510
24511
24512
24513
24514
24515
24516
24517
24518
24519
24520
24521
24522
24523
24524
24525
24526
24527
24528
24529
24530
24531
24532
24533
24534
24535
24536
24537
24538
24539
24540
24541
24542
24543
24544
24545
24546
24547
24548
24549
24550
24551
24552
24553
24554
24555
24556
24557
24558
24559
24560
24561
24562
24563
24564
24565
24566
24567
24568
24569
24570
24571
24572
24573
24574
24575
24576
24577
24578
24579
24580
24581
24582
24583
24584
24585
24586
24587
24588
24589
24590
24591
24592
24593
24594
24595
24596
24597
24598
24599
24600
24601
24602
24603
24604
24605
24606
24607
24608
24609
24610
24611
24612
24613
24614
24615
24616
24617
24618
24619
24620
24621
24622
24623
24624
24625
24626
24627
24628
24629
24630
24631
24632
24633
24634
24635
24636
24637
24638
24639
24640
24641
24642
24643
24644
24645
24646
24647
24648
24649
24650
24651
24652
24653
24654
24655
24656
24657
24658
24659
24660
24661
24662
24663
24664
24665
24666
24667
24668
24669
24670
24671
24672
24673
24674
24675
24676
24677
24678
24679
24680
24681
24682
24683
24684
24685
24686
24687
24688
24689
24690
24691
24692
24693
24694
24695
24696
24697
24698
24699
24700
24701
24702
24703
24704
24705
24706
24707
24708
24709
24710
24711
24712
24713
24714
24715
24716
24717
24718
24719
24720
24721
24722
24723
24724
24725
24726
24727
24728
24729
24730
24731
24732
24733
24734
24735
24736
24737
24738
24739
24740
24741
24742
24743
24744
24745
24746
24747
24748
24749
24750
24751
24752
24753
24754
24755
24756
24757
24758
24759
24760
24761
24762
24763
24764
24765
24766
24767
24768
24769
24770
24771
24772
24773
24774
24775
24776
24777
24778
24779
24780
24781
24782
24783
24784
24785
24786
24787
24788
24789
24790
24791
24792
24793
24794
24795
24796
24797
24798
24799
24800
24801
24802
24803
24804
24805
24806
24807
24808
24809
24810
24811
24812
24813
24814
24815
24816
24817
24818
24819
24820
24821
24822
24823
24824
24825
24826
24827
24828
24829
24830
24831
24832
24833
24834
24835
24836
24837
24838
24839
24840
24841
24842
24843
24844
24845
24846
24847
24848
24849
24850
24851
24852
24853
24854
24855
24856
24857
24858
24859
24860
24861
24862
24863
24864
24865
24866
24867
24868
24869
24870
24871
24872
24873
24874
24875
24876
24877
24878
24879
24880
24881
24882
24883
24884
24885
24886
24887
24888
24889
24890
24891
24892
24893
24894
24895
24896
24897
24898
24899
24900
24901
24902
24903
24904
24905
24906
24907
24908
24909
24910
24911
24912
24913
24914
24915
24916
24917
24918
24919
24920
24921
24922
24923
24924
24925
24926
24927
24928
24929
24930
24931
24932
24933
24934
24935
24936
24937
24938
24939
24940
24941
24942
24943
24944
24945
24946
24947
24948
24949
24950
24951
24952
24953
24954
24955
24956
24957
24958
24959
24960
24961
24962
24963
24964
24965
24966
24967
24968
24969
24970
24971
24972
24973
24974
24975
24976
24977
24978
24979
24980
24981
24982
24983
24984
24985
24986
24987
24988
24989
24990
24991
24992
24993
24994
24995
24996
24997
24998
24999
25000
25001
25002
25003
25004
25005
25006
25007
25008
25009
25010
25011
25012
25013
25014
25015
25016
25017
25018
25019
25020
25021
25022
25023
25024
25025
25026
25027
25028
25029
25030
25031
25032
25033
25034
25035
25036
25037
25038
25039
25040
25041
25042
25043
25044
25045
25046
25047
25048
25049
25050
25051
25052
25053
25054
25055
25056
25057
25058
25059
25060
25061
25062
25063
25064
25065
25066
25067
25068
25069
25070
25071
25072
25073
25074
25075
25076
25077
25078
25079
25080
25081
25082
25083
25084
25085
25086
25087
25088
25089
25090
25091
25092
25093
25094
25095
25096
25097
25098
25099
25100
25101
25102
25103
25104
25105
25106
25107
25108
25109
25110
25111
25112
25113
25114
25115
25116
25117
25118
25119
25120
25121
25122
25123
25124
25125
25126
25127
25128
25129
25130
25131
25132
25133
25134
25135
25136
25137
25138
25139
25140
25141
25142
25143
25144
25145
25146
25147
25148
25149
25150
25151
25152
25153
25154
25155
25156
25157
25158
25159
25160
25161
25162
25163
25164
25165
25166
25167
25168
25169
25170
25171
25172
25173
25174
25175
25176
25177
25178
25179
25180
25181
25182
25183
25184
25185
25186
25187
25188
25189
25190
25191
25192
25193
25194
25195
25196
25197
25198
25199
25200
25201
25202
25203
25204
25205
25206
25207
25208
25209
25210
25211
25212
25213
25214
25215
25216
25217
25218
25219
25220
25221
25222
25223
25224
25225
25226
25227
25228
25229
25230
25231
25232
25233
25234
25235
25236
25237
25238
25239
25240
25241
25242
25243
25244
25245
25246
25247
25248
25249
25250
25251
25252
25253
25254
25255
25256
25257
25258
25259
25260
25261
25262
25263
25264
25265
25266
25267
25268
25269
25270
25271
25272
25273
25274
25275
25276
25277
25278
25279
25280
25281
25282
25283
25284
25285
25286
25287
25288
25289
25290
25291
25292
25293
25294
25295
25296
25297
25298
25299
25300
25301
25302
25303
25304
25305
25306
25307
25308
25309
25310
25311
25312
25313
25314
25315
25316
25317
25318
25319
25320
25321
25322
25323
25324
25325
25326
25327
25328
25329
25330
25331
25332
25333
25334
25335
25336
25337
25338
25339
25340
25341
25342
25343
25344
25345
25346
25347
25348
25349
25350
25351
25352
25353
25354
25355
25356
25357
25358
25359
25360
25361
25362
25363
25364
25365
25366
25367
25368
25369
25370
25371
25372
25373
25374
25375
25376
25377
25378
25379
25380
25381
25382
25383
25384
25385
25386
25387
25388
25389
25390
25391
25392
25393
25394
25395
25396
25397
25398
25399
25400
25401
25402
25403
25404
25405
25406
25407
25408
25409
25410
25411
25412
25413
25414
25415
25416
25417
25418
25419
25420
25421
25422
25423
25424
25425
25426
25427
25428
25429
25430
25431
25432
25433
25434
25435
25436
25437
25438
25439
25440
25441
25442
25443
25444
25445
25446
25447
25448
25449
25450
25451
25452
25453
25454
25455
25456
25457
25458
25459
25460
25461
25462
25463
25464
25465
25466
25467
25468
25469
25470
25471
25472
25473
25474
25475
25476
25477
25478
25479
25480
25481
25482
25483
25484
25485
25486
25487
25488
25489
25490
25491
25492
25493
25494
25495
25496
25497
25498
25499
25500
25501
25502
25503
25504
25505
25506
25507
25508
25509
25510
25511
25512
25513
25514
25515
25516
25517
25518
25519
25520
25521
25522
25523
25524
25525
25526
25527
25528
25529
25530
25531
25532
25533
25534
25535
25536
25537
25538
25539
25540
25541
25542
25543
25544
25545
25546
25547
25548
25549
25550
25551
25552
25553
25554
25555
25556
25557
25558
25559
25560
25561
25562
25563
25564
25565
25566
25567
25568
25569
25570
25571
25572
25573
25574
25575
25576
25577
25578
25579
25580
25581
25582
25583
25584
25585
25586
25587
25588
25589
25590
25591
25592
25593
25594
25595
25596
25597
25598
25599
25600
25601
25602
25603
25604
25605
25606
25607
25608
25609
25610
25611
25612
25613
25614
25615
25616
25617
25618
25619
25620
25621
25622
25623
25624
25625
25626
25627
25628
25629
25630
25631
25632
25633
25634
25635
25636
25637
25638
25639
25640
25641
25642
25643
25644
25645
25646
25647
25648
25649
25650
25651
25652
25653
25654
25655
25656
25657
25658
25659
25660
25661
25662
25663
25664
25665
25666
25667
25668
25669
25670
25671
25672
25673
25674
25675
25676
25677
25678
25679
25680
25681
25682
25683
25684
25685
25686
25687
25688
25689
25690
25691
25692
25693
25694
25695
25696
25697
25698
25699
25700
25701
25702
25703
25704
25705
25706
25707
25708
25709
25710
25711
25712
25713
25714
25715
25716
25717
25718
25719
25720
25721
25722
25723
25724
25725
25726
25727
25728
25729
25730
25731
25732
25733
25734
25735
25736
25737
25738
25739
25740
25741
25742
25743
25744
25745
25746
25747
25748
25749
25750
25751
25752
25753
25754
25755
25756
25757
25758
25759
25760
25761
25762
25763
25764
25765
25766
25767
25768
25769
25770
25771
25772
25773
25774
25775
25776
25777
25778
25779
25780
25781
25782
25783
25784
25785
25786
25787
25788
25789
25790
25791
25792
25793
25794
25795
25796
25797
25798
25799
25800
25801
25802
25803
25804
25805
25806
25807
25808
25809
25810
25811
25812
25813
25814
25815
25816
25817
25818
25819
25820
25821
25822
25823
25824
25825
25826
25827
25828
25829
25830
25831
25832
25833
25834
25835
25836
25837
25838
25839
25840
25841
25842
25843
25844
25845
25846
25847
25848
25849
25850
25851
25852
25853
25854
25855
25856
25857
25858
25859
25860
25861
25862
25863
25864
25865
25866
25867
25868
25869
25870
25871
25872
25873
25874
25875
25876
25877
25878
25879
25880
25881
25882
25883
25884
25885
25886
25887
25888
25889
25890
25891
25892
25893
25894
25895
25896
25897
25898
25899
25900
25901
25902
25903
25904
25905
25906
25907
25908
25909
25910
25911
25912
25913
25914
25915
25916
25917
25918
25919
25920
25921
25922
25923
25924
25925
25926
25927
25928
25929
25930
25931
25932
25933
25934
25935
25936
25937
25938
25939
25940
25941
25942
25943
25944
25945
25946
25947
25948
25949
25950
25951
25952
25953
25954
25955
25956
25957
25958
25959
25960
25961
25962
25963
25964
25965
25966
25967
25968
25969
25970
25971
25972
25973
25974
25975
25976
25977
25978
25979
25980
25981
25982
25983
25984
25985
25986
25987
25988
25989
25990
25991
25992
25993
25994
25995
25996
25997
25998
25999
26000
26001
26002
26003
26004
26005
26006
26007
26008
26009
26010
26011
26012
26013
26014
26015
26016
26017
26018
26019
26020
26021
26022
26023
26024
26025
26026
26027
26028
26029
26030
26031
26032
26033
26034
26035
26036
26037
26038
26039
26040
26041
26042
26043
26044
26045
26046
26047
26048
26049
26050
26051
26052
26053
26054
26055
26056
26057
26058
26059
26060
26061
26062
26063
26064
26065
26066
26067
26068
26069
26070
26071
26072
26073
26074
26075
26076
26077
26078
26079
26080
26081
26082
26083
26084
26085
26086
26087
26088
26089
26090
26091
26092
26093
26094
26095
26096
26097
26098
26099
26100
26101
26102
26103
26104
26105
26106
26107
26108
26109
26110
26111
26112
26113
26114
26115
26116
26117
26118
26119
26120
26121
26122
26123
26124
26125
26126
26127
26128
26129
26130
26131
26132
26133
26134
26135
26136
26137
26138
26139
26140
26141
26142
26143
26144
26145
26146
26147
26148
26149
26150
26151
26152
26153
26154
26155
26156
26157
26158
26159
26160
26161
26162
26163
26164
26165
26166
26167
26168
26169
26170
26171
26172
26173
26174
26175
26176
26177
26178
26179
26180
26181
26182
26183
26184
26185
26186
26187
26188
26189
26190
26191
26192
26193
26194
26195
26196
26197
26198
26199
26200
26201
26202
26203
26204
26205
26206
26207
26208
26209
26210
26211
26212
26213
26214
26215
26216
26217
26218
26219
26220
26221
26222
26223
26224
26225
26226
26227
26228
26229
26230
26231
26232
26233
26234
26235
26236
26237
26238
26239
26240
26241
26242
26243
26244
26245
26246
26247
26248
26249
26250
26251
26252
26253
26254
26255
26256
26257
26258
26259
26260
26261
26262
26263
26264
26265
26266
26267
26268
26269
26270
26271
26272
26273
26274
26275
26276
26277
26278
26279
26280
26281
26282
26283
26284
26285
26286
26287
26288
26289
26290
26291
26292
26293
26294
26295
26296
26297
26298
26299
26300
26301
26302
26303
26304
26305
26306
26307
26308
26309
26310
26311
26312
26313
26314
26315
26316
26317
26318
26319
26320
26321
26322
26323
26324
26325
26326
26327
26328
26329
26330
26331
26332
26333
26334
26335
26336
26337
26338
26339
26340
26341
26342
26343
26344
26345
26346
26347
26348
26349
26350
26351
26352
26353
26354
26355
26356
26357
26358
26359
26360
26361
26362
26363
26364
26365
26366
26367
26368
26369
26370
26371
26372
26373
26374
26375
26376
26377
26378
26379
26380
26381
26382
26383
26384
26385
26386
26387
26388
26389
26390
26391
26392
26393
26394
26395
26396
26397
26398
26399
26400
26401
26402
26403
26404
26405
26406
26407
26408
26409
26410
26411
26412
26413
26414
26415
26416
26417
26418
26419
26420
26421
26422
26423
26424
26425
26426
26427
26428
26429
26430
26431
26432
26433
26434
26435
26436
26437
26438
26439
26440
26441
26442
26443
26444
26445
26446
26447
26448
26449
26450
26451
26452
26453
26454
26455
26456
26457
26458
26459
26460
26461
26462
26463
26464
26465
26466
26467
26468
26469
26470
26471
26472
26473
26474
26475
26476
26477
26478
26479
26480
26481
26482
26483
26484
26485
26486
26487
26488
26489
26490
26491
26492
26493
26494
26495
26496
26497
26498
26499
26500
26501
26502
26503
26504
26505
26506
26507
26508
26509
26510
26511
26512
26513
26514
26515
26516
26517
26518
26519
26520
26521
26522
26523
26524
26525
26526
26527
26528
26529
26530
26531
26532
26533
26534
26535
26536
26537
26538
26539
26540
26541
26542
26543
26544
26545
26546
26547
26548
26549
26550
26551
26552
26553
26554
26555
26556
26557
26558
26559
26560
26561
26562
26563
26564
26565
26566
26567
26568
26569
26570
26571
26572
26573
26574
26575
26576
26577
26578
26579
26580
26581
26582
26583
26584
26585
26586
26587
26588
26589
26590
26591
26592
26593
26594
26595
26596
26597
26598
26599
26600
26601
26602
26603
26604
26605
26606
26607
26608
26609
26610
26611
26612
26613
26614
26615
26616
26617
26618
26619
26620
26621
26622
26623
26624
26625
26626
26627
26628
26629
26630
26631
26632
26633
26634
26635
26636
26637
26638
26639
26640
26641
26642
26643
26644
26645
26646
26647
26648
26649
26650
26651
26652
26653
26654
26655
26656
26657
26658
26659
26660
26661
26662
26663
26664
26665
26666
26667
26668
26669
26670
26671
26672
26673
26674
26675
26676
26677
26678
26679
26680
26681
26682
26683
26684
26685
26686
26687
26688
26689
26690
26691
26692
26693
26694
26695
26696
26697
26698
26699
26700
26701
26702
26703
26704
26705
26706
26707
26708
26709
26710
26711
26712
26713
26714
26715
26716
26717
26718
26719
26720
26721
26722
26723
26724
26725
26726
26727
26728
26729
26730
26731
26732
26733
26734
26735
26736
26737
26738
26739
26740
26741
26742
26743
26744
26745
26746
26747
26748
26749
26750
26751
26752
26753
26754
26755
26756
26757
26758
26759
26760
26761
26762
26763
26764
26765
26766
26767
26768
26769
26770
26771
26772
26773
26774
26775
26776
26777
26778
26779
26780
26781
26782
26783
26784
26785
26786
26787
26788
26789
26790
26791
26792
26793
26794
26795
26796
26797
26798
26799
26800
26801
26802
26803
26804
26805
26806
26807
26808
26809
26810
26811
26812
26813
26814
26815
26816
26817
26818
26819
26820
26821
26822
26823
26824
26825
26826
26827
26828
26829
26830
26831
26832
26833
26834
26835
26836
26837
26838
26839
26840
26841
26842
26843
26844
26845
26846
26847
26848
26849
26850
26851
26852
26853
26854
26855
26856
26857
26858
26859
26860
26861
26862
26863
26864
26865
26866
26867
26868
26869
26870
26871
26872
26873
26874
26875
26876
26877
26878
26879
26880
26881
26882
26883
26884
26885
26886
26887
26888
26889
26890
26891
26892
26893
26894
26895
26896
26897
26898
26899
26900
26901
26902
26903
26904
26905
26906
26907
26908
26909
26910
26911
26912
26913
26914
26915
26916
26917
26918
26919
26920
26921
26922
26923
26924
26925
26926
26927
26928
26929
26930
26931
26932
26933
26934
26935
26936
26937
26938
26939
26940
26941
26942
26943
26944
26945
26946
26947
26948
26949
26950
26951
26952
26953
26954
26955
26956
26957
26958
26959
26960
26961
26962
26963
26964
26965
26966
26967
26968
26969
26970
26971
26972
26973
26974
26975
26976
26977
26978
26979
26980
26981
26982
26983
26984
26985
26986
26987
26988
26989
26990
26991
26992
26993
26994
26995
26996
26997
26998
26999
27000
27001
27002
27003
27004
27005
27006
27007
27008
27009
27010
27011
27012
27013
27014
27015
27016
27017
27018
27019
27020
27021
27022
27023
27024
27025
27026
27027
27028
27029
27030
27031
27032
27033
27034
27035
27036
27037
27038
27039
27040
27041
27042
27043
27044
27045
27046
27047
27048
27049
27050
27051
27052
27053
27054
27055
27056
27057
27058
27059
27060
27061
27062
27063
27064
27065
27066
27067
27068
27069
27070
27071
27072
27073
27074
27075
27076
27077
27078
27079
27080
27081
27082
27083
27084
27085
27086
27087
27088
27089
27090
27091
27092
27093
27094
27095
27096
27097
27098
27099
27100
27101
27102
27103
27104
27105
27106
27107
27108
27109
27110
27111
27112
27113
27114
27115
27116
27117
27118
27119
27120
27121
27122
27123
27124
27125
27126
27127
27128
27129
27130
27131
27132
27133
27134
27135
27136
27137
27138
27139
27140
27141
27142
27143
27144
27145
27146
27147
27148
27149
27150
27151
27152
27153
27154
27155
27156
27157
27158
27159
27160
27161
27162
27163
27164
27165
27166
27167
27168
27169
27170
27171
27172
27173
27174
27175
27176
27177
27178
27179
27180
27181
27182
27183
27184
27185
27186
27187
27188
27189
27190
27191
27192
27193
27194
27195
27196
27197
27198
27199
27200
27201
27202
27203
27204
27205
27206
27207
27208
27209
27210
27211
27212
27213
27214
27215
27216
27217
27218
27219
27220
27221
27222
27223
27224
27225
27226
27227
27228
27229
27230
27231
27232
27233
27234
27235
27236
27237
27238
27239
27240
27241
27242
27243
27244
27245
27246
27247
27248
27249
27250
27251
27252
27253
27254
27255
27256
27257
27258
27259
27260
27261
27262
27263
27264
27265
27266
27267
27268
27269
27270
27271
27272
27273
27274
27275
27276
27277
27278
27279
27280
27281
27282
27283
27284
27285
27286
27287
27288
27289
27290
27291
27292
27293
27294
27295
27296
27297
27298
27299
27300
27301
27302
27303
27304
27305
27306
27307
27308
27309
27310
27311
27312
27313
27314
27315
27316
27317
27318
27319
27320
27321
27322
27323
27324
27325
27326
27327
27328
27329
27330
27331
27332
27333
27334
27335
27336
27337
27338
27339
27340
27341
27342
27343
27344
27345
27346
27347
27348
27349
27350
27351
27352
27353
27354
27355
27356
27357
27358
27359
27360
27361
27362
27363
27364
27365
27366
27367
27368
27369
27370
27371
27372
27373
27374
27375
27376
27377
27378
27379
27380
27381
27382
27383
27384
27385
27386
27387
27388
27389
27390
27391
27392
27393
27394
27395
27396
27397
27398
27399
27400
27401
27402
27403
27404
27405
27406
27407
27408
27409
27410
27411
27412
27413
27414
27415
27416
27417
27418
27419
27420
27421
27422
27423
27424
27425
27426
27427
27428
27429
27430
27431
27432
27433
27434
27435
27436
27437
27438
27439
27440
27441
27442
27443
27444
27445
27446
27447
27448
27449
27450
27451
27452
27453
27454
27455
27456
27457
27458
27459
27460
27461
27462
27463
27464
27465
27466
27467
27468
27469
27470
27471
27472
27473
27474
27475
27476
27477
27478
27479
27480
27481
27482
27483
27484
27485
27486
27487
27488
27489
27490
27491
27492
27493
27494
27495
27496
27497
27498
27499
27500
27501
27502
27503
27504
27505
27506
27507
27508
27509
27510
27511
27512
27513
27514
27515
27516
27517
27518
27519
27520
27521
27522
27523
27524
27525
27526
27527
27528
27529
27530
27531
27532
27533
27534
27535
27536
27537
27538
27539
27540
27541
27542
27543
27544
27545
27546
27547
27548
27549
27550
27551
27552
27553
27554
27555
27556
27557
27558
27559
27560
27561
27562
27563
27564
27565
27566
27567
27568
27569
27570
27571
27572
27573
27574
27575
27576
27577
27578
27579
27580
27581
27582
27583
27584
27585
27586
27587
27588
27589
27590
27591
27592
27593
27594
27595
27596
27597
27598
27599
27600
27601
27602
27603
27604
27605
27606
27607
27608
27609
27610
27611
27612
27613
27614
27615
27616
27617
27618
27619
27620
27621
27622
27623
27624
27625
27626
27627
27628
27629
27630
27631
27632
27633
27634
27635
27636
27637
27638
27639
27640
27641
27642
27643
27644
27645
27646
27647
27648
27649
27650
27651
27652
27653
27654
27655
27656
27657
27658
27659
27660
27661
27662
27663
27664
27665
27666
27667
27668
27669
27670
27671
27672
27673
27674
27675
27676
27677
27678
27679
27680
27681
27682
27683
27684
27685
27686
27687
27688
27689
27690
27691
27692
27693
27694
27695
27696
27697
27698
27699
27700
27701
27702
27703
27704
27705
27706
27707
27708
27709
27710
27711
27712
27713
27714
27715
27716
27717
27718
27719
27720
27721
27722
27723
27724
27725
27726
27727
27728
27729
27730
27731
27732
27733
27734
27735
27736
27737
27738
27739
27740
27741
27742
27743
27744
27745
27746
27747
27748
27749
27750
27751
27752
27753
27754
27755
27756
27757
27758
27759
27760
27761
27762
27763
27764
27765
27766
27767
27768
27769
27770
27771
27772
27773
27774
27775
27776
27777
27778
27779
27780
27781
27782
27783
27784
27785
27786
27787
27788
27789
27790
27791
27792
27793
27794
27795
27796
27797
27798
27799
27800
27801
27802
27803
27804
27805
27806
27807
27808
27809
27810
27811
27812
27813
27814
27815
27816
27817
27818
27819
27820
27821
27822
27823
27824
27825
27826
27827
27828
27829
27830
27831
27832
27833
27834
27835
27836
27837
27838
27839
27840
27841
27842
27843
27844
27845
27846
27847
27848
27849
27850
27851
27852
27853
27854
27855
27856
27857
27858
27859
27860
27861
27862
27863
27864
27865
27866
27867
27868
27869
27870
27871
27872
27873
27874
27875
27876
27877
27878
27879
27880
27881
27882
27883
27884
27885
27886
27887
27888
27889
27890
27891
27892
27893
27894
27895
27896
27897
27898
27899
27900
27901
27902
27903
27904
27905
27906
27907
27908
27909
27910
27911
27912
27913
27914
27915
27916
27917
27918
27919
27920
27921
27922
27923
27924
27925
27926
27927
27928
27929
27930
27931
27932
27933
27934
27935
27936
27937
27938
27939
27940
27941
27942
27943
27944
27945
27946
27947
27948
27949
27950
27951
27952
27953
27954
27955
27956
27957
27958
27959
27960
27961
27962
27963
27964
27965
27966
27967
27968
27969
27970
27971
27972
27973
27974
27975
27976
27977
27978
27979
27980
27981
27982
27983
27984
27985
27986
27987
27988
27989
27990
27991
27992
27993
27994
27995
27996
27997
27998
27999
28000
28001
28002
28003
28004
28005
28006
28007
28008
28009
28010
28011
28012
28013
28014
28015
28016
28017
28018
28019
28020
28021
28022
28023
28024
28025
28026
28027
28028
28029
28030
28031
28032
28033
28034
28035
28036
28037
28038
28039
28040
28041
28042
28043
28044
28045
28046
28047
28048
28049
28050
28051
28052
28053
28054
28055
28056
28057
28058
28059
28060
28061
28062
28063
28064
28065
28066
28067
28068
28069
28070
28071
28072
28073
28074
28075
28076
28077
28078
28079
28080
28081
28082
28083
28084
28085
28086
28087
28088
28089
28090
28091
28092
28093
28094
28095
28096
28097
28098
28099
28100
28101
28102
28103
28104
28105
28106
28107
28108
28109
28110
28111
28112
28113
28114
28115
28116
28117
28118
28119
28120
28121
28122
28123
28124
28125
28126
28127
28128
28129
28130
28131
28132
28133
28134
28135
28136
28137
28138
28139
28140
28141
28142
28143
28144
28145
28146
28147
28148
28149
28150
28151
28152
28153
28154
28155
28156
28157
28158
28159
28160
28161
28162
28163
28164
28165
28166
28167
28168
28169
28170
28171
28172
28173
28174
28175
28176
28177
28178
28179
28180
28181
28182
28183
28184
28185
28186
28187
28188
28189
28190
28191
28192
28193
28194
28195
28196
28197
28198
28199
28200
28201
28202
28203
28204
28205
28206
28207
28208
28209
28210
28211
28212
28213
28214
28215
28216
28217
28218
28219
28220
28221
28222
28223
28224
28225
28226
28227
28228
28229
28230
28231
28232
28233
28234
28235
28236
28237
28238
28239
28240
28241
28242
28243
28244
28245
28246
28247
28248
28249
28250
28251
28252
28253
28254
28255
28256
28257
28258
28259
28260
28261
28262
28263
28264
28265
28266
28267
28268
28269
28270
28271
28272
28273
28274
28275
28276
28277
28278
28279
28280
28281
28282
28283
28284
28285
28286
28287
28288
28289
28290
28291
28292
28293
28294
28295
28296
28297
28298
28299
28300
28301
28302
28303
28304
28305
28306
28307
28308
28309
28310
28311
28312
28313
28314
28315
28316
28317
28318
28319
28320
28321
28322
28323
28324
28325
28326
28327
28328
28329
28330
28331
28332
28333
28334
28335
28336
28337
28338
28339
28340
28341
28342
28343
28344
28345
28346
28347
28348
28349
28350
28351
28352
28353
28354
28355
28356
28357
28358
28359
28360
28361
28362
28363
28364
28365
28366
28367
28368
28369
28370
28371
28372
28373
28374
28375
28376
28377
28378
28379
28380
28381
28382
28383
28384
28385
28386
28387
28388
28389
28390
28391
28392
28393
28394
28395
28396
28397
28398
28399
28400
28401
28402
28403
28404
28405
28406
28407
28408
28409
28410
28411
28412
28413
28414
28415
28416
28417
28418
28419
28420
28421
28422
28423
28424
28425
28426
28427
28428
28429
28430
28431
28432
28433
28434
28435
28436
28437
28438
28439
28440
28441
28442
28443
28444
28445
28446
28447
28448
28449
28450
28451
28452
28453
28454
28455
28456
28457
28458
28459
28460
28461
28462
28463
28464
28465
28466
28467
28468
28469
28470
28471
28472
28473
28474
28475
28476
28477
28478
28479
28480
28481
28482
28483
28484
28485
28486
28487
28488
28489
28490
28491
28492
28493
28494
28495
28496
28497
28498
28499
28500
28501
28502
28503
28504
28505
28506
28507
28508
28509
28510
28511
28512
28513
28514
28515
28516
28517
28518
28519
28520
28521
28522
28523
28524
28525
28526
28527
28528
28529
28530
28531
28532
28533
28534
28535
28536
28537
28538
28539
28540
28541
28542
28543
28544
28545
28546
28547
28548
28549
28550
28551
28552
28553
28554
28555
28556
28557
28558
28559
28560
28561
28562
28563
28564
28565
28566
28567
28568
28569
28570
28571
28572
28573
28574
28575
28576
28577
28578
28579
28580
28581
28582
28583
28584
28585
28586
28587
28588
28589
28590
28591
28592
28593
28594
28595
28596
28597
28598
28599
28600
28601
28602
28603
28604
28605
28606
28607
28608
28609
28610
28611
28612
28613
28614
28615
28616
28617
28618
28619
28620
28621
28622
28623
28624
28625
28626
28627
28628
28629
28630
28631
28632
28633
28634
28635
28636
28637
28638
28639
28640
28641
28642
28643
28644
28645
28646
28647
28648
28649
28650
28651
28652
28653
28654
28655
28656
28657
28658
28659
28660
28661
28662
28663
28664
28665
28666
28667
28668
28669
28670
28671
28672
28673
28674
28675
28676
28677
28678
28679
28680
28681
28682
28683
28684
28685
28686
28687
28688
28689
28690
28691
28692
28693
28694
28695
28696
28697
28698
28699
28700
28701
28702
28703
28704
28705
28706
28707
28708
28709
28710
28711
28712
28713
28714
28715
28716
28717
28718
28719
28720
28721
28722
28723
28724
28725
28726
28727
28728
28729
28730
28731
28732
28733
28734
28735
28736
28737
28738
28739
28740
28741
28742
28743
28744
28745
28746
28747
28748
28749
28750
28751
28752
28753
28754
28755
28756
28757
28758
28759
28760
28761
28762
28763
28764
28765
28766
28767
28768
28769
28770
28771
28772
28773
28774
28775
28776
28777
28778
28779
28780
28781
28782
28783
28784
28785
28786
28787
28788
28789
28790
28791
28792
28793
28794
28795
28796
28797
28798
28799
28800
28801
28802
28803
28804
28805
28806
28807
28808
28809
28810
28811
28812
28813
28814
28815
28816
28817
28818
28819
28820
28821
28822
28823
28824
28825
28826
28827
28828
28829
28830
28831
28832
28833
28834
28835
28836
28837
28838
28839
28840
28841
28842
28843
28844
28845
28846
28847
28848
28849
28850
28851
28852
28853
28854
28855
28856
28857
28858
28859
28860
28861
28862
28863
28864
28865
28866
28867
28868
28869
28870
28871
28872
28873
28874
28875
28876
28877
28878
28879
28880
28881
28882
28883
28884
28885
28886
28887
28888
28889
28890
28891
28892
28893
28894
28895
28896
28897
28898
28899
28900
28901
28902
28903
28904
28905
28906
28907
28908
28909
28910
28911
28912
28913
28914
28915
28916
28917
28918
28919
28920
28921
28922
28923
28924
28925
28926
28927
28928
28929
28930
28931
28932
28933
28934
28935
28936
28937
28938
28939
28940
28941
28942
28943
28944
28945
28946
28947
28948
28949
28950
28951
28952
28953
28954
28955
28956
28957
28958
28959
28960
28961
28962
28963
28964
28965
28966
28967
28968
28969
28970
28971
28972
28973
28974
28975
28976
28977
28978
28979
28980
28981
28982
28983
28984
28985
28986
28987
28988
28989
28990
28991
28992
28993
28994
28995
28996
28997
28998
28999
29000
29001
29002
29003
29004
29005
29006
29007
29008
29009
29010
29011
29012
29013
29014
29015
29016
29017
29018
29019
29020
29021
29022
29023
29024
29025
29026
29027
29028
29029
29030
29031
29032
29033
29034
29035
29036
29037
29038
29039
29040
29041
29042
29043
29044
29045
29046
29047
29048
29049
29050
29051
29052
29053
29054
29055
29056
29057
29058
29059
29060
29061
29062
29063
29064
29065
29066
29067
29068
29069
29070
29071
29072
29073
29074
29075
29076
29077
29078
29079
29080
29081
29082
29083
29084
29085
29086
29087
29088
29089
29090
29091
29092
29093
29094
29095
29096
29097
29098
29099
29100
29101
29102
29103
29104
29105
29106
29107
29108
29109
29110
29111
29112
29113
29114
29115
29116
29117
29118
29119
29120
29121
29122
29123
29124
29125
29126
29127
29128
29129
29130
29131
29132
29133
29134
29135
29136
29137
29138
29139
29140
29141
29142
29143
29144
29145
29146
29147
29148
29149
29150
29151
29152
29153
29154
29155
29156
29157
29158
29159
29160
29161
29162
29163
29164
29165
29166
29167
29168
29169
29170
29171
29172
29173
29174
29175
29176
29177
29178
29179
29180
29181
29182
29183
29184
29185
29186
29187
29188
29189
29190
29191
29192
29193
29194
29195
29196
29197
29198
29199
29200
29201
29202
29203
29204
29205
29206
29207
29208
29209
29210
29211
29212
29213
29214
29215
29216
29217
29218
29219
29220
29221
29222
29223
29224
29225
29226
29227
29228
29229
29230
29231
29232
29233
29234
29235
29236
29237
29238
29239
29240
29241
29242
29243
29244
29245
29246
29247
29248
29249
29250
29251
29252
29253
29254
29255
29256
29257
29258
29259
29260
29261
29262
29263
29264
29265
29266
29267
29268
29269
29270
29271
29272
29273
29274
29275
29276
29277
29278
29279
29280
29281
29282
29283
29284
29285
29286
29287
29288
29289
29290
29291
29292
29293
29294
29295
29296
29297
29298
29299
29300
29301
29302
29303
29304
29305
29306
29307
29308
29309
29310
29311
29312
29313
29314
29315
29316
29317
29318
29319
29320
29321
29322
29323
29324
29325
29326
29327
29328
29329
29330
29331
29332
29333
29334
29335
29336
29337
29338
29339
29340
29341
29342
29343
29344
29345
29346
29347
29348
29349
29350
29351
29352
29353
29354
29355
29356
29357
29358
29359
29360
29361
29362
29363
29364
29365
29366
29367
29368
29369
29370
29371
29372
29373
29374
29375
29376
29377
29378
29379
29380
29381
29382
29383
29384
29385
29386
29387
29388
29389
29390
29391
29392
29393
29394
29395
29396
29397
29398
29399
29400
29401
29402
29403
29404
29405
29406
29407
29408
29409
29410
29411
29412
29413
29414
29415
29416
29417
29418
29419
29420
29421
29422
29423
29424
29425
29426
29427
29428
29429
29430
29431
29432
29433
29434
29435
29436
29437
29438
29439
29440
29441
29442
29443
29444
29445
29446
29447
29448
29449
29450
29451
29452
29453
29454
29455
29456
29457
29458
29459
29460
29461
29462
29463
29464
29465
29466
29467
29468
29469
29470
29471
29472
29473
29474
29475
29476
29477
29478
29479
29480
29481
29482
29483
29484
29485
29486
29487
29488
29489
29490
29491
29492
29493
29494
29495
29496
29497
29498
29499
29500
29501
29502
29503
29504
29505
29506
29507
29508
29509
29510
29511
29512
29513
29514
29515
29516
29517
29518
29519
29520
29521
29522
29523
29524
29525
29526
29527
29528
29529
29530
29531
29532
29533
29534
29535
29536
29537
29538
29539
29540
29541
29542
29543
29544
29545
29546
29547
29548
29549
29550
29551
29552
29553
29554
29555
29556
29557
29558
29559
29560
29561
29562
29563
29564
29565
29566
29567
29568
29569
29570
29571
29572
29573
29574
29575
29576
29577
29578
29579
29580
29581
29582
29583
29584
29585
29586
29587
29588
29589
29590
29591
29592
29593
29594
29595
29596
29597
29598
29599
29600
29601
29602
29603
29604
29605
29606
29607
29608
29609
29610
29611
29612
29613
29614
29615
29616
29617
29618
29619
29620
29621
29622
29623
29624
29625
29626
29627
29628
29629
29630
29631
29632
29633
29634
29635
29636
29637
29638
29639
29640
29641
29642
29643
29644
29645
29646
29647
29648
29649
29650
29651
29652
29653
29654
29655
29656
29657
29658
29659
29660
29661
29662
29663
29664
29665
29666
29667
29668
29669
29670
29671
29672
29673
29674
29675
29676
29677
29678
29679
29680
29681
29682
29683
29684
29685
29686
29687
29688
29689
29690
29691
29692
29693
29694
29695
29696
29697
29698
29699
29700
29701
29702
29703
29704
29705
29706
29707
29708
29709
29710
29711
29712
29713
29714
29715
29716
29717
29718
29719
29720
29721
29722
29723
29724
29725
29726
29727
29728
29729
29730
29731
29732
29733
29734
29735
29736
29737
29738
29739
29740
29741
29742
29743
29744
29745
29746
29747
29748
29749
29750
29751
29752
29753
29754
29755
29756
29757
29758
29759
29760
29761
29762
29763
29764
29765
29766
29767
29768
29769
29770
29771
29772
29773
29774
29775
29776
29777
29778
29779
29780
29781
29782
29783
29784
29785
29786
29787
29788
29789
29790
29791
29792
29793
29794
29795
29796
29797
29798
29799
29800
29801
29802
29803
29804
29805
29806
29807
29808
29809
29810
29811
29812
29813
29814
29815
29816
29817
29818
29819
29820
29821
29822
29823
29824
29825
29826
29827
29828
29829
29830
29831
29832
29833
29834
29835
29836
29837
29838
29839
29840
29841
29842
29843
29844
29845
29846
29847
29848
29849
29850
29851
29852
29853
29854
29855
29856
29857
29858
29859
29860
29861
29862
29863
29864
29865
29866
29867
29868
29869
29870
29871
29872
29873
29874
29875
29876
29877
29878
29879
29880
29881
29882
29883
29884
29885
29886
29887
29888
29889
29890
29891
29892
29893
29894
29895
29896
29897
29898
29899
29900
29901
29902
29903
29904
29905
29906
29907
29908
29909
29910
29911
29912
29913
29914
29915
29916
29917
29918
29919
29920
29921
29922
29923
29924
29925
29926
29927
29928
29929
29930
29931
29932
29933
29934
29935
29936
29937
29938
29939
29940
29941
29942
29943
29944
29945
29946
29947
29948
29949
29950
29951
29952
29953
29954
29955
29956
29957
29958
29959
29960
29961
29962
29963
29964
29965
29966
29967
29968
29969
29970
29971
29972
29973
29974
29975
29976
29977
29978
29979
29980
29981
29982
29983
29984
29985
29986
29987
29988
29989
29990
29991
29992
29993
29994
29995
29996
29997
29998
29999
30000
30001
30002
30003
30004
30005
30006
30007
30008
30009
30010
30011
30012
30013
30014
30015
30016
30017
30018
30019
30020
30021
30022
30023
30024
30025
30026
30027
30028
30029
30030
30031
30032
30033
30034
30035
30036
30037
30038
30039
30040
30041
30042
30043
30044
30045
30046
30047
30048
30049
30050
30051
30052
30053
30054
30055
30056
30057
30058
30059
30060
30061
30062
30063
30064
30065
30066
30067
30068
30069
30070
30071
30072
30073
30074
30075
30076
30077
30078
30079
30080
30081
30082
30083
30084
30085
30086
30087
30088
30089
30090
30091
30092
30093
30094
30095
30096
30097
30098
30099
30100
30101
30102
30103
30104
30105
30106
30107
30108
30109
30110
30111
30112
30113
30114
30115
30116
30117
30118
30119
30120
30121
30122
30123
30124
30125
30126
30127
30128
30129
30130
30131
30132
30133
30134
30135
30136
30137
30138
30139
30140
30141
30142
30143
30144
30145
30146
30147
30148
30149
30150
30151
30152
30153
30154
30155
30156
30157
30158
30159
30160
30161
30162
30163
30164
30165
30166
30167
30168
30169
30170
30171
30172
30173
30174
30175
30176
30177
30178
30179
30180
30181
30182
30183
30184
30185
30186
30187
30188
30189
30190
30191
30192
30193
30194
30195
30196
30197
30198
30199
30200
30201
30202
30203
30204
30205
30206
30207
30208
30209
30210
30211
30212
30213
30214
30215
30216
30217
30218
30219
30220
30221
30222
30223
30224
30225
30226
30227
30228
30229
30230
30231
30232
30233
30234
30235
30236
30237
30238
30239
30240
30241
30242
30243
30244
30245
30246
30247
30248
30249
30250
30251
30252
30253
30254
30255
30256
30257
30258
30259
30260
30261
30262
30263
30264
30265
30266
30267
30268
30269
30270
30271
30272
30273
30274
30275
30276
30277
30278
30279
30280
30281
30282
30283
30284
30285
30286
30287
30288
30289
30290
30291
30292
30293
30294
30295
30296
30297
30298
30299
30300
30301
30302
30303
30304
30305
30306
30307
30308
30309
30310
30311
30312
30313
30314
30315
30316
30317
30318
30319
30320
30321
30322
30323
30324
30325
30326
30327
30328
30329
30330
30331
30332
30333
30334
30335
30336
30337
30338
30339
30340
30341
30342
30343
30344
30345
30346
30347
30348
30349
30350
30351
30352
30353
30354
30355
30356
30357
30358
30359
30360
30361
30362
30363
30364
30365
30366
30367
30368
30369
30370
30371
30372
30373
30374
30375
30376
30377
30378
30379
30380
30381
30382
30383
30384
30385
30386
30387
30388
30389
30390
30391
30392
30393
30394
30395
30396
30397
30398
30399
30400
30401
30402
30403
30404
30405
30406
30407
30408
30409
30410
30411
30412
30413
30414
30415
30416
30417
30418
30419
30420
30421
30422
30423
30424
30425
30426
30427
30428
30429
30430
30431
30432
30433
30434
30435
30436
30437
30438
30439
30440
30441
30442
30443
30444
30445
30446
30447
30448
30449
30450
30451
30452
30453
30454
30455
30456
30457
30458
30459
30460
30461
30462
30463
30464
30465
30466
30467
30468
30469
30470
30471
30472
30473
30474
30475
30476
30477
30478
30479
30480
30481
30482
30483
30484
30485
30486
30487
30488
30489
30490
30491
30492
30493
30494
30495
30496
30497
30498
30499
30500
30501
30502
30503
30504
30505
30506
30507
30508
30509
30510
30511
30512
30513
30514
30515
30516
30517
30518
30519
30520
30521
30522
30523
30524
30525
30526
30527
30528
30529
30530
30531
30532
30533
30534
30535
30536
30537
30538
30539
30540
30541
30542
30543
30544
30545
30546
30547
30548
30549
30550
30551
30552
30553
30554
30555
30556
30557
30558
30559
30560
30561
30562
30563
30564
30565
30566
30567
30568
30569
30570
30571
30572
30573
30574
30575
30576
30577
30578
30579
30580
30581
30582
30583
30584
30585
30586
30587
30588
30589
30590
30591
30592
30593
30594
30595
30596
30597
30598
30599
30600
30601
30602
30603
30604
30605
30606
30607
30608
30609
30610
30611
30612
30613
30614
30615
30616
30617
30618
30619
30620
30621
30622
30623
30624
30625
30626
30627
30628
30629
30630
30631
30632
30633
30634
30635
30636
30637
30638
30639
30640
30641
30642
30643
30644
30645
30646
30647
30648
30649
30650
30651
30652
30653
30654
30655
30656
30657
30658
30659
30660
30661
30662
30663
30664
30665
30666
30667
30668
30669
30670
30671
30672
30673
30674
30675
30676
30677
30678
30679
30680
30681
30682
30683
30684
30685
30686
30687
30688
30689
30690
30691
30692
30693
30694
30695
30696
30697
30698
30699
30700
30701
30702
30703
30704
30705
30706
30707
30708
30709
30710
30711
30712
30713
30714
30715
30716
30717
30718
30719
30720
30721
30722
30723
30724
30725
30726
30727
30728
30729
30730
30731
30732
30733
30734
30735
30736
30737
30738
30739
30740
30741
30742
30743
30744
30745
30746
30747
30748
30749
30750
30751
30752
30753
30754
30755
30756
30757
30758
30759
30760
30761
30762
30763
30764
30765
30766
30767
30768
30769
30770
30771
30772
30773
30774
30775
30776
30777
30778
30779
30780
30781
30782
30783
30784
30785
30786
30787
30788
30789
30790
30791
30792
30793
30794
30795
30796
30797
30798
30799
30800
30801
30802
30803
30804
30805
30806
30807
30808
30809
30810
30811
30812
30813
30814
30815
30816
30817
30818
30819
30820
30821
30822
30823
30824
30825
30826
30827
30828
30829
30830
30831
30832
30833
30834
30835
30836
30837
30838
30839
30840
30841
30842
30843
30844
30845
30846
30847
30848
30849
30850
30851
30852
30853
30854
30855
30856
30857
30858
30859
30860
30861
30862
30863
30864
30865
30866
30867
30868
30869
30870
30871
30872
30873
30874
30875
30876
30877
30878
30879
30880
30881
30882
30883
30884
30885
30886
30887
30888
30889
30890
30891
30892
30893
30894
30895
30896
30897
30898
30899
30900
30901
30902
30903
30904
30905
30906
30907
30908
30909
30910
30911
30912
30913
30914
30915
30916
30917
30918
30919
30920
30921
30922
30923
30924
30925
30926
30927
30928
30929
30930
30931
30932
30933
30934
30935
30936
30937
30938
30939
30940
30941
30942
30943
30944
30945
30946
30947
30948
30949
30950
30951
30952
30953
30954
30955
30956
30957
30958
30959
30960
30961
30962
30963
30964
30965
30966
30967
30968
30969
30970
30971
30972
30973
30974
30975
30976
30977
30978
30979
30980
30981
30982
30983
30984
30985
30986
30987
30988
30989
30990
30991
30992
30993
30994
30995
30996
30997
30998
30999
31000
31001
31002
31003
31004
31005
31006
31007
31008
31009
31010
31011
31012
31013
31014
31015
31016
31017
31018
31019
31020
31021
31022
31023
31024
31025
31026
31027
31028
31029
31030
31031
31032
31033
31034
31035
31036
31037
31038
31039
31040
31041
31042
31043
31044
31045
31046
31047
31048
31049
31050
31051
31052
31053
31054
31055
31056
31057
31058
31059
31060
31061
31062
31063
31064
31065
31066
31067
31068
31069
31070
31071
31072
31073
31074
31075
31076
31077
31078
31079
31080
31081
31082
31083
31084
31085
31086
31087
31088
31089
31090
31091
31092
31093
31094
31095
31096
31097
31098
31099
31100
31101
31102
31103
31104
31105
31106
31107
31108
31109
31110
31111
31112
31113
31114
31115
31116
31117
31118
31119
31120
31121
31122
31123
31124
31125
31126
31127
31128
31129
31130
31131
31132
31133
31134
31135
31136
31137
31138
31139
31140
31141
31142
31143
31144
31145
31146
31147
31148
31149
31150
31151
31152
31153
31154
31155
31156
31157
31158
31159
31160
31161
31162
31163
31164
31165
31166
31167
31168
31169
31170
31171
31172
31173
31174
31175
31176
31177
31178
31179
31180
31181
31182
31183
31184
31185
31186
31187
31188
31189
31190
31191
31192
31193
31194
31195
31196
31197
31198
31199
31200
31201
31202
31203
31204
31205
31206
31207
31208
31209
31210
31211
31212
31213
31214
31215
31216
31217
31218
31219
31220
31221
31222
31223
31224
31225
31226
31227
31228
31229
31230
31231
31232
31233
31234
31235
31236
31237
31238
31239
31240
31241
31242
31243
31244
31245
31246
31247
31248
31249
31250
31251
31252
31253
31254
31255
31256
31257
31258
31259
31260
31261
31262
31263
31264
31265
31266
31267
31268
31269
31270
31271
31272
31273
31274
31275
31276
31277
31278
31279
31280
31281
31282
31283
31284
31285
31286
31287
31288
31289
31290
31291
31292
31293
31294
31295
31296
31297
31298
31299
31300
31301
31302
31303
31304
31305
31306
31307
31308
31309
31310
31311
31312
31313
31314
31315
31316
31317
31318
31319
31320
31321
31322
31323
31324
31325
31326
31327
31328
31329
31330
31331
31332
31333
31334
31335
31336
31337
31338
31339
31340
31341
31342
31343
31344
31345
31346
31347
31348
31349
31350
31351
31352
31353
31354
31355
31356
31357
31358
31359
31360
31361
31362
31363
31364
31365
31366
31367
31368
31369
31370
31371
31372
31373
31374
31375
31376
31377
31378
31379
31380
31381
31382
31383
31384
31385
31386
31387
31388
31389
31390
31391
31392
31393
31394
31395
31396
31397
31398
31399
31400
31401
31402
31403
31404
31405
31406
31407
31408
31409
31410
31411
31412
31413
31414
31415
31416
31417
31418
31419
31420
31421
31422
31423
31424
31425
31426
31427
31428
31429
31430
31431
31432
31433
31434
31435
31436
31437
31438
31439
31440
31441
31442
31443
31444
31445
31446
31447
31448
31449
31450
31451
31452
31453
31454
31455
31456
31457
31458
31459
31460
31461
31462
31463
31464
31465
31466
31467
31468
31469
31470
31471
31472
31473
31474
31475
31476
31477
31478
31479
31480
31481
31482
31483
31484
31485
31486
31487
31488
31489
31490
31491
31492
31493
31494
31495
31496
31497
31498
31499
31500
31501
31502
31503
31504
31505
31506
31507
31508
31509
31510
31511
31512
31513
31514
31515
31516
31517
31518
31519
31520
31521
31522
31523
31524
31525
31526
31527
31528
31529
31530
31531
31532
31533
31534
31535
31536
31537
31538
31539
31540
31541
31542
31543
31544
31545
31546
31547
31548
31549
31550
31551
31552
31553
31554
31555
31556
31557
31558
31559
31560
31561
31562
31563
31564
31565
31566
31567
31568
31569
31570
31571
31572
31573
31574
31575
31576
31577
31578
31579
31580
31581
31582
31583
31584
31585
31586
31587
31588
31589
31590
31591
31592
31593
31594
31595
31596
31597
31598
31599
31600
31601
31602
31603
31604
31605
31606
31607
31608
31609
31610
31611
31612
31613
31614
31615
31616
31617
31618
31619
31620
31621
31622
31623
31624
31625
31626
31627
31628
31629
31630
31631
31632
31633
31634
31635
31636
31637
31638
31639
31640
31641
31642
31643
31644
31645
31646
31647
31648
31649
31650
31651
31652
31653
31654
31655
31656
31657
31658
31659
31660
31661
31662
31663
31664
31665
31666
31667
31668
31669
31670
31671
31672
31673
31674
31675
31676
31677
31678
31679
31680
31681
31682
31683
31684
31685
31686
31687
31688
31689
31690
31691
31692
31693
31694
31695
31696
31697
31698
31699
31700
31701
31702
31703
31704
31705
31706
31707
31708
31709
31710
31711
31712
31713
31714
31715
31716
31717
31718
31719
31720
31721
31722
31723
31724
31725
31726
31727
31728
31729
31730
31731
31732
31733
31734
31735
31736
31737
31738
31739
31740
31741
31742
31743
31744
31745
31746
31747
31748
31749
31750
31751
31752
31753
31754
31755
31756
31757
31758
31759
31760
31761
31762
31763
31764
31765
31766
31767
31768
31769
31770
31771
31772
31773
31774
31775
31776
31777
31778
31779
31780
31781
31782
31783
31784
31785
31786
31787
31788
31789
31790
31791
31792
31793
31794
31795
31796
31797
31798
31799
31800
31801
31802
31803
31804
31805
31806
31807
31808
31809
31810
31811
31812
31813
31814
31815
31816
31817
31818
31819
31820
31821
31822
31823
31824
31825
31826
31827
31828
31829
31830
31831
31832
31833
31834
31835
31836
31837
31838
31839
31840
31841
31842
31843
31844
31845
31846
31847
31848
31849
31850
31851
31852
31853
31854
31855
31856
31857
31858
31859
31860
31861
31862
31863
31864
31865
31866
31867
31868
31869
31870
31871
31872
31873
31874
31875
31876
31877
31878
31879
31880
31881
31882
31883
31884
31885
31886
31887
31888
31889
31890
31891
31892
31893
31894
31895
31896
31897
31898
31899
31900
31901
31902
31903
31904
31905
31906
31907
31908
31909
31910
31911
31912
31913
31914
31915
31916
31917
31918
31919
31920
31921
31922
31923
31924
31925
31926
31927
31928
31929
31930
31931
31932
31933
31934
31935
31936
31937
31938
31939
31940
31941
31942
31943
31944
31945
31946
31947
31948
31949
31950
31951
31952
31953
31954
31955
31956
31957
31958
31959
31960
31961
31962
31963
31964
31965
31966
31967
31968
31969
31970
31971
31972
31973
31974
31975
31976
31977
31978
31979
31980
31981
31982
31983
31984
31985
31986
31987
31988
31989
31990
31991
31992
31993
31994
31995
31996
31997
31998
31999
32000
32001
32002
32003
32004
32005
32006
32007
32008
32009
32010
32011
32012
32013
32014
32015
32016
32017
32018
32019
32020
32021
32022
32023
32024
32025
32026
32027
32028
32029
32030
32031
32032
32033
32034
32035
32036
32037
32038
32039
32040
32041
32042
32043
32044
32045
32046
32047
32048
32049
32050
32051
32052
32053
32054
32055
32056
32057
32058
32059
32060
32061
32062
32063
32064
32065
32066
32067
32068
32069
32070
32071
32072
32073
32074
32075
32076
32077
32078
32079
32080
32081
32082
32083
32084
32085
32086
32087
32088
32089
32090
32091
32092
32093
32094
32095
32096
32097
32098
32099
32100
32101
32102
32103
32104
32105
32106
32107
32108
32109
32110
32111
32112
32113
32114
32115
32116
32117
32118
32119
32120
32121
32122
32123
32124
32125
32126
32127
32128
32129
32130
32131
32132
32133
32134
32135
32136
32137
32138
32139
32140
32141
32142
32143
32144
32145
32146
32147
32148
32149
32150
32151
32152
32153
32154
32155
32156
32157
32158
32159
32160
32161
32162
32163
32164
32165
32166
32167
32168
32169
32170
32171
32172
32173
32174
32175
32176
32177
32178
32179
32180
32181
32182
32183
32184
32185
32186
32187
32188
32189
32190
32191
32192
32193
32194
32195
32196
32197
32198
32199
32200
32201
32202
32203
32204
32205
32206
32207
32208
32209
32210
32211
32212
32213
32214
32215
32216
32217
32218
32219
32220
32221
32222
32223
32224
32225
32226
32227
32228
32229
32230
32231
32232
32233
32234
32235
32236
32237
32238
32239
32240
32241
32242
32243
32244
32245
32246
32247
32248
32249
32250
32251
32252
32253
32254
32255
32256
32257
32258
32259
32260
32261
32262
32263
32264
32265
32266
32267
32268
32269
32270
32271
32272
32273
32274
32275
32276
32277
32278
32279
32280
32281
32282
32283
32284
32285
32286
32287
32288
32289
32290
32291
32292
32293
32294
32295
32296
32297
32298
32299
32300
32301
32302
32303
32304
32305
32306
32307
32308
32309
32310
32311
32312
32313
32314
32315
32316
32317
32318
32319
32320
32321
32322
32323
32324
32325
32326
32327
32328
32329
32330
32331
32332
32333
32334
32335
32336
32337
32338
32339
32340
32341
32342
32343
32344
32345
32346
32347
32348
32349
32350
32351
32352
32353
32354
32355
32356
32357
32358
32359
32360
32361
32362
32363
32364
32365
32366
32367
32368
32369
32370
32371
32372
32373
32374
32375
32376
32377
32378
32379
32380
32381
32382
32383
32384
32385
32386
32387
32388
32389
32390
32391
32392
32393
32394
32395
32396
32397
32398
32399
32400
32401
32402
32403
32404
32405
32406
32407
32408
32409
32410
32411
32412
32413
32414
32415
32416
32417
32418
32419
32420
32421
32422
32423
32424
32425
32426
32427
32428
32429
32430
32431
32432
32433
32434
32435
32436
32437
32438
32439
32440
32441
32442
32443
32444
32445
32446
32447
32448
32449
32450
32451
32452
32453
32454
32455
32456
32457
32458
32459
32460
32461
32462
32463
32464
32465
32466
32467
32468
32469
32470
32471
32472
32473
32474
32475
32476
32477
32478
32479
32480
32481
32482
32483
32484
32485
32486
32487
32488
32489
32490
32491
32492
32493
32494
32495
32496
32497
32498
32499
32500
32501
32502
32503
32504
32505
32506
32507
32508
32509
32510
32511
32512
32513
32514
32515
32516
32517
32518
32519
32520
32521
32522
32523
32524
32525
32526
32527
32528
32529
32530
32531
32532
32533
32534
32535
32536
32537
32538
32539
32540
32541
32542
32543
32544
32545
32546
32547
32548
32549
32550
32551
32552
32553
32554
32555
32556
32557
32558
32559
32560
32561
32562
32563
32564
32565
32566
32567
32568
32569
32570
32571
32572
32573
32574
32575
32576
32577
32578
32579
32580
32581
32582
32583
32584
32585
32586
32587
32588
32589
32590
32591
32592
32593
32594
32595
32596
32597
32598
32599
32600
32601
32602
32603
32604
32605
32606
32607
32608
32609
32610
32611
32612
32613
32614
32615
32616
32617
32618
32619
32620
32621
32622
32623
32624
32625
32626
32627
32628
32629
32630
32631
32632
32633
32634
32635
32636
32637
32638
32639
32640
32641
32642
32643
32644
32645
32646
32647
32648
32649
32650
32651
32652
32653
32654
32655
32656
32657
32658
32659
32660
32661
32662
32663
32664
32665
32666
32667
32668
32669
32670
32671
32672
32673
32674
32675
32676
32677
32678
32679
32680
32681
32682
32683
32684
32685
32686
32687
32688
32689
32690
32691
32692
32693
32694
32695
32696
32697
32698
32699
32700
32701
32702
32703
32704
32705
32706
32707
32708
32709
32710
32711
32712
32713
32714
32715
32716
32717
32718
32719
32720
32721
32722
32723
32724
32725
32726
32727
32728
32729
32730
32731
32732
32733
32734
32735
32736
32737
32738
32739
32740
32741
32742
32743
32744
32745
32746
32747
32748
32749
32750
32751
32752
32753
32754
32755
32756
32757
32758
32759
32760
32761
32762
32763
32764
32765
32766
32767
32768
32769
32770
32771
32772
32773
32774
32775
32776
32777
32778
32779
32780
32781
32782
32783
32784
32785
32786
32787
32788
32789
32790
32791
32792
32793
32794
32795
32796
32797
32798
32799
32800
32801
32802
32803
32804
32805
32806
32807
32808
32809
32810
32811
32812
32813
32814
32815
32816
32817
32818
32819
32820
32821
32822
32823
32824
32825
32826
32827
32828
32829
32830
32831
32832
32833
32834
32835
32836
32837
32838
32839
32840
32841
32842
32843
32844
32845
32846
32847
32848
32849
32850
32851
32852
32853
32854
32855
32856
32857
32858
32859
32860
32861
32862
32863
32864
32865
32866
32867
32868
32869
32870
32871
32872
32873
32874
32875
32876
32877
32878
32879
32880
32881
32882
32883
32884
32885
32886
32887
32888
32889
32890
32891
32892
32893
32894
32895
32896
32897
32898
32899
32900
32901
32902
32903
32904
32905
32906
32907
32908
32909
32910
32911
32912
32913
32914
32915
32916
32917
32918
32919
32920
32921
32922
32923
32924
32925
32926
32927
32928
32929
32930
32931
32932
32933
32934
32935
32936
32937
32938
32939
32940
32941
32942
32943
32944
32945
32946
32947
32948
32949
32950
32951
32952
32953
32954
32955
32956
32957
32958
32959
32960
32961
32962
32963
32964
32965
32966
32967
32968
32969
32970
32971
32972
32973
32974
32975
32976
32977
32978
32979
32980
32981
32982
32983
32984
32985
32986
32987
32988
32989
32990
32991
32992
32993
32994
32995
32996
32997
32998
32999
33000
33001
33002
33003
33004
33005
33006
33007
33008
33009
33010
33011
33012
33013
33014
33015
33016
33017
33018
33019
33020
33021
33022
33023
33024
33025
33026
33027
33028
33029
33030
33031
33032
33033
33034
33035
33036
33037
33038
33039
33040
33041
33042
33043
33044
33045
33046
33047
33048
33049
33050
33051
33052
33053
33054
33055
33056
33057
33058
33059
33060
33061
33062
33063
33064
33065
33066
33067
33068
33069
33070
33071
33072
33073
33074
33075
33076
33077
33078
33079
33080
33081
33082
33083
33084
33085
33086
33087
33088
33089
33090
33091
33092
33093
33094
33095
33096
33097
33098
33099
33100
33101
33102
33103
33104
33105
33106
33107
33108
33109
33110
33111
33112
33113
33114
33115
33116
33117
33118
33119
33120
33121
33122
33123
33124
33125
33126
33127
33128
33129
33130
33131
33132
33133
33134
33135
33136
33137
33138
33139
33140
33141
33142
33143
33144
33145
33146
33147
33148
33149
33150
33151
33152
33153
33154
33155
33156
33157
33158
33159
33160
33161
33162
33163
33164
33165
33166
33167
33168
33169
33170
33171
33172
33173
33174
33175
33176
33177
33178
33179
33180
33181
33182
33183
33184
33185
33186
33187
33188
33189
33190
33191
33192
33193
33194
33195
33196
33197
33198
33199
33200
33201
33202
33203
33204
33205
33206
33207
33208
33209
33210
33211
33212
33213
33214
33215
33216
33217
33218
33219
33220
33221
33222
33223
33224
33225
33226
33227
33228
33229
33230
33231
33232
33233
33234
33235
33236
33237
33238
33239
33240
33241
33242
33243
33244
33245
33246
33247
33248
33249
33250
33251
33252
33253
33254
33255
33256
33257
33258
33259
33260
33261
33262
33263
33264
33265
33266
33267
33268
33269
33270
33271
33272
33273
33274
33275
33276
33277
33278
33279
33280
33281
33282
33283
33284
33285
33286
33287
33288
33289
33290
33291
33292
33293
33294
33295
33296
33297
33298
33299
33300
33301
33302
33303
33304
33305
33306
33307
33308
33309
33310
33311
33312
33313
33314
33315
33316
33317
33318
33319
33320
33321
33322
33323
33324
33325
33326
33327
33328
33329
33330
33331
33332
33333
33334
33335
33336
33337
33338
33339
33340
33341
33342
33343
33344
33345
33346
33347
33348
33349
33350
33351
33352
33353
33354
33355
33356
33357
33358
33359
33360
33361
33362
33363
33364
33365
33366
33367
33368
33369
33370
33371
33372
33373
33374
33375
33376
33377
33378
33379
33380
33381
33382
33383
33384
33385
33386
33387
33388
33389
33390
33391
33392
33393
33394
33395
33396
33397
33398
33399
33400
33401
33402
33403
33404
33405
33406
33407
33408
33409
33410
33411
33412
33413
33414
33415
33416
33417
33418
33419
33420
33421
33422
33423
33424
33425
33426
33427
33428
33429
33430
33431
33432
33433
33434
33435
33436
33437
33438
33439
33440
33441
33442
33443
33444
33445
33446
33447
33448
33449
33450
33451
33452
33453
33454
33455
33456
33457
33458
33459
33460
33461
33462
33463
33464
33465
33466
33467
33468
33469
33470
33471
33472
33473
33474
33475
33476
33477
33478
33479
33480
33481
33482
33483
33484
33485
33486
33487
33488
33489
33490
33491
33492
33493
33494
33495
33496
33497
33498
33499
33500
33501
33502
33503
33504
33505
33506
33507
33508
33509
33510
33511
33512
33513
33514
33515
33516
33517
33518
33519
33520
33521
33522
33523
33524
33525
33526
33527
33528
33529
33530
33531
33532
33533
33534
33535
33536
33537
33538
33539
33540
33541
33542
33543
33544
33545
33546
33547
33548
33549
33550
33551
33552
33553
33554
33555
33556
33557
33558
33559
33560
33561
33562
33563
33564
33565
33566
33567
33568
33569
33570
33571
33572
33573
33574
33575
33576
33577
33578
33579
33580
33581
33582
33583
33584
33585
33586
33587
33588
33589
33590
33591
33592
33593
33594
33595
33596
33597
33598
33599
33600
33601
33602
33603
33604
33605
33606
33607
33608
33609
33610
33611
33612
33613
33614
33615
33616
33617
33618
33619
33620
33621
33622
33623
33624
33625
33626
33627
33628
33629
33630
33631
33632
33633
33634
33635
33636
33637
33638
33639
33640
33641
33642
33643
33644
33645
33646
33647
33648
33649
33650
33651
33652
33653
33654
33655
33656
33657
33658
33659
33660
33661
33662
33663
33664
33665
33666
33667
33668
33669
33670
33671
33672
33673
33674
33675
33676
33677
33678
33679
33680
33681
33682
33683
33684
33685
33686
33687
33688
33689
33690
33691
33692
33693
33694
33695
33696
33697
33698
33699
33700
33701
33702
33703
33704
33705
33706
33707
33708
33709
33710
33711
33712
33713
33714
33715
33716
33717
33718
33719
33720
33721
33722
33723
33724
33725
33726
33727
33728
33729
33730
33731
33732
33733
33734
33735
33736
33737
33738
33739
33740
33741
33742
33743
33744
33745
33746
33747
33748
33749
33750
33751
33752
33753
33754
33755
33756
33757
33758
33759
33760
33761
33762
33763
33764
33765
33766
33767
33768
33769
33770
33771
33772
33773
33774
33775
33776
33777
33778
33779
33780
33781
33782
33783
33784
33785
33786
33787
33788
33789
33790
33791
33792
33793
33794
33795
33796
33797
33798
33799
33800
33801
33802
33803
33804
33805
33806
33807
33808
33809
33810
33811
33812
33813
33814
33815
33816
33817
33818
33819
33820
33821
33822
33823
33824
33825
33826
33827
33828
33829
33830
33831
33832
33833
33834
33835
33836
33837
33838
33839
33840
33841
33842
33843
33844
33845
33846
33847
33848
33849
33850
33851
33852
33853
33854
33855
33856
33857
33858
33859
33860
33861
33862
33863
33864
33865
33866
33867
33868
33869
33870
33871
33872
33873
33874
33875
33876
33877
33878
33879
33880
33881
33882
33883
33884
33885
33886
33887
33888
33889
33890
33891
33892
33893
33894
33895
33896
33897
33898
33899
33900
33901
33902
33903
33904
33905
33906
33907
33908
33909
33910
33911
33912
33913
33914
33915
33916
33917
33918
33919
33920
33921
33922
33923
33924
33925
33926
33927
33928
33929
33930
33931
33932
33933
33934
33935
33936
33937
33938
33939
33940
33941
33942
33943
33944
33945
33946
33947
33948
33949
33950
33951
33952
33953
33954
33955
33956
33957
33958
33959
33960
33961
33962
33963
33964
33965
33966
33967
33968
33969
33970
33971
33972
33973
33974
33975
33976
33977
33978
33979
33980
33981
33982
33983
33984
33985
33986
33987
33988
33989
33990
33991
33992
33993
33994
33995
33996
33997
33998
33999
34000
34001
34002
34003
34004
34005
34006
34007
34008
34009
34010
34011
34012
34013
34014
34015
34016
34017
34018
34019
34020
34021
34022
34023
34024
34025
34026
34027
34028
34029
34030
34031
34032
34033
34034
34035
34036
34037
34038
34039
34040
34041
34042
34043
34044
34045
34046
34047
34048
34049
34050
34051
34052
34053
34054
34055
34056
34057
34058
34059
34060
34061
34062
34063
34064
34065
34066
34067
34068
34069
34070
34071
34072
34073
34074
34075
34076
34077
34078
34079
34080
34081
34082
34083
34084
34085
34086
34087
34088
34089
34090
34091
34092
34093
34094
34095
34096
34097
34098
34099
34100
34101
34102
34103
34104
34105
34106
34107
34108
34109
34110
34111
34112
34113
34114
34115
34116
34117
34118
34119
34120
34121
34122
34123
34124
34125
34126
34127
34128
34129
34130
34131
34132
34133
34134
34135
34136
34137
34138
34139
34140
34141
34142
34143
34144
34145
34146
34147
34148
34149
34150
34151
34152
34153
34154
34155
34156
34157
34158
34159
34160
34161
34162
34163
34164
34165
34166
34167
34168
34169
34170
34171
34172
34173
34174
34175
34176
34177
34178
34179
34180
34181
34182
34183
34184
34185
34186
34187
34188
34189
34190
34191
34192
34193
34194
34195
34196
34197
34198
34199
34200
34201
34202
34203
34204
34205
34206
34207
34208
34209
34210
34211
34212
34213
34214
34215
34216
34217
34218
34219
34220
34221
34222
34223
34224
34225
34226
34227
34228
34229
34230
34231
34232
34233
34234
34235
34236
34237
34238
34239
34240
34241
34242
34243
34244
34245
34246
34247
34248
34249
34250
34251
34252
34253
34254
34255
34256
34257
34258
34259
34260
34261
34262
34263
34264
34265
34266
34267
34268
34269
34270
34271
34272
34273
34274
34275
34276
34277
34278
34279
34280
34281
34282
34283
34284
34285
34286
34287
34288
34289
34290
34291
34292
34293
34294
34295
34296
34297
34298
34299
34300
34301
34302
34303
34304
34305
34306
34307
34308
34309
34310
34311
34312
34313
34314
34315
34316
34317
34318
34319
34320
34321
34322
34323
34324
34325
34326
34327
34328
34329
34330
34331
34332
34333
34334
34335
34336
34337
34338
34339
34340
34341
34342
34343
34344
34345
34346
34347
34348
34349
34350
34351
34352
34353
34354
34355
34356
34357
34358
34359
34360
34361
34362
34363
34364
34365
34366
34367
34368
34369
34370
34371
34372
34373
34374
34375
34376
34377
34378
34379
34380
34381
34382
34383
34384
34385
34386
34387
34388
34389
34390
34391
34392
34393
34394
34395
34396
34397
34398
34399
34400
34401
34402
34403
34404
34405
34406
34407
34408
34409
34410
34411
34412
34413
34414
34415
34416
34417
34418
34419
34420
34421
34422
34423
34424
34425
34426
34427
34428
34429
34430
34431
34432
34433
34434
34435
34436
34437
34438
34439
34440
34441
34442
34443
34444
34445
34446
34447
34448
34449
34450
34451
34452
34453
34454
34455
34456
34457
34458
34459
34460
34461
34462
34463
34464
34465
34466
34467
34468
34469
34470
34471
34472
34473
34474
34475
34476
34477
34478
34479
34480
34481
34482
34483
34484
34485
34486
34487
34488
34489
34490
34491
34492
34493
34494
34495
34496
34497
34498
34499
34500
34501
34502
34503
34504
34505
34506
34507
34508
34509
34510
34511
34512
34513
34514
34515
34516
34517
34518
34519
34520
34521
34522
34523
34524
34525
34526
34527
34528
34529
34530
34531
34532
34533
34534
34535
34536
34537
34538
34539
34540
34541
34542
34543
34544
34545
34546
34547
34548
34549
34550
34551
34552
34553
34554
34555
34556
34557
34558
34559
34560
34561
34562
34563
34564
34565
34566
34567
34568
34569
34570
34571
34572
34573
34574
34575
34576
34577
34578
34579
34580
34581
34582
34583
34584
34585
34586
34587
34588
34589
34590
34591
34592
34593
34594
34595
34596
34597
34598
34599
34600
34601
34602
34603
34604
34605
34606
34607
34608
34609
34610
34611
34612
34613
34614
34615
34616
34617
34618
34619
34620
34621
34622
34623
34624
34625
34626
34627
34628
34629
34630
34631
34632
34633
34634
34635
34636
34637
34638
34639
34640
34641
34642
34643
34644
34645
34646
34647
34648
34649
34650
34651
34652
34653
34654
34655
34656
34657
34658
34659
34660
34661
34662
34663
34664
34665
34666
34667
34668
34669
34670
34671
34672
34673
34674
34675
34676
34677
34678
34679
34680
34681
34682
34683
34684
34685
34686
34687
34688
34689
34690
34691
34692
34693
34694
34695
34696
34697
34698
34699
34700
34701
34702
34703
34704
34705
34706
34707
34708
34709
34710
34711
34712
34713
34714
34715
34716
34717
34718
34719
34720
34721
34722
34723
34724
34725
34726
34727
34728
34729
34730
34731
34732
34733
34734
34735
34736
34737
34738
34739
34740
34741
34742
34743
34744
34745
34746
34747
34748
34749
34750
34751
34752
34753
34754
34755
34756
34757
34758
34759
34760
34761
34762
34763
34764
34765
34766
34767
34768
34769
34770
34771
34772
34773
34774
34775
34776
34777
34778
34779
34780
34781
34782
34783
34784
34785
34786
34787
34788
34789
34790
34791
34792
34793
34794
34795
34796
34797
34798
34799
34800
34801
34802
34803
34804
34805
34806
34807
34808
34809
34810
34811
34812
34813
34814
34815
34816
34817
34818
34819
34820
34821
34822
34823
34824
34825
34826
34827
34828
34829
34830
34831
34832
34833
34834
34835
34836
34837
34838
34839
34840
34841
34842
34843
34844
34845
34846
34847
34848
34849
34850
34851
34852
34853
34854
34855
34856
34857
34858
34859
34860
34861
34862
34863
34864
34865
34866
34867
34868
34869
34870
34871
34872
34873
34874
34875
34876
34877
34878
34879
34880
34881
34882
34883
34884
34885
34886
34887
34888
34889
34890
34891
34892
34893
34894
34895
34896
34897
34898
34899
34900
34901
34902
34903
34904
34905
34906
34907
34908
34909
34910
34911
34912
34913
34914
34915
34916
34917
34918
34919
34920
34921
34922
34923
34924
34925
34926
34927
34928
34929
34930
34931
34932
34933
34934
34935
34936
34937
34938
34939
34940
34941
34942
34943
34944
34945
34946
34947
34948
34949
34950
34951
34952
34953
34954
34955
34956
34957
34958
34959
34960
34961
34962
34963
34964
34965
34966
34967
34968
34969
34970
34971
34972
34973
34974
34975
34976
34977
34978
34979
34980
34981
34982
34983
34984
34985
34986
34987
34988
34989
34990
34991
34992
34993
34994
34995
34996
34997
34998
34999
35000
35001
35002
35003
35004
35005
35006
35007
35008
35009
35010
35011
35012
35013
35014
35015
35016
35017
35018
35019
35020
35021
35022
35023
35024
35025
35026
35027
35028
35029
35030
35031
35032
35033
35034
35035
35036
35037
35038
35039
35040
35041
35042
35043
35044
35045
35046
35047
35048
35049
35050
35051
35052
35053
35054
35055
35056
35057
35058
35059
35060
35061
35062
35063
35064
35065
35066
35067
35068
35069
35070
35071
35072
35073
35074
35075
35076
35077
35078
35079
35080
35081
35082
35083
35084
35085
35086
35087
35088
35089
35090
35091
35092
35093
35094
35095
35096
35097
35098
35099
35100
35101
35102
35103
35104
35105
35106
35107
35108
35109
35110
35111
35112
35113
35114
35115
35116
35117
35118
35119
35120
35121
35122
35123
35124
35125
35126
35127
35128
35129
35130
35131
35132
35133
35134
35135
35136
35137
35138
35139
35140
35141
35142
35143
35144
35145
35146
35147
35148
35149
35150
35151
35152
35153
35154
35155
35156
35157
35158
35159
35160
35161
35162
35163
35164
35165
35166
35167
35168
35169
35170
35171
35172
35173
35174
35175
35176
35177
35178
35179
35180
35181
35182
35183
35184
35185
35186
35187
35188
35189
35190
35191
35192
35193
35194
35195
35196
35197
35198
35199
35200
35201
35202
35203
35204
35205
35206
35207
35208
35209
35210
35211
35212
35213
35214
35215
35216
35217
35218
35219
35220
35221
35222
35223
35224
35225
35226
35227
35228
35229
35230
35231
35232
35233
35234
35235
35236
35237
35238
35239
35240
35241
35242
35243
35244
35245
35246
35247
35248
35249
35250
35251
35252
35253
35254
35255
35256
35257
35258
35259
35260
35261
35262
35263
35264
35265
35266
35267
35268
35269
35270
35271
35272
35273
35274
35275
35276
35277
35278
35279
35280
35281
35282
35283
35284
35285
35286
35287
35288
35289
35290
35291
35292
35293
35294
35295
35296
35297
35298
35299
35300
35301
35302
35303
35304
35305
35306
35307
35308
35309
35310
35311
35312
35313
35314
35315
35316
35317
35318
35319
35320
35321
35322
35323
35324
35325
35326
35327
35328
35329
35330
35331
35332
35333
35334
35335
35336
35337
35338
35339
35340
35341
35342
35343
35344
35345
35346
35347
35348
35349
35350
35351
35352
35353
35354
35355
35356
35357
35358
35359
35360
35361
35362
35363
35364
35365
35366
35367
35368
35369
35370
35371
35372
35373
35374
35375
35376
35377
35378
35379
35380
35381
35382
35383
35384
35385
35386
35387
35388
35389
35390
35391
35392
35393
35394
35395
35396
35397
35398
35399
35400
35401
35402
35403
35404
35405
35406
35407
35408
35409
35410
35411
35412
35413
35414
35415
35416
35417
35418
35419
35420
35421
35422
35423
35424
35425
35426
35427
35428
35429
35430
35431
|
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 2000 Sun Microsystems, Inc. All rights reserved.
Use is subject to license terms.
ident "%Z%%M% %I% %E% SMI"
System Call Files
------ ---- -----
The universal dumping grounds for system calls in Solaris 2.x,
common/os/scalls.c and common/fs/vncalls.c, have been sub-divided into
smaller files.
The old files had become quite large, and contained much completely
unrelated code. From a software engineering standpoint, it didn't seem
like a good idea to permit system calls or underlying routines
to be cognizant of the internal interfaces and underlying routines
of unrelated system calls.
From a practical standpoint, recompiling all of scalls.c or vncalls.c
after making only a small change in one system call seemed like
cruel and unusual punishment. Also, running "bringover" after
changing scalls.c or vncalls.c in one's own environment had a
high probability of encountering a conflict.
In an attempt to improve maintainability, we have split these files
and created new directories to hold the results. One hopes that this
new organization will prove easier to maintain and change.
The principles listed below guided the split-up. Please try to adhere
to them if you add new system calls.
1) System calls now live in directories called "syscall". Architecture
independant system calls live in common/syscall and architecture
dependant system calls live in sparc/syscall or i86/syscall.
2) Most system calls have their own separate file. We try to keep
these files as small as possible.
3) Unrelated system calls should NEVER be put in the same file. Do
not consider any of these files "dumping grounds" for new system
call work.
4) Some files DO contain more than one system call. This occurs
under the following restricted conditions:
o System calls that are internally related, either because
they alone call a set of static functions to do the dirty
work, or because they access locally-defined static data.
The system calls in sigqueue.c and lwpsys.c are examples
of the first case; lwp_sobj.c is an example of the second.
o Fairly trivial pairs of "get-" and "set-" operation system
calls. The file rlimit.c, containing getrlimit() and
setrlimit() is a case in point.
o System calls that are basically "variations on a theme,"
such as the the different forms of stat in stat.c.
5) If a number of system calls make use of a local function, or,
if a function is used more widely than in a few system calls,
then perhaps this function needs to be moved to one of the
kernel-implementation files in common/os or common/fs. For
example, this was done with the functions namesetattr and
fdsetattr, which were used by several different system calls.
These functions were moved into common/os/fio.c, where they
seemed to fit better.
-------------------------------------------------------------------
System Call Reorganization
------ ---- --------------
The system calls in common/os/scalls.c, common/fs/vncalls.c
have been broken up into smaller files. In addition, system
calls that previously resided in <arch>/os/archdep.c have
been removed from that file. The table below describes the
manner in which the files have been split up.
The original syscall files have not been deleted, but have been
renamed to reflect their diminished contents. The file scalls.c
has been renamed to ssig.c, and vncalls.c has been renamed to
poll.c.
Syscall Entry Point Old File New File
------------------- --- ---- --- ----
gtime scalls.c common/syscall/time.c
stime scalls.c common/syscall/time.c
adjtime scalls.c common/syscall/adjtime.c
times scalls.c common/syscall/times.c
sysconfig scalls.c common/syscall/sysconfig.c
setuid scalls.c common/syscall/uid.c
getuid scalls.c common/syscall/uid.c
seteuid scalls.c common/syscall/uid.c
setgid scalls.c common/syscall/gid.c
getgid scalls.c common/syscall/gid.c
setegid scalls.c common/syscall/gid.c
getpid scalls.c common/syscall/getpid.c
setgroups scalls.c common/syscall/groups.c
getgroups scalls.c common/syscall/groups.c
setpgrp scalls.c common/syscall/pgrpsys.c
pause scalls.c common/syscall/pause.c
ssig scalls.c common/syscall/ssig.c
sigtimedwait scalls.c common/syscall/sigtimedwait.c
sigsuspend scalls.c common/syscall/sigsuspend.c
sigaltstack scalls.c common/syscall/sigaltstack.c
sigpending scalls.c common/syscall/sigpending.c
sigprocmask scalls.c common/syscall/sigprocmask.c
sigaction scalls.c common/syscall/sigaction.c
kill scalls.c common/syscall/sigqueue.c
sigqueue scalls.c common/syscall/sigqueue.c
sigsendsys scalls.c common/syscall/sigsendset.c
profil scalls.c common/syscall/profil.c
alarm scalls.c common/syscall/alarm.c
umask scalls.c common/syscall/umask.c
ulimit scalls.c common/syscall/rlimit.c
getrlimit scalls.c common/syscall/rlimit.c
setrlimit scalls.c common/syscall/rlimit.c
utssys scalls.c common/syscall/utssys.c
uname scalls.c common/syscall/uname.c
uadmin scalls.c common/syscall/uadmin.c
systeminfo scalls.c common/syscall/systeminfo.c
syslwp_create scalls.c common/syscall/lwp_create.c
syslwp_exit scalls.c common/syscall/lwp_create.c
syslwp_syspend scalls.c common/syscall/lwpsys.c
syslwp_continue scalls.c common/syscall/lwpsys.c
lwp_kill scalls.c common/syscall/lwpsys.c
lwp_wait scalls.c common/syscall/lwpsys.c
yield scalls.c common/syscall/yield.c
lwp_self scalls.c common/syscall/lwp_self.c
lwp_info scalls.c common/syscall/lwp_info.c
lwp_mutex_lock scalls.c common/syscall/lwp_sobj.c
lwp_mutex_unlock scalls.c common/syscall/lwp_sobj.c
lwp_cond_wait scalls.c common/syscall/lwp_sobj.c
lwp_cond_signal scalls.c common/syscall/lwp_sobj.c
lwp_cond_broadcast scalls.c common/syscall/lwp_sobj.c
lwp_sema_p scalls.c common/syscall/lwp_sobj.c
lwp_sema_v scalls.c common/syscall/lwp_sobj.c
open vncalls.c common/syscall/open.c
creat vncalls.c common/syscall/open.c
close vncalls.c common/syscall/close.c
read vncalls.c common/syscall/rw.c
write vncalls.c common/syscall/rw.c
pread vncalls.c common/syscall/rw.c
pwrite vncalls.c common/syscall/rw.c
readv vncalls.c common/syscall/rw.c
writev vncalls.c common/syscall/rw.c
chdir vncalls.c common/syscall/chdir.c
fchdir vncalls.c common/syscall/chdir.c
chroot vncalls.c common/syscall/chdir.c
fchroot vncalls.c common/syscall/chdir.c
mknod vncalls.c common/syscall/mknod.c
xmknod vncalls.c common/syscall/mknod.c
mkdir vncalls.c common/syscall/mkdir.c
link vncalls.c common/syscall/link.c
rename vncalls.c common/syscall/rename.c
symlink vncalls.c common/syscall/symlink.c
unlink vncalls.c common/syscall/unlink.c
rmdir vncalls.c common/syscall/rmdir.c
getdents vncalls.c common/syscall/getdents.c
lseek vncalls.c common/syscall/lseek.c
llseek vncalls.c common/syscall/lseek.c
access vncalls.c common/syscall/access.c
stat vncalls.c common/syscall/stat.c
lstat vncalls.c common/syscall/stat.c
fstat vncalls.c common/syscall/stat.c
xstat vncalls.c common/syscall/stat.c
lxstat vncalls.c common/syscall/stat.c
fxstat vncalls.c common/syscall/stat.c
fpathconf vncalls.c common/syscall/pathconf.c
pathconf vncalls.c common/syscall/pathconf.c
readlink vncalls.c common/syscall/readlink.c
chmod vncalls.c common/syscall/chmod.c
fchmod vncalls.c common/syscall/chmod.c
chown vncalls.c common/syscall/chown.c
lchown vncalls.c common/syscall/chown.c
fchown vncalls.c common/syscall/chown.c
utime vncalls.c common/syscall/utime.c
utimes vncalls.c common/syscall/utime.c
fdsync vncalls.c common/syscall/fdsync.c
fcntl vncalls.c common/syscall/fcntl.c
dup vncalls.c common/syscall/dup.c
ioctl vncalls.c common/syscall/ioctl.c
stty vncalls.c common/syscall/ioctl.c
gtty vncalls.c common/syscall/ioctl.c
poll vncalls.c common/syscall/poll.c
acl vncalls.c common/syscall/acl.c
facl vncalls.c common/syscall/acl.c
mount vfs.c common/syscall/mount.c
statfs vfs.c common/syscall/statfs.c
fstatfs vfs.c common/syscall/statfs.c
statvfs vfs.c common/syscall/statvfs.c
fstatvfs vfs.c common/syscall/statvfs.c
sync vfs.c common/syscall/sync.c
sysfs vfs.c common/syscall/sysfs.c
umount vfs.c common/syscall/umount.c
nice priocntl.c common/syscall/nice.c
pipe os/pipe.c common/syscall/pipe.c
msgsys os/msg.c common/syscall/msg.c
semsys os/sem.c common/syscall/sem.c
shmsys os/shm.c common/syscall/shm.c
getcontext sparc/archdep.c sparc/syscall/getcontext.c
lwp_getprivate sparc/archdep.c sparc/syscall/lwp_private.c
lwp_setprivate sparc/archdep.c sparc/syscall/lwp_private.c
getcontext i86/archdep.c i86/syscall/getcontext.c
lwp_getprivate i86/archdep.c i86/syscall/lwp_private.c
lwp_setprivate i86/archdep.c i86/syscall/lwp_private.c
-----------------------------------------------------------------
Most of the system calls in this directory have been converted
to use C-style argument passing, instead of the old uap-pointer
method. This usually makes the system calls faster and more
"natural" in implementation.
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/cred_impl.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/pathname.h>
#include <sys/vnode.h>
#include <sys/uio.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/file.h>
#include <fs/fs_subr.h>
#include <c2/audit.h>
#include <sys/fcntl.h>
/*
* Determine accessibility of file.
*/
#define E_OK 010 /* use effective ids */
#define R_OK 004
#define W_OK 002
#define X_OK 001
static int
caccess(char *fname, int fmode, vnode_t *startvp)
{
vnode_t *vp;
cred_t *tmpcr;
int error;
int mode;
int eok;
cred_t *cr;
int estale_retry = 0;
if (fmode & ~(E_OK|R_OK|W_OK|X_OK))
return (EINVAL);
mode = ((fmode & (R_OK|W_OK|X_OK)) << 6);
cr = CRED();
/* OK to use effective uid/gid, i.e., no need to crdup(CRED())? */
eok = (fmode & E_OK) ||
(cr->cr_uid == cr->cr_ruid && cr->cr_gid == cr->cr_rgid);
if (eok)
tmpcr = cr;
else {
tmpcr = crdup(cr);
tmpcr->cr_uid = cr->cr_ruid;
tmpcr->cr_gid = cr->cr_rgid;
tmpcr->cr_ruid = cr->cr_uid;
tmpcr->cr_rgid = cr->cr_gid;
}
lookup:
if (error = lookupnameatcred(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp,
startvp, tmpcr)) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
if (!eok)
crfree(tmpcr);
return (error);
}
if (mode) {
error = VOP_ACCESS(vp, mode, 0, tmpcr, NULL);
if (error) {
if ((error == ESTALE) &&
fs_need_estale_retry(estale_retry++)) {
VN_RELE(vp);
goto lookup;
}
}
}
if (!eok)
crfree(tmpcr);
VN_RELE(vp);
return (error);
}
int
faccessat(int fd, char *fname, int fmode, int flag)
{
vnode_t *startvp;
int error;
if ((flag & ~AT_EACCESS) != 0)
return (set_errno(EINVAL));
if (fname == NULL)
return (set_errno(EFAULT));
if ((error = fgetstartvp(fd, fname, &startvp)) != 0)
return (set_errno(error));
if (AU_AUDITING() && startvp != NULL)
audit_setfsat_path(1);
/* Do not allow E_OK unless AT_EACCESS flag is set */
fmode &= ~E_OK;
if (flag & AT_EACCESS)
fmode |= E_OK;
error = caccess(fname, fmode, startvp);
if (startvp != NULL)
VN_RELE(startvp);
if (error)
return (set_errno(error));
return (0);
}
int
access(char *fname, int fmode)
{
return (faccessat(AT_FDCWD, fname, fmode, 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/kmem.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/cmn_err.h>
#include <sys/user.h>
#include <sys/cred.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/pathname.h>
#include <sys/modctl.h>
#include <sys/acctctl.h>
#include <sys/bitmap.h>
#include <sys/exacct.h>
#include <sys/policy.h>
/*
* acctctl(2)
*
* acctctl() provides the administrative interface to the extended accounting
* subsystem. The process and task accounting facilities are configurable:
* resources can be individually specified for recording in the appropriate
* accounting file.
*
* The current implementation of acctctl() requires that the process and task
* and flow files be distinct across all zones.
*
* Locking
* Each accounting species has an ac_info_t which contains a mutex,
* used to protect the ac_info_t's contents, and to serialize access to the
* appropriate file.
*/
static list_t exacct_globals_list;
static kmutex_t exacct_globals_list_lock;
static int
ac_state_set(ac_info_t *info, void *buf, size_t bufsz)
{
int state;
if (buf == NULL || (bufsz != sizeof (int)))
return (EINVAL);
if (copyin(buf, &state, bufsz) != 0)
return (EFAULT);
if (state != AC_ON && state != AC_OFF)
return (EINVAL);
mutex_enter(&info->ac_lock);
info->ac_state = state;
mutex_exit(&info->ac_lock);
return (0);
}
static int
ac_state_get(ac_info_t *info, void *buf, size_t bufsz)
{
if (buf == NULL || (bufsz != sizeof (int)))
return (EINVAL);
mutex_enter(&info->ac_lock);
if (copyout(&info->ac_state, buf, bufsz) != 0) {
mutex_exit(&info->ac_lock);
return (EFAULT);
}
mutex_exit(&info->ac_lock);
return (0);
}
static boolean_t
ac_file_in_use(vnode_t *vp)
{
boolean_t in_use = B_FALSE;
struct exacct_globals *acg;
if (vp == NULL)
return (B_FALSE);
mutex_enter(&exacct_globals_list_lock);
/*
* Start off by grabbing all locks.
*/
for (acg = list_head(&exacct_globals_list); acg != NULL;
acg = list_next(&exacct_globals_list, acg)) {
mutex_enter(&acg->ac_proc.ac_lock);
mutex_enter(&acg->ac_task.ac_lock);
mutex_enter(&acg->ac_flow.ac_lock);
mutex_enter(&acg->ac_net.ac_lock);
}
for (acg = list_head(&exacct_globals_list); !in_use && acg != NULL;
acg = list_next(&exacct_globals_list, acg)) {
/*
* We need to verify that we aren't already using this file for
* accounting in any zone.
*/
if (vn_compare(acg->ac_proc.ac_vnode, vp) ||
vn_compare(acg->ac_task.ac_vnode, vp) ||
vn_compare(acg->ac_flow.ac_vnode, vp) ||
vn_compare(acg->ac_net.ac_vnode, vp))
in_use = B_TRUE;
}
/*
* Drop all locks.
*/
for (acg = list_head(&exacct_globals_list); acg != NULL;
acg = list_next(&exacct_globals_list, acg)) {
mutex_exit(&acg->ac_proc.ac_lock);
mutex_exit(&acg->ac_task.ac_lock);
mutex_exit(&acg->ac_flow.ac_lock);
mutex_exit(&acg->ac_net.ac_lock);
}
mutex_exit(&exacct_globals_list_lock);
return (in_use);
}
static int
ac_file_set(ac_info_t *info, void *ubuf, size_t bufsz)
{
int error = 0;
void *kbuf;
void *namebuf;
int namelen;
vnode_t *vp;
void *hdr;
size_t hdrsize;
vattr_t va;
if (ubuf == NULL) {
mutex_enter(&info->ac_lock);
/*
* Closing accounting file
*/
if (info->ac_vnode != NULL) {
error = VOP_CLOSE(info->ac_vnode, FWRITE, 1, 0,
CRED(), NULL);
if (error) {
mutex_exit(&info->ac_lock);
return (error);
}
VN_RELE(info->ac_vnode);
info->ac_vnode = NULL;
}
if (info->ac_file != NULL) {
kmem_free(info->ac_file, strlen(info->ac_file) + 1);
info->ac_file = NULL;
}
mutex_exit(&info->ac_lock);
return (error);
}
if (bufsz < 2 || bufsz > MAXPATHLEN)
return (EINVAL);
/*
* We have to copy in the whole buffer since we can't tell the length
* of the string in user's address space.
*/
kbuf = kmem_zalloc(bufsz, KM_SLEEP);
if ((error = copyinstr((char *)ubuf, (char *)kbuf, bufsz, NULL)) != 0) {
kmem_free(kbuf, bufsz);
return (error);
}
if (*((char *)kbuf) != '/') {
kmem_free(kbuf, bufsz);
return (EINVAL);
}
/*
* Now, allocate the space where we are going to save the
* name of the accounting file and kmem_free kbuf. We have to do this
* now because it is not good to sleep in kmem_alloc() while
* holding ac_info's lock.
*/
namelen = strlen(kbuf) + 1;
namebuf = kmem_alloc(namelen, KM_SLEEP);
(void) strcpy(namebuf, kbuf);
kmem_free(kbuf, bufsz);
/*
* Check if this file already exists.
*/
error = lookupname(namebuf, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
/*
* Check if the file is already in use.
*/
if (!error) {
if (ac_file_in_use(vp)) {
/*
* If we're already using it then return EBUSY
*/
kmem_free(namebuf, namelen);
VN_RELE(vp);
return (EBUSY);
}
VN_RELE(vp);
}
/*
* Create an exacct header here because exacct_create_header() may
* sleep so we should not be holding ac_lock. At this point we cannot
* reliably know if we need the header or not, so we may end up not
* using the header.
*/
hdr = exacct_create_header(&hdrsize);
/*
* Now, grab info's ac_lock and try to set up everything.
*/
mutex_enter(&info->ac_lock);
if ((error = vn_open(namebuf, UIO_SYSSPACE,
FCREAT | FWRITE | FOFFMAX, 0600, &vp, CRCREAT, 0)) != 0) {
mutex_exit(&info->ac_lock);
kmem_free(namebuf, namelen);
kmem_free(hdr, hdrsize);
return (error);
}
if (vp->v_type != VREG) {
VN_RELE(vp);
mutex_exit(&info->ac_lock);
kmem_free(namebuf, namelen);
kmem_free(hdr, hdrsize);
return (EACCES);
}
if (info->ac_vnode != NULL) {
/*
* Switch from an old file to a new file by swapping
* their vnode pointers.
*/
vnode_t *oldvp;
oldvp = info->ac_vnode;
info->ac_vnode = vp;
vp = oldvp;
} else {
/*
* Start writing accounting records to a new file.
*/
info->ac_vnode = vp;
vp = NULL;
}
if (vp) {
/*
* We still need to close the old file.
*/
if ((error = VOP_CLOSE(vp, FWRITE, 1, 0, CRED(), NULL)) != 0) {
VN_RELE(vp);
mutex_exit(&info->ac_lock);
kmem_free(namebuf, namelen);
kmem_free(hdr, hdrsize);
return (error);
}
VN_RELE(vp);
if (info->ac_file != NULL) {
kmem_free(info->ac_file,
strlen(info->ac_file) + 1);
info->ac_file = NULL;
}
}
info->ac_file = namebuf;
/*
* Write the exacct header only if the file is empty.
*/
error = VOP_GETATTR(info->ac_vnode, &va, AT_SIZE, CRED(), NULL);
if (error == 0 && va.va_size == 0)
error = exacct_write_header(info, hdr, hdrsize);
mutex_exit(&info->ac_lock);
kmem_free(hdr, hdrsize);
return (error);
}
static int
ac_file_get(ac_info_t *info, void *buf, size_t bufsz)
{
int error = 0;
vnode_t *vnode;
char *file;
mutex_enter(&info->ac_lock);
file = info->ac_file;
vnode = info->ac_vnode;
if (file == NULL || vnode == NULL) {
mutex_exit(&info->ac_lock);
return (ENOTACTIVE);
}
if (strlen(file) >= bufsz)
error = ENOMEM;
else
error = copyoutstr(file, buf, MAXPATHLEN, NULL);
mutex_exit(&info->ac_lock);
return (error);
}
static int
ac_res_set(ac_info_t *info, void *buf, size_t bufsz, int maxres)
{
ac_res_t *res;
ac_res_t *tmp;
ulong_t *maskp;
int id;
uint_t counter = 0;
/*
* Validate that a non-zero buffer, sized within limits and to an
* integral number of ac_res_t's has been specified.
*/
if (bufsz == 0 ||
bufsz > sizeof (ac_res_t) * (AC_MAX_RES + 1) ||
(bufsz / sizeof (ac_res_t)) * sizeof (ac_res_t) != bufsz)
return (EINVAL);
tmp = res = kmem_alloc(bufsz, KM_SLEEP);
if (copyin(buf, res, bufsz) != 0) {
kmem_free(res, bufsz);
return (EFAULT);
}
maskp = (ulong_t *)&info->ac_mask;
mutex_enter(&info->ac_lock);
while ((id = tmp->ar_id) != AC_NONE && counter < maxres + 1) {
if (id > maxres || id < 0) {
mutex_exit(&info->ac_lock);
kmem_free(res, bufsz);
return (EINVAL);
}
if (tmp->ar_state == AC_ON) {
BT_SET(maskp, id);
} else if (tmp->ar_state == AC_OFF) {
BT_CLEAR(maskp, id);
} else {
mutex_exit(&info->ac_lock);
kmem_free(res, bufsz);
return (EINVAL);
}
tmp++;
counter++;
}
mutex_exit(&info->ac_lock);
kmem_free(res, bufsz);
return (0);
}
static int
ac_res_get(ac_info_t *info, void *buf, size_t bufsz, int maxres)
{
int error = 0;
ac_res_t *res;
ac_res_t *tmp;
size_t ressz = sizeof (ac_res_t) * (maxres + 1);
ulong_t *maskp;
int id;
if (bufsz < ressz)
return (EINVAL);
tmp = res = kmem_alloc(ressz, KM_SLEEP);
mutex_enter(&info->ac_lock);
maskp = (ulong_t *)&info->ac_mask;
for (id = 1; id <= maxres; id++) {
tmp->ar_id = id;
tmp->ar_state = BT_TEST(maskp, id);
tmp++;
}
tmp->ar_id = AC_NONE;
tmp->ar_state = AC_OFF;
mutex_exit(&info->ac_lock);
error = copyout(res, buf, ressz);
kmem_free(res, ressz);
return (error);
}
/*
* acctctl()
*
* Overview
* acctctl() is the entry point for the acctctl(2) system call.
*
* Return values
* On successful completion, return 0; otherwise -1 is returned and errno is
* set appropriately.
*
* Caller's context
* Called from the system call path.
*/
int
acctctl(int cmd, void *buf, size_t bufsz)
{
int error = 0;
int mode = AC_MODE(cmd);
int option = AC_OPTION(cmd);
int maxres;
ac_info_t *info;
zone_t *zone = curproc->p_zone;
struct exacct_globals *acg;
acg = zone_getspecific(exacct_zone_key, zone);
/*
* exacct_zone_key and associated per-zone state were initialized when
* the module was loaded.
*/
ASSERT(exacct_zone_key != ZONE_KEY_UNINITIALIZED);
ASSERT(acg != NULL);
switch (mode) { /* sanity check */
case AC_TASK:
info = &acg->ac_task;
maxres = AC_TASK_MAX_RES;
break;
case AC_PROC:
info = &acg->ac_proc;
maxres = AC_PROC_MAX_RES;
break;
/*
* Flow/net accounting isn't configurable in non-global
* zones, but we have this field on a per-zone basis for future
* expansion as well as the ability to return default "unset"
* values for the various AC_*_GET queries. AC_*_SET commands
* fail with EPERM for AC_FLOW and AC_NET in non-global zones.
*/
case AC_FLOW:
info = &acg->ac_flow;
maxres = AC_FLOW_MAX_RES;
break;
case AC_NET:
info = &acg->ac_net;
maxres = AC_NET_MAX_RES;
break;
default:
return (set_errno(EINVAL));
}
switch (option) {
case AC_STATE_SET:
if ((error = secpolicy_acct(CRED())) != 0)
break;
if ((mode == AC_FLOW || mode == AC_NET) &&
getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
break;
}
error = ac_state_set(info, buf, bufsz);
break;
case AC_STATE_GET:
error = ac_state_get(info, buf, bufsz);
break;
case AC_FILE_SET:
if ((error = secpolicy_acct(CRED())) != 0)
break;
if ((mode == AC_FLOW || mode == AC_NET) &&
getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
break;
}
error = ac_file_set(info, buf, bufsz);
break;
case AC_FILE_GET:
error = ac_file_get(info, buf, bufsz);
break;
case AC_RES_SET:
if ((error = secpolicy_acct(CRED())) != 0)
break;
if ((mode == AC_FLOW || mode == AC_NET) &&
getzoneid() != GLOBAL_ZONEID) {
error = EPERM;
break;
}
error = ac_res_set(info, buf, bufsz, maxres);
break;
case AC_RES_GET:
error = ac_res_get(info, buf, bufsz, maxres);
break;
default:
return (set_errno(EINVAL));
}
if (error)
return (set_errno(error));
return (0);
}
static struct sysent ac_sysent = {
3,
SE_NOUNLOAD | SE_ARGC | SE_32RVAL1,
acctctl
};
static struct modlsys modlsys = {
&mod_syscallops,
"acctctl system call",
&ac_sysent
};
#ifdef _SYSCALL32_IMPL
static struct modlsys modlsys32 = {
&mod_syscallops32,
"32-bit acctctl system call",
&ac_sysent
};
#endif
static struct modlinkage modlinkage = {
MODREV_1,
&modlsys,
#ifdef _SYSCALL32_IMPL
&modlsys32,
#endif
NULL
};
/* ARGSUSED */
static void *
exacct_zone_init(zoneid_t zoneid)
{
struct exacct_globals *acg;
acg = kmem_zalloc(sizeof (*acg), KM_SLEEP);
mutex_enter(&exacct_globals_list_lock);
list_insert_tail(&exacct_globals_list, acg);
mutex_exit(&exacct_globals_list_lock);
return (acg);
}
static void
exacct_free_info(ac_info_t *info)
{
mutex_enter(&info->ac_lock);
if (info->ac_vnode) {
(void) VOP_CLOSE(info->ac_vnode, FWRITE, 1, 0, kcred, NULL);
VN_RELE(info->ac_vnode);
kmem_free(info->ac_file, strlen(info->ac_file) + 1);
}
info->ac_state = AC_OFF;
info->ac_vnode = NULL;
info->ac_file = NULL;
mutex_exit(&info->ac_lock);
}
/* ARGSUSED */
static void
exacct_zone_shutdown(zoneid_t zoneid, void *data)
{
struct exacct_globals *acg = data;
/*
* The accounting files need to be closed during shutdown rather than
* destroy, since otherwise the filesystem they reside on may fail to
* unmount, thus causing the entire zone halt/reboot to fail.
*/
exacct_free_info(&acg->ac_proc);
exacct_free_info(&acg->ac_task);
exacct_free_info(&acg->ac_flow);
exacct_free_info(&acg->ac_net);
}
/* ARGSUSED */
static void
exacct_zone_fini(zoneid_t zoneid, void *data)
{
struct exacct_globals *acg = data;
mutex_enter(&exacct_globals_list_lock);
list_remove(&exacct_globals_list, acg);
mutex_exit(&exacct_globals_list_lock);
mutex_destroy(&acg->ac_proc.ac_lock);
mutex_destroy(&acg->ac_task.ac_lock);
mutex_destroy(&acg->ac_flow.ac_lock);
mutex_destroy(&acg->ac_net.ac_lock);
kmem_free(acg, sizeof (*acg));
}
int
_init()
{
int error;
mutex_init(&exacct_globals_list_lock, NULL, MUTEX_DEFAULT, NULL);
list_create(&exacct_globals_list, sizeof (struct exacct_globals),
offsetof(struct exacct_globals, ac_link));
zone_key_create(&exacct_zone_key, exacct_zone_init,
exacct_zone_shutdown, exacct_zone_fini);
if ((error = mod_install(&modlinkage)) != 0) {
(void) zone_key_delete(exacct_zone_key);
exacct_zone_key = ZONE_KEY_UNINITIALIZED;
mutex_destroy(&exacct_globals_list_lock);
list_destroy(&exacct_globals_list);
}
return (error);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
int
_fini()
{
return (EBUSY);
}
/*
* 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.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/pathname.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/mode.h>
#include <sys/uio.h>
#include <sys/kmem.h>
#include <sys/filio.h>
#include <sys/acl.h>
#include <sys/cmn_err.h>
#include <acl/acl_common.h>
#include <sys/unistd.h>
#include <sys/debug.h>
#include <fs/fs_subr.h>
static int cacl(int cmd, int nentries, void *aclbufp,
vnode_t *vp, int *rv);
/*
* Get/Set ACL of a file.
*/
int
acl(const char *fname, int cmd, int nentries, void *aclbufp)
{
struct vnode *vp;
int error;
int rv = 0;
int estale_retry = 0;
/* Sanity check arguments */
if (fname == NULL)
return (set_errno(EINVAL));
lookup:
error = lookupname((char *)fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp);
if (error) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
error = cacl(cmd, nentries, aclbufp, vp, &rv);
VN_RELE(vp);
if (error) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (rv);
}
/*
* Get/Set ACL of a file with facl system call.
*/
int
facl(int fdes, int cmd, int nentries, void *aclbufp)
{
file_t *fp;
int error;
int rv = 0;
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
if (fp->f_flag & FREVOKED) {
releasef(fdes);
return (set_errno(EBADF));
}
error = cacl(cmd, nentries, aclbufp, fp->f_vnode, &rv);
releasef(fdes);
if (error)
return (set_errno(error));
return (rv);
}
/*
* Common code for acl() and facl().
*/
static int
cacl(int cmd, int nentries, void *aclbufp, vnode_t *vp, int *rv)
{
int error;
int aclbsize; /* size of acl list in bytes */
int dfaclbsize; /* size of default acl list in bytes */
int numacls;
caddr_t uaddrp;
aclent_t *aclp, *aaclp;
vsecattr_t vsecattr;
size_t entry_size;
ASSERT(vp);
bzero(&vsecattr, sizeof (vsecattr_t));
dfaclbsize = 0;
switch (cmd) {
case ACE_GETACLCNT:
case GETACLCNT:
if (cmd == GETACLCNT) {
entry_size = sizeof (aclent_t);
vsecattr.vsa_mask = VSA_ACLCNT | VSA_DFACLCNT;
} else {
entry_size = sizeof (ace_t);
vsecattr.vsa_mask = VSA_ACECNT;
}
if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED(), NULL))
return (error);
*rv = vsecattr.vsa_aclcnt + vsecattr.vsa_dfaclcnt;
if (vsecattr.vsa_aclcnt && vsecattr.vsa_aclentp) {
kmem_free(vsecattr.vsa_aclentp,
vsecattr.vsa_aclcnt * entry_size);
}
if (vsecattr.vsa_dfaclcnt && vsecattr.vsa_dfaclentp) {
kmem_free(vsecattr.vsa_dfaclentp,
vsecattr.vsa_dfaclcnt * entry_size);
}
break;
case GETACL:
/*
* Minimum ACL size is three entries so might as well
* bail out here.
*/
if (nentries < 3)
return (EINVAL);
/*
* NULL output buffer is also a pretty easy bail out.
*/
if (aclbufp == NULL)
return (EFAULT);
vsecattr.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL |
VSA_DFACLCNT;
if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED(), NULL))
return (error);
/* Check user's buffer is big enough */
numacls = vsecattr.vsa_aclcnt + vsecattr.vsa_dfaclcnt;
aclbsize = vsecattr.vsa_aclcnt * sizeof (aclent_t);
dfaclbsize = vsecattr.vsa_dfaclcnt * sizeof (aclent_t);
if (numacls > nentries) {
error = ENOSPC;
goto errout;
}
/* Sort the acl & default acl lists */
if (vsecattr.vsa_aclcnt > 1)
ksort((caddr_t)vsecattr.vsa_aclentp,
vsecattr.vsa_aclcnt, sizeof (aclent_t), cmp2acls);
if (vsecattr.vsa_dfaclcnt > 1)
ksort((caddr_t)vsecattr.vsa_dfaclentp,
vsecattr.vsa_dfaclcnt, sizeof (aclent_t), cmp2acls);
/* Copy out acl's */
uaddrp = (caddr_t)aclbufp;
if (aclbsize > 0) { /* bug #1262490 */
if (copyout(vsecattr.vsa_aclentp, uaddrp, aclbsize)) {
error = EFAULT;
goto errout;
}
}
/* Copy out default acl's */
if (dfaclbsize > 0) {
uaddrp += aclbsize;
if (copyout(vsecattr.vsa_dfaclentp,
uaddrp, dfaclbsize)) {
error = EFAULT;
goto errout;
}
}
*rv = numacls;
if (vsecattr.vsa_aclcnt) {
kmem_free(vsecattr.vsa_aclentp,
vsecattr.vsa_aclcnt * sizeof (aclent_t));
}
if (vsecattr.vsa_dfaclcnt) {
kmem_free(vsecattr.vsa_dfaclentp,
vsecattr.vsa_dfaclcnt * sizeof (aclent_t));
}
break;
case ACE_GETACL:
if (aclbufp == NULL)
return (EFAULT);
vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED(), NULL))
return (error);
aclbsize = vsecattr.vsa_aclcnt * sizeof (ace_t);
if (vsecattr.vsa_aclcnt > nentries) {
error = ENOSPC;
goto errout;
}
if (aclbsize > 0) {
if ((error = copyout(vsecattr.vsa_aclentp,
aclbufp, aclbsize)) != 0) {
goto errout;
}
}
*rv = vsecattr.vsa_aclcnt;
if (vsecattr.vsa_aclcnt) {
kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
}
break;
case SETACL:
/*
* Minimum ACL size is three entries so might as well
* bail out here. Also limit request size to prevent user
* from allocating too much kernel memory. Maximum size
* is MAX_ACL_ENTRIES for the ACL part and MAX_ACL_ENTRIES
* for the default ACL part. (bug 4058667)
*/
if (nentries < 3 || nentries > (MAX_ACL_ENTRIES * 2))
return (EINVAL);
/*
* NULL output buffer is also an easy bail out.
*/
if (aclbufp == NULL)
return (EFAULT);
vsecattr.vsa_mask = VSA_ACL;
aclbsize = nentries * sizeof (aclent_t);
vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
aaclp = vsecattr.vsa_aclentp;
vsecattr.vsa_aclcnt = nentries;
uaddrp = (caddr_t)aclbufp;
if (copyin(uaddrp, vsecattr.vsa_aclentp, aclbsize)) {
kmem_free(aaclp, aclbsize);
return (EFAULT);
}
/* Sort the acl list */
ksort((caddr_t)vsecattr.vsa_aclentp,
vsecattr.vsa_aclcnt, sizeof (aclent_t), cmp2acls);
/* Break into acl and default acl lists */
for (numacls = 0, aclp = vsecattr.vsa_aclentp;
numacls < vsecattr.vsa_aclcnt;
aclp++, numacls++) {
if (aclp->a_type & ACL_DEFAULT)
break;
}
/* Find where defaults start (if any) */
if (numacls < vsecattr.vsa_aclcnt) {
vsecattr.vsa_mask |= VSA_DFACL;
vsecattr.vsa_dfaclcnt = nentries - numacls;
vsecattr.vsa_dfaclentp = aclp;
vsecattr.vsa_aclcnt = numacls;
}
/* Adjust if they're all defaults */
if (vsecattr.vsa_aclcnt == 0) {
vsecattr.vsa_mask &= ~VSA_ACL;
vsecattr.vsa_aclentp = NULL;
}
/* Only directories can have defaults */
if (vsecattr.vsa_dfaclcnt && vp->v_type != VDIR) {
kmem_free(aaclp, aclbsize);
return (ENOTDIR);
}
(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
if (error = VOP_SETSECATTR(vp, &vsecattr, 0, CRED(), NULL)) {
kmem_free(aaclp, aclbsize);
VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
return (error);
}
/*
* Should return 0 upon success according to the man page
* and SVR4 semantics. (Bug #1214399: SETACL returns wrong rc)
*/
*rv = 0;
kmem_free(aaclp, aclbsize);
VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
break;
case ACE_SETACL:
if (nentries < 1 || nentries > MAX_ACL_ENTRIES)
return (EINVAL);
if (aclbufp == NULL)
return (EFAULT);
vsecattr.vsa_mask = VSA_ACE;
aclbsize = nentries * sizeof (ace_t);
vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
aaclp = vsecattr.vsa_aclentp;
vsecattr.vsa_aclcnt = nentries;
vsecattr.vsa_aclentsz = aclbsize;
uaddrp = (caddr_t)aclbufp;
if (copyin(uaddrp, vsecattr.vsa_aclentp, aclbsize)) {
kmem_free(aaclp, aclbsize);
return (EFAULT);
}
(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
if (error = VOP_SETSECATTR(vp, &vsecattr, 0, CRED(), NULL)) {
kmem_free(aaclp, aclbsize);
VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
return (error);
}
*rv = 0;
kmem_free(aaclp, aclbsize);
VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
break;
default:
return (EINVAL);
}
return (0);
errout:
if (aclbsize && vsecattr.vsa_aclentp)
kmem_free(vsecattr.vsa_aclentp, aclbsize);
if (dfaclbsize && vsecattr.vsa_dfaclentp)
kmem_free(vsecattr.vsa_dfaclentp, dfaclbsize);
return (error);
}
/*
* 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 (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 1999,2001-2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/tuneable.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/time.h>
#include <sys/debug.h>
#include <sys/model.h>
#include <sys/policy.h>
int
adjtime(const struct timeval *delta, struct timeval *olddelta)
{
struct timeval atv, oatv;
int64_t ndelta;
int64_t old_delta;
int s;
model_t datamodel = get_udatamodel();
if (secpolicy_settime(CRED()) != 0)
return (set_errno(EPERM));
if (datamodel == DATAMODEL_NATIVE) {
if (copyin(delta, &atv, sizeof (atv)))
return (set_errno(EFAULT));
} else {
struct timeval32 atv32;
if (copyin(delta, &atv32, sizeof (atv32)))
return (set_errno(EFAULT));
TIMEVAL32_TO_TIMEVAL(&atv, &atv32);
}
if (atv.tv_usec <= -MICROSEC || atv.tv_usec >= MICROSEC)
return (set_errno(EINVAL));
/*
* The SVID specifies that if delta is 0, then there is
* no effect upon time correction, just return olddelta.
*/
ndelta = (int64_t)atv.tv_sec * NANOSEC + atv.tv_usec * 1000;
mutex_enter(&tod_lock);
s = hr_clock_lock();
old_delta = timedelta;
if (ndelta)
timedelta = ndelta;
/*
* Always set tod_needsync on all adjtime() calls, since it implies
* someone is watching over us and keeping the local clock in sync.
*/
tod_needsync = 1;
hr_clock_unlock(s);
mutex_exit(&tod_lock);
if (olddelta) {
oatv.tv_sec = old_delta / NANOSEC;
oatv.tv_usec = (old_delta % NANOSEC) / 1000;
if (datamodel == DATAMODEL_NATIVE) {
if (copyout(&oatv, olddelta, sizeof (oatv)))
return (set_errno(EFAULT));
} else {
struct timeval32 oatv32;
if (TIMEVAL_OVERFLOW(&oatv))
return (set_errno(EOVERFLOW));
TIMEVAL_TO_TIMEVAL32(&oatv32, &oatv);
if (copyout(&oatv32, olddelta, sizeof (oatv32)))
return (set_errno(EFAULT));
}
}
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 (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/signal.h>
#include <sys/proc.h>
#include <sys/time.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
static void
sigalarm2proc(void *arg)
{
proc_t *p = arg;
mutex_enter(&p->p_lock);
p->p_alarmid = 0;
sigtoproc(p, NULL, SIGALRM);
mutex_exit(&p->p_lock);
}
int
alarm(int deltat)
{
proc_t *p = ttoproc(curthread);
clock_t del = 0;
clock_t ret;
timeout_id_t tmp_id;
clock_t delta = (uint_t)deltat;
/*
* We must single-thread this code relative to other
* lwps in the same process also performing an alarm().
* The mutex dance in the while loop is necessary because
* we cannot call untimeout() while holding a lock that
* is grabbed by the timeout function, sigalarm2proc().
* We can, however, hold p->p_lock across realtime_timeout().
*/
mutex_enter(&p->p_lock);
while ((tmp_id = p->p_alarmid) != 0) {
p->p_alarmid = 0;
mutex_exit(&p->p_lock);
del = untimeout(tmp_id);
mutex_enter(&p->p_lock);
}
if (del < 0)
ret = 0;
else
ret = (del + hz - 1) / hz; /* convert to seconds */
/*
* Our implementation defined limit for alarm is
* LONG_MAX / hz. Anything larger gets truncated
* to that limit. If delta is negative we can
* assume a wrap has occurred so peg delta in
* that case too. 64 bit platforms have higher limit.
*/
if (delta > (LONG_MAX / hz) || delta < 0)
delta = LONG_MAX / hz;
if (delta)
p->p_alarmid = realtime_timeout(sigalarm2proc, p, delta * hz);
mutex_exit(&p->p_lock);
return (ret);
}
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2020 The University of Queensland
*/
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/policy.h>
#include <c2/audit.h>
#include <c2/audit_kernel.h>
#include <c2/audit_record.h>
#define CLEAR_VAL -1
extern kmutex_t pidlock;
uint32_t audit_policy; /* global audit policies in force */
/*ARGSUSED1*/
int
auditsys(struct auditcalls *uap, rval_t *rvp)
{
int err;
int result = 0;
if (audit_active == C2AUDIT_DISABLED)
return (ENOTSUP);
switch (uap->code) {
case BSM_GETAUID:
result = getauid((caddr_t)uap->a1);
break;
case BSM_SETAUID:
result = setauid((caddr_t)uap->a1);
break;
case BSM_GETAUDIT:
result = getaudit((caddr_t)uap->a1);
break;
case BSM_GETAUDIT_ADDR:
result = getaudit_addr((caddr_t)uap->a1, (int)uap->a2);
break;
case BSM_SETAUDIT:
result = setaudit((caddr_t)uap->a1);
break;
case BSM_SETAUDIT_ADDR:
result = setaudit_addr((caddr_t)uap->a1, (int)uap->a2);
break;
case BSM_AUDITCTL:
result = auditctl((int)uap->a1, (caddr_t)uap->a2, (int)uap->a3);
break;
case BSM_AUDIT:
if (audit_active == C2AUDIT_UNLOADED)
return (0);
result = audit((caddr_t)uap->a1, (int)uap->a2);
break;
case BSM_AUDITDOOR:
if (audit_active == C2AUDIT_LOADED) {
result = auditdoor((int)uap->a1);
break;
}
/* FALLTHROUGH */
default:
if (audit_active == C2AUDIT_LOADED) {
result = EINVAL;
break;
}
/* Return a different error when not privileged */
err = secpolicy_audit_config(CRED());
if (err == 0)
return (EINVAL);
else
return (err);
}
rvp->r_vals = result;
return (result);
}
/*
* Return the audit user ID for the current process. Currently only
* the privileged processes may see the audit id. That may change.
* If copyout is unsucessful return EFAULT.
*/
int
getauid(caddr_t auid_p)
{
const auditinfo_addr_t *ainfo;
if (secpolicy_audit_getattr(CRED(), B_FALSE) != 0)
return (EPERM);
ainfo = crgetauinfo(CRED());
if (ainfo == NULL)
return (EINVAL);
if (copyout(&ainfo->ai_auid, auid_p, sizeof (au_id_t)))
return (EFAULT);
return (0);
}
/*
* Set the audit userid, for a process. This can only be changed by
* privileged processes. The audit userid is inherited across forks & execs.
* Passed in is a pointer to the au_id_t; if copyin unsuccessful return EFAULT.
*/
int
setauid(caddr_t auid_p)
{
proc_t *p;
au_id_t auid;
cred_t *newcred;
auditinfo_addr_t *auinfo;
if (secpolicy_audit_config(CRED()) != 0)
return (EPERM);
if (copyin(auid_p, &auid, sizeof (au_id_t))) {
return (EFAULT);
}
newcred = cralloc();
if ((auinfo = crgetauinfo_modifiable(newcred)) == NULL) {
crfree(newcred);
return (EINVAL);
}
/* grab p_crlock and switch to new cred */
p = curproc;
mutex_enter(&p->p_crlock);
crcopy_to(p->p_cred, newcred);
p->p_cred = newcred;
auinfo->ai_auid = auid; /* update the auid */
/* unlock and broadcast the cred changes */
mutex_exit(&p->p_crlock);
crset(p, newcred);
return (0);
}
/*
* Get the audit state information from the current process.
* Return EFAULT if copyout fails.
*/
int
getaudit(caddr_t info_p)
{
STRUCT_DECL(auditinfo, info);
const auditinfo_addr_t *ainfo;
model_t model;
if (secpolicy_audit_getattr(CRED(), B_FALSE) != 0)
return (EPERM);
model = get_udatamodel();
STRUCT_INIT(info, model);
ainfo = crgetauinfo(CRED());
if (ainfo == NULL)
return (EINVAL);
/* trying to read a process with an IPv6 address? */
if (ainfo->ai_termid.at_type == AU_IPv6)
return (EOVERFLOW);
STRUCT_FSET(info, ai_auid, ainfo->ai_auid);
STRUCT_FSET(info, ai_mask, ainfo->ai_mask);
#ifdef _LP64
if (model == DATAMODEL_ILP32) {
dev32_t dev;
/* convert internal 64 bit form to 32 bit version */
if (cmpldev(&dev, ainfo->ai_termid.at_port) == 0) {
return (EOVERFLOW);
}
STRUCT_FSET(info, ai_termid.port, dev);
} else
STRUCT_FSET(info, ai_termid.port, ainfo->ai_termid.at_port);
#else
STRUCT_FSET(info, ai_termid.port, ainfo->ai_termid.at_port);
#endif
STRUCT_FSET(info, ai_termid.machine, ainfo->ai_termid.at_addr[0]);
STRUCT_FSET(info, ai_asid, ainfo->ai_asid);
if (copyout(STRUCT_BUF(info), info_p, STRUCT_SIZE(info)))
return (EFAULT);
return (0);
}
/*
* Get the audit state information from the current process.
* Return EFAULT if copyout fails.
*/
int
getaudit_addr(caddr_t info_p, int len)
{
STRUCT_DECL(auditinfo_addr, info);
const auditinfo_addr_t *ainfo;
model_t model;
if (secpolicy_audit_getattr(CRED(), B_FALSE) != 0)
return (EPERM);
model = get_udatamodel();
STRUCT_INIT(info, model);
if (len < STRUCT_SIZE(info))
return (EOVERFLOW);
ainfo = crgetauinfo(CRED());
if (ainfo == NULL)
return (EINVAL);
STRUCT_FSET(info, ai_auid, ainfo->ai_auid);
STRUCT_FSET(info, ai_mask, ainfo->ai_mask);
#ifdef _LP64
if (model == DATAMODEL_ILP32) {
dev32_t dev;
/* convert internal 64 bit form to 32 bit version */
if (cmpldev(&dev, ainfo->ai_termid.at_port) == 0) {
return (EOVERFLOW);
}
STRUCT_FSET(info, ai_termid.at_port, dev);
} else
STRUCT_FSET(info, ai_termid.at_port, ainfo->ai_termid.at_port);
#else
STRUCT_FSET(info, ai_termid.at_port, ainfo->ai_termid.at_port);
#endif
STRUCT_FSET(info, ai_termid.at_type, ainfo->ai_termid.at_type);
STRUCT_FSET(info, ai_termid.at_addr[0], ainfo->ai_termid.at_addr[0]);
STRUCT_FSET(info, ai_termid.at_addr[1], ainfo->ai_termid.at_addr[1]);
STRUCT_FSET(info, ai_termid.at_addr[2], ainfo->ai_termid.at_addr[2]);
STRUCT_FSET(info, ai_termid.at_addr[3], ainfo->ai_termid.at_addr[3]);
STRUCT_FSET(info, ai_asid, ainfo->ai_asid);
if (copyout(STRUCT_BUF(info), info_p, STRUCT_SIZE(info)))
return (EFAULT);
return (0);
}
/*
* Set the audit state information for the current process.
* Return EFAULT if copyout fails.
*/
int
setaudit(caddr_t info_p)
{
STRUCT_DECL(auditinfo, info);
proc_t *p;
cred_t *newcred;
model_t model;
auditinfo_addr_t *ainfo;
if (secpolicy_audit_config(CRED()) != 0)
return (EPERM);
model = get_udatamodel();
STRUCT_INIT(info, model);
if (copyin(info_p, STRUCT_BUF(info), STRUCT_SIZE(info)))
return (EFAULT);
newcred = cralloc();
if ((ainfo = crgetauinfo_modifiable(newcred)) == NULL) {
crfree(newcred);
return (EINVAL);
}
/* grab p_crlock and switch to new cred */
p = curproc;
mutex_enter(&p->p_crlock);
crcopy_to(p->p_cred, newcred);
p->p_cred = newcred;
/* Set audit mask, id, termid and session id as specified */
ainfo->ai_auid = STRUCT_FGET(info, ai_auid);
#ifdef _LP64
/* only convert to 64 bit if coming from a 32 bit binary */
if (model == DATAMODEL_ILP32)
ainfo->ai_termid.at_port =
DEVEXPL(STRUCT_FGET(info, ai_termid.port));
else
ainfo->ai_termid.at_port = STRUCT_FGET(info, ai_termid.port);
#else
ainfo->ai_termid.at_port = STRUCT_FGET(info, ai_termid.port);
#endif
ainfo->ai_termid.at_type = AU_IPv4;
ainfo->ai_termid.at_addr[0] = STRUCT_FGET(info, ai_termid.machine);
ainfo->ai_asid = STRUCT_FGET(info, ai_asid);
ainfo->ai_mask = STRUCT_FGET(info, ai_mask);
/* unlock and broadcast the cred changes */
mutex_exit(&p->p_crlock);
crset(p, newcred);
return (0);
}
/*
* Set the audit state information for the current process.
* Return EFAULT if copyin fails.
*/
int
setaudit_addr(caddr_t info_p, int len)
{
STRUCT_DECL(auditinfo_addr, info);
proc_t *p;
cred_t *newcred;
model_t model;
int i;
int type;
auditinfo_addr_t *ainfo;
if (secpolicy_audit_config(CRED()) != 0)
return (EPERM);
model = get_udatamodel();
STRUCT_INIT(info, model);
if (len < STRUCT_SIZE(info))
return (EOVERFLOW);
if (copyin(info_p, STRUCT_BUF(info), STRUCT_SIZE(info)))
return (EFAULT);
type = STRUCT_FGET(info, ai_termid.at_type);
if ((type != AU_IPv4) && (type != AU_IPv6))
return (EINVAL);
newcred = cralloc();
if ((ainfo = crgetauinfo_modifiable(newcred)) == NULL) {
crfree(newcred);
return (EINVAL);
}
/* grab p_crlock and switch to new cred */
p = curproc;
mutex_enter(&p->p_crlock);
crcopy_to(p->p_cred, newcred);
p->p_cred = newcred;
/* Set audit mask, id, termid and session id as specified */
ainfo->ai_auid = STRUCT_FGET(info, ai_auid);
ainfo->ai_mask = STRUCT_FGET(info, ai_mask);
#ifdef _LP64
/* only convert to 64 bit if coming from a 32 bit binary */
if (model == DATAMODEL_ILP32)
ainfo->ai_termid.at_port =
DEVEXPL(STRUCT_FGET(info, ai_termid.at_port));
else
ainfo->ai_termid.at_port = STRUCT_FGET(info, ai_termid.at_port);
#else
ainfo->ai_termid.at_port = STRUCT_FGET(info, ai_termid.at_port);
#endif
ainfo->ai_termid.at_type = type;
bzero(&ainfo->ai_termid.at_addr[0], sizeof (ainfo->ai_termid.at_addr));
for (i = 0; i < (type/sizeof (int)); i++)
ainfo->ai_termid.at_addr[i] =
STRUCT_FGET(info, ai_termid.at_addr[i]);
if (ainfo->ai_termid.at_type == AU_IPv6 &&
IN6_IS_ADDR_V4MAPPED(((in6_addr_t *)ainfo->ai_termid.at_addr))) {
ainfo->ai_termid.at_type = AU_IPv4;
ainfo->ai_termid.at_addr[0] = ainfo->ai_termid.at_addr[3];
ainfo->ai_termid.at_addr[1] = 0;
ainfo->ai_termid.at_addr[2] = 0;
ainfo->ai_termid.at_addr[3] = 0;
}
ainfo->ai_asid = STRUCT_FGET(info, ai_asid);
/* unlock and broadcast the cred changes */
mutex_exit(&p->p_crlock);
crset(p, newcred);
return (0);
}
/*
* Get the global policy flag
*/
static int
getpolicy(caddr_t data)
{
uint32_t policy;
au_kcontext_t *kctx = GET_KCTX_PZ;
policy = audit_policy | kctx->auk_policy;
if (copyout(&policy, data, sizeof (policy)))
return (EFAULT);
return (0);
}
/*
* Set the global and local policy flags
*
* The global flags only make sense from the global zone;
* the local flags depend on the AUDIT_PERZONE policy:
* if the perzone policy is set, then policy is set separately
* per zone, else held only in the global zone.
*
* The initial value of a local zone's policy flag is determined
* by the value of the global zone's flags at the time the
* local zone is created.
*
* While auditconfig(8) allows setting and unsetting policies one bit
* at a time, the mask passed in from auditconfig() is created by a
* syscall to getpolicy and then modified based on the auditconfig()
* cmd line, so the input policy value is used to replace the existing
* policy.
*/
static int
setpolicy(caddr_t data)
{
uint32_t policy;
au_kcontext_t *kctx;
if (copyin(data, &policy, sizeof (policy)))
return (EFAULT);
kctx = GET_KCTX_NGZ;
if (INGLOBALZONE(curproc)) {
if (policy & ~(AUDIT_GLOBAL | AUDIT_LOCAL))
return (EINVAL);
audit_policy = policy & AUDIT_GLOBAL;
} else {
if (!(audit_policy & AUDIT_PERZONE))
return (EINVAL);
if (policy & ~AUDIT_LOCAL) /* global bits are a no-no */
return (EINVAL);
}
kctx->auk_policy = policy & AUDIT_LOCAL;
/*
* auk_current_vp is NULL before auditd starts (or during early
* auditd starup) or if auditd is halted; in either case,
* notification of a policy change is not needed, since auditd
* reads policy as it comes up. The error return from au_doormsg()
* is ignored to avoid a race condition -- for example if auditd
* segv's, the audit state may be "auditing" but the door may
* be closed. Returning an error if the door is open makes it
* impossible for Greenline to restart auditd.
*
* Note that auk_current_vp can change (e.g. to NULL) as long as we
* aren't holding auk_svc_lock -- so au_door_upcall() will eventually
* double-check this condition after it takes auk_svc_lock.
*/
if (kctx->auk_current_vp != NULL)
(void) au_doormsg(kctx, AU_DBUF_POLICY, &policy);
/*
* Wake up anyone who might have blocked on full audit
* partitions. audit daemons need to set AUDIT_FULL when no
* space so we can tell if we should start dropping records.
*/
mutex_enter(&(kctx->auk_queue.lock));
if ((policy & (AUDIT_CNT | AUDIT_SCNT) &&
(kctx->auk_queue.cnt >= kctx->auk_queue.hiwater)))
cv_broadcast(&(kctx->auk_queue.write_cv));
mutex_exit(&(kctx->auk_queue.lock));
return (0);
}
static int
getamask(caddr_t data)
{
au_kcontext_t *kctx;
kctx = GET_KCTX_PZ;
if (copyout(&kctx->auk_info.ai_amask, data, sizeof (au_mask_t)))
return (EFAULT);
return (0);
}
static int
setamask(caddr_t data)
{
au_mask_t mask;
au_kcontext_t *kctx;
if (!(audit_policy & AUDIT_PERZONE) && !INGLOBALZONE(curproc))
return (EINVAL);
kctx = GET_KCTX_NGZ;
if (copyin(data, &mask, sizeof (au_mask_t)))
return (EFAULT);
kctx->auk_info.ai_amask = mask;
return (0);
}
static int
getkmask(caddr_t data)
{
au_kcontext_t *kctx;
kctx = GET_KCTX_PZ;
if (copyout(&kctx->auk_info.ai_namask, data, sizeof (au_mask_t)))
return (EFAULT);
return (0);
}
static int
setkmask(caddr_t data)
{
au_mask_t mask;
au_kcontext_t *kctx;
if (!(audit_policy & AUDIT_PERZONE) && !INGLOBALZONE(curproc))
return (EINVAL);
kctx = GET_KCTX_NGZ;
if (copyin(data, &mask, sizeof (au_mask_t)))
return (EFAULT);
kctx->auk_info.ai_namask = mask;
return (0);
}
static int
getkaudit(caddr_t info_p, int len)
{
STRUCT_DECL(auditinfo_addr, info);
model_t model;
au_kcontext_t *kctx = GET_KCTX_PZ;
model = get_udatamodel();
STRUCT_INIT(info, model);
if (len < STRUCT_SIZE(info))
return (EOVERFLOW);
STRUCT_FSET(info, ai_auid, kctx->auk_info.ai_auid);
STRUCT_FSET(info, ai_mask, kctx->auk_info.ai_namask);
#ifdef _LP64
if (model == DATAMODEL_ILP32) {
dev32_t dev;
/* convert internal 64 bit form to 32 bit version */
if (cmpldev(&dev, kctx->auk_info.ai_termid.at_port) == 0) {
return (EOVERFLOW);
}
STRUCT_FSET(info, ai_termid.at_port, dev);
} else {
STRUCT_FSET(info, ai_termid.at_port,
kctx->auk_info.ai_termid.at_port);
}
#else
STRUCT_FSET(info, ai_termid.at_port,
kctx->auk_info.ai_termid.at_port);
#endif
STRUCT_FSET(info, ai_termid.at_type,
kctx->auk_info.ai_termid.at_type);
STRUCT_FSET(info, ai_termid.at_addr[0],
kctx->auk_info.ai_termid.at_addr[0]);
STRUCT_FSET(info, ai_termid.at_addr[1],
kctx->auk_info.ai_termid.at_addr[1]);
STRUCT_FSET(info, ai_termid.at_addr[2],
kctx->auk_info.ai_termid.at_addr[2]);
STRUCT_FSET(info, ai_termid.at_addr[3],
kctx->auk_info.ai_termid.at_addr[3]);
STRUCT_FSET(info, ai_asid, kctx->auk_info.ai_asid);
if (copyout(STRUCT_BUF(info), info_p, STRUCT_SIZE(info)))
return (EFAULT);
return (0);
}
/*
* the host address for AUDIT_PERZONE == 0 is that of the global
* zone and for local zones it is of the current zone.
*/
static int
setkaudit(caddr_t info_p, int len)
{
STRUCT_DECL(auditinfo_addr, info);
model_t model;
au_kcontext_t *kctx;
if (!(audit_policy & AUDIT_PERZONE) && !INGLOBALZONE(curproc))
return (EINVAL);
kctx = GET_KCTX_NGZ;
model = get_udatamodel();
STRUCT_INIT(info, model);
if (len < STRUCT_SIZE(info))
return (EOVERFLOW);
if (copyin(info_p, STRUCT_BUF(info), STRUCT_SIZE(info)))
return (EFAULT);
if ((STRUCT_FGET(info, ai_termid.at_type) != AU_IPv4) &&
(STRUCT_FGET(info, ai_termid.at_type) != AU_IPv6))
return (EINVAL);
/* Set audit mask, termid and session id as specified */
kctx->auk_info.ai_auid = STRUCT_FGET(info, ai_auid);
kctx->auk_info.ai_namask = STRUCT_FGET(info, ai_mask);
#ifdef _LP64
/* only convert to 64 bit if coming from a 32 bit binary */
if (model == DATAMODEL_ILP32)
kctx->auk_info.ai_termid.at_port =
DEVEXPL(STRUCT_FGET(info, ai_termid.at_port));
else
kctx->auk_info.ai_termid.at_port =
STRUCT_FGET(info, ai_termid.at_port);
#else
kctx->auk_info.ai_termid.at_port = STRUCT_FGET(info, ai_termid.at_port);
#endif
kctx->auk_info.ai_termid.at_type = STRUCT_FGET(info, ai_termid.at_type);
bzero(&kctx->auk_info.ai_termid.at_addr[0],
sizeof (kctx->auk_info.ai_termid.at_addr));
kctx->auk_info.ai_termid.at_addr[0] =
STRUCT_FGET(info, ai_termid.at_addr[0]);
kctx->auk_info.ai_termid.at_addr[1] =
STRUCT_FGET(info, ai_termid.at_addr[1]);
kctx->auk_info.ai_termid.at_addr[2] =
STRUCT_FGET(info, ai_termid.at_addr[2]);
kctx->auk_info.ai_termid.at_addr[3] =
STRUCT_FGET(info, ai_termid.at_addr[3]);
kctx->auk_info.ai_asid = STRUCT_FGET(info, ai_asid);
if (kctx->auk_info.ai_termid.at_type == AU_IPv6 &&
IN6_IS_ADDR_V4MAPPED(
((in6_addr_t *)kctx->auk_info.ai_termid.at_addr))) {
kctx->auk_info.ai_termid.at_type = AU_IPv4;
kctx->auk_info.ai_termid.at_addr[0] =
kctx->auk_info.ai_termid.at_addr[3];
kctx->auk_info.ai_termid.at_addr[1] = 0;
kctx->auk_info.ai_termid.at_addr[2] = 0;
kctx->auk_info.ai_termid.at_addr[3] = 0;
}
if (kctx->auk_info.ai_termid.at_type == AU_IPv6)
kctx->auk_hostaddr_valid = IN6_IS_ADDR_UNSPECIFIED(
(in6_addr_t *)kctx->auk_info.ai_termid.at_addr) ? 0 : 1;
else
kctx->auk_hostaddr_valid =
(kctx->auk_info.ai_termid.at_addr[0] ==
htonl(INADDR_ANY)) ? 0 : 1;
return (0);
}
static int
getqctrl(caddr_t data)
{
au_kcontext_t *kctx = GET_KCTX_PZ;
STRUCT_DECL(au_qctrl, qctrl);
STRUCT_INIT(qctrl, get_udatamodel());
mutex_enter(&(kctx->auk_queue.lock));
STRUCT_FSET(qctrl, aq_hiwater, kctx->auk_queue.hiwater);
STRUCT_FSET(qctrl, aq_lowater, kctx->auk_queue.lowater);
STRUCT_FSET(qctrl, aq_bufsz, kctx->auk_queue.bufsz);
STRUCT_FSET(qctrl, aq_delay, kctx->auk_queue.delay);
mutex_exit(&(kctx->auk_queue.lock));
if (copyout(STRUCT_BUF(qctrl), data, STRUCT_SIZE(qctrl)))
return (EFAULT);
return (0);
}
static int
setqctrl(caddr_t data)
{
au_kcontext_t *kctx;
struct au_qctrl qctrl_tmp;
STRUCT_DECL(au_qctrl, qctrl);
STRUCT_INIT(qctrl, get_udatamodel());
if (!(audit_policy & AUDIT_PERZONE) && !INGLOBALZONE(curproc))
return (EINVAL);
kctx = GET_KCTX_NGZ;
if (copyin(data, STRUCT_BUF(qctrl), STRUCT_SIZE(qctrl)))
return (EFAULT);
qctrl_tmp.aq_hiwater = (size_t)STRUCT_FGET(qctrl, aq_hiwater);
qctrl_tmp.aq_lowater = (size_t)STRUCT_FGET(qctrl, aq_lowater);
qctrl_tmp.aq_bufsz = (size_t)STRUCT_FGET(qctrl, aq_bufsz);
qctrl_tmp.aq_delay = (clock_t)STRUCT_FGET(qctrl, aq_delay);
/* enforce sane values */
if (qctrl_tmp.aq_hiwater <= qctrl_tmp.aq_lowater)
return (EINVAL);
if (qctrl_tmp.aq_hiwater < AQ_LOWATER)
return (EINVAL);
if (qctrl_tmp.aq_hiwater > AQ_MAXHIGH)
return (EINVAL);
if (qctrl_tmp.aq_bufsz < AQ_BUFSZ)
return (EINVAL);
if (qctrl_tmp.aq_bufsz > AQ_MAXBUFSZ)
return (EINVAL);
if (qctrl_tmp.aq_delay == 0)
return (EINVAL);
if (qctrl_tmp.aq_delay > AQ_MAXDELAY)
return (EINVAL);
/* update everything at once so things are consistant */
mutex_enter(&(kctx->auk_queue.lock));
kctx->auk_queue.hiwater = qctrl_tmp.aq_hiwater;
kctx->auk_queue.lowater = qctrl_tmp.aq_lowater;
kctx->auk_queue.bufsz = qctrl_tmp.aq_bufsz;
kctx->auk_queue.delay = qctrl_tmp.aq_delay;
if (kctx->auk_queue.rd_block &&
kctx->auk_queue.cnt > kctx->auk_queue.lowater)
cv_broadcast(&(kctx->auk_queue.read_cv));
if (kctx->auk_queue.wt_block &&
kctx->auk_queue.cnt < kctx->auk_queue.hiwater)
cv_broadcast(&(kctx->auk_queue.write_cv));
mutex_exit(&(kctx->auk_queue.lock));
return (0);
}
static int
getcwd(caddr_t data, int length)
{
struct p_audit_data *pad;
struct audit_path *app;
int pathlen;
pad = P2A(curproc);
ASSERT(pad != NULL);
mutex_enter(&(pad->pad_lock));
app = pad->pad_cwd;
au_pathhold(app);
mutex_exit(&(pad->pad_lock));
pathlen = app->audp_sect[1] - app->audp_sect[0];
if (pathlen > length) {
au_pathrele(app);
return (E2BIG);
}
if (copyout(app->audp_sect[0], data, pathlen)) {
au_pathrele(app);
return (EFAULT);
}
au_pathrele(app);
return (0);
}
static int
getcar(caddr_t data, int length)
{
struct p_audit_data *pad;
struct audit_path *app;
int pathlen;
pad = P2A(curproc);
ASSERT(pad != NULL);
mutex_enter(&(pad->pad_lock));
app = pad->pad_root;
au_pathhold(app);
mutex_exit(&(pad->pad_lock));
pathlen = app->audp_sect[1] - app->audp_sect[0];
if (pathlen > length) {
au_pathrele(app);
return (E2BIG);
}
if (copyout(app->audp_sect[0], data, pathlen)) {
au_pathrele(app);
return (EFAULT);
}
au_pathrele(app);
return (0);
}
static int
getstat(caddr_t data)
{
au_kcontext_t *kctx = GET_KCTX_PZ;
membar_consumer();
if (copyout((caddr_t)&(kctx->auk_statistics), data, sizeof (au_stat_t)))
return (EFAULT);
return (0);
}
static int
setstat(caddr_t data)
{
au_kcontext_t *kctx = GET_KCTX_PZ;
au_stat_t au_stat;
if (!(audit_policy & AUDIT_PERZONE) && !INGLOBALZONE(curproc))
return (EINVAL);
if (copyin(data, &au_stat, sizeof (au_stat_t)))
return (EFAULT);
if (au_stat.as_generated == CLEAR_VAL)
kctx->auk_statistics.as_generated = 0;
if (au_stat.as_nonattrib == CLEAR_VAL)
kctx->auk_statistics.as_nonattrib = 0;
if (au_stat.as_kernel == CLEAR_VAL)
kctx->auk_statistics.as_kernel = 0;
if (au_stat.as_audit == CLEAR_VAL)
kctx->auk_statistics.as_audit = 0;
if (au_stat.as_auditctl == CLEAR_VAL)
kctx->auk_statistics.as_auditctl = 0;
if (au_stat.as_enqueue == CLEAR_VAL)
kctx->auk_statistics.as_enqueue = 0;
if (au_stat.as_written == CLEAR_VAL)
kctx->auk_statistics.as_written = 0;
if (au_stat.as_wblocked == CLEAR_VAL)
kctx->auk_statistics.as_wblocked = 0;
if (au_stat.as_rblocked == CLEAR_VAL)
kctx->auk_statistics.as_rblocked = 0;
if (au_stat.as_dropped == CLEAR_VAL)
kctx->auk_statistics.as_dropped = 0;
if (au_stat.as_totalsize == CLEAR_VAL)
kctx->auk_statistics.as_totalsize = 0;
membar_producer();
return (0);
}
static int
setumask(caddr_t data)
{
STRUCT_DECL(auditinfo, user_info);
struct proc *p;
const auditinfo_addr_t *ainfo;
model_t model;
/* setumask not applicable in non-global zones without perzone policy */
if (!(audit_policy & AUDIT_PERZONE) && (!INGLOBALZONE(curproc)))
return (EINVAL);
model = get_udatamodel();
STRUCT_INIT(user_info, model);
if (copyin(data, STRUCT_BUF(user_info), STRUCT_SIZE(user_info)))
return (EFAULT);
mutex_enter(&pidlock); /* lock the process queue against updates */
for (p = practive; p != NULL; p = p->p_next) {
cred_t *cr;
/* if in non-global zone only modify processes in same zone */
if (!HASZONEACCESS(curproc, p->p_zone->zone_id))
continue;
mutex_enter(&p->p_lock); /* so process doesn't go away */
/* skip system processes and ones being created or going away */
if (p->p_stat == SIDL || p->p_stat == SZOMB ||
(p->p_flag & (SSYS | SEXITING | SEXITLWPS))) {
mutex_exit(&p->p_lock);
continue;
}
mutex_enter(&p->p_crlock);
crhold(cr = p->p_cred);
mutex_exit(&p->p_crlock);
ainfo = crgetauinfo(cr);
if (ainfo == NULL) {
mutex_exit(&p->p_lock);
crfree(cr);
continue;
}
if (ainfo->ai_auid == STRUCT_FGET(user_info, ai_auid)) {
au_mask_t mask;
int err;
/*
* Here's a process which matches the specified auid.
* If its mask doesn't already match the new mask,
* save the new mask in the pad, to be picked up
* next syscall.
*/
mask = STRUCT_FGET(user_info, ai_mask);
err = bcmp(&mask, &ainfo->ai_mask, sizeof (au_mask_t));
crfree(cr);
if (err != 0) {
struct p_audit_data *pad = P2A(p);
ASSERT(pad != NULL);
mutex_enter(&(pad->pad_lock));
pad->pad_flags |= PAD_SETMASK;
pad->pad_newmask = mask;
mutex_exit(&(pad->pad_lock));
/*
* No need to call set_proc_pre_sys(), since
* t_pre_sys is ALWAYS on when audit is
* enabled...due to syscall auditing.
*/
}
} else {
crfree(cr);
}
mutex_exit(&p->p_lock);
}
mutex_exit(&pidlock);
return (0);
}
static int
setsmask(caddr_t data)
{
STRUCT_DECL(auditinfo, user_info);
struct proc *p;
const auditinfo_addr_t *ainfo;
model_t model;
/* setsmask not applicable in non-global zones without perzone policy */
if (!(audit_policy & AUDIT_PERZONE) && (!INGLOBALZONE(curproc)))
return (EINVAL);
model = get_udatamodel();
STRUCT_INIT(user_info, model);
if (copyin(data, STRUCT_BUF(user_info), STRUCT_SIZE(user_info)))
return (EFAULT);
mutex_enter(&pidlock); /* lock the process queue against updates */
for (p = practive; p != NULL; p = p->p_next) {
cred_t *cr;
/* if in non-global zone only modify processes in same zone */
if (!HASZONEACCESS(curproc, p->p_zone->zone_id))
continue;
mutex_enter(&p->p_lock); /* so process doesn't go away */
/* skip system processes and ones being created or going away */
if (p->p_stat == SIDL || p->p_stat == SZOMB ||
(p->p_flag & (SSYS | SEXITING | SEXITLWPS))) {
mutex_exit(&p->p_lock);
continue;
}
mutex_enter(&p->p_crlock);
crhold(cr = p->p_cred);
mutex_exit(&p->p_crlock);
ainfo = crgetauinfo(cr);
if (ainfo == NULL) {
mutex_exit(&p->p_lock);
crfree(cr);
continue;
}
if (ainfo->ai_asid == STRUCT_FGET(user_info, ai_asid)) {
au_mask_t mask;
int err;
/*
* Here's a process which matches the specified asid.
* If its mask doesn't already match the new mask,
* save the new mask in the pad, to be picked up
* next syscall.
*/
mask = STRUCT_FGET(user_info, ai_mask);
err = bcmp(&mask, &ainfo->ai_mask, sizeof (au_mask_t));
crfree(cr);
if (err != 0) {
struct p_audit_data *pad = P2A(p);
ASSERT(pad != NULL);
mutex_enter(&(pad->pad_lock));
pad->pad_flags |= PAD_SETMASK;
pad->pad_newmask = mask;
mutex_exit(&(pad->pad_lock));
/*
* No need to call set_proc_pre_sys(), since
* t_pre_sys is ALWAYS on when audit is
* enabled...due to syscall auditing.
*/
}
} else {
crfree(cr);
}
mutex_exit(&p->p_lock);
}
mutex_exit(&pidlock);
return (0);
}
/*
* Get the current audit state of the system
*/
static int
getcond(caddr_t data)
{
au_kcontext_t *kctx = GET_KCTX_PZ;
if (copyout(&(kctx->auk_auditstate), data, sizeof (int)))
return (EFAULT);
return (0);
}
/*
* Set the current audit state of the system to on (AUC_AUDITING) or
* off (AUC_NOAUDIT).
*/
/* ARGSUSED */
static int
setcond(caddr_t data)
{
int auditstate;
au_kcontext_t *kctx;
if (!(audit_policy & AUDIT_PERZONE) && (!INGLOBALZONE(curproc)))
return (EINVAL);
kctx = GET_KCTX_NGZ;
if (copyin(data, &auditstate, sizeof (int)))
return (EFAULT);
switch (auditstate) {
case AUC_AUDITING: /* Turn auditing on */
if (audit_active == C2AUDIT_UNLOADED)
audit_init_module();
kctx->auk_auditstate = AUC_AUDITING;
if (!(audit_policy & AUDIT_PERZONE) && INGLOBALZONE(curproc))
set_all_zone_usr_proc_sys(ALL_ZONES);
else
set_all_zone_usr_proc_sys(curproc->p_zone->zone_id);
break;
case AUC_NOAUDIT: /* Turn auditing off */
if (kctx->auk_auditstate == AUC_NOAUDIT)
break;
kctx->auk_auditstate = AUC_NOAUDIT;
/* clear out the audit queue */
mutex_enter(&(kctx->auk_queue.lock));
if (kctx->auk_queue.wt_block)
cv_broadcast(&(kctx->auk_queue.write_cv));
/* unblock au_output_thread */
cv_broadcast(&(kctx->auk_queue.read_cv));
mutex_exit(&(kctx->auk_queue.lock));
break;
default:
return (EINVAL);
}
return (0);
}
static int
getclass(caddr_t data)
{
au_evclass_map_t event;
au_kcontext_t *kctx = GET_KCTX_PZ;
if (copyin(data, &event, sizeof (au_evclass_map_t)))
return (EFAULT);
if (event.ec_number > MAX_KEVENTS)
return (EINVAL);
event.ec_class = kctx->auk_ets[event.ec_number];
if (copyout(&event, data, sizeof (au_evclass_map_t)))
return (EFAULT);
return (0);
}
static int
setclass(caddr_t data)
{
au_evclass_map_t event;
au_kcontext_t *kctx;
if (!(audit_policy & AUDIT_PERZONE) && !INGLOBALZONE(curproc))
return (EINVAL);
kctx = GET_KCTX_NGZ;
if (copyin(data, &event, sizeof (au_evclass_map_t)))
return (EFAULT);
if (event.ec_number > MAX_KEVENTS)
return (EINVAL);
kctx->auk_ets[event.ec_number] = event.ec_class;
return (0);
}
static int
getpinfo(caddr_t data)
{
STRUCT_DECL(auditpinfo, apinfo);
proc_t *proc;
const auditinfo_addr_t *ainfo;
model_t model;
cred_t *cr, *newcred;
model = get_udatamodel();
STRUCT_INIT(apinfo, model);
if (copyin(data, STRUCT_BUF(apinfo), STRUCT_SIZE(apinfo)))
return (EFAULT);
newcred = cralloc();
mutex_enter(&pidlock);
if ((proc = prfind(STRUCT_FGET(apinfo, ap_pid))) == NULL) {
mutex_exit(&pidlock);
crfree(newcred);
return (ESRCH); /* no such process */
}
mutex_enter(&proc->p_lock); /* so process doesn't go away */
mutex_exit(&pidlock);
audit_update_context(proc, newcred); /* make sure it's up-to-date */
mutex_enter(&proc->p_crlock);
crhold(cr = proc->p_cred);
mutex_exit(&proc->p_crlock);
mutex_exit(&proc->p_lock);
ainfo = crgetauinfo(cr);
if (ainfo == NULL) {
crfree(cr);
return (EINVAL);
}
/* designated process has an ipv6 address? */
if (ainfo->ai_termid.at_type == AU_IPv6) {
crfree(cr);
return (EOVERFLOW);
}
STRUCT_FSET(apinfo, ap_auid, ainfo->ai_auid);
STRUCT_FSET(apinfo, ap_asid, ainfo->ai_asid);
#ifdef _LP64
if (model == DATAMODEL_ILP32) {
dev32_t dev;
/* convert internal 64 bit form to 32 bit version */
if (cmpldev(&dev, ainfo->ai_termid.at_port) == 0) {
crfree(cr);
return (EOVERFLOW);
}
STRUCT_FSET(apinfo, ap_termid.port, dev);
} else
STRUCT_FSET(apinfo, ap_termid.port, ainfo->ai_termid.at_port);
#else
STRUCT_FSET(apinfo, ap_termid.port, ainfo->ai_termid.at_port);
#endif
STRUCT_FSET(apinfo, ap_termid.machine, ainfo->ai_termid.at_addr[0]);
STRUCT_FSET(apinfo, ap_mask, ainfo->ai_mask);
crfree(cr);
if (copyout(STRUCT_BUF(apinfo), data, STRUCT_SIZE(apinfo)))
return (EFAULT);
return (0);
}
static int
getpinfo_addr(caddr_t data, int len)
{
STRUCT_DECL(auditpinfo_addr, apinfo);
proc_t *proc;
const auditinfo_addr_t *ainfo;
model_t model;
cred_t *cr, *newcred;
model = get_udatamodel();
STRUCT_INIT(apinfo, model);
if (len < STRUCT_SIZE(apinfo))
return (EOVERFLOW);
if (copyin(data, STRUCT_BUF(apinfo), STRUCT_SIZE(apinfo)))
return (EFAULT);
newcred = cralloc();
mutex_enter(&pidlock);
if ((proc = prfind(STRUCT_FGET(apinfo, ap_pid))) == NULL) {
mutex_exit(&pidlock);
crfree(newcred);
return (ESRCH);
}
mutex_enter(&proc->p_lock); /* so process doesn't go away */
mutex_exit(&pidlock);
audit_update_context(proc, newcred); /* make sure it's up-to-date */
mutex_enter(&proc->p_crlock);
crhold(cr = proc->p_cred);
mutex_exit(&proc->p_crlock);
mutex_exit(&proc->p_lock);
ainfo = crgetauinfo(cr);
if (ainfo == NULL) {
crfree(cr);
return (EINVAL);
}
STRUCT_FSET(apinfo, ap_auid, ainfo->ai_auid);
STRUCT_FSET(apinfo, ap_asid, ainfo->ai_asid);
#ifdef _LP64
if (model == DATAMODEL_ILP32) {
dev32_t dev;
/* convert internal 64 bit form to 32 bit version */
if (cmpldev(&dev, ainfo->ai_termid.at_port) == 0) {
crfree(cr);
return (EOVERFLOW);
}
STRUCT_FSET(apinfo, ap_termid.at_port, dev);
} else
STRUCT_FSET(apinfo, ap_termid.at_port,
ainfo->ai_termid.at_port);
#else
STRUCT_FSET(apinfo, ap_termid.at_port, ainfo->ai_termid.at_port);
#endif
STRUCT_FSET(apinfo, ap_termid.at_type, ainfo->ai_termid.at_type);
STRUCT_FSET(apinfo, ap_termid.at_addr[0], ainfo->ai_termid.at_addr[0]);
STRUCT_FSET(apinfo, ap_termid.at_addr[1], ainfo->ai_termid.at_addr[1]);
STRUCT_FSET(apinfo, ap_termid.at_addr[2], ainfo->ai_termid.at_addr[2]);
STRUCT_FSET(apinfo, ap_termid.at_addr[3], ainfo->ai_termid.at_addr[3]);
STRUCT_FSET(apinfo, ap_mask, ainfo->ai_mask);
crfree(cr);
if (copyout(STRUCT_BUF(apinfo), data, STRUCT_SIZE(apinfo)))
return (EFAULT);
return (0);
}
static int
setpmask(caddr_t data)
{
STRUCT_DECL(auditpinfo, apinfo);
proc_t *proc;
cred_t *newcred;
auditinfo_addr_t *ainfo;
struct p_audit_data *pad;
model_t model;
model = get_udatamodel();
STRUCT_INIT(apinfo, model);
if (copyin(data, STRUCT_BUF(apinfo), STRUCT_SIZE(apinfo)))
return (EFAULT);
mutex_enter(&pidlock);
if ((proc = prfind(STRUCT_FGET(apinfo, ap_pid))) == NULL) {
mutex_exit(&pidlock);
return (ESRCH);
}
mutex_enter(&proc->p_lock); /* so process doesn't go away */
mutex_exit(&pidlock);
newcred = cralloc();
if ((ainfo = crgetauinfo_modifiable(newcred)) == NULL) {
mutex_exit(&proc->p_lock);
crfree(newcred);
return (EINVAL);
}
mutex_enter(&proc->p_crlock);
crcopy_to(proc->p_cred, newcred);
proc->p_cred = newcred;
ainfo->ai_mask = STRUCT_FGET(apinfo, ap_mask);
/*
* Unlock. No need to broadcast changes via set_proc_pre_sys(),
* since t_pre_sys is ALWAYS on when audit is enabled... due to
* syscall auditing.
*/
crfree(newcred);
mutex_exit(&proc->p_crlock);
/* Reset flag for any previous pending mask change; this supercedes */
pad = P2A(proc);
ASSERT(pad != NULL);
mutex_enter(&(pad->pad_lock));
pad->pad_flags &= ~PAD_SETMASK;
mutex_exit(&(pad->pad_lock));
mutex_exit(&proc->p_lock);
return (0);
}
/*
* The out of control system call
* This is audit kitchen sink aka auditadm, aka auditon
*/
int
auditctl(
int cmd,
caddr_t data,
int length)
{
int result;
switch (cmd) {
case A_GETAMASK:
case A_GETCOND:
case A_GETCAR:
case A_GETCLASS:
case A_GETCWD:
case A_GETKAUDIT:
case A_GETKMASK:
case A_GETPINFO:
case A_GETPINFO_ADDR:
case A_GETPOLICY:
case A_GETQCTRL:
case A_GETSTAT:
if (secpolicy_audit_getattr(CRED(), B_FALSE) != 0)
return (EPERM);
break;
default:
if (secpolicy_audit_config(CRED()) != 0)
return (EPERM);
break;
}
switch (cmd) {
case A_GETPOLICY:
result = getpolicy(data);
break;
case A_SETPOLICY:
result = setpolicy(data);
break;
case A_GETAMASK:
result = getamask(data);
break;
case A_SETAMASK:
result = setamask(data);
break;
case A_GETKMASK:
result = getkmask(data);
break;
case A_SETKMASK:
result = setkmask(data);
break;
case A_GETKAUDIT:
result = getkaudit(data, length);
break;
case A_SETKAUDIT:
result = setkaudit(data, length);
break;
case A_GETQCTRL:
result = getqctrl(data);
break;
case A_SETQCTRL:
result = setqctrl(data);
break;
case A_GETCWD:
result = getcwd(data, length);
break;
case A_GETCAR:
result = getcar(data, length);
break;
case A_GETSTAT:
result = getstat(data);
break;
case A_SETSTAT:
result = setstat(data);
break;
case A_SETUMASK:
result = setumask(data);
break;
case A_SETSMASK:
result = setsmask(data);
break;
case A_GETCOND:
result = getcond(data);
break;
case A_SETCOND:
result = setcond(data);
break;
case A_GETCLASS:
result = getclass(data);
break;
case A_SETCLASS:
result = setclass(data);
break;
case A_GETPINFO:
result = getpinfo(data);
break;
case A_GETPINFO_ADDR:
result = getpinfo_addr(data, length);
break;
case A_SETPMASK:
result = setpmask(data);
break;
default:
result = EINVAL;
break;
}
return (result);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/brand.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/zone.h>
/*
* brand(2) system call.
*/
int64_t
brandsys(int cmd, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3,
uintptr_t arg4, uintptr_t arg5, uintptr_t arg6)
{
struct proc *p = curthread->t_procp;
int64_t rval = 0;
int err;
/*
* The brandsys system call can only be executed from inside a
* branded zone.
*/
if (INGLOBALZONE(p) || !ZONE_IS_BRANDED(p->p_zone))
return (set_errno(ENOSYS));
if ((err = ZBROP(p->p_zone)->b_brandsys(cmd, &rval, arg1, arg2, arg3,
arg4, arg5, arg6)) != 0)
return (set_errno(err));
return (rval);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/pathname.h>
#include <sys/var.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/mode.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/poll.h>
#include <sys/kmem.h>
#include <sys/filio.h>
#include <sys/cmn_err.h>
#include <sys/policy.h>
#include <sys/zone.h>
#include <sys/debug.h>
#include <c2/audit.h>
#include <fs/fs_subr.h>
/*
* Change current working directory (".").
*/
static int chdirec(vnode_t *, int ischroot, int do_traverse);
int
chdir(char *fname)
{
vnode_t *vp;
int error;
int estale_retry = 0;
lookup:
if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
error = chdirec(vp, 0, 1);
if (error) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (0);
}
/*
* File-descriptor based version of 'chdir'.
*/
int
fchdir(int fd)
{
vnode_t *vp;
file_t *fp;
int error;
if ((fp = getf(fd)) == NULL)
return (set_errno(EBADF));
vp = fp->f_vnode;
VN_HOLD(vp);
releasef(fd);
error = chdirec(vp, 0, 0);
if (error)
return (set_errno(error));
return (0);
}
/*
* Change notion of root ("/") directory.
*/
int
chroot(char *fname)
{
vnode_t *vp;
int error;
int estale_retry = 0;
lookup:
if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
error = chdirec(vp, 1, 1);
if (error) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (0);
}
/*
* ++++++++++++++++++++++++
* ++ SunOS4.1 Buyback ++
* ++++++++++++++++++++++++
* Change root directory with a user given fd
*/
int
fchroot(int fd)
{
vnode_t *vp;
file_t *fp;
int error;
if ((fp = getf(fd)) == NULL)
return (set_errno(EBADF));
vp = fp->f_vnode;
VN_HOLD(vp);
releasef(fd);
error = chdirec(vp, 1, 0);
if (error)
return (set_errno(error));
return (0);
}
static int
chdirec(vnode_t *vp, int ischroot, int do_traverse)
{
int error;
vnode_t *oldvp;
proc_t *pp = curproc;
vnode_t **vpp;
refstr_t *cwd;
int newcwd = 1;
if (vp->v_type != VDIR) {
error = ENOTDIR;
goto bad;
}
if (error = VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL))
goto bad;
/*
* The VOP_ACCESS() may have covered 'vp' with a new filesystem,
* if 'vp' is an autoFS vnode. Traverse the mountpoint so
* that we don't end up with a covered current directory.
*/
if (vn_mountedvfs(vp) != NULL && do_traverse) {
if (error = traverse(&vp))
goto bad;
}
/*
* Special chroot semantics: chroot is allowed if privileged
* or if the target is really a loopback mount of the root (or
* root of the zone) as determined by comparing dev and inode
* numbers
*/
if (ischroot) {
struct vattr tattr;
struct vattr rattr;
vnode_t *zonevp = curproc->p_zone->zone_rootvp;
tattr.va_mask = AT_FSID|AT_NODEID;
if (error = VOP_GETATTR(vp, &tattr, 0, CRED(), NULL))
goto bad;
rattr.va_mask = AT_FSID|AT_NODEID;
if (error = VOP_GETATTR(zonevp, &rattr, 0, CRED(), NULL))
goto bad;
if ((tattr.va_fsid != rattr.va_fsid ||
tattr.va_nodeid != rattr.va_nodeid) &&
(error = secpolicy_chroot(CRED())) != 0)
goto bad;
vpp = &PTOU(pp)->u_rdir;
} else {
vpp = &PTOU(pp)->u_cdir;
}
/* update abs cwd/root path see c2/audit.c */
if (AU_AUDITING())
audit_chdirec(vp, vpp);
mutex_enter(&pp->p_lock);
/*
* This bit of logic prevents us from overwriting u_cwd if we are
* changing to the same directory. We set the cwd to NULL so that we
* don't try to do the lookup on the next call to getcwd().
*/
if (!ischroot && *vpp != NULL && vp != NULL && VN_CMP(*vpp, vp))
newcwd = 0;
oldvp = *vpp;
*vpp = vp;
if ((cwd = PTOU(pp)->u_cwd) != NULL && newcwd)
PTOU(pp)->u_cwd = NULL;
mutex_exit(&pp->p_lock);
if (cwd && newcwd)
refstr_rele(cwd);
if (oldvp)
VN_RELE(oldvp);
return (0);
bad:
VN_RELE(vp);
return (error);
}
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/dirent.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/pathname.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/mode.h>
#include <sys/uio.h>
#include <sys/filio.h>
#include <sys/debug.h>
/*
* Change mode of file.
*/
int
fchmodat(int fd, char *path, int mode, int flag)
{
struct vattr vattr;
int error;
if (flag & ~AT_SYMLINK_NOFOLLOW)
return (set_errno(EINVAL));
vattr.va_mode = mode & MODEMASK;
vattr.va_mask = AT_MODE;
error = fsetattrat(fd, path, flag, &vattr);
if (error)
return (set_errno(error));
return (0);
}
/*
* Change mode of file given path name.
*/
int
chmod(char *path, int mode)
{
return (fchmodat(AT_FDCWD, path, mode, 0));
}
/*
* Change mode of file given file descriptor.
*/
int
fchmod(int fd, int mode)
{
return (fchmodat(fd, NULL, mode, 0));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/pathname.h>
#include <sys/var.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/mode.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/filio.h>
#include <sys/fcntl.h>
#include <sys/debug.h>
#include <c2/audit.h>
/*
* Change ownership of file.
*/
int
fchownat(int fd, char *path, uid_t uid, gid_t gid, int flag)
{
struct vattr vattr;
int error;
struct zone *zone = crgetzone(CRED());
if (uid != (uid_t)-1 && !VALID_UID(uid, zone) ||
gid != (gid_t)-1 && !VALID_GID(gid, zone)) {
return (set_errno(EINVAL));
}
vattr.va_uid = uid;
vattr.va_gid = gid;
vattr.va_mask = 0;
if (vattr.va_uid != -1)
vattr.va_mask |= AT_UID;
if (vattr.va_gid != -1)
vattr.va_mask |= AT_GID;
error = fsetattrat(fd, path, flag, &vattr);
if (error)
return (set_errno(error));
return (0);
}
int
chown(char *path, uid_t uid, gid_t gid)
{
return (fchownat(AT_FDCWD, path, uid, gid, 0));
}
int
lchown(char *path, uid_t uid, gid_t gid)
{
return (fchownat(AT_FDCWD, path, uid, gid, AT_SYMLINK_NOFOLLOW));
}
int
fchown(int fd, uid_t uid, uid_t gid)
{
return (fchownat(fd, NULL, uid, gid, 0));
}
/*
* 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 (c) 1998 by Sun Microsystems, Inc.
* All rights reserved.
*/
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/cladm.h>
/*
* cladm(2) cluster administation system call.
*/
int
cladm(int fac, int cmd, void *arg)
{
int error = 0;
int copyout_bootflags;
switch (fac) {
case CL_INITIALIZE:
if (cmd != CL_GET_BOOTFLAG) {
error = EINVAL;
break;
}
/*
* The CLUSTER_INSTALLING and CLUSTER_DCS_ENABLED bootflags are
* internal flags. We do not want to expose these to the user
* level.
*/
copyout_bootflags = (cluster_bootflags &
~(CLUSTER_INSTALLING | CLUSTER_DCS_ENABLED));
if (copyout(©out_bootflags, arg, sizeof (int))) {
error = EFAULT;
}
break;
case CL_CONFIG:
/*
* We handle CL_NODEID here so that the node number
* can be returned if the system is configured as part
* of a cluster but not booted as part of the cluster.
*/
if (cmd == CL_NODEID) {
nodeid_t nid;
/* return error if not configured as a cluster */
if (!(cluster_bootflags & CLUSTER_CONFIGURED)) {
error = ENOSYS;
break;
}
nid = clconf_get_nodeid();
error = copyout(&nid, arg, sizeof (nid));
break;
}
/* FALLTHROUGH */
default:
if ((cluster_bootflags & (CLUSTER_CONFIGURED|CLUSTER_BOOTED)) !=
(CLUSTER_CONFIGURED|CLUSTER_BOOTED)) {
error = EINVAL;
break;
}
error = cladmin(fac, cmd, arg);
/*
* error will be -1 if the cladm module cannot be loaded;
* otherwise, it is the errno value returned
* (see {i86,sparc}/ml/modstubs.S).
*/
if (error < 0)
error = ENOSYS;
break;
}
return (error ? set_errno(error) : 0);
}
/*
* 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 1998 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#ident "%Z%%M% %I% %E% SMI"
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/debug.h>
/*
* Close a file.
*/
int
close(int fdes)
{
int error;
if ((error = closeandsetf(fdes, NULL)) != 0)
return (set_errno(error));
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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/atomic.h>
#include <sys/kmem.h>
#include <sys/sysmacros.h>
#include <sys/procset.h>
#include <sys/corectl.h>
#include <sys/zone.h>
#include <sys/cmn_err.h>
#include <sys/policy.h>
/*
* Core File Settings
* ------------------
*
* A process's core file path and content live in separate reference-counted
* structures. The corectl_content_t structure is fairly straightforward --
* the only subtlety is that we only really _need_ the mutex on architectures
* on which 64-bit memory operations are not atomic. The corectl_path_t
* structure is slightly trickier in that it contains a refstr_t rather than
* just a char * string. This is to allow consumers of the data in that
* structure (the core dumping sub-system for example) to safely use the
* string without holding any locks on it in light of updates.
*
* At system and zone boot, init_core() sets init(8)'s core file path and
* content to the same value as the fields core_default_path and
* core_default_content respectively (for the global zone). All subsequent
* children of init(8) reference those same settings. During boot coreadm(8)
* is invoked with the -u option to update the system settings from
* /etc/coreadm.conf. This has the effect of also changing the values in
* core_default_path and core_default_content which updates the core file
* settings for all processes in the zone. Each zone has different default
* settings; when processes enter a non-global zone, their core file path and
* content are set to the zone's default path and content.
*
* Processes that have their core file settings explicitly overridden using
* coreadm(8) no longer reference core_default_path or core_default_content
* so subsequent changes to the default will not affect them.
*/
zone_key_t core_zone_key;
static int set_proc_info(pid_t pid, const char *path, core_content_t content);
static corectl_content_t *
corectl_content_alloc(core_content_t cc)
{
corectl_content_t *ccp;
ccp = kmem_zalloc(sizeof (corectl_content_t), KM_SLEEP);
ccp->ccc_content = cc;
ccp->ccc_refcnt = 1;
return (ccp);
}
core_content_t
corectl_content_value(corectl_content_t *ccp)
{
core_content_t content;
mutex_enter(&ccp->ccc_mtx);
content = ccp->ccc_content;
mutex_exit(&ccp->ccc_mtx);
return (content);
}
static void
corectl_content_set(corectl_content_t *ccp, core_content_t content)
{
mutex_enter(&ccp->ccc_mtx);
ccp->ccc_content = content;
mutex_exit(&ccp->ccc_mtx);
}
void
corectl_content_hold(corectl_content_t *ccp)
{
atomic_inc_32(&ccp->ccc_refcnt);
}
void
corectl_content_rele(corectl_content_t *ccp)
{
if (atomic_dec_32_nv(&ccp->ccc_refcnt) == 0)
kmem_free(ccp, sizeof (corectl_content_t));
}
static corectl_path_t *
corectl_path_alloc(const char *path)
{
corectl_path_t *ccp;
ccp = kmem_zalloc(sizeof (corectl_path_t), KM_SLEEP);
ccp->ccp_path = refstr_alloc(path);
ccp->ccp_refcnt = 1;
return (ccp);
}
refstr_t *
corectl_path_value(corectl_path_t *ccp)
{
refstr_t *path;
mutex_enter(&ccp->ccp_mtx);
refstr_hold(path = ccp->ccp_path);
mutex_exit(&ccp->ccp_mtx);
return (path);
}
static void
corectl_path_set(corectl_path_t *ccp, const char *path)
{
refstr_t *npath = refstr_alloc(path);
mutex_enter(&ccp->ccp_mtx);
refstr_rele(ccp->ccp_path);
ccp->ccp_path = npath;
mutex_exit(&ccp->ccp_mtx);
}
void
corectl_path_hold(corectl_path_t *ccp)
{
atomic_inc_32(&ccp->ccp_refcnt);
}
void
corectl_path_rele(corectl_path_t *ccp)
{
if (atomic_dec_32_nv(&ccp->ccp_refcnt) == 0) {
refstr_rele(ccp->ccp_path);
kmem_free(ccp, sizeof (corectl_path_t));
}
}
/*
* Constructor routine to be called when a zone is created.
*/
/*ARGSUSED*/
static void *
core_init_zone(zoneid_t zoneid)
{
struct core_globals *cg;
cg = kmem_alloc(sizeof (*cg), KM_SLEEP);
mutex_init(&cg->core_lock, NULL, MUTEX_DEFAULT, NULL);
cg->core_file = NULL;
cg->core_options = CC_PROCESS_PATH;
cg->core_content = CC_CONTENT_DEFAULT;
cg->core_rlimit = RLIM64_INFINITY;
cg->core_default_path = corectl_path_alloc("core");
cg->core_default_content = corectl_content_alloc(CC_CONTENT_DEFAULT);
return (cg);
}
/*
* Destructor routine to be called when a zone is destroyed.
*/
/*ARGSUSED*/
static void
core_free_zone(zoneid_t zoneid, void *arg)
{
struct core_globals *cg = arg;
if (cg == NULL)
return;
if (cg->core_file != NULL)
refstr_rele(cg->core_file);
corectl_path_rele(cg->core_default_path);
corectl_content_rele(cg->core_default_content);
kmem_free(cg, sizeof (*cg));
}
/*
* Called from start_init_common(), to set init's core file path and content.
*/
void
init_core(void)
{
struct core_globals *cg;
/*
* The first time we hit this, in the global zone, we have to
* initialize the zsd key.
*/
if (INGLOBALZONE(curproc)) {
zone_key_create(&core_zone_key, core_init_zone, NULL,
core_free_zone);
}
/*
* zone_key_create will have called core_init_zone for the
* global zone, which sets up the default path and content
* variables.
*/
VERIFY((cg = zone_getspecific(core_zone_key, curproc->p_zone)) != NULL);
corectl_path_hold(cg->core_default_path);
corectl_content_hold(cg->core_default_content);
curproc->p_corefile = cg->core_default_path;
curproc->p_content = cg->core_default_content;
}
int
corectl(int subcode, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
{
int error = 0;
proc_t *p;
refstr_t *rp;
size_t size;
char *path;
core_content_t content = CC_CONTENT_INVALID;
struct core_globals *cg;
zone_t *zone = curproc->p_zone;
cg = zone_getspecific(core_zone_key, zone);
ASSERT(cg != NULL);
switch (subcode) {
case CC_SET_OPTIONS:
if ((error = secpolicy_coreadm(CRED())) == 0) {
if (arg1 & ~CC_OPTIONS)
error = EINVAL;
else
cg->core_options = (uint32_t)arg1;
}
break;
case CC_GET_OPTIONS:
return (cg->core_options);
case CC_GET_GLOBAL_PATH:
case CC_GET_DEFAULT_PATH:
case CC_GET_PROCESS_PATH:
if (subcode == CC_GET_GLOBAL_PATH) {
mutex_enter(&cg->core_lock);
if ((rp = cg->core_file) != NULL)
refstr_hold(rp);
mutex_exit(&cg->core_lock);
} else if (subcode == CC_GET_DEFAULT_PATH) {
rp = corectl_path_value(cg->core_default_path);
} else {
rp = NULL;
mutex_enter(&pidlock);
if ((p = prfind((pid_t)arg3)) == NULL ||
p->p_stat == SIDL) {
mutex_exit(&pidlock);
error = ESRCH;
} else {
mutex_enter(&p->p_lock);
mutex_exit(&pidlock);
mutex_enter(&p->p_crlock);
if (!hasprocperm(p->p_cred, CRED()))
error = EPERM;
else if (p->p_corefile != NULL)
rp = corectl_path_value(p->p_corefile);
mutex_exit(&p->p_crlock);
mutex_exit(&p->p_lock);
}
}
if (rp == NULL) {
if (error == 0 && suword8((void *)arg1, 0))
error = EFAULT;
} else {
error = copyoutstr(refstr_value(rp), (char *)arg1,
(size_t)arg2, NULL);
refstr_rele(rp);
}
break;
case CC_SET_GLOBAL_PATH:
case CC_SET_DEFAULT_PATH:
if ((error = secpolicy_coreadm(CRED())) != 0)
break;
/* FALLTHROUGH */
case CC_SET_PROCESS_PATH:
if ((size = MIN((size_t)arg2, MAXPATHLEN)) == 0) {
error = EINVAL;
break;
}
path = kmem_alloc(size, KM_SLEEP);
error = copyinstr((char *)arg1, path, size, NULL);
if (error == 0) {
if (subcode == CC_SET_PROCESS_PATH) {
error = set_proc_info((pid_t)arg3, path, 0);
} else if (subcode == CC_SET_DEFAULT_PATH) {
corectl_path_set(cg->core_default_path, path);
} else if (*path != '\0' && *path != '/') {
error = EINVAL;
} else {
refstr_t *nrp = refstr_alloc(path);
mutex_enter(&cg->core_lock);
rp = cg->core_file;
if (*path == '\0')
cg->core_file = NULL;
else
refstr_hold(cg->core_file = nrp);
mutex_exit(&cg->core_lock);
if (rp != NULL)
refstr_rele(rp);
refstr_rele(nrp);
}
}
kmem_free(path, size);
break;
case CC_SET_GLOBAL_CONTENT:
case CC_SET_DEFAULT_CONTENT:
if ((error = secpolicy_coreadm(CRED())) != 0)
break;
/* FALLTHROUGH */
case CC_SET_PROCESS_CONTENT:
error = copyin((void *)arg1, &content, sizeof (content));
if (error != 0)
break;
/*
* If any unknown bits are set, don't let this charade
* continue.
*/
if (content & ~CC_CONTENT_ALL) {
error = EINVAL;
break;
}
if (subcode == CC_SET_PROCESS_CONTENT) {
error = set_proc_info((pid_t)arg2, NULL, content);
} else if (subcode == CC_SET_DEFAULT_CONTENT) {
corectl_content_set(cg->core_default_content, content);
} else {
mutex_enter(&cg->core_lock);
cg->core_content = content;
mutex_exit(&cg->core_lock);
}
break;
case CC_GET_GLOBAL_CONTENT:
content = cg->core_content;
error = copyout(&content, (void *)arg1, sizeof (content));
break;
case CC_GET_DEFAULT_CONTENT:
content = corectl_content_value(cg->core_default_content);
error = copyout(&content, (void *)arg1, sizeof (content));
break;
case CC_GET_PROCESS_CONTENT:
mutex_enter(&pidlock);
if ((p = prfind((pid_t)arg2)) == NULL || p->p_stat == SIDL) {
mutex_exit(&pidlock);
error = ESRCH;
break;
}
mutex_enter(&p->p_lock);
mutex_exit(&pidlock);
mutex_enter(&p->p_crlock);
if (!hasprocperm(p->p_cred, CRED()))
error = EPERM;
else if (p->p_content == NULL)
content = CC_CONTENT_NONE;
else
content = corectl_content_value(p->p_content);
mutex_exit(&p->p_crlock);
mutex_exit(&p->p_lock);
if (error == 0)
error = copyout(&content, (void *)arg1,
sizeof (content));
break;
default:
error = EINVAL;
break;
}
if (error)
return (set_errno(error));
return (0);
}
typedef struct {
int cc_count;
corectl_path_t *cc_path;
corectl_content_t *cc_content;
} counter_t;
static int
set_one_proc_info(proc_t *p, counter_t *counterp)
{
corectl_path_t *corefile;
corectl_content_t *content;
mutex_enter(&p->p_crlock);
if (!(p->p_flag & SSYS) && hasprocperm(p->p_cred, CRED())) {
mutex_exit(&p->p_crlock);
counterp->cc_count++;
if (counterp->cc_path != NULL) {
corectl_path_hold(counterp->cc_path);
mutex_enter(&p->p_lock);
corefile = p->p_corefile;
p->p_corefile = counterp->cc_path;
mutex_exit(&p->p_lock);
if (corefile != NULL)
corectl_path_rele(corefile);
} else {
corectl_content_hold(counterp->cc_content);
mutex_enter(&p->p_lock);
content = p->p_content;
p->p_content = counterp->cc_content;
mutex_exit(&p->p_lock);
if (content != NULL)
corectl_content_rele(content);
}
} else {
mutex_exit(&p->p_crlock);
}
return (0);
}
static int
set_proc_info(pid_t pid, const char *path, core_content_t content)
{
proc_t *p;
counter_t counter;
int error = 0;
counter.cc_count = 0;
/*
* Only one of the core file path or content can be set at a time.
*/
if (path != NULL) {
counter.cc_path = corectl_path_alloc(path);
counter.cc_content = NULL;
} else {
counter.cc_path = NULL;
counter.cc_content = corectl_content_alloc(content);
}
if (pid == -1) {
procset_t set;
setprocset(&set, POP_AND, P_ALL, P_MYID, P_ALL, P_MYID);
error = dotoprocs(&set, set_one_proc_info, (char *)&counter);
if (error == 0 && counter.cc_count == 0)
error = EPERM;
} else if (pid > 0) {
mutex_enter(&pidlock);
if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
error = ESRCH;
} else {
(void) set_one_proc_info(p, &counter);
if (counter.cc_count == 0)
error = EPERM;
}
mutex_exit(&pidlock);
} else {
int nfound = 0;
pid_t pgid;
if (pid == 0)
pgid = curproc->p_pgrp;
else
pgid = -pid;
mutex_enter(&pidlock);
for (p = pgfind(pgid); p != NULL; p = p->p_pglink) {
if (p->p_stat != SIDL) {
nfound++;
(void) set_one_proc_info(p, &counter);
}
}
mutex_exit(&pidlock);
if (nfound == 0)
error = ESRCH;
else if (counter.cc_count == 0)
error = EPERM;
}
if (path != NULL)
corectl_path_rele(counter.cc_path);
else
corectl_content_rele(counter.cc_content);
if (error)
return (set_errno(error));
return (0);
}
/*
* Give current process the default core settings for its current zone;
* used for processes entering a zone via zone_enter.
*/
void
set_core_defaults(void)
{
proc_t *p = curproc;
struct core_globals *cg;
corectl_path_t *oldpath, *newpath;
corectl_content_t *oldcontent, *newcontent;
cg = zone_getspecific(core_zone_key, p->p_zone);
/* make local copies of default values to protect against change */
newpath = cg->core_default_path;
newcontent = cg->core_default_content;
corectl_path_hold(newpath);
corectl_content_hold(newcontent);
mutex_enter(&p->p_lock);
oldpath = p->p_corefile;
p->p_corefile = newpath;
oldcontent = p->p_content;
p->p_content = newcontent;
mutex_exit(&p->p_lock);
if (oldpath != NULL)
corectl_path_rele(oldpath);
if (oldcontent != NULL)
corectl_content_rele(oldcontent);
}
/*
* 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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/acctctl.h>
#include <sys/cmn_err.h>
#include <sys/cred.h>
#include <sys/errno.h>
#include <sys/exacct.h>
#include <sys/modctl.h>
#include <sys/procset.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/task.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/policy.h>
/*
* getacct(2), putacct(2), and wracct(2) system calls
*
* The extended accounting subsystem provides three root-privileged system
* calls for interacting with the actual resource data associated with each
* task or process. getacct() copies a packed exacct record reflecting the
* resource usage out to the buffer provided by the user. wracct() writes a
* record to the appropriate extended accounting file. putacct() takes the
* buffer provided by the user, and appends a "tag" record associated with the
* specified task or project that encapsulates the user data. All three of
* these functions exit early if extended accounting is not active for the
* requested entity type.
*
* Locking
* Under the terminology introduced in os/task.c, all three of these system
* calls are task observers, when executing on an existing task.
*/
/*
* getacct_callback() is used to copyout the buffer with accounting records
* from the kernel back to the user. It also sets actual to the size of the
* kernel buffer--the required minimum size for a successful outbound copy.
*/
/* ARGSUSED */
static int
getacct_callback(ac_info_t *unused, void *ubuf, size_t usize, void *kbuf,
size_t ksize, size_t *actual)
{
size_t size = MIN(usize, ksize);
if (ubuf != NULL && copyout(kbuf, ubuf, size) != 0)
return (EFAULT);
*actual = ksize;
return (0);
}
static int
getacct_task(ac_info_t *ac_task, taskid_t tkid, void *buf, size_t bufsize,
size_t *sizep)
{
task_t *tk;
int error;
mutex_enter(&ac_task->ac_lock);
if (ac_task->ac_state == AC_OFF) {
mutex_exit(&ac_task->ac_lock);
return (ENOTACTIVE);
}
mutex_exit(&ac_task->ac_lock);
if ((tk = task_hold_by_id(tkid)) == NULL)
return (ESRCH);
error = exacct_assemble_task_usage(ac_task, tk,
getacct_callback, buf, bufsize, sizep, EW_PARTIAL);
task_rele(tk);
return (error);
}
static int
getacct_proc(ac_info_t *ac_proc, pid_t pid, void *buf, size_t bufsize,
size_t *sizep)
{
proc_t *p;
proc_usage_t *pu;
ulong_t mask[AC_MASK_SZ];
ulong_t *ac_mask = &mask[0];
int error;
mutex_enter(&ac_proc->ac_lock);
if (ac_proc->ac_state == AC_OFF) {
mutex_exit(&ac_proc->ac_lock);
return (ENOTACTIVE);
}
bt_copy(&ac_proc->ac_mask[0], ac_mask, AC_MASK_SZ);
mutex_exit(&ac_proc->ac_lock);
pu = kmem_zalloc(sizeof (proc_usage_t), KM_SLEEP);
pu->pu_command = kmem_zalloc(MAXCOMLEN + 1, KM_SLEEP);
mutex_enter(&pidlock);
if ((p = prfind(pid)) == NULL) {
mutex_exit(&pidlock);
kmem_free(pu->pu_command, MAXCOMLEN + 1);
kmem_free(pu, sizeof (proc_usage_t));
return (ESRCH);
}
mutex_enter(&p->p_lock);
mutex_exit(&pidlock);
exacct_calculate_proc_usage(p, pu, ac_mask, EW_PARTIAL, 0);
mutex_exit(&p->p_lock);
error = exacct_assemble_proc_usage(ac_proc, pu,
getacct_callback, buf, bufsize, sizep, EW_PARTIAL);
kmem_free(pu->pu_command, MAXCOMLEN + 1);
kmem_free(pu, sizeof (proc_usage_t));
return (error);
}
static ssize_t
getacct(idtype_t idtype, id_t id, void *buf, size_t bufsize)
{
size_t size = 0;
int error;
struct exacct_globals *acg;
if (bufsize > EXACCT_MAX_BUFSIZE)
bufsize = EXACCT_MAX_BUFSIZE;
acg = zone_getspecific(exacct_zone_key, curproc->p_zone);
switch (idtype) {
case P_PID:
error = getacct_proc(&acg->ac_proc, id, buf, bufsize, &size);
break;
case P_TASKID:
error = getacct_task(&acg->ac_task, id, buf, bufsize, &size);
break;
default:
error = EINVAL;
break;
}
return (error == 0 ? (ssize_t)size : set_errno(error));
}
static int
putacct(idtype_t idtype, id_t id, void *buf, size_t bufsize, int flags)
{
int error;
taskid_t tkid;
proc_t *p;
task_t *tk;
void *kbuf;
struct exacct_globals *acg;
if (bufsize == 0 || bufsize > EXACCT_MAX_BUFSIZE)
return (set_errno(EINVAL));
kbuf = kmem_alloc(bufsize, KM_SLEEP);
if (copyin(buf, kbuf, bufsize) != 0) {
error = EFAULT;
goto out;
}
acg = zone_getspecific(exacct_zone_key, curproc->p_zone);
switch (idtype) {
case P_PID:
mutex_enter(&pidlock);
if ((p = prfind(id)) == NULL) {
mutex_exit(&pidlock);
error = ESRCH;
} else {
zone_t *zone = p->p_zone;
tkid = p->p_task->tk_tkid;
zone_hold(zone);
mutex_exit(&pidlock);
error = exacct_tag_proc(&acg->ac_proc, id, tkid, kbuf,
bufsize, flags, zone->zone_nodename);
zone_rele(zone);
}
break;
case P_TASKID:
if ((tk = task_hold_by_id(id)) != NULL) {
error = exacct_tag_task(&acg->ac_task, tk, kbuf,
bufsize, flags);
task_rele(tk);
} else {
error = ESRCH;
}
break;
default:
error = EINVAL;
break;
}
out:
kmem_free(kbuf, bufsize);
return (error == 0 ? error : set_errno(error));
}
static int
wracct_task(ac_info_t *ac_task, taskid_t tkid, int flag, size_t *sizep)
{
task_t *tk;
int error;
mutex_enter(&ac_task->ac_lock);
if (ac_task->ac_state == AC_OFF || ac_task->ac_vnode == NULL) {
mutex_exit(&ac_task->ac_lock);
return (ENOTACTIVE);
}
mutex_exit(&ac_task->ac_lock);
if ((tk = task_hold_by_id(tkid)) == NULL)
return (ESRCH);
error = exacct_assemble_task_usage(ac_task, tk, exacct_commit_callback,
NULL, 0, sizep, flag);
task_rele(tk);
return (error);
}
static int
wracct_proc(ac_info_t *ac_proc, pid_t pid, int flag, size_t *sizep)
{
proc_t *p;
proc_usage_t *pu;
ulong_t mask[AC_MASK_SZ];
ulong_t *ac_mask = &mask[0];
int error;
mutex_enter(&ac_proc->ac_lock);
if (ac_proc->ac_state == AC_OFF || ac_proc->ac_vnode == NULL) {
mutex_exit(&ac_proc->ac_lock);
return (ENOTACTIVE);
}
bt_copy(&ac_proc->ac_mask[0], ac_mask, AC_MASK_SZ);
mutex_exit(&ac_proc->ac_lock);
pu = kmem_zalloc(sizeof (proc_usage_t), KM_SLEEP);
pu->pu_command = kmem_zalloc(MAXCOMLEN + 1, KM_SLEEP);
mutex_enter(&pidlock);
if ((p = prfind(pid)) == NULL) {
mutex_exit(&pidlock);
kmem_free(pu->pu_command, MAXCOMLEN + 1);
kmem_free(pu, sizeof (proc_usage_t));
return (ESRCH);
}
mutex_enter(&p->p_lock);
mutex_exit(&pidlock);
exacct_calculate_proc_usage(p, pu, ac_mask, flag, 0);
mutex_exit(&p->p_lock);
error = exacct_assemble_proc_usage(ac_proc, pu,
exacct_commit_callback, NULL, 0, sizep, flag);
kmem_free(pu->pu_command, MAXCOMLEN + 1);
kmem_free(pu, sizeof (proc_usage_t));
return (error);
}
static int
wracct(idtype_t idtype, id_t id, int flags)
{
int error;
size_t size = 0;
struct exacct_globals *acg;
/*
* Validate flags.
*/
switch (flags) {
case EW_PARTIAL:
case EW_INTERVAL:
break;
default:
return (set_errno(EINVAL));
}
acg = zone_getspecific(exacct_zone_key, curproc->p_zone);
switch (idtype) {
case P_PID:
if (flags == EW_INTERVAL)
return (set_errno(ENOTSUP));
error = wracct_proc(&acg->ac_proc, id, flags, &size);
break;
case P_TASKID:
error = wracct_task(&acg->ac_task, id, flags, &size);
break;
default:
error = EINVAL;
break;
}
return (error == 0 ? error : set_errno(error));
}
static long
exacct(int code, idtype_t idtype, id_t id, void *buf, size_t bufsize,
int flags)
{
if (secpolicy_acct(CRED()) != 0)
return (set_errno(EPERM));
if (exacct_zone_key == ZONE_KEY_UNINITIALIZED)
return (set_errno(ENOTACTIVE));
switch (code) {
case 0:
return (getacct(idtype, id, buf, bufsize));
case 1:
return (putacct(idtype, id, buf, bufsize, flags));
case 2:
return (wracct(idtype, id, flags));
default:
return (set_errno(EINVAL));
}
}
#if defined(_LP64)
#define SE_LRVAL SE_64RVAL
#else
#define SE_LRVAL SE_32RVAL1
#endif
static struct sysent exacctsys_sysent = {
6,
SE_NOUNLOAD | SE_ARGC | SE_LRVAL,
(int (*)())(uintptr_t)exacct
};
static struct modlsys modlsys = {
&mod_syscallops,
"extended accounting facility",
&exacctsys_sysent
};
#ifdef _SYSCALL32_IMPL
static struct sysent exacctsys_sysent32 = {
6,
SE_NOUNLOAD | SE_ARGC | SE_32RVAL1,
(int (*)())(uintptr_t)exacct
};
static struct modlsys modlsys32 = {
&mod_syscallops32,
"32-bit extended accounting facility",
&exacctsys_sysent32
};
#endif
static struct modlinkage modlinkage = {
MODREV_1,
&modlsys,
#ifdef _SYSCALL32_IMPL
&modlsys32,
#endif
NULL
};
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_fini(void)
{
return (mod_remove(&modlinkage));
}
int
_info(struct modinfo *mip)
{
return (mod_info(&modlinkage, mip));
}
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
* Copyright 2018, Joyent, Inc.
* Copyright 2024 Oxide Computer Company
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/flock.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/mode.h>
#include <sys/proc.h>
#include <sys/filio.h>
#include <sys/share.h>
#include <sys/debug.h>
#include <sys/rctl.h>
#include <sys/nbmlock.h>
#include <sys/cmn_err.h>
static int flock_check(vnode_t *, flock64_t *, offset_t, offset_t);
static int flock_get_start(vnode_t *, flock64_t *, offset_t, u_offset_t *);
static void fd_too_big(proc_t *);
/*
* File control.
*/
int
fcntl(int fdes, int cmd, intptr_t arg, intptr_t arg1)
{
int iarg;
int error = 0;
int retval;
proc_t *p;
file_t *fp;
vnode_t *vp;
u_offset_t offset;
u_offset_t start;
struct vattr vattr;
int in_crit;
int flag;
struct flock sbf;
struct flock64 bf;
struct o_flock obf;
struct flock64_32 bf64_32;
struct fshare fsh;
struct shrlock shr;
struct shr_locowner shr_own;
offset_t maxoffset;
model_t datamodel;
int fdres;
#if defined(_ILP32) && !defined(lint) && defined(_SYSCALL32)
ASSERT(sizeof (struct flock) == sizeof (struct flock32));
ASSERT(sizeof (struct flock64) == sizeof (struct flock64_32));
#endif
#if defined(_LP64) && !defined(lint) && defined(_SYSCALL32)
ASSERT(sizeof (struct flock) == sizeof (struct flock64_64));
ASSERT(sizeof (struct flock64) == sizeof (struct flock64_64));
#endif
/*
* Most fcntl() calls take either 2 or 3 arguments. The introduction of
* F_DUP3FD added a version that takes a 4th argument (referred to as
* arg1). While fcntl() traditionally has had loose validation, we
* strictly validate this new arg.
*/
switch (cmd) {
case F_DUP3FD:
if ((arg1 & ~(FD_CLOEXEC | FD_CLOFORK)) != 0) {
error = EINVAL;
goto out;
}
break;
default:
if (arg1 != 0) {
error = EINVAL;
goto out;
}
break;
}
/*
* First, for speed, deal with the subset of cases
* that do not require getf() / releasef().
*/
switch (cmd) {
case F_GETFD:
if ((error = f_getfd_error(fdes, &flag)) == 0)
retval = flag;
goto out;
case F_SETFD:
error = f_setfd_error(fdes, (int)arg);
retval = 0;
goto out;
case F_GETFL:
if ((error = f_getfl(fdes, &flag)) == 0) {
retval = (flag & (FMASK | FASYNC));
if ((flag & (FSEARCH | FEXEC)) == 0)
retval += FOPEN;
else
retval |= (flag & (FSEARCH | FEXEC));
}
goto out;
case F_GETXFL:
if ((error = f_getfl(fdes, &flag)) == 0) {
retval = flag;
if ((flag & (FSEARCH | FEXEC)) == 0)
retval += FOPEN;
}
goto out;
case F_BADFD:
if ((error = f_badfd(fdes, &fdres, (int)arg)) == 0)
retval = fdres;
goto out;
}
/*
* Second, for speed, deal with the subset of cases that
* require getf() / releasef() but do not require copyin.
*/
if ((fp = getf(fdes)) == NULL) {
error = EBADF;
goto out;
}
iarg = (int)arg;
switch (cmd) {
case F_DUPFD:
case F_DUPFD_CLOEXEC:
case F_DUPFD_CLOFORK:
p = curproc;
if ((uint_t)iarg >= p->p_fno_ctl) {
if (iarg >= 0)
fd_too_big(p);
error = EINVAL;
goto done;
}
/*
* We need to increment the f_count reference counter
* before allocating a new file descriptor.
* Doing it other way round opens a window for race condition
* with closeandsetf() on the target file descriptor which can
* close the file still referenced by the original
* file descriptor.
*/
mutex_enter(&fp->f_tlock);
fp->f_count++;
mutex_exit(&fp->f_tlock);
if ((retval = ufalloc_file(iarg, fp)) == -1) {
/*
* New file descriptor can't be allocated.
* Revert the reference count.
*/
mutex_enter(&fp->f_tlock);
fp->f_count--;
mutex_exit(&fp->f_tlock);
error = EMFILE;
} else {
if (cmd == F_DUPFD_CLOEXEC) {
f_setfd_or(retval, FD_CLOEXEC);
}
if (cmd == F_DUPFD_CLOFORK) {
f_setfd_or(retval, FD_CLOFORK);
}
}
goto done;
case F_DUP2FD_CLOEXEC:
case F_DUP2FD_CLOFORK:
if (fdes == iarg) {
error = EINVAL;
goto done;
}
/*FALLTHROUGH*/
case F_DUP2FD:
case F_DUP3FD:
p = curproc;
if (fdes == iarg) {
retval = iarg;
} else if ((uint_t)iarg >= p->p_fno_ctl) {
if (iarg >= 0)
fd_too_big(p);
error = EBADF;
} else {
/*
* We can't hold our getf(fdes) across the call to
* closeandsetf() because it creates a window for
* deadlock: if one thread is doing dup2(a, b) while
* another is doing dup2(b, a), each one will block
* waiting for the other to call releasef(). The
* solution is to increment the file reference count
* (which we have to do anyway), then releasef(fdes),
* then closeandsetf(). Incrementing f_count ensures
* that fp won't disappear after we call releasef().
* When closeandsetf() fails, we try avoid calling
* closef() because of all the side effects.
*/
mutex_enter(&fp->f_tlock);
fp->f_count++;
mutex_exit(&fp->f_tlock);
releasef(fdes);
if ((error = closeandsetf(iarg, fp)) == 0) {
if (cmd == F_DUP2FD_CLOEXEC) {
f_setfd_or(iarg, FD_CLOEXEC);
} else if (cmd == F_DUP2FD_CLOFORK) {
f_setfd_or(iarg, FD_CLOFORK);
} else if (cmd == F_DUP3FD) {
f_setfd_or(iarg, (int)arg1);
}
retval = iarg;
} else {
mutex_enter(&fp->f_tlock);
if (fp->f_count > 1) {
fp->f_count--;
mutex_exit(&fp->f_tlock);
} else {
mutex_exit(&fp->f_tlock);
(void) closef(fp);
}
}
goto out;
}
goto done;
case F_SETFL:
vp = fp->f_vnode;
flag = fp->f_flag;
if ((iarg & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
iarg &= ~FNDELAY;
if ((error = VOP_SETFL(vp, flag, iarg, fp->f_cred, NULL)) ==
0) {
iarg &= FMASK;
mutex_enter(&fp->f_tlock);
fp->f_flag &= ~FMASK | (FREAD|FWRITE);
fp->f_flag |= (iarg - FOPEN) & ~(FREAD|FWRITE);
mutex_exit(&fp->f_tlock);
}
retval = 0;
goto done;
}
/*
* Finally, deal with the expensive cases.
*/
retval = 0;
in_crit = 0;
maxoffset = MAXOFF_T;
datamodel = DATAMODEL_NATIVE;
#if defined(_SYSCALL32_IMPL)
if ((datamodel = get_udatamodel()) == DATAMODEL_ILP32)
maxoffset = MAXOFF32_T;
#endif
vp = fp->f_vnode;
flag = fp->f_flag;
offset = fp->f_offset;
switch (cmd) {
/*
* The file system and vnode layers understand and implement
* locking with flock64 structures. So here once we pass through
* the test for compatibility as defined by LFS API, (for F_SETLK,
* F_SETLKW, F_GETLK, F_GETLKW, F_OFD_GETLK, F_OFD_SETLK, F_OFD_SETLKW,
* F_FREESP) we transform the flock structure to a flock64 structure
* and send it to the lower layers. Similarly in case of GETLK and
* OFD_GETLK the returned flock64 structure is transformed to a flock
* structure if everything fits in nicely, otherwise we return
* EOVERFLOW.
*/
case F_GETLK:
case F_O_GETLK:
case F_SETLK:
case F_SETLKW:
case F_SETLK_NBMAND:
case F_OFD_GETLK:
case F_OFD_SETLK:
case F_OFD_SETLKW:
case F_FLOCK:
case F_FLOCKW:
/*
* Copy in input fields only.
*/
if (cmd == F_O_GETLK) {
if (datamodel != DATAMODEL_ILP32) {
error = EINVAL;
break;
}
if (copyin((void *)arg, &obf, sizeof (obf))) {
error = EFAULT;
break;
}
bf.l_type = obf.l_type;
bf.l_whence = obf.l_whence;
bf.l_start = (off64_t)obf.l_start;
bf.l_len = (off64_t)obf.l_len;
bf.l_sysid = (int)obf.l_sysid;
bf.l_pid = obf.l_pid;
} else if (datamodel == DATAMODEL_NATIVE) {
if (copyin((void *)arg, &sbf, sizeof (sbf))) {
error = EFAULT;
break;
}
/*
* XXX In an LP64 kernel with an LP64 application
* there's no need to do a structure copy here
* struct flock == struct flock64. However,
* we did it this way to avoid more conditional
* compilation.
*/
bf.l_type = sbf.l_type;
bf.l_whence = sbf.l_whence;
bf.l_start = (off64_t)sbf.l_start;
bf.l_len = (off64_t)sbf.l_len;
bf.l_sysid = sbf.l_sysid;
bf.l_pid = sbf.l_pid;
}
#if defined(_SYSCALL32_IMPL)
else {
struct flock32 sbf32;
if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
error = EFAULT;
break;
}
bf.l_type = sbf32.l_type;
bf.l_whence = sbf32.l_whence;
bf.l_start = (off64_t)sbf32.l_start;
bf.l_len = (off64_t)sbf32.l_len;
bf.l_sysid = sbf32.l_sysid;
bf.l_pid = sbf32.l_pid;
}
#endif /* _SYSCALL32_IMPL */
/*
* 64-bit support: check for overflow for 32-bit lock ops
*/
if ((error = flock_check(vp, &bf, offset, maxoffset)) != 0)
break;
if (cmd == F_FLOCK || cmd == F_FLOCKW) {
/* FLOCK* locking is always over the entire file. */
if (bf.l_whence != 0 || bf.l_start != 0 ||
bf.l_len != 0) {
error = EINVAL;
break;
}
if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
error = EINVAL;
break;
}
}
if (cmd == F_OFD_GETLK || cmd == F_OFD_SETLK ||
cmd == F_OFD_SETLKW) {
/*
* TBD OFD-style locking is currently limited to
* covering the entire file.
*/
if (bf.l_whence != 0 || bf.l_start != 0 ||
bf.l_len != 0) {
error = EINVAL;
break;
}
}
/*
* Not all of the filesystems understand F_O_GETLK, and
* there's no need for them to know. Map it to F_GETLK.
*
* The *_frlock functions in the various file systems basically
* do some validation and then funnel everything through the
* fs_frlock function. For OFD-style locks fs_frlock will do
* nothing so that once control returns here we can call the
* ofdlock function with the correct fp. For OFD-style locks
* the unsupported remote file systems, such as NFS, detect and
* reject the OFD-style cmd argument.
*/
if ((error = VOP_FRLOCK(vp, (cmd == F_O_GETLK) ? F_GETLK : cmd,
&bf, flag, offset, NULL, fp->f_cred, NULL)) != 0)
break;
if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
/*
* This is an OFD-style lock so we need to handle it
* here. Because OFD-style locks are associated with
* the file_t we didn't have enough info down the
* VOP_FRLOCK path immediately above.
*/
if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
break;
}
/*
* If command is GETLK and no lock is found, only
* the type field is changed.
*/
if ((cmd == F_O_GETLK || cmd == F_GETLK ||
cmd == F_OFD_GETLK) && bf.l_type == F_UNLCK) {
/* l_type always first entry, always a short */
if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
sizeof (bf.l_type)))
error = EFAULT;
break;
}
if (cmd == F_O_GETLK) {
/*
* Return an SVR3 flock structure to the user.
*/
obf.l_type = (int16_t)bf.l_type;
obf.l_whence = (int16_t)bf.l_whence;
obf.l_start = (int32_t)bf.l_start;
obf.l_len = (int32_t)bf.l_len;
if (bf.l_sysid > SHRT_MAX || bf.l_pid > SHRT_MAX) {
/*
* One or both values for the above fields
* is too large to store in an SVR3 flock
* structure.
*/
error = EOVERFLOW;
break;
}
obf.l_sysid = (int16_t)bf.l_sysid;
obf.l_pid = (int16_t)bf.l_pid;
if (copyout(&obf, (void *)arg, sizeof (obf)))
error = EFAULT;
} else if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
/*
* Copy out SVR4 flock.
*/
int i;
if (bf.l_start > maxoffset || bf.l_len > maxoffset) {
error = EOVERFLOW;
break;
}
if (datamodel == DATAMODEL_NATIVE) {
for (i = 0; i < 4; i++)
sbf.l_pad[i] = 0;
/*
* XXX In an LP64 kernel with an LP64
* application there's no need to do a
* structure copy here as currently
* struct flock == struct flock64.
* We did it this way to avoid more
* conditional compilation.
*/
sbf.l_type = bf.l_type;
sbf.l_whence = bf.l_whence;
sbf.l_start = (off_t)bf.l_start;
sbf.l_len = (off_t)bf.l_len;
sbf.l_sysid = bf.l_sysid;
sbf.l_pid = bf.l_pid;
if (copyout(&sbf, (void *)arg, sizeof (sbf)))
error = EFAULT;
}
#if defined(_SYSCALL32_IMPL)
else {
struct flock32 sbf32;
if (bf.l_start > MAXOFF32_T ||
bf.l_len > MAXOFF32_T) {
error = EOVERFLOW;
break;
}
for (i = 0; i < 4; i++)
sbf32.l_pad[i] = 0;
sbf32.l_type = (int16_t)bf.l_type;
sbf32.l_whence = (int16_t)bf.l_whence;
sbf32.l_start = (off32_t)bf.l_start;
sbf32.l_len = (off32_t)bf.l_len;
sbf32.l_sysid = (int32_t)bf.l_sysid;
sbf32.l_pid = (pid32_t)bf.l_pid;
if (copyout(&sbf32,
(void *)arg, sizeof (sbf32)))
error = EFAULT;
}
#endif
}
break;
case F_CHKFL:
/*
* This is for internal use only, to allow the vnode layer
* to validate a flags setting before applying it. User
* programs can't issue it.
*/
error = EINVAL;
break;
case F_ALLOCSP:
case F_FREESP:
case F_ALLOCSP64:
case F_FREESP64:
/*
* Test for not-a-regular-file (and returning EINVAL)
* before testing for open-for-writing (and returning EBADF).
* This is relied upon by posix_fallocate() in libc.
*/
if (vp->v_type != VREG) {
error = EINVAL;
break;
}
if ((flag & FWRITE) == 0) {
error = EBADF;
break;
}
if (datamodel != DATAMODEL_ILP32 &&
(cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
error = EINVAL;
break;
}
#if defined(_ILP32) || defined(_SYSCALL32_IMPL)
if (datamodel == DATAMODEL_ILP32 &&
(cmd == F_ALLOCSP || cmd == F_FREESP)) {
struct flock32 sbf32;
/*
* For compatibility we overlay an SVR3 flock on an SVR4
* flock. This works because the input field offsets
* in "struct flock" were preserved.
*/
if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
error = EFAULT;
break;
} else {
bf.l_type = sbf32.l_type;
bf.l_whence = sbf32.l_whence;
bf.l_start = (off64_t)sbf32.l_start;
bf.l_len = (off64_t)sbf32.l_len;
bf.l_sysid = sbf32.l_sysid;
bf.l_pid = sbf32.l_pid;
}
}
#endif /* _ILP32 || _SYSCALL32_IMPL */
#if defined(_LP64)
if (datamodel == DATAMODEL_LP64 &&
(cmd == F_ALLOCSP || cmd == F_FREESP)) {
if (copyin((void *)arg, &bf, sizeof (bf))) {
error = EFAULT;
break;
}
}
#endif /* defined(_LP64) */
#if !defined(_LP64) || defined(_SYSCALL32_IMPL)
if (datamodel == DATAMODEL_ILP32 &&
(cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
error = EFAULT;
break;
} else {
/*
* Note that the size of flock64 is different in
* the ILP32 and LP64 models, due to the l_pad
* field. We do not want to assume that the
* flock64 structure is laid out the same in
* ILP32 and LP64 environments, so we will
* copy in the ILP32 version of flock64
* explicitly and copy it to the native
* flock64 structure.
*/
bf.l_type = (short)bf64_32.l_type;
bf.l_whence = (short)bf64_32.l_whence;
bf.l_start = bf64_32.l_start;
bf.l_len = bf64_32.l_len;
bf.l_sysid = (int)bf64_32.l_sysid;
bf.l_pid = (pid_t)bf64_32.l_pid;
}
}
#endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
if (cmd == F_ALLOCSP || cmd == F_FREESP)
error = flock_check(vp, &bf, offset, maxoffset);
else if (cmd == F_ALLOCSP64 || cmd == F_FREESP64)
error = flock_check(vp, &bf, offset, MAXOFFSET_T);
if (error)
break;
if (vp->v_type == VREG && bf.l_len == 0 &&
bf.l_start > OFFSET_MAX(fp)) {
error = EFBIG;
break;
}
/*
* Make sure that there are no conflicting non-blocking
* mandatory locks in the region being manipulated. If
* there are such locks then return EACCES.
*/
if ((error = flock_get_start(vp, &bf, offset, &start)) != 0)
break;
if (nbl_need_check(vp)) {
u_offset_t begin;
ssize_t length;
nbl_start_crit(vp, RW_READER);
in_crit = 1;
vattr.va_mask = AT_SIZE;
if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
!= 0)
break;
begin = start > vattr.va_size ? vattr.va_size : start;
length = vattr.va_size > start ? vattr.va_size - start :
start - vattr.va_size;
if (nbl_conflict(vp, NBL_WRITE, begin, length, 0,
NULL)) {
error = EACCES;
break;
}
}
if (cmd == F_ALLOCSP64)
cmd = F_ALLOCSP;
else if (cmd == F_FREESP64)
cmd = F_FREESP;
error = VOP_SPACE(vp, cmd, &bf, flag, offset, fp->f_cred, NULL);
break;
#if !defined(_LP64) || defined(_SYSCALL32_IMPL)
case F_GETLK64:
case F_SETLK64:
case F_SETLKW64:
case F_SETLK64_NBMAND:
case F_OFD_GETLK64:
case F_OFD_SETLK64:
case F_OFD_SETLKW64:
case F_FLOCK64:
case F_FLOCKW64:
/*
* Large Files: Here we set cmd as *LK and send it to
* lower layers. *LK64 is only for the user land.
* Most of the comments described above for F_SETLK
* applies here too.
* Large File support is only needed for ILP32 apps!
*/
if (datamodel != DATAMODEL_ILP32) {
error = EINVAL;
break;
}
if (cmd == F_GETLK64)
cmd = F_GETLK;
else if (cmd == F_SETLK64)
cmd = F_SETLK;
else if (cmd == F_SETLKW64)
cmd = F_SETLKW;
else if (cmd == F_SETLK64_NBMAND)
cmd = F_SETLK_NBMAND;
else if (cmd == F_OFD_GETLK64)
cmd = F_OFD_GETLK;
else if (cmd == F_OFD_SETLK64)
cmd = F_OFD_SETLK;
else if (cmd == F_OFD_SETLKW64)
cmd = F_OFD_SETLKW;
else if (cmd == F_FLOCK64)
cmd = F_FLOCK;
else if (cmd == F_FLOCKW64)
cmd = F_FLOCKW;
/*
* Note that the size of flock64 is different in the ILP32
* and LP64 models, due to the sucking l_pad field.
* We do not want to assume that the flock64 structure is
* laid out in the same in ILP32 and LP64 environments, so
* we will copy in the ILP32 version of flock64 explicitly
* and copy it to the native flock64 structure.
*/
if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
error = EFAULT;
break;
}
bf.l_type = (short)bf64_32.l_type;
bf.l_whence = (short)bf64_32.l_whence;
bf.l_start = bf64_32.l_start;
bf.l_len = bf64_32.l_len;
bf.l_sysid = (int)bf64_32.l_sysid;
bf.l_pid = (pid_t)bf64_32.l_pid;
if ((error = flock_check(vp, &bf, offset, MAXOFFSET_T)) != 0)
break;
if (cmd == F_FLOCK || cmd == F_FLOCKW) {
/* FLOCK* locking is always over the entire file. */
if (bf.l_whence != 0 || bf.l_start != 0 ||
bf.l_len != 0) {
error = EINVAL;
break;
}
if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
error = EINVAL;
break;
}
}
if (cmd == F_OFD_GETLK || cmd == F_OFD_SETLK ||
cmd == F_OFD_SETLKW) {
/*
* TBD OFD-style locking is currently limited to
* covering the entire file.
*/
if (bf.l_whence != 0 || bf.l_start != 0 ||
bf.l_len != 0) {
error = EINVAL;
break;
}
}
/*
* The *_frlock functions in the various file systems basically
* do some validation and then funnel everything through the
* fs_frlock function. For OFD-style locks fs_frlock will do
* nothing so that once control returns here we can call the
* ofdlock function with the correct fp. For OFD-style locks
* the unsupported remote file systems, such as NFS, detect and
* reject the OFD-style cmd argument.
*/
if ((error = VOP_FRLOCK(vp, cmd, &bf, flag, offset,
NULL, fp->f_cred, NULL)) != 0)
break;
if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
/*
* This is an OFD-style lock so we need to handle it
* here. Because OFD-style locks are associated with
* the file_t we didn't have enough info down the
* VOP_FRLOCK path immediately above.
*/
if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
break;
}
if ((cmd == F_GETLK || cmd == F_OFD_GETLK) &&
bf.l_type == F_UNLCK) {
if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
sizeof (bf.l_type)))
error = EFAULT;
break;
}
if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
int i;
/*
* We do not want to assume that the flock64 structure
* is laid out in the same in ILP32 and LP64
* environments, so we will copy out the ILP32 version
* of flock64 explicitly after copying the native
* flock64 structure to it.
*/
for (i = 0; i < 4; i++)
bf64_32.l_pad[i] = 0;
bf64_32.l_type = (int16_t)bf.l_type;
bf64_32.l_whence = (int16_t)bf.l_whence;
bf64_32.l_start = bf.l_start;
bf64_32.l_len = bf.l_len;
bf64_32.l_sysid = (int32_t)bf.l_sysid;
bf64_32.l_pid = (pid32_t)bf.l_pid;
if (copyout(&bf64_32, (void *)arg, sizeof (bf64_32)))
error = EFAULT;
}
break;
#endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
case F_SHARE:
case F_SHARE_NBMAND:
case F_UNSHARE:
/*
* Copy in input fields only.
*/
if (copyin((void *)arg, &fsh, sizeof (fsh))) {
error = EFAULT;
break;
}
/*
* Local share reservations always have this simple form
*/
shr.s_access = fsh.f_access;
shr.s_deny = fsh.f_deny;
shr.s_sysid = 0;
shr.s_pid = ttoproc(curthread)->p_pid;
shr_own.sl_pid = shr.s_pid;
shr_own.sl_id = fsh.f_id;
shr.s_own_len = sizeof (shr_own);
shr.s_owner = (caddr_t)&shr_own;
error = VOP_SHRLOCK(vp, cmd, &shr, flag, fp->f_cred, NULL);
break;
default:
error = EINVAL;
break;
}
if (in_crit)
nbl_end_crit(vp);
done:
releasef(fdes);
out:
if (error)
return (set_errno(error));
return (retval);
}
int
flock_check(vnode_t *vp, flock64_t *flp, offset_t offset, offset_t max)
{
struct vattr vattr;
int error;
u_offset_t start, end;
/*
* Determine the starting point of the request
*/
switch (flp->l_whence) {
case 0: /* SEEK_SET */
start = (u_offset_t)flp->l_start;
if (start > max)
return (EINVAL);
break;
case 1: /* SEEK_CUR */
if (flp->l_start > (max - offset))
return (EOVERFLOW);
start = (u_offset_t)(flp->l_start + offset);
if (start > max)
return (EINVAL);
break;
case 2: /* SEEK_END */
vattr.va_mask = AT_SIZE;
if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
return (error);
if (flp->l_start > (max - (offset_t)vattr.va_size))
return (EOVERFLOW);
start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
if (start > max)
return (EINVAL);
break;
default:
return (EINVAL);
}
/*
* Determine the range covered by the request.
*/
if (flp->l_len == 0)
end = MAXEND;
else if ((offset_t)flp->l_len > 0) {
if (flp->l_len > (max - start + 1))
return (EOVERFLOW);
end = (u_offset_t)(start + (flp->l_len - 1));
ASSERT(end <= max);
} else {
/*
* Negative length; why do we even allow this ?
* Because this allows easy specification of
* the last n bytes of the file.
*/
end = start;
start += (u_offset_t)flp->l_len;
(start)++;
if (start > max)
return (EINVAL);
ASSERT(end <= max);
}
ASSERT(start <= max);
if (flp->l_type == F_UNLCK && flp->l_len > 0 &&
end == (offset_t)max) {
flp->l_len = 0;
}
if (start > end)
return (EINVAL);
return (0);
}
static int
flock_get_start(vnode_t *vp, flock64_t *flp, offset_t offset, u_offset_t *start)
{
struct vattr vattr;
int error;
/*
* Determine the starting point of the request. Assume that it is
* a valid starting point.
*/
switch (flp->l_whence) {
case 0: /* SEEK_SET */
*start = (u_offset_t)flp->l_start;
break;
case 1: /* SEEK_CUR */
*start = (u_offset_t)(flp->l_start + offset);
break;
case 2: /* SEEK_END */
vattr.va_mask = AT_SIZE;
if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
return (error);
*start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
break;
default:
return (EINVAL);
}
return (0);
}
/*
* Take rctl action when the requested file descriptor is too big.
*/
static void
fd_too_big(proc_t *p)
{
mutex_enter(&p->p_lock);
(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
p->p_rctls, p, RCA_SAFE);
mutex_exit(&p->p_lock);
}
/*
* 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.
* Copyright 2024 Oxide Computer Company
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/types.h>
#include <sys/cred.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/vfs.h>
#include <sys/file.h>
#include <sys/fdsync.h>
/*
* This is the common system call for fsync(), fdatasync(), and syncfs(). It
* performs the requested I/O synchronization for the file descriptor.
*/
int
fdsync(int fd, uint32_t arg)
{
file_t *fp;
int ret;
if ((fp = getf(fd)) == NULL) {
return (set_errno(EBADF));
}
switch (arg) {
case FDSYNC_FS:
ret = VFS_SYNCFS(fp->f_vnode->v_vfsp, 0, fp->f_cred);
break;
case FDSYNC_FILE:
ret = VOP_FSYNC(fp->f_vnode, FSYNC, fp->f_cred, NULL);
break;
case FDSYNC_DATA:
ret = VOP_FSYNC(fp->f_vnode, FDSYNC, fp->f_cred, NULL);
break;
default:
ret = EINVAL;
break;
}
releasef(fd);
if (ret != 0) {
(void) set_errno(ret);
}
return (ret);
}
/*
* 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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
*/
#include <sys/copyops.h>
#include <sys/errno.h>
#include <sys/kmem.h>
#include <sys/param.h>
#include <sys/pathname.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/vnode.h>
int
getcwd(char *buf, size_t buflen)
{
int err;
char *kbuf;
size_t kbuflen;
/*
* If the buffer cannot accommodate one character and nul terminator,
* it is too small.
*/
if (buflen < 2)
return (set_errno(ERANGE));
/*
* The user should be able to specify any size buffer, but we don't want
* to arbitrarily allocate huge kernel buffers just because the user
* requests it. So we'll start with MAXPATHLEN (which should hold any
* normal path), and only increase it if we fail with
* ERANGE / ENAMETOOLONG.
*
* To protect against unbounded memory usage, cap to kmem_max_cached.
* This is far bigger than the length of any practical path on the
* system, and avoids allocating memory from the kmem_oversized arena.
*/
kbuflen = MIN(buflen, MAXPATHLEN);
while (kbuflen <= kmem_max_cached) {
kbuf = kmem_alloc(kbuflen, KM_SLEEP);
if (((err = dogetcwd(kbuf, kbuflen)) == 0) &&
(copyout(kbuf, buf, strlen(kbuf) + 1) != 0)) {
err = EFAULT;
}
kmem_free(kbuf, kbuflen);
/*
* dogetcwd() inconsistently returns ERANGE or ENAMETOOLONG
* depending on whether it calls dirtopath() and then whether
* the subsequent operations run out of space whilst
* evaluating a cached vnode path or otherwise.
*/
if (err == ENAMETOOLONG || err == ERANGE) {
/* For some reason, getcwd() uses ERANGE. */
err = ERANGE;
/*
* If the user's buffer really was too small, give up.
*/
if (kbuflen == buflen)
break;
kbuflen = MIN(kbuflen * 2, buflen);
} else {
break;
}
}
return ((err != 0) ? set_errno(err) : 0);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/inttypes.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/dirent.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/mode.h>
#include <sys/uio.h>
#include <sys/filio.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#if defined(_SYSCALL32_IMPL) || defined(_ILP32)
/*
* Get directory entries in a file system-independent format.
*
* The 32-bit version of this function now allocates a buffer to grab the
* directory entries in dirent64 formats from VOP_READDIR routines.
* The dirent64 structures are converted to dirent32 structures and
* copied to the user space.
*
* Both 32-bit and 64-bit versions of libc use getdents64() and therefore
* we don't expect any major performance impact due to the extra kmem_alloc's
* and copying done in this routine.
*/
/*
* Native 32-bit system call for non-large-file applications.
*/
int
getdents32(int fd, void *buf, size_t count)
{
vnode_t *vp;
file_t *fp;
struct uio auio;
struct iovec aiov;
register int error;
int sink;
char *newbuf;
char *obuf;
int bufsize;
int osize, nsize;
struct dirent64 *dp;
struct dirent32 *op;
if (count < sizeof (struct dirent32))
return (set_errno(EINVAL));
if ((fp = getf(fd)) == NULL)
return (set_errno(EBADF));
vp = fp->f_vnode;
if (vp->v_type != VDIR) {
releasef(fd);
return (set_errno(ENOTDIR));
}
if (!(fp->f_flag & FREAD)) {
releasef(fd);
return (set_errno(EBADF));
}
/*
* Don't let the user overcommit kernel resources.
*/
if (count > MAXGETDENTS_SIZE)
count = MAXGETDENTS_SIZE;
bufsize = count;
newbuf = kmem_alloc(bufsize, KM_SLEEP);
obuf = kmem_alloc(bufsize, KM_SLEEP);
aiov.iov_base = newbuf;
aiov.iov_len = count;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_loffset = fp->f_offset;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_resid = count;
auio.uio_fmode = 0;
auio.uio_extflg = UIO_COPY_CACHED;
(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
error = VOP_READDIR(vp, &auio, fp->f_cred, &sink, NULL, 0);
VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
if (error)
goto out;
count = count - auio.uio_resid;
fp->f_offset = auio.uio_loffset;
dp = (struct dirent64 *)newbuf;
op = (struct dirent32 *)obuf;
osize = 0;
nsize = 0;
while (nsize < count) {
uint32_t reclen, namlen;
/*
* This check ensures that the 64 bit d_ino and d_off
* fields will fit into their 32 bit equivalents.
*
* Although d_off is a signed value, the check is done
* against the full 32 bits because certain file systems,
* NFS for one, allow directory cookies to use the full
* 32 bits. We use uint64_t because there is no exact
* unsigned analog to the off64_t type of dp->d_off.
*/
if (dp->d_ino > (ino64_t)UINT32_MAX ||
dp->d_off > (uint64_t)UINT32_MAX) {
error = EOVERFLOW;
goto out;
}
op->d_ino = (ino32_t)dp->d_ino;
op->d_off = (off32_t)dp->d_off;
namlen = strlen(dp->d_name);
reclen = DIRENT32_RECLEN(namlen);
op->d_reclen = (uint16_t)reclen;
/* use strncpy(9f) to zero out uninitialized bytes */
(void) strncpy(op->d_name, dp->d_name,
DIRENT32_NAMELEN(reclen));
nsize += (uint_t)dp->d_reclen;
osize += (uint_t)op->d_reclen;
dp = (struct dirent64 *)((char *)dp + (uint_t)dp->d_reclen);
op = (struct dirent32 *)((char *)op + (uint_t)op->d_reclen);
}
ASSERT(osize <= count);
ASSERT((char *)op <= (char *)obuf + bufsize);
ASSERT((char *)dp <= (char *)newbuf + bufsize);
if ((error = copyout(obuf, buf, osize)) < 0)
error = EFAULT;
out:
kmem_free(newbuf, bufsize);
kmem_free(obuf, bufsize);
if (error) {
releasef(fd);
return (set_errno(error));
}
releasef(fd);
return (osize);
}
#endif /* _SYSCALL32 || _ILP32 */
int
getdents64(int fd, void *buf, size_t count)
{
vnode_t *vp;
file_t *fp;
struct uio auio;
struct iovec aiov;
register int error;
int sink;
if (count < sizeof (struct dirent64))
return (set_errno(EINVAL));
/*
* Don't let the user overcommit kernel resources.
*/
if (count > MAXGETDENTS_SIZE)
count = MAXGETDENTS_SIZE;
if ((fp = getf(fd)) == NULL)
return (set_errno(EBADF));
vp = fp->f_vnode;
if (vp->v_type != VDIR) {
releasef(fd);
return (set_errno(ENOTDIR));
}
if (!(fp->f_flag & FREAD)) {
releasef(fd);
return (set_errno(EBADF));
}
aiov.iov_base = buf;
aiov.iov_len = count;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_loffset = fp->f_offset;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_resid = count;
auio.uio_fmode = 0;
auio.uio_extflg = UIO_COPY_CACHED;
(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
error = VOP_READDIR(vp, &auio, fp->f_cred, &sink, NULL, 0);
VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
if (error) {
releasef(fd);
return (set_errno(error));
}
count = count - auio.uio_resid;
fp->f_offset = auio.uio_loffset;
releasef(fd);
return (count);
}
/*
* 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.
* Copyright 2011 Joyent, Inc. All rights reserved.
*/
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/loadavg.h>
#include <sys/zone.h>
#include <sys/pool_pset.h>
/*
* Extract elements of the raw avenrun array from the kernel for the
* implementation of getloadavg(3c)
*/
int
getloadavg(int *buf, int nelem)
{
int *loadbuf = &avenrun[0];
int error;
if (nelem < 0)
return (set_errno(EINVAL));
if (nelem > LOADAVG_NSTATS)
nelem = LOADAVG_NSTATS;
if (!INGLOBALZONE(curproc)) {
loadbuf = &curproc->p_zone->zone_avenrun[0];
}
error = copyout(loadbuf, buf, nelem * sizeof (avenrun[0]));
if (error)
return (set_errno(EFAULT));
return (nelem);
}
/*
* 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/systm.h>
#include <vm/page.h>
#include <sys/errno.h>
/*
* Return supported page sizes.
*/
int
getpagesizes(int legacy, size_t *buf, int nelem)
{
int i, pagesizes = page_num_user_pagesizes(legacy);
size_t *pgsza;
if (nelem < 0) {
return (set_errno(EINVAL));
}
if (nelem == 0 && buf != NULL) {
return (set_errno(EINVAL));
}
if (nelem == 0 && buf == NULL) {
return (pagesizes);
}
if (buf == NULL) {
return (set_errno(EINVAL));
}
if (nelem > pagesizes) {
nelem = pagesizes;
}
pgsza = kmem_alloc(sizeof (*pgsza) * nelem, KM_SLEEP);
for (i = 0; i < nelem; i++) {
pgsza[i] = page_get_user_pagesize(i);
}
if (copyout(pgsza, buf, nelem * sizeof (*pgsza)) != 0) {
kmem_free(pgsza, sizeof (*pgsza) * nelem);
return (set_errno(EFAULT));
}
kmem_free(pgsza, sizeof (*pgsza) * nelem);
return (nelem);
}
#if defined(_SYSCALL32_IMPL)
/*
* Some future platforms will support page sizes larger than
* a 32-bit address space.
*/
int
getpagesizes32(int legacy, size32_t *buf, int nelem)
{
int i, pagesizes = page_num_user_pagesizes(legacy);
size32_t *pgsza32;
size_t pgsz;
int rc;
if (nelem < 0) {
return (set_errno(EINVAL));
}
if (nelem == 0 && buf != NULL) {
return (set_errno(EINVAL));
}
pgsza32 = kmem_alloc(sizeof (*pgsza32) * pagesizes, KM_SLEEP);
for (i = 0; i < pagesizes; i++) {
pgsz = page_get_user_pagesize(i);
pgsza32[i] = (size32_t)pgsz;
if (pgsz > (size32_t)-1) {
pagesizes = i - 1;
break;
}
}
ASSERT(pagesizes > 0);
ASSERT(page_get_user_pagesize(pagesizes - 1) <= (size32_t)-1);
if (nelem > pagesizes) {
nelem = pagesizes;
}
if (nelem == 0 && buf == NULL) {
rc = pagesizes;
goto done;
}
if (buf == NULL) {
rc = set_errno(EINVAL);
goto done;
}
if (copyout(pgsza32, buf, nelem * sizeof (*pgsza32)) != 0) {
rc = set_errno(EFAULT);
goto done;
}
rc = nelem;
done:
kmem_free(pgsza32, sizeof (*pgsza32) *
page_num_user_pagesizes(legacy));
return (rc);
}
#endif
/*
* 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.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/debug.h>
#include <sys/zone.h>
int64_t
getpid(void)
{
rval_t r;
proc_t *p;
p = ttoproc(curthread);
r.r_val1 = p->p_pid;
if (p->p_flag & SZONETOP)
r.r_val2 = curproc->p_zone->zone_zsched->p_pid;
else
r.r_val2 = p->p_ppid;
return (r.r_vals);
}
/*
* 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 (c) 2018, Joyent, Inc.
*/
/*
* getrandom system call implementation
*/
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/systm.h>
#include <sys/random.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sysmacros.h>
#include <sys/random.h>
/*
* Impose a maximum upper bound on the number of bytes that we'll read in one
* go, ala a read of /dev/random. For /dev/urandom, we clamp it based on our
* return value, because the system call returns an int, we can't handle more
* than INT_MAX.
*/
#define MAXRANDBYTES 1024
#define MAXURANDBYTES INT_MAX
ssize_t
getrandom(void *bufp, size_t buflen, unsigned int flags)
{
int out = 0;
uint8_t rbytes[128];
uint8_t *buf = bufp;
if (flags & ~(GRND_NONBLOCK | GRND_RANDOM))
return (set_errno(EINVAL));
if ((flags & GRND_RANDOM) && buflen > MAXRANDBYTES) {
buflen = MAXRANDBYTES;
} else if (buflen > MAXURANDBYTES) {
buflen = MAXURANDBYTES;
}
while (out < buflen) {
int err;
size_t len = MIN(sizeof (rbytes), buflen - out);
if (flags & GRND_RANDOM) {
if (flags & GRND_NONBLOCK)
err = random_get_bytes(rbytes, len);
else
err = random_get_blocking_bytes(rbytes, len);
} else {
err = random_get_pseudo_bytes(rbytes, len);
}
if (err == 0) {
if (ddi_copyout(rbytes, buf + out, len, 0) != 0)
return (set_errno(EFAULT));
out += len;
} else if (err == EAGAIN && out > 0) {
break;
} else {
return (set_errno(err));
}
}
return (out);
}
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/cred_impl.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/debug.h>
#include <sys/policy.h>
int
setgid(gid_t gid)
{
proc_t *p;
int error;
int do_nocd = 0;
cred_t *cr, *newcr;
ksid_t ksid, *ksp;
zone_t *zone = crgetzone(CRED());
if (!VALID_GID(gid, zone))
return (set_errno(EINVAL));
if (gid > MAXUID) {
if (ksid_lookupbygid(zone, gid, &ksid) != 0)
return (set_errno(EINVAL));
ksp = &ksid;
} else {
ksp = NULL;
}
/*
* Need to pre-allocate the new cred structure before grabbing
* the p_crlock mutex. We cannot hold the mutex across the
* secpolicy functions.
*/
newcr = cralloc_ksid();
p = ttoproc(curthread);
mutex_enter(&p->p_crlock);
retry:
cr = p->p_cred;
crhold(cr);
mutex_exit(&p->p_crlock);
if ((gid == cr->cr_rgid || gid == cr->cr_sgid) &&
secpolicy_allow_setid(cr, -1, B_TRUE) != 0) {
mutex_enter(&p->p_crlock);
crfree(cr);
if (cr != p->p_cred)
goto retry;
error = 0;
crcopy_to(cr, newcr);
p->p_cred = newcr;
newcr->cr_gid = gid;
crsetsid(newcr, ksp, KSID_GROUP);
mutex_exit(&p->p_crlock);
} else if ((error = secpolicy_allow_setid(cr, -1, B_FALSE)) == 0) {
mutex_enter(&p->p_crlock);
crfree(cr);
if (cr != p->p_cred)
goto retry;
/*
* A privileged process that makes itself look like a
* set-gid process must be marked to produce no core dump.
*/
if (cr->cr_gid != gid ||
cr->cr_rgid != gid ||
cr->cr_sgid != gid)
do_nocd = 1;
crcopy_to(cr, newcr);
p->p_cred = newcr;
newcr->cr_gid = gid;
newcr->cr_rgid = gid;
newcr->cr_sgid = gid;
crsetsid(newcr, ksp, KSID_GROUP);
mutex_exit(&p->p_crlock);
} else {
crfree(newcr);
crfree(cr);
if (ksp != NULL)
ksid_rele(ksp);
}
if (error == 0) {
if (do_nocd) {
mutex_enter(&p->p_lock);
p->p_flag |= SNOCD;
mutex_exit(&p->p_lock);
}
crset(p, newcr); /* broadcast to process threads */
return (0);
}
return (set_errno(error));
}
int64_t
getgid(void)
{
rval_t r;
cred_t *cr;
cr = curthread->t_cred;
r.r_val1 = cr->cr_rgid;
r.r_val2 = cr->cr_gid;
return (r.r_vals);
}
int
setegid(gid_t gid)
{
proc_t *p;
cred_t *cr, *newcr;
int error = EPERM;
int do_nocd = 0;
ksid_t ksid, *ksp;
zone_t *zone = crgetzone(CRED());
if (!VALID_GID(gid, zone))
return (set_errno(EINVAL));
if (gid > MAXUID) {
if (ksid_lookupbygid(zone, gid, &ksid) != 0)
return (set_errno(EINVAL));
ksp = &ksid;
} else {
ksp = NULL;
}
/*
* Need to pre-allocate the new cred structure before grabbing
* the p_crlock mutex.
*/
newcr = cralloc_ksid();
p = ttoproc(curthread);
mutex_enter(&p->p_crlock);
retry:
crhold(cr = p->p_cred);
mutex_exit(&p->p_crlock);
if (gid == cr->cr_rgid || gid == cr->cr_gid || gid == cr->cr_sgid ||
(error = secpolicy_allow_setid(cr, -1, B_FALSE)) == 0) {
mutex_enter(&p->p_crlock);
crfree(cr);
if (cr != p->p_cred)
goto retry;
/*
* A privileged process that makes itself look like a
* set-gid process must be marked to produce no core dump.
*/
if (cr->cr_gid != gid && error == 0)
do_nocd = 1;
error = 0;
crcopy_to(cr, newcr);
p->p_cred = newcr;
newcr->cr_gid = gid;
crsetsid(newcr, ksp, KSID_GROUP);
mutex_exit(&p->p_crlock);
} else {
crfree(newcr);
crfree(cr);
if (ksp != NULL)
ksid_rele(ksp);
}
if (error == 0) {
if (do_nocd) {
mutex_enter(&p->p_lock);
p->p_flag |= SNOCD;
mutex_exit(&p->p_lock);
}
crset(p, newcr); /* broadcast to process threads */
return (0);
}
return (set_errno(error));
}
/*
* Buy-back from SunOS 4.x
*
* Like setgid() and setegid() combined -except- that non-root users
* can change cr_rgid to cr_gid, and the semantics of cr_sgid are
* subtly different.
*/
int
setregid(gid_t rgid, gid_t egid)
{
proc_t *p;
int error = EPERM;
int do_nocd = 0;
cred_t *cr, *newcr;
ksid_t ksid, *ksp;
zone_t *zone = crgetzone(CRED());
if ((rgid != -1 && !VALID_GID(rgid, zone)) ||
(egid != -1 && !VALID_GID(egid, zone)))
return (set_errno(EINVAL));
if (egid != -1 && egid > MAXUID) {
if (ksid_lookupbygid(zone, egid, &ksid) != 0)
return (set_errno(EINVAL));
ksp = &ksid;
} else {
ksp = NULL;
}
/*
* Need to pre-allocate the new cred structure before grabbing
* the p_crlock mutex.
*/
newcr = cralloc_ksid();
p = ttoproc(curthread);
mutex_enter(&p->p_crlock);
cr = p->p_cred;
if ((rgid == -1 ||
rgid == cr->cr_rgid || rgid == cr->cr_gid || rgid == cr->cr_sgid) &&
(egid == -1 || egid == cr->cr_rgid || egid == cr->cr_gid ||
egid == cr->cr_sgid) ||
(error = secpolicy_allow_setid(cr, -1, B_FALSE)) == 0) {
crhold(cr);
crcopy_to(cr, newcr);
p->p_cred = newcr;
if (egid != -1) {
newcr->cr_gid = egid;
crsetsid(newcr, ksp, KSID_GROUP);
}
if (rgid != -1)
newcr->cr_rgid = rgid;
/*
* "If the real gid is being changed, or the effective gid is
* being changed to a value not equal to the real gid, the
* saved gid is set to the new effective gid."
*/
if (rgid != -1 ||
(egid != -1 && newcr->cr_gid != newcr->cr_rgid))
newcr->cr_sgid = newcr->cr_gid;
/*
* A privileged process that makes itself look like a
* set-gid process must be marked to produce no core dump.
*/
if ((cr->cr_gid != newcr->cr_gid ||
cr->cr_rgid != newcr->cr_rgid ||
cr->cr_sgid != newcr->cr_sgid) && error == 0)
do_nocd = 1;
error = 0;
crfree(cr);
}
mutex_exit(&p->p_crlock);
if (error == 0) {
if (do_nocd) {
mutex_enter(&p->p_lock);
p->p_flag |= SNOCD;
mutex_exit(&p->p_lock);
}
crset(p, newcr); /* broadcast to process threads */
return (0);
}
crfree(newcr);
if (ksp != NULL)
ksid_rele(ksp);
return (set_errno(error));
}
/*
* 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) 1984, 1986, 1987, 1988, 1989 AT&T
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/cred.h>
#include <sys/sid.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/policy.h>
int
setgroups(int gidsetsize, gid_t *gidset)
{
proc_t *p;
cred_t *cr, *newcr;
int i;
int n = gidsetsize;
int error;
int scnt = 0;
ksidlist_t *ksl = NULL;
zone_t *zone;
struct credgrp *grps = NULL;
/* Perform the cheapest tests before grabbing p_crlock */
if (n > ngroups_max || n < 0)
return (set_errno(EINVAL));
zone = crgetzone(CRED());
if (n != 0) {
const gid_t *groups;
grps = crgrpcopyin(n, gidset);
if (grps == NULL)
return (set_errno(EFAULT));
groups = crgetggroups(grps);
for (i = 0; i < n; i++) {
if (!VALID_GID(groups[i], zone)) {
crgrprele(grps);
return (set_errno(EINVAL));
}
if (groups[i] > MAXUID)
scnt++;
}
if (scnt > 0) {
ksl = kcrsid_gidstosids(zone, n, (gid_t *)groups);
if (ksl == NULL) {
crgrprele(grps);
return (set_errno(EINVAL));
}
}
}
/*
* Need to pre-allocate the new cred structure before acquiring
* the p_crlock mutex.
*/
newcr = cralloc_ksid();
p = ttoproc(curthread);
mutex_enter(&p->p_crlock);
retry:
cr = p->p_cred;
crhold(cr);
mutex_exit(&p->p_crlock);
if ((error = secpolicy_allow_setid(cr, -1, B_FALSE)) != 0) {
if (grps != NULL)
crgrprele(grps);
if (ksl != NULL)
ksidlist_rele(ksl);
crfree(newcr);
crfree(cr);
return (set_errno(error));
}
mutex_enter(&p->p_crlock);
crfree(cr);
if (cr != p->p_cred)
goto retry;
crdup_to(cr, newcr);
crsetsidlist(newcr, ksl);
crsetcredgrp(newcr, grps);
p->p_cred = newcr;
crhold(newcr); /* hold for the current thread */
crfree(cr); /* free the old one */
mutex_exit(&p->p_crlock);
/*
* Broadcast new cred to process threads (including the current one).
*/
crset(p, newcr);
return (0);
}
int
getgroups(int gidsetsize, gid_t *gidset)
{
struct cred *cr;
int n;
cr = curthread->t_cred;
n = crgetngroups(cr);
if (gidsetsize != 0) {
if (gidsetsize < n)
return (set_errno(EINVAL));
if (copyout(crgetgroups(cr), gidset, n * sizeof (gid_t)))
return (set_errno(EFAULT));
}
return (n);
}
/*
* 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.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/ttold.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/mode.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/kmem.h>
#include <sys/filio.h>
#include <sys/sunddi.h>
#include <sys/debug.h>
#include <sys/int_limits.h>
#include <sys/model.h>
/*
* I/O control.
*/
int
ioctl(int fdes, int cmd, intptr_t arg)
{
file_t *fp;
int error = 0;
vnode_t *vp;
struct vattr vattr;
int32_t flag;
int rv = 0;
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
vp = fp->f_vnode;
if (vp->v_type == VREG || vp->v_type == VDIR) {
/*
* Handle these two ioctls for regular files and
* directories. All others will usually be failed
* with ENOTTY by the VFS-dependent code. System V
* always failed all ioctls on regular files, but SunOS
* supported these.
*/
switch (cmd) {
case FIONREAD: {
/*
* offset is int32_t because that is what FIONREAD
* is defined in terms of. We cap at INT_MAX as in
* other cases for this ioctl.
*/
int32_t offset;
vattr.va_mask = AT_SIZE;
error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred, NULL);
if (error) {
releasef(fdes);
return (set_errno(error));
}
offset = MIN(vattr.va_size - fp->f_offset, INT_MAX);
if (copyout(&offset, (caddr_t)arg, sizeof (offset))) {
releasef(fdes);
return (set_errno(EFAULT));
}
releasef(fdes);
return (0);
}
case FIONBIO:
if (copyin((caddr_t)arg, &flag, sizeof (flag))) {
releasef(fdes);
return (set_errno(EFAULT));
}
mutex_enter(&fp->f_tlock);
if (flag)
fp->f_flag |= FNDELAY;
else
fp->f_flag &= ~FNDELAY;
mutex_exit(&fp->f_tlock);
releasef(fdes);
return (0);
default:
break;
}
}
/*
* ioctl() now passes in the model information in some high bits.
*/
flag = fp->f_flag | get_udatamodel();
error = VOP_IOCTL(fp->f_vnode, cmd, arg, flag, CRED(), &rv, NULL);
if (error != 0) {
releasef(fdes);
return (set_errno(error));
}
switch (cmd) {
case FIONBIO:
if (copyin((caddr_t)arg, &flag, sizeof (flag))) {
releasef(fdes);
return (set_errno(EFAULT));
}
mutex_enter(&fp->f_tlock);
if (flag)
fp->f_flag |= FNDELAY;
else
fp->f_flag &= ~FNDELAY;
mutex_exit(&fp->f_tlock);
break;
default:
break;
}
releasef(fdes);
return (rv);
}
/*
* Old stty and gtty. (Still.)
*/
int
stty(int fdes, intptr_t arg)
{
return (ioctl(fdes, TIOCSETP, arg));
}
int
gtty(int fdes, intptr_t arg)
{
return (ioctl(fdes, TIOCGETP, arg));
}
/*
* 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 (c) 2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#include <sys/proc.h>
/*
* System call returns true if the process was the result of exec'ing a set-uid
* or set-gid executable or was exec'ed with mismatch between real and
* effective uids or gids; false in all other cases.
*/
int
issetugid(void)
{
return ((curproc->p_flag & SUGID) != 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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2015 Joyent, Inc.
*/
/*
* lgroup system calls
*/
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/sunddi.h>
#include <sys/systm.h>
#include <sys/mman.h>
#include <sys/cpupart.h>
#include <sys/lgrp.h>
#include <sys/lgrp_user.h>
#include <sys/promif.h> /* for prom_printf() */
#include <sys/sysmacros.h>
#include <sys/policy.h>
#include <vm/as.h>
/* definitions for mi_validity */
#define VALID_ADDR 1
#define VALID_REQ 2
/*
* run through the given number of addresses and requests and return the
* corresponding memory information for each address
*/
static int
meminfo(int addr_count, struct meminfo *mip)
{
size_t in_size, out_size, req_size, val_size;
struct as *as;
struct hat *hat;
int i, j, out_idx, info_count;
lgrp_t *lgrp;
pfn_t pfn;
ssize_t pgsz;
int *req_array, *val_array;
uint64_t *in_array, *out_array;
uint64_t addr, paddr;
uintptr_t vaddr;
int ret = 0;
struct meminfo minfo;
#if defined(_SYSCALL32_IMPL)
struct meminfo32 minfo32;
#endif
/*
* Make sure that there is at least one address to translate and
* limit how many virtual addresses the kernel can do per call
*/
if (addr_count < 1)
return (set_errno(EINVAL));
else if (addr_count > MAX_MEMINFO_CNT)
addr_count = MAX_MEMINFO_CNT;
if (get_udatamodel() == DATAMODEL_NATIVE) {
if (copyin(mip, &minfo, sizeof (struct meminfo)))
return (set_errno(EFAULT));
}
#if defined(_SYSCALL32_IMPL)
else {
bzero(&minfo, sizeof (minfo));
if (copyin(mip, &minfo32, sizeof (struct meminfo32)))
return (set_errno(EFAULT));
minfo.mi_inaddr = (const uint64_t *)(uintptr_t)
minfo32.mi_inaddr;
minfo.mi_info_req = (const uint_t *)(uintptr_t)
minfo32.mi_info_req;
minfo.mi_info_count = minfo32.mi_info_count;
minfo.mi_outdata = (uint64_t *)(uintptr_t)
minfo32.mi_outdata;
minfo.mi_validity = (uint_t *)(uintptr_t)
minfo32.mi_validity;
}
#endif
/*
* all the input parameters have been copied in:-
* addr_count - number of input addresses
* minfo.mi_inaddr - array of input addresses
* minfo.mi_info_req - array of types of information requested
* minfo.mi_info_count - no. of pieces of info requested for each addr
* minfo.mi_outdata - array into which the results are placed
* minfo.mi_validity - array containing bitwise result codes; 0th bit
* evaluates validity of corresponding input
* address, 1st bit validity of response to first
* member of info_req, etc.
*/
/* make sure mi_info_count is within limit */
info_count = minfo.mi_info_count;
if (info_count < 1 || info_count > MAX_MEMINFO_REQ)
return (set_errno(EINVAL));
/*
* allocate buffer in_array for the input addresses and copy them in
*/
in_size = sizeof (uint64_t) * addr_count;
in_array = kmem_alloc(in_size, KM_SLEEP);
if (copyin(minfo.mi_inaddr, in_array, in_size)) {
kmem_free(in_array, in_size);
return (set_errno(EFAULT));
}
/*
* allocate buffer req_array for the input info_reqs and copy them in
*/
req_size = sizeof (uint_t) * info_count;
req_array = kmem_alloc(req_size, KM_SLEEP);
if (copyin(minfo.mi_info_req, req_array, req_size)) {
kmem_free(req_array, req_size);
kmem_free(in_array, in_size);
return (set_errno(EFAULT));
}
/*
* Validate privs for each req.
*/
for (i = 0; i < info_count; i++) {
switch (req_array[i] & MEMINFO_MASK) {
case MEMINFO_VLGRP:
case MEMINFO_VPAGESIZE:
break;
default:
if (secpolicy_meminfo(CRED()) != 0) {
kmem_free(req_array, req_size);
kmem_free(in_array, in_size);
return (set_errno(EPERM));
}
break;
}
}
/*
* allocate buffer out_array which holds the results and will have
* to be copied out later
*/
out_size = sizeof (uint64_t) * addr_count * info_count;
out_array = kmem_alloc(out_size, KM_SLEEP);
/*
* allocate buffer val_array which holds the validity bits and will
* have to be copied out later
*/
val_size = sizeof (uint_t) * addr_count;
val_array = kmem_alloc(val_size, KM_SLEEP);
if ((req_array[0] & MEMINFO_MASK) == MEMINFO_PLGRP) {
/* find the corresponding lgroup for each physical address */
for (i = 0; i < addr_count; i++) {
paddr = in_array[i];
pfn = btop(paddr);
lgrp = lgrp_pfn_to_lgrp(pfn);
if (lgrp) {
out_array[i] = lgrp->lgrp_id;
val_array[i] = VALID_ADDR | VALID_REQ;
} else {
out_array[i] = 0;
val_array[i] = 0;
}
}
} else {
/* get the corresponding memory info for each virtual address */
as = curproc->p_as;
AS_LOCK_ENTER(as, RW_READER);
hat = as->a_hat;
for (i = out_idx = 0; i < addr_count; i++, out_idx +=
info_count) {
addr = in_array[i];
vaddr = (uintptr_t)(addr & ~PAGEOFFSET);
if (!as_segat(as, (caddr_t)vaddr)) {
val_array[i] = 0;
continue;
}
val_array[i] = VALID_ADDR;
pfn = hat_getpfnum(hat, (caddr_t)vaddr);
if (pfn != PFN_INVALID) {
paddr = (uint64_t)((pfn << PAGESHIFT) |
(addr & PAGEOFFSET));
for (j = 0; j < info_count; j++) {
switch (req_array[j] & MEMINFO_MASK) {
case MEMINFO_VPHYSICAL:
/*
* return the physical address
* corresponding to the input
* virtual address
*/
out_array[out_idx + j] = paddr;
val_array[i] |= VALID_REQ << j;
break;
case MEMINFO_VLGRP:
/*
* return the lgroup of physical
* page corresponding to the
* input virtual address
*/
lgrp = lgrp_pfn_to_lgrp(pfn);
if (lgrp) {
out_array[out_idx + j] =
lgrp->lgrp_id;
val_array[i] |=
VALID_REQ << j;
}
break;
case MEMINFO_VPAGESIZE:
/*
* return the size of physical
* page corresponding to the
* input virtual address
*/
pgsz = hat_getpagesize(hat,
(caddr_t)vaddr);
if (pgsz != -1) {
out_array[out_idx + j] =
pgsz;
val_array[i] |=
VALID_REQ << j;
}
break;
case MEMINFO_VREPLCNT:
/*
* for future use:-
* return the no. replicated
* physical pages corresponding
* to the input virtual address,
* so it is always 0 at the
* moment
*/
out_array[out_idx + j] = 0;
val_array[i] |= VALID_REQ << j;
break;
case MEMINFO_VREPL:
/*
* for future use:-
* return the nth physical
* replica of the specified
* virtual address
*/
break;
case MEMINFO_VREPL_LGRP:
/*
* for future use:-
* return the lgroup of nth
* physical replica of the
* specified virtual address
*/
break;
case MEMINFO_PLGRP:
/*
* this is for physical address
* only, shouldn't mix with
* virtual address
*/
break;
default:
break;
}
}
}
}
AS_LOCK_EXIT(as);
}
/* copy out the results and validity bits and free the buffers */
if ((copyout(out_array, minfo.mi_outdata, out_size) != 0) ||
(copyout(val_array, minfo.mi_validity, val_size) != 0))
ret = set_errno(EFAULT);
kmem_free(in_array, in_size);
kmem_free(out_array, out_size);
kmem_free(req_array, req_size);
kmem_free(val_array, val_size);
return (ret);
}
/*
* Initialize lgroup affinities for thread
*/
void
lgrp_affinity_init(lgrp_affinity_t **bufaddr)
{
if (bufaddr)
*bufaddr = NULL;
}
/*
* Free lgroup affinities for thread and set to NULL
* just in case thread gets recycled
*/
void
lgrp_affinity_free(lgrp_affinity_t **bufaddr)
{
if (bufaddr && *bufaddr) {
kmem_free(*bufaddr, nlgrpsmax * sizeof (lgrp_affinity_t));
*bufaddr = NULL;
}
}
#define P_ANY -2 /* cookie specifying any ID */
/*
* Find LWP with given ID in specified process and get its affinity for
* specified lgroup
*/
lgrp_affinity_t
lgrp_affinity_get_thread(proc_t *p, id_t lwpid, lgrp_id_t lgrp)
{
lgrp_affinity_t aff;
int found;
kthread_t *t;
ASSERT(MUTEX_HELD(&p->p_lock));
aff = LGRP_AFF_NONE;
found = 0;
t = p->p_tlist;
/*
* The process may be executing in proc_exit() and its p->p_list may be
* already NULL.
*/
if (t == NULL)
return (set_errno(ESRCH));
do {
if (t->t_tid == lwpid || lwpid == P_ANY) {
thread_lock(t);
/*
* Check to see whether caller has permission to set
* affinity for LWP
*/
if (t->t_cid == 0 || !hasprocperm(t->t_cred, CRED())) {
thread_unlock(t);
return (set_errno(EPERM));
}
if (t->t_lgrp_affinity)
aff = t->t_lgrp_affinity[lgrp];
thread_unlock(t);
found = 1;
break;
}
} while ((t = t->t_forw) != p->p_tlist);
if (!found)
aff = set_errno(ESRCH);
return (aff);
}
/*
* Get lgroup affinity for given LWP
*/
lgrp_affinity_t
lgrp_affinity_get(lgrp_affinity_args_t *ap)
{
lgrp_affinity_t aff;
lgrp_affinity_args_t args;
id_t id;
idtype_t idtype;
lgrp_id_t lgrp;
proc_t *p;
kthread_t *t;
/*
* Copyin arguments
*/
if (copyin(ap, &args, sizeof (lgrp_affinity_args_t)) != 0)
return (set_errno(EFAULT));
id = args.id;
idtype = args.idtype;
lgrp = args.lgrp;
/*
* Check for invalid lgroup
*/
if (lgrp < 0 || lgrp == LGRP_NONE)
return (set_errno(EINVAL));
/*
* Check for existing lgroup
*/
if (lgrp > lgrp_alloc_max)
return (set_errno(ESRCH));
/*
* Get lgroup affinity for given LWP or process
*/
switch (idtype) {
case P_LWPID:
/*
* LWP in current process
*/
p = curproc;
mutex_enter(&p->p_lock);
if (id != P_MYID) /* different thread */
aff = lgrp_affinity_get_thread(p, id, lgrp);
else { /* current thread */
aff = LGRP_AFF_NONE;
t = curthread;
thread_lock(t);
if (t->t_lgrp_affinity)
aff = t->t_lgrp_affinity[lgrp];
thread_unlock(t);
}
mutex_exit(&p->p_lock);
break;
case P_PID:
/*
* Process
*/
mutex_enter(&pidlock);
if (id == P_MYID)
p = curproc;
else {
p = prfind(id);
if (p == NULL) {
mutex_exit(&pidlock);
return (set_errno(ESRCH));
}
}
mutex_enter(&p->p_lock);
aff = lgrp_affinity_get_thread(p, P_ANY, lgrp);
mutex_exit(&p->p_lock);
mutex_exit(&pidlock);
break;
default:
aff = set_errno(EINVAL);
break;
}
return (aff);
}
/*
* Find lgroup for which this thread has most affinity in specified partition
* starting from home lgroup unless specified starting lgroup is preferred
*/
lpl_t *
lgrp_affinity_best(kthread_t *t, struct cpupart *cpupart, lgrp_id_t start,
boolean_t prefer_start)
{
lgrp_affinity_t *affs;
lgrp_affinity_t best_aff;
lpl_t *best_lpl;
lgrp_id_t finish;
lgrp_id_t home;
lgrp_id_t lgrpid;
lpl_t *lpl;
ASSERT(t != NULL);
ASSERT((MUTEX_HELD(&cpu_lock) || curthread->t_preempt > 0) ||
(MUTEX_HELD(&ttoproc(t)->p_lock) && THREAD_LOCK_HELD(t)));
ASSERT(cpupart != NULL);
if (t->t_lgrp_affinity == NULL)
return (NULL);
affs = t->t_lgrp_affinity;
/*
* Thread bound to CPU
*/
if (t->t_bind_cpu != PBIND_NONE) {
cpu_t *cp;
/*
* Find which lpl has most affinity among leaf lpl directly
* containing CPU and its ancestor lpls
*/
cp = cpu[t->t_bind_cpu];
best_lpl = lpl = cp->cpu_lpl;
best_aff = affs[best_lpl->lpl_lgrpid];
while (lpl->lpl_parent != NULL) {
lpl = lpl->lpl_parent;
lgrpid = lpl->lpl_lgrpid;
if (affs[lgrpid] > best_aff) {
best_lpl = lpl;
best_aff = affs[lgrpid];
}
}
return (best_lpl);
}
/*
* Start searching from home lgroup unless given starting lgroup is
* preferred or home lgroup isn't in given pset. Use root lgroup as
* starting point if both home and starting lgroups aren't in given
* pset.
*/
ASSERT(start >= 0 && start <= lgrp_alloc_max);
home = t->t_lpl->lpl_lgrpid;
if (!prefer_start && LGRP_CPUS_IN_PART(home, cpupart))
lgrpid = home;
else if (start != LGRP_NONE && LGRP_CPUS_IN_PART(start, cpupart))
lgrpid = start;
else
lgrpid = LGRP_ROOTID;
best_lpl = &cpupart->cp_lgrploads[lgrpid];
best_aff = affs[lgrpid];
finish = lgrpid;
do {
/*
* Skip any lgroups that don't have CPU resources
* in this processor set.
*/
if (!LGRP_CPUS_IN_PART(lgrpid, cpupart)) {
if (++lgrpid > lgrp_alloc_max)
lgrpid = 0; /* wrap the search */
continue;
}
/*
* Find lgroup with most affinity
*/
lpl = &cpupart->cp_lgrploads[lgrpid];
if (affs[lgrpid] > best_aff) {
best_aff = affs[lgrpid];
best_lpl = lpl;
}
if (++lgrpid > lgrp_alloc_max)
lgrpid = 0; /* wrap the search */
} while (lgrpid != finish);
/*
* No lgroup (in this pset) with any affinity
*/
if (best_aff == LGRP_AFF_NONE)
return (NULL);
lgrpid = best_lpl->lpl_lgrpid;
ASSERT(LGRP_CPUS_IN_PART(lgrpid, cpupart) && best_lpl->lpl_ncpu > 0);
return (best_lpl);
}
/*
* Set thread's affinity for given lgroup
*/
int
lgrp_affinity_set_thread(kthread_t *t, lgrp_id_t lgrp, lgrp_affinity_t aff,
lgrp_affinity_t **aff_buf)
{
lgrp_affinity_t *affs;
lgrp_id_t best;
lpl_t *best_lpl;
lgrp_id_t home;
int retval;
ASSERT(t != NULL);
ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
retval = 0;
thread_lock(t);
/*
* Check to see whether caller has permission to set affinity for
* thread
*/
if (t->t_cid == 0 || !hasprocperm(t->t_cred, CRED())) {
thread_unlock(t);
return (set_errno(EPERM));
}
if (t->t_lgrp_affinity == NULL) {
if (aff == LGRP_AFF_NONE) {
thread_unlock(t);
return (0);
}
ASSERT(aff_buf != NULL && *aff_buf != NULL);
t->t_lgrp_affinity = *aff_buf;
*aff_buf = NULL;
}
affs = t->t_lgrp_affinity;
affs[lgrp] = aff;
/*
* Find lgroup for which thread has most affinity,
* starting with lgroup for which affinity being set
*/
best_lpl = lgrp_affinity_best(t, t->t_cpupart, lgrp, B_TRUE);
/*
* Rehome if found lgroup with more affinity than home or lgroup for
* which affinity is being set has same affinity as home
*/
home = t->t_lpl->lpl_lgrpid;
if (best_lpl != NULL && best_lpl != t->t_lpl) {
best = best_lpl->lpl_lgrpid;
if (affs[best] > affs[home] || (affs[best] == affs[home] &&
best == lgrp))
lgrp_move_thread(t, best_lpl, 1);
}
thread_unlock(t);
return (retval);
}
/*
* Set process' affinity for specified lgroup
*/
int
lgrp_affinity_set_proc(proc_t *p, lgrp_id_t lgrp, lgrp_affinity_t aff,
lgrp_affinity_t **aff_buf_array)
{
lgrp_affinity_t *buf;
int err = 0;
int i;
int retval;
kthread_t *t;
ASSERT(MUTEX_HELD(&pidlock) && MUTEX_HELD(&p->p_lock));
ASSERT(aff_buf_array != NULL);
i = 0;
t = p->p_tlist;
if (t != NULL) {
do {
/*
* Set lgroup affinity for thread
*/
buf = aff_buf_array[i];
retval = lgrp_affinity_set_thread(t, lgrp, aff, &buf);
if (err == 0 && retval != 0)
err = retval;
/*
* Advance pointer to next buffer
*/
if (buf == NULL) {
ASSERT(i < p->p_lwpcnt);
aff_buf_array[i] = NULL;
i++;
}
} while ((t = t->t_forw) != p->p_tlist);
}
return (err);
}
/*
* Set LWP's or process' affinity for specified lgroup
*
* When setting affinities, pidlock, process p_lock, and thread_lock()
* need to be held in that order to protect target thread's pset, process,
* process contents, and thread contents. thread_lock() does splhigh(),
* so it ends up having similiar effect as kpreempt_disable(), so it will
* protect calls to lgrp_move_thread() and lgrp_choose() from pset changes.
*/
int
lgrp_affinity_set(lgrp_affinity_args_t *ap)
{
lgrp_affinity_t aff;
lgrp_affinity_t *aff_buf;
lgrp_affinity_args_t args;
id_t id;
idtype_t idtype;
lgrp_id_t lgrp;
int nthreads;
proc_t *p;
int retval;
/*
* Copyin arguments
*/
if (copyin(ap, &args, sizeof (lgrp_affinity_args_t)) != 0)
return (set_errno(EFAULT));
idtype = args.idtype;
id = args.id;
lgrp = args.lgrp;
aff = args.aff;
/*
* Check for invalid lgroup
*/
if (lgrp < 0 || lgrp == LGRP_NONE)
return (set_errno(EINVAL));
/*
* Check for existing lgroup
*/
if (lgrp > lgrp_alloc_max)
return (set_errno(ESRCH));
/*
* Check for legal affinity
*/
if (aff != LGRP_AFF_NONE && aff != LGRP_AFF_WEAK &&
aff != LGRP_AFF_STRONG)
return (set_errno(EINVAL));
/*
* Must be process or LWP ID
*/
if (idtype != P_LWPID && idtype != P_PID)
return (set_errno(EINVAL));
retval = EINVAL;
/*
* Set given LWP's or process' affinity for specified lgroup
*/
switch (idtype) {
case P_LWPID:
/*
* Allocate memory for thread's lgroup affinities
* ahead of time w/o holding locks
*/
aff_buf = kmem_zalloc(nlgrpsmax * sizeof (lgrp_affinity_t),
KM_SLEEP);
p = curproc;
/*
* Set affinity for thread
*/
mutex_enter(&p->p_lock);
if (id == P_MYID) { /* current thread */
retval = lgrp_affinity_set_thread(curthread, lgrp, aff,
&aff_buf);
} else if (p->p_tlist == NULL) {
retval = set_errno(ESRCH);
} else { /* other thread */
int found = 0;
kthread_t *t;
t = p->p_tlist;
do {
if (t->t_tid == id) {
retval = lgrp_affinity_set_thread(t,
lgrp, aff, &aff_buf);
found = 1;
break;
}
} while ((t = t->t_forw) != p->p_tlist);
if (!found)
retval = set_errno(ESRCH);
}
mutex_exit(&p->p_lock);
/*
* Free memory for lgroup affinities,
* since thread didn't need it
*/
if (aff_buf)
kmem_free(aff_buf,
nlgrpsmax * sizeof (lgrp_affinity_t));
break;
case P_PID:
do {
lgrp_affinity_t **aff_buf_array;
int i;
size_t size;
/*
* Get process
*/
mutex_enter(&pidlock);
if (id == P_MYID)
p = curproc;
else
p = prfind(id);
if (p == NULL) {
mutex_exit(&pidlock);
return (set_errno(ESRCH));
}
/*
* Get number of threads in process
*
* NOTE: Only care about user processes,
* so p_lwpcnt should be number of threads.
*/
mutex_enter(&p->p_lock);
nthreads = p->p_lwpcnt;
mutex_exit(&p->p_lock);
mutex_exit(&pidlock);
if (nthreads < 1)
return (set_errno(ESRCH));
/*
* Preallocate memory for lgroup affinities for
* each thread in process now to avoid holding
* any locks. Allocate an array to hold a buffer
* for each thread.
*/
aff_buf_array = kmem_zalloc(nthreads *
sizeof (lgrp_affinity_t *), KM_SLEEP);
size = nlgrpsmax * sizeof (lgrp_affinity_t);
for (i = 0; i < nthreads; i++)
aff_buf_array[i] = kmem_zalloc(size, KM_SLEEP);
mutex_enter(&pidlock);
/*
* Get process again since dropped locks to allocate
* memory (except current process)
*/
if (id != P_MYID)
p = prfind(id);
/*
* Process went away after we dropped locks and before
* reacquiring them, so drop locks, free memory, and
* return.
*/
if (p == NULL) {
mutex_exit(&pidlock);
for (i = 0; i < nthreads; i++)
kmem_free(aff_buf_array[i], size);
kmem_free(aff_buf_array,
nthreads * sizeof (lgrp_affinity_t *));
return (set_errno(ESRCH));
}
mutex_enter(&p->p_lock);
/*
* See whether number of threads is same
* If not, drop locks, free memory, and try again
*/
if (nthreads != p->p_lwpcnt) {
mutex_exit(&p->p_lock);
mutex_exit(&pidlock);
for (i = 0; i < nthreads; i++)
kmem_free(aff_buf_array[i], size);
kmem_free(aff_buf_array,
nthreads * sizeof (lgrp_affinity_t *));
continue;
}
/*
* Set lgroup affinity for threads in process
*/
retval = lgrp_affinity_set_proc(p, lgrp, aff,
aff_buf_array);
mutex_exit(&p->p_lock);
mutex_exit(&pidlock);
/*
* Free any leftover memory, since some threads may
* have already allocated memory and set lgroup
* affinities before
*/
for (i = 0; i < nthreads; i++)
if (aff_buf_array[i] != NULL)
kmem_free(aff_buf_array[i], size);
kmem_free(aff_buf_array,
nthreads * sizeof (lgrp_affinity_t *));
break;
} while (nthreads != p->p_lwpcnt);
break;
default:
retval = set_errno(EINVAL);
break;
}
return (retval);
}
/*
* Return the latest generation number for the lgroup hierarchy
* with the given view
*/
lgrp_gen_t
lgrp_generation(lgrp_view_t view)
{
cpupart_t *cpupart;
uint_t gen;
kpreempt_disable();
/*
* Determine generation number for given view
*/
if (view == LGRP_VIEW_OS)
/*
* Return generation number of lgroup hierarchy for OS view
*/
gen = lgrp_gen;
else {
/*
* For caller's view, use generation numbers for lgroup
* hierarchy and caller's pset
* NOTE: Caller needs to check for change in pset ID
*/
cpupart = curthread->t_cpupart;
ASSERT(cpupart);
gen = lgrp_gen + cpupart->cp_gen;
}
kpreempt_enable();
return (gen);
}
lgrp_id_t
lgrp_home_thread(kthread_t *t)
{
lgrp_id_t home;
ASSERT(t != NULL);
ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
thread_lock(t);
/*
* Check to see whether caller has permission to set affinity for
* thread
*/
if (t->t_cid == 0 || !hasprocperm(t->t_cred, CRED())) {
thread_unlock(t);
return (set_errno(EPERM));
}
home = lgrp_home_id(t);
thread_unlock(t);
return (home);
}
/*
* Get home lgroup of given process or thread
*/
lgrp_id_t
lgrp_home_get(idtype_t idtype, id_t id)
{
proc_t *p;
lgrp_id_t retval;
kthread_t *t;
/*
* Get home lgroup of given LWP or process
*/
switch (idtype) {
case P_LWPID:
p = curproc;
/*
* Set affinity for thread
*/
mutex_enter(&p->p_lock);
if (id == P_MYID) { /* current thread */
retval = lgrp_home_thread(curthread);
} else if (p->p_tlist == NULL) {
retval = set_errno(ESRCH);
} else { /* other thread */
int found = 0;
t = p->p_tlist;
do {
if (t->t_tid == id) {
retval = lgrp_home_thread(t);
found = 1;
break;
}
} while ((t = t->t_forw) != p->p_tlist);
if (!found)
retval = set_errno(ESRCH);
}
mutex_exit(&p->p_lock);
break;
case P_PID:
/*
* Get process
*/
mutex_enter(&pidlock);
if (id == P_MYID)
p = curproc;
else
p = prfind(id);
if (p == NULL) {
mutex_exit(&pidlock);
return (set_errno(ESRCH));
}
mutex_enter(&p->p_lock);
t = p->p_tlist;
if (t == NULL)
retval = set_errno(ESRCH);
else
retval = lgrp_home_thread(t);
mutex_exit(&p->p_lock);
mutex_exit(&pidlock);
break;
default:
retval = set_errno(EINVAL);
break;
}
return (retval);
}
/*
* Return latency between "from" and "to" lgroups
*
* This latency number can only be used for relative comparison
* between lgroups on the running system, cannot be used across platforms,
* and may not reflect the actual latency. It is platform and implementation
* specific, so platform gets to decide its value. It would be nice if the
* number was at least proportional to make comparisons more meaningful though.
*/
int
lgrp_latency(lgrp_id_t from, lgrp_id_t to)
{
lgrp_t *from_lgrp;
int i;
int latency;
int latency_max;
lgrp_t *to_lgrp;
ASSERT(MUTEX_HELD(&cpu_lock));
if (from < 0 || to < 0)
return (set_errno(EINVAL));
if (from > lgrp_alloc_max || to > lgrp_alloc_max)
return (set_errno(ESRCH));
from_lgrp = lgrp_table[from];
to_lgrp = lgrp_table[to];
if (!LGRP_EXISTS(from_lgrp) || !LGRP_EXISTS(to_lgrp)) {
return (set_errno(ESRCH));
}
/*
* Get latency for same lgroup
*/
if (from == to) {
latency = from_lgrp->lgrp_latency;
return (latency);
}
/*
* Get latency between leaf lgroups
*/
if (from_lgrp->lgrp_childcnt == 0 && to_lgrp->lgrp_childcnt == 0)
return (lgrp_plat_latency(from_lgrp->lgrp_plathand,
to_lgrp->lgrp_plathand));
/*
* Determine max latency between resources in two lgroups
*/
latency_max = 0;
for (i = 0; i <= lgrp_alloc_max; i++) {
lgrp_t *from_rsrc;
int j;
lgrp_t *to_rsrc;
from_rsrc = lgrp_table[i];
if (!LGRP_EXISTS(from_rsrc) ||
!klgrpset_ismember(from_lgrp->lgrp_set[LGRP_RSRC_CPU], i))
continue;
for (j = 0; j <= lgrp_alloc_max; j++) {
to_rsrc = lgrp_table[j];
if (!LGRP_EXISTS(to_rsrc) ||
klgrpset_ismember(to_lgrp->lgrp_set[LGRP_RSRC_MEM],
j) == 0)
continue;
latency = lgrp_plat_latency(from_rsrc->lgrp_plathand,
to_rsrc->lgrp_plathand);
if (latency > latency_max)
latency_max = latency;
}
}
return (latency_max);
}
/*
* Return lgroup interface version number
* 0 - none
* 1 - original
* 2 - lgrp_latency_cookie() and lgrp_resources() added
*/
int
lgrp_version(int version)
{
/*
* Return LGRP_VER_NONE when requested version isn't supported
*/
if (version < LGRP_VER_NONE || version > LGRP_VER_CURRENT)
return (LGRP_VER_NONE);
/*
* Return current version when LGRP_VER_NONE passed in
*/
if (version == LGRP_VER_NONE)
return (LGRP_VER_CURRENT);
/*
* Otherwise, return supported version.
*/
return (version);
}
/*
* Snapshot of lgroup hieararchy
*
* One snapshot is kept and is based on the kernel's native data model, so
* a 32-bit snapshot is kept for the 32-bit kernel and a 64-bit one for the
* 64-bit kernel. If a 32-bit user wants a snapshot from the 64-bit kernel,
* the kernel generates a 32-bit snapshot from the data in its 64-bit snapshot.
*
* The format is defined by lgroup snapshot header and the layout of
* the snapshot in memory is as follows:
* 1) lgroup snapshot header
* - specifies format of snapshot
* - defined by lgrp_snapshot_header_t
* 2) lgroup info array
* - contains information about each lgroup
* - one element for each lgroup
* - each element is defined by lgrp_info_t
* 3) lgroup CPU ID array
* - contains list (array) of CPU IDs for each lgroup
* - lgrp_info_t points into array and specifies how many CPUs belong to
* given lgroup
* 4) lgroup parents array
* - contains lgroup bitmask of parents for each lgroup
* - bitmask is an array of unsigned longs and its size depends on nlgrpsmax
* 5) lgroup children array
* - contains lgroup bitmask of children for each lgroup
* - bitmask is an array of unsigned longs and its size depends on nlgrpsmax
* 6) lgroup resources array
* - contains lgroup bitmask of resources for each lgroup
* - bitmask is an array of unsigned longs and its size depends on nlgrpsmax
* 7) lgroup latency table
* - contains latency from each lgroup to each of other lgroups
*
* NOTE: Must use nlgrpsmax for per lgroup data structures because lgroups
* may be sparsely allocated.
*/
lgrp_snapshot_header_t *lgrp_snap = NULL; /* lgroup snapshot */
static kmutex_t lgrp_snap_lock; /* snapshot lock */
/*
* Take a snapshot of lgroup hierarchy and return size of buffer
* needed to hold snapshot
*/
static int
lgrp_snapshot(void)
{
size_t bitmask_size;
size_t bitmasks_size;
size_t bufsize;
int cpu_index;
size_t cpuids_size;
int i;
int j;
size_t info_size;
size_t lats_size;
ulong_t *lgrp_children;
processorid_t *lgrp_cpuids;
lgrp_info_t *lgrp_info;
int **lgrp_lats;
ulong_t *lgrp_parents;
ulong_t *lgrp_rsets;
ulong_t *lgrpset;
int snap_ncpus;
int snap_nlgrps;
int snap_nlgrpsmax;
size_t snap_hdr_size;
#ifdef _SYSCALL32_IMPL
model_t model = DATAMODEL_NATIVE;
/*
* Have up-to-date snapshot, so check to see whether caller is 32-bit
* program and need to return size of 32-bit snapshot now.
*/
model = get_udatamodel();
if (model == DATAMODEL_ILP32 && lgrp_snap &&
lgrp_snap->ss_gen == lgrp_gen) {
snap_nlgrpsmax = lgrp_snap->ss_nlgrps_max;
/*
* Calculate size of buffer needed for 32-bit snapshot,
* rounding up size of each object to allow for alignment
* of next object in buffer.
*/
snap_hdr_size = P2ROUNDUP(sizeof (lgrp_snapshot_header32_t),
sizeof (caddr32_t));
info_size =
P2ROUNDUP(snap_nlgrpsmax * sizeof (lgrp_info32_t),
sizeof (processorid_t));
cpuids_size =
P2ROUNDUP(lgrp_snap->ss_ncpus * sizeof (processorid_t),
sizeof (ulong_t));
/*
* lgroup bitmasks needed for parents, children, and resources
* for each lgroup and pset lgroup set
*/
bitmask_size = BT_SIZEOFMAP(snap_nlgrpsmax);
bitmasks_size = (((2 + LGRP_RSRC_COUNT) *
snap_nlgrpsmax) + 1) * bitmask_size;
/*
* Size of latency table and buffer
*/
lats_size = snap_nlgrpsmax * sizeof (caddr32_t) +
snap_nlgrpsmax * snap_nlgrpsmax * sizeof (int);
bufsize = snap_hdr_size + info_size + cpuids_size +
bitmasks_size + lats_size;
return (bufsize);
}
#endif /* _SYSCALL32_IMPL */
/*
* Check whether snapshot is up-to-date
* Free it and take another one if not
*/
if (lgrp_snap) {
if (lgrp_snap->ss_gen == lgrp_gen)
return (lgrp_snap->ss_size);
kmem_free(lgrp_snap, lgrp_snap->ss_size);
lgrp_snap = NULL;
}
/*
* Allocate memory for snapshot
* w/o holding cpu_lock while waiting for memory
*/
while (lgrp_snap == NULL) {
int old_generation;
/*
* Take snapshot of lgroup generation number
* and configuration size dependent information
* NOTE: Only count number of online CPUs,
* since only online CPUs appear in lgroups.
*/
mutex_enter(&cpu_lock);
old_generation = lgrp_gen;
snap_ncpus = ncpus_online;
snap_nlgrps = nlgrps;
snap_nlgrpsmax = nlgrpsmax;
mutex_exit(&cpu_lock);
/*
* Calculate size of buffer needed for snapshot,
* rounding up size of each object to allow for alignment
* of next object in buffer.
*/
snap_hdr_size = P2ROUNDUP(sizeof (lgrp_snapshot_header_t),
sizeof (void *));
info_size = P2ROUNDUP(snap_nlgrpsmax * sizeof (lgrp_info_t),
sizeof (processorid_t));
cpuids_size = P2ROUNDUP(snap_ncpus * sizeof (processorid_t),
sizeof (ulong_t));
/*
* lgroup bitmasks needed for pset lgroup set and parents,
* children, and resource sets for each lgroup
*/
bitmask_size = BT_SIZEOFMAP(snap_nlgrpsmax);
bitmasks_size = (((2 + LGRP_RSRC_COUNT) *
snap_nlgrpsmax) + 1) * bitmask_size;
/*
* Size of latency table and buffer
*/
lats_size = snap_nlgrpsmax * sizeof (int *) +
snap_nlgrpsmax * snap_nlgrpsmax * sizeof (int);
bufsize = snap_hdr_size + info_size + cpuids_size +
bitmasks_size + lats_size;
/*
* Allocate memory for buffer
*/
lgrp_snap = kmem_zalloc(bufsize, KM_NOSLEEP);
if (lgrp_snap == NULL)
return (set_errno(ENOMEM));
/*
* Check whether generation number has changed
*/
mutex_enter(&cpu_lock);
if (lgrp_gen == old_generation)
break; /* hasn't change, so done. */
/*
* Generation number changed, so free memory and try again.
*/
mutex_exit(&cpu_lock);
kmem_free(lgrp_snap, bufsize);
lgrp_snap = NULL;
}
/*
* Fill in lgroup snapshot header
* (including pointers to tables of lgroup info, CPU IDs, and parents
* and children)
*/
lgrp_snap->ss_version = LGRP_VER_CURRENT;
/*
* XXX For now, liblgrp only needs to know whether the hierarchy
* XXX only has one level or not
*/
if (snap_nlgrps == 1)
lgrp_snap->ss_levels = 1;
else
lgrp_snap->ss_levels = 2;
lgrp_snap->ss_root = LGRP_ROOTID;
lgrp_snap->ss_nlgrps = lgrp_snap->ss_nlgrps_os = snap_nlgrps;
lgrp_snap->ss_nlgrps_max = snap_nlgrpsmax;
lgrp_snap->ss_ncpus = snap_ncpus;
lgrp_snap->ss_gen = lgrp_gen;
lgrp_snap->ss_view = LGRP_VIEW_OS;
lgrp_snap->ss_pset = 0; /* NOTE: caller should set if needed */
lgrp_snap->ss_size = bufsize;
lgrp_snap->ss_magic = (uintptr_t)lgrp_snap;
lgrp_snap->ss_info = lgrp_info =
(lgrp_info_t *)((uintptr_t)lgrp_snap + snap_hdr_size);
lgrp_snap->ss_cpuids = lgrp_cpuids =
(processorid_t *)((uintptr_t)lgrp_info + info_size);
lgrp_snap->ss_lgrpset = lgrpset =
(ulong_t *)((uintptr_t)lgrp_cpuids + cpuids_size);
lgrp_snap->ss_parents = lgrp_parents =
(ulong_t *)((uintptr_t)lgrpset + bitmask_size);
lgrp_snap->ss_children = lgrp_children =
(ulong_t *)((uintptr_t)lgrp_parents + (snap_nlgrpsmax *
bitmask_size));
lgrp_snap->ss_rsets = lgrp_rsets =
(ulong_t *)((uintptr_t)lgrp_children + (snap_nlgrpsmax *
bitmask_size));
lgrp_snap->ss_latencies = lgrp_lats =
(int **)((uintptr_t)lgrp_rsets + (LGRP_RSRC_COUNT *
snap_nlgrpsmax * bitmask_size));
/*
* Fill in lgroup information
*/
cpu_index = 0;
for (i = 0; i < snap_nlgrpsmax; i++) {
struct cpu *cp;
int cpu_count;
struct cpu *head;
int k;
lgrp_t *lgrp;
lgrp = lgrp_table[i];
if (!LGRP_EXISTS(lgrp)) {
bzero(&lgrp_info[i], sizeof (lgrp_info[i]));
lgrp_info[i].info_lgrpid = LGRP_NONE;
continue;
}
lgrp_info[i].info_lgrpid = i;
lgrp_info[i].info_latency = lgrp->lgrp_latency;
/*
* Fill in parents, children, and lgroup resources
*/
lgrp_info[i].info_parents =
(ulong_t *)((uintptr_t)lgrp_parents + (i * bitmask_size));
if (lgrp->lgrp_parent)
BT_SET(lgrp_info[i].info_parents,
lgrp->lgrp_parent->lgrp_id);
lgrp_info[i].info_children =
(ulong_t *)((uintptr_t)lgrp_children + (i * bitmask_size));
for (j = 0; j < snap_nlgrpsmax; j++)
if (klgrpset_ismember(lgrp->lgrp_children, j))
BT_SET(lgrp_info[i].info_children, j);
lgrp_info[i].info_rset =
(ulong_t *)((uintptr_t)lgrp_rsets +
(i * LGRP_RSRC_COUNT * bitmask_size));
for (j = 0; j < LGRP_RSRC_COUNT; j++) {
ulong_t *rset;
rset = (ulong_t *)((uintptr_t)lgrp_info[i].info_rset +
(j * bitmask_size));
for (k = 0; k < snap_nlgrpsmax; k++)
if (klgrpset_ismember(lgrp->lgrp_set[j], k))
BT_SET(rset, k);
}
/*
* Fill in CPU IDs
*/
cpu_count = 0;
lgrp_info[i].info_cpuids = NULL;
cp = head = lgrp->lgrp_cpu;
if (head != NULL) {
lgrp_info[i].info_cpuids = &lgrp_cpuids[cpu_index];
do {
lgrp_cpuids[cpu_index] = cp->cpu_id;
cpu_index++;
cpu_count++;
cp = cp->cpu_next_lgrp;
} while (cp != head);
}
ASSERT(cpu_count == lgrp->lgrp_cpucnt);
lgrp_info[i].info_ncpus = cpu_count;
/*
* Fill in memory sizes for lgroups that directly contain
* memory
*/
if (klgrpset_ismember(lgrp->lgrp_set[LGRP_RSRC_MEM], i)) {
lgrp_info[i].info_mem_free =
lgrp_mem_size(i, LGRP_MEM_SIZE_FREE);
lgrp_info[i].info_mem_install =
lgrp_mem_size(i, LGRP_MEM_SIZE_INSTALL);
}
/*
* Fill in latency table and buffer
*/
lgrp_lats[i] = (int *)((uintptr_t)lgrp_lats + snap_nlgrpsmax *
sizeof (int *) + i * snap_nlgrpsmax * sizeof (int));
for (j = 0; j < snap_nlgrpsmax; j++) {
lgrp_t *to;
to = lgrp_table[j];
if (!LGRP_EXISTS(to))
continue;
lgrp_lats[i][j] = lgrp_latency(lgrp->lgrp_id,
to->lgrp_id);
}
}
ASSERT(cpu_index == snap_ncpus);
mutex_exit(&cpu_lock);
#ifdef _SYSCALL32_IMPL
/*
* Check to see whether caller is 32-bit program and need to return
* size of 32-bit snapshot now that snapshot has been taken/updated.
* May not have been able to do this earlier if snapshot was out of
* date or didn't exist yet.
*/
if (model == DATAMODEL_ILP32) {
snap_nlgrpsmax = lgrp_snap->ss_nlgrps_max;
/*
* Calculate size of buffer needed for 32-bit snapshot,
* rounding up size of each object to allow for alignment
* of next object in buffer.
*/
snap_hdr_size = P2ROUNDUP(sizeof (lgrp_snapshot_header32_t),
sizeof (caddr32_t));
info_size =
P2ROUNDUP(snap_nlgrpsmax * sizeof (lgrp_info32_t),
sizeof (processorid_t));
cpuids_size =
P2ROUNDUP(lgrp_snap->ss_ncpus * sizeof (processorid_t),
sizeof (ulong_t));
bitmask_size = BT_SIZEOFMAP(snap_nlgrpsmax);
bitmasks_size = (((2 + LGRP_RSRC_COUNT) * snap_nlgrpsmax) +
1) * bitmask_size;
/*
* Size of latency table and buffer
*/
lats_size = (snap_nlgrpsmax * sizeof (caddr32_t)) +
(snap_nlgrpsmax * snap_nlgrpsmax * sizeof (int));
bufsize = snap_hdr_size + info_size + cpuids_size +
bitmasks_size + lats_size;
return (bufsize);
}
#endif /* _SYSCALL32_IMPL */
return (lgrp_snap->ss_size);
}
/*
* Copy snapshot into given user buffer, fix up any pointers in buffer to point
* into user instead of kernel address space, and return size of buffer
* needed to hold snapshot
*/
static int
lgrp_snapshot_copy(char *buf, size_t bufsize)
{
size_t bitmask_size;
int cpu_index;
size_t cpuids_size;
int i;
size_t info_size;
lgrp_info_t *lgrp_info;
int retval;
size_t snap_hdr_size;
int snap_ncpus;
int snap_nlgrpsmax;
lgrp_snapshot_header_t *user_snap;
lgrp_info_t *user_info;
lgrp_info_t *user_info_buffer;
processorid_t *user_cpuids;
ulong_t *user_lgrpset;
ulong_t *user_parents;
ulong_t *user_children;
int **user_lats;
int **user_lats_buffer;
ulong_t *user_rsets;
if (lgrp_snap == NULL)
return (0);
if (buf == NULL || bufsize <= 0)
return (lgrp_snap->ss_size);
/*
* User needs to try getting size of buffer again
* because given buffer size is too small.
* The lgroup hierarchy may have changed after they asked for the size
* but before the snapshot was taken.
*/
if (bufsize < lgrp_snap->ss_size)
return (set_errno(EAGAIN));
snap_ncpus = lgrp_snap->ss_ncpus;
snap_nlgrpsmax = lgrp_snap->ss_nlgrps_max;
/*
* Fill in lgrpset now because caller may have change psets
*/
kpreempt_disable();
for (i = 0; i < snap_nlgrpsmax; i++) {
if (klgrpset_ismember(curthread->t_cpupart->cp_lgrpset,
i)) {
BT_SET(lgrp_snap->ss_lgrpset, i);
}
}
kpreempt_enable();
/*
* Copy lgroup snapshot (snapshot header, lgroup info, and CPU IDs)
* into user buffer all at once
*/
if (copyout(lgrp_snap, buf, lgrp_snap->ss_size) != 0)
return (set_errno(EFAULT));
/*
* Round up sizes of lgroup snapshot header and info for alignment
*/
snap_hdr_size = P2ROUNDUP(sizeof (lgrp_snapshot_header_t),
sizeof (void *));
info_size = P2ROUNDUP(snap_nlgrpsmax * sizeof (lgrp_info_t),
sizeof (processorid_t));
cpuids_size = P2ROUNDUP(snap_ncpus * sizeof (processorid_t),
sizeof (ulong_t));
bitmask_size = BT_SIZEOFMAP(snap_nlgrpsmax);
/*
* Calculate pointers into user buffer for lgroup snapshot header,
* info, and CPU IDs
*/
user_snap = (lgrp_snapshot_header_t *)buf;
user_info = (lgrp_info_t *)((uintptr_t)user_snap + snap_hdr_size);
user_cpuids = (processorid_t *)((uintptr_t)user_info + info_size);
user_lgrpset = (ulong_t *)((uintptr_t)user_cpuids + cpuids_size);
user_parents = (ulong_t *)((uintptr_t)user_lgrpset + bitmask_size);
user_children = (ulong_t *)((uintptr_t)user_parents +
(snap_nlgrpsmax * bitmask_size));
user_rsets = (ulong_t *)((uintptr_t)user_children +
(snap_nlgrpsmax * bitmask_size));
user_lats = (int **)((uintptr_t)user_rsets +
(LGRP_RSRC_COUNT * snap_nlgrpsmax * bitmask_size));
/*
* Copyout magic number (ie. pointer to beginning of buffer)
*/
if (copyout(&buf, &user_snap->ss_magic, sizeof (buf)) != 0)
return (set_errno(EFAULT));
/*
* Fix up pointers in user buffer to point into user buffer
* not kernel snapshot
*/
if (copyout(&user_info, &user_snap->ss_info, sizeof (user_info)) != 0)
return (set_errno(EFAULT));
if (copyout(&user_cpuids, &user_snap->ss_cpuids,
sizeof (user_cpuids)) != 0)
return (set_errno(EFAULT));
if (copyout(&user_lgrpset, &user_snap->ss_lgrpset,
sizeof (user_lgrpset)) != 0)
return (set_errno(EFAULT));
if (copyout(&user_parents, &user_snap->ss_parents,
sizeof (user_parents)) != 0)
return (set_errno(EFAULT));
if (copyout(&user_children, &user_snap->ss_children,
sizeof (user_children)) != 0)
return (set_errno(EFAULT));
if (copyout(&user_rsets, &user_snap->ss_rsets,
sizeof (user_rsets)) != 0)
return (set_errno(EFAULT));
if (copyout(&user_lats, &user_snap->ss_latencies,
sizeof (user_lats)) != 0)
return (set_errno(EFAULT));
/*
* Make copies of lgroup info and latency table, fix up pointers,
* and then copy them into user buffer
*/
user_info_buffer = kmem_zalloc(info_size, KM_NOSLEEP);
if (user_info_buffer == NULL)
return (set_errno(ENOMEM));
user_lats_buffer = kmem_zalloc(snap_nlgrpsmax * sizeof (int *),
KM_NOSLEEP);
if (user_lats_buffer == NULL) {
kmem_free(user_info_buffer, info_size);
return (set_errno(ENOMEM));
}
lgrp_info = (lgrp_info_t *)((uintptr_t)lgrp_snap + snap_hdr_size);
bcopy(lgrp_info, user_info_buffer, info_size);
cpu_index = 0;
for (i = 0; i < snap_nlgrpsmax; i++) {
ulong_t *snap_rset;
/*
* Skip non-existent lgroups
*/
if (user_info_buffer[i].info_lgrpid == LGRP_NONE)
continue;
/*
* Update free memory size since it changes frequently
* Only do so for lgroups directly containing memory
*
* NOTE: This must be done before changing the pointers to
* point into user space since we need to dereference
* lgroup resource set
*/
snap_rset = &lgrp_info[i].info_rset[LGRP_RSRC_MEM *
BT_BITOUL(snap_nlgrpsmax)];
if (BT_TEST(snap_rset, i))
user_info_buffer[i].info_mem_free =
lgrp_mem_size(i, LGRP_MEM_SIZE_FREE);
/*
* Fix up pointers to parents, children, resources, and
* latencies
*/
user_info_buffer[i].info_parents =
(ulong_t *)((uintptr_t)user_parents + (i * bitmask_size));
user_info_buffer[i].info_children =
(ulong_t *)((uintptr_t)user_children + (i * bitmask_size));
user_info_buffer[i].info_rset =
(ulong_t *)((uintptr_t)user_rsets +
(i * LGRP_RSRC_COUNT * bitmask_size));
user_lats_buffer[i] = (int *)((uintptr_t)user_lats +
(snap_nlgrpsmax * sizeof (int *)) + (i * snap_nlgrpsmax *
sizeof (int)));
/*
* Fix up pointer to CPU IDs
*/
if (user_info_buffer[i].info_ncpus == 0) {
user_info_buffer[i].info_cpuids = NULL;
continue;
}
user_info_buffer[i].info_cpuids = &user_cpuids[cpu_index];
cpu_index += user_info_buffer[i].info_ncpus;
}
ASSERT(cpu_index == snap_ncpus);
/*
* Copy lgroup info and latency table with pointers fixed up to point
* into user buffer out to user buffer now
*/
retval = lgrp_snap->ss_size;
if (copyout(user_info_buffer, user_info, info_size) != 0)
retval = set_errno(EFAULT);
kmem_free(user_info_buffer, info_size);
if (copyout(user_lats_buffer, user_lats, snap_nlgrpsmax *
sizeof (int *)) != 0)
retval = set_errno(EFAULT);
kmem_free(user_lats_buffer, snap_nlgrpsmax * sizeof (int *));
return (retval);
}
#ifdef _SYSCALL32_IMPL
/*
* Make 32-bit copy of snapshot, fix up any pointers in buffer to point
* into user instead of kernel address space, copy 32-bit snapshot into
* given user buffer, and return size of buffer needed to hold snapshot
*/
static int
lgrp_snapshot_copy32(caddr32_t buf, size32_t bufsize)
{
size32_t bitmask_size;
size32_t bitmasks_size;
size32_t children_size;
int cpu_index;
size32_t cpuids_size;
int i;
int j;
size32_t info_size;
size32_t lats_size;
lgrp_info_t *lgrp_info;
lgrp_snapshot_header32_t *lgrp_snap32;
lgrp_info32_t *lgrp_info32;
processorid_t *lgrp_cpuids32;
caddr32_t *lgrp_lats32;
int **lgrp_lats32_kernel;
uint_t *lgrp_set32;
uint_t *lgrp_parents32;
uint_t *lgrp_children32;
uint_t *lgrp_rsets32;
size32_t parents_size;
size32_t rsets_size;
size32_t set_size;
size32_t snap_hdr_size;
int snap_ncpus;
int snap_nlgrpsmax;
size32_t snap_size;
if (lgrp_snap == NULL)
return (0);
snap_ncpus = lgrp_snap->ss_ncpus;
snap_nlgrpsmax = lgrp_snap->ss_nlgrps_max;
/*
* Calculate size of buffer needed for 32-bit snapshot,
* rounding up size of each object to allow for alignment
* of next object in buffer.
*/
snap_hdr_size = P2ROUNDUP(sizeof (lgrp_snapshot_header32_t),
sizeof (caddr32_t));
info_size = P2ROUNDUP(snap_nlgrpsmax * sizeof (lgrp_info32_t),
sizeof (processorid_t));
cpuids_size = P2ROUNDUP(snap_ncpus * sizeof (processorid_t),
sizeof (ulong_t));
bitmask_size = BT_SIZEOFMAP32(snap_nlgrpsmax);
set_size = bitmask_size;
parents_size = snap_nlgrpsmax * bitmask_size;
children_size = snap_nlgrpsmax * bitmask_size;
rsets_size = P2ROUNDUP(LGRP_RSRC_COUNT * snap_nlgrpsmax *
(int)bitmask_size, sizeof (caddr32_t));
bitmasks_size = set_size + parents_size + children_size + rsets_size;
/*
* Size of latency table and buffer
*/
lats_size = (snap_nlgrpsmax * sizeof (caddr32_t)) +
(snap_nlgrpsmax * snap_nlgrpsmax * sizeof (int));
snap_size = snap_hdr_size + info_size + cpuids_size + bitmasks_size +
lats_size;
if (buf == 0 || bufsize <= 0) {
return (snap_size);
}
/*
* User needs to try getting size of buffer again
* because given buffer size is too small.
* The lgroup hierarchy may have changed after they asked for the size
* but before the snapshot was taken.
*/
if (bufsize < snap_size)
return (set_errno(EAGAIN));
/*
* Make 32-bit copy of snapshot, fix up pointers to point into user
* buffer not kernel, and then copy whole thing into user buffer
*/
lgrp_snap32 = kmem_zalloc(snap_size, KM_NOSLEEP);
if (lgrp_snap32 == NULL)
return (set_errno(ENOMEM));
/*
* Calculate pointers into 32-bit copy of snapshot
* for lgroup info, CPU IDs, pset lgroup bitmask, parents, children,
* resources, and latency table and buffer
*/
lgrp_info32 = (lgrp_info32_t *)((uintptr_t)lgrp_snap32 +
snap_hdr_size);
lgrp_cpuids32 = (processorid_t *)((uintptr_t)lgrp_info32 + info_size);
lgrp_set32 = (uint_t *)((uintptr_t)lgrp_cpuids32 + cpuids_size);
lgrp_parents32 = (uint_t *)((uintptr_t)lgrp_set32 + set_size);
lgrp_children32 = (uint_t *)((uintptr_t)lgrp_parents32 + parents_size);
lgrp_rsets32 = (uint_t *)((uintptr_t)lgrp_children32 + children_size);
lgrp_lats32 = (caddr32_t *)((uintptr_t)lgrp_rsets32 + rsets_size);
/*
* Make temporary lgroup latency table of pointers for kernel to use
* to fill in rows of table with latencies from each lgroup
*/
lgrp_lats32_kernel = kmem_zalloc(snap_nlgrpsmax * sizeof (int *),
KM_NOSLEEP);
if (lgrp_lats32_kernel == NULL) {
kmem_free(lgrp_snap32, snap_size);
return (set_errno(ENOMEM));
}
/*
* Fill in 32-bit lgroup snapshot header
* (with pointers into user's buffer for lgroup info, CPU IDs,
* bit masks, and latencies)
*/
lgrp_snap32->ss_version = lgrp_snap->ss_version;
lgrp_snap32->ss_levels = lgrp_snap->ss_levels;
lgrp_snap32->ss_nlgrps = lgrp_snap32->ss_nlgrps_os =
lgrp_snap->ss_nlgrps;
lgrp_snap32->ss_nlgrps_max = snap_nlgrpsmax;
lgrp_snap32->ss_root = lgrp_snap->ss_root;
lgrp_snap32->ss_ncpus = lgrp_snap->ss_ncpus;
lgrp_snap32->ss_gen = lgrp_snap->ss_gen;
lgrp_snap32->ss_view = LGRP_VIEW_OS;
lgrp_snap32->ss_size = snap_size;
lgrp_snap32->ss_magic = buf;
lgrp_snap32->ss_info = buf + snap_hdr_size;
lgrp_snap32->ss_cpuids = lgrp_snap32->ss_info + info_size;
lgrp_snap32->ss_lgrpset = lgrp_snap32->ss_cpuids + cpuids_size;
lgrp_snap32->ss_parents = lgrp_snap32->ss_lgrpset + bitmask_size;
lgrp_snap32->ss_children = lgrp_snap32->ss_parents +
(snap_nlgrpsmax * bitmask_size);
lgrp_snap32->ss_rsets = lgrp_snap32->ss_children +
(snap_nlgrpsmax * bitmask_size);
lgrp_snap32->ss_latencies = lgrp_snap32->ss_rsets +
(LGRP_RSRC_COUNT * snap_nlgrpsmax * bitmask_size);
/*
* Fill in lgrpset now because caller may have change psets
*/
kpreempt_disable();
for (i = 0; i < snap_nlgrpsmax; i++) {
if (klgrpset_ismember(curthread->t_cpupart->cp_lgrpset,
i)) {
BT_SET32(lgrp_set32, i);
}
}
kpreempt_enable();
/*
* Fill in 32-bit copy of lgroup info and fix up pointers
* to point into user's buffer instead of kernel's
*/
cpu_index = 0;
lgrp_info = lgrp_snap->ss_info;
for (i = 0; i < snap_nlgrpsmax; i++) {
uint_t *children;
uint_t *lgrp_rset;
uint_t *parents;
ulong_t *snap_rset;
/*
* Skip non-existent lgroups
*/
if (lgrp_info[i].info_lgrpid == LGRP_NONE) {
bzero(&lgrp_info32[i], sizeof (lgrp_info32[i]));
lgrp_info32[i].info_lgrpid = LGRP_NONE;
continue;
}
/*
* Fill in parents, children, lgroup resource set, and
* latencies from snapshot
*/
parents = (uint_t *)((uintptr_t)lgrp_parents32 +
i * bitmask_size);
children = (uint_t *)((uintptr_t)lgrp_children32 +
i * bitmask_size);
snap_rset = (ulong_t *)((uintptr_t)lgrp_snap->ss_rsets +
(i * LGRP_RSRC_COUNT * BT_SIZEOFMAP(snap_nlgrpsmax)));
lgrp_rset = (uint_t *)((uintptr_t)lgrp_rsets32 +
(i * LGRP_RSRC_COUNT * bitmask_size));
lgrp_lats32_kernel[i] = (int *)((uintptr_t)lgrp_lats32 +
snap_nlgrpsmax * sizeof (caddr32_t) + i * snap_nlgrpsmax *
sizeof (int));
for (j = 0; j < snap_nlgrpsmax; j++) {
int k;
uint_t *rset;
if (BT_TEST(&lgrp_snap->ss_parents[i], j))
BT_SET32(parents, j);
if (BT_TEST(&lgrp_snap->ss_children[i], j))
BT_SET32(children, j);
for (k = 0; k < LGRP_RSRC_COUNT; k++) {
rset = (uint_t *)((uintptr_t)lgrp_rset +
k * bitmask_size);
if (BT_TEST(&snap_rset[k], j))
BT_SET32(rset, j);
}
lgrp_lats32_kernel[i][j] =
lgrp_snap->ss_latencies[i][j];
}
/*
* Fix up pointer to latency buffer
*/
lgrp_lats32[i] = lgrp_snap32->ss_latencies +
snap_nlgrpsmax * sizeof (caddr32_t) + i * snap_nlgrpsmax *
sizeof (int);
/*
* Fix up pointers for parents, children, and resources
*/
lgrp_info32[i].info_parents = lgrp_snap32->ss_parents +
(i * bitmask_size);
lgrp_info32[i].info_children = lgrp_snap32->ss_children +
(i * bitmask_size);
lgrp_info32[i].info_rset = lgrp_snap32->ss_rsets +
(i * LGRP_RSRC_COUNT * bitmask_size);
/*
* Fill in memory and CPU info
* Only fill in memory for lgroups directly containing memory
*/
snap_rset = &lgrp_info[i].info_rset[LGRP_RSRC_MEM *
BT_BITOUL(snap_nlgrpsmax)];
if (BT_TEST(snap_rset, i)) {
lgrp_info32[i].info_mem_free = lgrp_mem_size(i,
LGRP_MEM_SIZE_FREE);
lgrp_info32[i].info_mem_install =
lgrp_info[i].info_mem_install;
}
lgrp_info32[i].info_ncpus = lgrp_info[i].info_ncpus;
lgrp_info32[i].info_lgrpid = lgrp_info[i].info_lgrpid;
lgrp_info32[i].info_latency = lgrp_info[i].info_latency;
if (lgrp_info32[i].info_ncpus == 0) {
lgrp_info32[i].info_cpuids = 0;
continue;
}
/*
* Fix up pointer for CPU IDs
*/
lgrp_info32[i].info_cpuids = lgrp_snap32->ss_cpuids +
(cpu_index * sizeof (processorid_t));
cpu_index += lgrp_info32[i].info_ncpus;
}
ASSERT(cpu_index == snap_ncpus);
/*
* Copy lgroup CPU IDs into 32-bit snapshot
* before copying it out into user's buffer
*/
bcopy(lgrp_snap->ss_cpuids, lgrp_cpuids32, cpuids_size);
/*
* Copy 32-bit lgroup snapshot into user's buffer all at once
*/
if (copyout(lgrp_snap32, (void *)(uintptr_t)buf, snap_size) != 0) {
kmem_free(lgrp_snap32, snap_size);
kmem_free(lgrp_lats32_kernel, snap_nlgrpsmax * sizeof (int *));
return (set_errno(EFAULT));
}
kmem_free(lgrp_snap32, snap_size);
kmem_free(lgrp_lats32_kernel, snap_nlgrpsmax * sizeof (int *));
return (snap_size);
}
#endif /* _SYSCALL32_IMPL */
int
lgrpsys(int subcode, long ia, void *ap)
{
size_t bufsize;
int latency;
switch (subcode) {
case LGRP_SYS_AFFINITY_GET:
return (lgrp_affinity_get((lgrp_affinity_args_t *)ap));
case LGRP_SYS_AFFINITY_SET:
return (lgrp_affinity_set((lgrp_affinity_args_t *)ap));
case LGRP_SYS_GENERATION:
return (lgrp_generation(ia));
case LGRP_SYS_HOME:
return (lgrp_home_get((idtype_t)ia, (id_t)(uintptr_t)ap));
case LGRP_SYS_LATENCY:
mutex_enter(&cpu_lock);
latency = lgrp_latency(ia, (lgrp_id_t)(uintptr_t)ap);
mutex_exit(&cpu_lock);
return (latency);
case LGRP_SYS_MEMINFO:
return (meminfo(ia, (struct meminfo *)ap));
case LGRP_SYS_VERSION:
return (lgrp_version(ia));
case LGRP_SYS_SNAPSHOT:
mutex_enter(&lgrp_snap_lock);
bufsize = lgrp_snapshot();
if (ap && ia > 0) {
if (get_udatamodel() == DATAMODEL_NATIVE)
bufsize = lgrp_snapshot_copy(ap, ia);
#ifdef _SYSCALL32_IMPL
else
bufsize = lgrp_snapshot_copy32(
(caddr32_t)(uintptr_t)ap, ia);
#endif /* _SYSCALL32_IMPL */
}
mutex_exit(&lgrp_snap_lock);
return (bufsize);
default:
break;
}
return (set_errno(EINVAL));
}
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/uio.h>
#include <sys/debug.h>
#include <c2/audit.h>
/*
* Make a hard link.
*/
int
linkat(int ffd, char *from, int tfd, char *to, int flag)
{
vnode_t *fstartvp = NULL;
vnode_t *tstartvp = NULL;
enum symfollow follow;
int error;
if (flag & ~AT_SYMLINK_FOLLOW)
return (set_errno(EINVAL));
follow = (flag & AT_SYMLINK_FOLLOW)? FOLLOW : NO_FOLLOW;
if (from == NULL || to == NULL)
return (set_errno(EFAULT));
if ((error = fgetstartvp(ffd, from, &fstartvp)) != 0)
goto out;
if ((error = fgetstartvp(tfd, to, &tstartvp)) != 0)
goto out;
error = vn_linkat(fstartvp, from, follow, tstartvp, to, UIO_USERSPACE);
out:
if (fstartvp != NULL)
VN_RELE(fstartvp);
if (tstartvp != NULL)
VN_RELE(tstartvp);
if (error)
return (set_errno(error));
return (0);
}
int
link(char *from, char *to)
{
return (linkat(AT_FDCWD, from, AT_FDCWD, to, 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.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/debug.h>
#include <sys/cmn_err.h>
#include <sys/filio.h>
/*
* These are defined in unistd.h - but we can't include that
*/
#define SEEK_SET 0 /* Set file pointer to "offset" */
#define SEEK_CUR 1 /* Set file pointer to current plus "offset" */
#define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
#define SEEK_DATA 3 /* Set file pointer to next data past offset */
#define SEEK_HOLE 4 /* Set file pointer to next hole past offset */
/*
* Seek on a file
*/
#if defined(_SYSCALL32_IMPL) || defined(_ILP32)
/*
* Workhorse for the 32-bit seek variants: lseek32 and llseek32
*
* 'max' represents the maximum possible representation of offset
* in the data type corresponding to lseek and llseek. It is
* MAXOFF32_T for off32_t and MAXOFFSET_T for off64_t.
* We return EOVERFLOW if we cannot represent the resulting offset
* in the data type.
* We provide support for character devices to be seeked beyond MAXOFF32_T
* by lseek. To maintain compatibility in such cases lseek passes
* the arguments carefully to lseek_common when file is not regular.
* (/dev/kmem is a good example of a > 2Gbyte seek!)
*/
static int
lseek32_common(file_t *fp, int stype, offset_t off, offset_t max,
offset_t *retoff)
{
vnode_t *vp;
struct vattr vattr;
int error;
u_offset_t noff;
offset_t curoff, newoff;
int reg;
vp = fp->f_vnode;
reg = (vp->v_type == VREG);
curoff = fp->f_offset;
switch (stype) {
case SEEK_SET:
noff = (u_offset_t)off;
if (reg && noff > max) {
error = EINVAL;
goto out;
}
break;
case SEEK_CUR:
if (reg && off > (max - curoff)) {
error = EOVERFLOW;
goto out;
}
noff = (u_offset_t)(off + curoff);
if (reg && noff > max) {
error = EINVAL;
goto out;
}
break;
case SEEK_END:
vattr.va_mask = AT_SIZE;
if (error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred, NULL)) {
goto out;
}
if (reg && (off > (max - (offset_t)vattr.va_size))) {
error = EOVERFLOW;
goto out;
}
noff = (u_offset_t)(off + (offset_t)vattr.va_size);
if (reg && noff > max) {
error = EINVAL;
goto out;
}
break;
case SEEK_DATA:
/*
* Get and set the file pointer to the offset of the next
* data past "off"
*/
noff = (u_offset_t)off;
error = VOP_IOCTL(vp, _FIO_SEEK_DATA, (intptr_t)(&noff),
FKIOCTL, kcred, NULL, NULL);
if (error) {
if (error != ENOTTY)
return (error);
/*
* The ioctl is not supported, check the supplied
* "off" is not past the end of file
*/
vattr.va_mask = AT_SIZE;
error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred, NULL);
if (error)
return (error);
if (noff >= (u_offset_t)vattr.va_size)
return (ENXIO);
}
if (reg && (noff > max))
return (EOVERFLOW);
fp->f_offset = (offset_t)noff;
(*retoff) = (offset_t)noff;
return (0);
case SEEK_HOLE:
/*
* Get and set the file pointer to the offset of the next
* hole past "off"
*/
noff = (u_offset_t)off;
error = VOP_IOCTL(vp, _FIO_SEEK_HOLE, (intptr_t)(&noff),
FKIOCTL, kcred, NULL, NULL);
if (error) {
if (error != ENOTTY)
return (error);
/*
* ioctl is not supported, if the off is valid return
* the "virtual hole" at the end of the file.
*/
vattr.va_mask = AT_SIZE;
error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred, NULL);
if (error)
return (error);
if (off < (offset_t)vattr.va_size)
noff = (u_offset_t)vattr.va_size;
else
return (ENXIO);
}
if (reg && (noff > max))
return (EOVERFLOW);
fp->f_offset = (offset_t)noff;
(*retoff) = (offset_t)noff;
return (0);
default:
error = EINVAL;
goto out;
}
ASSERT((reg && noff <= max) || !reg);
newoff = (offset_t)noff;
if ((error = VOP_SEEK(vp, curoff, &newoff, NULL)) == 0) {
fp->f_offset = newoff;
(*retoff) = newoff;
return (0);
}
out:
return (error);
}
off32_t
lseek32(int32_t fdes, off32_t off, int32_t stype)
{
file_t *fp;
int error;
offset_t retoff;
if ((fp = getf(fdes)) == NULL)
return ((off32_t)set_errno(EBADF));
/*
* lseek32 returns EOVERFLOW if we cannot represent the resulting
* offset from seek in a 32-bit off_t.
* The following routines are sensitive to sign extensions and
* calculations and if ever you change this make sure it works for
* special files.
*
* When VREG is not set we do the check for stype != SEEK_SET
* to send the unsigned value to lseek_common and not the sign
* extended value. (The maximum representable value is not
* checked by lseek_common for special files.)
*/
if (fp->f_vnode->v_type == VREG || stype != SEEK_SET)
error = lseek32_common(fp, stype, (offset_t)off,
(offset_t)MAXOFF32_T, &retoff);
else if (stype == SEEK_SET)
error = lseek32_common(fp, stype, (offset_t)(uint_t)off,
(offset_t)(uint_t)UINT_MAX, &retoff);
releasef(fdes);
if (!error)
return ((off32_t)retoff);
return ((off32_t)set_errno(error));
}
/*
* 64-bit seeks from 32-bit applications
*/
offset_t
llseek32(int32_t fdes, uint32_t off1, uint32_t off2, int stype)
{
file_t *fp;
int error;
offset_t retoff;
#if defined(_LITTLE_ENDIAN)
offset_t off = ((u_offset_t)off2 << 32) | (u_offset_t)off1;
#else
offset_t off = ((u_offset_t)off1 << 32) | (u_offset_t)off2;
#endif
if ((fp = getf(fdes)) == NULL)
error = EBADF;
else {
error = lseek32_common(fp, stype, off, MAXOFFSET_T, &retoff);
releasef(fdes);
}
return (error ? (offset_t)set_errno(error) : retoff);
}
#endif /* _SYSCALL32_IMPL || _ILP32 */
#ifdef _LP64
/*
* Seek on a file.
*
* Life is almost simple again (at least until we do 128-bit files ;-)
* This is both 'lseek' and 'llseek' to a 64-bit application.
*/
off_t
lseek64(int fdes, off_t off, int stype)
{
file_t *fp;
vnode_t *vp;
struct vattr vattr;
int error;
off_t old_off;
offset_t new_off;
if ((fp = getf(fdes)) == NULL)
return ((off_t)set_errno(EBADF));
vp = fp->f_vnode;
new_off = off;
switch (stype) {
case SEEK_CUR:
new_off += fp->f_offset;
break;
case SEEK_END:
vattr.va_mask = AT_SIZE;
if ((error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred, NULL)) != 0)
goto lseek64error;
new_off += vattr.va_size;
break;
case SEEK_SET:
break;
case SEEK_DATA:
/*
* Get and set the file pointer to the offset of the next
* data past "off"
*/
new_off = (offset_t)off;
error = VOP_IOCTL(vp, _FIO_SEEK_DATA, (intptr_t)(&new_off),
FKIOCTL, kcred, NULL, NULL);
if (error) {
if (error != ENOTTY) {
goto lseek64error;
}
/*
* The ioctl is not supported, check the supplied off
* is not past end of file
*/
vattr.va_mask = AT_SIZE;
error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred, NULL);
if (error)
goto lseek64error;
if (new_off >= (offset_t)vattr.va_size) {
error = ENXIO;
goto lseek64error;
}
}
fp->f_offset = new_off;
releasef(fdes);
return (new_off);
case SEEK_HOLE:
/*
* Get and set the file pointer to the offset of the next
* hole past "off"
*/
new_off = off;
error = VOP_IOCTL(vp, _FIO_SEEK_HOLE, (intptr_t)(&new_off),
FKIOCTL, kcred, NULL, NULL);
if (error) {
if (error != ENOTTY)
goto lseek64error;
/*
* ioctl is not supported, if the off is valid return
* the "virtual hole" at the end of the file.
*/
vattr.va_mask = AT_SIZE;
error = VOP_GETATTR(vp, &vattr, 0, fp->f_cred, NULL);
if (error)
goto lseek64error;
if (off < (offset_t)vattr.va_size) {
new_off = (offset_t)vattr.va_size;
} else {
error = ENXIO;
goto lseek64error;
}
}
fp->f_offset = new_off;
releasef(fdes);
return (new_off);
default:
error = EINVAL;
goto lseek64error;
}
old_off = fp->f_offset;
if ((error = VOP_SEEK(vp, old_off, &new_off, NULL)) == 0) {
fp->f_offset = new_off;
releasef(fdes);
return (new_off);
}
lseek64error:
releasef(fdes);
return ((off_t)set_errno(error));
}
#endif /* _LP64 */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/syscall.h>
#include <sys/proc.h>
#include <sys/processor.h>
#include <sys/fault.h>
#include <sys/ucontext.h>
#include <sys/signal.h>
#include <sys/unistd.h>
#include <sys/procfs.h>
#include <sys/prsystm.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/klwp.h>
#include <sys/pool.h>
/*
* System call to create an lwp.
*
* Notes on the LWP_DETACHED and LWP_DAEMON flags:
*
* A detached lwp (LWP_DETACHED) cannot be the specific target of
* lwp_wait() (it is not joinable), but lwp_wait(0, ...) is required
* to sleep until all non-daemon detached lwps have terminated before
* returning EDEADLK because a detached lwp might create a non-detached lwp
* that could then be returned by lwp_wait(0, ...). See also lwp_detach().
*
* A daemon lwp (LWP_DAEMON) is a detached lwp that has the additional
* property that it does not affect the termination condition of the
* process: The last non-daemon lwp to call lwp_exit() causes the process
* to exit and lwp_wait(0, ...) does not sleep waiting for daemon lwps
* to terminate. See the block comment before lwp_wait().
*/
int
syslwp_create(ucontext_t *ucp, int flags, id_t *new_lwp)
{
klwp_t *lwp;
proc_t *p = ttoproc(curthread);
kthread_t *t;
ucontext_t uc;
#ifdef _SYSCALL32_IMPL
ucontext32_t uc32;
#endif /* _SYSCALL32_IMPL */
k_sigset_t sigmask;
int tid;
model_t model = get_udatamodel();
uintptr_t thrptr = 0;
if (flags & ~(LWP_DAEMON|LWP_DETACHED|LWP_SUSPENDED))
return (set_errno(EINVAL));
/*
* lwp_create() is disallowed for the /proc agent lwp.
*/
if (curthread == p->p_agenttp)
return (set_errno(ENOTSUP));
if (model == DATAMODEL_NATIVE) {
if (copyin(ucp, &uc, sizeof (ucontext_t)))
return (set_errno(EFAULT));
sigutok(&uc.uc_sigmask, &sigmask);
}
#ifdef _SYSCALL32_IMPL
else {
if (copyin(ucp, &uc32, sizeof (ucontext32_t)))
return (set_errno(EFAULT));
sigutok(&uc32.uc_sigmask, &sigmask);
#if defined(__sparc)
ucontext_32ton(&uc32, &uc, NULL, NULL);
#else /* __amd64 */
ucontext_32ton(&uc32, &uc);
/*
* libc stashed thrptr into unused kernel %sp.
* See setup_context() in libc.
*/
thrptr = (uint32_t)uc32.uc_mcontext.gregs[ESP];
#endif
}
#endif /* _SYSCALL32_IMPL */
/*
* Tell machine specific code that we are creating a new lwp
*/
LWP_MMODEL_NEWLWP();
(void) save_syscall_args(); /* save args for tracing first */
mutex_enter(&curproc->p_lock);
pool_barrier_enter();
mutex_exit(&curproc->p_lock);
lwp = lwp_create(lwp_rtt, NULL, 0, curproc, TS_STOPPED,
curthread->t_pri, &sigmask, curthread->t_cid, 0);
mutex_enter(&curproc->p_lock);
pool_barrier_exit();
mutex_exit(&curproc->p_lock);
if (lwp == NULL)
return (set_errno(EAGAIN));
lwp_load(lwp, uc.uc_mcontext.gregs, thrptr);
t = lwptot(lwp);
/*
* Copy the new lwp's lwpid into the caller's specified buffer.
*/
if (new_lwp && copyout(&t->t_tid, new_lwp, sizeof (id_t))) {
/*
* caller's buffer is not writable, return
* EFAULT, and terminate new lwp.
*/
mutex_enter(&p->p_lock);
t->t_proc_flag |= TP_EXITLWP;
t->t_sig_check = 1;
t->t_sysnum = 0;
t->t_proc_flag &= ~TP_HOLDLWP;
lwp_create_done(t);
mutex_exit(&p->p_lock);
return (set_errno(EFAULT));
}
/*
* clone callers context, if any. must be invoked
* while -not- holding p_lock.
*/
if (curthread->t_ctx)
lwp_createctx(curthread, t);
/*
* copy current contract templates
*/
lwp_ctmpl_copy(lwp, ttolwp(curthread));
mutex_enter(&p->p_lock);
/*
* Copy the syscall arguments to the new lwp's arg area
* for the benefit of debuggers.
*/
t->t_sysnum = SYS_lwp_create;
lwp->lwp_ap = lwp->lwp_arg;
lwp->lwp_arg[0] = (long)ucp;
lwp->lwp_arg[1] = (long)flags;
lwp->lwp_arg[2] = (long)new_lwp;
lwp->lwp_argsaved = 1;
if (!(flags & (LWP_DETACHED|LWP_DAEMON)))
t->t_proc_flag |= TP_TWAIT;
if (flags & LWP_DAEMON) {
t->t_proc_flag |= TP_DAEMON;
p->p_lwpdaemon++;
}
tid = (int)t->t_tid; /* for /proc debuggers */
/*
* We now set the newly-created lwp running.
* If it is being created as LWP_SUSPENDED, we leave its
* TP_HOLDLWP flag set so it will stop in system call exit.
*/
if (!(flags & LWP_SUSPENDED))
t->t_proc_flag &= ~TP_HOLDLWP;
lwp_create_done(t);
mutex_exit(&p->p_lock);
return (tid);
}
/*
* Exit the calling lwp
*/
void
syslwp_exit()
{
proc_t *p = ttoproc(curthread);
mutex_enter(&p->p_lock);
lwp_exit();
/* NOTREACHED */
}
/*
* 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.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/time.h>
#include <sys/debug.h>
#include <sys/model.h>
#include <sys/msacct.h>
/*
* Get the time accounting information for the calling LWP.
*/
int
lwp_info(timestruc_t *tvp)
{
timestruc_t tv[2];
hrtime_t hrutime, hrstime;
klwp_t *lwp = ttolwp(curthread);
hrutime = lwp->lwp_mstate.ms_acct[LMS_USER];
hrstime = lwp->lwp_mstate.ms_acct[LMS_SYSTEM] +
lwp->lwp_mstate.ms_acct[LMS_TRAP];
scalehrtime(&hrutime);
scalehrtime(&hrstime);
hrt2ts(hrutime, &tv[0]);
hrt2ts(hrstime, &tv[1]);
if (get_udatamodel() == DATAMODEL_NATIVE) {
if (copyout(tv, tvp, sizeof (tv)))
return (set_errno(EFAULT));
} else {
timestruc32_t tv32[2];
if (TIMESPEC_OVERFLOW(&tv[0]) ||
TIMESPEC_OVERFLOW(&tv[1]))
return (set_errno(EOVERFLOW)); /* unlikely */
TIMESPEC_TO_TIMESPEC32(&tv32[0], &tv[0]);
TIMESPEC_TO_TIMESPEC32(&tv32[1], &tv[1]);
if (copyout(tv32, tvp, sizeof (tv32)))
return (set_errno(EFAULT));
}
return (0);
}
/*
* 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 (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* Copyright (c) 1994 Sun Microsystems, Inc. */
/* All Rights Reserved */
#ident "%Z%%M% %I% %E% SMI" /* from SVr4.0 1.78 */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/thread.h>
int
lwp_self()
{
return (curthread->t_tid);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2015 Joyent, Inc.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/prsystm.h>
#include <sys/kmem.h>
#include <sys/sobject.h>
#include <sys/fault.h>
#include <sys/procfs.h>
#include <sys/watchpoint.h>
#include <sys/time.h>
#include <sys/cmn_err.h>
#include <sys/machlock.h>
#include <sys/debug.h>
#include <sys/synch.h>
#include <sys/synch32.h>
#include <sys/mman.h>
#include <sys/class.h>
#include <sys/schedctl.h>
#include <sys/sleepq.h>
#include <sys/policy.h>
#include <sys/lwpchan_impl.h>
#include <sys/turnstile.h>
#include <sys/atomic.h>
#include <sys/lwp_timer_impl.h>
#include <sys/lwp_upimutex_impl.h>
#include <vm/as.h>
#include <sys/sdt.h>
static kthread_t *lwpsobj_owner(caddr_t);
static void lwp_unsleep(kthread_t *t);
static void lwp_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip);
static void lwp_mutex_cleanup(lwpchan_entry_t *ent, uint16_t lockflg);
static void lwp_mutex_unregister(void *uaddr);
static void set_owner_pid(lwp_mutex_t *, uintptr_t, pid_t);
static int iswanted(kthread_t *, lwpchan_t *);
extern int lwp_cond_signal(lwp_cond_t *cv);
/*
* Maximum number of user prio inheritance locks that can be held by a thread.
* Used to limit kmem for each thread. This is a per-thread limit that
* can be administered on a system wide basis (using /etc/system).
*
* Also, when a limit, say maxlwps is added for numbers of lwps within a
* process, the per-thread limit automatically becomes a process-wide limit
* of maximum number of held upi locks within a process:
* maxheldupimx = maxnestupimx * maxlwps;
*/
static uint32_t maxnestupimx = 2000;
/*
* The sobj_ops vector exports a set of functions needed when a thread
* is asleep on a synchronization object of this type.
*/
static sobj_ops_t lwp_sobj_ops = {
SOBJ_USER, lwpsobj_owner, lwp_unsleep, lwp_change_pri
};
static kthread_t *lwpsobj_pi_owner(upimutex_t *up);
static sobj_ops_t lwp_sobj_pi_ops = {
SOBJ_USER_PI, lwpsobj_pi_owner, turnstile_unsleep,
turnstile_change_pri
};
static sleepq_head_t lwpsleepq[NSLEEPQ];
upib_t upimutextab[UPIMUTEX_TABSIZE];
#define LWPCHAN_LOCK_SHIFT 10 /* 1024 locks for each pool */
#define LWPCHAN_LOCK_SIZE (1 << LWPCHAN_LOCK_SHIFT)
/*
* We know that both lc_wchan and lc_wchan0 are addresses that most
* likely are 8-byte aligned, so we shift off the low-order 3 bits.
* 'pool' is either 0 or 1.
*/
#define LWPCHAN_LOCK_HASH(X, pool) \
(((((X) >> 3) ^ ((X) >> (LWPCHAN_LOCK_SHIFT + 3))) & \
(LWPCHAN_LOCK_SIZE - 1)) + ((pool)? LWPCHAN_LOCK_SIZE : 0))
static kmutex_t lwpchanlock[2 * LWPCHAN_LOCK_SIZE];
/*
* Is this a POSIX threads user-level lock requiring priority inheritance?
*/
#define UPIMUTEX(type) ((type) & LOCK_PRIO_INHERIT)
static sleepq_head_t *
lwpsqhash(lwpchan_t *lwpchan)
{
uint_t x = (uintptr_t)lwpchan->lc_wchan ^ (uintptr_t)lwpchan->lc_wchan0;
return (&lwpsleepq[SQHASHINDEX(x)]);
}
/*
* Lock an lwpchan.
* Keep this in sync with lwpchan_unlock(), below.
*/
static void
lwpchan_lock(lwpchan_t *lwpchan, int pool)
{
uint_t x = (uintptr_t)lwpchan->lc_wchan ^ (uintptr_t)lwpchan->lc_wchan0;
mutex_enter(&lwpchanlock[LWPCHAN_LOCK_HASH(x, pool)]);
}
/*
* Unlock an lwpchan.
* Keep this in sync with lwpchan_lock(), above.
*/
static void
lwpchan_unlock(lwpchan_t *lwpchan, int pool)
{
uint_t x = (uintptr_t)lwpchan->lc_wchan ^ (uintptr_t)lwpchan->lc_wchan0;
mutex_exit(&lwpchanlock[LWPCHAN_LOCK_HASH(x, pool)]);
}
/*
* Delete mappings from the lwpchan cache for pages that are being
* unmapped by as_unmap(). Given a range of addresses, "start" to "end",
* all mappings within the range are deleted from the lwpchan cache.
*/
void
lwpchan_delete_mapping(proc_t *p, caddr_t start, caddr_t end)
{
lwpchan_data_t *lcp;
lwpchan_hashbucket_t *hashbucket;
lwpchan_hashbucket_t *endbucket;
lwpchan_entry_t *ent;
lwpchan_entry_t **prev;
caddr_t addr;
mutex_enter(&p->p_lcp_lock);
lcp = p->p_lcp;
hashbucket = lcp->lwpchan_cache;
endbucket = hashbucket + lcp->lwpchan_size;
for (; hashbucket < endbucket; hashbucket++) {
if (hashbucket->lwpchan_chain == NULL)
continue;
mutex_enter(&hashbucket->lwpchan_lock);
prev = &hashbucket->lwpchan_chain;
/* check entire chain */
while ((ent = *prev) != NULL) {
addr = ent->lwpchan_addr;
if (start <= addr && addr < end) {
*prev = ent->lwpchan_next;
/*
* We do this only for the obsolete type
* USYNC_PROCESS_ROBUST. Otherwise robust
* locks do not draw ELOCKUNMAPPED or
* EOWNERDEAD due to being unmapped.
*/
if (ent->lwpchan_pool == LWPCHAN_MPPOOL &&
(ent->lwpchan_type & USYNC_PROCESS_ROBUST))
lwp_mutex_cleanup(ent, LOCK_UNMAPPED);
/*
* If there is a user-level robust lock
* registration, mark it as invalid.
*/
if ((addr = ent->lwpchan_uaddr) != NULL)
lwp_mutex_unregister(addr);
kmem_free(ent, sizeof (*ent));
atomic_dec_32(&lcp->lwpchan_entries);
} else {
prev = &ent->lwpchan_next;
}
}
mutex_exit(&hashbucket->lwpchan_lock);
}
mutex_exit(&p->p_lcp_lock);
}
/*
* Given an lwpchan cache pointer and a process virtual address,
* return a pointer to the corresponding lwpchan hash bucket.
*/
static lwpchan_hashbucket_t *
lwpchan_bucket(lwpchan_data_t *lcp, uintptr_t addr)
{
uint_t i;
/*
* All user-level sync object addresses are 8-byte aligned.
* Ignore the lowest 3 bits of the address and use the
* higher-order 2*lwpchan_bits bits for the hash index.
*/
addr >>= 3;
i = (addr ^ (addr >> lcp->lwpchan_bits)) & lcp->lwpchan_mask;
return (lcp->lwpchan_cache + i);
}
/*
* (Re)allocate the per-process lwpchan cache.
*/
static void
lwpchan_alloc_cache(proc_t *p, uint_t bits)
{
lwpchan_data_t *lcp;
lwpchan_data_t *old_lcp;
lwpchan_hashbucket_t *hashbucket;
lwpchan_hashbucket_t *endbucket;
lwpchan_hashbucket_t *newbucket;
lwpchan_entry_t *ent;
lwpchan_entry_t *next;
uint_t count;
ASSERT(bits >= LWPCHAN_INITIAL_BITS && bits <= LWPCHAN_MAX_BITS);
lcp = kmem_alloc(sizeof (lwpchan_data_t), KM_SLEEP);
lcp->lwpchan_bits = bits;
lcp->lwpchan_size = 1 << lcp->lwpchan_bits;
lcp->lwpchan_mask = lcp->lwpchan_size - 1;
lcp->lwpchan_entries = 0;
lcp->lwpchan_cache = kmem_zalloc(lcp->lwpchan_size *
sizeof (lwpchan_hashbucket_t), KM_SLEEP);
lcp->lwpchan_next_data = NULL;
mutex_enter(&p->p_lcp_lock);
if ((old_lcp = p->p_lcp) != NULL) {
if (old_lcp->lwpchan_bits >= bits) {
/* someone beat us to it */
mutex_exit(&p->p_lcp_lock);
kmem_free(lcp->lwpchan_cache, lcp->lwpchan_size *
sizeof (lwpchan_hashbucket_t));
kmem_free(lcp, sizeof (lwpchan_data_t));
return;
}
/*
* Acquire all of the old hash table locks.
*/
hashbucket = old_lcp->lwpchan_cache;
endbucket = hashbucket + old_lcp->lwpchan_size;
for (; hashbucket < endbucket; hashbucket++)
mutex_enter(&hashbucket->lwpchan_lock);
/*
* Move all of the old hash table entries to the
* new hash table. The new hash table has not yet
* been installed so we don't need any of its locks.
*/
count = 0;
hashbucket = old_lcp->lwpchan_cache;
for (; hashbucket < endbucket; hashbucket++) {
ent = hashbucket->lwpchan_chain;
while (ent != NULL) {
next = ent->lwpchan_next;
newbucket = lwpchan_bucket(lcp,
(uintptr_t)ent->lwpchan_addr);
ent->lwpchan_next = newbucket->lwpchan_chain;
newbucket->lwpchan_chain = ent;
ent = next;
count++;
}
hashbucket->lwpchan_chain = NULL;
}
lcp->lwpchan_entries = count;
}
/*
* Retire the old hash table. We can't actually kmem_free() it
* now because someone may still have a pointer to it. Instead,
* we link it onto the new hash table's list of retired hash tables.
* The new hash table is double the size of the previous one, so
* the total size of all retired hash tables is less than the size
* of the new one. exit() and exec() free the retired hash tables
* (see lwpchan_destroy_cache(), below).
*/
lcp->lwpchan_next_data = old_lcp;
/*
* As soon as we store the new lcp, future locking operations will
* use it. Therefore, we must ensure that all the state we've just
* established reaches global visibility before the new lcp does.
*/
membar_producer();
p->p_lcp = lcp;
if (old_lcp != NULL) {
/*
* Release all of the old hash table locks.
*/
hashbucket = old_lcp->lwpchan_cache;
for (; hashbucket < endbucket; hashbucket++)
mutex_exit(&hashbucket->lwpchan_lock);
}
mutex_exit(&p->p_lcp_lock);
}
/*
* Deallocate the lwpchan cache, and any dynamically allocated mappings.
* Called when the process exits or execs. All lwps except one have
* exited so we need no locks here.
*/
void
lwpchan_destroy_cache(int exec)
{
proc_t *p = curproc;
lwpchan_hashbucket_t *hashbucket;
lwpchan_hashbucket_t *endbucket;
lwpchan_data_t *lcp;
lwpchan_entry_t *ent;
lwpchan_entry_t *next;
uint16_t lockflg;
lcp = p->p_lcp;
p->p_lcp = NULL;
lockflg = exec? LOCK_UNMAPPED : LOCK_OWNERDEAD;
hashbucket = lcp->lwpchan_cache;
endbucket = hashbucket + lcp->lwpchan_size;
for (; hashbucket < endbucket; hashbucket++) {
ent = hashbucket->lwpchan_chain;
hashbucket->lwpchan_chain = NULL;
while (ent != NULL) {
next = ent->lwpchan_next;
if (ent->lwpchan_pool == LWPCHAN_MPPOOL &&
(ent->lwpchan_type & (USYNC_PROCESS | LOCK_ROBUST))
== (USYNC_PROCESS | LOCK_ROBUST))
lwp_mutex_cleanup(ent, lockflg);
kmem_free(ent, sizeof (*ent));
ent = next;
}
}
while (lcp != NULL) {
lwpchan_data_t *next_lcp = lcp->lwpchan_next_data;
kmem_free(lcp->lwpchan_cache, lcp->lwpchan_size *
sizeof (lwpchan_hashbucket_t));
kmem_free(lcp, sizeof (lwpchan_data_t));
lcp = next_lcp;
}
}
/*
* Return zero when there is an entry in the lwpchan cache for the
* given process virtual address and non-zero when there is not.
* The returned non-zero value is the current length of the
* hash chain plus one. The caller holds the hash bucket lock.
*/
static uint_t
lwpchan_cache_mapping(caddr_t addr, int type, int pool, lwpchan_t *lwpchan,
lwpchan_hashbucket_t *hashbucket)
{
lwpchan_entry_t *ent;
uint_t count = 1;
for (ent = hashbucket->lwpchan_chain; ent; ent = ent->lwpchan_next) {
if (ent->lwpchan_addr == addr) {
if (ent->lwpchan_type != type ||
ent->lwpchan_pool != pool) {
/*
* This shouldn't happen, but might if the
* process reuses its memory for different
* types of sync objects. We test first
* to avoid grabbing the memory cache line.
*/
ent->lwpchan_type = (uint16_t)type;
ent->lwpchan_pool = (uint16_t)pool;
}
*lwpchan = ent->lwpchan_lwpchan;
return (0);
}
count++;
}
return (count);
}
/*
* Return the cached lwpchan mapping if cached, otherwise insert
* a virtual address to lwpchan mapping into the cache.
*/
static int
lwpchan_get_mapping(struct as *as, caddr_t addr, caddr_t uaddr,
int type, lwpchan_t *lwpchan, int pool)
{
proc_t *p = curproc;
lwpchan_data_t *lcp;
lwpchan_hashbucket_t *hashbucket;
lwpchan_entry_t *ent;
memid_t memid;
uint_t count;
uint_t bits;
top:
/* initialize the lwpchan cache, if necesary */
if ((lcp = p->p_lcp) == NULL) {
lwpchan_alloc_cache(p, LWPCHAN_INITIAL_BITS);
goto top;
}
hashbucket = lwpchan_bucket(lcp, (uintptr_t)addr);
mutex_enter(&hashbucket->lwpchan_lock);
if (lcp != p->p_lcp) {
/* someone resized the lwpchan cache; start over */
mutex_exit(&hashbucket->lwpchan_lock);
goto top;
}
if (lwpchan_cache_mapping(addr, type, pool, lwpchan, hashbucket) == 0) {
/* it's in the cache */
mutex_exit(&hashbucket->lwpchan_lock);
return (1);
}
mutex_exit(&hashbucket->lwpchan_lock);
if (as_getmemid(as, addr, &memid) != 0)
return (0);
lwpchan->lc_wchan0 = (caddr_t)(uintptr_t)memid.val[0];
lwpchan->lc_wchan = (caddr_t)(uintptr_t)memid.val[1];
ent = kmem_alloc(sizeof (lwpchan_entry_t), KM_SLEEP);
mutex_enter(&hashbucket->lwpchan_lock);
if (lcp != p->p_lcp) {
/* someone resized the lwpchan cache; start over */
mutex_exit(&hashbucket->lwpchan_lock);
kmem_free(ent, sizeof (*ent));
goto top;
}
count = lwpchan_cache_mapping(addr, type, pool, lwpchan, hashbucket);
if (count == 0) {
/* someone else added this entry to the cache */
mutex_exit(&hashbucket->lwpchan_lock);
kmem_free(ent, sizeof (*ent));
return (1);
}
if (count > lcp->lwpchan_bits + 2 && /* larger table, longer chains */
(bits = lcp->lwpchan_bits) < LWPCHAN_MAX_BITS) {
/* hash chain too long; reallocate the hash table */
mutex_exit(&hashbucket->lwpchan_lock);
kmem_free(ent, sizeof (*ent));
lwpchan_alloc_cache(p, bits + 1);
goto top;
}
ent->lwpchan_addr = addr;
ent->lwpchan_uaddr = uaddr;
ent->lwpchan_type = (uint16_t)type;
ent->lwpchan_pool = (uint16_t)pool;
ent->lwpchan_lwpchan = *lwpchan;
ent->lwpchan_next = hashbucket->lwpchan_chain;
hashbucket->lwpchan_chain = ent;
atomic_inc_32(&lcp->lwpchan_entries);
mutex_exit(&hashbucket->lwpchan_lock);
return (1);
}
/*
* Return a unique pair of identifiers that corresponds to a
* synchronization object's virtual address. Process-shared
* sync objects usually get vnode/offset from as_getmemid().
*/
static int
get_lwpchan(struct as *as, caddr_t addr, int type, lwpchan_t *lwpchan, int pool)
{
/*
* If the lwp synch object is defined to be process-private,
* we just make the first field of the lwpchan be 'as' and
* the second field be the synch object's virtual address.
* (segvn_getmemid() does the same for MAP_PRIVATE mappings.)
* The lwpchan cache is used only for process-shared objects.
*/
if (!(type & USYNC_PROCESS)) {
lwpchan->lc_wchan0 = (caddr_t)as;
lwpchan->lc_wchan = addr;
return (1);
}
return (lwpchan_get_mapping(as, addr, NULL, type, lwpchan, pool));
}
static void
lwp_block(lwpchan_t *lwpchan)
{
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
sleepq_head_t *sqh;
thread_lock(t);
t->t_flag |= T_WAKEABLE;
t->t_lwpchan = *lwpchan;
t->t_sobj_ops = &lwp_sobj_ops;
t->t_release = 0;
sqh = lwpsqhash(lwpchan);
disp_lock_enter_high(&sqh->sq_lock);
CL_SLEEP(t);
DTRACE_SCHED(sleep);
THREAD_SLEEP(t, &sqh->sq_lock);
sleepq_insert(&sqh->sq_queue, t);
thread_unlock(t);
lwp->lwp_asleep = 1;
lwp->lwp_sysabort = 0;
lwp->lwp_ru.nvcsw++;
(void) new_mstate(curthread, LMS_SLEEP);
}
static kthread_t *
lwpsobj_pi_owner(upimutex_t *up)
{
return (up->upi_owner);
}
static struct upimutex *
upi_get(upib_t *upibp, lwpchan_t *lcp)
{
struct upimutex *upip;
for (upip = upibp->upib_first; upip != NULL;
upip = upip->upi_nextchain) {
if (upip->upi_lwpchan.lc_wchan0 == lcp->lc_wchan0 &&
upip->upi_lwpchan.lc_wchan == lcp->lc_wchan)
break;
}
return (upip);
}
static void
upi_chain_add(upib_t *upibp, struct upimutex *upimutex)
{
ASSERT(MUTEX_HELD(&upibp->upib_lock));
/*
* Insert upimutex at front of list. Maybe a bit unfair
* but assume that not many lwpchans hash to the same
* upimutextab bucket, i.e. the list of upimutexes from
* upib_first is not too long.
*/
upimutex->upi_nextchain = upibp->upib_first;
upibp->upib_first = upimutex;
}
static void
upi_chain_del(upib_t *upibp, struct upimutex *upimutex)
{
struct upimutex **prev;
ASSERT(MUTEX_HELD(&upibp->upib_lock));
prev = &upibp->upib_first;
while (*prev != upimutex) {
prev = &(*prev)->upi_nextchain;
}
*prev = upimutex->upi_nextchain;
upimutex->upi_nextchain = NULL;
}
/*
* Add upimutex to chain of upimutexes held by curthread.
* Returns number of upimutexes held by curthread.
*/
static uint32_t
upi_mylist_add(struct upimutex *upimutex)
{
kthread_t *t = curthread;
/*
* Insert upimutex at front of list of upimutexes owned by t. This
* would match typical LIFO order in which nested locks are acquired
* and released.
*/
upimutex->upi_nextowned = t->t_upimutex;
t->t_upimutex = upimutex;
t->t_nupinest++;
ASSERT(t->t_nupinest > 0);
return (t->t_nupinest);
}
/*
* Delete upimutex from list of upimutexes owned by curthread.
*/
static void
upi_mylist_del(struct upimutex *upimutex)
{
kthread_t *t = curthread;
struct upimutex **prev;
/*
* Since the order in which nested locks are acquired and released,
* is typically LIFO, and typical nesting levels are not too deep, the
* following should not be expensive in the general case.
*/
prev = &t->t_upimutex;
while (*prev != upimutex) {
prev = &(*prev)->upi_nextowned;
}
*prev = upimutex->upi_nextowned;
upimutex->upi_nextowned = NULL;
ASSERT(t->t_nupinest > 0);
t->t_nupinest--;
}
/*
* Returns true if upimutex is owned. Should be called only when upim points
* to kmem which cannot disappear from underneath.
*/
static int
upi_owned(upimutex_t *upim)
{
return (upim->upi_owner == curthread);
}
/*
* Returns pointer to kernel object (upimutex_t *) if lp is owned.
*/
static struct upimutex *
lwp_upimutex_owned(lwp_mutex_t *lp, uint8_t type)
{
lwpchan_t lwpchan;
upib_t *upibp;
struct upimutex *upimutex;
if (!get_lwpchan(curproc->p_as, (caddr_t)lp, type,
&lwpchan, LWPCHAN_MPPOOL))
return (NULL);
upibp = &UPI_CHAIN(lwpchan);
mutex_enter(&upibp->upib_lock);
upimutex = upi_get(upibp, &lwpchan);
if (upimutex == NULL || upimutex->upi_owner != curthread) {
mutex_exit(&upibp->upib_lock);
return (NULL);
}
mutex_exit(&upibp->upib_lock);
return (upimutex);
}
/*
* Unlocks upimutex, waking up waiters if any. upimutex kmem is freed if
* no lock hand-off occurrs.
*/
static void
upimutex_unlock(struct upimutex *upimutex, uint16_t flag)
{
turnstile_t *ts;
upib_t *upibp;
kthread_t *newowner;
upi_mylist_del(upimutex);
upibp = upimutex->upi_upibp;
mutex_enter(&upibp->upib_lock);
if (upimutex->upi_waiter != 0) { /* if waiters */
ts = turnstile_lookup(upimutex);
if (ts != NULL && !(flag & LOCK_NOTRECOVERABLE)) {
/* hand-off lock to highest prio waiter */
newowner = ts->ts_sleepq[TS_WRITER_Q].sq_first;
upimutex->upi_owner = newowner;
if (ts->ts_waiters == 1)
upimutex->upi_waiter = 0;
turnstile_wakeup(ts, TS_WRITER_Q, 1, newowner);
mutex_exit(&upibp->upib_lock);
return;
} else if (ts != NULL) {
/* LOCK_NOTRECOVERABLE: wakeup all */
turnstile_wakeup(ts, TS_WRITER_Q, ts->ts_waiters, NULL);
} else {
/*
* Misleading w bit. Waiters might have been
* interrupted. No need to clear the w bit (upimutex
* will soon be freed). Re-calculate PI from existing
* waiters.
*/
turnstile_exit(upimutex);
turnstile_pi_recalc();
}
}
/*
* no waiters, or LOCK_NOTRECOVERABLE.
* remove from the bucket chain of upi mutexes.
* de-allocate kernel memory (upimutex).
*/
upi_chain_del(upimutex->upi_upibp, upimutex);
mutex_exit(&upibp->upib_lock);
kmem_free(upimutex, sizeof (upimutex_t));
}
static int
lwp_upimutex_lock(lwp_mutex_t *lp, uint8_t type, int try, lwp_timer_t *lwptp)
{
label_t ljb;
int error = 0;
lwpchan_t lwpchan;
uint16_t flag;
upib_t *upibp;
volatile struct upimutex *upimutex = NULL;
turnstile_t *ts;
uint32_t nupinest;
volatile int upilocked = 0;
if (on_fault(&ljb)) {
if (upilocked)
upimutex_unlock((upimutex_t *)upimutex, 0);
error = EFAULT;
goto out;
}
if (!get_lwpchan(curproc->p_as, (caddr_t)lp, type,
&lwpchan, LWPCHAN_MPPOOL)) {
error = EFAULT;
goto out;
}
upibp = &UPI_CHAIN(lwpchan);
retry:
mutex_enter(&upibp->upib_lock);
upimutex = upi_get(upibp, &lwpchan);
if (upimutex == NULL) {
/* lock available since lwpchan has no upimutex */
upimutex = kmem_zalloc(sizeof (upimutex_t), KM_SLEEP);
upi_chain_add(upibp, (upimutex_t *)upimutex);
upimutex->upi_owner = curthread; /* grab lock */
upimutex->upi_upibp = upibp;
upimutex->upi_vaddr = lp;
upimutex->upi_lwpchan = lwpchan;
mutex_exit(&upibp->upib_lock);
nupinest = upi_mylist_add((upimutex_t *)upimutex);
upilocked = 1;
fuword16_noerr(&lp->mutex_flag, &flag);
if (nupinest > maxnestupimx &&
secpolicy_resource(CRED()) != 0) {
upimutex_unlock((upimutex_t *)upimutex, flag);
error = ENOMEM;
goto out;
}
if (flag & LOCK_NOTRECOVERABLE) {
/*
* Since the setting of LOCK_NOTRECOVERABLE
* was done under the high-level upi mutex,
* in lwp_upimutex_unlock(), this flag needs to
* be checked while holding the upi mutex.
* If set, this thread should return without
* the lock held, and with the right error code.
*/
upimutex_unlock((upimutex_t *)upimutex, flag);
upilocked = 0;
error = ENOTRECOVERABLE;
} else if (flag & (LOCK_OWNERDEAD | LOCK_UNMAPPED)) {
if (flag & LOCK_OWNERDEAD)
error = EOWNERDEAD;
else if (type & USYNC_PROCESS_ROBUST)
error = ELOCKUNMAPPED;
else
error = EOWNERDEAD;
}
goto out;
}
/*
* If a upimutex object exists, it must have an owner.
* This is due to lock hand-off, and release of upimutex when no
* waiters are present at unlock time,
*/
ASSERT(upimutex->upi_owner != NULL);
if (upimutex->upi_owner == curthread) {
/*
* The user wrapper can check if the mutex type is
* ERRORCHECK: if not, it should stall at user-level.
* If so, it should return the error code.
*/
mutex_exit(&upibp->upib_lock);
error = EDEADLK;
goto out;
}
if (try == UPIMUTEX_TRY) {
mutex_exit(&upibp->upib_lock);
error = EBUSY;
goto out;
}
/*
* Block for the lock.
*/
if ((error = lwptp->lwpt_time_error) != 0) {
/*
* The SUSV3 Posix spec is very clear that we
* should get no error from validating the
* timer until we would actually sleep.
*/
mutex_exit(&upibp->upib_lock);
goto out;
}
if (lwptp->lwpt_tsp != NULL) {
/*
* Unlike the protocol for other lwp timedwait operations,
* we must drop t_delay_lock before going to sleep in
* turnstile_block() for a upi mutex.
* See the comments below and in turnstile.c
*/
mutex_enter(&curthread->t_delay_lock);
(void) lwp_timer_enqueue(lwptp);
mutex_exit(&curthread->t_delay_lock);
}
/*
* Now, set the waiter bit and block for the lock in turnstile_block().
* No need to preserve the previous wbit since a lock try is not
* attempted after setting the wait bit. Wait bit is set under
* the upib_lock, which is not released until the turnstile lock
* is acquired. Say, the upimutex is L:
*
* 1. upib_lock is held so the waiter does not have to retry L after
* setting the wait bit: since the owner has to grab the upib_lock
* to unlock L, it will certainly see the wait bit set.
* 2. upib_lock is not released until the turnstile lock is acquired.
* This is the key to preventing a missed wake-up. Otherwise, the
* owner could acquire the upib_lock, and the tc_lock, to call
* turnstile_wakeup(). All this, before the waiter gets tc_lock
* to sleep in turnstile_block(). turnstile_wakeup() will then not
* find this waiter, resulting in the missed wakeup.
* 3. The upib_lock, being a kernel mutex, cannot be released while
* holding the tc_lock (since mutex_exit() could need to acquire
* the same tc_lock)...and so is held when calling turnstile_block().
* The address of upib_lock is passed to turnstile_block() which
* releases it after releasing all turnstile locks, and before going
* to sleep in swtch().
* 4. The waiter value cannot be a count of waiters, because a waiter
* can be interrupted. The interrupt occurs under the tc_lock, at
* which point, the upib_lock cannot be locked, to decrement waiter
* count. So, just treat the waiter state as a bit, not a count.
*/
ts = turnstile_lookup((upimutex_t *)upimutex);
upimutex->upi_waiter = 1;
error = turnstile_block(ts, TS_WRITER_Q, (upimutex_t *)upimutex,
&lwp_sobj_pi_ops, &upibp->upib_lock, lwptp);
/*
* Hand-off implies that we wakeup holding the lock, except when:
* - deadlock is detected
* - lock is not recoverable
* - we got an interrupt or timeout
* If we wake up due to an interrupt or timeout, we may
* or may not be holding the lock due to mutex hand-off.
* Use lwp_upimutex_owned() to check if we do hold the lock.
*/
if (error != 0) {
if ((error == EINTR || error == ETIME) &&
(upimutex = lwp_upimutex_owned(lp, type))) {
/*
* Unlock and return - the re-startable syscall will
* try the lock again if we got EINTR.
*/
(void) upi_mylist_add((upimutex_t *)upimutex);
upimutex_unlock((upimutex_t *)upimutex, 0);
}
/*
* The only other possible error is EDEADLK. If so, upimutex
* is valid, since its owner is deadlocked with curthread.
*/
ASSERT(error == EINTR || error == ETIME ||
(error == EDEADLK && !upi_owned((upimutex_t *)upimutex)));
ASSERT(!lwp_upimutex_owned(lp, type));
goto out;
}
if (lwp_upimutex_owned(lp, type)) {
ASSERT(lwp_upimutex_owned(lp, type) == upimutex);
nupinest = upi_mylist_add((upimutex_t *)upimutex);
upilocked = 1;
}
/*
* Now, need to read the user-level lp->mutex_flag to do the following:
*
* - if lock is held, check if EOWNERDEAD or ELOCKUNMAPPED
* should be returned.
* - if lock isn't held, check if ENOTRECOVERABLE should
* be returned.
*
* Now, either lp->mutex_flag is readable or it's not. If not
* readable, the on_fault path will cause a return with EFAULT
* as it should. If it is readable, the state of the flag
* encodes the robustness state of the lock:
*
* If the upimutex is locked here, the flag's LOCK_OWNERDEAD
* or LOCK_UNMAPPED setting will influence the return code
* appropriately. If the upimutex is not locked here, this
* could be due to a spurious wake-up or a NOTRECOVERABLE
* event. The flag's setting can be used to distinguish
* between these two events.
*/
fuword16_noerr(&lp->mutex_flag, &flag);
if (upilocked) {
/*
* If the thread wakes up from turnstile_block with the lock
* held, the flag could not be set to LOCK_NOTRECOVERABLE,
* since it would not have been handed-off the lock.
* So, no need to check for this case.
*/
if (nupinest > maxnestupimx &&
secpolicy_resource(CRED()) != 0) {
upimutex_unlock((upimutex_t *)upimutex, flag);
upilocked = 0;
error = ENOMEM;
} else if (flag & (LOCK_OWNERDEAD | LOCK_UNMAPPED)) {
if (flag & LOCK_OWNERDEAD)
error = EOWNERDEAD;
else if (type & USYNC_PROCESS_ROBUST)
error = ELOCKUNMAPPED;
else
error = EOWNERDEAD;
}
} else {
/*
* Wake-up without the upimutex held. Either this is a
* spurious wake-up (due to signals, forkall(), whatever), or
* it is a LOCK_NOTRECOVERABLE robustness event. The setting
* of the mutex flag can be used to distinguish between the
* two events.
*/
if (flag & LOCK_NOTRECOVERABLE) {
error = ENOTRECOVERABLE;
} else {
/*
* Here, the flag could be set to LOCK_OWNERDEAD or
* not. In both cases, this is a spurious wakeup,
* since the upi lock is not held, but the thread
* has returned from turnstile_block().
*
* The user flag could be LOCK_OWNERDEAD if, at the
* same time as curthread having been woken up
* spuriously, the owner (say Tdead) has died, marked
* the mutex flag accordingly, and handed off the lock
* to some other waiter (say Tnew). curthread just
* happened to read the flag while Tnew has yet to deal
* with the owner-dead event.
*
* In this event, curthread should retry the lock.
* If Tnew is able to cleanup the lock, curthread
* will eventually get the lock with a zero error code,
* If Tnew is unable to cleanup, its eventual call to
* unlock the lock will result in the mutex flag being
* set to LOCK_NOTRECOVERABLE, and the wake-up of
* all waiters, including curthread, which will then
* eventually return ENOTRECOVERABLE due to the above
* check.
*
* Of course, if the user-flag is not set with
* LOCK_OWNERDEAD, retrying is the thing to do, since
* this is definitely a spurious wakeup.
*/
goto retry;
}
}
out:
no_fault();
return (error);
}
static int
lwp_upimutex_unlock(lwp_mutex_t *lp, uint8_t type)
{
label_t ljb;
int error = 0;
lwpchan_t lwpchan;
uint16_t flag;
upib_t *upibp;
volatile struct upimutex *upimutex = NULL;
volatile int upilocked = 0;
if (on_fault(&ljb)) {
if (upilocked)
upimutex_unlock((upimutex_t *)upimutex, 0);
error = EFAULT;
goto out;
}
if (!get_lwpchan(curproc->p_as, (caddr_t)lp, type,
&lwpchan, LWPCHAN_MPPOOL)) {
error = EFAULT;
goto out;
}
upibp = &UPI_CHAIN(lwpchan);
mutex_enter(&upibp->upib_lock);
upimutex = upi_get(upibp, &lwpchan);
/*
* If the lock is not held, or the owner is not curthread, return
* error. The user-level wrapper can return this error or stall,
* depending on whether mutex is of ERRORCHECK type or not.
*/
if (upimutex == NULL || upimutex->upi_owner != curthread) {
mutex_exit(&upibp->upib_lock);
error = EPERM;
goto out;
}
mutex_exit(&upibp->upib_lock); /* release for user memory access */
upilocked = 1;
fuword16_noerr(&lp->mutex_flag, &flag);
if (flag & (LOCK_OWNERDEAD | LOCK_UNMAPPED)) {
/*
* transition mutex to the LOCK_NOTRECOVERABLE state.
*/
flag &= ~(LOCK_OWNERDEAD | LOCK_UNMAPPED);
flag |= LOCK_NOTRECOVERABLE;
suword16_noerr(&lp->mutex_flag, flag);
}
set_owner_pid(lp, 0, 0);
upimutex_unlock((upimutex_t *)upimutex, flag);
upilocked = 0;
out:
no_fault();
return (error);
}
/*
* Set the owner and ownerpid fields of a user-level mutex. Note, this function
* uses the suword*_noerr routines which must be called between
* on_fault/no_fault. However, this routine itself does not do the
* on_fault/no_fault and it is assumed all the callers will do so instead!
*/
static void
set_owner_pid(lwp_mutex_t *lp, uintptr_t owner, pid_t pid)
{
union {
uint64_t word64;
uint32_t word32[2];
} un;
un.word64 = (uint64_t)owner;
suword32_noerr(&lp->mutex_ownerpid, pid);
#if defined(_LP64)
if (((uintptr_t)lp & (_LONG_LONG_ALIGNMENT - 1)) == 0) { /* aligned */
suword64_noerr(&lp->mutex_owner, un.word64);
return;
}
#endif
/* mutex is unaligned or we are running on a 32-bit kernel */
suword32_noerr((uint32_t *)&lp->mutex_owner, un.word32[0]);
suword32_noerr((uint32_t *)&lp->mutex_owner + 1, un.word32[1]);
}
/*
* Clear the contents of a user-level mutex; return the flags.
* Used only by upi_dead() and lwp_mutex_cleanup(), below.
*/
static uint16_t
lwp_clear_mutex(lwp_mutex_t *lp, uint16_t lockflg)
{
uint16_t flag;
fuword16_noerr(&lp->mutex_flag, &flag);
if ((flag &
(LOCK_OWNERDEAD | LOCK_UNMAPPED | LOCK_NOTRECOVERABLE)) == 0) {
flag |= lockflg;
suword16_noerr(&lp->mutex_flag, flag);
}
set_owner_pid(lp, 0, 0);
suword8_noerr(&lp->mutex_rcount, 0);
return (flag);
}
/*
* Mark user mutex state, corresponding to kernel upimutex,
* as LOCK_UNMAPPED or LOCK_OWNERDEAD, as appropriate
*/
static int
upi_dead(upimutex_t *upip, uint16_t lockflg)
{
label_t ljb;
int error = 0;
lwp_mutex_t *lp;
if (on_fault(&ljb)) {
error = EFAULT;
goto out;
}
lp = upip->upi_vaddr;
(void) lwp_clear_mutex(lp, lockflg);
suword8_noerr(&lp->mutex_lockw, 0);
out:
no_fault();
return (error);
}
/*
* Unlock all upimutexes held by curthread, since curthread is dying.
* For each upimutex, attempt to mark its corresponding user mutex object as
* dead.
*/
void
upimutex_cleanup()
{
kthread_t *t = curthread;
uint16_t lockflg = (ttoproc(t)->p_proc_flag & P_PR_EXEC)?
LOCK_UNMAPPED : LOCK_OWNERDEAD;
struct upimutex *upip;
while ((upip = t->t_upimutex) != NULL) {
if (upi_dead(upip, lockflg) != 0) {
/*
* If the user object associated with this upimutex is
* unmapped, unlock upimutex with the
* LOCK_NOTRECOVERABLE flag, so that all waiters are
* woken up. Since user object is unmapped, it could
* not be marked as dead or notrecoverable.
* The waiters will now all wake up and return
* ENOTRECOVERABLE, since they would find that the lock
* has not been handed-off to them.
* See lwp_upimutex_lock().
*/
upimutex_unlock(upip, LOCK_NOTRECOVERABLE);
} else {
/*
* The user object has been updated as dead.
* Unlock the upimutex: if no waiters, upip kmem will
* be freed. If there is a waiter, the lock will be
* handed off. If exit() is in progress, each existing
* waiter will successively get the lock, as owners
* die, and each new owner will call this routine as
* it dies. The last owner will free kmem, since
* it will find the upimutex has no waiters. So,
* eventually, the kmem is guaranteed to be freed.
*/
upimutex_unlock(upip, 0);
}
/*
* Note that the call to upimutex_unlock() above will delete
* upimutex from the t_upimutexes chain. And so the
* while loop will eventually terminate.
*/
}
}
int
lwp_mutex_timedlock(lwp_mutex_t *lp, timespec_t *tsp, uintptr_t owner)
{
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
proc_t *p = ttoproc(t);
lwp_timer_t lwpt;
caddr_t timedwait;
int error = 0;
int time_error;
clock_t tim = -1;
uchar_t waiters;
volatile int locked = 0;
volatile int watched = 0;
label_t ljb;
volatile uint8_t type = 0;
lwpchan_t lwpchan;
sleepq_head_t *sqh;
uint16_t flag;
int imm_timeout = 0;
if ((caddr_t)lp >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
/*
* Put the lwp in an orderly state for debugging,
* in case we are stopped while sleeping, below.
*/
prstop(PR_REQUESTED, 0);
timedwait = (caddr_t)tsp;
if ((time_error = lwp_timer_copyin(&lwpt, tsp)) == 0 &&
lwpt.lwpt_imm_timeout) {
imm_timeout = 1;
timedwait = NULL;
}
/*
* Although LMS_USER_LOCK implies "asleep waiting for user-mode lock",
* this micro state is really a run state. If the thread indeed blocks,
* this state becomes valid. If not, the state is converted back to
* LMS_SYSTEM. So, it is OK to set the mstate here, instead of just
* when blocking.
*/
(void) new_mstate(t, LMS_USER_LOCK);
if (on_fault(&ljb)) {
if (locked)
lwpchan_unlock(&lwpchan, LWPCHAN_MPPOOL);
error = EFAULT;
goto out;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword8_noerr(&lp->mutex_type, (uint8_t *)&type);
suword8_noerr(&lp->mutex_type, type);
if (UPIMUTEX(type)) {
no_fault();
error = lwp_upimutex_lock(lp, type, UPIMUTEX_BLOCK, &lwpt);
if (error == 0 || error == EOWNERDEAD ||
error == ELOCKUNMAPPED) {
volatile int locked = error != 0;
if (on_fault(&ljb)) {
if (locked != 0)
error = lwp_upimutex_unlock(lp, type);
else
error = EFAULT;
goto upierr;
}
set_owner_pid(lp, owner,
(type & USYNC_PROCESS)? p->p_pid : 0);
no_fault();
}
upierr:
if (tsp && !time_error) /* copyout the residual time left */
error = lwp_timer_copyout(&lwpt, error);
if (error)
return (set_errno(error));
return (0);
}
if (!get_lwpchan(curproc->p_as, (caddr_t)lp, type,
&lwpchan, LWPCHAN_MPPOOL)) {
error = EFAULT;
goto out;
}
lwpchan_lock(&lwpchan, LWPCHAN_MPPOOL);
locked = 1;
if (type & LOCK_ROBUST) {
fuword16_noerr(&lp->mutex_flag, &flag);
if (flag & LOCK_NOTRECOVERABLE) {
lwpchan_unlock(&lwpchan, LWPCHAN_MPPOOL);
error = ENOTRECOVERABLE;
goto out;
}
}
fuword8_noerr(&lp->mutex_waiters, &waiters);
suword8_noerr(&lp->mutex_waiters, 1);
/*
* If watchpoints are set, they need to be restored, since
* atomic accesses of memory such as the call to ulock_try()
* below cannot be watched.
*/
watched = watch_disable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
while (!ulock_try(&lp->mutex_lockw)) {
if (time_error) {
/*
* The SUSV3 Posix spec is very clear that we
* should get no error from validating the
* timer until we would actually sleep.
*/
error = time_error;
break;
}
if (watched) {
watch_enable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
watched = 0;
}
if (timedwait) {
/*
* If we successfully queue the timeout,
* then don't drop t_delay_lock until
* we are on the sleep queue (below).
*/
mutex_enter(&t->t_delay_lock);
if (lwp_timer_enqueue(&lwpt) != 0) {
mutex_exit(&t->t_delay_lock);
imm_timeout = 1;
timedwait = NULL;
}
}
lwp_block(&lwpchan);
/*
* Nothing should happen to cause the lwp to go to
* sleep again until after it returns from swtch().
*/
if (timedwait)
mutex_exit(&t->t_delay_lock);
locked = 0;
lwpchan_unlock(&lwpchan, LWPCHAN_MPPOOL);
if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || imm_timeout)
setrun(t);
swtch();
t->t_flag &= ~T_WAKEABLE;
if (timedwait)
tim = lwp_timer_dequeue(&lwpt);
setallwatch();
if (ISSIG(t, FORREAL) || lwp->lwp_sysabort || MUSTRETURN(p, t))
error = EINTR;
else if (imm_timeout || (timedwait && tim == -1))
error = ETIME;
if (error) {
lwp->lwp_asleep = 0;
lwp->lwp_sysabort = 0;
watched = watch_disable_addr((caddr_t)lp, sizeof (*lp),
S_WRITE);
/*
* Need to re-compute waiters bit. The waiters field in
* the lock is not reliable. Either of two things could
* have occurred: no lwp may have called lwp_release()
* for me but I have woken up due to a signal or
* timeout. In this case, the waiter bit is incorrect
* since it is still set to 1, set above.
* OR an lwp_release() did occur for some other lwp on
* the same lwpchan. In this case, the waiter bit is
* correct. But which event occurred, one can't tell.
* So, recompute.
*/
lwpchan_lock(&lwpchan, LWPCHAN_MPPOOL);
locked = 1;
sqh = lwpsqhash(&lwpchan);
disp_lock_enter(&sqh->sq_lock);
waiters = iswanted(sqh->sq_queue.sq_first, &lwpchan);
disp_lock_exit(&sqh->sq_lock);
break;
}
lwp->lwp_asleep = 0;
watched = watch_disable_addr((caddr_t)lp, sizeof (*lp),
S_WRITE);
lwpchan_lock(&lwpchan, LWPCHAN_MPPOOL);
locked = 1;
fuword8_noerr(&lp->mutex_waiters, &waiters);
suword8_noerr(&lp->mutex_waiters, 1);
if (type & LOCK_ROBUST) {
fuword16_noerr(&lp->mutex_flag, &flag);
if (flag & LOCK_NOTRECOVERABLE) {
error = ENOTRECOVERABLE;
break;
}
}
}
if (t->t_mstate == LMS_USER_LOCK)
(void) new_mstate(t, LMS_SYSTEM);
if (error == 0) {
set_owner_pid(lp, owner, (type & USYNC_PROCESS)? p->p_pid : 0);
if (type & LOCK_ROBUST) {
fuword16_noerr(&lp->mutex_flag, &flag);
if (flag & (LOCK_OWNERDEAD | LOCK_UNMAPPED)) {
if (flag & LOCK_OWNERDEAD)
error = EOWNERDEAD;
else if (type & USYNC_PROCESS_ROBUST)
error = ELOCKUNMAPPED;
else
error = EOWNERDEAD;
}
}
}
suword8_noerr(&lp->mutex_waiters, waiters);
locked = 0;
lwpchan_unlock(&lwpchan, LWPCHAN_MPPOOL);
out:
no_fault();
if (watched)
watch_enable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
if (tsp && !time_error) /* copyout the residual time left */
error = lwp_timer_copyout(&lwpt, error);
if (error)
return (set_errno(error));
return (0);
}
static int
iswanted(kthread_t *t, lwpchan_t *lwpchan)
{
/*
* The caller holds the dispatcher lock on the sleep queue.
*/
while (t != NULL) {
if (t->t_lwpchan.lc_wchan0 == lwpchan->lc_wchan0 &&
t->t_lwpchan.lc_wchan == lwpchan->lc_wchan)
return (1);
t = t->t_link;
}
return (0);
}
/*
* Return the highest priority thread sleeping on this lwpchan.
*/
static kthread_t *
lwp_queue_waiter(lwpchan_t *lwpchan)
{
sleepq_head_t *sqh;
kthread_t *tp;
sqh = lwpsqhash(lwpchan);
disp_lock_enter(&sqh->sq_lock); /* lock the sleep queue */
for (tp = sqh->sq_queue.sq_first; tp != NULL; tp = tp->t_link) {
if (tp->t_lwpchan.lc_wchan0 == lwpchan->lc_wchan0 &&
tp->t_lwpchan.lc_wchan == lwpchan->lc_wchan)
break;
}
disp_lock_exit(&sqh->sq_lock);
return (tp);
}
static int
lwp_release(lwpchan_t *lwpchan, uchar_t *waiters, int sync_type)
{
sleepq_head_t *sqh;
kthread_t *tp;
kthread_t **tpp;
sqh = lwpsqhash(lwpchan);
disp_lock_enter(&sqh->sq_lock); /* lock the sleep queue */
tpp = &sqh->sq_queue.sq_first;
while ((tp = *tpp) != NULL) {
if (tp->t_lwpchan.lc_wchan0 == lwpchan->lc_wchan0 &&
tp->t_lwpchan.lc_wchan == lwpchan->lc_wchan) {
/*
* The following is typically false. It could be true
* only if lwp_release() is called from
* lwp_mutex_wakeup() after reading the waiters field
* from memory in which the lwp lock used to be, but has
* since been re-used to hold a lwp cv or lwp semaphore.
* The thread "tp" found to match the lwp lock's wchan
* is actually sleeping for the cv or semaphore which
* now has the same wchan. In this case, lwp_release()
* should return failure.
*/
if (sync_type != (tp->t_flag & T_WAITCVSEM)) {
ASSERT(sync_type == 0);
/*
* assert that this can happen only for mutexes
* i.e. sync_type == 0, for correctly written
* user programs.
*/
disp_lock_exit(&sqh->sq_lock);
return (0);
}
*waiters = iswanted(tp->t_link, lwpchan);
sleepq_unlink(tpp, tp);
DTRACE_SCHED1(wakeup, kthread_t *, tp);
tp->t_wchan0 = NULL;
tp->t_wchan = NULL;
tp->t_sobj_ops = NULL;
tp->t_release = 1;
THREAD_TRANSITION(tp); /* drops sleepq lock */
CL_WAKEUP(tp);
thread_unlock(tp); /* drop run queue lock */
return (1);
}
tpp = &tp->t_link;
}
*waiters = 0;
disp_lock_exit(&sqh->sq_lock);
return (0);
}
static void
lwp_release_all(lwpchan_t *lwpchan)
{
sleepq_head_t *sqh;
kthread_t *tp;
kthread_t **tpp;
sqh = lwpsqhash(lwpchan);
disp_lock_enter(&sqh->sq_lock); /* lock sleep q queue */
tpp = &sqh->sq_queue.sq_first;
while ((tp = *tpp) != NULL) {
if (tp->t_lwpchan.lc_wchan0 == lwpchan->lc_wchan0 &&
tp->t_lwpchan.lc_wchan == lwpchan->lc_wchan) {
sleepq_unlink(tpp, tp);
DTRACE_SCHED1(wakeup, kthread_t *, tp);
tp->t_wchan0 = NULL;
tp->t_wchan = NULL;
tp->t_sobj_ops = NULL;
CL_WAKEUP(tp);
thread_unlock_high(tp); /* release run queue lock */
} else {
tpp = &tp->t_link;
}
}
disp_lock_exit(&sqh->sq_lock); /* drop sleep q lock */
}
/*
* unblock a lwp that is trying to acquire this mutex. the blocked
* lwp resumes and retries to acquire the lock.
*/
int
lwp_mutex_wakeup(lwp_mutex_t *lp, int release_all)
{
proc_t *p = ttoproc(curthread);
lwpchan_t lwpchan;
uchar_t waiters;
volatile int locked = 0;
volatile int watched = 0;
volatile uint8_t type = 0;
label_t ljb;
int error = 0;
if ((caddr_t)lp >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
watched = watch_disable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
if (on_fault(&ljb)) {
if (locked)
lwpchan_unlock(&lwpchan, LWPCHAN_MPPOOL);
error = EFAULT;
goto out;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword8_noerr(&lp->mutex_type, (uint8_t *)&type);
suword8_noerr(&lp->mutex_type, type);
if (!get_lwpchan(curproc->p_as, (caddr_t)lp, type,
&lwpchan, LWPCHAN_MPPOOL)) {
error = EFAULT;
goto out;
}
lwpchan_lock(&lwpchan, LWPCHAN_MPPOOL);
locked = 1;
/*
* Always wake up an lwp (if any) waiting on lwpchan. The woken lwp will
* re-try the lock in lwp_mutex_timedlock(). The call to lwp_release()
* may fail. If it fails, do not write into the waiter bit.
* The call to lwp_release() might fail due to one of three reasons:
*
* 1. due to the thread which set the waiter bit not actually
* sleeping since it got the lock on the re-try. The waiter
* bit will then be correctly updated by that thread. This
* window may be closed by reading the wait bit again here
* and not calling lwp_release() at all if it is zero.
* 2. the thread which set the waiter bit and went to sleep
* was woken up by a signal. This time, the waiter recomputes
* the wait bit in the return with EINTR code.
* 3. the waiter bit read by lwp_mutex_wakeup() was in
* memory that has been re-used after the lock was dropped.
* In this case, writing into the waiter bit would cause data
* corruption.
*/
if (release_all)
lwp_release_all(&lwpchan);
else if (lwp_release(&lwpchan, &waiters, 0))
suword8_noerr(&lp->mutex_waiters, waiters);
lwpchan_unlock(&lwpchan, LWPCHAN_MPPOOL);
out:
no_fault();
if (watched)
watch_enable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
if (error)
return (set_errno(error));
return (0);
}
/*
* lwp_cond_wait() has four arguments, a pointer to a condition variable,
* a pointer to a mutex, a pointer to a timespec for a timed wait and
* a flag telling the kernel whether or not to honor the kernel/user
* schedctl parking protocol (see schedctl_is_park() in schedctl.c).
* The kernel puts the lwp to sleep on a unique pair of caddr_t's called an
* lwpchan, returned by get_lwpchan(). If the timespec pointer is non-NULL,
* it is used an an in/out parameter. On entry, it contains the relative
* time until timeout. On exit, we copyout the residual time left to it.
*/
int
lwp_cond_wait(lwp_cond_t *cv, lwp_mutex_t *mp, timespec_t *tsp, int check_park)
{
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
proc_t *p = ttoproc(t);
lwp_timer_t lwpt;
lwpchan_t cv_lwpchan;
lwpchan_t m_lwpchan;
caddr_t timedwait;
volatile uint16_t type = 0;
volatile uint8_t mtype = 0;
uchar_t waiters;
volatile int error;
clock_t tim = -1;
volatile int locked = 0;
volatile int m_locked = 0;
volatile int cvwatched = 0;
volatile int mpwatched = 0;
label_t ljb;
volatile int no_lwpchan = 1;
int imm_timeout = 0;
int imm_unpark = 0;
if ((caddr_t)cv >= p->p_as->a_userlimit ||
(caddr_t)mp >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
/*
* Put the lwp in an orderly state for debugging,
* in case we are stopped while sleeping, below.
*/
prstop(PR_REQUESTED, 0);
timedwait = (caddr_t)tsp;
if ((error = lwp_timer_copyin(&lwpt, tsp)) != 0)
return (set_errno(error));
if (lwpt.lwpt_imm_timeout) {
imm_timeout = 1;
timedwait = NULL;
}
(void) new_mstate(t, LMS_USER_LOCK);
if (on_fault(&ljb)) {
if (no_lwpchan) {
error = EFAULT;
goto out;
}
if (m_locked) {
m_locked = 0;
lwpchan_unlock(&m_lwpchan, LWPCHAN_MPPOOL);
}
if (locked) {
locked = 0;
lwpchan_unlock(&cv_lwpchan, LWPCHAN_CVPOOL);
}
/*
* set up another on_fault() for a possible fault
* on the user lock accessed at "efault"
*/
if (on_fault(&ljb)) {
if (m_locked) {
m_locked = 0;
lwpchan_unlock(&m_lwpchan, LWPCHAN_MPPOOL);
}
goto out;
}
error = EFAULT;
goto efault;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword8_noerr(&mp->mutex_type, (uint8_t *)&mtype);
suword8_noerr(&mp->mutex_type, mtype);
if (UPIMUTEX(mtype) == 0) {
/* convert user level mutex, "mp", to a unique lwpchan */
/* check if mtype is ok to use below, instead of type from cv */
if (!get_lwpchan(p->p_as, (caddr_t)mp, mtype,
&m_lwpchan, LWPCHAN_MPPOOL)) {
error = EFAULT;
goto out;
}
}
fuword16_noerr(&cv->cond_type, (uint16_t *)&type);
suword16_noerr(&cv->cond_type, type);
/* convert user level condition variable, "cv", to a unique lwpchan */
if (!get_lwpchan(p->p_as, (caddr_t)cv, type,
&cv_lwpchan, LWPCHAN_CVPOOL)) {
error = EFAULT;
goto out;
}
no_lwpchan = 0;
cvwatched = watch_disable_addr((caddr_t)cv, sizeof (*cv), S_WRITE);
if (UPIMUTEX(mtype) == 0)
mpwatched = watch_disable_addr((caddr_t)mp, sizeof (*mp),
S_WRITE);
/*
* lwpchan_lock ensures that the calling lwp is put to sleep atomically
* with respect to a possible wakeup which is a result of either
* an lwp_cond_signal() or an lwp_cond_broadcast().
*
* What's misleading, is that the lwp is put to sleep after the
* condition variable's mutex is released. This is OK as long as
* the release operation is also done while holding lwpchan_lock.
* The lwp is then put to sleep when the possibility of pagefaulting
* or sleeping is completely eliminated.
*/
lwpchan_lock(&cv_lwpchan, LWPCHAN_CVPOOL);
locked = 1;
if (UPIMUTEX(mtype) == 0) {
lwpchan_lock(&m_lwpchan, LWPCHAN_MPPOOL);
m_locked = 1;
suword8_noerr(&cv->cond_waiters_kernel, 1);
/*
* unlock the condition variable's mutex. (pagefaults are
* possible here.)
*/
set_owner_pid(mp, 0, 0);
ulock_clear(&mp->mutex_lockw);
fuword8_noerr(&mp->mutex_waiters, &waiters);
if (waiters != 0) {
/*
* Given the locking of lwpchan_lock around the release
* of the mutex and checking for waiters, the following
* call to lwp_release() can fail ONLY if the lock
* acquirer is interrupted after setting the waiter bit,
* calling lwp_block() and releasing lwpchan_lock.
* In this case, it could get pulled off the lwp sleep
* q (via setrun()) before the following call to
* lwp_release() occurs. In this case, the lock
* requestor will update the waiter bit correctly by
* re-evaluating it.
*/
if (lwp_release(&m_lwpchan, &waiters, 0))
suword8_noerr(&mp->mutex_waiters, waiters);
}
m_locked = 0;
lwpchan_unlock(&m_lwpchan, LWPCHAN_MPPOOL);
} else {
suword8_noerr(&cv->cond_waiters_kernel, 1);
error = lwp_upimutex_unlock(mp, mtype);
if (error) { /* if the upimutex unlock failed */
locked = 0;
lwpchan_unlock(&cv_lwpchan, LWPCHAN_CVPOOL);
goto out;
}
}
no_fault();
if (mpwatched) {
watch_enable_addr((caddr_t)mp, sizeof (*mp), S_WRITE);
mpwatched = 0;
}
if (cvwatched) {
watch_enable_addr((caddr_t)cv, sizeof (*cv), S_WRITE);
cvwatched = 0;
}
if (check_park && (!schedctl_is_park() || t->t_unpark)) {
/*
* We received a signal at user-level before calling here
* or another thread wants us to return immediately
* with EINTR. See lwp_unpark().
*/
imm_unpark = 1;
t->t_unpark = 0;
timedwait = NULL;
} else if (timedwait) {
/*
* If we successfully queue the timeout,
* then don't drop t_delay_lock until
* we are on the sleep queue (below).
*/
mutex_enter(&t->t_delay_lock);
if (lwp_timer_enqueue(&lwpt) != 0) {
mutex_exit(&t->t_delay_lock);
imm_timeout = 1;
timedwait = NULL;
}
}
t->t_flag |= T_WAITCVSEM;
lwp_block(&cv_lwpchan);
/*
* Nothing should happen to cause the lwp to go to sleep
* until after it returns from swtch().
*/
if (timedwait)
mutex_exit(&t->t_delay_lock);
locked = 0;
lwpchan_unlock(&cv_lwpchan, LWPCHAN_CVPOOL);
if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) ||
(imm_timeout | imm_unpark))
setrun(t);
swtch();
t->t_flag &= ~(T_WAITCVSEM | T_WAKEABLE);
if (timedwait)
tim = lwp_timer_dequeue(&lwpt);
if (ISSIG(t, FORREAL) || lwp->lwp_sysabort ||
MUSTRETURN(p, t) || imm_unpark)
error = EINTR;
else if (imm_timeout || (timedwait && tim == -1))
error = ETIME;
lwp->lwp_asleep = 0;
lwp->lwp_sysabort = 0;
setallwatch();
if (t->t_mstate == LMS_USER_LOCK)
(void) new_mstate(t, LMS_SYSTEM);
if (tsp && check_park) /* copyout the residual time left */
error = lwp_timer_copyout(&lwpt, error);
/* the mutex is reacquired by the caller on return to user level */
if (error) {
/*
* If we were concurrently lwp_cond_signal()d and we
* received a UNIX signal or got a timeout, then perform
* another lwp_cond_signal() to avoid consuming the wakeup.
*/
if (t->t_release)
(void) lwp_cond_signal(cv);
return (set_errno(error));
}
return (0);
efault:
/*
* make sure that the user level lock is dropped before
* returning to caller, since the caller always re-acquires it.
*/
if (UPIMUTEX(mtype) == 0) {
lwpchan_lock(&m_lwpchan, LWPCHAN_MPPOOL);
m_locked = 1;
set_owner_pid(mp, 0, 0);
ulock_clear(&mp->mutex_lockw);
fuword8_noerr(&mp->mutex_waiters, &waiters);
if (waiters != 0) {
/*
* See comment above on lock clearing and lwp_release()
* success/failure.
*/
if (lwp_release(&m_lwpchan, &waiters, 0))
suword8_noerr(&mp->mutex_waiters, waiters);
}
m_locked = 0;
lwpchan_unlock(&m_lwpchan, LWPCHAN_MPPOOL);
} else {
(void) lwp_upimutex_unlock(mp, mtype);
}
out:
no_fault();
if (mpwatched)
watch_enable_addr((caddr_t)mp, sizeof (*mp), S_WRITE);
if (cvwatched)
watch_enable_addr((caddr_t)cv, sizeof (*cv), S_WRITE);
if (t->t_mstate == LMS_USER_LOCK)
(void) new_mstate(t, LMS_SYSTEM);
return (set_errno(error));
}
/*
* wakeup one lwp that's blocked on this condition variable.
*/
int
lwp_cond_signal(lwp_cond_t *cv)
{
proc_t *p = ttoproc(curthread);
lwpchan_t lwpchan;
uchar_t waiters;
volatile uint16_t type = 0;
volatile int locked = 0;
volatile int watched = 0;
label_t ljb;
int error = 0;
if ((caddr_t)cv >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
watched = watch_disable_addr((caddr_t)cv, sizeof (*cv), S_WRITE);
if (on_fault(&ljb)) {
if (locked)
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
error = EFAULT;
goto out;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword16_noerr(&cv->cond_type, (uint16_t *)&type);
suword16_noerr(&cv->cond_type, type);
if (!get_lwpchan(curproc->p_as, (caddr_t)cv, type,
&lwpchan, LWPCHAN_CVPOOL)) {
error = EFAULT;
goto out;
}
lwpchan_lock(&lwpchan, LWPCHAN_CVPOOL);
locked = 1;
fuword8_noerr(&cv->cond_waiters_kernel, &waiters);
if (waiters != 0) {
/*
* The following call to lwp_release() might fail but it is
* OK to write into the waiters bit below, since the memory
* could not have been re-used or unmapped (for correctly
* written user programs) as in the case of lwp_mutex_wakeup().
* For an incorrect program, we should not care about data
* corruption since this is just one instance of other places
* where corruption can occur for such a program. Of course
* if the memory is unmapped, normal fault recovery occurs.
*/
(void) lwp_release(&lwpchan, &waiters, T_WAITCVSEM);
suword8_noerr(&cv->cond_waiters_kernel, waiters);
}
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
out:
no_fault();
if (watched)
watch_enable_addr((caddr_t)cv, sizeof (*cv), S_WRITE);
if (error)
return (set_errno(error));
return (0);
}
/*
* wakeup every lwp that's blocked on this condition variable.
*/
int
lwp_cond_broadcast(lwp_cond_t *cv)
{
proc_t *p = ttoproc(curthread);
lwpchan_t lwpchan;
volatile uint16_t type = 0;
volatile int locked = 0;
volatile int watched = 0;
label_t ljb;
uchar_t waiters;
int error = 0;
if ((caddr_t)cv >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
watched = watch_disable_addr((caddr_t)cv, sizeof (*cv), S_WRITE);
if (on_fault(&ljb)) {
if (locked)
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
error = EFAULT;
goto out;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword16_noerr(&cv->cond_type, (uint16_t *)&type);
suword16_noerr(&cv->cond_type, type);
if (!get_lwpchan(curproc->p_as, (caddr_t)cv, type,
&lwpchan, LWPCHAN_CVPOOL)) {
error = EFAULT;
goto out;
}
lwpchan_lock(&lwpchan, LWPCHAN_CVPOOL);
locked = 1;
fuword8_noerr(&cv->cond_waiters_kernel, &waiters);
if (waiters != 0) {
lwp_release_all(&lwpchan);
suword8_noerr(&cv->cond_waiters_kernel, 0);
}
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
out:
no_fault();
if (watched)
watch_enable_addr((caddr_t)cv, sizeof (*cv), S_WRITE);
if (error)
return (set_errno(error));
return (0);
}
int
lwp_sema_trywait(lwp_sema_t *sp)
{
kthread_t *t = curthread;
proc_t *p = ttoproc(t);
label_t ljb;
volatile int locked = 0;
volatile int watched = 0;
volatile uint16_t type = 0;
int count;
lwpchan_t lwpchan;
uchar_t waiters;
int error = 0;
if ((caddr_t)sp >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
watched = watch_disable_addr((caddr_t)sp, sizeof (*sp), S_WRITE);
if (on_fault(&ljb)) {
if (locked)
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
error = EFAULT;
goto out;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword16_noerr((void *)&sp->sema_type, (uint16_t *)&type);
suword16_noerr((void *)&sp->sema_type, type);
if (!get_lwpchan(p->p_as, (caddr_t)sp, type,
&lwpchan, LWPCHAN_CVPOOL)) {
error = EFAULT;
goto out;
}
lwpchan_lock(&lwpchan, LWPCHAN_CVPOOL);
locked = 1;
fuword32_noerr((void *)&sp->sema_count, (uint32_t *)&count);
if (count == 0)
error = EBUSY;
else
suword32_noerr((void *)&sp->sema_count, --count);
if (count != 0) {
fuword8_noerr(&sp->sema_waiters, &waiters);
if (waiters != 0) {
(void) lwp_release(&lwpchan, &waiters, T_WAITCVSEM);
suword8_noerr(&sp->sema_waiters, waiters);
}
}
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
out:
no_fault();
if (watched)
watch_enable_addr((caddr_t)sp, sizeof (*sp), S_WRITE);
if (error)
return (set_errno(error));
return (0);
}
/*
* See lwp_cond_wait(), above, for an explanation of the 'check_park' argument.
*/
int
lwp_sema_timedwait(lwp_sema_t *sp, timespec_t *tsp, int check_park)
{
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
proc_t *p = ttoproc(t);
lwp_timer_t lwpt;
caddr_t timedwait;
clock_t tim = -1;
label_t ljb;
volatile int locked = 0;
volatile int watched = 0;
volatile uint16_t type = 0;
int count;
lwpchan_t lwpchan;
uchar_t waiters;
int error = 0;
int time_error;
int imm_timeout = 0;
int imm_unpark = 0;
if ((caddr_t)sp >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
/*
* Put the lwp in an orderly state for debugging,
* in case we are stopped while sleeping, below.
*/
prstop(PR_REQUESTED, 0);
timedwait = (caddr_t)tsp;
if ((time_error = lwp_timer_copyin(&lwpt, tsp)) == 0 &&
lwpt.lwpt_imm_timeout) {
imm_timeout = 1;
timedwait = NULL;
}
watched = watch_disable_addr((caddr_t)sp, sizeof (*sp), S_WRITE);
if (on_fault(&ljb)) {
if (locked)
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
error = EFAULT;
goto out;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword16_noerr((void *)&sp->sema_type, (uint16_t *)&type);
suword16_noerr((void *)&sp->sema_type, type);
if (!get_lwpchan(p->p_as, (caddr_t)sp, type,
&lwpchan, LWPCHAN_CVPOOL)) {
error = EFAULT;
goto out;
}
lwpchan_lock(&lwpchan, LWPCHAN_CVPOOL);
locked = 1;
fuword32_noerr((void *)&sp->sema_count, (uint32_t *)&count);
while (error == 0 && count == 0) {
if (time_error) {
/*
* The SUSV3 Posix spec is very clear that we
* should get no error from validating the
* timer until we would actually sleep.
*/
error = time_error;
break;
}
suword8_noerr(&sp->sema_waiters, 1);
if (watched)
watch_enable_addr((caddr_t)sp, sizeof (*sp), S_WRITE);
if (check_park && (!schedctl_is_park() || t->t_unpark)) {
/*
* We received a signal at user-level before calling
* here or another thread wants us to return
* immediately with EINTR. See lwp_unpark().
*/
imm_unpark = 1;
t->t_unpark = 0;
timedwait = NULL;
} else if (timedwait) {
/*
* If we successfully queue the timeout,
* then don't drop t_delay_lock until
* we are on the sleep queue (below).
*/
mutex_enter(&t->t_delay_lock);
if (lwp_timer_enqueue(&lwpt) != 0) {
mutex_exit(&t->t_delay_lock);
imm_timeout = 1;
timedwait = NULL;
}
}
t->t_flag |= T_WAITCVSEM;
lwp_block(&lwpchan);
/*
* Nothing should happen to cause the lwp to sleep
* again until after it returns from swtch().
*/
if (timedwait)
mutex_exit(&t->t_delay_lock);
locked = 0;
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) ||
(imm_timeout | imm_unpark))
setrun(t);
swtch();
t->t_flag &= ~(T_WAITCVSEM | T_WAKEABLE);
if (timedwait)
tim = lwp_timer_dequeue(&lwpt);
setallwatch();
if (ISSIG(t, FORREAL) || lwp->lwp_sysabort ||
MUSTRETURN(p, t) || imm_unpark)
error = EINTR;
else if (imm_timeout || (timedwait && tim == -1))
error = ETIME;
lwp->lwp_asleep = 0;
lwp->lwp_sysabort = 0;
watched = watch_disable_addr((caddr_t)sp,
sizeof (*sp), S_WRITE);
lwpchan_lock(&lwpchan, LWPCHAN_CVPOOL);
locked = 1;
fuword32_noerr((void *)&sp->sema_count, (uint32_t *)&count);
}
if (error == 0)
suword32_noerr((void *)&sp->sema_count, --count);
if (count != 0) {
(void) lwp_release(&lwpchan, &waiters, T_WAITCVSEM);
suword8_noerr(&sp->sema_waiters, waiters);
}
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
out:
no_fault();
if (watched)
watch_enable_addr((caddr_t)sp, sizeof (*sp), S_WRITE);
if (tsp && check_park && !time_error)
error = lwp_timer_copyout(&lwpt, error);
if (error)
return (set_errno(error));
return (0);
}
int
lwp_sema_post(lwp_sema_t *sp)
{
proc_t *p = ttoproc(curthread);
label_t ljb;
volatile int locked = 0;
volatile int watched = 0;
volatile uint16_t type = 0;
int count;
lwpchan_t lwpchan;
uchar_t waiters;
int error = 0;
if ((caddr_t)sp >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
watched = watch_disable_addr((caddr_t)sp, sizeof (*sp), S_WRITE);
if (on_fault(&ljb)) {
if (locked)
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
error = EFAULT;
goto out;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword16_noerr(&sp->sema_type, (uint16_t *)&type);
suword16_noerr(&sp->sema_type, type);
if (!get_lwpchan(curproc->p_as, (caddr_t)sp, type,
&lwpchan, LWPCHAN_CVPOOL)) {
error = EFAULT;
goto out;
}
lwpchan_lock(&lwpchan, LWPCHAN_CVPOOL);
locked = 1;
fuword32_noerr(&sp->sema_count, (uint32_t *)&count);
if (count == _SEM_VALUE_MAX)
error = EOVERFLOW;
else
suword32_noerr(&sp->sema_count, ++count);
if (count == 1) {
fuword8_noerr(&sp->sema_waiters, &waiters);
if (waiters) {
(void) lwp_release(&lwpchan, &waiters, T_WAITCVSEM);
suword8_noerr(&sp->sema_waiters, waiters);
}
}
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
out:
no_fault();
if (watched)
watch_enable_addr((caddr_t)sp, sizeof (*sp), S_WRITE);
if (error)
return (set_errno(error));
return (0);
}
#define TRW_WANT_WRITE 0x1
#define TRW_LOCK_GRANTED 0x2
#define READ_LOCK 0
#define WRITE_LOCK 1
#define TRY_FLAG 0x10
#define READ_LOCK_TRY (READ_LOCK | TRY_FLAG)
#define WRITE_LOCK_TRY (WRITE_LOCK | TRY_FLAG)
/*
* Release one writer or one or more readers. Compute the rwstate word to
* reflect the new state of the queue. For a safe hand-off we copy the new
* rwstate value back to userland before we wake any of the new lock holders.
*
* Note that sleepq_insert() implements a prioritized FIFO (with writers
* being given precedence over readers of the same priority).
*
* If the first thread is a reader we scan the queue releasing all readers
* until we hit a writer or the end of the queue. If the first thread is a
* writer we still need to check for another writer.
*/
void
lwp_rwlock_release(lwpchan_t *lwpchan, lwp_rwlock_t *rw)
{
sleepq_head_t *sqh;
kthread_t *tp;
kthread_t **tpp;
kthread_t *tpnext;
kthread_t *wakelist = NULL;
uint32_t rwstate = 0;
int wcount = 0;
int rcount = 0;
sqh = lwpsqhash(lwpchan);
disp_lock_enter(&sqh->sq_lock);
tpp = &sqh->sq_queue.sq_first;
while ((tp = *tpp) != NULL) {
if (tp->t_lwpchan.lc_wchan0 == lwpchan->lc_wchan0 &&
tp->t_lwpchan.lc_wchan == lwpchan->lc_wchan) {
if (tp->t_writer & TRW_WANT_WRITE) {
if ((wcount++ == 0) && (rcount == 0)) {
rwstate |= URW_WRITE_LOCKED;
/* Just one writer to wake. */
sleepq_unlink(tpp, tp);
wakelist = tp;
/* tpp already set for next thread. */
continue;
} else {
rwstate |= URW_HAS_WAITERS;
/* We need look no further. */
break;
}
} else {
rcount++;
if (wcount == 0) {
rwstate++;
/* Add reader to wake list. */
sleepq_unlink(tpp, tp);
tp->t_link = wakelist;
wakelist = tp;
/* tpp already set for next thread. */
continue;
} else {
rwstate |= URW_HAS_WAITERS;
/* We need look no further. */
break;
}
}
}
tpp = &tp->t_link;
}
/* Copy the new rwstate back to userland. */
suword32_noerr(&rw->rwlock_readers, rwstate);
/* Wake the new lock holder(s) up. */
tp = wakelist;
while (tp != NULL) {
DTRACE_SCHED1(wakeup, kthread_t *, tp);
tp->t_wchan0 = NULL;
tp->t_wchan = NULL;
tp->t_sobj_ops = NULL;
tp->t_writer |= TRW_LOCK_GRANTED;
tpnext = tp->t_link;
tp->t_link = NULL;
CL_WAKEUP(tp);
thread_unlock_high(tp);
tp = tpnext;
}
disp_lock_exit(&sqh->sq_lock);
}
/*
* We enter here holding the user-level mutex, which we must release before
* returning or blocking. Based on lwp_cond_wait().
*/
static int
lwp_rwlock_lock(lwp_rwlock_t *rw, timespec_t *tsp, int rd_wr)
{
lwp_mutex_t *mp = NULL;
kthread_t *t = curthread;
kthread_t *tp;
klwp_t *lwp = ttolwp(t);
proc_t *p = ttoproc(t);
lwp_timer_t lwpt;
lwpchan_t lwpchan;
lwpchan_t mlwpchan;
caddr_t timedwait;
volatile uint16_t type = 0;
volatile uint8_t mtype = 0;
uchar_t mwaiters;
volatile int error = 0;
int time_error;
clock_t tim = -1;
volatile int locked = 0;
volatile int mlocked = 0;
volatile int watched = 0;
volatile int mwatched = 0;
label_t ljb;
volatile int no_lwpchan = 1;
int imm_timeout = 0;
int try_flag;
uint32_t rwstate;
int acquired = 0;
/* We only check rw because the mutex is included in it. */
if ((caddr_t)rw >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
/*
* Put the lwp in an orderly state for debugging,
* in case we are stopped while sleeping, below.
*/
prstop(PR_REQUESTED, 0);
/* We must only report this error if we are about to sleep (later). */
timedwait = (caddr_t)tsp;
if ((time_error = lwp_timer_copyin(&lwpt, tsp)) == 0 &&
lwpt.lwpt_imm_timeout) {
imm_timeout = 1;
timedwait = NULL;
}
(void) new_mstate(t, LMS_USER_LOCK);
if (on_fault(&ljb)) {
if (no_lwpchan) {
error = EFAULT;
goto out_nodrop;
}
if (mlocked) {
mlocked = 0;
lwpchan_unlock(&mlwpchan, LWPCHAN_MPPOOL);
}
if (locked) {
locked = 0;
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
}
/*
* Set up another on_fault() for a possible fault
* on the user lock accessed at "out_drop".
*/
if (on_fault(&ljb)) {
if (mlocked) {
mlocked = 0;
lwpchan_unlock(&mlwpchan, LWPCHAN_MPPOOL);
}
error = EFAULT;
goto out_nodrop;
}
error = EFAULT;
goto out_nodrop;
}
/* Process rd_wr (including sanity check). */
try_flag = (rd_wr & TRY_FLAG);
rd_wr &= ~TRY_FLAG;
if ((rd_wr != READ_LOCK) && (rd_wr != WRITE_LOCK)) {
error = EINVAL;
goto out_nodrop;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
mp = &rw->mutex;
fuword8_noerr(&mp->mutex_type, (uint8_t *)&mtype);
fuword16_noerr(&rw->rwlock_type, (uint16_t *)&type);
suword8_noerr(&mp->mutex_type, mtype);
suword16_noerr(&rw->rwlock_type, type);
/* We can only continue for simple USYNC_PROCESS locks. */
if ((mtype != USYNC_PROCESS) || (type != USYNC_PROCESS)) {
error = EINVAL;
goto out_nodrop;
}
/* Convert user level mutex, "mp", to a unique lwpchan. */
if (!get_lwpchan(p->p_as, (caddr_t)mp, mtype,
&mlwpchan, LWPCHAN_MPPOOL)) {
error = EFAULT;
goto out_nodrop;
}
/* Convert user level rwlock, "rw", to a unique lwpchan. */
if (!get_lwpchan(p->p_as, (caddr_t)rw, type,
&lwpchan, LWPCHAN_CVPOOL)) {
error = EFAULT;
goto out_nodrop;
}
no_lwpchan = 0;
watched = watch_disable_addr((caddr_t)rw, sizeof (*rw), S_WRITE);
mwatched = watch_disable_addr((caddr_t)mp, sizeof (*mp), S_WRITE);
/*
* lwpchan_lock() ensures that the calling LWP is put to sleep
* atomically with respect to a possible wakeup which is a result
* of lwp_rwlock_unlock().
*
* What's misleading is that the LWP is put to sleep after the
* rwlock's mutex is released. This is OK as long as the release
* operation is also done while holding mlwpchan. The LWP is then
* put to sleep when the possibility of pagefaulting or sleeping
* has been completely eliminated.
*/
lwpchan_lock(&lwpchan, LWPCHAN_CVPOOL);
locked = 1;
lwpchan_lock(&mlwpchan, LWPCHAN_MPPOOL);
mlocked = 1;
/*
* Fetch the current rwlock state.
*
* The possibility of spurious wake-ups or killed waiters means
* rwstate's URW_HAS_WAITERS bit may indicate false positives.
* We only fix these if they are important to us.
*
* Although various error states can be observed here (e.g. the lock
* is not held, but there are waiters) we assume these are applicaton
* errors and so we take no corrective action.
*/
fuword32_noerr(&rw->rwlock_readers, &rwstate);
/*
* We cannot legitimately get here from user-level
* without URW_HAS_WAITERS being set.
* Set it now to guard against user-level error.
*/
rwstate |= URW_HAS_WAITERS;
/*
* We can try only if the lock isn't held by a writer.
*/
if (!(rwstate & URW_WRITE_LOCKED)) {
tp = lwp_queue_waiter(&lwpchan);
if (tp == NULL) {
/*
* Hmmm, rwstate indicates waiters but there are
* none queued. This could just be the result of a
* spurious wakeup, so let's ignore it.
*
* We now have a chance to acquire the lock
* uncontended, but this is the last chance for
* a writer to acquire the lock without blocking.
*/
if (rd_wr == READ_LOCK) {
rwstate++;
acquired = 1;
} else if ((rwstate & URW_READERS_MASK) == 0) {
rwstate |= URW_WRITE_LOCKED;
acquired = 1;
}
} else if (rd_wr == READ_LOCK) {
/*
* This is the last chance for a reader to acquire
* the lock now, but it can only do so if there is
* no writer of equal or greater priority at the
* head of the queue .
*
* It is also just possible that there is a reader
* at the head of the queue. This may be the result
* of a spurious wakeup or an application failure.
* In this case we only acquire the lock if we have
* equal or greater priority. It is not our job to
* release spurious waiters.
*/
pri_t our_pri = DISP_PRIO(t);
pri_t his_pri = DISP_PRIO(tp);
if ((our_pri > his_pri) || ((our_pri == his_pri) &&
!(tp->t_writer & TRW_WANT_WRITE))) {
rwstate++;
acquired = 1;
}
}
}
if (acquired || try_flag || time_error) {
/*
* We're not going to block this time.
*/
suword32_noerr(&rw->rwlock_readers, rwstate);
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
locked = 0;
if (acquired) {
/*
* Got the lock!
*/
error = 0;
} else if (try_flag) {
/*
* We didn't get the lock and we're about to block.
* If we're doing a trylock, return EBUSY instead.
*/
error = EBUSY;
} else if (time_error) {
/*
* The SUSV3 POSIX spec is very clear that we should
* get no error from validating the timer (above)
* until we would actually sleep.
*/
error = time_error;
}
goto out_drop;
}
/*
* We're about to block, so indicate what kind of waiter we are.
*/
t->t_writer = 0;
if (rd_wr == WRITE_LOCK)
t->t_writer = TRW_WANT_WRITE;
suword32_noerr(&rw->rwlock_readers, rwstate);
/*
* Unlock the rwlock's mutex (pagefaults are possible here).
*/
set_owner_pid(mp, 0, 0);
ulock_clear(&mp->mutex_lockw);
fuword8_noerr(&mp->mutex_waiters, &mwaiters);
if (mwaiters != 0) {
/*
* Given the locking of mlwpchan around the release of
* the mutex and checking for waiters, the following
* call to lwp_release() can fail ONLY if the lock
* acquirer is interrupted after setting the waiter bit,
* calling lwp_block() and releasing mlwpchan.
* In this case, it could get pulled off the LWP sleep
* queue (via setrun()) before the following call to
* lwp_release() occurs, and the lock requestor will
* update the waiter bit correctly by re-evaluating it.
*/
if (lwp_release(&mlwpchan, &mwaiters, 0))
suword8_noerr(&mp->mutex_waiters, mwaiters);
}
lwpchan_unlock(&mlwpchan, LWPCHAN_MPPOOL);
mlocked = 0;
no_fault();
if (mwatched) {
watch_enable_addr((caddr_t)mp, sizeof (*mp), S_WRITE);
mwatched = 0;
}
if (watched) {
watch_enable_addr((caddr_t)rw, sizeof (*rw), S_WRITE);
watched = 0;
}
if (timedwait) {
/*
* If we successfully queue the timeout,
* then don't drop t_delay_lock until
* we are on the sleep queue (below).
*/
mutex_enter(&t->t_delay_lock);
if (lwp_timer_enqueue(&lwpt) != 0) {
mutex_exit(&t->t_delay_lock);
imm_timeout = 1;
timedwait = NULL;
}
}
t->t_flag |= T_WAITCVSEM;
lwp_block(&lwpchan);
/*
* Nothing should happen to cause the LWp to go to sleep until after
* it returns from swtch().
*/
if (timedwait)
mutex_exit(&t->t_delay_lock);
locked = 0;
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || imm_timeout)
setrun(t);
swtch();
/*
* We're back, but we need to work out why. Were we interrupted? Did
* we timeout? Were we granted the lock?
*/
error = EAGAIN;
acquired = (t->t_writer & TRW_LOCK_GRANTED);
t->t_writer = 0;
t->t_flag &= ~(T_WAITCVSEM | T_WAKEABLE);
if (timedwait)
tim = lwp_timer_dequeue(&lwpt);
if (ISSIG(t, FORREAL) || lwp->lwp_sysabort || MUSTRETURN(p, t))
error = EINTR;
else if (imm_timeout || (timedwait && tim == -1))
error = ETIME;
lwp->lwp_asleep = 0;
lwp->lwp_sysabort = 0;
setallwatch();
/*
* If we were granted the lock we don't care about EINTR or ETIME.
*/
if (acquired)
error = 0;
if (t->t_mstate == LMS_USER_LOCK)
(void) new_mstate(t, LMS_SYSTEM);
if (error)
return (set_errno(error));
return (0);
out_drop:
/*
* Make sure that the user level lock is dropped before returning
* to the caller.
*/
if (!mlocked) {
lwpchan_lock(&mlwpchan, LWPCHAN_MPPOOL);
mlocked = 1;
}
set_owner_pid(mp, 0, 0);
ulock_clear(&mp->mutex_lockw);
fuword8_noerr(&mp->mutex_waiters, &mwaiters);
if (mwaiters != 0) {
/*
* See comment above on lock clearing and lwp_release()
* success/failure.
*/
if (lwp_release(&mlwpchan, &mwaiters, 0))
suword8_noerr(&mp->mutex_waiters, mwaiters);
}
lwpchan_unlock(&mlwpchan, LWPCHAN_MPPOOL);
mlocked = 0;
out_nodrop:
no_fault();
if (mwatched)
watch_enable_addr((caddr_t)mp, sizeof (*mp), S_WRITE);
if (watched)
watch_enable_addr((caddr_t)rw, sizeof (*rw), S_WRITE);
if (t->t_mstate == LMS_USER_LOCK)
(void) new_mstate(t, LMS_SYSTEM);
if (error)
return (set_errno(error));
return (0);
}
/*
* We enter here holding the user-level mutex but, unlike lwp_rwlock_lock(),
* we never drop the lock.
*/
static int
lwp_rwlock_unlock(lwp_rwlock_t *rw)
{
kthread_t *t = curthread;
proc_t *p = ttoproc(t);
lwpchan_t lwpchan;
volatile uint16_t type = 0;
volatile int error = 0;
volatile int locked = 0;
volatile int watched = 0;
label_t ljb;
volatile int no_lwpchan = 1;
uint32_t rwstate;
/* We only check rw because the mutex is included in it. */
if ((caddr_t)rw >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
if (on_fault(&ljb)) {
if (no_lwpchan) {
error = EFAULT;
goto out_nodrop;
}
if (locked) {
locked = 0;
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
}
error = EFAULT;
goto out_nodrop;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword16_noerr(&rw->rwlock_type, (uint16_t *)&type);
suword16_noerr(&rw->rwlock_type, type);
/* We can only continue for simple USYNC_PROCESS locks. */
if (type != USYNC_PROCESS) {
error = EINVAL;
goto out_nodrop;
}
/* Convert user level rwlock, "rw", to a unique lwpchan. */
if (!get_lwpchan(p->p_as, (caddr_t)rw, type,
&lwpchan, LWPCHAN_CVPOOL)) {
error = EFAULT;
goto out_nodrop;
}
no_lwpchan = 0;
watched = watch_disable_addr((caddr_t)rw, sizeof (*rw), S_WRITE);
lwpchan_lock(&lwpchan, LWPCHAN_CVPOOL);
locked = 1;
/*
* We can resolve multiple readers (except the last reader) here.
* For the last reader or a writer we need lwp_rwlock_release(),
* to which we also delegate the task of copying the new rwstate
* back to userland (see the comment there).
*/
fuword32_noerr(&rw->rwlock_readers, &rwstate);
if (rwstate & URW_WRITE_LOCKED)
lwp_rwlock_release(&lwpchan, rw);
else if ((rwstate & URW_READERS_MASK) > 0) {
rwstate--;
if ((rwstate & URW_READERS_MASK) == 0)
lwp_rwlock_release(&lwpchan, rw);
else
suword32_noerr(&rw->rwlock_readers, rwstate);
}
lwpchan_unlock(&lwpchan, LWPCHAN_CVPOOL);
locked = 0;
error = 0;
out_nodrop:
no_fault();
if (watched)
watch_enable_addr((caddr_t)rw, sizeof (*rw), S_WRITE);
if (error)
return (set_errno(error));
return (0);
}
int
lwp_rwlock_sys(int subcode, lwp_rwlock_t *rwlp, timespec_t *tsp)
{
switch (subcode) {
case 0:
return (lwp_rwlock_lock(rwlp, tsp, READ_LOCK));
case 1:
return (lwp_rwlock_lock(rwlp, tsp, WRITE_LOCK));
case 2:
return (lwp_rwlock_lock(rwlp, NULL, READ_LOCK_TRY));
case 3:
return (lwp_rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY));
case 4:
return (lwp_rwlock_unlock(rwlp));
}
return (set_errno(EINVAL));
}
/*
* Return the owner of the user-level s-object.
* Since we can't really do this, return NULL.
*/
/* ARGSUSED */
static kthread_t *
lwpsobj_owner(caddr_t sobj)
{
return ((kthread_t *)NULL);
}
/*
* Wake up a thread asleep on a user-level synchronization
* object.
*/
static void
lwp_unsleep(kthread_t *t)
{
ASSERT(THREAD_LOCK_HELD(t));
if (t->t_wchan0 != NULL) {
sleepq_head_t *sqh;
sleepq_t *sqp = t->t_sleepq;
if (sqp != NULL) {
sqh = lwpsqhash(&t->t_lwpchan);
ASSERT(&sqh->sq_queue == sqp);
sleepq_unsleep(t);
disp_lock_exit_high(&sqh->sq_lock);
CL_SETRUN(t);
return;
}
}
panic("lwp_unsleep: thread %p not on sleepq", (void *)t);
}
/*
* Change the priority of a thread asleep on a user-level
* synchronization object. To maintain proper priority order,
* we:
* o dequeue the thread.
* o change its priority.
* o re-enqueue the thread.
* Assumption: the thread is locked on entry.
*/
static void
lwp_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
{
ASSERT(THREAD_LOCK_HELD(t));
if (t->t_wchan0 != NULL) {
sleepq_t *sqp = t->t_sleepq;
sleepq_dequeue(t);
*t_prip = pri;
sleepq_insert(sqp, t);
} else
panic("lwp_change_pri: %p not on a sleep queue", (void *)t);
}
/*
* Clean up a left-over process-shared robust mutex
*/
static void
lwp_mutex_cleanup(lwpchan_entry_t *ent, uint16_t lockflg)
{
uint16_t flag;
uchar_t waiters;
label_t ljb;
pid_t owner_pid;
lwp_mutex_t *lp;
volatile int locked = 0;
volatile int watched = 0;
volatile struct upimutex *upimutex = NULL;
volatile int upilocked = 0;
if ((ent->lwpchan_type & (USYNC_PROCESS | LOCK_ROBUST))
!= (USYNC_PROCESS | LOCK_ROBUST))
return;
lp = (lwp_mutex_t *)ent->lwpchan_addr;
watched = watch_disable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
if (on_fault(&ljb)) {
if (locked)
lwpchan_unlock(&ent->lwpchan_lwpchan, LWPCHAN_MPPOOL);
if (upilocked)
upimutex_unlock((upimutex_t *)upimutex, 0);
goto out;
}
fuword32_noerr(&lp->mutex_ownerpid, (uint32_t *)&owner_pid);
if (UPIMUTEX(ent->lwpchan_type)) {
lwpchan_t lwpchan = ent->lwpchan_lwpchan;
upib_t *upibp = &UPI_CHAIN(lwpchan);
if (owner_pid != curproc->p_pid)
goto out;
mutex_enter(&upibp->upib_lock);
upimutex = upi_get(upibp, &lwpchan);
if (upimutex == NULL || upimutex->upi_owner != curthread) {
mutex_exit(&upibp->upib_lock);
goto out;
}
mutex_exit(&upibp->upib_lock);
upilocked = 1;
flag = lwp_clear_mutex(lp, lockflg);
suword8_noerr(&lp->mutex_lockw, 0);
upimutex_unlock((upimutex_t *)upimutex, flag);
} else {
lwpchan_lock(&ent->lwpchan_lwpchan, LWPCHAN_MPPOOL);
locked = 1;
/*
* Clear the spinners count because one of our
* threads could have been spinning for this lock
* at user level when the process was suddenly killed.
* There is no harm in this since user-level libc code
* will adapt to the sudden change in the spinner count.
*/
suword8_noerr(&lp->mutex_spinners, 0);
if (owner_pid != curproc->p_pid) {
/*
* We are not the owner. There may or may not be one.
* If there are waiters, we wake up one or all of them.
* It doesn't hurt to wake them up in error since
* they will just retry the lock and go to sleep
* again if necessary.
*/
fuword8_noerr(&lp->mutex_waiters, &waiters);
if (waiters != 0) { /* there are waiters */
fuword16_noerr(&lp->mutex_flag, &flag);
if (flag & LOCK_NOTRECOVERABLE) {
lwp_release_all(&ent->lwpchan_lwpchan);
suword8_noerr(&lp->mutex_waiters, 0);
} else if (lwp_release(&ent->lwpchan_lwpchan,
&waiters, 0)) {
suword8_noerr(&lp->mutex_waiters,
waiters);
}
}
} else {
/*
* We are the owner. Release it.
*/
(void) lwp_clear_mutex(lp, lockflg);
ulock_clear(&lp->mutex_lockw);
fuword8_noerr(&lp->mutex_waiters, &waiters);
if (waiters &&
lwp_release(&ent->lwpchan_lwpchan, &waiters, 0))
suword8_noerr(&lp->mutex_waiters, waiters);
}
lwpchan_unlock(&ent->lwpchan_lwpchan, LWPCHAN_MPPOOL);
}
out:
no_fault();
if (watched)
watch_enable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
}
/*
* Register a process-shared robust mutex in the lwpchan cache.
*/
int
lwp_mutex_register(lwp_mutex_t *lp, caddr_t uaddr)
{
int error = 0;
volatile int watched;
label_t ljb;
uint8_t type;
lwpchan_t lwpchan;
if ((caddr_t)lp >= (caddr_t)USERLIMIT)
return (set_errno(EFAULT));
watched = watch_disable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
if (on_fault(&ljb)) {
error = EFAULT;
} else {
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword8_noerr(&lp->mutex_type, &type);
suword8_noerr(&lp->mutex_type, type);
if ((type & (USYNC_PROCESS|LOCK_ROBUST))
!= (USYNC_PROCESS|LOCK_ROBUST)) {
error = EINVAL;
} else if (!lwpchan_get_mapping(curproc->p_as, (caddr_t)lp,
uaddr, type, &lwpchan, LWPCHAN_MPPOOL)) {
error = EFAULT;
}
}
no_fault();
if (watched)
watch_enable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
if (error)
return (set_errno(error));
return (0);
}
/*
* There is a user-level robust lock registration in libc.
* Mark it as invalid by storing -1 into the location of the pointer.
*/
static void
lwp_mutex_unregister(void *uaddr)
{
if (get_udatamodel() == DATAMODEL_NATIVE) {
(void) sulword(uaddr, (ulong_t)-1);
#ifdef _SYSCALL32_IMPL
} else {
(void) suword32(uaddr, (uint32_t)-1);
#endif
}
}
int
lwp_mutex_trylock(lwp_mutex_t *lp, uintptr_t owner)
{
kthread_t *t = curthread;
proc_t *p = ttoproc(t);
int error = 0;
volatile int locked = 0;
volatile int watched = 0;
label_t ljb;
volatile uint8_t type = 0;
uint16_t flag;
lwpchan_t lwpchan;
if ((caddr_t)lp >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
(void) new_mstate(t, LMS_USER_LOCK);
if (on_fault(&ljb)) {
if (locked)
lwpchan_unlock(&lwpchan, LWPCHAN_MPPOOL);
error = EFAULT;
goto out;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword8_noerr(&lp->mutex_type, (uint8_t *)&type);
suword8_noerr(&lp->mutex_type, type);
if (UPIMUTEX(type)) {
no_fault();
error = lwp_upimutex_lock(lp, type, UPIMUTEX_TRY, NULL);
if (error == 0 || error == EOWNERDEAD ||
error == ELOCKUNMAPPED) {
volatile int locked = error != 0;
if (on_fault(&ljb)) {
if (locked != 0)
error = lwp_upimutex_unlock(lp, type);
else
error = EFAULT;
goto upierr;
}
set_owner_pid(lp, owner,
(type & USYNC_PROCESS)? p->p_pid : 0);
no_fault();
}
upierr:
if (error)
return (set_errno(error));
return (0);
}
if (!get_lwpchan(curproc->p_as, (caddr_t)lp, type,
&lwpchan, LWPCHAN_MPPOOL)) {
error = EFAULT;
goto out;
}
lwpchan_lock(&lwpchan, LWPCHAN_MPPOOL);
locked = 1;
if (type & LOCK_ROBUST) {
fuword16_noerr(&lp->mutex_flag, &flag);
if (flag & LOCK_NOTRECOVERABLE) {
lwpchan_unlock(&lwpchan, LWPCHAN_MPPOOL);
error = ENOTRECOVERABLE;
goto out;
}
}
watched = watch_disable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
if (!ulock_try(&lp->mutex_lockw))
error = EBUSY;
else {
set_owner_pid(lp, owner, (type & USYNC_PROCESS)? p->p_pid : 0);
if (type & LOCK_ROBUST) {
fuword16_noerr(&lp->mutex_flag, &flag);
if (flag & (LOCK_OWNERDEAD | LOCK_UNMAPPED)) {
if (flag & LOCK_OWNERDEAD)
error = EOWNERDEAD;
else if (type & USYNC_PROCESS_ROBUST)
error = ELOCKUNMAPPED;
else
error = EOWNERDEAD;
}
}
}
locked = 0;
lwpchan_unlock(&lwpchan, LWPCHAN_MPPOOL);
out:
if (t->t_mstate == LMS_USER_LOCK)
(void) new_mstate(t, LMS_SYSTEM);
no_fault();
if (watched)
watch_enable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
if (error)
return (set_errno(error));
return (0);
}
/*
* unlock the mutex and unblock lwps that is trying to acquire this mutex.
* the blocked lwp resumes and retries to acquire the lock.
*/
int
lwp_mutex_unlock(lwp_mutex_t *lp)
{
proc_t *p = ttoproc(curthread);
lwpchan_t lwpchan;
uchar_t waiters;
volatile int locked = 0;
volatile int watched = 0;
volatile uint8_t type = 0;
label_t ljb;
uint16_t flag;
int error = 0;
if ((caddr_t)lp >= p->p_as->a_userlimit)
return (set_errno(EFAULT));
if (on_fault(&ljb)) {
if (locked)
lwpchan_unlock(&lwpchan, LWPCHAN_MPPOOL);
error = EFAULT;
goto out;
}
/*
* Force Copy-on-write if necessary and ensure that the
* synchronization object resides in read/write memory.
* Cause an EFAULT return now if this is not so.
*/
fuword8_noerr(&lp->mutex_type, (uint8_t *)&type);
suword8_noerr(&lp->mutex_type, type);
if (UPIMUTEX(type)) {
no_fault();
error = lwp_upimutex_unlock(lp, type);
if (error)
return (set_errno(error));
return (0);
}
watched = watch_disable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
if (!get_lwpchan(curproc->p_as, (caddr_t)lp, type,
&lwpchan, LWPCHAN_MPPOOL)) {
error = EFAULT;
goto out;
}
lwpchan_lock(&lwpchan, LWPCHAN_MPPOOL);
locked = 1;
if (type & LOCK_ROBUST) {
fuword16_noerr(&lp->mutex_flag, &flag);
if (flag & (LOCK_OWNERDEAD | LOCK_UNMAPPED)) {
flag &= ~(LOCK_OWNERDEAD | LOCK_UNMAPPED);
flag |= LOCK_NOTRECOVERABLE;
suword16_noerr(&lp->mutex_flag, flag);
}
}
set_owner_pid(lp, 0, 0);
ulock_clear(&lp->mutex_lockw);
/*
* Always wake up an lwp (if any) waiting on lwpchan. The woken lwp will
* re-try the lock in lwp_mutex_timedlock(). The call to lwp_release()
* may fail. If it fails, do not write into the waiter bit.
* The call to lwp_release() might fail due to one of three reasons:
*
* 1. due to the thread which set the waiter bit not actually
* sleeping since it got the lock on the re-try. The waiter
* bit will then be correctly updated by that thread. This
* window may be closed by reading the wait bit again here
* and not calling lwp_release() at all if it is zero.
* 2. the thread which set the waiter bit and went to sleep
* was woken up by a signal. This time, the waiter recomputes
* the wait bit in the return with EINTR code.
* 3. the waiter bit read by lwp_mutex_wakeup() was in
* memory that has been re-used after the lock was dropped.
* In this case, writing into the waiter bit would cause data
* corruption.
*/
fuword8_noerr(&lp->mutex_waiters, &waiters);
if (waiters) {
if ((type & LOCK_ROBUST) &&
(flag & LOCK_NOTRECOVERABLE)) {
lwp_release_all(&lwpchan);
suword8_noerr(&lp->mutex_waiters, 0);
} else if (lwp_release(&lwpchan, &waiters, 0)) {
suword8_noerr(&lp->mutex_waiters, waiters);
}
}
lwpchan_unlock(&lwpchan, LWPCHAN_MPPOOL);
out:
no_fault();
if (watched)
watch_enable_addr((caddr_t)lp, sizeof (*lp), S_WRITE);
if (error)
return (set_errno(error));
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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/debug.h>
#include <sys/mutex.h>
#include <sys/atomic.h>
#include <sys/timer.h>
#include <sys/lwp_timer_impl.h>
#include <sys/callo.h>
/*
* lwp_timer_timeout() is called from a timeout set up in lwp_cond_wait(),
* lwp_mutex_timedlock(), lwp_sema_timedwait() or lwp_rwlock_lock().
*
* It recomputes the time remaining until the absolute time when the
* wait is supposed to timeout and either calls realtime_timeout()
* to reschedule itself or calls setrun() on the sleeping thread.
*
* This is done to ensure that the waiting thread does not wake up
* due to timer expiration until the absolute future time of the
* timeout has been reached. Until that time, the thread must
* remain on its sleep queue.
*
* An lwp_timer_t structure is used to pass information
* about the sleeping thread to the timeout function.
*/
static void
lwp_timer_timeout(void *arg)
{
lwp_timer_t *lwptp = arg;
kthread_t *t = lwptp->lwpt_thread;
timespec_t now, delta;
mutex_enter(&t->t_delay_lock);
gethrestime(&now);
/*
* Requeue the timeout if no one has reset the system time
* and if the absolute future time has not been reached.
*/
if (lwptp->lwpt_timecheck == timechanged &&
(lwptp->lwpt_rqtime.tv_sec > now.tv_sec ||
(lwptp->lwpt_rqtime.tv_sec == now.tv_sec &&
lwptp->lwpt_rqtime.tv_nsec > now.tv_nsec))) {
lwptp->lwpt_imm_timeout = 0;
delta = lwptp->lwpt_rqtime;
timespecsub(&delta, &now);
lwptp->lwpt_id = timeout_generic(CALLOUT_REALTIME,
lwp_timer_timeout, lwptp, ts2hrt(&delta), nsec_per_tick,
(CALLOUT_FLAG_HRESTIME | CALLOUT_FLAG_ROUNDUP));
} else {
/*
* Set the thread running only if it is asleep on
* its lwpchan sleep queue (not if it is asleep on
* the t_delay_lock mutex).
*/
thread_lock(t);
/* do this for the benefit of upi mutexes */
(void) atomic_cas_uint(&lwptp->lwpt_imm_timeout, 0, 1);
if (t->t_state == TS_SLEEP &&
(t->t_flag & T_WAKEABLE) &&
t->t_wchan0 != NULL)
setrun_locked(t);
thread_unlock(t);
}
mutex_exit(&t->t_delay_lock);
}
int
lwp_timer_copyin(lwp_timer_t *lwptp, timespec_t *tsp)
{
timespec_t now;
int error = 0;
if (tsp == NULL) /* not really an error, just need to bzero() */
goto err;
lwptp->lwpt_timecheck = timechanged; /* do this before gethrestime() */
gethrestime(&now); /* do this before copyin() */
if (curproc->p_model == DATAMODEL_NATIVE) {
if (copyin(tsp, &lwptp->lwpt_rqtime, sizeof (timespec_t))) {
error = EFAULT;
goto err;
}
} else {
timespec32_t ts32;
if (copyin(tsp, &ts32, sizeof (timespec32_t))) {
error = EFAULT;
goto err;
}
TIMESPEC32_TO_TIMESPEC(&lwptp->lwpt_rqtime, &ts32);
}
if (itimerspecfix(&lwptp->lwpt_rqtime)) {
error = EINVAL;
goto err;
}
/*
* Unless the requested timeout is zero,
* get the precise future (absolute) time at
* which we are to time out and return ETIME.
* We must not return ETIME before that time.
*/
if (lwptp->lwpt_rqtime.tv_sec == 0 && lwptp->lwpt_rqtime.tv_nsec == 0) {
bzero(lwptp, sizeof (lwp_timer_t));
lwptp->lwpt_imm_timeout = 1;
} else {
lwptp->lwpt_thread = curthread;
lwptp->lwpt_tsp = tsp;
lwptp->lwpt_time_error = 0;
lwptp->lwpt_id = 0;
lwptp->lwpt_imm_timeout = 0;
timespecadd(&lwptp->lwpt_rqtime, &now);
}
return (0);
err:
bzero(lwptp, sizeof (lwp_timer_t));
lwptp->lwpt_time_error = error;
return (error);
}
int
lwp_timer_enqueue(lwp_timer_t *lwptp)
{
timespec_t now, delta;
ASSERT(lwptp->lwpt_thread == curthread);
ASSERT(MUTEX_HELD(&curthread->t_delay_lock));
gethrestime(&now);
if (lwptp->lwpt_timecheck == timechanged &&
(lwptp->lwpt_rqtime.tv_sec > now.tv_sec ||
(lwptp->lwpt_rqtime.tv_sec == now.tv_sec &&
lwptp->lwpt_rqtime.tv_nsec > now.tv_nsec))) {
/*
* Queue the timeout.
*/
lwptp->lwpt_imm_timeout = 0;
delta = lwptp->lwpt_rqtime;
timespecsub(&delta, &now);
lwptp->lwpt_id = timeout_generic(CALLOUT_REALTIME,
lwp_timer_timeout, lwptp, ts2hrt(&delta), nsec_per_tick,
(CALLOUT_FLAG_HRESTIME | CALLOUT_FLAG_ROUNDUP));
return (0);
}
/*
* Time has already run out or someone reset the system time;
* just cause an immediate timeout.
*/
lwptp->lwpt_imm_timeout = 1;
return (1);
}
clock_t
lwp_timer_dequeue(lwp_timer_t *lwptp)
{
kthread_t *t = curthread;
clock_t tim = -1;
callout_id_t tmp_id;
mutex_enter(&t->t_delay_lock);
while ((tmp_id = lwptp->lwpt_id) != 0) {
lwptp->lwpt_id = 0;
mutex_exit(&t->t_delay_lock);
tim = untimeout_default(tmp_id, 0);
mutex_enter(&t->t_delay_lock);
}
mutex_exit(&t->t_delay_lock);
return (tim);
}
int
lwp_timer_copyout(lwp_timer_t *lwptp, int error)
{
timespec_t rmtime;
timespec_t now;
if (lwptp->lwpt_tsp == NULL) /* nothing to do */
return (error);
rmtime.tv_sec = rmtime.tv_nsec = 0;
if (error != ETIME) {
gethrestime(&now);
if ((now.tv_sec < lwptp->lwpt_rqtime.tv_sec) ||
((now.tv_sec == lwptp->lwpt_rqtime.tv_sec) &&
(now.tv_nsec < lwptp->lwpt_rqtime.tv_nsec))) {
rmtime = lwptp->lwpt_rqtime;
timespecsub(&rmtime, &now);
}
}
if (curproc->p_model == DATAMODEL_NATIVE) {
if (copyout(&rmtime, lwptp->lwpt_tsp, sizeof (timespec_t)))
error = EFAULT;
} else {
timespec32_t rmtime32;
TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime);
if (copyout(&rmtime32, lwptp->lwpt_tsp, sizeof (timespec32_t)))
error = EFAULT;
}
return (error);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2016 by Delphix. All rights reserved.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/prsystm.h>
#include <sys/cred.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/signal.h>
#include <sys/kmem.h>
#include <sys/unistd.h>
#include <sys/cmn_err.h>
#include <sys/schedctl.h>
#include <sys/debug.h>
#include <sys/contract/process_impl.h>
kthread_t *
idtot(proc_t *p, id_t lwpid)
{
lwpdir_t *ldp;
if ((ldp = lwp_hash_lookup(p, lwpid)) != NULL)
return (ldp->ld_entry->le_thread);
return (NULL);
}
/*
* Same as idtot(), but acquire and return
* the tid hash table entry lock on success.
* This allows lwp_unpark() to do its job without acquiring
* p->p_lock (and thereby causing congestion problems when
* the application calls lwp_unpark() too often).
*/
static kthread_t *
idtot_and_lock(proc_t *p, id_t lwpid, kmutex_t **mpp)
{
lwpdir_t *ldp;
kthread_t *t;
if ((ldp = lwp_hash_lookup_and_lock(p, lwpid, mpp)) != NULL) {
if ((t = ldp->ld_entry->le_thread) == NULL)
mutex_exit(*mpp);
return (t);
}
return (NULL);
}
/*
* Stop an lwp of the current process
*/
int
syslwp_suspend(id_t lwpid)
{
kthread_t *t;
int error;
proc_t *p = ttoproc(curthread);
mutex_enter(&p->p_lock);
if ((t = idtot(p, lwpid)) == NULL)
error = ESRCH;
else
error = lwp_suspend(t);
mutex_exit(&p->p_lock);
if (error)
return (set_errno(error));
return (0);
}
int
syslwp_continue(id_t lwpid)
{
kthread_t *t;
proc_t *p = ttoproc(curthread);
mutex_enter(&p->p_lock);
if ((t = idtot(p, lwpid)) == NULL) {
mutex_exit(&p->p_lock);
return (set_errno(ESRCH));
}
lwp_continue(t);
mutex_exit(&p->p_lock);
return (0);
}
int
lwp_kill(id_t lwpid, int sig)
{
sigqueue_t *sqp;
kthread_t *t;
proc_t *p = ttoproc(curthread);
if (sig < 0 || sig >= NSIG)
return (set_errno(EINVAL));
if (sig != 0)
sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
mutex_enter(&p->p_lock);
if ((t = idtot(p, lwpid)) == NULL) {
mutex_exit(&p->p_lock);
if (sig != 0)
kmem_free(sqp, sizeof (sigqueue_t));
return (set_errno(ESRCH));
}
if (sig == 0) {
mutex_exit(&p->p_lock);
return (0);
}
sqp->sq_info.si_signo = sig;
sqp->sq_info.si_code = SI_LWP;
sqp->sq_info.si_pid = p->p_pid;
sqp->sq_info.si_ctid = PRCTID(p);
sqp->sq_info.si_zoneid = getzoneid();
sqp->sq_info.si_uid = crgetruid(CRED());
sigaddqa(p, t, sqp);
mutex_exit(&p->p_lock);
return (0);
}
/*
* This is the specification of lwp_wait() from the _lwp_wait(2) manual page:
*
* The lwp_wait() function blocks the current lwp until the lwp specified
* by 'lwpid' terminates. If the specified lwp terminated prior to the call
* to lwp_wait(), then lwp_wait() returns immediately. If 'lwpid' is zero,
* then lwp_wait() waits for any undetached lwp in the current process.
* If 'lwpid' is not zero, then it must specify an undetached lwp in the
* current process. If 'departed' is not NULL, then it points to a location
* where the id of the exited lwp is stored.
*
* When an lwp exits and there are one or more lwps in the process waiting
* for this specific lwp to exit, then one of the waiting lwps is unblocked
* and it returns from lwp_wait() successfully. Any other lwps waiting for
* this same lwp to exit are also unblocked, however, they return from
* lwp_wait() with the error ESRCH. If there are no lwps in the process
* waiting for this specific lwp to exit but there are one or more lwps
* waiting for any lwp to exit, then one of the waiting lwps is unblocked
* and it returns from lwp_wait() successfully.
*
* If an lwp is waiting for any lwp to exit, it blocks until an undetached
* lwp for which no other lwp is waiting terminates, at which time it returns
* successfully, or until all other lwps in the process are either daemon
* lwps or lwps waiting in lwp_wait(), in which case it returns EDEADLK.
*/
int
lwp_wait(id_t lwpid, id_t *departed)
{
proc_t *p = ttoproc(curthread);
int error = 0;
int daemon = (curthread->t_proc_flag & TP_DAEMON)? 1 : 0;
lwpent_t *target_lep;
lwpdir_t *ldp;
lwpent_t *lep;
/*
* lwp_wait() is not supported for the /proc agent lwp.
*/
if (curthread == p->p_agenttp)
return (set_errno(ENOTSUP));
mutex_enter(&p->p_lock);
prbarrier(p);
curthread->t_waitfor = lwpid;
p->p_lwpwait++;
p->p_lwpdwait += daemon;
target_lep = NULL;
if (lwpid != 0) {
if ((ldp = lwp_hash_lookup(p, lwpid)) == NULL)
target_lep = NULL;
else {
target_lep = ldp->ld_entry;
target_lep->le_waiters++;
target_lep->le_dwaiters += daemon;
}
}
while (error == 0) {
kthread_t *t;
id_t tid;
int i;
if (lwpid != 0) {
/*
* Look for a specific zombie lwp.
*/
if (target_lep == NULL)
error = ESRCH;
else if ((t = target_lep->le_thread) != NULL) {
if (!(t->t_proc_flag & TP_TWAIT))
error = EINVAL;
} else {
/*
* We found the zombie we are waiting for.
*/
ASSERT(p->p_zombcnt > 0);
p->p_zombcnt--;
p->p_lwpwait--;
p->p_lwpdwait -= daemon;
curthread->t_waitfor = -1;
lwp_hash_out(p, lwpid);
mutex_exit(&p->p_lock);
if (departed != NULL &&
copyout(&lwpid, departed, sizeof (id_t)))
return (set_errno(EFAULT));
return (0);
}
} else {
/*
* Look for any zombie lwp.
*/
int some_non_daemon_will_return = 0;
/* for each entry in the lwp directory... */
ldp = p->p_lwpdir;
for (i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
if ((lep = ldp->ld_entry) == NULL ||
lep->le_thread != NULL)
continue;
/*
* We found a zombie lwp. If there is some
* other thread waiting specifically for the
* zombie we just found, then defer to the other
* waiting thread and continue searching for
* another zombie. Also check to see if there
* is some non-daemon thread sleeping here in
* lwp_wait() that will succeed and return when
* we drop p->p_lock. This is tested below.
*/
tid = lep->le_lwpid;
if (lep->le_waiters != 0) {
if (lep->le_waiters - lep->le_dwaiters)
some_non_daemon_will_return = 1;
continue;
}
/*
* We found a zombie that no one else
* is specifically waiting for.
*/
ASSERT(p->p_zombcnt > 0);
p->p_zombcnt--;
p->p_lwpwait--;
p->p_lwpdwait -= daemon;
curthread->t_waitfor = -1;
lwp_hash_out(p, tid);
mutex_exit(&p->p_lock);
if (departed != NULL &&
copyout(&tid, departed, sizeof (id_t)))
return (set_errno(EFAULT));
return (0);
}
/*
* We are waiting for anyone. If all non-daemon lwps
* are waiting here, and if we determined above that
* no non-daemon lwp will return, we have deadlock.
*/
if (!some_non_daemon_will_return &&
p->p_lwpcnt == p->p_lwpdaemon +
(p->p_lwpwait - p->p_lwpdwait))
error = EDEADLK;
}
if (error == 0 && lwpid != 0) {
/*
* We are waiting for a specific non-zombie lwp.
* Fail if there is a deadlock loop.
*/
for (;;) {
if (t == curthread) {
error = EDEADLK;
break;
}
/* who are they waiting for? */
if ((tid = t->t_waitfor) == -1)
break;
if (tid == 0) {
/*
* The lwp we are waiting for is
* waiting for anyone (transitively).
* If there are no zombies right now
* and if we would have deadlock due
* to all non-daemon lwps waiting here,
* wake up the lwp that is waiting for
* anyone so it can return EDEADLK.
*/
if (p->p_zombcnt == 0 &&
p->p_lwpcnt == p->p_lwpdaemon +
p->p_lwpwait - p->p_lwpdwait)
cv_broadcast(&p->p_lwpexit);
break;
}
if ((ldp = lwp_hash_lookup(p, tid)) == NULL ||
(t = ldp->ld_entry->le_thread) == NULL)
break;
}
}
if (error)
break;
/*
* Wait for some lwp to terminate.
*/
if (!cv_wait_sig(&p->p_lwpexit, &p->p_lock))
error = EINTR;
prbarrier(p);
if (lwpid != 0) {
if ((ldp = lwp_hash_lookup(p, lwpid)) == NULL)
target_lep = NULL;
else
target_lep = ldp->ld_entry;
}
}
if (lwpid != 0 && target_lep != NULL) {
target_lep->le_waiters--;
target_lep->le_dwaiters -= daemon;
}
p->p_lwpwait--;
p->p_lwpdwait -= daemon;
curthread->t_waitfor = -1;
mutex_exit(&p->p_lock);
return (set_errno(error));
}
int
lwp_detach(id_t lwpid)
{
kthread_t *t;
proc_t *p = ttoproc(curthread);
lwpdir_t *ldp;
int error = 0;
mutex_enter(&p->p_lock);
prbarrier(p);
if ((ldp = lwp_hash_lookup(p, lwpid)) == NULL)
error = ESRCH;
else if ((t = ldp->ld_entry->le_thread) != NULL) {
if (!(t->t_proc_flag & TP_TWAIT))
error = EINVAL;
else {
t->t_proc_flag &= ~TP_TWAIT;
cv_broadcast(&p->p_lwpexit);
}
} else {
ASSERT(p->p_zombcnt > 0);
p->p_zombcnt--;
lwp_hash_out(p, lwpid);
}
mutex_exit(&p->p_lock);
if (error)
return (set_errno(error));
return (0);
}
/*
* Unpark the specified lwp.
*/
static int
lwp_unpark(id_t lwpid)
{
proc_t *p = ttoproc(curthread);
kthread_t *t;
kmutex_t *mp;
int error = 0;
if ((t = idtot_and_lock(p, lwpid, &mp)) == NULL) {
error = ESRCH;
} else {
mutex_enter(&t->t_delay_lock);
t->t_unpark = 1;
cv_signal(&t->t_delay_cv);
mutex_exit(&t->t_delay_lock);
mutex_exit(mp);
}
return (error);
}
/*
* Cancel a previous unpark for the specified lwp.
*
* This interface exists ONLY to support older versions of libthread, which
* called lwp_unpark(self) to force calls to lwp_park(self) to return
* immediately. These older libthreads required a mechanism to cancel the
* lwp_unpark(self).
*
* libc does not call this interface. Instead, the sc_park flag in the
* schedctl page is cleared to force calls to lwp_park() to return
* immediately.
*/
static int
lwp_unpark_cancel(id_t lwpid)
{
proc_t *p = ttoproc(curthread);
kthread_t *t;
kmutex_t *mp;
int error = 0;
if ((t = idtot_and_lock(p, lwpid, &mp)) == NULL) {
error = ESRCH;
} else {
mutex_enter(&t->t_delay_lock);
t->t_unpark = 0;
mutex_exit(&t->t_delay_lock);
mutex_exit(mp);
}
return (error);
}
/*
* Sleep until we are set running by lwp_unpark() or until we are
* interrupted by a signal or until we exhaust our timeout.
* timeoutp is an in/out parameter. On entry, it contains the relative
* time until timeout. On exit, we copyout the residual time left to it.
*/
static int
lwp_park(timespec_t *timeoutp, id_t lwpid)
{
timespec_t rqtime;
timespec_t rmtime;
timespec_t now;
timespec_t *rqtp = NULL;
kthread_t *t = curthread;
int timecheck = 0;
int error = 0;
model_t datamodel = ttoproc(t)->p_model;
if (lwpid != 0) /* unpark the other lwp, if any */
(void) lwp_unpark(lwpid);
if (timeoutp) {
timecheck = timechanged;
gethrestime(&now);
if (datamodel == DATAMODEL_NATIVE) {
if (copyin(timeoutp, &rqtime, sizeof (timespec_t))) {
error = EFAULT;
goto out;
}
} else {
timespec32_t timeout32;
if (copyin(timeoutp, &timeout32, sizeof (timeout32))) {
error = EFAULT;
goto out;
}
TIMESPEC32_TO_TIMESPEC(&rqtime, &timeout32)
}
if (itimerspecfix(&rqtime)) {
error = EINVAL;
goto out;
}
/*
* Convert the timespec value into absolute time.
*/
timespecadd(&rqtime, &now);
rqtp = &rqtime;
}
(void) new_mstate(t, LMS_USER_LOCK);
mutex_enter(&t->t_delay_lock);
if (!schedctl_is_park())
error = EINTR;
while (error == 0 && t->t_unpark == 0) {
switch (cv_waituntil_sig(&t->t_delay_cv,
&t->t_delay_lock, rqtp, timecheck)) {
case 0:
error = EINTR;
break;
case -1:
error = ETIME;
break;
}
}
t->t_unpark = 0;
mutex_exit(&t->t_delay_lock);
if (timeoutp != NULL) {
rmtime.tv_sec = rmtime.tv_nsec = 0;
if (error != ETIME) {
gethrestime(&now);
if ((now.tv_sec < rqtime.tv_sec) ||
((now.tv_sec == rqtime.tv_sec) &&
(now.tv_nsec < rqtime.tv_nsec))) {
rmtime = rqtime;
timespecsub(&rmtime, &now);
}
}
if (datamodel == DATAMODEL_NATIVE) {
if (copyout(&rmtime, timeoutp, sizeof (rmtime)))
error = EFAULT;
} else {
timespec32_t rmtime32;
TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime);
if (copyout(&rmtime32, timeoutp, sizeof (rmtime32)))
error = EFAULT;
}
}
out:
schedctl_unpark();
if (t->t_mstate == LMS_USER_LOCK)
(void) new_mstate(t, LMS_SYSTEM);
return (error);
}
#define MAXLWPIDS 1024
/*
* Unpark all of the specified lwps.
* Do it in chunks of MAXLWPIDS to avoid allocating too much memory.
*/
static int
lwp_unpark_all(id_t *lwpidp, int nids)
{
proc_t *p = ttoproc(curthread);
kthread_t *t;
kmutex_t *mp;
int error = 0;
id_t *lwpid;
size_t lwpidsz;
int n;
int i;
if (nids <= 0)
return (EINVAL);
lwpidsz = MIN(nids, MAXLWPIDS) * sizeof (id_t);
lwpid = kmem_alloc(lwpidsz, KM_SLEEP);
while (nids > 0) {
n = MIN(nids, MAXLWPIDS);
if (copyin(lwpidp, lwpid, n * sizeof (id_t))) {
error = EFAULT;
break;
}
for (i = 0; i < n; i++) {
if ((t = idtot_and_lock(p, lwpid[i], &mp)) == NULL) {
error = ESRCH;
} else {
mutex_enter(&t->t_delay_lock);
t->t_unpark = 1;
cv_signal(&t->t_delay_cv);
mutex_exit(&t->t_delay_lock);
mutex_exit(mp);
}
}
lwpidp += n;
nids -= n;
}
kmem_free(lwpid, lwpidsz);
return (error);
}
/*
* SYS_lwp_park() system call.
*/
int
syslwp_park(int which, uintptr_t arg1, uintptr_t arg2)
{
int error;
switch (which) {
case 0:
error = lwp_park((timespec_t *)arg1, (id_t)arg2);
break;
case 1:
error = lwp_unpark((id_t)arg1);
break;
case 2:
error = lwp_unpark_all((id_t *)arg1, (int)arg2);
break;
case 3:
/*
* This subcode is not used by libc. It exists ONLY to
* support older versions of libthread which do not use
* the sc_park flag in the schedctl page.
*
* These versions of libthread need to be modifed or emulated
* to change calls to syslwp_park(1, tid, 0) to
* syslwp_park(3, tid).
*/
error = lwp_unpark_cancel((id_t)arg1);
break;
case 4:
/*
* This subcode is not used by libc. It exists ONLY to
* support older versions of libthread which do not use
* the sc_park flag in the schedctl page.
*
* These versions of libthread need to be modified or emulated
* to change calls to syslwp_park(0, ts, tid) to
* syslwp_park(4, ts, tid).
*/
schedctl_set_park();
error = lwp_park((timespec_t *)arg1, (id_t)arg2);
break;
default:
error = EINVAL;
break;
}
if (error)
return (set_errno(error));
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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2015 Joyent, Inc.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/types.h>
#include <sys/bitmap.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/unistd.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/mman.h>
#include <sys/tuneable.h>
#include <sys/cmn_err.h>
#include <sys/cred.h>
#include <sys/vmsystm.h>
#include <sys/debug.h>
#include <sys/policy.h>
#include <vm/as.h>
#include <vm/seg.h>
static uint_t mem_getpgszc(size_t);
/*
* Memory control operations
*/
int
memcntl(caddr_t addr, size_t len, int cmd, caddr_t arg, int attr, int mask)
{
struct as *as = ttoproc(curthread)->p_as;
struct proc *p = ttoproc(curthread);
size_t pgsz;
uint_t szc, oszc, pgcmd;
int error = 0;
faultcode_t fc;
uintptr_t iarg;
STRUCT_DECL(memcntl_mha, mha);
if (mask)
return (set_errno(EINVAL));
if ((cmd == MC_LOCKAS) || (cmd == MC_UNLOCKAS)) {
if ((addr != 0) || (len != 0)) {
return (set_errno(EINVAL));
}
} else if (cmd != MC_HAT_ADVISE) {
if (((uintptr_t)addr & PAGEOFFSET) != 0 || len == 0) {
return (set_errno(EINVAL));
}
/*
* We're only concerned with the address range
* here, not the protections. The protections
* are only used as a "filter" in this code,
* they aren't set or modified here.
*/
if (valid_usr_range(addr, len, 0, as,
as->a_userlimit) != RANGE_OKAY) {
return (set_errno(ENOMEM));
}
}
if (cmd == MC_HAT_ADVISE) {
if (attr != 0 || mask != 0) {
return (set_errno(EINVAL));
}
} else {
if ((VALID_ATTR & attr) != attr) {
return (set_errno(EINVAL));
}
if ((attr & SHARED) && (attr & PRIVATE)) {
return (set_errno(EINVAL));
}
if (((cmd == MC_LOCKAS) || (cmd == MC_LOCK) ||
(cmd == MC_UNLOCKAS) || (cmd == MC_UNLOCK)) &&
(error = secpolicy_lock_memory(CRED())) != 0)
return (set_errno(error));
}
if (attr) {
attr |= PROT_USER;
}
oszc = 0;
switch (cmd) {
case MC_SYNC:
/*
* MS_SYNC used to be defined to be zero but is now non-zero.
* For binary compatibility we still accept zero
* (the absence of MS_ASYNC) to mean the same thing.
*/
iarg = (uintptr_t)arg;
if ((iarg & ~MS_INVALIDATE) == 0)
iarg |= MS_SYNC;
if (((iarg & ~(MS_SYNC|MS_ASYNC|MS_INVALIDATE)) != 0) ||
((iarg & (MS_SYNC|MS_ASYNC)) == (MS_SYNC|MS_ASYNC))) {
error = set_errno(EINVAL);
} else {
error = as_ctl(as, addr, len, cmd, attr, iarg, NULL, 0);
if (error) {
(void) set_errno(error);
}
}
return (error);
case MC_LOCKAS:
if ((uintptr_t)arg & ~(MCL_FUTURE|MCL_CURRENT) ||
(uintptr_t)arg == 0) {
return (set_errno(EINVAL));
}
break;
case MC_LOCK:
case MC_UNLOCKAS:
case MC_UNLOCK:
break;
case MC_HAT_ADVISE:
/*
* Set prefered page size.
*/
STRUCT_INIT(mha, get_udatamodel());
if (copyin(arg, STRUCT_BUF(mha), STRUCT_SIZE(mha))) {
return (set_errno(EFAULT));
}
pgcmd = STRUCT_FGET(mha, mha_cmd);
/*
* Currently only MHA_MAPSIZE_VA, MHA_MAPSIZE_STACK
* and MHA_MAPSIZE_BSSBRK are supported. Only one
* command may be specified at a time.
*/
if ((~(MHA_MAPSIZE_VA|MHA_MAPSIZE_STACK|MHA_MAPSIZE_BSSBRK) &
pgcmd) || pgcmd == 0 || !ISP2(pgcmd) ||
STRUCT_FGET(mha, mha_flags))
return (set_errno(EINVAL));
pgsz = STRUCT_FGET(mha, mha_pagesize);
/*
* call platform specific map_pgsz() routine to get the
* optimal pgsz if pgsz is 0.
*
* For stack and heap operations addr and len must be zero.
*/
if ((pgcmd & (MHA_MAPSIZE_BSSBRK|MHA_MAPSIZE_STACK)) != 0) {
if (addr != NULL || len != 0) {
return (set_errno(EINVAL));
}
/*
* Disable autompss for this process unless pgsz == 0,
* which means the system should pick. In the
* pgsz == 0 case, leave the SAUTOLPG setting alone, as
* we don't want to enable it when someone has
* disabled automatic large page selection for the
* whole system.
*/
mutex_enter(&p->p_lock);
if (pgsz != 0) {
p->p_flag &= ~SAUTOLPG;
}
mutex_exit(&p->p_lock);
as_rangelock(as);
if (pgsz == 0) {
int type;
if (pgcmd == MHA_MAPSIZE_BSSBRK)
type = MAPPGSZ_HEAP;
else
type = MAPPGSZ_STK;
pgsz = map_pgsz(type, p, 0, 0, 1);
}
} else {
/*
* addr and len must be valid for range specified.
*/
if (valid_usr_range(addr, len, 0, as,
as->a_userlimit) != RANGE_OKAY) {
return (set_errno(ENOMEM));
}
/*
* Note that we don't disable automatic large page
* selection for anon segments based on use of
* memcntl().
*/
if (pgsz == 0) {
error = as_set_default_lpsize(as, addr, len);
if (error) {
(void) set_errno(error);
}
return (error);
}
/*
* addr and len must be prefered page size aligned
*/
if (!IS_P2ALIGNED(addr, pgsz) ||
!IS_P2ALIGNED(len, pgsz)) {
return (set_errno(EINVAL));
}
}
szc = mem_getpgszc(pgsz);
if (szc == (uint_t)-1) {
if ((pgcmd & (MHA_MAPSIZE_BSSBRK|MHA_MAPSIZE_STACK))
!= 0) {
as_rangeunlock(as);
}
return (set_errno(EINVAL));
}
/*
* For stack and heap operations we first need to pad
* out existing range (create new mappings) to the new
* prefered page size boundary. Also the start of the
* .bss for the heap or user's stack base may not be on
* the new prefered page size boundary. For these cases
* we align the base of the request on the new prefered
* page size.
*/
if (pgcmd & MHA_MAPSIZE_BSSBRK) {
if (szc == p->p_brkpageszc) {
as_rangeunlock(as);
return (0);
}
if (szc > p->p_brkpageszc) {
error = brk_internal(p->p_brkbase
+ p->p_brksize, szc);
if (error) {
as_rangeunlock(as);
return (set_errno(error));
}
}
/*
* It is possible for brk_internal to silently fail to
* promote the heap size, so don't panic or ASSERT.
*/
if (!IS_P2ALIGNED(p->p_brkbase + p->p_brksize, pgsz)) {
as_rangeunlock(as);
return (set_errno(ENOMEM));
}
oszc = p->p_brkpageszc;
p->p_brkpageszc = szc;
addr = (caddr_t)P2ROUNDUP((uintptr_t)p->p_bssbase,
pgsz);
len = (p->p_brkbase + p->p_brksize) - addr;
ASSERT(IS_P2ALIGNED(len, pgsz));
/*
* Perhaps no existing pages to promote.
*/
if (len == 0) {
as_rangeunlock(as);
return (0);
}
}
/*
* The code below, as does grow.c, assumes stacks always grow
* downward.
*/
if (pgcmd & MHA_MAPSIZE_STACK) {
if (szc == p->p_stkpageszc) {
as_rangeunlock(as);
return (0);
}
if (szc > p->p_stkpageszc) {
error = grow_internal(p->p_usrstack -
p->p_stksize, szc);
if (error) {
as_rangeunlock(as);
return (set_errno(error));
}
}
/*
* It is possible for grow_internal to silently fail to
* promote the stack size, so don't panic or ASSERT.
*/
if (!IS_P2ALIGNED(p->p_usrstack - p->p_stksize, pgsz)) {
as_rangeunlock(as);
return (set_errno(ENOMEM));
}
oszc = p->p_stkpageszc;
p->p_stkpageszc = szc;
addr = p->p_usrstack - p->p_stksize;
len = P2ALIGN(p->p_stksize, pgsz);
/*
* Perhaps nothing to promote.
*/
if (len == 0 || addr >= p->p_usrstack ||
(addr + len) < addr) {
as_rangeunlock(as);
return (0);
}
}
ASSERT(IS_P2ALIGNED(addr, pgsz));
ASSERT(IS_P2ALIGNED(len, pgsz));
error = as_setpagesize(as, addr, len, szc, B_TRUE);
/*
* On stack or heap failures restore original
* pg size code.
*/
if (error) {
if ((pgcmd & MHA_MAPSIZE_BSSBRK) != 0) {
p->p_brkpageszc = oszc;
}
if ((pgcmd & MHA_MAPSIZE_STACK) != 0) {
p->p_stkpageszc = oszc;
}
(void) set_errno(error);
}
if ((pgcmd & (MHA_MAPSIZE_BSSBRK|MHA_MAPSIZE_STACK)) != 0) {
as_rangeunlock(as);
}
return (error);
case MC_ADVISE:
if ((uintptr_t)arg == MADV_FREE ||
(uintptr_t)arg == MADV_PURGE) {
len &= PAGEMASK;
}
switch ((uintptr_t)arg) {
case MADV_WILLNEED:
fc = as_faulta(as, addr, len);
if (fc) {
if (FC_CODE(fc) == FC_OBJERR)
error = set_errno(FC_ERRNO(fc));
else if (FC_CODE(fc) == FC_NOMAP)
error = set_errno(ENOMEM);
else
error = set_errno(EINVAL);
return (error);
}
break;
case MADV_DONTNEED:
/*
* For now, don't need is turned into an as_ctl(MC_SYNC)
* operation flagged for async invalidate.
*/
error = as_ctl(as, addr, len, MC_SYNC, attr,
MS_ASYNC | MS_INVALIDATE, NULL, 0);
if (error)
(void) set_errno(error);
return (error);
default:
error = as_ctl(as, addr, len, cmd, attr,
(uintptr_t)arg, NULL, 0);
if (error)
(void) set_errno(error);
return (error);
}
break;
case MC_INHERIT_ZERO:
if (arg != 0 || attr != 0 || mask != 0)
return (set_errno(EINVAL));
break;
default:
return (set_errno(EINVAL));
}
error = as_ctl(as, addr, len, cmd, attr, (uintptr_t)arg, NULL, 0);
if (error)
(void) set_errno(error);
return (error);
}
/*
* Return page size code for page size passed in. If
* matching page size not found or supported, return -1.
*/
static uint_t
mem_getpgszc(size_t pgsz) {
return ((uint_t)page_szc_user_filtered(pgsz));
}
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/uio.h>
#include <sys/debug.h>
#include <c2/audit.h>
/*
* Make a directory.
*/
int
mkdirat(int fd, char *dname, int dmode)
{
vnode_t *startvp;
vnode_t *vp;
struct vattr vattr;
int error;
vattr.va_type = VDIR;
vattr.va_mode = dmode & PERMMASK;
vattr.va_mask = AT_TYPE|AT_MODE;
if (dname == NULL)
return (set_errno(EFAULT));
if ((error = fgetstartvp(fd, dname, &startvp)) != 0)
return (set_errno(error));
if (AU_AUDITING() && startvp != NULL)
audit_setfsat_path(1);
error = vn_createat(dname, UIO_USERSPACE, &vattr, EXCL, 0, &vp,
CRMKDIR, 0, PTOU(curproc)->u_cmask, startvp);
if (startvp != NULL)
VN_RELE(startvp);
if (error)
return (set_errno(error));
VN_RELE(vp);
return (0);
}
int
mkdir(char *dname, int dmode)
{
return (mkdirat(AT_FDCWD, dname, dmode));
}
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/user.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/mode.h>
#include <sys/uio.h>
#include <sys/mkdev.h>
#include <sys/policy.h>
#include <sys/debug.h>
#include <c2/audit.h>
/*
* Create a special file, a regular file, or a FIFO.
* fname - pathname passed by user
* fmode - mode of pathname
* dev = device number - b/c specials only
*/
int
mknodat(int fd, char *fname, mode_t fmode, dev_t dev)
{
vnode_t *startvp;
vnode_t *vp;
struct vattr vattr;
int error;
enum create why;
/*
* Zero type is equivalent to a regular file.
*/
if ((fmode & S_IFMT) == 0)
fmode |= S_IFREG;
/*
* Must be privileged unless making a FIFO node.
*/
if (((fmode & S_IFMT) != S_IFIFO) && secpolicy_sys_devices(CRED()) != 0)
return (set_errno(EPERM));
/*
* Set up desired attributes and vn_create the file.
*/
vattr.va_type = IFTOVT(fmode);
vattr.va_mode = fmode & MODEMASK;
vattr.va_mask = AT_TYPE|AT_MODE;
if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
if (get_udatamodel() != DATAMODEL_NATIVE)
dev = expldev(dev);
if (dev == NODEV || (getemajor(dev)) == (major_t)NODEV)
return (set_errno(EINVAL));
vattr.va_rdev = dev;
vattr.va_mask |= AT_RDEV;
}
if (fname == NULL)
return (set_errno(EFAULT));
if ((error = fgetstartvp(fd, fname, &startvp)) != 0)
return (set_errno(error));
if (AU_AUDITING() && startvp != NULL)
audit_setfsat_path(1);
why = ((fmode & S_IFMT) == S_IFDIR) ? CRMKDIR : CRMKNOD;
error = vn_createat(fname, UIO_USERSPACE, &vattr, EXCL, 0, &vp,
why, 0, PTOU(curproc)->u_cmask, startvp);
if (startvp != NULL)
VN_RELE(startvp);
if (error)
return (set_errno(error));
VN_RELE(vp);
return (0);
}
int
mknod(char *fname, mode_t fmode, dev_t dev)
{
return (mknodat(AT_FDCWD, fname, fmode, dev));
}
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/mman.h>
#include <sys/cred.h>
#include <sys/model.h>
#include <sys/vnode.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/file.h>
#include <sys/vfs.h>
#include <sys/sysmacros.h>
#include <sys/mmapobj.h>
/*
* We will "allocate" this many mmapobj_result_t segments on the stack
* in an attempt to avoid the need to call kmem_alloc. This value should
* cover 99% of the known ELF libraries as well as AOUT (4.x) libraries.
*/
#define MOBJ_STACK_SEGS 6
static void
mmapobj_copy_64to32(mmapobj_result_t *source, mmapobj_result32_t *dest, int num)
{
int i;
for (i = 0; i < num; i++) {
dest[i].mr_addr = (caddr32_t)(uintptr_t)source[i].mr_addr;
dest[i].mr_msize = (size32_t)source[i].mr_msize;
dest[i].mr_fsize = (size32_t)source[i].mr_fsize;
dest[i].mr_offset = (size32_t)source[i].mr_offset;
dest[i].mr_prot = source[i].mr_prot;
dest[i].mr_flags = source[i].mr_flags;
}
}
int
mmapobjsys(int fd, uint_t flags, mmapobj_result_t *storage,
uint_t *elements, void *arg)
{
uint_t num_mapped;
uint_t num_in;
int error;
int old_error;
size_t padding = 0;
mmapobj_result_t stack_mr[MOBJ_STACK_SEGS];
mmapobj_result_t *mrp = stack_mr;
struct file *fp;
struct vnode *vp;
model_t model;
int convert_64to32 = 0;
uint_t alloc_num = 0;
/* Verify flags */
if ((flags & ~MMOBJ_ALL_FLAGS) != 0) {
return (set_errno(EINVAL));
}
if (((flags & MMOBJ_PADDING) == 0) && arg != NULL) {
return (set_errno(EINVAL));
}
fp = getf(fd);
if (fp == NULL) {
return (set_errno(EBADF));
}
vp = fp->f_vnode;
if ((fp->f_flag & FREAD) == 0) {
error = EACCES;
goto out;
}
error = copyin(elements, &num_mapped, sizeof (uint_t));
if (error) {
error = EFAULT;
goto out;
}
num_in = num_mapped;
model = get_udatamodel();
if (model != DATAMODEL_NATIVE) {
ASSERT(model == DATAMODEL_ILP32);
convert_64to32 = 1;
}
if (flags & MMOBJ_PADDING) {
if (convert_64to32) {
size32_t padding32;
error = copyin(arg, &padding32, sizeof (padding32));
padding = padding32;
} else {
error = copyin(arg, &padding, sizeof (padding));
}
if (error) {
error = EFAULT;
goto out;
}
/*
* Need to catch overflow here for the 64 bit case. For the
* 32 bit case, overflow would round up to 4G which would
* not be able to fit in any address space and thus ENOMEM
* would be returned after calling into mmapobj.
*/
if (padding) {
padding = P2ROUNDUP(padding, PAGESIZE);
if (padding == 0) {
error = ENOMEM;
goto out;
}
}
/* turn off padding if no bytes were requested */
if (padding == 0) {
flags = flags & (~MMOBJ_PADDING);
}
}
if (num_mapped > MOBJ_STACK_SEGS) {
num_mapped = MOBJ_STACK_SEGS;
}
retry:
error = mmapobj(vp, flags, mrp, &num_mapped, padding, fp->f_cred);
if (error == E2BIG && alloc_num == 0) {
if (num_mapped > MOBJ_STACK_SEGS && num_mapped <= num_in) {
mrp = kmem_alloc(sizeof (mmapobj_result_t) * num_mapped,
KM_SLEEP);
alloc_num = num_mapped;
goto retry;
}
}
old_error = error;
if (error == 0 || error == E2BIG) {
error = copyout(&num_mapped, elements, sizeof (uint_t));
if (error) {
error = EFAULT;
/*
* We only mapped in segments if the mmapobj call
* succeeded, so only unmap for that case.
*/
if (old_error == 0) {
mmapobj_unmap(mrp, num_mapped, num_mapped, 0);
}
} else if (num_in < num_mapped) {
ASSERT(old_error == E2BIG);
error = E2BIG;
} else {
if (convert_64to32) {
mmapobj_result32_t *mrp32;
/* Need to translate from 64bit to 32bit */
mrp32 = kmem_alloc(num_mapped * sizeof (*mrp32),
KM_SLEEP);
mmapobj_copy_64to32(mrp, mrp32, num_mapped);
error = copyout(mrp32, (void *)storage,
num_mapped * sizeof (mmapobj_result32_t));
kmem_free(mrp32, num_mapped * sizeof (*mrp32));
} else {
error = copyout(mrp, (void *)storage,
num_mapped * sizeof (mmapobj_result_t));
}
if (error) {
error = EFAULT;
mmapobj_unmap(mrp, num_mapped, num_mapped, 0);
}
}
}
/*
* If stack_mr was not large enough, then we had to allocate
* a larger piece of memory to hold the mmapobj_result array.
*/
if (alloc_num != 0) {
ASSERT(mrp != stack_mr);
ASSERT(num_mapped > MOBJ_STACK_SEGS);
kmem_free(mrp,
alloc_num * sizeof (mmapobj_result_t));
}
out:
releasef(fd);
if (error) {
return (set_errno(error));
} else {
return (0);
}
}
/*
* 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.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/user.h>
#include <sys/fstyp.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/mount.h>
#include <sys/vfs.h>
#include <sys/cred.h>
#include <sys/vnode.h>
#include <sys/dnlc.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/cmn_err.h>
#include <sys/swap.h>
#include <sys/debug.h>
#include <sys/pathname.h>
#include <sys/cladm.h>
/*
* System calls.
*/
/*
* "struct mounta" defined in sys/vfs.h.
*/
/* ARGSUSED */
int
mount(long *lp, rval_t *rp)
{
vnode_t *vp = NULL;
struct vfs *vfsp; /* dummy argument */
int error;
struct mounta *uap;
#if defined(_LP64)
struct mounta native;
/*
* Make a struct mounta if we are DATAMODEL_LP64
*/
uap = &native;
uap->spec = (char *)*lp++;
uap->dir = (char *)*lp++;
uap->flags = (int)*lp++;
uap->fstype = (char *)*lp++;
uap->dataptr = (char *)*lp++;
uap->datalen = (int)*lp++;
uap->optptr = (char *)*lp++;
uap->optlen = (int)*lp++;
#else /* !defined(_LP64) */
/*
* 32 bit kernels can take a shortcut and just cast
* the args array to the structure.
*/
uap = (struct mounta *)lp;
#endif /* _LP64 */
/*
* Resolve second path name (mount point).
*/
if (error = lookupname(uap->dir, UIO_USERSPACE, FOLLOW, NULLVPP, &vp))
return (set_errno(error));
/*
* Some mount flags are disallowed through the system call interface.
*/
uap->flags &= MS_MASK;
if ((vp->v_flag & VPXFS) && ((uap->flags & MS_GLOBAL) != MS_GLOBAL)) {
/*
* Clustering: if we're doing a mount onto the global
* namespace, and the mount is not a global mount, return
* an error.
*/
error = ENOTSUP;
} else if (uap->flags & MS_GLOBAL) {
/*
* Clustering: global mount specified.
*/
if ((cluster_bootflags & CLUSTER_BOOTED) == 0) {
/*
* If we're not booted as a cluster,
* global mounts are not allowed.
*/
error = ENOTSUP;
} else {
error = domount("pxfs", uap, vp, CRED(), &vfsp);
if (!error)
VFS_RELE(vfsp);
}
} else {
error = domount(NULL, uap, vp, CRED(), &vfsp);
if (!error)
VFS_RELE(vfsp);
}
VN_RELE(vp);
rp->r_val2 = error;
return (error ? set_errno(error) : 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/cred.h>
#include <sys/proc.h>
#include <sys/debug.h>
#include <sys/class.h>
#include <sys/mutex.h>
#include <sys/schedctl.h>
/*
* We support the nice system call for compatibility although
* the priocntl system call supports a superset of nice's functionality.
* We support nice only for time sharing threads. It will fail
* if called by a thread from another class.
*/
int
nice(int niceness)
{
int error = 0;
int err, retval;
kthread_t *t;
proc_t *p = curproc;
mutex_enter(&p->p_lock);
t = p->p_tlist;
do {
err = CL_DONICE(t, CRED(), niceness, &retval);
schedctl_set_cidpri(t);
if (error == 0 && err)
error = set_errno(err);
} while ((t = t->t_forw) != p->p_tlist);
mutex_exit(&p->p_lock);
if (error)
return (error);
return (retval);
}
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) David L. Mills 1993, 1994
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appears in all copies and that both the
* copyright notice and this permission notice appear in supporting
* documentation, and that the name University of Delaware not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. The University of Delaware
* makes no representations about the suitability this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/*
* Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
*/
/*
* Modification history kern_ntptime.c
*
* 24 Sep 94 David L. Mills
* Tightened code at exits.
*
* 24 Mar 94 David L. Mills
* Revised syscall interface to include new variables for PPS
* time discipline.
*
* 14 Feb 94 David L. Mills
* Added code for external clock
*
* 28 Nov 93 David L. Mills
* Revised frequency scaling to conform with adjusted parameters
*
* 17 Sep 93 David L. Mills
* Created file
*/
/*
* ntp_gettime(), ntp_adjtime() - precision time interface
*
* These routines consitute the Network Time Protocol (NTP) interfaces
* for user and daemon application programs. The ntp_gettime() routine
* provides the time, maximum error (synch distance) and estimated error
* (dispersion) to client user application programs. The ntp_adjtime()
* routine is used by the NTP daemon to adjust the system clock to an
* externally derived time. The time offset and related variables set by
* this routine are used by clock() to adjust the phase and
* frequency of the phase-lock loop which controls the system clock.
*/
#include <sys/param.h>
#include <sys/user.h>
#include <sys/vnode.h>
#include <sys/proc.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/cpuvar.h>
#include <sys/timer.h>
#include <sys/debug.h>
#include <sys/timex.h>
#include <sys/model.h>
#include <sys/policy.h>
/*
* ntp_gettime() - NTP user application interface
*/
int
ntp_gettime(struct ntptimeval *tp)
{
timestruc_t tod;
struct ntptimeval ntv;
model_t datamodel = get_udatamodel();
gethrestime(&tod);
if (tod.tv_sec > TIME32_MAX)
return (set_errno(EOVERFLOW));
ntv.time.tv_sec = tod.tv_sec;
ntv.time.tv_usec = tod.tv_nsec / (NANOSEC / MICROSEC);
ntv.maxerror = time_maxerror;
ntv.esterror = time_esterror;
if (datamodel == DATAMODEL_NATIVE) {
if (copyout(&ntv, tp, sizeof (ntv)))
return (set_errno(EFAULT));
} else {
struct ntptimeval32 ntv32;
if (TIMEVAL_OVERFLOW(&ntv.time))
return (set_errno(EOVERFLOW));
TIMEVAL_TO_TIMEVAL32(&ntv32.time, &ntv.time);
ntv32.maxerror = ntv.maxerror;
ntv32.esterror = ntv.esterror;
if (copyout(&ntv32, tp, sizeof (ntv32)))
return (set_errno(EFAULT));
}
/*
* Status word error decode. If any of these conditions
* occur, an error is returned, instead of the status
* word. Most applications will care only about the fact
* the system clock may not be trusted, not about the
* details.
*
* Hardware or software error
*/
if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
/*
* PPS signal lost when either time or frequency
* synchronization requested
*/
(time_status & (STA_PPSFREQ | STA_PPSTIME) &&
!(time_status & STA_PPSSIGNAL)) ||
/*
* PPS jitter exceeded when time synchronization
* requested
*/
(time_status & STA_PPSTIME && time_status & STA_PPSJITTER) ||
/*
* PPS wander exceeded or calibration error when
* frequency synchronization requested
*/
(time_status & STA_PPSFREQ &&
time_status & (STA_PPSWANDER | STA_PPSERROR))) {
return (TIME_ERROR);
}
return (time_state);
}
/*
* ntp_adjtime() - NTP daemon application interface
*/
int
ntp_adjtime(struct timex *tp)
{
struct timex ntv;
int modes;
if (copyin(tp, &ntv, sizeof (ntv)))
return (set_errno(EFAULT));
/*
* Update selected clock variables - only privileged users can
* change anything. Note that there is no error checking here on
* the assumption privileged users know what they're doing.
*/
modes = ntv.modes;
if (modes != 0 && secpolicy_settime(CRED()) != 0)
return (set_errno(EPERM));
/*
* If the time constant is being set, validate it first so that
* no changes are made if it is out of range.
*/
if ((modes & MOD_TIMECONST) && (ntv.constant < 0 || ntv.constant > 30))
return (set_errno(EINVAL));
mutex_enter(&tod_lock);
if (modes & MOD_MAXERROR)
time_maxerror = ntv.maxerror;
if (modes & MOD_ESTERROR)
time_esterror = ntv.esterror;
if (modes & MOD_STATUS) {
time_status &= STA_RONLY;
time_status |= ntv.status & ~STA_RONLY;
}
if (modes & MOD_TIMECONST)
time_constant = ntv.constant;
if (modes & MOD_OFFSET)
clock_update(ntv.offset);
/*
* clock_update() updates time_freq so keep MOD_FREQUENCY after
* MOD_OFFSET.
*/
if (modes & MOD_FREQUENCY) {
time_freq = ntv.freq - pps_freq;
/*
* If the frequency offset was adjusted, then set tod_needsync
* since it implies someone is watching over the system clock.
*/
int s = hr_clock_lock();
tod_needsync = 1;
hr_clock_unlock(s);
}
/*
* Retrieve all clock variables
*/
ntv.offset = time_offset / SCALE_UPDATE;
ntv.freq = time_freq + pps_freq;
ntv.maxerror = time_maxerror;
ntv.esterror = time_esterror;
ntv.status = time_status;
ntv.constant = time_constant;
ntv.precision = time_precision;
ntv.tolerance = time_tolerance;
ntv.shift = pps_shift;
ntv.ppsfreq = pps_freq;
ntv.jitter = pps_jitter >> PPS_AVG;
ntv.stabil = pps_stabil;
ntv.calcnt = pps_calcnt;
ntv.errcnt = pps_errcnt;
ntv.jitcnt = pps_jitcnt;
ntv.stbcnt = pps_stbcnt;
mutex_exit(&tod_lock);
if (copyout(&ntv, tp, sizeof (ntv)))
return (set_errno(EFAULT));
/*
* Status word error decode. See comments in
* ntp_gettime() routine.
*/
if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
(time_status & (STA_PPSFREQ | STA_PPSTIME) &&
!(time_status & STA_PPSSIGNAL)) ||
(time_status & STA_PPSTIME &&
time_status & STA_PPSJITTER) ||
(time_status & STA_PPSFREQ &&
time_status & (STA_PPSWANDER | STA_PPSERROR)))
return (TIME_ERROR);
return (time_state);
}
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
* Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
*/
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/user.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <sys/vnode.h>
#include <sys/vfs.h>
#include <sys/file.h>
#include <sys/mode.h>
#include <sys/uio.h>
#include <sys/debug.h>
#include <c2/audit.h>
/*
* Common code for openat(). Check permissions, allocate an open
* file structure, and call the device open routine (if any).
*/
static int
copen(int startfd, char *fname, int filemode, int createmode)
{
struct pathname pn;
vnode_t *vp, *sdvp;
file_t *fp, *startfp;
enum vtype type;
int error;
int fd, dupfd;
vnode_t *startvp;
proc_t *p = curproc;
uio_seg_t seg = UIO_USERSPACE;
char *open_filename = fname;
uint32_t auditing = AU_AUDITING();
char startchar;
if (filemode & (FSEARCH|FEXEC)) {
/*
* Must be one or the other and neither FREAD nor FWRITE
* Must not be any of FAPPEND FCREAT FTRUNC FXATTR FXATTRDIROPEN
* XXX: Should these just be silently ignored?
*/
if ((filemode & (FREAD|FWRITE)) ||
(filemode & (FSEARCH|FEXEC)) == (FSEARCH|FEXEC) ||
(filemode & (FAPPEND|FCREAT|FTRUNC|FXATTR|FXATTRDIROPEN)))
return (set_errno(EINVAL));
}
if (startfd == AT_FDCWD) {
/*
* Regular open()
*/
startvp = NULL;
} else {
/*
* We're here via openat()
*/
if (copyin(fname, &startchar, sizeof (char)))
return (set_errno(EFAULT));
/*
* if startchar is / then startfd is ignored
*/
if (startchar == '/')
startvp = NULL;
else {
if ((startfp = getf(startfd)) == NULL)
return (set_errno(EBADF));
startvp = startfp->f_vnode;
VN_HOLD(startvp);
releasef(startfd);
}
}
/*
* Handle __openattrdirat() requests
*/
if (filemode & FXATTRDIROPEN) {
if (auditing && startvp != NULL)
audit_setfsat_path(1);
error = lookupnameat(fname, seg, FOLLOW, NULLVPP, &vp, startvp);
if (startvp != NULL)
VN_RELE(startvp);
if (error != 0)
return (set_errno(error));
startvp = vp;
}
/*
* Do we need to go into extended attribute space?
*/
if (filemode & FXATTR) {
if (startfd == AT_FDCWD) {
if (copyin(fname, &startchar, sizeof (char)))
return (set_errno(EFAULT));
/*
* If startchar == '/' then no extended attributes
* are looked up.
*/
if (startchar == '/') {
startvp = NULL;
} else {
mutex_enter(&p->p_lock);
startvp = PTOU(p)->u_cdir;
VN_HOLD(startvp);
mutex_exit(&p->p_lock);
}
}
/*
* Make sure we have a valid extended attribute request.
* We must either have a real fd or AT_FDCWD and a relative
* pathname.
*/
if (startvp == NULL) {
goto noxattr;
}
}
if (filemode & (FXATTR|FXATTRDIROPEN)) {
vattr_t vattr;
if (error = pn_get(fname, UIO_USERSPACE, &pn)) {
goto out;
}
/*
* In order to access hidden attribute directory the
* user must be able to stat() the file
*/
vattr.va_mask = AT_ALL;
if (error = VOP_GETATTR(startvp, &vattr, 0, CRED(), NULL)) {
pn_free(&pn);
goto out;
}
if ((startvp->v_vfsp->vfs_flag & VFS_XATTR) != 0 ||
vfs_has_feature(startvp->v_vfsp, VFSFT_SYSATTR_VIEWS)) {
error = VOP_LOOKUP(startvp, "", &sdvp, &pn,
(filemode & FXATTRDIROPEN) ? LOOKUP_XATTR :
LOOKUP_XATTR|CREATE_XATTR_DIR, rootvp, CRED(),
NULL, NULL, NULL);
} else {
error = EINVAL;
}
/*
* For __openattrdirat() use "." as filename to open
* as part of vn_openat()
*/
if (error == 0 && (filemode & FXATTRDIROPEN)) {
open_filename = ".";
seg = UIO_SYSSPACE;
}
pn_free(&pn);
if (error != 0)
goto out;
VN_RELE(startvp);
startvp = sdvp;
}
noxattr:
if ((filemode & (FREAD|FWRITE|FSEARCH|FEXEC|FXATTRDIROPEN)) != 0) {
if ((filemode & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
filemode &= ~FNDELAY;
error = falloc((vnode_t *)NULL, filemode, &fp, &fd);
if (error == 0) {
if (auditing && startvp != NULL)
audit_setfsat_path(1);
/*
* Last arg is a don't-care term if
* !(filemode & FCREAT).
*/
error = vn_openat(open_filename, seg, filemode,
(int)(createmode & MODEMASK),
&vp, CRCREAT, PTOU(curproc)->u_cmask,
startvp, fd);
if (startvp != NULL)
VN_RELE(startvp);
if (error == 0) {
if ((vp->v_flag & VDUP) == 0) {
fp->f_vnode = vp;
mutex_exit(&fp->f_tlock);
/*
* We must now fill in the slot
* falloc reserved.
*/
setf(fd, fp);
if ((filemode & FCLOEXEC) != 0) {
f_setfd_or(fd, FD_CLOEXEC);
}
if ((filemode & FCLOFORK) != 0) {
f_setfd_or(fd, FD_CLOFORK);
}
return (fd);
} else {
/*
* Special handling for /dev/fd.
* Give up the file pointer
* and dup the indicated file descriptor
* (in v_rdev). This is ugly, but I've
* seen worse.
*/
unfalloc(fp);
dupfd = getminor(vp->v_rdev);
type = vp->v_type;
mutex_enter(&vp->v_lock);
vp->v_flag &= ~VDUP;
mutex_exit(&vp->v_lock);
VN_RELE(vp);
if (type != VCHR)
return (set_errno(EINVAL));
if ((fp = getf(dupfd)) == NULL) {
setf(fd, NULL);
return (set_errno(EBADF));
}
mutex_enter(&fp->f_tlock);
fp->f_count++;
mutex_exit(&fp->f_tlock);
setf(fd, fp);
if ((filemode & FCLOEXEC) != 0) {
f_setfd_or(fd, FD_CLOEXEC);
}
if ((filemode & FCLOFORK) != 0) {
f_setfd_or(fd, FD_CLOFORK);
}
releasef(dupfd);
}
return (fd);
} else {
setf(fd, NULL);
unfalloc(fp);
return (set_errno(error));
}
}
} else {
error = EINVAL;
}
out:
if (startvp != NULL)
VN_RELE(startvp);
return (set_errno(error));
}
#define OPENMODE32(fmode) (((fmode) & (FSEARCH | FEXEC))? \
(fmode) : (fmode) - FOPEN)
#define OPENMODE64(fmode) (OPENMODE32(fmode) | FOFFMAX)
#ifdef _LP64
#define OPENMODE(fmode) OPENMODE64(fmode)
#else
#define OPENMODE(fmode) OPENMODE32(fmode)
#endif
/*
* Open a file.
*/
int
openat(int fd, char *path, int fmode, int cmode)
{
return (copen(fd, path, OPENMODE(fmode), cmode));
}
int
open(char *path, int fmode, int cmode)
{
return (openat(AT_FDCWD, path, fmode, cmode));
}
#if defined(_ILP32) || defined(_SYSCALL32_IMPL)
/*
* Open for large files in 32-bit environment. Sets the FOFFMAX flag.
*/
int
openat64(int fd, char *path, int fmode, int cmode)
{
return (copen(fd, path, OPENMODE64(fmode), cmode));
}
int
open64(char *path, int fmode, int cmode)
{
return (openat64(AT_FDCWD, path, fmode, cmode));
}
#endif /* _ILP32 || _SYSCALL32_IMPL */
#ifdef _SYSCALL32_IMPL
/*
* Open for 32-bit compatibility on 64-bit kernel
*/
int
openat32(int fd, char *path, int fmode, int cmode)
{
return (copen(fd, path, OPENMODE32(fmode), cmode));
}
int
open32(char *path, int fmode, int cmode)
{
return (openat32(AT_FDCWD, path, fmode, cmode));
}
#endif /* _SYSCALL32_IMPL */
/*
* 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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright 2019 Joyent, Inc.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/var.h>
#include <sys/thread.h>
#include <sys/cpuvar.h>
#include <sys/kstat.h>
#include <sys/uadmin.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/cmn_err.h>
#include <sys/procset.h>
#include <sys/processor.h>
#include <sys/debug.h>
#include <sys/policy.h>
#include <sys/smt.h>
/*
* CPU state diagram
*
* P_SPARE
* P_POWEROFF <---> P_OFFLINE <---> P_ONLINE <---> P_NOINTR
* P_FAULTED
* P_DISABLED
*/
int
p_online_internal_locked(processorid_t cpun, int new_status, int *old_status)
{
cpu_t *cp;
int status;
int error = 0;
int flags = 0;
ASSERT(MUTEX_HELD(&cpu_lock));
if (cpun == P_ALL_SIBLINGS) {
if (new_status != P_DISABLED) {
error = EINVAL;
goto out;
}
return (smt_disable());
}
if ((cp = cpu_get(cpun)) == NULL) {
error = EINVAL;
goto out;
}
if (new_status & P_FORCED)
flags = CPU_FORCED;
*old_status = status = cpu_get_state(cp); /* get processor status */
new_status &= ~P_FORCED;
/*
* Perform credentials check.
*/
switch (new_status) {
case P_STATUS:
goto out;
case P_ONLINE:
case P_OFFLINE:
case P_NOINTR:
case P_FAULTED:
case P_SPARE:
if (secpolicy_ponline(CRED()) != 0)
error = EPERM;
break;
case P_DISABLED:
default:
error = EINVAL;
break;
}
if (error)
goto out;
/*
* return 0 if the CPU is already in the desired new state.
*/
if (status == new_status)
goto out;
switch (new_status) {
case P_ONLINE:
switch (status) {
case P_POWEROFF:
/*
* If CPU is powered off, power it on.
*/
if (error = cpu_poweron(cp))
break;
ASSERT(cpu_get_state(cp) == P_OFFLINE);
/* FALLTHROUGH */
case P_DISABLED:
case P_OFFLINE:
case P_FAULTED:
case P_SPARE:
/*
* If CPU is in one of the offline states,
* bring it online.
*/
error = cpu_online(cp, flags);
break;
case P_NOINTR:
cpu_intr_enable(cp);
break;
}
break;
case P_OFFLINE:
switch (status) {
case P_NOINTR:
/*
* Before we take the CPU offline, we first enable I/O
* interrupts.
*/
cpu_intr_enable(cp);
/* FALLTHROUGH */
case P_ONLINE:
case P_DISABLED:
case P_FAULTED:
case P_SPARE:
/*
* CPU is online, or in a special offline state.
* Take it offline.
*/
error = cpu_offline(cp, flags);
break;
case P_POWEROFF:
/*
* If CPU is powered off, power it on.
*/
error = cpu_poweron(cp);
break;
}
break;
case P_NOINTR:
switch (status) {
case P_POWEROFF:
/*
* if CPU is powered off, power it on.
*/
if (error = cpu_poweron(cp))
break;
ASSERT(cpu_get_state(cp) == P_OFFLINE);
/* FALLTHROUGH */
case P_DISABLED:
case P_OFFLINE:
case P_FAULTED:
case P_SPARE:
/*
* First, bring the CPU online.
*/
if (error = cpu_online(cp, flags))
break;
/* FALLTHROUGH */
case P_ONLINE:
/*
* CPU is now online. Try to disable interrupts.
*/
error = cpu_intr_disable(cp);
break;
}
break;
case P_FAULTED:
switch (status) {
case P_POWEROFF:
/*
* If CPU is powered off, power it on.
*/
if (error = cpu_poweron(cp))
break;
ASSERT(cpu_get_state(cp) == P_OFFLINE);
/*FALLTHROUGH*/
case P_DISABLED:
case P_OFFLINE:
case P_ONLINE:
case P_NOINTR:
case P_SPARE:
/*
* Mark this CPU as faulted.
*/
error = cpu_faulted(cp, flags);
break;
}
break;
case P_SPARE:
switch (status) {
case P_POWEROFF:
/*
* If CPU is powered off, power it on.
*/
if (error = cpu_poweron(cp))
break;
ASSERT(cpu_get_state(cp) == P_OFFLINE);
/*FALLTHROUGH*/
case P_DISABLED:
case P_OFFLINE:
case P_FAULTED:
case P_ONLINE:
case P_NOINTR:
/*
* Mark this CPU as a spare.
*/
error = cpu_spare(cp, flags);
break;
}
break;
}
out:
return (error);
}
int
p_online_internal(processorid_t cpun, int new_status, int *old_status)
{
int rc;
mutex_enter(&cpu_lock); /* protects CPU states */
rc = p_online_internal_locked(cpun, new_status, old_status);
mutex_exit(&cpu_lock);
return (rc);
}
/*
* p_online(2) - get/change processor operational status.
*
* As noted in os/cpu.c, the P_ONLINE and other state constants are for use
* only in this system call path and other paths conveying CPU state to
* userland. In general, other kernel consumers should be using the accessor
* functions in uts/common/os/cpu.c.
*/
int
p_online(processorid_t cpun, int new_status)
{
int ret;
int old_status;
ret = p_online_internal(cpun, new_status, &old_status);
if (ret != 0)
return (set_errno(ret));
return (old_status);
}
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/pathname.h>
#include <sys/vnode.h>
#include <sys/vfs.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/debug.h>
#include <fs/fs_subr.h>
/*
* Common code for pathconf(), fpathconf() system calls
*/
static long
cpathconf(register vnode_t *vp, int cmd, struct cred *cr)
{
struct statvfs64 sb;
int error;
ulong_t val;
switch (cmd) {
case _PC_2_SYMLINKS:
if (error = VOP_PATHCONF(vp, _PC_SYMLINK_MAX, &val, cr, NULL))
return ((long)set_errno(error));
return ((long)(val > 0));
case _PC_ALLOC_SIZE_MIN:
case _PC_REC_INCR_XFER_SIZE:
case _PC_REC_MAX_XFER_SIZE:
case _PC_REC_MIN_XFER_SIZE:
case _PC_REC_XFER_ALIGN:
if ((error = VFS_STATVFS(vp->v_vfsp, &sb)) != 0)
return ((long)set_errno(error));
/*
* There is generally no harm in doing larger transfers, but
* there's a point of diminishing returns. With 1MB transfers,
* even if they're random, you get very close to platter speed.
* Se we return 1MB as the maximum transfer size.
*/
if (cmd == _PC_REC_MAX_XFER_SIZE)
return ((long)MAX(sb.f_bsize, 1UL << 20));
/*
* By definition, f_frsize is the smallest filesystem block.
* However, _PC_ALLOC_SIZE_MIN is intended to define the
* threshold for direct I/O. This implies two requirements:
* the VM and I/O subsystems must be able to create mappings
* for DMA, which requires at least page alignment; and the
* filesystem must avoid read/modify/write, which generally
* requires multiples of its 'preferred' blocksize, f_bsize.
*
* PAGESIZE alignment is sufficient for DMA and block copy.
* Rounding up to the filesystem 'preferred' blocksize
* works just as well.
*
* All together, this means that the remaining parameters
* map into the same value.
*/
return ((long)MAX(sb.f_bsize, PAGESIZE));
case _PC_ASYNC_IO:
return (1l);
case _PC_PRIO_IO:
return ((long)set_errno(EINVAL));
case _PC_SYNC_IO:
if (!(error = VOP_FSYNC(vp, FSYNC, cr, NULL)))
return (1l);
return ((long)set_errno(error));
case _PC_XATTR_ENABLED:
return ((vp->v_vfsp->vfs_flag & VFS_XATTR) ? 1 : 0);
default:
if (error = VOP_PATHCONF(vp, cmd, &val, cr, NULL))
return ((long)set_errno(error));
return (val);
}
/* NOTREACHED */
}
/* fpathconf/pathconf interfaces */
long
fpathconf(int fdes, int name)
{
file_t *fp;
long retval;
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
retval = cpathconf(fp->f_vnode, name, fp->f_cred);
releasef(fdes);
return (retval);
}
long
pathconf(char *fname, int name)
{
vnode_t *vp;
long retval;
int error;
int estale_retry = 0;
lookup:
if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return ((long)set_errno(error));
}
retval = cpathconf(vp, name, CRED());
VN_RELE(vp);
return (retval);
}
/*
* 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 (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1994-2000 by Sun Microsystems, Inc.
* All rights reserved.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/condvar.h>
#include <sys/debug.h>
/*
* Indefinite wait.
*/
int
pause()
{
mutex_enter(&curthread->t_delay_lock);
while (cv_wait_sig_swap(&curthread->t_delay_cv,
&curthread->t_delay_lock))
;
mutex_exit(&curthread->t_delay_lock);
return (set_errno(EINTR));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/session.h>
#include <sys/debug.h>
/* ARGSUSED */
int
setpgrp(int flag, int pid, int pgid)
{
proc_t *p = curproc;
int retval = 0;
int sid;
switch (flag) {
case 1: /* setpgrp() */
mutex_enter(&pidlock);
if (p->p_sessp->s_sidp != p->p_pidp && !pgmembers(p->p_pid)) {
mutex_exit(&pidlock);
sess_create();
} else
mutex_exit(&pidlock);
mutex_enter(&p->p_splock);
sid = p->p_sessp->s_sid;
mutex_exit(&p->p_splock);
return (sid);
case 3: /* setsid() */
mutex_enter(&pidlock);
if (p->p_pgidp == p->p_pidp || pgmembers(p->p_pid)) {
mutex_exit(&pidlock);
return (set_errno(EPERM));
}
mutex_exit(&pidlock);
sess_create();
mutex_enter(&p->p_splock);
sid = p->p_sessp->s_sid;
mutex_exit(&p->p_splock);
return (sid);
case 5: /* setpgid() */
{
mutex_enter(&pidlock);
if (pid == 0)
pid = p->p_pid;
else if (pid < 0 || pid >= maxpid) {
mutex_exit(&pidlock);
return (set_errno(EINVAL));
} else if (pid != p->p_pid) {
for (p = p->p_child; /* empty */; p = p->p_sibling) {
if (p == NULL) {
mutex_exit(&pidlock);
return (set_errno(ESRCH));
}
if (p->p_pid == pid)
break;
}
if (p->p_flag & SEXECED) {
mutex_exit(&pidlock);
return (set_errno(EACCES));
}
if (p->p_sessp != ttoproc(curthread)->p_sessp) {
mutex_exit(&pidlock);
return (set_errno(EPERM));
}
}
if (p->p_sessp->s_sid == pid) {
mutex_exit(&pidlock);
return (set_errno(EPERM));
}
if (pgid == 0)
pgid = p->p_pid;
else if (pgid < 0 || pgid >= maxpid) {
mutex_exit(&pidlock);
return (set_errno(EINVAL));
}
if (p->p_pgrp == pgid) {
mutex_exit(&pidlock);
break;
} else if (p->p_pid == pgid) {
/*
* We need to protect p_pgidp with p_lock because
* /proc looks at it while holding only p_lock.
*/
mutex_enter(&p->p_lock);
pgexit(p);
pgjoin(p, p->p_pidp);
mutex_exit(&p->p_lock);
} else {
register proc_t *q;
if ((q = pgfind(pgid)) == NULL ||
q->p_sessp != p->p_sessp) {
mutex_exit(&pidlock);
return (set_errno(EPERM));
}
/*
* See comment above about p_lock and /proc
*/
mutex_enter(&p->p_lock);
pgexit(p);
pgjoin(p, q->p_pgidp);
mutex_exit(&p->p_lock);
}
mutex_exit(&pidlock);
break;
}
case 0: /* getpgrp() */
mutex_enter(&pidlock);
retval = p->p_pgrp;
mutex_exit(&pidlock);
break;
case 2: /* getsid() */
case 4: /* getpgid() */
if (pid < 0 || pid >= maxpid) {
return (set_errno(EINVAL));
}
mutex_enter(&pidlock);
if (pid != 0 && p->p_pid != pid &&
((p = prfind(pid)) == NULL || p->p_stat == SIDL)) {
mutex_exit(&pidlock);
return (set_errno(ESRCH));
}
if (flag == 2)
retval = p->p_sessp->s_sid;
else
retval = p->p_pgrp;
mutex_exit(&pidlock);
break;
}
return (retval);
}
/*
* 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 2013 OmniTI Computer Consulting, Inc. All rights reserved.
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2011 Bayard G. Bell. All rights reserved.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/errno.h>
#include <sys/debug.h>
#include <sys/fs/fifonode.h>
#include <sys/fcntl.h>
/*
* This is the loadable module wrapper.
*/
#include <sys/modctl.h>
#include <sys/syscall.h>
int pipe(intptr_t fds, int);
static struct sysent pipe_sysent = {
2,
SE_ARGC | SE_32RVAL1 | SE_NOUNLOAD,
(int (*)())pipe
};
/*
* Module linkage information for the kernel.
*/
static struct modlsys modlsys = {
&mod_syscallops, "pipe(2) syscall", &pipe_sysent
};
#ifdef _SYSCALL32_IMPL
static struct modlsys modlsys32 = {
&mod_syscallops32, "32-bit pipe(2) syscall", &pipe_sysent
};
#endif
static struct modlinkage modlinkage = {
MODREV_1,
&modlsys,
#ifdef _SYSCALL32_IMPL
&modlsys32,
#endif
NULL
};
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_fini(void)
{
return (EBUSY);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
/*
* pipe(2) system call.
* Create a pipe by connecting two streams together. Associate
* each end of the pipe with a vnode, a file descriptor and
* one of the streams.
*/
int
pipe(intptr_t arg, int flags)
{
vnode_t *vp1, *vp2;
struct file *fp1, *fp2;
int error = 0;
int flag1, flag2, iflags;
int fd1, fd2;
/*
* Validate allowed flags.
*/
if ((flags & ~(FCLOEXEC|FNONBLOCK|FCLOFORK)) != 0) {
return (set_errno(EINVAL));
}
/*
* Allocate and initialize two vnodes.
*/
makepipe(&vp1, &vp2);
/*
* Allocate and initialize two file table entries and two
* file pointers. Each file pointer is open for read and
* write.
*/
if (error = falloc(vp1, FWRITE|FREAD, &fp1, &fd1)) {
VN_RELE(vp1);
VN_RELE(vp2);
return (set_errno(error));
}
if (error = falloc(vp2, FWRITE|FREAD, &fp2, &fd2))
goto out2;
/*
* Create two stream heads and attach to each vnode.
*/
if (error = fifo_stropen(&vp1, FWRITE|FREAD, fp1->f_cred, 0, 0))
goto out;
if (error = fifo_stropen(&vp2, FWRITE|FREAD, fp2->f_cred, 0, 0)) {
(void) VOP_CLOSE(vp1, FWRITE|FREAD, 1, (offset_t)0,
fp1->f_cred, NULL);
goto out;
}
strmate(vp1, vp2);
VTOF(vp1)->fn_ino = VTOF(vp2)->fn_ino = fifogetid();
/*
* Set the O_NONBLOCK flag if requested.
*/
if (flags & FNONBLOCK) {
flag1 = fp1->f_flag;
flag2 = fp2->f_flag;
iflags = flags & FNONBLOCK;
if (error = VOP_SETFL(vp1, flag1, iflags, fp1->f_cred, NULL)) {
goto out_vop_close;
}
fp1->f_flag |= iflags;
if (error = VOP_SETFL(vp2, flag2, iflags, fp2->f_cred, NULL)) {
goto out_vop_close;
}
fp2->f_flag |= iflags;
}
/*
* Return the file descriptors to the user. They now
* point to two different vnodes which have different
* stream heads.
*/
if (copyout(&fd1, &((int *)arg)[0], sizeof (int)) ||
copyout(&fd2, &((int *)arg)[1], sizeof (int))) {
error = EFAULT;
goto out_vop_close;
}
/*
* Now fill in the entries that falloc reserved
*/
mutex_exit(&fp1->f_tlock);
mutex_exit(&fp2->f_tlock);
setf(fd1, fp1);
setf(fd2, fp2);
/*
* Optionally set the FCLOEXEC and FCLOFORK flags
*/
if ((flags & FCLOEXEC) != 0) {
f_setfd_or(fd1, FD_CLOEXEC);
f_setfd_or(fd2, FD_CLOEXEC);
}
if ((flags & FCLOFORK) != 0) {
f_setfd_or(fd1, FD_CLOFORK);
f_setfd_or(fd2, FD_CLOFORK);
}
return (0);
out_vop_close:
(void) VOP_CLOSE(vp1, FWRITE|FREAD, 1, (offset_t)0, fp1->f_cred, NULL);
(void) VOP_CLOSE(vp2, FWRITE|FREAD, 1, (offset_t)0, fp2->f_cred, NULL);
out:
setf(fd2, NULL);
unfalloc(fp2);
out2:
setf(fd1, NULL);
unfalloc(fp1);
VN_RELE(vp1);
VN_RELE(vp2);
return (set_errno(error));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 2012, 2016 by Delphix. All rights reserved.
* Copyright 2015, Joyent, Inc.
* Copyright 2022 Oxide Computer Company
*/
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/user.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/mode.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/poll_impl.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/bitmap.h>
#include <sys/kstat.h>
#include <sys/rctl.h>
#include <sys/port_impl.h>
#include <sys/schedctl.h>
#include <sys/cpu.h>
#define NPHLOCKS 64 /* Number of locks; must be power of 2 */
#define PHLOCKADDR(php) &plocks[(((uintptr_t)(php)) >> 8) & (NPHLOCKS - 1)]
#define PHLOCK(php) PHLOCKADDR(php).pp_lock
#define PH_ENTER(php) mutex_enter(PHLOCK(php))
#define PH_EXIT(php) mutex_exit(PHLOCK(php))
#define VALID_POLL_EVENTS (POLLIN | POLLPRI | POLLOUT | POLLRDNORM \
| POLLRDBAND | POLLWRBAND | POLLHUP | POLLERR | POLLNVAL)
/*
* global counters to collect some stats
*/
static struct {
kstat_named_t polllistmiss; /* failed to find a cached poll list */
kstat_named_t pollcachehit; /* list matched 100% w/ cached one */
kstat_named_t pollcachephit; /* list matched < 100% w/ cached one */
kstat_named_t pollcachemiss; /* every list entry is dif from cache */
kstat_named_t pollunlockfail; /* failed to perform pollunlock */
} pollstats = {
{ "polllistmiss", KSTAT_DATA_UINT64 },
{ "pollcachehit", KSTAT_DATA_UINT64 },
{ "pollcachephit", KSTAT_DATA_UINT64 },
{ "pollcachemiss", KSTAT_DATA_UINT64 },
{ "pollunlockfail", KSTAT_DATA_UINT64 }
};
kstat_named_t *pollstats_ptr = (kstat_named_t *)&pollstats;
uint_t pollstats_ndata = sizeof (pollstats) / sizeof (kstat_named_t);
struct pplock {
kmutex_t pp_lock;
short pp_flag;
kcondvar_t pp_wait_cv;
int32_t pp_pad; /* to a nice round 16 bytes */
};
static struct pplock plocks[NPHLOCKS]; /* Hash array of pollhead locks */
/* Contention lock & list for preventing deadlocks in recursive /dev/poll. */
static kmutex_t pollstate_contenders_lock;
static pollstate_t *pollstate_contenders = NULL;
#ifdef DEBUG
static int pollchecksanity(pollstate_t *, nfds_t);
static int pollcheckxref(pollstate_t *, int);
static void pollcheckphlist(void);
static int pollcheckrevents(pollstate_t *, int, int, int);
static void checkpolldat(pollstate_t *);
#endif /* DEBUG */
static int plist_chkdupfd(file_t *, polldat_t *, pollstate_t *, pollfd_t *, int,
int *);
/*
* Data structure overview:
* The per-thread poll state consists of
* one pollstate_t
* one pollcache_t
* one bitmap with one event bit per fd
* a (two-dimensional) hashed array of polldat_t structures - one entry
* per fd
*
* This conglomerate of data structures interact with
* the pollhead which is used by VOP_POLL and pollwakeup
* (protected by the PHLOCK, cached array of plocks), and
* the fpollinfo list hanging off the fi_list which is used to notify
* poll when a cached fd is closed. This is protected by uf_lock.
*
* Invariants:
* pd_php (pollhead pointer) is set iff (if and only if) the polldat
* is on that pollhead. This is modified atomically under pc_lock.
*
* pd_fp (file_t pointer) is set iff the thread is on the fpollinfo
* list for that open file.
* This is modified atomically under pc_lock.
*
* pd_count is the sum (over all values of i) of pd_ref[i].xf_refcnt.
* Iff pd_ref[i].xf_refcnt >= 1 then
* ps_pcacheset[i].pcs_pollfd[pd_ref[i].xf_position].fd == pd_fd
* Iff pd_ref[i].xf_refcnt > 1 then
* In ps_pcacheset[i].pcs_pollfd between index
* pd_ref[i].xf_position] and the end of the list
* there are xf_refcnt entries with .fd == pd_fd
*
* Locking design:
* Whenever possible the design relies on the fact that the poll cache state
* is per thread thus for both poll and exit it is self-synchronizing.
* Thus the key interactions where other threads access the state are:
* pollwakeup (and polltime), and
* close cleaning up the cached references to an open file
*
* The two key locks in poll proper is ps_lock and pc_lock.
*
* The ps_lock is used for synchronization between poll, (lwp_)exit and close
* to ensure that modifications to pollcacheset structure are serialized.
* This lock is held through most of poll() except where poll sleeps
* since there is little need to handle closes concurrently with the execution
* of poll.
* The pc_lock protects most of the fields in pollcache structure and polldat
* structures (which are accessed by poll, pollwakeup, and polltime)
* with the exception of fields that are only modified when only one thread
* can access this per-thread state.
* Those exceptions occur in poll when first allocating the per-thread state,
* when poll grows the number of polldat (never shrinks), and when
* exit/pollcleanup has ensured that there are no references from either
* pollheads or fpollinfo to the threads poll state.
*
* Poll(2) system call is the only path which ps_lock and pc_lock are both
* held, in that order. It needs ps_lock to synchronize with close and
* lwp_exit; and pc_lock with pollwakeup.
*
* The locking interaction between pc_lock and PHLOCK take into account
* that poll acquires these locks in the order of pc_lock and then PHLOCK
* while pollwakeup does it in the reverse order. Thus pollwakeup implements
* deadlock avoidance by dropping the locks and reacquiring them in the
* reverse order. For this to work pollwakeup needs to prevent the thread
* from exiting and freeing all of the poll related state. Thus is done
* using
* the pc_no_exit lock
* the pc_busy counter
* the pc_busy_cv condition variable
*
* The locking interaction between pc_lock and uf_lock has similar
* issues. Poll holds ps_lock and/or pc_lock across calls to getf/releasef
* which acquire uf_lock. The poll cleanup in close needs to hold uf_lock
* to prevent poll or exit from doing a delfpollinfo after which the thread
* might exit. But the cleanup needs to acquire pc_lock when modifying
* the poll cache state. The solution is to use pc_busy and do the close
* cleanup in two phases:
* First close calls pollblockexit which increments pc_busy.
* This prevents the per-thread poll related state from being freed.
* Then close drops uf_lock and calls pollcacheclean.
* This routine can then acquire pc_lock and remove any references
* to the closing fd (as well as recording that it has been closed
* so that a POLLNVAL can be generated even if the fd is reused before
* poll has been woken up and checked getf() again).
*
* When removing a polled fd from poll cache, the fd is always removed
* from pollhead list first and then from fpollinfo list, i.e.,
* polldat_disassociate() is called before delfpollinfo().
*
*
* Locking hierarchy:
* pc_no_exit is a leaf level lock.
* ps_lock is held when acquiring pc_lock (except when pollwakeup
* acquires pc_lock).
* pc_lock might be held when acquiring PHLOCK (polldat_associate/
* polldat_disassociate)
* pc_lock is always held (but this is not required)
* when acquiring PHLOCK (in polladd/polldat_disassociate and pollwakeup
* called from pcache_clean_entry).
* pc_lock is held across addfpollinfo/delfpollinfo which acquire
* uf_lock.
* pc_lock is held across getf/releasef which acquire uf_lock.
* ps_lock might be held across getf/releasef which acquire uf_lock.
* pollwakeup tries to acquire pc_lock while holding PHLOCK
* but drops the locks and reacquire them in reverse order to avoid
* deadlock.
*
* Note also that there is deadlock avoidance support for VOP_POLL routines
* and pollwakeup involving a file system or driver lock.
* See below.
*/
/*
* Deadlock avoidance support for VOP_POLL() routines. This is
* sometimes necessary to prevent deadlock between polling threads
* (which hold poll locks on entry to xx_poll(), then acquire foo)
* and pollwakeup() threads (which hold foo, then acquire poll locks).
*
* pollunlock(*cookie) releases whatever poll locks the current thread holds,
* setting a cookie for use by pollrelock();
*
* pollrelock(cookie) reacquires previously dropped poll locks;
*
* polllock(php, mutex) does the common case: pollunlock(),
* acquire the problematic mutex, pollrelock().
*
* If polllock() or pollunlock() return non-zero, it indicates that a recursive
* /dev/poll is in progress and pollcache locks cannot be dropped. Callers
* must handle this by indicating a POLLNVAL in the revents of the VOP_POLL.
*/
int
pollunlock(int *lockstate)
{
pollstate_t *ps = curthread->t_pollstate;
pollcache_t *pcp;
ASSERT(lockstate != NULL);
/*
* There is no way to safely perform a pollunlock() while in the depths
* of a recursive /dev/poll operation.
*/
if (ps != NULL && ps->ps_depth > 1) {
ps->ps_flags |= POLLSTATE_ULFAIL;
pollstats.pollunlockfail.value.ui64++;
return (-1);
}
/*
* t_pollcache is set by /dev/poll and event ports (port_fd.c).
* If the pollrelock/pollunlock is called as a result of poll(2),
* the t_pollcache should be NULL.
*/
if (curthread->t_pollcache == NULL)
pcp = ps->ps_pcache;
else
pcp = curthread->t_pollcache;
if (!mutex_owned(&pcp->pc_lock)) {
*lockstate = 0;
} else {
*lockstate = 1;
mutex_exit(&pcp->pc_lock);
}
return (0);
}
/*
* The pc_lock and pc_flag fields of port_fdcache_t must exactly match those of
* pollcache_t as they are accessed through t_pollcache as if they were part of
* a "real" pollcache.
*/
CTASSERT(offsetof(pollcache_t, pc_lock) == offsetof(port_fdcache_t, pc_lock));
CTASSERT(offsetof(pollcache_t, pc_flag) == offsetof(port_fdcache_t, pc_flag));
void
pollrelock(int lockstate)
{
pollstate_t *ps = curthread->t_pollstate;
pollcache_t *pcp;
/* Skip this whole ordeal if the pollcache was not locked to begin */
if (lockstate == 0)
return;
/*
* t_pollcache is set by /dev/poll and event ports (port_fd.c).
* If the pollrelock/pollunlock is called as a result of poll(2),
* the t_pollcache should be NULL.
*/
if (curthread->t_pollcache == NULL)
pcp = ps->ps_pcache;
else
pcp = curthread->t_pollcache;
mutex_enter(&pcp->pc_lock);
}
/* ARGSUSED */
int
polllock(pollhead_t *php, kmutex_t *lp)
{
if (mutex_tryenter(lp) == 0) {
int state;
if (pollunlock(&state) != 0) {
return (-1);
}
mutex_enter(lp);
pollrelock(state);
}
return (0);
}
static int
poll_common(pollfd_t *fds, nfds_t nfds, timespec_t *tsp, k_sigset_t *ksetp)
{
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
proc_t *p = ttoproc(t);
int fdcnt = 0;
int i;
hrtime_t deadline; /* hrtime value when we want to return */
pollfd_t *pollfdp;
pollstate_t *ps;
pollcache_t *pcp;
int error = 0;
nfds_t old_nfds;
int cacheindex = 0; /* which cache set is used */
/*
* Determine the precise future time of the requested timeout, if any.
*/
if (tsp == NULL) {
deadline = -1;
} else if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) {
deadline = 0;
} else {
/* They must wait at least a tick. */
deadline = ((hrtime_t)tsp->tv_sec * NANOSEC) + tsp->tv_nsec;
deadline = MAX(deadline, nsec_per_tick);
deadline += gethrtime();
}
/*
* Reset our signal mask, if requested.
*/
if (ksetp != NULL) {
mutex_enter(&p->p_lock);
schedctl_finish_sigblock(t);
lwp->lwp_sigoldmask = t->t_hold;
t->t_hold = *ksetp;
t->t_flag |= T_TOMASK;
/*
* Call cv_reltimedwait_sig() just to check for signals.
* We will return immediately with either 0 or -1.
*/
if (!cv_reltimedwait_sig(&t->t_delay_cv, &p->p_lock, 0,
TR_CLOCK_TICK)) {
mutex_exit(&p->p_lock);
error = EINTR;
goto pollout;
}
mutex_exit(&p->p_lock);
}
/*
* Check to see if this one just wants to use poll() as a timeout.
* If yes then bypass all the other stuff and make it sleep.
*/
if (nfds == 0) {
/*
* Sleep until we have passed the requested future
* time or until interrupted by a signal.
* Do not check for signals if we do not want to wait.
*/
if (deadline != 0) {
mutex_enter(&t->t_delay_lock);
while ((error = cv_timedwait_sig_hrtime(&t->t_delay_cv,
&t->t_delay_lock, deadline)) > 0)
continue;
mutex_exit(&t->t_delay_lock);
error = (error == 0) ? EINTR : 0;
}
goto pollout;
}
if (nfds > p->p_fno_ctl) {
mutex_enter(&p->p_lock);
(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
p->p_rctls, p, RCA_SAFE);
mutex_exit(&p->p_lock);
error = EINVAL;
goto pollout;
}
/*
* Need to allocate memory for pollstate before anything because
* the mutex and cv are created in this space
*/
ps = pollstate_create();
if (ps->ps_pcache == NULL)
ps->ps_pcache = pcache_alloc();
pcp = ps->ps_pcache;
/*
* NOTE: for performance, buffers are saved across poll() calls.
* The theory is that if a process polls heavily, it tends to poll
* on the same set of descriptors. Therefore, we only reallocate
* buffers when nfds changes. There is no hysteresis control,
* because there is no data to suggest that this is necessary;
* the penalty of reallocating is not *that* great in any event.
*/
old_nfds = ps->ps_nfds;
if (nfds != old_nfds) {
kmem_free(ps->ps_pollfd, old_nfds * sizeof (pollfd_t));
pollfdp = kmem_alloc(nfds * sizeof (pollfd_t), KM_SLEEP);
ps->ps_pollfd = pollfdp;
ps->ps_nfds = nfds;
}
pollfdp = ps->ps_pollfd;
if (copyin(fds, pollfdp, nfds * sizeof (pollfd_t))) {
error = EFAULT;
goto pollout;
}
if (fds == NULL) {
/*
* If the process has page 0 mapped, then the copyin() above
* will succeed even if fds is NULL. However, our cached
* poll lists are keyed by the address of the passed-in fds
* structure, and we use the value NULL to indicate an unused
* poll cache list entry. As such, we elect not to support
* NULL as a valid (user) memory address and fail the poll()
* call.
*/
error = EINVAL;
goto pollout;
}
/*
* If this thread polls for the first time, allocate ALL poll
* cache data structures and cache the poll fd list. This
* allocation is delayed till now because lwp's polling 0 fd
* (i.e. using poll as timeout()) don't need this memory.
*/
mutex_enter(&ps->ps_lock);
pcp = ps->ps_pcache;
ASSERT(pcp != NULL);
if (pcp->pc_bitmap == NULL) {
pcache_create(pcp, nfds);
/*
* poll and cache this poll fd list in ps_pcacheset[0].
*/
error = pcacheset_cache_list(ps, fds, &fdcnt, cacheindex);
if (fdcnt || error) {
mutex_exit(&ps->ps_lock);
goto pollout;
}
} else {
pollcacheset_t *pcset = ps->ps_pcacheset;
/*
* Not first time polling. Select a cached poll list by
* matching user pollfd list buffer address.
*/
for (cacheindex = 0; cacheindex < ps->ps_nsets; cacheindex++) {
if (pcset[cacheindex].pcs_usradr == (uintptr_t)fds) {
if ((++pcset[cacheindex].pcs_count) == 0) {
/*
* counter is wrapping around.
*/
pcacheset_reset_count(ps, cacheindex);
}
/*
* examine and resolve possible
* difference of the current poll
* list and previously cached one.
* If there is an error during resolve(),
* the callee will guarantee the consistency
* of cached poll list and cache content.
*/
error = pcacheset_resolve(ps, nfds, &fdcnt,
cacheindex);
if (error) {
mutex_exit(&ps->ps_lock);
goto pollout;
}
break;
}
/*
* Note that pcs_usradr field of an used entry won't be
* 0 because it stores the address of passed-in fds,
* and 0 fds will not be cached (Then it is either
* the special timeout case when nfds is 0 or it returns
* failure directly).
*/
if (pcset[cacheindex].pcs_usradr == (uintptr_t)NULL) {
/*
* found an unused entry. Use it to cache
* this poll list.
*/
error = pcacheset_cache_list(ps, fds, &fdcnt,
cacheindex);
if (fdcnt || error) {
mutex_exit(&ps->ps_lock);
goto pollout;
}
break;
}
}
if (cacheindex == ps->ps_nsets) {
/*
* We failed to find a matching cached poll fd list.
* replace an old list.
*/
pollstats.polllistmiss.value.ui64++;
cacheindex = pcacheset_replace(ps);
ASSERT(cacheindex < ps->ps_nsets);
pcset[cacheindex].pcs_usradr = (uintptr_t)fds;
error = pcacheset_resolve(ps, nfds, &fdcnt, cacheindex);
if (error) {
mutex_exit(&ps->ps_lock);
goto pollout;
}
}
}
/*
* Always scan the bitmap with the lock on the pollcache held.
* This is to make sure that a wakeup does not come undetected.
* If the lock is not held, a pollwakeup could have come for an
* fd we already checked but before this thread sleeps, in which
* case the wakeup is missed. Now we hold the pcache lock and
* check the bitmap again. This will prevent wakeup from happening
* while we hold pcache lock since pollwakeup() will also lock
* the pcache before updating poll bitmap.
*/
mutex_enter(&pcp->pc_lock);
for (;;) {
pcp->pc_flag = 0;
error = pcache_poll(pollfdp, ps, nfds, &fdcnt, cacheindex);
if (fdcnt || error) {
mutex_exit(&pcp->pc_lock);
mutex_exit(&ps->ps_lock);
break;
}
/*
* If PC_POLLWAKE is set, a pollwakeup() was performed on
* one of the file descriptors. This can happen only if
* one of the VOP_POLL() functions dropped pcp->pc_lock.
* The only current cases of this is in procfs (prpoll())
* and STREAMS (strpoll()).
*/
if (pcp->pc_flag & PC_POLLWAKE)
continue;
/*
* If you get here, the poll of fds was unsuccessful.
* Wait until some fd becomes readable, writable, or gets
* an exception, or until a signal or a timeout occurs.
* Do not check for signals if we have a zero timeout.
*/
mutex_exit(&ps->ps_lock);
if (deadline == 0) {
error = -1;
} else {
error = cv_timedwait_sig_hrtime(&pcp->pc_cv,
&pcp->pc_lock, deadline);
}
mutex_exit(&pcp->pc_lock);
/*
* If we have received a signal or timed out
* then break out and return.
*/
if (error <= 0) {
error = (error == 0) ? EINTR : 0;
break;
}
/*
* We have not received a signal or timed out.
* Continue around and poll fds again.
*/
mutex_enter(&ps->ps_lock);
mutex_enter(&pcp->pc_lock);
}
pollout:
/*
* If we changed the signal mask but we received
* no signal then restore the signal mask.
* Otherwise psig() will deal with the signal mask.
*/
if (ksetp != NULL) {
mutex_enter(&p->p_lock);
if (lwp->lwp_cursig == 0) {
t->t_hold = lwp->lwp_sigoldmask;
t->t_flag &= ~T_TOMASK;
}
mutex_exit(&p->p_lock);
}
if (error)
return (set_errno(error));
/*
* Copy out the events and return the fdcnt to the user.
*/
if (nfds != 0 &&
copyout(pollfdp, fds, nfds * sizeof (pollfd_t)))
return (set_errno(EFAULT));
#ifdef DEBUG
/*
* Another sanity check:
*/
if (fdcnt) {
int reventcnt = 0;
for (i = 0; i < nfds; i++) {
if (pollfdp[i].fd < 0) {
ASSERT(pollfdp[i].revents == 0);
continue;
}
if (pollfdp[i].revents) {
reventcnt++;
}
}
ASSERT(fdcnt == reventcnt);
} else {
for (i = 0; i < nfds; i++) {
ASSERT(pollfdp[i].revents == 0);
}
}
#endif /* DEBUG */
return (fdcnt);
}
/*
* This is the system call trap that poll(),
* select() and pselect() are built upon.
* It is a private interface between libc and the kernel.
*/
int
pollsys(pollfd_t *fds, nfds_t nfds, timespec_t *timeoutp, sigset_t *setp)
{
timespec_t ts;
timespec_t *tsp;
sigset_t set;
k_sigset_t kset;
k_sigset_t *ksetp;
model_t datamodel = get_udatamodel();
if (timeoutp == NULL)
tsp = NULL;
else {
if (datamodel == DATAMODEL_NATIVE) {
if (copyin(timeoutp, &ts, sizeof (ts)))
return (set_errno(EFAULT));
} else {
timespec32_t ts32;
if (copyin(timeoutp, &ts32, sizeof (ts32)))
return (set_errno(EFAULT));
TIMESPEC32_TO_TIMESPEC(&ts, &ts32)
}
if (itimerspecfix(&ts))
return (set_errno(EINVAL));
tsp = &ts;
}
if (setp == NULL)
ksetp = NULL;
else {
if (copyin(setp, &set, sizeof (set)))
return (set_errno(EFAULT));
sigutok(&set, &kset);
ksetp = &kset;
}
return (poll_common(fds, nfds, tsp, ksetp));
}
/*
* Clean up any state left around by poll(2). Called when a thread exits.
*/
void
pollcleanup()
{
pollstate_t *ps = curthread->t_pollstate;
pollcache_t *pcp;
if (ps == NULL)
return;
pcp = ps->ps_pcache;
/*
* free up all cached poll fds
*/
if (pcp == NULL) {
/* this pollstate is used by /dev/poll */
goto pollcleanout;
}
if (pcp->pc_bitmap != NULL) {
ASSERT(MUTEX_NOT_HELD(&ps->ps_lock));
/*
* a close lwp can race with us when cleaning up a polldat
* entry. We hold the ps_lock when cleaning hash table.
* Since this pollcache is going away anyway, there is no
* need to hold the pc_lock.
*/
mutex_enter(&ps->ps_lock);
pcache_clean(pcp);
mutex_exit(&ps->ps_lock);
#ifdef DEBUG
/*
* At this point, all fds cached by this lwp should be
* cleaned up. There should be no fd in fi_list still
* reference this thread.
*/
checkfpollinfo(); /* sanity check */
pollcheckphlist(); /* sanity check */
#endif /* DEBUG */
}
/*
* Be sure no one is referencing thread before exiting
*/
mutex_enter(&pcp->pc_no_exit);
ASSERT(pcp->pc_busy >= 0);
while (pcp->pc_busy > 0)
cv_wait(&pcp->pc_busy_cv, &pcp->pc_no_exit);
mutex_exit(&pcp->pc_no_exit);
pollcleanout:
pollstate_destroy(ps);
curthread->t_pollstate = NULL;
}
/*
* pollwakeup() - poke threads waiting in poll() for some event
* on a particular object.
*
* The threads hanging off of the specified pollhead structure are scanned.
* If their event mask matches the specified event(s), then pollnotify() is
* called to poke the thread.
*
* Multiple events may be specified. When POLLHUP or POLLERR are specified,
* all waiting threads are poked.
*
* It is important that pollnotify() not drop the lock protecting the list
* of threads.
*/
void
pollwakeup(pollhead_t *php, short events_arg)
{
polldat_t *pdp;
int events = (ushort_t)events_arg;
struct plist {
port_t *pp;
int pevents;
struct plist *next;
};
struct plist *plhead = NULL, *pltail = NULL;
retry:
PH_ENTER(php);
for (pdp = php->ph_list; pdp; pdp = pdp->pd_next) {
if ((pdp->pd_events & events) ||
(events & (POLLHUP | POLLERR))) {
pollcache_t *pcp;
if (pdp->pd_portev != NULL) {
port_kevent_t *pkevp = pdp->pd_portev;
/*
* Object (fd) is associated with an event port,
* => send event notification to the port.
*/
ASSERT(pkevp->portkev_source == PORT_SOURCE_FD);
mutex_enter(&pkevp->portkev_lock);
if (pkevp->portkev_flags & PORT_KEV_VALID) {
int pevents;
pkevp->portkev_flags &= ~PORT_KEV_VALID;
pkevp->portkev_events |= events &
(pdp->pd_events | POLLHUP |
POLLERR);
/*
* portkev_lock mutex will be released
* by port_send_event().
*/
port_send_event(pkevp);
/*
* If we have some thread polling the
* port's fd, add it to the list. They
* will be notified later.
* The port_pollwkup() will flag the
* port_t so that it will not disappear
* till port_pollwkdone() is called.
*/
pevents =
port_pollwkup(pkevp->portkev_port);
if (pevents) {
struct plist *t;
t = kmem_zalloc(
sizeof (struct plist),
KM_SLEEP);
t->pp = pkevp->portkev_port;
t->pevents = pevents;
if (plhead == NULL) {
plhead = t;
} else {
pltail->next = t;
}
pltail = t;
}
} else {
mutex_exit(&pkevp->portkev_lock);
}
continue;
}
pcp = pdp->pd_pcache;
/*
* Try to grab the lock for this thread. If
* we don't get it then we may deadlock so
* back out and restart all over again. Note
* that the failure rate is very very low.
*/
if (mutex_tryenter(&pcp->pc_lock)) {
pollnotify(pcp, pdp->pd_fd);
mutex_exit(&pcp->pc_lock);
} else {
/*
* We are here because:
* 1) This thread has been woke up
* and is trying to get out of poll().
* 2) Some other thread is also here
* but with a different pollhead lock.
*
* So, we need to drop the lock on pollhead
* because of (1) but we want to prevent
* that thread from doing lwp_exit() or
* devpoll close. We want to ensure that
* the pollcache pointer is still invalid.
*
* Solution: Grab the pcp->pc_no_exit lock,
* increment the pc_busy counter, drop every
* lock in sight. Get out of the way and wait
* for type (2) threads to finish.
*/
mutex_enter(&pcp->pc_no_exit);
pcp->pc_busy++; /* prevents exit()'s */
mutex_exit(&pcp->pc_no_exit);
PH_EXIT(php);
mutex_enter(&pcp->pc_lock);
mutex_exit(&pcp->pc_lock);
mutex_enter(&pcp->pc_no_exit);
pcp->pc_busy--;
if (pcp->pc_busy == 0) {
/*
* Wakeup the thread waiting in
* thread_exit().
*/
cv_signal(&pcp->pc_busy_cv);
}
mutex_exit(&pcp->pc_no_exit);
goto retry;
}
}
}
/*
* Event ports - If this php is of the port on the list,
* call port_pollwkdone() to release it. The port_pollwkdone()
* needs to be called before dropping the PH lock so that any new
* thread attempting to poll this port are blocked. There can be
* only one thread here in pollwakeup notifying this port's fd.
*/
if (plhead != NULL && &plhead->pp->port_pollhd == php) {
struct plist *t;
port_pollwkdone(plhead->pp);
t = plhead;
plhead = plhead->next;
kmem_free(t, sizeof (struct plist));
}
PH_EXIT(php);
/*
* Event ports - Notify threads polling the event port's fd.
* This is normally done in port_send_event() where it calls
* pollwakeup() on the port. But, for PORT_SOURCE_FD source alone,
* we do it here in pollwakeup() to avoid a recursive call.
*/
if (plhead != NULL) {
php = &plhead->pp->port_pollhd;
events = plhead->pevents;
goto retry;
}
}
/*
* This function is called to inform a thread (or threads) that an event being
* polled on has occurred. The pollstate lock on the thread should be held
* on entry.
*/
void
pollnotify(pollcache_t *pcp, int fd)
{
ASSERT(fd < pcp->pc_mapsize);
ASSERT(MUTEX_HELD(&pcp->pc_lock));
BT_SET(pcp->pc_bitmap, fd);
pcp->pc_flag |= PC_POLLWAKE;
cv_broadcast(&pcp->pc_cv);
pcache_wake_parents(pcp);
}
/*
* Associate a polldat entry with a pollhead (add it to ph_list).
*
* The polldat struct is used by pollwakeup to wake sleeping pollers when polled
* events has happened.
*/
void
polldat_associate(polldat_t *pdp, pollhead_t *php)
{
ASSERT3P(pdp->pd_php, ==, NULL);
ASSERT3P(pdp->pd_next, ==, NULL);
PH_ENTER(php);
#ifdef DEBUG
/* The polldat should not be already on the list */
for (polldat_t *wp = php->ph_list; wp != NULL; wp = wp->pd_next) {
ASSERT3P(wp, !=, pdp);
}
#endif /* DEBUG */
pdp->pd_next = php->ph_list;
php->ph_list = pdp;
pdp->pd_php = php;
PH_EXIT(php);
}
/*
* Disassociate a polldat from its pollhead (if such an association exists).
*/
void
polldat_disassociate(polldat_t *pdp)
{
pollhead_t *php;
/*
* Acquire the lock for the pollhead which this polldat is associated
* with. This must be done with care, re-checking pd_php after entering
* the pollhead lock, since a racing pollhead_clean() could have already
* performed the disassociation.
*/
for (;;) {
php = pdp->pd_php;
if (php == NULL) {
/* polldat is not associated with a pollhead */
return;
}
/*
* The lock for a given pollhead is not stored in the pollhead
* itself, but is rather a global entry in an array (plocks)
* which the pollhead pointer hashes into (see: PHLOCK()).
*/
PH_ENTER(php);
if (pdp->pd_php == php) {
break;
}
PH_EXIT(php);
}
polldat_t **wpp = &php->ph_list, *wp = php->ph_list;
while (wp != NULL) {
if (wp == pdp) {
/* Unlink the polldat from the list */
*wpp = pdp->pd_next;
pdp->pd_next = NULL;
break;
}
wpp = &wp->pd_next;
wp = wp->pd_next;
}
#ifdef DEBUG
/* It would be unexpected if pdp was not in the pollhead list */
ASSERT(wp != NULL);
/* Assert that pdp is not duplicated somewhere later in the list */
for (wp = *wpp; wp; wp = wp->pd_next) {
ASSERT(wp != pdp);
}
#endif /* DEBUG */
pdp->pd_php = NULL;
PH_EXIT(php);
}
/*
* walk through the poll fd lists to see if they are identical. This is an
* expensive operation and should not be done more than once for each poll()
* call.
*
* As an optimization (i.e., not having to go through the lists more than
* once), this routine also clear the revents field of pollfd in 'current'.
* Zeroing out the revents field of each entry in current poll list is
* required by poll man page.
*
* Since the events field of cached list has illegal poll events filtered
* out, the current list applies the same filtering before comparison.
*
* The routine stops when it detects a meaningful difference, or when it
* exhausts the lists.
*/
int
pcacheset_cmp(pollfd_t *current, pollfd_t *cached, pollfd_t *newlist, int n)
{
int ix;
for (ix = 0; ix < n; ix++) {
/* Prefetch 64 bytes worth of 8-byte elements */
if ((ix & 0x7) == 0) {
prefetch_write_many((caddr_t)¤t[ix + 8]);
prefetch_write_many((caddr_t)&cached[ix + 8]);
}
if (current[ix].fd == cached[ix].fd) {
/*
* Filter out invalid poll events while we are in
* inside the loop.
*/
if (current[ix].events & ~VALID_POLL_EVENTS) {
current[ix].events &= VALID_POLL_EVENTS;
if (newlist != NULL)
newlist[ix].events = current[ix].events;
}
if (current[ix].events == cached[ix].events) {
current[ix].revents = 0;
continue;
}
}
if ((current[ix].fd < 0) && (cached[ix].fd < 0)) {
current[ix].revents = 0;
continue;
}
return (ix);
}
return (ix);
}
/*
* This routine returns a pointer to a cached poll fd entry, or NULL if it
* does not find it in the hash table.
*/
polldat_t *
pcache_lookup_fd(pollcache_t *pcp, int fd)
{
int hashindex;
polldat_t *pdp;
hashindex = POLLHASH(pcp->pc_hashsize, fd);
pdp = pcp->pc_hash[hashindex];
while (pdp != NULL) {
if (pdp->pd_fd == fd)
break;
pdp = pdp->pd_hashnext;
}
return (pdp);
}
polldat_t *
pcache_alloc_fd(int nsets)
{
polldat_t *pdp;
pdp = kmem_zalloc(sizeof (polldat_t), KM_SLEEP);
if (nsets > 0) {
pdp->pd_ref = kmem_zalloc(sizeof (xref_t) * nsets, KM_SLEEP);
pdp->pd_nsets = nsets;
}
return (pdp);
}
/*
* This routine inserts a polldat into the pollcache's hash table. It
* may be necessary to grow the size of the hash table.
*/
void
pcache_insert_fd(pollcache_t *pcp, polldat_t *pdp, nfds_t nfds)
{
int hashindex;
int fd;
if ((pcp->pc_fdcount > pcp->pc_hashsize * POLLHASHTHRESHOLD) ||
(nfds > pcp->pc_hashsize * POLLHASHTHRESHOLD)) {
pcache_grow_hashtbl(pcp, nfds);
}
fd = pdp->pd_fd;
hashindex = POLLHASH(pcp->pc_hashsize, fd);
pdp->pd_hashnext = pcp->pc_hash[hashindex];
pcp->pc_hash[hashindex] = pdp;
pcp->pc_fdcount++;
#ifdef DEBUG
{
/*
* same fd should not appear on a hash list twice
*/
polldat_t *pdp1;
for (pdp1 = pdp->pd_hashnext; pdp1; pdp1 = pdp1->pd_hashnext) {
ASSERT(pdp->pd_fd != pdp1->pd_fd);
}
}
#endif /* DEBUG */
}
/*
* Grow the hash table -- either double the table size or round it to the
* nearest multiples of POLLHASHCHUNKSZ, whichever is bigger. Rehash all the
* elements on the hash table.
*/
void
pcache_grow_hashtbl(pollcache_t *pcp, nfds_t nfds)
{
int oldsize;
polldat_t **oldtbl;
polldat_t *pdp, *pdp1;
int i;
#ifdef DEBUG
int count = 0;
#endif
ASSERT(pcp->pc_hashsize % POLLHASHCHUNKSZ == 0);
oldsize = pcp->pc_hashsize;
oldtbl = pcp->pc_hash;
if (nfds > pcp->pc_hashsize * POLLHASHINC) {
pcp->pc_hashsize = (nfds + POLLHASHCHUNKSZ - 1) &
~(POLLHASHCHUNKSZ - 1);
} else {
pcp->pc_hashsize = pcp->pc_hashsize * POLLHASHINC;
}
pcp->pc_hash = kmem_zalloc(pcp->pc_hashsize * sizeof (polldat_t *),
KM_SLEEP);
/*
* rehash existing elements
*/
pcp->pc_fdcount = 0;
for (i = 0; i < oldsize; i++) {
pdp = oldtbl[i];
while (pdp != NULL) {
pdp1 = pdp->pd_hashnext;
pcache_insert_fd(pcp, pdp, nfds);
pdp = pdp1;
#ifdef DEBUG
count++;
#endif
}
}
kmem_free(oldtbl, oldsize * sizeof (polldat_t *));
ASSERT(pcp->pc_fdcount == count);
}
void
pcache_grow_map(pollcache_t *pcp, int fd)
{
int newsize;
ulong_t *newmap;
/*
* grow to nearest multiple of POLLMAPCHUNK, assuming POLLMAPCHUNK is
* power of 2.
*/
newsize = (fd + POLLMAPCHUNK) & ~(POLLMAPCHUNK - 1);
newmap = kmem_zalloc((newsize / BT_NBIPUL) * sizeof (ulong_t),
KM_SLEEP);
/*
* don't want pollwakeup to set a bit while growing the bitmap.
*/
ASSERT(mutex_owned(&pcp->pc_lock) == 0);
mutex_enter(&pcp->pc_lock);
bcopy(pcp->pc_bitmap, newmap,
(pcp->pc_mapsize / BT_NBIPUL) * sizeof (ulong_t));
kmem_free(pcp->pc_bitmap,
(pcp->pc_mapsize /BT_NBIPUL) * sizeof (ulong_t));
pcp->pc_bitmap = newmap;
pcp->pc_mapsize = newsize;
mutex_exit(&pcp->pc_lock);
}
/*
* remove all the reference from pollhead list and fpollinfo lists.
*/
void
pcache_clean(pollcache_t *pcp)
{
int i;
polldat_t **hashtbl;
polldat_t *pdp;
ASSERT(MUTEX_HELD(&curthread->t_pollstate->ps_lock));
hashtbl = pcp->pc_hash;
for (i = 0; i < pcp->pc_hashsize; i++) {
for (pdp = hashtbl[i]; pdp; pdp = pdp->pd_hashnext) {
polldat_disassociate(pdp);
if (pdp->pd_fp != NULL) {
delfpollinfo(pdp->pd_fd);
pdp->pd_fp = NULL;
}
}
}
}
void
pcacheset_invalidate(pollstate_t *ps, polldat_t *pdp)
{
int i;
int fd = pdp->pd_fd;
/*
* we come here because an earlier close() on this cached poll fd.
*/
ASSERT(pdp->pd_fp == NULL);
ASSERT(MUTEX_HELD(&ps->ps_lock));
pdp->pd_events = 0;
for (i = 0; i < ps->ps_nsets; i++) {
xref_t *refp;
pollcacheset_t *pcsp;
ASSERT(pdp->pd_ref != NULL);
refp = &pdp->pd_ref[i];
if (refp->xf_refcnt) {
ASSERT(refp->xf_position >= 0);
pcsp = &ps->ps_pcacheset[i];
if (refp->xf_refcnt == 1) {
pcsp->pcs_pollfd[refp->xf_position].fd = -1;
refp->xf_refcnt = 0;
pdp->pd_count--;
} else if (refp->xf_refcnt > 1) {
int j;
/*
* turn off every appearance in pcs_pollfd list
*/
for (j = refp->xf_position;
j < pcsp->pcs_nfds; j++) {
if (pcsp->pcs_pollfd[j].fd == fd) {
pcsp->pcs_pollfd[j].fd = -1;
refp->xf_refcnt--;
pdp->pd_count--;
}
}
}
ASSERT(refp->xf_refcnt == 0);
refp->xf_position = POLLPOSINVAL;
}
}
ASSERT(pdp->pd_count == 0);
}
/*
* Insert poll fd into the pollcache, and add poll registration.
* This routine is called after getf() and before releasef(). So the vnode
* can not disappear even if we block here.
* If there is an error, the polled fd is not cached.
*/
int
pcache_insert(pollstate_t *ps, file_t *fp, pollfd_t *pollfdp, int *fdcntp,
ssize_t pos, int which)
{
pollcache_t *pcp = ps->ps_pcache;
polldat_t *pdp;
int error;
int fd;
pollhead_t *memphp = NULL;
xref_t *refp;
int newpollfd = 0;
ASSERT(MUTEX_HELD(&ps->ps_lock));
/*
* The poll caching uses the existing VOP_POLL interface. If there
* is no polled events, we want the polled device to set its "some
* one is sleeping in poll" flag. When the polled events happen
* later, the driver will call pollwakeup(). We achieve this by
* always passing 0 in the third parameter ("anyyet") when calling
* VOP_POLL. This parameter is not looked at by drivers when the
* polled events exist. If a driver chooses to ignore this parameter
* and call pollwakeup whenever the polled events happen, that will
* be OK too.
*/
ASSERT(curthread->t_pollcache == NULL);
error = VOP_POLL(fp->f_vnode, pollfdp->events, 0, &pollfdp->revents,
&memphp, NULL);
if (error) {
return (error);
}
if (pollfdp->revents) {
(*fdcntp)++;
}
/*
* polling the underlying device succeeded. Now we can cache it.
* A close can't come in here because we have not done a releasef()
* yet.
*/
fd = pollfdp->fd;
pdp = pcache_lookup_fd(pcp, fd);
if (pdp == NULL) {
ASSERT(ps->ps_nsets > 0);
pdp = pcache_alloc_fd(ps->ps_nsets);
newpollfd = 1;
}
/*
* If this entry was used to cache a poll fd which was closed, and
* this entry has not been cleaned, do it now.
*/
if ((pdp->pd_count > 0) && (pdp->pd_fp == NULL)) {
pcacheset_invalidate(ps, pdp);
ASSERT(pdp->pd_next == NULL);
}
if (pdp->pd_count == 0) {
pdp->pd_fd = fd;
pdp->pd_fp = fp;
addfpollinfo(fd);
pdp->pd_thread = curthread;
pdp->pd_pcache = pcp;
/*
* the entry is never used or cleared by removing a cached
* pollfd (pcache_delete_fd). So all the fields should be clear.
*/
ASSERT(pdp->pd_next == NULL);
}
/*
* A polled fd is considered cached. So there should be a fpollinfo
* entry on uf_fpollinfo list.
*/
ASSERT(infpollinfo(fd));
/*
* If there is an inconsistency, we want to know it here.
*/
ASSERT(pdp->pd_fp == fp);
/*
* XXX pd_events is a union of all polled events on this fd, possibly
* by different threads. Unless this is a new first poll(), pd_events
* never shrinks. If an event is no longer polled by a process, there
* is no way to cancel that event. In that case, poll degrade to its
* old form -- polling on this fd every time poll() is called. The
* assumption is an app always polls the same type of events.
*/
pdp->pd_events |= pollfdp->events;
pdp->pd_count++;
/*
* There is not much special handling for multiple appearances of
* same fd other than xf_position always recording the first
* appearance in poll list. If this is called from pcacheset_cache_list,
* a VOP_POLL is called on every pollfd entry; therefore each
* revents and fdcnt should be set correctly. If this is called from
* pcacheset_resolve, we don't care about fdcnt here. Pollreadmap will
* pick up the right count and handle revents field of each pollfd
* entry.
*/
ASSERT(pdp->pd_ref != NULL);
refp = &pdp->pd_ref[which];
if (refp->xf_refcnt == 0) {
refp->xf_position = pos;
} else {
/*
* xf_position records the fd's first appearance in poll list
*/
if (pos < refp->xf_position) {
refp->xf_position = pos;
}
}
ASSERT(pollfdp->fd == ps->ps_pollfd[refp->xf_position].fd);
refp->xf_refcnt++;
if (fd >= pcp->pc_mapsize) {
pcache_grow_map(pcp, fd);
}
if (fd > pcp->pc_mapend) {
pcp->pc_mapend = fd;
}
if (newpollfd != 0) {
pcache_insert_fd(ps->ps_pcache, pdp, ps->ps_nfds);
}
if (memphp) {
if (pdp->pd_php == NULL) {
polldat_associate(pdp, memphp);
} else {
if (memphp != pdp->pd_php) {
/*
* layered devices (e.g. console driver)
* may change the vnode and thus the pollhead
* pointer out from underneath us.
*/
polldat_disassociate(pdp);
polldat_associate(pdp, memphp);
}
}
}
/*
* Since there is a considerable window between VOP_POLL and when
* we actually put the polldat struct on the pollhead list, we could
* miss a pollwakeup. In the case of polling additional events, we
* don't update the events until after VOP_POLL. So we could miss
* pollwakeup there too. So we always set the bit here just to be
* safe. The real performance gain is in subsequent pcache_poll.
*/
mutex_enter(&pcp->pc_lock);
BT_SET(pcp->pc_bitmap, fd);
mutex_exit(&pcp->pc_lock);
return (0);
}
/*
* The entry is not really deleted. The fields are cleared so that the
* entry is no longer useful, but it will remain in the hash table for reuse
* later. It will be freed when the polling lwp exits.
*/
int
pcache_delete_fd(pollstate_t *ps, int fd, size_t pos, int which, uint_t cevent)
{
pollcache_t *pcp = ps->ps_pcache;
polldat_t *pdp;
xref_t *refp;
ASSERT(fd < pcp->pc_mapsize);
ASSERT(MUTEX_HELD(&ps->ps_lock));
pdp = pcache_lookup_fd(pcp, fd);
ASSERT(pdp != NULL);
ASSERT(pdp->pd_count > 0);
ASSERT(pdp->pd_ref != NULL);
refp = &pdp->pd_ref[which];
if (pdp->pd_count == 1) {
pdp->pd_events = 0;
refp->xf_position = POLLPOSINVAL;
ASSERT(refp->xf_refcnt == 1);
refp->xf_refcnt = 0;
/*
* It is possible for a wakeup thread to get ahead of the
* following polldat_disassociate and set the bit in bitmap.
* That is OK because the bit will be cleared here anyway.
*/
polldat_disassociate(pdp);
pdp->pd_count = 0;
if (pdp->pd_fp != NULL) {
pdp->pd_fp = NULL;
delfpollinfo(fd);
}
mutex_enter(&pcp->pc_lock);
BT_CLEAR(pcp->pc_bitmap, fd);
mutex_exit(&pcp->pc_lock);
return (0);
}
if ((cevent & POLLCLOSED) == POLLCLOSED) {
/*
* fd cached here has been closed. This is the first
* pcache_delete_fd called after the close. Clean up the
* entire entry.
*/
pcacheset_invalidate(ps, pdp);
ASSERT(pdp->pd_php == NULL);
mutex_enter(&pcp->pc_lock);
BT_CLEAR(pcp->pc_bitmap, fd);
mutex_exit(&pcp->pc_lock);
return (0);
}
#ifdef DEBUG
if (getf(fd) != NULL) {
ASSERT(infpollinfo(fd));
releasef(fd);
}
#endif /* DEBUG */
pdp->pd_count--;
ASSERT(refp->xf_refcnt > 0);
if (--refp->xf_refcnt == 0) {
refp->xf_position = POLLPOSINVAL;
} else {
ASSERT(pos >= refp->xf_position);
if (pos == refp->xf_position) {
/*
* The xref position is no longer valid.
* Reset it to a special value and let
* caller know it needs to updatexref()
* with a new xf_position value.
*/
refp->xf_position = POLLPOSTRANS;
return (1);
}
}
return (0);
}
void
pcache_update_xref(pollcache_t *pcp, int fd, ssize_t pos, int which)
{
polldat_t *pdp;
pdp = pcache_lookup_fd(pcp, fd);
ASSERT(pdp != NULL);
ASSERT(pdp->pd_ref != NULL);
pdp->pd_ref[which].xf_position = pos;
}
#ifdef DEBUG
/*
* For each polled fd, it's either in the bitmap or cached in
* pcache hash table. If this routine returns 0, something is wrong.
*/
static int
pollchecksanity(pollstate_t *ps, nfds_t nfds)
{
int i;
int fd;
pollcache_t *pcp = ps->ps_pcache;
polldat_t *pdp;
pollfd_t *pollfdp = ps->ps_pollfd;
file_t *fp;
ASSERT(MUTEX_HELD(&ps->ps_lock));
for (i = 0; i < nfds; i++) {
fd = pollfdp[i].fd;
if (fd < 0) {
ASSERT(pollfdp[i].revents == 0);
continue;
}
if (pollfdp[i].revents == POLLNVAL)
continue;
if ((fp = getf(fd)) == NULL)
continue;
pdp = pcache_lookup_fd(pcp, fd);
ASSERT(pdp != NULL);
ASSERT(infpollinfo(fd));
ASSERT(pdp->pd_fp == fp);
releasef(fd);
if (BT_TEST(pcp->pc_bitmap, fd))
continue;
if (pdp->pd_php == NULL)
return (0);
}
return (1);
}
#endif /* DEBUG */
/*
* resolve the difference between the current poll list and a cached one.
*/
int
pcacheset_resolve(pollstate_t *ps, nfds_t nfds, int *fdcntp, int which)
{
int i;
pollcache_t *pcp = ps->ps_pcache;
pollfd_t *newlist = NULL;
pollfd_t *current = ps->ps_pollfd;
pollfd_t *cached;
pollcacheset_t *pcsp;
int common;
int count = 0;
int offset;
int remain;
int fd;
file_t *fp;
int fdcnt = 0;
int cnt = 0;
nfds_t old_nfds;
int error = 0;
int mismatch = 0;
ASSERT(MUTEX_HELD(&ps->ps_lock));
#ifdef DEBUG
checkpolldat(ps);
#endif
pcsp = &ps->ps_pcacheset[which];
old_nfds = pcsp->pcs_nfds;
common = (nfds > old_nfds) ? old_nfds : nfds;
if (nfds != old_nfds) {
/*
* the length of poll list has changed. allocate a new
* pollfd list.
*/
newlist = kmem_alloc(nfds * sizeof (pollfd_t), KM_SLEEP);
bcopy(current, newlist, sizeof (pollfd_t) * nfds);
}
/*
* Compare the overlapping part of the current fd list with the
* cached one. Whenever a difference is found, resolve it.
* The comparison is done on the current poll list and the
* cached list. But we may be setting up the newlist to be the
* cached list for next poll.
*/
cached = pcsp->pcs_pollfd;
remain = common;
while (count < common) {
int tmpfd;
pollfd_t *np;
np = (newlist != NULL) ? &newlist[count] : NULL;
offset = pcacheset_cmp(¤t[count], &cached[count], np,
remain);
/*
* Collect stats. If lists are completed the first time,
* it's a hit. Otherwise, it's a partial hit or miss.
*/
if ((count == 0) && (offset == common)) {
pollstats.pollcachehit.value.ui64++;
} else {
mismatch++;
}
count += offset;
if (offset < remain) {
ASSERT(count < common);
ASSERT((current[count].fd != cached[count].fd) ||
(current[count].events != cached[count].events));
/*
* Filter out invalid events.
*/
if (current[count].events & ~VALID_POLL_EVENTS) {
if (newlist != NULL) {
newlist[count].events =
current[count].events &=
VALID_POLL_EVENTS;
} else {
current[count].events &=
VALID_POLL_EVENTS;
}
}
/*
* when resolving a difference, we always remove the
* fd from cache before inserting one into cache.
*/
if (cached[count].fd >= 0) {
tmpfd = cached[count].fd;
if (pcache_delete_fd(ps, tmpfd, count, which,
(uint_t)cached[count].events)) {
/*
* This should be rare but needed for
* correctness.
*
* The first appearance in cached list
* is being "turned off". The same fd
* appear more than once in the cached
* poll list. Find the next one on the
* list and update the cached
* xf_position field.
*/
for (i = count + 1; i < old_nfds; i++) {
if (cached[i].fd == tmpfd) {
pcache_update_xref(pcp,
tmpfd, (ssize_t)i,
which);
break;
}
}
ASSERT(i <= old_nfds);
}
/*
* In case a new cache list is allocated,
* need to keep both cache lists in sync
* b/c the new one can be freed if we have
* an error later.
*/
cached[count].fd = -1;
if (newlist != NULL) {
newlist[count].fd = -1;
}
}
if ((tmpfd = current[count].fd) >= 0) {
/*
* add to the cached fd tbl and bitmap.
*/
if ((fp = getf(tmpfd)) == NULL) {
current[count].revents = POLLNVAL;
if (newlist != NULL) {
newlist[count].fd = -1;
}
cached[count].fd = -1;
fdcnt++;
} else {
/*
* Here we don't care about the
* fdcnt. We will examine the bitmap
* later and pick up the correct
* fdcnt there. So we never bother
* to check value of 'cnt'.
*/
error = pcache_insert(ps, fp,
¤t[count], &cnt,
(ssize_t)count, which);
/*
* if no error, we want to do releasef
* after we updated cache poll list
* entry so that close() won't race
* us.
*/
if (error) {
/*
* If we encountered an error,
* we have invalidated an
* entry in cached poll list
* (in pcache_delete_fd() above)
* but failed to add one here.
* This is OK b/c what's in the
* cached list is consistent
* with content of cache.
* It will not have any ill
* effect on next poll().
*/
releasef(tmpfd);
if (newlist != NULL) {
kmem_free(newlist,
nfds *
sizeof (pollfd_t));
}
return (error);
}
/*
* If we have allocated a new(temp)
* cache list, we need to keep both
* in sync b/c the new one can be freed
* if we have an error later.
*/
if (newlist != NULL) {
newlist[count].fd =
current[count].fd;
newlist[count].events =
current[count].events;
}
cached[count].fd = current[count].fd;
cached[count].events =
current[count].events;
releasef(tmpfd);
}
} else {
current[count].revents = 0;
}
count++;
remain = common - count;
}
}
if (mismatch != 0) {
if (mismatch == common) {
pollstats.pollcachemiss.value.ui64++;
} else {
pollstats.pollcachephit.value.ui64++;
}
}
/*
* take care of the non overlapping part of a list
*/
if (nfds > old_nfds) {
ASSERT(newlist != NULL);
for (i = old_nfds; i < nfds; i++) {
/* filter out invalid events */
if (current[i].events & ~VALID_POLL_EVENTS) {
newlist[i].events = current[i].events =
current[i].events & VALID_POLL_EVENTS;
}
if ((fd = current[i].fd) < 0) {
current[i].revents = 0;
continue;
}
/*
* add to the cached fd tbl and bitmap.
*/
if ((fp = getf(fd)) == NULL) {
current[i].revents = POLLNVAL;
newlist[i].fd = -1;
fdcnt++;
continue;
}
/*
* Here we don't care about the
* fdcnt. We will examine the bitmap
* later and pick up the correct
* fdcnt there. So we never bother to
* check 'cnt'.
*/
error = pcache_insert(ps, fp, ¤t[i], &cnt,
(ssize_t)i, which);
releasef(fd);
if (error) {
/*
* Here we are half way through adding newly
* polled fd. Undo enough to keep the cache
* list consistent with the cache content.
*/
pcacheset_remove_list(ps, current, old_nfds,
i, which, 0);
kmem_free(newlist, nfds * sizeof (pollfd_t));
return (error);
}
}
}
if (old_nfds > nfds) {
/*
* remove the fd's which are no longer polled.
*/
pcacheset_remove_list(ps, pcsp->pcs_pollfd, nfds, old_nfds,
which, 1);
}
/*
* set difference resolved. update nfds and cachedlist
* in pollstate struct.
*/
if (newlist != NULL) {
kmem_free(pcsp->pcs_pollfd, old_nfds * sizeof (pollfd_t));
/*
* By now, the pollfd.revents field should
* all be zeroed.
*/
pcsp->pcs_pollfd = newlist;
pcsp->pcs_nfds = nfds;
}
ASSERT(*fdcntp == 0);
*fdcntp = fdcnt;
/*
* By now for every fd in pollfdp, one of the following should be
* true. Otherwise we will miss a polled event.
*
* 1. the bit corresponding to the fd in bitmap is set. So VOP_POLL
* will be called on this fd in next poll.
* 2. the fd is cached in the pcache (i.e. pd_php is set). So
* pollnotify will happen.
*/
ASSERT(pollchecksanity(ps, nfds));
/*
* make sure cross reference between cached poll lists and cached
* poll fds are correct.
*/
ASSERT(pollcheckxref(ps, which));
/*
* ensure each polldat in pollcache reference a polled fd in
* pollcacheset.
*/
#ifdef DEBUG
checkpolldat(ps);
#endif
return (0);
}
#ifdef DEBUG
static int
pollscanrevents(pollcache_t *pcp, pollfd_t *pollfdp, nfds_t nfds)
{
int i;
int reventcnt = 0;
for (i = 0; i < nfds; i++) {
if (pollfdp[i].fd < 0) {
ASSERT(pollfdp[i].revents == 0);
continue;
}
if (pollfdp[i].revents) {
reventcnt++;
}
if (pollfdp[i].revents && (pollfdp[i].revents != POLLNVAL)) {
ASSERT(BT_TEST(pcp->pc_bitmap, pollfdp[i].fd));
}
}
return (reventcnt);
}
#endif /* DEBUG */
/*
* read the bitmap and poll on fds corresponding to the '1' bits. The ps_lock
* is held upon entry.
*/
int
pcache_poll(pollfd_t *pollfdp, pollstate_t *ps, nfds_t nfds, int *fdcntp,
int which)
{
int i;
pollcache_t *pcp;
int fd;
int begin, end, done;
pollhead_t *php;
int fdcnt;
int error = 0;
file_t *fp;
polldat_t *pdp;
xref_t *refp;
int entry;
pcp = ps->ps_pcache;
ASSERT(MUTEX_HELD(&ps->ps_lock));
ASSERT(MUTEX_HELD(&pcp->pc_lock));
retry:
done = 0;
begin = 0;
fdcnt = 0;
end = pcp->pc_mapend;
while ((fdcnt < nfds) && !done) {
php = NULL;
/*
* only poll fds which may have events
*/
fd = bt_getlowbit(pcp->pc_bitmap, begin, end);
ASSERT(fd <= end);
if (fd >= 0) {
ASSERT(pollcheckrevents(ps, begin, fd, which));
/*
* adjust map pointers for next round
*/
if (fd == end) {
done = 1;
} else {
begin = fd + 1;
}
/*
* A bitmap caches poll state information of
* multiple poll lists. Call VOP_POLL only if
* the bit corresponds to an fd in this poll
* list.
*/
pdp = pcache_lookup_fd(pcp, fd);
ASSERT(pdp != NULL);
ASSERT(pdp->pd_ref != NULL);
refp = &pdp->pd_ref[which];
if (refp->xf_refcnt == 0)
continue;
entry = refp->xf_position;
ASSERT((entry >= 0) && (entry < nfds));
ASSERT(pollfdp[entry].fd == fd);
/*
* we are in this routine implies that we have
* successfully polled this fd in the past.
* Check to see this fd is closed while we are
* blocked in poll. This ensures that we don't
* miss a close on the fd in the case this fd is
* reused.
*/
if (pdp->pd_fp == NULL) {
ASSERT(pdp->pd_count > 0);
pollfdp[entry].revents = POLLNVAL;
fdcnt++;
if (refp->xf_refcnt > 1) {
/*
* this fd appeared multiple time
* in the poll list. Find all of them.
*/
for (i = entry + 1; i < nfds; i++) {
if (pollfdp[i].fd == fd) {
pollfdp[i].revents =
POLLNVAL;
fdcnt++;
}
}
}
pcacheset_invalidate(ps, pdp);
continue;
}
/*
* We can be here polling a device that is being
* closed (i.e. the file pointer is set to NULL,
* but pollcacheclean has not happened yet).
*/
if ((fp = getf(fd)) == NULL) {
pollfdp[entry].revents = POLLNVAL;
fdcnt++;
if (refp->xf_refcnt > 1) {
/*
* this fd appeared multiple time
* in the poll list. Find all of them.
*/
for (i = entry + 1; i < nfds; i++) {
if (pollfdp[i].fd == fd) {
pollfdp[i].revents =
POLLNVAL;
fdcnt++;
}
}
}
continue;
}
ASSERT(pdp->pd_fp == fp);
ASSERT(infpollinfo(fd));
/*
* Since we no longer hold poll head lock across
* VOP_POLL, pollunlock logic can be simplifed.
*/
ASSERT(pdp->pd_php == NULL ||
MUTEX_NOT_HELD(PHLOCK(pdp->pd_php)));
/*
* underlying file systems may set a "pollpending"
* flag when it sees the poll may block. Pollwakeup()
* is called by wakeup thread if pollpending is set.
* Pass a 0 fdcnt so that the underlying file system
* will set the "pollpending" flag set when there is
* no polled events.
*
* Use pollfdp[].events for actual polling because
* the pd_events is union of all cached poll events
* on this fd. The events parameter also affects
* how the polled device sets the "poll pending"
* flag.
*/
ASSERT(curthread->t_pollcache == NULL);
error = VOP_POLL(fp->f_vnode, pollfdp[entry].events, 0,
&pollfdp[entry].revents, &php, NULL);
/*
* releasef after completely done with this cached
* poll entry. To prevent close() coming in to clear
* this entry.
*/
if (error) {
releasef(fd);
break;
}
/*
* layered devices (e.g. console driver)
* may change the vnode and thus the pollhead
* pointer out from underneath us.
*/
if (php != NULL && pdp->pd_php != NULL &&
php != pdp->pd_php) {
releasef(fd);
polldat_disassociate(pdp);
polldat_associate(pdp, php);
/*
* We could have missed a wakeup on the new
* target device. Make sure the new target
* gets polled once.
*/
BT_SET(pcp->pc_bitmap, fd);
goto retry;
}
if (pollfdp[entry].revents) {
ASSERT(refp->xf_refcnt >= 1);
fdcnt++;
if (refp->xf_refcnt > 1) {
/*
* this fd appeared multiple time
* in the poll list. This is rare but
* we have to look at all of them for
* correctness.
*/
error = plist_chkdupfd(fp, pdp, ps,
pollfdp, entry, &fdcnt);
if (error > 0) {
releasef(fd);
break;
}
if (error < 0) {
goto retry;
}
}
releasef(fd);
} else {
/*
* VOP_POLL didn't return any revents. We can
* clear the bit in bitmap only if we have the
* pollhead ptr cached and no other cached
* entry is polling different events on this fd.
* VOP_POLL may have dropped the ps_lock. Make
* sure pollwakeup has not happened before clear
* the bit.
*/
if ((pdp->pd_php != NULL) &&
(pollfdp[entry].events == pdp->pd_events) &&
((pcp->pc_flag & PC_POLLWAKE) == 0)) {
BT_CLEAR(pcp->pc_bitmap, fd);
}
/*
* if the fd can be cached now but not before,
* do it now.
*/
if ((pdp->pd_php == NULL) && (php != NULL)) {
polldat_associate(pdp, php);
/*
* We are inserting a polldat struct for
* the first time. We may have missed a
* wakeup on this device. Re-poll once.
* This should be a rare event.
*/
releasef(fd);
goto retry;
}
if (refp->xf_refcnt > 1) {
/*
* this fd appeared multiple time
* in the poll list. This is rare but
* we have to look at all of them for
* correctness.
*/
error = plist_chkdupfd(fp, pdp, ps,
pollfdp, entry, &fdcnt);
if (error > 0) {
releasef(fd);
break;
}
if (error < 0) {
goto retry;
}
}
releasef(fd);
}
} else {
done = 1;
ASSERT(pollcheckrevents(ps, begin, end + 1, which));
}
}
if (!error) {
ASSERT(*fdcntp + fdcnt == pollscanrevents(pcp, pollfdp, nfds));
*fdcntp += fdcnt;
}
return (error);
}
/*
* Going through the poll list without much locking. Poll all fds and
* cache all valid fds in the pollcache.
*/
int
pcacheset_cache_list(pollstate_t *ps, pollfd_t *fds, int *fdcntp, int which)
{
pollfd_t *pollfdp = ps->ps_pollfd;
pollcacheset_t *pcacheset = ps->ps_pcacheset;
pollfd_t *newfdlist;
int i;
int fd;
file_t *fp;
int error = 0;
ASSERT(MUTEX_HELD(&ps->ps_lock));
ASSERT(which < ps->ps_nsets);
ASSERT(pcacheset != NULL);
ASSERT(pcacheset[which].pcs_pollfd == NULL);
newfdlist = kmem_alloc(ps->ps_nfds * sizeof (pollfd_t), KM_SLEEP);
/*
* cache the new poll list in pollcachset.
*/
bcopy(pollfdp, newfdlist, sizeof (pollfd_t) * ps->ps_nfds);
pcacheset[which].pcs_pollfd = newfdlist;
pcacheset[which].pcs_nfds = ps->ps_nfds;
pcacheset[which].pcs_usradr = (uintptr_t)fds;
/*
* We have saved a copy of current poll fd list in one pollcacheset.
* The 'revents' field of the new list is not yet set to 0. Loop
* through the new list just to do that is expensive. We do that
* while polling the list.
*/
for (i = 0; i < ps->ps_nfds; i++) {
fd = pollfdp[i].fd;
/*
* We also filter out the illegal poll events in the event
* field for the cached poll list/set.
*/
if (pollfdp[i].events & ~VALID_POLL_EVENTS) {
newfdlist[i].events = pollfdp[i].events =
pollfdp[i].events & VALID_POLL_EVENTS;
}
if (fd < 0) {
pollfdp[i].revents = 0;
continue;
}
if ((fp = getf(fd)) == NULL) {
pollfdp[i].revents = POLLNVAL;
/*
* invalidate this cache entry in the cached poll list
*/
newfdlist[i].fd = -1;
(*fdcntp)++;
continue;
}
/*
* cache this fd.
*/
error = pcache_insert(ps, fp, &pollfdp[i], fdcntp, (ssize_t)i,
which);
releasef(fd);
if (error) {
/*
* Here we are half way through caching a new
* poll list. Undo every thing.
*/
pcacheset_remove_list(ps, pollfdp, 0, i, which, 0);
kmem_free(newfdlist, ps->ps_nfds * sizeof (pollfd_t));
pcacheset[which].pcs_pollfd = NULL;
pcacheset[which].pcs_usradr = (uintptr_t)NULL;
break;
}
}
return (error);
}
/*
* called by pollcacheclean() to set the fp NULL. It also sets polled events
* in pcacheset entries to a special events 'POLLCLOSED'. Do a pollwakeup to
* wake any sleeping poller, then remove the polldat from the driver.
* The routine is called with ps_pcachelock held.
*/
void
pcache_clean_entry(pollstate_t *ps, int fd)
{
pollcache_t *pcp;
polldat_t *pdp;
int i;
ASSERT(ps != NULL);
ASSERT(MUTEX_HELD(&ps->ps_lock));
pcp = ps->ps_pcache;
ASSERT(pcp);
pdp = pcache_lookup_fd(pcp, fd);
ASSERT(pdp != NULL);
/*
* the corresponding fpollinfo in fi_list has been removed by
* a close on this fd. Reset the cached fp ptr here.
*/
pdp->pd_fp = NULL;
/*
* XXX - This routine also touches data in pcacheset struct.
*
* set the event in cached poll lists to POLLCLOSED. This invalidate
* the cached poll fd entry in that poll list, which will force a
* removal of this cached entry in next poll(). The cleanup is done
* at the removal time.
*/
ASSERT(pdp->pd_ref != NULL);
for (i = 0; i < ps->ps_nsets; i++) {
xref_t *refp;
pollcacheset_t *pcsp;
refp = &pdp->pd_ref[i];
if (refp->xf_refcnt) {
ASSERT(refp->xf_position >= 0);
pcsp = &ps->ps_pcacheset[i];
if (refp->xf_refcnt == 1) {
pcsp->pcs_pollfd[refp->xf_position].events =
(short)POLLCLOSED;
}
if (refp->xf_refcnt > 1) {
int j;
/*
* mark every matching entry in pcs_pollfd
*/
for (j = refp->xf_position;
j < pcsp->pcs_nfds; j++) {
if (pcsp->pcs_pollfd[j].fd == fd) {
pcsp->pcs_pollfd[j].events =
(short)POLLCLOSED;
}
}
}
}
}
if (pdp->pd_php) {
/*
* Using pdp->pd_php is a bit risky here, as we lack any
* protection from a racing close operation which could free
* that pollhead prior to pollwakeup() acquiring the locks
* necessary to make it safe.
*/
pollwakeup(pdp->pd_php, POLLHUP);
polldat_disassociate(pdp);
}
}
void
pcache_wake_parents(pollcache_t *pcp)
{
pcachelink_t *pl, *pln;
ASSERT(MUTEX_HELD(&pcp->pc_lock));
for (pl = pcp->pc_parents; pl != NULL; pl = pln) {
mutex_enter(&pl->pcl_lock);
if (pl->pcl_state == PCL_VALID) {
ASSERT(pl->pcl_parent_pc != NULL);
cv_broadcast(&pl->pcl_parent_pc->pc_cv);
}
pln = pl->pcl_parent_next;
mutex_exit(&pl->pcl_lock);
}
}
/*
* Initialize thread pollstate structure.
* It will persist for the life of the thread, until it calls pollcleanup().
*/
pollstate_t *
pollstate_create()
{
pollstate_t *ps = curthread->t_pollstate;
if (ps == NULL) {
/*
* This is the first time this thread has ever polled, so we
* have to create its pollstate structure.
*/
ps = kmem_zalloc(sizeof (pollstate_t), KM_SLEEP);
ps->ps_nsets = POLLFDSETS;
ps->ps_pcacheset = pcacheset_create(ps->ps_nsets);
curthread->t_pollstate = ps;
} else {
ASSERT(ps->ps_depth == 0);
ASSERT(ps->ps_flags == 0);
ASSERT(ps->ps_pc_stack[0] == 0);
}
return (ps);
}
void
pollstate_destroy(pollstate_t *ps)
{
if (ps->ps_pollfd != NULL) {
kmem_free(ps->ps_pollfd, ps->ps_nfds * sizeof (pollfd_t));
ps->ps_pollfd = NULL;
}
if (ps->ps_pcache != NULL) {
pcache_destroy(ps->ps_pcache);
ps->ps_pcache = NULL;
}
pcacheset_destroy(ps->ps_pcacheset, ps->ps_nsets);
ps->ps_pcacheset = NULL;
if (ps->ps_dpbuf != NULL) {
kmem_free(ps->ps_dpbuf, ps->ps_dpbufsize);
ps->ps_dpbuf = NULL;
}
mutex_destroy(&ps->ps_lock);
kmem_free(ps, sizeof (pollstate_t));
}
static int
pollstate_contend(pollstate_t *ps, pollcache_t *pcp)
{
pollstate_t *rem, *next;
pollcache_t *desired_pc;
int result = 0, depth_total;
mutex_enter(&pollstate_contenders_lock);
/*
* There is a small chance that the pollcache of interest became
* available while we were waiting on the contenders lock.
*/
if (mutex_tryenter(&pcp->pc_lock) != 0) {
goto out;
}
/*
* Walk the list of contended pollstates, searching for evidence of a
* deadlock condition.
*/
depth_total = ps->ps_depth;
desired_pc = pcp;
for (rem = pollstate_contenders; rem != NULL; rem = next) {
int i, j;
next = rem->ps_contend_nextp;
/* Is this pollstate holding the pollcache of interest? */
for (i = 0; i < rem->ps_depth; i++) {
if (rem->ps_pc_stack[i] != desired_pc) {
continue;
}
/*
* The remote pollstate holds the pollcache lock we
* desire. If it is waiting on a pollcache we hold,
* then we can report the obvious deadlock.
*/
ASSERT(rem->ps_contend_pc != NULL);
for (j = 0; j < ps->ps_depth; j++) {
if (rem->ps_contend_pc == ps->ps_pc_stack[j]) {
rem->ps_flags |= POLLSTATE_STALEMATE;
result = -1;
goto out;
}
}
/*
* The remote pollstate is not blocking on a pollcache
* which would deadlock against us. That pollcache
* may, however, be held by a pollstate which would
* result in a deadlock.
*
* To detect such a condition, we continue walking
* through the list using the pollcache blocking the
* remote thread as our new search target.
*
* Return to the front of pollstate_contenders since it
* is not ordered to guarantee complete dependency
* traversal. The below depth tracking places an upper
* bound on iterations.
*/
desired_pc = rem->ps_contend_pc;
next = pollstate_contenders;
/*
* The recursion depth of the remote pollstate is used
* to calculate a final depth for the local /dev/poll
* recursion, since those locks will be acquired
* eventually. If that value exceeds the defined
* limit, we can report the failure now instead of
* recursing to that failure depth.
*/
depth_total += (rem->ps_depth - i);
if (depth_total >= POLLMAXDEPTH) {
result = -1;
goto out;
}
}
}
/*
* No deadlock partner was found. The only course of action is to
* record ourself as a contended pollstate and wait for the pollcache
* mutex to become available.
*/
ps->ps_contend_pc = pcp;
ps->ps_contend_nextp = pollstate_contenders;
ps->ps_contend_pnextp = &pollstate_contenders;
if (pollstate_contenders != NULL) {
pollstate_contenders->ps_contend_pnextp =
&ps->ps_contend_nextp;
}
pollstate_contenders = ps;
mutex_exit(&pollstate_contenders_lock);
mutex_enter(&pcp->pc_lock);
mutex_enter(&pollstate_contenders_lock);
/*
* Our acquisition of the pollcache mutex may be due to another thread
* giving up in the face of deadlock with us. If that is the case,
* we too should report the failure.
*/
if ((ps->ps_flags & POLLSTATE_STALEMATE) != 0) {
result = -1;
ps->ps_flags &= ~POLLSTATE_STALEMATE;
mutex_exit(&pcp->pc_lock);
}
/* Remove ourself from the contenders list. */
if (ps->ps_contend_nextp != NULL) {
ps->ps_contend_nextp->ps_contend_pnextp =
ps->ps_contend_pnextp;
}
*ps->ps_contend_pnextp = ps->ps_contend_nextp;
ps->ps_contend_pc = NULL;
ps->ps_contend_nextp = NULL;
ps->ps_contend_pnextp = NULL;
out:
mutex_exit(&pollstate_contenders_lock);
return (result);
}
int
pollstate_enter(pollcache_t *pcp)
{
pollstate_t *ps = curthread->t_pollstate;
int i;
if (ps == NULL) {
/*
* The thread pollstate may not be initialized if VOP_POLL is
* called on a recursion-enabled /dev/poll handle from outside
* the poll() or /dev/poll codepaths.
*/
return (PSE_FAIL_POLLSTATE);
}
if (ps->ps_depth >= POLLMAXDEPTH) {
return (PSE_FAIL_DEPTH);
}
/*
* Check the desired pollcache against pollcaches we already have
* locked. Such a loop is the most simple deadlock scenario.
*/
for (i = 0; i < ps->ps_depth; i++) {
if (ps->ps_pc_stack[i] == pcp) {
return (PSE_FAIL_LOOP);
}
}
ASSERT(ps->ps_pc_stack[i] == NULL);
if (ps->ps_depth == 0) {
/* Locking initial the pollcache requires no caution */
mutex_enter(&pcp->pc_lock);
} else if (mutex_tryenter(&pcp->pc_lock) == 0) {
if (pollstate_contend(ps, pcp) != 0) {
/* This pollcache cannot safely be locked. */
return (PSE_FAIL_DEADLOCK);
}
}
ps->ps_pc_stack[ps->ps_depth++] = pcp;
return (PSE_SUCCESS);
}
void
pollstate_exit(pollcache_t *pcp)
{
pollstate_t *ps = curthread->t_pollstate;
VERIFY(ps != NULL);
VERIFY(ps->ps_pc_stack[ps->ps_depth - 1] == pcp);
mutex_exit(&pcp->pc_lock);
ps->ps_pc_stack[--ps->ps_depth] = NULL;
VERIFY(ps->ps_depth >= 0);
}
/*
* We are holding the appropriate uf_lock entering this routine.
* Bump up the ps_busy count to prevent the thread from exiting.
*/
void
pollblockexit(fpollinfo_t *fpip)
{
for (; fpip; fpip = fpip->fp_next) {
pollcache_t *pcp = fpip->fp_thread->t_pollstate->ps_pcache;
mutex_enter(&pcp->pc_no_exit);
pcp->pc_busy++; /* prevents exit()'s */
mutex_exit(&pcp->pc_no_exit);
}
}
/*
* Complete phase 2 of cached poll fd cleanup. Call pcache_clean_entry to mark
* the pcacheset events field POLLCLOSED to force the next poll() to remove
* this cache entry. We can't clean the polldat entry clean up here because
* lwp block in poll() needs the info to return. Wakeup anyone blocked in
* poll and let exiting lwp go. No lock is help upon entry. So it's OK for
* pcache_clean_entry to call pollwakeup().
*/
void
pollcacheclean(fpollinfo_t *fip, int fd)
{
struct fpollinfo *fpip, *fpip2;
fpip = fip;
while (fpip) {
pollstate_t *ps = fpip->fp_thread->t_pollstate;
pollcache_t *pcp = ps->ps_pcache;
mutex_enter(&ps->ps_lock);
pcache_clean_entry(ps, fd);
mutex_exit(&ps->ps_lock);
mutex_enter(&pcp->pc_no_exit);
pcp->pc_busy--;
if (pcp->pc_busy == 0) {
/*
* Wakeup the thread waiting in
* thread_exit().
*/
cv_signal(&pcp->pc_busy_cv);
}
mutex_exit(&pcp->pc_no_exit);
fpip2 = fpip;
fpip = fpip->fp_next;
kmem_free(fpip2, sizeof (fpollinfo_t));
}
}
/*
* one of the cache line's counter is wrapping around. Reset all cache line
* counters to zero except one. This is simplistic, but probably works
* effectively.
*/
void
pcacheset_reset_count(pollstate_t *ps, int index)
{
int i;
ASSERT(MUTEX_HELD(&ps->ps_lock));
for (i = 0; i < ps->ps_nsets; i++) {
if (ps->ps_pcacheset[i].pcs_pollfd != NULL) {
ps->ps_pcacheset[i].pcs_count = 0;
}
}
ps->ps_pcacheset[index].pcs_count = 1;
}
/*
* this routine implements poll cache list replacement policy.
* It is currently choose the "least used".
*/
int
pcacheset_replace(pollstate_t *ps)
{
int i;
int index = 0;
ASSERT(MUTEX_HELD(&ps->ps_lock));
for (i = 1; i < ps->ps_nsets; i++) {
if (ps->ps_pcacheset[index].pcs_count >
ps->ps_pcacheset[i].pcs_count) {
index = i;
}
}
ps->ps_pcacheset[index].pcs_count = 0;
return (index);
}
/*
* this routine is called by strclose to remove remaining polldat struct on
* the pollhead list of the device being closed. There are two reasons as why
* the polldat structures still remain on the pollhead list:
*
* (1) The layered device(e.g.the console driver).
* In this case, the existence of a polldat implies that the thread putting
* the polldat on this list has not exited yet. Before the thread exits, it
* will have to hold this pollhead lock to remove the polldat. So holding the
* pollhead lock here effectively prevents the thread which put the polldat
* on this list from exiting.
*
* (2) /dev/poll.
* When a polled fd is cached in /dev/poll, its polldat will remain on the
* pollhead list if the process has not done a POLLREMOVE before closing the
* polled fd. We just unlink it here.
*/
void
pollhead_clean(pollhead_t *php)
{
polldat_t *pdp;
/*
* In case(1), while we must prevent the thread in question from
* exiting, we must also obey the proper locking order, i.e.
* (ps_lock -> phlock).
*/
PH_ENTER(php);
while (php->ph_list != NULL) {
pollstate_t *ps;
pollcache_t *pcp;
pdp = php->ph_list;
ASSERT(pdp->pd_php == php);
if (pdp->pd_thread == NULL) {
/*
* This is case(2). Since the ph_lock is sufficient
* to synchronize this lwp with any other /dev/poll
* lwp, just unlink the polldat.
*/
php->ph_list = pdp->pd_next;
pdp->pd_php = NULL;
pdp->pd_next = NULL;
continue;
}
ps = pdp->pd_thread->t_pollstate;
ASSERT(ps != NULL);
pcp = pdp->pd_pcache;
ASSERT(pcp != NULL);
mutex_enter(&pcp->pc_no_exit);
pcp->pc_busy++; /* prevents exit()'s */
mutex_exit(&pcp->pc_no_exit);
/*
* Now get the locks in proper order to avoid deadlock.
*/
PH_EXIT(php);
mutex_enter(&ps->ps_lock);
/*
* while we dropped the pollhead lock, the element could be
* taken off the list already.
*/
PH_ENTER(php);
if (pdp->pd_php == php) {
ASSERT(pdp == php->ph_list);
php->ph_list = pdp->pd_next;
pdp->pd_php = NULL;
pdp->pd_next = NULL;
}
PH_EXIT(php);
mutex_exit(&ps->ps_lock);
mutex_enter(&pcp->pc_no_exit);
pcp->pc_busy--;
if (pcp->pc_busy == 0) {
/*
* Wakeup the thread waiting in
* thread_exit().
*/
cv_signal(&pcp->pc_busy_cv);
}
mutex_exit(&pcp->pc_no_exit);
PH_ENTER(php);
}
PH_EXIT(php);
}
/*
* The remove_list is called to cleanup a partially cached 'current' list or
* to remove a partial list which is no longer cached. The flag value of 1
* indicates the second case.
*/
void
pcacheset_remove_list(pollstate_t *ps, pollfd_t *pollfdp, int start, int end,
int cacheindex, int flag)
{
int i;
ASSERT(MUTEX_HELD(&ps->ps_lock));
for (i = start; i < end; i++) {
if ((pollfdp[i].fd >= 0) &&
(flag || !(pollfdp[i].revents & POLLNVAL))) {
if (pcache_delete_fd(ps, pollfdp[i].fd, i, cacheindex,
(uint_t)pollfdp[i].events)) {
int j;
int fd = pollfdp[i].fd;
for (j = i + 1; j < end; j++) {
if (pollfdp[j].fd == fd) {
pcache_update_xref(
ps->ps_pcache, fd,
(ssize_t)j, cacheindex);
break;
}
}
ASSERT(j <= end);
}
}
}
}
#ifdef DEBUG
#include<sys/strsubr.h>
/*
* make sure curthread is not on anyone's pollhead list any more.
*/
static void
pollcheckphlist()
{
int i;
file_t *fp;
uf_entry_t *ufp;
uf_info_t *fip = P_FINFO(curproc);
struct stdata *stp;
polldat_t *pdp;
mutex_enter(&fip->fi_lock);
for (i = 0; i < fip->fi_nfiles; i++) {
UF_ENTER(ufp, fip, i);
if ((fp = ufp->uf_file) != NULL) {
if ((stp = fp->f_vnode->v_stream) != NULL) {
PH_ENTER(&stp->sd_pollist);
pdp = stp->sd_pollist.ph_list;
while (pdp) {
ASSERT(pdp->pd_thread != curthread);
pdp = pdp->pd_next;
}
PH_EXIT(&stp->sd_pollist);
}
}
UF_EXIT(ufp);
}
mutex_exit(&fip->fi_lock);
}
/*
* for resolved set poll list, the xref info in the pcache should be
* consistent with this poll list.
*/
static int
pollcheckxref(pollstate_t *ps, int cacheindex)
{
pollfd_t *pollfdp = ps->ps_pcacheset[cacheindex].pcs_pollfd;
pollcache_t *pcp = ps->ps_pcache;
polldat_t *pdp;
int i;
xref_t *refp;
for (i = 0; i < ps->ps_pcacheset[cacheindex].pcs_nfds; i++) {
if (pollfdp[i].fd < 0) {
continue;
}
pdp = pcache_lookup_fd(pcp, pollfdp[i].fd);
ASSERT(pdp != NULL);
ASSERT(pdp->pd_ref != NULL);
refp = &pdp->pd_ref[cacheindex];
if (refp->xf_position >= 0) {
ASSERT(refp->xf_refcnt >= 1);
ASSERT(pollfdp[refp->xf_position].fd == pdp->pd_fd);
if (refp->xf_refcnt > 1) {
int j;
int count = 0;
for (j = refp->xf_position;
j < ps->ps_pcacheset[cacheindex].pcs_nfds;
j++) {
if (pollfdp[j].fd == pdp->pd_fd) {
count++;
}
}
ASSERT(count == refp->xf_refcnt);
}
}
}
return (1);
}
/*
* For every cached pollfd, its polldat struct should be consistent with
* what is in the pcacheset lists.
*/
static void
checkpolldat(pollstate_t *ps)
{
pollcache_t *pcp = ps->ps_pcache;
polldat_t **hashtbl;
int i;
hashtbl = pcp->pc_hash;
for (i = 0; i < pcp->pc_hashsize; i++) {
polldat_t *pdp;
for (pdp = hashtbl[i]; pdp; pdp = pdp->pd_hashnext) {
ASSERT(pdp->pd_ref != NULL);
if (pdp->pd_count > 0) {
xref_t *refp;
int j;
pollcacheset_t *pcsp;
pollfd_t *pollfd;
for (j = 0; j < ps->ps_nsets; j++) {
refp = &pdp->pd_ref[j];
if (refp->xf_refcnt > 0) {
pcsp = &ps->ps_pcacheset[j];
ASSERT(refp->xf_position <
pcsp->pcs_nfds);
pollfd = pcsp->pcs_pollfd;
ASSERT(pdp->pd_fd ==
pollfd[refp->xf_position].
fd);
}
}
}
}
}
}
/*
* every wfd element on ph_list must have a corresponding fpollinfo on the
* uf_fpollinfo list. This is a variation of infpollinfo() w/o holding locks.
*/
void
checkwfdlist(vnode_t *vp, fpollinfo_t *fpip)
{
stdata_t *stp;
polldat_t *pdp;
fpollinfo_t *fpip2;
if ((stp = vp->v_stream) == NULL) {
return;
}
PH_ENTER(&stp->sd_pollist);
for (pdp = stp->sd_pollist.ph_list; pdp; pdp = pdp->pd_next) {
if (pdp->pd_thread != NULL &&
pdp->pd_thread->t_procp == curthread->t_procp) {
for (fpip2 = fpip; fpip2; fpip2 = fpip2->fp_next) {
if (pdp->pd_thread == fpip2->fp_thread) {
break;
}
}
ASSERT(fpip2 != NULL);
}
}
PH_EXIT(&stp->sd_pollist);
}
/*
* For each cached fd whose bit is not set in bitmap, its revents field in
* current poll list should be 0.
*/
static int
pollcheckrevents(pollstate_t *ps, int begin, int end, int cacheindex)
{
pollcache_t *pcp = ps->ps_pcache;
pollfd_t *pollfdp = ps->ps_pollfd;
int i;
for (i = begin; i < end; i++) {
polldat_t *pdp;
ASSERT(!BT_TEST(pcp->pc_bitmap, i));
pdp = pcache_lookup_fd(pcp, i);
if (pdp && pdp->pd_fp != NULL) {
xref_t *refp;
int entry;
ASSERT(pdp->pd_ref != NULL);
refp = &pdp->pd_ref[cacheindex];
if (refp->xf_refcnt == 0) {
continue;
}
entry = refp->xf_position;
ASSERT(entry >= 0);
ASSERT(pollfdp[entry].revents == 0);
if (refp->xf_refcnt > 1) {
int j;
for (j = entry + 1; j < ps->ps_nfds; j++) {
if (pollfdp[j].fd == i) {
ASSERT(pollfdp[j].revents == 0);
}
}
}
}
}
return (1);
}
#endif /* DEBUG */
pollcache_t *
pcache_alloc()
{
return (kmem_zalloc(sizeof (pollcache_t), KM_SLEEP));
}
void
pcache_create(pollcache_t *pcp, nfds_t nfds)
{
size_t mapsize;
/*
* allocate enough bits for the poll fd list
*/
if ((mapsize = POLLMAPCHUNK) <= nfds) {
mapsize = (nfds + POLLMAPCHUNK - 1) & ~(POLLMAPCHUNK - 1);
}
pcp->pc_bitmap = kmem_zalloc((mapsize / BT_NBIPUL) * sizeof (ulong_t),
KM_SLEEP);
pcp->pc_mapsize = mapsize;
/*
* The hash size is at least POLLHASHCHUNKSZ. If user polls a large
* number of fd to start with, allocate a bigger hash table (to the
* nearest multiple of POLLHASHCHUNKSZ) because dynamically growing a
* hash table is expensive.
*/
if (nfds < POLLHASHCHUNKSZ) {
pcp->pc_hashsize = POLLHASHCHUNKSZ;
} else {
pcp->pc_hashsize = (nfds + POLLHASHCHUNKSZ - 1) &
~(POLLHASHCHUNKSZ - 1);
}
pcp->pc_hash = kmem_zalloc(pcp->pc_hashsize * sizeof (polldat_t *),
KM_SLEEP);
}
void
pcache_destroy(pollcache_t *pcp)
{
polldat_t **hashtbl;
int i;
hashtbl = pcp->pc_hash;
for (i = 0; i < pcp->pc_hashsize; i++) {
if (hashtbl[i] != NULL) {
polldat_t *pdp, *pdp2;
pdp = hashtbl[i];
while (pdp != NULL) {
pdp2 = pdp->pd_hashnext;
if (pdp->pd_ref != NULL) {
kmem_free(pdp->pd_ref, sizeof (xref_t) *
pdp->pd_nsets);
}
kmem_free(pdp, sizeof (polldat_t));
pdp = pdp2;
pcp->pc_fdcount--;
}
}
}
ASSERT(pcp->pc_fdcount == 0);
kmem_free(pcp->pc_hash, sizeof (polldat_t *) * pcp->pc_hashsize);
kmem_free(pcp->pc_bitmap,
sizeof (ulong_t) * (pcp->pc_mapsize/BT_NBIPUL));
mutex_destroy(&pcp->pc_no_exit);
mutex_destroy(&pcp->pc_lock);
cv_destroy(&pcp->pc_cv);
cv_destroy(&pcp->pc_busy_cv);
kmem_free(pcp, sizeof (pollcache_t));
}
pollcacheset_t *
pcacheset_create(int nsets)
{
return (kmem_zalloc(sizeof (pollcacheset_t) * nsets, KM_SLEEP));
}
void
pcacheset_destroy(pollcacheset_t *pcsp, int nsets)
{
int i;
for (i = 0; i < nsets; i++) {
if (pcsp[i].pcs_pollfd != NULL) {
kmem_free(pcsp[i].pcs_pollfd, pcsp[i].pcs_nfds *
sizeof (pollfd_t));
}
}
kmem_free(pcsp, sizeof (pollcacheset_t) * nsets);
}
/*
* Check each duplicated poll fd in the poll list. It may be necessary to
* VOP_POLL the same fd again using different poll events. getf() has been
* done by caller. This routine returns 0 if it can sucessfully process the
* entire poll fd list. It returns -1 if underlying vnode has changed during
* a VOP_POLL, in which case the caller has to repoll. It returns a positive
* value if VOP_POLL failed.
*/
static int
plist_chkdupfd(file_t *fp, polldat_t *pdp, pollstate_t *psp, pollfd_t *pollfdp,
int entry, int *fdcntp)
{
int i;
int fd;
nfds_t nfds = psp->ps_nfds;
fd = pollfdp[entry].fd;
for (i = entry + 1; i < nfds; i++) {
if (pollfdp[i].fd == fd) {
if (pollfdp[i].events == pollfdp[entry].events) {
if ((pollfdp[i].revents =
pollfdp[entry].revents) != 0) {
(*fdcntp)++;
}
} else {
int error;
pollhead_t *php;
pollcache_t *pcp = psp->ps_pcache;
/*
* the events are different. VOP_POLL on this
* fd so that we don't miss any revents.
*/
php = NULL;
ASSERT(curthread->t_pollcache == NULL);
error = VOP_POLL(fp->f_vnode,
pollfdp[i].events, 0,
&pollfdp[i].revents, &php, NULL);
if (error) {
return (error);
}
/*
* layered devices(e.g. console driver)
* may change the vnode and thus the pollhead
* pointer out from underneath us.
*/
if (php != NULL && pdp->pd_php != NULL &&
php != pdp->pd_php) {
polldat_disassociate(pdp);
polldat_associate(pdp, php);
/*
* We could have missed a wakeup on the
* new target device. Make sure the new
* target gets polled once.
*/
BT_SET(pcp->pc_bitmap, fd);
return (-1);
}
if (pollfdp[i].revents) {
(*fdcntp)++;
}
}
}
}
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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/cred_impl.h>
#include <sys/errno.h>
#include <sys/klpd.h>
#include <sys/proc.h>
#include <sys/priv_impl.h>
#include <sys/policy.h>
#include <sys/ddi.h>
#include <sys/thread.h>
#include <sys/cmn_err.h>
#include <c2/audit.h>
/*
* System call support for manipulating privileges.
*
*
* setppriv(2) - set process privilege set
* getppriv(2) - get process privilege set
* getprivimplinfo(2) - get process privilege implementation information
* setpflags(2) - set process (privilege) flags
* getpflags(2) - get process (privilege) flags
*/
/*
* setppriv (priv_op_t, priv_ptype_t, priv_set_t)
*/
static int
setppriv(priv_op_t op, priv_ptype_t type, priv_set_t *in_pset)
{
priv_set_t pset, *target;
cred_t *cr, *pcr;
proc_t *p;
boolean_t donocd = B_FALSE;
if (!PRIV_VALIDSET(type) || !PRIV_VALIDOP(op))
return (set_errno(EINVAL));
if (copyin(in_pset, &pset, sizeof (priv_set_t)))
return (set_errno(EFAULT));
p = ttoproc(curthread);
cr = cralloc();
mutex_enter(&p->p_crlock);
retry:
pcr = p->p_cred;
if (AU_AUDITING())
audit_setppriv(op, type, &pset, pcr);
/*
* Filter out unallowed request (bad op and bad type)
*/
switch (op) {
case PRIV_ON:
case PRIV_SET:
/*
* Turning on privileges; the limit set cannot grow,
* other sets can but only as long as they remain subsets
* of P. Only immediately after exec holds that P <= L.
*/
if (type == PRIV_LIMIT &&
!priv_issubset(&pset, &CR_LPRIV(pcr))) {
mutex_exit(&p->p_crlock);
crfree(cr);
return (set_errno(EPERM));
}
if (!priv_issubset(&pset, &CR_OPPRIV(pcr)) &&
!priv_issubset(&pset, priv_getset(pcr, type))) {
mutex_exit(&p->p_crlock);
/* Policy override should not grow beyond L either */
if (type != PRIV_INHERITABLE ||
!priv_issubset(&pset, &CR_LPRIV(pcr)) ||
secpolicy_require_privs(CRED(), &pset) != 0) {
crfree(cr);
return (set_errno(EPERM));
}
mutex_enter(&p->p_crlock);
if (pcr != p->p_cred)
goto retry;
donocd = B_TRUE;
}
break;
case PRIV_OFF:
/* PRIV_OFF is always allowed */
break;
}
/*
* OK! everything is cool.
* Do cred COW.
*/
crcopy_to(pcr, cr);
/*
* If we change the effective, permitted or limit set, we attain
* "privilege awareness".
*/
if (type != PRIV_INHERITABLE)
priv_set_PA(cr);
target = &(CR_PRIVS(cr)->crprivs[type]);
switch (op) {
case PRIV_ON:
priv_union(&pset, target);
break;
case PRIV_OFF:
priv_inverse(&pset);
priv_intersect(target, &pset);
/*
* Fall-thru to set target and change other process
* privilege sets.
*/
/*FALLTHRU*/
case PRIV_SET:
*target = pset;
/*
* Take privileges no longer permitted out
* of other effective sets as well.
* Limit set is enforced at exec() time.
*/
if (type == PRIV_PERMITTED)
priv_intersect(&pset, &CR_EPRIV(cr));
break;
}
/*
* When we give up privileges not in the inheritable set,
* set SNOCD if not already set; first we compute the
* privileges removed from P using Diff = (~P') & P
* and then we check whether the removed privileges are
* a subset of I. If we retain uid 0, all privileges
* are required anyway so don't set SNOCD.
*/
if (type == PRIV_PERMITTED && (p->p_flag & SNOCD) == 0 &&
cr->cr_uid != 0 && cr->cr_ruid != 0 && cr->cr_suid != 0) {
priv_set_t diff = CR_OPPRIV(cr);
priv_inverse(&diff);
priv_intersect(&CR_OPPRIV(pcr), &diff);
donocd = !priv_issubset(&diff, &CR_IPRIV(cr));
}
p->p_cred = cr;
mutex_exit(&p->p_crlock);
if (donocd) {
mutex_enter(&p->p_lock);
p->p_flag |= SNOCD;
mutex_exit(&p->p_lock);
}
/*
* The basic_test privilege should not be removed from E;
* if that has happened, then some programmer typically set the E/P to
* empty. That is not portable.
*/
if ((type == PRIV_EFFECTIVE || type == PRIV_PERMITTED) &&
priv_basic_test >= 0 && !PRIV_ISASSERT(target, priv_basic_test)) {
proc_t *p = curproc;
pid_t pid = p->p_pid;
char *fn = PTOU(p)->u_comm;
cmn_err(CE_WARN, "%s[%d]: setppriv: basic_test privilege "
"removed from E/P", fn, pid);
}
crset(p, cr); /* broadcast to process threads */
return (0);
}
/*
* getppriv (priv_ptype_t, priv_set_t *)
*/
static int
getppriv(priv_ptype_t type, priv_set_t *pset)
{
if (!PRIV_VALIDSET(type))
return (set_errno(EINVAL));
if (copyout(priv_getset(CRED(), type), pset, sizeof (priv_set_t)) != 0)
return (set_errno(EFAULT));
return (0);
}
static int
getprivimplinfo(void *buf, size_t bufsize)
{
int err;
err = copyout(priv_hold_implinfo(), buf, min(bufsize, privinfosize));
priv_release_implinfo();
if (err)
return (set_errno(EFAULT));
return (0);
}
/*
* Set process flags in the given target cred. If NULL is specified, then
* CRED() is used; otherwise the cred is assumed to be modifiable (i.e. newly
* crdup'ed, or equivalent). Some flags are set in the proc rather than cred;
* for these, curproc is always used.
*
* For now we cheat: the flags are actually bit masks so we can simplify
* some; we do make sure that the arguments are valid, though.
*/
int
setpflags(uint_t flag, uint_t val, cred_t *tcr)
{
cred_t *cr, *pcr;
proc_t *p = curproc;
uint_t newflags;
boolean_t use_curcred = (tcr == NULL);
if (val > 1 || (flag != PRIV_DEBUG && flag != PRIV_AWARE &&
flag != NET_MAC_AWARE && flag != NET_MAC_AWARE_INHERIT &&
flag != __PROC_PROTECT && flag != PRIV_XPOLICY &&
flag != PRIV_AWARE_RESET && flag != PRIV_PFEXEC)) {
return (EINVAL);
}
if (flag == __PROC_PROTECT) {
mutex_enter(&p->p_lock);
if (val == 0)
p->p_flag &= ~SNOCD;
else
p->p_flag |= SNOCD;
mutex_exit(&p->p_lock);
return (0);
}
if (use_curcred) {
cr = cralloc();
mutex_enter(&p->p_crlock);
pcr = p->p_cred;
} else {
cr = pcr = tcr;
}
newflags = CR_FLAGS(pcr);
if (val != 0) {
if (flag == PRIV_AWARE)
newflags &= ~PRIV_AWARE_RESET;
newflags |= flag;
} else {
newflags &= ~flag;
}
/* No change */
if (CR_FLAGS(pcr) == newflags) {
if (use_curcred) {
mutex_exit(&p->p_crlock);
crfree(cr);
}
return (0);
}
/*
* Setting either the NET_MAC_AWARE or NET_MAC_AWARE_INHERIT
* flags is a restricted operation.
*
* When invoked via the PRIVSYS_SETPFLAGS syscall
* we require that the current cred has the net_mac_aware
* privilege in its effective set.
*
* When called from within the kernel by label-aware
* services such as NFS, we don't require a privilege check.
*
*/
if ((flag == NET_MAC_AWARE || flag == NET_MAC_AWARE_INHERIT) &&
(val == 1) && use_curcred) {
if (secpolicy_net_mac_aware(pcr) != 0) {
mutex_exit(&p->p_crlock);
crfree(cr);
return (EPERM);
}
}
/* Trying to unset PA; if we can't, return an error */
if (flag == PRIV_AWARE && val == 0 && !priv_can_clear_PA(pcr)) {
if (use_curcred) {
mutex_exit(&p->p_crlock);
crfree(cr);
}
return (EPERM);
}
/* Committed to changing the flag */
if (use_curcred)
crcopy_to(pcr, cr);
if (flag == PRIV_AWARE) {
if (val != 0)
priv_set_PA(cr);
else
priv_adjust_PA(cr);
} else {
CR_FLAGS(cr) = newflags;
}
/*
* Unsetting the flag has as side effect getting rid of
* the per-credential policy.
*/
if (flag == PRIV_XPOLICY && val == 0)
crsetcrklpd(cr, NULL);
if (use_curcred) {
p->p_cred = cr;
mutex_exit(&p->p_crlock);
crset(p, cr);
}
return (0);
}
/*
* Getpflags. Currently only implements single bit flags.
*/
uint_t
getpflags(uint_t flag, const cred_t *cr)
{
if (flag != PRIV_DEBUG && flag != PRIV_AWARE &&
flag != NET_MAC_AWARE && flag != NET_MAC_AWARE_INHERIT &&
flag != PRIV_XPOLICY && flag != PRIV_PFEXEC &&
flag != PRIV_AWARE_RESET)
return ((uint_t)-1);
return ((CR_FLAGS(cr) & flag) != 0);
}
/*
* Privilege system call entry point
*/
int
privsys(int code, priv_op_t op, priv_ptype_t type, void *buf, size_t bufsize,
int itype)
{
int retv;
extern int issetugid(void);
switch (code) {
case PRIVSYS_SETPPRIV:
if (bufsize < sizeof (priv_set_t))
return (set_errno(ENOMEM));
return (setppriv(op, type, buf));
case PRIVSYS_GETPPRIV:
if (bufsize < sizeof (priv_set_t))
return (set_errno(ENOMEM));
return (getppriv(type, buf));
case PRIVSYS_GETIMPLINFO:
return (getprivimplinfo(buf, bufsize));
case PRIVSYS_SETPFLAGS:
retv = setpflags((uint_t)op, (uint_t)type, NULL);
return (retv != 0 ? set_errno(retv) : 0);
case PRIVSYS_GETPFLAGS:
retv = (int)getpflags((uint_t)op, CRED());
return (retv == -1 ? set_errno(EINVAL) : retv);
case PRIVSYS_ISSETUGID:
return (issetugid());
case PRIVSYS_KLPD_REG:
if (bufsize < sizeof (priv_set_t))
return (set_errno(ENOMEM));
return ((int)klpd_reg((int)op, (idtype_t)itype, (id_t)type,
buf));
case PRIVSYS_KLPD_UNREG:
return ((int)klpd_unreg((int)op, (idtype_t)itype, (id_t)type));
case PRIVSYS_PFEXEC_REG:
return ((int)pfexec_reg((int)op));
case PRIVSYS_PFEXEC_UNREG:
return ((int)pfexec_unreg((int)op));
}
return (set_errno(EINVAL));
}
#ifdef _SYSCALL32_IMPL
int
privsys32(int code, priv_op_t op, priv_ptype_t type, caddr32_t buf,
size32_t bufsize, int itype)
{
return (privsys(code, op, type, (void *)(uintptr_t)buf,
(size_t)bufsize, itype));
}
#endif
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/var.h>
#include <sys/thread.h>
#include <sys/cpuvar.h>
#include <sys/kstat.h>
#include <sys/uadmin.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/cmn_err.h>
#include <sys/procset.h>
#include <sys/processor.h>
#include <sys/debug.h>
#include <sys/task.h>
#include <sys/project.h>
#include <sys/zone.h>
#include <sys/contract_impl.h>
#include <sys/contract/process_impl.h>
/*
* Bind all the threads of a process to a CPU.
*/
static int
cpu_bind_process(proc_t *pp, processorid_t bind, processorid_t *obind,
int *error)
{
kthread_t *tp;
kthread_t *fp;
int err = 0;
int i;
ASSERT(MUTEX_HELD(&pidlock));
/* skip kernel processes */
if (pp->p_flag & SSYS) {
*obind = PBIND_NONE;
*error = ENOTSUP;
return (0);
}
mutex_enter(&pp->p_lock);
tp = pp->p_tlist;
if (tp != NULL) {
fp = tp;
do {
i = cpu_bind_thread(tp, bind, obind, error);
if (err == 0)
err = i;
} while ((tp = tp->t_forw) != fp);
}
mutex_exit(&pp->p_lock);
return (err);
}
/*
* Bind all the processes of a task to a CPU.
*/
static int
cpu_bind_task(task_t *tk, processorid_t bind, processorid_t *obind,
int *error)
{
proc_t *p;
int err = 0;
int i;
ASSERT(MUTEX_HELD(&pidlock));
if ((p = tk->tk_memb_list) == NULL)
return (ESRCH);
do {
if (!(p->p_flag & SSYS)) {
i = cpu_bind_process(p, bind, obind, error);
if (err == 0)
err = i;
}
} while ((p = p->p_tasknext) != tk->tk_memb_list);
return (err);
}
/*
* Bind all the processes in a project to a CPU.
*/
static int
cpu_bind_project(kproject_t *kpj, processorid_t bind, processorid_t *obind,
int *error)
{
proc_t *p;
int err = 0;
int i;
ASSERT(MUTEX_HELD(&pidlock));
for (p = practive; p != NULL; p = p->p_next) {
if (p->p_tlist == NULL)
continue;
if (p->p_task->tk_proj == kpj && !(p->p_flag & SSYS)) {
i = cpu_bind_process(p, bind, obind, error);
if (err == 0)
err = i;
}
}
return (err);
}
/*
* Bind all the processes in a zone to a CPU.
*/
int
cpu_bind_zone(zone_t *zptr, processorid_t bind, processorid_t *obind,
int *error)
{
proc_t *p;
int err = 0;
int i;
ASSERT(MUTEX_HELD(&pidlock));
for (p = practive; p != NULL; p = p->p_next) {
if (p->p_tlist == NULL)
continue;
if (p->p_zone == zptr && !(p->p_flag & SSYS)) {
i = cpu_bind_process(p, bind, obind, error);
if (err == 0)
err = i;
}
}
return (err);
}
/*
* Bind all the processes in a process contract to a CPU.
*/
int
cpu_bind_contract(cont_process_t *ctp, processorid_t bind, processorid_t *obind,
int *error)
{
proc_t *p;
int err = 0;
int i;
ASSERT(MUTEX_HELD(&pidlock));
for (p = practive; p != NULL; p = p->p_next) {
if (p->p_tlist == NULL)
continue;
if (p->p_ct_process == ctp) {
i = cpu_bind_process(p, bind, obind, error);
if (err == 0)
err = i;
}
}
return (err);
}
/*
* processor_bind(2) - Processor binding interfaces.
*/
int
processor_bind(idtype_t idtype, id_t id, processorid_t bind,
processorid_t *obindp)
{
processorid_t obind = PBIND_NONE;
int ret = 0;
int err = 0;
cpu_t *cp;
kthread_id_t tp;
proc_t *pp;
task_t *tk;
kproject_t *kpj;
zone_t *zptr;
contract_t *ct;
/*
* Since we might be making a binding to a processor, hold the
* cpu_lock so that the processor cannot be taken offline while
* we do this.
*/
mutex_enter(&cpu_lock);
/*
* Check to be sure binding processor ID is valid.
*/
switch (bind) {
default:
if ((cp = cpu_get(bind)) == NULL ||
(cp->cpu_flags & (CPU_QUIESCED | CPU_OFFLINE)))
ret = EINVAL;
else if ((cp->cpu_flags & CPU_READY) == 0)
ret = EIO;
break;
case PBIND_NONE:
case PBIND_QUERY:
case PBIND_HARD:
case PBIND_SOFT:
case PBIND_QUERY_TYPE:
break;
}
if (ret) {
mutex_exit(&cpu_lock);
return (set_errno(ret));
}
switch (idtype) {
case P_LWPID:
pp = curproc;
mutex_enter(&pp->p_lock);
if (id == P_MYID) {
ret = cpu_bind_thread(curthread, bind, &obind, &err);
} else {
int found = 0;
tp = pp->p_tlist;
do {
if (tp->t_tid == id) {
ret = cpu_bind_thread(tp,
bind, &obind, &err);
found = 1;
break;
}
} while ((tp = tp->t_forw) != pp->p_tlist);
if (!found)
ret = ESRCH;
}
mutex_exit(&pp->p_lock);
break;
case P_PID:
/*
* Note. Cannot use dotoprocs here because it doesn't find
* system class processes, which are legal to query.
*/
mutex_enter(&pidlock);
if (id == P_MYID) {
ret = cpu_bind_process(curproc, bind, &obind, &err);
} else if ((pp = prfind(id)) != NULL) {
ret = cpu_bind_process(pp, bind, &obind, &err);
} else {
ret = ESRCH;
}
mutex_exit(&pidlock);
break;
case P_TASKID:
mutex_enter(&pidlock);
if (id == P_MYID) {
proc_t *p = curproc;
id = p->p_task->tk_tkid;
}
if ((tk = task_hold_by_id(id)) != NULL) {
ret = cpu_bind_task(tk, bind, &obind, &err);
mutex_exit(&pidlock);
task_rele(tk);
} else {
mutex_exit(&pidlock);
ret = ESRCH;
}
break;
case P_PROJID:
pp = curproc;
if (id == P_MYID)
id = curprojid();
if ((kpj = project_hold_by_id(id, pp->p_zone,
PROJECT_HOLD_FIND)) == NULL) {
ret = ESRCH;
} else {
mutex_enter(&pidlock);
ret = cpu_bind_project(kpj, bind, &obind, &err);
mutex_exit(&pidlock);
project_rele(kpj);
}
break;
case P_ZONEID:
if (id == P_MYID)
id = getzoneid();
if ((zptr = zone_find_by_id(id)) == NULL) {
ret = ESRCH;
} else {
mutex_enter(&pidlock);
ret = cpu_bind_zone(zptr, bind, &obind, &err);
mutex_exit(&pidlock);
zone_rele(zptr);
}
break;
case P_CTID:
if (id == P_MYID)
id = PRCTID(curproc);
if ((ct = contract_type_ptr(process_type, id,
curproc->p_zone->zone_uniqid)) == NULL) {
ret = ESRCH;
} else {
mutex_enter(&pidlock);
ret = cpu_bind_contract(ct->ct_data,
bind, &obind, &err);
mutex_exit(&pidlock);
contract_rele(ct);
}
break;
case P_CPUID:
if (id == P_MYID || bind != PBIND_NONE || cpu_get(id) == NULL)
ret = EINVAL;
else
ret = cpu_unbind(id, B_TRUE);
break;
case P_ALL:
if (id == P_MYID || bind != PBIND_NONE) {
ret = EINVAL;
} else {
int i;
cpu_t *cp = cpu_list;
do {
if ((cp->cpu_flags & CPU_EXISTS) == 0)
continue;
i = cpu_unbind(cp->cpu_id, B_TRUE);
if (ret == 0)
ret = i;
} while ((cp = cp->cpu_next) != cpu_list);
}
break;
default:
/*
* Spec says this is invalid, even though we could
* handle other idtypes.
*/
ret = EINVAL;
break;
}
mutex_exit(&cpu_lock);
/*
* If no search error occurred, see if any permissions errors did.
*/
if (ret == 0)
ret = err;
if (ret == 0 && obindp != NULL)
if (copyout((caddr_t)&obind, (caddr_t)obindp,
sizeof (obind)) == -1)
ret = EFAULT;
return (ret ? set_errno(ret) : 0); /* return success or failure */
}
/*
* 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 (c) 1992, 1994, 1998 by Sun Microsystems, Inc.
* All rights reserved.
*/
#ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/var.h>
#include <sys/thread.h>
#include <sys/cpuvar.h>
#include <sys/kstat.h>
#include <sys/uadmin.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/cmn_err.h>
#include <sys/procset.h>
#include <sys/processor.h>
#include <sys/debug.h>
/*
* processor_info(2) - return information on a processor.
*/
int
processor_info(processorid_t cpun, processor_info_t *infop)
{
cpu_t *cp;
processor_info_t temp;
mutex_enter(&cpu_lock);
if ((cp = cpu_get(cpun)) == NULL) {
mutex_exit(&cpu_lock);
return (set_errno(EINVAL));
}
bcopy(&cp->cpu_type_info, &temp, sizeof (temp));
mutex_exit(&cpu_lock);
/*
* The spec indicates that the rest of the information is meaningless
* if the CPU is offline, but if presented by the machine-dependent
* layer, it is probably still accurate. It seems OK to copy it all in
* either case.
*/
if (copyout((caddr_t)&temp, (caddr_t)infop,
sizeof (processor_info_t)))
return (set_errno(EFAULT));
return (0);
}
/*
* 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 (c) 1998, Sun Microsystems, Inc.
* All rights reserved.
* Copyright 2012 Milan Jurik. All rights reserved.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/debug.h>
/*
* Profiling.
*/
int
profil(unsigned short *bufbase, size_t bufsize, ulong_t pcoffset,
uint_t pcscale)
{
struct proc *p = ttoproc(curthread);
if (pcscale == 1)
pcscale = 0;
mutex_enter(&p->p_pflock);
p->p_prof.pr_base = bufbase;
p->p_prof.pr_size = bufsize;
p->p_prof.pr_off = pcoffset;
p->p_prof.pr_scale = pcscale;
/* pcsample and profil are mutually exclusive */
p->p_prof.pr_samples = 0;
mutex_exit(&p->p_pflock);
mutex_enter(&p->p_lock);
set_proc_post_sys(p); /* activate post_syscall profiling code */
mutex_exit(&p->p_lock);
return (0);
}
/*
* PC Sampling
*/
long
pcsample(void *buf, long nsamples)
{
struct proc *p = ttoproc(curthread);
long count = 0;
if (nsamples < 0 ||
((get_udatamodel() != DATAMODEL_NATIVE) && (nsamples > INT32_MAX)))
return (set_errno(EINVAL));
mutex_enter(&p->p_pflock);
p->p_prof.pr_base = buf;
p->p_prof.pr_size = nsamples;
p->p_prof.pr_scale = 1;
count = p->p_prof.pr_samples;
p->p_prof.pr_samples = 0;
mutex_exit(&p->p_pflock);
mutex_enter(&p->p_lock);
set_proc_post_sys(p); /* activate post_syscall profiling code */
mutex_exit(&p->p_lock);
return (count);
}
/*
* 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 2015, Richard Lowe. */
#include <sys/ddi.h>
#include <sys/errno.h>
#include <sys/policy.h>
#include <sys/proc.h>
#include <sys/procset.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <c2/audit.h>
struct psdargs {
psecflagwhich_t which;
const secflagdelta_t *delta;
};
void
secflags_apply_delta(secflagset_t *set, const secflagdelta_t *delta)
{
if (delta->psd_ass_active) {
secflags_copy(set, &delta->psd_assign);
} else {
if (!secflags_isempty(delta->psd_add)) {
secflags_union(set, &delta->psd_add);
}
if (!secflags_isempty(delta->psd_rem)) {
secflags_difference(set, &delta->psd_rem);
}
}
}
static int
psecdo(proc_t *p, struct psdargs *args)
{
secflagset_t *set;
int ret = 0;
mutex_enter(&p->p_lock);
if (secpolicy_psecflags(CRED(), p, curproc) != 0) {
ret = EPERM;
goto out;
}
if (!psecflags_validate_delta(&p->p_secflags, args->delta)) {
ret = EINVAL;
goto out;
}
if (AU_AUDITING())
audit_psecflags(p, args->which, args->delta);
switch (args->which) {
case PSF_INHERIT:
set = &p->p_secflags.psf_inherit;
break;
case PSF_LOWER:
set = &p->p_secflags.psf_lower;
break;
case PSF_UPPER:
set = &p->p_secflags.psf_upper;
break;
default:
ASSERT(0);
goto out;
}
secflags_apply_delta(set, args->delta);
/*
* Add any flag now in the lower that is not in the inheritable.
*/
secflags_union(&p->p_secflags.psf_inherit, &p->p_secflags.psf_lower);
out:
mutex_exit(&p->p_lock);
return (ret);
}
int
psecflags(procset_t *psp, psecflagwhich_t which, secflagdelta_t *ap)
{
procset_t procset;
secflagdelta_t args;
int rv = 0;
struct psdargs psd = {
.which = which,
};
/* Can never change the effective flags */
if (psd.which == PSF_EFFECTIVE)
return (EINVAL);
if (copyin(psp, &procset, sizeof (procset)) != 0)
return (set_errno(EFAULT));
if (copyin(ap, &args, sizeof (secflagdelta_t)) != 0)
return (set_errno(EFAULT));
psd.delta = &args;
/* secflags are per-process, procset must be in terms of processes */
if ((procset.p_lidtype == P_LWPID) ||
(procset.p_ridtype == P_LWPID))
return (set_errno(EINVAL));
rv = dotoprocs(&procset, psecdo, (caddr_t)&psd);
return (rv ? set_errno(rv) : 0);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/cmn_err.h>
#include <sys/cpuvar.h>
#include <sys/thread.h>
#include <sys/disp.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <sys/sysmacros.h>
#include <sys/cpupart.h>
#include <sys/pset.h>
#include <sys/modctl.h>
#include <sys/syscall.h>
#include <sys/task.h>
#include <sys/loadavg.h>
#include <sys/fss.h>
#include <sys/pool.h>
#include <sys/pool_pset.h>
#include <sys/policy.h>
#include <sys/zone.h>
#include <sys/contract/process_impl.h>
static int pset(int, long, long, long, long);
static struct sysent pset_sysent = {
5,
SE_ARGC | SE_NOUNLOAD,
(int (*)())pset,
};
static struct modlsys modlsys = {
&mod_syscallops, "processor sets", &pset_sysent
};
#ifdef _SYSCALL32_IMPL
static struct modlsys modlsys32 = {
&mod_syscallops32, "32-bit pset(2) syscall", &pset_sysent
};
#endif
static struct modlinkage modlinkage = {
MODREV_1,
&modlsys,
#ifdef _SYSCALL32_IMPL
&modlsys32,
#endif
NULL
};
#define PSET_BADATTR(attr) ((~PSET_NOESCAPE) & (attr))
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static int
pset_create(psetid_t *psetp)
{
psetid_t newpset;
int error;
if (secpolicy_pset(CRED()) != 0)
return (set_errno(EPERM));
pool_lock();
if (pool_state == POOL_ENABLED) {
pool_unlock();
return (set_errno(ENOTSUP));
}
error = cpupart_create(&newpset);
if (error) {
pool_unlock();
return (set_errno(error));
}
if (copyout(&newpset, psetp, sizeof (psetid_t)) != 0) {
(void) cpupart_destroy(newpset);
pool_unlock();
return (set_errno(EFAULT));
}
pool_unlock();
return (error);
}
static int
pset_destroy(psetid_t pset)
{
int error;
if (secpolicy_pset(CRED()) != 0)
return (set_errno(EPERM));
pool_lock();
if (pool_state == POOL_ENABLED) {
pool_unlock();
return (set_errno(ENOTSUP));
}
error = cpupart_destroy(pset);
pool_unlock();
if (error)
return (set_errno(error));
else
return (0);
}
static int
pset_assign(psetid_t pset, processorid_t cpuid, psetid_t *opset, int forced)
{
psetid_t oldpset;
int error = 0;
cpu_t *cp;
if (pset != PS_QUERY && secpolicy_pset(CRED()) != 0)
return (set_errno(EPERM));
pool_lock();
if (pset != PS_QUERY && pool_state == POOL_ENABLED) {
pool_unlock();
return (set_errno(ENOTSUP));
}
mutex_enter(&cpu_lock);
if ((cp = cpu_get(cpuid)) == NULL) {
mutex_exit(&cpu_lock);
pool_unlock();
return (set_errno(EINVAL));
}
oldpset = cpupart_query_cpu(cp);
if (pset != PS_QUERY)
error = cpupart_attach_cpu(pset, cp, forced);
mutex_exit(&cpu_lock);
pool_unlock();
if (error)
return (set_errno(error));
if (opset != NULL)
if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
return (set_errno(EFAULT));
return (0);
}
static int
pset_info(psetid_t pset, int *typep, uint_t *numcpusp,
processorid_t *cpulistp)
{
int pset_type;
uint_t user_ncpus = 0, real_ncpus, copy_ncpus;
processorid_t *pset_cpus = NULL;
int error = 0;
if (numcpusp != NULL) {
if (copyin(numcpusp, &user_ncpus, sizeof (uint_t)) != 0)
return (set_errno(EFAULT));
}
if (user_ncpus > max_ncpus) /* sanity check */
user_ncpus = max_ncpus;
if (user_ncpus != 0 && cpulistp != NULL)
pset_cpus = kmem_alloc(sizeof (processorid_t) * user_ncpus,
KM_SLEEP);
real_ncpus = user_ncpus;
if ((error = cpupart_get_cpus(&pset, pset_cpus, &real_ncpus)) != 0)
goto out;
/*
* Now copyout the information about this processor set.
*/
/*
* Get number of cpus to copy back. If the user didn't pass in
* a big enough buffer, only copy back as many cpus as fits in
* the buffer but copy back the real number of cpus.
*/
if (user_ncpus != 0 && cpulistp != NULL) {
copy_ncpus = MIN(real_ncpus, user_ncpus);
if (copyout(pset_cpus, cpulistp,
sizeof (processorid_t) * copy_ncpus) != 0) {
error = EFAULT;
goto out;
}
}
if (pset_cpus != NULL)
kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus);
if (typep != NULL) {
if (pset == PS_NONE)
pset_type = PS_NONE;
else
pset_type = PS_PRIVATE;
if (copyout(&pset_type, typep, sizeof (int)) != 0)
return (set_errno(EFAULT));
}
if (numcpusp != NULL)
if (copyout(&real_ncpus, numcpusp, sizeof (uint_t)) != 0)
return (set_errno(EFAULT));
return (0);
out:
if (pset_cpus != NULL)
kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus);
return (set_errno(error));
}
static int
pset_bind_thread(kthread_t *tp, psetid_t pset, psetid_t *oldpset, void *projbuf,
void *zonebuf)
{
int error = 0;
ASSERT(pool_lock_held());
ASSERT(MUTEX_HELD(&cpu_lock));
ASSERT(MUTEX_HELD(&ttoproc(tp)->p_lock));
*oldpset = tp->t_bind_pset;
switch (pset) {
case PS_SOFT:
TB_PSET_SOFT_SET(tp);
break;
case PS_HARD:
TB_PSET_HARD_SET(tp);
break;
case PS_QUERY:
break;
case PS_QUERY_TYPE:
*oldpset = TB_PSET_IS_SOFT(tp) ? PS_SOFT : PS_HARD;
break;
default:
/*
* Must have the same UID as the target process or
* have PRIV_PROC_OWNER privilege.
*/
if (!hasprocperm(tp->t_cred, CRED()))
return (EPERM);
/*
* Unbinding of an unbound thread should always succeed.
*/
if (*oldpset == PS_NONE && pset == PS_NONE)
return (0);
/*
* Only privileged processes can move threads from psets with
* PSET_NOESCAPE attribute.
*/
if ((tp->t_cpupart->cp_attr & PSET_NOESCAPE) &&
secpolicy_pbind(CRED()) != 0)
return (EPERM);
if ((error = cpupart_bind_thread(tp, pset, 0,
projbuf, zonebuf)) == 0)
tp->t_bind_pset = pset;
break;
}
return (error);
}
static int
pset_bind_process(proc_t *pp, psetid_t pset, psetid_t *oldpset, void *projbuf,
void *zonebuf)
{
int error = 0;
kthread_t *tp;
/* skip kernel processes */
if ((pset != PS_QUERY) && pp->p_flag & SSYS) {
*oldpset = PS_NONE;
return (ENOTSUP);
}
mutex_enter(&pp->p_lock);
tp = pp->p_tlist;
if (tp != NULL) {
do {
int rval;
rval = pset_bind_thread(tp, pset, oldpset, projbuf,
zonebuf);
if (error == 0)
error = rval;
} while ((tp = tp->t_forw) != pp->p_tlist);
} else
error = ESRCH;
mutex_exit(&pp->p_lock);
return (error);
}
static int
pset_bind_task(task_t *tk, psetid_t pset, psetid_t *oldpset, void *projbuf,
void *zonebuf)
{
int error = 0;
proc_t *pp;
ASSERT(MUTEX_HELD(&pidlock));
if ((pp = tk->tk_memb_list) == NULL) {
return (ESRCH);
}
do {
int rval;
if (!(pp->p_flag & SSYS)) {
rval = pset_bind_process(pp, pset, oldpset, projbuf,
zonebuf);
if (error == 0)
error = rval;
}
} while ((pp = pp->p_tasknext) != tk->tk_memb_list);
return (error);
}
static int
pset_bind_project(kproject_t *kpj, psetid_t pset, psetid_t *oldpset,
void *projbuf, void *zonebuf)
{
int error = 0;
proc_t *pp;
ASSERT(MUTEX_HELD(&pidlock));
for (pp = practive; pp != NULL; pp = pp->p_next) {
if (pp->p_tlist == NULL)
continue;
if (pp->p_task->tk_proj == kpj && !(pp->p_flag & SSYS)) {
int rval;
rval = pset_bind_process(pp, pset, oldpset, projbuf,
zonebuf);
if (error == 0)
error = rval;
}
}
return (error);
}
static int
pset_bind_zone(zone_t *zptr, psetid_t pset, psetid_t *oldpset, void *projbuf,
void *zonebuf)
{
int error = 0;
proc_t *pp;
ASSERT(MUTEX_HELD(&pidlock));
for (pp = practive; pp != NULL; pp = pp->p_next) {
if (pp->p_zone == zptr && !(pp->p_flag & SSYS)) {
int rval;
rval = pset_bind_process(pp, pset, oldpset, projbuf,
zonebuf);
if (error == 0)
error = rval;
}
}
return (error);
}
/*
* Unbind all threads from the specified processor set, or from all
* processor sets.
*/
static int
pset_unbind(psetid_t pset, void *projbuf, void *zonebuf, idtype_t idtype)
{
psetid_t olbind;
kthread_t *tp;
int error = 0;
int rval;
proc_t *pp;
ASSERT(MUTEX_HELD(&cpu_lock));
if (idtype == P_PSETID && cpupart_find(pset) == NULL)
return (EINVAL);
mutex_enter(&pidlock);
for (pp = practive; pp != NULL; pp = pp->p_next) {
mutex_enter(&pp->p_lock);
tp = pp->p_tlist;
/*
* Skip zombies and kernel processes, and processes in
* other zones, if called from a non-global zone.
*/
if (tp == NULL || (pp->p_flag & SSYS) ||
!HASZONEACCESS(curproc, pp->p_zone->zone_id)) {
mutex_exit(&pp->p_lock);
continue;
}
do {
if ((idtype == P_PSETID && tp->t_bind_pset != pset) ||
(idtype == P_ALL && tp->t_bind_pset == PS_NONE))
continue;
rval = pset_bind_thread(tp, PS_NONE, &olbind,
projbuf, zonebuf);
if (error == 0)
error = rval;
} while ((tp = tp->t_forw) != pp->p_tlist);
mutex_exit(&pp->p_lock);
}
mutex_exit(&pidlock);
return (error);
}
static int
pset_bind_contract(cont_process_t *ctp, psetid_t pset, psetid_t *oldpset,
void *projbuf, void *zonebuf)
{
int error = 0;
proc_t *pp;
ASSERT(MUTEX_HELD(&pidlock));
for (pp = practive; pp != NULL; pp = pp->p_next) {
if (pp->p_ct_process == ctp) {
int rval;
rval = pset_bind_process(pp, pset, oldpset, projbuf,
zonebuf);
if (error == 0)
error = rval;
}
}
return (error);
}
/*
* Bind the lwp:id of process:pid to processor set: pset
*/
static int
pset_bind_lwp(psetid_t pset, id_t id, pid_t pid, psetid_t *opset)
{
kthread_t *tp;
proc_t *pp;
psetid_t oldpset;
void *projbuf, *zonebuf;
int error = 0;
pool_lock();
mutex_enter(&cpu_lock);
projbuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_PROJ);
zonebuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_ZONE);
mutex_enter(&pidlock);
if ((pid == P_MYID && id == P_MYID) ||
(pid == curproc->p_pid && id == P_MYID)) {
pp = curproc;
tp = curthread;
mutex_enter(&pp->p_lock);
} else {
if (pid == P_MYID) {
pp = curproc;
} else if ((pp = prfind(pid)) == NULL) {
error = ESRCH;
goto err;
}
if (pp != curproc && id == P_MYID) {
error = EINVAL;
goto err;
}
mutex_enter(&pp->p_lock);
if ((tp = idtot(pp, id)) == NULL) {
mutex_exit(&pp->p_lock);
error = ESRCH;
goto err;
}
}
error = pset_bind_thread(tp, pset, &oldpset, projbuf, zonebuf);
mutex_exit(&pp->p_lock);
err:
mutex_exit(&pidlock);
fss_freebuf(projbuf, FSS_ALLOC_PROJ);
fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
mutex_exit(&cpu_lock);
pool_unlock();
if (opset != NULL) {
if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
return (set_errno(EFAULT));
}
if (error != 0)
return (set_errno(error));
return (0);
}
static int
pset_bind(psetid_t pset, idtype_t idtype, id_t id, psetid_t *opset)
{
kthread_t *tp;
proc_t *pp;
task_t *tk;
kproject_t *kpj;
contract_t *ct;
zone_t *zptr;
psetid_t oldpset;
int error = 0;
void *projbuf, *zonebuf;
pool_lock();
if ((pset != PS_QUERY) && (pset != PS_SOFT) &&
(pset != PS_HARD) && (pset != PS_QUERY_TYPE)) {
/*
* Check if the set actually exists before checking
* permissions. This is the historical error
* precedence. Note that if pset was PS_MYID, the
* cpupart_get_cpus call will change it to the
* processor set id of the caller (or PS_NONE if the
* caller is not bound to a processor set).
*/
if (pool_state == POOL_ENABLED) {
pool_unlock();
return (set_errno(ENOTSUP));
}
if (cpupart_get_cpus(&pset, NULL, NULL) != 0) {
pool_unlock();
return (set_errno(EINVAL));
} else if (pset != PS_NONE && secpolicy_pbind(CRED()) != 0) {
pool_unlock();
return (set_errno(EPERM));
}
}
/*
* Pre-allocate enough buffers for FSS for all active projects
* and for all active zones on the system. Unused buffers will
* be freed later by fss_freebuf().
*/
mutex_enter(&cpu_lock);
projbuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_PROJ);
zonebuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_ZONE);
switch (idtype) {
case P_LWPID:
pp = curproc;
mutex_enter(&pidlock);
mutex_enter(&pp->p_lock);
if (id == P_MYID) {
tp = curthread;
} else {
if ((tp = idtot(pp, id)) == NULL) {
mutex_exit(&pp->p_lock);
mutex_exit(&pidlock);
error = ESRCH;
break;
}
}
error = pset_bind_thread(tp, pset, &oldpset, projbuf, zonebuf);
mutex_exit(&pp->p_lock);
mutex_exit(&pidlock);
break;
case P_PID:
mutex_enter(&pidlock);
if (id == P_MYID) {
pp = curproc;
} else if ((pp = prfind(id)) == NULL) {
mutex_exit(&pidlock);
error = ESRCH;
break;
}
error = pset_bind_process(pp, pset, &oldpset, projbuf, zonebuf);
mutex_exit(&pidlock);
break;
case P_TASKID:
mutex_enter(&pidlock);
if (id == P_MYID)
id = curproc->p_task->tk_tkid;
if ((tk = task_hold_by_id(id)) == NULL) {
mutex_exit(&pidlock);
error = ESRCH;
break;
}
error = pset_bind_task(tk, pset, &oldpset, projbuf, zonebuf);
mutex_exit(&pidlock);
task_rele(tk);
break;
case P_PROJID:
pp = curproc;
if (id == P_MYID)
id = curprojid();
if ((kpj = project_hold_by_id(id, pp->p_zone,
PROJECT_HOLD_FIND)) == NULL) {
error = ESRCH;
break;
}
mutex_enter(&pidlock);
error = pset_bind_project(kpj, pset, &oldpset, projbuf,
zonebuf);
mutex_exit(&pidlock);
project_rele(kpj);
break;
case P_ZONEID:
if (id == P_MYID)
id = getzoneid();
if ((zptr = zone_find_by_id(id)) == NULL) {
error = ESRCH;
break;
}
mutex_enter(&pidlock);
error = pset_bind_zone(zptr, pset, &oldpset, projbuf, zonebuf);
mutex_exit(&pidlock);
zone_rele(zptr);
break;
case P_CTID:
if (id == P_MYID)
id = PRCTID(curproc);
if ((ct = contract_type_ptr(process_type, id,
curproc->p_zone->zone_uniqid)) == NULL) {
error = ESRCH;
break;
}
mutex_enter(&pidlock);
error = pset_bind_contract(ct->ct_data, pset, &oldpset, projbuf,
zonebuf);
mutex_exit(&pidlock);
contract_rele(ct);
break;
case P_PSETID:
if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
error = EINVAL;
break;
}
error = pset_unbind(id, projbuf, zonebuf, idtype);
break;
case P_ALL:
if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
error = EINVAL;
break;
}
error = pset_unbind(PS_NONE, projbuf, zonebuf, idtype);
break;
default:
error = EINVAL;
break;
}
fss_freebuf(projbuf, FSS_ALLOC_PROJ);
fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
mutex_exit(&cpu_lock);
pool_unlock();
if (error != 0)
return (set_errno(error));
if (opset != NULL) {
if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
return (set_errno(EFAULT));
}
return (0);
}
/*
* Report load average statistics for the specified processor set.
*/
static int
pset_getloadavg(psetid_t pset, int *buf, int nelem)
{
int loadbuf[LOADAVG_NSTATS];
int error = 0;
if (nelem < 0)
return (set_errno(EINVAL));
/*
* We keep the same number of load average statistics for processor
* sets as we do for the system as a whole.
*/
if (nelem > LOADAVG_NSTATS)
nelem = LOADAVG_NSTATS;
mutex_enter(&cpu_lock);
error = cpupart_get_loadavg(pset, loadbuf, nelem);
mutex_exit(&cpu_lock);
if (!error && nelem && copyout(loadbuf, buf, nelem * sizeof (int)) != 0)
error = EFAULT;
if (error)
return (set_errno(error));
else
return (0);
}
/*
* Return list of active processor sets, up to a maximum indicated by
* numpsets. The total number of processor sets is stored in the
* location pointed to by numpsets.
*/
static int
pset_list(psetid_t *psetlist, uint_t *numpsets)
{
uint_t user_npsets = 0;
uint_t real_npsets;
psetid_t *psets = NULL;
int error = 0;
if (numpsets != NULL) {
if (copyin(numpsets, &user_npsets, sizeof (uint_t)) != 0)
return (set_errno(EFAULT));
}
/*
* Get the list of all processor sets. First we need to find
* out how many there are, so we can allocate a large enough
* buffer.
*/
mutex_enter(&cpu_lock);
if (!INGLOBALZONE(curproc) && pool_pset_enabled()) {
psetid_t psetid = zone_pset_get(curproc->p_zone);
if (psetid == PS_NONE) {
real_npsets = 0;
} else {
real_npsets = 1;
psets = kmem_alloc(real_npsets * sizeof (psetid_t),
KM_SLEEP);
psets[0] = psetid;
}
} else {
real_npsets = cpupart_list(NULL, 0, CP_ALL);
if (real_npsets) {
psets = kmem_alloc(real_npsets * sizeof (psetid_t),
KM_SLEEP);
(void) cpupart_list(psets, real_npsets, CP_ALL);
}
}
mutex_exit(&cpu_lock);
if (user_npsets > real_npsets)
user_npsets = real_npsets;
if (numpsets != NULL) {
if (copyout(&real_npsets, numpsets, sizeof (uint_t)) != 0)
error = EFAULT;
else if (psetlist != NULL && user_npsets != 0) {
if (copyout(psets, psetlist,
user_npsets * sizeof (psetid_t)) != 0)
error = EFAULT;
}
}
if (psets != NULL)
kmem_free(psets, real_npsets * sizeof (psetid_t));
if (error)
return (set_errno(error));
else
return (0);
}
static int
pset_setattr(psetid_t pset, uint_t attr)
{
int error;
if (secpolicy_pset(CRED()) != 0)
return (set_errno(EPERM));
pool_lock();
if (pool_state == POOL_ENABLED) {
pool_unlock();
return (set_errno(ENOTSUP));
}
if (pset == PS_QUERY || PSET_BADATTR(attr)) {
pool_unlock();
return (set_errno(EINVAL));
}
if ((error = cpupart_setattr(pset, attr)) != 0) {
pool_unlock();
return (set_errno(error));
}
pool_unlock();
return (0);
}
static int
pset_getattr(psetid_t pset, uint_t *attrp)
{
int error = 0;
uint_t attr;
if (pset == PS_QUERY)
return (set_errno(EINVAL));
if ((error = cpupart_getattr(pset, &attr)) != 0)
return (set_errno(error));
if (copyout(&attr, attrp, sizeof (uint_t)) != 0)
return (set_errno(EFAULT));
return (0);
}
static int
pset(int subcode, long arg1, long arg2, long arg3, long arg4)
{
switch (subcode) {
case PSET_CREATE:
return (pset_create((psetid_t *)arg1));
case PSET_DESTROY:
return (pset_destroy((psetid_t)arg1));
case PSET_ASSIGN:
return (pset_assign((psetid_t)arg1,
(processorid_t)arg2, (psetid_t *)arg3, 0));
case PSET_INFO:
return (pset_info((psetid_t)arg1, (int *)arg2,
(uint_t *)arg3, (processorid_t *)arg4));
case PSET_BIND:
return (pset_bind((psetid_t)arg1, (idtype_t)arg2,
(id_t)arg3, (psetid_t *)arg4));
case PSET_BIND_LWP:
return (pset_bind_lwp((psetid_t)arg1, (id_t)arg2,
(pid_t)arg3, (psetid_t *)arg4));
case PSET_GETLOADAVG:
return (pset_getloadavg((psetid_t)arg1, (int *)arg2,
(int)arg3));
case PSET_LIST:
return (pset_list((psetid_t *)arg1, (uint_t *)arg2));
case PSET_SETATTR:
return (pset_setattr((psetid_t)arg1, (uint_t)arg2));
case PSET_GETATTR:
return (pset_getattr((psetid_t)arg1, (uint_t *)arg2));
case PSET_ASSIGN_FORCED:
return (pset_assign((psetid_t)arg1,
(processorid_t)arg2, (psetid_t *)arg3, 1));
default:
return (set_errno(EINVAL));
}
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/cmn_err.h>
#include <sys/cred.h>
#include <sys/errno.h>
#include <sys/rctl.h>
#include <sys/rctl_impl.h>
#include <sys/strlog.h>
#include <sys/syslog.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/policy.h>
#include <sys/proc.h>
#include <sys/task.h>
/*
* setrctl(2), getrctl(2), and private rctlsys(2*) system calls
*
* Resource control block (rctlblk_ptr_t, rctl_opaque_t)
* The resource control system call interfaces present the resource control
* values and flags via the resource control block abstraction, made manifest
* via an opaque data type with strict type definitions. Keeping the formal
* definitions in the rcontrol block allows us to be clever in the kernel,
* combining attributes where appropriate in the current implementation while
* preserving binary compatibility in the face of implementation changes.
*/
#define RBX_TO_BLK 0x1
#define RBX_FROM_BLK 0x2
#define RBX_VAL 0x4
#define RBX_CTL 0x8
static void
rctlsys_rblk_xfrm(rctl_opaque_t *blk, rctl_dict_entry_t *rde,
rctl_val_t *val, int flags)
{
if (flags & RBX_FROM_BLK) {
if (flags & RBX_VAL) {
/*
* Firing time cannot be set.
*/
val->rcv_privilege = blk->rcq_privilege;
val->rcv_value = blk->rcq_value;
val->rcv_flagaction = blk->rcq_local_flagaction;
val->rcv_action_signal = blk->rcq_local_signal;
val->rcv_action_recip_pid =
blk->rcq_local_recipient_pid;
}
if (flags & RBX_CTL) {
rde->rcd_flagaction = blk->rcq_global_flagaction;
rde->rcd_syslog_level = blk->rcq_global_syslog_level;
/*
* Because the strlog() interface supports fewer options
* than are made available via the syslog() interface to
* userland, we map the syslog level down to a smaller
* set of distinct logging behaviours.
*/
rde->rcd_strlog_flags = 0;
switch (blk->rcq_global_syslog_level) {
case LOG_EMERG:
case LOG_ALERT:
case LOG_CRIT:
rde->rcd_strlog_flags |= SL_CONSOLE;
/*FALLTHROUGH*/
case LOG_ERR:
rde->rcd_strlog_flags |= SL_ERROR;
/*FALLTHROUGH*/
case LOG_WARNING:
rde->rcd_strlog_flags |= SL_WARN;
break;
case LOG_NOTICE:
rde->rcd_strlog_flags |= SL_CONSOLE;
/*FALLTHROUGH*/
case LOG_INFO: /* informational */
case LOG_DEBUG: /* debug-level messages */
default:
rde->rcd_strlog_flags |= SL_NOTE;
break;
}
}
} else {
bzero(blk, sizeof (rctl_opaque_t));
if (flags & RBX_VAL) {
blk->rcq_privilege = val->rcv_privilege;
blk->rcq_value = val->rcv_value;
blk->rcq_enforced_value = rctl_model_value(rde,
curproc, val->rcv_value);
blk->rcq_local_flagaction = val->rcv_flagaction;
blk->rcq_local_signal = val->rcv_action_signal;
blk->rcq_firing_time = val->rcv_firing_time;
blk->rcq_local_recipient_pid =
val->rcv_action_recip_pid;
}
if (flags & RBX_CTL) {
blk->rcq_global_flagaction = rde->rcd_flagaction;
blk->rcq_global_syslog_level = rde->rcd_syslog_level;
}
}
}
/*
* int rctl_invalid_value(rctl_dict_entry_t *, rctl_val_t *)
*
* Overview
* Perform basic validation of proposed new resource control value against the
* global properties set on the control. Any system call operation presented
* with an invalid resource control value should return -1 and set errno to
* EINVAL.
*
* Return values
* 0 if valid, 1 if invalid.
*
* Caller's context
* No restriction on context.
*/
int
rctl_invalid_value(rctl_dict_entry_t *rde, rctl_val_t *rval)
{
rctl_val_t *sys_rval;
if (rval->rcv_privilege != RCPRIV_BASIC &&
rval->rcv_privilege != RCPRIV_PRIVILEGED &&
rval->rcv_privilege != RCPRIV_SYSTEM)
return (1);
if (rval->rcv_flagaction & ~RCTL_LOCAL_MASK)
return (1);
if (rval->rcv_privilege == RCPRIV_BASIC &&
(rde->rcd_flagaction & RCTL_GLOBAL_NOBASIC) != 0)
return (1);
if ((rval->rcv_flagaction & RCTL_LOCAL_DENY) == 0 &&
(rde->rcd_flagaction & RCTL_GLOBAL_DENY_ALWAYS) != 0)
return (1);
if ((rval->rcv_flagaction & RCTL_LOCAL_DENY) &&
(rde->rcd_flagaction & RCTL_GLOBAL_DENY_NEVER))
return (1);
if ((rval->rcv_flagaction & RCTL_LOCAL_SIGNAL) &&
(rde->rcd_flagaction & RCTL_GLOBAL_SIGNAL_NEVER))
return (1);
if ((rval->rcv_flagaction & RCTL_LOCAL_SIGNAL) &&
rval->rcv_action_signal == 0)
return (1);
if (rval->rcv_action_signal == SIGXCPU &&
(rde->rcd_flagaction & RCTL_GLOBAL_CPU_TIME) == 0)
return (1);
else if (rval->rcv_action_signal == SIGXFSZ &&
(rde->rcd_flagaction & RCTL_GLOBAL_FILE_SIZE) == 0)
return (1);
else if (rval->rcv_action_signal != SIGHUP &&
rval->rcv_action_signal != SIGABRT &&
rval->rcv_action_signal != SIGKILL &&
rval->rcv_action_signal != SIGTERM &&
rval->rcv_action_signal != SIGSTOP &&
rval->rcv_action_signal != SIGXCPU &&
rval->rcv_action_signal != SIGXFSZ &&
rval->rcv_action_signal != SIGXRES &&
rval->rcv_action_signal != 0) /* That is, no signal is ok. */
return (1);
sys_rval = rde->rcd_default_value;
while (sys_rval->rcv_privilege != RCPRIV_SYSTEM)
sys_rval = sys_rval->rcv_next;
if (rval->rcv_value > sys_rval->rcv_value)
return (1);
return (0);
}
/*
* static long rctlsys_get(char *name, rctl_opaque_t *old_rblk,
* rctl_opaque_t *new_rblk, int flags)
*
* Overview
* rctlsys_get() is the implementation of the core logic of getrctl(2), the
* public system call for fetching resource control values. Three mutually
* exclusive flag values are supported: RCTL_USAGE, RCTL_FIRST and RCTL_NEXT.
* When RCTL_USAGE is presented, the current usage for the resource control
* is returned in new_blk if the resource control provides an implementation
* of the usage operation. When RCTL_FIRST is presented, the value of
* old_rblk is ignored, and the first value in the resource control value
* sequence for the named control is transformed and placed in the user
* memory location at new_rblk. In the RCTL_NEXT case, the value of old_rblk
* is examined, and the next value in the sequence is transformed and placed
* at new_rblk.
*/
static long
rctlsys_get(char *name, rctl_opaque_t *old_rblk, rctl_opaque_t *new_rblk,
int flags)
{
rctl_val_t *nval;
rctl_opaque_t *nblk;
rctl_hndl_t hndl;
char *kname;
size_t klen;
rctl_dict_entry_t *krde;
int ret;
int action = flags & (~RCTLSYS_ACTION_MASK);
if (flags & (~RCTLSYS_MASK))
return (set_errno(EINVAL));
if (action != RCTL_FIRST && action != RCTL_NEXT &&
action != RCTL_USAGE)
return (set_errno(EINVAL));
if (new_rblk == NULL || name == NULL)
return (set_errno(EFAULT));
kname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
krde = kmem_alloc(sizeof (rctl_dict_entry_t), KM_SLEEP);
if (copyinstr(name, kname, MAXPATHLEN, &klen) != 0) {
kmem_free(kname, MAXPATHLEN);
kmem_free(krde, sizeof (rctl_dict_entry_t));
return (set_errno(EFAULT));
}
if ((hndl = rctl_hndl_lookup(kname)) == -1) {
kmem_free(kname, MAXPATHLEN);
kmem_free(krde, sizeof (rctl_dict_entry_t));
return (set_errno(EINVAL));
}
if (rctl_global_get(kname, krde) == -1) {
kmem_free(kname, MAXPATHLEN);
kmem_free(krde, sizeof (rctl_dict_entry_t));
return (set_errno(ESRCH));
}
kmem_free(kname, MAXPATHLEN);
if (action != RCTL_USAGE)
nval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
if (action == RCTL_USAGE) {
rctl_set_t *rset;
rctl_t *rctl;
rctl_qty_t usage;
mutex_enter(&curproc->p_lock);
if ((rset = rctl_entity_obtain_rset(krde, curproc)) == NULL) {
mutex_exit(&curproc->p_lock);
kmem_free(krde, sizeof (rctl_dict_entry_t));
return (set_errno(ESRCH));
}
mutex_enter(&rset->rcs_lock);
if (rctl_set_find(rset, hndl, &rctl) == -1) {
mutex_exit(&rset->rcs_lock);
mutex_exit(&curproc->p_lock);
kmem_free(krde, sizeof (rctl_dict_entry_t));
return (set_errno(ESRCH));
}
if (RCTLOP_NO_USAGE(rctl)) {
mutex_exit(&rset->rcs_lock);
mutex_exit(&curproc->p_lock);
kmem_free(krde, sizeof (rctl_dict_entry_t));
return (set_errno(ENOTSUP));
}
usage = RCTLOP_GET_USAGE(rctl, curproc);
mutex_exit(&rset->rcs_lock);
mutex_exit(&curproc->p_lock);
nblk = kmem_zalloc(sizeof (rctl_opaque_t), KM_SLEEP);
nblk->rcq_value = usage;
ret = copyout(nblk, new_rblk, sizeof (rctl_opaque_t));
kmem_free(nblk, sizeof (rctl_opaque_t));
kmem_free(krde, sizeof (rctl_dict_entry_t));
return (ret == 0 ? 0 : set_errno(EFAULT));
} else if (action == RCTL_FIRST) {
mutex_enter(&curproc->p_lock);
if (ret = rctl_local_get(hndl, NULL, nval, curproc)) {
mutex_exit(&curproc->p_lock);
kmem_cache_free(rctl_val_cache, nval);
kmem_free(krde, sizeof (rctl_dict_entry_t));
return (set_errno(ret));
}
mutex_exit(&curproc->p_lock);
} else {
/*
* RCTL_NEXT
*/
rctl_val_t *oval;
rctl_opaque_t *oblk;
oblk = kmem_alloc(sizeof (rctl_opaque_t), KM_SLEEP);
if (copyin(old_rblk, oblk, sizeof (rctl_opaque_t)) == -1) {
kmem_cache_free(rctl_val_cache, nval);
kmem_free(oblk, sizeof (rctl_opaque_t));
kmem_free(krde, sizeof (rctl_dict_entry_t));
return (set_errno(EFAULT));
}
oval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
rctlsys_rblk_xfrm(oblk, NULL, oval, RBX_FROM_BLK | RBX_VAL);
mutex_enter(&curproc->p_lock);
ret = rctl_local_get(hndl, oval, nval, curproc);
mutex_exit(&curproc->p_lock);
kmem_cache_free(rctl_val_cache, oval);
kmem_free(oblk, sizeof (rctl_opaque_t));
if (ret != 0) {
kmem_cache_free(rctl_val_cache, nval);
kmem_free(krde, sizeof (rctl_dict_entry_t));
return (set_errno(ret));
}
}
nblk = kmem_alloc(sizeof (rctl_opaque_t), KM_SLEEP);
rctlsys_rblk_xfrm(nblk, krde, nval, RBX_TO_BLK | RBX_VAL | RBX_CTL);
kmem_free(krde, sizeof (rctl_dict_entry_t));
kmem_cache_free(rctl_val_cache, nval);
if (copyout(nblk, new_rblk, sizeof (rctl_opaque_t)) == -1) {
kmem_free(nblk, sizeof (rctl_opaque_t));
return (set_errno(EFAULT));
}
kmem_free(nblk, sizeof (rctl_opaque_t));
return (0);
}
/*
* static long rctlsys_set(char *name, rctl_opaque_t *old_rblk,
* rctl_opaque_t *new_rblk, int flags)
*
* Overview
* rctlsys_set() is the implementation of the core login of setrctl(2), which
* allows the establishment of resource control values. Flags may take on any
* of three exclusive values: RCTL_INSERT, RCTL_DELETE, and RCTL_REPLACE.
* RCTL_INSERT ignores old_rblk and inserts the value in the appropriate
* position in the ordered sequence of resource control values. RCTL_DELETE
* ignores old_rblk and deletes the first resource control value matching
* (value, priority) in the given resource block. If no matching value is
* found, -1 is returned and errno is set to ENOENT. Finally, in the case of
* RCTL_REPLACE, old_rblk is used to match (value, priority); the matching
* resource control value in the sequence is replaced with the contents of
* new_rblk. Again, if no match is found, -1 is returned and errno is set to
* ENOENT.
*
* rctlsys_set() causes a cursor test, which can reactivate resource controls
* that have previously fired.
*/
static long
rctlsys_set(char *name, rctl_opaque_t *old_rblk, rctl_opaque_t *new_rblk,
int flags)
{
rctl_val_t *nval;
rctl_dict_entry_t *rde;
rctl_opaque_t *nblk;
rctl_hndl_t hndl;
char *kname;
size_t klen;
long ret = 0;
proc_t *pp = NULL;
pid_t pid;
int action = flags & (~RCTLSYS_ACTION_MASK);
rctl_val_t *oval;
rctl_val_t *rval1;
rctl_val_t *rval2;
rctl_val_t *tval;
rctl_opaque_t *oblk;
if (flags & (~RCTLSYS_MASK))
return (set_errno(EINVAL));
if (action != RCTL_INSERT &&
action != RCTL_DELETE &&
action != RCTL_REPLACE)
return (set_errno(EINVAL));
if (new_rblk == NULL || name == NULL)
return (set_errno(EFAULT));
kname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
if (copyinstr(name, kname, MAXPATHLEN, &klen) != 0) {
kmem_free(kname, MAXPATHLEN);
return (set_errno(EFAULT));
}
if ((hndl = rctl_hndl_lookup(kname)) == -1) {
kmem_free(kname, MAXPATHLEN);
return (set_errno(EINVAL));
}
kmem_free(kname, MAXPATHLEN);
rde = rctl_dict_lookup_hndl(hndl);
nblk = kmem_alloc(sizeof (rctl_opaque_t), KM_SLEEP);
if (copyin(new_rblk, nblk, sizeof (rctl_opaque_t)) == -1) {
kmem_free(nblk, sizeof (rctl_opaque_t));
return (set_errno(EFAULT));
}
nval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
rctlsys_rblk_xfrm(nblk, NULL, nval, RBX_FROM_BLK | RBX_VAL);
if (rctl_invalid_value(rde, nval)) {
kmem_free(nblk, sizeof (rctl_opaque_t));
kmem_cache_free(rctl_val_cache, nval);
return (set_errno(EINVAL));
}
/* allocate what we might need before potentially grabbing p_lock */
oblk = kmem_alloc(sizeof (rctl_opaque_t), KM_SLEEP);
oval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
rval1 = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
rval2 = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
if (nval->rcv_privilege == RCPRIV_BASIC) {
if (flags & RCTL_USE_RECIPIENT_PID) {
pid = nval->rcv_action_recip_pid;
/* case for manipulating rctl values on other procs */
if (pid != curproc->p_pid) {
/* cannot be other pid on process rctls */
if (rde->rcd_entity == RCENTITY_PROCESS) {
ret = set_errno(EINVAL);
goto rctlsys_out;
}
/*
* must have privilege to manipulate controls
* on other processes
*/
if (secpolicy_rctlsys(CRED(), B_FALSE) != 0) {
ret = set_errno(EACCES);
goto rctlsys_out;
}
pid = nval->rcv_action_recip_pid;
mutex_enter(&pidlock);
pp = prfind(pid);
if (!pp) {
mutex_exit(&pidlock);
ret = set_errno(ESRCH);
goto rctlsys_out;
}
/*
* idle or zombie procs have either not yet
* set up their rctls or have already done
* their rctl_set_tearoff's.
*/
if (pp->p_stat == SZOMB ||
pp->p_stat == SIDL) {
mutex_exit(&pidlock);
ret = set_errno(ESRCH);
goto rctlsys_out;
}
/*
* hold this pp's p_lock to ensure that
* it does not do it's rctl_set_tearoff
* If we did not do this, we could
* potentially add rctls to the entity
* with a recipient that is a process
* that has exited.
*/
mutex_enter(&pp->p_lock);
mutex_exit(&pidlock);
/*
* We know that curproc's task, project,
* and zone pointers will not change
* because functions that change them
* call holdlwps(SHOLDFORK1) first.
*/
/*
* verify that the found pp is in the
* current task. If it is, then it
* is also within the current project
* and zone.
*/
if (rde->rcd_entity == RCENTITY_TASK &&
pp->p_task != curproc->p_task) {
ret = set_errno(ESRCH);
goto rctlsys_out;
}
ASSERT(pp->p_task->tk_proj ==
curproc->p_task->tk_proj);
ASSERT(pp->p_zone == curproc->p_zone);
nval->rcv_action_recipient = pp;
nval->rcv_action_recip_pid = pid;
} else {
/* for manipulating rctl values on this proc */
mutex_enter(&curproc->p_lock);
pp = curproc;
nval->rcv_action_recipient = curproc;
nval->rcv_action_recip_pid = curproc->p_pid;
}
} else {
/* RCTL_USE_RECIPIENT_PID not set, use this proc */
mutex_enter(&curproc->p_lock);
pp = curproc;
nval->rcv_action_recipient = curproc;
nval->rcv_action_recip_pid = curproc->p_pid;
}
} else {
/* privileged controls have no recipient pid */
mutex_enter(&curproc->p_lock);
pp = curproc;
nval->rcv_action_recipient = NULL;
nval->rcv_action_recip_pid = -1;
}
nval->rcv_firing_time = 0;
if (action == RCTL_REPLACE) {
if (copyin(old_rblk, oblk, sizeof (rctl_opaque_t)) == -1) {
ret = set_errno(EFAULT);
goto rctlsys_out;
}
rctlsys_rblk_xfrm(oblk, NULL, oval, RBX_FROM_BLK | RBX_VAL);
if (rctl_invalid_value(rde, oval)) {
ret = set_errno(EINVAL);
goto rctlsys_out;
}
if (oval->rcv_privilege == RCPRIV_BASIC) {
if (!(flags & RCTL_USE_RECIPIENT_PID)) {
oval->rcv_action_recipient = curproc;
oval->rcv_action_recip_pid = curproc->p_pid;
}
} else {
oval->rcv_action_recipient = NULL;
oval->rcv_action_recip_pid = -1;
}
/*
* Find the real value we're attempting to replace on the
* sequence, rather than trusting the one delivered from
* userland.
*/
if (ret = rctl_local_get(hndl, NULL, rval1, pp)) {
(void) set_errno(ret);
goto rctlsys_out;
}
do {
if (rval1->rcv_privilege == RCPRIV_SYSTEM ||
rctl_val_cmp(oval, rval1, 0) == 0)
break;
tval = rval1;
rval1 = rval2;
rval2 = tval;
} while (rctl_local_get(hndl, rval2, rval1, pp) == 0);
if (rval1->rcv_privilege == RCPRIV_SYSTEM) {
if (rctl_val_cmp(oval, rval1, 1) == 0)
ret = set_errno(EPERM);
else
ret = set_errno(ESRCH);
goto rctlsys_out;
}
bcopy(rval1, oval, sizeof (rctl_val_t));
/*
* System controls are immutable.
*/
if (nval->rcv_privilege == RCPRIV_SYSTEM) {
ret = set_errno(EPERM);
goto rctlsys_out;
}
/*
* Only privileged processes in the global zone can modify
* privileged rctls of type RCENTITY_ZONE; replacing privileged
* controls with basic ones are not allowed either. Lowering a
* lowerable one might be OK for privileged processes in a
* non-global zone, but lowerable rctls probably don't make
* sense for zones (hence, not modifiable from within a zone).
*/
if (rde->rcd_entity == RCENTITY_ZONE &&
(nval->rcv_privilege == RCPRIV_PRIVILEGED ||
oval->rcv_privilege == RCPRIV_PRIVILEGED) &&
secpolicy_rctlsys(CRED(), B_TRUE) != 0) {
ret = set_errno(EACCES);
goto rctlsys_out;
}
/*
* Must be privileged to replace a privileged control with
* a basic one.
*/
if (oval->rcv_privilege == RCPRIV_PRIVILEGED &&
nval->rcv_privilege != RCPRIV_PRIVILEGED &&
secpolicy_rctlsys(CRED(), B_FALSE) != 0) {
ret = set_errno(EACCES);
goto rctlsys_out;
}
/*
* Must have lowerable global property for non-privileged
* to lower the value of a privileged control; otherwise must
* have sufficient privileges to modify privileged controls
* at all.
*/
if (oval->rcv_privilege == RCPRIV_PRIVILEGED &&
nval->rcv_privilege == RCPRIV_PRIVILEGED &&
((((rde->rcd_flagaction & RCTL_GLOBAL_LOWERABLE) == 0) ||
oval->rcv_flagaction != nval->rcv_flagaction ||
oval->rcv_action_signal != nval->rcv_action_signal ||
oval->rcv_value < nval->rcv_value)) &&
secpolicy_rctlsys(CRED(), B_FALSE) != 0) {
ret = set_errno(EACCES);
goto rctlsys_out;
}
if (ret = rctl_local_replace(hndl, oval, nval, pp)) {
(void) set_errno(ret);
goto rctlsys_out;
}
/* ensure that nval is not freed */
nval = NULL;
} else if (action == RCTL_INSERT) {
/*
* System controls are immutable.
*/
if (nval->rcv_privilege == RCPRIV_SYSTEM) {
ret = set_errno(EPERM);
goto rctlsys_out;
}
/*
* Only privileged processes in the global zone may add
* privileged zone.* rctls. Only privileged processes
* may add other privileged rctls.
*/
if (nval->rcv_privilege == RCPRIV_PRIVILEGED) {
if ((rde->rcd_entity == RCENTITY_ZONE &&
secpolicy_rctlsys(CRED(), B_TRUE) != 0) ||
(rde->rcd_entity != RCENTITY_ZONE &&
secpolicy_rctlsys(CRED(), B_FALSE) != 0)) {
ret = set_errno(EACCES);
goto rctlsys_out;
}
}
/*
* Only one basic control is allowed per rctl.
* If a basic control is being inserted, delete
* any other basic control.
*/
if ((nval->rcv_privilege == RCPRIV_BASIC) &&
(rctl_local_get(hndl, NULL, rval1, pp) == 0)) {
do {
if (rval1->rcv_privilege == RCPRIV_BASIC &&
rval1->rcv_action_recipient == curproc) {
(void) rctl_local_delete(hndl, rval1,
pp);
if (rctl_local_get(hndl, NULL, rval1,
pp) != 0)
break;
}
tval = rval1;
rval1 = rval2;
rval2 = tval;
} while (rctl_local_get(hndl, rval2, rval1, pp)
== 0);
}
if (ret = rctl_local_insert(hndl, nval, pp)) {
(void) set_errno(ret);
goto rctlsys_out;
}
/* ensure that nval is not freed */
nval = NULL;
} else {
/*
* RCTL_DELETE
*/
if (nval->rcv_privilege == RCPRIV_SYSTEM) {
ret = set_errno(EPERM);
goto rctlsys_out;
}
if (nval->rcv_privilege == RCPRIV_PRIVILEGED) {
if ((rde->rcd_entity == RCENTITY_ZONE &&
secpolicy_rctlsys(CRED(), B_TRUE) != 0) ||
(rde->rcd_entity != RCENTITY_ZONE &&
secpolicy_rctlsys(CRED(), B_FALSE) != 0)) {
ret = set_errno(EACCES);
goto rctlsys_out;
}
}
if (ret = rctl_local_delete(hndl, nval, pp)) {
(void) set_errno(ret);
goto rctlsys_out;
}
}
rctlsys_out:
if (pp)
mutex_exit(&pp->p_lock);
kmem_free(nblk, sizeof (rctl_opaque_t));
kmem_free(oblk, sizeof (rctl_opaque_t));
/* only free nval if we did not rctl_local_insert it */
if (nval)
kmem_cache_free(rctl_val_cache, nval);
kmem_cache_free(rctl_val_cache, oval);
kmem_cache_free(rctl_val_cache, rval1);
kmem_cache_free(rctl_val_cache, rval2);
return (ret);
}
static long
rctlsys_lst(char *ubuf, size_t ubufsz)
{
char *kbuf;
size_t kbufsz;
kbufsz = rctl_build_name_buf(&kbuf);
if (kbufsz <= ubufsz &&
copyout(kbuf, ubuf, kbufsz) != 0) {
kmem_free(kbuf, kbufsz);
return (set_errno(EFAULT));
}
kmem_free(kbuf, kbufsz);
return (kbufsz);
}
static long
rctlsys_ctl(char *name, rctl_opaque_t *rblk, int flags)
{
rctl_dict_entry_t *krde;
rctl_opaque_t *krblk;
char *kname;
size_t klen;
kname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
if (name == NULL || copyinstr(name, kname, MAXPATHLEN, &klen) != 0) {
kmem_free(kname, MAXPATHLEN);
return (set_errno(EFAULT));
}
switch (flags) {
case RCTLCTL_GET:
krde = kmem_alloc(sizeof (rctl_dict_entry_t), KM_SLEEP);
krblk = kmem_zalloc(sizeof (rctl_opaque_t), KM_SLEEP);
if (rctl_global_get(kname, krde) == -1) {
kmem_free(krde, sizeof (rctl_dict_entry_t));
kmem_free(krblk, sizeof (rctl_opaque_t));
kmem_free(kname, MAXPATHLEN);
return (set_errno(ESRCH));
}
rctlsys_rblk_xfrm(krblk, krde, NULL, RBX_TO_BLK | RBX_CTL);
if (copyout(krblk, rblk, sizeof (rctl_opaque_t)) != 0) {
kmem_free(krde, sizeof (rctl_dict_entry_t));
kmem_free(krblk, sizeof (rctl_opaque_t));
kmem_free(kname, MAXPATHLEN);
return (set_errno(EFAULT));
}
kmem_free(krde, sizeof (rctl_dict_entry_t));
kmem_free(krblk, sizeof (rctl_opaque_t));
kmem_free(kname, MAXPATHLEN);
break;
case RCTLCTL_SET:
if (secpolicy_rctlsys(CRED(), B_TRUE) != 0) {
kmem_free(kname, MAXPATHLEN);
return (set_errno(EPERM));
}
krde = kmem_alloc(sizeof (rctl_dict_entry_t), KM_SLEEP);
krblk = kmem_zalloc(sizeof (rctl_opaque_t), KM_SLEEP);
if (rctl_global_get(kname, krde) == -1) {
kmem_free(krde, sizeof (rctl_dict_entry_t));
kmem_free(krblk, sizeof (rctl_opaque_t));
kmem_free(kname, MAXPATHLEN);
return (set_errno(ESRCH));
}
if (copyin(rblk, krblk, sizeof (rctl_opaque_t)) != 0) {
kmem_free(krde, sizeof (rctl_dict_entry_t));
kmem_free(krblk, sizeof (rctl_opaque_t));
kmem_free(kname, MAXPATHLEN);
return (set_errno(EFAULT));
}
rctlsys_rblk_xfrm(krblk, krde, NULL, RBX_FROM_BLK | RBX_CTL);
if (rctl_global_set(kname, krde) == -1) {
kmem_free(krde, sizeof (rctl_dict_entry_t));
kmem_free(krblk, sizeof (rctl_opaque_t));
kmem_free(kname, MAXPATHLEN);
return (set_errno(ESRCH));
}
kmem_free(krde, sizeof (rctl_dict_entry_t));
kmem_free(krblk, sizeof (rctl_opaque_t));
kmem_free(kname, MAXPATHLEN);
break;
default:
kmem_free(kname, MAXPATHLEN);
return (set_errno(EINVAL));
}
return (0);
}
/*
* The arbitrary maximum number of rctl_opaque_t that we can pass to
* rctl_projset().
*/
#define RCTL_PROJSET_MAXSIZE 1024
static long
rctlsys_projset(char *name, rctl_opaque_t *rblk, size_t size, int flags)
{
rctl_dict_entry_t *krde;
rctl_opaque_t *krblk;
char *kname;
size_t klen;
rctl_hndl_t hndl;
rctl_val_t *new_values = NULL;
rctl_val_t *alloc_values = NULL;
rctl_val_t *new_val;
rctl_val_t *alloc_val;
int error = 0;
int count;
kname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
if (name == NULL || copyinstr(name, kname, MAXPATHLEN, &klen) != 0) {
kmem_free(kname, MAXPATHLEN);
return (set_errno(EFAULT));
}
if (size > RCTL_PROJSET_MAXSIZE) {
kmem_free(kname, MAXPATHLEN);
return (set_errno(EINVAL));
}
if ((hndl = rctl_hndl_lookup(kname)) == -1) {
kmem_free(kname, MAXPATHLEN);
return (set_errno(EINVAL));
}
krde = rctl_dict_lookup_hndl(hndl);
/* If not a project entity then exit */
if ((krde->rcd_entity != RCENTITY_PROJECT) || (size <= 0)) {
kmem_free(kname, MAXPATHLEN);
return (set_errno(EINVAL));
}
if (secpolicy_rctlsys(CRED(), B_FALSE) != 0) {
kmem_free(kname, MAXPATHLEN);
return (set_errno(EPERM));
}
/* Allocate an array large enough for all resource control blocks */
krblk = kmem_zalloc(sizeof (rctl_opaque_t) * size, KM_SLEEP);
if (copyin(rblk, krblk, sizeof (rctl_opaque_t) * size) == 0) {
for (count = 0; (count < size) && (error == 0); count++) {
new_val = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
alloc_val = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
rctlsys_rblk_xfrm(&krblk[count], NULL, new_val,
RBX_FROM_BLK | RBX_VAL);
/*
* Project entity resource control values should always
* be privileged
*/
if (new_val->rcv_privilege != RCPRIV_PRIVILEGED) {
kmem_cache_free(rctl_val_cache, new_val);
kmem_cache_free(rctl_val_cache, alloc_val);
error = EPERM;
} else if (rctl_invalid_value(krde, new_val) == 0) {
/*
* This is a project entity; we do not set
* rcv_action_recipient or rcv_action_recip_pid
*/
new_val->rcv_action_recipient = NULL;
new_val->rcv_action_recip_pid = -1;
new_val->rcv_flagaction |= RCTL_LOCAL_PROJDB;
new_val->rcv_firing_time = 0;
new_val->rcv_prev = NULL;
new_val->rcv_next = new_values;
new_values = new_val;
/*
* alloc_val is left largely uninitialized, it
* is a pre-allocated rctl_val_t which is used
* later in rctl_local_replace_all() /
* rctl_local_insert_all().
*/
alloc_val->rcv_prev = NULL;
alloc_val->rcv_next = alloc_values;
alloc_values = alloc_val;
} else {
kmem_cache_free(rctl_val_cache, new_val);
kmem_cache_free(rctl_val_cache, alloc_val);
error = EINVAL;
}
}
} else {
error = EFAULT;
}
kmem_free(krblk, sizeof (rctl_opaque_t) * size);
kmem_free(kname, MAXPATHLEN);
if (error) {
/*
* We will have the same number of items in the alloc_values
* linked list, as we have in new_values. However, we remain
* cautious, and teardown the linked lists individually.
*/
while (new_values != NULL) {
new_val = new_values;
new_values = new_values->rcv_next;
kmem_cache_free(rctl_val_cache, new_val);
}
while (alloc_values != NULL) {
alloc_val = alloc_values;
alloc_values = alloc_values->rcv_next;
kmem_cache_free(rctl_val_cache, alloc_val);
}
return (set_errno(error));
}
/*
* We take the p_lock here to maintain consistency with other functions
* - rctlsys_get() and rctlsys_set()
*/
mutex_enter(&curproc->p_lock);
if (flags & TASK_PROJ_PURGE) {
(void) rctl_local_replace_all(hndl, new_values, alloc_values,
curproc);
} else {
(void) rctl_local_insert_all(hndl, new_values, alloc_values,
curproc);
}
mutex_exit(&curproc->p_lock);
return (0);
}
long
rctlsys(int code, char *name, void *obuf, void *nbuf, size_t obufsz, int flags)
{
switch (code) {
case 0:
return (rctlsys_get(name, obuf, nbuf, flags));
case 1:
return (rctlsys_set(name, obuf, nbuf, flags));
case 2:
/*
* Private call for rctl_walk(3C).
*/
return (rctlsys_lst(obuf, obufsz));
case 3:
/*
* Private code for rctladm(8): "rctlctl".
*/
return (rctlsys_ctl(name, obuf, flags));
case 4:
/*
* Private code for setproject(3PROJECT).
*/
return (rctlsys_projset(name, nbuf, obufsz, flags));
default:
return (set_errno(EINVAL));
}
}
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/pathname.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/uio.h>
#include <sys/debug.h>
#include <fs/fs_subr.h>
#include <c2/audit.h>
/*
* Read the contents of a symbolic link.
*/
ssize_t
readlinkat(int dfd, char *name, char *buf, size_t count)
{
vnode_t *startvp;
vnode_t *vp;
struct iovec aiov;
struct uio auio;
int error;
struct vattr vattr;
ssize_t cnt;
int estale_retry = 0;
if ((cnt = (ssize_t)count) < 0)
return (set_errno(EINVAL));
if (name == NULL)
return (set_errno(EFAULT));
if ((error = fgetstartvp(dfd, name, &startvp)) != 0)
return (set_errno(error));
lookup:
if (AU_AUDITING() && startvp != NULL)
audit_setfsat_path(1);
if (error = lookupnameat(name, UIO_USERSPACE, NO_FOLLOW,
NULLVPP, &vp, startvp)) {
if (error == ESTALE && fs_need_estale_retry(estale_retry++))
goto lookup;
goto out;
}
if (vp->v_type != VLNK) {
/*
* Ask the underlying filesystem if it wants this
* object to look like a symlink at user-level.
*/
vattr.va_mask = AT_TYPE;
error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL);
if (error || vattr.va_type != VLNK) {
VN_RELE(vp);
if ((error == ESTALE) &&
fs_need_estale_retry(estale_retry++))
goto lookup;
error = EINVAL;
goto out;
}
}
aiov.iov_base = buf;
aiov.iov_len = cnt;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_loffset = 0;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_extflg = UIO_COPY_CACHED;
auio.uio_resid = cnt;
error = VOP_READLINK(vp, &auio, CRED(), NULL);
VN_RELE(vp);
if (error == ESTALE && fs_need_estale_retry(estale_retry++))
goto lookup;
out:
if (startvp != NULL)
VN_RELE(startvp);
if (error)
return (set_errno(error));
return ((ssize_t)(cnt - auio.uio_resid));
}
ssize_t
readlink(char *name, char *buf, size_t count)
{
return (readlinkat(AT_FDCWD, name, buf, count));
}
#ifdef _SYSCALL32_IMPL
/*
* readlink32() intentionally returns a ssize_t rather than ssize32_t;
* see the comments above read32 for details.
*/
ssize_t
readlinkat32(int dfd, caddr32_t name, caddr32_t buf, size32_t count)
{
return ((ssize32_t)readlinkat(dfd, (char *)(uintptr_t)name,
(char *)(uintptr_t)buf, (ssize32_t)count));
}
ssize_t
readlink32(caddr32_t name, caddr32_t buf, size32_t count)
{
return ((ssize32_t)readlinkat(AT_FDCWD, (char *)(uintptr_t)name,
(char *)(uintptr_t)buf, (ssize32_t)count));
}
#endif /* _SYSCALL32_IMPL */
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/uio.h>
#include <sys/debug.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <c2/audit.h>
/*
* Rename a file relative to a given directory
*/
int
renameat(int ffd, char *from, int tfd, char *to)
{
vnode_t *fstartvp = NULL;
vnode_t *tstartvp = NULL;
int error;
if (from == NULL || to == NULL)
return (set_errno(EFAULT));
if ((error = fgetstartvp(ffd, from, &fstartvp)) != 0)
goto out;
if ((error = fgetstartvp(tfd, to, &tstartvp)) != 0)
goto out;
error = vn_renameat(fstartvp, from, tstartvp, to, UIO_USERSPACE);
out:
if (fstartvp != NULL)
VN_RELE(fstartvp);
if (tstartvp != NULL)
VN_RELE(tstartvp);
if (error)
return (set_errno(error));
return (0);
}
int
rename(char *from, char *to)
{
return (renameat(AT_FDCWD, from, AT_FDCWD, to));
}
/*
* 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
*/
#ident "%Z%%M% %I% %E% SMI"
/*
* Copyright 1997 by Sun Microsystems, Inc.
* All rights reserved.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/pathname.h>
int
resolvepath(char *path, char *buf, size_t count)
{
struct pathname lookpn;
struct pathname resolvepn;
int error;
if (count == 0)
return (0);
if (error = pn_get(path, UIO_USERSPACE, &lookpn))
return (set_errno(error));
pn_alloc(&resolvepn);
error = lookuppn(&lookpn, &resolvepn, FOLLOW, NULL, NULL);
if (error == 0) {
if (count > resolvepn.pn_pathlen)
count = resolvepn.pn_pathlen;
if (copyout(resolvepn.pn_path, buf, count))
error = EFAULT;
}
pn_free(&resolvepn);
pn_free(&lookpn);
if (error)
return (set_errno(error));
return ((int)count);
}
/*
* 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.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/inttypes.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/tuneable.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/resource.h>
#include <sys/ulimit.h>
#include <sys/debug.h>
#include <sys/rctl.h>
#include <vm/as.h>
/*
* Perhaps ulimit could be moved into a user library, as calls to
* getrlimit and setrlimit, were it not for binary compatibility
* restrictions.
*/
long
ulimit(int cmd, long arg)
{
proc_t *p = curproc;
long retval;
switch (cmd) {
case UL_GFILLIM: /* Return current file size limit. */
{
rlim64_t filesize;
mutex_enter(&p->p_lock);
filesize = rctl_enforced_value(rctlproc_legacy[RLIMIT_FSIZE],
p->p_rctls, p);
mutex_exit(&p->p_lock);
if (get_udatamodel() == DATAMODEL_ILP32) {
/*
* File size is returned in blocks for ulimit.
* This function is deprecated and therefore LFS API
* didn't define the behaviour of ulimit.
* Here we return maximum value of file size possible
* so that applications that do not check errors
* continue to work.
*/
if (filesize > MAXOFF32_T)
filesize = MAXOFF32_T;
retval = ((int)filesize >> SCTRSHFT);
} else
retval = filesize >> SCTRSHFT;
break;
}
case UL_SFILLIM: /* Set new file size limit. */
{
int error = 0;
rlim64_t lim = (rlim64_t)arg;
struct rlimit64 rl64;
rctl_alloc_gp_t *gp = rctl_rlimit_set_prealloc(1);
if (lim >= (((rlim64_t)MAXOFFSET_T) >> SCTRSHFT))
lim = (rlim64_t)RLIM64_INFINITY;
else
lim <<= SCTRSHFT;
rl64.rlim_max = rl64.rlim_cur = lim;
mutex_enter(&p->p_lock);
if (error = rctl_rlimit_set(rctlproc_legacy[RLIMIT_FSIZE], p,
&rl64, gp, RCTL_LOCAL_DENY | RCTL_LOCAL_SIGNAL, SIGXFSZ,
CRED())) {
mutex_exit(&p->p_lock);
rctl_prealloc_destroy(gp);
return (set_errno(error));
}
mutex_exit(&p->p_lock);
rctl_prealloc_destroy(gp);
retval = arg;
break;
}
case UL_GMEMLIM: /* Return maximum possible break value. */
{
struct seg *seg;
struct seg *nextseg;
struct as *as = p->p_as;
caddr_t brkend;
caddr_t brkbase;
size_t size;
rlim64_t size_ctl;
rlim64_t vmem_ctl;
/*
* Find the segment with a virtual address
* greater than the end of the current break.
*/
nextseg = NULL;
mutex_enter(&p->p_lock);
brkbase = (caddr_t)p->p_brkbase;
brkend = (caddr_t)p->p_brkbase + p->p_brksize;
mutex_exit(&p->p_lock);
/*
* Since we can't return less than the current break,
* initialize the return value to the current break
*/
retval = (long)brkend;
AS_LOCK_ENTER(as, RW_READER);
for (seg = as_findseg(as, brkend, 0); seg != NULL;
seg = AS_SEGNEXT(as, seg)) {
if (seg->s_base >= brkend) {
nextseg = seg;
break;
}
}
mutex_enter(&p->p_lock);
size_ctl = rctl_enforced_value(rctlproc_legacy[RLIMIT_DATA],
p->p_rctls, p);
vmem_ctl = rctl_enforced_value(rctlproc_legacy[RLIMIT_VMEM],
p->p_rctls, p);
mutex_exit(&p->p_lock);
/*
* First, calculate the maximum break value based on
* the user's RLIMIT_DATA, but also taking into account
* that this value cannot be greater than as->a_userlimit.
* We also take care to make sure that we don't overflow
* in the calculation.
*/
/*
* Since we are casting the RLIMIT_DATA value to a
* ulong (a 32-bit value in the 32-bit kernel) we have
* to pass this assertion.
*/
ASSERT32((size_t)size_ctl <= UINT32_MAX);
size = (size_t)size_ctl;
if (as->a_userlimit - brkbase > size)
retval = MAX((size_t)retval, (size_t)(brkbase + size));
/* don't return less than current */
else
retval = (long)as->a_userlimit;
/*
* The max break cannot extend into the next segment
*/
if (nextseg != NULL)
retval = MIN((uintptr_t)retval,
(uintptr_t)nextseg->s_base);
/*
* Handle the case where there is an limit on RLIMIT_VMEM
*/
if (vmem_ctl < UINT64_MAX) {
/* calculate brkend based on the end of page */
caddr_t brkendpg = (caddr_t)roundup((uintptr_t)brkend,
PAGESIZE);
/*
* Large Files: The following assertion has to pass
* through to ensure the correctness of the cast.
*/
ASSERT32(vmem_ctl <= UINT32_MAX);
size = (size_t)(vmem_ctl & PAGEMASK);
if (as->a_size < size)
size -= as->a_size;
else
size = 0;
/*
* Take care to not overflow the calculation
*/
if (as->a_userlimit - brkendpg > size)
retval = MIN((size_t)retval,
(size_t)(brkendpg + size));
}
AS_LOCK_EXIT(as);
/* truncate to same boundary as sbrk */
switch (get_udatamodel()) {
default:
case DATAMODEL_ILP32:
retval = retval & ~(8-1);
break;
case DATAMODEL_LP64:
retval = retval & ~(16-1);
break;
}
break;
}
case UL_GDESLIM: /* Return approximate number of open files */
{
rlim64_t fdno_ctl;
mutex_enter(&curproc->p_lock);
fdno_ctl = rctl_enforced_value(rctlproc_legacy[RLIMIT_NOFILE],
curproc->p_rctls, curproc);
ASSERT(fdno_ctl <= INT_MAX);
retval = (rlim_t)fdno_ctl;
mutex_exit(&curproc->p_lock);
break;
}
default:
return (set_errno(EINVAL));
}
return (retval);
}
#ifdef _SYSCALL32_IMPL
int
ulimit32(int cmd, int arg)
{
return ((int)ulimit(cmd, (long)arg));
}
#endif /* _SYSCALL32_IMPL */
#if defined(_ILP32) || defined(_SYSCALL32_IMPL)
/*
* Large Files: getrlimit returns RLIM_SAVED_CUR or RLIM_SAVED_MAX when
* rlim_cur or rlim_max is not representable in 32-bit rlim_t. These
* values are just tokens which will be used in setrlimit to set the
* correct limits. The current limits are saved in the saved_rlimit members
* in user structures when the token is returned. setrlimit restores
* the limit values to these saved values when the token is passed.
* Consider the following common scenario of the apps:
*
* limit = getrlimit();
* savedlimit = limit;
* limit = limit1;
* setrlimit(limit)
* // execute all processes in the new rlimit state.
* setrlimit(savedlimit) // restore the old values.
*
* Most apps don't check error returns from getrlimit or setrlimit
* and this is why we return tokens when the correct value
* cannot be represented in rlim_t. For more discussion refer to
* the LFS API document.
*
* In the 64-bit kernel, all existing resource limits are treated in this
* manner. In the 32-bit kernel, CPU time is treated equivalently to the
* file size limit above; the VM-related limits are not. The macro,
* RLIM_SAVED(x), returns true if the resource limit should be handled in
* this way on the current kernel.
*/
int
getrlimit32(int resource, struct rlimit32 *rlp)
{
struct rlimit32 rlim32;
struct rlimit64 rlim64;
struct proc *p = curproc;
struct user *up = PTOU(p);
int savecur = 0;
int savemax = 0;
if (resource < 0 || resource >= RLIM_NLIMITS)
return (set_errno(EINVAL));
mutex_enter(&p->p_lock);
(void) rctl_rlimit_get(rctlproc_legacy[resource], p, &rlim64);
mutex_exit(&p->p_lock);
if (rlim64.rlim_max > (rlim64_t)UINT32_MAX) {
if (rlim64.rlim_max == RLIM64_INFINITY)
rlim32.rlim_max = RLIM32_INFINITY;
else {
savemax = 1;
rlim32.rlim_max = RLIM32_SAVED_MAX;
/*CONSTCOND*/
ASSERT(RLIM_SAVED(resource));
}
if (rlim64.rlim_cur == RLIM64_INFINITY)
rlim32.rlim_cur = RLIM32_INFINITY;
else if (rlim64.rlim_cur == rlim64.rlim_max) {
savecur = 1;
rlim32.rlim_cur = RLIM32_SAVED_MAX;
/*CONSTCOND*/
ASSERT(RLIM_SAVED(resource));
} else if (rlim64.rlim_cur > (rlim64_t)UINT32_MAX) {
savecur = 1;
rlim32.rlim_cur = RLIM32_SAVED_CUR;
/*CONSTCOND*/
ASSERT(RLIM_SAVED(resource));
} else
rlim32.rlim_cur = rlim64.rlim_cur;
/*
* save the current limits in user structure.
*/
/*CONSTCOND*/
if (RLIM_SAVED(resource)) {
mutex_enter(&p->p_lock);
if (savemax)
up->u_saved_rlimit[resource].rlim_max =
rlim64.rlim_max;
if (savecur)
up->u_saved_rlimit[resource].rlim_cur =
rlim64.rlim_cur;
mutex_exit(&p->p_lock);
}
} else {
ASSERT(rlim64.rlim_cur <= (rlim64_t)UINT32_MAX);
rlim32.rlim_max = rlim64.rlim_max;
rlim32.rlim_cur = rlim64.rlim_cur;
}
if (copyout(&rlim32, rlp, sizeof (rlim32)))
return (set_errno(EFAULT));
return (0);
}
/*
* See comments above getrlimit32(). When the tokens are passed in the
* rlimit structure the values are considered equal to the values
* stored in saved_rlimit members of user structure.
* When the user passes RLIM_INFINITY to set the resource limit to
* unlimited internally understand this value as RLIM64_INFINITY and
* let rlimit() do the job.
*/
int
setrlimit32(int resource, struct rlimit32 *rlp)
{
struct rlimit32 rlim32;
struct rlimit64 rlim64;
struct rlimit64 saved_rlim;
int error;
struct proc *p = ttoproc(curthread);
struct user *up = PTOU(p);
rctl_alloc_gp_t *gp;
if (resource < 0 || resource >= RLIM_NLIMITS)
return (set_errno(EINVAL));
if (copyin(rlp, &rlim32, sizeof (rlim32)))
return (set_errno(EFAULT));
gp = rctl_rlimit_set_prealloc(1);
/*
* Disallow resource limit tunnelling
*/
/*CONSTCOND*/
if (RLIM_SAVED(resource)) {
mutex_enter(&p->p_lock);
saved_rlim = up->u_saved_rlimit[resource];
mutex_exit(&p->p_lock);
} else {
saved_rlim.rlim_max = (rlim64_t)rlim32.rlim_max;
saved_rlim.rlim_cur = (rlim64_t)rlim32.rlim_cur;
}
switch (rlim32.rlim_cur) {
case RLIM32_INFINITY:
rlim64.rlim_cur = RLIM64_INFINITY;
break;
case RLIM32_SAVED_CUR:
rlim64.rlim_cur = saved_rlim.rlim_cur;
break;
case RLIM32_SAVED_MAX:
rlim64.rlim_cur = saved_rlim.rlim_max;
break;
default:
rlim64.rlim_cur = (rlim64_t)rlim32.rlim_cur;
break;
}
switch (rlim32.rlim_max) {
case RLIM32_INFINITY:
rlim64.rlim_max = RLIM64_INFINITY;
break;
case RLIM32_SAVED_MAX:
rlim64.rlim_max = saved_rlim.rlim_max;
break;
case RLIM32_SAVED_CUR:
rlim64.rlim_max = saved_rlim.rlim_cur;
break;
default:
rlim64.rlim_max = (rlim64_t)rlim32.rlim_max;
break;
}
mutex_enter(&p->p_lock);
if (error = rctl_rlimit_set(rctlproc_legacy[resource], p, &rlim64, gp,
rctlproc_flags[resource], rctlproc_signals[resource], CRED())) {
mutex_exit(&p->p_lock);
rctl_prealloc_destroy(gp);
return (set_errno(error));
}
mutex_exit(&p->p_lock);
rctl_prealloc_destroy(gp);
return (0);
}
#endif /* _ILP32 && _SYSCALL32_IMPL */
int
getrlimit64(int resource, struct rlimit64 *rlp)
{
struct rlimit64 rlim64;
struct proc *p = ttoproc(curthread);
if (resource < 0 || resource >= RLIM_NLIMITS)
return (set_errno(EINVAL));
mutex_enter(&p->p_lock);
(void) rctl_rlimit_get(rctlproc_legacy[resource], p, &rlim64);
mutex_exit(&p->p_lock);
if (copyout(&rlim64, rlp, sizeof (rlim64)))
return (set_errno(EFAULT));
return (0);
}
int
setrlimit64(int resource, struct rlimit64 *rlp)
{
struct rlimit64 rlim64;
struct proc *p = ttoproc(curthread);
int error;
rctl_alloc_gp_t *gp;
if (resource < 0 || resource >= RLIM_NLIMITS)
return (set_errno(EINVAL));
if (copyin(rlp, &rlim64, sizeof (rlim64)))
return (set_errno(EFAULT));
gp = rctl_rlimit_set_prealloc(1);
mutex_enter(&p->p_lock);
if (error = rctl_rlimit_set(rctlproc_legacy[resource], p, &rlim64, gp,
rctlproc_flags[resource], rctlproc_signals[resource], CRED())) {
mutex_exit(&p->p_lock);
rctl_prealloc_destroy(gp);
return (set_errno(error));
}
mutex_exit(&p->p_lock);
rctl_prealloc_destroy(gp);
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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Implement fast getrusage call
*/
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <sys/resource.h>
#include <sys/vm_usage.h>
static int
getrusage(void *user_rusage)
{
struct rusage r;
kthread_t *t = curthread;
proc_t *p = ttoproc(t);
hrtime_t snsecs, unsecs;
klwp_t *lwp;
bzero(&r, sizeof (struct rusage));
mutex_enter(&p->p_lock);
if (p->p_defunct > 0) {
r.ru_majflt = p->p_ru.majflt;
r.ru_minflt = p->p_ru.minflt;
r.ru_nswap = p->p_ru.nswap;
r.ru_inblock = p->p_ru.inblock;
r.ru_oublock = p->p_ru.oublock;
r.ru_msgsnd = p->p_ru.msgsnd;
r.ru_msgrcv = p->p_ru.msgrcv;
r.ru_nsignals = p->p_ru.nsignals;
r.ru_nvcsw = p->p_ru.nvcsw;
r.ru_nivcsw = p->p_ru.nivcsw;
}
unsecs = mstate_aggr_state(p, LMS_USER);
snsecs = mstate_aggr_state(p, LMS_SYSTEM);
do {
if (t->t_proc_flag & TP_LWPEXIT)
continue;
lwp = ttolwp(t);
r.ru_majflt += lwp->lwp_ru.majflt;
r.ru_minflt += lwp->lwp_ru.minflt;
r.ru_nswap += lwp->lwp_ru.nswap;
r.ru_inblock += lwp->lwp_ru.inblock;
r.ru_oublock += lwp->lwp_ru.oublock;
r.ru_msgsnd += lwp->lwp_ru.msgsnd;
r.ru_msgrcv += lwp->lwp_ru.msgrcv;
r.ru_nsignals += lwp->lwp_ru.nsignals;
r.ru_nvcsw += lwp->lwp_ru.nvcsw;
r.ru_nivcsw += lwp->lwp_ru.nivcsw;
} while ((t = t->t_forw) != curthread);
mutex_exit(&p->p_lock);
hrt2tv(unsecs, &r.ru_utime);
hrt2tv(snsecs, &r.ru_stime);
#ifdef _SYSCALL32_IMPL
if (get_udatamodel() == DATAMODEL_ILP32) {
struct rusage32 r32;
bzero(&r32, sizeof (struct rusage32));
r32.ru_utime.tv_sec = r.ru_utime.tv_sec;
r32.ru_utime.tv_usec = r.ru_utime.tv_usec;
r32.ru_stime.tv_sec = r.ru_stime.tv_sec;
r32.ru_stime.tv_usec = r.ru_stime.tv_usec;
r32.ru_majflt = (int32_t)r.ru_majflt;
r32.ru_minflt = (int32_t)r.ru_minflt;
r32.ru_nswap = (int32_t)r.ru_nswap;
r32.ru_inblock = (int32_t)r.ru_inblock;
r32.ru_oublock = (int32_t)r.ru_oublock;
r32.ru_msgsnd = (int32_t)r.ru_msgsnd;
r32.ru_msgrcv = (int32_t)r.ru_msgrcv;
r32.ru_nsignals = (int32_t)r.ru_nsignals;
r32.ru_nvcsw = (int32_t)r.ru_nvcsw;
r32.ru_nivcsw = (int32_t)r.ru_nivcsw;
if (copyout(&r32, user_rusage, sizeof (r32)) != 0)
return (set_errno(EFAULT));
} else
#endif /* _SYSCALL32_IMPL */
if (copyout(&r, user_rusage, sizeof (r)) != 0)
return (set_errno(EFAULT));
return (0);
}
static int
getrusage_chld(void *user_rusage)
{
struct rusage r;
kthread_t *t = curthread;
proc_t *p = ttoproc(t);
hrtime_t snsecs, unsecs;
bzero(&r, sizeof (struct rusage));
mutex_enter(&p->p_lock);
unsecs = p->p_cacct[LMS_USER];
snsecs = p->p_cacct[LMS_SYSTEM] + p->p_cacct[LMS_TRAP];
r.ru_majflt = p->p_cru.majflt;
r.ru_minflt = p->p_cru.minflt;
r.ru_nswap = p->p_cru.nswap;
r.ru_inblock = p->p_cru.inblock;
r.ru_oublock = p->p_cru.oublock;
r.ru_msgsnd = p->p_cru.msgsnd;
r.ru_msgrcv = p->p_cru.msgrcv;
r.ru_nsignals = p->p_cru.nsignals;
r.ru_nvcsw = p->p_cru.nvcsw;
r.ru_nivcsw = p->p_cru.nivcsw;
mutex_exit(&p->p_lock);
hrt2tv(unsecs, &r.ru_utime);
hrt2tv(snsecs, &r.ru_stime);
#ifdef _SYSCALL32_IMPL
if (get_udatamodel() == DATAMODEL_ILP32) {
struct rusage32 r32;
bzero(&r32, sizeof (struct rusage32));
r32.ru_utime.tv_sec = r.ru_utime.tv_sec;
r32.ru_utime.tv_usec = r.ru_utime.tv_usec;
r32.ru_stime.tv_sec = r.ru_stime.tv_sec;
r32.ru_stime.tv_usec = r.ru_stime.tv_usec;
r32.ru_majflt = (int32_t)r.ru_majflt;
r32.ru_minflt = (int32_t)r.ru_minflt;
r32.ru_nswap = (int32_t)r.ru_nswap;
r32.ru_inblock = (int32_t)r.ru_inblock;
r32.ru_oublock = (int32_t)r.ru_oublock;
r32.ru_msgsnd = (int32_t)r.ru_msgsnd;
r32.ru_msgrcv = (int32_t)r.ru_msgrcv;
r32.ru_nsignals = (int32_t)r.ru_nsignals;
r32.ru_nvcsw = (int32_t)r.ru_nvcsw;
r32.ru_nivcsw = (int32_t)r.ru_nivcsw;
if (copyout(&r32, user_rusage, sizeof (r32)) != 0)
return (set_errno(EFAULT));
} else
#endif /* _SYSCALL32_IMPL */
if (copyout(&r, user_rusage, sizeof (r)) != 0)
return (set_errno(EFAULT));
return (0);
}
static int
getrusage_lwp(void *user_rusage)
{
struct rusage r;
kthread_t *t = curthread;
klwp_t *lwp;
hrtime_t snsecs, unsecs;
struct mstate *ms;
bzero(&r, sizeof (struct rusage));
lwp = ttolwp(t);
ms = &lwp->lwp_mstate;
unsecs = ms->ms_acct[LMS_USER];
snsecs = ms->ms_acct[LMS_SYSTEM] + ms->ms_acct[LMS_TRAP];
scalehrtime(&unsecs);
scalehrtime(&snsecs);
r.ru_majflt = lwp->lwp_ru.majflt;
r.ru_minflt = lwp->lwp_ru.minflt;
r.ru_nswap = lwp->lwp_ru.nswap;
r.ru_inblock = lwp->lwp_ru.inblock;
r.ru_oublock = lwp->lwp_ru.oublock;
r.ru_msgsnd = lwp->lwp_ru.msgsnd;
r.ru_msgrcv = lwp->lwp_ru.msgrcv;
r.ru_nsignals = lwp->lwp_ru.nsignals;
r.ru_nvcsw = lwp->lwp_ru.nvcsw;
r.ru_nivcsw = lwp->lwp_ru.nivcsw;
hrt2tv(unsecs, &r.ru_utime);
hrt2tv(snsecs, &r.ru_stime);
#ifdef _SYSCALL32_IMPL
if (get_udatamodel() == DATAMODEL_ILP32) {
struct rusage32 r32;
bzero(&r32, sizeof (struct rusage32));
r32.ru_utime.tv_sec = r.ru_utime.tv_sec;
r32.ru_utime.tv_usec = r.ru_utime.tv_usec;
r32.ru_stime.tv_sec = r.ru_stime.tv_sec;
r32.ru_stime.tv_usec = r.ru_stime.tv_usec;
r32.ru_majflt = (int32_t)r.ru_majflt;
r32.ru_minflt = (int32_t)r.ru_minflt;
r32.ru_nswap = (int32_t)r.ru_nswap;
r32.ru_inblock = (int32_t)r.ru_inblock;
r32.ru_oublock = (int32_t)r.ru_oublock;
r32.ru_msgsnd = (int32_t)r.ru_msgsnd;
r32.ru_msgrcv = (int32_t)r.ru_msgrcv;
r32.ru_nsignals = (int32_t)r.ru_nsignals;
r32.ru_nvcsw = (int32_t)r.ru_nvcsw;
r32.ru_nivcsw = (int32_t)r.ru_nivcsw;
if (copyout(&r32, user_rusage, sizeof (r32)) != 0)
return (set_errno(EFAULT));
} else
#endif /* _SYSCALL32_IMPL */
if (copyout(&r, user_rusage, sizeof (r)) != 0)
return (set_errno(EFAULT));
return (0);
}
int
rusagesys(int code, void *arg1, void *arg2, void *arg3, void *arg4)
{
switch (code) {
case _RUSAGESYS_GETRUSAGE:
return (getrusage(arg1));
case _RUSAGESYS_GETRUSAGE_CHLD:
return (getrusage_chld(arg1));
case _RUSAGESYS_GETRUSAGE_LWP:
return (getrusage_lwp(arg1));
case _RUSAGESYS_GETVMUSAGE:
return (vm_getusage((uint_t)(uintptr_t)arg1, (time_t)arg2,
(vmusage_t *)arg3, (size_t *)arg4, 0));
default:
return (set_errno(EINVAL));
}
}
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2020, Joyent, Inc.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/inttypes.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/cpuvar.h>
#include <sys/uio.h>
#include <sys/debug.h>
#include <sys/rctl.h>
#include <sys/nbmlock.h>
#include <sys/limits.h>
#define COPYOUT_MAX_CACHE (1<<17) /* 128K */
size_t copyout_max_cached = COPYOUT_MAX_CACHE; /* global so it's patchable */
/*
* read, write, pread, pwrite, readv, and writev syscalls.
*
* 64-bit open: all open's are large file opens.
* Large Files: the behaviour of read depends on whether the fd
* corresponds to large open or not.
* 32-bit open: FOFFMAX flag not set.
* read until MAXOFF32_T - 1 and read at MAXOFF32_T returns
* EOVERFLOW if count is non-zero and if size of file
* is > MAXOFF32_T. If size of file is <= MAXOFF32_T read
* at >= MAXOFF32_T returns EOF.
*/
/*
* Native system call
*/
ssize_t
read(int fdes, void *cbuf, size_t count)
{
struct uio auio;
struct iovec aiov;
file_t *fp;
register vnode_t *vp;
struct cpu *cp;
int fflag, ioflag, rwflag;
ssize_t cnt, bcount;
int error = 0;
u_offset_t fileoff;
int in_crit = 0;
if ((cnt = (ssize_t)count) < 0)
return (set_errno(EINVAL));
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
if (((fflag = fp->f_flag) & FREAD) == 0) {
error = EBADF;
goto out;
}
vp = fp->f_vnode;
if (vp->v_type == VREG && cnt == 0) {
goto out;
}
rwflag = 0;
aiov.iov_base = cbuf;
aiov.iov_len = cnt;
/*
* We have to enter the critical region before calling VOP_RWLOCK
* to avoid a deadlock with write() calls.
*/
if (nbl_need_check(vp)) {
int svmand;
nbl_start_crit(vp, RW_READER);
in_crit = 1;
error = nbl_svmand(vp, fp->f_cred, &svmand);
if (error != 0)
goto out;
if (nbl_conflict(vp, NBL_READ, fp->f_offset, cnt, svmand,
NULL)) {
error = EACCES;
goto out;
}
}
(void) VOP_RWLOCK(vp, rwflag, NULL);
/*
* We do the following checks inside VOP_RWLOCK so as to
* prevent file size from changing while these checks are
* being done. Also, we load fp's offset to the local
* variable fileoff because we can have a parallel lseek
* going on (f_offset is not protected by any lock) which
* could change f_offset. We need to see the value only
* once here and take a decision. Seeing it more than once
* can lead to incorrect functionality.
*/
fileoff = (u_offset_t)fp->f_offset;
if (fileoff >= OFFSET_MAX(fp) && (vp->v_type == VREG)) {
struct vattr va;
va.va_mask = AT_SIZE;
if ((error = VOP_GETATTR(vp, &va, 0, fp->f_cred, NULL))) {
VOP_RWUNLOCK(vp, rwflag, NULL);
goto out;
}
if (fileoff >= va.va_size) {
cnt = 0;
VOP_RWUNLOCK(vp, rwflag, NULL);
goto out;
} else {
error = EOVERFLOW;
VOP_RWUNLOCK(vp, rwflag, NULL);
goto out;
}
}
if ((vp->v_type == VREG) &&
(fileoff + cnt > OFFSET_MAX(fp))) {
cnt = (ssize_t)(OFFSET_MAX(fp) - fileoff);
}
auio.uio_loffset = fileoff;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = bcount = cnt;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = MAXOFFSET_T;
auio.uio_fmode = fflag;
/*
* Only use bypass caches when the count is large enough
*/
if (bcount <= copyout_max_cached)
auio.uio_extflg = UIO_COPY_CACHED;
else
auio.uio_extflg = UIO_COPY_DEFAULT;
ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
/* If read sync is not asked for, filter sync flags */
if ((ioflag & FRSYNC) == 0)
ioflag &= ~(FSYNC|FDSYNC);
error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
cnt -= auio.uio_resid;
CPU_STATS_ENTER_K();
cp = CPU;
CPU_STATS_ADDQ(cp, sys, sysread, 1);
CPU_STATS_ADDQ(cp, sys, readch, (ulong_t)cnt);
CPU_STATS_EXIT_K();
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
if (vp->v_type == VFIFO) /* Backward compatibility */
fp->f_offset = cnt;
else if (((fp->f_flag & FAPPEND) == 0) ||
(vp->v_type != VREG) || (bcount != 0)) /* POSIX */
fp->f_offset = auio.uio_loffset;
VOP_RWUNLOCK(vp, rwflag, NULL);
if (error == EINTR && cnt != 0)
error = 0;
out:
if (in_crit)
nbl_end_crit(vp);
releasef(fdes);
if (error)
return (set_errno(error));
return (cnt);
}
/*
* Native system call
*/
ssize_t
write(int fdes, void *cbuf, size_t count)
{
struct uio auio;
struct iovec aiov;
file_t *fp;
register vnode_t *vp;
struct cpu *cp;
int fflag, ioflag, rwflag;
ssize_t cnt, bcount;
int error = 0;
u_offset_t fileoff;
int in_crit = 0;
if ((cnt = (ssize_t)count) < 0)
return (set_errno(EINVAL));
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
if (((fflag = fp->f_flag) & FWRITE) == 0) {
error = EBADF;
goto out;
}
vp = fp->f_vnode;
if (vp->v_type == VREG && cnt == 0) {
goto out;
}
rwflag = 1;
aiov.iov_base = cbuf;
aiov.iov_len = cnt;
/*
* We have to enter the critical region before calling VOP_RWLOCK
* to avoid a deadlock with ufs.
*/
if (nbl_need_check(vp)) {
int svmand;
nbl_start_crit(vp, RW_READER);
in_crit = 1;
error = nbl_svmand(vp, fp->f_cred, &svmand);
if (error != 0)
goto out;
if (nbl_conflict(vp, NBL_WRITE, fp->f_offset, cnt, svmand,
NULL)) {
error = EACCES;
goto out;
}
}
(void) VOP_RWLOCK(vp, rwflag, NULL);
fileoff = fp->f_offset;
if (vp->v_type == VREG) {
/*
* We raise psignal if write for >0 bytes causes
* it to exceed the ulimit.
*/
if (fileoff >= curproc->p_fsz_ctl) {
VOP_RWUNLOCK(vp, rwflag, NULL);
mutex_enter(&curproc->p_lock);
(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
curproc->p_rctls, curproc, RCA_UNSAFE_SIGINFO);
mutex_exit(&curproc->p_lock);
error = EFBIG;
goto out;
}
/*
* We return EFBIG if write is done at an offset
* greater than the offset maximum for this file structure.
*/
if (fileoff >= OFFSET_MAX(fp)) {
VOP_RWUNLOCK(vp, rwflag, NULL);
error = EFBIG;
goto out;
}
/*
* Limit the bytes to be written upto offset maximum for
* this open file structure.
*/
if (fileoff + cnt > OFFSET_MAX(fp))
cnt = (ssize_t)(OFFSET_MAX(fp) - fileoff);
}
auio.uio_loffset = fileoff;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = bcount = cnt;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = curproc->p_fsz_ctl;
auio.uio_fmode = fflag;
auio.uio_extflg = UIO_COPY_DEFAULT;
ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
error = VOP_WRITE(vp, &auio, ioflag, fp->f_cred, NULL);
cnt -= auio.uio_resid;
CPU_STATS_ENTER_K();
cp = CPU;
CPU_STATS_ADDQ(cp, sys, syswrite, 1);
CPU_STATS_ADDQ(cp, sys, writech, (ulong_t)cnt);
CPU_STATS_EXIT_K();
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
if (vp->v_type == VFIFO) /* Backward compatibility */
fp->f_offset = cnt;
else if (((fp->f_flag & FAPPEND) == 0) ||
(vp->v_type != VREG) || (bcount != 0)) /* POSIX */
fp->f_offset = auio.uio_loffset;
VOP_RWUNLOCK(vp, rwflag, NULL);
if (error == EINTR && cnt != 0)
error = 0;
out:
if (in_crit)
nbl_end_crit(vp);
releasef(fdes);
if (error)
return (set_errno(error));
return (cnt);
}
ssize_t
pread(int fdes, void *cbuf, size_t count, off_t offset)
{
struct uio auio;
struct iovec aiov;
file_t *fp;
register vnode_t *vp;
struct cpu *cp;
int fflag, ioflag, rwflag;
ssize_t bcount;
int error = 0;
u_offset_t fileoff = (u_offset_t)(ulong_t)offset;
#ifdef _SYSCALL32_IMPL
u_offset_t maxoff = get_udatamodel() == DATAMODEL_ILP32 ?
MAXOFF32_T : MAXOFFSET_T;
#else
const u_offset_t maxoff = MAXOFF32_T;
#endif
int in_crit = 0;
if ((bcount = (ssize_t)count) < 0)
return (set_errno(EINVAL));
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
if (((fflag = fp->f_flag) & (FREAD)) == 0) {
error = EBADF;
goto out;
}
rwflag = 0;
vp = fp->f_vnode;
if (vp->v_type == VREG) {
if (bcount == 0)
goto out;
/*
* Return EINVAL if an invalid offset comes to pread.
* Negative offset from user will cause this error.
*/
if (fileoff > maxoff) {
error = EINVAL;
goto out;
}
/*
* Limit offset such that we don't read or write
* a file beyond the maximum offset representable in
* an off_t structure.
*/
if (fileoff + bcount > maxoff)
bcount = (ssize_t)((offset_t)maxoff - fileoff);
} else if (vp->v_type == VFIFO) {
error = ESPIPE;
goto out;
}
/*
* We have to enter the critical region before calling VOP_RWLOCK
* to avoid a deadlock with ufs.
*/
if (nbl_need_check(vp)) {
int svmand;
nbl_start_crit(vp, RW_READER);
in_crit = 1;
error = nbl_svmand(vp, fp->f_cred, &svmand);
if (error != 0)
goto out;
if (nbl_conflict(vp, NBL_READ, fileoff, bcount, svmand,
NULL)) {
error = EACCES;
goto out;
}
}
aiov.iov_base = cbuf;
aiov.iov_len = bcount;
(void) VOP_RWLOCK(vp, rwflag, NULL);
if (vp->v_type == VREG && fileoff == (u_offset_t)maxoff) {
struct vattr va;
va.va_mask = AT_SIZE;
if ((error = VOP_GETATTR(vp, &va, 0, fp->f_cred, NULL))) {
VOP_RWUNLOCK(vp, rwflag, NULL);
goto out;
}
VOP_RWUNLOCK(vp, rwflag, NULL);
/*
* We have to return EOF if fileoff is >= file size.
*/
if (fileoff >= va.va_size) {
bcount = 0;
goto out;
}
/*
* File is greater than or equal to maxoff and therefore
* we return EOVERFLOW.
*/
error = EOVERFLOW;
goto out;
}
auio.uio_loffset = fileoff;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = bcount;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = MAXOFFSET_T;
auio.uio_fmode = fflag;
auio.uio_extflg = UIO_COPY_CACHED;
ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
/* If read sync is not asked for, filter sync flags */
if ((ioflag & FRSYNC) == 0)
ioflag &= ~(FSYNC|FDSYNC);
error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
bcount -= auio.uio_resid;
CPU_STATS_ENTER_K();
cp = CPU;
CPU_STATS_ADDQ(cp, sys, sysread, 1);
CPU_STATS_ADDQ(cp, sys, readch, (ulong_t)bcount);
CPU_STATS_EXIT_K();
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)bcount;
VOP_RWUNLOCK(vp, rwflag, NULL);
if (error == EINTR && bcount != 0)
error = 0;
out:
if (in_crit)
nbl_end_crit(vp);
releasef(fdes);
if (error)
return (set_errno(error));
return (bcount);
}
ssize_t
pwrite(int fdes, void *cbuf, size_t count, off_t offset)
{
struct uio auio;
struct iovec aiov;
file_t *fp;
register vnode_t *vp;
struct cpu *cp;
int fflag, ioflag, rwflag;
ssize_t bcount;
int error = 0;
u_offset_t fileoff = (u_offset_t)(ulong_t)offset;
#ifdef _SYSCALL32_IMPL
u_offset_t maxoff = get_udatamodel() == DATAMODEL_ILP32 ?
MAXOFF32_T : MAXOFFSET_T;
#else
const u_offset_t maxoff = MAXOFF32_T;
#endif
int in_crit = 0;
if ((bcount = (ssize_t)count) < 0)
return (set_errno(EINVAL));
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
if (((fflag = fp->f_flag) & (FWRITE)) == 0) {
error = EBADF;
goto out;
}
rwflag = 1;
vp = fp->f_vnode;
if (vp->v_type == VREG) {
if (bcount == 0)
goto out;
/*
* return EINVAL for offsets that cannot be
* represented in an off_t.
*/
if (fileoff > maxoff) {
error = EINVAL;
goto out;
}
/*
* Take appropriate action if we are trying to write above the
* resource limit.
*/
if (fileoff >= curproc->p_fsz_ctl) {
mutex_enter(&curproc->p_lock);
(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
curproc->p_rctls, curproc, RCA_UNSAFE_SIGINFO);
mutex_exit(&curproc->p_lock);
error = EFBIG;
goto out;
}
/*
* Don't allow pwrite to cause file sizes to exceed
* maxoff.
*/
if (fileoff == maxoff) {
error = EFBIG;
goto out;
}
if (fileoff + count > maxoff)
bcount = (ssize_t)((u_offset_t)maxoff - fileoff);
} else if (vp->v_type == VFIFO) {
error = ESPIPE;
goto out;
}
/*
* We have to enter the critical region before calling VOP_RWLOCK
* to avoid a deadlock with ufs.
*/
if (nbl_need_check(vp)) {
int svmand;
nbl_start_crit(vp, RW_READER);
in_crit = 1;
error = nbl_svmand(vp, fp->f_cred, &svmand);
if (error != 0)
goto out;
if (nbl_conflict(vp, NBL_WRITE, fileoff, bcount, svmand,
NULL)) {
error = EACCES;
goto out;
}
}
aiov.iov_base = cbuf;
aiov.iov_len = bcount;
(void) VOP_RWLOCK(vp, rwflag, NULL);
auio.uio_loffset = fileoff;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = bcount;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = curproc->p_fsz_ctl;
auio.uio_fmode = fflag;
auio.uio_extflg = UIO_COPY_CACHED;
/*
* The SUSv4 POSIX specification states:
* The pwrite() function shall be equivalent to write(), except
* that it writes into a given position and does not change
* the file offset (regardless of whether O_APPEND is set).
* To make this be true, we omit the FAPPEND flag from ioflag.
*/
ioflag = auio.uio_fmode & (FSYNC|FDSYNC|FRSYNC);
error = VOP_WRITE(vp, &auio, ioflag, fp->f_cred, NULL);
bcount -= auio.uio_resid;
CPU_STATS_ENTER_K();
cp = CPU;
CPU_STATS_ADDQ(cp, sys, syswrite, 1);
CPU_STATS_ADDQ(cp, sys, writech, (ulong_t)bcount);
CPU_STATS_EXIT_K();
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)bcount;
VOP_RWUNLOCK(vp, rwflag, NULL);
if (error == EINTR && bcount != 0)
error = 0;
out:
if (in_crit)
nbl_end_crit(vp);
releasef(fdes);
if (error)
return (set_errno(error));
return (bcount);
}
ssize_t
readv(int fdes, struct iovec *iovp, int iovcnt)
{
struct uio auio;
struct iovec buf[IOV_MAX_STACK], *aiov = buf;
int aiovlen = 0;
file_t *fp;
register vnode_t *vp;
struct cpu *cp;
int fflag, ioflag, rwflag;
ssize_t count, bcount;
int error = 0;
int i;
u_offset_t fileoff;
int in_crit = 0;
if (iovcnt <= 0 || iovcnt > IOV_MAX)
return (set_errno(EINVAL));
if (iovcnt > IOV_MAX_STACK) {
aiovlen = iovcnt * sizeof (iovec_t);
aiov = kmem_alloc(aiovlen, KM_SLEEP);
}
#ifdef _SYSCALL32_IMPL
/*
* 32-bit callers need to have their iovec expanded,
* while ensuring that they can't move more than 2Gbytes
* of data in a single call.
*/
if (get_udatamodel() == DATAMODEL_ILP32) {
struct iovec32 buf32[IOV_MAX_STACK], *aiov32 = buf32;
int aiov32len;
ssize32_t count32;
aiov32len = iovcnt * sizeof (iovec32_t);
if (aiovlen != 0)
aiov32 = kmem_alloc(aiov32len, KM_SLEEP);
if (copyin(iovp, aiov32, aiov32len)) {
if (aiovlen != 0) {
kmem_free(aiov32, aiov32len);
kmem_free(aiov, aiovlen);
}
return (set_errno(EFAULT));
}
count32 = 0;
for (i = 0; i < iovcnt; i++) {
ssize32_t iovlen32 = aiov32[i].iov_len;
count32 += iovlen32;
if (iovlen32 < 0 || count32 < 0) {
if (aiovlen != 0) {
kmem_free(aiov32, aiov32len);
kmem_free(aiov, aiovlen);
}
return (set_errno(EINVAL));
}
aiov[i].iov_len = iovlen32;
aiov[i].iov_base =
(caddr_t)(uintptr_t)aiov32[i].iov_base;
}
if (aiovlen != 0)
kmem_free(aiov32, aiov32len);
} else
#endif
if (copyin(iovp, aiov, iovcnt * sizeof (iovec_t))) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EFAULT));
}
count = 0;
for (i = 0; i < iovcnt; i++) {
ssize_t iovlen = aiov[i].iov_len;
count += iovlen;
if (iovlen < 0 || count < 0) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EINVAL));
}
}
if ((fp = getf(fdes)) == NULL) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EBADF));
}
if (((fflag = fp->f_flag) & FREAD) == 0) {
error = EBADF;
goto out;
}
vp = fp->f_vnode;
if (vp->v_type == VREG && count == 0) {
goto out;
}
rwflag = 0;
/*
* We have to enter the critical region before calling VOP_RWLOCK
* to avoid a deadlock with ufs.
*/
if (nbl_need_check(vp)) {
int svmand;
nbl_start_crit(vp, RW_READER);
in_crit = 1;
error = nbl_svmand(vp, fp->f_cred, &svmand);
if (error != 0)
goto out;
if (nbl_conflict(vp, NBL_READ, fp->f_offset, count, svmand,
NULL)) {
error = EACCES;
goto out;
}
}
(void) VOP_RWLOCK(vp, rwflag, NULL);
fileoff = fp->f_offset;
/*
* Behaviour is same as read. Please see comments in read.
*/
if ((vp->v_type == VREG) && (fileoff >= OFFSET_MAX(fp))) {
struct vattr va;
va.va_mask = AT_SIZE;
if ((error = VOP_GETATTR(vp, &va, 0, fp->f_cred, NULL))) {
VOP_RWUNLOCK(vp, rwflag, NULL);
goto out;
}
if (fileoff >= va.va_size) {
VOP_RWUNLOCK(vp, rwflag, NULL);
count = 0;
goto out;
} else {
VOP_RWUNLOCK(vp, rwflag, NULL);
error = EOVERFLOW;
goto out;
}
}
if ((vp->v_type == VREG) && (fileoff + count > OFFSET_MAX(fp))) {
count = (ssize_t)(OFFSET_MAX(fp) - fileoff);
}
auio.uio_loffset = fileoff;
auio.uio_iov = aiov;
auio.uio_iovcnt = iovcnt;
auio.uio_resid = bcount = count;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = MAXOFFSET_T;
auio.uio_fmode = fflag;
if (bcount <= copyout_max_cached)
auio.uio_extflg = UIO_COPY_CACHED;
else
auio.uio_extflg = UIO_COPY_DEFAULT;
ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
/* If read sync is not asked for, filter sync flags */
if ((ioflag & FRSYNC) == 0)
ioflag &= ~(FSYNC|FDSYNC);
error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
count -= auio.uio_resid;
CPU_STATS_ENTER_K();
cp = CPU;
CPU_STATS_ADDQ(cp, sys, sysread, 1);
CPU_STATS_ADDQ(cp, sys, readch, (ulong_t)count);
CPU_STATS_EXIT_K();
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)count;
if (vp->v_type == VFIFO) /* Backward compatibility */
fp->f_offset = count;
else if (((fp->f_flag & FAPPEND) == 0) ||
(vp->v_type != VREG) || (bcount != 0)) /* POSIX */
fp->f_offset = auio.uio_loffset;
VOP_RWUNLOCK(vp, rwflag, NULL);
if (error == EINTR && count != 0)
error = 0;
out:
if (in_crit)
nbl_end_crit(vp);
releasef(fdes);
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
if (error)
return (set_errno(error));
return (count);
}
ssize_t
writev(int fdes, struct iovec *iovp, int iovcnt)
{
struct uio auio;
struct iovec buf[IOV_MAX_STACK], *aiov = buf;
int aiovlen = 0;
file_t *fp;
register vnode_t *vp;
struct cpu *cp;
int fflag, ioflag, rwflag;
ssize_t count, bcount;
int error = 0;
int i;
u_offset_t fileoff;
int in_crit = 0;
if (iovcnt <= 0 || iovcnt > IOV_MAX)
return (set_errno(EINVAL));
if (iovcnt > IOV_MAX_STACK) {
aiovlen = iovcnt * sizeof (iovec_t);
aiov = kmem_alloc(aiovlen, KM_SLEEP);
}
#ifdef _SYSCALL32_IMPL
/*
* 32-bit callers need to have their iovec expanded,
* while ensuring that they can't move more than 2Gbytes
* of data in a single call.
*/
if (get_udatamodel() == DATAMODEL_ILP32) {
struct iovec32 buf32[IOV_MAX_STACK], *aiov32 = buf32;
int aiov32len;
ssize32_t count32;
aiov32len = iovcnt * sizeof (iovec32_t);
if (aiovlen != 0)
aiov32 = kmem_alloc(aiov32len, KM_SLEEP);
if (copyin(iovp, aiov32, aiov32len)) {
if (aiovlen != 0) {
kmem_free(aiov32, aiov32len);
kmem_free(aiov, aiovlen);
}
return (set_errno(EFAULT));
}
count32 = 0;
for (i = 0; i < iovcnt; i++) {
ssize32_t iovlen = aiov32[i].iov_len;
count32 += iovlen;
if (iovlen < 0 || count32 < 0) {
if (aiovlen != 0) {
kmem_free(aiov32, aiov32len);
kmem_free(aiov, aiovlen);
}
return (set_errno(EINVAL));
}
aiov[i].iov_len = iovlen;
aiov[i].iov_base =
(caddr_t)(uintptr_t)aiov32[i].iov_base;
}
if (aiovlen != 0)
kmem_free(aiov32, aiov32len);
} else
#endif
if (copyin(iovp, aiov, iovcnt * sizeof (iovec_t))) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EFAULT));
}
count = 0;
for (i = 0; i < iovcnt; i++) {
ssize_t iovlen = aiov[i].iov_len;
count += iovlen;
if (iovlen < 0 || count < 0) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EINVAL));
}
}
if ((fp = getf(fdes)) == NULL) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EBADF));
}
if (((fflag = fp->f_flag) & FWRITE) == 0) {
error = EBADF;
goto out;
}
vp = fp->f_vnode;
if (vp->v_type == VREG && count == 0) {
goto out;
}
rwflag = 1;
/*
* We have to enter the critical region before calling VOP_RWLOCK
* to avoid a deadlock with ufs.
*/
if (nbl_need_check(vp)) {
int svmand;
nbl_start_crit(vp, RW_READER);
in_crit = 1;
error = nbl_svmand(vp, fp->f_cred, &svmand);
if (error != 0)
goto out;
if (nbl_conflict(vp, NBL_WRITE, fp->f_offset, count, svmand,
NULL)) {
error = EACCES;
goto out;
}
}
(void) VOP_RWLOCK(vp, rwflag, NULL);
fileoff = fp->f_offset;
/*
* Behaviour is same as write. Please see comments for write.
*/
if (vp->v_type == VREG) {
if (fileoff >= curproc->p_fsz_ctl) {
VOP_RWUNLOCK(vp, rwflag, NULL);
mutex_enter(&curproc->p_lock);
(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
curproc->p_rctls, curproc, RCA_UNSAFE_SIGINFO);
mutex_exit(&curproc->p_lock);
error = EFBIG;
goto out;
}
if (fileoff >= OFFSET_MAX(fp)) {
VOP_RWUNLOCK(vp, rwflag, NULL);
error = EFBIG;
goto out;
}
if (fileoff + count > OFFSET_MAX(fp))
count = (ssize_t)(OFFSET_MAX(fp) - fileoff);
}
auio.uio_loffset = fileoff;
auio.uio_iov = aiov;
auio.uio_iovcnt = iovcnt;
auio.uio_resid = bcount = count;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = curproc->p_fsz_ctl;
auio.uio_fmode = fflag;
auio.uio_extflg = UIO_COPY_DEFAULT;
ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
error = VOP_WRITE(vp, &auio, ioflag, fp->f_cred, NULL);
count -= auio.uio_resid;
CPU_STATS_ENTER_K();
cp = CPU;
CPU_STATS_ADDQ(cp, sys, syswrite, 1);
CPU_STATS_ADDQ(cp, sys, writech, (ulong_t)count);
CPU_STATS_EXIT_K();
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)count;
if (vp->v_type == VFIFO) /* Backward compatibility */
fp->f_offset = count;
else if (((fp->f_flag & FAPPEND) == 0) ||
(vp->v_type != VREG) || (bcount != 0)) /* POSIX */
fp->f_offset = auio.uio_loffset;
VOP_RWUNLOCK(vp, rwflag, NULL);
if (error == EINTR && count != 0)
error = 0;
out:
if (in_crit)
nbl_end_crit(vp);
releasef(fdes);
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
if (error)
return (set_errno(error));
return (count);
}
ssize_t
preadv(int fdes, struct iovec *iovp, int iovcnt, off_t offset,
off_t extended_offset)
{
struct uio auio;
struct iovec buf[IOV_MAX_STACK], *aiov = buf;
int aiovlen = 0;
file_t *fp;
register vnode_t *vp;
struct cpu *cp;
int fflag, ioflag, rwflag;
ssize_t count, bcount;
int error = 0;
int i;
/*
* In a 64-bit kernel, this interface supports native 64-bit
* applications as well as 32-bit applications using both standard and
* large-file access. For 32-bit large-file aware applications, the
* offset is passed as two parameters which are joined into the actual
* offset used. The 64-bit libc always passes 0 for the extended_offset.
* Note that off_t is a signed value, but the preadv/pwritev API treats
* the offset as a position in the file for the operation, so passing
* a negative value will likely fail the maximum offset checks below
* because we convert it to an unsigned value which will be larger than
* the maximum valid offset.
*/
#if defined(_SYSCALL32_IMPL) || defined(_ILP32)
u_offset_t fileoff = ((u_offset_t)extended_offset << 32) |
(u_offset_t)offset;
#else /* _SYSCALL32_IMPL || _ILP32 */
u_offset_t fileoff = (u_offset_t)(ulong_t)offset;
#endif /* _SYSCALL32_IMPR || _ILP32 */
int in_crit = 0;
if (iovcnt <= 0 || iovcnt > IOV_MAX)
return (set_errno(EINVAL));
if (iovcnt > IOV_MAX_STACK) {
aiovlen = iovcnt * sizeof (iovec_t);
aiov = kmem_alloc(aiovlen, KM_SLEEP);
}
#ifdef _SYSCALL32_IMPL
/*
* 32-bit callers need to have their iovec expanded,
* while ensuring that they can't move more than 2Gbytes
* of data in a single call.
*/
if (get_udatamodel() == DATAMODEL_ILP32) {
struct iovec32 buf32[IOV_MAX_STACK], *aiov32 = buf32;
int aiov32len;
ssize32_t count32;
aiov32len = iovcnt * sizeof (iovec32_t);
if (aiovlen != 0)
aiov32 = kmem_alloc(aiov32len, KM_SLEEP);
if (copyin(iovp, aiov32, aiov32len)) {
if (aiovlen != 0) {
kmem_free(aiov32, aiov32len);
kmem_free(aiov, aiovlen);
}
return (set_errno(EFAULT));
}
count32 = 0;
for (i = 0; i < iovcnt; i++) {
ssize32_t iovlen32 = aiov32[i].iov_len;
count32 += iovlen32;
if (iovlen32 < 0 || count32 < 0) {
if (aiovlen != 0) {
kmem_free(aiov32, aiov32len);
kmem_free(aiov, aiovlen);
}
return (set_errno(EINVAL));
}
aiov[i].iov_len = iovlen32;
aiov[i].iov_base =
(caddr_t)(uintptr_t)aiov32[i].iov_base;
}
if (aiovlen != 0)
kmem_free(aiov32, aiov32len);
} else
#endif /* _SYSCALL32_IMPL */
if (copyin(iovp, aiov, iovcnt * sizeof (iovec_t))) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EFAULT));
}
count = 0;
for (i = 0; i < iovcnt; i++) {
ssize_t iovlen = aiov[i].iov_len;
count += iovlen;
if (iovlen < 0 || count < 0) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EINVAL));
}
}
if ((bcount = count) < 0) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EINVAL));
}
if ((fp = getf(fdes)) == NULL) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EBADF));
}
if (((fflag = fp->f_flag) & FREAD) == 0) {
error = EBADF;
goto out;
}
vp = fp->f_vnode;
rwflag = 0;
/*
* Behaviour is same as read(2). Please see comments in read above.
*/
if (vp->v_type == VREG) {
if (bcount == 0)
goto out;
/* Handle offset past maximum offset allowed for file. */
if (fileoff >= OFFSET_MAX(fp)) {
struct vattr va;
va.va_mask = AT_SIZE;
error = VOP_GETATTR(vp, &va, 0, fp->f_cred, NULL);
if (error == 0) {
if (fileoff >= va.va_size) {
count = 0;
} else {
error = EOVERFLOW;
}
}
goto out;
}
ASSERT(bcount == count);
/* Note: modified count used in nbl_conflict() call below. */
if ((fileoff + count) > OFFSET_MAX(fp))
count = (ssize_t)(OFFSET_MAX(fp) - fileoff);
} else if (vp->v_type == VFIFO) {
error = ESPIPE;
goto out;
}
/*
* We have to enter the critical region before calling VOP_RWLOCK
* to avoid a deadlock with ufs.
*/
if (nbl_need_check(vp)) {
int svmand;
nbl_start_crit(vp, RW_READER);
in_crit = 1;
error = nbl_svmand(vp, fp->f_cred, &svmand);
if (error != 0)
goto out;
if (nbl_conflict(vp, NBL_WRITE, fileoff, count, svmand, NULL)) {
error = EACCES;
goto out;
}
}
(void) VOP_RWLOCK(vp, rwflag, NULL);
auio.uio_loffset = fileoff;
auio.uio_iov = aiov;
auio.uio_iovcnt = iovcnt;
auio.uio_resid = bcount = count;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = MAXOFFSET_T;
auio.uio_fmode = fflag;
if (bcount <= copyout_max_cached)
auio.uio_extflg = UIO_COPY_CACHED;
else
auio.uio_extflg = UIO_COPY_DEFAULT;
ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
count -= auio.uio_resid;
CPU_STATS_ENTER_K();
cp = CPU;
CPU_STATS_ADDQ(cp, sys, sysread, 1);
CPU_STATS_ADDQ(cp, sys, readch, (ulong_t)count);
CPU_STATS_EXIT_K();
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)count;
VOP_RWUNLOCK(vp, rwflag, NULL);
if (error == EINTR && count != 0)
error = 0;
out:
if (in_crit)
nbl_end_crit(vp);
releasef(fdes);
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
if (error)
return (set_errno(error));
return (count);
}
ssize_t
pwritev(int fdes, struct iovec *iovp, int iovcnt, off_t offset,
off_t extended_offset)
{
struct uio auio;
struct iovec buf[IOV_MAX_STACK], *aiov = buf;
int aiovlen = 0;
file_t *fp;
register vnode_t *vp;
struct cpu *cp;
int fflag, ioflag, rwflag;
ssize_t count, bcount;
int error = 0;
int i;
/*
* See the comment in preadv for how the offset is handled.
*/
#if defined(_SYSCALL32_IMPL) || defined(_ILP32)
u_offset_t fileoff = ((u_offset_t)extended_offset << 32) |
(u_offset_t)offset;
#else /* _SYSCALL32_IMPL || _ILP32 */
u_offset_t fileoff = (u_offset_t)(ulong_t)offset;
#endif /* _SYSCALL32_IMPR || _ILP32 */
int in_crit = 0;
if (iovcnt <= 0 || iovcnt > IOV_MAX)
return (set_errno(EINVAL));
if (iovcnt > IOV_MAX_STACK) {
aiovlen = iovcnt * sizeof (iovec_t);
aiov = kmem_alloc(aiovlen, KM_SLEEP);
}
#ifdef _SYSCALL32_IMPL
/*
* 32-bit callers need to have their iovec expanded,
* while ensuring that they can't move more than 2Gbytes
* of data in a single call.
*/
if (get_udatamodel() == DATAMODEL_ILP32) {
struct iovec32 buf32[IOV_MAX_STACK], *aiov32 = buf32;
int aiov32len;
ssize32_t count32;
aiov32len = iovcnt * sizeof (iovec32_t);
if (aiovlen != 0)
aiov32 = kmem_alloc(aiov32len, KM_SLEEP);
if (copyin(iovp, aiov32, aiov32len)) {
if (aiovlen != 0) {
kmem_free(aiov32, aiov32len);
kmem_free(aiov, aiovlen);
}
return (set_errno(EFAULT));
}
count32 = 0;
for (i = 0; i < iovcnt; i++) {
ssize32_t iovlen32 = aiov32[i].iov_len;
count32 += iovlen32;
if (iovlen32 < 0 || count32 < 0) {
if (aiovlen != 0) {
kmem_free(aiov32, aiov32len);
kmem_free(aiov, aiovlen);
}
return (set_errno(EINVAL));
}
aiov[i].iov_len = iovlen32;
aiov[i].iov_base =
(caddr_t)(uintptr_t)aiov32[i].iov_base;
}
if (aiovlen != 0)
kmem_free(aiov32, aiov32len);
} else
#endif /* _SYSCALL32_IMPL */
if (copyin(iovp, aiov, iovcnt * sizeof (iovec_t))) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EFAULT));
}
count = 0;
for (i = 0; i < iovcnt; i++) {
ssize_t iovlen = aiov[i].iov_len;
count += iovlen;
if (iovlen < 0 || count < 0) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EINVAL));
}
}
if ((bcount = count) < 0) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EINVAL));
}
if ((fp = getf(fdes)) == NULL) {
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
return (set_errno(EBADF));
}
if (((fflag = fp->f_flag) & FWRITE) == 0) {
error = EBADF;
goto out;
}
vp = fp->f_vnode;
rwflag = 1;
/*
* The kernel's write(2) code checks OFFSET_MAX and the rctl, and
* returns EFBIG when fileoff exceeds either limit. We do the same.
*/
if (vp->v_type == VREG) {
if (bcount == 0)
goto out;
/*
* Don't allow pwritev to cause file size to exceed the proper
* offset limit.
*/
if (fileoff >= OFFSET_MAX(fp)) {
error = EFBIG;
goto out;
}
/*
* Take appropriate action if we are trying
* to write above the resource limit.
*/
if (fileoff >= curproc->p_fsz_ctl) {
mutex_enter(&curproc->p_lock);
/*
* Return value ignored because it lists
* actions taken, but we are in an error case.
* We don't have any actions that depend on
* what could happen in this call, so we ignore
* the return value.
*/
(void) rctl_action(
rctlproc_legacy[RLIMIT_FSIZE],
curproc->p_rctls, curproc,
RCA_UNSAFE_SIGINFO);
mutex_exit(&curproc->p_lock);
error = EFBIG;
goto out;
}
ASSERT(bcount == count);
/* Note: modified count used in nbl_conflict() call below. */
if ((fileoff + count) > OFFSET_MAX(fp))
count = (ssize_t)(OFFSET_MAX(fp) - fileoff);
} else if (vp->v_type == VFIFO) {
error = ESPIPE;
goto out;
}
/*
* We have to enter the critical region before calling VOP_RWLOCK
* to avoid a deadlock with ufs.
*/
if (nbl_need_check(vp)) {
int svmand;
nbl_start_crit(vp, RW_READER);
in_crit = 1;
error = nbl_svmand(vp, fp->f_cred, &svmand);
if (error != 0)
goto out;
if (nbl_conflict(vp, NBL_WRITE, fileoff, count, svmand, NULL)) {
error = EACCES;
goto out;
}
}
(void) VOP_RWLOCK(vp, rwflag, NULL);
auio.uio_loffset = fileoff;
auio.uio_iov = aiov;
auio.uio_iovcnt = iovcnt;
auio.uio_resid = bcount = count;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = curproc->p_fsz_ctl;
auio.uio_fmode = fflag;
auio.uio_extflg = UIO_COPY_CACHED;
ioflag = auio.uio_fmode & (FSYNC|FDSYNC|FRSYNC);
error = VOP_WRITE(vp, &auio, ioflag, fp->f_cred, NULL);
count -= auio.uio_resid;
CPU_STATS_ENTER_K();
cp = CPU;
CPU_STATS_ADDQ(cp, sys, syswrite, 1);
CPU_STATS_ADDQ(cp, sys, writech, (ulong_t)count);
CPU_STATS_EXIT_K();
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)count;
VOP_RWUNLOCK(vp, rwflag, NULL);
if (error == EINTR && count != 0)
error = 0;
out:
if (in_crit)
nbl_end_crit(vp);
releasef(fdes);
if (aiovlen != 0)
kmem_free(aiov, aiovlen);
if (error)
return (set_errno(error));
return (count);
}
#if defined(_SYSCALL32_IMPL) || defined(_ILP32)
/*
* This syscall supplies 64-bit file offsets to 32-bit applications only.
*/
ssize32_t
pread64(int fdes, void *cbuf, size32_t count, uint32_t offset_1,
uint32_t offset_2)
{
struct uio auio;
struct iovec aiov;
file_t *fp;
register vnode_t *vp;
struct cpu *cp;
int fflag, ioflag, rwflag;
ssize_t bcount;
int error = 0;
u_offset_t fileoff;
int in_crit = 0;
#if defined(_LITTLE_ENDIAN)
fileoff = ((u_offset_t)offset_2 << 32) | (u_offset_t)offset_1;
#else
fileoff = ((u_offset_t)offset_1 << 32) | (u_offset_t)offset_2;
#endif
if ((bcount = (ssize_t)count) < 0 || bcount > INT32_MAX)
return (set_errno(EINVAL));
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
if (((fflag = fp->f_flag) & (FREAD)) == 0) {
error = EBADF;
goto out;
}
rwflag = 0;
vp = fp->f_vnode;
if (vp->v_type == VREG) {
if (bcount == 0)
goto out;
/*
* Same as pread. See comments in pread.
*/
if (fileoff > MAXOFFSET_T) {
error = EINVAL;
goto out;
}
if (fileoff + bcount > MAXOFFSET_T)
bcount = (ssize_t)(MAXOFFSET_T - fileoff);
} else if (vp->v_type == VFIFO) {
error = ESPIPE;
goto out;
}
/*
* We have to enter the critical region before calling VOP_RWLOCK
* to avoid a deadlock with ufs.
*/
if (nbl_need_check(vp)) {
int svmand;
nbl_start_crit(vp, RW_READER);
in_crit = 1;
error = nbl_svmand(vp, fp->f_cred, &svmand);
if (error != 0)
goto out;
if (nbl_conflict(vp, NBL_READ, fileoff, bcount, svmand,
NULL)) {
error = EACCES;
goto out;
}
}
aiov.iov_base = cbuf;
aiov.iov_len = bcount;
(void) VOP_RWLOCK(vp, rwflag, NULL);
auio.uio_loffset = fileoff;
/*
* Note: File size can never be greater than MAXOFFSET_T.
* If ever we start supporting 128 bit files the code
* similar to the one in pread at this place should be here.
* Here we avoid the unnecessary VOP_GETATTR() when we
* know that fileoff == MAXOFFSET_T implies that it is always
* greater than or equal to file size.
*/
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = bcount;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = MAXOFFSET_T;
auio.uio_fmode = fflag;
auio.uio_extflg = UIO_COPY_CACHED;
ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
/* If read sync is not asked for, filter sync flags */
if ((ioflag & FRSYNC) == 0)
ioflag &= ~(FSYNC|FDSYNC);
error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
bcount -= auio.uio_resid;
CPU_STATS_ENTER_K();
cp = CPU;
CPU_STATS_ADDQ(cp, sys, sysread, 1);
CPU_STATS_ADDQ(cp, sys, readch, (ulong_t)bcount);
CPU_STATS_EXIT_K();
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)bcount;
VOP_RWUNLOCK(vp, rwflag, NULL);
if (error == EINTR && bcount != 0)
error = 0;
out:
if (in_crit)
nbl_end_crit(vp);
releasef(fdes);
if (error)
return (set_errno(error));
return (bcount);
}
/*
* This syscall supplies 64-bit file offsets to 32-bit applications only.
*/
ssize32_t
pwrite64(int fdes, void *cbuf, size32_t count, uint32_t offset_1,
uint32_t offset_2)
{
struct uio auio;
struct iovec aiov;
file_t *fp;
register vnode_t *vp;
struct cpu *cp;
int fflag, ioflag, rwflag;
ssize_t bcount;
int error = 0;
u_offset_t fileoff;
int in_crit = 0;
#if defined(_LITTLE_ENDIAN)
fileoff = ((u_offset_t)offset_2 << 32) | (u_offset_t)offset_1;
#else
fileoff = ((u_offset_t)offset_1 << 32) | (u_offset_t)offset_2;
#endif
if ((bcount = (ssize_t)count) < 0 || bcount > INT32_MAX)
return (set_errno(EINVAL));
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
if (((fflag = fp->f_flag) & (FWRITE)) == 0) {
error = EBADF;
goto out;
}
rwflag = 1;
vp = fp->f_vnode;
if (vp->v_type == VREG) {
if (bcount == 0)
goto out;
/*
* See comments in pwrite.
*/
if (fileoff > MAXOFFSET_T) {
error = EINVAL;
goto out;
}
if (fileoff >= curproc->p_fsz_ctl) {
mutex_enter(&curproc->p_lock);
(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
curproc->p_rctls, curproc, RCA_SAFE);
mutex_exit(&curproc->p_lock);
error = EFBIG;
goto out;
}
if (fileoff == MAXOFFSET_T) {
error = EFBIG;
goto out;
}
if (fileoff + bcount > MAXOFFSET_T)
bcount = (ssize_t)((u_offset_t)MAXOFFSET_T - fileoff);
} else if (vp->v_type == VFIFO) {
error = ESPIPE;
goto out;
}
/*
* We have to enter the critical region before calling VOP_RWLOCK
* to avoid a deadlock with ufs.
*/
if (nbl_need_check(vp)) {
int svmand;
nbl_start_crit(vp, RW_READER);
in_crit = 1;
error = nbl_svmand(vp, fp->f_cred, &svmand);
if (error != 0)
goto out;
if (nbl_conflict(vp, NBL_WRITE, fileoff, bcount, svmand,
NULL)) {
error = EACCES;
goto out;
}
}
aiov.iov_base = cbuf;
aiov.iov_len = bcount;
(void) VOP_RWLOCK(vp, rwflag, NULL);
auio.uio_loffset = fileoff;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = bcount;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = curproc->p_fsz_ctl;
auio.uio_fmode = fflag;
auio.uio_extflg = UIO_COPY_CACHED;
/*
* The SUSv4 POSIX specification states:
* The pwrite() function shall be equivalent to write(), except
* that it writes into a given position and does not change
* the file offset (regardless of whether O_APPEND is set).
* To make this be true, we omit the FAPPEND flag from ioflag.
*/
ioflag = auio.uio_fmode & (FSYNC|FDSYNC|FRSYNC);
error = VOP_WRITE(vp, &auio, ioflag, fp->f_cred, NULL);
bcount -= auio.uio_resid;
CPU_STATS_ENTER_K();
cp = CPU;
CPU_STATS_ADDQ(cp, sys, syswrite, 1);
CPU_STATS_ADDQ(cp, sys, writech, (ulong_t)bcount);
CPU_STATS_EXIT_K();
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)bcount;
VOP_RWUNLOCK(vp, rwflag, NULL);
if (error == EINTR && bcount != 0)
error = 0;
out:
if (in_crit)
nbl_end_crit(vp);
releasef(fdes);
if (error)
return (set_errno(error));
return (bcount);
}
#endif /* _SYSCALL32_IMPL || _ILP32 */
#ifdef _SYSCALL32_IMPL
/*
* Tail-call elimination of xxx32() down to xxx()
*
* A number of xxx32 system calls take a len (or count) argument and
* return a number in the range [0,len] or -1 on error.
* Given an ssize32_t input len, the downcall xxx() will return
* a 64-bit value that is -1 or in the range [0,len] which actually
* is a proper return value for the xxx32 call. So even if the xxx32
* calls can be considered as returning a ssize32_t, they are currently
* declared as returning a ssize_t as this enables tail-call elimination.
*
* The cast of len (or count) to ssize32_t is needed to ensure we pass
* down negative input values as such and let the downcall handle error
* reporting. Functions covered by this comments are:
*
* rw.c: read32, write32, pread32, pwrite32, readv32, writev32.
* socksyscall.c: recv32, recvfrom32, send32, sendto32.
* readlink.c: readlink32.
*/
ssize_t
read32(int32_t fdes, caddr32_t cbuf, size32_t count)
{
return (read(fdes,
(void *)(uintptr_t)cbuf, (ssize32_t)count));
}
ssize_t
write32(int32_t fdes, caddr32_t cbuf, size32_t count)
{
return (write(fdes,
(void *)(uintptr_t)cbuf, (ssize32_t)count));
}
ssize_t
pread32(int32_t fdes, caddr32_t cbuf, size32_t count, off32_t offset)
{
return (pread(fdes,
(void *)(uintptr_t)cbuf, (ssize32_t)count,
(off_t)(uint32_t)offset));
}
ssize_t
pwrite32(int32_t fdes, caddr32_t cbuf, size32_t count, off32_t offset)
{
return (pwrite(fdes,
(void *)(uintptr_t)cbuf, (ssize32_t)count,
(off_t)(uint32_t)offset));
}
ssize_t
readv32(int32_t fdes, caddr32_t iovp, int32_t iovcnt)
{
return (readv(fdes, (void *)(uintptr_t)iovp, iovcnt));
}
ssize_t
writev32(int32_t fdes, caddr32_t iovp, int32_t iovcnt)
{
return (writev(fdes, (void *)(uintptr_t)iovp, iovcnt));
}
#endif /* _SYSCALL32_IMPL */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Inter-Process Communication Semaphore Facility.
*
* See os/ipc.c for a description of common IPC functionality.
*
* Resource controls
* -----------------
*
* Control: zone.max-sem-ids (rc_zone_semmni)
* Description: Maximum number of semaphore ids allowed a zone.
*
* When semget() is used to allocate a semaphore set, one id is
* allocated. If the id allocation doesn't succeed, semget() fails
* and errno is set to ENOSPC. Upon successful semctl(, IPC_RMID)
* the id is deallocated.
*
* Control: project.max-sem-ids (rc_project_semmni)
* Description: Maximum number of semaphore ids allowed a project.
*
* When semget() is used to allocate a semaphore set, one id is
* allocated. If the id allocation doesn't succeed, semget() fails
* and errno is set to ENOSPC. Upon successful semctl(, IPC_RMID)
* the id is deallocated.
*
* Control: process.max-sem-nsems (rc_process_semmsl)
* Description: Maximum number of semaphores allowed per semaphore set.
*
* When semget() is used to allocate a semaphore set, the size of the
* set is compared with this limit. If the number of semaphores
* exceeds the limit, semget() fails and errno is set to EINVAL.
*
* Control: process.max-sem-ops (rc_process_semopm)
* Description: Maximum number of semaphore operations allowed per
* semop call.
*
* When semget() successfully allocates a semaphore set, the minimum
* enforced value of this limit is used to initialize the
* "system-imposed maximum" number of operations a semop() call for
* this set can perform.
*
* Undo structures
* ---------------
*
* Removing the undo structure tunables involved a serious redesign of
* how they were implemented. There is now one undo structure for
* every process/semaphore array combination (lazily allocated, of
* course), and each is equal in size to the semaphore it corresponds
* to. To avoid scalability and performance problems, the undo
* structures are stored in two places: a per-process AVL tree sorted
* by ksemid pointer (p_semacct, protected by p_lock) and an unsorted
* per-semaphore linked list (sem_undos, protected by the semaphore's
* ID lock). The former is used by semop, where a lookup is performed
* once and cached if SEM_UNDO is specified for any of the operations,
* and at process exit where the undoable operations are rolled back.
* The latter is used when removing the semaphore, so the undo
* structures can be removed from the appropriate processes' trees.
*
* The undo structure itself contains pointers to the ksemid and proc
* to which it corresponds, a list node, an AVL node, and an array of
* adjust-on-exit (AOE) values. When an undo structure is allocated it
* is immediately added to both the process's tree and the semaphore's
* list. Lastly, the reference count on the semaphore is increased.
*
* Avoiding a lock ordering violation between p_lock and the ID lock,
* wont to occur when there is a race between a process exiting and the
* removal of a semaphore, mandates the delicate dance that exists
* between semexit and sem_rmid.
*
* sem_rmid, holding the ID lock, iterates through all undo structures
* and for each takes the appropriate process's p_lock and checks to
* see if p_semacct is NULL. If it is, it skips that undo structure
* and continues to the next. Otherwise, it removes the undo structure
* from both the AVL tree and the semaphore's list, and releases the
* hold that the undo structure had on the semaphore.
*
* The important other half of this is semexit, which will immediately
* take p_lock, obtain the AVL pointer, clear p_semacct, and drop
* p_lock. From this point on it is semexit's responsibility to clean
* up all undo structures found in the tree -- a coexecuting sem_rmid
* will see the NULL p_semacct and skip that undo structure. It walks
* the AVL tree (using avl_destroy_nodes) and for each undo structure
* takes the appropriate semaphore's ID lock (always legal since the
* undo structure has a hold on the semaphore), updates all semaphores
* with non-zero AOE values, and removes the structure from the
* semaphore's list. It then drops the structure's reference on the
* semaphore, drops the ID lock, and frees the undo structure.
*/
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/vmem.h>
#include <sys/kmem.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/ipc.h>
#include <sys/ipc_impl.h>
#include <sys/sem.h>
#include <sys/sem_impl.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/cpuvar.h>
#include <sys/debug.h>
#include <sys/var.h>
#include <sys/cmn_err.h>
#include <sys/modctl.h>
#include <sys/syscall.h>
#include <sys/avl.h>
#include <sys/list.h>
#include <sys/zone.h>
#include <c2/audit.h>
extern rctl_hndl_t rc_zone_semmni;
extern rctl_hndl_t rc_project_semmni;
extern rctl_hndl_t rc_process_semmsl;
extern rctl_hndl_t rc_process_semopm;
static ipc_service_t *sem_svc;
static zone_key_t sem_zone_key;
/*
* The following tunables are obsolete. Though for compatibility we
* still read and interpret seminfo_semmsl, seminfo_semopm and
* seminfo_semmni (see os/project.c and os/rctl_proc.c), the preferred
* mechanism for administrating the IPC Semaphore facility is through
* the resource controls described at the top of this file.
*/
int seminfo_semaem = 16384; /* (obsolete) */
int seminfo_semmap = 10; /* (obsolete) */
int seminfo_semmni = 10; /* (obsolete) */
int seminfo_semmns = 60; /* (obsolete) */
int seminfo_semmnu = 30; /* (obsolete) */
int seminfo_semmsl = 25; /* (obsolete) */
int seminfo_semopm = 10; /* (obsolete) */
int seminfo_semume = 10; /* (obsolete) */
int seminfo_semusz = 96; /* (obsolete) */
int seminfo_semvmx = 32767; /* (obsolete) */
#define SEM_MAXUCOPS 4096 /* max # of unchecked ops per semop call */
#define SEM_UNDOSZ(n) (sizeof (struct sem_undo) + (n - 1) * sizeof (int))
static int semsys(int opcode, uintptr_t a0, uintptr_t a1,
uintptr_t a2, uintptr_t a3);
static void sem_dtor(kipc_perm_t *);
static void sem_rmid(kipc_perm_t *);
static void sem_remove_zone(zoneid_t, void *);
static struct sysent ipcsem_sysent = {
5,
SE_NOUNLOAD | SE_ARGC | SE_32RVAL1,
semsys
};
/*
* Module linkage information for the kernel.
*/
static struct modlsys modlsys = {
&mod_syscallops, "System V semaphore facility", &ipcsem_sysent
};
#ifdef _SYSCALL32_IMPL
static struct modlsys modlsys32 = {
&mod_syscallops32, "32-bit System V semaphore facility", &ipcsem_sysent
};
#endif
static struct modlinkage modlinkage = {
MODREV_1,
&modlsys,
#ifdef _SYSCALL32_IMPL
&modlsys32,
#endif
NULL
};
int
_init(void)
{
int result;
sem_svc = ipcs_create("semids", rc_project_semmni, rc_zone_semmni,
sizeof (ksemid_t), sem_dtor, sem_rmid, AT_IPC_SEM,
offsetof(ipc_rqty_t, ipcq_semmni));
zone_key_create(&sem_zone_key, NULL, sem_remove_zone, NULL);
if ((result = mod_install(&modlinkage)) == 0)
return (0);
(void) zone_key_delete(sem_zone_key);
ipcs_destroy(sem_svc);
return (result);
}
int
_fini(void)
{
return (EBUSY);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static void
sem_dtor(kipc_perm_t *perm)
{
ksemid_t *sp = (ksemid_t *)perm;
kmem_free(sp->sem_base,
P2ROUNDUP(sp->sem_nsems * sizeof (struct sem), 64));
list_destroy(&sp->sem_undos);
}
/*
* sem_undo_add - Create or update adjust on exit entry.
*/
static int
sem_undo_add(short val, ushort_t num, struct sem_undo *undo)
{
int newval = undo->un_aoe[num] - val;
if (newval > USHRT_MAX || newval < -USHRT_MAX)
return (ERANGE);
undo->un_aoe[num] = newval;
return (0);
}
/*
* sem_undo_clear - clears all undo entries for specified semaphores
*
* Used when semaphores are reset by SETVAL or SETALL.
*/
static void
sem_undo_clear(ksemid_t *sp, ushort_t low, ushort_t high)
{
struct sem_undo *undo;
int i;
ASSERT(low <= high);
ASSERT(high < sp->sem_nsems);
for (undo = list_head(&sp->sem_undos); undo;
undo = list_next(&sp->sem_undos, undo))
for (i = low; i <= high; i++)
undo->un_aoe[i] = 0;
}
/*
* sem_rollback - roll back work done so far if unable to complete operation
*/
static void
sem_rollback(ksemid_t *sp, struct sembuf *op, int n, struct sem_undo *undo)
{
struct sem *semp; /* semaphore ptr */
for (op += n - 1; n--; op--) {
if (op->sem_op == 0)
continue;
semp = &sp->sem_base[op->sem_num];
semp->semval -= op->sem_op;
if (op->sem_flg & SEM_UNDO) {
ASSERT(undo != NULL);
(void) sem_undo_add(-op->sem_op, op->sem_num, undo);
}
}
}
static void
sem_rmid(kipc_perm_t *perm)
{
ksemid_t *sp = (ksemid_t *)perm;
struct sem *semp;
struct sem_undo *undo;
size_t size = SEM_UNDOSZ(sp->sem_nsems);
int i;
/*LINTED*/
while (undo = list_head(&sp->sem_undos)) {
list_remove(&sp->sem_undos, undo);
mutex_enter(&undo->un_proc->p_lock);
if (undo->un_proc->p_semacct == NULL) {
mutex_exit(&undo->un_proc->p_lock);
continue;
}
avl_remove(undo->un_proc->p_semacct, undo);
mutex_exit(&undo->un_proc->p_lock);
kmem_free(undo, size);
ipc_rele_locked(sem_svc, (kipc_perm_t *)sp);
}
for (i = 0; i < sp->sem_nsems; i++) {
semp = &sp->sem_base[i];
semp->semval = semp->sempid = 0;
if (semp->semncnt) {
cv_broadcast(&semp->semncnt_cv);
semp->semncnt = 0;
}
if (semp->semzcnt) {
cv_broadcast(&semp->semzcnt_cv);
semp->semzcnt = 0;
}
}
}
/*
* semctl - Semctl system call.
*/
static int
semctl(int semid, uint_t semnum, int cmd, uintptr_t arg)
{
ksemid_t *sp; /* ptr to semaphore header */
struct sem *p; /* ptr to semaphore */
unsigned int i; /* loop control */
ushort_t *vals, *vp;
size_t vsize = 0;
int error = 0;
int retval = 0;
struct cred *cr;
kmutex_t *lock;
model_t mdl = get_udatamodel();
STRUCT_DECL(semid_ds, sid);
struct semid_ds64 ds64;
STRUCT_INIT(sid, mdl);
cr = CRED();
/*
* Perform pre- or non-lookup actions (e.g. copyins, RMID).
*/
switch (cmd) {
case IPC_SET:
if (copyin((void *)arg, STRUCT_BUF(sid), STRUCT_SIZE(sid)))
return (set_errno(EFAULT));
break;
case IPC_SET64:
if (copyin((void *)arg, &ds64, sizeof (struct semid_ds64)))
return (set_errno(EFAULT));
break;
case SETALL:
if ((lock = ipc_lookup(sem_svc, semid,
(kipc_perm_t **)&sp)) == NULL)
return (set_errno(EINVAL));
vsize = sp->sem_nsems * sizeof (*vals);
mutex_exit(lock);
/* allocate space to hold all semaphore values */
vals = kmem_alloc(vsize, KM_SLEEP);
if (copyin((void *)arg, vals, vsize)) {
kmem_free(vals, vsize);
return (set_errno(EFAULT));
}
break;
case IPC_RMID:
if (error = ipc_rmid(sem_svc, semid, cr))
return (set_errno(error));
return (0);
}
if ((lock = ipc_lookup(sem_svc, semid, (kipc_perm_t **)&sp)) == NULL) {
if (vsize != 0)
kmem_free(vals, vsize);
return (set_errno(EINVAL));
}
switch (cmd) {
/* Set ownership and permissions. */
case IPC_SET:
if (error = ipcperm_set(sem_svc, cr, &sp->sem_perm,
&STRUCT_BUF(sid)->sem_perm, mdl)) {
mutex_exit(lock);
return (set_errno(error));
}
sp->sem_ctime = gethrestime_sec();
mutex_exit(lock);
return (0);
/* Get semaphore data structure. */
case IPC_STAT:
if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
mutex_exit(lock);
return (set_errno(error));
}
ipcperm_stat(&STRUCT_BUF(sid)->sem_perm, &sp->sem_perm, mdl);
STRUCT_FSETP(sid, sem_base, NULL); /* kernel addr */
STRUCT_FSET(sid, sem_nsems, sp->sem_nsems);
STRUCT_FSET(sid, sem_otime, sp->sem_otime);
STRUCT_FSET(sid, sem_ctime, sp->sem_ctime);
STRUCT_FSET(sid, sem_binary, sp->sem_binary);
mutex_exit(lock);
if (copyout(STRUCT_BUF(sid), (void *)arg, STRUCT_SIZE(sid)))
return (set_errno(EFAULT));
return (0);
case IPC_SET64:
if (error = ipcperm_set64(sem_svc, cr, &sp->sem_perm,
&ds64.semx_perm)) {
mutex_exit(lock);
return (set_errno(error));
}
sp->sem_ctime = gethrestime_sec();
mutex_exit(lock);
return (0);
case IPC_STAT64:
ipcperm_stat64(&ds64.semx_perm, &sp->sem_perm);
ds64.semx_nsems = sp->sem_nsems;
ds64.semx_otime = sp->sem_otime;
ds64.semx_ctime = sp->sem_ctime;
mutex_exit(lock);
if (copyout(&ds64, (void *)arg, sizeof (struct semid_ds64)))
return (set_errno(EFAULT));
return (0);
/* Get # of processes sleeping for greater semval. */
case GETNCNT:
if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
mutex_exit(lock);
return (set_errno(error));
}
if (semnum >= sp->sem_nsems) {
mutex_exit(lock);
return (set_errno(EINVAL));
}
retval = sp->sem_base[semnum].semncnt;
mutex_exit(lock);
return (retval);
/* Get pid of last process to operate on semaphore. */
case GETPID:
if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
mutex_exit(lock);
return (set_errno(error));
}
if (semnum >= sp->sem_nsems) {
mutex_exit(lock);
return (set_errno(EINVAL));
}
retval = sp->sem_base[semnum].sempid;
mutex_exit(lock);
return (retval);
/* Get semval of one semaphore. */
case GETVAL:
if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
mutex_exit(lock);
return (set_errno(error));
}
if (semnum >= sp->sem_nsems) {
mutex_exit(lock);
return (set_errno(EINVAL));
}
retval = sp->sem_base[semnum].semval;
mutex_exit(lock);
return (retval);
/* Get all semvals in set. */
case GETALL:
if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
mutex_exit(lock);
return (set_errno(error));
}
/* allocate space to hold all semaphore values */
vsize = sp->sem_nsems * sizeof (*vals);
vals = vp = kmem_alloc(vsize, KM_SLEEP);
for (i = sp->sem_nsems, p = sp->sem_base; i--; p++, vp++)
bcopy(&p->semval, vp, sizeof (p->semval));
mutex_exit(lock);
if (copyout((void *)vals, (void *)arg, vsize)) {
kmem_free(vals, vsize);
return (set_errno(EFAULT));
}
kmem_free(vals, vsize);
return (0);
/* Get # of processes sleeping for semval to become zero. */
case GETZCNT:
if (error = ipcperm_access(&sp->sem_perm, SEM_R, cr)) {
mutex_exit(lock);
return (set_errno(error));
}
if (semnum >= sp->sem_nsems) {
mutex_exit(lock);
return (set_errno(EINVAL));
}
retval = sp->sem_base[semnum].semzcnt;
mutex_exit(lock);
return (retval);
/* Set semval of one semaphore. */
case SETVAL:
if (error = ipcperm_access(&sp->sem_perm, SEM_A, cr)) {
mutex_exit(lock);
return (set_errno(error));
}
if (semnum >= sp->sem_nsems) {
mutex_exit(lock);
return (set_errno(EINVAL));
}
if ((uint_t)arg > USHRT_MAX) {
mutex_exit(lock);
return (set_errno(ERANGE));
}
p = &sp->sem_base[semnum];
if ((p->semval = (ushort_t)arg) != 0) {
if (p->semncnt) {
cv_broadcast(&p->semncnt_cv);
}
} else if (p->semzcnt) {
cv_broadcast(&p->semzcnt_cv);
}
p->sempid = curproc->p_pid;
sem_undo_clear(sp, (ushort_t)semnum, (ushort_t)semnum);
mutex_exit(lock);
return (0);
/* Set semvals of all semaphores in set. */
case SETALL:
/* Check if semaphore set has been deleted and reallocated. */
if (sp->sem_nsems * sizeof (*vals) != vsize) {
error = set_errno(EINVAL);
goto seterr;
}
if (error = ipcperm_access(&sp->sem_perm, SEM_A, cr)) {
error = set_errno(error);
goto seterr;
}
sem_undo_clear(sp, 0, sp->sem_nsems - 1);
for (i = 0, p = sp->sem_base; i < sp->sem_nsems;
(p++)->sempid = curproc->p_pid) {
if ((p->semval = vals[i++]) != 0) {
if (p->semncnt) {
cv_broadcast(&p->semncnt_cv);
}
} else if (p->semzcnt) {
cv_broadcast(&p->semzcnt_cv);
}
}
seterr:
mutex_exit(lock);
kmem_free(vals, vsize);
return (error);
default:
mutex_exit(lock);
return (set_errno(EINVAL));
}
/* NOTREACHED */
}
/*
* semexit - Called by exit() to clean up on process exit.
*/
void
semexit(proc_t *pp)
{
avl_tree_t *tree;
struct sem_undo *undo;
void *cookie = NULL;
mutex_enter(&pp->p_lock);
tree = pp->p_semacct;
pp->p_semacct = NULL;
mutex_exit(&pp->p_lock);
while (undo = avl_destroy_nodes(tree, &cookie)) {
ksemid_t *sp = undo->un_sp;
size_t size = SEM_UNDOSZ(sp->sem_nsems);
int i;
(void) ipc_lock(sem_svc, sp->sem_perm.ipc_id);
if (!IPC_FREE(&sp->sem_perm)) {
for (i = 0; i < sp->sem_nsems; i++) {
int adj = undo->un_aoe[i];
if (adj) {
struct sem *semp = &sp->sem_base[i];
int v = (int)semp->semval + adj;
if (v < 0 || v > USHRT_MAX)
continue;
semp->semval = (ushort_t)v;
if (v == 0 && semp->semzcnt)
cv_broadcast(&semp->semzcnt_cv);
if (adj > 0 && semp->semncnt)
cv_broadcast(&semp->semncnt_cv);
}
}
list_remove(&sp->sem_undos, undo);
}
ipc_rele(sem_svc, (kipc_perm_t *)sp);
kmem_free(undo, size);
}
avl_destroy(tree);
kmem_free(tree, sizeof (avl_tree_t));
}
/*
* Remove all semaphores associated with a given zone. Called by
* zone_shutdown when the zone is halted.
*/
/*ARGSUSED1*/
static void
sem_remove_zone(zoneid_t zoneid, void *arg)
{
ipc_remove_zone(sem_svc, zoneid);
}
/*
* semget - Semget system call.
*/
static int
semget(key_t key, int nsems, int semflg)
{
ksemid_t *sp;
kmutex_t *lock;
int id, error;
proc_t *pp = curproc;
top:
if (error = ipc_get(sem_svc, key, semflg, (kipc_perm_t **)&sp, &lock))
return (set_errno(error));
if (!IPC_FREE(&sp->sem_perm)) {
/*
* A semaphore with the requested key exists.
*/
if (!((nsems >= 0) && (nsems <= sp->sem_nsems))) {
mutex_exit(lock);
return (set_errno(EINVAL));
}
} else {
/*
* This is a new semaphore set. Finish initialization.
*/
if (nsems <= 0 || (rctl_test(rc_process_semmsl, pp->p_rctls, pp,
nsems, RCA_SAFE) & RCT_DENY)) {
mutex_exit(lock);
mutex_exit(&pp->p_lock);
ipc_cleanup(sem_svc, (kipc_perm_t *)sp);
return (set_errno(EINVAL));
}
mutex_exit(lock);
mutex_exit(&pp->p_lock);
/*
* We round the allocation up to coherency granularity
* so that multiple semaphore allocations won't result
* in the false sharing of their sem structures.
*/
sp->sem_base =
kmem_zalloc(P2ROUNDUP(nsems * sizeof (struct sem), 64),
KM_SLEEP);
sp->sem_binary = (nsems == 1);
sp->sem_nsems = (ushort_t)nsems;
sp->sem_ctime = gethrestime_sec();
sp->sem_otime = 0;
list_create(&sp->sem_undos, sizeof (struct sem_undo),
offsetof(struct sem_undo, un_list));
if (error = ipc_commit_begin(sem_svc, key, semflg,
(kipc_perm_t *)sp)) {
if (error == EAGAIN)
goto top;
return (set_errno(error));
}
sp->sem_maxops =
rctl_enforced_value(rc_process_semopm, pp->p_rctls, pp);
if (rctl_test(rc_process_semmsl, pp->p_rctls, pp, nsems,
RCA_SAFE) & RCT_DENY) {
ipc_cleanup(sem_svc, (kipc_perm_t *)sp);
return (set_errno(EINVAL));
}
lock = ipc_commit_end(sem_svc, &sp->sem_perm);
}
if (AU_AUDITING())
audit_ipcget(AT_IPC_SEM, (void *)sp);
id = sp->sem_perm.ipc_id;
mutex_exit(lock);
return (id);
}
/*
* semids system call.
*/
static int
semids(int *buf, uint_t nids, uint_t *pnids)
{
int error;
if (error = ipc_ids(sem_svc, buf, nids, pnids))
return (set_errno(error));
return (0);
}
/*
* Helper function for semop - copies in the provided timespec and
* computes the absolute future time after which we must return.
*/
static int
compute_timeout(timespec_t **tsp, timespec_t *ts, timespec_t *now,
timespec_t *timeout)
{
model_t datamodel = get_udatamodel();
if (datamodel == DATAMODEL_NATIVE) {
if (copyin(timeout, ts, sizeof (timespec_t)))
return (EFAULT);
} else {
timespec32_t ts32;
if (copyin(timeout, &ts32, sizeof (timespec32_t)))
return (EFAULT);
TIMESPEC32_TO_TIMESPEC(ts, &ts32)
}
if (itimerspecfix(ts))
return (EINVAL);
/*
* Convert the timespec value into absolute time.
*/
timespecadd(ts, now);
*tsp = ts;
return (0);
}
/*
* Undo structure comparator. We sort based on ksemid_t pointer.
*/
static int
sem_undo_compar(const void *x, const void *y)
{
struct sem_undo *undo1 = (struct sem_undo *)x;
struct sem_undo *undo2 = (struct sem_undo *)y;
if (undo1->un_sp < undo2->un_sp)
return (-1);
if (undo1->un_sp > undo2->un_sp)
return (1);
return (0);
}
/*
* Helper function for semop - creates an undo structure and adds it to
* the process's avl tree and the semaphore's list.
*/
static int
sem_undo_alloc(proc_t *pp, ksemid_t *sp, kmutex_t **lock,
struct sem_undo *template, struct sem_undo **un)
{
size_t size;
struct sem_undo *undo;
avl_tree_t *tree = NULL;
avl_index_t where;
mutex_exit(*lock);
size = SEM_UNDOSZ(sp->sem_nsems);
undo = kmem_zalloc(size, KM_SLEEP);
undo->un_proc = pp;
undo->un_sp = sp;
if (pp->p_semacct == NULL)
tree = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
*lock = ipc_lock(sem_svc, sp->sem_perm.ipc_id);
if (IPC_FREE(&sp->sem_perm)) {
kmem_free(undo, size);
if (tree)
kmem_free(tree, sizeof (avl_tree_t));
return (EIDRM);
}
mutex_enter(&pp->p_lock);
if (tree) {
if (pp->p_semacct == NULL) {
avl_create(tree, sem_undo_compar,
sizeof (struct sem_undo),
offsetof(struct sem_undo, un_avl));
pp->p_semacct = tree;
} else {
kmem_free(tree, sizeof (avl_tree_t));
}
}
if (*un = avl_find(pp->p_semacct, template, &where)) {
mutex_exit(&pp->p_lock);
kmem_free(undo, size);
} else {
*un = undo;
avl_insert(pp->p_semacct, undo, where);
mutex_exit(&pp->p_lock);
list_insert_head(&sp->sem_undos, undo);
ipc_hold(sem_svc, (kipc_perm_t *)sp);
}
return (0);
}
/*
* semop - Semop system call.
*/
static int
semop(int semid, struct sembuf *sops, size_t nsops, timespec_t *timeout)
{
ksemid_t *sp = NULL;
kmutex_t *lock;
struct sembuf *op; /* ptr to operation */
int i; /* loop control */
struct sem *semp; /* ptr to semaphore */
int error = 0;
struct sembuf *uops; /* ptr to copy of user ops */
struct sembuf x_sem; /* avoid kmem_alloc's */
timespec_t now, ts, *tsp = NULL;
int timecheck = 0;
int cvres, needundo, mode;
struct sem_undo *undo;
proc_t *pp = curproc;
int held = 0;
CPU_STATS_ADDQ(CPU, sys, sema, 1); /* bump semaphore op count */
/*
* To avoid the cost of copying in 'timeout' in the common
* case, we could only grab the time here and defer the copyin
* and associated computations until we are about to block.
*
* The down side to this is that we would then have to spin
* some goto top nonsense to avoid the copyin behind the semid
* lock. As a common use of timed semaphores is as an explicit
* blocking mechanism, this could incur a greater penalty.
*
* If we eventually decide that this would be a wise route to
* take, the deferrable functionality is completely contained
* in 'compute_timeout', and the interface is defined such that
* we can legally not validate 'timeout' if it is unused.
*/
if (timeout != NULL) {
timecheck = timechanged;
gethrestime(&now);
if (error = compute_timeout(&tsp, &ts, &now, timeout))
return (set_errno(error));
}
/*
* Allocate space to hold the vector of semaphore ops. If
* there is only 1 operation we use a preallocated buffer on
* the stack for speed.
*
* Since we don't want to allow the user to allocate an
* arbitrary amount of kernel memory, we need to check against
* the number of operations allowed by the semaphore. We only
* bother doing this if the number of operations is larger than
* SEM_MAXUCOPS.
*/
if (nsops == 1)
uops = &x_sem;
else if (nsops == 0)
return (0);
else if (nsops <= SEM_MAXUCOPS)
uops = kmem_alloc(nsops * sizeof (*uops), KM_SLEEP);
if (nsops > SEM_MAXUCOPS) {
if ((lock = ipc_lookup(sem_svc, semid,
(kipc_perm_t **)&sp)) == NULL)
return (set_errno(EFAULT));
if (nsops > sp->sem_maxops) {
mutex_exit(lock);
return (set_errno(E2BIG));
}
held = 1;
ipc_hold(sem_svc, (kipc_perm_t *)sp);
mutex_exit(lock);
uops = kmem_alloc(nsops * sizeof (*uops), KM_SLEEP);
if (copyin(sops, uops, nsops * sizeof (*op))) {
error = EFAULT;
(void) ipc_lock(sem_svc, sp->sem_perm.ipc_id);
goto semoperr;
}
lock = ipc_lock(sem_svc, sp->sem_perm.ipc_id);
if (IPC_FREE(&sp->sem_perm)) {
error = EIDRM;
goto semoperr;
}
} else {
/*
* This could be interleaved with the above code, but
* keeping them separate improves readability.
*/
if (copyin(sops, uops, nsops * sizeof (*op))) {
error = EFAULT;
goto semoperr_unlocked;
}
if ((lock = ipc_lookup(sem_svc, semid,
(kipc_perm_t **)&sp)) == NULL) {
error = EINVAL;
goto semoperr_unlocked;
}
if (nsops > sp->sem_maxops) {
error = E2BIG;
goto semoperr;
}
}
/*
* Scan all operations. Verify that sem #s are in range and
* this process is allowed the requested operations. If any
* operations are marked SEM_UNDO, find (or allocate) the undo
* structure for this process and semaphore.
*/
needundo = 0;
mode = 0;
for (i = 0, op = uops; i++ < nsops; op++) {
mode |= op->sem_op ? SEM_A : SEM_R;
if (op->sem_num >= sp->sem_nsems) {
error = EFBIG;
goto semoperr;
}
if ((op->sem_flg & SEM_UNDO) && op->sem_op)
needundo = 1;
}
if (error = ipcperm_access(&sp->sem_perm, mode, CRED()))
goto semoperr;
if (needundo) {
struct sem_undo template;
template.un_sp = sp;
mutex_enter(&pp->p_lock);
if (pp->p_semacct)
undo = avl_find(pp->p_semacct, &template, NULL);
else
undo = NULL;
mutex_exit(&pp->p_lock);
if (undo == NULL) {
if (!held) {
held = 1;
ipc_hold(sem_svc, (kipc_perm_t *)sp);
}
if (error = sem_undo_alloc(pp, sp, &lock, &template,
&undo))
goto semoperr;
/* sem_undo_alloc unlocks the semaphore */
if (error = ipcperm_access(&sp->sem_perm, mode, CRED()))
goto semoperr;
}
}
check:
/*
* Loop waiting for the operations to be satisfied atomically.
* Actually, do the operations and undo them if a wait is needed
* or an error is detected.
*/
for (i = 0; i < nsops; i++) {
op = &uops[i];
semp = &sp->sem_base[op->sem_num];
/*
* Raise the semaphore (i.e. sema_v)
*/
if (op->sem_op > 0) {
if (op->sem_op + (int)semp->semval > USHRT_MAX ||
((op->sem_flg & SEM_UNDO) &&
(error = sem_undo_add(op->sem_op, op->sem_num,
undo)))) {
if (i)
sem_rollback(sp, uops, i, undo);
if (error == 0)
error = ERANGE;
goto semoperr;
}
semp->semval += op->sem_op;
/*
* If we are only incrementing the semaphore value
* by one on a binary semaphore, we can cv_signal.
*/
if (semp->semncnt) {
if (op->sem_op == 1 && sp->sem_binary)
cv_signal(&semp->semncnt_cv);
else
cv_broadcast(&semp->semncnt_cv);
}
if (semp->semzcnt && !semp->semval)
cv_broadcast(&semp->semzcnt_cv);
continue;
}
/*
* Lower the semaphore (i.e. sema_p)
*/
if (op->sem_op < 0) {
if (semp->semval >= (unsigned)(-op->sem_op)) {
if ((op->sem_flg & SEM_UNDO) &&
(error = sem_undo_add(op->sem_op,
op->sem_num, undo))) {
if (i)
sem_rollback(sp, uops, i, undo);
goto semoperr;
}
semp->semval += op->sem_op;
if (semp->semzcnt && !semp->semval)
cv_broadcast(&semp->semzcnt_cv);
continue;
}
if (i)
sem_rollback(sp, uops, i, undo);
if (op->sem_flg & IPC_NOWAIT) {
error = EAGAIN;
goto semoperr;
}
/*
* Mark the semaphore set as not a binary type
* if we are decrementing the value by more than 1.
*
* V operations will resort to cv_broadcast
* for this set because there are too many weird
* cases that have to be caught.
*/
if (op->sem_op < -1)
sp->sem_binary = 0;
if (!held) {
held = 1;
ipc_hold(sem_svc, (kipc_perm_t *)sp);
}
semp->semncnt++;
cvres = cv_waituntil_sig(&semp->semncnt_cv, lock,
tsp, timecheck);
lock = ipc_relock(sem_svc, sp->sem_perm.ipc_id, lock);
if (!IPC_FREE(&sp->sem_perm)) {
ASSERT(semp->semncnt != 0);
semp->semncnt--;
if (cvres > 0) /* normal wakeup */
goto check;
}
/* EINTR or EAGAIN overrides EIDRM */
if (cvres == 0)
error = EINTR;
else if (cvres < 0)
error = EAGAIN;
else
error = EIDRM;
goto semoperr;
}
/*
* Wait for zero value
*/
if (semp->semval) {
if (i)
sem_rollback(sp, uops, i, undo);
if (op->sem_flg & IPC_NOWAIT) {
error = EAGAIN;
goto semoperr;
}
if (!held) {
held = 1;
ipc_hold(sem_svc, (kipc_perm_t *)sp);
}
semp->semzcnt++;
cvres = cv_waituntil_sig(&semp->semzcnt_cv, lock,
tsp, timecheck);
lock = ipc_relock(sem_svc, sp->sem_perm.ipc_id, lock);
/*
* Don't touch semp if the semaphores have been removed.
*/
if (!IPC_FREE(&sp->sem_perm)) {
ASSERT(semp->semzcnt != 0);
semp->semzcnt--;
if (cvres > 0) /* normal wakeup */
goto check;
}
/* EINTR or EAGAIN overrides EIDRM */
if (cvres == 0)
error = EINTR;
else if (cvres < 0)
error = EAGAIN;
else
error = EIDRM;
goto semoperr;
}
}
/* All operations succeeded. Update sempid for accessed semaphores. */
for (i = 0, op = uops; i++ < nsops;
sp->sem_base[(op++)->sem_num].sempid = pp->p_pid)
;
sp->sem_otime = gethrestime_sec();
if (held)
ipc_rele(sem_svc, (kipc_perm_t *)sp);
else
mutex_exit(lock);
/* Before leaving, deallocate the buffer that held the user semops */
if (nsops != 1)
kmem_free(uops, sizeof (*uops) * nsops);
return (0);
/*
* Error return labels
*/
semoperr:
if (held)
ipc_rele(sem_svc, (kipc_perm_t *)sp);
else
mutex_exit(lock);
semoperr_unlocked:
/* Before leaving, deallocate the buffer that held the user semops */
if (nsops != 1)
kmem_free(uops, sizeof (*uops) * nsops);
return (set_errno(error));
}
/*
* semsys - System entry point for semctl, semget, and semop system calls.
*/
static int
semsys(int opcode, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4)
{
int error;
switch (opcode) {
case SEMCTL:
error = semctl((int)a1, (uint_t)a2, (int)a3, a4);
break;
case SEMGET:
error = semget((key_t)a1, (int)a2, (int)a3);
break;
case SEMOP:
error = semop((int)a1, (struct sembuf *)a2, (size_t)a3, 0);
break;
case SEMIDS:
error = semids((int *)a1, (uint_t)a2, (uint_t *)a3);
break;
case SEMTIMEDOP:
error = semop((int)a1, (struct sembuf *)a2, (size_t)a3,
(timespec_t *)a4);
break;
default:
error = set_errno(EINVAL);
break;
}
return (error);
}
/*
* 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 2022 Garrett D'Amore
*/
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/conf.h>
#include <sys/cred.h>
#include <sys/kmem.h>
#include <sys/sysmacros.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/debug.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/file.h>
#include <sys/open.h>
#include <sys/user.h>
#include <sys/termios.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/sunddi.h>
#include <sys/esunddi.h>
#include <sys/flock.h>
#include <sys/modctl.h>
#include <sys/cmn_err.h>
#include <sys/vmsystm.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <fs/sockfs/sockcommon.h>
#include <fs/sockfs/socktpi.h>
#include <netinet/in.h>
#include <sys/sendfile.h>
#include <sys/un.h>
#include <sys/tihdr.h>
#include <sys/atomic.h>
#include <inet/common.h>
#include <inet/ip.h>
#include <inet/ip6.h>
#include <inet/tcp.h>
extern int sosendfile64(file_t *, file_t *, const struct ksendfilevec64 *,
ssize32_t *);
extern int snf_segmap(file_t *, vnode_t *, u_offset_t, u_offset_t, ssize_t *,
boolean_t);
extern sotpi_info_t *sotpi_sototpi(struct sonode *);
#define SEND_MAX_CHUNK 16
#if defined(_SYSCALL32_IMPL) || defined(_ILP32)
/*
* 64 bit offsets for 32 bit applications only running either on
* 64 bit kernel or 32 bit kernel. For 32 bit apps, we can't transfer
* more than 2GB of data.
*/
static int
sendvec_chunk64(file_t *fp, u_offset_t *fileoff, struct ksendfilevec64 *sfv,
int copy_cnt, ssize32_t *count)
{
struct vnode *vp;
ushort_t fflag;
int ioflag;
size32_t cnt;
ssize32_t sfv_len;
ssize32_t tmpcount;
u_offset_t sfv_off;
struct uio auio;
struct iovec aiov;
int i, error;
fflag = fp->f_flag;
vp = fp->f_vnode;
for (i = 0; i < copy_cnt; i++) {
if (ISSIG(curthread, JUSTLOOKING))
return (EINTR);
/*
* Do similar checks as "write" as we are writing
* sfv_len bytes into "vp".
*/
sfv_len = (ssize32_t)sfv->sfv_len;
if (sfv_len == 0) {
sfv++;
continue;
}
if (sfv_len < 0)
return (EINVAL);
if (vp->v_type == VREG) {
if (*fileoff >= curproc->p_fsz_ctl) {
mutex_enter(&curproc->p_lock);
(void) rctl_action(
rctlproc_legacy[RLIMIT_FSIZE],
curproc->p_rctls, curproc, RCA_SAFE);
mutex_exit(&curproc->p_lock);
return (EFBIG);
}
if (*fileoff >= OFFSET_MAX(fp))
return (EFBIG);
if (*fileoff + sfv_len > OFFSET_MAX(fp))
return (EINVAL);
}
tmpcount = *count + sfv_len;
if (tmpcount < 0)
return (EINVAL);
sfv_off = sfv->sfv_off;
auio.uio_extflg = UIO_COPY_DEFAULT;
if (sfv->sfv_fd == SFV_FD_SELF) {
aiov.iov_len = sfv_len;
aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
auio.uio_loffset = *fileoff;
auio.uio_iovcnt = 1;
auio.uio_resid = sfv_len;
auio.uio_iov = &aiov;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = curproc->p_fsz_ctl;
auio.uio_fmode = fflag;
ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
while (sfv_len > 0) {
error = VOP_WRITE(vp, &auio, ioflag,
fp->f_cred, NULL);
cnt = sfv_len - auio.uio_resid;
sfv_len -= cnt;
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
if (vp->v_type == VREG)
*fileoff += cnt;
*count += cnt;
if (error != 0)
return (error);
}
} else {
file_t *ffp;
vnode_t *readvp;
size_t size;
caddr_t ptr;
if ((ffp = getf(sfv->sfv_fd)) == NULL)
return (EBADF);
if ((ffp->f_flag & FREAD) == 0) {
releasef(sfv->sfv_fd);
return (EBADF);
}
readvp = ffp->f_vnode;
if (readvp->v_type != VREG) {
releasef(sfv->sfv_fd);
return (EINVAL);
}
/*
* No point reading and writing to same vp,
* as long as both are regular files. readvp is not
* locked; but since we got it from an open file the
* contents will be valid during the time of access.
*/
if (vn_compare(vp, readvp)) {
releasef(sfv->sfv_fd);
return (EINVAL);
}
/*
* Optimize the regular file over
* the socket case.
*/
if (vp->v_type == VSOCK) {
error = sosendfile64(fp, ffp, sfv,
(ssize32_t *)&cnt);
*count += cnt;
if (error)
return (error);
sfv++;
continue;
}
/*
* Note: we assume readvp != vp. "vp" is already
* locked, and "readvp" must not be.
*/
if (readvp < vp) {
VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
} else {
(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
}
/*
* Same checks as in pread64.
*/
if (sfv_off > MAXOFFSET_T) {
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
releasef(sfv->sfv_fd);
return (EINVAL);
}
if (sfv_off + sfv_len > MAXOFFSET_T)
sfv_len = (ssize32_t)(MAXOFFSET_T - sfv_off);
/* Find the native blocksize to transfer data */
size = MIN(vp->v_vfsp->vfs_bsize,
readvp->v_vfsp->vfs_bsize);
size = sfv_len < size ? sfv_len : size;
ptr = kmem_alloc(size, KM_NOSLEEP);
if (ptr == NULL) {
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
releasef(sfv->sfv_fd);
return (ENOMEM);
}
while (sfv_len > 0) {
size_t iov_len;
iov_len = MIN(size, sfv_len);
aiov.iov_base = ptr;
aiov.iov_len = iov_len;
auio.uio_loffset = sfv_off;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = iov_len;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_llimit = MAXOFFSET_T;
auio.uio_fmode = ffp->f_flag;
ioflag = auio.uio_fmode &
(FAPPEND|FSYNC|FDSYNC|FRSYNC);
/*
* If read sync is not asked for,
* filter sync flags
*/
if ((ioflag & FRSYNC) == 0)
ioflag &= ~(FSYNC|FDSYNC);
error = VOP_READ(readvp, &auio, ioflag,
fp->f_cred, NULL);
if (error) {
kmem_free(ptr, size);
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
releasef(sfv->sfv_fd);
return (error);
}
/*
* Check how must data was really read.
* Decrement the 'len' and increment the
* 'off' appropriately.
*/
cnt = iov_len - auio.uio_resid;
if (cnt == 0) {
/*
* If we were reading a pipe (currently
* not implemented), we may now lose
* data.
*/
kmem_free(ptr, size);
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
releasef(sfv->sfv_fd);
return (EINVAL);
}
sfv_len -= cnt;
sfv_off += cnt;
aiov.iov_base = ptr;
aiov.iov_len = cnt;
auio.uio_loffset = *fileoff;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = cnt;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_llimit = curproc->p_fsz_ctl;
auio.uio_fmode = fflag;
ioflag = auio.uio_fmode &
(FAPPEND|FSYNC|FDSYNC|FRSYNC);
error = VOP_WRITE(vp, &auio, ioflag,
fp->f_cred, NULL);
/*
* Check how much data was written. Increment
* the 'len' and decrement the 'off' if all
* the data was not written.
*/
cnt -= auio.uio_resid;
sfv_len += auio.uio_resid;
sfv_off -= auio.uio_resid;
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
if (vp->v_type == VREG)
*fileoff += cnt;
*count += cnt;
if (error != 0) {
kmem_free(ptr, size);
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
releasef(sfv->sfv_fd);
return (error);
}
}
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
releasef(sfv->sfv_fd);
kmem_free(ptr, size);
}
sfv++;
}
return (0);
}
static ssize32_t
sendvec64(file_t *fp, const struct ksendfilevec64 *vec, int sfvcnt,
size32_t *xferred, int fildes)
{
u_offset_t fileoff;
int copy_cnt;
const struct ksendfilevec64 *copy_vec;
struct ksendfilevec64 sfv[SEND_MAX_CHUNK];
struct vnode *vp;
int error;
ssize32_t count = 0;
vp = fp->f_vnode;
(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
copy_vec = vec;
fileoff = fp->f_offset;
do {
copy_cnt = MIN(sfvcnt, SEND_MAX_CHUNK);
if (copyin(copy_vec, sfv, copy_cnt *
sizeof (struct ksendfilevec64))) {
error = EFAULT;
break;
}
error = sendvec_chunk64(fp, &fileoff, sfv, copy_cnt, &count);
if (error != 0)
break;
copy_vec += copy_cnt;
sfvcnt -= copy_cnt;
} while (sfvcnt > 0);
if (vp->v_type == VREG)
fp->f_offset += count;
VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
if (copyout(&count, xferred, sizeof (count)))
error = EFAULT;
releasef(fildes);
if (error != 0)
return (set_errno(error));
return (count);
}
#endif
static int
sendvec_small_chunk(file_t *fp, u_offset_t *fileoff, struct sendfilevec *sfv,
int copy_cnt, ssize_t total_size, int maxblk, ssize_t *count)
{
struct vnode *vp;
struct uio auio;
struct iovec aiov;
ushort_t fflag;
int ioflag;
int i, error;
size_t cnt;
ssize_t sfv_len;
u_offset_t sfv_off;
#ifdef _SYSCALL32_IMPL
model_t model = get_udatamodel();
u_offset_t maxoff = (model == DATAMODEL_ILP32) ?
MAXOFF32_T : MAXOFFSET_T;
#else
const u_offset_t maxoff = MAXOFF32_T;
#endif
mblk_t *dmp = NULL;
int wroff;
int buf_left = 0;
size_t iov_len;
mblk_t *head, *tmp;
size_t size = total_size;
size_t extra;
int tail_len;
struct nmsghdr msg;
fflag = fp->f_flag;
vp = fp->f_vnode;
ASSERT(vp->v_type == VSOCK);
ASSERT(maxblk > 0);
/* If nothing to send, return */
if (total_size == 0)
return (0);
if (vp->v_stream != NULL) {
wroff = (int)vp->v_stream->sd_wroff;
tail_len = (int)vp->v_stream->sd_tail;
} else {
struct sonode *so;
so = VTOSO(vp);
wroff = so->so_proto_props.sopp_wroff;
tail_len = so->so_proto_props.sopp_tail;
}
extra = wroff + tail_len;
buf_left = MIN(total_size, maxblk);
head = dmp = allocb(buf_left + extra, BPRI_HI);
if (head == NULL)
return (ENOMEM);
head->b_wptr = head->b_rptr = head->b_rptr + wroff;
bzero(&msg, sizeof (msg));
auio.uio_extflg = UIO_COPY_DEFAULT;
for (i = 0; i < copy_cnt; i++) {
if (ISSIG(curthread, JUSTLOOKING)) {
freemsg(head);
return (EINTR);
}
/*
* Do similar checks as "write" as we are writing
* sfv_len bytes into "vp".
*/
sfv_len = (ssize_t)sfv->sfv_len;
if (sfv_len == 0) {
sfv++;
continue;
}
/* Check for overflow */
#ifdef _SYSCALL32_IMPL
if (model == DATAMODEL_ILP32) {
if (((ssize32_t)(*count + sfv_len)) < 0) {
freemsg(head);
return (EINVAL);
}
} else
#endif
if ((*count + sfv_len) < 0) {
freemsg(head);
return (EINVAL);
}
sfv_off = (u_offset_t)(ulong_t)sfv->sfv_off;
if (sfv->sfv_fd == SFV_FD_SELF) {
while (sfv_len > 0) {
if (buf_left == 0) {
tmp = dmp;
buf_left = MIN(total_size, maxblk);
iov_len = MIN(buf_left, sfv_len);
dmp = allocb(buf_left + extra, BPRI_HI);
if (dmp == NULL) {
freemsg(head);
return (ENOMEM);
}
dmp->b_wptr = dmp->b_rptr =
dmp->b_rptr + wroff;
tmp->b_cont = dmp;
} else {
iov_len = MIN(buf_left, sfv_len);
}
aiov.iov_len = iov_len;
aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
auio.uio_loffset = *fileoff;
auio.uio_iovcnt = 1;
auio.uio_resid = iov_len;
auio.uio_iov = &aiov;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = curproc->p_fsz_ctl;
auio.uio_fmode = fflag;
buf_left -= iov_len;
total_size -= iov_len;
sfv_len -= iov_len;
sfv_off += iov_len;
error = uiomove((caddr_t)dmp->b_wptr,
iov_len, UIO_WRITE, &auio);
if (error != 0) {
freemsg(head);
return (error);
}
dmp->b_wptr += iov_len;
}
} else {
file_t *ffp;
vnode_t *readvp;
if ((ffp = getf(sfv->sfv_fd)) == NULL) {
freemsg(head);
return (EBADF);
}
if ((ffp->f_flag & FREAD) == 0) {
releasef(sfv->sfv_fd);
freemsg(head);
return (EACCES);
}
readvp = ffp->f_vnode;
if (readvp->v_type != VREG) {
releasef(sfv->sfv_fd);
freemsg(head);
return (EINVAL);
}
/*
* No point reading and writing to same vp,
* as long as both are regular files. readvp is not
* locked; but since we got it from an open file the
* contents will be valid during the time of access.
*/
if (vn_compare(vp, readvp)) {
releasef(sfv->sfv_fd);
freemsg(head);
return (EINVAL);
}
/*
* Note: we assume readvp != vp. "vp" is already
* locked, and "readvp" must not be.
*/
if (readvp < vp) {
VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
} else {
(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
}
/* Same checks as in pread */
if (sfv_off > maxoff) {
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
releasef(sfv->sfv_fd);
freemsg(head);
return (EINVAL);
}
if (sfv_off + sfv_len > maxoff) {
total_size -= (sfv_off + sfv_len - maxoff);
sfv_len = (ssize_t)((offset_t)maxoff -
sfv_off);
}
while (sfv_len > 0) {
if (buf_left == 0) {
tmp = dmp;
buf_left = MIN(total_size, maxblk);
iov_len = MIN(buf_left, sfv_len);
dmp = allocb(buf_left + extra, BPRI_HI);
if (dmp == NULL) {
VOP_RWUNLOCK(readvp,
V_WRITELOCK_FALSE, NULL);
releasef(sfv->sfv_fd);
freemsg(head);
return (ENOMEM);
}
dmp->b_wptr = dmp->b_rptr =
dmp->b_rptr + wroff;
tmp->b_cont = dmp;
} else {
iov_len = MIN(buf_left, sfv_len);
}
aiov.iov_base = (caddr_t)dmp->b_wptr;
aiov.iov_len = iov_len;
auio.uio_loffset = sfv_off;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = iov_len;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_llimit = MAXOFFSET_T;
auio.uio_fmode = ffp->f_flag;
ioflag = auio.uio_fmode &
(FAPPEND|FSYNC|FDSYNC|FRSYNC);
/*
* If read sync is not asked for,
* filter sync flags
*/
if ((ioflag & FRSYNC) == 0)
ioflag &= ~(FSYNC|FDSYNC);
error = VOP_READ(readvp, &auio, ioflag,
fp->f_cred, NULL);
if (error != 0) {
/*
* If we were reading a pipe (currently
* not implemented), we may now loose
* data.
*/
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
releasef(sfv->sfv_fd);
freemsg(head);
return (error);
}
/*
* Check how much data was really read.
* Decrement the 'len' and increment the
* 'off' appropriately.
*/
cnt = iov_len - auio.uio_resid;
if (cnt == 0) {
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
releasef(sfv->sfv_fd);
freemsg(head);
return (EINVAL);
}
sfv_len -= cnt;
sfv_off += cnt;
total_size -= cnt;
buf_left -= cnt;
dmp->b_wptr += cnt;
}
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
releasef(sfv->sfv_fd);
}
sfv++;
}
ASSERT(total_size == 0);
error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &head);
if (error != 0) {
if (head != NULL)
freemsg(head);
return (error);
}
ttolwp(curthread)->lwp_ru.ioch += (ulong_t)size;
*count += size;
return (0);
}
static int
sendvec_chunk(file_t *fp, u_offset_t *fileoff, struct sendfilevec *sfv,
int copy_cnt, ssize_t *count)
{
struct vnode *vp;
struct uio auio;
struct iovec aiov;
ushort_t fflag;
int ioflag;
int i, error;
size_t cnt;
ssize_t sfv_len;
u_offset_t sfv_off;
#ifdef _SYSCALL32_IMPL
model_t model = get_udatamodel();
u_offset_t maxoff = (model == DATAMODEL_ILP32) ?
MAXOFF32_T : MAXOFFSET_T;
#else
const u_offset_t maxoff = MAXOFF32_T;
#endif
mblk_t *dmp = NULL;
char *buf = NULL;
size_t extra = 0;
int maxblk, wroff, tail_len;
struct sonode *so;
stdata_t *stp;
struct nmsghdr msg;
maxblk = 0;
wroff = 0;
fflag = fp->f_flag;
vp = fp->f_vnode;
so = NULL;
stp = NULL;
if (vp->v_type == VSOCK) {
so = VTOSO(vp);
if (vp->v_stream != NULL) {
stp = vp->v_stream;
wroff = (int)stp->sd_wroff;
tail_len = (int)stp->sd_tail;
maxblk = (int)stp->sd_maxblk;
} else {
stp = NULL;
wroff = so->so_proto_props.sopp_wroff;
tail_len = so->so_proto_props.sopp_tail;
maxblk = so->so_proto_props.sopp_maxblk;
}
extra = wroff + tail_len;
}
bzero(&msg, sizeof (msg));
auio.uio_extflg = UIO_COPY_DEFAULT;
for (i = 0; i < copy_cnt; i++) {
if (ISSIG(curthread, JUSTLOOKING))
return (EINTR);
/*
* Do similar checks as "write" as we are writing
* sfv_len bytes into "vp".
*/
sfv_len = (ssize_t)sfv->sfv_len;
if (sfv_len == 0) {
sfv++;
continue;
}
if (vp->v_type == VREG) {
if (*fileoff >= curproc->p_fsz_ctl) {
mutex_enter(&curproc->p_lock);
(void) rctl_action(
rctlproc_legacy[RLIMIT_FSIZE],
curproc->p_rctls, curproc, RCA_SAFE);
mutex_exit(&curproc->p_lock);
return (EFBIG);
}
if (*fileoff >= maxoff)
return (EFBIG);
if (*fileoff + sfv_len > maxoff)
return (EINVAL);
}
/* Check for overflow */
#ifdef _SYSCALL32_IMPL
if (model == DATAMODEL_ILP32) {
if (((ssize32_t)(*count + sfv_len)) < 0)
return (EINVAL);
} else
#endif
if ((*count + sfv_len) < 0)
return (EINVAL);
sfv_off = (u_offset_t)(ulong_t)sfv->sfv_off;
if (sfv->sfv_fd == SFV_FD_SELF) {
if (vp->v_type == VSOCK) {
while (sfv_len > 0) {
size_t iov_len;
iov_len = sfv_len;
/*
* Socket filters can limit the mblk
* size, so limit reads to maxblk if
* there are filters present.
*/
if (so->so_filter_active > 0 &&
maxblk != INFPSZ)
iov_len = MIN(iov_len, maxblk);
aiov.iov_len = iov_len;
aiov.iov_base =
(caddr_t)(uintptr_t)sfv_off;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_loffset = *fileoff;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_fmode = fflag;
auio.uio_llimit = curproc->p_fsz_ctl;
auio.uio_resid = iov_len;
dmp = allocb(iov_len + extra, BPRI_HI);
if (dmp == NULL)
return (ENOMEM);
dmp->b_wptr = dmp->b_rptr =
dmp->b_rptr + wroff;
error = uiomove((caddr_t)dmp->b_wptr,
iov_len, UIO_WRITE, &auio);
if (error != 0) {
freeb(dmp);
return (error);
}
dmp->b_wptr += iov_len;
error = socket_sendmblk(VTOSO(vp),
&msg, fflag, CRED(), &dmp);
if (error != 0) {
if (dmp != NULL)
freeb(dmp);
return (error);
}
ttolwp(curthread)->lwp_ru.ioch +=
(ulong_t)iov_len;
*count += iov_len;
sfv_len -= iov_len;
sfv_off += iov_len;
}
} else {
aiov.iov_len = sfv_len;
aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_loffset = *fileoff;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_fmode = fflag;
auio.uio_llimit = curproc->p_fsz_ctl;
auio.uio_resid = sfv_len;
ioflag = auio.uio_fmode &
(FAPPEND|FSYNC|FDSYNC|FRSYNC);
while (sfv_len > 0) {
error = VOP_WRITE(vp, &auio, ioflag,
fp->f_cred, NULL);
cnt = sfv_len - auio.uio_resid;
sfv_len -= cnt;
ttolwp(curthread)->lwp_ru.ioch +=
(ulong_t)cnt;
*fileoff += cnt;
*count += cnt;
if (error != 0)
return (error);
}
}
} else {
int segmapit = 0;
file_t *ffp;
vnode_t *readvp;
struct vnode *realvp;
size_t size;
caddr_t ptr;
if ((ffp = getf(sfv->sfv_fd)) == NULL)
return (EBADF);
if ((ffp->f_flag & FREAD) == 0) {
releasef(sfv->sfv_fd);
return (EBADF);
}
readvp = ffp->f_vnode;
if (VOP_REALVP(readvp, &realvp, NULL) == 0)
readvp = realvp;
if (readvp->v_type != VREG) {
releasef(sfv->sfv_fd);
return (EINVAL);
}
/*
* No point reading and writing to same vp,
* as long as both are regular files. readvp is not
* locked; but since we got it from an open file the
* contents will be valid during the time of access.
*/
if (vn_compare(vp, readvp)) {
releasef(sfv->sfv_fd);
return (EINVAL);
}
/*
* Note: we assume readvp != vp. "vp" is already
* locked, and "readvp" must not be.
*/
if (readvp < vp) {
VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
} else {
(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
}
/* Same checks as in pread */
if (sfv_off > maxoff) {
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
releasef(sfv->sfv_fd);
return (EINVAL);
}
if (sfv_off + sfv_len > maxoff) {
sfv_len = (ssize_t)((offset_t)maxoff -
sfv_off);
}
/* Find the native blocksize to transfer data */
size = MIN(vp->v_vfsp->vfs_bsize,
readvp->v_vfsp->vfs_bsize);
size = sfv_len < size ? sfv_len : size;
if (vp->v_type != VSOCK) {
segmapit = 0;
buf = kmem_alloc(size, KM_NOSLEEP);
if (buf == NULL) {
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
releasef(sfv->sfv_fd);
return (ENOMEM);
}
} else {
uint_t copyflag;
copyflag = stp != NULL ? stp->sd_copyflag :
so->so_proto_props.sopp_zcopyflag;
/*
* Socket filters can limit the mblk size,
* so limit reads to maxblk if there are
* filters present.
*/
if (so->so_filter_active > 0 &&
maxblk != INFPSZ)
size = MIN(size, maxblk);
if (vn_has_flocks(readvp) ||
readvp->v_flag & VNOMAP ||
copyflag & STZCVMUNSAFE) {
segmapit = 0;
} else if (copyflag & STZCVMSAFE) {
segmapit = 1;
} else {
int on = 1;
if (socket_setsockopt(VTOSO(vp),
SOL_SOCKET, SO_SND_COPYAVOID,
&on, sizeof (on), CRED()) == 0)
segmapit = 1;
}
}
if (segmapit) {
struct vattr va;
boolean_t nowait;
va.va_mask = AT_SIZE;
error = VOP_GETATTR(readvp, &va, 0, kcred,
NULL);
if (error != 0 || sfv_off >= va.va_size) {
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
releasef(sfv->sfv_fd);
return (error);
}
/* Read as much as possible. */
if (sfv_off + sfv_len > va.va_size)
sfv_len = va.va_size - sfv_off;
nowait = (sfv->sfv_flag & SFV_NOWAIT) != 0;
error = snf_segmap(fp, readvp, sfv_off,
(u_offset_t)sfv_len, (ssize_t *)&cnt,
nowait);
releasef(sfv->sfv_fd);
*count += cnt;
if (error)
return (error);
sfv++;
continue;
}
while (sfv_len > 0) {
size_t iov_len;
iov_len = MIN(size, sfv_len);
if (vp->v_type == VSOCK) {
dmp = allocb(iov_len + extra, BPRI_HI);
if (dmp == NULL) {
VOP_RWUNLOCK(readvp,
V_WRITELOCK_FALSE, NULL);
releasef(sfv->sfv_fd);
return (ENOMEM);
}
dmp->b_wptr = dmp->b_rptr =
dmp->b_rptr + wroff;
ptr = (caddr_t)dmp->b_rptr;
} else {
ptr = buf;
}
aiov.iov_base = ptr;
aiov.iov_len = iov_len;
auio.uio_loffset = sfv_off;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = iov_len;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_llimit = MAXOFFSET_T;
auio.uio_fmode = ffp->f_flag;
ioflag = auio.uio_fmode &
(FAPPEND|FSYNC|FDSYNC|FRSYNC);
/*
* If read sync is not asked for,
* filter sync flags
*/
if ((ioflag & FRSYNC) == 0)
ioflag &= ~(FSYNC|FDSYNC);
error = VOP_READ(readvp, &auio, ioflag,
fp->f_cred, NULL);
if (error != 0) {
/*
* If we were reading a pipe (currently
* not implemented), we may now lose
* data.
*/
if (vp->v_type == VSOCK)
freeb(dmp);
else
kmem_free(buf, size);
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
releasef(sfv->sfv_fd);
return (error);
}
/*
* Check how much data was really read.
* Decrement the 'len' and increment the
* 'off' appropriately.
*/
cnt = iov_len - auio.uio_resid;
if (cnt == 0) {
if (vp->v_type == VSOCK)
freeb(dmp);
else
kmem_free(buf, size);
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
NULL);
releasef(sfv->sfv_fd);
return (EINVAL);
}
sfv_len -= cnt;
sfv_off += cnt;
if (vp->v_type == VSOCK) {
dmp->b_wptr = dmp->b_rptr + cnt;
error = socket_sendmblk(VTOSO(vp),
&msg, fflag, CRED(), &dmp);
if (error != 0) {
if (dmp != NULL)
freeb(dmp);
VOP_RWUNLOCK(readvp,
V_WRITELOCK_FALSE, NULL);
releasef(sfv->sfv_fd);
return (error);
}
ttolwp(curthread)->lwp_ru.ioch +=
(ulong_t)cnt;
*count += cnt;
} else {
aiov.iov_base = ptr;
aiov.iov_len = cnt;
auio.uio_loffset = *fileoff;
auio.uio_resid = cnt;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_llimit = curproc->p_fsz_ctl;
auio.uio_fmode = fflag;
ioflag = auio.uio_fmode &
(FAPPEND|FSYNC|FDSYNC|FRSYNC);
error = VOP_WRITE(vp, &auio, ioflag,
fp->f_cred, NULL);
/*
* Check how much data was written.
* Increment the 'len' and decrement the
* 'off' if all the data was not
* written.
*/
cnt -= auio.uio_resid;
sfv_len += auio.uio_resid;
sfv_off -= auio.uio_resid;
ttolwp(curthread)->lwp_ru.ioch +=
(ulong_t)cnt;
*fileoff += cnt;
*count += cnt;
if (error != 0) {
kmem_free(buf, size);
VOP_RWUNLOCK(readvp,
V_WRITELOCK_FALSE, NULL);
releasef(sfv->sfv_fd);
return (error);
}
}
}
if (buf) {
kmem_free(buf, size);
buf = NULL;
}
VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
releasef(sfv->sfv_fd);
}
sfv++;
}
return (0);
}
ssize_t
sendfilev(int opcode, int fildes, const struct sendfilevec *vec, int sfvcnt,
size_t *xferred)
{
int error = 0;
int first_vector_error = 0;
file_t *fp;
struct vnode *vp;
struct sonode *so = NULL;
u_offset_t fileoff;
int copy_cnt;
const struct sendfilevec *copy_vec;
struct sendfilevec sfv[SEND_MAX_CHUNK];
ssize_t count = 0;
#ifdef _SYSCALL32_IMPL
struct ksendfilevec32 sfv32[SEND_MAX_CHUNK];
#endif
ssize_t total_size;
int i;
boolean_t is_sock = B_FALSE;
int maxblk = 0;
if (sfvcnt <= 0)
return (set_errno(EINVAL));
if ((fp = getf(fildes)) == NULL)
return (set_errno(EBADF));
if (((fp->f_flag) & FWRITE) == 0) {
error = EBADF;
goto err;
}
fileoff = fp->f_offset;
vp = fp->f_vnode;
switch (vp->v_type) {
case VSOCK:
so = VTOSO(vp);
is_sock = B_TRUE;
if (SOCK_IS_NONSTR(so)) {
maxblk = so->so_proto_props.sopp_maxblk;
} else {
maxblk = (int)vp->v_stream->sd_maxblk;
}
/*
* We need to make sure that the socket that we're sending on
* supports sendfile behavior. sockfs doesn't know that the APIs
* we want to use are coming from sendfile, so we can't rely on
* it to check for us.
*/
if ((so->so_mode & SM_SENDFILESUPP) == 0) {
error = EOPNOTSUPP;
goto err;
}
break;
case VREG:
break;
default:
error = EINVAL;
goto err;
}
switch (opcode) {
case SENDFILEV :
break;
#if defined(_SYSCALL32_IMPL) || defined(_ILP32)
case SENDFILEV64 :
return (sendvec64(fp, (struct ksendfilevec64 *)vec, sfvcnt,
(size32_t *)xferred, fildes));
#endif
default :
error = ENOSYS;
break;
}
(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
copy_vec = vec;
do {
total_size = 0;
copy_cnt = MIN(sfvcnt, SEND_MAX_CHUNK);
#ifdef _SYSCALL32_IMPL
/* 32-bit callers need to have their iovec expanded. */
if (get_udatamodel() == DATAMODEL_ILP32) {
if (copyin(copy_vec, sfv32,
copy_cnt * sizeof (ksendfilevec32_t))) {
error = EFAULT;
break;
}
for (i = 0; i < copy_cnt; i++) {
sfv[i].sfv_fd = sfv32[i].sfv_fd;
sfv[i].sfv_off =
(off_t)(uint32_t)sfv32[i].sfv_off;
sfv[i].sfv_len = (size_t)sfv32[i].sfv_len;
total_size += sfv[i].sfv_len;
sfv[i].sfv_flag = sfv32[i].sfv_flag;
/*
* Individual elements of the vector must not
* wrap or overflow, as later math is signed.
* Equally total_size needs to be checked after
* each vector is added in, to be sure that
* rogue values haven't overflowed the counter.
*/
if (((ssize32_t)sfv[i].sfv_len < 0) ||
((ssize32_t)total_size < 0)) {
/*
* Truncate the vector to send data
* described by elements before the
* error.
*/
copy_cnt = i;
first_vector_error = EINVAL;
/* total_size can't be trusted */
if ((ssize32_t)total_size < 0)
error = EINVAL;
break;
}
}
/* Nothing to do, process errors */
if (copy_cnt == 0)
break;
} else {
#endif
if (copyin(copy_vec, sfv,
copy_cnt * sizeof (sendfilevec_t))) {
error = EFAULT;
break;
}
for (i = 0; i < copy_cnt; i++) {
total_size += sfv[i].sfv_len;
/*
* Individual elements of the vector must not
* wrap or overflow, as later math is signed.
* Equally total_size needs to be checked after
* each vector is added in, to be sure that
* rogue values haven't overflowed the counter.
*/
if (((ssize_t)sfv[i].sfv_len < 0) ||
(total_size < 0)) {
/*
* Truncate the vector to send data
* described by elements before the
* error.
*/
copy_cnt = i;
first_vector_error = EINVAL;
/* total_size can't be trusted */
if (total_size < 0)
error = EINVAL;
break;
}
}
/* Nothing to do, process errors */
if (copy_cnt == 0)
break;
#ifdef _SYSCALL32_IMPL
}
#endif
/*
* The task between deciding to use sendvec_small_chunk
* and sendvec_chunk is dependant on multiple things:
*
* i) latency is important for smaller files. So if the
* data is smaller than 'tcp_slow_start_initial' times
* maxblk, then use sendvec_small_chunk which creates
* maxblk size mblks and chains them together and sends
* them to TCP in one shot. It also leaves 'wroff' size
* space for the headers in each mblk.
*
* ii) for total size bigger than 'tcp_slow_start_initial'
* time maxblk, its probably real file data which is
* dominating. So its better to use sendvec_chunk because
* performance goes to dog if we don't do pagesize reads.
* sendvec_chunk will do pagesize reads and write them
* in pagesize mblks to TCP.
*
* Side Notes: A write to file has not been optimized.
* Future zero copy code will plugin into sendvec_chunk
* only because doing zero copy for files smaller then
* pagesize is useless.
*/
if (is_sock) {
if ((total_size <= (4 * maxblk)) &&
error == 0) {
error = sendvec_small_chunk(fp,
&fileoff, sfv, copy_cnt,
total_size, maxblk, &count);
} else {
error = sendvec_chunk(fp, &fileoff,
sfv, copy_cnt, &count);
}
} else {
ASSERT(vp->v_type == VREG);
error = sendvec_chunk(fp, &fileoff, sfv, copy_cnt,
&count);
}
#ifdef _SYSCALL32_IMPL
if (get_udatamodel() == DATAMODEL_ILP32) {
copy_vec = (const struct sendfilevec *)
((char *)copy_vec +
(copy_cnt * sizeof (ksendfilevec32_t)));
} else
#endif
copy_vec += copy_cnt;
sfvcnt -= copy_cnt;
/* Process all vector members up to first error */
} while ((sfvcnt > 0) && first_vector_error == 0 && error == 0);
if (vp->v_type == VREG)
fp->f_offset += count;
VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
#ifdef _SYSCALL32_IMPL
if (get_udatamodel() == DATAMODEL_ILP32) {
ssize32_t count32 = (ssize32_t)count;
if (copyout(&count32, xferred, sizeof (count32)))
error = EFAULT;
releasef(fildes);
if (error != 0)
return (set_errno(error));
if (first_vector_error != 0)
return (set_errno(first_vector_error));
return (count32);
}
#endif
if (copyout(&count, xferred, sizeof (count)))
error = EFAULT;
releasef(fildes);
if (error != 0)
return (set_errno(error));
if (first_vector_error != 0)
return (set_errno(first_vector_error));
return (count);
err:
ASSERT(error != 0);
releasef(fildes);
return (set_errno(error));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* SID system call.
*/
#include <sys/sid.h>
#include <sys/cred.h>
#include <sys/errno.h>
#include <sys/systm.h>
#include <sys/policy.h>
#include <sys/door.h>
#include <sys/kidmap.h>
#include <sys/proc.h>
static uint64_t
allocids(int flag, int nuids, int ngids)
{
rval_t r;
uid_t su = 0;
gid_t sg = 0;
struct door_info di;
door_handle_t dh;
int err;
zone_t *zone = crgetzone(CRED());
dh = idmap_get_door(zone);
if (dh == NULL)
return (set_errno(EPERM));
if ((err = door_ki_info(dh, &di)) != 0) {
door_ki_rele(dh);
return (set_errno(err));
}
door_ki_rele(dh);
if (curproc->p_pid != di.di_target)
return (set_errno(EPERM));
if (flag)
idmap_purge_cache(zone);
if (nuids < 0 || ngids < 0)
return (set_errno(EINVAL));
if (flag != 0 || nuids > 0)
err = eph_uid_alloc(zone, flag, &su, nuids);
if (err == 0 && (flag != 0 || ngids > 0))
err = eph_gid_alloc(zone, flag, &sg, ngids);
if (err != 0)
return (set_errno(EOVERFLOW));
r.r_val1 = su;
r.r_val2 = sg;
return (r.r_vals);
}
static int
idmap_reg(int did)
{
door_handle_t dh;
int err;
cred_t *cr = CRED();
if ((err = secpolicy_idmap(cr)) != 0)
return (set_errno(err));
dh = door_ki_lookup(did);
if (dh == NULL)
return (set_errno(EBADF));
if ((err = idmap_reg_dh(crgetzone(cr), dh)) != 0)
return (set_errno(err));
return (0);
}
static int
idmap_unreg(int did)
{
door_handle_t dh = door_ki_lookup(did);
int res;
zone_t *zone;
if (dh == NULL)
return (set_errno(EINVAL));
zone = crgetzone(CRED());
res = idmap_unreg_dh(zone, dh);
door_ki_rele(dh);
if (res != 0)
return (set_errno(res));
return (0);
}
static uint64_t
idmap_flush_kcache(void)
{
struct door_info di;
door_handle_t dh;
int err;
zone_t *zone = crgetzone(CRED());
dh = idmap_get_door(zone);
if (dh == NULL)
return (set_errno(EPERM));
if ((err = door_ki_info(dh, &di)) != 0) {
door_ki_rele(dh);
return (set_errno(err));
}
door_ki_rele(dh);
if (curproc->p_pid != di.di_target)
return (set_errno(EPERM));
idmap_purge_cache(zone);
return (0);
}
uint64_t
sidsys(int op, int flag, int nuids, int ngids)
{
switch (op) {
case SIDSYS_ALLOC_IDS:
return (allocids(flag, nuids, ngids));
case SIDSYS_IDMAP_REG:
return (idmap_reg(flag));
case SIDSYS_IDMAP_UNREG:
return (idmap_unreg(flag));
case SIDSYS_IDMAP_FLUSH_KCACHE:
return (idmap_flush_kcache());
default:
return (set_errno(EINVAL));
}
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/fault.h>
#include <sys/signal.h>
#include <sys/siginfo.h>
#include <sys/debug.h>
int
sigaction(int sig, struct sigaction *actp, struct sigaction *oactp)
{
struct sigaction act;
struct sigaction oact;
k_sigset_t set;
proc_t *p;
user_t *ua;
int sigcld_look = 0;
if (sig <= 0 || sig >= NSIG ||
(actp != NULL && sigismember(&cantmask, sig)))
return (set_errno(EINVAL));
/*
* act and oact might be the same address, so copyin act first.
*/
if (actp) {
#if defined(__sparc)
void (*handler)();
#endif
if (copyin(actp, &act, sizeof (act)))
return (set_errno(EFAULT));
#if defined(__sparc)
/*
* Check alignment of handler
*/
handler = act.sa_handler;
if (handler != SIG_IGN && handler != SIG_DFL &&
((uintptr_t)handler & 0x3) != 0)
return (set_errno(EINVAL));
#endif
}
p = curproc;
ua = PTOU(p);
mutex_enter(&p->p_lock);
if (oactp) {
int flags;
void (*disp)();
disp = ua->u_signal[sig - 1];
flags = 0;
if (disp != SIG_DFL && disp != SIG_IGN) {
set = ua->u_sigmask[sig-1];
if (sigismember(&p->p_siginfo, sig))
flags |= SA_SIGINFO;
if (sigismember(&ua->u_sigrestart, sig))
flags |= SA_RESTART;
if (sigismember(&ua->u_sigonstack, sig))
flags |= SA_ONSTACK;
if (sigismember(&ua->u_sigresethand, sig))
flags |= SA_RESETHAND;
if (sigismember(&ua->u_signodefer, sig))
flags |= SA_NODEFER;
} else
sigemptyset(&set);
if (sig == SIGCLD) {
if (p->p_flag & SNOWAIT)
flags |= SA_NOCLDWAIT;
if (!(p->p_flag & SJCTL))
flags |= SA_NOCLDSTOP;
}
oact.sa_handler = disp;
oact.sa_flags = flags;
sigktou(&set, &oact.sa_mask);
}
if (actp) {
if (sig == SIGCLD)
sigcld_look = 1;
sigutok(&act.sa_mask, &set);
setsigact(sig, act.sa_handler, &set, act.sa_flags);
}
mutex_exit(&p->p_lock);
if (sigcld_look)
sigcld_repost();
if (oactp &&
copyout(&oact, oactp, sizeof (oact)))
return (set_errno(EFAULT));
return (0);
}
#ifdef _SYSCALL32_IMPL
int
sigaction32(int sig, struct sigaction32 *actp, struct sigaction32 *oactp)
{
struct sigaction32 act32;
struct sigaction32 oact32;
k_sigset_t set;
proc_t *p;
user_t *ua;
int sigcld_look = 0;
if (sig <= 0 || sig >= NSIG ||
(actp != NULL && sigismember(&cantmask, sig)))
return (set_errno(EINVAL));
/*
* act and oact might be the same address, so copyin act first.
*/
if (actp) {
#if defined(__sparc)
void (*handler)();
#endif
if (copyin(actp, &act32, sizeof (act32)))
return (set_errno(EFAULT));
#if defined(__sparc)
/*
* Check alignment of handler
*/
handler = (void (*)())(uintptr_t)act32.sa_handler;
if (handler != SIG_IGN && handler != SIG_DFL &&
((uintptr_t)handler & 0x3) != 0)
return (set_errno(EINVAL));
#endif
}
p = curproc;
ua = PTOU(p);
mutex_enter(&p->p_lock);
if (oactp) {
int flags;
void (*disp)();
disp = ua->u_signal[sig - 1];
flags = 0;
if (disp != SIG_DFL && disp != SIG_IGN) {
set = ua->u_sigmask[sig-1];
if (sigismember(&p->p_siginfo, sig))
flags |= SA_SIGINFO;
if (sigismember(&ua->u_sigrestart, sig))
flags |= SA_RESTART;
if (sigismember(&ua->u_sigonstack, sig))
flags |= SA_ONSTACK;
if (sigismember(&ua->u_sigresethand, sig))
flags |= SA_RESETHAND;
if (sigismember(&ua->u_signodefer, sig))
flags |= SA_NODEFER;
} else
sigemptyset(&set);
if (sig == SIGCLD) {
if (p->p_flag & SNOWAIT)
flags |= SA_NOCLDWAIT;
if (!(p->p_flag & SJCTL))
flags |= SA_NOCLDSTOP;
}
oact32.sa_handler = (caddr32_t)(uintptr_t)disp;
oact32.sa_flags = flags;
sigktou(&set, &oact32.sa_mask);
}
if (actp) {
if (sig == SIGCLD)
sigcld_look = 1;
sigutok(&act32.sa_mask, &set);
setsigact(sig, (void (*)())(uintptr_t)act32.sa_handler, &set,
act32.sa_flags);
}
mutex_exit(&p->p_lock);
if (sigcld_look)
sigcld_repost();
if (oactp &&
copyout(&oact32, oactp, sizeof (oact32)))
return (set_errno(EFAULT));
return (0);
}
#endif /* _SYSCALL32_IMPL */
/*
* 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.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/fault.h>
#include <sys/signal.h>
#include <sys/siginfo.h>
#include <sys/debug.h>
int
sigaltstack(struct sigaltstack *ssp, struct sigaltstack *oss)
{
klwp_t *lwp = ttolwp(curthread);
struct sigaltstack ss;
/*
* User's oss and ss might be the same address, so copyin first and
* save before copying out.
*/
if (ssp) {
if (lwp->lwp_sigaltstack.ss_flags & SS_ONSTACK)
return (set_errno(EPERM));
if (copyin(ssp, &ss, sizeof (ss)))
return (set_errno(EFAULT));
if (ss.ss_flags & ~SS_DISABLE)
return (set_errno(EINVAL));
if (!(ss.ss_flags & SS_DISABLE) && ss.ss_size < MINSIGSTKSZ)
return (set_errno(ENOMEM));
}
if (oss) {
if (copyout(&lwp->lwp_sigaltstack,
oss, sizeof (struct sigaltstack)))
return (set_errno(EFAULT));
}
if (ssp)
lwp->lwp_sigaltstack = ss;
return (0);
}
#ifdef _LP64
int
sigaltstack32(struct sigaltstack32 *ssp, struct sigaltstack32 *oss)
{
klwp_t *lwp = ttolwp(curthread);
struct sigaltstack *ss;
struct sigaltstack32 ss32, oss32;
/*
* User's oss and ss might be the same address, so copyin first and
* save before copying out.
*/
if (ssp) {
if (lwp->lwp_sigaltstack.ss_flags & SS_ONSTACK)
return (set_errno(EPERM));
if (copyin(ssp, &ss32, sizeof (ss32)))
return (set_errno(EFAULT));
if (ss32.ss_flags & ~SS_DISABLE)
return (set_errno(EINVAL));
if (!(ss32.ss_flags & SS_DISABLE) && ss32.ss_size < MINSIGSTKSZ)
return (set_errno(ENOMEM));
}
if (oss) {
/*
* copy to ILP32 struct before copyout.
*/
ss = &lwp->lwp_sigaltstack;
oss32.ss_sp = (caddr32_t)(uintptr_t)ss->ss_sp;
oss32.ss_size = (size32_t)ss->ss_size;
oss32.ss_flags = ss->ss_flags;
if (copyout(&oss32, oss, sizeof (oss32)))
return (set_errno(EFAULT));
}
if (ssp) {
ss = &lwp->lwp_sigaltstack;
ss->ss_sp = (void *)(uintptr_t)ss32.ss_sp;
ss->ss_size = (size_t)ss32.ss_size;
ss->ss_flags = ss32.ss_flags;
}
return (0);
}
#endif /* _LP64 */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/procset.h>
#include <sys/fault.h>
#include <sys/signal.h>
#include <sys/siginfo.h>
#include <sys/schedctl.h>
#include <vm/as.h>
#include <sys/debug.h>
#include <sys/contract/process_impl.h>
/*ARGSUSED*/
static int
copyin_siginfo(model_t datamodel, void *uaddr, k_siginfo_t *ksip)
{
#ifdef _SYSCALL32_IMPL
int ret;
if (datamodel == DATAMODEL_NATIVE) {
#endif
return (copyin(uaddr, ksip, sizeof (k_siginfo_t)));
#ifdef _SYSCALL32_IMPL
} else {
siginfo32_t si32;
if (ret = copyin(uaddr, &si32, sizeof (si32)))
return (ret);
siginfo_32tok(&si32, ksip);
}
return (0);
#endif
}
/*
* To find secured 64 bit id for signotify() call
* This depends upon as_getmemid() which returns
* unique vnode/offset for a user virtual address.
*/
static u_longlong_t
get_sigid(proc_t *p, caddr_t addr)
{
u_longlong_t snid = 0;
memid_t memid;
quad_t *tquad = (quad_t *)&snid;
if (!as_getmemid(p->p_as, addr, &memid)) {
tquad->val[0] = (int)memid.val[0];
tquad->val[1] = (int)memid.val[1];
}
return (snid);
}
#define SIGN_PTR(p, n) &((signotifyq_t *)(&p->p_signhdr[1]))[n];
int
signotify(int cmd, siginfo_t *siginfo, signotify_id_t *sn_id)
{
k_siginfo_t info;
signotify_id_t id;
proc_t *p;
proc_t *cp = curproc;
signotifyq_t *snqp;
struct cred *cr;
sigqueue_t *sqp;
sigqhdr_t *sqh;
u_longlong_t sid;
model_t datamodel = get_udatamodel();
if (copyin(sn_id, &id, sizeof (signotify_id_t)))
return (set_errno(EFAULT));
if (id.sn_index >= _SIGNOTIFY_MAX || id.sn_index < 0)
return (set_errno(EINVAL));
switch (cmd) {
case SN_PROC:
/* get snid for the given user address of signotifyid_t */
sid = get_sigid(cp, (caddr_t)sn_id);
if (id.sn_pid > 0) {
mutex_enter(&pidlock);
if ((p = prfind(id.sn_pid)) != NULL) {
mutex_enter(&p->p_lock);
if (p->p_signhdr != NULL) {
snqp = SIGN_PTR(p, id.sn_index);
if (snqp->sn_snid == sid) {
mutex_exit(&p->p_lock);
mutex_exit(&pidlock);
return (set_errno(EBUSY));
}
}
mutex_exit(&p->p_lock);
}
mutex_exit(&pidlock);
}
if (copyin_siginfo(datamodel, siginfo, &info))
return (set_errno(EFAULT));
/* The si_code value must indicate the signal will be queued */
if (!sigwillqueue(info.si_signo, info.si_code))
return (set_errno(EINVAL));
if (cp->p_signhdr == NULL) {
/* Allocate signotify pool first time */
sqh = sigqhdralloc(sizeof (signotifyq_t),
_SIGNOTIFY_MAX);
mutex_enter(&cp->p_lock);
if (cp->p_signhdr == NULL) {
/* hang the pool head on proc */
cp->p_signhdr = sqh;
} else {
/* another lwp allocated the pool, free ours */
sigqhdrfree(sqh);
}
} else {
mutex_enter(&cp->p_lock);
}
sqp = sigqalloc(cp->p_signhdr);
if (sqp == NULL) {
mutex_exit(&cp->p_lock);
return (set_errno(EAGAIN));
}
cr = CRED();
sqp->sq_info = info;
sqp->sq_info.si_pid = cp->p_pid;
sqp->sq_info.si_ctid = PRCTID(cp);
sqp->sq_info.si_zoneid = getzoneid();
sqp->sq_info.si_uid = crgetruid(cr);
/* fill the signotifyq_t fields */
((signotifyq_t *)sqp)->sn_snid = sid;
mutex_exit(&cp->p_lock);
/* complete the signotify_id_t fields */
id.sn_index = (signotifyq_t *)sqp - SIGN_PTR(cp, 0);
id.sn_pid = cp->p_pid;
break;
case SN_CANCEL:
case SN_SEND:
sid = get_sigid(cp, (caddr_t)sn_id);
mutex_enter(&pidlock);
if ((id.sn_pid <= 0) || ((p = prfind(id.sn_pid)) == NULL)) {
mutex_exit(&pidlock);
return (set_errno(EINVAL));
}
mutex_enter(&p->p_lock);
mutex_exit(&pidlock);
if (p->p_signhdr == NULL) {
mutex_exit(&p->p_lock);
return (set_errno(EINVAL));
}
snqp = SIGN_PTR(p, id.sn_index);
if (snqp->sn_snid == 0) {
mutex_exit(&p->p_lock);
return (set_errno(EINVAL));
}
if (snqp->sn_snid != sid) {
mutex_exit(&p->p_lock);
return (set_errno(EINVAL));
}
snqp->sn_snid = 0;
/* cmd == SN_CANCEL or signo == 0 (SIGEV_NONE) */
if (((sigqueue_t *)snqp)->sq_info.si_signo <= 0)
cmd = SN_CANCEL;
sigqsend(cmd, p, 0, (sigqueue_t *)snqp);
mutex_exit(&p->p_lock);
id.sn_pid = 0;
id.sn_index = 0;
break;
default :
return (set_errno(EINVAL));
}
if (copyout(&id, sn_id, sizeof (signotify_id_t)))
return (set_errno(EFAULT));
return (0);
}
int
sigresend(int sig, siginfo_t *siginfo, sigset_t *mask)
{
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
sigqueue_t *sqp = kmem_zalloc(sizeof (*sqp), KM_SLEEP);
sigset_t set;
k_sigset_t kset;
int error;
if (sig <= 0 || sig >= NSIG || sigismember(&cantmask, sig)) {
error = EINVAL;
goto bad;
}
if (siginfo == NULL) {
sqp->sq_info.si_signo = sig;
sqp->sq_info.si_code = SI_NOINFO;
} else {
if (copyin_siginfo(get_udatamodel(), siginfo, &sqp->sq_info)) {
error = EFAULT;
goto bad;
}
if (sqp->sq_info.si_signo != sig) {
error = EINVAL;
goto bad;
}
}
if (copyin(mask, &set, sizeof (set))) {
error = EFAULT;
goto bad;
}
sigutok(&set, &kset);
/*
* We don't need to acquire p->p_lock here;
* we are manipulating thread-private data.
*/
if (lwp->lwp_cursig || lwp->lwp_curinfo) {
t->t_sig_check = 1;
error = EAGAIN;
goto bad;
}
lwp->lwp_cursig = sig;
lwp->lwp_curinfo = sqp;
schedctl_finish_sigblock(t);
t->t_hold = kset;
t->t_sig_check = 1;
return (0);
bad:
kmem_free(sqp, sizeof (*sqp));
return (set_errno(error));
}
/*
* 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 1994-2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/fault.h>
#include <sys/signal.h>
#include <sys/schedctl.h>
#include <sys/debug.h>
int
sigpending(int flag, sigset_t *setp)
{
sigset_t set;
k_sigset_t kset;
proc_t *p;
switch (flag) {
case 1: /* sigpending */
p = ttoproc(curthread);
mutex_enter(&p->p_lock);
schedctl_finish_sigblock(curthread);
kset = p->p_sig;
sigorset(&kset, &curthread->t_sig);
sigandset(&kset, &curthread->t_hold);
mutex_exit(&p->p_lock);
break;
case 2: /* sigfillset */
kset = fillset;
break;
default:
return (set_errno(EINVAL));
}
sigktou(&kset, &set);
if (copyout((caddr_t)&set, (caddr_t)setp, sizeof (sigset_t)))
return (set_errno(EFAULT));
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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/fault.h>
#include <sys/signal.h>
#include <sys/schedctl.h>
#include <sys/debug.h>
/* ARGSUSED4 */
int64_t
lwp_sigmask(int how, uint_t bits0, uint_t bits1, uint_t bits2, uint_t bits3)
{
kthread_t *t = curthread;
proc_t *p = ttoproc(t);
rval_t rv;
/*
* We don't need to acquire p->p_lock here;
* we are manipulating thread-private data.
*/
schedctl_finish_sigblock(t);
bits0 &= (FILLSET0 & ~CANTMASK0);
bits1 &= (FILLSET1 & ~CANTMASK1);
bits2 &= (FILLSET2 & ~CANTMASK2);
/*
* As a sop to the s10 brand, we continue to return
* the first two words of the signal mask, regardless
* of the value of 'how', even though libc doesn't use them.
*/
rv.r_val1 = t->t_hold.__sigbits[0];
rv.r_val2 = t->t_hold.__sigbits[1];
switch (how) {
case SIG_BLOCK:
t->t_hold.__sigbits[0] |= bits0;
t->t_hold.__sigbits[1] |= bits1;
t->t_hold.__sigbits[2] |= bits2;
break;
case SIG_UNBLOCK:
t->t_hold.__sigbits[0] &= ~bits0;
t->t_hold.__sigbits[1] &= ~bits1;
t->t_hold.__sigbits[2] &= ~bits2;
if (sigcheck(p, t))
t->t_sig_check = 1;
break;
case SIG_SETMASK:
t->t_hold.__sigbits[0] = bits0;
t->t_hold.__sigbits[1] = bits1;
t->t_hold.__sigbits[2] = bits2;
if (sigcheck(p, t))
t->t_sig_check = 1;
break;
}
return (rv.r_vals);
}
int
sigprocmask(int how, sigset_t *setp, sigset_t *osetp)
{
sigset_t set;
sigset_t oset;
k_sigset_t kset;
/*
* User's osetp and setp might be the same address,
* so copyin first and save before copying out.
*/
if (setp) {
switch (how) {
case SIG_BLOCK:
case SIG_UNBLOCK:
case SIG_SETMASK:
break;
default:
return (set_errno(EINVAL));
}
if (copyin((caddr_t)setp, (caddr_t)&set, sizeof (sigset_t)))
return (set_errno(EFAULT));
sigutok(&set, &kset);
}
if (osetp) {
sigktou(&curthread->t_hold, &oset);
if (copyout((caddr_t)&oset, (caddr_t)osetp, sizeof (sigset_t)))
return (set_errno(EFAULT));
}
if (setp) {
(void) lwp_sigmask(how,
kset.__sigbits[0],
kset.__sigbits[1],
kset.__sigbits[2],
0);
}
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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/procset.h>
#include <sys/fault.h>
#include <sys/signal.h>
#include <sys/siginfo.h>
#include <sys/debug.h>
extern rctl_hndl_t rc_process_sigqueue;
static int
sigqkill(pid_t pid, sigsend_t *sigsend)
{
proc_t *p;
int error;
if ((uint_t)sigsend->sig >= NSIG)
return (EINVAL);
if (pid == -1) {
procset_t set;
setprocset(&set, POP_AND, P_ALL, P_MYID, P_ALL, P_MYID);
error = sigsendset(&set, sigsend);
} else if (pid > 0) {
mutex_enter(&pidlock);
if ((p = prfind(pid)) == NULL || p->p_stat == SIDL)
error = ESRCH;
else {
error = sigsendproc(p, sigsend);
if (error == 0 && sigsend->perm == 0)
error = EPERM;
}
mutex_exit(&pidlock);
} else {
int nfound = 0;
pid_t pgid;
if (pid == 0)
pgid = ttoproc(curthread)->p_pgrp;
else
pgid = -pid;
error = 0;
mutex_enter(&pidlock);
for (p = pgfind(pgid); p && !error; p = p->p_pglink) {
if (p->p_stat != SIDL) {
nfound++;
error = sigsendproc(p, sigsend);
}
}
mutex_exit(&pidlock);
if (nfound == 0)
error = ESRCH;
else if (error == 0 && sigsend->perm == 0)
error = EPERM;
}
return (error);
}
/*
* for implementations that don't require binary compatibility,
* the kill system call may be made into a library call to the
* sigsend system call
*/
int
kill(pid_t pid, int sig)
{
int error;
sigsend_t v;
bzero(&v, sizeof (v));
v.sig = sig;
v.checkperm = 1;
v.sicode = SI_USER;
if ((error = sigqkill(pid, &v)) != 0)
return (set_errno(error));
return (0);
}
/*
* The handling of small unions, like the sigval argument to sigqueue,
* is architecture dependent. We have adopted the convention that the
* value itself is passed in the storage which crosses the kernel
* protection boundary. This procedure will accept a scalar argument,
* and store it in the appropriate value member of the sigsend_t structure.
*/
int
sigqueue(pid_t pid, int sig, /* union sigval */ void *value,
int si_code, int block)
{
int error;
sigsend_t v;
sigqhdr_t *sqh;
proc_t *p = curproc;
/* The si_code value must indicate the signal will be queued */
if (pid <= 0 || !sigwillqueue(sig, si_code))
return (set_errno(EINVAL));
if ((sqh = p->p_sigqhdr) == NULL) {
rlim64_t sigqsz_max;
mutex_enter(&p->p_lock);
sigqsz_max = rctl_enforced_value(rc_process_sigqueue,
p->p_rctls, p);
mutex_exit(&p->p_lock);
/* Allocate sigqueue pool first time */
sqh = sigqhdralloc(sizeof (sigqueue_t), (uint_t)sigqsz_max);
mutex_enter(&p->p_lock);
if (p->p_sigqhdr == NULL) {
/* hang the pool head on proc */
p->p_sigqhdr = sqh;
} else {
/* another lwp allocated the pool, free ours */
sigqhdrfree(sqh);
sqh = p->p_sigqhdr;
}
mutex_exit(&p->p_lock);
}
do {
bzero(&v, sizeof (v));
v.sig = sig;
v.checkperm = 1;
v.sicode = si_code;
v.value.sival_ptr = value;
if ((error = sigqkill(pid, &v)) != EAGAIN || !block)
break;
/* block waiting for another chance to allocate a sigqueue_t */
mutex_enter(&sqh->sqb_lock);
while (sqh->sqb_count == 0) {
if (!cv_wait_sig(&sqh->sqb_cv, &sqh->sqb_lock)) {
error = EINTR;
break;
}
}
mutex_exit(&sqh->sqb_lock);
} while (error == EAGAIN);
if (error)
return (set_errno(error));
return (0);
}
#ifdef _SYSCALL32_IMPL
/*
* sigqueue32 - System call entry point for 32-bit callers on LP64 kernel,
* needed to handle the 32-bit sigvals as correctly as we can. We always
* assume that a 32-bit caller is passing an int. A 64-bit recipient
* that expects an int will therefore get it correctly. A 32-bit
* recipient will also get it correctly since siginfo_kto32() uses
* sival_int in the conversion. Since a 32-bit pointer has the same
* size and address in the sigval, it also converts correctly so that
* two 32-bit apps can exchange a pointer value. However, this means
* that a pointer sent by a 32-bit caller will be seen in the upper half
* by a 64-bit recipient, and only the upper half of a 64-bit pointer will
* be seen by a 32-bit recipient. This is the best solution that does
* not require severe hacking of the sigval union. Anyways, what it
* means to be sending pointers between processes with dissimilar
* models is unclear.
*/
int
sigqueue32(pid_t pid, int sig, /* union sigval32 */ caddr32_t value,
int si_code, int block)
{
union sigval sv;
bzero(&sv, sizeof (sv));
sv.sival_int = (int)value;
return (sigqueue(pid, sig, sv.sival_ptr, si_code, block));
}
#endif
/*
* 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 (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* Copyright (c) 1994 Sun Microsystems, Inc. */
/* All Rights Reserved */
/*
* Copyright (c) 1998 by Sun Microsystems, Inc.
* All rights reserved.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/fault.h>
#include <sys/procset.h>
#include <sys/signal.h>
#include <sys/siginfo.h>
#include <sys/debug.h>
int
sigsendsys(procset_t *psp, int sig)
{
int error;
procset_t set;
sigsend_t v;
if (sig < 0 || sig >= NSIG)
return (set_errno(EINVAL));
bzero(&v, sizeof (v));
v.sig = sig;
v.checkperm = 1;
v.sicode = SI_USER;
if (copyin((caddr_t)psp, (caddr_t)&set, sizeof (procset_t)))
return (set_errno(EFAULT));
if (error = sigsendset(&set, &v))
return (set_errno(error));
return (0);
}
/*
* 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 (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 1994-2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/fault.h>
#include <sys/procset.h>
#include <sys/signal.h>
#include <sys/schedctl.h>
#include <sys/debug.h>
int
sigsuspend(sigset_t *setp)
{
sigset_t set;
k_sigset_t kset;
proc_t *p = curproc;
if (copyin((caddr_t)setp, (caddr_t)&set, sizeof (sigset_t)))
return (set_errno(EFAULT));
sigutok(&set, &kset);
mutex_enter(&p->p_lock);
schedctl_finish_sigblock(curthread);
ttolwp(curthread)->lwp_sigoldmask = curthread->t_hold;
curthread->t_hold = kset;
curthread->t_sig_check = 1; /* so post-syscall will re-evaluate */
curthread->t_flag |= T_TOMASK;
/* pause() */
while (cv_wait_sig_swap(&curthread->t_delay_cv, &p->p_lock))
;
mutex_exit(&p->p_lock);
return (set_errno(EINTR));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/bitmap.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/fault.h>
#include <sys/procset.h>
#include <sys/signal.h>
#include <sys/siginfo.h>
#include <sys/time.h>
#include <sys/kmem.h>
#include <sys/schedctl.h>
#include <sys/debug.h>
#include <sys/condvar_impl.h>
#include <sys/model.h>
#include <sys/sdt.h>
#include <sys/zone.h>
static int
copyout_siginfo(model_t datamodel, k_siginfo_t *ksip, void *uaddr)
{
zoneid_t zoneid = getzoneid();
if (datamodel == DATAMODEL_NATIVE) {
if (SI_FROMUSER(ksip) && zoneid != GLOBAL_ZONEID &&
zoneid != ksip->si_zoneid) {
k_siginfo_t sani_sip = *ksip;
sani_sip.si_pid = curproc->p_zone->zone_zsched->p_pid;
sani_sip.si_uid = 0;
sani_sip.si_ctid = -1;
sani_sip.si_zoneid = zoneid;
if (copyout(&sani_sip, uaddr, sizeof (sani_sip)))
return (set_errno(EFAULT));
} else {
if (copyout(ksip, uaddr, sizeof (*ksip)))
return (set_errno(EFAULT));
}
}
#ifdef _SYSCALL32_IMPL
else {
siginfo32_t si32;
siginfo_kto32(ksip, &si32);
if (SI_FROMUSER(ksip) && zoneid != GLOBAL_ZONEID &&
zoneid != ksip->si_zoneid) {
si32.si_pid = curproc->p_zone->zone_zsched->p_pid;
si32.si_uid = 0;
si32.si_ctid = -1;
si32.si_zoneid = zoneid;
}
if (copyout(&si32, uaddr, sizeof (si32)))
return (set_errno(EFAULT));
}
#endif
return (ksip->si_signo);
}
/*
* Wait until a signal within a specified set is posted or until the
* time interval 'timeout' if specified. The signal is caught but
* not delivered. The value of the signal is returned to the caller.
*/
int
sigtimedwait(sigset_t *setp, siginfo_t *siginfop, timespec_t *timeoutp)
{
sigset_t set;
k_sigset_t oldmask;
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
proc_t *p = ttoproc(t);
timespec_t sig_timeout;
timespec_t *rqtp = NULL;
int timecheck = 0;
int ret;
int error = 0;
k_siginfo_t info, *infop;
model_t datamodel = get_udatamodel();
if (timeoutp) {
timespec_t now;
timecheck = timechanged;
gethrestime(&now);
if (datamodel == DATAMODEL_NATIVE) {
if (copyin(timeoutp, &sig_timeout,
sizeof (sig_timeout)))
return (set_errno(EFAULT));
} else {
timespec32_t timeout32;
if (copyin(timeoutp, &timeout32, sizeof (timeout32)))
return (set_errno(EFAULT));
TIMESPEC32_TO_TIMESPEC(&sig_timeout, &timeout32)
}
if (itimerspecfix(&sig_timeout))
return (set_errno(EINVAL));
/*
* Convert the timespec value into absolute time.
*/
timespecadd(&sig_timeout, &now);
rqtp = &sig_timeout;
}
if (copyin(setp, &set, sizeof (set)))
return (set_errno(EFAULT));
sigutok(&set, &t->t_sigwait);
if (sigisempty(&t->t_sigwait))
return (set_errno(EINVAL));
mutex_enter(&p->p_lock);
/*
* set the thread's signal mask to unmask
* those signals in the specified set.
*/
schedctl_finish_sigblock(t);
oldmask = t->t_hold;
sigdiffset(&t->t_hold, &t->t_sigwait);
/*
* Wait until we take a signal or until
* the absolute future time is passed.
*/
while ((ret = cv_waituntil_sig(&t->t_delay_cv, &p->p_lock,
rqtp, timecheck)) > 0)
continue;
if (ret == -1)
error = EAGAIN;
/*
* Restore thread's signal mask to its previous value.
*/
t->t_hold = oldmask;
t->t_sig_check = 1; /* so post_syscall sees new t_hold mask */
if (error) {
mutex_exit(&p->p_lock);
sigemptyset(&t->t_sigwait);
return (set_errno(error)); /* timer expired */
}
/*
* Don't bother with signal if it is not in request set.
*/
if (lwp->lwp_cursig == 0 ||
!sigismember(&t->t_sigwait, lwp->lwp_cursig)) {
mutex_exit(&p->p_lock);
/*
* lwp_cursig is zero if pokelwps() awakened cv_wait_sig().
* This happens if some other thread in this process called
* forkall() or exit().
*/
sigemptyset(&t->t_sigwait);
return (set_errno(EINTR));
}
if (lwp->lwp_curinfo)
infop = &lwp->lwp_curinfo->sq_info;
else {
infop = &info;
bzero(infop, sizeof (info));
infop->si_signo = lwp->lwp_cursig;
infop->si_code = SI_NOINFO;
}
lwp->lwp_ru.nsignals++;
ret = lwp->lwp_cursig;
DTRACE_PROC2(signal__clear, int, ret, ksiginfo_t *, infop);
lwp->lwp_cursig = 0;
lwp->lwp_extsig = 0;
mutex_exit(&p->p_lock);
if (siginfop)
ret = copyout_siginfo(datamodel, infop, siginfop);
if (lwp->lwp_curinfo) {
siginfofree(lwp->lwp_curinfo);
lwp->lwp_curinfo = NULL;
}
sigemptyset(&t->t_sigwait);
return (ret);
}
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
/*
* Get file attribute information through a file name or a file descriptor.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/pathname.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/mode.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/debug.h>
#include <sys/cmn_err.h>
#include <c2/audit.h>
#include <fs/fs_subr.h>
/*
* Get the vp to be stated and the cred to be used for the call
* to VOP_GETATTR
*/
static int
cstatat_getvp(int fd, char *name, int follow, vnode_t **vp, cred_t **cred)
{
vnode_t *startvp;
file_t *fp;
int error;
cred_t *cr;
int estale_retry = 0;
*vp = NULL;
/*
* Only return EFAULT for fstatat when fd == AT_FDCWD && name == NULL
*/
if (fd == AT_FDCWD) {
startvp = NULL;
cr = CRED();
crhold(cr);
} else {
char startchar;
if (copyin(name, &startchar, sizeof (char)))
return (EFAULT);
if (startchar != '/') {
if ((fp = getf(fd)) == NULL) {
return (EBADF);
}
startvp = fp->f_vnode;
cr = fp->f_cred;
crhold(cr);
VN_HOLD(startvp);
releasef(fd);
} else {
startvp = NULL;
cr = CRED();
crhold(cr);
}
}
*cred = cr;
if (AU_AUDITING() && startvp != NULL)
audit_setfsat_path(1);
lookup:
if (error = lookupnameat(name, UIO_USERSPACE, follow, NULLVPP,
vp, startvp)) {
if ((error == ESTALE) &&
fs_need_estale_retry(estale_retry++))
goto lookup;
if (startvp != NULL)
VN_RELE(startvp);
crfree(cr);
return (error);
}
if (startvp != NULL)
VN_RELE(startvp);
return (0);
}
/*
* Native syscall interfaces:
*
* N-bit kernel, N-bit applications, N-bit file offsets
*/
static int cstatat(int, char *, struct stat *, int, int);
static int cstat(vnode_t *vp, struct stat *, int, cred_t *);
/*
* fstat can and should be fast, do an inline implementation here.
*/
#define FSTAT_BODY(fd, sb, statfn) \
{ \
file_t *fp; \
int error; \
\
if (fd == AT_FDCWD) \
return (set_errno(EFAULT)); \
if ((fp = getf(fd)) == NULL) \
return (set_errno(EBADF)); \
error = statfn(fp->f_vnode, sb, 0, fp->f_cred); \
releasef(fd); \
if (error) \
return (set_errno(error)); \
return (0); \
}
int
fstat(int fd, struct stat *sb)
{
FSTAT_BODY(fd, sb, cstat)
}
int
fstatat(int fd, char *name, struct stat *sb, int flags)
{
int followflag;
int csflags;
if (name == NULL)
return (fstat(fd, sb));
followflag = (flags & AT_SYMLINK_NOFOLLOW);
csflags = (flags & _AT_TRIGGER ? ATTR_TRIGGER : 0);
if (followflag == 0)
csflags |= ATTR_REAL; /* flag for procfs lookups */
return (cstatat(fd, name, sb, followflag, csflags));
}
int
stat(char *name, struct stat *sb)
{
return (fstatat(AT_FDCWD, name, sb, 0));
}
int
lstat(char *name, struct stat *sb)
{
return (fstatat(AT_FDCWD, name, sb, AT_SYMLINK_NOFOLLOW));
}
/*
* Common code for stat(), lstat(), and fstat().
* (32-bit kernel, 32-bit applications, 32-bit files)
* (64-bit kernel, 64-bit applications, 64-bit files)
*/
static int
cstat(vnode_t *vp, struct stat *ubp, int flag, cred_t *cr)
{
struct vfssw *vswp;
struct stat sb;
vattr_t vattr;
int error;
vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
if ((error = VOP_GETATTR(vp, &vattr, flag, cr, NULL)) != 0)
return (error);
#ifdef _ILP32
/*
* (32-bit kernel, 32-bit applications, 32-bit files)
* NOTE: 32-bit kernel maintains a 64-bit unsigend va_size.
*
* st_size of devices (VBLK and VCHR special files) is a special case.
* POSIX does not define size behavior for special files, so the
* following Solaris specific behavior is not a violation. Solaris
* returns the size of the device.
*
* For compatibility with 32-bit programs which happen to do stat() on
* a (mknod) bigger than 2GB we suppress the large file EOVERFLOW and
* instead we return the value MAXOFF32_T (LONG_MAX).
*
* 32-bit applications that care about the size of devices should be
* built 64-bit or use a large file interface (lfcompile(7) or lf64(7)).
*/
if ((vattr.va_size > MAXOFF32_T) &&
((vp->v_type == VBLK) || (vp->v_type == VCHR))) {
/* OVERFLOW | UNKNOWN_SIZE */
vattr.va_size = MAXOFF32_T;
}
#endif /* _ILP32 */
if (vattr.va_size > MAXOFF_T || vattr.va_nblocks > LONG_MAX ||
vattr.va_nodeid > ULONG_MAX)
return (EOVERFLOW);
bzero(&sb, sizeof (sb));
sb.st_dev = vattr.va_fsid;
sb.st_ino = (ino_t)vattr.va_nodeid;
sb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
sb.st_nlink = vattr.va_nlink;
sb.st_uid = vattr.va_uid;
sb.st_gid = vattr.va_gid;
sb.st_rdev = vattr.va_rdev;
sb.st_size = (off_t)vattr.va_size;
sb.st_atim = vattr.va_atime;
sb.st_mtim = vattr.va_mtime;
sb.st_ctim = vattr.va_ctime;
sb.st_blksize = vattr.va_blksize;
sb.st_blocks = (blkcnt_t)vattr.va_nblocks;
if (vp->v_vfsp != NULL) {
vswp = &vfssw[vp->v_vfsp->vfs_fstype];
if (vswp->vsw_name && *vswp->vsw_name)
(void) strcpy(sb.st_fstype, vswp->vsw_name);
}
if (copyout(&sb, ubp, sizeof (sb)))
return (EFAULT);
return (0);
}
static int
cstatat(int fd, char *name, struct stat *sb, int follow, int flags)
{
vnode_t *vp;
int error;
cred_t *cred;
int link_follow;
int estale_retry = 0;
link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
lookup:
if (error = cstatat_getvp(fd, name, link_follow, &vp, &cred))
return (set_errno(error));
error = cstat(vp, sb, flags, cred);
crfree(cred);
VN_RELE(vp);
if (error != 0) {
if (error == ESTALE &&
fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (0);
}
#if defined(_SYSCALL32_IMPL)
/*
* 64-bit kernel, 32-bit applications, 32-bit file offsets
*/
static int cstatat32(int, char *, struct stat32 *, int, int);
static int cstat32(vnode_t *, struct stat32 *, int, cred_t *);
int
fstat32(int fd, struct stat32 *sb)
{
FSTAT_BODY(fd, sb, cstat32)
}
int
fstatat32(int fd, char *name, struct stat32 *sb, int flags)
{
int followflag;
int csflags;
if (name == NULL)
return (fstat32(fd, sb));
followflag = (flags & AT_SYMLINK_NOFOLLOW);
csflags = (flags & _AT_TRIGGER ? ATTR_TRIGGER : 0);
if (followflag == 0)
csflags |= ATTR_REAL; /* flag for procfs lookups */
return (cstatat32(fd, name, sb, followflag, csflags));
}
int
stat32(char *name, struct stat32 *sb)
{
return (fstatat32(AT_FDCWD, name, sb, 0));
}
int
lstat32(char *name, struct stat32 *sb)
{
return (fstatat32(AT_FDCWD, name, sb, AT_SYMLINK_NOFOLLOW));
}
static int
cstat32(vnode_t *vp, struct stat32 *ubp, int flag, struct cred *cr)
{
struct vfssw *vswp;
struct stat32 sb;
vattr_t vattr;
int error;
dev32_t st_dev, st_rdev;
vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
if (error = VOP_GETATTR(vp, &vattr, flag, cr, NULL))
return (error);
/* devices are a special case, see comments in cstat */
if ((vattr.va_size > MAXOFF32_T) &&
((vp->v_type == VBLK) || (vp->v_type == VCHR))) {
/* OVERFLOW | UNKNOWN_SIZE */
vattr.va_size = MAXOFF32_T;
}
/* check for large values */
if (!cmpldev(&st_dev, vattr.va_fsid) ||
!cmpldev(&st_rdev, vattr.va_rdev) ||
vattr.va_size > MAXOFF32_T ||
vattr.va_nblocks > INT32_MAX ||
vattr.va_nodeid > UINT32_MAX ||
TIMESPEC_OVERFLOW(&(vattr.va_atime)) ||
TIMESPEC_OVERFLOW(&(vattr.va_mtime)) ||
TIMESPEC_OVERFLOW(&(vattr.va_ctime)))
return (EOVERFLOW);
bzero(&sb, sizeof (sb));
sb.st_dev = st_dev;
sb.st_ino = (ino32_t)vattr.va_nodeid;
sb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
sb.st_nlink = vattr.va_nlink;
sb.st_uid = vattr.va_uid;
sb.st_gid = vattr.va_gid;
sb.st_rdev = st_rdev;
sb.st_size = (off32_t)vattr.va_size;
TIMESPEC_TO_TIMESPEC32(&(sb.st_atim), &(vattr.va_atime));
TIMESPEC_TO_TIMESPEC32(&(sb.st_mtim), &(vattr.va_mtime));
TIMESPEC_TO_TIMESPEC32(&(sb.st_ctim), &(vattr.va_ctime));
sb.st_blksize = vattr.va_blksize;
sb.st_blocks = (blkcnt32_t)vattr.va_nblocks;
if (vp->v_vfsp != NULL) {
vswp = &vfssw[vp->v_vfsp->vfs_fstype];
if (vswp->vsw_name && *vswp->vsw_name)
(void) strcpy(sb.st_fstype, vswp->vsw_name);
}
if (copyout(&sb, ubp, sizeof (sb)))
return (EFAULT);
return (0);
}
static int
cstatat32(int fd, char *name, struct stat32 *sb, int follow, int flags)
{
vnode_t *vp;
int error;
cred_t *cred;
int link_follow;
int estale_retry = 0;
link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
lookup:
if (error = cstatat_getvp(fd, name, link_follow, &vp, &cred))
return (set_errno(error));
error = cstat32(vp, sb, flags, cred);
crfree(cred);
VN_RELE(vp);
if (error != 0) {
if (error == ESTALE &&
fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (0);
}
#endif /* _SYSCALL32_IMPL */
#if defined(_ILP32)
/*
* 32-bit kernel, 32-bit applications, 64-bit file offsets.
*
* These routines are implemented differently on 64-bit kernels.
*/
static int cstatat64(int, char *, struct stat64 *, int, int);
static int cstat64(vnode_t *, struct stat64 *, int, cred_t *);
int
fstat64(int fd, struct stat64 *sb)
{
FSTAT_BODY(fd, sb, cstat64)
}
int
fstatat64(int fd, char *name, struct stat64 *sb, int flags)
{
int followflag;
int csflags;
if (name == NULL)
return (fstat64(fd, sb));
followflag = (flags & AT_SYMLINK_NOFOLLOW);
csflags = (flags & _AT_TRIGGER ? ATTR_TRIGGER : 0);
if (followflag == 0)
csflags |= ATTR_REAL; /* flag for procfs lookups */
return (cstatat64(fd, name, sb, followflag, csflags));
}
int
stat64(char *name, struct stat64 *sb)
{
return (fstatat64(AT_FDCWD, name, sb, 0));
}
int
lstat64(char *name, struct stat64 *sb)
{
return (fstatat64(AT_FDCWD, name, sb, AT_SYMLINK_NOFOLLOW));
}
static int
cstat64(vnode_t *vp, struct stat64 *ubp, int flag, cred_t *cr)
{
struct vfssw *vswp;
struct stat64 lsb;
vattr_t vattr;
int error;
vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
if (error = VOP_GETATTR(vp, &vattr, flag, cr, NULL))
return (error);
bzero(&lsb, sizeof (lsb));
lsb.st_dev = vattr.va_fsid;
lsb.st_ino = vattr.va_nodeid;
lsb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
lsb.st_nlink = vattr.va_nlink;
lsb.st_uid = vattr.va_uid;
lsb.st_gid = vattr.va_gid;
lsb.st_rdev = vattr.va_rdev;
lsb.st_size = vattr.va_size;
lsb.st_atim = vattr.va_atime;
lsb.st_mtim = vattr.va_mtime;
lsb.st_ctim = vattr.va_ctime;
lsb.st_blksize = vattr.va_blksize;
lsb.st_blocks = vattr.va_nblocks;
if (vp->v_vfsp != NULL) {
vswp = &vfssw[vp->v_vfsp->vfs_fstype];
if (vswp->vsw_name && *vswp->vsw_name)
(void) strcpy(lsb.st_fstype, vswp->vsw_name);
}
if (copyout(&lsb, ubp, sizeof (lsb)))
return (EFAULT);
return (0);
}
static int
cstatat64(int fd, char *name, struct stat64 *sb, int follow, int flags)
{
vnode_t *vp;
int error;
cred_t *cred;
int link_follow;
int estale_retry = 0;
link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
lookup:
if (error = cstatat_getvp(fd, name, link_follow, &vp, &cred))
return (set_errno(error));
error = cstat64(vp, sb, flags, cred);
crfree(cred);
VN_RELE(vp);
if (error != 0) {
if (error == ESTALE &&
fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (0);
}
#endif /* _ILP32 */
#if defined(_SYSCALL32_IMPL)
/*
* 64-bit kernel, 32-bit applications, 64-bit file offsets.
*
* We'd really like to call the "native" stat calls for these ones,
* but the problem is that the 64-bit ABI defines the 'stat64' structure
* differently from the way the 32-bit ABI defines it.
*/
static int cstatat64_32(int, char *, struct stat64_32 *, int, int);
static int cstat64_32(vnode_t *, struct stat64_32 *, int, cred_t *);
int
fstat64_32(int fd, struct stat64_32 *sb)
{
FSTAT_BODY(fd, sb, cstat64_32)
}
int
fstatat64_32(int fd, char *name, struct stat64_32 *sb, int flags)
{
int followflag;
int csflags;
if (name == NULL)
return (fstat64_32(fd, sb));
followflag = (flags & AT_SYMLINK_NOFOLLOW);
csflags = (flags & _AT_TRIGGER ? ATTR_TRIGGER : 0);
if (followflag == 0)
csflags |= ATTR_REAL; /* flag for procfs lookups */
return (cstatat64_32(fd, name, sb, followflag, csflags));
}
int
stat64_32(char *name, struct stat64_32 *sb)
{
return (fstatat64_32(AT_FDCWD, name, sb, 0));
}
int
lstat64_32(char *name, struct stat64_32 *sb)
{
return (fstatat64_32(AT_FDCWD, name, sb, AT_SYMLINK_NOFOLLOW));
}
static int
cstat64_32(vnode_t *vp, struct stat64_32 *ubp, int flag, cred_t *cr)
{
struct vfssw *vswp;
struct stat64_32 lsb;
vattr_t vattr;
int error;
dev32_t st_dev, st_rdev;
vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE;
if (error = VOP_GETATTR(vp, &vattr, flag, cr, NULL))
return (error);
if (!cmpldev(&st_dev, vattr.va_fsid) ||
!cmpldev(&st_rdev, vattr.va_rdev) ||
TIMESPEC_OVERFLOW(&(vattr.va_atime)) ||
TIMESPEC_OVERFLOW(&(vattr.va_mtime)) ||
TIMESPEC_OVERFLOW(&(vattr.va_ctime)))
return (EOVERFLOW);
bzero(&lsb, sizeof (lsb));
lsb.st_dev = st_dev;
lsb.st_ino = vattr.va_nodeid;
lsb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode;
lsb.st_nlink = vattr.va_nlink;
lsb.st_uid = vattr.va_uid;
lsb.st_gid = vattr.va_gid;
lsb.st_rdev = st_rdev;
lsb.st_size = vattr.va_size;
TIMESPEC_TO_TIMESPEC32(&(lsb.st_atim), &(vattr.va_atime));
TIMESPEC_TO_TIMESPEC32(&(lsb.st_mtim), &(vattr.va_mtime));
TIMESPEC_TO_TIMESPEC32(&(lsb.st_ctim), &(vattr.va_ctime));
lsb.st_blksize = vattr.va_blksize;
lsb.st_blocks = vattr.va_nblocks;
if (vp->v_vfsp != NULL) {
vswp = &vfssw[vp->v_vfsp->vfs_fstype];
if (vswp->vsw_name && *vswp->vsw_name)
(void) strcpy(lsb.st_fstype, vswp->vsw_name);
}
if (copyout(&lsb, ubp, sizeof (lsb)))
return (EFAULT);
return (0);
}
static int
cstatat64_32(int fd, char *name, struct stat64_32 *sb, int follow, int flags)
{
vnode_t *vp;
int error;
cred_t *cred;
int link_follow;
int estale_retry = 0;
link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW;
lookup:
if (error = cstatat_getvp(fd, name, link_follow, &vp, &cred))
return (set_errno(error));
error = cstat64_32(vp, sb, flags, cred);
crfree(cred);
VN_RELE(vp);
if (error != 0) {
if (error == ESTALE &&
fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (0);
}
#endif /* _SYSCALL32_IMPL */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/inttypes.h>
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/fstyp.h>
#include <sys/systm.h>
#include <sys/vfs.h>
#include <sys/statfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/pathname.h>
#include <vm/page.h>
#include <fs/fs_subr.h>
#if defined(_SYSCALL32_IMPL) || defined(_ILP32)
/*
* statfs(2) and fstatfs(2) have been replaced by statvfs(2) and
* fstatvfs(2) and will be removed from the system in a near-future
* release.
*
* Supported here purely for 32-bit compatibility.
*/
static int cstatfs(struct vfs *, struct statfs32 *, int);
int
statfs32(char *fname, struct statfs32 *sbp, int32_t len, int32_t fstyp)
{
vnode_t *vp;
int error;
int estale_retry = 0;
lookup:
if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
if (fstyp != 0)
error = EINVAL;
else
error = cstatfs(vp->v_vfsp, sbp, len);
VN_RELE(vp);
if (error) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (0);
}
int
fstatfs32(int32_t fdes, struct statfs32 *sbp, int32_t len, int32_t fstyp)
{
struct file *fp;
int error;
if (fstyp != 0)
return (set_errno(EINVAL));
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
error = cstatfs(fp->f_vnode->v_vfsp, sbp, len);
releasef(fdes);
if (error)
return (set_errno(error));
return (0);
}
/*
* Common routine for fstatfs and statfs.
*/
static int
cstatfs(struct vfs *vfsp, struct statfs32 *sbp, int len)
{
struct statfs32 sfs;
struct statvfs64 svfs;
int error, i;
char *cp, *cp2;
struct vfssw *vswp;
if (len < 0 || len > sizeof (struct statfs))
return (EINVAL);
if (error = VFS_STATVFS(vfsp, &svfs))
return (error);
if (svfs.f_blocks > UINT32_MAX || svfs.f_bfree > UINT32_MAX ||
svfs.f_files > UINT32_MAX || svfs.f_ffree > UINT32_MAX)
return (EOVERFLOW);
/*
* Map statvfs fields into the old statfs structure.
*/
bzero(&sfs, sizeof (sfs));
sfs.f_bsize = svfs.f_bsize;
sfs.f_frsize = (svfs.f_frsize == svfs.f_bsize) ? 0 : svfs.f_frsize;
sfs.f_blocks = svfs.f_blocks * (svfs.f_frsize / 512);
sfs.f_bfree = svfs.f_bfree * (svfs.f_frsize / 512);
sfs.f_files = svfs.f_files;
sfs.f_ffree = svfs.f_ffree;
cp = svfs.f_fstr;
cp2 = sfs.f_fname;
i = 0;
while (i++ < sizeof (sfs.f_fname))
if (*cp != '\0')
*cp2++ = *cp++;
else
*cp2++ = '\0';
while (*cp != '\0' &&
i++ < (sizeof (svfs.f_fstr) - sizeof (sfs.f_fpack)))
cp++;
(void) strncpy(sfs.f_fpack, cp + 1, sizeof (sfs.f_fpack));
if ((vswp = vfs_getvfssw(svfs.f_basetype)) == NULL)
sfs.f_fstyp = 0;
else {
sfs.f_fstyp = vswp - vfssw;
vfs_unrefvfssw(vswp);
}
if (copyout(&sfs, sbp, len))
return (EFAULT);
return (0);
}
#endif /* _SYSCALL32_IMPL || _ILP32 */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
/*
* Get file system statistics (statvfs and fstatvfs).
*/
#include <sys/types.h>
#include <sys/inttypes.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/fstyp.h>
#include <sys/systm.h>
#include <sys/vfs.h>
#include <sys/statvfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/pathname.h>
#include <vm/page.h>
#include <fs/fs_subr.h>
#define STATVFSCOPY(dst, src) \
(dst)->f_bsize = (src)->f_bsize; \
(dst)->f_frsize = (src)->f_frsize; \
(dst)->f_blocks = (src)->f_blocks; \
(dst)->f_bfree = (src)->f_bfree; \
(dst)->f_bavail = (src)->f_bavail; \
(dst)->f_files = (src)->f_files; \
(dst)->f_ffree = (src)->f_ffree; \
(dst)->f_favail = (src)->f_favail; \
(dst)->f_fsid = (src)->f_fsid; \
bcopy((src)->f_basetype, (dst)->f_basetype, \
sizeof ((dst)->f_basetype)); \
(dst)->f_flag = (src)->f_flag; \
(dst)->f_namemax = (src)->f_namemax; \
bcopy((src)->f_fstr, (dst)->f_fstr, \
sizeof ((dst)->f_fstr))
/*
* Common routines for statvfs and fstatvfs.
*/
static int
cstatvfs32(struct vfs *vfsp, struct statvfs32 *ubp)
{
struct statvfs64 ds64;
struct statvfs32 ds32;
int error;
#if !defined(lint)
ASSERT32(sizeof (struct statvfs) == sizeof (struct statvfs32));
ASSERT32(sizeof (struct statvfs64) == sizeof (struct statvfs64_32));
#endif
bzero(&ds64, sizeof (ds64));
if ((error = VFS_STATVFS(vfsp, &ds64)) != 0)
return (error);
/*
* VFS_STATVFS can return data that is incompatible with the space
* available the 32-bit statvfs structure. Check here to see if
* it will fit into the 32-bit structure, if not, return EOVERFLOW.
*
* The check for -1 is because some file systems return -1 in the
* fields that are irrelevant or nonessential, and we do not want
* to return EOVERFLOW for them. For example: df is expected to
* show -1 in the output for some of these fields on NFS mounted
* filesystems.
*/
if (ds64.f_files == (fsfilcnt64_t)-1)
ds64.f_files = UINT32_MAX;
if (ds64.f_ffree == (fsfilcnt64_t)-1)
ds64.f_ffree = UINT32_MAX;
if (ds64.f_favail == (fsfilcnt64_t)-1)
ds64.f_favail = UINT32_MAX;
if (ds64.f_bavail == (fsblkcnt64_t)-1)
ds64.f_bavail = UINT32_MAX;
if (ds64.f_bfree == (fsblkcnt64_t)-1)
ds64.f_bfree = UINT32_MAX;
if (ds64.f_blocks > UINT32_MAX || ds64.f_bfree > UINT32_MAX ||
ds64.f_bavail > UINT32_MAX || ds64.f_files > UINT32_MAX ||
ds64.f_ffree > UINT32_MAX || ds64.f_favail > UINT32_MAX)
return (EOVERFLOW);
#ifdef _LP64
/*
* On the 64-bit kernel, even these fields grow to 64-bit
* quantities in the statvfs64 structure.
*/
if (ds64.f_namemax == (ulong_t)-1l)
ds64.f_namemax = UINT32_MAX;
if (ds64.f_bsize > UINT32_MAX || ds64.f_frsize > UINT32_MAX ||
ds64.f_fsid > UINT32_MAX || ds64.f_flag > UINT32_MAX ||
ds64.f_namemax > UINT32_MAX)
return (EOVERFLOW);
#endif
bzero(&ds32, sizeof (ds32));
STATVFSCOPY(&ds32, &ds64);
if (copyout(&ds32, ubp, sizeof (ds32)) != 0)
return (EFAULT);
return (0);
}
static int
cstatvfs64(struct vfs *vfsp, struct statvfs64 *ubp)
{
struct statvfs64 ds64;
int error;
#if !defined(lint)
ASSERT64(sizeof (struct statvfs) == sizeof (struct statvfs64));
#endif
bzero(&ds64, sizeof (ds64));
if ((error = VFS_STATVFS(vfsp, &ds64)) != 0)
return (error);
if (copyout(&ds64, ubp, sizeof (ds64)) != 0)
return (EFAULT);
return (0);
}
/*
* Native system calls
*/
int
statvfs(char *fname, struct statvfs *sbp)
{
vnode_t *vp;
int error;
int estale_retry = 0;
lookup:
if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
#ifdef _LP64
error = cstatvfs64(vp->v_vfsp, (struct statvfs64 *)sbp);
#else
error = cstatvfs32(vp->v_vfsp, (struct statvfs32 *)sbp);
#endif
VN_RELE(vp);
if (error) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (0);
}
int
fstatvfs(int fdes, struct statvfs *sbp)
{
struct file *fp;
int error;
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
#ifdef _LP64
error = cstatvfs64(fp->f_vnode->v_vfsp, (struct statvfs64 *)sbp);
#else
error = cstatvfs32(fp->f_vnode->v_vfsp, (struct statvfs32 *)sbp);
#endif
releasef(fdes);
if (error)
return (set_errno(error));
return (0);
}
#if defined(_ILP32)
/*
* Large File system calls.
*
* (We deliberately don't have special "large file" system calls in the
* 64-bit kernel -- we just use the native versions, since they're just
* as functional.)
*/
int
statvfs64(char *fname, struct statvfs64 *sbp)
{
vnode_t *vp;
int error;
int estale_retry = 0;
lookup:
if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
error = cstatvfs64(vp->v_vfsp, sbp);
VN_RELE(vp);
if (error) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (0);
}
int
fstatvfs64(int fdes, struct statvfs64 *sbp)
{
struct file *fp;
int error;
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
error = cstatvfs64(fp->f_vnode->v_vfsp, sbp);
releasef(fdes);
if (error)
return (set_errno(error));
return (0);
}
#endif /* _ILP32 */
#ifdef _SYSCALL32_IMPL
static int
cstatvfs64_32(struct vfs *vfsp, struct statvfs64_32 *ubp)
{
struct statvfs64 ds64;
struct statvfs64_32 ds64_32;
int error;
bzero(&ds64, sizeof (ds64));
if ((error = VFS_STATVFS(vfsp, &ds64)) != 0)
return (error);
/*
* On the 64-bit kernel, even these fields grow to 64-bit
* quantities in the statvfs64 structure.
*/
if (ds64.f_namemax == (ulong_t)-1l)
ds64.f_namemax = UINT32_MAX;
if (ds64.f_bsize > UINT32_MAX || ds64.f_frsize > UINT32_MAX ||
ds64.f_fsid > UINT32_MAX || ds64.f_flag > UINT32_MAX ||
ds64.f_namemax > UINT32_MAX)
return (EOVERFLOW);
STATVFSCOPY(&ds64_32, &ds64);
if (copyout(&ds64_32, ubp, sizeof (ds64_32)) != 0)
return (EFAULT);
return (0);
}
/*
* ILP32 "small file" system calls on LP64 kernel
*/
int
statvfs32(char *fname, struct statvfs32 *sbp)
{
vnode_t *vp;
int error;
int estale_retry = 0;
lookup:
if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
error = cstatvfs32(vp->v_vfsp, sbp);
VN_RELE(vp);
if (error) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (0);
}
int
fstatvfs32(int fdes, struct statvfs32 *sbp)
{
struct file *fp;
int error;
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
error = cstatvfs32(fp->f_vnode->v_vfsp, sbp);
releasef(fdes);
if (error)
return (set_errno(error));
return (0);
}
/*
* ILP32 Large File system calls on LP64 kernel
*/
int
statvfs64_32(char *fname, struct statvfs64_32 *sbp)
{
vnode_t *vp;
int error;
int estale_retry = 0;
lookup:
if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
error = cstatvfs64_32(vp->v_vfsp, sbp);
VN_RELE(vp);
if (error) {
if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
goto lookup;
return (set_errno(error));
}
return (0);
}
int
fstatvfs64_32(int fdes, struct statvfs64_32 *sbp)
{
struct file *fp;
int error;
if ((fp = getf(fdes)) == NULL)
return (set_errno(EBADF));
error = cstatvfs64_32(fp->f_vnode->v_vfsp, sbp);
releasef(fdes);
if (error)
return (set_errno(error));
return (0);
}
#endif /* _SYSCALL32_IMPL */
/*
* 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.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/stropts.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/fs/fifonode.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/debug.h>
/*
* STREAMS system calls.
*/
int getmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *flagsp);
int putmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int flags);
int getpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *prip,
int *flagsp);
int putpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int pri,
int flags);
static int msgio(int fdes, struct strbuf *ctl, struct strbuf *data, int *rval,
int mode, unsigned char *prip, int *flagsp);
int
getmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *flagsp)
{
int error;
int localflags;
int realflags = 0;
unsigned char pri = 0;
int rv = 0;
/*
* Convert between old flags (localflags) and new flags (realflags).
*/
if (copyin(flagsp, &localflags, sizeof (*flagsp)))
return (set_errno(EFAULT));
switch (localflags) {
case 0:
realflags = MSG_ANY;
break;
case RS_HIPRI:
realflags = MSG_HIPRI;
break;
default:
return (set_errno(EINVAL));
}
if ((error = msgio(fdes, ctl, data, &rv, FREAD, &pri,
&realflags)) == 0) {
/*
* massage realflags based on localflags.
*/
if (realflags == MSG_HIPRI)
localflags = RS_HIPRI;
else
localflags = 0;
if (copyout(&localflags, flagsp, sizeof (*flagsp)))
error = EFAULT;
}
if (error != 0)
return (set_errno(error));
return (rv);
}
int
putmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int flags)
{
unsigned char pri = 0;
int realflags;
int error;
int rv = 0;
switch (flags) {
case RS_HIPRI:
realflags = MSG_HIPRI;
break;
case (RS_HIPRI|MSG_XPG4):
realflags = MSG_HIPRI|MSG_XPG4;
break;
case MSG_XPG4:
realflags = MSG_BAND|MSG_XPG4;
break;
case 0:
realflags = MSG_BAND;
break;
default:
return (set_errno(EINVAL));
}
error = msgio(fdes, ctl, data, &rv, FWRITE, &pri, &realflags);
if (error != 0)
return (set_errno(error));
return (rv);
}
int
getpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *prip,
int *flagsp)
{
int error;
int flags;
int intpri;
unsigned char pri;
int rv = 0;
if (copyin(flagsp, &flags, sizeof (flags)))
return (set_errno(EFAULT));
if (copyin(prip, &intpri, sizeof (intpri)))
return (set_errno(EFAULT));
if ((intpri > 255) || (intpri < 0))
return (set_errno(EINVAL));
pri = (unsigned char)intpri;
error = msgio(fdes, ctl, data, &rv, FREAD, &pri, &flags);
if (error != 0)
return (set_errno(error));
if (copyout(&flags, flagsp, sizeof (flags)))
return (set_errno(EFAULT));
intpri = (int)pri;
if (copyout(&intpri, prip, sizeof (intpri)))
return (set_errno(EFAULT));
return (rv);
}
int
putpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int intpri,
int flags)
{
unsigned char pri;
int rv = 0;
int error;
if ((intpri > 255) || (intpri < 0))
return (set_errno(EINVAL));
pri = (unsigned char)intpri;
error = msgio(fdes, ctl, data, &rv, FWRITE, &pri, &flags);
if (error != 0)
return (set_errno(error));
return (rv);
}
/*
* Common code for getmsg and putmsg calls: check permissions,
* copy in args, do preliminary setup, and switch to
* appropriate stream routine.
*/
static int
msgio(int fdes, struct strbuf *ctl, struct strbuf *data, int *rval,
int mode, unsigned char *prip, int *flagsp)
{
file_t *fp;
vnode_t *vp;
struct strbuf msgctl, msgdata;
int error;
int flag;
klwp_t *lwp = ttolwp(curthread);
rval_t rv;
if ((fp = getf(fdes)) == NULL)
return (EBADF);
if ((fp->f_flag & mode) == 0) {
releasef(fdes);
return (EBADF);
}
vp = fp->f_vnode;
if (vp->v_type == VFIFO) {
if (vp->v_stream) {
/*
* must use sd_vnode, could be named pipe
*/
(void) fifo_vfastoff(vp->v_stream->sd_vnode);
} else {
releasef(fdes);
return (ENOSTR);
}
} else if ((vp->v_type != VCHR && vp->v_type != VSOCK) ||
vp->v_stream == NULL) {
releasef(fdes);
return (ENOSTR);
}
if ((ctl != NULL) &&
copyin(ctl, &msgctl, sizeof (struct strbuf))) {
releasef(fdes);
return (EFAULT);
}
if ((data != NULL) &&
copyin(data, &msgdata, sizeof (struct strbuf))) {
releasef(fdes);
return (EFAULT);
}
if (mode == FREAD) {
if (ctl == NULL)
msgctl.maxlen = -1;
if (data == NULL)
msgdata.maxlen = -1;
flag = fp->f_flag;
rv.r_val1 = 0;
if (vp->v_type == VSOCK) {
error = sock_getmsg(vp, &msgctl, &msgdata, prip,
flagsp, flag, &rv);
} else {
error = strgetmsg(vp, &msgctl, &msgdata, prip,
flagsp, flag, &rv);
}
*rval = rv.r_val1;
if (error != 0) {
releasef(fdes);
return (error);
}
if (lwp != NULL)
lwp->lwp_ru.msgrcv++;
if (((ctl != NULL) &&
copyout(&msgctl, ctl, sizeof (struct strbuf))) ||
((data != NULL) &&
copyout(&msgdata, data, sizeof (struct strbuf)))) {
releasef(fdes);
return (EFAULT);
}
releasef(fdes);
return (0);
}
/*
* FWRITE case
*/
if (ctl == NULL)
msgctl.len = -1;
if (data == NULL)
msgdata.len = -1;
flag = fp->f_flag;
if (vp->v_type == VSOCK) {
error = sock_putmsg(vp, &msgctl, &msgdata, *prip, *flagsp,
flag);
} else {
error = strputmsg(vp, &msgctl, &msgdata, *prip, *flagsp, flag);
}
releasef(fdes);
if (error == 0 && lwp != NULL)
lwp->lwp_ru.msgsnd++;
return (error);
}
#if defined(_LP64) && defined(_SYSCALL32)
static int msgio32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data,
int *rval, int mode, unsigned char *prip, int *flagsp);
int
getmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t *flagsp)
{
int error;
int32_t localflags;
int realflags = 0;
unsigned char pri = 0;
int rv = 0;
/*
* Convert between old flags (localflags) and new flags (realflags).
*/
if (copyin(flagsp, &localflags, sizeof (*flagsp)))
return (set_errno(EFAULT));
switch (localflags) {
case 0:
realflags = MSG_ANY;
break;
case RS_HIPRI:
realflags = MSG_HIPRI;
break;
default:
return (set_errno(EINVAL));
}
if ((error = msgio32(fdes, ctl, data, &rv, FREAD, &pri,
&realflags)) == 0) {
/*
* massage realflags based on localflags.
*/
if (realflags == MSG_HIPRI)
localflags = RS_HIPRI;
else
localflags = 0;
if (copyout(&localflags, flagsp, sizeof (*flagsp)))
error = EFAULT;
}
if (error != 0)
return (set_errno(error));
return (rv);
}
int
putmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t flags)
{
unsigned char pri = 0;
int realflags;
int error;
int rv = 0;
switch (flags) {
case RS_HIPRI:
realflags = MSG_HIPRI;
break;
case (RS_HIPRI|MSG_XPG4):
realflags = MSG_HIPRI|MSG_XPG4;
break;
case MSG_XPG4:
realflags = MSG_BAND|MSG_XPG4;
break;
case 0:
realflags = MSG_BAND;
break;
default:
return (set_errno(EINVAL));
}
error = msgio32(fdes, ctl, data, &rv, FWRITE, &pri, &realflags);
if (error != 0)
return (set_errno(error));
return (rv);
}
int
getpmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t *prip,
int32_t *flagsp)
{
int error;
int32_t flags;
int32_t intpri;
unsigned char pri;
int rv = 0;
if (copyin(flagsp, &flags, sizeof (*flagsp)))
return (set_errno(EFAULT));
if (copyin(prip, &intpri, sizeof (intpri)))
return (set_errno(EFAULT));
if ((intpri > 255) || (intpri < 0))
return (set_errno(EINVAL));
pri = (unsigned char)intpri;
error = msgio32(fdes, ctl, data, &rv, FREAD, &pri, &flags);
if (error != 0)
return (set_errno(error));
if (copyout(&flags, flagsp, sizeof (flags)))
return (set_errno(EFAULT));
intpri = (int)pri;
if (copyout(&intpri, prip, sizeof (intpri)))
return (set_errno(EFAULT));
return (rv);
}
int
putpmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t intpri,
int32_t flags)
{
unsigned char pri;
int rv = 0;
int error;
if ((intpri > 255) || (intpri < 0))
return (set_errno(EINVAL));
pri = (unsigned char)intpri;
error = msgio32(fdes, ctl, data, &rv, FWRITE, &pri, &flags);
if (error != 0)
return (set_errno(error));
return (rv);
}
/*
* Common code for getmsg and putmsg calls: check permissions,
* copy in args, do preliminary setup, and switch to
* appropriate stream routine.
*/
static int
msgio32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int *rval,
int mode, unsigned char *prip, int *flagsp)
{
file_t *fp;
vnode_t *vp;
struct strbuf32 msgctl32, msgdata32;
struct strbuf msgctl, msgdata;
int error;
int flag;
klwp_t *lwp = ttolwp(curthread);
rval_t rv;
if ((fp = getf(fdes)) == NULL)
return (EBADF);
if ((fp->f_flag & mode) == 0) {
releasef(fdes);
return (EBADF);
}
vp = fp->f_vnode;
if (vp->v_type == VFIFO) {
if (vp->v_stream) {
/*
* must use sd_vnode, could be named pipe
*/
(void) fifo_vfastoff(vp->v_stream->sd_vnode);
} else {
releasef(fdes);
return (ENOSTR);
}
} else if ((vp->v_type != VCHR && vp->v_type != VSOCK) ||
vp->v_stream == NULL) {
releasef(fdes);
return (ENOSTR);
}
if (ctl != NULL) {
if (copyin(ctl, &msgctl32, sizeof (msgctl32))) {
releasef(fdes);
return (EFAULT);
}
msgctl.len = msgctl32.len;
msgctl.maxlen = msgctl32.maxlen;
msgctl.buf = (caddr_t)(uintptr_t)msgctl32.buf;
}
if (data != NULL) {
if (copyin(data, &msgdata32, sizeof (msgdata32))) {
releasef(fdes);
return (EFAULT);
}
msgdata.len = msgdata32.len;
msgdata.maxlen = msgdata32.maxlen;
msgdata.buf = (caddr_t)(uintptr_t)msgdata32.buf;
}
if (mode == FREAD) {
if (ctl == NULL)
msgctl.maxlen = -1;
if (data == NULL)
msgdata.maxlen = -1;
flag = fp->f_flag;
rv.r_val1 = 0;
if (vp->v_type == VSOCK) {
error = sock_getmsg(vp, &msgctl, &msgdata, prip,
flagsp, flag, &rv);
} else {
error = strgetmsg(vp, &msgctl, &msgdata, prip,
flagsp, flag, &rv);
}
*rval = rv.r_val1;
if (error != 0) {
releasef(fdes);
return (error);
}
if (lwp != NULL)
lwp->lwp_ru.msgrcv++;
if (ctl != NULL) {
/* XX64 - range check */
msgctl32.len = msgctl.len;
msgctl32.maxlen = msgctl.maxlen;
msgctl32.buf = (caddr32_t)(uintptr_t)msgctl.buf;
if (copyout(&msgctl32, ctl, sizeof (msgctl32))) {
releasef(fdes);
return (EFAULT);
}
}
if (data != NULL) {
/* XX64 - range check */
msgdata32.len = msgdata.len;
msgdata32.maxlen = msgdata.maxlen;
msgdata32.buf = (caddr32_t)(uintptr_t)msgdata.buf;
if (copyout(&msgdata32, data, sizeof (msgdata32))) {
releasef(fdes);
return (EFAULT);
}
}
releasef(fdes);
return (0);
}
/*
* FWRITE case
*/
if (ctl == NULL)
msgctl.len = -1;
if (data == NULL)
msgdata.len = -1;
flag = fp->f_flag;
if (vp->v_type == VSOCK) {
error = sock_putmsg(vp, &msgctl, &msgdata, *prip, *flagsp,
flag);
} else {
error = strputmsg(vp, &msgctl, &msgdata, *prip, *flagsp, flag);
}
releasef(fdes);
if (error == 0 && lwp != NULL)
lwp->lwp_ru.msgsnd++;
return (error);
}
#endif /* _LP64 && _SYSCALL32 */
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/pathname.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/debug.h>
#include <c2/audit.h>
#include <fs/fs_subr.h>
/*
* Create a symbolic link. Similar to link or rename except target
* name is passed as string argument, not converted to vnode reference.
*/
int
symlinkat(char *target, int dfd, char *linkname)
{
vnode_t *startvp;
vnode_t *dvp;
struct vattr vattr;
struct pathname lpn;
char *tbuf;
size_t tlen;
int error;
int estale_retry = 0;
int auditing = AU_AUDITING();
if (linkname == NULL)
return (set_errno(EFAULT));
if ((error = fgetstartvp(dfd, linkname, &startvp)) != 0)
return (set_errno(error));
top:
if (error = pn_get(linkname, UIO_USERSPACE, &lpn))
goto out;
if (auditing && startvp != NULL)
audit_setfsat_path(2);
if (error = lookuppnat(&lpn, NULL, NO_FOLLOW, &dvp, NULLVPP, startvp)) {
pn_free(&lpn);
if (error == ESTALE && fs_need_estale_retry(estale_retry++))
goto top;
goto out;
}
if (vn_is_readonly(dvp))
error = EROFS;
else if (pn_fixslash(&lpn))
error = ENOTDIR;
else {
tbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
if ((error = copyinstr(target, tbuf, MAXPATHLEN, &tlen)) == 0) {
vattr.va_type = VLNK;
vattr.va_mode = 0777;
vattr.va_mask = AT_TYPE|AT_MODE;
error = VOP_SYMLINK(dvp, lpn.pn_path, &vattr,
tbuf, CRED(), NULL, 0);
if (auditing)
audit_symlink_create(dvp, lpn.pn_path,
tbuf, error);
}
kmem_free(tbuf, MAXPATHLEN);
}
pn_free(&lpn);
VN_RELE(dvp);
if (error == ESTALE && fs_need_estale_retry(estale_retry++))
goto top;
out:
if (startvp != NULL)
VN_RELE(startvp);
if (error)
return (set_errno(error));
return (0);
}
int
symlink(char *target, char *linkname)
{
return (symlinkat(target, AT_FDCWD, linkname));
}
/*
* 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 (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* Copyright (c) 1994 Sun Microsystems, Inc. */
/* All Rights Reserved */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/systm.h>
#include <sys/vfs.h>
int
syssync()
{
vfs_sync(0);
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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2016 Joyent, Inc.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/tuneable.h>
#include <sys/errno.h>
#include <sys/var.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <sys/sysconfig.h>
#include <sys/resource.h>
#include <sys/ulimit.h>
#include <sys/unistd.h>
#include <sys/debug.h>
#include <sys/cpuvar.h>
#include <sys/mman.h>
#include <sys/timer.h>
#include <sys/zone.h>
#include <sys/vm_usage.h>
#include <vm/as.h>
extern rctl_hndl_t rc_process_sigqueue;
long
sysconfig(int which)
{
switch (which) {
/*
* if it is not handled in mach_sysconfig either
* it must be EINVAL.
*/
default:
return (mach_sysconfig(which)); /* `uname -i`/os */
case _CONFIG_CLK_TCK:
return ((long)hz); /* clock frequency per second */
case _CONFIG_PROF_TCK:
return ((long)hz); /* profiling clock freq per sec */
case _CONFIG_NGROUPS:
/*
* Maximum number of supplementary groups.
*/
return (ngroups_max);
case _CONFIG_OPEN_FILES:
/*
* Maximum number of open files (soft limit).
*/
{
rlim64_t fd_ctl;
mutex_enter(&curproc->p_lock);
fd_ctl = rctl_enforced_value(
rctlproc_legacy[RLIMIT_NOFILE], curproc->p_rctls,
curproc);
mutex_exit(&curproc->p_lock);
return ((ulong_t)fd_ctl);
}
case _CONFIG_CHILD_MAX:
/*
* Maximum number of processes.
*/
return (v.v_maxup);
case _CONFIG_POSIX_VER:
return (_POSIX_VERSION); /* current POSIX version */
case _CONFIG_PAGESIZE:
return (PAGESIZE);
case _CONFIG_XOPEN_VER:
return (_XOPEN_VERSION); /* current XOPEN version */
case _CONFIG_NPROC_CONF:
return (zone_ncpus_get(curproc->p_zone));
case _CONFIG_NPROC_ONLN:
return (zone_ncpus_online_get(curproc->p_zone));
case _CONFIG_NPROC_MAX:
return (max_ncpus);
case _CONFIG_NPROC_NCPU:
return (NCPU); /* Private sysconfig for direct NCPU access */
case _CONFIG_STACK_PROT:
return (curproc->p_stkprot & ~PROT_USER);
case _CONFIG_AIO_LISTIO_MAX:
return (_AIO_LISTIO_MAX);
case _CONFIG_AIO_MAX:
return (_AIO_MAX);
case _CONFIG_AIO_PRIO_DELTA_MAX:
return (0);
case _CONFIG_DELAYTIMER_MAX:
return (INT_MAX);
case _CONFIG_MQ_OPEN_MAX:
return (_MQ_OPEN_MAX);
case _CONFIG_MQ_PRIO_MAX:
return (_MQ_PRIO_MAX);
case _CONFIG_RTSIG_MAX:
return (_SIGRTMAX - _SIGRTMIN + 1);
case _CONFIG_SEM_NSEMS_MAX:
return (_SEM_NSEMS_MAX);
case _CONFIG_SEM_VALUE_MAX:
return (_SEM_VALUE_MAX);
case _CONFIG_SIGQUEUE_MAX:
/*
* Maximum number of outstanding queued signals.
*/
{
rlim64_t sigqsz_max;
mutex_enter(&curproc->p_lock);
sigqsz_max = rctl_enforced_value(rc_process_sigqueue,
curproc->p_rctls, curproc);
mutex_exit(&curproc->p_lock);
return ((uint_t)sigqsz_max);
}
case _CONFIG_SIGRT_MIN:
return (_SIGRTMIN);
case _CONFIG_SIGRT_MAX:
return (_SIGRTMAX);
case _CONFIG_TIMER_MAX:
return (timer_max);
case _CONFIG_PHYS_PAGES:
/*
* If the non-global zone has a phys. memory cap, use that.
* We always report the system-wide value for the global zone,
* even though rcapd can be used on the global zone too.
*/
if (!INGLOBALZONE(curproc) &&
curproc->p_zone->zone_phys_mcap != 0)
return (MIN(btop(curproc->p_zone->zone_phys_mcap),
physinstalled));
return (physinstalled);
case _CONFIG_AVPHYS_PAGES:
/*
* If the non-global zone has a phys. memory cap, use
* the phys. memory cap - zone's current rss. We always
* report the system-wide value for the global zone, even
* though rcapd can be used on the global zone too.
*/
if (!INGLOBALZONE(curproc) &&
curproc->p_zone->zone_phys_mcap != 0) {
pgcnt_t cap, rss, free;
vmusage_t in_use;
size_t cnt = 1;
cap = btop(curproc->p_zone->zone_phys_mcap);
if (cap > physinstalled)
return (freemem);
if (vm_getusage(VMUSAGE_ZONE, 1, &in_use, &cnt,
FKIOCTL) != 0)
in_use.vmu_rss_all = 0;
rss = btop(in_use.vmu_rss_all);
/*
* Because rcapd implements a soft cap, it is possible
* for rss to be temporarily over the cap.
*/
if (cap > rss)
free = cap - rss;
else
free = 0;
return (MIN(free, freemem));
}
return (freemem);
case _CONFIG_MAXPID:
return (maxpid);
case _CONFIG_CPUID_MAX:
return (max_cpuid);
case _CONFIG_EPHID_MAX:
return (MAXEPHUID);
case _CONFIG_UADDR_MAX:
return ((long)(uintptr_t)curproc->p_as->a_userlimit);
case _CONFIG_SYMLOOP_MAX:
return (MAXSYMLINKS);
}
}
/*
* 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 1993 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/fstyp.h>
#include <sys/systm.h>
#include <sys/mount.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/cmn_err.h>
#include <sys/buf.h>
#include <sys/debug.h>
#include <sys/pathname.h>
/*
* System call to map fstype numbers to names, and vice versa.
*/
static int sysfsind(char *);
static int sysfstyp(int, char *);
int
sysfs(int opcode, long a1, long a2)
{
int error;
switch (opcode) {
case GETFSIND:
error = sysfsind((char *)a1);
break;
case GETFSTYP:
error = sysfstyp((int)a1, (char *)a2);
break;
case GETNFSTYP:
/*
* Return number of fstypes configured in the system.
*/
return (nfstype - 1);
default:
error = set_errno(EINVAL);
}
return (error);
}
static int
sysfsind(char *fsname)
{
/*
* Translate fs identifier to an index into the vfssw structure.
*/
struct vfssw *vswp;
char fsbuf[FSTYPSZ];
int retval;
size_t len = 0;
retval = copyinstr(fsname, fsbuf, FSTYPSZ, &len);
if (retval == ENOENT) /* XXX */
retval = EINVAL; /* XXX */
if (len == 1) /* Includes null byte */
retval = EINVAL;
if (retval)
return (set_errno(retval));
/*
* Search the vfssw table for the fs identifier
* and return the index.
*/
if ((vswp = vfs_getvfssw(fsbuf)) != NULL) {
retval = vswp - vfssw;
vfs_unrefvfssw(vswp);
return (retval);
}
return (set_errno(EINVAL));
}
static int
sysfstyp(int index, char *cbuf)
{
/*
* Translate fstype index into an fs identifier.
*/
char *src;
struct vfssw *vswp;
char *osrc;
int error = 0;
if (index <= 0 || index >= nfstype)
return (set_errno(EINVAL));
RLOCK_VFSSW();
vswp = &vfssw[index];
osrc = src = vswp->vsw_name;
while (*src++)
;
if (copyout(osrc, cbuf, src - osrc))
error = set_errno(EFAULT);
RUNLOCK_VFSSW();
return (error);
}
/*
* 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 2020 Oxide Computer Company
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All rights reserved. */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/tuneable.h>
#include <sys/errno.h>
#include <sys/cred.h>
#include <sys/utsname.h>
#include <sys/systeminfo.h>
#include <sys/unistd.h>
#include <sys/debug.h>
#include <sys/bootconf.h>
#include <sys/socket.h>
#include <sys/policy.h>
#include <net/if.h>
#include <sys/sunddi.h>
#include <sys/promif.h>
#include <sys/zone.h>
#include <sys/model.h>
#include <netinet/inetutil.h>
static void get_netif_name(char *, char *);
long
systeminfo(int command, char *buf, long count)
{
int error = 0;
long strcnt, getcnt;
char *kstr;
char hostidp[HW_HOSTID_LEN];
if (count < 0 && command != SI_SET_HOSTNAME &&
command != SI_SET_SRPC_DOMAIN)
return (set_errno(EINVAL));
/*
* Deal with the common "get a string" case first.
*/
switch (command) {
case SI_SYSNAME:
kstr = utsname.sysname;
break;
case SI_HOSTNAME:
kstr = uts_nodename();
break;
case SI_RELEASE:
kstr = utsname.release;
break;
case SI_VERSION:
kstr = utsname.version;
break;
case SI_MACHINE:
kstr = utsname.machine;
break;
#ifdef _LP64
case SI_ADDRESS_WIDTH:
kstr = "64";
break;
case SI_ARCHITECTURE_64:
case SI_ARCHITECTURE_K:
kstr = architecture;
break;
case SI_ARCHITECTURE_32:
kstr = architecture_32;
break;
case SI_ARCHITECTURE:
kstr = architecture;
break;
case SI_ARCHITECTURE_NATIVE:
kstr = get_udatamodel() == DATAMODEL_NATIVE ?
architecture : architecture_32;
break;
#else
case SI_ADDRESS_WIDTH:
kstr = "32";
break;
case SI_ARCHITECTURE_K:
case SI_ARCHITECTURE_32:
case SI_ARCHITECTURE:
case SI_ARCHITECTURE_NATIVE:
kstr = architecture;
break;
#endif
case SI_HW_SERIAL:
(void) snprintf(hostidp, sizeof (hostidp), "%u",
zone_get_hostid(curzone));
kstr = hostidp;
break;
case SI_HW_PROVIDER:
kstr = hw_provider;
break;
case SI_SRPC_DOMAIN:
kstr = curproc->p_zone->zone_domain;
break;
case SI_PLATFORM:
kstr = platform;
break;
case SI_ISALIST:
kstr = isa_list;
break;
default:
kstr = NULL;
break;
}
if (kstr != NULL) {
strcnt = strlen(kstr);
if (count > 0) {
if (count <= strcnt) {
getcnt = count - 1;
if (subyte(buf + getcnt, 0) < 0)
return (set_errno(EFAULT));
} else {
getcnt = strcnt + 1;
}
if (copyout(kstr, buf, getcnt))
return (set_errno(EFAULT));
}
return (strcnt + 1);
}
switch (command) {
case SI_DHCP_CACHE:
{
char *tmp;
unsigned int tlen, octlen;
if (dhcack == NULL) {
tmp = "";
strcnt = 0;
} else {
/*
* If the interface didn't have a name (bindable
* driver) to begin with, it might have one now.
* So, re-run strplumb_get_netdev_path() to see
* if one can be established at this time.
*/
if (netdev_path == NULL || netdev_path[0] == '\0') {
netdev_path = strplumb_get_netdev_path();
}
/*
* If the interface name has not yet been resolved
* and a validnetdev_path[] was stashed by
* loadrootmodules in swapgeneric.c, or established
* above, resolve the interface name now.
*/
if (dhcifname[0] == '\0' &&
netdev_path != NULL && netdev_path[0] != '\0') {
get_netif_name(netdev_path, dhcifname);
}
/*
* Form reply:
* IFNAMESIZ array of dhcp i/f
* hexascii representation of dhcp reply
*/
octlen = dhcacklen * 2 + 1;
tlen = octlen + IFNAMSIZ;
tmp = kmem_alloc(tlen, KM_SLEEP);
(void) strncpy(tmp, dhcifname, IFNAMSIZ);
if (octet_to_hexascii(dhcack, dhcacklen,
&tmp[IFNAMSIZ], &octlen) != 0) {
kmem_free(tmp, tlen);
error = EINVAL;
break;
} else {
strcnt = IFNAMSIZ + octlen;
}
}
if (count > 0) {
if (count <= strcnt) {
getcnt = count - 1;
if (subyte((buf + getcnt), 0) < 0)
goto fail;
} else {
getcnt = strcnt + 1;
}
if (copyout(tmp, buf, getcnt))
goto fail;
}
if (strcnt != 0)
kmem_free(tmp, tlen);
return (strcnt + 1);
fail:
if (strcnt != 0)
kmem_free(tmp, tlen);
error = EFAULT;
break;
}
case SI_SET_HOSTNAME:
{
size_t len;
char name[SYS_NMLN];
char *name_to_use;
if ((error = secpolicy_systeminfo(CRED())) != 0)
break;
name_to_use = uts_nodename();
if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
break;
/*
* Must be non-NULL string and string
* must be less than SYS_NMLN chars.
*/
if (len < 2 || (len == SYS_NMLN && name[SYS_NMLN-1] != '\0')) {
error = EINVAL;
break;
}
/*
* Copy the name into the relevant zone's nodename.
*/
(void) strcpy(name_to_use, name);
/*
* Notify other interested parties that the nodename was set
*/
if (name_to_use == utsname.nodename) /* global zone nodename */
nodename_set();
return (len);
}
case SI_SET_SRPC_DOMAIN:
{
char name[SYS_NMLN];
size_t len;
if ((error = secpolicy_systeminfo(CRED())) != 0)
break;
if ((error = copyinstr(buf, name, SYS_NMLN, &len)) != 0)
break;
/*
* If string passed in is longer than length
* allowed for domain name, fail.
*/
if (len == SYS_NMLN && name[SYS_NMLN-1] != '\0') {
error = EINVAL;
break;
}
(void) strcpy(curproc->p_zone->zone_domain, name);
return (len);
}
default:
error = EINVAL;
break;
}
return (set_errno(error));
}
/*
* i_path_find_node: Internal routine used by path_to_devinfo
* to locate a given nodeid in the device tree.
*/
struct i_path_findnode {
pnode_t nodeid;
dev_info_t *dip;
};
static int
i_path_find_node(dev_info_t *dev, void *arg)
{
struct i_path_findnode *f = (struct i_path_findnode *)arg;
if (ddi_get_nodeid(dev) == (int)f->nodeid) {
f->dip = dev;
return (DDI_WALK_TERMINATE);
}
return (DDI_WALK_CONTINUE);
}
/*
* Return the devinfo node to a boot device
*/
static dev_info_t *
path_to_devinfo(char *path)
{
struct i_path_findnode fn;
extern dev_info_t *top_devinfo;
/*
* Get the nodeid of the given pathname, if such a mapping exists.
*/
fn.dip = NULL;
fn.nodeid = prom_finddevice(path);
if (fn.nodeid != OBP_BADNODE) {
/*
* Find the nodeid in our copy of the device tree and return
* whatever name we used to bind this node to a driver.
*/
ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn));
}
return (fn.dip);
}
/*
* Determine the network interface name from the device path argument.
*/
static void
get_netif_name(char *devname, char *ifname)
{
dev_info_t *dip;
major_t ndev;
char *name;
int unit;
dip = path_to_devinfo(devname);
if (dip == NULL) {
cmn_err(CE_WARN, "get_netif_name: "
"can't bind driver for '%s'\n", devname);
return;
}
ndev = ddi_driver_major(dip);
if (ndev == -1) {
cmn_err(CE_WARN, "get_netif_name: "
"no driver bound to '%s'\n", devname);
return;
}
name = ddi_major_to_name(ndev);
if (name == NULL) {
cmn_err(CE_WARN, "get_netif_name: "
"no name for major number %d\n", ndev);
return;
}
unit = i_ddi_devi_get_ppa(dip);
if (unit < 0) {
cmn_err(CE_WARN, "get_netif_name: "
"illegal unit number %d\n", unit);
return;
}
(void) snprintf(ifname, IFNAMSIZ, "%s%d", name, unit);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Copyright (c) 2013, Joyent, Inc. All rights reserved.
*/
/*
* System calls for creating and inquiring about tasks and projects
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/thread.h>
#include <sys/proc.h>
#include <sys/task.h>
#include <sys/systm.h>
#include <sys/project.h>
#include <sys/cpuvar.h>
#include <sys/policy.h>
#include <sys/zone.h>
#include <sys/rctl.h>
/*
* Limit projlist to 256k projects.
*/
#define MAX_PROJLIST_BUFSIZE 1048576
typedef struct projlist_walk {
projid_t *pw_buf;
size_t pw_bufsz;
} projlist_walk_t;
/*
* taskid_t tasksys_settaskid(projid_t projid, uint_t flags);
*
* Overview
* Place the calling process in a new task if sufficiently privileged. If the
* present task is finalized, the process may not create a new task.
*
* Return values
* 0 on success, errno on failure.
*/
static long
tasksys_settaskid(projid_t projid, uint_t flags)
{
proc_t *p = ttoproc(curthread);
kproject_t *oldpj;
kproject_t *kpj;
task_t *tk, *oldtk;
rctl_entity_p_t e;
zone_t *zone;
int rctlfail = 0;
if (secpolicy_tasksys(CRED()) != 0)
return (set_errno(EPERM));
if (projid < 0)
return (set_errno(EINVAL));
if (flags & ~TASK_FINAL)
return (set_errno(EINVAL));
mutex_enter(&pidlock);
if (p->p_task->tk_flags & TASK_FINAL) {
mutex_exit(&pidlock);
return (set_errno(EACCES));
}
mutex_exit(&pidlock);
/*
* Try to stop all other lwps in the process while we're changing
* our project. This way, curthread doesn't need to grab its own
* thread_lock to find its project ID (see curprojid()). If this
* is the /proc agent lwp, we know that the other lwps are already
* held. If we failed to hold all lwps, bail out and return EINTR.
*/
if (curthread != p->p_agenttp && !holdlwps(SHOLDFORK1))
return (set_errno(EINTR));
/*
* Put a hold on our new project and make sure that nobody is
* trying to bind it to a pool while we're joining.
*/
kpj = project_hold_by_id(projid, p->p_zone, PROJECT_HOLD_INSERT);
e.rcep_p.proj = kpj;
e.rcep_t = RCENTITY_PROJECT;
mutex_enter(&p->p_lock);
oldpj = p->p_task->tk_proj;
zone = p->p_zone;
mutex_enter(&zone->zone_nlwps_lock);
mutex_enter(&zone->zone_mem_lock);
if (kpj->kpj_nlwps + p->p_lwpcnt > kpj->kpj_nlwps_ctl)
if (rctl_test_entity(rc_project_nlwps, kpj->kpj_rctls, p, &e,
p->p_lwpcnt, 0) & RCT_DENY)
rctlfail = 1;
if (kpj->kpj_ntasks + 1 > kpj->kpj_ntasks_ctl)
if (rctl_test_entity(rc_project_ntasks, kpj->kpj_rctls, p, &e,
1, 0) & RCT_DENY)
rctlfail = 1;
if (kpj != proj0p && kpj->kpj_nprocs + 1 > kpj->kpj_nprocs_ctl)
if (rctl_test_entity(rc_project_nprocs, kpj->kpj_rctls, p, &e,
1, 0) & RCT_DENY)
rctlfail = 1;
if (kpj->kpj_data.kpd_locked_mem + p->p_locked_mem >
kpj->kpj_data.kpd_locked_mem_ctl)
if (rctl_test_entity(rc_project_locked_mem, kpj->kpj_rctls, p,
&e, p->p_locked_mem, 0) & RCT_DENY)
rctlfail = 1;
mutex_enter(&(kpj->kpj_data.kpd_crypto_lock));
if (kpj->kpj_data.kpd_crypto_mem + p->p_crypto_mem >
kpj->kpj_data.kpd_crypto_mem_ctl)
if (rctl_test_entity(rc_project_crypto_mem, kpj->kpj_rctls, p,
&e, p->p_crypto_mem, 0) & RCT_DENY)
rctlfail = 1;
if (rctlfail) {
mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
mutex_exit(&zone->zone_mem_lock);
mutex_exit(&zone->zone_nlwps_lock);
if (curthread != p->p_agenttp)
continuelwps(p);
mutex_exit(&p->p_lock);
project_rele(kpj);
return (set_errno(EAGAIN));
}
kpj->kpj_data.kpd_crypto_mem += p->p_crypto_mem;
mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
kpj->kpj_data.kpd_locked_mem += p->p_locked_mem;
kpj->kpj_nlwps += p->p_lwpcnt;
kpj->kpj_ntasks++;
kpj->kpj_nprocs++;
oldpj->kpj_data.kpd_locked_mem -= p->p_locked_mem;
mutex_enter(&(oldpj->kpj_data.kpd_crypto_lock));
oldpj->kpj_data.kpd_crypto_mem -= p->p_crypto_mem;
mutex_exit(&(oldpj->kpj_data.kpd_crypto_lock));
oldpj->kpj_nlwps -= p->p_lwpcnt;
oldpj->kpj_nprocs--;
mutex_exit(&zone->zone_mem_lock);
mutex_exit(&zone->zone_nlwps_lock);
mutex_exit(&p->p_lock);
mutex_enter(&kpj->kpj_poolbind);
tk = task_create(projid, curproc->p_zone);
mutex_enter(&cpu_lock);
/*
* Returns with p_lock held.
*/
oldtk = task_join(tk, flags);
if (curthread != p->p_agenttp)
continuelwps(p);
mutex_exit(&p->p_lock);
mutex_exit(&cpu_lock);
mutex_exit(&kpj->kpj_poolbind);
task_rele(oldtk);
project_rele(kpj);
return (tk->tk_tkid);
}
/*
* taskid_t tasksys_gettaskid(void);
*
* Overview
* Return the current task ID for this process.
*
* Return value
* The ID for the task to which the current process belongs.
*/
static long
tasksys_gettaskid()
{
long ret;
proc_t *p = ttoproc(curthread);
mutex_enter(&pidlock);
ret = p->p_task->tk_tkid;
mutex_exit(&pidlock);
return (ret);
}
/*
* projid_t tasksys_getprojid(void);
*
* Overview
* Return the current project ID for this process.
*
* Return value
* The ID for the project to which the current process belongs.
*/
static long
tasksys_getprojid()
{
long ret;
proc_t *p = ttoproc(curthread);
mutex_enter(&pidlock);
ret = p->p_task->tk_proj->kpj_id;
mutex_exit(&pidlock);
return (ret);
}
static int
tasksys_projlist_cb(kproject_t *kp, void *buf)
{
projlist_walk_t *pw = (projlist_walk_t *)buf;
if (pw && pw->pw_bufsz >= sizeof (projid_t)) {
*pw->pw_buf = kp->kpj_id;
pw->pw_buf++;
pw->pw_bufsz -= sizeof (projid_t);
}
return (0);
}
/*
* long tasksys_projlist(void *buf, size_t bufsz)
*
* Overview
* Return a buffer containing the project IDs of all currently active projects
* in the current zone.
*
* Return values
* The minimum size of a buffer sufficiently large to contain all of the
* active project IDs, or -1 if an error occurs during copyout.
*/
static long
tasksys_projlist(void *buf, size_t bufsz)
{
long ret = 0;
projlist_walk_t pw;
void *kbuf;
if (buf == NULL || bufsz == 0)
return (project_walk_all(getzoneid(), tasksys_projlist_cb,
NULL));
if (bufsz > MAX_PROJLIST_BUFSIZE)
return (set_errno(ENOMEM));
kbuf = pw.pw_buf = kmem_zalloc(bufsz, KM_SLEEP);
pw.pw_bufsz = bufsz;
ret = project_walk_all(getzoneid(), tasksys_projlist_cb, &pw);
if (copyout(kbuf, buf, bufsz) == -1)
ret = set_errno(EFAULT);
kmem_free(kbuf, bufsz);
return (ret);
}
long
tasksys(int code, projid_t projid, uint_t flags, void *projidbuf, size_t pbufsz)
{
switch (code) {
case 0:
return (tasksys_settaskid(projid, flags));
case 1:
return (tasksys_gettaskid());
case 2:
return (tasksys_getprojid());
case 3:
return (tasksys_projlist(projidbuf, pbufsz));
default:
return (set_errno(EINVAL));
}
}
/*
* 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 1994-2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All rights reserved. */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/tuneable.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/time.h>
#include <sys/debug.h>
#include <sys/policy.h>
time_t
gtime(void)
{
return (gethrestime_sec());
}
int
stime(time_t time)
{
timestruc_t ts;
if (secpolicy_settime(CRED()) != 0)
return (set_errno(EPERM));
if (time < 0)
return (set_errno(EINVAL));
ts.tv_sec = time;
ts.tv_nsec = 0;
mutex_enter(&tod_lock);
tod_set(ts);
set_hrestime(&ts);
mutex_exit(&tod_lock);
return (0);
}
#if defined(_SYSCALL32_IMPL)
int
stime32(time32_t time)
{
if (time < 0)
return (set_errno(EINVAL));
return (stime((time_t)time));
}
#endif
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/tuneable.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/debug.h>
#include <sys/msacct.h>
/*
* Return system and user times.
*/
clock_t
times(struct tms *tp)
{
proc_t *p = ttoproc(curthread);
struct tms p_time;
clock_t ret_lbolt;
mutex_enter(&p->p_lock);
p_time.tms_utime = (clock_t)NSEC_TO_TICK(
mstate_aggr_state(p, LMS_USER));
p_time.tms_stime = (clock_t)NSEC_TO_TICK(
mstate_aggr_state(p, LMS_SYSTEM));
p_time.tms_cutime = p->p_cutime;
p_time.tms_cstime = p->p_cstime;
mutex_exit(&p->p_lock);
if (copyout(&p_time, tp, sizeof (p_time)))
return (set_errno(EFAULT));
ret_lbolt = ddi_get_lbolt();
return (ret_lbolt == -1 ? 0 : ret_lbolt);
}
#ifdef _SYSCALL32_IMPL
/*
* We deliberately -don't- return EOVERFLOW on type overflow,
* since the 32-bit kernel simply wraps 'em around.
*/
clock32_t
times32(struct tms32 *tp)
{
proc_t *p = ttoproc(curthread);
struct tms32 p_time;
clock32_t ret_lbolt;
mutex_enter(&p->p_lock);
p_time.tms_utime = (clock32_t)NSEC_TO_TICK(
mstate_aggr_state(p, LMS_USER));
p_time.tms_stime = (clock32_t)NSEC_TO_TICK(
mstate_aggr_state(p, LMS_SYSTEM));
p_time.tms_cutime = (clock32_t)p->p_cutime;
p_time.tms_cstime = (clock32_t)p->p_cstime;
mutex_exit(&p->p_lock);
if (copyout(&p_time, tp, sizeof (p_time)))
return (set_errno(EFAULT));
ret_lbolt = (clock32_t)ddi_get_lbolt();
return (ret_lbolt == (clock32_t)-1 ? 0 : ret_lbolt);
}
#endif /* _SYSCALL32_IMPL */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2013 Joyent, Inc. All rights reserved.
* Copyright 2024 Oxide Computer Company
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/swap.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/var.h>
#include <sys/uadmin.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <vm/seg_kmem.h>
#include <sys/modctl.h>
#include <sys/callb.h>
#include <sys/dumphdr.h>
#include <sys/debug.h>
#include <sys/ftrace.h>
#include <sys/cmn_err.h>
#include <sys/panic.h>
#include <sys/ddi.h>
#include <sys/ddi_periodic.h>
#include <sys/sunddi.h>
#include <sys/policy.h>
#include <sys/zone.h>
#include <sys/condvar.h>
#include <sys/thread.h>
#include <sys/sdt.h>
/*
* Administrivia system call. We provide this in two flavors: one for calling
* from the system call path (uadmin), and the other for calling from elsewhere
* within the kernel (kadmin). Callers must beware that certain uadmin cmd
* values (specifically A_SWAPCTL) are only supported by uadmin and not kadmin.
*/
extern ksema_t fsflush_sema;
kmutex_t ualock;
kcondvar_t uacond;
kthread_t *ua_shutdown_thread = NULL;
int sys_shutdown = 0;
volatile int fastreboot_dryrun = 0;
/*
* Kill all user processes in said zone. A special argument of ALL_ZONES is
* passed in when the system as a whole is shutting down. The lack of per-zone
* process lists is likely to make the following a performance bottleneck on a
* system with many zones.
*/
void
killall(zoneid_t zoneid)
{
proc_t *p;
ASSERT(zoneid != GLOBAL_ZONEID);
/*
* Kill all processes except kernel daemons and ourself.
* Make a first pass to stop all processes so they won't
* be trying to restart children as we kill them.
*/
mutex_enter(&pidlock);
for (p = practive; p != NULL; p = p->p_next) {
if ((zoneid == ALL_ZONES || p->p_zone->zone_id == zoneid) &&
p->p_exec != NULLVP && /* kernel daemons */
p->p_as != &kas &&
p->p_stat != SZOMB) {
mutex_enter(&p->p_lock);
p->p_flag |= SNOWAIT;
sigtoproc(p, NULL, SIGSTOP);
mutex_exit(&p->p_lock);
}
}
p = practive;
while (p != NULL) {
if ((zoneid == ALL_ZONES || p->p_zone->zone_id == zoneid) &&
p->p_exec != NULLVP && /* kernel daemons */
p->p_as != &kas &&
p->p_stat != SIDL &&
p->p_stat != SZOMB) {
mutex_enter(&p->p_lock);
if (sigismember(&p->p_sig, SIGKILL)) {
mutex_exit(&p->p_lock);
p = p->p_next;
} else {
sigtoproc(p, NULL, SIGKILL);
mutex_exit(&p->p_lock);
(void) cv_reltimedwait(&p->p_srwchan_cv,
&pidlock, hz, TR_CLOCK_TICK);
p = practive;
}
} else {
p = p->p_next;
}
}
mutex_exit(&pidlock);
}
/*
* Emits an SDT probe (sdt:::test) with 7 arguments. This is used to test
* arguments are passed properly, whether via registers or on the stack.
*/
static void
sdt_test_args(void)
{
DTRACE_PROBE7(test, int, 1, int, 2, int, 3, int, 4, int, 5,
int, 6, int, 7);
}
/*
* Same as above, but with the probe called as a tail call.
* Unfortunately, gcc doesn't yet have a [[musttail]] attribute that would
* either generate a tail call or error at compile time if it can't. Instead
* we use a separate function along with a optimize pragma. On x86, this does
* indeed generate a tail call as written.
*/
#pragma GCC push_options
#pragma GCC optimize("optimize-sibling-calls")
static void
sdt_test_args_tail_call(int a, int b, int c, int d, int e, int f, int g)
{
DTRACE_PROBE7(test, int, a, int, b, int, c, int, d,
int, e, int, f, int, g);
}
#pragma GCC pop_options
int
kadmin(int cmd, int fcn, void *mdep, cred_t *credp)
{
int error = 0;
char *buf;
size_t buflen = 0;
boolean_t invoke_cb = B_FALSE;
/*
* We might be called directly by the kernel's fault-handling code, so
* we can't assert that the caller is in the global zone.
*/
/*
* Make sure that cmd is one of the valid <sys/uadmin.h> command codes
* and that we have appropriate privileges for this action.
*/
switch (cmd) {
case A_FTRACE:
case A_SHUTDOWN:
case A_REBOOT:
case A_REMOUNT:
case A_FREEZE:
case A_DUMP:
case A_SDTTEST:
case A_CONFIG:
if (secpolicy_sys_config(credp, B_FALSE) != 0)
return (EPERM);
break;
default:
return (EINVAL);
}
/*
* Serialize these operations on ualock. If it is held, the
* system should shutdown, reboot, or remount shortly, unless there is
* an error. We need a cv rather than just a mutex because proper
* functioning of A_REBOOT relies on being able to interrupt blocked
* userland callers.
*
* We only clear ua_shutdown_thread after A_REMOUNT or A_CONFIG.
* Other commands should never return.
*/
if (cmd == A_SHUTDOWN || cmd == A_REBOOT || cmd == A_REMOUNT ||
cmd == A_CONFIG) {
mutex_enter(&ualock);
while (ua_shutdown_thread != NULL) {
if (cv_wait_sig(&uacond, &ualock) == 0) {
/*
* If we were interrupted, leave, and handle
* the signal (or exit, depending on what
* happened)
*/
mutex_exit(&ualock);
return (EINTR);
}
}
ua_shutdown_thread = curthread;
mutex_exit(&ualock);
}
switch (cmd) {
case A_SHUTDOWN:
{
proc_t *p = ttoproc(curthread);
/*
* Release (almost) all of our own resources if we are called
* from a user context, however if we are calling kadmin() from
* a kernel context then we do not release these resources.
*/
if (p != &p0) {
proc_is_exiting(p);
if ((error = exitlwps(0)) != 0) {
/*
* Another thread in this process also called
* exitlwps().
*/
mutex_enter(&ualock);
ua_shutdown_thread = NULL;
cv_signal(&uacond);
mutex_exit(&ualock);
return (error);
}
mutex_enter(&p->p_lock);
p->p_flag |= SNOWAIT;
sigfillset(&p->p_ignore);
curthread->t_lwp->lwp_cursig = 0;
curthread->t_lwp->lwp_extsig = 0;
if (p->p_exec) {
vnode_t *exec_vp = p->p_exec;
p->p_exec = NULLVP;
mutex_exit(&p->p_lock);
VN_RELE(exec_vp);
} else {
mutex_exit(&p->p_lock);
}
pollcleanup();
closeall(P_FINFO(curproc));
relvm();
} else {
/*
* Reset t_cred if not set because much of the
* filesystem code depends on CRED() being valid.
*/
if (curthread->t_cred == NULL)
curthread->t_cred = kcred;
}
/* indicate shutdown in progress */
sys_shutdown = 1;
/*
* Communcate that init shouldn't be restarted.
*/
zone_shutdown_global();
killall(ALL_ZONES);
/*
* If we are calling kadmin() from a kernel context then we
* do not release these resources.
*/
if (ttoproc(curthread) != &p0) {
VN_RELE(PTOU(curproc)->u_cdir);
if (PTOU(curproc)->u_rdir)
VN_RELE(PTOU(curproc)->u_rdir);
if (PTOU(curproc)->u_cwd)
refstr_rele(PTOU(curproc)->u_cwd);
PTOU(curproc)->u_cdir = rootdir;
PTOU(curproc)->u_rdir = NULL;
PTOU(curproc)->u_cwd = NULL;
}
/*
* Allow the reboot/halt/poweroff code a chance to do
* anything it needs to whilst we still have filesystems
* mounted, like loading any modules necessary for later
* performing the actual poweroff.
*/
if ((mdep != NULL) && (*(char *)mdep == '/')) {
buf = i_convert_boot_device_name(mdep, NULL, &buflen);
mdpreboot(cmd, fcn, buf);
} else
mdpreboot(cmd, fcn, mdep);
/*
* Allow fsflush to finish running and then prevent it
* from ever running again so that vfs_unmountall() and
* vfs_syncall() can acquire the vfs locks they need.
*/
sema_p(&fsflush_sema);
(void) callb_execute_class(CB_CL_UADMIN_PRE_VFS, 0);
vfs_unmountall();
(void) VFS_MOUNTROOT(rootvfs, ROOT_UNMOUNT);
vfs_syncall();
/*
* Check for (and unregister) any DDI periodic handlers that
* still exist, as they most likely constitute resource leaks:
*/
ddi_periodic_fini();
dump_ereports();
dump_messages();
invoke_cb = B_TRUE;
}
/* FALLTHROUGH */
case A_REBOOT:
if ((mdep != NULL) && (*(char *)mdep == '/')) {
buf = i_convert_boot_device_name(mdep, NULL, &buflen);
mdboot(cmd, fcn, buf, invoke_cb);
} else
mdboot(cmd, fcn, mdep, invoke_cb);
/* no return expected */
break;
case A_CONFIG:
switch (fcn) {
case AD_UPDATE_BOOT_CONFIG:
#ifndef __sparc
{
extern void fastboot_update_config(const char *);
fastboot_update_config(mdep);
}
#endif
break;
}
/* Let other threads enter the shutdown path now */
mutex_enter(&ualock);
ua_shutdown_thread = NULL;
cv_signal(&uacond);
mutex_exit(&ualock);
break;
case A_REMOUNT:
(void) VFS_MOUNTROOT(rootvfs, ROOT_REMOUNT);
/* Let other threads enter the shutdown path now */
mutex_enter(&ualock);
ua_shutdown_thread = NULL;
cv_signal(&uacond);
mutex_exit(&ualock);
break;
case A_FREEZE:
{
/*
* This is the entrypoint for all suspend/resume actions.
*/
extern int cpr(int, void *);
if (modload("misc", "cpr") == -1)
return (ENOTSUP);
/* Let the CPR module decide what to do with mdep */
error = cpr(fcn, mdep);
break;
}
case A_FTRACE:
{
switch (fcn) {
case AD_FTRACE_START:
(void) FTRACE_START();
break;
case AD_FTRACE_STOP:
(void) FTRACE_STOP();
break;
default:
error = EINVAL;
}
break;
}
case A_DUMP:
{
if (fcn == AD_NOSYNC) {
in_sync = 1;
break;
}
panic_bootfcn = fcn;
panic_forced = 1;
if ((mdep != NULL) && (*(char *)mdep == '/')) {
panic_bootstr = i_convert_boot_device_name(mdep,
NULL, &buflen);
} else
panic_bootstr = mdep;
#ifndef __sparc
extern void fastboot_update_and_load(int, char *);
fastboot_update_and_load(fcn, mdep);
#endif
panic("forced crash dump initiated at user request");
/*NOTREACHED*/
}
case A_SDTTEST:
{
sdt_test_args();
sdt_test_args_tail_call(1, 2, 3, 4, 5, 6, 7);
break;
}
default:
error = EINVAL;
}
return (error);
}
int
uadmin(int cmd, int fcn, uintptr_t mdep)
{
int error = 0, rv = 0;
size_t nbytes = 0;
cred_t *credp = CRED();
char *bootargs = NULL;
int reset_status = 0;
if (cmd == A_SHUTDOWN && fcn == AD_FASTREBOOT_DRYRUN) {
ddi_walk_devs(ddi_root_node(), check_driver_quiesce,
&reset_status);
if (reset_status != 0)
return (EIO);
else
return (0);
}
/*
* The swapctl system call doesn't have its own entry point: it uses
* uadmin as a wrapper so we just call it directly from here.
*/
if (cmd == A_SWAPCTL) {
if (get_udatamodel() == DATAMODEL_NATIVE)
error = swapctl(fcn, (void *)mdep, &rv);
#if defined(_SYSCALL32_IMPL)
else
error = swapctl32(fcn, (void *)mdep, &rv);
#endif /* _SYSCALL32_IMPL */
return (error ? set_errno(error) : rv);
}
/*
* Certain subcommands intepret a non-NULL mdep value as a pointer to
* a boot string. We pull that in as bootargs, if applicable.
*/
if (mdep != (uintptr_t)NULL &&
(cmd == A_SHUTDOWN || cmd == A_REBOOT || cmd == A_DUMP ||
cmd == A_FREEZE || cmd == A_CONFIG)) {
bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP);
if ((error = copyinstr((const char *)mdep, bootargs,
BOOTARGS_MAX, &nbytes)) != 0) {
kmem_free(bootargs, BOOTARGS_MAX);
return (set_errno(error));
}
}
/*
* Invoke the appropriate kadmin() routine.
*/
if (getzoneid() != GLOBAL_ZONEID)
error = zone_kadmin(cmd, fcn, bootargs, credp);
else
error = kadmin(cmd, fcn, bootargs, credp);
if (bootargs != NULL)
kmem_free(bootargs, BOOTARGS_MAX);
return (error ? set_errno(error) : 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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/ucred.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/systm.h>
#include <sys/stream.h>
#include <sys/strsun.h>
#include <sys/stropts.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/cmn_err.h>
#include <sys/socket.h>
#include <sys/strsubr.h>
#include <c2/audit.h>
/*
* Getpeerucred system call implementation.
*/
static int
getpeerucred(int fd, void *buf)
{
file_t *fp;
struct ucred_s *uc;
vnode_t *vp;
k_peercred_t kpc;
int err;
int32_t rval;
kpc.pc_cr = NULL;
kpc.pc_cpid = -1;
if ((fp = getf(fd)) == NULL)
return (set_errno(EBADF));
vp = fp->f_vnode;
switch (vp->v_type) {
case VFIFO:
case VSOCK:
err = VOP_IOCTL(vp, _I_GETPEERCRED, (intptr_t)&kpc,
FKIOCTL, CRED(), &rval, NULL);
break;
case VCHR: {
struct strioctl strioc;
if (vp->v_stream == NULL) {
err = ENOTSUP;
break;
}
strioc.ic_cmd = _I_GETPEERCRED;
strioc.ic_timout = INFTIM;
strioc.ic_len = (int)sizeof (k_peercred_t);
strioc.ic_dp = (char *)&kpc;
err = strdoioctl(vp->v_stream, &strioc, FNATIVE|FKIOCTL,
STR_NOSIG|K_TO_K, CRED(), &rval);
/*
* Map all unexpected error codes to ENOTSUP.
*/
switch (err) {
case 0:
case ENOTSUP:
case ENOTCONN:
case ENOMEM:
break;
default:
err = ENOTSUP;
break;
}
break;
}
default:
err = ENOTSUP;
break;
}
releasef(fd);
/*
* If someone gave us a credential, err will be 0.
*/
if (kpc.pc_cr != NULL) {
ASSERT(err == 0);
uc = cred2ucred(kpc.pc_cr, kpc.pc_cpid, NULL, CRED());
crfree(kpc.pc_cr);
err = copyout(uc, buf, uc->uc_size);
kmem_free(uc, uc->uc_size);
if (err != 0)
return (set_errno(EFAULT));
return (0);
}
return (set_errno(err));
}
static int
ucred_get(pid_t pid, void *ubuf)
{
proc_t *p;
cred_t *pcr;
int err;
struct ucred_s *uc;
uint32_t auditing = AU_AUDITING();
if (pid == P_MYID || pid == curproc->p_pid) {
pcr = CRED();
crhold(pcr);
pid = curproc->p_pid;
} else {
cred_t *updcred = NULL;
if (pid < 0)
return (set_errno(EINVAL));
if (auditing)
updcred = cralloc();
mutex_enter(&pidlock);
p = prfind(pid);
if (p == NULL) {
mutex_exit(&pidlock);
if (updcred != NULL)
crfree(updcred);
return (set_errno(ESRCH));
}
/*
* Assure that audit data in cred is up-to-date.
* updcred will be used or freed.
*/
if (auditing)
audit_update_context(p, updcred);
err = priv_proc_cred_perm(CRED(), p, &pcr, VREAD);
mutex_exit(&pidlock);
if (err != 0)
return (set_errno(err));
}
uc = cred2ucred(pcr, pid, NULL, CRED());
crfree(pcr);
err = copyout(uc, ubuf, uc->uc_size);
kmem_free(uc, uc->uc_size);
if (err)
return (set_errno(EFAULT));
return (0);
}
int
ucredsys(int code, int obj, void *buf)
{
switch (code) {
case UCREDSYS_UCREDGET:
return (ucred_get((pid_t)obj, buf));
case UCREDSYS_GETPEERUCRED:
return (getpeerucred(obj, buf));
default:
return (set_errno(EINVAL));
}
}
#ifdef _SYSCALL32_IMPL
int
ucredsys32(int arg1, int arg2, caddr32_t arg3)
{
return (ucredsys(arg1, arg2, (void *)(uintptr_t)arg3));
}
#endif
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/tuneable.h>
#include <sys/cred_impl.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/signal.h>
#include <sys/debug.h>
#include <sys/policy.h>
#include <sys/zone.h>
#include <sys/sid.h>
int
setuid(uid_t uid)
{
proc_t *p;
int error;
int do_nocd = 0;
int uidchge = 0;
cred_t *cr, *newcr;
uid_t oldruid = uid;
zoneid_t zoneid = getzoneid();
ksid_t ksid, *ksp;
zone_t *zone = crgetzone(CRED());
if (!VALID_UID(uid, zone))
return (set_errno(EINVAL));
if (uid > MAXUID) {
if (ksid_lookupbyuid(zone, uid, &ksid) != 0)
return (set_errno(EINVAL));
ksp = &ksid;
} else {
ksp = NULL;
}
/*
* Need to pre-allocate the new cred structure before grabbing
* the p_crlock mutex. We can't hold on to the p_crlock for most
* if this though, now that we allow kernel upcalls from the
* policy routines.
*/
newcr = cralloc_ksid();
p = ttoproc(curthread);
retry:
mutex_enter(&p->p_crlock);
retry_locked:
cr = p->p_cred;
crhold(cr);
mutex_exit(&p->p_crlock);
if ((uid == cr->cr_ruid || uid == cr->cr_suid) &&
secpolicy_allow_setid(cr, uid, B_TRUE) != 0) {
mutex_enter(&p->p_crlock);
crfree(cr);
if (cr != p->p_cred)
goto retry_locked;
error = 0;
crcopy_to(cr, newcr);
p->p_cred = newcr;
newcr->cr_uid = uid;
crsetsid(newcr, ksp, KSID_USER);
mutex_exit(&p->p_crlock);
} else if ((error = secpolicy_allow_setid(cr, uid, B_FALSE)) == 0) {
mutex_enter(&p->p_crlock);
crfree(cr);
if (cr != p->p_cred)
goto retry_locked;
if (!uidchge && uid != cr->cr_ruid) {
/*
* The ruid of the process is going to change. In order
* to avoid a race condition involving the
* process-count associated with the newly given ruid,
* we increment the count before assigning the
* credential to the process.
* To do that, we'll have to take pidlock, so we first
* release p_crlock.
*/
mutex_exit(&p->p_crlock);
uidchge = 1;
mutex_enter(&pidlock);
upcount_inc(uid, zoneid);
mutex_exit(&pidlock);
/*
* As we released p_crlock we can't rely on the cr
* we read. So retry the whole thing.
*/
goto retry;
}
/*
* A privileged process that gives up its privilege
* must be marked to produce no core dump.
*/
if (cr->cr_uid != uid ||
cr->cr_ruid != uid ||
cr->cr_suid != uid)
do_nocd = 1;
oldruid = cr->cr_ruid;
crcopy_to(cr, newcr);
p->p_cred = newcr;
newcr->cr_ruid = uid;
newcr->cr_suid = uid;
newcr->cr_uid = uid;
/* Remove the PRIV_PFEXEC, we changed the real uid. */
if (uidchge)
CR_FLAGS(newcr) &= ~PRIV_PFEXEC;
crsetsid(newcr, ksp, KSID_USER);
priv_reset_PA(newcr, B_TRUE);
ASSERT(uid != oldruid ? uidchge : 1);
mutex_exit(&p->p_crlock);
} else {
crfree(newcr);
crfree(cr);
if (ksp != NULL)
ksid_rele(ksp);
}
/*
* We decrement the number of processes associated with the oldruid
* to match the increment above, even if the ruid of the process
* did not change or an error occurred (oldruid == uid).
*/
if (uidchge) {
mutex_enter(&pidlock);
upcount_dec(oldruid, zoneid);
mutex_exit(&pidlock);
}
if (error == 0) {
if (do_nocd) {
mutex_enter(&p->p_lock);
p->p_flag |= SNOCD;
mutex_exit(&p->p_lock);
}
crset(p, newcr); /* broadcast to process threads */
return (0);
}
return (set_errno(error));
}
int64_t
getuid(void)
{
rval_t r;
cred_t *cr;
cr = curthread->t_cred;
r.r_val1 = cr->cr_ruid;
r.r_val2 = cr->cr_uid;
return (r.r_vals);
}
int
seteuid(uid_t uid)
{
proc_t *p;
int error = EPERM;
int do_nocd = 0;
cred_t *cr, *newcr;
ksid_t ksid, *ksp;
zone_t *zone = crgetzone(CRED());
if (!VALID_UID(uid, zone))
return (set_errno(EINVAL));
if (uid > MAXUID) {
if (ksid_lookupbyuid(zone, uid, &ksid) != 0)
return (set_errno(EINVAL));
ksp = &ksid;
} else {
ksp = NULL;
}
/*
* Need to pre-allocate the new cred structure before grabbing
* the p_crlock mutex.
*/
newcr = cralloc_ksid();
p = ttoproc(curthread);
mutex_enter(&p->p_crlock);
retry:
crhold(cr = p->p_cred);
mutex_exit(&p->p_crlock);
if (uid == cr->cr_ruid || uid == cr->cr_uid || uid == cr->cr_suid ||
(error = secpolicy_allow_setid(cr, uid, B_FALSE)) == 0) {
/*
* A privileged process that makes itself look like a
* set-uid process must be marked to produce no core dump,
* if the effective uid did changed.
*/
mutex_enter(&p->p_crlock);
crfree(cr);
if (cr != p->p_cred)
goto retry;
if (cr->cr_uid != uid && error == 0)
do_nocd = 1;
error = 0;
crcopy_to(cr, newcr);
p->p_cred = newcr;
newcr->cr_uid = uid;
crsetsid(newcr, ksp, KSID_USER);
priv_reset_PA(newcr, B_FALSE);
mutex_exit(&p->p_crlock);
if (do_nocd) {
mutex_enter(&p->p_lock);
p->p_flag |= SNOCD;
mutex_exit(&p->p_lock);
}
crset(p, newcr); /* broadcast to process threads */
return (0);
}
crfree(newcr);
crfree(cr);
if (ksp != NULL)
ksid_rele(ksp);
return (set_errno(error));
}
/*
* Buy-back from SunOS 4.x
*
* Like setuid() and seteuid() combined -except- that non-root users
* can change cr_ruid to cr_uid, and the semantics of cr_suid are
* subtly different.
*/
int
setreuid(uid_t ruid, uid_t euid)
{
proc_t *p;
int error = 0;
int do_nocd = 0;
int uidchge = 0;
uid_t oldruid = ruid;
cred_t *cr, *newcr;
zoneid_t zoneid = getzoneid();
ksid_t ksid, *ksp;
zone_t *zone = crgetzone(CRED());
if ((ruid != -1 && !VALID_UID(ruid, zone)) ||
(euid != -1 && !VALID_UID(euid, zone)))
return (set_errno(EINVAL));
if (euid != -1 && euid > MAXUID) {
if (ksid_lookupbyuid(zone, euid, &ksid) != 0)
return (set_errno(EINVAL));
ksp = &ksid;
} else {
ksp = NULL;
}
/*
* Need to pre-allocate the new cred structure before grabbing
* the p_crlock mutex.
*/
newcr = cralloc_ksid();
p = ttoproc(curthread);
retry:
mutex_enter(&p->p_crlock);
retry_locked:
crhold(cr = p->p_cred);
mutex_exit(&p->p_crlock);
if (ruid != -1 && ruid != cr->cr_ruid && ruid != cr->cr_uid &&
secpolicy_allow_setid(cr, ruid, B_FALSE) != 0) {
mutex_enter(&p->p_crlock);
crfree(cr);
if (cr != p->p_cred)
goto retry_locked;
error = EPERM;
} else if (euid != -1 &&
euid != cr->cr_ruid && euid != cr->cr_uid &&
euid != cr->cr_suid && secpolicy_allow_setid(cr, euid, B_FALSE)) {
mutex_enter(&p->p_crlock);
crfree(cr);
if (cr != p->p_cred)
goto retry_locked;
error = EPERM;
} else {
mutex_enter(&p->p_crlock);
crfree(cr);
if (cr != p->p_cred)
goto retry_locked;
if (!uidchge && ruid != -1 && cr->cr_ruid != ruid) {
/*
* The ruid of the process is going to change. In order
* to avoid a race condition involving the
* process-count associated with the newly given ruid,
* we increment the count before assigning the
* credential to the process.
* To do that, we'll have to take pidlock, so we first
* release p_crlock.
*/
mutex_exit(&p->p_crlock);
uidchge = 1;
mutex_enter(&pidlock);
upcount_inc(ruid, zoneid);
mutex_exit(&pidlock);
/*
* As we released p_crlock we can't rely on the cr
* we read. So retry the whole thing.
*/
goto retry;
}
crhold(cr);
crcopy_to(cr, newcr);
p->p_cred = newcr;
if (euid != -1) {
newcr->cr_uid = euid;
crsetsid(newcr, ksp, KSID_USER);
}
if (ruid != -1) {
/* Remove the PRIV_PFEXEC, we changed the real uid. */
if (uidchge)
CR_FLAGS(newcr) &= ~PRIV_PFEXEC;
oldruid = newcr->cr_ruid;
newcr->cr_ruid = ruid;
ASSERT(ruid != oldruid ? uidchge : 1);
}
/*
* "If the real uid is being changed, or the effective uid is
* being changed to a value not equal to the real uid, the
* saved uid is set to the new effective uid."
*/
if (ruid != -1 ||
(euid != -1 && newcr->cr_uid != newcr->cr_ruid))
newcr->cr_suid = newcr->cr_uid;
/*
* A process that gives up its privilege
* must be marked to produce no core dump.
*/
if ((cr->cr_uid != newcr->cr_uid ||
cr->cr_ruid != newcr->cr_ruid ||
cr->cr_suid != newcr->cr_suid))
do_nocd = 1;
priv_reset_PA(newcr, ruid != -1 && euid != -1 && ruid == euid);
crfree(cr);
}
mutex_exit(&p->p_crlock);
/*
* We decrement the number of processes associated with the oldruid
* to match the increment above, even if the ruid of the process
* did not change or an error occurred (oldruid == uid).
*/
if (uidchge) {
ASSERT(oldruid != -1 && ruid != -1);
mutex_enter(&pidlock);
upcount_dec(oldruid, zoneid);
mutex_exit(&pidlock);
}
if (error == 0) {
if (do_nocd) {
mutex_enter(&p->p_lock);
p->p_flag |= SNOCD;
mutex_exit(&p->p_lock);
}
crset(p, newcr); /* broadcast to process threads */
return (0);
}
crfree(newcr);
if (ksp != NULL)
ksid_rele(ksp);
return (set_errno(error));
}
/*
* 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.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/debug.h>
/*
* Mode mask for creation of files.
*/
int
umask(int mask)
{
mode_t t;
t = PTOU(curproc)->u_cmask;
PTOU(curproc)->u_cmask = (mode_t)(mask & PERMMASK);
return ((int)t);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/fstyp.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/mount.h>
#include <sys/vfs.h>
#include <sys/cred.h>
#include <sys/vnode.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/pathname.h>
#include <sys/policy.h>
#include <sys/zone.h>
#define UMOUNT2_SET_ERRNO(e, is_syscall) ((is_syscall) ? set_errno((e)) : (e))
/*
* The heart of the umount2 call - it is pulled out to allow kernel
* level particpation when the only reference is the vfs pointer.
*
* Note that some of the callers may not be in the context of a
* syscall (created by zthread_create() for example) and as such
* may not have an associated curthread->t_lwp. This is handled
* by is_syscall.
*/
int
umount2_engine(vfs_t *vfsp, int flag, cred_t *cr, int is_syscall)
{
int error;
/*
* Protect the call to vn_vfswlock() with the vfs reflock. This
* ensures vfs_vnodecovered will either be NULL (because someone
* beat us to the umount) or valid (because vfs_lock() prevents
* another umount from getting through here until we've called
* vn_vfswlock() on the covered vnode).
*
* At one point, we did the non-blocking version (vfs_lock()),
* and if it failed, bailed out with EBUSY. However, dounmount()
* calls vfs_lock_wait() and we drop the vfs lock before calling
* dounmount(), so there's no difference between waiting here
* for the lock or waiting there because grabbed it as soon as
* we drop it below. No returning with EBUSY at this point
* reduces the number of spurious unmount failures that happen
* as a side-effect of fsflush() and other mount and unmount
* operations that might be going on simultaneously.
*/
vfs_lock_wait(vfsp);
/*
* Call vn_vfswlock() on the covered vnode so that dounmount()
* can do its thing. It will call the corresponding vn_vfsunlock().
* Note that vfsp->vfs_vnodecovered can be NULL here, either because
* someone did umount on "/" or because someone beat us to the umount
* before we did the vfs_lock() above. In these cases, vn_vfswlock()
* returns EBUSY and we just pass that up. Also note that we're
* looking at a vnode without doing a VN_HOLD() on it. This is
* safe because it can't go away while something is mounted on it
* and we're locking out other umounts at this point.
*/
if (vn_vfswlock(vfsp->vfs_vnodecovered)) {
vfs_unlock(vfsp);
VFS_RELE(vfsp);
return (UMOUNT2_SET_ERRNO(EBUSY, is_syscall));
}
/*
* Now that the VVFSLOCK in the covered vnode is protecting this
* path, we don't need the vfs reflock or the hold on the vfs anymore.
*/
vfs_unlock(vfsp);
VFS_RELE(vfsp);
/*
* Perform the unmount.
*/
if ((error = dounmount(vfsp, flag, cr)) != 0)
return (UMOUNT2_SET_ERRNO(error, is_syscall));
return (0);
}
/*
* New umount() system call (for force unmount flag and perhaps others later).
*/
int
umount2(char *pathp, int flag)
{
struct pathname pn;
struct vfs *vfsp;
int error;
/*
* Some flags are disallowed through the system call interface.
*/
flag &= MS_UMOUNT_MASK;
/*
* Lookup user-supplied name by trying to match it against the
* mount points recorded at mount time. If no match is found
* (which can happen if the path to the mount point is specified
* differently between mount & umount, or if a block device were
* passed to umount) then we fall back to calling lookupname()
* to find the vfs. Doing it this way prevents calling lookupname()
* in most cases and that allows forcible umount to work even if
* lookupname() would hang (i.e. because an NFS server is dead).
*/
if (error = pn_get(pathp, UIO_USERSPACE, &pn))
return (set_errno(error));
/*
* Only a privileged user is allowed to bypass the security
* checks done by lookupname() and use the results from
* vfs_mntpoint2vfsp() instead. It could be argued that the
* proper check is FILE_DAC_SEARCH but we put it all
* under the mount privilege. Also, make sure the caller
* isn't in an environment with an alternate root (to the zone's root)
* directory, i.e. chroot(2).
*/
if (secpolicy_fs_unmount(CRED(), NULL) != 0 ||
(PTOU(curproc)->u_rdir != NULL &&
PTOU(curproc)->u_rdir != curproc->p_zone->zone_rootvp) ||
(vfsp = vfs_mntpoint2vfsp(pn.pn_path)) == NULL) {
vnode_t *fsrootvp;
/* fall back to lookupname() on path given to us */
if (error = lookupname(pn.pn_path, UIO_SYSSPACE, FOLLOW,
NULLVPP, &fsrootvp)) {
pn_free(&pn);
return (set_errno(error));
}
/*
* Find the vfs to be unmounted. The caller may have specified
* either the directory mount point (preferred) or else (for a
* disk-based file system) the block device which was mounted.
* Check to see which it is; if it's the device, search the VFS
* list to find the associated vfs entry.
*/
if (fsrootvp->v_flag & VROOT) {
vfsp = fsrootvp->v_vfsp;
VFS_HOLD(vfsp);
} else if (fsrootvp->v_type == VBLK)
vfsp = vfs_dev2vfsp(fsrootvp->v_rdev);
else
vfsp = NULL;
VN_RELE(fsrootvp);
if (vfsp == NULL) {
pn_free(&pn);
return (set_errno(EINVAL));
}
}
pn_free(&pn);
return (umount2_engine(vfsp, flag, CRED(), 1));
}
/*
* 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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/utsname.h>
#include <sys/debug.h>
int
uname(struct utsname *buf)
{
char *name_to_use = uts_nodename();
if (copyout(utsname.sysname, buf->sysname, strlen(utsname.sysname)+1)) {
return (set_errno(EFAULT));
}
if (copyout(name_to_use, buf->nodename, strlen(name_to_use)+1)) {
return (set_errno(EFAULT));
}
if (copyout(utsname.release, buf->release, strlen(utsname.release)+1)) {
return (set_errno(EFAULT));
}
if (copyout(utsname.version, buf->version, strlen(utsname.version)+1)) {
return (set_errno(EFAULT));
}
if (copyout(utsname.machine, buf->machine, strlen(utsname.machine)+1)) {
return (set_errno(EFAULT));
}
return (1); /* XXX why 1 and not 0? 1003.1 says "non-negative" */
}
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/uio.h>
#include <sys/debug.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <c2/audit.h>
/*
* Unlink a file from a directory
*/
int
unlinkat(int fd, char *name, int flags)
{
vnode_t *startvp;
int error;
if (name == NULL)
return (set_errno(EFAULT));
if ((error = fgetstartvp(fd, name, &startvp)) != 0)
return (set_errno(error));
if (AU_AUDITING() && startvp != NULL)
audit_setfsat_path(1);
error = vn_removeat(startvp, name, UIO_USERSPACE,
(flags == AT_REMOVEDIR) ? RMDIRECTORY : RMFILE);
if (startvp != NULL)
VN_RELE(startvp);
if (error)
return (set_errno(error));
return (0);
}
int
unlink(char *name)
{
return (unlinkat(AT_FDCWD, name, 0));
}
int
rmdir(char *name)
{
return (unlinkat(AT_FDCWD, name, AT_REMOVEDIR));
}
/*
* 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) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/vfs.h>
#include <sys/time.h>
#include <sys/debug.h>
#include <sys/model.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/pathname.h>
#include <c2/audit.h>
static int
cfutimesat(int fd, char *fname, int nmflag, vattr_t *vap, int flags, int follow)
{
file_t *fp;
vnode_t *startvp, *vp;
int error;
char startchar;
if (fd == AT_FDCWD && fname == NULL)
return (set_errno(EFAULT));
if (nmflag == 1 || (nmflag == 2 && fname != NULL)) {
if (copyin(fname, &startchar, sizeof (char)))
return (set_errno(EFAULT));
} else {
startchar = '\0';
}
if (fd == AT_FDCWD) {
startvp = NULL;
} else {
/*
* is this absolute path?
*/
if (startchar != '/') {
if ((fp = getf(fd)) == NULL)
return (set_errno(EBADF));
startvp = fp->f_vnode;
VN_HOLD(startvp);
releasef(fd);
} else {
startvp = NULL;
}
}
if ((nmflag == 1) || ((nmflag == 2) && (fname != NULL))) {
if (AU_AUDITING() && startvp != NULL)
audit_setfsat_path(1);
if ((error = lookupnameat(fname, UIO_USERSPACE,
follow, NULLVPP, &vp, startvp)) != 0) {
if (startvp != NULL)
VN_RELE(startvp);
return (set_errno(error));
}
} else {
vp = startvp;
VN_HOLD(vp);
}
if (startvp != NULL) {
VN_RELE(startvp);
}
if (vn_is_readonly(vp)) {
error = EROFS;
} else {
error = VOP_SETATTR(vp, vap, flags, CRED(), NULL);
}
VN_RELE(vp);
if (error != 0)
return (set_errno(error));
return (0);
}
static int
get_timespec_vattr(timespec_t *tsptr, struct vattr *vattr, int *flags)
{
timespec_t ts[2];
timespec_t now;
uint_t mask;
if (tsptr != NULL) {
if (get_udatamodel() == DATAMODEL_NATIVE) {
if (copyin(tsptr, ts, sizeof (ts)))
return (EFAULT);
} else {
timespec32_t ts32[2];
if (copyin(tsptr, ts32, sizeof (ts32)))
return (EFAULT);
TIMESPEC32_TO_TIMESPEC(&ts[0], &ts32[0]);
TIMESPEC32_TO_TIMESPEC(&ts[1], &ts32[1]);
}
if (ts[0].tv_nsec == UTIME_NOW || ts[1].tv_nsec == UTIME_NOW)
gethrestime(&now);
mask = 0;
if (ts[0].tv_nsec == UTIME_OMIT) {
ts[0].tv_nsec = 0;
} else {
mask |= AT_ATIME;
if (ts[0].tv_nsec == UTIME_NOW)
ts[0] = now;
else if (ts[0].tv_nsec < 0 || ts[0].tv_nsec >= NANOSEC)
return (EINVAL);
}
if (ts[1].tv_nsec == UTIME_OMIT) {
ts[1].tv_nsec = 0;
} else {
mask |= AT_MTIME;
if (ts[1].tv_nsec == UTIME_NOW)
ts[1] = now;
else if (ts[1].tv_nsec < 0 || ts[1].tv_nsec >= NANOSEC)
return (EINVAL);
}
vattr->va_atime = ts[0];
vattr->va_mtime = ts[1];
vattr->va_mask = mask;
*flags = ATTR_UTIME;
} else {
gethrestime(&now);
vattr->va_atime = now;
vattr->va_mtime = now;
vattr->va_mask = AT_ATIME | AT_MTIME;
*flags = 0;
}
return (0);
}
int
futimens(int fd, timespec_t *tsptr)
{
struct vattr vattr;
int flags;
int error;
if ((error = get_timespec_vattr(tsptr, &vattr, &flags)) != 0)
return (set_errno(error));
return (cfutimesat(fd, NULL, 2, &vattr, flags, FOLLOW));
}
int
utimensat(int fd, char *fname, timespec_t *tsptr, int flag)
{
struct vattr vattr;
int flags;
int error;
if ((error = get_timespec_vattr(tsptr, &vattr, &flags)) != 0)
return (set_errno(error));
return (cfutimesat(fd, fname, 1, &vattr, flags,
(flag & AT_SYMLINK_NOFOLLOW)? NO_FOLLOW : FOLLOW));
}
int
utimesys(int code,
uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
{
switch (code) {
case 0:
return (futimens((int)arg1, (timespec_t *)arg2));
case 1:
return (utimensat((int)arg1, (char *)arg2,
(timespec_t *)arg3, (int)arg4));
default:
return (set_errno(EINVAL));
}
}
/*
* 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.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/inttypes.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/session.h>
#include <sys/var.h>
#include <sys/utsname.h>
#include <sys/utssys.h>
#include <sys/ustat.h>
#include <sys/statvfs.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <sys/pathname.h>
#include <sys/modctl.h>
#include <sys/fs/snode.h>
#include <sys/sunldi_impl.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/cmn_err.h>
#include <sys/ddipropdefs.h>
#include <sys/ddi_impldefs.h>
#include <sys/modctl.h>
#include <sys/flock.h>
#include <sys/share.h>
#include <vm/as.h>
#include <vm/seg.h>
#include <vm/seg_vn.h>
#include <util/qsort.h>
#include <sys/zone.h>
/*
* utssys()
*/
static int uts_fusers(char *, int, intptr_t);
static int _statvfs64_by_dev(dev_t, struct statvfs64 *);
#if defined(_ILP32) || defined(_SYSCALL32_IMPL)
static int utssys_uname32(caddr_t, rval_t *);
static int utssys_ustat32(dev_t, struct ustat32 *);
int64_t
utssys32(void *buf, int arg, int type, void *outbp)
{
int error;
rval_t rv;
rv.r_vals = 0;
switch (type) {
case UTS_UNAME:
/*
* This is an obsolete way to get the utsname structure
* (it only gives you the first 8 characters of each field!)
* uname(2) is the preferred and better interface.
*/
error = utssys_uname32(buf, &rv);
break;
case UTS_USTAT:
error = utssys_ustat32(expldev((dev32_t)arg), buf);
break;
case UTS_FUSERS:
error = uts_fusers(buf, arg, (intptr_t)outbp);
break;
default:
error = EINVAL;
break;
}
return (error == 0 ? rv.r_vals : (int64_t)set_errno(error));
}
static int
utssys_uname32(caddr_t buf, rval_t *rvp)
{
if (copyout(utsname.sysname, buf, 8))
return (EFAULT);
buf += 8;
if (subyte(buf, 0) < 0)
return (EFAULT);
buf++;
if (copyout(uts_nodename(), buf, 8))
return (EFAULT);
buf += 8;
if (subyte(buf, 0) < 0)
return (EFAULT);
buf++;
if (copyout(utsname.release, buf, 8))
return (EFAULT);
buf += 8;
if (subyte(buf, 0) < 0)
return (EFAULT);
buf++;
if (copyout(utsname.version, buf, 8))
return (EFAULT);
buf += 8;
if (subyte(buf, 0) < 0)
return (EFAULT);
buf++;
if (copyout(utsname.machine, buf, 8))
return (EFAULT);
buf += 8;
if (subyte(buf, 0) < 0)
return (EFAULT);
rvp->r_val1 = 1;
return (0);
}
static int
utssys_ustat32(dev_t dev, struct ustat32 *cbuf)
{
struct ustat32 ust32;
struct statvfs64 stvfs;
fsblkcnt64_t fsbc64;
char *cp, *cp2;
int i, error;
if ((error = _statvfs64_by_dev(dev, &stvfs)) != 0)
return (error);
fsbc64 = stvfs.f_bfree * (stvfs.f_frsize / 512);
/*
* Check to see if the number of free blocks can be expressed
* in 31 bits or whether the number of free files is more than
* can be expressed in 32 bits and is not -1 (UINT64_MAX). NFS
* Version 2 does not support the number of free files and
* hence will return -1. -1, when translated from a 32 bit
* quantity to an unsigned 64 bit quantity, turns into UINT64_MAX.
*/
if (fsbc64 > INT32_MAX ||
(stvfs.f_ffree > UINT32_MAX && stvfs.f_ffree != UINT64_MAX))
return (EOVERFLOW);
ust32.f_tfree = (daddr32_t)fsbc64;
ust32.f_tinode = (ino32_t)stvfs.f_ffree;
cp = stvfs.f_fstr;
cp2 = ust32.f_fname;
i = 0;
while (i++ < sizeof (ust32.f_fname))
if (*cp != '\0')
*cp2++ = *cp++;
else
*cp2++ = '\0';
while (*cp != '\0' &&
(i++ < sizeof (stvfs.f_fstr) - sizeof (ust32.f_fpack)))
cp++;
(void) strncpy(ust32.f_fpack, cp + 1, sizeof (ust32.f_fpack));
if (copyout(&ust32, cbuf, sizeof (ust32)))
return (EFAULT);
return (0);
}
#endif /* _ILP32 || _SYSCALL32_IMPL */
#ifdef _LP64
static int uts_ustat64(dev_t, struct ustat *);
int64_t
utssys64(void *buf, long arg, int type, void *outbp)
{
int error;
rval_t rv;
rv.r_vals = 0;
switch (type) {
case UTS_USTAT:
error = uts_ustat64((dev_t)arg, buf);
break;
case UTS_FUSERS:
error = uts_fusers(buf, (int)arg, (intptr_t)outbp);
break;
default:
error = EINVAL;
break;
}
return (error == 0 ? rv.r_vals : (int64_t)set_errno(error));
}
static int
uts_ustat64(dev_t dev, struct ustat *cbuf)
{
struct ustat ust;
struct statvfs64 stvfs;
fsblkcnt64_t fsbc64;
char *cp, *cp2;
int i, error;
if ((error = _statvfs64_by_dev(dev, &stvfs)) != 0)
return (error);
fsbc64 = stvfs.f_bfree * (stvfs.f_frsize / 512);
ust.f_tfree = (daddr_t)fsbc64;
ust.f_tinode = (ino_t)stvfs.f_ffree;
cp = stvfs.f_fstr;
cp2 = ust.f_fname;
i = 0;
while (i++ < sizeof (ust.f_fname))
if (*cp != '\0')
*cp2++ = *cp++;
else
*cp2++ = '\0';
while (*cp != '\0' &&
(i++ < sizeof (stvfs.f_fstr) - sizeof (ust.f_fpack)))
cp++;
(void) strncpy(ust.f_fpack, cp + 1, sizeof (ust.f_fpack));
if (copyout(&ust, cbuf, sizeof (ust)))
return (EFAULT);
return (0);
}
#endif /* _LP64 */
/*
* Utility routine for the ustat implementations.
* (If it wasn't for the 'find-by-dev_t' semantic of ustat(2), we could push
* this all out into userland, sigh.)
*/
static int
_statvfs64_by_dev(dev_t dev, struct statvfs64 *svp)
{
vfs_t *vfsp;
int error;
if ((vfsp = vfs_dev2vfsp(dev)) == NULL) {
/*
* See if it's the root of our zone.
*/
vfsp = curproc->p_zone->zone_rootvp->v_vfsp;
if (vfsp->vfs_dev == dev) {
VFS_HOLD(vfsp);
} else {
vfsp = NULL;
}
}
if (vfsp == NULL)
return (EINVAL);
error = VFS_STATVFS(vfsp, svp);
VFS_RELE(vfsp);
return (error);
}
/*
* Check if this pid has an NBMAND lock or share reservation
* on this vp. llp is a snapshoted list of all NBMAND locks
* set by this pid. Return 1 if there is an NBMAND lock else
* return 0.
*/
static int
proc_has_nbmand_on_vp(vnode_t *vp, pid_t pid, locklist_t *llp)
{
/*
* Any NBMAND lock held by the process on this vp?
*/
while (llp) {
if (llp->ll_vp == vp) {
return (1);
}
llp = llp->ll_next;
}
/*
* Any NBMAND share reservation on the vp for this process?
*/
return (proc_has_nbmand_share_on_vp(vp, pid));
}
static fu_data_t *
dofusers(vnode_t *fvp, int flags)
{
fu_data_t *fu_data;
proc_t *prp;
vfs_t *cvfsp;
pid_t npids, pidx, *pidlist;
int v_proc = v.v_proc; /* max # of procs */
int pcnt = 0;
int contained = (flags & F_CONTAINED);
int nbmandonly = (flags & F_NBMANDLIST);
int dip_usage = (flags & F_DEVINFO);
int fvp_isdev = vn_matchops(fvp, spec_getvnodeops());
zone_t *zone = curproc->p_zone;
int inglobal = INGLOBALZONE(curproc);
/* get a pointer to the file system containing this vnode */
cvfsp = fvp->v_vfsp;
ASSERT(cvfsp);
/* allocate the data structure to return our results in */
fu_data = kmem_alloc(fu_data_size(v_proc), KM_SLEEP);
fu_data->fud_user_max = v_proc;
fu_data->fud_user_count = 0;
/* get a snapshot of all the pids we're going to check out */
pidlist = kmem_alloc(v_proc * sizeof (pid_t), KM_SLEEP);
mutex_enter(&pidlock);
for (npids = 0, prp = practive; prp != NULL; prp = prp->p_next) {
if (inglobal || prp->p_zone == zone)
pidlist[npids++] = prp->p_pid;
}
mutex_exit(&pidlock);
/* grab each process and check its file usage */
for (pidx = 0; pidx < npids; pidx++) {
locklist_t *llp = NULL;
uf_info_t *fip;
vnode_t *vp;
user_t *up;
sess_t *sp;
uid_t uid;
pid_t pid = pidlist[pidx];
int i, use_flag = 0;
/*
* grab prp->p_lock using sprlock()
* if sprlock() fails the process does not exists anymore
*/
prp = sprlock(pid);
if (prp == NULL)
continue;
/* get the processes credential info in case we need it */
mutex_enter(&prp->p_crlock);
uid = crgetruid(prp->p_cred);
mutex_exit(&prp->p_crlock);
/*
* it's safe to drop p_lock here because we
* called sprlock() before and it set the P_PR_LOCK
* flag for the process so it won't go away.
*/
mutex_exit(&prp->p_lock);
/*
* now we want to walk a processes open file descriptors
* to do this we need to grab the fip->fi_lock. (you
* can't hold p_lock when grabbing the fip->fi_lock.)
*/
fip = P_FINFO(prp);
mutex_enter(&fip->fi_lock);
/*
* Snapshot nbmand locks for pid
*/
llp = flk_active_nbmand_locks(prp->p_pid);
for (i = 0; i < fip->fi_nfiles; i++) {
uf_entry_t *ufp;
file_t *fp;
UF_ENTER(ufp, fip, i);
if (((fp = ufp->uf_file) == NULL) ||
((vp = fp->f_vnode) == NULL)) {
UF_EXIT(ufp);
continue;
}
/*
* if the target file (fvp) is not a device
* and corrosponds to the root of a filesystem
* (cvfsp), then check if it contains the file
* is use by this process (vp).
*/
if (contained && (vp->v_vfsp == cvfsp))
use_flag |= F_OPEN;
/*
* if the target file (fvp) is not a device,
* then check if it matches the file in use
* by this process (vp).
*/
if (!fvp_isdev && VN_CMP(fvp, vp))
use_flag |= F_OPEN;
/*
* if the target file (fvp) is a device,
* then check if the current file in use
* by this process (vp) maps to the same device
* minor node.
*/
if (fvp_isdev &&
vn_matchops(vp, spec_getvnodeops()) &&
(fvp->v_rdev == vp->v_rdev))
use_flag |= F_OPEN;
/*
* if the target file (fvp) is a device,
* and we're checking for device instance
* usage, then check if the current file in use
* by this process (vp) maps to the same device
* instance.
*/
if (dip_usage &&
vn_matchops(vp, spec_getvnodeops()) &&
(VTOCS(fvp)->s_dip == VTOCS(vp)->s_dip))
use_flag |= F_OPEN;
/*
* if the current file in use by this process (vp)
* doesn't match what we're looking for, move on
* to the next file in the process.
*/
if ((use_flag & F_OPEN) == 0) {
UF_EXIT(ufp);
continue;
}
if (proc_has_nbmand_on_vp(vp, prp->p_pid, llp)) {
/* A nbmand found so we're done. */
use_flag |= F_NBM;
UF_EXIT(ufp);
break;
}
UF_EXIT(ufp);
}
if (llp)
flk_free_locklist(llp);
mutex_exit(&fip->fi_lock);
/*
* If nbmand usage tracking is desired and no nbmand was
* found for this process, then no need to do further
* usage tracking for this process.
*/
if (nbmandonly && (!(use_flag & F_NBM))) {
/*
* grab the process lock again, clear the P_PR_LOCK
* flag, release the process, and continue.
*/
mutex_enter(&prp->p_lock);
sprunlock(prp);
continue;
}
/*
* All other types of usage.
* For the next few checks we need to hold p_lock.
*/
mutex_enter(&prp->p_lock);
up = PTOU(prp);
if (fvp_isdev) {
/*
* if the target file (fvp) is a device
* then check if it matches the processes tty
*
* we grab s_lock to protect ourselves against
* freectty() freeing the vnode out from under us.
*/
sp = prp->p_sessp;
mutex_enter(&sp->s_lock);
vp = prp->p_sessp->s_vp;
if (vp != NULL) {
if (fvp->v_rdev == vp->v_rdev)
use_flag |= F_TTY;
if (dip_usage &&
(VTOCS(fvp)->s_dip == VTOCS(vp)->s_dip))
use_flag |= F_TTY;
}
mutex_exit(&sp->s_lock);
} else {
/* check the processes current working directory */
if (up->u_cdir &&
(VN_CMP(fvp, up->u_cdir) ||
(contained && (up->u_cdir->v_vfsp == cvfsp))))
use_flag |= F_CDIR;
/* check the processes root directory */
if (up->u_rdir &&
(VN_CMP(fvp, up->u_rdir) ||
(contained && (up->u_rdir->v_vfsp == cvfsp))))
use_flag |= F_RDIR;
/* check the program text vnode */
if (prp->p_exec &&
(VN_CMP(fvp, prp->p_exec) ||
(contained && (prp->p_exec->v_vfsp == cvfsp))))
use_flag |= F_TEXT;
}
/* Now we can drop p_lock again */
mutex_exit(&prp->p_lock);
/*
* now we want to walk a processes memory mappings.
* to do this we need to grab the prp->p_as lock. (you
* can't hold p_lock when grabbing the prp->p_as lock.)
*/
if (prp->p_as != &kas) {
struct seg *seg;
struct as *as = prp->p_as;
AS_LOCK_ENTER(as, RW_READER);
for (seg = AS_SEGFIRST(as); seg;
seg = AS_SEGNEXT(as, seg)) {
/*
* if we can't get a backing vnode for this
* segment then skip it
*/
vp = NULL;
if ((SEGOP_GETVP(seg, seg->s_base, &vp)) ||
(vp == NULL))
continue;
/*
* if the target file (fvp) is not a device
* and corrosponds to the root of a filesystem
* (cvfsp), then check if it contains the
* vnode backing this segment (vp).
*/
if (contained && (vp->v_vfsp == cvfsp)) {
use_flag |= F_MAP;
break;
}
/*
* if the target file (fvp) is not a device,
* check if it matches the the vnode backing
* this segment (vp).
*/
if (!fvp_isdev && VN_CMP(fvp, vp)) {
use_flag |= F_MAP;
break;
}
/*
* if the target file (fvp) isn't a device,
* or the the vnode backing this segment (vp)
* isn't a device then continue.
*/
if (!fvp_isdev ||
!vn_matchops(vp, spec_getvnodeops()))
continue;
/*
* check if the vnode backing this segment
* (vp) maps to the same device minor node
* as the target device (fvp)
*/
if (fvp->v_rdev == vp->v_rdev) {
use_flag |= F_MAP;
break;
}
/*
* if we're checking for device instance
* usage, then check if the vnode backing
* this segment (vp) maps to the same device
* instance as the target device (fvp).
*/
if (dip_usage &&
(VTOCS(fvp)->s_dip == VTOCS(vp)->s_dip)) {
use_flag |= F_MAP;
break;
}
}
AS_LOCK_EXIT(as);
}
if (use_flag) {
ASSERT(pcnt < fu_data->fud_user_max);
fu_data->fud_user[pcnt].fu_flags = use_flag;
fu_data->fud_user[pcnt].fu_pid = pid;
fu_data->fud_user[pcnt].fu_uid = uid;
pcnt++;
}
/*
* grab the process lock again, clear the P_PR_LOCK
* flag, release the process, and continue.
*/
mutex_enter(&prp->p_lock);
sprunlock(prp);
}
kmem_free(pidlist, v_proc * sizeof (pid_t));
fu_data->fud_user_count = pcnt;
return (fu_data);
}
typedef struct dofkusers_arg {
vnode_t *fvp;
int flags;
int *error;
fu_data_t *fu_data;
} dofkusers_arg_t;
static int
dofkusers_walker(const ldi_usage_t *ldi_usage, void *arg)
{
dofkusers_arg_t *dofkusers_arg = (dofkusers_arg_t *)arg;
vnode_t *fvp = dofkusers_arg->fvp;
int flags = dofkusers_arg->flags;
int *error = dofkusers_arg->error;
fu_data_t *fu_data = dofkusers_arg->fu_data;
modid_t modid;
minor_t minor;
int instance;
int dip_usage = (flags & F_DEVINFO);
ASSERT(*error == 0);
ASSERT(vn_matchops(fvp, spec_getvnodeops()));
/*
* check if the dev_t of the target device matches the dev_t
* of the device we're trying to find usage info for.
*/
if (fvp->v_rdev != ldi_usage->tgt_devt) {
/*
* if the dev_ts don't match and we're not trying
* to find usage information for device instances
* then return
*/
if (!dip_usage)
return (LDI_USAGE_CONTINUE);
/*
* we're trying to find usage information for an
* device instance instead of just a minor node.
*
* check if the dip for the target device matches the
* dip of the device we're trying to find usage info for.
*/
if (VTOCS(fvp)->s_dip != ldi_usage->tgt_dip)
return (LDI_USAGE_CONTINUE);
}
if (fu_data->fud_user_count >= fu_data->fud_user_max) {
*error = E2BIG;
return (LDI_USAGE_TERMINATE);
}
/* get the device vnode user information */
modid = ldi_usage->src_modid;
ASSERT(modid != -1);
minor = instance = -1;
if (ldi_usage->src_dip != NULL) {
instance = DEVI(ldi_usage->src_dip)->devi_instance;
}
if (ldi_usage->src_devt != DDI_DEV_T_NONE) {
minor = getminor(ldi_usage->src_devt);
}
/* set the device vnode user information */
fu_data->fud_user[fu_data->fud_user_count].fu_flags = F_KERNEL;
fu_data->fud_user[fu_data->fud_user_count].fu_modid = modid;
fu_data->fud_user[fu_data->fud_user_count].fu_instance = instance;
fu_data->fud_user[fu_data->fud_user_count].fu_minor = minor;
fu_data->fud_user_count++;
return (LDI_USAGE_CONTINUE);
}
int
f_user_cmp(const void *arg1, const void *arg2)
{
f_user_t *f_user1 = (f_user_t *)arg1;
f_user_t *f_user2 = (f_user_t *)arg2;
/*
* we should only be called for f_user_t entires that represent
* a kernel file consumer
*/
ASSERT(f_user1->fu_flags & F_KERNEL);
ASSERT(f_user2->fu_flags & F_KERNEL);
if (f_user1->fu_modid != f_user2->fu_modid)
return ((f_user1->fu_modid < f_user2->fu_modid) ? -1 : 1);
if (f_user1->fu_instance != f_user2->fu_instance)
return ((f_user1->fu_instance < f_user2->fu_instance) ? -1 : 1);
if (f_user1->fu_minor != f_user2->fu_minor)
return ((f_user1->fu_minor < f_user2->fu_minor) ? -1 : 1);
return (0);
}
static fu_data_t *
dofkusers(vnode_t *fvp, int flags, int *error)
{
dofkusers_arg_t dofkusers_arg;
fu_data_t *fu_data;
int user_max, i;
/*
* we only keep track of kernel device consumers, so if the
* target vnode isn't a device then there's nothing to do here
*/
if (!vn_matchops(fvp, spec_getvnodeops()))
return (NULL);
/* allocate the data structure to return our results in */
user_max = ldi_usage_count();
fu_data = kmem_alloc(fu_data_size(user_max), KM_SLEEP);
fu_data->fud_user_max = user_max;
fu_data->fud_user_count = 0;
/* invoke the callback to collect device usage information */
dofkusers_arg.fvp = fvp;
dofkusers_arg.flags = flags;
dofkusers_arg.error = error;
dofkusers_arg.fu_data = fu_data;
ldi_usage_walker(&dofkusers_arg, dofkusers_walker);
/* check for errors */
if (*error != 0)
return (fu_data);
/* if there aren't any file consumers then return */
if (fu_data->fud_user_count == 0)
return (fu_data);
/*
* since we ignore the spec_type of the target we're trying to
* access it's possible that we could have duplicates entries in
* the list of consumers.
*
* we don't want to check for duplicate in the callback because
* we're holding locks in the ldi when the callback is invoked.
*
* so here we need to go through the array of file consumers
* and remove duplicate entries.
*/
/* first sort the array of file consumers */
qsort((caddr_t)fu_data->fud_user, fu_data->fud_user_count,
sizeof (f_user_t), f_user_cmp);
/* then remove any duplicate entires */
i = 1;
while (i < fu_data->fud_user_count) {
if (f_user_cmp(&fu_data->fud_user[i],
&fu_data->fud_user[i - 1]) != 0) {
/*
* the current element is unique, move onto
* the next one
*/
i++;
continue;
}
/*
* this entry is a duplicate so if it's not the last
* entry in the array then remove it.
*/
fu_data->fud_user_count--;
if (i == fu_data->fud_user_count)
break;
bcopy(&fu_data->fud_user[i + 1], &fu_data->fud_user[i],
sizeof (f_user_t) * (fu_data->fud_user_count - i));
}
return (fu_data);
}
/*
* Determine the ways in which processes and the kernel are using a named
* file or mounted file system (path). Normally return 0. In case of an
* error appropriate errno will be returned.
*
* Upon success, uts_fusers will also copyout the file usage information
* in the form of an array of f_user_t's that are contained within an
* fu_data_t pointed to by userbp.
*/
static int
uts_fusers(char *path, int flags, intptr_t userbp)
{
fu_data_t *fu_data = NULL, *fuk_data = NULL;
fu_data_t fu_header;
vnode_t *fvp = NULL;
size_t bcount;
int error = 0;
int total_max, total_out;
int contained = (flags & F_CONTAINED);
int dip_usage = (flags & F_DEVINFO);
int fvp_isdev;
/* figure out how man f_user_t's we can safetly copy out */
if (copyin((const void *)userbp, &total_max, sizeof (total_max)))
return (EFAULT);
/*
* check if we only want a count of how many kernel device
* consumers exist
*/
if (flags & F_KINFO_COUNT) {
fu_header.fud_user_max = total_max;
fu_header.fud_user_count = ldi_usage_count();
bcount = fu_data_size(0);
if (copyout(&fu_header, (void *)userbp, bcount))
return (EFAULT);
return (0);
}
/* get the vnode for the file we want to look up usage for */
error = lookupname(path, UIO_USERSPACE, FOLLOW, NULLVPP, &fvp);
if (error != 0)
return (error);
ASSERT(fvp);
fvp_isdev = vn_matchops(fvp, spec_getvnodeops());
/*
* if we want to report usage for all files contained within a
* file system then the target file better correspond to the
* root node of a mounted file system, or the root of a zone.
*/
if (contained && !(fvp->v_flag & VROOT) &&
fvp != curproc->p_zone->zone_rootvp) {
error = EINVAL;
goto out;
}
/*
* if we want to report usage for all files contained within a
* file system then the target file better not be a device.
*/
if (contained && fvp_isdev) {
error = EINVAL;
goto out;
}
/*
* if we want to report usage for a device instance then the
* target file better corrospond to a device
*/
if (dip_usage && !fvp_isdev) {
error = EINVAL;
goto out;
}
/*
* if the target vnode isn't a device and it has a reference count
* of one then no one else is going to have it open so we don't
* have any work to do.
*/
if (!fvp_isdev && (fvp->v_count == 1)) {
goto out;
}
/* look up usage information for this vnode */
fu_data = dofusers(fvp, flags);
fuk_data = dofkusers(fvp, flags, &error);
if (error != 0)
goto out;
/* get a count of the number of f_user_t's we need to copy out */
total_out = 0;
if (fu_data)
total_out += fu_data->fud_user_count;
if (fuk_data)
total_out += fuk_data->fud_user_count;
/* check if there is enough space to copyout all results */
if (total_out > total_max) {
error = E2BIG;
goto out;
}
/* copyout file usage info counts */
fu_header.fud_user_max = total_max;
fu_header.fud_user_count = total_out;
bcount = fu_data_size(0);
if (copyout(&fu_header, (void *)userbp, bcount)) {
error = EFAULT;
goto out;
}
/* copyout userland process file usage info */
if ((fu_data != NULL) && (fu_data->fud_user_count > 0)) {
userbp += bcount;
bcount = fu_data->fud_user_count * sizeof (f_user_t);
if (copyout(fu_data->fud_user, (void *)userbp, bcount)) {
error = EFAULT;
goto out;
}
}
/* copyout kernel file usage info */
if ((fuk_data != NULL) && (fuk_data->fud_user_count > 0)) {
userbp += bcount;
bcount = fuk_data->fud_user_count * sizeof (f_user_t);
if (copyout(fuk_data->fud_user, (void *)userbp, bcount)) {
error = EFAULT;
goto out;
}
}
out:
/* release the vnode that we were looking up usage for */
VN_RELE(fvp);
/* release any allocated memory */
if (fu_data)
kmem_free(fu_data, fu_data_size(fu_data->fud_user_max));
if (fuk_data)
kmem_free(fuk_data, fu_data_size(fuk_data->fud_user_max));
return (error);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/systm.h>
int
uucopy(const void *from, void *to, size_t size)
{
label_t ljb;
if (on_fault(&ljb))
return (set_errno(EFAULT));
ucopy(from, to, size);
no_fault();
return (0);
}
ssize_t
uucopystr(const char *from, char *to, size_t size)
{
label_t ljb;
size_t len;
if (on_fault(&ljb))
return (set_errno(EFAULT));
ucopystr(from, to, size, &len);
no_fault();
return ((ssize_t)len);
}
/*
* 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 1996-2002 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/thread.h>
#include <sys/disp.h>
#include <sys/debug.h>
#include <sys/cpuvar.h>
/*
* The calling LWP is preempted in favor of some other LWP.
*/
int
yield()
{
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
thread_lock(t);
lwp->lwp_ru.nvcsw++;
THREAD_TRANSITION(t);
CL_YIELD(t); /* does setbackdq */
thread_unlock_nopreempt(t);
swtch(); /* clears cpu_runrun and cpu_kprunrun */
return (0);
}
|