1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029
|
/*
* Copyright 1999-2009 University of Chicago
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
/**
* @file globus_gram_job_manager_request.c
* @brief Globus Job Management Request
*/
/*
* Include header files
*/
#include "globus_common.h"
#include "globus_gram_protocol.h"
#include "globus_gram_job_manager.h"
#include "globus_rsl_assist.h"
#include "globus_scheduler_event_generator_app.h"
#include <string.h>
#include <syslog.h>
#include <unistd.h>
enum
{
GRAM_JOB_MANAGER_COMMIT_TIMEOUT=60
};
static
int
globus_l_gram_symboltable_add(
globus_symboltable_t * symbol_table,
const char * symbol,
const char * value);
static
int
globus_l_gram_generate_id(
globus_gram_jobmanager_request_t * request,
char ** jm_restart,
uint64_t * uniq1p,
uint64_t * uniq2p);
static
int
globus_l_gram_init_cache(
globus_gram_jobmanager_request_t * request,
char ** cache_locationp,
globus_gass_cache_t * cache_handlep);
static
int
globus_l_gram_restart(
globus_gram_jobmanager_request_t * request,
globus_gram_jobmanager_request_t ** old_job_request);
static
int
globus_l_gram_populate_environment(
globus_gram_jobmanager_request_t * request);
static
int
globus_l_gram_add_environment(
globus_rsl_t * rsl,
const char * variable,
const char * value);
static
int
globus_l_gram_init_scratchdir(
globus_gram_jobmanager_request_t * request,
globus_rsl_t * rsl,
const char * scratch_dir_base,
char ** scratchdir);
static
int
globus_l_gram_validate_rsl(
globus_gram_jobmanager_request_t * request);
static
int
globus_l_gram_remote_io_url_file_create(
globus_gram_jobmanager_request_t * request,
const char * remote_io_url,
const char * job_dir,
char ** remote_io_url_filep);
static
int
globus_l_gram_export_cred(
globus_gram_jobmanager_request_t * request,
gss_cred_id_t cred,
const char * job_directory,
char ** proxy_filename);
static
int
globus_l_gram_make_job_dir(
globus_gram_jobmanager_request_t * request,
char ** job_directory);
static
int
globus_l_gram_check_position(
globus_gram_jobmanager_request_t * request,
globus_rsl_t * position_rsl);
static
void
globus_l_gram_event_destroy(void *datum);
#endif /* GLOBUS_DONT_DOCUMENT_INTERNAL */
/**
* Allocate and initialize a request.
*
* This function allocates a new request structure and clears all of the
* values in the structure. It also creates a script argument file which
* will be used when the job request is submitted.
*
* @param request
* A pointer to a globus_gram_jobmanager_request_t pointer. This
* will be modified to point to a freshly allocated request structure.
* @param manager
* Job manager state and configuration.
* @param rsl
* Job request or restart RSL string
* @param delegated_credential
* Credential delegated with the job request.
* @param response_ctx
* Security context to use for sending the job request response, may be
* GSS_C_NO_CONTEXT if the job RSL came from the command-line.
* @param reinit
* Boolean value indicating whether this is an internally-generated
* reinitialization of an existing job or a new job request from a
* client or command-line.
* @param old_job_contact
* Pointer to a string to be set to the old job contact if
* GLOBUS_GRAM_PROTOCOL_ERROR_OLD_JM_ALIVE. Set to NULL otherwise.
* @param old_job_request
* Pointer to a job request structure that will be set to an existing
* one if the return value is GLOBUS_GRAM_PROTOCOL_ERROR_OLD_JM_ALIVE. Set
* to NULL otherwise. If non-null, the caller must release a reference
* when done processing this.
*
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED
* Malloc failed
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL
* Bad RSL
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_RSL_EVALUATION_FAILED
* RSL evaluation failed
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_RSL_SCRATCH
* Invalid scratchdir RSL attribute
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_SCRATCH
* Invalid scratchdir path
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_PARAMETER_NOT_SUPPORTED
* RSL attribute not supported.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_SUBMIT_ATTRIBUTE
* Invalid submit RSL attribute.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_RESTART_ATTRIBUTE
* Invalid restart RSL attribute.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_STDIO_UPDATE_ATTRIBUTE
* Invalid stdio_update RSL attribute.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_OLD_JM_ALIVE
* Old Job Manager is still alive.
*/
int
globus_gram_job_manager_request_init(
globus_gram_jobmanager_request_t ** request,
globus_gram_job_manager_t * manager,
char * rsl,
gss_cred_id_t delegated_credential,
gss_ctx_id_t response_ctx,
const char * peer_address,
globus_gsi_cred_handle_t peer_cred,
globus_bool_t reinit,
char ** old_job_contact,
globus_gram_jobmanager_request_t ** old_job_request,
char ** gt3_failure_message)
{
globus_gram_jobmanager_request_t * r;
uint64_t uniq1, uniq2;
int rc;
const char * tmp_string;
int count;
int proxy_timeout;
if (old_job_contact)
{
*old_job_contact = NULL;
}
if (old_job_request)
{
*old_job_request = NULL;
}
r = calloc(1, sizeof(globus_gram_jobmanager_request_t));
if (r == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto request_malloc_failed;
}
r->job_log_level = -1;
r->config = manager->config;
r->manager = manager;
GlobusTimeAbstimeGetCurrent(r->job_stats.unsubmitted_timestamp);
r->status = GLOBUS_GRAM_PROTOCOL_JOB_STATE_UNSUBMITTED;
r->status_update_time = 0;
r->failure_code = 0;
r->exit_code = 0;
r->stop_reason = 0;
/* Won't be set until job has been submitted to the LRM */
r->job_id_string = NULL;
r->original_job_id_string = NULL;
r->poll_frequency = 10;
r->commit_extend = 0;
r->scratchdir = NULL;
r->creation_time = time(NULL);
r->queued_time = time(NULL);
r->cache_tag = NULL;
r->gateway_user = NULL;
r->expected_terminal_state = GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE;
r->gt3_failure_type = NULL;
r->gt3_failure_message = NULL;
r->gt3_failure_source = NULL;
r->gt3_failure_destination = NULL;
r->seg_last_timestamp = 0;
rc = globus_symboltable_init(
&r->symbol_table,
globus_hashtable_string_hash,
globus_hashtable_string_keyeq);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto symboltable_init_failed;
}
rc = globus_symboltable_create_scope(&r->symbol_table);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto symboltable_create_scope_failed;
}
rc = globus_i_gram_symbol_table_populate(
r->config,
&r->symbol_table);
if (rc != GLOBUS_SUCCESS)
{
goto symboltable_populate_failed;
}
r->rsl = globus_rsl_parse(rsl);
if (r->rsl == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL;
goto rsl_parse_failed;
}
rc = globus_rsl_assist_attributes_canonicalize(r->rsl);
if(rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL;
goto rsl_canonicalize_failed;
}
r->rsl_spec = globus_rsl_unparse(r->rsl);
if (r->rsl_spec == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto rsl_unparse_failed;
}
/* If this is a restart job, the id will come from the restart RSL
* value; otherwise, it will be generated from current pid and time
*/
rc = globus_l_gram_generate_id(
r,
&r->jm_restart,
&uniq1,
&uniq2);
if (rc != GLOBUS_SUCCESS)
{
goto failed_generate_id;
}
/* Unique ID is used to have a handle to a job that has its state saved
* and then the job is later restarted
*/
r->uniq_id = globus_common_create_string(
"%"PRIu64".%"PRIu64,
uniq1,
uniq2);
if (r->uniq_id == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto failed_set_uniq_id;
}
/* The job contact is how the client is able to send signals or cancel this
* job.
*/
r->job_contact = globus_common_create_string(
"%s%"PRIu64"/%"PRIu64"/",
r->manager->url_base,
uniq1,
uniq2);
if (r->job_contact == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto failed_set_job_contact;
}
rc = globus_l_gram_symboltable_add(
&r->symbol_table,
"GLOBUS_GRAM_JOB_CONTACT",
r->job_contact);
if (rc != GLOBUS_SUCCESS)
{
goto failed_add_contact_to_symboltable;
}
rc = globus_l_gram_symboltable_add(
&r->symbol_table,
"GLOBUS_GRAM_JOB_ID",
r->uniq_id);
if (rc != GLOBUS_SUCCESS)
{
goto failed_add_contact_to_symboltable;
}
r->job_contact_path = globus_common_create_string(
"/%"PRIu64"/%"PRIu64"/",
uniq1,
uniq2);
if (r->job_contact_path == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto failed_set_job_contact_path;
}
rc = globus_l_gram_make_job_dir(r, &r->job_dir);
if (rc != GLOBUS_SUCCESS)
{
goto failed_make_job_dir;
}
r->cached_stdout = globus_common_create_string(
"%s/%s",
r->job_dir,
"stdout");
if (r->cached_stdout == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto cached_stdout_malloc_failed;
}
rc = globus_symboltable_insert(
&r->symbol_table,
"GLOBUS_CACHED_STDOUT",
r->cached_stdout);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto cached_stdout_symboltable_failed;
}
r->cached_stderr = globus_common_create_string(
"%s/%s",
r->job_dir,
"stderr");
if (r->cached_stderr == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto cached_stderr_malloc_failed;
}
rc = globus_symboltable_insert(
&r->symbol_table,
"GLOBUS_CACHED_STDERR",
r->cached_stderr);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto cached_stderr_symboltable_failed;
}
rc = globus_gram_job_manager_state_file_set(
r,
&r->job_state_file);
if (rc != GLOBUS_SUCCESS)
{
goto failed_state_file_set;
}
r->client_contacts = NULL;
r->stage_in_todo = NULL;
r->stage_in_shared_todo = NULL;
r->stage_out_todo = NULL;
r->stage_stream_todo = NULL;
if (r->jm_restart)
{
rc = globus_l_gram_restart(r, old_job_request);
if (rc != GLOBUS_SUCCESS)
{
goto failed_restart;
}
manager->usagetracker->count_restarted++;
}
else
{
r->cache_tag = strdup(r->job_contact);
if (r->cache_tag == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto cache_tag_alloc_failed;
}
rc = globus_i_gram_get_tg_gateway_user(
response_ctx,
peer_cred,
&r->gateway_user);
if(rc != GLOBUS_SUCCESS)
{
goto get_gateway_user_failed;
}
}
rc = globus_gram_job_manager_rsl_eval_string(
&r->symbol_table,
r->config->scratch_dir_base,
&r->scratch_dir_base);
if(rc != GLOBUS_SUCCESS)
{
goto failed_eval_scratch_dir_base;
}
rc = globus_l_gram_init_scratchdir(
r,
r->rsl,
r->scratch_dir_base,
&r->scratchdir);
if(rc != GLOBUS_SUCCESS)
{
goto init_scratchdir_failed;
}
rc = globus_l_gram_init_cache(
r,
&r->cache_location,
&r->cache_handle);
if (rc != GLOBUS_SUCCESS)
{
goto init_cache_failed;
}
/* At this point, all of the RSL substitutions have been populated,
* including those based on runtime values, so we can validate the RSL
*/
rc = globus_l_gram_validate_rsl(r);
if(rc != GLOBUS_SUCCESS)
{
goto validate_rsl_failed;
}
rc = globus_gram_job_manager_rsl_attribute_get_int_value(
r->rsl,
GLOBUS_GRAM_PROTOCOL_COUNT_PARAM,
&count);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_COUNT;
goto invalid_count;
}
rc = globus_gram_job_manager_rsl_attribute_get_boolean_value(
r->rsl,
GLOBUS_GRAM_PROTOCOL_DRY_RUN_PARAM,
&r->dry_run);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_DRYRUN;
goto get_dry_run_failed;
}
/* Some clients send (two_phase_commit = yes), others send
* (two_phase_commit = timeout)
*/
rc = globus_gram_job_manager_rsl_attribute_get_int_value(
r->rsl,
GLOBUS_GRAM_PROTOCOL_TWO_PHASE_COMMIT_PARAM,
&r->two_phase_commit);
if (rc == GLOBUS_GRAM_PROTOCOL_ERROR_UNDEFINED_ATTRIBUTE)
{
r->two_phase_commit = 0;
}
else if (rc != GLOBUS_SUCCESS)
{
globus_bool_t tmp_bool;
rc = globus_gram_job_manager_rsl_attribute_get_boolean_value(
r->rsl,
GLOBUS_GRAM_PROTOCOL_TWO_PHASE_COMMIT_PARAM,
&tmp_bool);
if (rc == GLOBUS_SUCCESS)
{
r->two_phase_commit =
tmp_bool ? GRAM_JOB_MANAGER_COMMIT_TIMEOUT : 0;
}
else
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_TWO_PHASE_COMMIT;
goto get_two_phase_commit_failed;
}
globus_gram_job_manager_rsl_remove_attribute(
r->rsl,
GLOBUS_GRAM_PROTOCOL_TWO_PHASE_COMMIT_PARAM);
}
rc = globus_gram_job_manager_rsl_attribute_get_string_value(
r->rsl,
GLOBUS_GRAM_PROTOCOL_REMOTE_IO_URL_PARAM,
&tmp_string);
switch (rc)
{
case GLOBUS_GRAM_PROTOCOL_ERROR_UNDEFINED_ATTRIBUTE:
r->remote_io_url = NULL;
r->remote_io_url_file = NULL;
break;
case GLOBUS_SUCCESS:
if (tmp_string != NULL)
{
r->remote_io_url = strdup(tmp_string);
if (r->remote_io_url == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto get_remote_io_url_failed;
}
}
else
{
default:
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_REMOTE_IO_URL;
goto get_remote_io_url_failed;
}
}
rc = globus_l_gram_remote_io_url_file_create(
r,
r->remote_io_url,
r->job_dir,
&r->remote_io_url_file);
if (rc != GLOBUS_SUCCESS)
{
goto make_remote_io_url_file_failed;
}
/* TODO: Check that stdout and stderr, if a local files, can be written
* to
*/
rc = globus_gram_job_manager_rsl_attribute_get_int_value(
r->rsl,
GLOBUS_GRAM_PROTOCOL_PROXY_TIMEOUT_PARAM,
&proxy_timeout);
if (rc == GLOBUS_GRAM_PROTOCOL_ERROR_UNDEFINED_ATTRIBUTE)
{
rc = GLOBUS_SUCCESS;
}
else if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_PROXY_TIMEOUT;
goto bad_proxy_timeout;
}
rc = globus_gram_job_manager_rsl_attribute_get_string_value(
r->rsl,
"loglevel",
&tmp_string);
switch (rc)
{
case GLOBUS_GRAM_PROTOCOL_ERROR_UNDEFINED_ATTRIBUTE:
r->job_log_level = r->config->log_levels;
break;
case GLOBUS_SUCCESS:
if (tmp_string != NULL)
{
rc = globus_i_gram_parse_log_levels(
tmp_string,
&r->job_log_level,
&r->gt3_failure_message);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_ATTR;
goto get_job_log_levels_failed;
}
}
else
{
default:
rc = GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_ATTR;
goto get_job_log_levels_failed;
}
}
rc = globus_gram_job_manager_rsl_attribute_get_string_value(
r->rsl,
"logpattern",
&tmp_string);
switch (rc)
{
case GLOBUS_GRAM_PROTOCOL_ERROR_UNDEFINED_ATTRIBUTE:
r->log_pattern = NULL;
break;
case GLOBUS_SUCCESS:
if (tmp_string != NULL)
{
r->log_pattern = strdup(tmp_string);
}
else
{
default:
rc = GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_ATTR;
goto get_job_log_pattern_failed;
}
}
rc = globus_mutex_init(&r->mutex, NULL);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_NO_RESOURCES;
goto mutex_init_failed;
}
rc = globus_cond_init(&r->cond, NULL);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_NO_RESOURCES;
goto cond_init_failed;
}
rc = globus_gram_job_manager_staging_create_list(r);
if(rc != GLOBUS_SUCCESS)
{
goto staging_list_create_failed;
}
if (reinit && r->jm_restart)
{
r->jobmanager_state = GLOBUS_GRAM_JOB_MANAGER_STATE_TWO_PHASE_COMMITTED;
}
else
{
r->jobmanager_state = GLOBUS_GRAM_JOB_MANAGER_STATE_START;
}
if (r->jm_restart == NULL)
{
r->restart_state = GLOBUS_GRAM_JOB_MANAGER_STATE_START;
}
r->unsent_status_change = GLOBUS_FALSE;
r->poll_timer = GLOBUS_NULL_HANDLE;
rc = globus_fifo_init(&r->pending_queries);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_NO_RESOURCES;
goto pending_queries_init_failed;
}
rc = globus_l_gram_export_cred(
r,
delegated_credential,
r->job_dir,
&r->x509_user_proxy);
if (rc != GLOBUS_SUCCESS)
{
goto failed_export_cred;
}
rc = globus_l_gram_populate_environment(r);
if (rc != GLOBUS_SUCCESS)
{
goto failed_populate_environment;
}
r->streaming_requested = GLOBUS_FALSE;
rc = globus_gram_job_manager_history_file_set(r);
if (rc != GLOBUS_SUCCESS)
{
goto history_file_set_failed;
}
r->job_history_status = GLOBUS_GRAM_PROTOCOL_JOB_STATE_UNSUBMITTED;
r->response_context = response_ctx;
rc = globus_fifo_init(&r->seg_event_queue);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto seg_event_queue_init_failed;
}
if (r->job_stats.client_address == NULL && peer_address != NULL)
{
r->job_stats.client_address = strdup(peer_address);
if (r->job_stats.client_address == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto client_addr_strdup_failed;
}
}
if (r->job_stats.user_dn == NULL && peer_cred != NULL)
{
globus_result_t id_result;
char * id = NULL;
id_result = globus_gsi_cred_get_identity_name(
peer_cred,
&id);
if (id_result == GLOBUS_SUCCESS && id != NULL)
{
r->job_stats.user_dn = strdup(id);
OPENSSL_free(id);
}
}
if (r->jm_restart == NULL)
{
rc = globus_gram_job_manager_state_file_write(r);
}
if (rc != GLOBUS_SUCCESS)
{
client_addr_strdup_failed:
globus_fifo_destroy(&r->seg_event_queue);
seg_event_queue_init_failed:
if (r->job_history_file)
{
free(r->job_history_file);
r->job_history_file = NULL;
}
history_file_set_failed:
failed_populate_environment:
if (r->x509_user_proxy)
{
free(r->x509_user_proxy);
r->x509_user_proxy = NULL;
}
failed_export_cred:
pending_queries_init_failed:
staging_list_create_failed:
globus_cond_destroy(&r->cond);
cond_init_failed:
globus_mutex_destroy(&r->mutex);
mutex_init_failed:
if (r->log_pattern)
{
free(r->log_pattern);
}
get_job_log_pattern_failed:
get_job_log_levels_failed:
bad_proxy_timeout:
if (r->remote_io_url_file)
{
remove(r->remote_io_url_file);
free(r->remote_io_url_file);
}
make_remote_io_url_file_failed:
if (r->remote_io_url)
{
free(r->remote_io_url);
}
get_remote_io_url_failed:
get_two_phase_commit_failed:
get_dry_run_failed:
invalid_count:
validate_rsl_failed:
globus_gass_cache_close(&r->cache_handle);
free(r->cache_location);
init_cache_failed:
if (r->scratchdir)
{
globus_gram_job_manager_destroy_directory(r, r->scratchdir);
free(r->scratchdir);
r->scratchdir = NULL;
}
init_scratchdir_failed:
if (r->scratch_dir_base)
{
free(r->scratch_dir_base);
}
failed_eval_scratch_dir_base:
if (r->gateway_user)
{
free(r->gateway_user);
}
get_gateway_user_failed:
free(r->cache_tag);
cache_tag_alloc_failed:
failed_restart:
free(r->job_state_file);
failed_state_file_set:
cached_stderr_symboltable_failed:
free(r->cached_stderr);
cached_stderr_malloc_failed:
cached_stdout_symboltable_failed:
free(r->cached_stdout);
cached_stdout_malloc_failed:
if (rc != GLOBUS_GRAM_PROTOCOL_ERROR_OLD_JM_ALIVE)
{
globus_gram_job_manager_destroy_directory(r, r->job_dir);
}
free(r->job_dir);
failed_make_job_dir:
free(r->job_contact_path);
failed_set_job_contact_path:
failed_add_contact_to_symboltable:
if (r->job_contact)
{
free(r->job_contact);
}
failed_set_job_contact:
free(r->uniq_id);
failed_set_uniq_id:
if (r->jm_restart)
{
free(r->jm_restart);
}
failed_generate_id:
free(r->rsl_spec);
rsl_unparse_failed:
rsl_canonicalize_failed:
globus_rsl_free_recursive(r->rsl);
rsl_parse_failed:
symboltable_populate_failed:
symboltable_create_scope_failed:
globus_symboltable_destroy(&r->symbol_table);
if (r->gt3_failure_message)
{
if (gt3_failure_message != NULL)
{
*gt3_failure_message = r->gt3_failure_message;
}
else
{
free(r->gt3_failure_message);
}
}
if (r->gt3_failure_type)
{
free(r->gt3_failure_type);
}
if (r->gt3_failure_source)
{
free(r->gt3_failure_source);
}
if (r->gt3_failure_destination)
{
free(r->gt3_failure_destination);
}
symboltable_init_failed:
free(r);
r = NULL;
}
request_malloc_failed:
*request = r;
return rc;
}
/* globus_gram_job_manager_request_init() */
/**
* Load request and security context, initialize structures, and send reply.
*
* @param manager
* Job manager state
* @param http_body_fd
* File descriptor of the HTTP body file
* @param context_fd
* File descriptor pointing to the GSSAPI security context to use for the
* response.
* @param request
* Pointer to be set to the new job request
* @param context
* Pointer to be set to the security context of the initial job request
* @param contact
* Pointer to be set to the job contact of the client to send job state
* change notifications to.
* @param job_state_mask
* Pointer to be set to the job state mask for which job state changes
* the client is interested in.
* @param old_job_contact
* Pointer to a string to be set to the old job contact if
* GLOBUS_GRAM_PROTOCOL_ERROR_OLD_JM_ALIVE. Set to NULL otherwise.
* @param old_job_request
* Pointer to a job request structure that will be set to an existing
* one if the return value is GLOBUS_GRAM_PROTOCOL_ERROR_OLD_JM_ALIVE. Set
* to NULL otherwise. If non-null, the caller must release a reference
* when done processing this.
* @param gt3_failure_message
* Pointer to be set to an extended failure message to explain why the
* initialization failed.
*
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_GATEKEEPER_MISCONFIGURED
* Unable to import security context
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_PROTOCOL_FAILED
* Unable to read job request from http_body_fd
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED
* Malloc failed
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL
* Bad RSL
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_RSL_EVALUATION_FAILED
* RSL evaluation failed
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_RSL_SCRATCH
* Invalid scratchdir RSL attribute
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_SCRATCH
* Invalid scratchdir path
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_PARAMETER_NOT_SUPPORTED
* RSL attribute not supported.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_SUBMIT_ATTRIBUTE
* Invalid submit RSL attribute.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_RESTART_ATTRIBUTE
* Invalid restart RSL attribute.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_STDIO_UPDATE_ATTRIBUTE
* Invalid stdio_update RSL attribute.
*
* @note Even if this function returns a failure code, the @a context
* may still be initialized to the security context. If it points to a
* value other than GSS_C_NO_CONTEXT then it can be used by the caller
* to send the failure response.
*/
int
globus_gram_job_manager_request_load(
globus_gram_job_manager_t * manager,
int http_body_fd,
int context_fd,
gss_cred_id_t cred,
const char * peer_address,
globus_gsi_cred_handle_t peer_cred,
size_t content_length,
globus_gram_jobmanager_request_t ** request,
gss_ctx_id_t * context,
char ** contact,
int * job_state_mask,
char ** old_job_contact,
globus_gram_jobmanager_request_t ** old_job_request,
globus_bool_t * version_only,
char ** gt3_failure_message)
{
int rc;
char * rsl;
*request = NULL;
*context = GSS_C_NO_CONTEXT;
*contact = NULL;
*job_state_mask = 0;
*version_only = GLOBUS_FALSE;
if (context_fd != -1)
{
rc = globus_gram_job_manager_import_sec_context(
manager,
context_fd,
context);
if (rc != GLOBUS_SUCCESS)
{
goto import_context_failed;
}
}
rc = globus_gram_job_manager_read_request(
manager,
http_body_fd,
content_length,
&rsl,
contact,
job_state_mask,
version_only);
if (rc != GLOBUS_SUCCESS)
{
goto read_request_failed;
}
if (! (*version_only))
{
rc = globus_gram_job_manager_request_init(
request,
manager,
rsl,
cred,
*context,
peer_address,
peer_cred,
GLOBUS_FALSE,
old_job_contact,
old_job_request,
gt3_failure_message);
}
if (rc != GLOBUS_SUCCESS)
{
goto request_init_failed;
}
request_init_failed:
if (rsl)
{
free(rsl);
}
read_request_failed:
import_context_failed:
return rc;
}
/* globus_gram_job_manager_request_load() */
/** Verify that the job request is authorized, send a reply, and start the state machine for this request
*
* @param manager
* Job manager state
* @param request
* Job request to start
* @param response_fd
* Descriptor to write the response to
* @param client_contact
* Client to send job state changes to (may be NULL)
* @param job_state_mask
* Job state mask for sending state changes to the client
*/
int
globus_gram_job_manager_request_start(
globus_gram_job_manager_t * manager,
globus_gram_jobmanager_request_t * request,
int response_fd,
const char * client_contact,
int job_state_mask)
{
int rc;
int rc2;
int response_code;
char * job_contact;
if (request == NULL)
{
/* Reply to a bad request */
rc = GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_REQUEST;
goto bad_request;
}
rc = globus_gram_job_manager_add_request(
manager,
request->job_contact_path,
request);
GlobusGramJobManagerRequestLock(request);
if (rc != GLOBUS_SUCCESS)
{
goto add_request_failed;
}
rc = globus_gram_job_manager_call_authz_callout(
request->config,
request->response_context,
request->response_context,
request->uniq_id,
request->rsl,
"start");
if (rc != GLOBUS_SUCCESS)
{
goto authz_denied;
}
rc = globus_gram_job_manager_validate_username(request);
if (rc != 0)
{
goto username_denied;
}
if (client_contact != NULL)
{
rc = globus_gram_job_manager_contact_add(
request,
client_contact,
job_state_mask);
if (rc != GLOBUS_SUCCESS)
{
goto contact_add_failed;
}
}
if (request->dry_run)
{
request->failure_code = GLOBUS_GRAM_PROTOCOL_ERROR_DRYRUN;
rc = globus_gram_job_manager_request_set_status(
request,
GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED);
manager->usagetracker->count_dryrun++;
}
contact_add_failed:
username_denied:
authz_denied:
add_request_failed:
bad_request:
/* Reply to request with unsubmitted (and optionally
* two-phase-commit needed)
*/
switch (request->failure_code)
{
case GLOBUS_GRAM_PROTOCOL_ERROR_OLD_JM_ALIVE:
response_code = request->failure_code;
job_contact = request->old_job_contact;
break;
case GLOBUS_SUCCESS:
if (request->two_phase_commit)
{
response_code = GLOBUS_GRAM_PROTOCOL_ERROR_WAITING_FOR_COMMIT;
}
else
{
response_code = GLOBUS_SUCCESS;
}
job_contact = request->job_contact;
break;
default:
response_code = request->failure_code;
job_contact = NULL;
break;
}
rc2 = globus_gram_job_manager_reply(
request,
request->manager,
response_code,
job_contact,
response_fd,
request->response_context,
NULL);
if (rc == GLOBUS_SUCCESS && rc2 == GLOBUS_SUCCESS)
{
globus_reltime_t delay;
GlobusTimeReltimeSet(delay, 0, 0);
rc = globus_gram_job_manager_state_machine_register(
manager,
request,
&delay);
}
else if (rc == GLOBUS_SUCCESS && rc2 != GLOBUS_SUCCESS)
{
rc = rc2;
}
GlobusGramJobManagerRequestUnlock(request);
return rc;
}
/* globus_gram_job_manager_request_start() */
/**
* Deallocate memory related to a request.
*
* This function frees the data within the request, also destroying
* files associated with the request.
* The caller must not access the request after this function has returned.
*
* @param request
* Job request to destroy.
*
* @return GLOBUS_SUCCESS
*/
void
globus_gram_job_manager_request_destroy(
globus_gram_jobmanager_request_t * request)
{
if (!request)
{
return;
}
if (request->scratchdir)
{
globus_gram_job_manager_destroy_directory(
request,
request->scratchdir);
}
globus_gram_job_manager_request_free(request);
}
/* globus_gram_job_manager_request_destroy() */
/**
* Deallocate memory related to a request.
*
* This function frees the data within the request.
*
* @param request
* Job request to destroy.
*
* @return GLOBUS_SUCCESS
*/
void
globus_gram_job_manager_request_free(
globus_gram_jobmanager_request_t * request)
{
if (!request)
{
return;
}
if (request->job_id_string)
{
free(request->job_id_string);
}
if (request->original_job_id_string)
{
free(request->original_job_id_string);
}
if (request->uniq_id)
{
free(request->uniq_id);
}
if (request->cached_stdout)
{
free(request->cached_stdout);
}
if (request->cached_stderr)
{
free(request->cached_stderr);
}
if (request->jm_restart)
{
free(request->jm_restart);
}
if (request->scratch_dir_base)
{
free(request->scratch_dir_base);
}
if (request->scratchdir)
{
free(request->scratchdir);
}
if (request->cache_tag)
{
free(request->cache_tag);
}
if (request->symbol_table != NULL)
{
globus_symboltable_destroy(&request->symbol_table);
}
if (request->rsl_spec)
{
free(request->rsl_spec);
}
if (request->rsl)
{
globus_rsl_free_recursive(request->rsl);
}
if (request->remote_io_url)
{
free(request->remote_io_url);
}
if (request->remote_io_url_file)
{
free(request->remote_io_url_file);
}
if (request->x509_user_proxy)
{
free(request->x509_user_proxy);
}
if (request->job_state_file)
{
free(request->job_state_file);
}
if (request->gt3_failure_type)
{
free(request->gt3_failure_type);
}
if (request->gt3_failure_message)
{
free(request->gt3_failure_message);
}
if (request->gt3_failure_source)
{
free(request->gt3_failure_source);
}
if (request->gt3_failure_destination)
{
free(request->gt3_failure_destination);
}
globus_mutex_destroy(&request->mutex);
globus_cond_destroy(&request->cond);
globus_gram_job_manager_contact_list_free(request);
globus_gram_job_manager_staging_free_all(request);
globus_assert(request->poll_timer == GLOBUS_NULL_HANDLE);
if (request->job_contact)
{
free(request->job_contact);
}
if (request->job_contact_path)
{
free(request->job_contact_path);
}
if (request->pending_queries)
{
globus_fifo_destroy(&request->pending_queries);
}
if (request->job_history_file)
{
free(request->job_history_file);
}
if (request->job_dir)
{
free(request->job_dir);
}
if (request->cache_location)
{
free(request->cache_location);
}
if (request->gateway_user)
{
free(request->gateway_user);
}
if (request->cache_handle)
{
globus_gass_cache_close(&request->cache_handle);
}
if (request->response_context != GSS_C_NO_CONTEXT)
{
OM_uint32 minor_status;
gss_delete_sec_context(&minor_status, &request->response_context, NULL);
}
if (request->seg_event_queue)
{
globus_fifo_destroy_all(
&request->seg_event_queue,
globus_l_gram_event_destroy);
}
if (request->job_stats.client_address != NULL)
{
free(request->job_stats.client_address);
}
if (request->job_stats.user_dn != NULL)
{
free(request->job_stats.user_dn);
}
}
/* globus_gram_job_manager_request_free() */
/**
* Change the status associated with a job request
*
* Changes the status associated with a job request.
* There is now additional tracking data associated with the
* status that must be updated when the status is. This function
* handles managing it. It is NOT recommended that you directly
* change the status.
*
* @param request
* Job request to change status of.
* @param status
* Status to set the job request to.
*
* @return GLOBUS_SUCCESS assuming valid input.
* If the request is null, returns GLOBUS_FAILURE.
*/
int
globus_gram_job_manager_request_set_status(
globus_gram_jobmanager_request_t * request,
globus_gram_protocol_job_state_t status)
{
switch (status)
{
case GLOBUS_GRAM_PROTOCOL_JOB_STATE_PENDING:
GlobusTimeAbstimeGetCurrent(request->job_stats.pending_timestamp);
break;
case GLOBUS_GRAM_PROTOCOL_JOB_STATE_ACTIVE:
GlobusTimeAbstimeGetCurrent(request->job_stats.active_timestamp);
break;
case GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED:
GlobusTimeAbstimeGetCurrent(request->job_stats.failed_timestamp);
break;
case GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE:
GlobusTimeAbstimeGetCurrent(request->job_stats.done_timestamp);
break;
case GLOBUS_GRAM_PROTOCOL_JOB_STATE_UNSUBMITTED:
GlobusTimeAbstimeGetCurrent(request->job_stats.unsubmitted_timestamp);
break;
case GLOBUS_GRAM_PROTOCOL_JOB_STATE_STAGE_IN:
GlobusTimeAbstimeGetCurrent(request->job_stats.file_stage_in_timestamp);
break;
case GLOBUS_GRAM_PROTOCOL_JOB_STATE_STAGE_OUT:
GlobusTimeAbstimeGetCurrent(request->job_stats.file_stage_out_timestamp);
break;
case GLOBUS_GRAM_PROTOCOL_JOB_STATE_SUSPENDED:
case GLOBUS_GRAM_PROTOCOL_JOB_STATE_ALL:
break;
}
return globus_gram_job_manager_request_set_status_time(
request,
status,
time(0));
}
/* globus_gram_job_manager_request_set_status() */
/**
* Change the status associated with a job request
*
* Changes the status associated with a job request.
* There is now additional tracking data associated with the
* status that must be updated when the status is. This function
* handles managing it. It is NOT recommended that you directly
* change the status.
*
* @param request
* Job request to change status of.
* @param status
* Status to set the job request to.
* @param valid_time
* The status is known good as of this time (seconds since epoch)
*
* @return GLOBUS_SUCCESS assuming valid input.
* If the request is null, returns GLOBUS_FAILURE.
*/
int
globus_gram_job_manager_request_set_status_time(
globus_gram_jobmanager_request_t * request,
globus_gram_protocol_job_state_t status,
time_t valid_time)
{
if( ! request )
return GLOBUS_FAILURE;
request->status = status;
request->status_update_time = valid_time;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_DEBUG,
"event=gram.job.info "
"level=DEBUG "
"gramid=%s "
"job_status=%d "
"\n",
request->job_contact_path,
status);
if (request->manager != NULL)
{
globus_gram_job_manager_set_status(
request->manager,
request->job_contact_path,
request->status,
request->failure_code,
request->exit_code);
}
return GLOBUS_SUCCESS;
}
/* globus_gram_job_manager_request_set_status() */
/**
* Write data to the job manager log file
*
* This function writes data to the passed file, using a printf format
* string. Data is prefixed with a timestamp when written.
*
* @param log_fp
* Log file to write to.
* @param format
* Printf-style format string to be written.
* @param ...
* Parameters substituted into the format string, if needed.
*
* @return This function returns the value returned by vfprintf.
*/
void
globus_gram_job_manager_request_log(
globus_gram_jobmanager_request_t * request,
globus_gram_job_manager_log_level_t level,
const char * format,
... )
{
va_list ap;
int stdio_level = level;
/* Allow logging code to determine this thread's current job request to handle
* per-job log configuration
*/
globus_thread_setspecific(
globus_i_gram_request_key,
request);
/* job_log_level is initialized to the global log level, but can be modified via
* the log_level RSL attribute. If it masks the requested log level to zero, don't bother
* calling the logging functions
*/
if (request != NULL && request->job_log_level != -1)
{
stdio_level = level & request->job_log_level;
if (stdio_level == 0)
{
goto skip_stdio;
}
/*
* If we have a request-specific log level that differs from the global
* config, we need to make sure the log level matches the global type
* mask so that it won't get discarded by globus_logging_vwrite
*/
if (request->config)
{
stdio_level = request->config->log_levels;
}
}
va_start(ap, format);
globus_logging_vwrite(
globus_i_gram_job_manager_log_stdio,
stdio_level,
format,
ap);
va_end(ap);
skip_stdio:
if (globus_i_gram_job_manager_log_sys != NULL)
{
va_start(ap, format);
globus_logging_vwrite(
globus_i_gram_job_manager_log_sys,
level,
format,
ap);
va_end(ap);
}
globus_thread_setspecific(
globus_i_gram_request_key,
NULL);
}
/* globus_gram_job_manager_request_log() */
/**
* Write data to the job manager accounting file.
* Also use syslog() to allow for easy central collection.
*
* This function writes data to the passed file descriptor, if any,
* using a printf format string.
* Data is prefixed with a timestamp when written.
*
* @param format
* Printf-style format string to be written.
* @param ...
* Parameters substituted into the format string, if needed.
*
* @return This function returns the value returned by write().
*/
int
globus_gram_job_manager_request_acct(
globus_gram_jobmanager_request_t * request,
const char * format,
... )
{
static const char *jm_syslog_id = "gridinfo";
static int jm_syslog_fac = LOG_DAEMON;
static int jm_syslog_lvl = LOG_NOTICE;
static int jm_syslog_init;
struct tm *curr_tm;
time_t curr_time;
va_list ap;
int rc = -1;
int fd;
const char * gk_acct_fd_var = "GATEKEEPER_ACCT_FD";
const char * gk_acct_fd;
int n;
int t;
char buf[1024 * 128];
time( &curr_time );
curr_tm = localtime( &curr_time );
n = t = sprintf( buf, "JMA %04d/%02d/%02d %02d:%02d:%02d ",
curr_tm->tm_year + 1900,
curr_tm->tm_mon + 1, curr_tm->tm_mday,
curr_tm->tm_hour, curr_tm->tm_min,
curr_tm->tm_sec );
va_start( ap, format );
/*
* FIXME: we should use vsnprintf() here...
*/
n += vsprintf( buf + t, format, ap );
if (!jm_syslog_init)
{
const char *s;
if ((s = getenv( "JOBMANAGER_SYSLOG_ID" )) != 0)
{
jm_syslog_id = *s ? s : 0;
}
if ((s = getenv( "JOBMANAGER_SYSLOG_FAC" )) != 0)
{
if (sscanf( s, "%d", &jm_syslog_fac ) != 1 || jm_syslog_fac < 0)
{
jm_syslog_id = 0;
}
}
if ((s = getenv( "JOBMANAGER_SYSLOG_LVL" )) != 0)
{
if (sscanf( s, "%d", &jm_syslog_lvl ) != 1 || jm_syslog_lvl < 0)
{
jm_syslog_id = 0;
}
}
if (jm_syslog_id)
{
openlog( jm_syslog_id, LOG_PID, jm_syslog_fac );
}
jm_syslog_init = 1;
}
if (jm_syslog_id)
{
char *p, *q = buf;
while ((p = q) < buf + n) {
char c;
while ((c = *q) != 0 && c != '\n') {
q++;
}
*q = 0;
syslog( jm_syslog_lvl, "%s", p );
*q++ = c;
}
}
if (!(gk_acct_fd = getenv( gk_acct_fd_var )))
{
return -1;
}
if (sscanf( gk_acct_fd, "%d", &fd ) != 1)
{
return -1;
}
fcntl( fd, F_SETFD, FD_CLOEXEC );
if ((rc = write( fd, buf, n )) != n)
{
rc = -1;
}
return rc;
}
/* globus_gram_job_manager_request_acct() */
#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
/**
* Populate the request symbol table with values from the job manager config
*
* @param request
* Request to update the symbol table of
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED
* Malloc failed
*/
int
globus_i_gram_symbol_table_populate(
globus_gram_job_manager_config_t * config,
globus_symboltable_t * symbol_table)
{
int rc = GLOBUS_SUCCESS;
int i;
struct { char * symbol; char *value; } symbols[] =
{
{ "HOME", config->home },
{ "LOGNAME", config->logname },
{ "GLOBUS_ID", config->subject },
{ "GLOBUS_HOST_MANUFACTURER",config->globus_host_manufacturer},
{ "GLOBUS_HOST_CPUTYPE",config->globus_host_cputype},
{ "GLOBUS_HOST_OSNAME",config->globus_host_osname},
{ "GLOBUS_HOST_OSVERSION",config->globus_host_osversion},
{ "GLOBUS_GATEKEEPER_HOST",config->globus_gatekeeper_host},
{ "GLOBUS_GATEKEEPER_PORT",config->globus_gatekeeper_port},
{ "GLOBUS_GATEKEEPER_SUBJECT",config->globus_gatekeeper_subject},
{ "GLOBUS_LOCATION", config->target_globus_location },
{ "GLOBUS_CONDOR_OS", config->condor_os } /* Deprecated */,
{ "GLOBUS_CONDOR_ARCH", config->condor_arch } /* Deprecated */,
/* Others are job dependent values inserted after they are computed:
* - GLOBUS_GRAM_JOB_CONTACT
* - GLOBUS_CACHED_STDOUT
* - GLOBUS_CACHED_STDERR
* - SCRATCH_DIRECTORY
*/
{ NULL, NULL }
};
for (i = 0; symbols[i].symbol != NULL; i++)
{
if (symbols[i].value != NULL)
{
rc = globus_l_gram_symboltable_add(
symbol_table,
symbols[i].symbol,
symbols[i].value);
if (rc != GLOBUS_SUCCESS)
{
goto failed_insert_symbol;
}
}
}
if (rc != GLOBUS_SUCCESS)
{
failed_insert_symbol:
for (--i; i >=0; i--)
{
globus_symboltable_remove(
symbol_table,
symbols[i].symbol);
}
}
return rc;
}
/* globus_gram_symbol_table_populate() */
/**
* Insert a symbol into the RSL evaluation symbol table
*
* Also checks that the value is non-NULL and transforms the return
* value to a GRAM error code.
*
* @param symboltable
* Symbol table to insert the value to.
* @param symbol
* Symbol name to add to the table.
* @pram value
* Symbol value to add to the table. If NULL nothing is inserted.
*
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED
* Malloc failed
*/
static
int
globus_l_gram_symboltable_add(
globus_symboltable_t * symbol_table,
const char * symbol,
const char * value)
{
int rc = GLOBUS_SUCCESS;
if (value != NULL)
{
rc = globus_symboltable_insert(
symbol_table,
(void *) symbol,
(void *) value);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
}
}
return rc;
}
/* globus_l_gram_symboltable_add() */
static
int
globus_l_gram_generate_id(
globus_gram_jobmanager_request_t * request,
char ** jm_restart,
uint64_t * uniq1p,
uint64_t * uniq2p)
{
int rc = GLOBUS_SUCCESS;
*jm_restart = NULL;
if(globus_gram_job_manager_rsl_need_restart(request))
{
/* Need to do this before unique id is set */
rc = globus_gram_job_manager_rsl_eval_one_attribute(
request,
GLOBUS_GRAM_PROTOCOL_RESTART_PARAM,
jm_restart);
if (rc == GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_RESTART;
goto failed_jm_restart_eval;
}
else if (rc != GLOBUS_SUCCESS)
{
goto failed_jm_restart_eval;
}
else if (*jm_restart == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_RESTART;
goto failed_jm_restart_eval;
}
rc = sscanf(
request->jm_restart,
"https://%*[^:]:%*d/%"PRIu64"/%"PRIu64,
uniq1p,
uniq2p);
if (rc < 2)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_RESTART;
goto failed_jm_restart_scan;
}
rc = GLOBUS_SUCCESS;
}
else
{
globus_uuid_t uuid;
rc = globus_uuid_create(&uuid);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
}
request->jm_restart = NULL;
memcpy(uniq1p, uuid.binary.bytes, 8);
memcpy(uniq2p, uuid.binary.bytes+8, 8);
}
if (rc != GLOBUS_SUCCESS)
{
failed_jm_restart_scan:
if (request->jm_restart != NULL)
{
free(request->jm_restart);
}
}
failed_jm_restart_eval:
return rc;
}
/* globus_l_gram_generate_id() */
/**
* Determine the cache location to use for this job
*
* If the gass_cache RSL attribute is present, it is evaluated and used.
* Otherwise, if -cache-location was in the configuration, it used. Otherwise,
* the GASS cache library default is used.
*
* @param request
* Request to use to find which value to use.
* @param cache_locationp
* Pointer to set to the job-specific cache location.
* @param cache_handlep
* Pointer to the GASS cache handle to initialize for this job
*
* @retval GLOBUS_SUCCESS
* Success.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_RSL_CACHE
* Invalid gass_cache RSL parameter.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED
* Malloc failed.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_CACHE
* Invalid cache path.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_OPENING_CACHE
* Error opening cache.
*/
static
int
globus_l_gram_init_cache(
globus_gram_jobmanager_request_t * request,
char ** cache_locationp,
globus_gass_cache_t * cache_handlep)
{
int rc = GLOBUS_SUCCESS;
int gassrc = GLOBUS_SUCCESS;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_TRACE,
"event=gram.gass_cache_init.start "
"level=TRACE "
"gramid=%s "
"\n",
request->job_contact_path);
if (globus_gram_job_manager_rsl_attribute_exists(
request->rsl,
GLOBUS_GRAM_PROTOCOL_GASS_CACHE_PARAM))
{
/* If gass_cache is in RSL, we'll evaluate that and use it. */
rc = globus_gram_job_manager_rsl_eval_one_attribute(
request,
GLOBUS_GRAM_PROTOCOL_GASS_CACHE_PARAM,
cache_locationp);
if (rc != GLOBUS_SUCCESS)
{
if (rc == GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_CACHE;
}
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.gass_cache_init.end "
"level=ERROR "
"gramid=%s "
"status=%d "
"msg=\"%s\" "
"reason=\"%s\" "
"\n",
request->job_contact_path,
-rc,
"Error evaluating cache RSL attribute",
globus_gram_protocol_error_string(rc));
goto failed_cache_eval;
}
/* cache location in rsl, but not a literal after eval */
if ((*cache_locationp) == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_CACHE;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.gass_cache_init.end "
"level=ERROR "
"gramid=%s "
"status=%d "
"msg=\"%s\" "
"reason=\"%s\" "
"\n",
request->job_contact_path,
-rc,
"Error evaluating cache RSL attribute",
globus_gram_protocol_error_string(rc));
goto failed_cache_eval;
}
}
else if (request->config->cache_location != NULL)
{
/* If -cache-location was on command-line or config file, then
* eval and use it
*/
rc = globus_gram_job_manager_rsl_eval_string(
&request->symbol_table,
request->config->cache_location,
cache_locationp);
if(rc != GLOBUS_SUCCESS)
{
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.gass_cache_init.end "
"level=ERROR "
"gramid=%s "
"status=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
-rc,
globus_gram_protocol_error_string(rc));
goto failed_cache_eval;
}
}
else
{
/* I'd like to use GASS-default location for the cache for this,
* but we can't rely on being able to set the environment and having
* it remain valid if multiple jobs are being processed. Instead, we'll
* force it to what the library would do anyway.
*/
*cache_locationp = globus_common_create_string(
"%s/.globus/.gass_cache",
request->config->home);
}
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_TRACE,
"event=gram.gass_cache_init.info "
"level=TRACE "
"gramid=%s "
"path=%s\n",
request->job_contact_path,
*(cache_locationp));
memset(cache_handlep, 0, sizeof(globus_gass_cache_t));
gassrc = rc = globus_gass_cache_open(*cache_locationp, cache_handlep);
if(rc != GLOBUS_SUCCESS)
{
if (*cache_locationp)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_CACHE;
}
else
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_OPENING_CACHE;
}
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.gass_cache_init.end "
"level=ERROR "
"gramid=%s "
"status=%d "
"path=%s "
"msg=\"%s\" "
"gasserror=%d "
"reason=\"%s\"\n",
request->job_contact_path,
-rc,
*(cache_locationp),
"Error opening GASS cache",
gassrc,
globus_gass_cache_error_string(gassrc));
if (*cache_locationp)
{
free(*cache_locationp);
*cache_locationp = NULL;
}
}
else
{
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_TRACE,
"event=gram.gass_cache_init.end "
"level=TRACE "
"gramid=%s "
"status=%d "
"path=%s\n",
request->job_contact_path,
0,
*(cache_locationp));
}
failed_cache_eval:
return rc;
}
/* globus_l_gram_init_cache() */
static
int
globus_l_gram_restart(
globus_gram_jobmanager_request_t * request,
globus_gram_jobmanager_request_t ** old_job_request)
{
int rc;
globus_rsl_t * stdout_position;
globus_rsl_t * stderr_position;
globus_rsl_t * restart_rsl;
globus_rsl_t * original_rsl;
globus_rsl_t * restartcontacts;
globus_bool_t restart_contacts = GLOBUS_FALSE;
/* Evaluate the restart RSL, so that we can merge it with the original
* job RSL
*/
rc = globus_rsl_eval(request->rsl, &request->symbol_table);
if(rc != GLOBUS_SUCCESS)
{
request->failure_code =
GLOBUS_GRAM_PROTOCOL_ERROR_RSL_EVALUATION_FAILED;
goto rsl_eval_failed;
}
rc = globus_gram_job_manager_validate_rsl(
request,
request->rsl,
GLOBUS_GRAM_VALIDATE_JOB_MANAGER_RESTART);
if(rc != GLOBUS_SUCCESS)
{
goto rsl_validate_failed;
}
/*
* Eval after validating, as validation may insert
* RSL substitions when processing default values of
* RSL attributes
*/
rc = globus_rsl_eval(request->rsl, &request->symbol_table);
if(rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_EVALUATION_FAILED;
goto post_validate_eval_failed;
}
/* Free the restart RSL spec. Make room for the job
* request RSL which we'll read from the state file
*/
free(request->rsl_spec);
request->rsl_spec = NULL;
/* Remove the restart parameter from the RSL spec. */
globus_gram_job_manager_rsl_remove_attribute(
request->rsl,
GLOBUS_GRAM_PROTOCOL_RESTART_PARAM);
if (globus_gram_job_manager_request_exists(
request->manager,
request->job_contact_path))
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_OLD_JM_ALIVE;
}
else
{
/* Read the job state file. This has all sorts of side-effects on
* the request structure
*/
rc = globus_gram_job_manager_state_file_read(request);
}
if (rc == GLOBUS_GRAM_PROTOCOL_ERROR_OLD_JM_ALIVE)
{
/* Something is handling this request already. We'll check if it is
* this process. If so, we'll merge the RSLs (as if we had done a
* stdio update.
*/
rc = globus_gram_job_manager_add_reference(
request->manager,
request->job_contact_path,
"restart",
old_job_request);
if (rc != GLOBUS_SUCCESS)
{
/* OK. It's alive, but not our job. Let it be */
rc = GLOBUS_GRAM_PROTOCOL_ERROR_OLD_JM_ALIVE;
goto state_file_read_failed;
}
rc = globus_i_gram_request_stdio_update(
(*old_job_request),
request->rsl);
if (rc == GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_OLD_JM_ALIVE;
}
else
{
/* Likely, we return GLOBUS_SUCCESS in this case, the wrong error value */
rc = globus_gram_job_manager_remove_reference(
request->manager,
request->job_contact_path,
"restart");
*old_job_request = NULL;
}
goto old_jm_alive;
}
else if(rc != GLOBUS_SUCCESS)
{
goto state_file_read_failed;
}
original_rsl = globus_rsl_parse(request->rsl_spec);
if (!original_rsl)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL;
goto parse_original_rsl_failed;
}
restart_rsl = request->rsl;
request->rsl = NULL;
rc = globus_gram_job_manager_rsl_attribute_get_boolean_value(
restart_rsl,
"restartcontacts",
&restart_contacts);
if (rc != GLOBUS_SUCCESS || !restart_contacts)
{
globus_gram_job_manager_contact_list_free(request);
rc = GLOBUS_SUCCESS;
}
restartcontacts = globus_gram_job_manager_rsl_extract_relation(
restart_rsl,
"restartcontacts");
if (restartcontacts != NULL)
{
globus_rsl_free_recursive(restartcontacts);
}
/*
* Remove stdout_position and stderr_position. We don't do streaming
* any more, so we will reject any restart where the positions
* aren't 0 (validation file checks those).
*/
stdout_position = globus_gram_job_manager_rsl_extract_relation(
restart_rsl,
GLOBUS_GRAM_PROTOCOL_STDOUT_POSITION_PARAM);
if (stdout_position != NULL)
{
globus_rsl_free_recursive(stdout_position);
}
stderr_position = globus_gram_job_manager_rsl_extract_relation(
restart_rsl,
GLOBUS_GRAM_PROTOCOL_STDERR_POSITION_PARAM);
if (stderr_position != NULL)
{
globus_rsl_free_recursive(stderr_position);
}
request->rsl = globus_gram_job_manager_rsl_merge(
original_rsl,
restart_rsl);
if(request->rsl == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
}
request->job_stats.restart_count++;
if (original_rsl)
{
globus_rsl_free_recursive(original_rsl);
}
if (restart_rsl)
{
globus_rsl_free_recursive(restart_rsl);
}
parse_original_rsl_failed:
old_jm_alive:
state_file_read_failed:
post_validate_eval_failed:
rsl_validate_failed:
rsl_eval_failed:
return rc;
}
/* globus_l_gram_restart() */
int
globus_i_gram_request_stdio_update(
globus_gram_jobmanager_request_t * request,
globus_rsl_t * update_rsl)
{
int rc = GLOBUS_SUCCESS;
const char * tmp_string;
globus_rsl_t * tmp_rsl;
globus_rsl_t * stdout_position;
globus_rsl_t * stderr_position;
globus_rsl_t * original_rsl;
/* TODO: We should almost certainly validate RSL here
rc = globus_gram_job_manager_validate_rsl(
request,
tmp_rsl,
GLOBUS_GRAM_VALIDATE_STDIO_UPDATE);
if(rc != GLOBUS_SUCCESS)
{
goto parse_original_rsl_failed;
}
*/
/*
* Remove stdout_position and stderr_position. We don't do streaming
* any more, so we will reject any restart where the positions
* aren't 0.
*/
stdout_position = globus_gram_job_manager_rsl_extract_relation(
update_rsl,
GLOBUS_GRAM_PROTOCOL_STDOUT_POSITION_PARAM);
if (stdout_position != NULL)
{
globus_rsl_free_recursive(stdout_position);
}
stderr_position = globus_gram_job_manager_rsl_extract_relation(
update_rsl,
GLOBUS_GRAM_PROTOCOL_STDERR_POSITION_PARAM);
if (stderr_position != NULL)
{
globus_rsl_free_recursive(stderr_position);
}
original_rsl = globus_rsl_parse(request->rsl_spec);
if (!original_rsl)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL;
goto parse_original_rsl_failed;
}
/* TODO: it appears tmp_rsl leaks if a failure occurs */
tmp_rsl = globus_gram_job_manager_rsl_merge(
original_rsl,
update_rsl);
if (tmp_rsl == GLOBUS_NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL;
goto failed_rsl_merge;
}
/* The update_rsl, for job restarts, contains a "restartcontacts"
* attribute. This must be removed from the merged RSL prior to
* saving it to disk; otherwise, it will cause submits to choke.
* TODO: There should be a smarter way to do this with RSL validation.
*/
if (globus_gram_job_manager_rsl_remove_attribute(tmp_rsl, "restartcontacts"))
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL;
goto failed_rsl_merge;
}
char * tmp_rsl_spec;
if (!(tmp_rsl_spec = globus_rsl_unparse(tmp_rsl))) {
rc = GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL;
goto failed_rsl_merge;
}
rc = globus_rsl_eval(tmp_rsl, &request->symbol_table);
if(rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_EVALUATION_FAILED;
goto failed_rsl_merge;
}
rc = globus_gram_job_manager_validate_rsl(
request,
tmp_rsl,
GLOBUS_GRAM_VALIDATE_JOB_SUBMIT);
if(rc != GLOBUS_SUCCESS)
{
goto failed_rsl_merge;
}
rc = globus_rsl_eval(tmp_rsl, &request->symbol_table);
if(rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_EVALUATION_FAILED;
goto failed_rsl_merge;
}
globus_rsl_free_recursive(request->rsl);
request->rsl = tmp_rsl;
free(request->rsl_spec);
request->rsl_spec = tmp_rsl_spec;
rc = globus_gram_job_manager_streaming_list_replace(request);
if (rc != GLOBUS_SUCCESS)
{
goto staging_list_replace_failed;
}
rc = globus_gram_job_manager_rsl_attribute_get_string_value(
request->rsl,
GLOBUS_GRAM_PROTOCOL_REMOTE_IO_URL_PARAM,
&tmp_string);
switch (rc)
{
case GLOBUS_GRAM_PROTOCOL_ERROR_UNDEFINED_ATTRIBUTE:
rc = GLOBUS_SUCCESS;
break;
case GLOBUS_SUCCESS:
if (tmp_string != NULL)
{
if (request->remote_io_url)
{
free(request->remote_io_url);
}
request->remote_io_url = strdup(tmp_string);
if (request->remote_io_url == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto get_remote_io_url_failed;
}
}
else
{
default:
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_REMOTE_IO_URL;
goto get_remote_io_url_failed;
}
}
if (request->remote_io_url)
{
rc = globus_i_gram_remote_io_url_update(request);
}
/* Now that we've recreated the stdio, redo the staging list. */
globus_gram_job_manager_staging_free_all(request);
/*
globus_gram_jobmanager_request_t * request)
request->stage_in_todo = NULL;
request->stage_in_shared_todo = NULL;
request->stage_out_todo = NULL;
request->stage_stream_todo = NULL;
*/
rc = globus_gram_job_manager_staging_create_list(request);
if (rc != GLOBUS_SUCCESS) {
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.restart.info "
"level=ERROR "
"gramid=%s "
"status=%d "
"msg=\"%s\" "
"reason=\"%s\" "
"\n",
request->job_contact_path,
-rc,
"Unable to recreate staging list",
globus_gram_protocol_error_string(rc));
goto staging_list_replace_failed;
}
globus_gram_job_manager_state_file_write(request);
get_remote_io_url_failed:
staging_list_replace_failed:
failed_rsl_merge:
if (original_rsl) {
globus_rsl_free_recursive(original_rsl);
}
parse_original_rsl_failed:
return rc;
}
/* globus_i_gram_request_stdio_update() */
/**
* Add default environment variables to the job environment
*
* Adds GLOBUS_GASS_CACHE_DEFAULT, LOGNAME, HOME, and anything that
* is defined from the -extra-envar command-line option.
*
* @param request
* Request to modify
*
* @retval GLOBUS_SUCCESS
* Success.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
* Malloc failed.
*/
static
int
globus_l_gram_populate_environment(
globus_gram_jobmanager_request_t * request)
{
int rc;
rc = globus_l_gram_add_environment(
request->rsl,
"GLOBUS_GASS_CACHE_DEFAULT",
request->cache_location);
if (rc != GLOBUS_SUCCESS)
{
goto add_cache_default_failed;
}
rc = globus_l_gram_add_environment(
request->rsl,
"LOGNAME",
request->config->logname);
if (rc != GLOBUS_SUCCESS)
{
goto add_logname_failed;
}
rc = globus_l_gram_add_environment(
request->rsl,
"HOME",
request->config->home);
if (rc != GLOBUS_SUCCESS)
{
goto add_home_failed;
}
if (request->config->x509_cert_dir)
{
rc = globus_l_gram_add_environment(
request->rsl,
"X509_CERT_DIR",
request->config->x509_cert_dir);
if (rc != GLOBUS_SUCCESS)
{
goto add_x509_cert_dir_failed;
}
}
rc = globus_l_gram_add_environment(
request->rsl,
"GLOBUS_GRAM_JOB_CONTACT",
request->job_contact);
if (rc != GLOBUS_SUCCESS)
{
goto add_gram_job_contact_failed;
}
if (request->config->target_globus_location)
{
rc = globus_l_gram_add_environment(
request->rsl,
"GLOBUS_LOCATION",
request->config->target_globus_location);
if (rc != GLOBUS_SUCCESS)
{
goto add_globus_location_failed;
}
}
if (request->config->tcp_port_range)
{
rc = globus_l_gram_add_environment(
request->rsl,
"GLOBUS_TCP_PORT_RANGE",
request->config->tcp_port_range);
if (rc != GLOBUS_SUCCESS)
{
goto add_tcp_port_range_failed;
}
}
if (request->config->tcp_source_range)
{
rc = globus_l_gram_add_environment(
request->rsl,
"GLOBUS_TCP_SOURCE_RANGE",
request->config->tcp_source_range);
if (rc != GLOBUS_SUCCESS)
{
goto add_tcp_source_range_failed;
}
}
if (request->remote_io_url_file)
{
rc = globus_l_gram_add_environment(
request->rsl,
"GLOBUS_REMOTE_IO_URL",
request->remote_io_url_file);
if (rc != GLOBUS_SUCCESS)
{
goto add_remote_io_url_file;
}
}
if (request->x509_user_proxy)
{
rc = globus_l_gram_add_environment(
request->rsl,
"X509_USER_PROXY",
request->x509_user_proxy);
if (rc != GLOBUS_SUCCESS)
{
goto add_x509_user_proxy_failed;
}
}
if (request->config->extra_envvars)
{
globus_list_t *l = request->config->extra_envvars;
while (l)
{
char *p = globus_list_first(l);
char *q;
char *var, *val;
l = globus_list_rest(l);
if ((q = strchr(p, '=')) != NULL)
{
var = globus_common_create_string("%.*s",
(int) (q-p), p);
val = q+1;
}
else
{
var = strdup(p);
val = getenv(var);
}
if (var && val)
{
rc = globus_l_gram_add_environment(
request->rsl,
var,
val);
if (rc != GLOBUS_SUCCESS)
{
free(var);
goto add_extra_envvar_failed;
}
}
if (var)
{
free(var);
}
}
}
add_extra_envvar_failed:
add_x509_user_proxy_failed:
add_remote_io_url_file:
add_tcp_port_range_failed:
add_tcp_source_range_failed:
add_globus_location_failed:
add_gram_job_contact_failed:
add_x509_cert_dir_failed:
add_home_failed:
add_logname_failed:
add_cache_default_failed:
return rc;
}
/* globus_l_gram_populate_environment() */
/**
* Add an environment variable to the job environment
*
* @param rsl
* RSL to modify
* @param variable
* Environment variable name
* @param value
* Environment variable value. If NULL, this variable is ignored.
*
* @retval GLOBUS_SUCCESS
* Success.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
* Malloc failed.
*/
static
int
globus_l_gram_add_environment(
globus_rsl_t * rsl,
const char * variable,
const char * value)
{
int rc = GLOBUS_SUCCESS;
if (value != NULL)
{
rc = globus_gram_job_manager_rsl_env_add(
rsl,
variable,
value);
if (rc != 0)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
}
}
return rc;
}
/* globus_l_gram_add_environment() */
/**
* Initialize the scratchdir member of the job request if needed
*
* As a side effect, the path named by scratchdir will be created and
* variable SCRATCH_DIRECTORY will be added to both the symbol table and
* the job environment.
*
* @param request
* Request to act on.
* @param rsl
* RSL to check for "scratchdir" attribute
* @param scratch_dir_base
* Job-specific scratch_dir_base
* @param scratchdir
* Pointer to set to the new scratchdir
*
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_RSL_SCRATCH
* Invalid scratchdir RSL attribute
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED
* Malloc failed
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_SCRATCH
* Invalid scratchdir path
*/
static
int
globus_l_gram_init_scratchdir(
globus_gram_jobmanager_request_t * request,
globus_rsl_t * rsl,
const char * scratch_dir_base,
char ** scratchdir)
{
int rc = GLOBUS_SUCCESS;
char * dir = NULL;
char * template = NULL;
int i;
int created = 0;
enum { GLOBUS_GRAM_MKDIR_TRIES = 100 };
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_DEBUG,
"event=gram.init_scratchdir.start "
"level=DEBUG "
"gramid=%s "
"base=\"%s\" "
"\n",
request->job_contact_path,
scratch_dir_base);
/* In the case of a restart, this might have already been done */
if (request->jm_restart && request->scratchdir != NULL)
{
goto skip_mkdir;
}
if (! globus_gram_job_manager_rsl_attribute_exists(
rsl,
GLOBUS_GRAM_PROTOCOL_SCRATCHDIR_PARAM))
{
*scratchdir = NULL;
goto no_scratch;
}
rc = globus_gram_job_manager_rsl_eval_one_attribute(
request,
GLOBUS_GRAM_PROTOCOL_SCRATCHDIR_PARAM,
&dir);
if(rc != GLOBUS_SUCCESS)
{
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.init_scratchdir.end "
"level=ERROR "
"gramid=%s "
"msg=\"%s\" "
"attribute=%s "
"status=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
"RSL evaluation failed",
GLOBUS_GRAM_PROTOCOL_SCRATCHDIR_PARAM,
-rc,
globus_gram_protocol_error_string(rc));
if (rc == GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_SCRATCH;
}
goto eval_scratchdir_failed;
}
else if (dir == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_SCRATCH;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.init_scratchdir.end "
"level=ERROR "
"gramid=%s "
"msg=\"%s\" "
"attribute=%s "
"status=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
"RSL evaluation didn't yield a string",
GLOBUS_GRAM_PROTOCOL_SCRATCHDIR_PARAM,
-rc,
globus_gram_protocol_error_string(rc));
goto eval_scratchdir_failed;
}
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_TRACE,
"event=gram.init_scratchdir.info "
"level=TRACE "
"gramid=%s "
"dir=\"%s\" "
"\n",
request->job_contact_path,
dir);
if (dir[0] == '/')
{
template = globus_common_create_string(
"%s/gram_scratch_XXXXXX",
dir);
}
else
{
template = globus_common_create_string(
"%s/%s/gram_scratch_XXXXXX",
scratch_dir_base,
dir);
}
if (template == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.init_scratchdir.end "
"level=ERROR "
"gramid=%s "
"msg=\"%s\" "
"status=%d "
"errno=%d "
"reason=\"%s\"\n",
request->job_contact_path,
"Directory template allocation failed",
-rc,
errno,
strerror(errno));
goto template_malloc_failed;
}
for (i = 0, created = 0; i < GLOBUS_GRAM_MKDIR_TRIES && !created; i++)
{
char * scratchname;
scratchname = strdup(template);
if (scratchname == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.init_scratchdir.end "
"level=ERROR "
"gramid=%s "
"msg=\"%s\" "
"status=%d "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
"Directory template allocation failed",
-rc,
errno,
strerror(errno));
goto scratchname_strdup_failed;
}
*scratchdir = mktemp(scratchname);
if (mkdir(*scratchdir, S_IRWXU) != 0)
{
if (errno != EEXIST && errno != EINTR)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_SCRATCH;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.init_scratchdir.end "
"level=ERROR "
"gramid=%s "
"msg=\"%s\" "
"status=%d "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
"Error creating directory",
-rc,
errno,
strerror(errno));
goto fatal_mkdir_err;
}
else
{
free(*scratchdir);
*scratchdir = NULL;
}
}
else
{
created = 1;
}
}
if (*scratchdir == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_SCRATCH;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.init_scratchdir.end "
"level=ERROR "
"gramid=%s "
"msg=\"%s\" "
"status=%d "
"\n",
request->job_contact_path,
"Error creating directory",
-rc);
goto fatal_mkdir_err;
}
skip_mkdir:
rc = globus_symboltable_insert(
&request->symbol_table,
"SCRATCH_DIRECTORY",
*scratchdir);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.init_scratchdir.end "
"level=ERROR "
"gramid=%s "
"msg=\"%s\" "
"status=%d "
"\n",
request->job_contact_path,
"Error inserting scratch directory into RSL symbol table",
-rc);
goto insert_symbol_failed;
}
rc = globus_l_gram_add_environment(
request->rsl,
"SCRATCH_DIRECTORY",
*scratchdir);
if (rc != GLOBUS_SUCCESS)
{
globus_symboltable_remove(
&request->symbol_table,
"SCRATCH_DIRECTORY");
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.init_scratchdir.end "
"level=ERROR "
"gramid=%s "
"msg=\"%s\" "
"status=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
"Error inserting scratch directory into job environment",
-rc,
globus_gram_protocol_error_string(rc));
insert_symbol_failed:
rmdir(*scratchdir);
fatal_mkdir_err:
free(*scratchdir);
*scratchdir = NULL;
}
else
scratchname_strdup_failed:
if (template)
{
free(template);
}
template_malloc_failed:
if (dir)
{
free(dir);
}
eval_scratchdir_failed:
no_scratch:
if (rc == GLOBUS_SUCCESS)
{
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_DEBUG,
"event=gram.init_scratchdir.end "
"level=DEBUG "
"gramid=%s "
"status=%d "
"%s=\"%s\" "
"\n",
request->job_contact_path,
0,
*scratchdir ? "path" : "reason",
*scratchdir ? *scratchdir : "scratch_dir not in RSL");
}
return rc;
}
/* globus_l_gram_init_scratchdir() */
/**
* Remove a directory and all of its contents
*
* @param request
* Request related to this directory.
* @param directory
* Directory to remove.
*/
void
globus_gram_job_manager_destroy_directory(
globus_gram_jobmanager_request_t * request,
const char * directory)
{
int rc;
char * path;
char * new_path;
DIR * dir;
struct dirent * entry;
struct stat st;
globus_list_t * unchecked_dir_list = NULL;
globus_list_t * dir_list = NULL;
int failures = 0;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_TRACE,
"event=gram.directory_destroy.start "
"level=TRACE "
"gramid=%s "
"path=\"%s\" "
"\n",
request->job_contact_path,
directory);
path = strdup(directory);
if (path == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_WARN,
"event=gram.directory_destroy.end "
"level=WARN "
"gramid=%s "
"path=\"%s\" "
"status=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
directory,
-rc,
globus_gram_protocol_error_string(rc));
goto path_strdup_failed;
}
rc = globus_list_insert(&unchecked_dir_list, path);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_WARN,
"event=gram.directory_destroy.end "
"level=WARN "
"gramid=%s "
"path=\"%s\" "
"status=%d "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
directory,
-rc,
errno,
strerror(errno));
free(path);
goto unchecked_dir_insert_failed;
}
while (!globus_list_empty(unchecked_dir_list))
{
/* We walk the directory structure once, removing all non-directory
* entries. We store the directories in a list for a second pass.
*
* Note, we don't do much error handling here. If we fail, we'll leave
* a dropping and note it in the log, but there's not much we can do.
*/
path = globus_list_remove(&unchecked_dir_list, unchecked_dir_list);
if (path == NULL)
{
continue;
}
rc = globus_list_insert(&dir_list, path);
if (rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_WARN,
"event=gram.directory_destroy.info "
"level=WARN "
"gramid=%s "
"path=\"%s\" "
"msg=\"%s\" "
"status=%d "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
path,
"List insert failed",
-rc,
errno,
strerror(errno));
failures++;
continue;
}
dir = opendir(path);
if (dir == NULL)
{
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_WARN,
"event=gram.directory_destroy.info "
"level=WARN "
"gramid=%s "
"path=\"%s\" "
"msg=\"%s\" "
"status=%d "
"errno=%d "
"reason=\"%s\"\n",
request->job_contact_path,
path,
"opendir failed",
-1,
errno,
strerror(errno));
failures++;
continue;
}
while (globus_libc_readdir_r(dir, &entry) == 0 && entry != NULL)
{
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0)
{
free(entry);
continue;
}
new_path = globus_common_create_string(
"%s/%s",
path,
entry->d_name);
if (new_path == NULL)
{
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_WARN,
"event=gram.directory_destroy.info "
"level=WARN "
"gramid=%s "
"path=\"%s\" "
"file=\"%s\" "
"msg=\"%s\" "
"status=%d "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
path,
entry->d_name,
"Malloc failed",
-1,
errno,
strerror(errno));
failures++;
free(entry);
continue;
}
rc = lstat(new_path, &st);
if (rc < 0)
{
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_WARN,
"event=gram.directory_destroy.info "
"level=WARN "
"gramid=%s "
"path=\"%s\" "
"msg=\"%s\" "
"status=%d "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
new_path,
"lstat failed",
-1,
errno,
strerror(errno));
failures++;
free(entry);
continue;
}
if (st.st_mode & S_IFDIR)
{
rc = globus_list_insert(&unchecked_dir_list, new_path);
if (rc != GLOBUS_SUCCESS)
{
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_WARN,
"event=gram.directory_destroy.info "
"level=WARN "
"gramid=%s "
"path=\"%s\" "
"msg=\"%s\" "
"status=%d "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
new_path,
"List insert failed",
-1,
errno,
strerror(errno));
free(new_path);
failures++;
free(entry);
continue;
}
}
else
{
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_TRACE,
"event=gram.directory_destroy.info "
"level=TRACE "
"gramid=%s "
"path=\"%s\" "
"msg=\"About to unlink\" "
"\n",
request->job_contact_path,
new_path);
rc = unlink(new_path);
if (rc < 0)
{
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_WARN,
"event=gram.directory_destroy.info "
"level=WARN "
"gramid=%s "
"path=\"%s\" "
"msg=\"%s\" "
"status=%d "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
new_path,
"Unlink failed",
-1,
errno,
strerror(errno));
failures++;
}
free(new_path);
new_path = NULL;
free(entry);
}
}
closedir(dir);
}
while (!globus_list_empty(dir_list))
{
/* Second pass removes (should be empty) subdirectories */
path = globus_list_remove(&dir_list, dir_list);
rc = rmdir(path);
if (rc < 0)
{
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_WARN,
"event=gram.directory_destroy.info "
"level=WARN "
"gramid=%s "
"path=\"%s\" "
"msg=\"%s\" "
"status=%d "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
path,
"rmdir failed",
-1,
errno,
strerror(errno));
failures++;
}
free(path);
}
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_DEBUG,
"event=gram.directory_destroy.end "
"level=DEBUG "
"gramid=%s "
"path=\"%s\" "
"failures=%d "
"status=%d "
"\n",
request->job_contact_path,
directory,
failures,
failures == 0 ? 0 : -1);
unchecked_dir_insert_failed:
path_strdup_failed:
return;
}
/* globus_gram_job_manager_destroy_directory() */
/**
* Evaluate and validate the job RSL
*
*
* @param request
* Job request to validate
*
* @retval GLOBUS_SUCCESS
* Success.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_RSL_EVALUATION_FAILED
* RSL evaluation failed.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL
* Invalid RSL.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_PARAMETER_NOT_SUPPORTED
* RSL attribute not supported.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_SUBMIT_ATTRIBUTE
* Invalid submit RSL attribute.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_RESTART_ATTRIBUTE;
* Invalid restart RSL attribute.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_STDIO_UPDATE_ATTRIBUTE;
* Invalid stdio_update RSL attribute.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
* Malloc failed.
*/
static
int
globus_l_gram_validate_rsl(
globus_gram_jobmanager_request_t * request)
{
int rc = GLOBUS_SUCCESS;
rc = globus_rsl_eval(request->rsl, &request->symbol_table);
if(rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_EVALUATION_FAILED;
goto rsl_eval_failed;
}
rc = globus_gram_job_manager_validate_rsl(
request,
request->rsl,
GLOBUS_GRAM_VALIDATE_JOB_SUBMIT);
if(rc != GLOBUS_SUCCESS)
{
goto validate_rsl_failed;
}
rc = globus_rsl_eval(request->rsl, &request->symbol_table);
if(rc != GLOBUS_SUCCESS)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_EVALUATION_FAILED;
goto rsl_eval_failed2;
}
rsl_eval_failed2:
validate_rsl_failed:
rsl_eval_failed:
return rc;
}
/* globus_l_gram_validate_rsl() */
/**
* Create remote_io_url file in the job directory
*
* @param request
* Request to log messages for
* @param remote_io_url
* Value to write to the remote_io_url file
* @param remote_io_url_filep
* Pointer to be set to the remote_io_url_file path upon success.
*
* @retval GLOBUS_SUCCESS
* Success.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED
* Malloc failed.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_RSL_REMOTE_IO_URL
* Unable to create remote_io_url file.
*/
static
int
globus_l_gram_remote_io_url_file_create(
globus_gram_jobmanager_request_t * request,
const char * remote_io_url,
const char * job_dir,
char ** remote_io_url_filep)
{
int rc = GLOBUS_SUCCESS;
FILE * fp;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_TRACE,
"event=gram.remote_io_url_file_create.start "
"level=TRACE "
"gramid=%s "
"url=\"%s\" "
"\n",
request->job_contact_path,
remote_io_url ? remote_io_url : "");
if (!remote_io_url)
{
*remote_io_url_filep = NULL;
goto out;
}
*remote_io_url_filep = globus_common_create_string(
"%s/remote_io_file",
job_dir);
if (remote_io_url_filep == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.remote_io_url_file_create.end "
"level=ERROR "
"gramid=%s "
"url=\"%s\" "
"status=%d "
"msg=\"%s\" "
"errno=%d "
"reason=\"%s\"\n",
request->job_contact_path,
remote_io_url,
-rc,
"Error allocating path string",
errno,
strerror(errno));
goto set_remote_io_url_file_failed;
}
fp = fopen(*remote_io_url_filep, "r");
if (fp != NULL)
{
char * tmp;
globus_bool_t skip_it = GLOBUS_FALSE;
rc = fseek(fp, 0, SEEK_END);
if (rc < 0)
{
goto close_readfp;
}
rc = ftell(fp);
tmp = malloc(rc+1);
if (tmp == NULL)
{
goto close_readfp;
}
if (fseek(fp, 0, SEEK_SET) < 0)
{
goto free_tmp;
}
tmp[rc] = 0;
if (fgets(tmp, rc, fp) == NULL)
{
goto free_tmp;
}
/* trim trailing \n */
rc = strlen(tmp)-1;
if (tmp[rc] == '\n')
{
tmp[rc] = 0;
}
if (strcmp(tmp, remote_io_url) == 0)
{
skip_it = GLOBUS_TRUE;
}
free_tmp:
free(tmp);
close_readfp:
fclose(fp);
fp = NULL;
rc = GLOBUS_SUCCESS;
if (skip_it)
{
goto out;
}
}
fp = fopen(*remote_io_url_filep, "w");
if (fp == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_REMOTE_IO_URL;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.remote_io_url_file_create.end "
"level=ERROR "
"gramid=%s "
"url=\"%s\" "
"path=\"%s\" "
"status=%d "
"msg=\"%s\" "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
remote_io_url,
*remote_io_url_filep,
-rc,
"Error opening file",
errno,
strerror(errno));
goto fopen_failed;
}
rc = fprintf(fp, "%s\n", remote_io_url);
if (rc < (1+strlen(remote_io_url)))
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_RSL_REMOTE_IO_URL;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.remote_io_url_file_create.end "
"level=ERROR "
"gramid=%s "
"url=\"%s\" "
"path=\"%s\" "
"status=%d "
"msg=\"%s\" "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
remote_io_url,
*remote_io_url_filep,
-rc,
"Error writing remote_io file",
errno,
strerror(errno));
goto write_failed;
}
rc = GLOBUS_SUCCESS;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_TRACE,
"event=gram.remote_io_url_file_create.end "
"level=TRACE "
"gramid=%s "
"url=\"%s\" "
"path=\"%s\" "
"status=0 "
"\n",
request->job_contact_path,
remote_io_url,
*remote_io_url_filep);
write_failed:
fclose(fp);
if (rc != GLOBUS_SUCCESS)
{
fopen_failed:
free(*remote_io_url_filep);
*remote_io_url_filep = NULL;
}
set_remote_io_url_file_failed:
out:
return rc;
}
/* globus_l_gram_remote_io_url_file_create() */
static
int
globus_l_gram_export_cred(
globus_gram_jobmanager_request_t * request,
gss_cred_id_t cred,
const char * job_directory,
char ** proxy_filename)
{
OM_uint32 major_status, minor_status;
char * filename = NULL;
int file;
gss_buffer_desc buffer;
int rc = GLOBUS_SUCCESS;
char * gt3_failure_message = NULL;
int save_errno;
int written;
if (cred == GSS_C_NO_CREDENTIAL && !request->jm_restart)
{
goto no_cred;
}
if (cred != GSS_C_NO_CREDENTIAL)
{
major_status = gss_export_cred(
&minor_status,
cred,
GSS_C_NO_OID,
0,
&buffer);
if (GSS_ERROR(major_status))
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_OPENING_CACHE_USER_PROXY;
(void) globus_gss_assist_display_status_str(
>3_failure_message,
"Export proxy failed",
major_status,
minor_status,
0);
goto export_cred_failed;
}
}
filename = globus_common_create_string(
"%s/x509_user_proxy",
job_directory);
if (filename == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
goto malloc_filename_failed;
}
if (cred == GSS_C_NO_CREDENTIAL && request->jm_restart)
{
goto jm_restart_done;
}
file = open(
filename,
O_WRONLY|O_CREAT|O_TRUNC,
S_IRUSR|S_IWUSR);
if (file < 0)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_OPENING_CACHE_USER_PROXY;
save_errno = errno;
gt3_failure_message = globus_common_create_string(
"Error opening proxy file for writing: %s: %s (%d)",
filename,
strerror(save_errno),
save_errno);
goto fopen_failed;
}
written = 0;
do
{
rc = write(
file,
((char *) buffer.value) + written,
buffer.length - written);
if (rc < 0)
{
save_errno = errno;
rc = GLOBUS_GRAM_PROTOCOL_ERROR_OPENING_CACHE_USER_PROXY;
gt3_failure_message = globus_common_create_string(
"Error writing proxy file: %s: %s (%d)",
filename,
strerror(save_errno),
save_errno);
goto fwrite_failed;
}
else if (rc == 0)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_OPENING_CACHE_USER_PROXY;
gt3_failure_message = globus_common_create_string(
"Error writing proxy file: %s: %s",
filename,
"short write");
goto fwrite_failed;
}
else
{
written += rc;
}
}
while (written < buffer.length);
rc = GLOBUS_SUCCESS;
fwrite_failed:
rc = close(file);
if (rc != 0)
{
save_errno = errno;
rc = GLOBUS_GRAM_PROTOCOL_ERROR_OPENING_CACHE_USER_PROXY;
gt3_failure_message = globus_common_create_string(
"Error writing proxy file: %s: %s (%d)",
filename,
strerror(save_errno),
save_errno);
}
fopen_failed:
if (rc != GLOBUS_SUCCESS)
{
free(filename);
filename = NULL;
}
malloc_filename_failed:
gss_release_buffer(&minor_status, &buffer);
jm_restart_done:
export_cred_failed:
if (request->gt3_failure_message == NULL)
{
request->gt3_failure_message = gt3_failure_message;
}
else
{
free(gt3_failure_message);
}
no_cred:
*proxy_filename = filename;
return rc;
}
/* globus_l_gram_export_cred() */
/**
* Create job directory
*
* The job directory is used internally by the Job Manager to store various
* pieces of job-specific data: stdout, stderr, proxy, and job scripts.
*
* @param request
* Request to process
* @param job_directory
* Pointer to be set to the new value of the job directory.
*
* @retval GLOBUS_SUCCESS
* Success.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED
* Malloc failed.
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_ARG_FILE_CREATION_FAILED
* Error creating directory.
*/
static
int
globus_l_gram_make_job_dir(
globus_gram_jobmanager_request_t * request,
char ** job_directory)
{
char * out_file = NULL;
char * tmp;
int rc;
struct stat statbuf;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_TRACE,
"event=gram.make_job_dir.start "
"level=TRACE "
"gramid=%s "
"\n",
request->job_contact_path);
out_file = globus_common_create_string(
"%s/.globus/job/%s/%s",
request->config->home,
request->config->short_hostname,
request->uniq_id);
if (out_file == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_MALLOC_FAILED;
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.make_job_dir.end "
"level=ERROR "
"gramid=%s "
"status=%d "
"msg=\"%s\" "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
-rc,
"Error allocating path string",
errno,
strerror(errno));
goto out;
}
if ((rc = stat(out_file, &statbuf)) < 0)
{
tmp = out_file;
while (tmp != NULL)
{
tmp = strchr(tmp+1, '/');
if (tmp != out_file)
{
if (tmp != NULL)
{
*tmp = '\0';
}
if ((rc = stat(out_file, &statbuf)) < 0)
{
/* Path component does not exist, try to make it */
errno = 0;
rc = mkdir(out_file, S_IRWXU);
if (rc == -1 && errno != EEXIST)
{
/* Error creating directory */
if (request->gt3_failure_message == NULL)
{
request->gt3_failure_message =
globus_common_create_string(
"mkdir failed: %s: %s",
out_file,
strerror(errno));
}
}
}
if ((rc = stat(out_file, &statbuf)) < 0)
{
int save_errno = errno;
rc = GLOBUS_GRAM_PROTOCOL_ERROR_ARG_FILE_CREATION_FAILED;
if (request->gt3_failure_message == NULL)
{
request->gt3_failure_message =
globus_common_create_string(
"stat failed: %s: %s",
out_file,
strerror(save_errno));
}
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_ERROR,
"event=gram.make_job_dir.end "
"level=ERROR "
"gramid=%s "
"status=%d "
"path=%s "
"msg=\"%s\" "
"errno=%d "
"reason=\"%s\" "
"\n",
request->job_contact_path,
-rc,
out_file,
"Error creating directory",
save_errno,
strerror(save_errno));
goto error_exit;
}
if (tmp != NULL)
{
*tmp = '/';
}
}
}
}
globus_gram_job_manager_request_log(
request,
GLOBUS_GRAM_JOB_MANAGER_LOG_TRACE,
"event=gram.make_job_dir.end "
"level=TRACE "
"gramid=%s "
"status=0 "
"path=%s "
"\n",
request->job_contact_path,
out_file);
error_exit:
if (rc != GLOBUS_SUCCESS)
{
free(out_file);
out_file = NULL;
}
out:
*job_directory = out_file;
return rc;
}
/* globus_l_gram_make_job_dir() */
/**
* Check that all stdout_position or stderr_values are 0
*
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_STDOUT_POSITION
* Invalid stdout_position
* @retval GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_STDERR_POSITION
* Invalid stderr_position
*/
static
int
globus_l_gram_check_position(
globus_gram_jobmanager_request_t * request,
globus_rsl_t * position_rsl)
{
int rc = GLOBUS_SUCCESS;
globus_rsl_value_t * value_seq;
globus_list_t * values;
const char * value_string;
long longval;
char charval;
value_seq = globus_rsl_relation_get_value_sequence(position_rsl);
if (value_seq == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL;
goto non_sequence;
}
values = globus_rsl_value_sequence_get_value_list(value_seq);
while (!globus_list_empty(values))
{
value_string = globus_rsl_value_literal_get_string(
globus_list_first(values));
values = globus_list_rest(values);
if (value_string == NULL)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL;
goto non_literal;
}
errno = 0;
if (scanf("%ld%c", &longval, &charval) != 1)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_BAD_RSL;
goto non_zero;
}
}
non_zero:
non_literal:
non_sequence:
if (rc != GLOBUS_SUCCESS)
{
if (strcmp(
globus_rsl_relation_get_attribute(position_rsl),
"stdoutposition") == 0)
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_STDOUT_POSITION;
}
else
{
rc = GLOBUS_GRAM_PROTOCOL_ERROR_INVALID_STDERR_POSITION;
}
}
return rc;
}
/* globus_l_gram_check_position() */
static
void
globus_l_gram_event_destroy(void *datum)
{
globus_scheduler_event_destroy(datum);
}
/* globus_l_gram_event_destroy() */
#endif /* GLOBUS_DONT_DOCUMENT_INTERNAL */
|