Loading...
   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
/*
 * Copyright (c) 2005-2020 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */

/*
 * sysctl system call.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/file_internal.h>
#include <sys/vnode_internal.h>
#include <sys/unistd.h>
#include <sys/buf.h>
#include <sys/ioctl.h>
#include <sys/namei.h>
#include <sys/tty.h>
#include <sys/disklabel.h>
#include <sys/vm.h>
#include <sys/reason.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <sys/aio_kern.h>
#include <sys/kern_memorystatus.h>

#include <security/audit/audit.h>

#include <mach/machine.h>
#include <mach/mach_types.h>
#include <mach/vm_param.h>
#include <kern/task.h>
#include <kern/kalloc.h>
#include <kern/assert.h>
#include <kern/policy_internal.h>

#include <vm/vm_kern.h>
#include <vm/vm_map.h>
#include <mach/host_info.h>
#include <mach/task_info.h>
#include <mach/thread_info.h>
#include <mach/vm_region.h>
#include <mach/vm_types.h>

#include <sys/mount_internal.h>
#include <sys/proc_info.h>
#include <sys/bsdtask_info.h>
#include <sys/kdebug.h>
#include <sys/sysproto.h>
#include <sys/msgbuf.h>
#include <sys/priv.h>

#include <sys/guarded.h>

#include <machine/machine_routines.h>

#include <kern/ipc_misc.h>

#include <vm/vm_protos.h>

/* Needed by proc_pidnoteexit(), proc_pidlistuptrs() */
#include <sys/event.h>
#include <sys/codesign.h>

/* Needed by proc_listcoalitions() */
#ifdef CONFIG_COALITIONS
#include <sys/coalition.h>
#endif

#if CONFIG_MACF
#include <security/mac_framework.h>
#endif

struct pshmnode;
struct psemnode;
struct pipe;
struct kqueue;
struct atalk;

uint64_t get_dispatchqueue_offset_from_proc(void *);
uint64_t get_dispatchqueue_serialno_offset_from_proc(void *);
uint64_t get_dispatchqueue_label_offset_from_proc(void *p);
uint64_t get_return_to_kernel_offset_from_proc(void *p);
uint64_t get_wq_quantum_offset_from_proc(void *p);
int proc_info_internal(int callnum, int pid, uint32_t flags, uint64_t ext_id, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval);

/*
 * TODO: Replace the noinline attribute below.  Currently, it serves
 * to avoid stack bloat caused by inlining multiple functions that
 * have large stack footprints; when the functions are independent
 * of each other (will not both be called in any given call to the
 * caller), this only serves to bloat the stack, as we allocate
 * space for both functions, despite the fact that we only need a
 * fraction of that space.
 *
 * Long term, these functions should not be allocating everything on
 * the stack, and should move large allocations (the huge structs
 * that proc info deals in) to the heap, or eliminate them if
 * possible.
 *
 * The functions that most desperately need to improve stack usage
 * (starting with the worst offenders):
 *   proc_pidvnodepathinfo
 *   proc_pidinfo
 *   proc_pidregionpathinfo
 *   pid_vnodeinfopath
 *   pid_pshminfo
 *   pid_pseminfo
 *   pid_socketinfo
 *   proc_pid_rusage
 *   proc_pidoriginatorinfo
 */

