1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276
|
/*
* Copyright (C) 1990-2010 by CERN/IT/PDP/DM
* All rights reserved
*/
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: rfio_call64.c,v $ $Revision: 3541 $ $Date: 2010-04-29 11:27:17 +0200 (Thu, 29 Apr 2010) $ CERN/IT/PDP/DM Frederic Hemmer, Philippe Gaillardon";
#endif /* not lint */
/*
* Remote file I/O flags and declarations.
*/
#define DEBUG 0
#define RFIO_KERNEL 1
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#if defined(_WIN32)
#include "syslog.h"
#else
#include <sys/param.h>
#include <syslog.h> /* System logger */
#include <sys/time.h>
#endif
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif
#if defined(_AIX) || defined(hpux) || defined(SOLARIS) || defined(linux)
#include <signal.h>
#endif
#include <Castor_limits.h>
#include "rfio.h"
#include "rfio_server.h"
#include "rfcntl.h"
#include "log.h"
#include "net.h"
#include "u64subr.h"
#if defined(_AIX)
#include <sys/select.h>
#endif
#include <sys/types.h>
#if !defined(_WIN32)
#include <netinet/in.h>
#if ((defined(IRIX5) || defined(IRIX6)) && ! (defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) && defined(PDP_ENDIAN)))
#ifdef LITTLE_ENDIAN
#undef LITTLE_ENDIAN
#endif
#define LITTLE_ENDIAN 1234
#ifdef BIG_ENDIAN
#undef BIG_ENDIAN
#endif
#define BIG_ENDIAN 4321
#ifdef PDP_ENDIAN
#undef PDP_ENDIAN
#endif
#define PDP_ENDIAN 3412
#endif
#include <netinet/tcp.h>
#endif
#include <sys/stat.h>
#ifdef linux
#include <sys/uio.h>
#endif
#include <fcntl.h>
/* For multithreading stuff, only tested under Linux at present */
#include <Cthread_api.h>
#include "Csemaphore.h"
/* If daemonv3_rdmt is true, reading from disk will be multithreaded
The circular buffer will have in this case daemonv3_rdmt_nbuf buffers of
size daemonv3_rdmt_bufsize. See defaults in rfio_server.h */
static int daemonv3_rdmt, daemonv3_rdmt_nbuf, daemonv3_rdmt_bufsize;
/* If daemonv3_wrmt is true, reading from disk will be multithreaded
The circular buffer will have in this case daemonv3_wrmt_nbuf buffers of
size daemonv3_wrmt_bufsize. See defaults in rfio_server.h */
static int daemonv3_wrmt, daemonv3_wrmt_nbuf, daemonv3_wrmt_bufsize;
/* The circular buffer definition */
static struct element {
char *p;
int len;
} *array = NULL;
/* The two semaphores to synchonize accesses to the circular buffer */
CSemaphore empty64, full64;
/* Number of buffers produced and consumed */
int produced64 = 0;
int consumed64 = 0;
/* Variable used for error reporting between disk writer thread
and main thread reading from the network */
static int write_error;
/* Variable set by the main thread reading from the network
to tell the disk reader thread to stop */
static volatile int stop_read;
extern char *getconfent() ;
extern int checkkey(int sock, u_short key );
extern int chsuser(int uid, int gid, char *hostname, int *ptrcode, char *permstr) ;
extern struct passwd stagersuperuser;
#if defined(_WIN32)
#if !defined (MAX_THREADS)
#define MAX_THREADS 64
#endif /* MAX_THREADS */
extern DWORD tls_i; /* thread local storage index */
extern struct thData {
SOCKET ns; /* control socket */
struct sockaddr_storage from;
int fromlen;
int mode;
int _is_remote;
int fd;
/* all globals, which have to be local for thread */
char *rqstbuf; /* Request buffer */
char *filename; /* file name */
char *iobuffer; /* Data communication buffer */
int iobufsiz; /* Current io buffer size */
SOCKET data_s; /* Data listen socket (v3) */
SOCKET data_sock; /* Data accept socket (v3) */
SOCKET ctrl_sock; /* the control socket (v3) */
int first_write;
int first_read;
int byte_read_from_network;
struct rfiostat myinfo;
char from_host[MAXHOSTNAMELEN];
/* Context for the open/firstwrite/close handlers */
void *handler_context;
} *td;
#define rqstbuf td->rqstbuf
#define filename td->filename
#define iobuffer td->iobuffer
#define iobufsiz td->iobufsiz
#define data_s td->data_s
#define data_sock td->data_sock
#define ctrl_sock td->ctrl_sock
#define first_write td->first_write
#define first_read td->first_read
#define byte_read_from_network td->byte_read_from_network
#define is_remote td->_is_remote
#define myinfo td->myinfo
#define handler_context td->handler_context
#else /* WIN32 */
/*
* Buffer declarations
*/
extern char rqstbuf[BUFSIZ] ; /* Request buffer */
extern char filename[MAXFILENAMSIZE];
static char *iobuffer ; /* Data communication buffer */
static int iobufsiz; /* Current io buffer size */
/* Context for the open/firstwrite/close handlers */
static void *handler_context;
#endif /* else WIN32 */
#if defined(_WIN32)
#define ECONNRESET WSAECONNRESET
#endif /* WIN32 */
extern int srchkreqsize _PROTO((SOCKET, char *, int));
/************************************************************************/
/* */
/* IO HANDLERS */
/* */
/************************************************************************/
#if !defined(_WIN32)
int srlockf64(s, infop, fd)
int s;
struct rfiostat *infop;
int fd;
{
char *p ;
LONG status = 0;
LONG len ;
LONG mode ;
int op;
off64_t siz;
int rcode = 0 ;
LONG how;
off64_t offset;
off64_t offsetout;
char tmpbuf[21];
p = rqstbuf + (2*WORDSIZE) ;
unmarshall_LONG(p, how) ;
unmarshall_LONG(p, len) ;
if ( (status = srchkreqsize(s,p,len)) == -1 ) {
rcode = errno;
} else {
log(LOG_DEBUG, "srlockf64(%d,%d): reading %d bytes\n", s, fd, len);
serrno = 0;
if (netread_timeout(s, rqstbuf, len, RFIO_CTRL_TIMEOUT) != len) {
log(LOG_ERR, "srlockf64: read(): %s\n", neterror());
return -1;
}
/*
* Reading request.
*/
p = rqstbuf ;
unmarshall_LONG(p, op) ;
unmarshall_HYPER(p, siz);
unmarshall_HYPER(p, offset);
log(LOG_DEBUG, "srlockf64(%d,%d): operation %d, size %s\n", s, fd, op,
u64tostr(siz,tmpbuf,0));
infop->lockop++;
/*
* lseek() if needed.
*/
if ( how != -1 ) {
log(LOG_DEBUG, "lseek64(%d,%d): lseek64(%d,%s,%d)\n", s, fd, fd,
u64tostr(offset,tmpbuf,0), how) ;
infop->seekop++;
if ( (offsetout= lseek64(fd,offset,how)) == -1 ) {
rcode= errno ;
status = -1;
log(LOG_ERR, "srlockf64(%d,%d): error %d in lseek64\n", s, fd, rcode);
}
}
if (status == 0 ) {
if ( (status = lockf64(fd, op, siz)) < 0 ) {
rcode = errno ;
log(LOG_ERR, "srlockf64(%d,%d): error %d in lockf64(%d,%d,%s)\n",
s, fd, rcode, fd, op, u64tostr(siz,tmpbuf,0));
}
else
status = 0 ;
}
}
p = rqstbuf ;
marshall_LONG(p, status);
marshall_LONG(p, rcode);
#if defined(SACCT)
rfioacct(RQST_LOCKF,-1,-1,s,op,(int)siz,status,rcode,NULL,NULL,NULL);
#endif /* SACCT */
log(LOG_DEBUG, "srlockf64: sending back status %d rcode %d\n", status, rcode);
serrno = 0;
if (netwrite_timeout(s, rqstbuf, 2*LONGSIZE, RFIO_CTRL_TIMEOUT) != (2*LONGSIZE)) {
log(LOG_ERR, "srlockf64: write(): %s\n", neterror());
return -1 ;
}
return status ;
}
#endif /* !WIN32 */
#if !defined(_WIN32)
int srlstat64(s, rt, host)
int s;
int rt; /* Is it a remote site call ? */
char *host; /* Where the request comes from */
{
char *auth_id;
char *mech;
int need_user_check = 1;
char * p ;
int status = 0, rcode = 0 ;
int len ;
int replen;
struct stat64 statbuf ;
char user[CA_MAXUSRNAMELEN+1];
int uid,gid;
p= rqstbuf + 2*WORDSIZE ;
unmarshall_LONG(p,len) ;
if ( (status = srchkreqsize(s,p,len)) == -1 ) {
rcode = errno;
} else {
/*
* Reading stat request.
*/
log(LOG_DEBUG, "srlstat64(%d): reading %d bytes\n", s, len);
serrno = 0;
if ((status = netread_timeout(s,rqstbuf,len,RFIO_CTRL_TIMEOUT)) != len) {
log(LOG_ERR, "srlstat64: read(): %s\n", neterror());
return -1;
}
p= rqstbuf ;
uid = gid = 0;
status = 0;
*user = *filename = '\0';
unmarshall_WORD(p,uid);
unmarshall_WORD(p,gid);
get_client_actual_id(&uid, &gid, &mech, &auth_id, &rt, NULL);
if (unmarshall_STRINGN(p, user, CA_MAXUSRNAMELEN+1) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = E2BIG;
}
}
if (unmarshall_STRINGN(p, filename, MAXFILENAMSIZE) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = SENAMETOOLONG;
}
}
if ( (status == 0) && rt ) {
char to[100];
int rcd;
int to_uid, to_gid ;
if ( (rcd = get_user(host,user,uid,gid,to,&to_uid,&to_gid)) == -ENOENT ) {
log(LOG_ERR, "srlstat64: get_user(): Error opening mapping file\n") ;
status= -1;
errno = EINVAL;
rcode = errno ;
}
if ( !status && abs(rcd) == 1 ) {
log(LOG_ERR, "srlstat64: No entry in mapping file for (%s,%s,%d,%d)\n",
host, user, uid, gid);
status= -1;
errno=EACCES;
rcode=errno;
}
else {
log(LOG_DEBUG, "srlstat64: (%s,%s,%d,%d) mapped to %s(%d,%d)\n",
host, user, uid, gid, to, to_uid, to_gid) ;
uid = to_uid ;
gid = to_gid ;
}
}
if ( !status ) {
status = rfio_handle_stat64(filename, 1, mech, auth_id, uid, gid,
&statbuf, &need_user_check);
if (status < 0) {
char alarmbuf[1024];
rcode = serrno;
sprintf(alarmbuf,"srlstat64(): %s",filename) ;
log(LOG_DEBUG, "srlstat64: rfio_handler_stat refused stat: %s\n", sstrerror(serrno)) ;
rfio_alrm(rcode,alarmbuf) ;
memset(&statbuf,'\0',sizeof(statbuf));
}
}
if ( !status && need_user_check) {
if ( (status=chsuser(uid,gid,host,&rcode,"FTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"WTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"RTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"XTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"OPENTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"STATTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"POPENTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"LINKTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"CHMODTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"CHOWNTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"MKDIRTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"RMDIRTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"RENAMETRUST")) < 0 ) {
if (status == -2)
log(LOG_ERR, "srlstat64: uid %d not allowed to stat()\n", uid);
else
log(LOG_ERR, "srlstat64: failed at chsuser(), rcode %d\n", rcode);
memset(&statbuf,'\0',sizeof(statbuf));
status = rcode ;
}
else {
status= ( lstat64(filename, &statbuf) < 0 ) ? errno : 0 ;
log(LOG_INFO, "srlstat64: file: %s , status %d\n", filename, status) ;
}
}
}
p = rqstbuf ;
marshall_WORD(p, statbuf.st_dev);
marshall_HYPER(p, statbuf.st_ino);
marshall_WORD(p, statbuf.st_mode);
marshall_WORD(p, statbuf.st_nlink);
marshall_WORD(p, statbuf.st_uid);
marshall_WORD(p, statbuf.st_gid);
marshall_HYPER(p, statbuf.st_size);
marshall_LONG(p, statbuf.st_atime);
marshall_LONG(p, statbuf.st_mtime);
marshall_LONG(p, statbuf.st_ctime);
/*
* Bug #2646. This is one of the rare cases when the errno
* is returned in the status parameter.
*/
if ( status == -1 && rcode > 0 ) status = rcode;
marshall_LONG(p, status);
marshall_LONG(p, statbuf.st_blksize);
marshall_HYPER(p, statbuf.st_blocks);
replen = 3*HYPERSIZE+5*LONGSIZE+5*WORDSIZE;
log(LOG_DEBUG, "srlstat64: sending back %d\n", status);
serrno = 0;
if (netwrite_timeout(s,rqstbuf, replen, RFIO_CTRL_TIMEOUT) != replen) {
log(LOG_ERR, "srlstat64: write(): %s\n", neterror());
return -1 ;
}
return 0 ;
}
#endif /* !WIN32 */
int srstat64(s, rt, host)
SOCKET s;
int rt; /* Is it a remote site call ? */
char *host; /* Where the request comes from */
{
char *auth_id;
char *mech;
int need_user_check = 1;
char *p ;
int status = 0, rcode = 0 ;
int len ;
int replen;
struct stat64 statbuf ;
char user[CA_MAXUSRNAMELEN+1];
int uid,gid;
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
p= rqstbuf + 2*WORDSIZE ;
unmarshall_LONG(p,len) ;
if ( (status = srchkreqsize(s,p,len)) == -1 ) {
rcode = errno;
} else {
/*
* Reading stat request.
*/
log(LOG_DEBUG, "srstat64(%d): reading %d bytes\n", s, len);
serrno = 0;
if ((status = netread_timeout(s,rqstbuf,len,RFIO_CTRL_TIMEOUT)) != len) {
log(LOG_ERR, "srstat64: read(): %s\n", neterror());
return -1;
}
p = rqstbuf ;
uid = gid = 0;
status = 0;
*user = *filename = '\0';
unmarshall_WORD(p,uid);
unmarshall_WORD(p,gid);
get_client_actual_id(&uid, &gid, &mech, &auth_id, &rt, NULL);
if (unmarshall_STRINGN(p, user, CA_MAXUSRNAMELEN+1) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = E2BIG;
}
}
if (unmarshall_STRINGN(p, filename, MAXFILENAMSIZE) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = SENAMETOOLONG;
}
}
if ( (status == 0) && rt ) {
char to[100];
int rcd;
int to_uid, to_gid ;
if ( (rcd = get_user(host,user,uid,gid,to,&to_uid,&to_gid)) == -ENOENT ) {
log(LOG_ERR, "srstat64: get_user(): Error opening mapping file\n") ;
status= -1;
errno = EINVAL;
rcode = errno ;
}
if ( !status && abs(rcd) == 1 ) {
log(LOG_ERR, "srstat64: No entry in mapping file for (%s,%s,%d,%d)\n",
host, user, uid, gid);
status= -1;
errno=EACCES;
rcode=errno;
}
else {
log(LOG_DEBUG, "srstat64: (%s,%s,%d,%d) mapped to %s(%d,%d)\n",
host, user, uid, gid, to, to_uid, to_gid) ;
uid = to_uid ;
gid = to_gid ;
}
}
if ( !status ) {
status = rfio_handle_stat64(filename, 0, mech, auth_id, uid, gid,
&statbuf, &need_user_check);
if (status < 0) {
char alarmbuf[1024];
rcode = serrno;
sprintf(alarmbuf,"srstat64(): %s",filename) ;
log(LOG_DEBUG, "srstat64: rfio_handler_stat refused stat: %s\n", sstrerror(serrno)) ;
rfio_alrm(rcode,alarmbuf) ;
memset(&statbuf,'\0',sizeof(statbuf));
}
}
if ( !status && need_user_check) {
/*
* Trust root for stat() if trusted for any other privileged operation
*/
rcode = 0;
if (
(status=chsuser(uid,gid,host,&rcode,"FTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"WTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"RTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"XTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"OPENTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"STATTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"POPENTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"LINKTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"CHMODTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"CHOWNTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"MKDIRTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"RMDIRTRUST")) < 0 &&
(status=chsuser(uid,gid,host,&rcode,"RENAMETRUST")) < 0 ) {
if (status == -2)
log(LOG_ERR, "srstat64: uid %d not allowed to stat()\n", uid);
else
log(LOG_ERR, "srstat64: failed at chsuser(), rcode %d\n", rcode);
memset(&statbuf,'\0',sizeof(statbuf));
status = rcode ;
}
else {
status= ( stat64(filename, &statbuf) < 0 ) ? errno : 0 ;
log(LOG_INFO, "srstat64: file: %s for (%d,%d) status %d\n",
filename, uid, gid, status) ;
}
}
}
p = rqstbuf ;
marshall_WORD(p, statbuf.st_dev);
marshall_HYPER(p, statbuf.st_ino);
marshall_WORD(p, statbuf.st_mode);
marshall_WORD(p, statbuf.st_nlink);
marshall_WORD(p, statbuf.st_uid);
marshall_WORD(p, statbuf.st_gid);
marshall_HYPER(p, statbuf.st_size);
marshall_LONG(p, statbuf.st_atime);
marshall_LONG(p, statbuf.st_mtime);
marshall_LONG(p, statbuf.st_ctime);
/*
* Bug #2646. This is one of the rare cases when the errno
* is returned in the status parameter.
*/
if ( status == -1 && rcode > 0 ) status = rcode;
marshall_LONG(p, status);
#if !defined(_WIN32)
marshall_LONG(p, statbuf.st_blksize);
marshall_HYPER(p, statbuf.st_blocks);
#endif
replen = 3*HYPERSIZE+5*LONGSIZE+5*WORDSIZE;
serrno = 0;
if (netwrite_timeout(s, rqstbuf, replen, RFIO_CTRL_TIMEOUT) != replen) {
log(LOG_ERR, "srstat64: write(): %s\n", neterror());
return -1 ;
}
return 0 ;
}
int srstatfs64(s)
int s;
{
int status = 0 ;
int rcode = 0;
int len ;
char *p ;
char path[MAXFILENAMSIZE] ;
struct rfstatfs64 statfsbuf ;
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
p= rqstbuf + 2*WORDSIZE ;
unmarshall_LONG(p,len) ;
if ( (status = srchkreqsize(s,p,len)) == -1 ) {
rcode = errno;
} else {
log(LOG_DEBUG,"srstatfs64(): reading %d bytes\n",len) ;
serrno = 0;
if ((status = netread_timeout(s,rqstbuf,len,RFIO_CTRL_TIMEOUT)) != len) {
log(LOG_ERR,"srstatfs64(): read(): %s\n", neterror());
return -1;
}
p= rqstbuf ;
status = 0;
*path = '\0';
if (unmarshall_STRINGN(p, path, MAXFILENAMSIZE) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = SENAMETOOLONG;
}
}
if ( !status ) {
status = rfstatfs64(path,&statfsbuf) ;
rcode = errno ;
log(LOG_INFO,"srstatfs64: path : %s , status %d\n",path,status ) ;
}
/*
* Shipping the results
*/
}
p = rqstbuf ;
marshall_LONG( p, statfsbuf.bsize ) ;
marshall_HYPER( p, statfsbuf.totblks ) ;
marshall_HYPER( p, statfsbuf.freeblks ) ;
marshall_HYPER( p, statfsbuf.totnods ) ;
marshall_HYPER( p, statfsbuf.freenods ) ;
marshall_LONG( p, status ) ;
marshall_LONG( p, rcode ) ;
log(LOG_DEBUG, "srstatfs64: sending back %d\n", status);
serrno = 0;
if (netwrite_timeout(s,rqstbuf,3*LONGSIZE+4*HYPERSIZE,RFIO_CTRL_TIMEOUT) != (3*LONGSIZE+4*HYPERSIZE)) {
log(LOG_ERR, "srstatfs64: netwrite_timeout(): %s\n", neterror());
return -1 ;
}
return status ;
}
int sropen64(s, rt, host)
SOCKET s;
int rt; /* Is it a remote site call ? */
char *host; /* Where the request comes from */
{
char *auth_id;
char *mech;
int status;
int rcode = 0;
char *p;
int len;
int replen;
int fd ;
LONG flags, mode;
int uid,gid;
WORD mask, ftype, passwd, mapping;
char account[MAXACCTSIZE]; /* account string */
char user[CA_MAXUSRNAMELEN+1]; /* User name */
char reqhost[MAXHOSTNAMELEN];
off64_t offsetin, offsetout;
SOCKET sock;
char tmpbuf[21], tmpbuf2[21];
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
p = rqstbuf + 2*WORDSIZE ;
unmarshall_LONG(p, len) ;
if ( (status = srchkreqsize(s,p,len)) == -1 ) {
rcode = errno;
} else {
/*
* Reading open request.
*/
log(LOG_DEBUG, "sropen64(%d): reading %d bytes\n", s, len) ;
serrno = 0;
if ((status = netread_timeout(s,rqstbuf,len,RFIO_CTRL_TIMEOUT)) != len) {
log(LOG_ERR, "sropen64: read(): %s\n", neterror());
return -1 ;
}
p = rqstbuf ;
status = 0;
*account = *filename = *user = *reqhost = '\0';
unmarshall_WORD(p, uid);
unmarshall_WORD(p, gid);
unmarshall_WORD(p, mask);
unmarshall_WORD(p, ftype);
unmarshall_LONG(p, flags);
unmarshall_LONG(p, mode);
if (unmarshall_STRINGN(p, account, MAXACCTSIZE) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = E2BIG;
}
}
if (unmarshall_STRINGN(p, filename, MAXFILENAMSIZE) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = SENAMETOOLONG;
}
}
if (unmarshall_STRINGN(p, user, CA_MAXUSRNAMELEN+1) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = E2BIG;
}
}
if (unmarshall_STRINGN(p, reqhost, MAXHOSTNAMELEN) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = E2BIG;
}
}
unmarshall_LONG(p, passwd);
unmarshall_WORD(p, mapping);
get_client_actual_id(&uid, &gid, &mech, &auth_id, &rt, &mapping);
log(LOG_DEBUG, "sropen64: Opening file %s for remote user: %s\n", filename, user);
if (rt)
log(LOG_DEBUG, "sropen64: Mapping : %s\n", mapping ? "yes" : "no" );
if (rt && !mapping) {
log(LOG_DEBUG, "sropen64: name : %d, uid: %d, gid: %d\n", passwd, uid, gid);
}
/*
* Someone in the site has tried to specify (uid,gid) directly !
*/
if ( (status == 0) && !mapping && !rt) {
log(LOG_INFO, "sropen64: attempt to make non-mapped I/O and modify uid or gid !\n");
errno=EACCES ;
rcode=errno ;
status= -1 ;
}
if ( (status == 0) && rt ) {
openlog("rfio", LOG_PID, LOG_USER);
syslog(LOG_ALERT, "sropen64: connection %s mapping by %s(%d,%d) from %s",
(mapping ? "with" : "without"), user, uid, gid, host) ;
closelog() ;
}
/*
* MAPPED mode: user will be mapped to user "to"
*/
if ( !status && rt && mapping ) {
char to[100];
int rcd,to_uid,to_gid;
log(LOG_DEBUG, "sropen64: Mapping (%s, %d, %d) \n", user, uid, gid );
if ( (rcd = get_user(host,user,uid,gid,to,&to_uid,&to_gid)) == -ENOENT ) {
log(LOG_ERR, "sropen64: get_user() error opening mapping file\n") ;
status = -1;
errno = EINVAL;
rcode = SEHOSTREFUSED ;
}
else if ( abs(rcd) == 1 ) {
log(LOG_ERR, "sropen64: No entry found in mapping file for (%s,%s,%d,%d)\n",
host,user,uid,gid);
status = -1;
errno = EACCES;
rcode = SEHOSTREFUSED;
}
else {
log(LOG_DEBUG, "sropen64: (%s,%s,%d,%d) mapped to %s(%d,%d)\n",
host, user, uid, gid, to, to_uid, to_gid) ;
uid = to_uid ;
gid = to_gid ;
if ( uid < 100 || gid < 100 ) {
errno = EACCES;
status = -1;
rcode = SEHOSTREFUSED;
}
}
}
/*
* DIRECT access: the user specifies uid & gid by himself
*/
if ( !status && rt && !mapping ){
char * rtuser;
if ( (rtuser=getconfent ("RTUSER","CHECK",0) ) == NULL || ! strcmp (rtuser,"YES") )
{
/* Port is also passwd */
#if defined(_WIN32)
if( (sock = connecttpread(reqhost, passwd)) != INVALID_SOCKET
&& !checkkey(sock, passwd) )
#else
if( (sock = connecttpread(reqhost, passwd)) >= 0 && !checkkey(sock, passwd) )
#endif
{
status= -1 ;
errno = EACCES;
rcode= errno ;
log(LOG_ERR, "sropen64: DIRECT mapping : permission denied\n");
}
#if defined(_WIN32)
if( sock == INVALID_SOCKET )
#else
if( sock < 0 )
#endif
{
status= -1 ;
log(LOG_ERR, "sropen64: DIRECT mapping failed: Couldn't connect %s\n", reqhost);
rcode = EACCES;
}
}
else
log(LOG_INFO, "sropen64: Any DIRECT rfio request from out of site is authorized\n");
}
if ( !status ) {
char *pfn = NULL;
int need_user_check = 1;
log(LOG_DEBUG, "sropen64: uid %d gid %d mask %o ftype %d flags 0%o mode 0%o\n",
uid, gid, mask, ftype, flags, mode);
log(LOG_DEBUG, "sropen64: account: %s\n", account);
log(LOG_INFO, "sropen64: (%s,0%o,0%o) for (%d,%d)\n",
filename, flags, mode, uid, gid);
status = rfio_handle_open(filename, ntohopnflg(flags), mode, mech, auth_id,
uid, gid, &pfn, &handler_context, &need_user_check);
if (status < 0) {
char alarmbuf[1024];
rcode = serrno;
sprintf(alarmbuf,"sropen(): %s",filename) ;
log(LOG_DEBUG, "sropen: rfio_handler_open refused open: %s\n", sstrerror(serrno)) ;
rfio_alrm(rcode,alarmbuf) ;
}
if (! status && need_user_check && ((status=chsuser(uid,gid,host,&rcode,((ntohopnflg(flags) & (O_WRONLY|O_RDWR)) != 0) ? "WTRUST" : "RTRUST")) < 0) &&
((status=chsuser(uid,gid,host,&rcode,"OPENTRUST")) < 0) ) {
if (status == -2)
log(LOG_ERR, "sropen64: uid %d not allowed to open64()\n", uid);
else
log(LOG_ERR, "sropen64: failed at chsuser(), rcode %d\n", rcode);
status = -1 ;
}
if (! status) {
if (! need_user_check) { /* managed file */
mode = 0660;
(void) umask(0) ;
} else
(void) umask(mask) ;
errno = 0;
fd = open64(filename, ntohopnflg(flags), mode) ;
log(LOG_DEBUG, "sropen64: open64(%s,0%o,0%o) returned %x (hex)\n",
filename, flags, mode, fd);
if (fd < 0) {
char alarmbuf[1024];
sprintf(alarmbuf,"sropen64: %s", filename) ;
status= -1 ;
rcode= errno ;
log(LOG_DEBUG, "sropen64: open64: %s\n", strerror(errno)) ;
rfio_alrm(rcode,alarmbuf) ;
}
else {
if (! need_user_check && (ntohopnflg(flags) & O_CREAT)) /* new managed file */
chown(filename, stagersuperuser.pw_uid, stagersuperuser.pw_gid);
/*
* Getting current offset
*/
offsetin = 0;
errno = 0;
offsetout= lseek64(fd, offsetin, SEEK_CUR) ;
log(LOG_DEBUG, "sropen64: lseek64(%d,%s,SEEK_CUR) returned %s\n",
fd, u64tostr(offsetin,tmpbuf,0), u64tostr(offsetout,tmpbuf2,0)) ;
if ( offsetout < 0 ) {
rcode = errno ;
status = -1;
}
else status = 0;
}
}
if (pfn != NULL) free(pfn);
}
}
/*
* Sending back status.
*/
p= rqstbuf ;
marshall_WORD(p,RQST_OPEN64) ;
marshall_LONG(p,status);
marshall_LONG(p,rcode) ;
marshall_LONG(p,0) ;
marshall_HYPER(p,offsetout) ;
#if defined(SACCT)
rfioacct(RQST_OPEN64,uid,gid,s,(int)ntohopnflg(flags),(int)mode,status,rcode,NULL,filename,NULL);
#endif /* SACCT */
log(LOG_DEBUG, "sropen64: sending back status(%d) and errno(%d)\n", status, rcode);
replen = WORDSIZE+3*LONGSIZE+HYPERSIZE;
serrno = 0;
if (netwrite_timeout(s,rqstbuf,replen,RFIO_CTRL_TIMEOUT) != replen) {
log(LOG_ERR, "sropen64: write(): %s\n", neterror());
return -1 ;
}
return fd ;
}
int srwrite64(s, infop, fd)
SOCKET s;
int fd;
struct rfiostat * infop ;
{
int status; /* Return code */
int rcode; /* To send back errno */
int how; /* lseek mode */
off64_t offset; /* lseek offset */
off64_t offsetout; /* lseek offset */
int size; /* Requeste write size */
char *p; /* Pointer to buffer */
int reqlen; /* residual request len */
int replen=WORDSIZE+3*LONGSIZE; /* Reply buffer length */
char tmpbuf[21];
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
p= rqstbuf + 2*WORDSIZE ;
unmarshall_LONG(p, size);
unmarshall_LONG(p, how) ;
/*
* Receiving request: we have 4 bytes more to be read
*/
p= rqstbuf + RQSTSIZE ;
reqlen = HYPERSIZE;
log(LOG_DEBUG, "srwrite64(%d, %d)): reading %d bytes\n", s, fd, reqlen) ;
serrno = 0;
if ((status = netread_timeout(s, p, reqlen, RFIO_CTRL_TIMEOUT)) != reqlen) {
log(LOG_ERR, "srwrite64: read(): %s\n", neterror());
return -1 ;
}
unmarshall_HYPER(p,offset) ;
log(LOG_DEBUG, "srwrite64(%d, %d): size %d, how %d offset %s\n", s, fd, size,
how, u64tostr(offset,tmpbuf,0));
/*
* Checking if buffer is large enough.
*/
if (iobufsiz < size) {
int optval ; /* setsockopt opt value */
if (iobufsiz > 0) {
log(LOG_DEBUG, "srwrite64: freeing %x\n", iobuffer);
(void) free(iobuffer) ;
}
if ((iobuffer = malloc(size)) == NULL) {
status= -1 ;
rcode= errno ;
p= rqstbuf ;
marshall_WORD(p,RQST_WRITE64) ;
marshall_LONG(p,status) ;
marshall_LONG(p,rcode) ;
marshall_LONG(p,0) ;
log(LOG_ERR, "srwrite64: malloc(): %s\n", strerror(errno)) ;
serrno = 0;
if ( netwrite_timeout(s,rqstbuf,replen,RFIO_CTRL_TIMEOUT) != replen ) {
log(LOG_ERR, "srwrite64: netwrite(): %s\n", neterror());
return -1 ;
}
return -1 ;
}
iobufsiz = size ;
optval = (iobufsiz > 64 * 1024) ? iobufsiz : (64 * 1024);
log(LOG_DEBUG, "srwrite64: allocated %d bytes at %x\n", size, iobuffer) ;
serrno = 0;
if( setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*)&optval, sizeof(optval)) == SOCKET_ERROR )
log(LOG_ERR, "srwrite64: setsockopt(SO_RCVBUF): %s\n", neterror());
else
log(LOG_DEBUG, "srwrite64: setsockopt(SO_RCVBUF): %d\n", optval) ;
}
/*
* Reading data on the network.
*/
p= iobuffer;
serrno = 0;
if (netread_timeout(s,p,size,RFIO_DATA_TIMEOUT) != size) {
log(LOG_ERR, "srwrite64: read(): %s\n", neterror());
return -1 ;
}
/*
* lseek() if needed.
*/
if ( how != -1 ) {
log(LOG_DEBUG, "srwrite64(%d,%d): lseek64(%d,%s,%d)\n", s, fd, fd,
u64tostr(offset,tmpbuf,0), how) ;
infop->seekop++;
if ( (offsetout = lseek64(fd,offset,how)) == -1 ) {
rcode= errno ;
status = -1;
p= rqstbuf ;
marshall_WORD(p,RQST_WRITE64) ;
marshall_LONG(p,status) ;
marshall_LONG(p,rcode) ;
marshall_LONG(p,0) ;
serrno = 0;
if ( netwrite_timeout(s,rqstbuf,replen,RFIO_CTRL_TIMEOUT) != replen ) {
log(LOG_ERR, "srwrite64: netwrite(): %s\n", neterror());
return -1 ;
}
return -1 ;
}
}
/*
* Writing data on disk.
*/
infop->wnbr+= size ;
log(LOG_DEBUG, "srwrite64: writing %d bytes on %d\n", size, fd);
status = write(fd,p,size) ;
rcode= ( status < 0 ) ? errno : 0 ;
if ( status < 0 ) {
char alarmbuf[1024];
sprintf(alarmbuf,"srwrite64(): %s",filename) ;
rfio_alrm(rcode,alarmbuf) ;
}
p = rqstbuf;
marshall_WORD(p,RQST_WRITE64) ;
marshall_LONG(p,status) ;
marshall_LONG(p,rcode) ;
marshall_LONG(p,0) ;
log(LOG_DEBUG, "srwrite64: status %d, rcode %d\n", status, rcode) ;
serrno = 0;
if (netwrite_timeout(s,rqstbuf,replen,RFIO_CTRL_TIMEOUT) != replen) {
log(LOG_ERR, "srwrite64: write(): %s\n", neterror());
return -1 ;
}
return status ;
}
int srread64(s, infop, fd)
SOCKET s;
int fd;
struct rfiostat * infop ;
{
int status ; /* Return code */
int rcode ; /* To send back errno */
int how ; /* lseek mode */
off64_t offset ; /* lseek offset */
off64_t offsetout ; /* lseek offset */
int size ; /* Requested read size */
char *p ; /* Pointer to buffer */
int msgsiz ; /* Message size */
int reqlen; /* residual request length */
int replen=WORDSIZE+3*LONGSIZE; /* Reply header length */
char tmpbuf[21];
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
p= rqstbuf + 2*WORDSIZE ;
unmarshall_LONG(p, size) ;
unmarshall_LONG(p,how) ;
/*
* Receiving request: we have 8 bytes more to read
*/
p= rqstbuf + RQSTSIZE ;
reqlen = HYPERSIZE;
log(LOG_DEBUG, "srread64(%d, %d): reading %d bytes\n", s, fd, reqlen) ;
serrno = 0;
if ((status = netread_timeout(s, p , reqlen, RFIO_CTRL_TIMEOUT)) != reqlen) {
log(LOG_ERR, "srread64: read(): %s\n", neterror());
return -1 ;
}
unmarshall_HYPER(p,offset) ;
/*
* lseek() if needed.
*/
if ( how != -1 ) {
log(LOG_DEBUG, "srread64(%d,%d): lseek64(%d,%s,%d)\n", s, fd, fd,
u64tostr(offset,tmpbuf,0), how) ;
infop->seekop++;
if ( (offsetout= lseek64(fd,offset,how)) == -1 ) {
rcode= errno ;
status = -1;
p= rqstbuf ;
marshall_WORD(p,RQST_READ64) ;
marshall_LONG(p,status) ;
marshall_LONG(p,rcode) ;
marshall_LONG(p,0) ;
serrno = 0;
if ( netwrite_timeout(s,rqstbuf,replen,RFIO_CTRL_TIMEOUT) != replen ) {
log(LOG_ERR, "srread64: write(): %s\n", neterror());
return -1 ;
}
return -1 ;
}
}
/*
* Allocating buffer if not large enough.
*/
log(LOG_DEBUG, "srread64(%d, %d): checking buffer size %d\n", s, fd, size);
if (iobufsiz < (size+replen)) {
int optval ; /* setsockopt opt value */
if (iobufsiz > 0) {
log(LOG_DEBUG, "srread64: freeing %x\n", iobuffer);
(void) free(iobuffer);
}
if ((iobuffer = malloc(size+replen)) == NULL) {
status= -1 ;
rcode= errno ;
log(LOG_ERR, "srread64: malloc(): %s\n", strerror(errno)) ;
p= rqstbuf ;
marshall_WORD(p,RQST_READ64) ;
marshall_LONG(p,status) ;
marshall_LONG(p,rcode) ;
marshall_LONG(p,0) ;
serrno = 0;
if ( netwrite_timeout(s,rqstbuf,replen,RFIO_CTRL_TIMEOUT) != replen ) {
log(LOG_ERR, "srread64: netwrite(): %s\n", neterror());
return -1 ;
}
return -1 ;
}
iobufsiz = size + replen ;
log(LOG_DEBUG, "srread64: allocated %d bytes at %x\n", size, iobuffer);
optval = (iobufsiz > 64 * 1024) ? iobufsiz : (64 * 1024);
serrno = 0;
if( setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char*)&optval, sizeof(optval))
== SOCKET_ERROR )
log(LOG_ERR, "srread64: setsockopt(SO_SNDBUF): %s\n", neterror());
else
log(LOG_DEBUG, "srread64: setsockopt(SO_SNDBUF): %d\n", optval);
}
p = iobuffer + replen;
status = read(fd, p, size) ;
if ( status < 0 ) {
char alarmbuf[1024];
sprintf(alarmbuf,"srread64(): %s",filename) ;
rcode= errno ;
msgsiz= replen ;
rfio_alrm(rcode,alarmbuf) ;
} else {
rcode= 0 ;
infop->rnbr+= status ;
msgsiz= status+replen ;
}
p= iobuffer ;
marshall_WORD(p,RQST_READ64) ;
marshall_LONG(p,status) ;
marshall_LONG(p,rcode) ;
marshall_LONG(p,status) ;
log(LOG_DEBUG, "srread64: returning status %d, rcode %d\n", status, rcode) ;
serrno = 0;
if (netwrite_timeout(s,iobuffer,msgsiz,RFIO_CTRL_TIMEOUT) != msgsiz) {
log(LOG_ERR, "srread64: write(): %s\n", neterror());
return -1 ;
}
return status ;
}
int srreadahd64(s, infop, fd)
SOCKET s;
int fd;
struct rfiostat *infop ;
{
int status; /* Return code */
int rcode; /* To send back errno */
int how; /* lseek mode */
off64_t offset; /* lseek offset */
off64_t offsetout; /* lseek offset */
int size; /* Requested read size */
int first; /* First block sent */
char *p; /* Pointer to buffer */
int reqlen; /* residual request length */
char tmpbuf[21];
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
/*
* Receiving request.
*/
log(LOG_DEBUG, "rreadahd64(%d, %d)\n",s, fd);
p= rqstbuf + 2*WORDSIZE ;
unmarshall_LONG(p,size);
unmarshall_LONG(p,how) ;
/*
* Receiving request: we have 8 bytes more to read
*/
p= rqstbuf + RQSTSIZE ;
reqlen = HYPERSIZE;
log(LOG_DEBUG, "rreadahd64(%d, %d): reading %d bytes\n", s, fd, reqlen) ;
serrno = 0;
if ((status = netread_timeout(s, p , reqlen, RFIO_CTRL_TIMEOUT)) != reqlen) {
log(LOG_ERR, "rreadahd64: read(): %s\n", neterror());
return -1 ;
}
unmarshall_HYPER(p,offset) ;
/*
* lseek() if needed.
*/
if ( how != -1 ) {
log(LOG_DEBUG,"rread64(%d,%d): lseek64(%d,%s,%d)\n", s, fd, fd,
u64tostr(offset,tmpbuf,0), how) ;
infop->seekop++;
if ( (offsetout= lseek64(fd,offset,how)) == -1 ) {
rcode= errno ;
status= -1 ;
p= iobuffer ;
marshall_WORD(p,RQST_FIRSTREAD) ;
marshall_LONG(p,status) ;
marshall_LONG(p,rcode) ;
serrno = 0;
if ( netwrite_timeout(s,iobuffer,iobufsiz,RFIO_CTRL_TIMEOUT) != iobufsiz ) {
log(LOG_ERR, "rreadahd64(): netwrite_timeout(): %s\n",neterror());
return -1 ;
}
return status ;
}
}
/*
* Allocating buffer if not large enough.
*/
log(LOG_DEBUG, "rreadahd64(%d, %d): checking buffer size %d\n", s, fd, size);
if (iobufsiz < (size+WORDSIZE+3*LONGSIZE)) {
int optval ; /* setsockopt opt value */
if (iobufsiz > 0) {
log(LOG_DEBUG, "rreadahd64(): freeing %x\n",iobuffer);
(void) free(iobuffer);
}
if ((iobuffer = malloc(size+WORDSIZE+3*LONGSIZE)) == NULL) {
log(LOG_ERR, "rreadahd64: malloc(): %s\n", strerror(errno)) ;
(void) close(s) ;
return -1 ;
}
iobufsiz = size+WORDSIZE+3*LONGSIZE ;
optval = (iobufsiz > 64 * 1024) ? iobufsiz : (64 * 1024);
log(LOG_DEBUG, "rreadahd64(): allocated %d bytes at %x\n",iobufsiz,iobuffer);
serrno = 0;
if( setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char*)&optval, sizeof(optval)) == SOCKET_ERROR )
log(LOG_ERR, "rreadahd64(): setsockopt(SO_SNDBUF): %s\n", neterror());
else
log(LOG_DEBUG, "rreadahd64(): setsockopt(SO_SNDBUF): %d\n",optval) ;
}
/*
* Reading data and sending it.
*/
for(first= 1;;first= 0) {
fd_set fds ;
struct timeval timeout ;
/*
* Has a new request arrived ?
*/
FD_ZERO(&fds) ;
FD_SET(s,&fds) ;
timeout.tv_sec = 0 ;
timeout.tv_usec= 0 ;
serrno = 0;
if( select(FD_SETSIZE, &fds, (fd_set*)0, (fd_set*)0, &timeout) == SOCKET_ERROR ) {
log(LOG_ERR,"rreadahd64(): select(): %s\n", neterror());
return -1;
}
if ( FD_ISSET(s,&fds) ) {
log(LOG_DEBUG,"rreadahd64(): returns because of new request\n") ;
return 0 ;
}
/*
* Reading disk ...
*/
p= iobuffer + WORDSIZE + 3*LONGSIZE ;
status = read(fd,p,size);
if (status < 0) {
rcode= errno ;
iobufsiz= WORDSIZE+3*LONGSIZE ;
}
else {
rcode= 0 ;
infop->rnbr+= status ;
iobufsiz = status+WORDSIZE+3*LONGSIZE ;
}
log(LOG_DEBUG, "rreadahd64: status %d, rcode %d\n", status, rcode);
/*
* Sending data.
*/
p= iobuffer ;
marshall_WORD(p,(first)?RQST_FIRSTREAD:RQST_READAHD64) ;
marshall_LONG(p,status) ;
marshall_LONG(p, rcode) ;
marshall_LONG(p, status) ;
serrno = 0;
if ( netwrite_timeout(s, iobuffer, iobufsiz, RFIO_CTRL_TIMEOUT) != iobufsiz ) {
log(LOG_ERR, "rreadahd64(): netwrite_timeout(): %s\n", neterror());
return -1 ;
}
/*
* The end of file has been reached
* or an error was encountered.
*/
if ( status != size ) {
return 0 ;
}
}
}
int srfstat64(s, infop, fd)
SOCKET s;
int fd;
struct rfiostat *infop;
{
int status;
int rcode = 0;
int msgsiz;
int headlen = 3*LONGSIZE+WORDSIZE;
char *p;
struct stat64 statbuf;
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
log(LOG_DEBUG, "rfstat64(%d, %d)\n", s, fd) ;
infop->statop++;
/*
* Issuing the fstat()
*/
status= fstat64(fd, &statbuf) ;
if ( status < 0) {
rcode= errno ;
log(LOG_ERR,"rfstat64(%d,%d): fstat64 gave rc %d, errno=%d\n", s, fd, status, errno);
}
else errno = 0;
#if !defined(_WIN32)
msgsiz= 5*WORDSIZE + 4*LONGSIZE + 3*HYPERSIZE ;
#else
msgsiz= 5*WORDSIZE + 3*LONGSIZE + 2*HYPERSIZE ;
#endif
p = rqstbuf;
marshall_WORD(p, RQST_FSTAT64) ;
marshall_LONG(p, status) ;
marshall_LONG(p, rcode) ;
marshall_LONG(p, msgsiz) ;
marshall_WORD(p, statbuf.st_dev);
marshall_HYPER(p, statbuf.st_ino);
marshall_WORD(p, statbuf.st_mode);
marshall_WORD(p, statbuf.st_nlink);
marshall_WORD(p, statbuf.st_uid);
marshall_WORD(p, statbuf.st_gid);
marshall_HYPER(p, statbuf.st_size);
marshall_LONG(p, statbuf.st_atime);
marshall_LONG(p, statbuf.st_mtime);
marshall_LONG(p, statbuf.st_ctime);
#if !defined(_WIN32)
marshall_LONG(p, statbuf.st_blksize);
marshall_HYPER(p, statbuf.st_blocks);
#endif
log(LOG_DEBUG, "rfstat64(%d,%d): sending back %d bytes, status=%d\n", s, fd, msgsiz, status) ;
serrno = 0;
if (netwrite_timeout(s,rqstbuf,headlen+msgsiz,RFIO_CTRL_TIMEOUT) != (headlen+msgsiz) ) {
log(LOG_ERR,"rfstat64(%d,%d): netwrite(): %s\n", s, fd, neterror());
return -1 ;
}
return 0 ;
}
int srlseek64(s, request, infop, fd)
SOCKET s;
int request;
int fd;
struct rfiostat *infop;
{
int status;
int rcode;
int rlen;
off64_t offset;
off64_t offsetout;
int how;
char *p;
char tmpbuf[21];
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
p= rqstbuf + 2*WORDSIZE ;
unmarshall_HYPER(p,offset) ;
unmarshall_LONG(p,how) ;
log(LOG_DEBUG, "srlseek64(%d, %d): offset64 %s, how: %x\n", s, fd,
u64tostr(offset,tmpbuf,0), how) ;
offsetout = lseek64(fd, offset, how) ;
if (offsetout == (off64_t)-1 ) status = -1;
else status = 0;
rcode= ( status < 0 ) ? errno : 0 ;
log(LOG_DEBUG, "srlseek64: status %s, rcode %d\n", u64tostr(offsetout,tmpbuf,0), rcode) ;
p= rqstbuf ;
marshall_WORD(p,request) ;
marshall_HYPER(p, offsetout) ;
marshall_LONG(p,rcode) ;
rlen = WORDSIZE+LONGSIZE+HYPERSIZE;
serrno = 0;
if (netwrite_timeout(s,rqstbuf,rlen,RFIO_CTRL_TIMEOUT) != rlen) {
log(LOG_ERR, "srlseek64: netwrite(): %s\n", neterror()) ;
return -1 ;
}
return status ;
}
int srpreseek64(s, infop, fd)
SOCKET s;
int fd;
struct rfiostat *infop;
{
int status; /* Return code */
int size; /* Buffer size */
int nblock; /* Nb of blocks to read */
int first; /* First block sent */
char *p; /* Pointer to buffer */
int reqno; /* Request number */
struct iovec64 *v; /* List of requests */
char *trp = NULL; /* Temporary buffer */
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
p= rqstbuf + 2*WORDSIZE ;
unmarshall_LONG(p,size) ;
unmarshall_LONG(p,nblock) ;
log(LOG_DEBUG,"rpreseek64(%d, %d)\n",s,fd) ;
/*
* A temporary buffer may need to be created
* to receive the request.
*/
if ( nblock*(HYPERSIZE+LONGSIZE) > BUFSIZ ) {
if ( (trp= (char *) malloc(nblock*(HYPERSIZE+LONGSIZE))) == NULL ) {
return -1 ;
}
}
p= ( trp ) ? trp : rqstbuf ;
/*
* Receiving the request.
*/
log(LOG_DEBUG,"rpreseek64: reading %d bytes\n",nblock*(HYPERSIZE+LONGSIZE)) ;
serrno = 0;
if ( netread_timeout(s,p,nblock*(HYPERSIZE+LONGSIZE),RFIO_CTRL_TIMEOUT) != (nblock*(HYPERSIZE+LONGSIZE)) ) {
log(LOG_ERR,"rpreseek64: netread(): %s\n", neterror());
if ( trp ) (void) free(trp) ;
return -1 ;
}
/*
* Allocating space for the list of requests.
*/
if ( (v= ( struct iovec64 *) malloc(nblock*sizeof(struct iovec64))) == NULL ) {
log(LOG_ERR, "rpreseek64: malloc(): %s\n",strerror(errno)) ;
if ( trp ) (void) free(trp) ;
(void) close(s) ;
return -1 ;
}
/*
* Filling list request.
*/
for(reqno= 0;reqno<nblock;reqno++) {
unmarshall_HYPER(p,v[reqno].iov_base) ;
unmarshall_LONG(p,v[reqno].iov_len) ;
}
/*
* Freeing temporary buffer if needed.
*/
if ( trp ) (void) free(trp) ;
/*
* Allocating new data buffer if the
* current one is not large enough.
*/
log(LOG_DEBUG,"rpreseek64(%d, %d): checking buffer size %d\n",s,fd,size) ;
if (iobufsiz < (size+WORDSIZE+3*LONGSIZE)) {
int optval ; /* setsockopt opt value */
if (iobufsiz > 0) {
log(LOG_DEBUG, "rpreseek64(): freeing %x\n",iobuffer);
(void) free(iobuffer);
}
if ((iobuffer = malloc(size+WORDSIZE+3*LONGSIZE)) == NULL) {
log(LOG_ERR, "rpreseek64: malloc(): %s\n", strerror(errno)) ;
(void) close(s) ;
return -1 ;
}
iobufsiz = size+WORDSIZE+3*LONGSIZE ;
optval = (iobufsiz > 64 * 1024) ? iobufsiz : (64 * 1024);
log(LOG_DEBUG, "rpreseek64(): allocated %d bytes at %x\n",iobufsiz,iobuffer) ;
serrno = 0;
if( setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char*)&optval, sizeof(optval)) == SOCKET_ERROR )
log(LOG_ERR, "rpreseek64(): setsockopt(SO_SNDBUF): %s\n", neterror());
else
log(LOG_DEBUG, "rpreseek64(): setsockopt(SO_SNDBUF): %d\n",optval) ;
}
/*
* Reading data and sending it.
*/
reqno= 0 ;
for(first= 1;;first= 0) {
struct timeval timeout ;
fd_set fds ;
int nbfree ;
int nb ;
off64_t offsetout ;
/*
* Has a new request arrived ?
* The preseek is then interrupted.
*/
FD_ZERO(&fds) ;
FD_SET(s,&fds) ;
timeout.tv_sec = 0 ;
timeout.tv_usec= 0 ;
serrno = 0;
if( select(FD_SETSIZE, &fds, (fd_set*)0, (fd_set*)0, &timeout) == SOCKET_ERROR ) {
log(LOG_ERR,"rpreseek64(): select(): %s\n", neterror());
return -1;
}
if ( FD_ISSET(s,&fds) ) {
log(LOG_DEBUG,"rpreseek64(): returns because of new request\n") ;
return 0 ;
}
/*
* Filling buffer.
*/
p= iobuffer + WORDSIZE + 3*LONGSIZE ;
for(nb= 0, nbfree= size; HYPERSIZE+3*LONGSIZE<nbfree && reqno<nblock; reqno ++, nb ++ ) {
nbfree -= HYPERSIZE+3*LONGSIZE ;
offsetout= lseek64(fd,v[reqno].iov_base,SEEK_SET) ;
if ( offsetout == (off64_t)-1 ) {
status= -1 ;
marshall_HYPER(p,v[reqno].iov_base) ;
marshall_LONG(p,v[reqno].iov_len) ;
marshall_LONG(p,status) ;
marshall_LONG(p,errno) ;
continue ;
}
if ( v[reqno].iov_len <= nbfree ) {
status= read(fd,p+HYPERSIZE+3*LONGSIZE,v[reqno].iov_len) ;
marshall_HYPER(p,v[reqno].iov_base) ;
marshall_LONG(p,v[reqno].iov_len) ;
marshall_LONG(p,status) ;
marshall_LONG(p,errno) ;
if ( status != -1 ) {
infop->rnbr += status ;
nbfree -= status ;
p += status ;
}
}
else {
/*
* The record is broken into two pieces.
*/
status= read(fd,p+HYPERSIZE+3*LONGSIZE,nbfree) ;
marshall_HYPER(p,v[reqno].iov_base) ;
marshall_LONG(p,nbfree) ;
marshall_LONG(p,status) ;
marshall_LONG(p,errno) ;
if ( status != -1 ) {
infop->rnbr += status ;
nbfree -= status ;
p += status ;
v[reqno].iov_base += status ;
v[reqno].iov_len -= status ;
reqno -- ;
}
}
}
p= iobuffer ;
if ( first ) {
marshall_WORD(p,RQST_FIRSTSEEK) ;
}
else {
marshall_WORD(p,(reqno == nblock) ? RQST_LASTSEEK:RQST_PRESEEK64) ;
}
marshall_LONG(p,nb) ;
marshall_LONG(p,0) ;
marshall_LONG(p,size) ;
log(LOG_DEBUG,"rpreseek64(): sending %d bytes\n",iobufsiz) ;
serrno = 0;
if ( netwrite_timeout(s,iobuffer,iobufsiz,RFIO_CTRL_TIMEOUT) != iobufsiz ) {
log(LOG_ERR, "rpreseek64(): netwrite_timeout(): %s\n", neterror());
return -1 ;
}
/*
* All the data needed has been
* sent over the network.
*/
if ( reqno == nblock )
return 0 ;
}
}
/* Warning : the new sequential transfer mode cannot be used with
several files open at a time (because of those global variables)*/
#if !defined(_WIN32) /* moved to global structure */
static int data_sock; /* Data socket */
static int ctrl_sock; /* the control socket */
static int first_write;
static int first_read;
static off64_t byte_read_from_network;
static struct rfiostat myinfo;
#endif /* WIN32 */
int sropen64_v3(s, rt, host)
SOCKET s;
int rt; /* Is it a remote site call ? */
char *host; /* Where the request comes from */
{
char *auth_id;
char *mech;
int status;
int rcode = 0;
char *p;
int len;
int replen;
int fd = -1;
LONG flags, mode;
int uid,gid;
WORD mask, ftype, passwd, mapping;
char account[MAXACCTSIZE]; /* account string */
char user[CA_MAXUSRNAMELEN+1]; /* User name */
char reqhost[MAXHOSTNAMELEN];
#if defined(_WIN32)
SOCKET sock;
#else
int sock; /* Control socket */
int data_s; /* Data socket */
#endif
#if defined(_AIX)
socklen_t fromlen;
#else
int fromlen;
#endif
int port;
struct sockaddr_storage from;
extern int max_rcvbuf;
extern int max_sndbuf;
int optlen; /* Socket option length */
int yes; /* Socket option value */
int maxseg; /* Socket option segment*/
off64_t offsetout; /* Offset */
char tmpbuf[21];
#if defined(_WIN32)
struct thData *td;
#endif
extern int bind_v3_data_port (char*, SOCKET);
#if defined(_WIN32)
td = (struct thData*)TlsGetValue(tls_i);
#endif
/* Initialization of global variables */
ctrl_sock = s;
data_sock = -1;
first_write = 1;
first_read = 1;
/* Init myinfo to zeros */
myinfo.readop = myinfo.writop = myinfo.flusop = myinfo.statop = myinfo.seekop
= myinfo.presop = 0;
myinfo.rnbr = myinfo.wnbr = (off64_t)0;
/* Will remain at this value (indicates that the new sequential transfer mode has been used) */
myinfo.aheadop = 1;
byte_read_from_network = (off64_t)0;
p= rqstbuf + 2*WORDSIZE ;
unmarshall_LONG(p, len) ;
if ( (status = srchkreqsize(s,p,len)) == -1 ) {
rcode = errno;
} else {
/*
* Reading open request.
*/
log(LOG_DEBUG,"ropen64_v3(%d): reading %d bytes\n", s, len) ;
serrno = 0;
if ((status = netread_timeout(s,rqstbuf,len,RFIO_CTRL_TIMEOUT)) != len) {
log(LOG_ERR,"ropen64_v3: read(): %s\n", neterror());
return -1 ;
}
p= rqstbuf ;
status = 0;
*account = *filename = *user = *reqhost = '\0';
unmarshall_WORD(p, uid);
unmarshall_WORD(p, gid);
unmarshall_WORD(p, mask);
unmarshall_WORD(p, ftype);
unmarshall_LONG(p, flags);
unmarshall_LONG(p, mode);
if (unmarshall_STRINGN(p, account, MAXACCTSIZE) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = E2BIG;
}
}
if (unmarshall_STRINGN(p, filename, MAXFILENAMSIZE) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = SENAMETOOLONG;
}
}
if (unmarshall_STRINGN(p, user, CA_MAXUSRNAMELEN+1) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = E2BIG;
}
}
if (unmarshall_STRINGN(p, reqhost, MAXHOSTNAMELEN) == -1 ) {
if (status == 0) {
status = -1 ;
rcode = E2BIG;
}
}
unmarshall_LONG(p, passwd);
unmarshall_WORD(p, mapping);
get_client_actual_id(&uid, &gid, &mech, &auth_id, &rt, &mapping);
log(LOG_DEBUG,"ropen64_v3: Opening file %s for remote user: %s\n", filename, user);
if (rt)
log(LOG_DEBUG,"ropen64_v3: Mapping : %s\n", mapping ? "yes" : "no");
if (rt && !mapping) {
log(LOG_DEBUG,"ropen64_v3: user : %d, uid: %d, gid: %d\n", passwd, uid, gid);
}
/*
* Someone in the site has tried to specify (uid,gid) directly !
*/
if (!mapping && !rt) {
log(LOG_INFO,"ropen64_v3: attempt to make non-mapped I/O and modify uid or gid !\n");
errno=EACCES ;
rcode=errno ;
status= -1 ;
}
if ( rt ) {
openlog("rfio", LOG_PID, LOG_USER);
syslog(LOG_ALERT, "rfio: connection %s mapping by %s(%d,%d) from %s\n",
(mapping ? "with" : "without"), user, uid, gid, host) ;
#if !defined(_WIN32)
closelog();
#endif
}
/*
* MAPPED mode: user will be mapped to user "to"
*/
if ( !status && rt && mapping ) {
char to[100];
int rcd,to_uid,to_gid;
log(LOG_DEBUG,"ropen64_v3: Mapping (%s, %d, %d) \n", user, uid, gid );
if ( (rcd = get_user(host,user,uid,gid,to,&to_uid,&to_gid)) == -ENOENT ) {
log(LOG_ERR,"ropen64_v3: get_user() error opening mapping file\n") ;
status = -1;
errno = EINVAL;
rcode = SEHOSTREFUSED ;
}
else if ( abs(rcd) == 1 ) {
log(LOG_ERR,"ropen64_v3: No entry found in mapping file for (%s,%s,%d,%d)\n",
host, user, uid, gid);
status = -1;
errno = EACCES;
rcode = SEHOSTREFUSED;
}
else {
log(LOG_DEBUG,"ropen64_v3: (%s,%s,%d,%d) mapped to %s(%d,%d)\n",
host, user, uid, gid, to, to_uid, to_gid) ;
uid = to_uid ;
gid = to_gid ;
if ( uid < 100 || gid < 100 ) {
errno = EACCES;
status = -1;
rcode = SEHOSTREFUSED;
}
}
}
/*
* DIRECT access: the user specifies uid & gid by himself
*/
if( !status && rt && !mapping ) {
char *rtuser;
if( (rtuser = getconfent("RTUSER","CHECK",0) ) == NULL || ! strcmp (rtuser,"YES") )
{
/* Port is also passwd */
sock = connecttpread(reqhost, passwd);
#if defined(_WIN32)
if( (sock != INVALID_SOCKET) && !checkkey(sock, passwd) )
#else
if( (sock >= 0) && !checkkey(sock, passwd) )
#endif
{
status= -1 ;
errno = EACCES;
rcode= errno ;
log(LOG_ERR,"ropen64_v3: DIRECT mapping : permission denied\n");
}
#if defined(_WIN32)
if( sock == INVALID_SOCKET )
#else
if (sock < 0)
#endif
{
status= -1 ;
log(LOG_ERR,"ropen64_v3: DIRECT mapping failed: Couldn't connect %s\n", reqhost);
rcode = EACCES;
}
}
else
log(LOG_INFO ,"ropen64_v3: Any DIRECT rfio request from out of site is authorized\n");
}
if ( !status ) {
char *pfn = NULL;
int need_user_check = 1;
log(LOG_DEBUG, "ropen64_v3: uid %d gid %d mask %o ftype %d flags 0%o mode 0%o\n",
uid, gid, mask, ftype, flags, mode);
log(LOG_DEBUG, "ropen64_v3: account: %s\n", account);
log(LOG_INFO, "ropen64_v3: (%s,0%o,0%o) for (%d,%d)\n", filename, flags, mode, uid, gid);
status = rfio_handle_open(filename, ntohopnflg(flags), mode, mech, auth_id,
uid, gid, &pfn, &handler_context, &need_user_check);
if (status < 0) {
char alarmbuf[1024];
rcode = serrno;
sprintf(alarmbuf,"sropen(): %s",filename) ;
log(LOG_DEBUG, "sropen: rfio_handler_open refused open: %s\n", sstrerror(serrno)) ;
rfio_alrm(rcode,alarmbuf) ;
}
#if !defined(_WIN32)
if (! status && need_user_check && ((status=chsuser(uid,gid,host,&rcode,((ntohopnflg(flags) & (O_WRONLY|O_RDWR)) != 0) ? "WTRUST" : "RTRUST")) < 0) &&
((status=chsuser(uid,gid,host,&rcode,"OPENTRUST")) < 0) ) {
if (status == -2)
log(LOG_ERR,"ropen64_v3: uid %d not allowed to open()\n", uid);
else
log(LOG_ERR,"ropen64_v3: failed at chsuser(), rcode %d\n", rcode);
status = -1 ;
}
#endif
if (! status) {
if (! need_user_check) { /* managed file */
mode = 0660;
(void) umask(0) ;
} else
(void) umask(mask) ;
fd = open64(filename, ntohopnflg(flags), mode);
#if defined(_WIN32)
_setmode( fd, _O_BINARY ); /* default is text mode */
#endif
if (fd < 0) {
status= -1 ;
rcode= errno ;
log(LOG_DEBUG,"ropen64_v3: open64(%s,0%o,0%o): %s\n",
filename, ntohopnflg(flags), mode, strerror(errno)) ;
}
else {
/* File is opened */
log(LOG_DEBUG,"ropen64_v3: open64(%s,0%o,0%o) returned %d \n",
filename, ntohopnflg(flags), mode, fd) ;
if (! need_user_check && (ntohopnflg(flags) & O_CREAT)) /* new managed file */
chown(filename, stagersuperuser.pw_uid, stagersuperuser.pw_gid);
/*
* Getting current offset
*/
offsetout = lseek64(fd, (off64_t)0, SEEK_CUR) ;
if (offsetout == ((off64_t)-1) ) {
log(LOG_ERR,"ropen64_v3: lseek64(%d,0,SEEK_CUR): %s\n", fd,strerror(errno)) ;
status = -1;
rcode = errno;
}
else {
log(LOG_DEBUG,"ropen64_v3: lseek64(%d,0,SEEK_CUR) returned %s\n",
fd, u64tostr(offsetout,tmpbuf,0)) ;
status = 0;
}
}
}
if (pfn != NULL) free(pfn);
}
if (! status && fd >= 0) {
fromlen = sizeof(from);
serrno = 0;
if (getsockname(ctrl_sock, (struct sockaddr *)&from, &fromlen)) {
log(LOG_ERR, "ropen64_v3: getsockname(): %s\n", neterror());
#if defined(_WIN32)
return(-1);
#else
exit(1);
#endif
}
serrno = 0;
data_s = socket(from.ss_family, SOCK_STREAM, 0);
if( data_s == INVALID_SOCKET ) {
log( LOG_ERR, "ropen64_v3: datasocket(): %s\n", neterror());
#if defined(_WIN32)
return(-1);
#else
exit(1);
#endif
}
log(LOG_DEBUG, "ropen64_v3: data socket created fd=%d\n", data_s);
serrno = 0;
if ((port = bind_v3_data_port ("ropen64_v3", data_s)) < 0) {
#if defined(_WIN32)
return(-1);
#else
exit(1);
#endif
}
log(LOG_INFO, "ropen64_v3: assigning data port %d\n", port);
serrno = 0;
if( listen(data_s, 5) == INVALID_SOCKET ) {
log(LOG_ERR, "ropen64_v3: listen(%d): %s\n", data_s, neterror());
#if defined(_WIN32)
return(-1);
#else
exit(1);
#endif
}
}
}
/*
* Sending back status to the client
*/
p= rqstbuf ;
marshall_WORD(p,RQST_OPEN64_V3) ;
replen = RQSTSIZE+HYPERSIZE;
marshall_LONG(p,status);
marshall_LONG(p,rcode) ;
marshall_LONG(p,port) ;
marshall_HYPER(p, offsetout);
log(LOG_DEBUG, "ropen64_v3: sending back status(%d) and errno(%d)\n", status, rcode);
errno = ECONNRESET;
serrno = 0;
if (netwrite_timeout(s,rqstbuf,replen,RFIO_CTRL_TIMEOUT) != replen) {
log(LOG_ERR,"ropen64_v3: write(): %s\n", neterror());
return -1 ;
}
if (! status && fd >= 0)
{
/*
* The rcvbuf on the data socket must be set _before_
* the connection is accepted! Otherwise the receiver will
* only offer the default window, which for large MTU devices
* (such as HIPPI) is far too small and performance goes down
* the drain.
*
* The sndbuf must be set on the socket returned by accept()
* as it is not inherited (at least not on SGI).
* 98/08/05 - Jes
*/
log(LOG_DEBUG, "ropen64_v3: doing setsockopt %d RCVBUF\n", data_s);
serrno = 0;
if( setsockopt(data_s, SOL_SOCKET, SO_RCVBUF, (char*)&max_rcvbuf,
sizeof(max_rcvbuf)) == SOCKET_ERROR ) {
log(LOG_ERR, "ropen64_v3: setsockopt %d rcvbuf(%d bytes): %s\n",
data_s, max_rcvbuf, neterror());
}
else
log(LOG_DEBUG, "ropen64_v3: setsockopt rcvbuf on data socket %d (%d bytes)\n",
data_s, max_rcvbuf);
for (;;)
{
fromlen = sizeof(from);
log(LOG_DEBUG, "ropen64_v3: wait for accept to complete\n");
serrno = 0;
data_sock = accept(data_s, (struct sockaddr*)&from, &fromlen);
if( data_sock == INVALID_SOCKET ) {
log(LOG_ERR, "ropen64_v3: data accept(%d): %s\n", data_s, neterror());
#if defined(_WIN32)
return(-1);
#else
exit(1);
#endif
}
else break;
}
log(LOG_DEBUG, "ropen64_v3: data accept is ok, fildesc=%d\n", data_sock);
/*
* Set the send socket buffer on the data socket (see comment
* above before accept())
*/
log(LOG_DEBUG, "ropen64_v3: doing setsockopt %d SNDBUF\n", data_sock);
serrno = 0;
if( setsockopt(data_sock, SOL_SOCKET, SO_SNDBUF, (char*)&max_sndbuf,
sizeof(max_sndbuf)) == SOCKET_ERROR) {
log(LOG_ERR, "ropen64_v3: setsockopt %d SNDBUF(%d bytes): %s\n",
data_sock, max_sndbuf, neterror());
} else
log(LOG_DEBUG, "ropen64_v3: setsockopt SNDBUF on data socket %d(%d bytes)\n",
data_sock, max_sndbuf);
/* Set the keepalive option on both sockets */
yes = 1;
serrno = 0;
if( setsockopt(data_sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&yes,
sizeof(yes)) == SOCKET_ERROR) {
log(LOG_ERR, "ropen64_v3: setsockopt keepalive on data socket%d: %s\n", data_sock, neterror());
} else
log(LOG_DEBUG, "ropen64_v3: setsockopt keepalive on data socket %d done\n", data_sock);
yes = 1;
serrno = 0;
if( setsockopt(ctrl_sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&yes,
sizeof(yes)) == SOCKET_ERROR ) {
log(LOG_ERR, "ropen64_v3: setsockopt keepalive on ctrl socket %d: %s\n",
ctrl_sock, neterror());
} else
log(LOG_DEBUG, "ropen64_v3: setsockopt keepalive on ctrl socket %d done\n", ctrl_sock);
#if (defined(__osf__) && defined(__alpha) && defined(DUXV4))
/* Set the keepalive interval to 20 mns instead of the default 2 hours */
yes = 20 * 60;
if (setsockopt(data_sock, IPPROTO_TCP, TCP_KEEPIDLE,(char *)&yes, sizeof(yes)) < 0) {
log(LOG_ERR, "ropen64_v3: setsockopt keepidle on data socket %d: %s\n",
data_sock, strerror(errno));
}
log(LOG_DEBUG, "ropen64_v3: setsockopt keepidle on data socket %d done (%d sec)\n",
data_sock, yes);
yes = 20 * 60;
if (setsockopt(ctrl_sock, IPPROTO_TCP, TCP_KEEPIDLE, (char *)&yes, sizeof(yes)) < 0) {
log(LOG_ERR, "ropen64_v3: setsockopt keepidle on ctrl socket %d: %s\n",
ctrl_sock, strerror(errno));
}
log(LOG_DEBUG, "ropen64_v3: setsockopt keepidle on ctrl socket %d done (%d sec)\n",
ctrl_sock, yes);
#endif
#if !(defined(__osf__) && defined(__alpha) && defined(DUXV4))
/*
* TCP_NODELAY seems to cause small Hippi packets on Digital UNIX 4.x
*/
yes = 1;
serrno = 0;
if( setsockopt(data_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&yes,
sizeof(yes)) == SOCKET_ERROR ) {
log(LOG_ERR, "ropen64_v3: setsockopt nodelay on data socket %d: %s\n",
data_sock, neterror());
} else
log(LOG_DEBUG,"ropen64_v3: setsockopt nodelay option set on data socket %d\n", data_sock);
#endif /* !(defined(__osf__) && defined(__alpha) && defined(DUXV4)) */
yes = 1;
serrno = 0;
if( setsockopt(ctrl_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&yes,
sizeof(yes)) == SOCKET_ERROR ) {
log(LOG_ERR, "ropen64_v3: setsockopt nodelay on ctrl socket %d: %s\n",
ctrl_sock, neterror());
} else
log(LOG_DEBUG,"ropen64_v3: setsockopt nodelay option set on ctrl socket %d\n", ctrl_sock);
}
#if defined(SACCT)
rfioacct(RQST_OPEN64_V3,uid,gid,s,(int)ntohopnflg(flags),(int)mode,status,rcode,NULL,filename,NULL);
#endif /* SACCT */
return fd ;
}
int srclose64_v3(s, infop, fd)
SOCKET s;
int fd;
struct rfiostat *infop;
{
int status;
int rcode;
char *p;
char tmpbuf[21], tmpbuf2[21];
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
/* Issue statistics */
log(LOG_INFO,"rclose64_v3(%d, %d): %d read, %d readahead, %d write, %d flush, %d stat, %d lseek and %d lockf\n",
s, fd, myinfo.readop, myinfo.aheadop, myinfo.writop, myinfo.flusop, myinfo.statop,
myinfo.seekop, myinfo.lockop);
log(LOG_INFO,"rclose64_v3(%d, %d): %s bytes read and %s bytes written\n",
s, fd, u64tostr(myinfo.rnbr,tmpbuf,0), u64tostr(myinfo.wnbr,tmpbuf2,0)) ;
/* Close the local file */
status = close(fd) ;
rcode = ( status < 0 ) ? errno : 0 ;
/* Close the data socket */
if (data_sock >= 0) {
serrno = 0;
if( netclose(data_sock) == SOCKET_ERROR )
log(LOG_DEBUG, "rclose64_v3: Error closing data socket fildesc=%d, error=%s\n",
data_sock, neterror());
else
log(LOG_DEBUG, "rclose64_v3 : closing data socket fildesc=%d\n", data_sock);
data_sock = -1;
}
/* Issue accounting */
#if defined(SACCT)
rfioacct(RQST_CLOSE64_V3,0,0,s,0,0,status,rcode,&myinfo,NULL,NULL);
#endif /* SACCT */
/* Send the answer to the client via ctrl_sock */
p= rqstbuf;
marshall_WORD(p, RQST_CLOSE64_V3);
marshall_LONG(p, status);
marshall_LONG(p, rcode);
errno = ECONNRESET;
serrno = 0;
if (netwrite_timeout(s, rqstbuf, RQSTSIZE, RFIO_CTRL_TIMEOUT) != RQSTSIZE) {
log(LOG_ERR, "rclose64_v3: netwrite(): %s\n", neterror());
return -1 ;
}
/* close the ctrl_sock */
serrno = 0;
if( netclose(s) == SOCKET_ERROR )
log(LOG_DEBUG, "rclose64_v3: Error closing control socket fildesc=%d, error=%s\n",
s, neterror());
else
log(LOG_DEBUG, "rclose64_v3 : closing ctrl socket fildesc=%d\n", s);
return status ;
}
static void *produce64_thread(int *ptr)
{
int fd = *ptr;
int byte_read = -1;
int error = 0;
off64_t total_produced = 0;
char tmpbuf[21];
while ((! error) && (byte_read != 0)) {
if (stop_read)
return (NULL);
log(LOG_DEBUG, "produce64_thread: calling Csemaphore_down(&empty64)\n");
Csemaphore_down(&empty64);
log(LOG_DEBUG, "produce64_thread: read() at %s:%d\n", __FILE__, __LINE__);
byte_read = read(fd,array[produced64 % daemonv3_rdmt_nbuf].p,daemonv3_rdmt_bufsize);
if (byte_read > 0) {
total_produced += byte_read;
/* printf("Has read in buf %d (len %d)\n",produced64 % daemonv3_rdmt_nbuf,byte_read); */
log(LOG_DEBUG, "Has read in buf %d (len %d)\n",produced64 % daemonv3_rdmt_nbuf,byte_read);
array[produced64 % daemonv3_rdmt_nbuf].len = byte_read;
}
else {
if (byte_read == 0) {
log(LOG_DEBUG,"produce64_thread: End of reading : total produced = %s,buffers=%d\n",
u64tostr(total_produced,tmpbuf,0),produced64);
/* array[produced64 % daemonv3_rdmt_nbuf].p = NULL; */
array[produced64 % daemonv3_rdmt_nbuf].len = 0;
}
else {
array[produced64 % daemonv3_rdmt_nbuf].len = -errno;
error = -1;
}
}
produced64++;
log(LOG_DEBUG, "produce64_thread: calling Csemaphore_up(&full64)\n");
Csemaphore_up(&full64);
}
return(NULL);
}
static void *consume64_thread(int *ptr)
{
int fd = *ptr;
int byte_written = -1;
int error = 0, end = 0;
off64_t total_consumed = 0;
char *buffer_to_write;
int len_to_write;
int saved_errno;
char tmpbuf[21];
while ((! error) && (! end)) {
log(LOG_DEBUG, "consume64_thread: calling Csemaphore_down(&full64)\n");
Csemaphore_down(&full64);
buffer_to_write = array[consumed64 % daemonv3_wrmt_nbuf].p;
len_to_write = array[consumed64 % daemonv3_wrmt_nbuf].len;
if (len_to_write > 0) {
log(LOG_DEBUG,"consume64_thread: Trying to write %d bytes from %X\n",
len_to_write, buffer_to_write);
byte_written = write(fd, buffer_to_write, len_to_write);
saved_errno = errno;
log(LOG_DEBUG, "consume64_thread: succeeded to write %d bytes\n", byte_written);
/* If the write is successfull but incomplete (fs is full) we
report the ENOSPC error immediately in order to simplify the
code */
if ((byte_written >= 0) && (byte_written != len_to_write)) {
byte_written = -1;
saved_errno = errno = ENOSPC;
}
/* Error reporting to global var */
if (byte_written == -1) {
error = 1;
if (Cthread_mutex_lock(&write_error)) {
log(LOG_ERR,"consume64_thread: Cannot get mutex : serrno=%d\n", serrno);
return(NULL);
}
write_error = saved_errno;
if (Cthread_mutex_unlock(&write_error)) {
log(LOG_ERR,"consume64_thread: Cannot release mutex : serrno=%d\n", serrno);
return(NULL);
}
log(LOG_DEBUG,"consume64_thread: Error when writing : buffers=%d, error=%d\n",
consumed64,saved_errno);
}
else {
/* All bytes written to disks */
total_consumed += byte_written;
log(LOG_DEBUG,"consume64_thread: Has written buf %d to disk (len %d)\n",
consumed64 % daemonv3_wrmt_nbuf, byte_written);
}
} /* if (len_to_write > 0) */
else {
if (len_to_write == 0) {
log(LOG_DEBUG,"consume64_thread: End of writing : total consumed = %s, buffers=%d\n",
u64tostr(total_consumed,tmpbuf,0), consumed64);
end = 1;
}
else {
/* Error indicated by the thread reading from network, this thread just terminates */
log(LOG_DEBUG,"consume64_thread: Error on reading : total consumed = %s, buffers=%d\n",
u64tostr(total_consumed,tmpbuf,0), consumed64);
error = 1;
}
} /* else (len_to_write > 0) */
consumed64++;
log(LOG_DEBUG, "consume64_thread: calling Csemaphore_up(&empty64)\n");
Csemaphore_up(&empty64);
} /* End of while */
return(NULL);
}
static void wait_consumer64_thread(int *cidp)
{
log(LOG_DEBUG,"wait_consumer64_thread: Entering wait_consumer64_thread\n");
if (*cidp<0) return;
/* Indicate to the consumer thread that an error has occured */
/* The consumer thread will then terminate */
Csemaphore_down(&empty64);
array[produced64 % daemonv3_wrmt_nbuf].len = -1;
produced64++;
Csemaphore_up(&full64);
log(LOG_INFO, "wait_consumer64_thread: Joining thread\n");
if (Cthread_join(*cidp,NULL) < 0) {
log(LOG_ERR,"wait_consumer64_thread: Error joining consumer thread, serrno=%d\n",serrno);
return;
}
*cidp = -1;
}
static void wait_producer64_thread(int *cidp)
{
log(LOG_DEBUG,"wait_producer64_thread: Entering wait_producer64_thread\n");
if (*cidp<0) return;
if (!stop_read) {
stop_read = 1;
Csemaphore_up(&empty64);
}
log(LOG_INFO, "wait_producer64_thread: Joining thread\n");
if (Cthread_join(*cidp,NULL) < 0) {
log(LOG_ERR,"wait_producer64_thread: Error joining producer thread, serrno=%d\n",serrno);
return;
}
*cidp = -1;
}
/* This function is used when finding an error condition on read64_v3
- Close the data stream
- Trace statistics
*/
static int readerror64_v3(s, infop, cidp)
SOCKET s;
struct rfiostat* infop;
int *cidp;
{
char tmpbuf[21], tmpbuf2[21];
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
/* We close the data socket */
if (data_sock >= 0) {
serrno = 0;
if( netclose(data_sock) == SOCKET_ERROR )
log(LOG_DEBUG, "readerror64_v3: Error closing data socket fildesc=%d, error=%s\n",
data_sock, neterror());
else
log(LOG_DEBUG, "readerror64_v3 : closing data socket fildesc=%d\n", data_sock);
data_sock = -1;
}
/* Issue statistic messages - There is no accounting record generated */
log(LOG_INFO,
"readerror64_v3(%d): %d read, %d readahead, %d write, %d flush, %d stat, %d lseek and %d lockf\n",
s, infop->readop, infop->aheadop, infop->writop, infop->flusop, infop->statop,
infop->seekop, infop->lockop);
log(LOG_INFO,"readerror64_v3(%d): %s bytes read and %s bytes written\n",
s, u64tostr(infop->rnbr,tmpbuf,0), u64tostr(infop->wnbr,tmpbuf2,0)) ;
/* Free allocated memory */
if (!daemonv3_rdmt) {
log(LOG_DEBUG,"readerror64_v3: freeing iobuffer at 0X%X\n", iobuffer);
if (iobufsiz > 0) {
free(iobuffer);
iobuffer = NULL;
iobufsiz = 0;
}
}
else {
wait_producer64_thread(cidp);
if (array) {
int el;
for (el=0; el < daemonv3_rdmt_nbuf; el++) {
log(LOG_DEBUG,"readerror64_v3: freeing array element %d at 0X%X\n", el,array[el].p);
free(array[el].p);
array[el].p = NULL;
}
log(LOG_DEBUG,"readerror64_v3: freeing array at 0X%X\n", array);
free(array);
array = NULL;
}
}
return 0;
}
#if defined(_WIN32)
int srread64_v3(s, infop, fd)
SOCKET s;
#else /* WIN32 */
int srread64_v3(ctrl_sock, infop, fd)
int ctrl_sock;
#endif /* else WIN32 */
int fd;
struct rfiostat* infop;
{
int status; /* Return code */
int rcode; /* To send back errno */
int how; /* lseek mode */
off64_t offset; /* lseek offset */
off64_t offsetout; /* lseek offset */
int size; /* Requested write size */
char *p; /* Pointer to buffer */
#if !defined(_WIN32)
char *iobuffer;
#endif
off64_t bytes2send;
fd_set fdvar, fdvar2;
extern int max_sndbuf;
int optlen;
struct stat64 st;
char rfio_buf[BUFSIZ];
int eof_met;
int DISKBUFSIZE_READ = (1 * 1024 * 1024);
int n;
int cid1 = -1;
int el;
char tmpbuf[21];
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
ctrl_sock = s;
#endif /* WIN32 */
/*
* Receiving request,
*/
log(LOG_DEBUG, "rread64_v3(%d,%d)\n",ctrl_sock, fd);
if (first_read) {
char *p;
first_read = 0;
eof_met = 0;
if( (p = getconfent("RFIO", "DAEMONV3_RDSIZE", 0)) != NULL ) {
if (atoi(p) > 0)
DISKBUFSIZE_READ = atoi(p);
}
daemonv3_rdmt = DAEMONV3_RDMT;
if( (p = getconfent("RFIO", "DAEMONV3_RDMT", 0)) != NULL )
if (*p == '0')
daemonv3_rdmt = 0;
else
daemonv3_rdmt = 1;
daemonv3_rdmt_nbuf = DAEMONV3_RDMT_NBUF;
if( (p = getconfent("RFIO", "DAEMONV3_RDMT_NBUF", 0)) != NULL )
if (atoi(p) > 0)
daemonv3_rdmt_nbuf = atoi(p);
daemonv3_rdmt_bufsize = DAEMONV3_RDMT_BUFSIZE;
if( (p = getconfent("RFIO", "DAEMONV3_RDMT_BUFSIZE", 0)) != NULL )
if (atoi(p) > 0)
daemonv3_rdmt_bufsize = atoi(p);
log(LOG_DEBUG,
"rread64_v3(%d) : daemonv3_rdmt=%d,daemonv3_rdmt_nbuf=%d,daemonv3_rdmt_bufsize=%d\n",
ctrl_sock, daemonv3_rdmt,daemonv3_rdmt_nbuf,daemonv3_rdmt_bufsize);
if (daemonv3_rdmt) {
/* Indicates we are using RFIO V3 and multithreadding while reading */
myinfo.aheadop = 3;
/* Allocating circular buffer itself */
log(LOG_DEBUG, "rread64_v3: allocating circular buffer : %d bytes\n",
sizeof(struct element) * daemonv3_rdmt_nbuf);
array = (struct element *)malloc(sizeof(struct element) * daemonv3_rdmt_nbuf);
if (array == NULL) {
log(LOG_ERR, "rread64_v3: malloc array: ERROR occured (errno=%d)\n", errno);
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1 ;
}
log(LOG_DEBUG, "rread64_v3: malloc array allocated : 0X%X\n", array);
/* Allocating memory for each element of circular buffer */
for (el=0; el < daemonv3_rdmt_nbuf; el++) {
log(LOG_DEBUG, "rread64_v3: allocating circular buffer element %d: %d bytes\n",
el, daemonv3_rdmt_bufsize);
if ((array[el].p = (char *)malloc(daemonv3_rdmt_bufsize)) == NULL) {
log(LOG_ERR, "rread64_v3: malloc array element %d, size %d: ERROR %d occured\n",
el, daemonv3_rdmt_bufsize, errno);
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1 ;
}
log(LOG_DEBUG, "rread64_v3: malloc array element %d allocated : 0X%X\n",
el, array[el].p);
}
}
else {
log(LOG_DEBUG, "rread64_v3: allocating malloc buffer : %d bytes\n", DISKBUFSIZE_READ);
if ((iobuffer = (char *)malloc(DISKBUFSIZE_READ)) == NULL) {
log(LOG_ERR, "rread64_v3: malloc: ERROR occured (errno=%d)\n", errno);
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1 ;
}
log(LOG_DEBUG, "rread64_v3: malloc buffer allocated : 0X%X\n", iobuffer);
iobufsiz = DISKBUFSIZE_READ;
}
if (fstat64(fd,&st) < 0) {
log(LOG_ERR, "rread64_v3: fstat(): ERROR occured (errno=%d)\n", errno);
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1 ;
}
log(LOG_DEBUG, "rread64_v3: filesize : %s bytes\n", u64tostr(st.st_size,tmpbuf,0));
offsetout = lseek64(fd,0L,SEEK_CUR);
if (offsetout == (off64_t)-1) {
log(LOG_ERR, "rread64_v3: lseek64(%d,0,SEEK_CUR): %s\n", fd, strerror(errno)) ;
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1 ;
}
bytes2send = st.st_size - offsetout;
if (bytes2send < 0) bytes2send = 0;
log(LOG_DEBUG, "rread64_v3: %s bytes to send (offset taken into account)\n",
u64tostr(bytes2send,tmpbuf,0));
p = rfio_buf;
marshall_WORD(p,RQST_READ64_V3);
marshall_HYPER(p,bytes2send);
log(LOG_DEBUG, "rread64_v3: sending %d bytes\n", RQSTSIZE);
errno = ECONNRESET;
serrno = 0;
n = netwrite_timeout(ctrl_sock, rfio_buf, RQSTSIZE, RFIO_CTRL_TIMEOUT);
if (n != RQSTSIZE) {
log(LOG_ERR, "rread64_v3: netwrite() ERROR: %s\n", neterror());
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1 ;
}
if (daemonv3_rdmt) {
Csemaphore_init(&empty64,daemonv3_rdmt_nbuf);
Csemaphore_init(&full64,0);
stop_read = 0;
if ((cid1 = Cthread_create((void *(*)(void *))produce64_thread,(void *)&fd)) < 0) {
log(LOG_ERR,"rread64_v3: Cannot create producer thread : serrno=%d,errno=%d\n",
serrno, errno);
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return(-1);
}
}
} /* end of if (first_read) */
/*
* Reading data from the network.
*/
while (1)
{
struct timeval t;
fd_set *write_fdset;
FD_ZERO(&fdvar);
FD_SET(ctrl_sock, &fdvar);
FD_ZERO(&fdvar2);
if (data_sock >= 0) FD_SET(data_sock, &fdvar2);
t.tv_sec = 10;
t.tv_usec = 0;
if (eof_met)
write_fdset = NULL;
else
write_fdset = &fdvar2;
log(LOG_DEBUG,"srread64_v3: doing select\n") ;
serrno = 0;
if( select(FD_SETSIZE, &fdvar, write_fdset, NULL, &t) == SOCKET_ERROR ) {
log(LOG_ERR, "srread64_v3: select failed: %s\n", neterror());
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1;
}
if( FD_ISSET(ctrl_sock, &fdvar) ) {
int n, magic, code;
/* Something received on the control socket */
log(LOG_DEBUG, "srread64_v3: ctrl socket: reading %d bytes\n", RQSTSIZE) ;
errno = ECONNRESET;
serrno = 0;
n = netread_timeout(ctrl_sock,rqstbuf,RQSTSIZE,RFIO_CTRL_TIMEOUT);
if (n != RQSTSIZE) {
log(LOG_ERR, "srread64_v3: read ctrl socket %d: netread(): %s\n",
ctrl_sock, neterror());
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1 ;
}
p = rqstbuf ;
unmarshall_WORD(p,magic) ;
unmarshall_WORD(p,code) ;
/* what to do ? */
if (code == RQST_CLOSE64_V3) {
log(LOG_DEBUG,"srread64_v3: close request: magic: %x code: %x\n", magic, code);
if (!daemonv3_rdmt) {
log(LOG_DEBUG,"srread64_v3: freeing iobuffer at 0X%X\n", iobuffer);
if (iobufsiz > 0) {
free(iobuffer);
iobufsiz = 0;
iobuffer = NULL;
}
}
else {
wait_producer64_thread(&cid1);
if (cid1 >= 0) {
log(LOG_ERR,"srread64_v3: Error joining producer, serrno=%d\n", serrno);
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return(-1);
}
for (el=0; el < daemonv3_rdmt_nbuf; el++) {
log(LOG_DEBUG,"srread64_v3: freeing array element %d at 0X%X\n", el,array[el].p);
free(array[el].p);
array[el].p = NULL;
}
log(LOG_DEBUG,"srread64_v3: freeing array at 0X%X\n", array);
free(array);
array = NULL;
}
srclose64_v3(ctrl_sock,&myinfo,fd);
return 0;
}
else {
log(LOG_ERR,"srread64_v3: unknown request: magic: %x code: %x\n", magic,code);
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return(-1);
}
} /* if( FD_ISSET(ctrl_sock, &fdvar) ) */
/*
* Reading data on disk.
*/
if( !eof_met && data_sock >= 0 && (FD_ISSET(data_sock, &fdvar2)) ) {
if (daemonv3_rdmt) {
Csemaphore_down(&full64);
if (array[consumed64 % daemonv3_rdmt_nbuf].len > 0) {
iobuffer = array[consumed64 % daemonv3_rdmt_nbuf].p;
status = array[consumed64 % daemonv3_rdmt_nbuf].len;
}
else
if (array[consumed64 % daemonv3_rdmt_nbuf].len == 0) {
status = 0;
iobuffer = NULL;
log(LOG_DEBUG,"srread64_v3: Waiting for producer thread\n");
if (Cthread_join(cid1,NULL) < 0) {
log(LOG_ERR,"srread64_v3: Error joining producer, serrno=%d\n", serrno);
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return(-1);
}
cid1 = -1;
}
else
if (array[consumed64 % daemonv3_rdmt_nbuf].len < 0) {
status = -1;
errno = -(array[consumed64 % daemonv3_rdmt_nbuf].len);
}
consumed64++;
}
else
status = read(fd,iobuffer,DISKBUFSIZE_READ);
rcode = (status < 0) ? errno:0;
log(LOG_DEBUG, "srread64_v3: %d bytes have been read on disk\n", status) ;
if (status == 0) {
if (daemonv3_rdmt) {
log(LOG_DEBUG, "srread64_v3: calling Csemaphore_up(&empty64)\n");
Csemaphore_up(&empty64);
}
eof_met = 1;
p = rqstbuf;
marshall_WORD(p,REP_EOF) ;
log(LOG_DEBUG, "rread64_v3: eof\n") ;
errno = ECONNRESET;
serrno = 0;
n = netwrite_timeout(ctrl_sock, rqstbuf, RQSTSIZE, RFIO_CTRL_TIMEOUT);
if (n != RQSTSIZE) {
log(LOG_ERR,"rread64_v3: netwrite(): %s\n", neterror());
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1 ;
}
} /* status == 0 */
else {
if (status < 0) {
if (daemonv3_rdmt)
Csemaphore_up(&empty64);
p = rqstbuf;
marshall_WORD(p, REP_ERROR);
marshall_LONG(p, status);
marshall_LONG(p, rcode);
log(LOG_DEBUG, "rread64_v3: status %d, rcode %d\n", status, rcode) ;
errno = ECONNRESET;
serrno = 0;
n = netwrite_timeout(ctrl_sock, rqstbuf, RQSTSIZE, RFIO_CTRL_TIMEOUT);
if (n != RQSTSIZE) {
log(LOG_ERR, "rread64_v3: netwrite(): %s\n", neterror());
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1 ;
}
log(LOG_DEBUG, "read64_v3: waiting ack for error\n");
serrno = 0;
n = netread_timeout(ctrl_sock,rqstbuf,RQSTSIZE,RFIO_CTRL_TIMEOUT);
if (n != RQSTSIZE) {
if (n == 0)
#if defined(_WIN32)
WSASetLastError(WSAECONNRESET);
#else
errno = ECONNRESET;
#endif
log(LOG_ERR, "read64_v3: read ctrl socket %d: read(): %s\n",
ctrl_sock, neterror());
} /* n != RQSTSIZE */
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return(-1);
} /* status < 0 */
else {
/* status > 0, reading was succesfully */
log(LOG_DEBUG, "rread64_v3: writing %d bytes to data socket %d\n", status, data_sock) ;
#if defined(_WIN32)
WSASetLastError(WSAECONNRESET);
if( (n = send(data_sock, iobuffer, status, 0)) != status ) {
log(LOG_ERR, "rread64_v3: send() (to data sock): %s\n", geterr() );
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1;
}
#else /* WIN32 */
errno = ECONNRESET;
if( (n = netwrite(data_sock, iobuffer, status)) != status ) {
log(LOG_ERR, "rread64_v3: netwrite(): %s\n", strerror(errno));
readerror64_v3(ctrl_sock, &myinfo, &cid1);
return -1 ;
}
#endif /* else WIN32 */
if (daemonv3_rdmt)
Csemaphore_up(&empty64);
myinfo.rnbr += status;
myinfo.readop++;
} /* else status < 0 */
} /* else status == 0 */
} /* if( !eof_met && (FD_ISSET(data_sock, &fdvar2)) ) */
} /* while (1) */
}
/* This function is used when finding an error condition on write64_v3
- send a REP on the control stream
- empty the DATA stream
- wait for acknowledge on control stream
*/
static int writerror64_v3(s, rcode, infop, cidp)
SOCKET s;
int rcode;
struct rfiostat *infop;
int *cidp;
{
char *p; /* Pointer to buffer */
int status;
int n;
int el;
fd_set fdvar2;
struct timeval t;
int sizeofdummy;
char tmpbuf[21], tmpbuf2[21];
/*
* Put dummy on heap to avoid large arrays in thread stack
*/
unsigned char *dummy = NULL;
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
/* Send back error message */
status = -1;
p = rqstbuf;
marshall_WORD(p, REP_ERROR);
marshall_LONG(p, status);
marshall_LONG(p, rcode);
log(LOG_ERR, "writerror64_v3: status %d, rcode %d (%s)\n", status, rcode, strerror(rcode));
errno = ECONNRESET;
serrno = 0;
n = netwrite_timeout(s, rqstbuf, RQSTSIZE, RFIO_CTRL_TIMEOUT);
if (n != RQSTSIZE) {
log(LOG_ERR, "writerror64_v3: write(): %s\n", neterror());
/* Consumer thread already exited after error */
}
/*
* To avoid overflowing the local thread stack we must
* put dummy on heap
*/
sizeofdummy = 256 * 1024;
dummy = (unsigned char *)malloc(sizeof(unsigned char) * sizeofdummy);
if (dummy == NULL)
log(LOG_ERR, "writerror64_v3: malloc(%d) for dummy: %s\n",
sizeofdummy, strerror(errno)) ;
/*
There is a potential deadlock here since the client may be stuck
in netwrite (cf rfio_write64_v3), trying to write data on the data
socket while both socket buffers (client + server) are full.
To avoid this problem, we empty the data socket while waiting
for the ack to be received on the control socket
*/
while (dummy) {
FD_ZERO(&fdvar2);
FD_SET(s, &fdvar2);
if (data_sock >= 0) FD_SET(data_sock, &fdvar2);
t.tv_sec = 1;
t.tv_usec = 0;
log(LOG_DEBUG,"writerror64_v3: doing select after error writing on disk\n") ;
#if defined(_WIN32)
if( select(FD_SETSIZE, &fdvar2, NULL, NULL, &t) == SOCKET_ERROR )
#else
if( select(FD_SETSIZE, &fdvar2, NULL, NULL, &t) < 0 )
#endif
{
#if defined(_WIN32)
errno = WSAGetLastError();
#endif
log(LOG_ERR, "writerror64_v3: select fdvar2 failed (errno=%d)\n", errno) ;
/* Consumer thread already exited after error */
break ;
}
if ( FD_ISSET(s, &fdvar2) ) {
/* The ack has been received on the control socket */
log(LOG_DEBUG, "writerror64_v3: waiting ack for error\n");
serrno = 0;
n = netread_timeout(s, rqstbuf, RQSTSIZE, RFIO_CTRL_TIMEOUT);
if (n != RQSTSIZE) {
/* Connexion dropped (n==0) or some another error */
if (n == 0)
#if defined(_WIN32)
WSASetLastError(WSAECONNRESET);
#else
errno = ECONNRESET;
#endif
log(LOG_ERR, "writerror64_v3: read ctrl socket: read(): %s\n", neterror());
/* Consumer thread already exited after error */
break;
}
/* We have got the reply */
break;
} /* if ( FD_ISSET(ctrl_sock, &fdvar2) ) */
if (data_sock >= 0 && FD_ISSET(data_sock, &fdvar2)) {
/* Read as much data as possible from the data socket */
log(LOG_DEBUG, "writerror64_v3: emptying data socket (last disk write)\n");
serrno = 0;
#if defined(_WIN32)
n = recv(data_sock, dummy, sizeofdummy, 0);
if( (n == 0) || (n == SOCKET_ERROR) )
#else
n = read(data_sock, dummy, sizeofdummy);
if( n <= 0 )
#endif
{
/* Connexion dropped (n==0) or some another error */
if (n == 0)
#if defined(_WIN32)
WSASetLastError(WSAECONNRESET);
#else
errno = ECONNRESET;
#endif
log(LOG_ERR, "writerror64_v3: read emptying data socket: recv(): %s\n",
neterror());
/* Consumer thread already exited after error */
break;
}
log(LOG_DEBUG, "writerror64_v3: emptying data socket, %d bytes read\n", n);
} /* if (FD_ISSET(data_sock,&fdvar2)) */
} /* End of while (1) : drop the data */
/* Close the data socket */
if (data_sock >= 0) {
serrno = 0;
if( netclose(data_sock) == SOCKET_ERROR )
log(LOG_DEBUG, "writerror64_v3: Error closing data socket fildesc=%d, error=%s\n",
data_sock, neterror());
else
log(LOG_DEBUG, "writerror64_v3 : closing data socket fildesc=%d\n", data_sock);
data_sock = -1;
}
/* Issue statistic messages - There is no accounting record generated */
log(LOG_INFO,
"writerror64_v3(%d): %d read, %d readahead, %d write, %d flush, %d stat, %d lseek and %d lockf\n",
s, infop->readop, infop->aheadop, infop->writop, infop->flusop, infop->statop,
infop->seekop, infop->lockop);
log(LOG_INFO,"writerror64_v3(%d): %s bytes read and %s bytes written\n",
s, u64tostr(infop->rnbr,tmpbuf,0), u64tostr(infop->wnbr,tmpbuf2,0)) ;
/* free the allocated ressources */
free(dummy);
dummy = NULL;
if (!daemonv3_wrmt) {
log(LOG_DEBUG,"writerror64_v3: freeing iobuffer at 0X%X\n", iobuffer);
free(iobuffer);
iobuffer = NULL;
iobufsiz = 0;
}
else {
wait_consumer64_thread(cidp);
if (array) {
for (el=0; el < daemonv3_wrmt_nbuf; el++) {
log(LOG_DEBUG,"writerror64_v3: freeing array element %d at 0X%X\n",
el,array[el].p);
free(array[el].p);
array[el].p = NULL;
}
log(LOG_DEBUG,"writerror64_v3: freeing array at 0X%X\n", array);
free(array);
array = NULL;
}
}
return 0;
}
int srwrite64_v3(s, infop, fd)
SOCKET s;
int fd;
struct rfiostat *infop;
{
int status; /* Return code */
int rcode; /* To send back errno */
int how; /* lseek mode */
int offset; /* lseek offset */
int size; /* Requested write size */
char *p; /* Pointer to buffer */
#if !defined(_WIN32)
char *iobuffer;
#endif
fd_set fdvar;
off64_t byte_written_by_client = 0;
int eof_received = 0;
int invalid_received = 0;
extern int max_rcvbuf;
int maxseg;
#if defined(_AIX)
socklen_t optlen;
#else
int optlen;
#endif
int byte_in_diskbuffer = 0;
char *iobuffer_p;
int max_rcv_wat;
struct timeval t;
int DISKBUFSIZE_WRITE = (1*1024*1024);
int el;
int cid2 = -1;
int saved_errno;
char tmpbuf[21], tmpbuf2[21];
#if defined(_WIN32)
struct thData *td;
td = (struct thData*)TlsGetValue(tls_i);
#endif
/*
* Receiving request,
*/
log(LOG_DEBUG, "rwrite64_v3(%d, %d)\n",s, fd);
if( first_write ) {
char *p;
first_write = 0;
if ((p = getconfent("RFIO","DAEMONV3_WRSIZE",0)) != NULL) {
if (atoi(p) > 0)
DISKBUFSIZE_WRITE = atoi(p);
}
daemonv3_wrmt = DAEMONV3_WRMT;
if( (p = getconfent("RFIO", "DAEMONV3_WRMT", 0)) != NULL )
if (*p == '0')
daemonv3_wrmt = 0;
else
daemonv3_wrmt = 1;
daemonv3_wrmt_nbuf = DAEMONV3_WRMT_NBUF;
if( (p = getconfent("RFIO", "DAEMONV3_WRMT_NBUF", 0)) != NULL )
if (atoi(p) > 0)
daemonv3_wrmt_nbuf = atoi(p);
daemonv3_wrmt_bufsize = DAEMONV3_WRMT_BUFSIZE;
DISKBUFSIZE_WRITE = DAEMONV3_WRMT_BUFSIZE;
if( (p = getconfent("RFIO", "DAEMONV3_WRMT_BUFSIZE", 0)) != NULL )
if (atoi(p) > 0) {
daemonv3_wrmt_bufsize = atoi(p);
DISKBUFSIZE_WRITE = atoi(p);
}
log(LOG_DEBUG,
"rwrite64_v3: daemonv3_wrmt=%d,daemonv3_wrmt_nbuf=%d,daemonv3_wrmt_bufsize=%d\n",
daemonv3_wrmt,daemonv3_wrmt_nbuf,daemonv3_wrmt_bufsize);
if (daemonv3_wrmt) {
/* Indicates we are using RFIO V3 and multithreading while writing */
myinfo.aheadop = 3;
/* Allocating circular buffer itself */
log(LOG_DEBUG, "rwrite64_v3: allocating circular buffer: %d bytes\n",
sizeof(struct element) * daemonv3_wrmt_nbuf);
array = (struct element *)malloc(sizeof(struct element) * daemonv3_wrmt_nbuf);
if (array == NULL) {
log(LOG_ERR, "rwrite64_v3: malloc array: ERROR occured (errno=%d)\n", errno);
return -1 ;
}
log(LOG_DEBUG, "rwrite64_v3: malloc array allocated at 0X%X\n", array);
/* Allocating memory for each element of circular buffer */
for (el=0; el < daemonv3_wrmt_nbuf; el++) {
log(LOG_DEBUG, "rwrite64_v3: allocating circular buffer element %d: %d bytes\n",
el,daemonv3_wrmt_bufsize);
if ((array[el].p = (char *)malloc(daemonv3_wrmt_bufsize)) == NULL) {
log(LOG_ERR, "rwrite64_v3: malloc array element %d: ERROR occured (errno=%d)\n",
el, errno);
return -1 ;
}
log(LOG_DEBUG, "rwrite64_v3: malloc array element %d allocated at 0X%X\n",
el, array[el].p);
}
}
/* Don't allocate this buffer if we are multithreaded */
if (!daemonv3_wrmt) {
log(LOG_DEBUG, "rwrite64_v3: allocating malloc buffer: %d bytes\n", DISKBUFSIZE_WRITE);
if ((iobuffer = (char *)malloc(DISKBUFSIZE_WRITE)) == NULL) {
log(LOG_ERR, "rwrite64_v3: malloc: ERROR occured (errno=%d)\n", errno);
return -1 ;
}
log(LOG_DEBUG, "rwrite64_v3: malloc buffer allocated at 0X%X\n", iobuffer);
iobufsiz = DISKBUFSIZE_WRITE;
}
byte_in_diskbuffer = 0;
if (daemonv3_wrmt)
iobuffer_p = NULL; /* For safety */
else
iobuffer_p = iobuffer;
#if !defined(_WIN32)
optlen = sizeof(maxseg);
if (getsockopt(data_sock,IPPROTO_TCP,TCP_MAXSEG,(char *)&maxseg,&optlen) < 0) {
log(LOG_ERR, "rwrite64_v3: getsockopt: ERROR occured (errno=%d)\n", errno) ;
return -1 ;
}
log(LOG_DEBUG,"rwrite64_v3: max TCP segment: %d\n", maxseg);
#endif /* WIN32 */
if (daemonv3_wrmt) {
Csemaphore_init(&empty64,daemonv3_wrmt_nbuf);
Csemaphore_init(&full64,0);
if ((cid2 = Cthread_create((void *(*)(void *))consume64_thread,(void *)&fd)) < 0) {
log(LOG_ERR,"rwrite64_v3: Cannot create consumer thread : serrno=%d, errno=%d\n",
serrno, errno);
return(-1);
}
}
} /* if (firstwrite) */
/*
* Reading data from the network.
*/
while (1) {
FD_ZERO(&fdvar);
FD_SET(ctrl_sock, &fdvar);
if (data_sock >= 0) FD_SET(data_sock, &fdvar);
t.tv_sec = 10;
t.tv_usec = 0;
log(LOG_DEBUG,"rwrite64_v3: doing select\n") ;
serrno = 0;
if( select(FD_SETSIZE, &fdvar, NULL, NULL, &t) == SOCKET_ERROR ) {
log(LOG_ERR, "rwrite64_v3: select: %s\n", neterror());
if (daemonv3_wrmt)
wait_consumer64_thread(&cid2);
return -1;
}
/* Anything received on control socket ? */
if( FD_ISSET(ctrl_sock, &fdvar) ) {
int n, magic, code;
/* Something received on the control socket */
log(LOG_DEBUG, "rwrite64_v3: ctrl socket: reading %d bytes\n", RQSTSIZE) ;
serrno = 0;
n = netread_timeout(ctrl_sock, rqstbuf, RQSTSIZE, RFIO_CTRL_TIMEOUT);
if ( n != RQSTSIZE ) {
if (n == 0)
#if defined(_WIN32)
WSASetLastError(WSAECONNRESET);
#else
errno = ECONNRESET;
#endif
log(LOG_ERR, "rwrite64_v3: read ctrl socket: netread(): %s\n", neterror());
if (daemonv3_wrmt)
wait_consumer64_thread(&cid2);
return -1;
}
p = rqstbuf ;
unmarshall_WORD(p, magic) ;
unmarshall_WORD(p, code) ;
unmarshall_HYPER(p, byte_written_by_client);
if (code == RQST_CLOSE64_V3) {
log(LOG_DEBUG,"rwrite64_v3: close request: magic: %x code: %x\n", magic, code) ;
eof_received = 1;
}
else {
log(LOG_ERR,"rwrite64_v3: unknown request: magic: %x code: %x\n", magic, code) ;
writerror64_v3(ctrl_sock, EINVAL, &myinfo, &cid2);
return -1;
}
log(LOG_DEBUG, "rwrite64_v3: data socket: read_from_net=%s, written_by_client=%s\n",
u64tostr(byte_read_from_network,tmpbuf,0), u64tostr(byte_written_by_client,tmpbuf2,0));
} /* if (FD_ISSET(ctrl_sock, &fdvar)) */
/* Anything received on data socket ? */
if (data_sock >= 0 && FD_ISSET(data_sock, &fdvar)) {
int n,can_be_read;
if ((daemonv3_wrmt) && (byte_in_diskbuffer == 0)) {
log(LOG_DEBUG, "rwrite64_v3: Data received on data socket, new buffer %d requested\n",
produced64 % daemonv3_wrmt_nbuf);
Csemaphore_down(&empty64);
iobuffer = iobuffer_p = array[produced64 % daemonv3_wrmt_nbuf].p;
}
log(LOG_DEBUG,"rwrite64_v3: iobuffer_p = %X,DISKBUFSIZE_WRITE = %d, data socket = %d\n",
iobuffer_p, DISKBUFSIZE_WRITE, data_sock);
serrno = 0;
#if defined(_WIN32)
n = recv(data_sock, iobuffer_p, DISKBUFSIZE_WRITE-byte_in_diskbuffer, 0);
if( (n == 0) || n == SOCKET_ERROR )
#else
n = read(data_sock, iobuffer_p, DISKBUFSIZE_WRITE-byte_in_diskbuffer);
if( n <= 0 )
#endif
{
if (n == 0)
#if defined(_WIN32)
WSASetLastError(WSAECONNRESET);
#else
errno = ECONNRESET;
#endif
log(LOG_ERR, "rwrite64_v3: read data socket: recv(): %s\n", neterror());
if (daemonv3_wrmt)
wait_consumer64_thread(&cid2);
return -1;
}
log(LOG_DEBUG, "rwrite64_v3: read data socket %d: %d bytes\n", data_sock, n);
byte_read_from_network += n;
byte_in_diskbuffer += n;
iobuffer_p += n;
} /* if (FD_ISSET(data_sock,&fdvar)) */
/*
* Writing data on disk.
*/
if (byte_in_diskbuffer && (byte_in_diskbuffer == DISKBUFSIZE_WRITE ||
(eof_received && byte_written_by_client == byte_read_from_network)) ) {
log(LOG_DEBUG, "rwrite64_v3: writing %d bytes on disk\n", byte_in_diskbuffer);
if (daemonv3_wrmt) {
array[produced64 % daemonv3_wrmt_nbuf].len = byte_in_diskbuffer;
produced64++;
Csemaphore_up(&full64);
} /* if (daemonv3_wrmt) */
else {
status = write(fd, iobuffer, byte_in_diskbuffer);
/* If the write is successfull but incomplete (fs is full) we
report the ENOSPC error immediately in order to simplify the
code */
if ((status > 0) && (status != byte_in_diskbuffer)) {
status = -1;
errno = ENOSPC;
}
}
/* Get the status value */
if (daemonv3_wrmt) {
/* Caution: the write is done asynchroneously */
/* So the status is not related to the above write! */
/* Get the last write status within a mutex transaction */
if (Cthread_mutex_lock(&write_error)) {
log(LOG_ERR,"rwrite64_v3: Cannot get mutex : serrno=%d\n", serrno);
return(-1);
}
if (write_error) {
status = -1;
saved_errno = write_error;
}
else
status = byte_in_diskbuffer;
if (Cthread_mutex_unlock(&write_error)) {
log(LOG_ERR,"rwrite64_v3: Cannot release mutex : serrno=%d\n", serrno);
return(-1);
}
/* End of mutex transaction */
}
if ((daemonv3_wrmt) && (status == -1)) errno = saved_errno;
rcode = (status < 0) ? errno:0;
if (status < 0) {
/* Error in writting buffer */
writerror64_v3(ctrl_sock, rcode, &myinfo, &cid2);
return -1;
} /* if (status < 0) */
/* The data has be written on disk */
myinfo.wnbr += byte_in_diskbuffer;
myinfo.writop++;
byte_in_diskbuffer = 0;
iobuffer_p = iobuffer;
} /* if (byte_in_diskbuffer == DISKBUFSIZE_WRITE || eof_received || ...) */
/* Have we finished ? */
if ( eof_received && byte_written_by_client == myinfo.wnbr ) {
if (!daemonv3_wrmt) {
log(LOG_DEBUG,"rwrite64_v3: freeing iobuffer at 0X%X\n", iobuffer);
free(iobuffer);
iobuffer = NULL;
iobufsiz = 0;
}
else {
/* Indicate to the consumer thread that writing is finished */
/* if the actual buffer wasn't empty */
log(LOG_DEBUG, "rwrite64_v3: Terminates thread by null buffer %d requested\n",
produced64 % daemonv3_wrmt_nbuf);
Csemaphore_down(&empty64);
array[produced64 % daemonv3_wrmt_nbuf].len = 0;
produced64++;
Csemaphore_up(&full64);
/* Wait for consumer thread */
/* We can then safely catch deferred disk write errors */
log(LOG_INFO,"Joining thread\n");
if (Cthread_join(cid2,NULL) < 0) {
log(LOG_ERR,"Error joining consumer, serrno=%d\n", serrno);
return(-1);
}
cid2 = -1;
/* Get the last write status within a mutex transaction */
if (Cthread_mutex_lock(&write_error)) {
log(LOG_ERR,"rwrite64_v3: Cannot get mutex : serrno=%d\n", serrno);
return(-1);
}
if (write_error) {
status = -1;
rcode = write_error;
}
else status = 0;
if (Cthread_mutex_unlock(&write_error)) {
log(LOG_ERR,"rwrite64_v3: Cannot release mutex : serrno=%d\n", serrno);
return(-1);
}
/* End of mutex transaction */
/* If status is bad, bad news.... */
if (status < 0) {
writerror64_v3(ctrl_sock, rcode, &myinfo, &cid2);
return -1;
} /* if (status < 0) */
/* Free the buffers */
for (el=0; el < daemonv3_wrmt_nbuf; el++) {
log(LOG_DEBUG,"rwrite64_v3: freeing array element %d at 0X%X\n", el, array[el].p);
free(array[el].p);
array[el].p = NULL;
}
log(LOG_DEBUG,"rwrite64_v3: freeing array at 0X%X\n", array);
free(array);
array = NULL;
}
/* Send back final status and close sockets... and return */
srclose64_v3(ctrl_sock, &myinfo, fd);
return 0;
} /* if ( eof_received && byte_written_by_client == myinfo.wnbr ) */
} /* while (1) */
}
|