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 | /* * Copyright (c) 2004-2021 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@ */ #include <stdarg.h> #include <sys/param.h> #include <sys/systm.h> #include <sys/event.h> // for kqueue related stuff #include <sys/fsevents.h> #if CONFIG_FSE #include <sys/namei.h> #include <sys/filedesc.h> #include <sys/kernel.h> #include <sys/file_internal.h> #include <sys/stat.h> #include <sys/vnode_internal.h> #include <sys/mount_internal.h> #include <sys/proc_internal.h> #include <sys/kauth.h> #include <sys/uio.h> #include <kern/kalloc.h> #include <sys/dirent.h> #include <sys/attr.h> #include <sys/sysctl.h> #include <sys/ubc.h> #include <machine/cons.h> #include <miscfs/specfs/specdev.h> #include <miscfs/devfs/devfs.h> #include <sys/filio.h> #include <kern/locks.h> #include <libkern/OSAtomic.h> #include <kern/zalloc.h> #include <mach/mach_time.h> #include <kern/thread_call.h> #include <kern/clock.h> #include <IOKit/IOBSD.h> #include <security/audit/audit.h> #include <bsm/audit_kevents.h> #include <pexpert/pexpert.h> #include <libkern/section_keywords.h> typedef struct kfs_event { LIST_ENTRY(kfs_event) kevent_list; uint64_t abstime; // when this event happened (mach_absolute_time()) int16_t type; // type code of this event uint16_t flags; // per-event flags int32_t refcount; // number of clients referencing this pid_t pid; int32_t spare; union { struct regular_event { // This must match the layout of fse_info // exactly, except for the "nlink" field is // not included here. See copy_out_kfse() // for all of the sordid details, and also // the _Static_assert() statements below. ino64_t ino; dev_t dev; int32_t mode; uid_t uid; uint32_t document_id; struct kfs_event *dest; // if this is a two-file op const char *str; uint16_t len; } regular_event; struct { ino64_t src_ino; ino64_t dst_ino; uint64_t docid; dev_t dev; } docid_event; struct { audit_token_t audit_token; const char *str; uint16_t len; } access_granted_event; }; } kfs_event; _Static_assert(offsetof(struct regular_event, ino) == offsetof(fse_info, ino), "kfs_event and fse_info out-of-sync"); _Static_assert(offsetof(struct regular_event, dev) == offsetof(fse_info, dev), "kfs_event and fse_info out-of-sync"); _Static_assert(offsetof(struct regular_event, mode) == offsetof(fse_info, mode), "kfs_event and fse_info out-of-sync"); _Static_assert(offsetof(struct regular_event, uid) == offsetof(fse_info, uid), "kfs_event and fse_info out-of-sync"); _Static_assert(offsetof(struct regular_event, document_id) == offsetof(fse_info, document_id), "kfs_event and fse_info out-of-sync"); #define KFSE_INFO_COPYSIZE offsetof(fse_info, nlink) // flags for the flags field #define KFSE_COMBINED_EVENTS 0x0001 #define KFSE_CONTAINS_DROPPED_EVENTS 0x0002 #define KFSE_ON_LIST 0x0004 #define KFSE_BEING_CREATED 0x0008 LIST_HEAD(kfse_list, kfs_event) kfse_list_head = LIST_HEAD_INITIALIZER(x); int num_events_outstanding = 0; int num_pending_rename = 0; struct fsevent_handle; typedef struct fs_event_watcher { int8_t *event_list; // the events we're interested in int32_t num_events; dev_t *devices_not_to_watch;// report events from devices not in this list uint32_t num_devices; int32_t flags; kfs_event **event_queue; int32_t eventq_size; // number of event pointers in queue int32_t num_readers; int32_t rd; // read index into the event_queue int32_t wr; // write index into the event_queue int32_t blockers; int32_t my_id; uint32_t num_dropped; uint64_t max_event_id; struct fsevent_handle *fseh; pid_t pid; char proc_name[(2 * MAXCOMLEN) + 1]; } fs_event_watcher; // fs_event_watcher flags #define WATCHER_DROPPED_EVENTS 0x0001 #define WATCHER_CLOSING 0x0002 #define WATCHER_WANTS_COMPACT_EVENTS 0x0004 #define WATCHER_WANTS_EXTENDED_INFO 0x0008 #define WATCHER_APPLE_SYSTEM_SERVICE 0x0010 // fseventsd, coreservicesd, mds, revisiond #define MAX_WATCHERS 8 static fs_event_watcher *watcher_table[MAX_WATCHERS]; #define DEFAULT_MAX_KFS_EVENTS 4096 static int max_kfs_events = DEFAULT_MAX_KFS_EVENTS; // we allocate kfs_event structures out of this zone static zone_t event_zone; static int fs_event_init = 0; // // this array records whether anyone is interested in a // particular type of event. if no one is, we bail out // early from the event delivery // static int16_t fs_event_type_watchers[FSE_MAX_EVENTS]; // the device currently being unmounted: static dev_t fsevent_unmount_dev = 0; // how many ACKs are still outstanding: static int fsevent_unmount_ack_count = 0; static int watcher_add_event(fs_event_watcher *watcher, kfs_event *kfse); static void fsevents_wakeup(fs_event_watcher *watcher); // // Locks // static LCK_ATTR_DECLARE(fsevent_lock_attr, 0, 0); static LCK_GRP_DECLARE(fsevent_mutex_group, "fsevent-mutex"); static LCK_GRP_DECLARE(fsevent_rw_group, "fsevent-rw"); static LCK_RW_DECLARE_ATTR(event_handling_lock, // handles locking for event manipulation and recycling &fsevent_rw_group, &fsevent_lock_attr); static LCK_MTX_DECLARE_ATTR(watch_table_lock, &fsevent_mutex_group, &fsevent_lock_attr); static LCK_MTX_DECLARE_ATTR(event_buf_lock, &fsevent_mutex_group, &fsevent_lock_attr); static LCK_MTX_DECLARE_ATTR(event_writer_lock, &fsevent_mutex_group, &fsevent_lock_attr); /* Explicitly declare qsort so compiler doesn't complain */ __private_extern__ void qsort( void * array, size_t nmembers, size_t member_size, int (*)(const void *, const void *)); static int is_ignored_directory(const char *path) { if (!path) { return 0; } #define IS_TLD(x) strnstr(__DECONST(char *, path), x, MAXPATHLEN) if (IS_TLD("/.Spotlight-V100/") || IS_TLD("/.MobileBackups/") || IS_TLD("/Backups.backupdb/")) { return 1; } #undef IS_TLD return 0; } static void fsevents_internal_init(void) { int i; if (fs_event_init++ != 0) { return; } for (i = 0; i < FSE_MAX_EVENTS; i++) { fs_event_type_watchers[i] = 0; } memset(watcher_table, 0, sizeof(watcher_table)); PE_get_default("kern.maxkfsevents", &max_kfs_events, sizeof(max_kfs_events)); event_zone = zone_create_ext("fs-event-buf", sizeof(kfs_event), ZC_NOGC | ZC_NOCALLOUT, ZONE_ID_ANY, ^(zone_t z) { // mark the zone as exhaustible so that it will not // ever grow beyond what we initially filled it with zone_set_exhaustible(z, max_kfs_events, /* exhausts */ true); }); zone_fill_initially(event_zone, max_kfs_events); } static void lock_watch_table(void) { lck_mtx_lock(&watch_table_lock); } static void unlock_watch_table(void) { lck_mtx_unlock(&watch_table_lock); } static void lock_fs_event_list(void) { lck_mtx_lock(&event_buf_lock); } static void unlock_fs_event_list(void) { lck_mtx_unlock(&event_buf_lock); } // forward prototype static void release_event_ref(kfs_event *kfse); static boolean_t watcher_cares_about_dev(fs_event_watcher *watcher, dev_t dev) { unsigned int i; // if devices_not_to_watch is NULL then we care about all // events from all devices if (watcher->devices_not_to_watch == NULL) { return true; } for (i = 0; i < watcher->num_devices; i++) { if (dev == watcher->devices_not_to_watch[i]) { // found a match! that means we do not // want events from this device. return false; } } // if we're here it's not in the devices_not_to_watch[] // list so that means we do care about it return true; } int need_fsevent(int type, vnode_t vp) { if (type >= 0 && type < FSE_MAX_EVENTS && fs_event_type_watchers[type] == 0) { return 0; } // events in /dev aren't really interesting... if (vp->v_tag == VT_DEVFS) { return 0; } return 1; } #define is_throw_away(x) ((x) == FSE_STAT_CHANGED || (x) == FSE_CONTENT_MODIFIED) int num_dropped = 0; static struct timeval last_print; // // These variables are used to track coalescing multiple identical // events for the same vnode/pathname. If we get the same event // type and same vnode/pathname as the previous event, we just drop // the event since it's superfluous. This improves some micro- // benchmarks considerably and actually has a real-world impact on // tests like a Finder copy where multiple stat-changed events can // get coalesced. // static int last_event_type = -1; static void *last_ptr = NULL; static char last_str[MAXPATHLEN]; static int last_nlen = 0; static int last_vid = -1; static uint64_t last_coalesced_time = 0; static void *last_event_ptr = NULL; static pid_t last_pid = -1; int last_coalesced = 0; static mach_timebase_info_data_t sTimebaseInfo = { 0, 0 }; #define MAX_HARDLINK_NOTIFICATIONS 128 static inline void kfse_init(kfs_event *kfse, int type, uint64_t time, proc_t p) { memset(kfse, 0, sizeof(*kfse)); kfse->refcount = 1; kfse->type = (int16_t)type; kfse->abstime = time; kfse->pid = proc_getpid(p); OSBitOrAtomic16(KFSE_BEING_CREATED, &kfse->flags); } int add_fsevent(int type, vfs_context_t ctx, ...) { struct proc *p = vfs_context_proc(ctx); int i, arg_type, ret; kfs_event *kfse, *kfse_dest = NULL, *cur; fs_event_watcher *watcher; va_list ap; int error = 0, did_alloc = 0; int64_t orig_linkcount = -1; dev_t dev = 0; uint64_t now, elapsed; uint64_t orig_linkid = 0, next_linkid = 0; uint64_t link_parentid = 0; char *pathbuff = NULL, *path_override = NULL; char *link_name = NULL; vnode_t link_vp = NULL; int pathbuff_len = 0; uthread_t ut = get_bsdthread_info(current_thread()); bool do_all_links = true; bool do_cache_reset = false; if (type == FSE_CONTENT_MODIFIED_NO_HLINK) { do_all_links = false; type = FSE_CONTENT_MODIFIED; } restart: va_start(ap, ctx); // ignore bogus event types.. if (type < 0 || type >= FSE_MAX_EVENTS) { return EINVAL; } // if no one cares about this type of event, bail out if (fs_event_type_watchers[type] == 0) { va_end(ap); return 0; } now = mach_absolute_time(); // find a free event and snag it for our use // NOTE: do not do anything that would block until // the lock is dropped. lock_fs_event_list(); // // check if this event is identical to the previous one... // (as long as it's not an event type that can never be the // same as a previous event) // if (path_override == NULL && type != FSE_CREATE_FILE && type != FSE_DELETE && type != FSE_RENAME && type != FSE_EXCHANGE && type != FSE_CHOWN && type != FSE_DOCID_CHANGED && type != FSE_DOCID_CREATED && type != FSE_CLONE && // don't coalesce FSE_ACCESS_GRANTED because it could // have been granted to a different process. type != FSE_ACCESS_GRANTED) { void *ptr = NULL; int vid = 0, was_str = 0, nlen = 0; for (arg_type = va_arg(ap, int32_t); arg_type != FSE_ARG_DONE; arg_type = va_arg(ap, int32_t)) { switch (arg_type) { case FSE_ARG_VNODE: { ptr = va_arg(ap, void *); vid = vnode_vid((struct vnode *)ptr); last_str[0] = '\0'; break; } case FSE_ARG_STRING: { nlen = va_arg(ap, int32_t); ptr = va_arg(ap, void *); was_str = 1; break; } } if (ptr != NULL) { break; } } if (sTimebaseInfo.denom == 0) { (void) clock_timebase_info(&sTimebaseInfo); } elapsed = (now - last_coalesced_time); if (sTimebaseInfo.denom != sTimebaseInfo.numer) { if (sTimebaseInfo.denom == 1) { elapsed *= sTimebaseInfo.numer; } else { // this could overflow... the worst that will happen is that we'll // send (or not send) an extra event so I'm not going to worry about // doing the math right like dtrace_abs_to_nano() does. elapsed = (elapsed * sTimebaseInfo.numer) / (uint64_t)sTimebaseInfo.denom; } } if (type == last_event_type && (elapsed < 1000000000) && (last_pid == proc_getpid(p)) && ((vid && vid == last_vid && last_ptr == ptr) || (last_str[0] && last_nlen == nlen && ptr && strcmp(last_str, ptr) == 0)) ) { last_coalesced++; unlock_fs_event_list(); va_end(ap); return 0; } else { last_ptr = ptr; if (ptr && was_str) { strlcpy(last_str, ptr, sizeof(last_str)); } last_nlen = nlen; last_vid = vid; last_event_type = type; last_coalesced_time = now; last_pid = proc_getpid(p); } } va_start(ap, ctx); kfse = zalloc_noblock(event_zone); if (kfse && (type == FSE_RENAME || type == FSE_EXCHANGE || type == FSE_CLONE)) { kfse_dest = zalloc_noblock(event_zone); if (kfse_dest == NULL) { did_alloc = 1; zfree(event_zone, kfse); kfse = NULL; } } if (kfse == NULL) { // yikes! no free events unlock_fs_event_list(); lock_watch_table(); for (i = 0; i < MAX_WATCHERS; i++) { watcher = watcher_table[i]; if (watcher == NULL) { continue; } watcher->flags |= WATCHER_DROPPED_EVENTS; fsevents_wakeup(watcher); } unlock_watch_table(); { struct timeval current_tv; num_dropped++; // only print a message at most once every 5 seconds microuptime(¤t_tv); if ((current_tv.tv_sec - last_print.tv_sec) > 10) { int ii; void *junkptr = zalloc_noblock(event_zone), *listhead = kfse_list_head.lh_first; printf("add_fsevent: event queue is full! dropping events (num dropped events: %d; num events outstanding: %d).\n", num_dropped, num_events_outstanding); printf("add_fsevent: kfse_list head %p ; num_pending_rename %d\n", listhead, num_pending_rename); printf("add_fsevent: zalloc sez: %p\n", junkptr); printf("add_fsevent: event_zone info: %d 0x%x\n", ((int *)event_zone)[0], ((int *)event_zone)[1]); lock_watch_table(); for (ii = 0; ii < MAX_WATCHERS; ii++) { if (watcher_table[ii] == NULL) { continue; } printf("add_fsevent: watcher %s %p: rd %4d wr %4d q_size %4d flags 0x%x\n", watcher_table[ii]->proc_name, watcher_table[ii], watcher_table[ii]->rd, watcher_table[ii]->wr, watcher_table[ii]->eventq_size, watcher_table[ii]->flags); } unlock_watch_table(); last_print = current_tv; if (junkptr) { zfree(event_zone, junkptr); } } } if (pathbuff) { release_pathbuff(pathbuff); pathbuff = NULL; } return ENOSPC; } kfse_init(kfse, type, now, p); last_event_ptr = kfse; if (type == FSE_RENAME || type == FSE_EXCHANGE || type == FSE_CLONE) { kfse_init(kfse_dest, type, now, p); kfse->regular_event.dest = kfse_dest; } num_events_outstanding++; if (kfse->type == FSE_RENAME) { num_pending_rename++; } LIST_INSERT_HEAD(&kfse_list_head, kfse, kevent_list); OSBitOrAtomic16(KFSE_ON_LIST, &kfse->flags); if (kfse->refcount < 1) { panic("add_fsevent: line %d: kfse recount %d but should be at least 1", __LINE__, kfse->refcount); } unlock_fs_event_list(); // at this point it's safe to unlock // // now process the arguments passed in and copy them into // the kfse // cur = kfse; if (type == FSE_DOCID_CREATED || type == FSE_DOCID_CHANGED) { // // These events are special and not like the other events. // They only have a dev_t, src inode #, dest inode #, and // a doc-id (va_arg'd to us in that order). If we don't // get one of them, then the error-check filler will // catch it. // do_all_links = false; arg_type = va_arg(ap, int32_t); if (arg_type == FSE_ARG_DEV) { cur->docid_event.dev = (dev_t)(va_arg(ap, dev_t)); } arg_type = va_arg(ap, int32_t); if (arg_type == FSE_ARG_INO) { cur->docid_event.src_ino = (ino64_t)(va_arg(ap, ino64_t)); } arg_type = va_arg(ap, int32_t); if (arg_type == FSE_ARG_INO) { cur->docid_event.dst_ino = (ino64_t)(va_arg(ap, ino64_t)); } arg_type = va_arg(ap, int32_t); if (arg_type == FSE_ARG_INT32) { cur->docid_event.docid = (uint64_t)va_arg(ap, uint32_t); } else if (arg_type == FSE_ARG_INT64) { cur->docid_event.docid = (uint64_t)va_arg(ap, uint64_t); } goto done_with_args; } if (type == FSE_ACCESS_GRANTED) { // // This one is also different. We get a path string // and (maybe) and audit token. If we don't get the // audit token, we extract is from the vfs_context_t. // audit_token_t *atokenp = NULL; vnode_t vp = NULL; char *path_str = NULL; size_t path_strlen = 0; void *arg; int32_t len32; do_all_links = false; while ((arg_type = va_arg(ap, int32_t)) != FSE_ARG_DONE) { switch (arg_type) { case FSE_ARG_STRING: len32 = va_arg(ap, int32_t); arg = va_arg(ap, char *); if (path_str == NULL) { path_str = arg; path_strlen = len32; } break; case FSE_ARG_PATH: arg = va_arg(ap, char *); if (path_str == NULL) { path_str = arg; } break; case FSE_ARG_VNODE: arg = va_arg(ap, vnode_t); if (vp == NULL) { vp = arg; } break; case FSE_ARG_AUDIT_TOKEN: arg = va_arg(ap, audit_token_t *); if (atokenp == NULL) { atokenp = arg; } break; default: printf("add_fsevent: FSE_ACCESS_GRANTED unknown type %d\n", arg_type); // just skip one 32-bit word and hope we // sync up... (void)va_arg(ap, int32_t); } } if (atokenp != NULL) { memcpy(&cur->access_granted_event.audit_token, atokenp, sizeof(cur->access_granted_event.audit_token)); } else if (vfs_context_copy_audit_token(ctx, &cur->access_granted_event.audit_token) != 0) { OSBitOrAtomic16(KFSE_CONTAINS_DROPPED_EVENTS, &cur->flags); goto done_with_args; } // // If we got FSE_ARG_STRING, the length includes the // terminating NUL. If we got FSE_ARG_PATH, all we // got was the string pointer, so get the length and // adjust. If we didn't get either, then the caller // needs to have provided us with a vnode, and with // that we can get the path. // if (path_str != NULL) { if (path_strlen == 0) { path_strlen = strlen(path_str) + 1; } } else if (vp != NULL) { pathbuff = get_pathbuff(); pathbuff_len = MAXPATHLEN; pathbuff[0] = '\0'; if (vn_getpath_no_firmlink(vp, pathbuff, &pathbuff_len) == 0) { path_str = pathbuff; path_strlen = pathbuff_len; } } if (path_str != NULL) { assert(path_strlen <= INT16_MAX); cur->access_granted_event.str = vfs_addname(path_str, (uint32_t)path_strlen, 0, 0); if (path_str == pathbuff) { release_pathbuff(pathbuff); pathbuff = NULL; } } if (cur->access_granted_event.str == NULL) { OSBitOrAtomic16(KFSE_CONTAINS_DROPPED_EVENTS, &cur->flags); } goto done_with_args; } if (type == FSE_UNMOUNT_PENDING) { // Just a dev_t // We use the same fields as the regular event, but we // don't have all of the data. do_all_links = false; arg_type = va_arg(ap, int32_t); if (arg_type == FSE_ARG_DEV) { cur->regular_event.dev = (dev_t)(va_arg(ap, dev_t)); } cur->regular_event.dest = NULL; cur->regular_event.str = NULL; cur->regular_event.len = 0; goto done_with_args; } for (arg_type = va_arg(ap, int32_t); arg_type != FSE_ARG_DONE; arg_type = va_arg(ap, int32_t)) { switch (arg_type) { case FSE_ARG_VNODE: { // this expands out into multiple arguments to the client struct vnode *vp; struct vnode_attr va; if (kfse->regular_event.str != NULL) { cur = kfse_dest; } vp = va_arg(ap, struct vnode *); if (vp == NULL) { panic("add_fsevent: you can't pass me a NULL vnode ptr (type %d)!", cur->type); } VATTR_INIT(&va); VATTR_WANTED(&va, va_fsid); VATTR_WANTED(&va, va_fileid); VATTR_WANTED(&va, va_mode); VATTR_WANTED(&va, va_uid); VATTR_WANTED(&va, va_document_id); VATTR_WANTED(&va, va_nlink); if ((ret = vnode_getattr(vp, &va, vfs_context_kernel())) != 0) { // printf("add_fsevent: failed to getattr on vp %p (%d)\n", cur->fref.vp, ret); cur->regular_event.str = NULL; error = EINVAL; goto clean_up; } cur->regular_event.dev = dev = (dev_t)va.va_fsid; cur->regular_event.ino = (ino64_t)va.va_fileid; cur->regular_event.mode = (int32_t)vnode_vttoif(vnode_vtype(vp)) | va.va_mode; cur->regular_event.uid = va.va_uid; cur->regular_event.document_id = va.va_document_id; if (vp->v_flag & VISHARDLINK) { cur->regular_event.mode |= FSE_MODE_HLINK; if ((vp->v_type == VDIR && va.va_dirlinkcount == 0) || (vp->v_type == VREG && va.va_nlink == 0)) { cur->regular_event.mode |= FSE_MODE_LAST_HLINK; } if (orig_linkid == 0) { orig_linkid = cur->regular_event.ino; orig_linkcount = MIN(va.va_nlink, MAX_HARDLINK_NOTIFICATIONS); link_vp = vp; if (vp->v_mount->mnt_kern_flag & MNTK_PATH_FROM_ID && !link_name) { VATTR_INIT(&va); VATTR_WANTED(&va, va_parentid); VATTR_WANTED(&va, va_name); link_name = zalloc(ZV_NAMEI); va.va_name = link_name; if ((ret = vnode_getattr(vp, &va, vfs_context_kernel()) != 0) || !(VATTR_IS_SUPPORTED(&va, va_name)) || !(VATTR_IS_SUPPORTED(&va, va_parentid))) { zfree(ZV_NAMEI, link_name); link_name = NULL; } if (link_name) { link_parentid = va.va_parentid; } va.va_name = NULL; } } } // if we haven't gotten the path yet, get it. if (pathbuff == NULL && path_override == NULL) { pathbuff = get_pathbuff(); pathbuff_len = MAXPATHLEN; pathbuff[0] = '\0'; if ((ret = vn_getpath_no_firmlink(vp, pathbuff, &pathbuff_len)) != 0 || pathbuff[0] == '\0') { OSBitOrAtomic16(KFSE_CONTAINS_DROPPED_EVENTS, &cur->flags); do { if (vp->v_parent != NULL) { vp = vp->v_parent; } else if (vp->v_mount) { strlcpy(pathbuff, vp->v_mount->mnt_vfsstat.f_mntonname, MAXPATHLEN); break; } else { vp = NULL; } if (vp == NULL) { break; } pathbuff_len = MAXPATHLEN; ret = vn_getpath_no_firmlink(vp, pathbuff, &pathbuff_len); } while (ret == ENOSPC); if (ret != 0 || vp == NULL) { error = ENOENT; goto clean_up; } } } else if (path_override) { pathbuff = path_override; pathbuff_len = (int)strlen(path_override) + 1; } else { strlcpy(pathbuff, "NOPATH", MAXPATHLEN); pathbuff_len = (int)strlen(pathbuff) + 1; } // store the path by adding it to the global string table cur->regular_event.len = (u_int16_t)pathbuff_len; cur->regular_event.str = vfs_addname(pathbuff, pathbuff_len, 0, 0); if (cur->regular_event.str == NULL || cur->regular_event.str[0] == '\0') { panic("add_fsevent: was not able to add path %s to event %p.", pathbuff, cur); } if (pathbuff != path_override) { release_pathbuff(pathbuff); } pathbuff = NULL; break; } case FSE_ARG_FINFO: { fse_info *fse; fse = va_arg(ap, fse_info *); cur->regular_event.dev = dev = (dev_t)fse->dev; cur->regular_event.ino = (ino64_t)fse->ino; cur->regular_event.mode = (int32_t)fse->mode; cur->regular_event.uid = (uid_t)fse->uid; cur->regular_event.document_id = (uint32_t)fse->document_id; // if it's a hard-link and this is the last link, flag it if (fse->mode & FSE_MODE_HLINK) { if (fse->nlink == 0) { cur->regular_event.mode |= FSE_MODE_LAST_HLINK; } if (orig_linkid == 0) { orig_linkid = cur->regular_event.ino; orig_linkcount = MIN(fse->nlink, MAX_HARDLINK_NOTIFICATIONS); } } if (cur->regular_event.mode & FSE_TRUNCATED_PATH) { OSBitOrAtomic16(KFSE_CONTAINS_DROPPED_EVENTS, &cur->flags); cur->regular_event.mode &= ~FSE_TRUNCATED_PATH; } break; } case FSE_ARG_STRING: if (kfse->regular_event.str != NULL) { cur = kfse_dest; } cur->regular_event.len = (int16_t)(va_arg(ap, int32_t) & 0x7fff); if (cur->regular_event.len >= 1) { cur->regular_event.str = vfs_addname(va_arg(ap, char *), cur->regular_event.len, 0, 0); } else { printf("add_fsevent: funny looking string length: %d\n", (int)cur->regular_event.len); cur->regular_event.len = 2; cur->regular_event.str = vfs_addname("/", cur->regular_event.len, 0, 0); } if (cur->regular_event.str[0] == 0) { printf("add_fsevent: bogus looking string (len %d)\n", cur->regular_event.len); } break; case FSE_ARG_INT32: { uint32_t ival = (uint32_t)va_arg(ap, int32_t); kfse->regular_event.uid = ival; break; } default: printf("add_fsevent: unknown type %d\n", arg_type); // just skip one 32-bit word and hope we sync up... (void)va_arg(ap, int32_t); } } done_with_args: va_end(ap); // XXX Memory barrier here? if (kfse_dest) { OSBitAndAtomic16(~KFSE_BEING_CREATED, &kfse_dest->flags); } OSBitAndAtomic16(~KFSE_BEING_CREATED, &kfse->flags); // // now we have to go and let everyone know that // is interested in this type of event // lock_watch_table(); for (i = 0; i < MAX_WATCHERS; i++) { watcher = watcher_table[i]; if (watcher == NULL) { continue; } if (type < watcher->num_events && watcher->event_list[type] == FSE_REPORT && watcher_cares_about_dev(watcher, dev)) { if (watcher_add_event(watcher, kfse) != 0) { watcher->num_dropped++; continue; } } // if (kfse->refcount < 1) { // panic("add_fsevent: line %d: kfse recount %d but should be at least 1", __LINE__, kfse->refcount); // } } unlock_watch_table(); clean_up: if (pathbuff) { release_pathbuff(pathbuff); pathbuff = NULL; } // replicate events for sibling hardlinks if (do_all_links && (kfse->regular_event.mode & FSE_MODE_HLINK) && !(kfse->regular_event.mode & FSE_MODE_LAST_HLINK) && (type == FSE_STAT_CHANGED || type == FSE_CONTENT_MODIFIED || type == FSE_FINDER_INFO_CHANGED || type == FSE_XATTR_MODIFIED)) { if (orig_linkcount > 0 && orig_linkid != 0) { #ifndef APFSIOC_NEXT_LINK #define APFSIOC_NEXT_LINK _IOWR('J', 10, uint64_t) #endif if (path_override == NULL) { path_override = get_pathbuff(); } if (next_linkid == 0) { next_linkid = orig_linkid; } if (link_vp) { mount_t mp = NULL; vnode_t mnt_rootvp = NULL; int iret = -1; mp = vnode_mount(link_vp); if (mp) { iret = VFS_ROOT(mp, &mnt_rootvp, vfs_context_kernel()); } if (iret == 0 && mnt_rootvp) { iret = VNOP_IOCTL(mnt_rootvp, APFSIOC_NEXT_LINK, (char *)&next_linkid, (int)0, vfs_context_kernel()); vnode_put(mnt_rootvp); } int32_t fsid0; int path_override_len = MAXPATHLEN; // continue resolving hardlink paths if there is a valid next_linkid retrieved // file systems not supporting APFSIOC_NEXT_LINK will skip replicating events for sibling hardlinks if (iret == 0 && next_linkid != 0) { fsid0 = link_vp->v_mount->mnt_vfsstat.f_fsid.val[0]; ut->uu_flag |= UT_KERN_RAGE_VNODES; if (!do_cache_reset) { do_cache_reset = true; } if ((iret = fsgetpath_internal(ctx, fsid0, next_linkid, MAXPATHLEN, path_override, FSOPT_NOFIRMLINKPATH, &path_override_len)) == 0) { orig_linkcount--; ut->uu_flag &= ~UT_KERN_RAGE_VNODES; if (orig_linkcount >= 0) { release_event_ref(kfse); goto restart; } } else { // failed to get override path // encountered a broken link or the linkid has been deleted before retrieving the path orig_linkcount--; ut->uu_flag &= ~UT_KERN_RAGE_VNODES; if (orig_linkcount >= 0) { goto clean_up; } } } } } } if (link_name) { /* * If we call fsgetpath on all the links, it will set the link origin cache * to the last link that the path was obtained for. * To restore the the original link id cache in APFS we need to issue a * lookup on the original directory + name for the link. */ if (do_cache_reset) { vnode_t dvp = NULLVP; if ((ret = VFS_VGET(link_vp->v_mount, (ino64_t)link_parentid, &dvp, vfs_context_kernel())) == 0) { vnode_t lvp = NULLVP; ret = vnode_lookupat(link_name, 0, &lvp, ctx, dvp); if (!ret) { vnode_put(lvp); lvp = NULLVP; } vnode_put(dvp); dvp = NULLVP; } ret = 0; } zfree(ZV_NAMEI, link_name); link_name = NULL; } if (path_override) { release_pathbuff(path_override); path_override = NULL; } release_event_ref(kfse); return error; } int test_fse_access_granted(vnode_t vp, unsigned long type, vfs_context_t ctx) { audit_token_t atoken; char *pathbuff; int error, pathbuff_len; if (type == 0) { return add_fsevent(FSE_ACCESS_GRANTED, ctx, FSE_ARG_VNODE, vp, FSE_ARG_DONE); } if (type == 1) { error = vfs_context_copy_audit_token(ctx, &atoken); if (error) { return error; } return add_fsevent(FSE_ACCESS_GRANTED, ctx, FSE_ARG_VNODE, vp, FSE_ARG_AUDIT_TOKEN, &atoken, FSE_ARG_DONE); } if (type == 2 || type == 3) { pathbuff = get_pathbuff(); pathbuff_len = MAXPATHLEN; pathbuff[0] = '\0'; error = vn_getpath_no_firmlink(vp, pathbuff, &pathbuff_len); if (error) { release_pathbuff(pathbuff); return error; } if (type == 2) { error = add_fsevent(FSE_ACCESS_GRANTED, ctx, FSE_ARG_STRING, pathbuff_len, pathbuff, FSE_ARG_DONE); } else { error = add_fsevent(FSE_ACCESS_GRANTED, ctx, FSE_ARG_PATH, pathbuff, FSE_ARG_DONE); } release_pathbuff(pathbuff); return error; } return ENOTSUP; } static void release_event_ref(kfs_event *kfse) { int old_refcount; kfs_event *dest = NULL; const char *path_str = NULL, *dest_path_str = NULL; lock_fs_event_list(); old_refcount = OSAddAtomic(-1, &kfse->refcount); if (old_refcount > 1) { unlock_fs_event_list(); return; } if (last_event_ptr == kfse) { last_event_ptr = NULL; last_event_type = -1; last_coalesced_time = 0; } if (kfse->refcount < 0) { panic("release_event_ref: bogus kfse refcount %d", kfse->refcount); } assert(kfse->refcount == 0); assert(kfse->type != FSE_INVALID); // // Get pointers to all the things so we can free without // holding any locks. // if (kfse->type != FSE_DOCID_CREATED && kfse->type != FSE_DOCID_CHANGED && kfse->type != FSE_ACCESS_GRANTED) { path_str = kfse->regular_event.str; dest = kfse->regular_event.dest; if (dest != NULL) { assert(dest->type != FSE_INVALID); if (OSAddAtomic(-1, &kfse->regular_event.dest->refcount) == 1) { dest_path_str = dest->regular_event.str; } else { dest = NULL; } } } if (dest != NULL) { if (dest->flags & KFSE_ON_LIST) { num_events_outstanding--; LIST_REMOVE(dest, kevent_list); } } if (kfse->flags & KFSE_ON_LIST) { num_events_outstanding--; LIST_REMOVE(kfse, kevent_list); if (kfse->type == FSE_RENAME) { num_pending_rename--; } } unlock_fs_event_list(); zfree(event_zone, kfse); if (dest != NULL) { zfree(event_zone, dest); } if (path_str != NULL) { vfs_removename(path_str); } if (dest_path_str != NULL) { vfs_removename(dest_path_str); } } #define FSEVENTS_WATCHER_ENTITLEMENT \ "com.apple.private.vfs.fsevents-watcher" // // We restrict this for two reasons: // // 1- So that naive processes don't get this firehose by default. // // 2- Because this event, when delivered to watcheres, includes the // audit token of the process granted the access, and we don't // want to leak that to random watchers. // #define FSEVENTS_ACCESS_GRANTED_WATCHER_ENTITLEMENT \ "com.apple.private.vfs.fsevents-access-granted-watcher" static bool watcher_is_entitled(task_t task) { // // We consider a process to be entitled to watch /dev/fsevents // if it has either FSEVENTS_WATCHER_ENTITLEMENT or // FSEVENTS_ACCESS_GRANTED_WATCHER_ENTITLEMENT. // return !!(IOTaskHasEntitlement(task, FSEVENTS_WATCHER_ENTITLEMENT) || IOTaskHasEntitlement(task, FSEVENTS_ACCESS_GRANTED_WATCHER_ENTITLEMENT)); } static bool watcher_is_entitled_for_access_granted(task_t task) { return !!IOTaskHasEntitlement(task, FSEVENTS_ACCESS_GRANTED_WATCHER_ENTITLEMENT); } static int add_watcher(int8_t *event_list, int32_t num_events, int32_t eventq_size, fs_event_watcher **watcher_out, void *fseh) { int i; fs_event_watcher *watcher; if (eventq_size <= 0 || eventq_size > 100 * max_kfs_events) { eventq_size = max_kfs_events; } // If the watcher wants FSE_ACCESS_GRANTED, ensure it has the // correct entitlement. If not, just silently drop that event. if (num_events > FSE_ACCESS_GRANTED && event_list[FSE_ACCESS_GRANTED] != FSE_IGNORE && !watcher_is_entitled_for_access_granted(current_task())) { event_list[FSE_ACCESS_GRANTED] = FSE_IGNORE; } // Note: the event_queue follows the fs_event_watcher struct // in memory so we only have to do one allocation watcher = kalloc_type(fs_event_watcher, kfs_event *, eventq_size, Z_WAITOK); if (watcher == NULL) { return ENOMEM; } watcher->event_list = event_list; watcher->num_events = num_events; watcher->devices_not_to_watch = NULL; watcher->num_devices = 0; watcher->flags = 0; watcher->event_queue = (kfs_event **)&watcher[1]; watcher->eventq_size = eventq_size; watcher->rd = 0; watcher->wr = 0; watcher->blockers = 0; watcher->num_readers = 0; watcher->max_event_id = 0; watcher->fseh = fseh; watcher->pid = proc_selfpid(); proc_selfname(watcher->proc_name, sizeof(watcher->proc_name)); watcher->num_dropped = 0; // XXXdbg - debugging if (watcher_is_entitled(current_task())) { watcher->flags |= WATCHER_APPLE_SYSTEM_SERVICE; } else { printf("fsevents: watcher %s (pid: %d) - Using /dev/fsevents directly is unsupported. Migrate to FSEventsFramework\n", watcher->proc_name, watcher->pid); } lock_watch_table(); // find a slot for the new watcher for (i = 0; i < MAX_WATCHERS; i++) { if (watcher_table[i] == NULL) { watcher->my_id = i; watcher_table[i] = watcher; break; } } if (i >= MAX_WATCHERS) { printf("fsevents: too many watchers!\n"); unlock_watch_table(); kfree_type(fs_event_watcher, kfs_event *, watcher->eventq_size, watcher); return ENOSPC; } // now update the global list of who's interested in // events of a particular type... for (i = 0; i < num_events; i++) { if (event_list[i] != FSE_IGNORE && i < FSE_MAX_EVENTS) { fs_event_type_watchers[i]++; } } unlock_watch_table(); *watcher_out = watcher; return 0; } static void remove_watcher(fs_event_watcher *target) { int i, j, counter = 0; fs_event_watcher *watcher; kfs_event *kfse; lock_watch_table(); for (j = 0; j < MAX_WATCHERS; j++) { watcher = watcher_table[j]; if (watcher != target) { continue; } watcher_table[j] = NULL; for (i = 0; i < watcher->num_events; i++) { if (watcher->event_list[i] != FSE_IGNORE && i < FSE_MAX_EVENTS) { fs_event_type_watchers[i]--; } } if (watcher->flags & WATCHER_CLOSING) { unlock_watch_table(); return; } // printf("fsevents: removing watcher %p (rd %d wr %d num_readers %d flags 0x%x)\n", watcher, watcher->rd, watcher->wr, watcher->num_readers, watcher->flags); watcher->flags |= WATCHER_CLOSING; OSAddAtomic(1, &watcher->num_readers); unlock_watch_table(); while (watcher->num_readers > 1 && counter++ < 5000) { lock_watch_table(); fsevents_wakeup(watcher); // in case they're asleep unlock_watch_table(); tsleep(watcher, PRIBIO, "fsevents-close", 1); } if (counter++ >= 5000) { // printf("fsevents: close: still have readers! (%d)\n", watcher->num_readers); panic("fsevents: close: still have readers! (%d)", watcher->num_readers); } // drain the event_queue lck_rw_lock_exclusive(&event_handling_lock); while (watcher->rd != watcher->wr) { kfse = watcher->event_queue[watcher->rd]; watcher->event_queue[watcher->rd] = NULL; watcher->rd = (watcher->rd + 1) % watcher->eventq_size; OSSynchronizeIO(); if (kfse != NULL && kfse->type != FSE_INVALID && kfse->refcount >= 1) { release_event_ref(kfse); } } lck_rw_unlock_exclusive(&event_handling_lock); kfree_data(watcher->event_list, watcher->num_events * sizeof(int8_t)); kfree_data(watcher->devices_not_to_watch, watcher->num_devices * sizeof(dev_t)); kfree_type(fs_event_watcher, kfs_event *, watcher->eventq_size, watcher); return; } unlock_watch_table(); } #define EVENT_DELAY_IN_MS 10 static thread_call_t event_delivery_timer = NULL; static int timer_set = 0; static void delayed_event_delivery(__unused void *param0, __unused void *param1) { int i; lock_watch_table(); for (i = 0; i < MAX_WATCHERS; i++) { if (watcher_table[i] != NULL && watcher_table[i]->rd != watcher_table[i]->wr) { fsevents_wakeup(watcher_table[i]); } } timer_set = 0; unlock_watch_table(); } // // The watch table must be locked before calling this function. // static void schedule_event_wakeup(void) { uint64_t deadline; if (event_delivery_timer == NULL) { event_delivery_timer = thread_call_allocate((thread_call_func_t)delayed_event_delivery, NULL); } clock_interval_to_deadline(EVENT_DELAY_IN_MS, 1000 * 1000, &deadline); thread_call_enter_delayed(event_delivery_timer, deadline); timer_set = 1; } #define MAX_NUM_PENDING 16 // // NOTE: the watch table must be locked before calling // this routine. // static int watcher_add_event(fs_event_watcher *watcher, kfs_event *kfse) { if (kfse->abstime > watcher->max_event_id) { watcher->max_event_id = kfse->abstime; } if (((watcher->wr + 1) % watcher->eventq_size) == watcher->rd) { watcher->flags |= WATCHER_DROPPED_EVENTS; fsevents_wakeup(watcher); return ENOSPC; } OSAddAtomic(1, &kfse->refcount); watcher->event_queue[watcher->wr] = kfse; OSSynchronizeIO(); watcher->wr = (watcher->wr + 1) % watcher->eventq_size; // // wake up the watcher if there are more than MAX_NUM_PENDING events. // otherwise schedule a timer (if one isn't already set) which will // send any pending events if no more are received in the next // EVENT_DELAY_IN_MS milli-seconds. // int32_t num_pending = 0; if (watcher->rd < watcher->wr) { num_pending = watcher->wr - watcher->rd; } if (watcher->rd > watcher->wr) { num_pending = watcher->wr + watcher->eventq_size - watcher->rd; } if (num_pending > (watcher->eventq_size * 3 / 4) && !(watcher->flags & WATCHER_APPLE_SYSTEM_SERVICE)) { /* Non-Apple Service is falling behind, start dropping events for this process */ lck_rw_lock_exclusive(&event_handling_lock); while (watcher->rd != watcher->wr) { kfse = watcher->event_queue[watcher->rd]; watcher->event_queue[watcher->rd] = NULL; watcher->rd = (watcher->rd + 1) % watcher->eventq_size; OSSynchronizeIO(); if (kfse != NULL && kfse->type != FSE_INVALID && kfse->refcount >= 1) { release_event_ref(kfse); } } watcher->flags |= WATCHER_DROPPED_EVENTS; lck_rw_unlock_exclusive(&event_handling_lock); printf("fsevents: watcher falling behind: %s (pid: %d) rd: %4d wr: %4d q_size: %4d flags: 0x%x\n", watcher->proc_name, watcher->pid, watcher->rd, watcher->wr, watcher->eventq_size, watcher->flags); fsevents_wakeup(watcher); } else if (num_pending > MAX_NUM_PENDING) { fsevents_wakeup(watcher); } else if (timer_set == 0) { schedule_event_wakeup(); } return 0; } static int fill_buff(uint16_t type, int32_t size, const void *data, char *buff, int32_t *_buff_idx, int32_t buff_sz, struct uio *uio) { int32_t amt, error = 0, buff_idx = *_buff_idx; uint16_t tmp; // // the +1 on the size is to guarantee that the main data // copy loop will always copy at least 1 byte // if ((buff_sz - buff_idx) <= (int)(2 * sizeof(uint16_t) + 1)) { if (buff_idx > uio_resid(uio)) { error = ENOSPC; goto get_out; } error = uiomove(buff, buff_idx, uio); if (error) { goto get_out; } buff_idx = 0; } // copy out the header (type & size) memcpy(&buff[buff_idx], &type, sizeof(uint16_t)); buff_idx += sizeof(uint16_t); tmp = size & 0xffff; memcpy(&buff[buff_idx], &tmp, sizeof(uint16_t)); buff_idx += sizeof(uint16_t); // now copy the body of the data, flushing along the way // if the buffer fills up. // while (size > 0) { amt = (size < (buff_sz - buff_idx)) ? size : (buff_sz - buff_idx); memcpy(&buff[buff_idx], data, amt); size -= amt; buff_idx += amt; data = (const char *)data + amt; if (size > (buff_sz - buff_idx)) { if (buff_idx > uio_resid(uio)) { error = ENOSPC; goto get_out; } error = uiomove(buff, buff_idx, uio); if (error) { goto get_out; } buff_idx = 0; } if (amt == 0) { // just in case... break; } } get_out: *_buff_idx = buff_idx; return error; } static int copy_out_kfse(fs_event_watcher *watcher, kfs_event *kfse, struct uio *uio) __attribute__((noinline)); static int copy_out_kfse(fs_event_watcher *watcher, kfs_event *kfse, struct uio *uio) { int error; uint16_t tmp16; int32_t type; kfs_event *cur; char evbuff[512]; int evbuff_idx = 0; if (kfse->type == FSE_INVALID) { panic("fsevents: copy_out_kfse: asked to copy out an invalid event (kfse %p, refcount %d)", kfse, kfse->refcount); } if (kfse->flags & KFSE_BEING_CREATED) { return 0; } if (((kfse->type == FSE_RENAME) || (kfse->type == FSE_CLONE)) && kfse->regular_event.dest == NULL) { // // This can happen if an event gets recycled but we had a // pointer to it in our event queue. The event is the // destination of a rename or clone which we'll process // separately (that is, another kfse points to this one // so it's ok to skip this guy because we'll process it // when we process the other one) error = 0; goto get_out; } if (watcher->flags & WATCHER_WANTS_EXTENDED_INFO) { type = (kfse->type & 0xfff); if (kfse->flags & KFSE_CONTAINS_DROPPED_EVENTS) { type |= (FSE_CONTAINS_DROPPED_EVENTS << FSE_FLAG_SHIFT); } else if (kfse->flags & KFSE_COMBINED_EVENTS) { type |= (FSE_COMBINED_EVENTS << FSE_FLAG_SHIFT); } } else { type = (int32_t)kfse->type; } // copy out the type of the event memcpy(evbuff, &type, sizeof(int32_t)); evbuff_idx += sizeof(int32_t); // copy out the pid of the person that generated the event memcpy(&evbuff[evbuff_idx], &kfse->pid, sizeof(pid_t)); evbuff_idx += sizeof(pid_t); cur = kfse; copy_again: if (kfse->type == FSE_DOCID_CHANGED || kfse->type == FSE_DOCID_CREATED) { dev_t dev = cur->docid_event.dev; ino64_t src_ino = cur->docid_event.src_ino; ino64_t dst_ino = cur->docid_event.dst_ino; uint64_t docid = cur->docid_event.docid; error = fill_buff(FSE_ARG_DEV, sizeof(dev_t), &dev, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } error = fill_buff(FSE_ARG_INO, sizeof(ino64_t), &src_ino, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } error = fill_buff(FSE_ARG_INO, sizeof(ino64_t), &dst_ino, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } error = fill_buff(FSE_ARG_INT64, sizeof(uint64_t), &docid, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } goto done; } if (kfse->type == FSE_UNMOUNT_PENDING) { dev_t dev = cur->regular_event.dev; error = fill_buff(FSE_ARG_DEV, sizeof(dev_t), &dev, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } goto done; } if (kfse->type == FSE_ACCESS_GRANTED) { // // KFSE_CONTAINS_DROPPED_EVENTS will be set if either // the path or audit token are bogus; don't copy out // either in that case. // if (cur->flags & KFSE_CONTAINS_DROPPED_EVENTS) { goto done; } error = fill_buff(FSE_ARG_STRING, cur->access_granted_event.len, cur->access_granted_event.str, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } error = fill_buff(FSE_ARG_AUDIT_TOKEN, sizeof(cur->access_granted_event.audit_token), &cur->access_granted_event.audit_token, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } goto done; } if (cur->regular_event.str == NULL || cur->regular_event.str[0] == '\0') { printf("copy_out_kfse:2: empty/short path (%s)\n", cur->regular_event.str); error = fill_buff(FSE_ARG_STRING, 2, "/", evbuff, &evbuff_idx, sizeof(evbuff), uio); } else { error = fill_buff(FSE_ARG_STRING, cur->regular_event.len, cur->regular_event.str, evbuff, &evbuff_idx, sizeof(evbuff), uio); } if (error != 0) { goto get_out; } if (cur->regular_event.dev == 0 && cur->regular_event.ino == 0) { // this happens when a rename event happens and the // destination of the rename did not previously exist. // it thus has no other file info so skip copying out // the stuff below since it isn't initialized goto done; } if (watcher->flags & WATCHER_WANTS_COMPACT_EVENTS) { // We rely on the layout of the "regular_event" // structure being the same as fse_info in order // to speed up this copy. The nlink field in // fse_info is not included. error = fill_buff(FSE_ARG_FINFO, KFSE_INFO_COPYSIZE, &cur->regular_event, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } } else { error = fill_buff(FSE_ARG_DEV, sizeof(dev_t), &cur->regular_event.dev, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } error = fill_buff(FSE_ARG_INO, sizeof(ino64_t), &cur->regular_event.ino, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } error = fill_buff(FSE_ARG_MODE, sizeof(int32_t), &cur->regular_event.mode, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } error = fill_buff(FSE_ARG_UID, sizeof(uid_t), &cur->regular_event.uid, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } error = fill_buff(FSE_ARG_GID, sizeof(gid_t), &cur->regular_event.document_id, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } } if (cur->regular_event.dest) { cur = cur->regular_event.dest; goto copy_again; } done: // very last thing: the time stamp error = fill_buff(FSE_ARG_INT64, sizeof(uint64_t), &cur->abstime, evbuff, &evbuff_idx, sizeof(evbuff), uio); if (error != 0) { goto get_out; } // check if the FSE_ARG_DONE will fit if (sizeof(uint16_t) > sizeof(evbuff) - evbuff_idx) { if (evbuff_idx > uio_resid(uio)) { error = ENOSPC; goto get_out; } error = uiomove(evbuff, evbuff_idx, uio); if (error) { goto get_out; } evbuff_idx = 0; } tmp16 = FSE_ARG_DONE; memcpy(&evbuff[evbuff_idx], &tmp16, sizeof(uint16_t)); evbuff_idx += sizeof(uint16_t); // flush any remaining data in the buffer (and hopefully // in most cases this is the only uiomove we'll do) if (evbuff_idx > uio_resid(uio)) { error = ENOSPC; } else { error = uiomove(evbuff, evbuff_idx, uio); } get_out: return error; } static int fmod_watch(fs_event_watcher *watcher, struct uio *uio) { int error = 0; user_ssize_t last_full_event_resid; kfs_event *kfse; uint16_t tmp16; int skipped; last_full_event_resid = uio_resid(uio); // need at least 2048 bytes of space (maxpathlen + 1 event buf) if (uio_resid(uio) < 2048 || watcher == NULL) { return EINVAL; } if (watcher->flags & WATCHER_CLOSING) { return 0; } if (OSAddAtomic(1, &watcher->num_readers) != 0) { // don't allow multiple threads to read from the fd at the same time OSAddAtomic(-1, &watcher->num_readers); return EAGAIN; } restart_watch: if (watcher->rd == watcher->wr) { if (watcher->flags & WATCHER_CLOSING) { OSAddAtomic(-1, &watcher->num_readers); return 0; } OSAddAtomic(1, &watcher->blockers); // there's nothing to do, go to sleep error = tsleep((caddr_t)watcher, PUSER | PCATCH, "fsevents_empty", 0); OSAddAtomic(-1, &watcher->blockers); if (error != 0 || (watcher->flags & WATCHER_CLOSING)) { OSAddAtomic(-1, &watcher->num_readers); return error; } } // if we dropped events, return that as an event first if (watcher->flags & WATCHER_DROPPED_EVENTS) { int32_t val = FSE_EVENTS_DROPPED; error = uiomove((caddr_t)&val, sizeof(int32_t), uio); if (error == 0) { val = 0; // a fake pid error = uiomove((caddr_t)&val, sizeof(int32_t), uio); tmp16 = FSE_ARG_DONE; // makes it a consistent msg error = uiomove((caddr_t)&tmp16, sizeof(int16_t), uio); last_full_event_resid = uio_resid(uio); } if (error) { OSAddAtomic(-1, &watcher->num_readers); return error; } watcher->flags &= ~WATCHER_DROPPED_EVENTS; } skipped = 0; lck_rw_lock_shared(&event_handling_lock); while (uio_resid(uio) > 0 && watcher->rd != watcher->wr) { if (watcher->flags & WATCHER_CLOSING) { break; } // // check if the event is something of interest to us // (since it may have been recycled/reused and changed // its type or which device it is for) // kfse = watcher->event_queue[watcher->rd]; if (!kfse || kfse->type == FSE_INVALID || kfse->type >= watcher->num_events || kfse->refcount < 1) { break; } if (watcher->event_list[kfse->type] == FSE_REPORT) { if (!(watcher->flags & WATCHER_APPLE_SYSTEM_SERVICE) && kfse->type != FSE_DOCID_CREATED && kfse->type != FSE_DOCID_CHANGED && kfse->type != FSE_ACCESS_GRANTED && is_ignored_directory(kfse->regular_event.str)) { // If this is not an Apple System Service, skip specified directories // radar://12034844 error = 0; skipped = 1; } else { skipped = 0; if (last_event_ptr == kfse) { last_event_ptr = NULL; last_event_type = -1; last_coalesced_time = 0; } error = copy_out_kfse(watcher, kfse, uio); if (error != 0) { // if an event won't fit or encountered an error while // we were copying it out, then backup to the last full // event and just bail out. if the error was ENOENT // then we can continue regular processing, otherwise // we should unlock things and return. uio_setresid(uio, last_full_event_resid); if (error != ENOENT) { lck_rw_unlock_shared(&event_handling_lock); error = 0; goto get_out; } } last_full_event_resid = uio_resid(uio); } } watcher->event_queue[watcher->rd] = NULL; watcher->rd = (watcher->rd + 1) % watcher->eventq_size; OSSynchronizeIO(); release_event_ref(kfse); } lck_rw_unlock_shared(&event_handling_lock); if (skipped && error == 0) { goto restart_watch; } get_out: OSAddAtomic(-1, &watcher->num_readers); return error; } // // Shoo watchers away from a volume that's about to be unmounted // (so that it can be cleanly unmounted). // void fsevent_unmount(__unused struct mount *mp, __unused vfs_context_t ctx) { #if !defined(XNU_TARGET_OS_OSX) dev_t dev = mp->mnt_vfsstat.f_fsid.val[0]; int error, waitcount = 0; struct timespec ts = {.tv_sec = 1, .tv_nsec = 0}; // wait for any other pending unmounts to complete lock_watch_table(); while (fsevent_unmount_dev != 0) { error = msleep((caddr_t)&fsevent_unmount_dev, &watch_table_lock, PRIBIO, "fsevent_unmount_wait", &ts); if (error == EWOULDBLOCK) { error = 0; } if (!error && (++waitcount >= 10)) { error = EWOULDBLOCK; printf("timeout waiting to signal unmount pending for dev %d (fsevent_unmount_dev %d)\n", dev, fsevent_unmount_dev); } if (error) { // there's a problem, bail out unlock_watch_table(); return; } } if (fs_event_type_watchers[FSE_UNMOUNT_PENDING] == 0) { // nobody watching for unmount pending events unlock_watch_table(); return; } // this is now the current unmount pending fsevent_unmount_dev = dev; fsevent_unmount_ack_count = fs_event_type_watchers[FSE_UNMOUNT_PENDING]; unlock_watch_table(); // send an event to notify the watcher they need to get off the mount error = add_fsevent(FSE_UNMOUNT_PENDING, ctx, FSE_ARG_DEV, dev, FSE_ARG_DONE); // wait for acknowledgment(s) (give up if it takes too long) lock_watch_table(); waitcount = 0; while (fsevent_unmount_dev == dev) { error = msleep((caddr_t)&fsevent_unmount_dev, &watch_table_lock, PRIBIO, "fsevent_unmount_pending", &ts); if (error == EWOULDBLOCK) { error = 0; } if (!error && (++waitcount >= 10)) { error = EWOULDBLOCK; printf("unmount pending ack timeout for dev %d\n", dev); } if (error) { // there's a problem, bail out if (fsevent_unmount_dev == dev) { fsevent_unmount_dev = 0; fsevent_unmount_ack_count = 0; } wakeup((caddr_t)&fsevent_unmount_dev); break; } } unlock_watch_table(); #endif /* ! XNU_TARGET_OS_OSX */ } // // /dev/fsevents device code // static int fsevents_installed = 0; typedef struct fsevent_handle { UInt32 flags; SInt32 active; fs_event_watcher *watcher; struct klist knotes; struct selinfo si; } fsevent_handle; #define FSEH_CLOSING 0x0001 static int fseventsf_read(struct fileproc *fp, struct uio *uio, __unused int flags, __unused vfs_context_t ctx) { fsevent_handle *fseh = (struct fsevent_handle *)fp_get_data(fp); int error; error = fmod_watch(fseh->watcher, uio); return error; } #pragma pack(push, 4) typedef struct fsevent_dev_filter_args32 { uint32_t num_devices; user32_addr_t devices; } fsevent_dev_filter_args32; typedef struct fsevent_dev_filter_args64 { uint32_t num_devices; user64_addr_t devices; } fsevent_dev_filter_args64; #pragma pack(pop) #define FSEVENTS_DEVICE_FILTER_32 _IOW('s', 100, fsevent_dev_filter_args32) #define FSEVENTS_DEVICE_FILTER_64 _IOW('s', 100, fsevent_dev_filter_args64) static int fseventsf_ioctl(struct fileproc *fp, u_long cmd, caddr_t data, vfs_context_t ctx) { fsevent_handle *fseh = (struct fsevent_handle *)fp_get_data(fp); int ret = 0; fsevent_dev_filter_args64 *devfilt_args, _devfilt_args; OSAddAtomic(1, &fseh->active); if (fseh->flags & FSEH_CLOSING) { OSAddAtomic(-1, &fseh->active); return 0; } switch (cmd) { case FIONBIO: case FIOASYNC: break; case FSEVENTS_WANT_COMPACT_EVENTS: { fseh->watcher->flags |= WATCHER_WANTS_COMPACT_EVENTS; break; } case FSEVENTS_WANT_EXTENDED_INFO: { fseh->watcher->flags |= WATCHER_WANTS_EXTENDED_INFO; break; } case FSEVENTS_GET_CURRENT_ID: { *(uint64_t *)data = fseh->watcher->max_event_id; ret = 0; break; } case FSEVENTS_DEVICE_FILTER_32: { if (proc_is64bit(vfs_context_proc(ctx))) { ret = EINVAL; break; } fsevent_dev_filter_args32 *devfilt_args32 = (fsevent_dev_filter_args32 *)data; devfilt_args = &_devfilt_args; memset(devfilt_args, 0, sizeof(fsevent_dev_filter_args64)); devfilt_args->num_devices = devfilt_args32->num_devices; devfilt_args->devices = CAST_USER_ADDR_T(devfilt_args32->devices); goto handle_dev_filter; } case FSEVENTS_DEVICE_FILTER_64: if (!proc_is64bit(vfs_context_proc(ctx))) { ret = EINVAL; break; } devfilt_args = (fsevent_dev_filter_args64 *)data; handle_dev_filter: { int new_num_devices, old_num_devices = 0; dev_t *devices_not_to_watch, *tmp = NULL; if (devfilt_args->num_devices > 256) { ret = EINVAL; break; } new_num_devices = devfilt_args->num_devices; if (new_num_devices == 0) { lock_watch_table(); tmp = fseh->watcher->devices_not_to_watch; fseh->watcher->devices_not_to_watch = NULL; old_num_devices = fseh->watcher->num_devices; fseh->watcher->num_devices = new_num_devices; unlock_watch_table(); kfree_data(tmp, old_num_devices * sizeof(dev_t)); break; } devices_not_to_watch = kalloc_data(new_num_devices * sizeof(dev_t), Z_WAITOK); if (devices_not_to_watch == NULL) { ret = ENOMEM; break; } ret = copyin((user_addr_t)devfilt_args->devices, (void *)devices_not_to_watch, new_num_devices * sizeof(dev_t)); if (ret) { kfree_data(devices_not_to_watch, new_num_devices * sizeof(dev_t)); break; } lock_watch_table(); old_num_devices = fseh->watcher->num_devices; fseh->watcher->num_devices = new_num_devices; tmp = fseh->watcher->devices_not_to_watch; fseh->watcher->devices_not_to_watch = devices_not_to_watch; unlock_watch_table(); kfree_data(tmp, old_num_devices * sizeof(dev_t)); break; } case FSEVENTS_UNMOUNT_PENDING_ACK: { lock_watch_table(); dev_t dev = *(dev_t *)data; if (fsevent_unmount_dev == dev) { if (--fsevent_unmount_ack_count <= 0) { fsevent_unmount_dev = 0; wakeup((caddr_t)&fsevent_unmount_dev); } } else { printf("unexpected unmount pending ack %d (%d)\n", dev, fsevent_unmount_dev); ret = EINVAL; } unlock_watch_table(); break; } default: ret = EINVAL; break; } OSAddAtomic(-1, &fseh->active); return ret; } static int fseventsf_select(struct fileproc *fp, int which, __unused void *wql, vfs_context_t ctx) { fsevent_handle *fseh = (struct fsevent_handle *)fp_get_data(fp); int ready = 0; if ((which != FREAD) || (fseh->watcher->flags & WATCHER_CLOSING)) { return 0; } // if there's nothing in the queue, we're not ready if (fseh->watcher->rd != fseh->watcher->wr) { ready = 1; } if (!ready) { lock_watch_table(); selrecord(vfs_context_proc(ctx), &fseh->si, wql); unlock_watch_table(); } return ready; } #if NOTUSED static int fseventsf_stat(__unused struct fileproc *fp, __unused struct stat *sb, __unused vfs_context_t ctx) { return ENOTSUP; } #endif static int fseventsf_close(struct fileglob *fg, __unused vfs_context_t ctx) { fsevent_handle *fseh = (struct fsevent_handle *)fg_get_data(fg); fs_event_watcher *watcher; OSBitOrAtomic(FSEH_CLOSING, &fseh->flags); while (OSAddAtomic(0, &fseh->active) > 0) { tsleep((caddr_t)fseh->watcher, PRIBIO, "fsevents-close", 1); } watcher = fseh->watcher; fg_set_data(fg, NULL); fseh->watcher = NULL; remove_watcher(watcher); selthreadclear(&fseh->si); kfree_type(fsevent_handle, fseh); return 0; } static void filt_fsevent_detach(struct knote *kn) { fsevent_handle *fseh = (struct fsevent_handle *)knote_kn_hook_get_raw(kn); lock_watch_table(); KNOTE_DETACH(&fseh->knotes, kn); unlock_watch_table(); } /* * Determine whether this knote should be active * * This is kind of subtle. * --First, notice if the vnode has been revoked: in so, override hint * --EVFILT_READ knotes are checked no matter what the hint is * --Other knotes activate based on hint. * --If hint is revoke, set special flags and activate */ static int filt_fsevent_common(struct knote *kn, struct kevent_qos_s *kev, long hint) { fsevent_handle *fseh = (struct fsevent_handle *)knote_kn_hook_get_raw(kn); int activate = 0; int32_t rd, wr, amt; int64_t data = 0; if (NOTE_REVOKE == hint) { kn->kn_flags |= (EV_EOF | EV_ONESHOT); activate = 1; } rd = fseh->watcher->rd; wr = fseh->watcher->wr; if (rd <= wr) { amt = wr - rd; } else { amt = fseh->watcher->eventq_size - (rd - wr); } switch (kn->kn_filter) { case EVFILT_READ: data = amt; activate = (data != 0); break; case EVFILT_VNODE: /* Check events this note matches against the hint */ if (kn->kn_sfflags & hint) { kn->kn_fflags |= (uint32_t)hint; /* Set which event occurred */ } if (kn->kn_fflags != 0) { activate = 1; } break; default: // nothing to do... break; } if (activate && kev) { knote_fill_kevent(kn, kev, data); } return activate; } static int filt_fsevent(struct knote *kn, long hint) { return filt_fsevent_common(kn, NULL, hint); } static int filt_fsevent_touch(struct knote *kn, struct kevent_qos_s *kev) { int res; lock_watch_table(); /* accept new fflags/data as saved */ kn->kn_sfflags = kev->fflags; kn->kn_sdata = kev->data; /* restrict the current results to the (smaller?) set of new interest */ /* * For compatibility with previous implementations, we leave kn_fflags * as they were before. */ //kn->kn_fflags &= kev->fflags; /* determine if the filter is now fired */ res = filt_fsevent_common(kn, NULL, 0); unlock_watch_table(); return res; } static int filt_fsevent_process(struct knote *kn, struct kevent_qos_s *kev) { int res; lock_watch_table(); res = filt_fsevent_common(kn, kev, 0); unlock_watch_table(); return res; } SECURITY_READ_ONLY_EARLY(struct filterops) fsevent_filtops = { .f_isfd = 1, .f_attach = NULL, .f_detach = filt_fsevent_detach, .f_event = filt_fsevent, .f_touch = filt_fsevent_touch, .f_process = filt_fsevent_process, }; static int fseventsf_kqfilter(struct fileproc *fp, struct knote *kn, __unused struct kevent_qos_s *kev) { fsevent_handle *fseh = (struct fsevent_handle *)fp_get_data(fp); int res; kn->kn_filtid = EVFILTID_FSEVENT; knote_kn_hook_set_raw(kn, (void *) fseh); lock_watch_table(); KNOTE_ATTACH(&fseh->knotes, kn); /* check to see if it is fired already */ res = filt_fsevent_common(kn, NULL, 0); unlock_watch_table(); return res; } static int fseventsf_drain(struct fileproc *fp, __unused vfs_context_t ctx) { int counter = 0; fsevent_handle *fseh = (struct fsevent_handle *)fp_get_data(fp); // if there are people still waiting, sleep for 10ms to // let them clean up and get out of there. however we // also don't want to get stuck forever so if they don't // exit after 5 seconds we're tearing things down anyway. while (fseh->watcher->blockers && counter++ < 500) { // issue wakeup in case anyone is blocked waiting for an event // do this each time we wakeup in case the blocker missed // the wakeup due to the unprotected test of WATCHER_CLOSING // and decision to tsleep in fmod_watch... this bit of // latency is a decent tradeoff against not having to // take and drop a lock in fmod_watch lock_watch_table(); fsevents_wakeup(fseh->watcher); unlock_watch_table(); tsleep((caddr_t)fseh->watcher, PRIBIO, "watcher-close", 1); } return 0; } static int fseventsopen(__unused dev_t dev, __unused int flag, __unused int mode, __unused struct proc *p) { if (!kauth_cred_issuser(kauth_cred_get())) { return EPERM; } return 0; } static int fseventsclose(__unused dev_t dev, __unused int flag, __unused int mode, __unused struct proc *p) { return 0; } static int fseventsread(__unused dev_t dev, __unused struct uio *uio, __unused int ioflag) { return EIO; } static int parse_buffer_and_add_events(const char *buffer, size_t bufsize, vfs_context_t ctx, size_t *remainder) { const fse_info *finfo, *dest_finfo; const char *path, *ptr, *dest_path, *event_start = buffer; size_t path_len, dest_path_len; int type, err = 0; ptr = buffer; while ((ptr + sizeof(int) + sizeof(fse_info) + 1) < buffer + bufsize) { type = *(const int *)ptr; if (type < 0 || type == FSE_ACCESS_GRANTED || type >= FSE_MAX_EVENTS) { err = EINVAL; break; } ptr += sizeof(int); finfo = (const fse_info *)ptr; ptr += sizeof(fse_info); path = ptr; while (ptr < buffer + bufsize && *ptr != '\0') { ptr++; } if (ptr >= buffer + bufsize) { break; } ptr++; // advance over the trailing '\0' path_len = ptr - path; if (type != FSE_RENAME && type != FSE_EXCHANGE && type != FSE_CLONE) { event_start = ptr; // record where the next event starts err = add_fsevent(type, ctx, FSE_ARG_STRING, path_len, path, FSE_ARG_FINFO, finfo, FSE_ARG_DONE); if (err) { break; } continue; } // // if we're here we have to slurp up the destination finfo // and path so that we can pass them to the add_fsevent() // call. basically it's a copy of the above code. // dest_finfo = (const fse_info *)ptr; ptr += sizeof(fse_info); dest_path = ptr; while (ptr < buffer + bufsize && *ptr != '\0') { ptr++; } if (ptr >= buffer + bufsize) { break; } ptr++; // advance over the trailing '\0' event_start = ptr; // record where the next event starts dest_path_len = ptr - dest_path; // // If the destination inode number is non-zero, generate a rename // with both source and destination FSE_ARG_FINFO. Otherwise generate // a rename with only one FSE_ARG_FINFO. If you need to inject an // exchange with an inode of zero, just make that inode (and its path) // come in as the first one, not the second. // if (dest_finfo->ino) { err = add_fsevent(type, ctx, FSE_ARG_STRING, path_len, path, FSE_ARG_FINFO, finfo, FSE_ARG_STRING, dest_path_len, dest_path, FSE_ARG_FINFO, dest_finfo, FSE_ARG_DONE); } else { err = add_fsevent(type, ctx, FSE_ARG_STRING, path_len, path, FSE_ARG_FINFO, finfo, FSE_ARG_STRING, dest_path_len, dest_path, FSE_ARG_DONE); } if (err) { break; } } // if the last event wasn't complete, set the remainder // to be the last event start boundary. // *remainder = (long)((buffer + bufsize) - event_start); return err; } // // Note: this buffer size can not ever be less than // 2*MAXPATHLEN + 2*sizeof(fse_info) + sizeof(int) // because that is the max size for a single event. // I made it 4k to be a "nice" size. making it // smaller is not a good idea. // #define WRITE_BUFFER_SIZE 4096 static char *write_buffer = NULL; static int fseventswrite(__unused dev_t dev, struct uio *uio, __unused int ioflag) { int error = 0; size_t count, offset = 0, remainder = 0; vfs_context_t ctx = vfs_context_current(); lck_mtx_lock(&event_writer_lock); if (write_buffer == NULL) { write_buffer = zalloc_permanent(WRITE_BUFFER_SIZE, ZALIGN_64); } // // this loop copies in and processes the events written. // it takes care to copy in reasonable size chunks and // process them. if there is an event that spans a chunk // boundary we're careful to copy those bytes down to the // beginning of the buffer and read the next chunk in just // after it. // while (uio_resid(uio)) { count = MIN(WRITE_BUFFER_SIZE - offset, (size_t)uio_resid(uio)); error = uiomove(write_buffer + offset, (int)count, uio); if (error) { break; } error = parse_buffer_and_add_events(write_buffer, offset + count, ctx, &remainder); if (error) { break; } // // if there's any remainder, copy it down to the beginning // of the buffer so that it will get processed the next time // through the loop. note that the remainder always starts // at an event boundary. // memmove(write_buffer, (write_buffer + count + offset) - remainder, remainder); offset = remainder; } lck_mtx_unlock(&event_writer_lock); return error; } static const struct fileops fsevents_fops = { .fo_type = DTYPE_FSEVENTS, .fo_read = fseventsf_read, .fo_write = fo_no_write, .fo_ioctl = fseventsf_ioctl, .fo_select = fseventsf_select, .fo_close = fseventsf_close, .fo_kqfilter = fseventsf_kqfilter, .fo_drain = fseventsf_drain, }; typedef struct fsevent_clone_args32 { user32_addr_t event_list; int32_t num_events; int32_t event_queue_depth; user32_addr_t fd; } fsevent_clone_args32; typedef struct fsevent_clone_args64 { user64_addr_t event_list; int32_t num_events; int32_t event_queue_depth; user64_addr_t fd; } fsevent_clone_args64; #define FSEVENTS_CLONE_32 _IOW('s', 1, fsevent_clone_args32) #define FSEVENTS_CLONE_64 _IOW('s', 1, fsevent_clone_args64) static int fseventsioctl(__unused dev_t dev, u_long cmd, caddr_t data, __unused int flag, struct proc *p) { struct fileproc *f; int fd, error; fsevent_handle *fseh = NULL; fsevent_clone_args64 *fse_clone_args, _fse_clone; int8_t *event_list; int is64bit = proc_is64bit(p); switch (cmd) { case FSEVENTS_CLONE_32: { if (is64bit) { return EINVAL; } fsevent_clone_args32 *args32 = (fsevent_clone_args32 *)data; fse_clone_args = &_fse_clone; memset(fse_clone_args, 0, sizeof(fsevent_clone_args64)); fse_clone_args->event_list = CAST_USER_ADDR_T(args32->event_list); fse_clone_args->num_events = args32->num_events; fse_clone_args->event_queue_depth = args32->event_queue_depth; fse_clone_args->fd = CAST_USER_ADDR_T(args32->fd); goto handle_clone; } case FSEVENTS_CLONE_64: if (!is64bit) { return EINVAL; } fse_clone_args = (fsevent_clone_args64 *)data; handle_clone: if (fse_clone_args->num_events <= 0 || fse_clone_args->num_events > 4096) { return EINVAL; } fseh = kalloc_type(fsevent_handle, Z_WAITOK | Z_ZERO | Z_NOFAIL); klist_init(&fseh->knotes); event_list = kalloc_data(fse_clone_args->num_events * sizeof(int8_t), Z_WAITOK); if (event_list == NULL) { kfree_type(fsevent_handle, fseh); return ENOMEM; } error = copyin((user_addr_t)fse_clone_args->event_list, (void *)event_list, fse_clone_args->num_events * sizeof(int8_t)); if (error) { kfree_data(event_list, fse_clone_args->num_events * sizeof(int8_t)); kfree_type(fsevent_handle, fseh); return error; } /* * Lock down the user's "fd" result buffer so it's safe * to hold locks while we copy it out. */ error = vslock((user_addr_t)fse_clone_args->fd, sizeof(int32_t)); if (error) { kfree_data(event_list, fse_clone_args->num_events * sizeof(int8_t)); kfree_type(fsevent_handle, fseh); return error; } error = add_watcher(event_list, fse_clone_args->num_events, fse_clone_args->event_queue_depth, &fseh->watcher, fseh); if (error) { vsunlock((user_addr_t)fse_clone_args->fd, sizeof(int32_t), 0); kfree_data(event_list, fse_clone_args->num_events * sizeof(int8_t)); kfree_type(fsevent_handle, fseh); return error; } fseh->watcher->fseh = fseh; error = falloc(p, &f, &fd, vfs_context_current()); if (error) { remove_watcher(fseh->watcher); vsunlock((user_addr_t)fse_clone_args->fd, sizeof(int32_t), 0); kfree_data(event_list, fse_clone_args->num_events * sizeof(int8_t)); kfree_type(fsevent_handle, fseh); return error; } proc_fdlock(p); f->fp_glob->fg_flag = FREAD | FWRITE; f->fp_glob->fg_ops = &fsevents_fops; fp_set_data(f, fseh); /* * We can safely hold the proc_fdlock across this copyout() * because of the vslock() call above. The vslock() call * also ensures that we will never get an error, so assert * this. */ error = copyout((void *)&fd, (user_addr_t)fse_clone_args->fd, sizeof(int32_t)); assert(error == 0); procfdtbl_releasefd(p, fd, NULL); fp_drop(p, fd, f, 1); proc_fdunlock(p); vsunlock((user_addr_t)fse_clone_args->fd, sizeof(int32_t), 1); break; default: error = EINVAL; break; } return error; } static void fsevents_wakeup(fs_event_watcher *watcher) { selwakeup(&watcher->fseh->si); KNOTE(&watcher->fseh->knotes, NOTE_WRITE | NOTE_NONE); wakeup((caddr_t)watcher); } /* * A struct describing which functions will get invoked for certain * actions. */ static const struct cdevsw fsevents_cdevsw = { .d_open = fseventsopen, .d_close = fseventsclose, .d_read = fseventsread, .d_write = fseventswrite, .d_ioctl = fseventsioctl, .d_stop = eno_stop, .d_reset = eno_reset, .d_select = eno_select, .d_mmap = eno_mmap, .d_strategy = eno_strat, .d_reserved_1 = eno_getc, .d_reserved_2 = eno_putc, }; /* * Called to initialize our device, * and to register ourselves with devfs */ void fsevents_init(void) { int ret; if (fsevents_installed) { return; } fsevents_installed = 1; ret = cdevsw_add(-1, &fsevents_cdevsw); if (ret < 0) { fsevents_installed = 0; return; } devfs_make_node(makedev(ret, 0), DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0644, "fsevents"); fsevents_internal_init(); } char * get_pathbuff(void) { return zalloc(ZV_NAMEI); } void release_pathbuff(char *path) { if (path == NULL) { return; } zfree(ZV_NAMEI, path); } int get_fse_info(struct vnode *vp, fse_info *fse, __unused vfs_context_t ctx) { struct vnode_attr va; VATTR_INIT(&va); VATTR_WANTED(&va, va_fsid); va.va_vaflags |= VA_REALFSID; VATTR_WANTED(&va, va_fileid); VATTR_WANTED(&va, va_mode); VATTR_WANTED(&va, va_uid); VATTR_WANTED(&va, va_document_id); if (vp->v_flag & VISHARDLINK) { if (vp->v_type == VDIR) { VATTR_WANTED(&va, va_dirlinkcount); } else { VATTR_WANTED(&va, va_nlink); } } if (vnode_getattr(vp, &va, vfs_context_kernel()) != 0) { memset(fse, 0, sizeof(fse_info)); return -1; } return vnode_get_fse_info_from_vap(vp, fse, &va); } int vnode_get_fse_info_from_vap(vnode_t vp, fse_info *fse, struct vnode_attr *vap) { fse->ino = (ino64_t)vap->va_fileid; fse->dev = (dev_t)vap->va_fsid; fse->mode = (int32_t)vnode_vttoif(vnode_vtype(vp)) | vap->va_mode; fse->uid = (uid_t)vap->va_uid; fse->document_id = (uint32_t)vap->va_document_id; if (vp->v_flag & VISHARDLINK) { fse->mode |= FSE_MODE_HLINK; if (vp->v_type == VDIR) { fse->nlink = (uint64_t)vap->va_dirlinkcount; } else { fse->nlink = (uint64_t)vap->va_nlink; } } return 0; } void create_fsevent_from_kevent(vnode_t vp, uint32_t kevents, struct vnode_attr *vap) { int fsevent_type = FSE_CONTENT_MODIFIED, len; // the default is the most pessimistic char pathbuf[MAXPATHLEN]; fse_info fse; if (kevents & VNODE_EVENT_DELETE) { fsevent_type = FSE_DELETE; } else if (kevents & (VNODE_EVENT_EXTEND | VNODE_EVENT_WRITE)) { fsevent_type = FSE_CONTENT_MODIFIED; } else if (kevents & VNODE_EVENT_LINK) { fsevent_type = FSE_CREATE_FILE; } else if (kevents & VNODE_EVENT_RENAME) { fsevent_type = FSE_CREATE_FILE; // XXXdbg - should use FSE_RENAME but we don't have the destination info; } else if (kevents & (VNODE_EVENT_FILE_CREATED | VNODE_EVENT_FILE_REMOVED | VNODE_EVENT_DIR_CREATED | VNODE_EVENT_DIR_REMOVED)) { fsevent_type = FSE_STAT_CHANGED; // XXXdbg - because vp is a dir and the thing created/removed lived inside it } else { // a catch all for VNODE_EVENT_PERMS, VNODE_EVENT_ATTRIB and anything else fsevent_type = FSE_STAT_CHANGED; } // printf("convert_kevent: kevents 0x%x fsevent type 0x%x (for %s)\n", kevents, fsevent_type, vp->v_name ? vp->v_name : "(no-name)"); fse.dev = vap->va_fsid; fse.ino = vap->va_fileid; fse.mode = vnode_vttoif(vnode_vtype(vp)) | (uint32_t)vap->va_mode; if (vp->v_flag & VISHARDLINK) { fse.mode |= FSE_MODE_HLINK; if (vp->v_type == VDIR) { fse.nlink = vap->va_dirlinkcount; } else { fse.nlink = vap->va_nlink; } } if (vp->v_type == VDIR) { fse.mode |= FSE_REMOTE_DIR_EVENT; } fse.uid = vap->va_uid; fse.document_id = vap->va_document_id; len = sizeof(pathbuf); if (vn_getpath_no_firmlink(vp, pathbuf, &len) == 0) { add_fsevent(fsevent_type, vfs_context_current(), FSE_ARG_STRING, len, pathbuf, FSE_ARG_FINFO, &fse, FSE_ARG_DONE); } return; } #else /* CONFIG_FSE */ #include <sys/fsevents.h> /* * The get_pathbuff and release_pathbuff routines are used in places not * related to fsevents, and it's a handy abstraction, so define trivial * versions that don't cache a pool of buffers. This way, we don't have * to conditionalize the callers, and they still get the advantage of the * pool of buffers if CONFIG_FSE is turned on. */ char * get_pathbuff(void) { return zalloc(ZV_NAMEI); } void release_pathbuff(char *path) { zfree(ZV_NAMEI, path); } int add_fsevent(__unused int type, __unused vfs_context_t ctx, ...) { return 0; } int need_fsevent(__unused int type, __unused vnode_t vp) { return 0; } #endif /* CONFIG_FSE */ |