/* protos for proc_info calls */
static int __attribute__ ((noinline)) proc_listpids(uint32_t type, uint32_t tyoneinfo, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) proc_pidinfo(int pid, uint32_t flags, uint64_t ext_id, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) proc_pidfdinfo(int pid, int flavor, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) proc_kernmsgbuf(user_addr_t buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) proc_setcontrol(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) proc_pidfileportinfo(int pid, int flavor, mach_port_name_t name, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_dirtycontrol(int pid, int flavor, uint64_t arg, int32_t * retval);
static int __attribute__ ((noinline)) proc_terminate(int pid, int32_t * retval);
static int __attribute__ ((noinline)) proc_pid_rusage(int pid, int flavor, user_addr_t buffer, int32_t * retval);
static int __attribute__ ((noinline)) proc_pidoriginatorinfo(int pid, int flavor, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) proc_listcoalitions(int flavor, int coaltype, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_can_use_foreground_hw(int pid, user_addr_t reason, uint32_t resonsize, int32_t *retval);

/* protos for procpidinfo calls */
static int __attribute__ ((noinline)) proc_pidfdlist(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_pidbsdinfo(proc_t p, struct proc_bsdinfo *pbsd, int zombie);
static int __attribute__ ((noinline)) proc_pidshortbsdinfo(proc_t p, struct proc_bsdshortinfo *pbsd_shortp, int zombie);
static int __attribute__ ((noinline)) proc_pidtaskinfo(proc_t p, struct proc_taskinfo *ptinfo);
static int __attribute__ ((noinline)) proc_pidthreadinfo(proc_t p, uint64_t arg, bool thuniqueid, struct proc_threadinfo *pthinfo);
static int __attribute__ ((noinline)) proc_pidthreadpathinfo(proc_t p, uint64_t arg, struct proc_threadwithpathinfo *pinfo);
static int __attribute__ ((noinline)) proc_pidthreadschedinfo(proc_t p, uint64_t arg, struct proc_threadschedinfo *schedinfo);
static int __attribute__ ((noinline)) proc_pidlistthreads(proc_t p, bool thuniqueid, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_pidregioninfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_pidregionpathinfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_pidregionpathinfo2(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_pidregionpathinfo3(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_pidvnodepathinfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_pidpathinfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_pidworkqueueinfo(proc_t p, struct proc_workqueueinfo *pwqinfo);
static int __attribute__ ((noinline)) proc_pidfileportlist(proc_t p, user_addr_t buffer, size_t buffersize, int32_t *retval);
extern void __attribute__ ((noinline)) proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo);
static void __attribute__ ((noinline)) proc_archinfo(proc_t p, struct proc_archinfo *pai);
static void __attribute__ ((noinline)) proc_pidcoalitioninfo(proc_t p, struct proc_pidcoalitioninfo *pci);
static int __attribute__ ((noinline)) proc_pidnoteexit(proc_t p, uint64_t arg, uint32_t *data);
static int __attribute__ ((noinline)) proc_pidexitreasoninfo(proc_t p, struct proc_exitreasoninfo *peri, struct proc_exitreasonbasicinfo *pberi);
static int __attribute__ ((noinline)) proc_pidoriginatorpid_uuid(uuid_t uuid, uint32_t buffersize, pid_t *pid);
static int __attribute__ ((noinline)) proc_pidlistuptrs(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_piddynkqueueinfo(pid_t pid, int flavor, kqueue_id_t id, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_pidregionpath(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval);
static int __attribute__ ((noinline)) proc_pidipctableinfo(proc_t p, struct proc_ipctableinfo *table_info);

#if CONFIG_PROC_UDATA_STORAGE
int __attribute__ ((noinline)) proc_udata_info(pid_t pid, int flavor, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
#endif

/* protos for proc_pidfdinfo calls */
static int __attribute__ ((noinline)) pid_vnodeinfo(vnode_t vp, struct fileproc * fp, proc_t proc, user_addr_t  buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) pid_vnodeinfopath(vnode_t vp, struct fileproc * fp, proc_t proc, user_addr_t  buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) pid_socketinfo(socket_t so, struct fileproc *fp, proc_t proc, user_addr_t  buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) pid_pseminfo(struct psemnode * psem, struct fileproc * fp, proc_t proc, user_addr_t  buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) pid_pshminfo(struct pshmnode * pshm, struct fileproc * fp, proc_t proc, user_addr_t  buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) pid_pipeinfo(struct pipe * p, struct fileproc * fp, proc_t proc, user_addr_t  buffer, uint32_t buffersize, int32_t * retval);
static int __attribute__ ((noinline)) pid_kqueueinfo(struct kqueue * kq, struct fileproc * fp, proc_t proc, user_addr_t  buffer, uint32_t buffersize, int32_t * retval);


/* protos for misc */

static int fill_vnodeinfo(vnode_t vp, struct vnode_info *vinfo, boolean_t check_fsgetpath);
static void fill_fileinfo(struct fileproc *fp, proc_t proc, struct proc_fileinfo * finfo);
static int proc_security_policy(proc_t targetp, int callnum, int flavor, boolean_t check_same_user);
static void munge_vinfo_stat(struct stat64 *sbp, struct vinfo_stat *vsbp);
static int proc_piduuidinfo(pid_t pid, uuid_t uuid_buf, uint32_t buffersize);

extern int proc_pidpathinfo_internal(proc_t p, __unused uint64_t arg, char *buf, uint32_t buffersize, __unused int32_t *retval);
extern int cansignal(struct proc *, kauth_cred_t, struct proc *, int);
extern int proc_get_rusage(proc_t proc, int flavor, user_addr_t buffer, int is_zombie);

#define CHECK_SAME_USER         TRUE
#define NO_CHECK_SAME_USER      FALSE

uint64_t
get_dispatchqueue_offset_from_proc(void *p)
{
	if (p != NULL) {
		proc_t pself = (proc_t)p;
		return pself->p_dispatchqueue_offset;
	} else {
		return (uint64_t)0;
	}
}

uint64_t
get_wq_quantum_offset_from_proc(void *p)
{
	if (p != NULL) {
		proc_t pself = (proc_t)p;
		return pself->p_pthread_wq_quantum_offset;
	} else {
		return (uint64_t)0;
	}
}

uint64_t
get_dispatchqueue_serialno_offset_from_proc(void *p)
{
	if (p != NULL) {
		proc_t pself = (proc_t)p;
		return pself->p_dispatchqueue_serialno_offset;
	} else {
		return (uint64_t)0;
	}
}

uint64_t
get_dispatchqueue_label_offset_from_proc(void *p)
{
	if (p != NULL) {
		proc_t pself = (proc_t)p;
		return pself->p_dispatchqueue_label_offset;
	} else {
		return (uint64_t)0;
	}
}

uint64_t
get_return_to_kernel_offset_from_proc(void *p)
{
	if (p != NULL) {
		proc_t pself = (proc_t)p;
		return pself->p_return_to_kernel_offset;
	} else {
		return (uint64_t)0;
	}
}

/***************************** proc_info ********************/

int
proc_info(__unused struct proc *p, struct proc_info_args * uap, int32_t *retval)
{
	return proc_info_internal(uap->callnum, uap->pid, 0, 0, uap->flavor, uap->arg, uap->buffer, uap->buffersize, retval);
}

int
proc_info_extended_id(__unused struct proc *p, struct proc_info_extended_id_args *uap, int32_t *retval)
{
	uint32_t flags = uap->flags;

	if ((flags & (PIF_COMPARE_IDVERSION | PIF_COMPARE_UNIQUEID)) == (PIF_COMPARE_IDVERSION | PIF_COMPARE_UNIQUEID)) {
		return EINVAL;
	}

	return proc_info_internal(uap->callnum, uap->pid, flags, uap->ext_id, uap->flavor, uap->arg, uap->buffer, uap->buffersize, retval);
}

int
proc_info_internal(int callnum, int pid, uint32_t flags, uint64_t ext_id, int flavor, uint64_t arg, user_addr_t buffer, uint32_t  buffersize, int32_t * retval)
{
	switch (callnum) {
	case PROC_INFO_CALL_LISTPIDS:
		/* pid contains type and flavor contains typeinfo */
		return proc_listpids(pid, flavor, buffer, buffersize, retval);
	case PROC_INFO_CALL_PIDINFO:
		return proc_pidinfo(pid, flags, ext_id, flavor, arg, buffer, buffersize, retval);
	case PROC_INFO_CALL_PIDFDINFO:
		return proc_pidfdinfo(pid, flavor, (int)arg, buffer, buffersize, retval);
	case PROC_INFO_CALL_KERNMSGBUF:
		return proc_kernmsgbuf(buffer, buffersize, retval);
	case PROC_INFO_CALL_SETCONTROL:
		return proc_setcontrol(pid, flavor, arg, buffer, buffersize, retval);
	case PROC_INFO_CALL_PIDFILEPORTINFO:
		return proc_pidfileportinfo(pid, flavor, (mach_port_name_t)arg, buffer, buffersize, retval);
	case PROC_INFO_CALL_TERMINATE:
		return proc_terminate(pid, retval);
	case PROC_INFO_CALL_DIRTYCONTROL:
		return proc_dirtycontrol(pid, flavor, arg, retval);
	case PROC_INFO_CALL_PIDRUSAGE:
		return proc_pid_rusage(pid, flavor, buffer, retval);
	case PROC_INFO_CALL_PIDORIGINATORINFO:
		return proc_pidoriginatorinfo(pid, flavor, buffer, buffersize, retval);
	case PROC_INFO_CALL_LISTCOALITIONS:
		return proc_listcoalitions(pid /* flavor */, flavor /* coaltype */, buffer,
		           buffersize, retval);
	case PROC_INFO_CALL_CANUSEFGHW:
		return proc_can_use_foreground_hw(pid, buffer, buffersize, retval);
	case PROC_INFO_CALL_PIDDYNKQUEUEINFO:
		return proc_piddynkqueueinfo(pid, flavor, (kqueue_id_t)arg, buffer, buffersize, retval);
#if CONFIG_PROC_UDATA_STORAGE
	case PROC_INFO_CALL_UDATA_INFO:
		return proc_udata_info(pid, flavor, buffer, buffersize, retval);
#endif /* CONFIG_PROC_UDATA_STORAGE */
	default:
		return EINVAL;
	}

	return EINVAL;
}

/******************* proc_listpids routine ****************/
int
proc_listpids(uint32_t type, uint32_t typeinfo, user_addr_t buffer, uint32_t  buffersize, int32_t * retval)
{
	uint32_t numprocs = 0;
	uint32_t wantpids;
	int *kbuf;
	int *ptr;
	uint32_t n;
	int skip;
	struct proc * p;
	int error = 0;
	struct proclist *current_list;

	/* Do we have permission to look into this? */
	if ((error = proc_security_policy(PROC_NULL, PROC_INFO_CALL_LISTPIDS, type, NO_CHECK_SAME_USER))) {
		return error;
	}

	/* if the buffer is null, return num of procs */
	if (buffer == (user_addr_t)0) {
		*retval = ((nprocs + 20) * sizeof(int));
		return 0;
	}

	if (buffersize < sizeof(int)) {
		return ENOMEM;
	}
	wantpids = buffersize / sizeof(int);
	if ((nprocs + 20) > 0) {
		numprocs = (uint32_t)(nprocs + 20);
	}
	if (numprocs > wantpids) {
		numprocs = wantpids;
	}

	kbuf = (int *)kalloc_data(numprocs * sizeof(int), Z_WAITOK | Z_ZERO);
	if (kbuf == NULL) {
		return ENOMEM;
	}

	proc_list_lock();

	n = 0;
	ptr = kbuf;
	current_list = &allproc;
proc_loop:
	LIST_FOREACH(p, current_list, p_list) {
		skip = 0;
		switch (type) {
		case PROC_PGRP_ONLY:
			if (p->p_pgrpid != (pid_t)typeinfo) {
				skip = 1;
			}
			break;
		case PROC_PPID_ONLY:
			if ((p->p_ppid != (pid_t)typeinfo) && (((p->p_lflag & P_LTRACED) == 0) || (p->p_oppid != (pid_t)typeinfo))) {
				skip = 1;
			}
			break;

		case PROC_ALL_PIDS:
			skip = 0;
			break;
		case PROC_TTY_ONLY:
			if (p->p_flag & P_CONTROLT) {
				struct pgrp *pg = hazard_ptr_serialized_load(&p->p_pgrp);
				skip = pg != PGRP_NULL &&
				    os_atomic_load(&pg->pg_session->s_ttydev, relaxed) != (dev_t)typeinfo;
			} else {
				skip = 1;
			}
			break;
		case PROC_UID_ONLY:
			if (proc_ucred(p) == NULL) {
				skip = 1;
			} else {
				kauth_cred_t my_cred;
				uid_t uid;

				my_cred = kauth_cred_proc_ref(p);
				uid = kauth_cred_getuid(my_cred);
				kauth_cred_unref(&my_cred);
				if (uid != (uid_t)typeinfo) {
					skip = 1;
				}
			}
			break;
		case PROC_RUID_ONLY:
			if (proc_ucred(p) == NULL) {
				skip = 1;
			} else {
				kauth_cred_t my_cred;
				uid_t uid;

				my_cred = kauth_cred_proc_ref(p);
				uid = kauth_cred_getruid(my_cred);
				kauth_cred_unref(&my_cred);
				if (uid != (uid_t)typeinfo) {
					skip = 1;
				}
			}
			break;
		case PROC_KDBG_ONLY:
			if (p->p_kdebug == 0) {
				skip = 1;
			}
			break;
		default:
			skip = 1;
			break;
		}
		;

		if (skip == 0) {
			*ptr++ = proc_getpid(p);
			n++;
		}
		if (n >= numprocs) {
			break;
		}
	}

	if ((n < numprocs) && (current_list == &allproc)) {
		current_list = &zombproc;
		goto proc_loop;
	}

	proc_list_unlock();

	ptr = kbuf;
	error = copyout((caddr_t)ptr, buffer, n * sizeof(int));
	if (error == 0) {
		*retval = (n * sizeof(int));
	}
	kfree_data(kbuf, numprocs * sizeof(int));

	return error;
}


/********************************** proc_pidfdlist routines ********************************/

static size_t
proc_fdlist_internal(proc_t p, struct proc_fdinfo *pfd, size_t numfds)
{
	struct fileproc *fp;
	size_t count = 0;

	proc_fdlock(p);

	fdt_foreach(fp, p) {
		if (count >= numfds) {
			break;
		}
		file_type_t fdtype = FILEGLOB_DTYPE(fp->fp_glob);
		pfd[count].proc_fd = fdt_foreach_fd();
		pfd[count].proc_fdtype = (fdtype != DTYPE_ATALK) ?
		    fdtype : PROX_FDTYPE_ATALK;
		count++;
	}

	proc_fdunlock(p);
	return count;
}

int
proc_pidfdlist(proc_t p, user_addr_t buffer, uint32_t  buffersize, int32_t *retval)
{
	uint32_t numfds = 0;
	uint32_t needfds;
	char * kbuf;
	uint32_t count = 0;
	int error = 0;

	if (p->p_fd.fd_nfiles > 0) {
		numfds = (uint32_t)p->p_fd.fd_nfiles;
	}

	if (buffer == (user_addr_t) 0) {
		numfds += 20;
		*retval = (numfds * sizeof(struct proc_fdinfo));
		return 0;
	}

	/* buffersize is big enough atleast for one struct */
	needfds = buffersize / sizeof(struct proc_fdinfo);

	if (numfds > needfds) {
		numfds = needfds;
	}

	kbuf = (char *)kalloc_data(numfds * sizeof(struct proc_fdinfo), Z_WAITOK | Z_ZERO);
	if (kbuf == NULL) {
		return ENOMEM;
	}

	/* cannot overflow due to count <= numfds */
	count = (uint32_t)proc_fdlist_internal(p, (struct proc_fdinfo *)kbuf, (size_t)numfds);

	error = copyout(kbuf, buffer, count * sizeof(struct proc_fdinfo));
	kfree_data(kbuf, numfds * sizeof(struct proc_fdinfo));
	if (error == 0) {
		*retval = count * sizeof(struct proc_fdinfo);
	}
	return error;
}

/*
 * KPI variant of proc_pidfdlist.
 *
 * Caller is responsible for adding margin to *count when calling this in
 * circumstances where file descriptors can appear/disappear between the
 * two calls to this function.
 */
int
proc_fdlist(proc_t p, struct proc_fdinfo *buf, size_t *count)
{
	if (p == NULL || count == NULL) {
		return EINVAL;
	}

	if (buf == NULL) {
		proc_fdlock(p);
		*count = (size_t)p->p_fd.fd_afterlast;
		proc_fdunlock(p);
		return 0;
	}

	*count = proc_fdlist_internal(p, buf, *count);
	return 0;
}

int
proc_pidfileportlist(proc_t p,
    user_addr_t buffer, size_t buffersize, int32_t *retval)
{
	void *kbuf;
	size_t kbufsize;
	struct proc_fileportinfo *pfi;
	size_t needfileports, numfileports;
	int error;
	kern_return_t kr;

	needfileports = buffersize / sizeof(*pfi);
	if ((user_addr_t)0 == buffer || needfileports > (size_t)maxfilesperproc) {
		/*
		 * Either (i) the user is asking for a fileport count,
		 * or (ii) the number of fileports they're asking for is
		 * larger than the maximum number of open files (!); count
		 * them to bound subsequent heap allocations.
		 */
		numfileports = 0;
		switch (fileport_walk(p->task, &numfileports, NULL)) {
		case KERN_SUCCESS:
			break;
		case KERN_RESOURCE_SHORTAGE:
			return ENOMEM;
		case KERN_INVALID_TASK:
			return ESRCH;
		default:
			return EINVAL;
		}

		if (numfileports == 0) {
			*retval = 0;            /* none at all, bail */
			return 0;
		}
		if ((user_addr_t)0 == buffer) {
			numfileports += 20;     /* accelerate convergence */
			*retval = (int32_t)MIN(numfileports * sizeof(*pfi), INT32_MAX);
			return 0;
		}
		if (needfileports > numfileports) {
			needfileports = numfileports;
		}
	}

	assert(buffersize >= PROC_PIDLISTFILEPORTS_SIZE);

	kbufsize = needfileports * sizeof(*pfi);
	pfi = kbuf = kalloc_data(kbufsize, Z_WAITOK | Z_ZERO);
	if (kbuf == NULL) {
		return ENOMEM;
	}

	kr = fileport_walk(p->task, &numfileports,
	    ^bool (size_t i, mach_port_name_t name, struct fileglob *fg) {
		if (i < needfileports) {
		        file_type_t fdtype = FILEGLOB_DTYPE(fg);

		        pfi[i].proc_fdtype = (fdtype != DTYPE_ATALK) ?
		        fdtype : PROX_FDTYPE_ATALK;
		        pfi[i].proc_fileport = name;
		        return true;
		}
		return false; /* stop walking */
	});
	switch (kr) {
	case KERN_SUCCESS:
		if (numfileports) {
			if (numfileports > needfileports) {
				numfileports = needfileports;
			}
			error = copyout(kbuf, buffer, numfileports * sizeof(*pfi));
		} else {
			error = 0;
		}
		break;
	case KERN_RESOURCE_SHORTAGE:
		error = ENOMEM;
		break;
	case KERN_INVALID_TASK:
		error = ESRCH;
		break;
	default:
		error = EINVAL;
		break;
	}

	kfree_data(kbuf, kbufsize);
	if (error == 0) {
		*retval = (int32_t)MIN(numfileports * sizeof(*pfi), INT32_MAX);
	}
	return error;
}

int
proc_pidbsdinfo(proc_t p, struct proc_bsdinfo * pbsd, int zombie)
{
	struct pgrp *pg;
	kauth_cred_t my_cred;

	pg = proc_pgrp(p, NULL);

	my_cred = kauth_cred_proc_ref(p);
	bzero(pbsd, sizeof(struct proc_bsdinfo));
	pbsd->pbi_status = p->p_stat;
	pbsd->pbi_xstatus = p->p_xstat;
	pbsd->pbi_pid = proc_getpid(p);
	pbsd->pbi_ppid = p->p_ppid;
	pbsd->pbi_uid = kauth_cred_getuid(my_cred);
	pbsd->pbi_gid = kauth_cred_getgid(my_cred);
	pbsd->pbi_ruid =  kauth_cred_getruid(my_cred);
	pbsd->pbi_rgid = kauth_cred_getrgid(my_cred);
	pbsd->pbi_svuid =  kauth_cred_getsvuid(my_cred);
	pbsd->pbi_svgid = kauth_cred_getsvgid(my_cred);
	kauth_cred_unref(&my_cred);

	pbsd->pbi_nice = p->p_nice;
	pbsd->pbi_start_tvsec = p->p_start.tv_sec;
	pbsd->pbi_start_tvusec = p->p_start.tv_usec;
	bcopy(&p->p_comm, &pbsd->pbi_comm[0], MAXCOMLEN);
	pbsd->pbi_comm[MAXCOMLEN - 1] = '\0';
	bcopy(&p->p_name, &pbsd->pbi_name[0], 2 * MAXCOMLEN);
	pbsd->pbi_name[(2 * MAXCOMLEN) - 1] = '\0';

	pbsd->pbi_flags = 0;
	if ((p->p_flag & P_SYSTEM) == P_SYSTEM) {
		pbsd->pbi_flags |= PROC_FLAG_SYSTEM;
	}
	if ((p->p_lflag & P_LTRACED) == P_LTRACED) {
		pbsd->pbi_flags |= PROC_FLAG_TRACED;
	}
	if ((p->p_lflag & P_LEXIT) == P_LEXIT) {
		pbsd->pbi_flags |= PROC_FLAG_INEXIT;
	}
	if ((p->p_lflag & P_LPPWAIT) == P_LPPWAIT) {
		pbsd->pbi_flags |= PROC_FLAG_PPWAIT;
	}
	if ((p->p_flag & P_LP64) == P_LP64) {
		pbsd->pbi_flags |= PROC_FLAG_LP64;
	}
	if ((p->p_flag & P_CONTROLT) == P_CONTROLT) {
		pbsd->pbi_flags |= PROC_FLAG_CONTROLT;
	}
	if ((p->p_flag & P_THCWD) == P_THCWD) {
		pbsd->pbi_flags |= PROC_FLAG_THCWD;
	}
	if ((p->p_flag & P_SUGID) == P_SUGID) {
		pbsd->pbi_flags |= PROC_FLAG_PSUGID;
	}
	if ((p->p_flag & P_EXEC) == P_EXEC) {
		pbsd->pbi_flags |= PROC_FLAG_EXEC;
	}
	if ((p->p_flag & P_TRANSLATED) == P_TRANSLATED) {
		pbsd->pbi_flags |= PROC_FLAG_ROSETTA;
	}

	if (pg != PGRP_NULL) {
		if (SESS_LEADER(p, pg->pg_session)) {
			pbsd->pbi_flags |= PROC_FLAG_SLEADER;
		}
		if (pg->pg_session->s_ttyvp) {
			pbsd->pbi_flags |= PROC_FLAG_CTTY;
		}
	}

#if CONFIG_DELAY_IDLE_SLEEP
	if ((p->p_flag & P_DELAYIDLESLEEP) == P_DELAYIDLESLEEP) {
		pbsd->pbi_flags |= PROC_FLAG_DELAYIDLESLEEP;
	}
#endif /* CONFIG_DELAY_IDLE_SLEEP */

	switch (PROC_CONTROL_STATE(p)) {
	case P_PCTHROTTLE:
		pbsd->pbi_flags |= PROC_FLAG_PC_THROTTLE;
		break;
	case P_PCSUSP:
		pbsd->pbi_flags |= PROC_FLAG_PC_SUSP;
		break;
	case P_PCKILL:
		pbsd->pbi_flags |= PROC_FLAG_PC_KILL;
		break;
	}
	;

	switch (PROC_ACTION_STATE(p)) {
	case P_PCTHROTTLE:
		pbsd->pbi_flags |= PROC_FLAG_PA_THROTTLE;
		break;
	case P_PCSUSP:
		pbsd->pbi_flags |= PROC_FLAG_PA_SUSP;
		break;
	}
	;

	/* if process is a zombie skip bg state */
	if ((zombie == 0) && (p->p_stat != SZOMB) && (p->task != TASK_NULL)) {
		proc_get_darwinbgstate(p->task, &pbsd->pbi_flags);
	}

	if (zombie == 0) {
		pbsd->pbi_nfiles = p->p_fd.fd_nfiles;
	}

	pbsd->e_tdev = NODEV;
	if (pg != PGRP_NULL) {
		pbsd->pbi_pgid = p->p_pgrpid;
		pbsd->pbi_pjobc = pg->pg_jobc;
		if (p->p_flag & P_CONTROLT) {
			struct session *sessp = pg->pg_session;

			session_lock(sessp);
			pbsd->e_tdev  = os_atomic_load(&sessp->s_ttydev, relaxed);
			pbsd->e_tpgid = sessp->s_ttypgrpid;
			session_unlock(sessp);
		}
		pgrp_rele(pg);
	}

	return 0;
}


int
proc_pidshortbsdinfo(proc_t p, struct proc_bsdshortinfo * pbsd_shortp, int zombie)
{
	bzero(pbsd_shortp, sizeof(struct proc_bsdshortinfo));
	pbsd_shortp->pbsi_pid = proc_getpid(p);
	pbsd_shortp->pbsi_ppid = p->p_ppid;
	pbsd_shortp->pbsi_pgid = p->p_pgrpid;
	pbsd_shortp->pbsi_status = p->p_stat;
	bcopy(&p->p_comm, &pbsd_shortp->pbsi_comm[0], MAXCOMLEN);
	pbsd_shortp->pbsi_comm[MAXCOMLEN - 1] = '\0';

	pbsd_shortp->pbsi_flags = 0;
	if ((p->p_flag & P_SYSTEM) == P_SYSTEM) {
		pbsd_shortp->pbsi_flags |= PROC_FLAG_SYSTEM;
	}
	if ((p->p_lflag & P_LTRACED) == P_LTRACED) {
		pbsd_shortp->pbsi_flags |= PROC_FLAG_TRACED;
	}
	if ((p->p_lflag & P_LEXIT) == P_LEXIT) {
		pbsd_shortp->pbsi_flags |= PROC_FLAG_INEXIT;
	}
	if ((p->p_lflag & P_LPPWAIT) == P_LPPWAIT) {
		pbsd_shortp->pbsi_flags |= PROC_FLAG_PPWAIT;
	}
	if ((p->p_flag & P_LP64) == P_LP64) {
		pbsd_shortp->pbsi_flags |= PROC_FLAG_LP64;
	}
	if ((p->p_flag & P_CONTROLT) == P_CONTROLT) {
		pbsd_shortp->pbsi_flags |= PROC_FLAG_CONTROLT;
	}
	if ((p->p_flag & P_THCWD) == P_THCWD) {
		pbsd_shortp->pbsi_flags |= PROC_FLAG_THCWD;
	}
	if ((p->p_flag & P_SUGID) == P_SUGID) {
		pbsd_shortp->pbsi_flags |= PROC_FLAG_PSUGID;
	}
	if ((p->p_flag & P_EXEC) == P_EXEC) {
		pbsd_shortp->pbsi_flags |= PROC_FLAG_EXEC;
	}
	if ((p->p_flag & P_TRANSLATED) == P_TRANSLATED) {
		pbsd_shortp->pbsi_flags |= PROC_FLAG_ROSETTA;
	}
#if CONFIG_DELAY_IDLE_SLEEP
	if ((p->p_flag & P_DELAYIDLESLEEP) == P_DELAYIDLESLEEP) {
		pbsd_shortp->pbsi_flags |= PROC_FLAG_DELAYIDLESLEEP;
	}
#endif /* CONFIG_DELAY_IDLE_SLEEP */

	switch (PROC_CONTROL_STATE(p)) {
	case P_PCTHROTTLE:
		pbsd_shortp->pbsi_flags |= PROC_FLAG_PC_THROTTLE;
		break;
	case P_PCSUSP:
		pbsd_shortp->pbsi_flags |= PROC_FLAG_PC_SUSP;
		break;
	case P_PCKILL:
		pbsd_shortp->pbsi_flags |= PROC_FLAG_PC_KILL;
		break;
	}
	;

	switch (PROC_ACTION_STATE(p)) {
	case P_PCTHROTTLE:
		pbsd_shortp->pbsi_flags |= PROC_FLAG_PA_THROTTLE;
		break;
	case P_PCSUSP:
		pbsd_shortp->pbsi_flags |= PROC_FLAG_PA_SUSP;
		break;
	}
	;

	/* if process is a zombie skip bg state */
	if ((zombie == 0) && (p->p_stat != SZOMB) && (p->task != TASK_NULL)) {
		proc_get_darwinbgstate(p->task, &pbsd_shortp->pbsi_flags);
	}

	pbsd_shortp->pbsi_uid = p->p_uid;
	pbsd_shortp->pbsi_gid = p->p_gid;
	pbsd_shortp->pbsi_ruid =  p->p_ruid;
	pbsd_shortp->pbsi_rgid = p->p_rgid;
	pbsd_shortp->pbsi_svuid =  p->p_svuid;
	pbsd_shortp->pbsi_svgid = p->p_svgid;

	return 0;
}

int
proc_pidtaskinfo(proc_t p, struct proc_taskinfo * ptinfo)
{
	task_t task;

	task = p->task;

	bzero(ptinfo, sizeof(struct proc_taskinfo));
	fill_taskprocinfo(task, (struct proc_taskinfo_internal *)ptinfo);

	return 0;
}



int
proc_pidthreadinfo(proc_t p, uint64_t arg, bool thuniqueid, struct proc_threadinfo *pthinfo)
{
	int error = 0;
	uint64_t threadaddr = (uint64_t)arg;

	bzero(pthinfo, sizeof(struct proc_threadinfo));

	error = fill_taskthreadinfo(p->task, threadaddr, thuniqueid, (struct proc_threadinfo_internal *)pthinfo, NULL, NULL);
	if (error) {
		return ESRCH;
	} else {
		return 0;
	}
}

boolean_t
bsd_hasthreadname(void *uth)
{
	struct uthread *ut = (struct uthread*)uth;

	/* This doesn't check for the empty string; do we care? */
	if (ut->pth_name) {
		return TRUE;
	} else {
		return FALSE;
	}
}

void
bsd_getthreadname(void *uth, char *buffer)
{
	struct uthread *ut = (struct uthread *)uth;
	if (ut->pth_name) {
		bcopy(ut->pth_name, buffer, MAXTHREADNAMESIZE);
	} else {
		*buffer = '\0';
	}
}

/*
 * This is known to race with regards to the contents of the thread name; concurrent
 * callers may result in a garbled name.
 */
void
bsd_setthreadname(void *uth, const char *name)
{
	struct uthread *ut = (struct uthread *)uth;
	char * name_buf = NULL;

	if (!ut->pth_name) {
		/* If there is no existing thread name, allocate a buffer for one. */
		name_buf = kalloc_data(MAXTHREADNAMESIZE,
		    Z_WAITOK | Z_ZERO | Z_NOFAIL);

		/* Someone could conceivably have named the thread at the same time we did. */
		if (!OSCompareAndSwapPtr(NULL, name_buf, &ut->pth_name)) {
			kfree_data(name_buf, MAXTHREADNAMESIZE);
		}
	} else {
		kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV, ut->pth_name);
	}

	strncpy(ut->pth_name, name, MAXTHREADNAMESIZE - 1);
	kernel_debug_string_simple(TRACE_STRING_THREADNAME, ut->pth_name);
}

void
bsd_copythreadname(void *dst_uth, void *src_uth)
{
	struct uthread *dst_ut = (struct uthread *)dst_uth;
	struct uthread *src_ut = (struct uthread *)src_uth;

	if (src_ut->pth_name == NULL) {
		return;
	}

	if (dst_ut->pth_name == NULL) {
		dst_ut->pth_name = (char *)kalloc_data(MAXTHREADNAMESIZE, Z_WAITOK);
		if (dst_ut->pth_name == NULL) {
			return;
		}
	}

	bcopy(src_ut->pth_name, dst_ut->pth_name, MAXTHREADNAMESIZE);
	return;
}

void
bsd_threadcdir(void * uth, void *vptr, int *vidp)
{
	struct uthread * ut = (struct uthread *)uth;
	vnode_t vp;
	vnode_t *vpp = (vnode_t *)vptr;

	vp = ut->uu_cdir;
	if (vp != NULLVP) {
		if (vpp != NULL) {
			*vpp = vp;
			if (vidp != NULL) {
				*vidp = vp->v_id;
			}
		}
	}
}


int
proc_pidthreadpathinfo(proc_t p, uint64_t arg, struct proc_threadwithpathinfo *pinfo)
{
	vnode_t vp = NULLVP;
	int vid;
	int error = 0;
	uint64_t threadaddr = (uint64_t)arg;
	int count;

	bzero(pinfo, sizeof(struct proc_threadwithpathinfo));

	error = fill_taskthreadinfo(p->task, threadaddr, 0, (struct proc_threadinfo_internal *)&pinfo->pt, (void *)&vp, &vid);
	if (error) {
		return ESRCH;
	}

	if ((vp != NULLVP) && ((vnode_getwithvid(vp, vid)) == 0)) {
		error = fill_vnodeinfo(vp, &pinfo->pvip.vip_vi, FALSE);
		if (error == 0) {
			count = MAXPATHLEN;
			vn_getpath(vp, &pinfo->pvip.vip_path[0], &count);
			pinfo->pvip.vip_path[MAXPATHLEN - 1] = 0;
		}
		vnode_put(vp);
	}
	return error;
}



int
proc_pidlistthreads(proc_t p, bool thuniqueid, user_addr_t buffer, uint32_t  buffersize, int32_t *retval)
{
	uint32_t count = 0;
	int ret = 0;
	int error = 0;
	void * kbuf;
	uint32_t numthreads = 0;

	int num = get_numthreads(p->task) + 10;
	if (num > 0) {
		numthreads = (uint32_t)num;
	}

	count = buffersize / (sizeof(uint64_t));

	if (numthreads > count) {
		numthreads = count;
	}

	kbuf = kalloc_data(numthreads * sizeof(uint64_t), Z_WAITOK | Z_ZERO);
	if (kbuf == NULL) {
		return ENOMEM;
	}

	ret = fill_taskthreadlist(p->task, kbuf, numthreads, thuniqueid);

	error = copyout(kbuf, buffer, ret);
	kfree_data(kbuf, numthreads * sizeof(uint64_t));
	if (error == 0) {
		*retval = ret;
	}
	return error;
}


int
proc_pidregioninfo(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t  buffersize, int32_t *retval)
{
	struct proc_regioninfo preginfo;
	int ret, error = 0;

	bzero(&preginfo, sizeof(struct proc_regioninfo));
	ret = fill_procregioninfo( p->task, arg, (struct proc_regioninfo_internal *)&preginfo, (uintptr_t *)0, (uint32_t *)0);
	if (ret == 0) {
		return EINVAL;
	}
	error = copyout(&preginfo, buffer, sizeof(struct proc_regioninfo));
	if (error == 0) {
		*retval = sizeof(struct proc_regioninfo);
	}
	return error;
}


int
proc_pidregionpathinfo(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t  buffersize, int32_t *retval)
{
	struct proc_regionwithpathinfo preginfo;
	int ret, error = 0;
	uintptr_t vnodeaddr = 0;
	uint32_t vnodeid = 0;
	vnode_t vp;
	int count;

	bzero(&preginfo, sizeof(struct proc_regionwithpathinfo));

	ret = fill_procregioninfo( p->task, arg, (struct proc_regioninfo_internal *)&preginfo.prp_prinfo, (uintptr_t *)&vnodeaddr, (uint32_t *)&vnodeid);
	if (ret == 0) {
		return EINVAL;
	}
	if (vnodeaddr) {
		vp = (vnode_t)vnodeaddr;
		if ((vnode_getwithvid(vp, vnodeid)) == 0) {
			/* FILL THE VNODEINFO */
			error = fill_vnodeinfo(vp, &preginfo.prp_vip.vip_vi, FALSE);
			count = MAXPATHLEN;
			vn_getpath(vp, &preginfo.prp_vip.vip_path[0], &count);
			/* Always make sure it is null terminated */
			preginfo.prp_vip.vip_path[MAXPATHLEN - 1] = 0;
			vnode_put(vp);
		}
	}
	error = copyout(&preginfo, buffer, sizeof(struct proc_regionwithpathinfo));
	if (error == 0) {
		*retval = sizeof(struct proc_regionwithpathinfo);
	}
	return error;
}

int
proc_pidregionpathinfo2(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
{
	struct proc_regionwithpathinfo preginfo;
	int ret, error = 0;
	uintptr_t vnodeaddr = 0;
	uint32_t vnodeid = 0;
	vnode_t vp;
	int count;

	bzero(&preginfo, sizeof(struct proc_regionwithpathinfo));

	ret = fill_procregioninfo_onlymappedvnodes( p->task, arg, (struct proc_regioninfo_internal *)&preginfo.prp_prinfo, (uintptr_t *)&vnodeaddr, (uint32_t *)&vnodeid);
	if (ret == 0) {
		return EINVAL;
	}
	if (!vnodeaddr) {
		return EINVAL;
	}

	vp = (vnode_t)vnodeaddr;
	if ((vnode_getwithvid(vp, vnodeid)) == 0) {
		/* FILL THE VNODEINFO */
		error = fill_vnodeinfo(vp, &preginfo.prp_vip.vip_vi, FALSE);
		count = MAXPATHLEN;
		vn_getpath(vp, &preginfo.prp_vip.vip_path[0], &count);
		/* Always make sure it is null terminated */
		preginfo.prp_vip.vip_path[MAXPATHLEN - 1] = 0;
		vnode_put(vp);
	} else {
		return EINVAL;
	}

	error = copyout(&preginfo, buffer, sizeof(struct proc_regionwithpathinfo));
	if (error == 0) {
		*retval = sizeof(struct proc_regionwithpathinfo);
	}
	return error;
}

int
proc_pidregionpath(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
{
	struct proc_regionpath path = {};
	int ret, error = 0;
	uintptr_t vnodeaddr = 0;
	uint32_t vnodeid = 0;
	vnode_t vp;

	ret = find_region_details(p->task, (vm_map_offset_t) arg,
	    (uintptr_t *)&vnodeaddr, (uint32_t *)&vnodeid,
	    &path.prpo_addr, &path.prpo_regionlength);
	if (ret == 0) {
		return EINVAL;
	}
	if (!vnodeaddr) {
		return EINVAL;
	}

	vp = (vnode_t)vnodeaddr;
	if ((vnode_getwithvid(vp, vnodeid)) == 0) {
		int count = MAXPATHLEN;
		vn_getpath(vp, &path.prpo_path[0], &count);
		/* Always make sure it is null terminated */
		path.prpo_path[MAXPATHLEN - 1] = 0;
		vnode_put(vp);
	} else {
		return EINVAL;
	}

	error = copyout(&path, buffer, sizeof(struct proc_regionpath));
	if (error == 0) {
		*retval = sizeof(struct proc_regionpath);
	}
	return error;
}

int
proc_pidregionpathinfo3(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
{
	struct proc_regionwithpathinfo preginfo;
	int ret, error = 0;
	uintptr_t vnodeaddr;
	uint32_t vnodeid;
	vnode_t vp;
	int count;
	uint64_t addr = 0;

	/* Loop while looking for vnodes that match dev_t filter */
	do {
		bzero(&preginfo, sizeof(struct proc_regionwithpathinfo));
		vnodeaddr = 0;
		vnodeid = 0;

		ret = fill_procregioninfo_onlymappedvnodes( p->task, addr, (struct proc_regioninfo_internal *)&preginfo.prp_prinfo, (uintptr_t *)&vnodeaddr, (uint32_t *)&vnodeid);
		if (ret == 0) {
			return EINVAL;
		}
		if (!vnodeaddr) {
			return EINVAL;
		}

		vp = (vnode_t)vnodeaddr;
		if ((vnode_getwithvid(vp, vnodeid)) == 0) {
			/* Check if the vnode matches the filter, otherwise loop looking for the next memory region backed by a vnode */
			struct vnode_attr va;

			memset(&va, 0, sizeof(va));
			VATTR_INIT(&va);
			VATTR_WANTED(&va, va_fsid);
			VATTR_WANTED(&va, va_fsid64);

			ret = vnode_getattr(vp, &va, vfs_context_current());
			if (ret) {
				vnode_put(vp);
				return EINVAL;
			}

			if (vnode_get_va_fsid(&va) == arg) {
				/* FILL THE VNODEINFO */
				error = fill_vnodeinfo(vp, &preginfo.prp_vip.vip_vi, FALSE);
				count = MAXPATHLEN;
				vn_getpath(vp, &preginfo.prp_vip.vip_path[0], &count);
				/* Always make sure it is null terminated */
				preginfo.prp_vip.vip_path[MAXPATHLEN - 1] = 0;
				vnode_put(vp);
				break;
			}
			vnode_put(vp);
		} else {
			return EINVAL;
		}

		addr = preginfo.prp_prinfo.pri_address + preginfo.prp_prinfo.pri_size;
	} while (1);

	error = copyout(&preginfo, buffer, sizeof(struct proc_regionwithpathinfo));
	if (error == 0) {
		*retval = sizeof(struct proc_regionwithpathinfo);
	}
	return error;
}

/*
 * Path is relative to current process directory; may different from current
 * thread directory.
 */
int
proc_pidvnodepathinfo(proc_t p, __unused uint64_t arg, user_addr_t buffer, __unused uint32_t  buffersize, int32_t *retval)
{
	struct proc_vnodepathinfo pvninfo;
	int error = 0;
	vnode_t vncdirvp = NULLVP;
	uint32_t vncdirid = 0;
	vnode_t vnrdirvp = NULLVP;
	uint32_t vnrdirid = 0;
	int count;

	bzero(&pvninfo, sizeof(struct proc_vnodepathinfo));

	proc_fdlock(p);
	if (p->p_fd.fd_cdir) {
		vncdirvp = p->p_fd.fd_cdir;
		vncdirid = p->p_fd.fd_cdir->v_id;
	}
	if (p->p_fd.fd_rdir) {
		vnrdirvp = p->p_fd.fd_rdir;
		vnrdirid = p->p_fd.fd_rdir->v_id;
	}
	proc_fdunlock(p);

	if (vncdirvp != NULLVP) {
		if ((error = vnode_getwithvid(vncdirvp, vncdirid)) == 0) {
			/* FILL THE VNODEINFO */
			error = fill_vnodeinfo(vncdirvp, &pvninfo.pvi_cdir.vip_vi, TRUE);
			if (error == 0) {
				count = MAXPATHLEN;
				vn_getpath(vncdirvp, &pvninfo.pvi_cdir.vip_path[0], &count);
				pvninfo.pvi_cdir.vip_path[MAXPATHLEN - 1] = 0;
			}
			vnode_put(vncdirvp);
		} else {
			goto out;
		}
	}

	if ((error == 0) && (vnrdirvp != NULLVP)) {
		if ((error = vnode_getwithvid(vnrdirvp, vnrdirid)) == 0) {
			/* FILL THE VNODEINFO */
			error = fill_vnodeinfo(vnrdirvp, &pvninfo.pvi_rdir.vip_vi, TRUE);
			if (error == 0) {
				count = MAXPATHLEN;
				vn_getpath(vnrdirvp, &pvninfo.pvi_rdir.vip_path[0], &count);
				pvninfo.pvi_rdir.vip_path[MAXPATHLEN - 1] = 0;
			}
			vnode_put(vnrdirvp);
		} else {
			goto out;
		}
	}
	if (error == 0) {
		error = copyout(&pvninfo, buffer, sizeof(struct proc_vnodepathinfo));
		if (error == 0) {
			*retval = sizeof(struct proc_vnodepathinfo);
		}
	}
out:
	return error;
}

int
proc_pidpathinfo(proc_t p, __unused uint64_t arg, user_addr_t buffer, uint32_t buffersize, __unused int32_t *retval)
{
	int error;
	vnode_t tvp;
	int len = buffersize;
	char * buf;

	tvp = p->p_textvp;

	if (tvp == NULLVP) {
		return ESRCH;
	}

	buf = (char *)kalloc_data(buffersize, Z_WAITOK | Z_ZERO);
	if (buf == NULL) {
		return ENOMEM;
	}

	error = proc_pidpathinfo_internal(p, arg, buf, buffersize, retval);
	if (error == 0) {
		error = copyout(buf, buffer, len);
	}
	kfree_data(buf, buffersize);
	return error;
}

int
proc_pidpathinfo_internal(proc_t p, __unused uint64_t arg, char *buf, uint32_t buffersize, __unused int32_t *retval)
{
	vnode_t tvp;
	int vid, error;
	int len = buffersize;

	tvp = p->p_textvp;

	if (tvp == NULLVP) {
		return ESRCH;
	}

	vid = vnode_vid(tvp);
	error = vnode_getwithvid(tvp, vid);
	if (error == 0) {
		error = vn_getpath_fsenter(tvp, buf, &len);
		if (!error) {
			error = vnode_ref_ext(tvp, O_EVTONLY, 0);
		}
		vnode_put(tvp);
		if (error == 0) {
			vnode_t nvp = NULLVP;

			error = vnode_lookup(buf, 0, &nvp, vfs_context_current());
			if (error == 0) {
				vnode_put(nvp);
				nvp = NULLVP;
			} else if (vnode_isrecycled(tvp)) {
				error = ESRCH;
			} else {
				if (vnode_getwithvid(tvp, vid) == 0) {
					mount_t mp = vnode_mount(tvp);

					if (vfs_isunmount(mp)) {
						error = ESRCH;
					}
					vnode_put(tvp);
				} else {
					error = ESRCH;
				}
				if (error == EACCES) {
					vfs_context_t ctx = vfs_context_current();
#if DEVLOPMENT || DEBUG
					printf("%s : EACCES returned vnode_lookup for path %s for uid %d\n", __FUNCTION__, buf, (int)kauth_cred_getuid(ctx->vc_ucred));
#else
					printf("%s : EACCES returned by vnode_lookup for uid %d\n", __FUNCTION__, (int)kauth_cred_getuid(ctx->vc_ucred));
#endif

					nvp = NULLVP;
					error = vnode_lookup(buf, 0, &nvp, vfs_context_kernel());

					if (error == 0) {
						vnode_put(nvp);
						nvp = NULLVP;
					} else if (error == EACCES) {
#if DEVLOPMENT || DEBUG
						printf("%s : EACCES returned vnode_lookup for path %s for uid 0\n", __FUNCTION__, buf);
#else
						printf("%s : EACCES returned by vnode_lookup for uid 0\n", __FUNCTION__);
#endif
						/* This should be a panic for a local FS */
						error = ENODEV;
					}
				}
			}
			vnode_rele_ext(tvp, O_EVTONLY, 0);
		} else {
			error = ESRCH;
		}
	} else {
		error = ESRCH;
	}
	return error;
}


int
proc_pidworkqueueinfo(proc_t p, struct proc_workqueueinfo *pwqinfo)
{
	int error = 0;

	bzero(pwqinfo, sizeof(struct proc_workqueueinfo));

	error = fill_procworkqueue(p, pwqinfo);
	if (error) {
		return ESRCH;
	} else {
		return 0;
	}
}


void
proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo)
{
	p_uniqidinfo->p_uniqueid = proc_uniqueid(p);
	proc_getexecutableuuid(p, (unsigned char *)&p_uniqidinfo->p_uuid, sizeof(p_uniqidinfo->p_uuid));
	p_uniqidinfo->p_puniqueid = proc_puniqueid(p);
	p_uniqidinfo->p_idversion = proc_pidversion(p);
	p_uniqidinfo->p_reserve2 = 0;
	p_uniqidinfo->p_reserve3 = 0;
	p_uniqidinfo->p_reserve4 = 0;
}


static int
proc_piduuidinfo(pid_t pid, uuid_t uuid_buf, uint32_t buffersize)
{
	struct proc * p = PROC_NULL;
	int zombref = 0;

	if (buffersize < sizeof(uuid_t)) {
		return EINVAL;
	}

	if ((p = proc_find(pid)) == PROC_NULL) {
		p = proc_find_zombref(pid);
		zombref = 1;
	}
	if (p == PROC_NULL) {
		return ESRCH;
	}

	proc_getexecutableuuid(p, (unsigned char *)uuid_buf, buffersize);

	if (zombref) {
		proc_drop_zombref(p);
	} else {
		proc_rele(p);
	}

	return 0;
}

/*
 * Function to get the uuid and pid of the originator of the voucher.
 */
int
proc_pidoriginatorpid_uuid(uuid_t uuid, uint32_t buffersize, pid_t *pid)
{
	pid_t originator_pid;
	kern_return_t kr;
	int error;

	/*
	 * Get the current voucher origin pid. The pid returned here
	 * might not be valid or may have been recycled.
	 */
	kr = thread_get_current_voucher_origin_pid(&originator_pid);
	/* If errors, convert errors to appropriate format */
	if (kr) {
		if (kr == KERN_INVALID_TASK) {
			error = ESRCH;
		} else if (kr == KERN_INVALID_VALUE) {
			error = ENOATTR;
		} else {
			error = EINVAL;
		}
		return error;
	}

	*pid = originator_pid;
	error = proc_piduuidinfo(originator_pid, uuid, buffersize);
	return error;
}

/*
 * Function to get the uuid of the originator of the voucher.
 */
int
proc_pidoriginatoruuid(uuid_t uuid, uint32_t buffersize)
{
	pid_t originator_pid;
	return proc_pidoriginatorpid_uuid(uuid, buffersize, &originator_pid);
}

/*
 * Function to get the task ipc table size.
 */
int
proc_pidipctableinfo(proc_t p, struct proc_ipctableinfo *table_info)
{
	task_t task;
	int error = 0;

	task = p->task;

	bzero(table_info, sizeof(struct proc_ipctableinfo));
	error = fill_taskipctableinfo(task, &(table_info->table_size), &(table_info->table_free));

	if (error) {
		error = EINVAL;
	}

	return error;
}

int
proc_pidthreadschedinfo(proc_t p, uint64_t arg, struct proc_threadschedinfo *sched_info)
{
	int error;
	uint64_t const thread_id = (uint64_t)arg;
	task_t const task = p->task;

	bzero(sched_info, sizeof(*sched_info));
	error = fill_taskthreadschedinfo(task, thread_id, (struct proc_threadschedinfo_internal*)sched_info);

	if (error != 0) {
		error = EINVAL;
	}

	return error;
}

/***************************** proc_pidoriginatorinfo ***************************/

int
proc_pidoriginatorinfo(int pid, int flavor, user_addr_t buffer, uint32_t  buffersize, int32_t * retval)
{
	int error = ENOTSUP;
	uint32_t size;

	switch (flavor) {
	case PROC_PIDORIGINATOR_UUID:
		size = PROC_PIDORIGINATOR_UUID_SIZE;
		break;
	case PROC_PIDORIGINATOR_BGSTATE:
		size = PROC_PIDORIGINATOR_BGSTATE_SIZE;
		break;
	case PROC_PIDORIGINATOR_PID_UUID:
		size = PROC_PIDORIGINATOR_PID_UUID_SIZE;
		break;
	default:
		return EINVAL;
	}

	if (buffersize < size) {
		return ENOMEM;
	}

	if (pid != 0 && pid != proc_selfpid()) {
		return EINVAL;
	}

	switch (flavor) {
	case PROC_PIDORIGINATOR_UUID: {
		uuid_t uuid = {};

		error = proc_pidoriginatoruuid(uuid, sizeof(uuid));
		if (error != 0) {
			goto out;
		}

		error = copyout(uuid, buffer, size);
		if (error == 0) {
			*retval = size;
		}
	}
	break;

	case PROC_PIDORIGINATOR_PID_UUID: {
		struct proc_originatorinfo originator_info;
		bzero(&originator_info, sizeof(originator_info));

		error = proc_pidoriginatorpid_uuid(originator_info.originator_uuid,
		    sizeof(uuid_t), &originator_info.originator_pid);
		if (error != 0) {
			goto out;
		}

		error = copyout(&originator_info, buffer, size);
		if (error == 0) {
			*retval = size;
		}
	}
	break;

	case PROC_PIDORIGINATOR_BGSTATE: {
		uint32_t is_backgrounded = 0;
		error = proc_get_originatorbgstate(&is_backgrounded);
		if (error) {
			goto out;
		}

		error = copyout(&is_backgrounded, buffer, size);
		if (error == 0) {
			*retval = size;
		}
	}
	break;

	default:
		error = ENOTSUP;
	}
out:
	return error;
}

/***************************** proc_listcoalitions ***************************/
int
proc_listcoalitions(int flavor, int type, user_addr_t buffer,
    uint32_t buffersize, int32_t *retval)
{
#if CONFIG_COALITIONS
	int error = ENOTSUP;
	int coal_type;
	uint32_t elem_size;
	void *coalinfo = NULL;
	uint32_t k_buffersize = 0, copyout_sz = 0;
	int ncoals = 0, ncoals_ = 0;

	/* struct procinfo_coalinfo; */

	switch (flavor) {
	case LISTCOALITIONS_ALL_COALS:
		elem_size = LISTCOALITIONS_ALL_COALS_SIZE;
		coal_type = -1;
		break;
	case LISTCOALITIONS_SINGLE_TYPE:
		elem_size = LISTCOALITIONS_SINGLE_TYPE_SIZE;
		coal_type = type;
		break;
	default:
		return EINVAL;
	}

	/* find the total number of coalitions */
	ncoals = coalitions_get_list(coal_type, NULL, 0);

	if (ncoals == 0 || buffer == 0 || buffersize == 0) {
		/*
		 * user just wants buffer size
		 * or there are no coalitions
		 */
		error = 0;
		*retval = (int)(ncoals * elem_size);
		goto out;
	}

	k_buffersize = ncoals * elem_size;
	coalinfo = kalloc_data(k_buffersize, Z_WAITOK | Z_ZERO);
	if (!coalinfo) {
		error = ENOMEM;
		goto out;
	}

	switch (flavor) {
	case LISTCOALITIONS_ALL_COALS:
	case LISTCOALITIONS_SINGLE_TYPE:
		ncoals_ = coalitions_get_list(coal_type, coalinfo, ncoals);
		break;
	default:
		panic("memory corruption?!");
	}

	if (ncoals_ == 0) {
		/* all the coalitions disappeared... weird but valid */
		error = 0;
		*retval = 0;
		goto out;
	}

	/*
	 * Some coalitions may have disappeared between our initial check,
	 * and the the actual list acquisition.
	 * Only copy out what we really need.
	 */
	copyout_sz = k_buffersize;
	if (ncoals_ < ncoals) {
		copyout_sz = ncoals_ * elem_size;
	}

	/*
	 * copy the list up to user space
	 * (we're guaranteed to have a non-null pointer/size here)
	 */
	error = copyout(coalinfo, buffer,
	    copyout_sz < buffersize ? copyout_sz : buffersize);

	if (error == 0) {
		*retval = (int)copyout_sz;
	}

out:
	if (coalinfo) {
		kfree_data(coalinfo, k_buffersize);
	}

	return error;
#else
	/* no coalition support */
	(void)flavor;
	(void)type;
	(void)buffer;
	(void)buffersize;
	(void)retval;
	return ENOTSUP;
#endif
}


/*************************** proc_can_use_forgeound_hw **************************/
int
proc_can_use_foreground_hw(int pid, user_addr_t u_reason, uint32_t reasonsize, int32_t *retval)
{
	proc_t p = PROC_NULL;
	int error = 0;
	uint32_t reason = PROC_FGHW_ERROR;
	uint32_t isBG = 0;
	task_t task = TASK_NULL;
#if CONFIG_COALITIONS
	coalition_t coal = COALITION_NULL;
#endif

	*retval = 0;

	if (pid <= 0) {
		error = EINVAL;
		reason = PROC_FGHW_ERROR;
		goto out;
	}

	p = proc_find(pid);
	if (p == PROC_NULL) {
		error = ESRCH;
		reason = PROC_FGHW_ERROR;
		goto out;
	}

#if CONFIG_COALITIONS
	if (p != current_proc() &&
	    !kauth_cred_issuser(kauth_cred_get())) {
		error = EPERM;
		reason = PROC_FGHW_ERROR;
		goto out;
	}

	task = p->task;
	if (coalition_is_leader(task, task_get_coalition(task, COALITION_TYPE_JETSAM))) {
		task_reference(task);
	} else {
		/* current task is not a coalition leader: find the leader */
		task = coalition_get_leader(coal);
	}

	if (task != TASK_NULL) {
		/*
		 * If task is non-null, then it is the coalition leader of the
		 * current process' coalition. This could be the same task as
		 * the current_task, and that's OK.
		 */
		uint32_t flags = 0;
		int role;

		proc_get_darwinbgstate(task, &flags);
		if ((flags & PROC_FLAG_APPLICATION) != PROC_FLAG_APPLICATION) {
			/*
			 * Coalition leader is not an application, continue
			 * searching for other ways this task could gain
			 * access to HW
			 */
			reason = PROC_FGHW_DAEMON_LEADER;
			goto no_leader;
		}

		if (proc_get_effective_task_policy(task, TASK_POLICY_DARWIN_BG)) {
			/*
			 * If the leader of the current process' coalition has
			 * been marked as DARWIN_BG, then it definitely should
			 * not be using foreground hardware resources.
			 */
			reason = PROC_FGHW_LEADER_BACKGROUND;
			goto out;
		}

		role = proc_get_effective_task_policy(task, TASK_POLICY_ROLE);
		switch (role) {
		case TASK_FOREGROUND_APPLICATION: /* DARWIN_ROLE_UI_FOCAL */
		case TASK_BACKGROUND_APPLICATION: /* DARWIN_ROLE_UI */
			/*
			 * The leader of this coalition is a focal, UI app:
			 * access granted
			 * TODO: should extensions/plugins be allowed to use
			 *       this hardware?
			 */
			*retval = 1;
			reason = PROC_FGHW_OK;
			goto out;
		case TASK_DEFAULT_APPLICATION: /* DARWIN_ROLE_UI_NON_FOCAL */
		case TASK_NONUI_APPLICATION: /* DARWIN_ROLE_NON_UI */
		case TASK_THROTTLE_APPLICATION:
		case TASK_UNSPECIFIED:
		default:
			/* non-focal, non-ui apps don't get access */
			reason = PROC_FGHW_LEADER_NONUI;
			goto out;
		}
	}

no_leader:
	if (task != TASK_NULL) {
		task_deallocate(task);
		task = TASK_NULL;
	}
#endif /* CONFIG_COALITIONS */

	/*
	 * There is no reasonable semantic to investigate the currently
	 * adopted voucher of an arbitrary thread in a non-current process.
	 * We return '0'
	 */
	if (p != current_proc()) {
		error = EINVAL;
		goto out;
	}

	/*
	 * In the absence of coalitions, fall back to a voucher-based lookup
	 * where a daemon can used foreground HW if it's operating on behalf
	 * of a foreground application.
	 * NOTE: this is equivalent to a call to
	 *       proc_pidoriginatorinfo(PROC_PIDORIGINATOR_BGSTATE, &isBG, sizeof(isBG))
	 */
	isBG = 1;
	error = proc_get_originatorbgstate(&isBG);
	switch (error) {
	case 0:
		break;
	case ESRCH:
		reason = PROC_FGHW_NO_ORIGINATOR;
		error = 0;
		goto out;
	case ENOATTR:
		reason = PROC_FGHW_NO_VOUCHER_ATTR;
		error = 0;
		goto out;
	case EINVAL:
		reason = PROC_FGHW_DAEMON_NO_VOUCHER;
		error = 0;
		goto out;
	default:
		/* some other error occurred: report that to the caller */
		reason = PROC_FGHW_VOUCHER_ERROR;
		goto out;
	}

	if (isBG) {
		reason = PROC_FGHW_ORIGINATOR_BACKGROUND;
		error = 0;
	} else {
		/*
		 * The process itself is either a foreground app, or has
		 * adopted a voucher originating from an app that's still in
		 * the foreground
		 */
		reason = PROC_FGHW_DAEMON_OK;
		*retval = 1;
	}

out:
	if (task != TASK_NULL) {
		task_deallocate(task);
	}
	if (p != PROC_NULL) {
		proc_rele(p);
	}
	if (reasonsize >= sizeof(reason) && u_reason != (user_addr_t)0) {
		(void)copyout(&reason, u_reason, sizeof(reason));
	}
	return error;
}


/********************************** proc_pidinfo ********************************/


int
proc_pidinfo(int pid, uint32_t flags, uint64_t ext_id, int flavor, uint64_t arg, user_addr_t buffer, uint32_t  buffersize, int32_t * retval)
{
	struct proc * p = PROC_NULL;
	int error = ENOTSUP;
	int gotref = 0;
	int findzomb = 0;
	int shortversion = 0;
	uint32_t size;
	int zombie = 0;
	bool thuniqueid = false;
	int uniqidversion = 0;
	bool check_same_user;

	switch (flavor) {
	case PROC_PIDLISTFDS:
		size = PROC_PIDLISTFD_SIZE;
		if (buffer == USER_ADDR_NULL) {
			size = 0;
		}
		break;
	case PROC_PIDTBSDINFO:
		size = PROC_PIDTBSDINFO_SIZE;
		break;
	case PROC_PIDTASKINFO:
		size = PROC_PIDTASKINFO_SIZE;
		break;
	case PROC_PIDTASKALLINFO:
		size = PROC_PIDTASKALLINFO_SIZE;
		break;
	case PROC_PIDTHREADINFO:
		size = PROC_PIDTHREADINFO_SIZE;
		break;
	case PROC_PIDLISTTHREADIDS:
		size = PROC_PIDLISTTHREADIDS_SIZE;
		break;
	case PROC_PIDLISTTHREADS:
		size = PROC_PIDLISTTHREADS_SIZE;
		break;
	case PROC_PIDREGIONINFO:
		size = PROC_PIDREGIONINFO_SIZE;
		break;
	case PROC_PIDREGIONPATHINFO:
		size = PROC_PIDREGIONPATHINFO_SIZE;
		break;
	case PROC_PIDVNODEPATHINFO:
		size = PROC_PIDVNODEPATHINFO_SIZE;
		break;
	case PROC_PIDTHREADPATHINFO:
		size = PROC_PIDTHREADPATHINFO_SIZE;
		break;
	case PROC_PIDPATHINFO:
		size = MAXPATHLEN;
		break;
	case PROC_PIDWORKQUEUEINFO:
		/* kernel does not have workq info */
		if (pid == 0) {
			return EINVAL;
		} else {
			size = PROC_PIDWORKQUEUEINFO_SIZE;
		}
		break;
	case PROC_PIDT_SHORTBSDINFO:
		size = PROC_PIDT_SHORTBSDINFO_SIZE;
		break;
	case PROC_PIDLISTFILEPORTS:
		size = PROC_PIDLISTFILEPORTS_SIZE;
		if (buffer == (user_addr_t)0) {
			size = 0;
		}
		break;
	case PROC_PIDTHREADID64INFO:
		size = PROC_PIDTHREADID64INFO_SIZE;
		break;
	case PROC_PIDUNIQIDENTIFIERINFO:
		size = PROC_PIDUNIQIDENTIFIERINFO_SIZE;
		break;
	case PROC_PIDT_BSDINFOWITHUNIQID:
		size = PROC_PIDT_BSDINFOWITHUNIQID_SIZE;
		break;
	case PROC_PIDARCHINFO:
		size = PROC_PIDARCHINFO_SIZE;
		break;
	case PROC_PIDCOALITIONINFO:
		size = PROC_PIDCOALITIONINFO_SIZE;
		break;
	case PROC_PIDNOTEEXIT:
		/*
		 * Set findzomb explicitly because arg passed
		 * in is used as note exit status bits.
		 */
		size = PROC_PIDNOTEEXIT_SIZE;
		findzomb = 1;
		break;
	case PROC_PIDEXITREASONINFO:
		size = PROC_PIDEXITREASONINFO_SIZE;
		findzomb = 1;
		break;
	case PROC_PIDEXITREASONBASICINFO:
		size = PROC_PIDEXITREASONBASICINFOSIZE;
		findzomb = 1;
		break;
	case PROC_PIDREGIONPATHINFO2:
		size = PROC_PIDREGIONPATHINFO2_SIZE;
		break;
	case PROC_PIDREGIONPATHINFO3:
		size = PROC_PIDREGIONPATHINFO3_SIZE;
		break;
	case PROC_PIDLISTUPTRS:
		size = PROC_PIDLISTUPTRS_SIZE;
		if (buffer == USER_ADDR_NULL) {
			size = 0;
		}
		break;
	case PROC_PIDLISTDYNKQUEUES:
		size = PROC_PIDLISTDYNKQUEUES_SIZE;
		if (buffer == USER_ADDR_NULL) {
			size = 0;
		}
		break;
	case PROC_PIDVMRTFAULTINFO:
		size = sizeof(vm_rtfault_record_t);
		if (buffer == USER_ADDR_NULL) {
			size = 0;
		}
		break;
	case PROC_PIDPLATFORMINFO:
		size = PROC_PIDPLATFORMINFO_SIZE;
		findzomb = 1;
		break;
	case PROC_PIDREGIONPATH:
		size = PROC_PIDREGIONPATH_SIZE;
		break;
	case PROC_PIDIPCTABLEINFO:
		size = PROC_PIDIPCTABLEINFO_SIZE;
		break;
	case PROC_PIDTHREADSCHEDINFO:
		size = PROC_PIDTHREADSCHEDINFO_SIZE;
		break;
	default:
		return EINVAL;
	}

	if (buffersize < size) {
		return ENOMEM;
	}

	if ((flavor == PROC_PIDPATHINFO) && (buffersize > PROC_PIDPATHINFO_MAXSIZE)) {
		return EOVERFLOW;
	}

	/* Check if we need to look for zombies */
	if ((flavor == PROC_PIDTBSDINFO) || (flavor == PROC_PIDT_SHORTBSDINFO) || (flavor == PROC_PIDT_BSDINFOWITHUNIQID)
	    || (flavor == PROC_PIDUNIQIDENTIFIERINFO)) {
		if (arg) {
			findzomb = 1;
		}
	}

	if ((p = proc_find(pid)) == PROC_NULL) {
		if (findzomb) {
			p = proc_find_zombref(pid);
		}
		if (p == PROC_NULL) {
			error = ESRCH;
			goto out;
		}
		zombie = 1;
	} else {
		gotref = 1;
	}

	if ((flags & PIF_COMPARE_IDVERSION) && (ext_id != proc_pidversion(p))) {
		error = ESRCH;
		goto out;
	}
	if ((flags & PIF_COMPARE_UNIQUEID) && (ext_id != proc_uniqueid(p))) {
		error = ESRCH;
		goto out;
	}

	/* Certain operations don't require privileges */
	switch (flavor) {
	case PROC_PIDT_SHORTBSDINFO:
	case PROC_PIDUNIQIDENTIFIERINFO:
	case PROC_PIDPATHINFO:
	case PROC_PIDCOALITIONINFO:
	case PROC_PIDPLATFORMINFO:
		check_same_user = NO_CHECK_SAME_USER;
		break;
	default:
		check_same_user = CHECK_SAME_USER;
		break;
	}

	/* Do we have permission to look into this? */
	if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDINFO, flavor, check_same_user))) {
		goto out;
	}

	switch (flavor) {
	case PROC_PIDLISTFDS: {
		error = proc_pidfdlist(p, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDUNIQIDENTIFIERINFO: {
		struct proc_uniqidentifierinfo p_uniqidinfo;
		bzero(&p_uniqidinfo, sizeof(p_uniqidinfo));
		proc_piduniqidentifierinfo(p, &p_uniqidinfo);
		error = copyout(&p_uniqidinfo, buffer, sizeof(struct proc_uniqidentifierinfo));
		if (error == 0) {
			*retval = sizeof(struct proc_uniqidentifierinfo);
		}
	}
	break;

	case PROC_PIDT_SHORTBSDINFO:
		shortversion = 1;
		OS_FALLTHROUGH;
	case PROC_PIDT_BSDINFOWITHUNIQID:
	case PROC_PIDTBSDINFO: {
		struct proc_bsdinfo pbsd;
		struct proc_bsdshortinfo pbsd_short;
		struct proc_bsdinfowithuniqid pbsd_uniqid;

		if (flavor == PROC_PIDT_BSDINFOWITHUNIQID) {
			uniqidversion = 1;
		}

		if (shortversion != 0) {
			error = proc_pidshortbsdinfo(p, &pbsd_short, zombie);
		} else {
			error = proc_pidbsdinfo(p, &pbsd, zombie);
			if (uniqidversion != 0) {
				bzero(&pbsd_uniqid, sizeof(pbsd_uniqid));
				proc_piduniqidentifierinfo(p, &pbsd_uniqid.p_uniqidentifier);
				pbsd_uniqid.pbsd = pbsd;
			}
		}

		if (error == 0) {
			if (shortversion != 0) {
				error = copyout(&pbsd_short, buffer, sizeof(struct proc_bsdshortinfo));
				if (error == 0) {
					*retval = sizeof(struct proc_bsdshortinfo);
				}
			} else if (uniqidversion != 0) {
				error = copyout(&pbsd_uniqid, buffer, sizeof(struct proc_bsdinfowithuniqid));
				if (error == 0) {
					*retval = sizeof(struct proc_bsdinfowithuniqid);
				}
			} else {
				error = copyout(&pbsd, buffer, sizeof(struct proc_bsdinfo));
				if (error == 0) {
					*retval = sizeof(struct proc_bsdinfo);
				}
			}
		}
	}
	break;

	case PROC_PIDTASKINFO: {
		struct proc_taskinfo ptinfo;

		error =  proc_pidtaskinfo(p, &ptinfo);
		if (error == 0) {
			error = copyout(&ptinfo, buffer, sizeof(struct proc_taskinfo));
			if (error == 0) {
				*retval = sizeof(struct proc_taskinfo);
			}
		}
	}
	break;

	case PROC_PIDTASKALLINFO: {
		struct proc_taskallinfo pall;
		bzero(&pall, sizeof(pall));
		error = proc_pidbsdinfo(p, &pall.pbsd, 0);
		error =  proc_pidtaskinfo(p, &pall.ptinfo);
		if (error == 0) {
			error = copyout(&pall, buffer, sizeof(struct proc_taskallinfo));
			if (error == 0) {
				*retval = sizeof(struct proc_taskallinfo);
			}
		}
	}
	break;

	case PROC_PIDTHREADID64INFO:
		thuniqueid = true;
		OS_FALLTHROUGH;
	case PROC_PIDTHREADINFO:{
		struct proc_threadinfo pthinfo;

		error  = proc_pidthreadinfo(p, arg, thuniqueid, &pthinfo);
		if (error == 0) {
			error = copyout(&pthinfo, buffer, sizeof(struct proc_threadinfo));
			if (error == 0) {
				*retval = sizeof(struct proc_threadinfo);
			}
		}
	}
	break;

	case PROC_PIDLISTTHREADIDS:
		thuniqueid = true;
		OS_FALLTHROUGH;
	case PROC_PIDLISTTHREADS:{
		error =  proc_pidlistthreads(p, thuniqueid, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDREGIONINFO:{
		error =  proc_pidregioninfo(p, arg, buffer, buffersize, retval);
	}
	break;


	case PROC_PIDREGIONPATHINFO:{
		error =  proc_pidregionpathinfo(p, arg, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDREGIONPATHINFO2:{
		error =  proc_pidregionpathinfo2(p, arg, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDREGIONPATHINFO3:{
		error =  proc_pidregionpathinfo3(p, arg, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDVNODEPATHINFO:{
		error =  proc_pidvnodepathinfo(p, arg, buffer, buffersize, retval);
	}
	break;


	case PROC_PIDTHREADPATHINFO:{
		struct proc_threadwithpathinfo pinfo;

		error  = proc_pidthreadpathinfo(p, arg, &pinfo);
		if (error == 0) {
			error = copyout((caddr_t)&pinfo, buffer, sizeof(struct proc_threadwithpathinfo));
			if (error == 0) {
				*retval = sizeof(struct proc_threadwithpathinfo);
			}
		}
	}
	break;

	case PROC_PIDPATHINFO: {
		error =  proc_pidpathinfo(p, arg, buffer, buffersize, retval);
	}
	break;


	case PROC_PIDWORKQUEUEINFO:{
		struct proc_workqueueinfo pwqinfo;

		error  = proc_pidworkqueueinfo(p, &pwqinfo);
		if (error == 0) {
			error = copyout(&pwqinfo, buffer, sizeof(struct proc_workqueueinfo));
			if (error == 0) {
				*retval = sizeof(struct proc_workqueueinfo);
			}
		}
	}
	break;

	case PROC_PIDLISTFILEPORTS: {
		error = proc_pidfileportlist(p, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDARCHINFO: {
		struct proc_archinfo pai;
		bzero(&pai, sizeof(pai));
		proc_archinfo(p, &pai);
		error = copyout(&pai, buffer, sizeof(struct proc_archinfo));
		if (error == 0) {
			*retval = sizeof(struct proc_archinfo);
		}
	}
	break;

	case PROC_PIDCOALITIONINFO: {
		struct proc_pidcoalitioninfo pci;
		proc_pidcoalitioninfo(p, &pci);
		error = copyout(&pci, buffer, sizeof(struct proc_pidcoalitioninfo));
		if (error == 0) {
			*retval = sizeof(struct proc_pidcoalitioninfo);
		}
	}
	break;

	case PROC_PIDNOTEEXIT: {
		uint32_t data;
		error = proc_pidnoteexit(p, arg, &data);
		if (error == 0) {
			error = copyout(&data, buffer, sizeof(data));
			if (error == 0) {
				*retval = sizeof(data);
			}
		}
	}
	break;

	case PROC_PIDEXITREASONINFO: {
		struct proc_exitreasoninfo eri;

		error = copyin(buffer, &eri, sizeof(eri));
		if (error != 0) {
			break;
		}

		error = proc_pidexitreasoninfo(p, &eri, NULL);
		if (error == 0) {
			error = copyout(&eri, buffer, sizeof(eri));
			if (error == 0) {
				*retval =  sizeof(eri);
			}
		}
	}
	break;

	case PROC_PIDEXITREASONBASICINFO: {
		struct proc_exitreasonbasicinfo beri;

		bzero(&beri, sizeof(struct proc_exitreasonbasicinfo));

		error = proc_pidexitreasoninfo(p, NULL, &beri);
		if (error == 0) {
			error = copyout(&beri, buffer, sizeof(beri));
			if (error == 0) {
				*retval =  sizeof(beri);
			}
		}
	}
	break;

	case PROC_PIDLISTUPTRS:
		error = proc_pidlistuptrs(p, buffer, buffersize, retval);
		break;

	case PROC_PIDLISTDYNKQUEUES:
		error = kevent_copyout_proc_dynkqids(p, buffer, buffersize, retval);
		break;
	case PROC_PIDVMRTFAULTINFO: {
		/* This interface can only be employed on the current
		 * process. We will eventually enforce an entitlement.
		 */
		*retval = 0;

		if (p != current_proc()) {
			error = EINVAL;
			break;
		}

		size_t kbufsz = MIN(buffersize, vmrtfaultinfo_bufsz());
		void *vmrtfbuf = kalloc_data(kbufsz, Z_WAITOK | Z_ZERO);

		if (vmrtfbuf == NULL) {
			error = ENOMEM;
			break;
		}

		uint64_t effpid = get_current_unique_pid();
		/* The VM may choose to provide more comprehensive records
		 * for root-privileged users on internal configurations.
		 */
		boolean_t isroot = (suser(kauth_cred_get(), (u_short *)0) == 0);
		size_t num_extracted = 0;
		int vmf_residue = vmrtf_extract(effpid, isroot, kbufsz, vmrtfbuf, &num_extracted);
		size_t vmfsz = num_extracted * sizeof(vm_rtfault_record_t);

		*retval = (int32_t)MIN(num_extracted, INT32_MAX);

		error = 0;
		if (vmfsz) {
			error = copyout(vmrtfbuf, buffer, vmfsz);
		}

		if (error == 0) {
			if (vmf_residue) {
				error = ENOMEM;
			}
		}
		kfree_data(vmrtfbuf, kbufsz);
	}
	break;
	case PROC_PIDPLATFORMINFO: {
		proc_lock(p);
		uint32_t platform = proc_platform(p);
		proc_unlock(p);
		error = copyout(&platform, buffer, sizeof(uint32_t));
		if (error == 0) {
			*retval = sizeof(uint32_t);
		}
	} break;
	case PROC_PIDREGIONPATH: {
		error = proc_pidregionpath(p, arg, buffer, buffersize, retval);
	}
	break;
	case PROC_PIDIPCTABLEINFO: {
		struct proc_ipctableinfo table_info;

		error = proc_pidipctableinfo(p, &table_info);
		if (error == 0) {
			error = copyout(&table_info, buffer, sizeof(struct proc_ipctableinfo));
			if (error == 0) {
				*retval = sizeof(struct proc_ipctableinfo);
			}
		}
	}
	break;
	case PROC_PIDTHREADSCHEDINFO: {
		struct proc_threadschedinfo sched_info;

		error = proc_pidthreadschedinfo(p, arg, &sched_info);
		if (error == 0) {
			error = copyout(&sched_info, buffer, sizeof(sched_info));
			if (error == 0) {
				*retval = sizeof(sched_info);
			}
		}
	}
	break;
	default:
		error = ENOTSUP;
		break;
	}

out:
	if (gotref) {
		proc_rele(p);
	} else if (zombie) {
		proc_drop_zombref(p);
	}
	return error;
}


int
pid_vnodeinfo(vnode_t vp, struct fileproc * fp, proc_t proc, user_addr_t  buffer, __unused uint32_t buffersize, int32_t * retval)
{
	struct vnode_fdinfo vfi;
	uint32_t vid = vnode_vid(vp);
	int error = 0;

	if ((error = vnode_getwithvid(vp, vid)) != 0) {
		return error;
	}
	bzero(&vfi, sizeof(struct vnode_fdinfo));
	fill_fileinfo(fp, proc, &vfi.pfi);
	error = fill_vnodeinfo(vp, &vfi.pvi, FALSE);
	vnode_put(vp);
	if (error == 0) {
		error = copyout((caddr_t)&vfi, buffer, sizeof(struct vnode_fdinfo));
		if (error == 0) {
			*retval = sizeof(struct vnode_fdinfo);
		}
	}
	return error;
}

int
pid_vnodeinfopath(vnode_t vp, struct fileproc * fp, proc_t proc, user_addr_t  buffer, __unused uint32_t buffersize, int32_t * retval)
{
	struct vnode_fdinfowithpath vfip;
	uint32_t vid = vnode_vid(vp);
	int count, error = 0;

	if ((error = vnode_getwithvid(vp, vid)) != 0) {
		return error;
	}
	bzero(&vfip, sizeof(struct vnode_fdinfowithpath));
	fill_fileinfo(fp, proc, &vfip.pfi);
	error = fill_vnodeinfo(vp, &vfip.pvip.vip_vi, TRUE);
	if (error == 0) {
		count = MAXPATHLEN;
		vn_getpath(vp, &vfip.pvip.vip_path[0], &count);
		vfip.pvip.vip_path[MAXPATHLEN - 1] = 0;
		vnode_put(vp);
		error = copyout((caddr_t)&vfip, buffer, sizeof(struct vnode_fdinfowithpath));
		if (error == 0) {
			*retval = sizeof(struct vnode_fdinfowithpath);
		}
	} else {
		vnode_put(vp);
	}
	return error;
}

void
fill_fileinfo(struct fileproc * fp, proc_t proc, struct proc_fileinfo * fproc)
{
	fproc->fi_openflags = fp->fp_glob->fg_flag;
	fproc->fi_status = 0;
	fproc->fi_offset = fp->fp_glob->fg_offset;
	fproc->fi_type = FILEGLOB_DTYPE(fp->fp_glob);
	if (os_ref_get_count_raw(&fp->fp_glob->fg_count) > 1) {
		fproc->fi_status |= PROC_FP_SHARED;
	}
	if (proc != PROC_NULL) {
		if (fp->fp_flags & FP_CLOEXEC) {
			fproc->fi_status |= PROC_FP_CLEXEC;
		}
		if (fp->fp_flags & FP_CLOFORK) {
			fproc->fi_status |= PROC_FP_CLFORK;
		}
	}
	if (fp->fp_guard_attrs) {
		fproc->fi_status |= PROC_FP_GUARDED;
		fproc->fi_guardflags = 0;
		if (fp_isguarded(fp, GUARD_CLOSE)) {
			fproc->fi_guardflags |= PROC_FI_GUARD_CLOSE;
		}
		if (fp_isguarded(fp, GUARD_DUP)) {
			fproc->fi_guardflags |= PROC_FI_GUARD_DUP;
		}
		if (fp_isguarded(fp, GUARD_SOCKET_IPC)) {
			fproc->fi_guardflags |= PROC_FI_GUARD_SOCKET_IPC;
		}
		if (fp_isguarded(fp, GUARD_FILEPORT)) {
			fproc->fi_guardflags |= PROC_FI_GUARD_FILEPORT;
		}
	}
}



int
fill_vnodeinfo(vnode_t vp, struct vnode_info *vinfo, __unused boolean_t check_fsgetpath)
{
	vfs_context_t context;
	struct stat64 sb;
	int error = 0;

	bzero(&sb, sizeof(struct stat64));
	context = vfs_context_create((vfs_context_t)0);
#if CONFIG_MACF
	/* Called when vnode info is used by the caller to get vnode's path */
	if (check_fsgetpath) {
		error = mac_vnode_check_fsgetpath(context, vp);
	}
#endif
	if (!error) {
		error = vn_stat(vp, &sb, NULL, 1, 0, context);
		munge_vinfo_stat(&sb, &vinfo->vi_stat);
	}
	(void)vfs_context_rele(context);
	if (error != 0) {
		goto out;
	}

	if (vp->v_mount != dead_mountp) {
		vinfo->vi_fsid = vp->v_mount->mnt_vfsstat.f_fsid;
	} else {
		vinfo->vi_fsid.val[0] = 0;
		vinfo->vi_fsid.val[1] = 0;
	}
	vinfo->vi_type = vp->v_type;
out:
	return error;
}

int
pid_socketinfo(socket_t so, struct fileproc *fp, proc_t proc, user_addr_t  buffer, __unused uint32_t buffersize, int32_t * retval)
{
#if SOCKETS
	struct socket_fdinfo s;
	int error = 0;

	bzero(&s, sizeof(struct socket_fdinfo));
	fill_fileinfo(fp, proc, &s.pfi);
	if ((error = fill_socketinfo(so, &s.psi)) == 0) {
		if ((error = copyout(&s, buffer, sizeof(struct socket_fdinfo))) == 0) {
			*retval = sizeof(struct socket_fdinfo);
		}
	}
	return error;
#else
#pragma unused(so, fp, proc, fd, buffer)
	*retval = 0;
	return ENOTSUP;
#endif
}

int
pid_pseminfo(struct psemnode *psem, struct fileproc *fp, proc_t proc, user_addr_t  buffer, __unused uint32_t buffersize, int32_t * retval)
{
	struct psem_fdinfo pseminfo;
	int error = 0;

	bzero(&pseminfo, sizeof(struct psem_fdinfo));
	fill_fileinfo(fp, proc, &pseminfo.pfi);

	if ((error = fill_pseminfo(psem, &pseminfo.pseminfo)) == 0) {
		if ((error = copyout(&pseminfo, buffer, sizeof(struct psem_fdinfo))) == 0) {
			*retval = sizeof(struct psem_fdinfo);
		}
	}

	return error;
}

int
pid_pshminfo(struct pshmnode *pshm, struct fileproc *fp, proc_t proc, user_addr_t  buffer, __unused uint32_t buffersize, int32_t * retval)
{
	struct pshm_fdinfo pshminfo;
	int error = 0;

	bzero(&pshminfo, sizeof(struct pshm_fdinfo));
	fill_fileinfo(fp, proc, &pshminfo.pfi);

	if ((error = fill_pshminfo(pshm, &pshminfo.pshminfo)) == 0) {
		if ((error = copyout(&pshminfo, buffer, sizeof(struct pshm_fdinfo))) == 0) {
			*retval = sizeof(struct pshm_fdinfo);
		}
	}

	return error;
}

int
pid_pipeinfo(struct pipe *  p, struct fileproc *fp, proc_t proc, user_addr_t  buffer, __unused uint32_t buffersize, int32_t * retval)
{
	struct pipe_fdinfo pipeinfo;
	int error = 0;

	bzero(&pipeinfo, sizeof(struct pipe_fdinfo));
	fill_fileinfo(fp, proc, &pipeinfo.pfi);
	if ((error = fill_pipeinfo(p, &pipeinfo.pipeinfo)) == 0) {
		if ((error = copyout(&pipeinfo, buffer, sizeof(struct pipe_fdinfo))) == 0) {
			*retval = sizeof(struct pipe_fdinfo);
		}
	}

	return error;
}

int
pid_kqueueinfo(struct kqueue * kq, struct fileproc *fp, proc_t proc, user_addr_t  buffer, __unused uint32_t buffersize, int32_t * retval)
{
	struct kqueue_fdinfo kqinfo;
	int error = 0;

	bzero(&kqinfo, sizeof(struct kqueue_fdinfo));

	/* not all kq's are associated with a file (e.g. workqkq) */
	if (fp) {
		fill_fileinfo(fp, proc, &kqinfo.pfi);
	}

	if ((error = fill_kqueueinfo(kq, &kqinfo.kqueueinfo)) == 0) {
		if ((error = copyout(&kqinfo, buffer, sizeof(struct kqueue_fdinfo))) == 0) {
			*retval = sizeof(struct kqueue_fdinfo);
		}
	}

	return error;
}


/************************** proc_pidfdinfo routine ***************************/
int
proc_pidfdinfo(int pid, int flavor, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
{
	proc_t p;
	int error = ENOTSUP;
	struct fileproc *fp = NULL;
	uint32_t size;

	switch (flavor) {
	case PROC_PIDFDVNODEINFO:
		size = PROC_PIDFDVNODEINFO_SIZE;
		break;
	case PROC_PIDFDVNODEPATHINFO:
		size = PROC_PIDFDVNODEPATHINFO_SIZE;
		break;
	case PROC_PIDFDSOCKETINFO:
		size = PROC_PIDFDSOCKETINFO_SIZE;
		break;
	case PROC_PIDFDPSEMINFO:
		size = PROC_PIDFDPSEMINFO_SIZE;
		break;
	case PROC_PIDFDPSHMINFO:
		size = PROC_PIDFDPSHMINFO_SIZE;
		break;
	case PROC_PIDFDPIPEINFO:
		size = PROC_PIDFDPIPEINFO_SIZE;
		break;
	case PROC_PIDFDKQUEUEINFO:
		size = PROC_PIDFDKQUEUEINFO_SIZE;
		break;
	case PROC_PIDFDKQUEUE_EXTINFO:
		size = PROC_PIDFDKQUEUE_EXTINFO_SIZE;
		if (buffer == (user_addr_t)0) {
			size = 0;
		}
		break;
	case PROC_PIDFDATALKINFO:
		size = PROC_PIDFDATALKINFO_SIZE;
		break;

	default:
		return EINVAL;
	}

	if (buffersize < size) {
		return ENOMEM;
	}

	if ((p = proc_find(pid)) == PROC_NULL) {
		error = ESRCH;
		goto out;
	}

	/* Do we have permission to look into this? */
	if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDFDINFO, flavor, CHECK_SAME_USER))) {
		goto out1;
	}

	switch (flavor) {
	case PROC_PIDFDVNODEINFO: {
		if ((error = fp_get_ftype(p, fd, DTYPE_VNODE, EBADF, &fp)) != 0) {
			goto out1;
		}
		error = pid_vnodeinfo(fp->fp_glob->fg_data, fp, p, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDFDVNODEPATHINFO: {
		if ((error = fp_get_ftype(p, fd, DTYPE_VNODE, EBADF, &fp)) != 0) {
			goto out1;
		}
		error = pid_vnodeinfopath(fp->fp_glob->fg_data, fp, p, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDFDSOCKETINFO: {
		if ((error = fp_get_ftype(p, fd, DTYPE_SOCKET, ENOTSOCK, &fp)) != 0) {
			goto out1;
		}
		error = pid_socketinfo(fp->fp_glob->fg_data, fp, p, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDFDPSEMINFO: {
		if ((error = fp_get_ftype(p, fd, DTYPE_PSXSEM, EBADF, &fp)) != 0) {
			goto out1;
		}
		error = pid_pseminfo(fp->fp_glob->fg_data, fp, p, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDFDPSHMINFO: {
		if ((error = fp_get_ftype(p, fd, DTYPE_PSXSHM, EBADF, &fp)) != 0) {
			goto out1;
		}
		error = pid_pshminfo(fp->fp_glob->fg_data, fp, p, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDFDPIPEINFO: {
		if ((error = fp_get_ftype(p, fd, DTYPE_PIPE, EBADF, &fp)) != 0) {
			goto out1;
		}
		error = pid_pipeinfo(fp->fp_glob->fg_data, fp, p, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDFDKQUEUEINFO: {
		kqueue_t kqu;

		if (fd == -1) {
			if ((kqu.kqwq = p->p_fd.fd_wqkqueue) == NULL) {
				/* wqkqueue is initialized on-demand */
				error = 0;
				break;
			}
		} else if ((error = fp_get_ftype(p, fd, DTYPE_KQUEUE, EBADF, &fp)) != 0) {
			goto out1;
		} else {
			kqu.kq = fp->fp_glob->fg_data;
		}

		error = pid_kqueueinfo(kqu.kq, fp, p, buffer, buffersize, retval);
	}
	break;

	case PROC_PIDFDKQUEUE_EXTINFO: {
		kqueue_t kqu;

		if (fd == -1) {
			if ((kqu.kqwq = p->p_fd.fd_wqkqueue) == NULL) {
				/* wqkqueue is initialized on-demand */
				error = 0;
				break;
			}
		} else if ((error = fp_get_ftype(p, fd, DTYPE_KQUEUE, EBADF, &fp)) != 0) {
			goto out1;
		} else {
			kqu.kq = fp->fp_glob->fg_data;
		}
		error = pid_kqueue_extinfo(p, kqu.kq, buffer, buffersize, retval);
	}
	break;

	default: {
		error = EINVAL;
		goto out1;
	}
	}

	if (fp) {
		fp_drop(p, fd, fp, 0);
	}
out1:
	proc_rele(p);
out:
	return error;
}

#define MAX_UPTRS 16392

int
proc_pidlistuptrs(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval)
{
	uint32_t count = 0;
	int error = 0;
	void *kbuf = NULL;
	int32_t nuptrs = 0;

	if (buffer == USER_ADDR_NULL || buffersize < sizeof(uint64_t)) {
		buffersize = 0;
	} else {
		count = MIN(buffersize / sizeof(uint64_t), MAX_UPTRS);
		buffersize = count * sizeof(uint64_t);
		kbuf = kalloc_data(buffersize, Z_WAITOK);
	}

	nuptrs = kevent_proc_copy_uptrs(p, kbuf, buffersize);

	if (kbuf) {
		size_t copysize;
		if (os_mul_overflow(nuptrs, sizeof(uint64_t), &copysize)) {
			error = ERANGE;
			goto out;
		}
		if (copysize > buffersize) {
			copysize = buffersize;
		}
		error = copyout(kbuf, buffer, copysize);
	}

out:
	*retval = nuptrs;

	if (kbuf) {
		kfree_data(kbuf, buffersize);
		kbuf = NULL;
	}

	return error;
}

/*
 * Helper function for proc_pidfileportinfo
 */

struct fileport_info_args {
	int             fia_flavor;
	user_addr_t     fia_buffer;
	uint32_t        fia_buffersize;
	int32_t         *fia_retval;
};

static kern_return_t
proc_fileport_info(__unused mach_port_name_t name,
    struct fileglob *fg, void *arg)
{
	struct fileport_info_args *fia = arg;
	struct fileproc __fileproc, *fp = &__fileproc;
	int error;

	bzero(fp, sizeof(*fp));
	fp->fp_glob = fg;

	switch (fia->fia_flavor) {
	case PROC_PIDFILEPORTVNODEPATHINFO: {
		vnode_t vp;

		if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) {
			error = ENOTSUP;
			break;
		}
		vp = (struct vnode *)fg->fg_data;
		error = pid_vnodeinfopath(vp, fp, PROC_NULL,
		    fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
	}       break;

	case PROC_PIDFILEPORTSOCKETINFO: {
		socket_t so;

		if (FILEGLOB_DTYPE(fg) != DTYPE_SOCKET) {
			error = EOPNOTSUPP;
			break;
		}
		so = (socket_t)fg->fg_data;
		error = pid_socketinfo(so, fp, PROC_NULL,
		    fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
	}       break;

	case PROC_PIDFILEPORTPSHMINFO: {
		struct pshmnode *pshm;

		if (FILEGLOB_DTYPE(fg) != DTYPE_PSXSHM) {
			error = EBADF;          /* ick - mirror fp_getfpshm */
			break;
		}
		pshm = (struct pshmnode *)fg->fg_data;
		error = pid_pshminfo(pshm, fp, PROC_NULL,
		    fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
	}       break;

	case PROC_PIDFILEPORTPIPEINFO: {
		struct pipe *cpipe;

		if (FILEGLOB_DTYPE(fg) != DTYPE_PIPE) {
			error = EBADF;          /* ick - mirror fp_getfpipe */
			break;
		}
		cpipe = (struct pipe *)fg->fg_data;
		error = pid_pipeinfo(cpipe, fp, PROC_NULL,
		    fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
	}       break;

	default:
		error = EINVAL;
		break;
	}

	return error;
}

/************************* proc_pidfileportinfo routine *********************/
int
proc_pidfileportinfo(int pid, int flavor, mach_port_name_t name,
    user_addr_t buffer, uint32_t buffersize, int32_t *retval)
{
	proc_t p;
	int error = ENOTSUP;
	uint32_t size;
	struct fileport_info_args fia;

	/* fileport types are restricted by file_issendable() */

	switch (flavor) {
	case PROC_PIDFILEPORTVNODEPATHINFO:
		size = PROC_PIDFILEPORTVNODEPATHINFO_SIZE;
		break;
	case PROC_PIDFILEPORTSOCKETINFO:
		size = PROC_PIDFILEPORTSOCKETINFO_SIZE;
		break;
	case PROC_PIDFILEPORTPSHMINFO:
		size = PROC_PIDFILEPORTPSHMINFO_SIZE;
		break;
	case PROC_PIDFILEPORTPIPEINFO:
		size = PROC_PIDFILEPORTPIPEINFO_SIZE;
		break;
	default:
		return EINVAL;
	}

	if (buffersize < size) {
		return ENOMEM;
	}
	if ((p = proc_find(pid)) == PROC_NULL) {
		error = ESRCH;
		goto out;
	}

	/* Do we have permission to look into this? */
	if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDFILEPORTINFO, flavor, CHECK_SAME_USER))) {
		goto out1;
	}

	fia.fia_flavor = flavor;
	fia.fia_buffer = buffer;
	fia.fia_buffersize = buffersize;
	fia.fia_retval = retval;

	if (fileport_invoke(p->task, name,
	    proc_fileport_info, &fia, &error) != KERN_SUCCESS) {
		error = EINVAL;
	}
out1:
	proc_rele(p);
out:
	return error;
}

int
proc_security_policy(proc_t targetp, __unused int callnum, __unused int flavor, boolean_t check_same_user)
{
#if CONFIG_MACF
	int error = 0;

	if ((error = mac_proc_check_proc_info(current_proc(), targetp, callnum, flavor))) {
		return error;
	}
#endif

	/* The 'listpids' call doesn't have a target proc */
	if (targetp == PROC_NULL) {
		assert(callnum == PROC_INFO_CALL_LISTPIDS && check_same_user == NO_CHECK_SAME_USER);
		return 0;
	}

	/*
	 * Check for 'get information for processes owned by other users' privilege
	 * root has this privilege by default
	 */
	if (priv_check_cred(kauth_cred_get(), PRIV_GLOBAL_PROC_INFO, 0) == 0) {
		check_same_user = FALSE;
	}

	if (check_same_user) {
		kauth_cred_t target_cred;
		uid_t        target_uid;

		target_cred = kauth_cred_proc_ref(targetp);
		target_uid  = kauth_cred_getuid(target_cred);
		kauth_cred_unref(&target_cred);

		if (kauth_getuid() != target_uid) {
			return EPERM;
		}
	}

	return 0;
}

int
proc_kernmsgbuf(user_addr_t buffer, uint32_t buffersize, int32_t * retval)
{
#if CONFIG_MACF
	int error = 0;

	if ((error = mac_system_check_info(kauth_cred_get(), "kern.msgbuf"))) {
		return error;
	}
#endif

	if (suser(kauth_cred_get(), (u_short *)0) == 0) {
		return log_dmesg(buffer, buffersize, retval);
	} else {
		return EPERM;
	}
}

/* ********* process control sets on self only */
int
proc_setcontrol(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, __unused int32_t * retval)
{
	struct proc * pself = PROC_NULL;
	int error = 0;
	uint32_t pcontrol = (uint32_t)arg;
	struct uthread *ut = NULL;
	char name_buf[MAXTHREADNAMESIZE];

	pself = current_proc();
	if (pid != proc_getpid(pself)) {
		return EINVAL;
	}

	/* Do we have permission to look into this? */
	if ((error = proc_security_policy(pself, PROC_INFO_CALL_SETCONTROL, flavor, NO_CHECK_SAME_USER))) {
		goto out;
	}

	switch (flavor) {
	case PROC_SELFSET_PCONTROL: {
		if (pcontrol > P_PCMAX) {
			return EINVAL;
		}
		proc_lock(pself);
		/* reset existing control setting while retaining action state */
		pself->p_pcaction &= PROC_ACTION_MASK;
		/* set new control state */
		pself->p_pcaction |= pcontrol;
		proc_unlock(pself);
	}
	break;

	case PROC_SELFSET_THREADNAME: {
		/*
		 * This is a bit ugly, as it copies the name into the kernel, and then
		 * invokes bsd_setthreadname again to copy it into the uthread name
		 * buffer.  Hopefully this isn't such a hot codepath that an additional
		 * MAXTHREADNAMESIZE copy is a big issue.
		 */
		if (buffersize > (MAXTHREADNAMESIZE - 1)) {
			return ENAMETOOLONG;
		}

		ut = current_uthread();

		bzero(name_buf, MAXTHREADNAMESIZE);
		error = copyin(buffer, name_buf, buffersize);

		if (!error) {
			bsd_setthreadname(ut, name_buf);
		}
	}
	break;

	case PROC_SELFSET_VMRSRCOWNER: {
		/* need to to be superuser */
		if (suser(kauth_cred_get(), (u_short *)0) != 0) {
			error = EPERM;
			goto out;
		}

		proc_lock(pself);
		/* reset existing control setting while retaining action state */
		pself->p_lflag |= P_LVMRSRCOWNER;
		proc_unlock(pself);
	}
	break;

	case PROC_SELFSET_DELAYIDLESLEEP: {
#if CONFIG_DELAY_IDLE_SLEEP
		/* mark or clear the process property to delay idle sleep disk IO */
		if (pcontrol != 0) {
			OSBitOrAtomic(P_DELAYIDLESLEEP, &pself->p_flag);
		} else {
			OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP), &pself->p_flag);
		}
	}
	break;
#else
		error = ENOTSUP;
		goto out;
	}
#endif

	default:
		error = ENOTSUP;
	}

out:
	return error;
}

#if CONFIG_MEMORYSTATUS

int
proc_dirtycontrol(int pid, int flavor, uint64_t arg, int32_t *retval)
{
	struct proc *target_p;
	int error = 0;
	uint32_t pcontrol = (uint32_t)arg;
	kauth_cred_t my_cred, target_cred;
	boolean_t self = FALSE;
	boolean_t child = FALSE;
	boolean_t zombref = FALSE;
	pid_t selfpid;

	target_p = proc_find(pid);

	if (target_p == PROC_NULL) {
		if (flavor == PROC_DIRTYCONTROL_GET) {
			target_p = proc_find_zombref(pid);
			zombref = 1;
		}

		if (target_p == PROC_NULL) {
			return ESRCH;
		}
	}

	my_cred = kauth_cred_get();
	target_cred = kauth_cred_proc_ref(target_p);

	/* Do we have permission to look into this? */
	if ((error = proc_security_policy(target_p, PROC_INFO_CALL_DIRTYCONTROL, flavor, NO_CHECK_SAME_USER))) {
		goto out;
	}

	selfpid = proc_selfpid();
	if (pid == selfpid) {
		self = TRUE;
	} else if (target_p->p_ppid == selfpid) {
		child = TRUE;
	}

	switch (flavor) {
	case PROC_DIRTYCONTROL_TRACK: {
		/* Only allow the process itself, its parent, or root */
		if ((self == FALSE) && (child == FALSE) && kauth_cred_issuser(kauth_cred_get()) != TRUE) {
			error = EPERM;
			goto out;
		}

		error = memorystatus_dirty_track(target_p, pcontrol);
	}
	break;

	case PROC_DIRTYCONTROL_SET: {
		/* Check privileges; use cansignal() here since the process could be terminated */
		if (!cansignal(current_proc(), my_cred, target_p, SIGKILL)) {
			error = EPERM;
			goto out;
		}

		error = memorystatus_dirty_set(target_p, self, pcontrol);
	}
	break;

	case PROC_DIRTYCONTROL_GET: {
		/* No permissions check - dirty state is freely available */
		if (retval) {
			*retval = memorystatus_dirty_get(target_p, FALSE);
		} else {
			error = EINVAL;
		}
	}
	break;

	case PROC_DIRTYCONTROL_CLEAR: {
		/* Check privileges; use cansignal() here since the process could be terminated */
		if (!cansignal(current_proc(), my_cred, target_p, SIGKILL)) {
			error = EPERM;
			goto out;
		}

		error = memorystatus_dirty_clear(target_p, pcontrol);
	}
	break;
	}

out:
	if (zombref) {
		proc_drop_zombref(target_p);
	} else {
		proc_rele(target_p);
	}

	kauth_cred_unref(&target_cred);

	return error;
}
#else

int
proc_dirtycontrol(__unused int pid, __unused int flavor, __unused uint64_t arg, __unused int32_t *retval)
{
	return ENOTSUP;
}

#endif /* CONFIG_MEMORYSTATUS */

/*
 * proc_terminate() provides support for sudden termination.
 * SIGKILL is issued to tracked, clean processes; otherwise,
 * SIGTERM is sent.
 */

int
proc_terminate(int pid, int32_t *retval)
{
	int error = 0;
	proc_t p;
	kauth_cred_t uc = kauth_cred_get();
	int sig;

#if 0
	/* XXX: Check if these are necessary */
	AUDIT_ARG(pid, pid);
	AUDIT_ARG(signum, sig);
#endif

	if (pid <= 0 || retval == NULL) {
		return EINVAL;
	}

	if ((p = proc_find(pid)) == NULL) {
		return ESRCH;
	}

#if 0
	/* XXX: Check if these are necessary */
	AUDIT_ARG(process, p);
#endif

	/* Check privileges; if SIGKILL can be issued, then SIGTERM is also OK */
	if (!cansignal(current_proc(), uc, p, SIGKILL)) {
		error = EPERM;
		goto out;
	}

	/* Not allowed to sudden terminate yourself */
	if (p == current_proc()) {
		error = EPERM;
		goto out;
	}

#if CONFIG_MEMORYSTATUS
	/* Determine requisite signal to issue */
	sig = memorystatus_on_terminate(p);
#else
	sig = SIGTERM;
#endif

	proc_set_task_policy(p->task, TASK_POLICY_ATTRIBUTE,
	    TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);

	psignal(p, sig);
	*retval = sig;

out:
	proc_rele(p);

	return error;
}

/*
 * copy stat64 structure into vinfo_stat structure.
 */
static void
munge_vinfo_stat(struct stat64 *sbp, struct vinfo_stat *vsbp)
{
	bzero(vsbp, sizeof(struct vinfo_stat));

	vsbp->vst_dev = sbp->st_dev;
	vsbp->vst_mode = sbp->st_mode;
	vsbp->vst_nlink = sbp->st_nlink;
	vsbp->vst_ino = sbp->st_ino;
	vsbp->vst_uid = sbp->st_uid;
	vsbp->vst_gid = sbp->st_gid;
	vsbp->vst_atime = sbp->st_atimespec.tv_sec;
	vsbp->vst_atimensec = sbp->st_atimespec.tv_nsec;
	vsbp->vst_mtime = sbp->st_mtimespec.tv_sec;
	vsbp->vst_mtimensec = sbp->st_mtimespec.tv_nsec;
	vsbp->vst_ctime = sbp->st_ctimespec.tv_sec;
	vsbp->vst_ctimensec = sbp->st_ctimespec.tv_nsec;
	vsbp->vst_birthtime = sbp->st_birthtimespec.tv_sec;
	vsbp->vst_birthtimensec = sbp->st_birthtimespec.tv_nsec;
	vsbp->vst_size = sbp->st_size;
	vsbp->vst_blocks = sbp->st_blocks;
	vsbp->vst_blksize = sbp->st_blksize;
	vsbp->vst_flags = sbp->st_flags;
	vsbp->vst_gen = sbp->st_gen;
	vsbp->vst_rdev = sbp->st_rdev;
	vsbp->vst_qspare[0] = sbp->st_qspare[0];
	vsbp->vst_qspare[1] = sbp->st_qspare[1];
}

int
proc_pid_rusage(int pid, int flavor, user_addr_t buffer, __unused int32_t *retval)
{
	proc_t          p;
	int             error;
	int             zombie = 0;

	if ((p = proc_find(pid)) == PROC_NULL) {
		if ((p = proc_find_zombref(pid)) == PROC_NULL) {
			return ESRCH;
		}
		zombie = 1;
	}

	/* Do we have permission to look into this? */
	if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDRUSAGE, flavor, CHECK_SAME_USER))) {
		goto out;
	}

	error = proc_get_rusage(p, flavor, buffer, zombie);

out:
	if (zombie) {
		proc_drop_zombref(p);
	} else {
		proc_rele(p);
	}

	return error;
}

void
proc_archinfo(proc_t p, struct proc_archinfo *pai)
{
	proc_lock(p);
	{
		pai->p_cputype = p->p_cputype;
		pai->p_cpusubtype = p->p_cpusubtype;
	}
	proc_unlock(p);
}

void
proc_pidcoalitioninfo(proc_t p, struct proc_pidcoalitioninfo *ppci)
{
	bzero(ppci, sizeof(*ppci));
	proc_coalitionids(p, ppci->coalition_id);
}

int
proc_pidexitreasoninfo(proc_t p, struct proc_exitreasoninfo *peri, struct proc_exitreasonbasicinfo *pberi)
{
	uint32_t reason_data_size = 0;
	int error = 0;
	pid_t selfpid = proc_selfpid();

	proc_lock(p);

	/*
	 * One (and only one) of peri and pberi must be non-NULL.
	 */
	assert((peri != NULL) || (pberi != NULL));
	assert((peri == NULL) || (pberi == NULL));

	/*
	 * Allow access to the parent of the exiting
	 * child or the parent debugger only.
	 */
	do {
		if (p->p_ppid == selfpid) {
			break;  /* parent => ok */
		}
		if ((p->p_lflag & P_LTRACED) != 0 &&
		    (p->p_oppid == selfpid)) {
			break;  /* parent-in-waiting => ok */
		}
		proc_unlock(p);
		return EACCES;
	} while (0);

	if (p->p_exit_reason == OS_REASON_NULL) {
		proc_unlock(p);
		return ENOENT;
	}

	if (p->p_exit_reason->osr_kcd_buf != NULL) {
		reason_data_size = (uint32_t)kcdata_memory_get_used_bytes(&p->p_exit_reason->osr_kcd_descriptor);
	}

	if (peri != NULL) {
		peri->eri_namespace = p->p_exit_reason->osr_namespace;
		peri->eri_code = p->p_exit_reason->osr_code;
		peri->eri_flags = p->p_exit_reason->osr_flags;

		if ((peri->eri_kcd_buf == 0) || (peri->eri_reason_buf_size < reason_data_size)) {
			proc_unlock(p);
			return ENOMEM;
		}

		peri->eri_reason_buf_size = reason_data_size;
		if (reason_data_size != 0) {
			error = copyout(p->p_exit_reason->osr_kcd_buf, (user_addr_t)peri->eri_kcd_buf, reason_data_size);
		}
	} else {
		pberi->beri_namespace =  p->p_exit_reason->osr_namespace;
		pberi->beri_code = p->p_exit_reason->osr_code;
		pberi->beri_flags = p->p_exit_reason->osr_flags;
		pberi->beri_reason_buf_size = reason_data_size;
	}

	proc_unlock(p);

	return error;
}

/*
 * Wrapper to provide NOTE_EXIT_DETAIL and NOTE_EXITSTATUS
 * It mimics the data that is typically captured by the
 * EVFILT_PROC, NOTE_EXIT event mechanism.
 * See filt_proc() in kern_event.c.
 */
int
proc_pidnoteexit(proc_t p, uint64_t flags, uint32_t *data)
{
	uint32_t exit_data = 0;
	uint32_t exit_flags = (uint32_t)flags;

	proc_lock(p);

	/*
	 * Allow access to the parent of the exiting
	 * child or the parent debugger only.
	 */
	do {
		pid_t selfpid = proc_selfpid();

		if (p->p_ppid == selfpid) {
			break;  /* parent => ok */
		}
		if ((p->p_lflag & P_LTRACED) != 0 &&
		    (p->p_oppid == selfpid)) {
			break;  /* parent-in-waiting => ok */
		}
		proc_unlock(p);
		return EACCES;
	} while (0);

	if ((exit_flags & NOTE_EXITSTATUS) != 0) {
		/* The signal and exit status */
		exit_data |= (p->p_xstat & NOTE_PDATAMASK);
	}

	if ((exit_flags & NOTE_EXIT_DETAIL) != 0) {
		/* The exit detail */
		if ((p->p_lflag & P_LTERM_DECRYPTFAIL) != 0) {
			exit_data |= NOTE_EXIT_DECRYPTFAIL;
		}

		if ((p->p_lflag & P_LTERM_JETSAM) != 0) {
			exit_data |= NOTE_EXIT_MEMORY;

			switch (p->p_lflag & P_JETSAM_MASK) {
			case P_JETSAM_VMPAGESHORTAGE:
				exit_data |= NOTE_EXIT_MEMORY_VMPAGESHORTAGE;
				break;
			case P_JETSAM_VMTHRASHING:
				exit_data |= NOTE_EXIT_MEMORY_VMTHRASHING;
				break;
			case P_JETSAM_FCTHRASHING:
				exit_data |= NOTE_EXIT_MEMORY_FCTHRASHING;
				break;
			case P_JETSAM_VNODE:
				exit_data |= NOTE_EXIT_MEMORY_VNODE;
				break;
			case P_JETSAM_HIWAT:
				exit_data |= NOTE_EXIT_MEMORY_HIWAT;
				break;
			case P_JETSAM_PID:
				exit_data |= NOTE_EXIT_MEMORY_PID;
				break;
			case P_JETSAM_IDLEEXIT:
				exit_data |= NOTE_EXIT_MEMORY_IDLE;
				break;
			}
		}

		if ((proc_getcsflags(p) & CS_KILLED) != 0) {
			exit_data |= NOTE_EXIT_CSERROR;
		}
	}

	proc_unlock(p);

	*data = exit_data;

	return 0;
}

int
proc_piddynkqueueinfo(int pid, int flavor, kqueue_id_t kq_id,
    user_addr_t ubuf, uint32_t bufsize, int32_t *retval)
{
	proc_t p;
	int err;

	if (ubuf == USER_ADDR_NULL) {
		return EFAULT;
	}

	p = proc_find(pid);
	if (p == PROC_NULL) {
		return ESRCH;
	}

	err = proc_security_policy(p, PROC_INFO_CALL_PIDDYNKQUEUEINFO, 0, CHECK_SAME_USER);
	if (err) {
		goto out;
	}

	switch (flavor) {
	case PROC_PIDDYNKQUEUE_INFO:
		err = kevent_copyout_dynkqinfo(p, kq_id, ubuf, bufsize, retval);
		break;
	case PROC_PIDDYNKQUEUE_EXTINFO:
		err = kevent_copyout_dynkqextinfo(p, kq_id, ubuf, bufsize, retval);
		break;
	default:
		err = ENOTSUP;
		break;
	}

out:
	proc_rele(p);

	return err;
}

#if CONFIG_PROC_UDATA_STORAGE
int
proc_udata_info(int pid, int flavor, user_addr_t buffer, uint32_t bufsize, int32_t *retval)
{
	int err = 0;
	proc_t p;

	p = proc_find(pid);
	if (p == PROC_NULL) {
		return ESRCH;
	}

	/*
	 * Only support calls against oneself for the moment.
	 */
	if (proc_getpid(p) != proc_selfpid()) {
		err = EACCES;
		goto out;
	}

	if (bufsize != sizeof(p->p_user_data)) {
		err = EINVAL;
		goto out;
	}

	switch (flavor) {
	case PROC_UDATA_INFO_SET:
		err = copyin(buffer, &p->p_user_data, sizeof(p->p_user_data));
		break;
	case PROC_UDATA_INFO_GET:
		err = copyout(&p->p_user_data, buffer, sizeof(p->p_user_data));
		break;
	default:
		err = ENOTSUP;
		break;
	}

out:
	proc_rele(p);

	if (err == 0) {
		*retval = 0;
	}

	return err;
}
#endif /* CONFIG_PROC_UDATA_STORAGE */