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
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.net;
import java.io.EOFException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.nio.channels.CompletionHandler;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import javax.net.ssl.KeyManager;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.jni.Address;
import org.apache.tomcat.jni.Error;
import org.apache.tomcat.jni.File;
import org.apache.tomcat.jni.Library;
import org.apache.tomcat.jni.OS;
import org.apache.tomcat.jni.Poll;
import org.apache.tomcat.jni.Pool;
import org.apache.tomcat.jni.SSL;
import org.apache.tomcat.jni.SSLContext;
import org.apache.tomcat.jni.SSLContext.SNICallBack;
import org.apache.tomcat.jni.SSLSocket;
import org.apache.tomcat.jni.Sockaddr;
import org.apache.tomcat.jni.Socket;
import org.apache.tomcat.jni.Status;
import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.buf.ByteBufferUtils;
import org.apache.tomcat.util.collections.SynchronizedStack;
import org.apache.tomcat.util.compat.JrePlatform;
import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
import org.apache.tomcat.util.net.Acceptor.AcceptorState;
import org.apache.tomcat.util.net.openssl.OpenSSLContext;
import org.apache.tomcat.util.net.openssl.OpenSSLUtil;
/**
* APR tailored thread pool, providing the following services:
* <ul>
* <li>Socket acceptor thread</li>
* <li>Socket poller thread</li>
* <li>Sendfile thread</li>
* <li>Worker threads pool</li>
* </ul>
*
* @author Mladen Turk
* @author Remy Maucherat
*
* @deprecated The APR/Native Connector will be removed in Tomcat 10.1.x
* onwards.
*/
@Deprecated
public class AprEndpoint extends AbstractEndpoint<Long,Long> implements SNICallBack {
// -------------------------------------------------------------- Constants
private static final Log log = LogFactory.getLog(AprEndpoint.class);
// ----------------------------------------------------------------- Fields
/**
* Root APR memory pool.
*/
protected long rootPool = 0;
/**
* Server socket "pointer".
*/
protected volatile long serverSock = 0;
/**
* APR memory pool for the server socket.
*/
protected long serverSockPool = 0;
/**
* SSL context.
*/
protected long sslContext = 0;
private int previousAcceptedPort = -1;
private String previousAcceptedAddress = null;
private long previousAcceptedSocketNanoTime = 0;
// ------------------------------------------------------------ Constructor
public AprEndpoint() {
// Asynchronous IO has significantly lower performance with APR:
// - no IO vectoring
// - mandatory use of direct buffers forces output buffering
// - needs extra output flushes due to buffering
setUseAsyncIO(false);
}
// ------------------------------------------------------------- Properties
/**
* Defer accept.
*/
protected boolean deferAccept = true;
public void setDeferAccept(boolean deferAccept) { this.deferAccept = deferAccept; }
@Override
public boolean getDeferAccept() { return deferAccept; }
private boolean ipv6v6only = false;
public void setIpv6v6only(boolean ipv6v6only) { this.ipv6v6only = ipv6v6only; }
public boolean getIpv6v6only() { return ipv6v6only; }
/**
* Size of the sendfile (= concurrent files which can be served).
*/
protected int sendfileSize = 1 * 1024;
public void setSendfileSize(int sendfileSize) { this.sendfileSize = sendfileSize; }
public int getSendfileSize() { return sendfileSize; }
/**
* Poll interval, in microseconds. The smaller the value, the more CPU the poller
* will use, but the more responsive to activity it will be.
*/
protected int pollTime = 2000;
public int getPollTime() { return pollTime; }
public void setPollTime(int pollTime) { if (pollTime > 0) { this.pollTime = pollTime; } }
/*
* When the endpoint is created and configured, the APR library will not
* have been initialised. This flag is used to determine if the default
* value of useSendFile should be changed if the APR library indicates it
* supports send file once it has been initialised. If useSendFile is set
* by configuration, that configuration will always take priority.
*/
private boolean useSendFileSet = false;
@Override
public void setUseSendfile(boolean useSendfile) {
useSendFileSet = true;
super.setUseSendfile(useSendfile);
}
/*
* For internal use to avoid setting the useSendFileSet flag
*/
private void setUseSendfileInternal(boolean useSendfile) {
super.setUseSendfile(useSendfile);
}
/**
* The socket poller.
*/
protected Poller poller = null;
public Poller getPoller() {
return poller;
}
/**
* The static file sender.
*/
protected Sendfile sendfile = null;
public Sendfile getSendfile() {
return sendfile;
}
@Override
public InetSocketAddress getLocalAddress() throws IOException {
long s = serverSock;
if (s == 0) {
return null;
} else {
long sa;
try {
sa = Address.get(Socket.APR_LOCAL, s);
} catch (IOException ioe) {
// re-throw
throw ioe;
} catch (Exception e) {
// wrap
throw new IOException(e);
}
Sockaddr addr = Address.getInfo(sa);
if (addr.hostname == null) {
// any local address
if (addr.family == Socket.APR_INET6) {
return new InetSocketAddress("::", addr.port);
} else {
return new InetSocketAddress("0.0.0.0", addr.port);
}
}
return new InetSocketAddress(addr.hostname, addr.port);
}
}
/**
* This endpoint does not support <code>-1</code> for unlimited connections,
* nor does it support setting this attribute while the endpoint is running.
*
* {@inheritDoc}
*/
@Override
public void setMaxConnections(int maxConnections) {
if (maxConnections == -1) {
log.warn(sm.getString("endpoint.apr.maxConnections.unlimited",
Integer.valueOf(getMaxConnections())));
return;
}
if (running) {
log.warn(sm.getString("endpoint.apr.maxConnections.running",
Integer.valueOf(getMaxConnections())));
return;
}
super.setMaxConnections(maxConnections);
}
/**
* Path for the Unix Domain Socket, used to create the socket address.
*/
private String unixDomainSocketPath = null;
public String getUnixDomainSocketPath() { return this.unixDomainSocketPath; }
public void setUnixDomainSocketPath(String unixDomainSocketPath) {
this.unixDomainSocketPath = unixDomainSocketPath;
}
/**
* Permissions which will be set on the Unix Domain Socket if it is created.
*/
private String unixDomainSocketPathPermissions = null;
public String getUnixDomainSocketPathPermissions() { return this.unixDomainSocketPathPermissions; }
public void setUnixDomainSocketPathPermissions(String unixDomainSocketPathPermissions) {
this.unixDomainSocketPathPermissions = unixDomainSocketPathPermissions;
}
// --------------------------------------------------------- Public Methods
/**
* Obtain the number of kept alive sockets.
*
* @return The number of open sockets currently managed by the Poller
*/
public int getKeepAliveCount() {
if (poller == null) {
return 0;
}
return poller.getConnectionCount();
}
/**
* Obtain the number of sendfile sockets.
*
* @return The number of sockets currently managed by the Sendfile poller.
*/
public int getSendfileCount() {
if (sendfile == null) {
return 0;
}
return sendfile.getSendfileCount();
}
@Override
public String getId() {
if (getUnixDomainSocketPath() != null) {
return getUnixDomainSocketPath();
} else {
return null;
}
}
// ----------------------------------------------- Public Lifecycle Methods
/**
* Initialize the endpoint.
*/
@Override
public void bind() throws Exception {
int family;
String hostname = null;
// Create the root APR memory pool
try {
rootPool = Pool.create(0);
} catch (UnsatisfiedLinkError e) {
throw new Exception(sm.getString("endpoint.init.notavail"));
}
// Create the pool for the server socket
serverSockPool = Pool.create(rootPool);
// Create the APR address that will be bound
if (getUnixDomainSocketPath() != null) {
if (Library.APR_HAVE_UNIX) {
hostname = getUnixDomainSocketPath();
family = Socket.APR_UNIX;
}
else {
throw new Exception(sm.getString("endpoint.init.unixnotavail"));
}
}
else {
if (getAddress() != null) {
hostname = getAddress().getHostAddress();
}
family = Socket.APR_UNSPEC;
}
long sockAddress = Address.info(hostname, family, getPortWithOffset(), 0, rootPool);
// Create the APR server socket
if (family == Socket.APR_UNIX) {
serverSock = Socket.create(family, Socket.SOCK_STREAM, 0, rootPool);
}
else {
int saFamily = Address.getInfo(sockAddress).family;
serverSock = Socket.create(saFamily,
Socket.SOCK_STREAM,
Socket.APR_PROTO_TCP, rootPool);
if (OS.IS_UNIX) {
Socket.optSet(serverSock, Socket.APR_SO_REUSEADDR, 1);
}
if (Library.APR_HAVE_IPV6 && saFamily == Socket.APR_INET6) {
if (getIpv6v6only()) {
Socket.optSet(serverSock, Socket.APR_IPV6_V6ONLY, 1);
} else {
Socket.optSet(serverSock, Socket.APR_IPV6_V6ONLY, 0);
}
}
// Deal with the firewalls that tend to drop the inactive sockets
Socket.optSet(serverSock, Socket.APR_SO_KEEPALIVE, 1);
}
// Bind the server socket
int ret = Socket.bind(serverSock, sockAddress);
if (ret != 0) {
throw new Exception(sm.getString("endpoint.init.bind", "" + ret, Error.strerror(ret)));
}
// Start listening on the server socket
ret = Socket.listen(serverSock, getAcceptCount());
if (ret != 0) {
throw new Exception(sm.getString("endpoint.init.listen", "" + ret, Error.strerror(ret)));
}
if (family == Socket.APR_UNIX) {
if (getUnixDomainSocketPathPermissions() != null) {
FileAttribute<Set<PosixFilePermission>> attrs =
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(
getUnixDomainSocketPathPermissions()));
Path path = Paths.get(getUnixDomainSocketPath());
Files.setAttribute(path, attrs.name(), attrs.value());
}
} else {
if (OS.IS_WIN32 || OS.IS_WIN64) {
// On Windows set the reuseaddr flag after the bind/listen
Socket.optSet(serverSock, Socket.APR_SO_REUSEADDR, 1);
}
}
// Enable Sendfile by default if it has not been configured but usage on
// systems which don't support it cause major problems
if (!useSendFileSet) {
setUseSendfileInternal(Library.APR_HAS_SENDFILE);
} else if (getUseSendfile() && !Library.APR_HAS_SENDFILE) {
setUseSendfileInternal(false);
}
// Delay accepting of new connections until data is available
// Only Linux kernels 2.4 + have that implemented
// on other platforms this call is noop and will return APR_ENOTIMPL.
if (deferAccept) {
if (Socket.optSet(serverSock, Socket.APR_TCP_DEFER_ACCEPT, 1) == Status.APR_ENOTIMPL) {
deferAccept = false;
}
}
// Initialize SSL if needed
if (isSSLEnabled()) {
for (SSLHostConfig sslHostConfig : sslHostConfigs.values()) {
createSSLContext(sslHostConfig);
}
SSLHostConfig defaultSSLHostConfig = sslHostConfigs.get(getDefaultSSLHostConfigName());
if (defaultSSLHostConfig == null) {
throw new IllegalArgumentException(sm.getString("endpoint.noSslHostConfig",
getDefaultSSLHostConfigName(), getName()));
}
Long defaultSSLContext = defaultSSLHostConfig.getOpenSslContext();
sslContext = defaultSSLContext.longValue();
SSLContext.registerDefault(defaultSSLContext, this);
// For now, sendfile is not supported with SSL
if (getUseSendfile()) {
setUseSendfileInternal(false);
if (useSendFileSet) {
log.warn(sm.getString("endpoint.apr.noSendfileWithSSL"));
}
}
}
}
@Override
protected void createSSLContext(SSLHostConfig sslHostConfig) throws Exception {
OpenSSLContext sslContext = null;
Set<SSLHostConfigCertificate> certificates = sslHostConfig.getCertificates(true);
for (SSLHostConfigCertificate certificate : certificates) {
if (sslContext == null) {
SSLUtil sslUtil = new OpenSSLUtil(certificate);
sslHostConfig.setEnabledProtocols(sslUtil.getEnabledProtocols());
sslHostConfig.setEnabledCiphers(sslUtil.getEnabledCiphers());
try {
sslContext = (OpenSSLContext) sslUtil.createSSLContext(negotiableProtocols);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
} else {
SSLUtil sslUtil = new OpenSSLUtil(certificate);
KeyManager[] kms = sslUtil.getKeyManagers();
certificate.setCertificateKeyManager(OpenSSLUtil.chooseKeyManager(kms));
sslContext.addCertificate(certificate);
}
certificate.setSslContext(sslContext);
}
if (certificates.size() > 2) {
// TODO: Can this limitation be removed?
throw new Exception(sm.getString("endpoint.apr.tooManyCertFiles"));
}
}
@Override
public long getSslContext(String sniHostName) {
SSLHostConfig sslHostConfig = getSSLHostConfig(sniHostName);
Long ctx = sslHostConfig.getOpenSslContext();
if (ctx != null) {
return ctx.longValue();
}
// Default
return 0;
}
@Override
public boolean isAlpnSupported() {
// The APR/native connector always supports ALPN if TLS is in use
// because OpenSSL supports ALPN. Therefore, this is equivalent to
// testing of SSL is enabled.
return isSSLEnabled();
}
/**
* Start the APR endpoint, creating acceptor, poller and sendfile threads.
*/
@Override
public void startInternal() throws Exception {
if (!running) {
running = true;
paused = false;
if (socketProperties.getProcessorCache() != 0) {
processorCache = new SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
socketProperties.getProcessorCache());
}
// Create worker collection
if (getExecutor() == null) {
createExecutor();
}
initializeConnectionLatch();
// Start poller thread
poller = new Poller();
poller.init();
poller.start();
// Start sendfile thread
if (getUseSendfile()) {
sendfile = new Sendfile();
sendfile.init();
sendfile.start();
}
startAcceptorThread();
}
}
/**
* Stop the endpoint. This will cause all processing threads to stop.
*/
@Override
public void stopInternal() {
if (!paused) {
pause();
}
if (running) {
running = false;
// Stop new connections being accepted.
acceptor.stop(10);
// Stop the Poller calling select
poller.stop();
if (getUseSendfile()) {
sendfile.stop();
}
// Wait for the acceptor to shutdown
if (acceptor.getState() != AcceptorState.ENDED && !getBindOnInit()) {
log.warn(sm.getString("endpoint.warn.unlockAcceptorFailed", acceptor.getThreadName()));
// If the Acceptor is still running force
// the hard socket close.
if (serverSock != 0) {
Socket.shutdown(serverSock, Socket.APR_SHUTDOWN_READ);
serverSock = 0;
}
}
// Wait for Poller to stop
int waitMillis = 0;
try {
while (poller.pollerThread.isAlive() && waitMillis < 10000) {
waitMillis++;
Thread.sleep(1);
}
} catch (InterruptedException e) {
// Ignore
}
if (getUseSendfile()) {
try {
// Wait for the sendfile thread to exit, otherwise parallel
// destruction of sockets which are still in the poller can cause
// problems.
waitMillis = 0;
try {
while (sendfile.sendfileThread.isAlive() && waitMillis < 10000) {
waitMillis++;
Thread.sleep(1);
}
} catch (InterruptedException e) {
// Ignore
}
if (sendfile.sendfileThread.isAlive()) {
log.warn(sm.getString("endpoint.sendfileThreadStop"));
}
} catch (Exception e) {
// Ignore
}
}
// Close the SocketWrapper for each open connection - this should
// trigger a IOException when the app (or container) tries to write.
// Use the blocking status write lock as a proxy for a lock on
// writing to the socket. Don't want to close it while another
// thread is writing as that could trigger a JVM crash.
for (SocketWrapperBase<Long> socketWrapper : connections.values()) {
WriteLock wl = ((AprSocketWrapper) socketWrapper).getBlockingStatusWriteLock();
wl.lock();
try {
socketWrapper.close();
} finally {
wl.unlock();
}
}
for (Long socket : connections.keySet()) {
// Close the APR Socket. Need to do this before destroying the
// poller since that will also destroy the root pool for these
// sockets.
Socket.shutdown(socket.longValue(), Socket.APR_SHUTDOWN_READWRITE);
}
if (getUseSendfile()) {
try {
sendfile.destroy();
} catch (Exception e) {
// Ignore
}
sendfile = null;
}
try {
poller.destroy();
} catch (Exception e) {
// Ignore
}
poller = null;
connections.clear();
if (processorCache != null) {
processorCache.clear();
processorCache = null;
}
}
shutdownExecutor();
}
/**
* Deallocate APR memory pools, and close server socket.
*/
@Override
public void unbind() throws Exception {
if (running) {
stop();
}
// Destroy pool if it was initialised
if (serverSockPool != 0) {
Pool.destroy(serverSockPool);
serverSockPool = 0;
}
doCloseServerSocket();
destroySsl();
// Close all APR memory pools and resources if initialised
if (rootPool != 0) {
Pool.destroy(rootPool);
rootPool = 0;
}
getHandler().recycle();
}
@Override
protected void doCloseServerSocket() {
// Close server socket if it was initialised
if (serverSock != 0) {
Socket.close(serverSock);
serverSock = 0;
}
}
// ------------------------------------------------------ Protected Methods
/**
* Process the specified connection.
* @param socketWrapper The socket wrapper
* @return <code>true</code> if the socket was correctly configured
* and processing may continue, <code>false</code> if the socket needs to be
* close immediately
*/
protected boolean setSocketOptions(SocketWrapperBase<Long> socketWrapper) {
long socket = socketWrapper.getSocket().longValue();
// Process the connection
int step = 1;
try {
// 1: Set socket options: timeout, linger, etc
if (socketProperties.getSoLingerOn() && socketProperties.getSoLingerTime() >= 0) {
Socket.optSet(socket, Socket.APR_SO_LINGER, socketProperties.getSoLingerTime());
}
if (socketProperties.getTcpNoDelay()) {
Socket.optSet(socket, Socket.APR_TCP_NODELAY, (socketProperties.getTcpNoDelay() ? 1 : 0));
}
Socket.timeoutSet(socket, socketProperties.getSoTimeout() * 1000);
// 2: SSL handshake
step = 2;
if (sslContext != 0) {
int rv = SSLSocket.attach(sslContext, socket);
if (rv != Status.APR_SUCCESS) {
log.warn(sm.getString("endpoint.err.attach", Integer.valueOf(rv)));
return false;
}
// Need to make sure the socket doesn't get closed while the
// handshake is in progress as that could trigger a JVM crash.
// Like stopInternal(), use the blocking status write lock as a
// proxy for a lock on writing to the socket.
WriteLock wl = ((AprSocketWrapper) socketWrapper).getBlockingStatusWriteLock();
wl.lock();
try {
if (SSLSocket.handshake(socket) != 0) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.err.handshake") + ": " + SSL.getLastError());
}
return false;
}
} finally {
wl.unlock();
}
if (negotiableProtocols.size() > 0) {
byte[] negotiated = new byte[256];
int len = SSLSocket.getALPN(socket, negotiated);
String negotiatedProtocol =
new String(negotiated, 0, len, StandardCharsets.UTF_8);
if (negotiatedProtocol.length() > 0) {
socketWrapper.setNegotiatedProtocol(negotiatedProtocol);
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.alpn.negotiated", negotiatedProtocol));
}
}
}
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
if (log.isDebugEnabled()) {
if (step == 2) {
log.debug(sm.getString("endpoint.err.handshake"), t);
} else {
log.debug(sm.getString("endpoint.err.unexpected"), t);
}
}
// Tell to close the socket
return false;
}
return true;
}
/**
* Allocate a new poller of the specified size.
* @param size The size
* @param pool The pool from which the poller will be allocated
* @param timeout The timeout
* @return the poller pointer
*/
protected long allocatePoller(int size, long pool, int timeout) {
try {
return Poll.create(size, pool, 0, timeout * 1000);
} catch (Error e) {
if (Status.APR_STATUS_IS_EINVAL(e.getError())) {
log.info(sm.getString("endpoint.poll.limitedpollsize", "" + size));
throw new RuntimeException(e);
} else {
log.error(sm.getString("endpoint.poll.initfail"), e);
throw new RuntimeException(e);
}
}
}
/**
* Process given socket. This is called when the socket has been
* accepted.
* @param socket The socket
* @return <code>true</code> if the socket was correctly configured
* and processing may continue, <code>false</code> if the socket needs to be
* close immediately
*/
@Override
protected boolean setSocketOptions(Long socket) {
try {
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.debug.socket", socket));
}
// Do the duplicate accept check here rather than in serverSocketaccept()
// so we can cache the results in the SocketWrapper
AprSocketWrapper wrapper = new AprSocketWrapper(socket, this);
// Bug does not affect Windows platform and Unix Domain Socket. Skip the check.
if (!JrePlatform.IS_WINDOWS && getUnixDomainSocketPath() == null) {
long currentNanoTime = System.nanoTime();
if (wrapper.getRemotePort() == previousAcceptedPort) {
if (wrapper.getRemoteAddr().equals(previousAcceptedAddress)) {
if (currentNanoTime - previousAcceptedSocketNanoTime < 1000) {
throw new IOException(sm.getString("endpoint.err.duplicateAccept"));
}
}
}
previousAcceptedPort = wrapper.getRemotePort();
previousAcceptedAddress = wrapper.getRemoteAddr();
previousAcceptedSocketNanoTime = currentNanoTime;
}
connections.put(socket, wrapper);
wrapper.setKeepAliveLeft(getMaxKeepAliveRequests());
wrapper.setReadTimeout(getConnectionTimeout());
wrapper.setWriteTimeout(getConnectionTimeout());
getExecutor().execute(new SocketWithOptionsProcessor(wrapper));
return true;
} catch (RejectedExecutionException x) {
log.warn(sm.getString("endpoint.rejectedExecution", socket), x);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
// This means we got an OOM or similar creating a thread, or that
// the pool and its queue are full
log.error(sm.getString("endpoint.process.fail"), t);
}
return false;
}
@Override
protected Long serverSocketAccept() throws Exception {
// See setSocketOptions(Long) for duplicate accept check
long socket = Socket.accept(serverSock);
if (socket == 0) {
throw new IOException(sm.getString("endpoint.err.accept", getName()));
}
if (log.isDebugEnabled()) {
long sa = Address.get(Socket.APR_REMOTE, socket);
Sockaddr addr = Address.getInfo(sa);
log.debug(sm.getString("endpoint.apr.remoteport",
Long.valueOf(socket),
Long.valueOf(addr.port)));
}
return Long.valueOf(socket);
}
/**
* Process the given socket. Typically keep alive or upgraded protocol.
*
* @param socket The socket to process
* @param event The event to process
*
* @return <code>true</code> if the processing completed normally otherwise
* <code>false</code> which indicates an error occurred and that the
* socket should be closed
*/
protected boolean processSocket(long socket, SocketEvent event) {
SocketWrapperBase<Long> socketWrapper = connections.get(Long.valueOf(socket));
if (socketWrapper == null) {
// Socket probably closed from another thread. Triggering another
// close in case won't cause an issue.
return false;
}
if (event == SocketEvent.OPEN_READ && socketWrapper.readOperation != null) {
return socketWrapper.readOperation.process();
} else if (event == SocketEvent.OPEN_WRITE && socketWrapper.writeOperation != null) {
return socketWrapper.writeOperation.process();
} else {
return processSocket(socketWrapper, event, true);
}
}
@Override
protected SocketProcessorBase<Long> createSocketProcessor(
SocketWrapperBase<Long> socketWrapper, SocketEvent event) {
return new SocketProcessor(socketWrapper, event);
}
private void closeSocketInternal(long socket) {
closeSocket(Long.valueOf(socket));
}
@Override
protected void destroySocket(Long socket) {
countDownConnection();
destroySocketInternal(socket.longValue());
}
private void destroySocketInternal(long socket) {
if (log.isDebugEnabled()) {
String msg = sm.getString("endpoint.debug.destroySocket", Long.valueOf(socket));
if (log.isTraceEnabled()) {
log.trace(msg, new Exception());
} else {
log.debug(msg);
}
}
// Be VERY careful if you call this method directly. If it is called
// twice for the same socket the JVM will core. Currently this is only
// called from Poller.closePollset() to ensure kept alive connections
// are closed when calling stop() followed by start().
if (socket != 0) {
Socket.destroy(socket);
}
}
@Override
protected Log getLog() {
return log;
}
// -------------------------------------------------- SocketInfo Inner Class
public static class SocketInfo {
public long socket;
public long timeout;
public int flags;
public boolean read() {
return (flags & Poll.APR_POLLIN) == Poll.APR_POLLIN;
}
public boolean write() {
return (flags & Poll.APR_POLLOUT) == Poll.APR_POLLOUT;
}
public static int merge(int flag1, int flag2) {
return ((flag1 & Poll.APR_POLLIN) | (flag2 & Poll.APR_POLLIN))
| ((flag1 & Poll.APR_POLLOUT) | (flag2 & Poll.APR_POLLOUT));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Socket: [");
sb.append(socket);
sb.append("], timeout: [");
sb.append(timeout);
sb.append("], flags: [");
sb.append(flags);
return sb.toString();
}
}
// ---------------------------------------------- SocketTimeouts Inner Class
public static class SocketTimeouts {
protected int size;
protected long[] sockets;
protected long[] timeouts;
protected int pos = 0;
public SocketTimeouts(int size) {
this.size = 0;
sockets = new long[size];
timeouts = new long[size];
}
public void add(long socket, long timeout) {
sockets[size] = socket;
timeouts[size] = timeout;
size++;
}
/**
* Removes the specified socket from the poller.
*
* @param socket The socket to remove
*
* @return The configured timeout for the socket or zero if the socket
* was not in the list of socket timeouts
*/
public long remove(long socket) {
long result = 0;
for (int i = 0; i < size; i++) {
if (sockets[i] == socket) {
result = timeouts[i];
sockets[i] = sockets[size - 1];
timeouts[i] = timeouts[size - 1];
size--;
break;
}
}
return result;
}
public long check(long date) {
while (pos < size) {
if (date >= timeouts[pos]) {
long result = sockets[pos];
sockets[pos] = sockets[size - 1];
timeouts[pos] = timeouts[size - 1];
size--;
return result;
}
pos++;
}
pos = 0;
return 0;
}
}
// -------------------------------------------------- SocketList Inner Class
public static class SocketList {
protected volatile int size;
protected int pos;
protected long[] sockets;
protected long[] timeouts;
protected int[] flags;
protected SocketInfo info = new SocketInfo();
public SocketList(int size) {
this.size = 0;
pos = 0;
sockets = new long[size];
timeouts = new long[size];
flags = new int[size];
}
public int size() {
return this.size;
}
public SocketInfo get() {
if (pos == size) {
return null;
} else {
info.socket = sockets[pos];
info.timeout = timeouts[pos];
info.flags = flags[pos];
pos++;
return info;
}
}
public void clear() {
size = 0;
pos = 0;
}
public boolean add(long socket, long timeout, int flag) {
if (size == sockets.length) {
return false;
} else {
for (int i = 0; i < size; i++) {
if (sockets[i] == socket) {
flags[i] = SocketInfo.merge(flags[i], flag);
return true;
}
}
sockets[size] = socket;
timeouts[size] = timeout;
flags[size] = flag;
size++;
return true;
}
}
public boolean remove(long socket) {
for (int i = 0; i < size; i++) {
if (sockets[i] == socket) {
sockets[i] = sockets[size - 1];
timeouts[i] = timeouts[size - 1];
flags[size] = flags[size -1];
size--;
return true;
}
}
return false;
}
public void duplicate(SocketList copy) {
copy.size = size;
copy.pos = pos;
System.arraycopy(sockets, 0, copy.sockets, 0, size);
System.arraycopy(timeouts, 0, copy.timeouts, 0, size);
System.arraycopy(flags, 0, copy.flags, 0, size);
}
}
// ------------------------------------------------------ Poller Inner Class
public class Poller implements Runnable {
/**
* Pointer to the poller.
*/
private long aprPoller;
/**
* Actual poller size.
*/
private int pollerSize = 0;
/**
* Root pool.
*/
private long pool = 0;
/**
* Socket descriptors.
*/
private long[] desc;
/**
* List of sockets to be added to the poller.
*/
private SocketList addList = null; // Modifications guarded by this
/**
* List of sockets to be closed.
*/
private SocketList closeList = null; // Modifications guarded by this
/**
* Structure used for storing timeouts.
*/
private SocketTimeouts timeouts = null;
/**
* Last run of maintain. Maintain will run approximately once every one
* second (may be slightly longer between runs).
*/
private long lastMaintain = System.currentTimeMillis();
/**
* The number of connections currently inside this Poller. The correct
* operation of the Poller depends on this figure being correct. If it
* is not, it is possible that the Poller will enter a wait loop where
* it waits for the next connection to be added to the Poller before it
* calls poll when it should still be polling existing connections.
* Although not necessary at the time of writing this comment, it has
* been implemented as an AtomicInteger to ensure that it remains
* thread-safe.
*/
private AtomicInteger connectionCount = new AtomicInteger(0);
public int getConnectionCount() { return connectionCount.get(); }
private volatile Thread pollerThread;
private volatile boolean pollerRunning = true;
/**
* Create the poller.
*/
protected synchronized void init() {
pool = Pool.create(serverSockPool);
pollerSize = getMaxConnections();
timeouts = new SocketTimeouts(pollerSize);
// At the moment, setting the timeout is useless, but it could get
// used again as the normal poller could be faster using maintain.
// It might not be worth bothering though.
aprPoller = allocatePoller(pollerSize, pool, -1);
/*
* x2 - One descriptor for the socket, one for the event(s).
* x2 - Some APR implementations return multiple events for the
* same socket as different entries. Each socket is registered
* for a maximum of two events (read and write) at any one
* time.
*
* Therefore size is poller size *4.
*/
desc = new long[pollerSize * 4];
connectionCount.set(0);
addList = new SocketList(pollerSize);
closeList = new SocketList(pollerSize);
}
protected void start() {
pollerThread = new Thread(poller, getName() + "-Poller");
pollerThread.setPriority(threadPriority);
pollerThread.setDaemon(true);
pollerThread.start();
}
/*
* This method is synchronized so that it is not possible for a socket
* to be added to the Poller's addList once this method has completed.
*/
protected synchronized void stop() {
pollerRunning = false;
// In case the poller thread is in the idle wait
this.notify();
}
/**
* Destroy the poller.
*/
protected synchronized void destroy() {
// Wait for the poller thread to exit, otherwise parallel
// destruction of sockets which are still in the poller can cause
// problems.
int loops = 50;
while (loops > 0 && pollerThread.isAlive()) {
try {
this.wait(pollTime / 1000);
} catch (InterruptedException e) {
// Ignore
}
loops--;
}
if (pollerThread.isAlive()) {
log.warn(sm.getString("endpoint.pollerThreadStop"));
}
// Close all sockets in the close queue
SocketInfo info = closeList.get();
while (info != null) {
// Make sure we aren't trying add the socket as well as close it
addList.remove(info.socket);
// Make sure the socket isn't in the poller before we close it
removeFromPoller(info.socket);
// Poller isn't running at this point so use destroySocket()
// directly
closeSocketInternal(info.socket);
destroySocketInternal(info.socket);
info = closeList.get();
}
closeList.clear();
// Close all sockets in the add queue
info = addList.get();
while (info != null) {
// Make sure the socket isn't in the poller before we close it
removeFromPoller(info.socket);
// Poller isn't running at this point so use destroySocket()
// directly
closeSocketInternal(info.socket);
destroySocketInternal(info.socket);
info = addList.get();
}
addList.clear();
// Close all sockets still in the poller
int rv = Poll.pollset(aprPoller, desc);
if (rv > 0) {
for (int n = 0; n < rv; n++) {
closeSocketInternal(desc[n*2+1]);
destroySocketInternal(desc[n*2+1]);
}
}
Pool.destroy(pool);
connectionCount.set(0);
}
/**
* Add specified socket and associated pool to the poller. The socket
* will be added to a temporary array, and polled first after a maximum
* amount of time equal to pollTime (in most cases, latency will be much
* lower, however). Note: If both read and write are false, the socket
* will only be checked for timeout; if the socket was already present
* in the poller, a callback event will be generated and the socket will
* be removed from the poller.
*
* @param socket to add to the poller
* @param timeout to use for this connection in milliseconds
* @param flags Events to poll for (Poll.APR_POLLIN and/or
* Poll.APR_POLLOUT)
*/
private void add(long socket, long timeout, int flags) {
if (log.isDebugEnabled()) {
String msg = sm.getString("endpoint.debug.pollerAdd",
Long.valueOf(socket), Long.valueOf(timeout),
Integer.valueOf(flags));
if (log.isTraceEnabled()) {
log.trace(msg, new Exception());
} else {
log.debug(msg);
}
}
if (timeout <= 0) {
// Always put a timeout in
timeout = Integer.MAX_VALUE;
}
synchronized (this) {
// Add socket to the list. Newly added sockets will wait
// at most for pollTime before being polled.
if (addList.add(socket, timeout, flags)) {
// In case the poller thread is in the idle wait
this.notify();
}
}
}
/**
* Add specified socket to one of the pollers. Must only be called from
* {@link Poller#run()}.
*/
private boolean addToPoller(long socket, int events) {
int rv = Poll.add(aprPoller, socket, events);
if (rv == Status.APR_SUCCESS) {
connectionCount.incrementAndGet();
return true;
}
return false;
}
/*
* This is only called from the SocketWrapper to ensure that it is only
* called once per socket. Calling it more than once typically results
* in the JVM crash.
*/
private synchronized void close(long socket) {
closeList.add(socket, 0, 0);
// In case the poller thread is in the idle wait
this.notify();
}
/**
* Remove specified socket from the pollers. Must only be called from
* {@link Poller#run()}.
*/
private void removeFromPoller(long socket) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.debug.pollerRemove",
Long.valueOf(socket)));
}
int rv = Poll.remove(aprPoller, socket);
if (rv != Status.APR_NOTFOUND) {
connectionCount.decrementAndGet();
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.debug.pollerRemoved",
Long.valueOf(socket)));
}
}
timeouts.remove(socket);
}
/**
* Timeout checks. Must only be called from {@link Poller#run()}.
*/
private synchronized void maintain() {
long date = System.currentTimeMillis();
// Maintain runs at most once every 1s, although it will likely get
// called more
if ((date - lastMaintain) < 1000L) {
return;
} else {
lastMaintain = date;
}
long socket = timeouts.check(date);
while (socket != 0) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.debug.socketTimeout",
Long.valueOf(socket)));
}
SocketWrapperBase<Long> socketWrapper = connections.get(Long.valueOf(socket));
if (socketWrapper != null) {
socketWrapper.setError(new SocketTimeoutException());
if (socketWrapper.readOperation != null || socketWrapper.writeOperation != null) {
if (socketWrapper.readOperation != null) {
socketWrapper.readOperation.process();
} else {
socketWrapper.writeOperation.process();
}
} else {
processSocket(socketWrapper, SocketEvent.ERROR, true);
}
}
socket = timeouts.check(date);
}
}
/**
* Displays the list of sockets in the pollers.
*/
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append("Poller");
long[] res = new long[pollerSize * 2];
int count = Poll.pollset(aprPoller, res);
buf.append(" [ ");
for (int j = 0; j < count; j++) {
buf.append(desc[2*j+1]).append(' ');
}
buf.append(']');
return buf.toString();
}
/**
* The background thread that adds sockets to the Poller, checks the
* poller for triggered events and hands the associated socket off to an
* appropriate processor as events occur.
*/
@Override
public void run() {
SocketList localAddList = new SocketList(getMaxConnections());
SocketList localCloseList = new SocketList(getMaxConnections());
// Loop until we receive a shutdown command
while (pollerRunning) {
// Check timeouts if the poller is empty.
while (pollerRunning && connectionCount.get() < 1 &&
addList.size() < 1 && closeList.size() < 1) {
try {
if (getConnectionTimeout() > 0 && pollerRunning) {
maintain();
}
synchronized (this) {
// Make sure that no sockets have been placed in the
// addList or closeList since the check above.
// Without this check there could be a 10s pause
// with no processing since the notify() call in
// add()/close() would have no effect since it
// happened before this sync block was entered
if (pollerRunning && addList.size() < 1 && closeList.size() < 1) {
this.wait(10000);
}
}
} catch (InterruptedException e) {
// Ignore
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
getLog().warn(sm.getString("endpoint.timeout.err"));
}
}
// Don't add or poll if the poller has been stopped
if (!pollerRunning) {
break;
}
try {
// Duplicate the add and remove lists so that the syncs are
// minimised
synchronized (this) {
if (closeList.size() > 0) {
// Duplicate to another list, so that the syncing is
// minimal
closeList.duplicate(localCloseList);
closeList.clear();
} else {
localCloseList.clear();
}
}
synchronized (this) {
if (addList.size() > 0) {
// Duplicate to another list, so that the syncing is
// minimal
addList.duplicate(localAddList);
addList.clear();
} else {
localAddList.clear();
}
}
// Remove sockets
if (localCloseList.size() > 0) {
SocketInfo info = localCloseList.get();
while (info != null) {
localAddList.remove(info.socket);
removeFromPoller(info.socket);
closeSocketInternal(info.socket);
destroySocketInternal(info.socket);
info = localCloseList.get();
}
}
// Add sockets which are waiting to the poller
if (localAddList.size() > 0) {
SocketInfo info = localAddList.get();
while (info != null) {
if (log.isDebugEnabled()) {
log.debug(sm.getString(
"endpoint.debug.pollerAddDo",
Long.valueOf(info.socket)));
}
timeouts.remove(info.socket);
AprSocketWrapper wrapper =
(AprSocketWrapper) connections.get(Long.valueOf(info.socket));
if (wrapper != null) {
if (info.read() || info.write()) {
wrapper.pollerFlags = wrapper.pollerFlags |
(info.read() ? Poll.APR_POLLIN : 0) |
(info.write() ? Poll.APR_POLLOUT : 0);
// A socket can only be added to the poller
// once. Adding it twice will return an error
// which will close the socket. Therefore make
// sure the socket we are about to add isn't in
// the poller.
removeFromPoller(info.socket);
if (!addToPoller(info.socket, wrapper.pollerFlags)) {
wrapper.close();
} else {
timeouts.add(info.socket,
System.currentTimeMillis() +
info.timeout);
}
} else {
// Should never happen.
wrapper.close();
getLog().warn(sm.getString(
"endpoint.apr.pollAddInvalid", info));
}
}
info = localAddList.get();
}
}
// Flag to ask to reallocate the pool
boolean reset = false;
int rv = Poll.poll(aprPoller, pollTime, desc, true);
if (rv > 0) {
rv = mergeDescriptors(desc, rv);
connectionCount.addAndGet(-rv);
for (int n = 0; n < rv; n++) {
if (getLog().isDebugEnabled()) {
log.debug(sm.getString(
"endpoint.debug.pollerProcess",
Long.valueOf(desc[n*2+1]),
Long.valueOf(desc[n*2])));
}
long timeout = timeouts.remove(desc[n*2+1]);
AprSocketWrapper wrapper = (AprSocketWrapper)
connections.get(Long.valueOf(desc[n*2+1]));
if (wrapper == null) {
// Socket was closed in another thread while still in
// the Poller but wasn't removed from the Poller before
// new data arrived.
continue;
}
wrapper.pollerFlags = wrapper.pollerFlags & ~((int) desc[n*2]);
// Check for failed sockets and hand this socket off to a worker
if (((desc[n*2] & Poll.APR_POLLHUP) == Poll.APR_POLLHUP)
|| ((desc[n*2] & Poll.APR_POLLERR) == Poll.APR_POLLERR)
|| ((desc[n*2] & Poll.APR_POLLNVAL) == Poll.APR_POLLNVAL)) {
// Need to trigger error handling. Poller may return error
// codes plus the flags it was waiting for or it may just
// return an error code. We could handle the error here but
// if we do, there will be no exception associated with the
// error in application code. By signalling read/write is
// possible, a read/write will be attempted, fail and that
// will trigger an exception the application will see.
// Check the return flags first, followed by what the socket
// was registered for
if ((desc[n*2] & Poll.APR_POLLIN) == Poll.APR_POLLIN) {
// Error probably occurred during a non-blocking read
if (!processSocket(desc[n*2+1], SocketEvent.OPEN_READ)) {
// Close socket and clear pool
wrapper.close();
}
} else if ((desc[n*2] & Poll.APR_POLLOUT) == Poll.APR_POLLOUT) {
// Error probably occurred during a non-blocking write
if (!processSocket(desc[n*2+1], SocketEvent.OPEN_WRITE)) {
// Close socket and clear pool
wrapper.close();
}
} else if ((wrapper.pollerFlags & Poll.APR_POLLIN) == Poll.APR_POLLIN) {
// Can't tell what was happening when the error occurred but the
// socket is registered for non-blocking read so use that
if (!processSocket(desc[n*2+1], SocketEvent.OPEN_READ)) {
// Close socket and clear pool
wrapper.close();
}
} else if ((wrapper.pollerFlags & Poll.APR_POLLOUT) == Poll.APR_POLLOUT) {
// Can't tell what was happening when the error occurred but the
// socket is registered for non-blocking write so use that
if (!processSocket(desc[n*2+1], SocketEvent.OPEN_WRITE)) {
// Close socket and clear pool
wrapper.close();
}
} else {
// Close socket and clear pool
wrapper.close();
}
} else if (((desc[n*2] & Poll.APR_POLLIN) == Poll.APR_POLLIN)
|| ((desc[n*2] & Poll.APR_POLLOUT) == Poll.APR_POLLOUT)) {
boolean error = false;
if (((desc[n*2] & Poll.APR_POLLIN) == Poll.APR_POLLIN) &&
!processSocket(desc[n*2+1], SocketEvent.OPEN_READ)) {
error = true;
// Close socket and clear pool
wrapper.close();
}
if (!error &&
((desc[n*2] & Poll.APR_POLLOUT) == Poll.APR_POLLOUT) &&
!processSocket(desc[n*2+1], SocketEvent.OPEN_WRITE)) {
// Close socket and clear pool
error = true;
wrapper.close();
}
if (!error && wrapper.pollerFlags != 0) {
// If socket was registered for multiple events but
// only some of the occurred, re-register for the
// remaining events.
// timeout is the value of System.currentTimeMillis() that
// was set as the point that the socket will timeout. When
// adding to the poller, the timeout from now in
// milliseconds is required.
// So first, subtract the current timestamp
if (timeout > 0) {
timeout = timeout - System.currentTimeMillis();
}
// If the socket should have already expired by now,
// re-add it with a very short timeout
if (timeout <= 0) {
timeout = 1;
}
// Should be impossible but just in case since timeout will
// be cast to an int.
if (timeout > Integer.MAX_VALUE) {
timeout = Integer.MAX_VALUE;
}
add(desc[n*2+1], (int) timeout, wrapper.pollerFlags);
}
} else {
// Unknown event
getLog().warn(sm.getString(
"endpoint.apr.pollUnknownEvent",
Long.valueOf(desc[n*2])));
// Close socket and clear pool
wrapper.close();
}
}
} else if (rv < 0) {
int errn = -rv;
// Any non timeup or interrupted error is critical
if ((errn != Status.TIMEUP) && (errn != Status.EINTR)) {
if (errn > Status.APR_OS_START_USERERR) {
errn -= Status.APR_OS_START_USERERR;
}
getLog().error(sm.getString(
"endpoint.apr.pollError",
Integer.valueOf(errn),
Error.strerror(errn)));
// Destroy and reallocate the poller
reset = true;
}
}
if (reset && pollerRunning) {
// Reallocate the current poller
int count = Poll.pollset(aprPoller, desc);
long newPoller = allocatePoller(pollerSize, pool, -1);
// Don't restore connections for now, since I have not tested it
connectionCount.addAndGet(-count);
Poll.destroy(aprPoller);
aprPoller = newPoller;
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
getLog().warn(sm.getString("endpoint.poll.error"), t);
}
try {
// Process socket timeouts
if (getConnectionTimeout() > 0 && pollerRunning) {
// This works and uses only one timeout mechanism for everything, but the
// non event poller might be a bit faster by using the old maintain.
maintain();
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
getLog().warn(sm.getString("endpoint.timeout.err"), t);
}
}
synchronized (this) {
this.notifyAll();
}
}
private int mergeDescriptors(long[] desc, int startCount) {
/*
* https://bz.apache.org/bugzilla/show_bug.cgi?id=57653#c6 suggests
* this merging is only necessary on OSX and BSD.
*
* https://bz.apache.org/bugzilla/show_bug.cgi?id=56313 suggests the
* same, or a similar, issue is happening on Windows.
* Notes: Only the first startCount * 2 elements of the array
* are populated.
* The array is event, socket, event, socket etc.
*/
Map<Long,Long> merged = new HashMap<>(startCount);
for (int n = 0; n < startCount; n++) {
Long newValue = merged.merge(Long.valueOf(desc[2*n+1]), Long.valueOf(desc[2*n]),
(v1, v2) -> Long.valueOf(v1.longValue() | v2.longValue()));
if (log.isDebugEnabled()) {
if (newValue.longValue() != desc[2*n]) {
log.debug(sm.getString("endpoint.apr.pollMergeEvents",
Long.valueOf(desc[2*n+1]), Long.valueOf(desc[2*n]), newValue));
}
}
}
int i = 0;
for (Map.Entry<Long,Long> entry : merged.entrySet()) {
desc[i++] = entry.getValue().longValue();
desc[i++] = entry.getKey().longValue();
}
return merged.size();
}
}
// ----------------------------------------------- SendfileData Inner Class
/**
* SendfileData class.
*/
public static class SendfileData extends SendfileDataBase {
// File
protected long fd;
protected long fdpool;
// Socket and socket pool
protected long socket;
public SendfileData(String filename, long pos, long length) {
super(filename, pos, length);
}
}
// --------------------------------------------------- Sendfile Inner Class
public class Sendfile implements Runnable {
protected long sendfilePollset = 0;
protected long pool = 0;
protected long[] desc;
protected HashMap<Long, SendfileData> sendfileData;
protected int sendfileCount;
public int getSendfileCount() { return sendfileCount; }
protected ArrayList<SendfileData> addS;
private volatile Thread sendfileThread;
private volatile boolean sendfileRunning = true;
/**
* Create the sendfile poller.
*/
protected void init() {
pool = Pool.create(serverSockPool);
int size = sendfileSize;
if (size <= 0) {
size = 16 * 1024;
}
sendfilePollset = allocatePoller(size, pool, getConnectionTimeout());
desc = new long[size * 2];
sendfileData = new HashMap<>(size);
addS = new ArrayList<>();
}
protected void start() {
sendfileThread = new Thread(sendfile, getName() + "-Sendfile");
sendfileThread.setPriority(threadPriority);
sendfileThread.setDaemon(true);
sendfileThread.start();
}
protected synchronized void stop() {
sendfileRunning = false;
// In case the sendfile thread is in the idle wait
this.notify();
}
/**
* Destroy the poller.
*/
protected void destroy() {
// Close any socket remaining in the add queue
for (int i = (addS.size() - 1); i >= 0; i--) {
SendfileData data = addS.get(i);
closeSocketInternal(data.socket);
}
// Close all sockets still in the poller
int rv = Poll.pollset(sendfilePollset, desc);
if (rv > 0) {
for (int n = 0; n < rv; n++) {
closeSocketInternal(desc[n*2+1]);
}
}
Pool.destroy(pool);
sendfileData.clear();
}
/**
* Add the sendfile data to the sendfile poller. Note that in most cases,
* the initial non blocking calls to sendfile will return right away, and
* will be handled asynchronously inside the kernel. As a result,
* the poller will never be used.
*
* @param data containing the reference to the data which should be sent
* @return true if all the data has been sent right away, and false
* otherwise
*/
public SendfileState add(SendfileData data) {
SocketWrapperBase<Long> socketWrapper = connections.get(Long.valueOf(data.socket));
// Use the blocking status write lock as a proxy for a lock on
// writing to the socket. Don't want it to close in another thread
// while this thread is writing as that could trigger a JVM crash.
WriteLock wl = ((AprSocketWrapper) socketWrapper).getBlockingStatusWriteLock();
wl.lock();
// Initialize fd from data given
try {
data.fdpool = Socket.pool(data.socket);
data.fd = File.open
(data.fileName, File.APR_FOPEN_READ
| File.APR_FOPEN_SENDFILE_ENABLED | File.APR_FOPEN_BINARY,
0, data.fdpool);
// Set the socket to nonblocking mode
Socket.timeoutSet(data.socket, 0);
while (sendfileRunning) {
long nw = Socket.sendfilen(data.socket, data.fd,
data.pos, data.length, 0);
if (nw < 0) {
if (!(-nw == Status.EAGAIN)) {
Pool.destroy(data.fdpool);
data.socket = 0;
return SendfileState.ERROR;
} else {
// Break the loop and add the socket to poller.
break;
}
} else {
data.pos += nw;
data.length -= nw;
if (data.length == 0) {
// Entire file has been sent
Pool.destroy(data.fdpool);
// Set back socket to blocking mode
Socket.timeoutSet(data.socket, getConnectionTimeout() * 1000);
return SendfileState.DONE;
}
}
}
} catch (Exception e) {
log.warn(sm.getString("endpoint.sendfile.error"), e);
return SendfileState.ERROR;
} finally {
wl.unlock();
}
// Add socket to the list. Newly added sockets will wait
// at most for pollTime before being polled
synchronized (this) {
addS.add(data);
this.notify();
}
return SendfileState.PENDING;
}
/**
* Remove socket from the poller.
*
* @param data the sendfile data which should be removed
*/
protected void remove(SendfileData data) {
int rv = Poll.remove(sendfilePollset, data.socket);
if (rv == Status.APR_SUCCESS) {
sendfileCount--;
}
sendfileData.remove(Long.valueOf(data.socket));
}
/**
* The background thread that listens for incoming TCP/IP connections
* and hands them off to an appropriate processor.
*/
@Override
public void run() {
long maintainTime = 0;
// Loop until we receive a shutdown command
while (sendfileRunning) {
// Loop if poller is empty
while (sendfileRunning && sendfileCount < 1 && addS.size() < 1) {
// Reset maintain time.
maintainTime = 0;
try {
synchronized (this) {
if (sendfileRunning && sendfileCount < 1 && addS.size() < 1) {
this.wait();
}
}
} catch (InterruptedException e) {
// Ignore
}
}
// Don't add or poll if the poller has been stopped
if (!sendfileRunning) {
break;
}
try {
// Add socket to the poller
if (addS.size() > 0) {
synchronized (this) {
for (int i = (addS.size() - 1); i >= 0; i--) {
SendfileData data = addS.get(i);
int rv = Poll.add(sendfilePollset, data.socket, Poll.APR_POLLOUT);
if (rv == Status.APR_SUCCESS) {
sendfileData.put(Long.valueOf(data.socket), data);
sendfileCount++;
} else {
getLog().warn(sm.getString(
"endpoint.sendfile.addfail",
Integer.valueOf(rv),
Error.strerror(rv)));
// Can't do anything: close the socket right away
closeSocketInternal(data.socket);
}
}
addS.clear();
}
}
maintainTime += pollTime;
// Pool for the specified interval
int rv = Poll.poll(sendfilePollset, pollTime, desc, false);
if (rv > 0) {
for (int n = 0; n < rv; n++) {
// Get the sendfile state
SendfileData state =
sendfileData.get(Long.valueOf(desc[n*2+1]));
// Problem events
if (((desc[n*2] & Poll.APR_POLLHUP) == Poll.APR_POLLHUP)
|| ((desc[n*2] & Poll.APR_POLLERR) == Poll.APR_POLLERR)) {
// Close socket and clear pool
remove(state);
// Destroy file descriptor pool, which should close the file
// Close the socket, as the response would be incomplete
closeSocketInternal(state.socket);
continue;
}
// Write some data using sendfile
long nw = Socket.sendfilen(state.socket, state.fd,
state.pos,
state.length, 0);
if (nw < 0) {
// Close socket and clear pool
remove(state);
// Close the socket, as the response would be incomplete
// This will close the file too.
closeSocketInternal(state.socket);
continue;
}
state.pos += nw;
state.length -= nw;
if (state.length == 0) {
remove(state);
switch (state.keepAliveState) {
case NONE: {
// Close the socket since this is
// the end of the not keep-alive request.
closeSocketInternal(state.socket);
break;
}
case PIPELINED: {
// Destroy file descriptor pool, which should close the file
Pool.destroy(state.fdpool);
Socket.timeoutSet(state.socket, getConnectionTimeout() * 1000);
// Process the pipelined request data
if (!processSocket(state.socket, SocketEvent.OPEN_READ)) {
closeSocketInternal(state.socket);
}
break;
}
case OPEN: {
// Destroy file descriptor pool, which should close the file
Pool.destroy(state.fdpool);
Socket.timeoutSet(state.socket, getConnectionTimeout() * 1000);
// Put the socket back in the poller for
// processing of further requests
getPoller().add(state.socket, getKeepAliveTimeout(),
Poll.APR_POLLIN);
break;
}
}
}
}
} else if (rv < 0) {
int errn = -rv;
/* Any non timeup or interrupted error is critical */
if ((errn != Status.TIMEUP) && (errn != Status.EINTR)) {
if (errn > Status.APR_OS_START_USERERR) {
errn -= Status.APR_OS_START_USERERR;
}
getLog().error(sm.getString(
"endpoint.apr.pollError",
Integer.valueOf(errn),
Error.strerror(errn)));
// Handle poll critical failure
synchronized (this) {
destroy();
init();
}
continue;
}
}
// Call maintain for the sendfile poller
if (getConnectionTimeout() > 0 &&
maintainTime > 1000000L && sendfileRunning) {
rv = Poll.maintain(sendfilePollset, desc, false);
maintainTime = 0;
if (rv > 0) {
for (int n = 0; n < rv; n++) {
// Get the sendfile state
SendfileData state = sendfileData.get(Long.valueOf(desc[n]));
// Close socket and clear pool
remove(state);
// Destroy file descriptor pool, which should close the file
// Close the socket, as the response would be incomplete
closeSocketInternal(state.socket);
}
}
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
getLog().error(sm.getString("endpoint.poll.error"), t);
}
}
synchronized (this) {
this.notifyAll();
}
}
}
// --------------------------------- SocketWithOptionsProcessor Inner Class
/**
* This class is the equivalent of the Worker, but will simply use in an
* external Executor thread pool. This will also set the socket options
* and do the handshake.
*
* This is called after an accept().
*/
protected class SocketWithOptionsProcessor implements Runnable {
protected SocketWrapperBase<Long> socket = null;
public SocketWithOptionsProcessor(SocketWrapperBase<Long> socket) {
this.socket = socket;
}
@Override
public void run() {
synchronized (socket) {
if (!deferAccept) {
if (setSocketOptions(socket)) {
getPoller().add(socket.getSocket().longValue(),
getConnectionTimeout(), Poll.APR_POLLIN);
} else {
// Close socket and pool
getHandler().process(socket, SocketEvent.CONNECT_FAIL);
socket.close();
socket = null;
}
} else {
// Process the request from this socket
if (!setSocketOptions(socket)) {
// Close socket and pool
getHandler().process(socket, SocketEvent.CONNECT_FAIL);
socket.close();
socket = null;
return;
}
// Process the request from this socket
Handler.SocketState state = getHandler().process(socket, SocketEvent.OPEN_READ);
if (state == Handler.SocketState.CLOSED) {
// Close socket and pool
socket.close();
socket = null;
}
}
}
}
}
// -------------------------------------------- SocketProcessor Inner Class
/**
* This class is the equivalent of the Worker, but will simply use in an
* external Executor thread pool.
*/
protected class SocketProcessor extends SocketProcessorBase<Long> {
public SocketProcessor(SocketWrapperBase<Long> socketWrapper, SocketEvent event) {
super(socketWrapper, event);
}
@Override
protected void doRun() {
try {
// Process the request from this socket
SocketState state = getHandler().process(socketWrapper, event);
if (state == Handler.SocketState.CLOSED) {
// Close socket and pool
socketWrapper.close();
}
} finally {
socketWrapper = null;
event = null;
//return to cache
if (running && processorCache != null) {
processorCache.push(this);
}
}
}
}
public static class AprSocketWrapper extends SocketWrapperBase<Long> {
private static final int SSL_OUTPUT_BUFFER_SIZE = 8192;
private final ByteBuffer sslOutputBuffer;
// This field should only be used by Poller#run()
private int pollerFlags = 0;
/*
* Used if block/non-blocking is set at the socket level. The client is
* responsible for the thread-safe use of this field via the locks provided.
*/
private volatile boolean blockingStatus = true;
private final Lock blockingStatusReadLock;
private final WriteLock blockingStatusWriteLock;
public AprSocketWrapper(Long socket, AprEndpoint endpoint) {
super(socket, endpoint);
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
this.blockingStatusReadLock = lock.readLock();
this.blockingStatusWriteLock = lock.writeLock();
// TODO Make the socketWriteBuffer size configurable and align the
// SSL and app buffer size settings with NIO & NIO2.
if (endpoint.isSSLEnabled()) {
sslOutputBuffer = ByteBuffer.allocateDirect(SSL_OUTPUT_BUFFER_SIZE);
sslOutputBuffer.position(SSL_OUTPUT_BUFFER_SIZE);
} else {
sslOutputBuffer = null;
}
socketBufferHandler = new SocketBufferHandler(6 * 1500, 6 * 1500, true);
}
public boolean getBlockingStatus() { return blockingStatus; }
public void setBlockingStatus(boolean blockingStatus) {
this.blockingStatus = blockingStatus;
}
public Lock getBlockingStatusReadLock() { return blockingStatusReadLock; }
public WriteLock getBlockingStatusWriteLock() {
return blockingStatusWriteLock;
}
@Override
public int read(boolean block, byte[] b, int off, int len) throws IOException {
int nRead = populateReadBuffer(b, off, len);
if (nRead > 0) {
return nRead;
/*
* Since more bytes may have arrived since the buffer was last
* filled, it is an option at this point to perform a
* non-blocking read. However correctly handling the case if
* that read returns end of stream adds complexity. Therefore,
* at the moment, the preference is for simplicity.
*/
}
// Fill the read buffer as best we can.
nRead = fillReadBuffer(block);
// Fill as much of the remaining byte array as possible with the
// data that was just read
if (nRead > 0) {
socketBufferHandler.configureReadBufferForRead();
nRead = Math.min(nRead, len);
socketBufferHandler.getReadBuffer().get(b, off, nRead);
}
return nRead;
}
@Override
public int read(boolean block, ByteBuffer to) throws IOException {
int nRead = populateReadBuffer(to);
if (nRead > 0) {
return nRead;
/*
* Since more bytes may have arrived since the buffer was last
* filled, it is an option at this point to perform a
* non-blocking read. However correctly handling the case if
* that read returns end of stream adds complexity. Therefore,
* at the moment, the preference is for simplicity.
*/
}
// The socket read buffer capacity is socket.appReadBufSize
int limit = socketBufferHandler.getReadBuffer().capacity();
if (to.isDirect() && to.remaining() >= limit) {
to.limit(to.position() + limit);
nRead = fillReadBuffer(block, to);
if (log.isDebugEnabled()) {
log.debug("Socket: [" + this + "], Read direct from socket: [" + nRead + "]");
}
} else {
// Fill the read buffer as best we can.
nRead = fillReadBuffer(block);
if (log.isDebugEnabled()) {
log.debug("Socket: [" + this + "], Read into buffer: [" + nRead + "]");
}
// Fill as much of the remaining byte array as possible with the
// data that was just read
if (nRead > 0) {
nRead = populateReadBuffer(to);
}
}
return nRead;
}
private int fillReadBuffer(boolean block) throws IOException {
socketBufferHandler.configureReadBufferForWrite();
return fillReadBuffer(block, socketBufferHandler.getReadBuffer());
}
private int fillReadBuffer(boolean block, ByteBuffer to) throws IOException {
Lock readLock = getBlockingStatusReadLock();
WriteLock writeLock = getBlockingStatusWriteLock();
boolean readDone = false;
int result = 0;
readLock.lock();
try {
checkClosed();
if (getBlockingStatus() == block) {
if (block) {
Socket.timeoutSet(getSocket().longValue(), getReadTimeout() * 1000);
}
result = Socket.recvb(getSocket().longValue(), to, to.position(),
to.remaining());
readDone = true;
}
} finally {
readLock.unlock();
}
if (!readDone) {
writeLock.lock();
try {
checkClosed();
// Set the current settings for this socket
setBlockingStatus(block);
if (block) {
Socket.timeoutSet(getSocket().longValue(), getReadTimeout() * 1000);
} else {
Socket.timeoutSet(getSocket().longValue(), 0);
}
// Downgrade the lock
readLock.lock();
try {
writeLock.unlock();
result = Socket.recvb(getSocket().longValue(), to, to.position(),
to.remaining());
} finally {
readLock.unlock();
}
} finally {
// Should have been released above but may not have been on some
// exception paths
if (writeLock.isHeldByCurrentThread()) {
writeLock.unlock();
}
}
}
if (result > 0) {
to.position(to.position() + result);
return result;
} else if (result == 0 || -result == Status.EAGAIN) {
return 0;
} else if ((-result) == Status.ETIMEDOUT || (-result) == Status.TIMEUP) {
if (block) {
throw new SocketTimeoutException(sm.getString("iib.readtimeout"));
} else {
// Attempting to read from the socket when the poller
// has not signalled that there is data to read appears
// to behave like a blocking read with a short timeout
// on OSX rather than like a non-blocking read. If no
// data is read, treat the resulting timeout like a
// non-blocking read that returned no data.
return 0;
}
} else if (-result == Status.APR_EOF) {
return -1;
} else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
(-result == Status.APR_OS_START_SYSERR + 10053)) {
// 10053 on Windows is connection aborted
throw new EOFException(sm.getString("socket.apr.clientAbort"));
} else {
throw new IOException(sm.getString("socket.apr.read.error",
Integer.valueOf(-result), getSocket(), this));
}
}
@Override
public boolean isReadyForRead() throws IOException {
socketBufferHandler.configureReadBufferForRead();
if (socketBufferHandler.getReadBuffer().remaining() > 0) {
return true;
}
int read = fillReadBuffer(false);
boolean isReady = socketBufferHandler.getReadBuffer().position() > 0 || read == -1;
return isReady;
}
private void checkClosed() throws IOException {
if (isClosed()) {
throw new IOException(sm.getString("socket.apr.closed", getSocket()));
}
}
@Override
protected void doClose() {
if (log.isDebugEnabled()) {
log.debug("Calling [" + getEndpoint() + "].closeSocket([" + this + "])");
}
getEndpoint().connections.remove(getSocket());
socketBufferHandler.free();
socketBufferHandler = SocketBufferHandler.EMPTY;
nonBlockingWriteBuffer.clear();
if (sslOutputBuffer != null) {
ByteBufferUtils.cleanDirectBuffer(sslOutputBuffer);
}
Poller poller = ((AprEndpoint) getEndpoint()).getPoller();
if (poller != null) {
poller.close(getSocket().longValue());
}
}
@Override
protected boolean flushNonBlocking() throws IOException {
boolean dataLeft = !socketBufferHandler.isWriteBufferEmpty();
// Write to the socket, if there is anything to write
if (dataLeft) {
doWrite(false);
dataLeft = !socketBufferHandler.isWriteBufferEmpty();
}
if (!dataLeft && !nonBlockingWriteBuffer.isEmpty()) {
dataLeft = nonBlockingWriteBuffer.write(this, false);
if (!dataLeft && !socketBufferHandler.isWriteBufferEmpty()) {
doWrite(false);
dataLeft = !socketBufferHandler.isWriteBufferEmpty();
}
}
return dataLeft;
}
@Override
protected void doWrite(boolean block, ByteBuffer from) throws IOException {
Lock readLock = getBlockingStatusReadLock();
WriteLock writeLock = getBlockingStatusWriteLock();
readLock.lock();
try {
checkClosed();
if (getBlockingStatus() == block) {
if (block) {
Socket.timeoutSet(getSocket().longValue(), getWriteTimeout() * 1000);
}
doWriteInternal(from);
return;
}
} finally {
readLock.unlock();
}
writeLock.lock();
try {
checkClosed();
// Set the current settings for this socket
setBlockingStatus(block);
if (block) {
Socket.timeoutSet(getSocket().longValue(), getWriteTimeout() * 1000);
} else {
Socket.timeoutSet(getSocket().longValue(), 0);
}
// Downgrade the lock
readLock.lock();
try {
writeLock.unlock();
doWriteInternal(from);
} finally {
readLock.unlock();
}
} finally {
// Should have been released above but may not have been on some
// exception paths
if (writeLock.isHeldByCurrentThread()) {
writeLock.unlock();
}
}
}
private void doWriteInternal(ByteBuffer from) throws IOException {
if (previousIOException != null) {
/*
* Socket has previously seen an IOException on write.
*
* Blocking writes assume that buffer is always fully written so
* there is no code checking for incomplete writes, retaining
* the unwritten data and attempting to write it as part of a
* subsequent write call.
*
* Because of the above, when an IOException is triggered we
* need so skip subsequent attempts to write as otherwise it
* will appear to the client as if some data was dropped just
* before the connection is lost. It is better if the client
* just sees the dropped connection.
*/
throw new IOException(previousIOException);
}
int thisTime;
do {
thisTime = 0;
if (getEndpoint().isSSLEnabled()) {
if (sslOutputBuffer.remaining() == 0) {
// Buffer was fully written last time around
sslOutputBuffer.clear();
transfer(from, sslOutputBuffer);
sslOutputBuffer.flip();
} else {
// Buffer still has data from previous attempt to write
// APR + SSL requires that exactly the same parameters are
// passed when re-attempting the write
}
thisTime = Socket.sendb(getSocket().longValue(), sslOutputBuffer,
sslOutputBuffer.position(), sslOutputBuffer.limit());
if (thisTime > 0) {
sslOutputBuffer.position(sslOutputBuffer.position() + thisTime);
}
} else {
thisTime = Socket.sendb(getSocket().longValue(), from, from.position(),
from.remaining());
if (thisTime > 0) {
from.position(from.position() + thisTime);
}
}
if (Status.APR_STATUS_IS_EAGAIN(-thisTime)) {
thisTime = 0;
} else if (-thisTime == Status.APR_EOF) {
throw new EOFException(sm.getString("socket.apr.clientAbort"));
} else if ((OS.IS_WIN32 || OS.IS_WIN64) &&
(-thisTime == Status.APR_OS_START_SYSERR + 10053)) {
// 10053 on Windows is connection aborted
throw new EOFException(sm.getString("socket.apr.clientAbort"));
} else if (thisTime < 0) {
previousIOException = new IOException(sm.getString("socket.apr.write.error",
Integer.valueOf(-thisTime), getSocket(), this));
throw previousIOException;
}
} while ((thisTime > 0 || getBlockingStatus()) && from.hasRemaining());
// If there is data left in the buffer the socket will be registered for
// write further up the stack. This is to ensure the socket is only
// registered for write once as both container and user code can trigger
// write registration.
}
@Override
public void registerReadInterest() {
// Make sure an already closed socket is not added to the poller
synchronized (closed) {
if (isClosed()) {
return;
}
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.debug.registerRead", this));
}
Poller p = ((AprEndpoint) getEndpoint()).getPoller();
if (p != null) {
p.add(getSocket().longValue(), getReadTimeout(), Poll.APR_POLLIN);
}
}
}
@Override
public void registerWriteInterest() {
// Make sure an already closed socket is not added to the poller
synchronized (closed) {
if (isClosed()) {
return;
}
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.debug.registerWrite", this));
}
((AprEndpoint) getEndpoint()).getPoller().add(
getSocket().longValue(), getWriteTimeout(), Poll.APR_POLLOUT);
}
}
@Override
public SendfileDataBase createSendfileData(String filename, long pos, long length) {
return new SendfileData(filename, pos, length);
}
@Override
public SendfileState processSendfile(SendfileDataBase sendfileData) {
((SendfileData) sendfileData).socket = getSocket().longValue();
return ((AprEndpoint) getEndpoint()).getSendfile().add((SendfileData) sendfileData);
}
@Override
protected void populateRemoteAddr() {
if (isClosed()) {
return;
}
try {
long socket = getSocket().longValue();
long sa = Address.get(Socket.APR_REMOTE, socket);
remoteAddr = Address.getip(sa);
} catch (Exception e) {
log.warn(sm.getString("endpoint.warn.noRemoteAddr", getSocket()), e);
}
}
@Override
protected void populateRemoteHost() {
if (isClosed()) {
return;
}
try {
long socket = getSocket().longValue();
long sa = Address.get(Socket.APR_REMOTE, socket);
remoteHost = Address.getnameinfo(sa, 0);
if (remoteAddr == null) {
remoteAddr = Address.getip(sa);
}
} catch (Exception e) {
log.warn(sm.getString("endpoint.warn.noRemoteHost", getSocket()), e);
}
}
@Override
protected void populateRemotePort() {
if (isClosed()) {
return;
}
try {
long socket = getSocket().longValue();
long sa = Address.get(Socket.APR_REMOTE, socket);
Sockaddr addr = Address.getInfo(sa);
remotePort = addr.port;
} catch (Exception e) {
log.warn(sm.getString("endpoint.warn.noRemotePort", getSocket()), e);
}
}
@Override
protected void populateLocalName() {
if (isClosed()) {
return;
}
try {
long socket = getSocket().longValue();
long sa = Address.get(Socket.APR_LOCAL, socket);
localName =Address.getnameinfo(sa, 0);
} catch (Exception e) {
log.warn(sm.getString("endpoint.warn.noLocalName"), e);
}
}
@Override
protected void populateLocalAddr() {
if (isClosed()) {
return;
}
try {
long socket = getSocket().longValue();
long sa = Address.get(Socket.APR_LOCAL, socket);
localAddr = Address.getip(sa);
} catch (Exception e) {
log.warn(sm.getString("endpoint.warn.noLocalAddr"), e);
}
}
@Override
protected void populateLocalPort() {
if (isClosed()) {
return;
}
try {
long socket = getSocket().longValue();
long sa = Address.get(Socket.APR_LOCAL, socket);
Sockaddr addr = Address.getInfo(sa);
localPort = addr.port;
} catch (Exception e) {
log.warn(sm.getString("endpoint.warn.noLocalPort"), e);
}
}
@Override
public SSLSupport getSslSupport(String clientCertProvider) {
if (getEndpoint().isSSLEnabled()) {
return new AprSSLSupport(this, clientCertProvider);
} else {
return null;
}
}
@Override
public SSLSupport getSslSupport() {
throw new UnsupportedOperationException();
}
@Override
public void doClientAuth(SSLSupport sslSupport) throws IOException {
long socket = getSocket().longValue();
// Configure connection to require a certificate. This requires a
// re-handshake and must block until the re-handshake completes.
// Therefore, make sure socket is in blocking mode.
Lock readLock = getBlockingStatusReadLock();
WriteLock writeLock = getBlockingStatusWriteLock();
boolean renegotiateDone = false;
try {
readLock.lock();
try {
if (getBlockingStatus()) {
Socket.timeoutSet(getSocket().longValue(), getReadTimeout() * 1000);
SSLSocket.setVerify(socket, SSL.SSL_CVERIFY_REQUIRE, -1);
SSLSocket.renegotiate(socket);
renegotiateDone = true;
}
} finally {
readLock.unlock();
}
if (!renegotiateDone) {
writeLock.lock();
try {
// Set the current settings for this socket
setBlockingStatus(true);
Socket.timeoutSet(getSocket().longValue(), getReadTimeout() * 1000);
// Downgrade the lock
readLock.lock();
try {
writeLock.unlock();
SSLSocket.setVerify(socket, SSL.SSL_CVERIFY_REQUIRE, -1);
SSLSocket.renegotiate(socket);
} finally {
readLock.unlock();
}
} finally {
// Should have been released above but may not have been on some
// exception paths
if (writeLock.isHeldByCurrentThread()) {
writeLock.unlock();
}
}
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
throw new IOException(sm.getString("socket.sslreneg"), t);
}
}
@Override
public void setAppReadBufHandler(ApplicationBufferHandler handler) {
// no-op
}
String getSSLInfoS(int id) {
synchronized (closed) {
if (isClosed()) {
return null;
}
try {
return SSLSocket.getInfoS(getSocket().longValue(), id);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
int getSSLInfoI(int id) {
synchronized (closed) {
if (isClosed()) {
return 0;
}
try {
return SSLSocket.getInfoI(getSocket().longValue(), id);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
byte[] getSSLInfoB(int id) {
synchronized (closed) {
if (isClosed()) {
return null;
}
try {
return SSLSocket.getInfoB(getSocket().longValue(), id);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
@Override
protected <A> OperationState<A> newOperationState(boolean read,
ByteBuffer[] buffers, int offset, int length,
BlockingMode block, long timeout, TimeUnit unit, A attachment,
CompletionCheck check, CompletionHandler<Long, ? super A> handler,
Semaphore semaphore, VectoredIOCompletionHandler<A> completion) {
return new AprOperationState<>(read, buffers, offset, length, block,
timeout, unit, attachment, check, handler, semaphore, completion);
}
private class AprOperationState<A> extends OperationState<A> {
private volatile boolean inline = true;
private volatile long flushBytes = 0;
private AprOperationState(boolean read, ByteBuffer[] buffers, int offset, int length,
BlockingMode block, long timeout, TimeUnit unit, A attachment, CompletionCheck check,
CompletionHandler<Long, ? super A> handler, Semaphore semaphore,
VectoredIOCompletionHandler<A> completion) {
super(read, buffers, offset, length, block,
timeout, unit, attachment, check, handler, semaphore, completion);
}
@Override
protected boolean isInline() {
return inline;
}
@Override
public void run() {
// Perform the IO operation
// Called from the poller to continue the IO operation
long nBytes = 0;
if (getError() == null) {
try {
synchronized (this) {
if (!completionDone) {
// This filters out same notification until processing
// of the current one is done
if (log.isDebugEnabled()) {
log.debug("Skip concurrent " + (read ? "read" : "write") + " notification");
}
return;
}
// Find the buffer on which the operation will be performed (no vectoring with APR)
ByteBuffer buffer = null;
for (int i = 0; i < length; i++) {
if (buffers[i + offset].hasRemaining()) {
buffer = buffers[i + offset];
break;
}
}
if (buffer == null && flushBytes == 0) {
// Nothing to do
completion.completed(Long.valueOf(0), this);
return;
}
if (read) {
nBytes = read(false, buffer);
} else {
if (!flush(block == BlockingMode.BLOCK)) {
if (flushBytes > 0) {
// Flushing was done, continue processing
nBytes = flushBytes;
flushBytes = 0;
} else {
@SuppressWarnings("null") // Not possible
int remaining = buffer.remaining();
write(block == BlockingMode.BLOCK, buffer);
nBytes = remaining - buffer.remaining();
if (nBytes > 0 && flush(block == BlockingMode.BLOCK)) {
// We have to flush and it's incomplete, save the bytes written until done
inline = false;
registerWriteInterest();
flushBytes = nBytes;
return;
}
}
} else {
// Continue flushing
inline = false;
registerWriteInterest();
return;
}
}
if (nBytes != 0) {
completionDone = false;
}
}
} catch (IOException e) {
setError(e);
}
}
if (nBytes > 0) {
// The bytes processed are only updated in the completion handler
completion.completed(Long.valueOf(nBytes), this);
} else if (nBytes < 0 || getError() != null) {
IOException error = getError();
if (error == null) {
error = new EOFException();
}
completion.failed(error, this);
} else {
// As soon as the operation uses the poller, it is no longer inline
inline = false;
if (read) {
registerReadInterest();
} else {
registerWriteInterest();
}
}
}
}
}
}
|