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
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <locale.h>
#if defined(DARWIN) || defined(INTERIX)
# include <termios.h>
# include <sys/ioctl.h>
# include <grp.h>
#elif defined(HP1164) || defined(HP11)
# include <termios.h>
# include <stropts.h>
#elif defined(SOLARIS64) || defined(SOLARIS86) || defined(SOLARISAMD64)
# include <stropts.h>
# include <termio.h>
#elif defined(IRIX65)
# include <sys/ioctl.h>
# include <stropts.h>
# include <termio.h>
#elif defined(FREEBSD) || defined(NETBSD)
# include <termios.h>
#else
# include <termio.h>
#endif
#include "comm/commlib.h"
#include "comm/lists/cl_util.h"
#include "uti/sge_lock.h"
#include "uti/sge_rmon.h"
#include "uti/sge_hostname.h"
#include "uti/sge_log.h"
#include "uti/sge_stdlib.h"
#include "uti/sge_string.h"
#include "uti/sge_unistd.h"
#include "uti/sge_env.h"
#include "uti/sge_prog.h"
#include "uti/setup_path.h"
#include "uti/sge_bootstrap.h"
#include "uti/sge_uidgid.h"
#include "uti/sge_profiling.h"
#include "uti/msg_utilib.h"
#include "uti/sge_language.h"
#include "uti/sge_spool.h"
#include "uti/sge_time.h"
#include "uti/sge_csp_path.h"
#include "uti/sge_os.h"
#include "sgeobj/sge_answer.h"
#include "gdi/qm_name.h"
#include "gdi/msg_gdilib.h"
#include "sgeobj/sge_feature.h"
#include "sgeobj/sge_conf.h"
#include "sgeobj/sge_object.h"
#include "sge.h"
#include "msg_common.h"
/*
** need this for lInit(nmv)
*/
extern lNameSpace nmv[];
/* pipe for sge_daemonize_prepare() and sge_daemonize_finalize() */
static int fd_pipe[2];
#if 1
/* TODO: throw this out asap */
#include "gdi/sge_gdi.h"
void gdi_once_init(void);
void feature_mt_init(void);
void sc_mt_init(void);
#endif
#include "gdi/sge_gdi_ctx.h"
#include "gdi/sge_gdi2.h"
#include "gdi/sge_gdi_packet_internal.h"
typedef struct {
sge_env_state_class_t* sge_env_state_obj;
sge_prog_state_class_t* sge_prog_state_obj;
sge_path_state_class_t* sge_path_state_obj;
sge_bootstrap_state_class_t* sge_bootstrap_state_obj;
sge_csp_path_class_t* sge_csp_path_obj;
char* component_name;
char* thread_name;
char* master;
char* component_username;
char* username;
char* groupname;
uid_t uid;
gid_t gid;
char *ssl_private_key;
char *ssl_certificate;
lList *alp;
int last_commlib_error;
sge_error_class_t *eh;
bool is_qmaster_internal_client;
bool is_setup;
u_long32 last_qmaster_file_read;
} sge_gdi_ctx_t;
static pthread_key_t gdi_state_key;
static pthread_once_t gdi_once_control = PTHREAD_ONCE_INIT;
typedef struct {
/* gdi request base */
u_long32 request_id; /* incremented with each GDI request to have a unique request ID
it is ensured that the request ID is also contained in answer */
} gdi_state_t;
static void gdi_state_destroy(void* state) {
sge_free(&state);
}
void gdi_once_init(void) {
pthread_key_create(&gdi_state_key, &gdi_state_destroy);
}
static void gdi_state_init(gdi_state_t* state) {
state->request_id = 0;
}
/****** gdi/ctx/gdi_mt_init() ************************************************
* NAME
* gdi_mt_init() -- Initialize GDI state for multi threading use.
*
* SYNOPSIS
* void gdi_mt_init(void)
*
* FUNCTION
* Set up GDI. This function must be called at least once before any of the
* GDI functions is used. This function is idempotent, i.e. it is safe to
* call it multiple times.
*
* Thread local storage for the GDI state information is reserved.
*
* INPUTS
* void - NONE
*
* RESULT
* void - NONE
*
* NOTES
* MT-NOTE: gdi_mt_init() is MT safe
*
*******************************************************************************/
void gdi_mt_init(void)
{
pthread_once(&gdi_once_control, gdi_once_init);
}
/****** gdi/ctx/gdi_state_get_????() ************************************
* NAME
* gdi_state_get_????() - read access to gdilib global variables
*
* FUNCTION
* Provides access to either global variable or per thread global
* variable.
*
******************************************************************************/
u_long32 gdi_state_get_next_request_id(void)
{
GET_SPECIFIC(gdi_state_t, gdi_state,
gdi_state_init, gdi_state_key, "gdi_state_get_next_request_id");
gdi_state->request_id++;
return gdi_state->request_id;
}
typedef struct {
sge_gdi_ctx_class_t* ctx;
} sge_gdi_ctx_thread_local_t;
static pthread_once_t sge_gdi_ctx_once = PTHREAD_ONCE_INIT;
static pthread_key_t sge_gdi_ctx_key;
static void sge_gdi_thread_local_ctx_once_init(void);
static void sge_gdi_thread_local_ctx_destroy(void* theState);
static void sge_gdi_thread_local_ctx_init(sge_gdi_ctx_thread_local_t* theState);
static void sge_gdi_thread_local_ctx_once_init(void)
{
pthread_key_create(&sge_gdi_ctx_key, sge_gdi_thread_local_ctx_destroy);
}
static void sge_gdi_thread_local_ctx_destroy(void* theState) {
sge_gdi_ctx_thread_local_t *tl = (sge_gdi_ctx_thread_local_t*)theState;
tl->ctx = NULL;
sge_free(&theState);
}
static void sge_gdi_thread_local_ctx_init(sge_gdi_ctx_thread_local_t* theState)
{
memset(theState, 0, sizeof(sge_gdi_ctx_thread_local_t));
}
sge_gdi_ctx_class_t* sge_gdi_get_thread_local_ctx() {
pthread_once(&sge_gdi_ctx_once, sge_gdi_thread_local_ctx_once_init);
{
GET_SPECIFIC(sge_gdi_ctx_thread_local_t, tl, sge_gdi_thread_local_ctx_init, sge_gdi_ctx_key,
"sge_gdi_get_thread_local_ctx");
return tl->ctx;
}
}
void sge_gdi_set_thread_local_ctx(sge_gdi_ctx_class_t* ctx) {
DENTER(TOP_LAYER, "sge_gdi_set_thread_local_ctx");
pthread_once(&sge_gdi_ctx_once, sge_gdi_thread_local_ctx_once_init);
{
GET_SPECIFIC(sge_gdi_ctx_thread_local_t, tl, sge_gdi_thread_local_ctx_init, sge_gdi_ctx_key,
"set_thread_local_ctx");
tl->ctx = ctx;
if (ctx != NULL) {
sge_bootstrap_state_set_thread_local(ctx->get_sge_bootstrap_state(ctx));
log_state_set_log_context(ctx);
} else {
sge_bootstrap_state_set_thread_local(NULL);
log_state_set_log_context(NULL);
}
}
DRETURN_VOID;
}
static bool
sge_gdi_ctx_setup(sge_gdi_ctx_class_t *thiz, int prog_number, const char* component_name,
int thread_number, const char *thread_name, const char* username,
const char *groupname, const char *sge_root, const char *sge_cell,
int sge_qmaster_port, int sge_execd_port, bool from_services,
bool is_qmaster_internal_client);
static void sge_gdi_ctx_destroy(void *theState);
static void sge_gdi_ctx_set_is_setup(sge_gdi_ctx_class_t *thiz, bool is_setup);
static bool sge_gdi_ctx_is_setup(sge_gdi_ctx_class_t *thiz);
static void sge_gdi_ctx_class_get_errors(sge_gdi_ctx_class_t *thiz, lList **alpp, bool clear_errors);
static void sge_gdi_ctx_class_error(sge_gdi_ctx_class_t *thiz, int error_type, int error_quality, const char* fmt, ...);
static sge_env_state_class_t* get_sge_env_state(sge_gdi_ctx_class_t *thiz);
static sge_prog_state_class_t* get_sge_prog_state(sge_gdi_ctx_class_t *thiz);
static sge_path_state_class_t* get_sge_path_state(sge_gdi_ctx_class_t *thiz);
static sge_csp_path_class_t* get_sge_csp_path(sge_gdi_ctx_class_t *thiz);
static sge_bootstrap_state_class_t* get_sge_bootstrap_state(sge_gdi_ctx_class_t *thiz);
static int reresolve_qualified_hostname(sge_gdi_ctx_class_t *thiz);
static cl_com_handle_t* get_com_handle(sge_gdi_ctx_class_t *thiz);
static const char* get_component_name(sge_gdi_ctx_class_t *thiz);
static const char* get_thread_name(sge_gdi_ctx_class_t *thiz);
static const char* get_progname(sge_gdi_ctx_class_t *thiz);
static const char* get_ca_local_root(sge_gdi_ctx_class_t *thiz);
static const char* get_ca_root(sge_gdi_ctx_class_t *thiz);
static u_long32 get_who(sge_gdi_ctx_class_t *thiz);
static bool is_daemonized(sge_gdi_ctx_class_t *thiz);
static void set_daemonized(sge_gdi_ctx_class_t *thiz, bool daemonized);
static bool get_job_spooling(sge_gdi_ctx_class_t *thiz);
static void set_job_spooling(sge_gdi_ctx_class_t *thiz, bool job_spooling);
static u_long32 get_listener_thread_count(sge_gdi_ctx_class_t *thiz);
static u_long32 get_worker_thread_count(sge_gdi_ctx_class_t *thiz);
static u_long32 get_scheduler_thread_count(sge_gdi_ctx_class_t *thiz);
static u_long32 get_jvm_thread_count(sge_gdi_ctx_class_t *thiz);
static const char* get_master(sge_gdi_ctx_class_t *thiz, bool reread);
static u_long32 get_sge_qmaster_port(sge_gdi_ctx_class_t *thiz);
static u_long32 get_sge_execd_port(sge_gdi_ctx_class_t *thiz);
static const char* get_spooling_method(sge_gdi_ctx_class_t *thiz);
static const char* get_spooling_lib(sge_gdi_ctx_class_t *thiz);
static const char* get_spooling_params(sge_gdi_ctx_class_t *thiz);
static const char* get_username(sge_gdi_ctx_class_t *thiz);
static const char* get_cell_root(sge_gdi_ctx_class_t *thiz);
static const char* get_sge_root(sge_gdi_ctx_class_t *thiz);
static const char* get_groupname(sge_gdi_ctx_class_t *thiz);
static uid_t ctx_get_uid(sge_gdi_ctx_class_t *thiz);
static gid_t ctx_get_gid(sge_gdi_ctx_class_t *thiz);
static const char* get_qualified_hostname(sge_gdi_ctx_class_t *thiz);
static const char* get_unqualified_hostname(sge_gdi_ctx_class_t *thiz);
static const char* get_default_cell(sge_gdi_ctx_class_t *thiz);
static const char* get_admin_user(sge_gdi_ctx_class_t *thiz);
static const char* get_binary_path(sge_gdi_ctx_class_t *thiz);
static const char* get_qmaster_spool_dir(sge_gdi_ctx_class_t *thiz);
static const char* get_bootstrap_file(sge_gdi_ctx_class_t *thiz);
static const char* get_act_qmaster_file(sge_gdi_ctx_class_t *thiz);
static const char* get_acct_file(sge_gdi_ctx_class_t *thiz);
static const char* get_reporting_file(sge_gdi_ctx_class_t *thiz);
static const char* get_shadow_master_file(sge_gdi_ctx_class_t *thiz);
static sge_exit_func_t get_exit_func(sge_gdi_ctx_class_t *thiz);
static void set_exit_func(sge_gdi_ctx_class_t *thiz, sge_exit_func_t exfunc);
static void set_private_key(sge_gdi_ctx_class_t *thiz, const char *pkey);
static void set_certificate(sge_gdi_ctx_class_t *thiz, const char *cert);
static const char* get_private_key(sge_gdi_ctx_class_t *thiz);
static const char* get_certificate(sge_gdi_ctx_class_t *thiz);
static int ctx_get_last_commlib_error(sge_gdi_ctx_class_t *thiz);
static void ctx_set_last_commlib_error(sge_gdi_ctx_class_t *thiz, int cl_error);
static bool ctx_is_qmaster_internal_client(sge_gdi_ctx_class_t *thiz);
static int sge_gdi_ctx_class_prepare_enroll(sge_gdi_ctx_class_t *thiz);
static int sge_gdi_ctx_class_connect(sge_gdi_ctx_class_t *thiz);
static int sge_gdi_ctx_class_is_alive(sge_gdi_ctx_class_t *thiz);
static lList* sge_gdi_ctx_class_gdi_tsm(sge_gdi_ctx_class_t *thiz, const char *schedd_name, const char *cell);
static lList* sge_gdi_ctx_class_gdi_kill(sge_gdi_ctx_class_t *thiz, lList *id_list, const char *cell,
u_long32 option_flags, u_long32 action_flag);
static bool sge_gdi_ctx_class_gdi_get_mapping_name(sge_gdi_ctx_class_t *thiz, const char *requestedHost, char *buf, int buflen);
static bool sge_gdi_ctx_class_gdi_check_permission(sge_gdi_ctx_class_t *thiz, lList **alpp, int option);
static int sge_gdi_ctx_log_flush_func(cl_raw_list_t* list_p);
static void sge_gdi_ctx_class_dprintf(sge_gdi_ctx_class_t *ctx);
sge_gdi_ctx_class_t *
sge_gdi_ctx_class_create(int prog_number, const char *component_name,
int thread_number, const char *thread_name,
const char *username, const char *groupname,
const char *sge_root, const char *sge_cell,
int sge_qmaster_port, int sge_execd_port,
bool from_services, bool is_qmaster_internal_client,
lList **alpp)
{
sge_gdi_ctx_class_t *ret = (sge_gdi_ctx_class_t *)sge_malloc(sizeof(sge_gdi_ctx_class_t));
sge_gdi_ctx_t *gdi_ctx = NULL;
DENTER(TOP_LAYER, "sge_gdi_ctx_class_create");
if (!ret) {
answer_list_add_sprintf(alpp, STATUS_EMALLOC,
ANSWER_QUALITY_ERROR, "%s", MSG_MEMORY_MALLOCFAILED);
DRETURN(NULL);
}
if (is_qmaster_internal_client) {
ret->sge_gdi_packet_execute = sge_gdi_packet_execute_internal;
ret->sge_gdi_packet_wait_for_result = sge_gdi_packet_wait_for_result_internal;
} else {
ret->sge_gdi_packet_execute = sge_gdi_packet_execute_external;
ret->sge_gdi_packet_wait_for_result = sge_gdi_packet_wait_for_result_external;
}
ret->gdi = sge_gdi2;
ret->gdi_multi = sge_gdi2_multi;
ret->gdi_wait = sge_gdi2_wait;
ret->get_errors = sge_gdi_ctx_class_get_errors;
ret->prepare_enroll = sge_gdi_ctx_class_prepare_enroll;
ret->connect = sge_gdi_ctx_class_connect;
ret->is_alive = sge_gdi_ctx_class_is_alive;
ret->tsm = sge_gdi_ctx_class_gdi_tsm;
ret->kill = sge_gdi_ctx_class_gdi_kill;
ret->gdi_check_permission = sge_gdi_ctx_class_gdi_check_permission;
ret->gdi_get_mapping_name = sge_gdi_ctx_class_gdi_get_mapping_name;
ret->get_sge_env_state = get_sge_env_state;
ret->get_sge_prog_state = get_sge_prog_state;
ret->get_sge_path_state = get_sge_path_state;
ret->get_sge_bootstrap_state = get_sge_bootstrap_state;
ret->reresolve_qualified_hostname = reresolve_qualified_hostname;
ret->get_component_name = get_component_name;
ret->get_thread_name = get_thread_name;
ret->get_progname = get_progname;
ret->get_who = get_who;
ret->is_daemonized = is_daemonized;
ret->set_daemonized = set_daemonized;
ret->get_job_spooling = get_job_spooling;
ret->set_job_spooling = set_job_spooling;
ret->get_listener_thread_count = get_listener_thread_count;
ret->get_worker_thread_count = get_worker_thread_count;
ret->get_scheduler_thread_count = get_scheduler_thread_count;
ret->get_jvm_thread_count = get_jvm_thread_count;
ret->get_qualified_hostname = get_qualified_hostname;
ret->get_unqualified_hostname = get_unqualified_hostname;
ret->get_master = get_master;
ret->get_sge_qmaster_port = get_sge_qmaster_port;
ret->get_sge_execd_port = get_sge_execd_port;
ret->get_username = get_username;
ret->get_spooling_method = get_spooling_method;
ret->get_spooling_lib = get_spooling_lib;
ret->get_spooling_params = get_spooling_params;
ret->get_admin_user = get_admin_user;
ret->get_binary_path = get_binary_path;
ret->get_qmaster_spool_dir = get_qmaster_spool_dir;
ret->get_bootstrap_file = get_bootstrap_file;
ret->get_act_qmaster_file = get_act_qmaster_file;
ret->get_acct_file = get_acct_file;
ret->get_reporting_file = get_reporting_file;
ret->get_shadow_master_file = get_shadow_master_file;
ret->get_default_cell = get_default_cell;
ret->get_cell_root = get_cell_root;
ret->get_sge_root = get_sge_root;
ret->get_groupname = get_groupname;
ret->get_uid = ctx_get_uid;
ret->get_gid = ctx_get_gid;
ret->get_com_handle = get_com_handle;
ret->set_exit_func = set_exit_func;
ret->get_exit_func = get_exit_func;
ret->set_private_key = set_private_key;
ret->set_certificate = set_certificate;
ret->get_private_key = get_private_key;
ret->get_certificate = get_certificate;
ret->get_ca_root = get_ca_root;
ret->get_ca_local_root = get_ca_local_root;
ret->is_qmaster_internal_client = ctx_is_qmaster_internal_client;
ret->dprintf = sge_gdi_ctx_class_dprintf;
ret->sge_gdi_ctx_handle = (sge_gdi_ctx_t*)sge_malloc(sizeof(sge_gdi_ctx_t));
memset(ret->sge_gdi_ctx_handle, 0, sizeof(sge_gdi_ctx_t));
if (!ret->sge_gdi_ctx_handle) {
answer_list_add_sprintf(alpp, STATUS_EMALLOC,
ANSWER_QUALITY_ERROR, "%s", MSG_MEMORY_MALLOCFAILED);
sge_gdi_ctx_class_destroy(&ret);
DRETURN(NULL);
}
/*
** create error handler of context
*/
gdi_ctx = (sge_gdi_ctx_t*)ret->sge_gdi_ctx_handle;
gdi_ctx->eh = sge_error_class_create();
if (!gdi_ctx->eh) {
answer_list_add_sprintf(alpp, STATUS_EMALLOC,
ANSWER_QUALITY_ERROR, "%s", MSG_MEMORY_MALLOCFAILED);
DRETURN(NULL);
}
if (!sge_gdi_ctx_setup(ret, prog_number, component_name, thread_number, thread_name,
username, groupname, sge_root, sge_cell, sge_qmaster_port,
sge_execd_port, from_services, is_qmaster_internal_client)) {
sge_gdi_ctx_class_get_errors(ret, alpp, true);
sge_gdi_ctx_class_destroy(&ret);
DRETURN(NULL);
}
/*
** set default exit func, maybe overwritten
*/
ret->set_exit_func(ret, gdi2_default_exit_func);
DRETURN(ret);
}
void sge_gdi_ctx_class_destroy(sge_gdi_ctx_class_t **pst)
{
DENTER(TOP_LAYER, "sge_gdi_ctx_class_destroy");
if (!pst || !*pst) {
DRETURN_VOID;
}
/* free internal context structure */
sge_gdi_ctx_destroy((*pst)->sge_gdi_ctx_handle);
sge_free(pst);
DRETURN_VOID;
}
static void sge_gdi_ctx_class_get_errors(sge_gdi_ctx_class_t *thiz, lList **alpp, bool clear_errors)
{
sge_gdi_ctx_t *gdi_ctx = NULL;
DENTER(TOP_LAYER, "sge_gdi_ctx_class_get_errors");
if (!thiz || !thiz->sge_gdi_ctx_handle) {
DRETURN_VOID;
}
gdi_ctx = (sge_gdi_ctx_t*)thiz->sge_gdi_ctx_handle;
sge_error_to_answer_list(gdi_ctx->eh, alpp, clear_errors);
DRETURN_VOID;
}
static void sge_gdi_ctx_class_error(sge_gdi_ctx_class_t *thiz, int error_type, int error_quality, const char* fmt, ...)
{
sge_gdi_ctx_t *gdi_ctx = NULL;
DENTER(TOP_LAYER, "sge_gdi_ctx_class_error");
if (thiz != NULL && thiz->sge_gdi_ctx_handle != NULL) {
gdi_ctx = (sge_gdi_ctx_t*)thiz->sge_gdi_ctx_handle;
if (gdi_ctx->eh && fmt != NULL) {
va_list arg_list;
va_start(arg_list, fmt);
gdi_ctx->eh->verror(gdi_ctx->eh, error_type, error_quality, fmt, arg_list);
va_end(arg_list);
}
}
DRETURN_VOID;
}
static void sge_gdi_ctx_set_is_setup(sge_gdi_ctx_class_t *thiz, bool is_setup)
{
sge_gdi_ctx_t *gdi_ctx = NULL;
DENTER(TOP_LAYER, "sge_gdi_ctx_set_is_setup");
if (!thiz || !thiz->sge_gdi_ctx_handle) {
DRETURN_VOID;
}
gdi_ctx = (sge_gdi_ctx_t*)thiz->sge_gdi_ctx_handle;
gdi_ctx->is_setup = is_setup;
DRETURN_VOID;
}
static bool sge_gdi_ctx_is_setup(sge_gdi_ctx_class_t *thiz)
{
sge_gdi_ctx_t *gdi_ctx = NULL;
DENTER(TOP_LAYER, "sge_gdi_ctx_is_setup");
if (!thiz || !thiz->sge_gdi_ctx_handle) {
DRETURN(false);
}
gdi_ctx = (sge_gdi_ctx_t*)thiz->sge_gdi_ctx_handle;
DRETURN(gdi_ctx->is_setup);
}
static bool
sge_gdi_ctx_setup(sge_gdi_ctx_class_t *thiz, int prog_number, const char* component_name,
int thread_number, const char *thread_name, const char* username,
const char *groupname, const char *sge_root, const char *sge_cell,
int sge_qmaster_port, int sge_execd_port, bool from_services,
bool qmaster_internal_client)
{
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *)thiz->sge_gdi_ctx_handle;
sge_error_class_t *eh = es->eh;
DENTER(TOP_LAYER, "sge_gdi_ctx_setup");
/*
* Call all functions which have to be called once for each process.
* Those functions will then be called when the first thread of a process
* creates its context.
*/
prof_mt_init();
feature_mt_init();
gdi_mt_init();
sc_mt_init();
obj_mt_init();
bootstrap_mt_init();
sc_mt_init();
uidgid_mt_init();
path_mt_init();
/* TODO: shall we do that here ? */
lInit(nmv);
es->is_qmaster_internal_client = qmaster_internal_client;
es->sge_env_state_obj = sge_env_state_class_create(sge_root, sge_cell, sge_qmaster_port, sge_execd_port, from_services, qmaster_internal_client, eh);
if (!es->sge_env_state_obj) {
DRETURN(false);
}
es->sge_prog_state_obj = sge_prog_state_class_create(es->sge_env_state_obj, prog_number, eh);
if (!es->sge_prog_state_obj) {
DRETURN(false);
}
es->sge_path_state_obj = sge_path_state_class_create(es->sge_env_state_obj, eh);
if (!es->sge_path_state_obj) {
DRETURN(false);
}
es->sge_bootstrap_state_obj = sge_bootstrap_state_class_create(es->sge_path_state_obj, eh);
if (!es->sge_bootstrap_state_obj) {
DRETURN(false);
}
if (feature_initialize_from_string(es->sge_bootstrap_state_obj->get_security_mode(es->sge_bootstrap_state_obj))) {
DRETURN(false);
}
es->sge_csp_path_obj = sge_csp_path_class_create(es->sge_env_state_obj, es->sge_prog_state_obj, eh);
if (!es->sge_csp_path_obj) {
DRETURN(false);
}
if (component_name == NULL) {
es->component_name = strdup(prognames[prog_number]);
} else {
es->component_name = strdup(component_name);
}
if (thread_name == NULL) {
es->thread_name = strdup(prognames[prog_number]);
} else {
es->thread_name = strdup(thread_name);
}
/* set uid and gid */
{
struct passwd *pwd;
struct passwd pw_struct;
char *buffer;
int size;
size = get_pw_buffer_size();
buffer = sge_malloc(size);
pwd = sge_getpwnam_r(username, &pw_struct, buffer, size);
if (!pwd) {
eh->error(eh, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, "sge_getpwnam_r failed for username %s", username);
sge_free(&buffer);
DRETURN(false);
}
es->uid = pwd->pw_uid;
if (groupname != NULL) {
gid_t gid;
if (sge_group2gid(groupname, &gid, MAX_NIS_RETRIES) == 1) {
eh->error(eh, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, "sge_group2gid failed for groupname %s", groupname);
sge_free(&buffer);
DRETURN(false);
}
es->gid = gid;
} else {
es->gid = pwd->pw_gid;
}
sge_free(&buffer);
}
es->username = strdup(username);
#if defined( INTERIX )
/* Strip Windows domain name from user name */
{
char *plus_sign;
plus_sign = strstr(es->username, "+");
if (plus_sign!=NULL) {
char *to = es->username;
plus_sign++;
/* Can't use strcpy with overlapping strings. */
while (*plus_sign)
*to++ = *plus_sign++;
*to = '\0';
}
}
#endif
/*
** groupname
*/
if (groupname != NULL) {
es->groupname = strdup(groupname);
} else {
if (_sge_gid2group(es->gid, &(es->gid), &(es->groupname), MAX_NIS_RETRIES)) {
eh->error(eh, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, MSG_GDI_GETGRGIDXFAILEDERRORX_U, sge_u32c(es->gid));
DRETURN(false);
}
}
/*
** set the component_username and check if login is needed
*/
{
struct passwd *pwd = NULL;
char *buffer;
int size;
struct passwd pwentry;
size = get_pw_buffer_size();
buffer = sge_malloc(size);
if (getpwuid_r((uid_t)getuid(), &pwentry, buffer, size, &pwd) == 0 &&
pwd) {
es->component_username = sge_strdup(es->component_username, pwd->pw_name);
sge_free(&buffer);
} else {
eh->error(eh, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, "getpwuid_r failed");
sge_free(&buffer);
DRETURN(false);
}
#if 0
/*
** TODO: Login to system somehow and send something like a token with request
** similar like the secret key in CSP mode
*/
DPRINTF(("es->username: '%s', es->component_username: '%s'\n", es->username, es->component_username));
if (strcmp(es->username, es->component_username) != 0) {
#if 1
eh->error(eh, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, "!!!! Alert login needed !!!!!");
DRETURN(false);
#else
eh->error(eh, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, "!!!! First time login !!!!!");
/* sge_authenticate(es->username, callback_handler); */
#endif
}
#endif
}
DRETURN(true);
}
sge_gdi_ctx_class_t *
sge_gdi_ctx_class_create_from_bootstrap(int prog_number, const char* component_name,
int thread_number, const char *thread_name,
const char* url, const char* username, lList **alpp)
{
char sge_root[BUFSIZ];
char sge_cell[BUFSIZ];
char sge_qmaster_port[BUFSIZ];
char *token = NULL;
char sge_url[BUFSIZ];
struct saved_vars_s *url_ctx = NULL;
int sge_qmaster_p = 0;
int sge_execd_p = 0;
bool is_qmaster_internal_client = false;
bool from_services = false;
sge_gdi_ctx_class_t * ret = NULL;
DENTER(TOP_LAYER, "sge_gdi_ctx_class_create_from_bootstrap");
/* determine the connection type: local/remote */
if (!strncmp(url, "internal://", (sizeof("internal://")-1))) {
DPRINTF(("**** Using internal context for %s ****\n", component_name));
is_qmaster_internal_client = true;
}
/* parse the url */
DPRINTF(("url = "SFN"\n", url));
if (is_qmaster_internal_client) {
sscanf(url, "internal://%512s", sge_url);
} else {
sscanf(url, "bootstrap://%512s", sge_url);
}
DPRINTF(("sge_url = "SFN"\n", sge_url));
/* search for sge_root */
token = sge_strtok_r(sge_url, "@", &url_ctx);
if (token == NULL ) {
answer_list_add_sprintf(alpp, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, "invalid url, sge_root not found");
sge_free_saved_vars(url_ctx);
DRETURN(NULL);
}
sge_strlcpy(sge_root, token, BUFSIZ);
/* search for sge_cell */
token = sge_strtok_r(NULL, ":", &url_ctx);
if (token == NULL ) {
answer_list_add_sprintf(alpp, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, "invalid url, sge_cell not found");
sge_free_saved_vars(url_ctx);
DRETURN(NULL);
}
sge_strlcpy(sge_cell, token, BUFSIZ);
/* get the qmaster port */
token = sge_strtok_r(NULL, NULL, &url_ctx);
if (token == NULL ) {
answer_list_add_sprintf(alpp, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, "invalid url, qmaster_port not found");
sge_free_saved_vars(url_ctx);
DRETURN(NULL);
}
sge_strlcpy(sge_qmaster_port, token, BUFSIZ);
if (is_qmaster_internal_client) {
sge_qmaster_p = sge_get_qmaster_port(&from_services);
sge_execd_p = sge_get_execd_port();
DPRINTF(("**** from_services %s ****\n", from_services ? "true" : "false"));
} else {
sge_qmaster_p = atoi(sge_qmaster_port);
}
if (sge_qmaster_p <= 0 ) {
answer_list_add_sprintf(alpp, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, "invalid url, invalid sge_qmaster_port port %s", sge_qmaster_port);
sge_free_saved_vars(url_ctx);
DRETURN(NULL);
}
sge_free_saved_vars(url_ctx);
/*
* TODO we need a way to define the execd port, from_services is always false (certs from keystore) for bootstrap:* mode
* for internal:* mode the master port and the execd port can be fetched from env as for other master threads here also the
* from_services flag can be set
*/
ret = sge_gdi_ctx_class_create(prog_number, component_name, thread_number, thread_name,
username, NULL, sge_root, sge_cell, sge_qmaster_p, sge_execd_p,
from_services, is_qmaster_internal_client, alpp);
DRETURN(ret);
}
static void sge_gdi_ctx_destroy(void *theState)
{
sge_gdi_ctx_t *s = (sge_gdi_ctx_t *)theState;
DENTER(TOP_LAYER, "sge_gdi_ctx_destroy");
sge_env_state_class_destroy(&(s->sge_env_state_obj));
sge_prog_state_class_destroy(&(s->sge_prog_state_obj));
sge_path_state_class_destroy(&(s->sge_path_state_obj));
sge_bootstrap_state_class_destroy(&(s->sge_bootstrap_state_obj));
sge_csp_path_class_destroy(&(s->sge_csp_path_obj));
sge_free(&(s->master));
sge_free(&(s->username));
sge_free(&(s->groupname));
sge_free(&(s->component_name));
sge_free(&(s->thread_name));
sge_free(&(s->component_username));
sge_free(&(s->ssl_certificate));
sge_free(&(s->ssl_private_key));
sge_error_class_destroy(&(s->eh));
sge_free(&s);
DRETURN_VOID;
}
static int sge_gdi_ctx_class_connect(sge_gdi_ctx_class_t *thiz)
{
int ret = 0;
bool is_alive_check = true;
DENTER(TOP_LAYER, "sge_gdi_ctx_class_connect");
/*
** TODO: must contain similar functionality as sge_gdi_setup
*/
ret = sge_gdi_ctx_class_prepare_enroll(thiz);
/* check if master is alive */
if (ret == CL_RETVAL_OK && is_alive_check) {
const char *master = thiz->get_master(thiz, true);
DPRINTF(("thiz->get_master(thiz) = %s\n", master));
ret = thiz->is_alive(thiz);
}
DRETURN(ret);
}
static int sge_gdi_ctx_class_prepare_enroll(sge_gdi_ctx_class_t *thiz) {
sge_path_state_class_t* path_state = thiz->get_sge_path_state(thiz);
sge_bootstrap_state_class_t * bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
cl_host_resolve_method_t resolve_method = CL_SHORT;
cl_framework_t communication_framework = CL_CT_TCP;
cl_com_handle_t* handle = NULL;
const char *help = NULL;
const char *default_domain = NULL;
int cl_ret = CL_RETVAL_OK;
DENTER(TOP_LAYER, "sge_gdi_ctx_class_prepare_enroll");
/* context setup is complete => setup the commlib
** TODO:
** there is a problem in qsub if it is used with CL_RW_THREAD, the signaling in qsub -sync
** makes qsub hang
*/
if (cl_com_setup_commlib_complete() == false) {
char* env_sge_commlib_debug = getenv("SGE_DEBUG_LEVEL");
switch (thiz->get_who(thiz)) {
case QMASTER:
case QMON:
case DRMAA:
case JGDI:
case SCHEDD:
case EXECD:
{
INFO((SGE_EVENT, SFNMAX, MSG_GDI_MULTI_THREADED_STARTUP));
/* if SGE_DEBUG_LEVEL environment is set we use gdi log flush function */
/* you can set commlib debug level with env SGE_COMMLIB_DEBUG */
if (env_sge_commlib_debug != NULL) {
cl_ret = cl_com_setup_commlib(CL_RW_THREAD, CL_LOG_OFF, sge_gdi_ctx_log_flush_func);
} else {
/* here we use default commlib flush function */
cl_ret = cl_com_setup_commlib(CL_RW_THREAD, CL_LOG_OFF, NULL);
}
}
break;
default:
{
INFO((SGE_EVENT, SFNMAX, MSG_GDI_SINGLE_THREADED_STARTUP));
if (env_sge_commlib_debug != NULL) {
cl_ret = cl_com_setup_commlib(CL_NO_THREAD, CL_LOG_OFF, sge_gdi_ctx_log_flush_func);
} else {
cl_ret = cl_com_setup_commlib(CL_NO_THREAD, CL_LOG_OFF, NULL);
}
/*
** verbose logging is switched on by default
*/
log_state_set_log_verbose(1);
}
}
if (cl_ret != CL_RETVAL_OK) {
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR,
"cl_com_setup_commlib failed: %s", cl_get_error_text(cl_ret));
DRETURN(cl_ret);
}
}
/* set the alias file */
cl_ret = cl_com_set_alias_file((char*)path_state->get_alias_file(path_state));
if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) {
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR,
"cl_com_set_alias_file failed: %s", cl_get_error_text(cl_ret));
DRETURN(cl_ret);
}
/* setup the resolve method */
if( bootstrap_state->get_ignore_fqdn(bootstrap_state) == false ) {
resolve_method = CL_LONG;
}
if ((help = bootstrap_state->get_default_domain(bootstrap_state)) != NULL) {
if (SGE_STRCASECMP(help, NONE_STR) != 0) {
default_domain = help;
}
}
cl_ret = cl_com_set_resolve_method(resolve_method, (char*)default_domain);
if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) {
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR,
"cl_com_set_resolve_method failed: %s", cl_get_error_text(cl_ret));
DRETURN(cl_ret);
}
/*
** reresolve qualified hostname with use of host aliases
** (corresponds to reresolve_me_qualified_hostname)
*/
cl_ret = thiz->reresolve_qualified_hostname(thiz);
if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) {
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_WARNING,
"reresolve hostname failed: %s", cl_get_error_text(cl_ret));
DRETURN(cl_ret);
}
/*
** TODO set a general_communication_error
*/
cl_ret = cl_com_set_error_func(general_communication_error);
if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) {
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR,
"cl_com_set_error_func failed: %s", cl_get_error_text(cl_ret));
DRETURN(cl_ret);
}
/* TODO set tag name function */
cl_ret = cl_com_set_tag_name_func(sge_dump_message_tag);
if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) {
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR,
"cl_com_set_tag_name_func failed: %s", cl_get_error_text(cl_ret));
DRETURN(cl_ret);
}
#ifdef DEBUG_CLIENT_SUPPORT
/* set debug client callback function to rmon's debug client callback */
cl_ret = cl_com_set_application_debug_client_callback_func(rmon_debug_client_callback);
if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) {
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR,
"cl_com_set_application_debug_client_callback_func failed: %s", cl_get_error_text(cl_ret));
DRETURN(cl_ret);
}
#endif
handle = thiz->get_com_handle(thiz);
if (handle == NULL) {
/* handle does not exist, create one */
int me_who = thiz->get_who(thiz);
const char *progname = thiz->get_progname(thiz);
const char *master = thiz->get_master(thiz, true);
const char *qualified_hostname = thiz->get_qualified_hostname(thiz);
u_long32 sge_qmaster_port = thiz->get_sge_qmaster_port(thiz); /* TODO: reresolving of port from /etc/services etc. */
u_long32 sge_execd_port = thiz->get_sge_execd_port(thiz);
int my_component_id = 0; /* 1 for daemons, 0=automatical for clients */
if (master == NULL && !(me_who == QMASTER)) {
DRETURN(CL_RETVAL_UNKNOWN);
}
/*
** CSP initialize
*/
if (strcasecmp(bootstrap_state->get_security_mode(bootstrap_state), "csp") == 0) {
cl_ssl_setup_t *sec_ssl_setup_config = NULL;
cl_ssl_cert_mode_t ssl_cert_mode = CL_SSL_PEM_FILE;
sge_csp_path_class_t *sge_csp = get_sge_csp_path(thiz);
if (thiz->get_certificate(thiz) != NULL) {
ssl_cert_mode = CL_SSL_PEM_BYTE;
sge_csp->set_cert_file(sge_csp, thiz->get_certificate(thiz));
sge_csp->set_key_file(sge_csp, thiz->get_private_key(thiz));
}
sge_csp->dprintf(sge_csp);
communication_framework = CL_CT_SSL;
cl_ret = cl_com_create_ssl_setup(&sec_ssl_setup_config,
ssl_cert_mode,
CL_SSL_v23, /* ssl_method */
(char*)sge_csp->get_CA_cert_file(sge_csp), /* ssl_CA_cert_pem_file */
(char*)sge_csp->get_CA_key_file(sge_csp), /* ssl_CA_key_pem_file */
(char*)sge_csp->get_cert_file(sge_csp), /* ssl_cert_pem_file */
(char*)sge_csp->get_key_file(sge_csp), /* ssl_key_pem_file */
(char*)sge_csp->get_rand_file(sge_csp), /* ssl_rand_file */
(char*)sge_csp->get_reconnect_file(sge_csp), /* ssl_reconnect_file */
(char*)sge_csp->get_crl_file(sge_csp), /* ssl_crl_file */
sge_csp->get_refresh_time(sge_csp), /* ssl_refresh_time */
(char*)sge_csp->get_password(sge_csp), /* ssl_password */
sge_csp->get_verify_func(sge_csp)); /* ssl_verify_func (cl_ssl_verify_func_t) */
if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) {
DPRINTF(("return value of cl_com_create_ssl_setup(): %s\n", cl_get_error_text(cl_ret)));
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, MSG_GDI_CANT_CONNECT_HANDLE_SSUUS,
qualified_hostname,
progname, 0,
sge_qmaster_port,
cl_get_error_text(cl_ret));
DRETURN(cl_ret);
}
/*
** set the CSP credential info into commlib
*/
cl_ret = cl_com_specify_ssl_configuration(sec_ssl_setup_config);
if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) {
DPRINTF(("return value of cl_com_specify_ssl_configuration(): %s\n", cl_get_error_text(cl_ret)));
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, MSG_GDI_CANT_CONNECT_HANDLE_SSUUS,
(char*)thiz->get_component_name(thiz), 0,
sge_qmaster_port,
cl_get_error_text(cl_ret));
cl_com_free_ssl_setup(&sec_ssl_setup_config);
DRETURN(cl_ret);
}
cl_com_free_ssl_setup(&sec_ssl_setup_config);
}
if ( me_who == QMASTER ||
me_who == EXECD ||
me_who == SCHEDD ||
me_who == SHADOWD ) {
my_component_id = 1;
}
switch (me_who) {
case EXECD:
/* add qmaster as known endpoint */
DPRINTF(("re-read actual qmaster file (prepare_enroll)\n"));
cl_com_append_known_endpoint_from_name((char*)master,
(char*)prognames[QMASTER],
1,
sge_qmaster_port,
CL_CM_AC_DISABLED,
true);
handle = cl_com_create_handle(&cl_ret,
communication_framework,
CL_CM_CT_MESSAGE,
true,
sge_execd_port,
CL_TCP_DEFAULT,
(char*)thiz->get_component_name(thiz),
my_component_id,
1,
0);
cl_com_set_auto_close_mode(handle, CL_CM_AC_ENABLED);
if (handle == NULL) {
if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) {
/*
** TODO: eh error handler does no logging
*/
ERROR((SGE_EVENT, MSG_GDI_CANT_GET_COM_HANDLE_SSUUS,
qualified_hostname,
(char*)thiz->get_component_name(thiz),
sge_u32c(my_component_id),
sge_u32c(sge_execd_port),
cl_get_error_text(cl_ret)));
}
}
break;
case QMASTER:
DPRINTF(("creating QMASTER handle\n"));
cl_com_append_known_endpoint_from_name((char*)master,
(char*) prognames[QMASTER],
1,
sge_qmaster_port,
CL_CM_AC_DISABLED ,
true);
/* do a later qmaster commlib listen before creating qmaster handle */
/* TODO: CL_COMMLIB_DELAYED_LISTEN is set to false, because
enabling it might cause problems with current shadowd and
startup qmaster implementation */
cl_commlib_set_global_param(CL_COMMLIB_DELAYED_LISTEN, false);
handle = cl_com_create_handle(&cl_ret,
communication_framework,
CL_CM_CT_MESSAGE, /* message based tcp communication */
true,
sge_qmaster_port, /* create service on qmaster port */
CL_TCP_DEFAULT, /* use standard connect mode */
(char*)thiz->get_component_name(thiz),
my_component_id, /* this endpoint is called "qmaster"
and has id 1 */
1,
0); /* select timeout is set to 1 second 0 usec */
if (handle == NULL) {
if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) {
/*
** TODO: eh error handler does no logging
*/
ERROR((SGE_EVENT, MSG_GDI_CANT_GET_COM_HANDLE_SSUUS,
qualified_hostname,
(char*)thiz->get_component_name(thiz),
sge_u32c(my_component_id),
sge_u32c(sge_qmaster_port),
cl_get_error_text(cl_ret)));
}
} else {
/* TODO: remove reresolving here */
int alive_back = 0;
char act_resolved_qmaster_name[CL_MAXHOSTLEN];
cl_com_set_synchron_receive_timeout(handle, 5);
/* TODO: reresolve master */
master = thiz->get_master(thiz, true);
if (master != NULL) {
/* TODO: sge_hostcmp uses implicitly bootstrap state info */
/* check a running qmaster on different host */
if (getuniquehostname(master, act_resolved_qmaster_name, 0) == CL_RETVAL_OK &&
sge_hostcmp(act_resolved_qmaster_name, qualified_hostname) != 0) {
DPRINTF(("act_qmaster file contains host "SFQ" which doesn't match local host name "SFQ"\n",
master, qualified_hostname));
cl_com_set_error_func(NULL);
alive_back = thiz->is_alive(thiz);
cl_ret = cl_com_set_error_func(general_communication_error);
if (cl_ret != CL_RETVAL_OK) {
ERROR((SGE_EVENT, SFNMAX, cl_get_error_text(cl_ret)));
}
if (alive_back == CL_RETVAL_OK && getenv("SGE_TEST_HEARTBEAT_TIMEOUT") == NULL ) {
CRITICAL((SGE_EVENT, MSG_GDI_MASTER_ON_HOST_X_RUNINNG_TERMINATE_S, master));
/* TODO: remove !!! */
SGE_EXIT(NULL, 1);
} else {
DPRINTF(("qmaster on host "SFQ" is down\n", master));
}
} else {
DPRINTF(("act_qmaster file contains local host name\n"));
}
} else {
DPRINTF(("skipping qmaster alive check because act_qmaster is not availabe\n"));
}
}
break;
case QMON:
DPRINTF(("creating QMON GDI handle\n"));
handle = cl_com_create_handle(&cl_ret,
communication_framework,
CL_CM_CT_MESSAGE,
false,
sge_qmaster_port,
CL_TCP_DEFAULT,
(char*)thiz->get_component_name(thiz),
my_component_id,
1,
0);
cl_com_set_auto_close_mode(handle, CL_CM_AC_ENABLED);
if (handle == NULL) {
if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) {
ERROR((SGE_EVENT, MSG_GDI_CANT_CONNECT_HANDLE_SSUUS,
qualified_hostname,
(char*)thiz->get_component_name(thiz),
sge_u32c(my_component_id),
sge_u32c(sge_qmaster_port),
cl_get_error_text(cl_ret)));
}
}
break;
default:
/* this is for "normal" gdi clients of qmaster */
DPRINTF(("creating %s GDI handle\n", thiz->get_component_name(thiz)));
handle = cl_com_create_handle(&cl_ret,
communication_framework,
CL_CM_CT_MESSAGE,
false,
sge_qmaster_port,
CL_TCP_DEFAULT,
(char*)thiz->get_component_name(thiz),
my_component_id,
1,
0);
if (handle == NULL) {
/* if (cl_ret != CL_RETVAL_OK && cl_ret != ctx_get_last_commlib_error(thiz)) { */
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR,
MSG_GDI_CANT_CONNECT_HANDLE_SSUUS,
thiz->get_qualified_hostname(thiz),
thiz->get_component_name(thiz),
sge_u32c(my_component_id),
sge_u32c(sge_qmaster_port),
cl_get_error_text(cl_ret));
/* } */
}
break;
}
ctx_set_last_commlib_error(thiz, cl_ret);
}
#ifdef DEBUG_CLIENT_SUPPORT
/* set rmon callback for message printing (after handle creation) */
rmon_set_print_callback(gdi_rmon_print_callback_function);
#endif
if ((thiz->get_who(thiz) == QMASTER) && (getenv("SGE_TEST_SOCKET_BIND") != NULL)) {
/* this is for testsuite socket bind test (issue 1096 ) */
struct timeval now;
gettimeofday(&now,NULL);
/* if this environment variable is set, we wait 15 seconds after
communication lib setup */
DPRINTF(("waiting for 60 seconds, because environment SGE_TEST_SOCKET_BIND is set\n"));
while ( handle != NULL && now.tv_sec - handle->start_time.tv_sec <= 60 ) {
DPRINTF(("timeout: "sge_U32CFormat"\n",sge_u32c(now.tv_sec - handle->start_time.tv_sec)));
cl_commlib_trigger(handle, 1);
gettimeofday(&now,NULL);
}
DPRINTF(("continue with setup\n"));
}
DRETURN(cl_ret);
}
static int sge_gdi_ctx_class_is_alive(sge_gdi_ctx_class_t *thiz)
{
cl_com_SIRM_t* status = NULL;
int cl_ret = CL_RETVAL_OK;
cl_com_handle_t *handle = thiz->get_com_handle(thiz);
/* TODO */
const char* comp_name = prognames[QMASTER];
const char* comp_host = thiz->get_master(thiz, false);
int comp_id = 1;
int comp_port = thiz->get_sge_qmaster_port(thiz);
DENTER(TOP_LAYER, "sge_gdi_ctx_class_is_alive");
if (handle == NULL) {
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR,
"handle not found %s:0", thiz->get_component_name(thiz));
DRETURN(CL_RETVAL_PARAMS);
}
/*
* update endpoint information of qmaster in commlib
* qmaster could have changed due to migration
*/
cl_com_append_known_endpoint_from_name((char*)comp_host, (char*)comp_name, comp_id,
comp_port, CL_CM_AC_DISABLED, true);
DPRINTF(("to->comp_host, to->comp_name, to->comp_id: %s/%s/%d\n", comp_host?comp_host:"", comp_name?comp_name:"", comp_id));
cl_ret = cl_commlib_get_endpoint_status(handle, (char*)comp_host, (char*)comp_name, comp_id, &status);
if (cl_ret != CL_RETVAL_OK) {
sge_gdi_ctx_class_error(thiz, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR,
"cl_commlib_get_endpoint_status failed: "SFQ, cl_get_error_text(cl_ret));
} else {
DEBUG((SGE_EVENT, SFNMAX, MSG_GDI_QMASTER_STILL_RUNNING));
}
if (status != NULL) {
DEBUG((SGE_EVENT,MSG_GDI_ENDPOINT_UPTIME_UU, sge_u32c(status->runtime) ,
sge_u32c(status->application_status)));
cl_com_free_sirm_message(&status);
}
DRETURN(cl_ret);
}
static lList* sge_gdi_ctx_class_gdi_tsm(sge_gdi_ctx_class_t *thiz, const char *schedd_name, const char *cell)
{
lList *alp = NULL;
DENTER(TOP_LAYER, "sge_gdi_ctx_class_gdi_tsm");
alp = gdi2_tsm(thiz, schedd_name, cell);
DRETURN(alp);
}
static lList* sge_gdi_ctx_class_gdi_kill(sge_gdi_ctx_class_t *thiz, lList *id_list, const char *cell,
u_long32 option_flags, u_long32 action_flag)
{
lList *alp = NULL;
DENTER(TOP_LAYER, "sge_gdi_ctx_class_gdi_kill");
alp = gdi2_kill(thiz, id_list, cell, option_flags, action_flag);
DRETURN(alp);
}
static bool sge_gdi_ctx_class_gdi_check_permission(sge_gdi_ctx_class_t *thiz, lList **alpp, int option)
{
bool ret = false;
DENTER(TOP_LAYER, "sge_gdi_ctx_class_gdi_check_permission");
ret = sge_gdi2_check_permission(thiz, alpp, option);
DRETURN(ret);
}
static bool sge_gdi_ctx_class_gdi_get_mapping_name(sge_gdi_ctx_class_t *thiz, const char *requestedHost, char *buf, int buflen)
{
bool ret = false;
DENTER(TOP_LAYER, "sge_gdi_ctx_class_gdi_get_mapping_name");
ret = sge_gdi2_get_mapping_name(thiz, requestedHost, buf, buflen);
DRETURN(ret);
}
static void sge_gdi_ctx_class_dprintf(sge_gdi_ctx_class_t *ctx)
{
DENTER(TOP_LAYER, "sge_gdi_ctx_class_dprintf");
if (ctx == NULL) {
DRETURN_VOID;
}
DPRINTF(("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n"));
(ctx->get_sge_env_state(ctx))->dprintf(ctx->get_sge_env_state(ctx));
(ctx->get_sge_prog_state(ctx))->dprintf(ctx->get_sge_prog_state(ctx));
(ctx->get_sge_path_state(ctx))->dprintf(ctx->get_sge_path_state(ctx));
(ctx->get_sge_bootstrap_state(ctx))->dprintf(ctx->get_sge_bootstrap_state(ctx));
DPRINTF(("master: %s\n", ctx->get_master(ctx, false)));
DPRINTF(("uid/username: %d/%s\n", (int) ctx->get_uid(ctx), ctx->get_username(ctx)));
DPRINTF(("gid/groupname: %d/%s\n", (int) ctx->get_gid(ctx), ctx->get_groupname(ctx)));
DPRINTF(("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"));
DRETURN_VOID;
}
/** --------- getter/setter ------------------------------------------------- */
static cl_com_handle_t* get_com_handle(sge_gdi_ctx_class_t *thiz)
{
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return cl_com_get_handle(es->component_name, 0);
}
static sge_env_state_class_t* get_sge_env_state(sge_gdi_ctx_class_t *thiz)
{
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return es->sge_env_state_obj;
}
static sge_prog_state_class_t* get_sge_prog_state(sge_gdi_ctx_class_t *thiz)
{
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return es->sge_prog_state_obj;
}
static sge_path_state_class_t* get_sge_path_state(sge_gdi_ctx_class_t *thiz)
{
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return es->sge_path_state_obj;
}
static sge_csp_path_class_t* get_sge_csp_path(sge_gdi_ctx_class_t *thiz)
{
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return es->sge_csp_path_obj;
}
static sge_bootstrap_state_class_t* get_sge_bootstrap_state(sge_gdi_ctx_class_t *thiz)
{
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return es->sge_bootstrap_state_obj;
}
static const char* get_master(sge_gdi_ctx_class_t *thiz, bool reread) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
sge_path_state_class_t* path_state = thiz->get_sge_path_state(thiz);
sge_error_class_t *eh = es ? es->eh : NULL;
static bool error_already_logged = false;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_master");
if (es->master == NULL || reread) {
char err_str[SGE_PATH_MAX+128];
char master_name[CL_MAXHOSTLEN];
u_long32 now = sge_get_gmt();
/* fix system clock moved back situation */
if (es->last_qmaster_file_read > now) {
es->last_qmaster_file_read = 0;
}
if (es->master == NULL || now - es->last_qmaster_file_read >= 30) {
/* re-read act qmaster file (max. every 30 seconds) */
DPRINTF(("re-read actual qmaster file\n"));
es->last_qmaster_file_read = now;
if (get_qm_name(master_name, path_state->get_act_qmaster_file(path_state), err_str, sizeof(err_str)) == -1) {
if (eh != NULL && !error_already_logged) {
eh->error(eh, STATUS_EUNKNOWN, ANSWER_QUALITY_ERROR, MSG_GDI_READMASTERNAMEFAILED_S, err_str);
error_already_logged = true;
}
DRETURN(NULL);
}
error_already_logged = false;
DPRINTF(("(re-)reading act_qmaster file. Got master host \"%s\"\n", master_name));
/*
** TODO: thread locking needed here ?
*/
es->master = sge_strdup(es->master,master_name);
}
}
DRETURN(es->master);
}
static const char* get_qualified_hostname(sge_gdi_ctx_class_t *thiz) {
sge_prog_state_class_t* prog_state = thiz->get_sge_prog_state(thiz);
const char *qualified_hostname = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_progname");
qualified_hostname = prog_state->get_qualified_hostname(prog_state);
DRETURN(qualified_hostname);
}
static const char* get_unqualified_hostname(sge_gdi_ctx_class_t *thiz) {
sge_prog_state_class_t* prog_state = thiz->get_sge_prog_state(thiz);
const char *unqualified_hostname = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_unqualified_hostname");
unqualified_hostname = prog_state->get_unqualified_hostname(prog_state);
DRETURN(unqualified_hostname);
}
static const char* get_component_name(sge_gdi_ctx_class_t *thiz) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
const char* ret = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_component_name");
ret = es->component_name;
DRETURN(ret);
}
static const char* get_thread_name(sge_gdi_ctx_class_t *thiz) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
const char* ret = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_thread_name");
ret = es->thread_name;
DRETURN(ret);
}
static const char* get_progname(sge_gdi_ctx_class_t *thiz) {
sge_prog_state_class_t* prog_state = thiz->get_sge_prog_state(thiz);
const char *progname = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_progname");
progname = prog_state->get_sge_formal_prog_name(prog_state);
DRETURN(progname);
}
static u_long32 get_who(sge_gdi_ctx_class_t *thiz) {
sge_prog_state_class_t* prog_state = thiz->get_sge_prog_state(thiz);
u_long32 prognumber = 0;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_who");
prognumber = prog_state->get_who(prog_state);
DRETURN(prognumber);
}
static bool is_daemonized(sge_gdi_ctx_class_t *thiz) {
sge_prog_state_class_t* prog_state = thiz->get_sge_prog_state(thiz);
bool is_daemonized = false;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->is_daemonized");
is_daemonized = (prog_state->get_daemonized(prog_state)) ? true : false;
DRETURN(is_daemonized);
}
static void set_daemonized(sge_gdi_ctx_class_t *thiz, bool daemonized) {
sge_prog_state_class_t* prog_state = thiz->get_sge_prog_state(thiz);
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->set_daemonized");
prog_state->set_daemonized(prog_state, daemonized);
DRETURN_VOID;
}
static bool get_job_spooling(sge_gdi_ctx_class_t *thiz) {
sge_bootstrap_state_class_t* bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
bool job_spooling = true;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_job_spooling");
job_spooling = bootstrap_state->get_job_spooling(bootstrap_state);
DRETURN(job_spooling);
}
static void set_job_spooling(sge_gdi_ctx_class_t *thiz, bool job_spooling) {
sge_bootstrap_state_class_t* bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->set_job_spooling");
bootstrap_state->set_job_spooling(bootstrap_state, job_spooling);
DRETURN_VOID;
}
static const char* get_spooling_method(sge_gdi_ctx_class_t *thiz) {
sge_bootstrap_state_class_t* bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
const char *spooling_method = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_spooling_method");
spooling_method = bootstrap_state->get_spooling_method(bootstrap_state);
DRETURN(spooling_method);
}
static const char* get_spooling_lib(sge_gdi_ctx_class_t *thiz) {
sge_bootstrap_state_class_t* bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
const char *spooling_lib = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_spooling_lib");
spooling_lib = bootstrap_state->get_spooling_lib(bootstrap_state);
DRETURN(spooling_lib);
}
static const char* get_spooling_params(sge_gdi_ctx_class_t *thiz) {
sge_bootstrap_state_class_t* bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
const char *spooling_params = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_spooling_params");
spooling_params = bootstrap_state->get_spooling_params(bootstrap_state);
DRETURN(spooling_params);
}
static u_long32 get_listener_thread_count(sge_gdi_ctx_class_t *thiz) {
sge_bootstrap_state_class_t* bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
u_long32 thread_count = 0;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_listenr_thread_count");
thread_count = bootstrap_state->get_listener_thread_count(bootstrap_state);
DRETURN(thread_count);
}
static u_long32 get_worker_thread_count(sge_gdi_ctx_class_t *thiz) {
sge_bootstrap_state_class_t* bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
u_long32 thread_count = 0;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_worker_thread_count");
thread_count = bootstrap_state->get_worker_thread_count(bootstrap_state);
DRETURN(thread_count);
}
static u_long32 get_scheduler_thread_count(sge_gdi_ctx_class_t *thiz) {
sge_bootstrap_state_class_t* bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
u_long32 thread_count = 0;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_scheduler_thread_count");
thread_count = bootstrap_state->get_scheduler_thread_count(bootstrap_state);
DRETURN(thread_count);
}
static u_long32 get_jvm_thread_count(sge_gdi_ctx_class_t *thiz) {
sge_bootstrap_state_class_t* bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
u_long32 thread_count = 0;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_jvm_thread_count");
thread_count = bootstrap_state->get_jvm_thread_count(bootstrap_state);
DRETURN(thread_count);
}
static sge_exit_func_t get_exit_func(sge_gdi_ctx_class_t *thiz) {
sge_prog_state_class_t* prog_state = thiz->get_sge_prog_state(thiz);
sge_exit_func_t exit_func = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_exit_func");
exit_func = prog_state->get_exit_func(prog_state);
DRETURN(exit_func);
}
static void set_exit_func(sge_gdi_ctx_class_t *thiz, sge_exit_func_t exit_func) {
sge_prog_state_class_t* prog_state = thiz->get_sge_prog_state(thiz);
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->set_exit_func");
prog_state->set_exit_func(prog_state, exit_func);
DRETURN_VOID;
}
static void set_private_key(sge_gdi_ctx_class_t *thiz, const char *pkey) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->set_private_key");
if (es->ssl_private_key != NULL) {
sge_free(&(es->ssl_private_key));
}
es->ssl_private_key = pkey ? strdup(pkey): NULL;
DRETURN_VOID;
}
static const char* get_private_key(sge_gdi_ctx_class_t *thiz) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
const char *pkey = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_private_key");
pkey = es->ssl_private_key;
DRETURN(pkey);
}
static void set_certificate(sge_gdi_ctx_class_t *thiz, const char *cert) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->set_certificate");
if (es->ssl_certificate != NULL) {
sge_free(&(es->ssl_certificate));
}
es->ssl_certificate = cert ? strdup(cert) : NULL;
DRETURN_VOID;
}
static const char* get_certificate(sge_gdi_ctx_class_t *thiz) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
const char *cert = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_certificate");
cert = es->ssl_certificate;
DRETURN(cert);
}
static const char* get_ca_local_root(sge_gdi_ctx_class_t *thiz) {
sge_csp_path_class_t *sge_csp = get_sge_csp_path(thiz);
const char *ca_local_root = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_ca_local_root");
if (sge_csp != NULL) {
ca_local_root = sge_csp->get_ca_local_root(sge_csp);
}
DRETURN(ca_local_root);
}
static const char* get_ca_root(sge_gdi_ctx_class_t *thiz) {
sge_csp_path_class_t *sge_csp = get_sge_csp_path(thiz);
const char *ca_root = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_ca_root");
if (sge_csp != NULL) {
ca_root = sge_csp->get_ca_root(sge_csp);
}
DRETURN(ca_root);
}
static const char* get_default_cell(sge_gdi_ctx_class_t *thiz) {
sge_prog_state_class_t* prog_state = thiz->get_sge_prog_state(thiz);
const char *default_cell = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_default_cell");
default_cell = prog_state->get_default_cell(prog_state);
DRETURN(default_cell);
}
static const char* get_cell_root(sge_gdi_ctx_class_t *thiz) {
sge_path_state_class_t* path_state = thiz->get_sge_path_state(thiz);
const char *cell_root = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_cell_root");
cell_root = path_state->get_cell_root(path_state);
DRETURN(cell_root);
}
static const char* get_sge_root(sge_gdi_ctx_class_t *thiz) {
sge_path_state_class_t* path_state = thiz->get_sge_path_state(thiz);
const char *sge_root = NULL;
DENTER(BASIS_LAYER, "sge_gdi_ctx_class->get_sge_root");
sge_root = path_state->get_sge_root(path_state);
DRETURN(sge_root);
}
static const char* get_admin_user(sge_gdi_ctx_class_t *thiz) {
sge_bootstrap_state_class_t * bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
return bootstrap_state->get_admin_user(bootstrap_state);
}
static const char* get_binary_path(sge_gdi_ctx_class_t *thiz) {
sge_bootstrap_state_class_t * bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
return bootstrap_state->get_binary_path(bootstrap_state);
}
static const char* get_qmaster_spool_dir(sge_gdi_ctx_class_t *thiz) {
sge_bootstrap_state_class_t * bootstrap_state = thiz->get_sge_bootstrap_state(thiz);
return bootstrap_state->get_qmaster_spool_dir(bootstrap_state);
}
static const char* get_bootstrap_file(sge_gdi_ctx_class_t *thiz) {
sge_path_state_class_t *path_state = thiz->get_sge_path_state(thiz);
return path_state->get_bootstrap_file(path_state);
}
static const char* get_act_qmaster_file(sge_gdi_ctx_class_t *thiz) {
sge_path_state_class_t *path_state = thiz->get_sge_path_state(thiz);
return path_state->get_act_qmaster_file(path_state);
}
static const char* get_acct_file(sge_gdi_ctx_class_t *thiz) {
sge_path_state_class_t *path_state = thiz->get_sge_path_state(thiz);
return path_state->get_acct_file(path_state);
}
static const char* get_reporting_file(sge_gdi_ctx_class_t *thiz) {
sge_path_state_class_t *path_state = thiz->get_sge_path_state(thiz);
return path_state->get_reporting_file(path_state);
}
static const char* get_shadow_master_file(sge_gdi_ctx_class_t *thiz) {
sge_path_state_class_t *path_state = thiz->get_sge_path_state(thiz);
return path_state->get_shadow_masters_file(path_state);
}
static const char* get_username(sge_gdi_ctx_class_t *thiz) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return es->username;
}
static const char* get_groupname(sge_gdi_ctx_class_t *thiz) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return es->groupname;
}
static u_long32 get_sge_qmaster_port(sge_gdi_ctx_class_t *thiz) {
sge_env_state_class_t *es = thiz->get_sge_env_state(thiz);
return es->get_sge_qmaster_port(es);
}
static u_long32 get_sge_execd_port(sge_gdi_ctx_class_t *thiz) {
sge_env_state_class_t *es = thiz->get_sge_env_state(thiz);
return es->get_sge_execd_port(es);
}
static int ctx_get_last_commlib_error(sge_gdi_ctx_class_t *thiz) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return es->last_commlib_error;
}
static void ctx_set_last_commlib_error(sge_gdi_ctx_class_t *thiz, int cl_error) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
es->last_commlib_error = cl_error;
}
static uid_t ctx_get_uid(sge_gdi_ctx_class_t *thiz) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return es->uid;
}
static gid_t ctx_get_gid(sge_gdi_ctx_class_t *thiz) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return es->gid;
}
static bool ctx_is_qmaster_internal_client(sge_gdi_ctx_class_t *thiz) {
sge_gdi_ctx_t *es = (sge_gdi_ctx_t *) thiz->sge_gdi_ctx_handle;
return es->is_qmaster_internal_client;
}
static int sge_gdi_ctx_log_flush_func(cl_raw_list_t* list_p)
{
int ret_val;
cl_log_list_elem_t* elem = NULL;
DENTER(GDI_LAYER, "sge_gdi_ctx_log_flush_func");
if (list_p == NULL) {
DRETURN(CL_RETVAL_LOG_NO_LOGLIST);
}
if ((ret_val = cl_raw_list_lock(list_p)) != CL_RETVAL_OK) {
DRETURN(ret_val);
}
while ((elem = cl_log_list_get_first_elem(list_p) ) != NULL) {
char* param;
if (elem->log_parameter == NULL) {
param = "";
} else {
param = elem->log_parameter;
}
switch(elem->log_type) {
case CL_LOG_ERROR:
if (log_state_get_log_level() >= LOG_ERR) {
ERROR((SGE_EVENT, "%-15s=> %s %s (%s)", elem->log_thread_name, elem->log_message, param, elem->log_module_name));
} else {
printf("%-15s=> %s %s (%s)\n", elem->log_thread_name, elem->log_message, param, elem->log_module_name);
}
break;
case CL_LOG_WARNING:
if (log_state_get_log_level() >= LOG_WARNING) {
WARNING((SGE_EVENT,"%-15s=> %s %s (%s)", elem->log_thread_name, elem->log_message, param, elem->log_module_name));
} else {
printf("%-15s=> %s %s (%s)\n", elem->log_thread_name, elem->log_message, param, elem->log_module_name);
}
break;
case CL_LOG_INFO:
if (log_state_get_log_level() >= LOG_INFO) {
INFO((SGE_EVENT, "%-15s=> %s %s (%s)", elem->log_thread_name, elem->log_message, param, elem->log_module_name));
} else {
printf("%-15s=> %s %s (%s)\n", elem->log_thread_name, elem->log_message, param, elem->log_module_name);
}
break;
case CL_LOG_DEBUG:
if (log_state_get_log_level() >= LOG_DEBUG) {
DEBUG((SGE_EVENT, "%-15s=> %s %s (%s)", elem->log_thread_name, elem->log_message, param, elem->log_module_name));
} else {
printf("%-15s=> %s %s (%s)\n", elem->log_thread_name, elem->log_message, param, elem->log_module_name);
}
break;
case CL_LOG_OFF:
break;
}
cl_log_list_del_log(list_p);
}
if ((ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
DRETURN(ret_val);
}
DRETURN(CL_RETVAL_OK);
}
/*
** TODO:
** only helper function to do the setup for clients similar to sge_setup()
*/
int
sge_setup2(sge_gdi_ctx_class_t **context, u_long32 progid, u_long32 thread_id,
lList **alpp, bool is_qmaster_intern_client)
{
char user[128] = "";
char group[128] = "";
const char *sge_root = NULL;
const char *sge_cell = NULL;
u_long32 sge_qmaster_port = 0;
u_long32 sge_execd_port = 0;
bool from_services = false;
DENTER(TOP_LAYER, "sge_setup2");
if (context == NULL) {
answer_list_add_sprintf(alpp, STATUS_ESEMANTIC, ANSWER_QUALITY_CRITICAL,
"%s", MSG_GDI_CONTEXT_NULL);
DRETURN(AE_ERROR);
}
/*
** TODO:
** get the environment for now here -> sge_env_class_t should be enhanced and used instead as input param
*/
sge_root = getenv("SGE_ROOT");
if (sge_root == NULL) {
answer_list_add_sprintf(alpp, STATUS_ESEMANTIC,
ANSWER_QUALITY_CRITICAL, "%s", MSG_SGEROOTNOTSET);
DRETURN(AE_ERROR);
}
sge_cell = getenv("SGE_CELL")?getenv("SGE_CELL"):DEFAULT_CELL;
sge_qmaster_port = sge_get_qmaster_port(&from_services);
sge_execd_port = sge_get_execd_port();
/*
* uidgid_mt_init() needs to be called here already so that sge_uid2user()
* sge_gid2group() works correctly. Other *_mt_init() functions are called
* in sge_gdi_ctx_class_create();
*/
uidgid_mt_init();
if (sge_uid2user(geteuid(), user, sizeof(user), MAX_NIS_RETRIES)) {
answer_list_add_sprintf(alpp, STATUS_ESEMANTIC, ANSWER_QUALITY_CRITICAL,
MSG_SYSTEM_RESOLVEUSER_U, (u_long32) geteuid());
DRETURN(AE_ERROR);
}
#if defined(INTERIX)
{
/*
* If we are at boot time on Windows Vista, the primary group ID of the
* local Administrator is not yet set correctly, so asking for it doesn't
* make sense. We build it on our own, which is possible as it's always
* built after the same scheme: hostname+None.
*/
const char *group_none = "+None";
/* Read the hostname into the group string and add "+None" */
gethostname(group, sizeof(group)-strlen(group_none)-1);
strcat(group, group_none); /* RATS: ignore */
}
#else
if (sge_gid2group(getegid(), group, sizeof(group), MAX_NIS_RETRIES)) {
answer_list_add_sprintf(alpp, STATUS_ESEMANTIC, ANSWER_QUALITY_CRITICAL,
MSG_GDI_GETGRGIDXFAILEDERRORX_U, (u_long32)getegid());
DRETURN(AE_ERROR);
}
#endif
/* a dynamic eh handler is created */
*context = sge_gdi_ctx_class_create(progid, prognames[progid], thread_id,
threadnames[thread_id], user, group,
sge_root, sge_cell, sge_qmaster_port,
sge_execd_port, from_services,
is_qmaster_intern_client, alpp);
if (*context == NULL) {
DRETURN(AE_ERROR);
}
/*
** TODO: we set the log state context here
** this should be done more explicitily !!!
*/
log_state_set_log_context(*context);
/*
** TODO: bootstrap info is used in cull functions sge_hostcpy
** ignore_fqdn, domain_name
** Therefore we have to set it into the thread ctx
*/
sge_gdi_set_thread_local_ctx(*context);
DRETURN(AE_OK);
}
/*
** TODO:
** only helper function to do the setup for clients similar to sge_gdi_setup()
*/
int sge_gdi2_setup(sge_gdi_ctx_class_t **context_ref, u_long32 progid, u_long32 thread_id, lList **alpp)
{
int ret = AE_OK;
bool alpp_was_null = true;
DENTER(TOP_LAYER, "sge_gdi2_setup");
if (context_ref && sge_gdi_ctx_is_setup(*context_ref)) {
if (alpp_was_null) {
SGE_ADD_MSG_ID(sprintf(SGE_EVENT, SFNMAX, MSG_GDI_GDI_ALREADY_SETUP));
} else {
answer_list_add_sprintf(alpp, STATUS_EEXIST, ANSWER_QUALITY_WARNING,
"%s", MSG_GDI_GDI_ALREADY_SETUP);
}
DRETURN(AE_ALREADY_SETUP);
}
/* Ensure we get consisent floating point handling in case of
operation across en-like and fr-like locales (which have
different decimal points). */
setlocale(LC_NUMERIC, "C");
ret = sge_setup2(context_ref, progid, thread_id, alpp, false);
if (ret != AE_OK) {
DRETURN(ret);
}
if ((*context_ref)->prepare_enroll(*context_ref) != CL_RETVAL_OK) {
sge_gdi_ctx_class_get_errors(*context_ref, alpp, true);
DRETURN(AE_QMASTER_DOWN);
}
sge_gdi_ctx_set_is_setup(*context_ref, true);
DRETURN(AE_OK);
}
static int reresolve_qualified_hostname(sge_gdi_ctx_class_t *thiz) {
char unique_hostname[CL_MAXHOSTLEN];
sge_prog_state_class_t* prog_state = thiz->get_sge_prog_state(thiz);
int ret = CL_RETVAL_OK;
DENTER(TOP_LAYER, "gdi2_reresolve_qualified_hostname");
ret=getuniquehostname(prog_state->get_qualified_hostname(prog_state), unique_hostname, 0);
if( ret != CL_RETVAL_OK ) {
DRETURN(ret);
}
prog_state->set_qualified_hostname(prog_state, unique_hostname);
DRETURN(ret);
}
/****** gdi/ctx/sge_daemonize_prepare() *****************************************
* NAME
* sge_daemonize_prepare() -- prepare daemonize of process
*
* SYNOPSIS
* int sge_daemonize_prepare(void)
*
* FUNCTION
* The parent process will wait for the child's successful daemonizing.
* The client process will report successful daemonizing by a call to
* sge_daemonize_finalize().
* The parent process will exit with one of the following exit states:
*
* typedef enum uti_deamonize_state_type {
* SGE_DEAMONIZE_OK = 0, ok
* SGE_DAEMONIZE_DEAD_CHILD = 1, child exited before sending state
* SGE_DAEMONIZE_TIMEOUT = 2 timeout whild waiting for state
* } uti_deamonize_state_t;
*
* Daemonize the current application. Throws ourself into the
* background and dissassociates from any controlling ttys.
* Don't close filedescriptors mentioned in 'keep_open'.
*
* sge_daemonize_prepare() and sge_daemonize_finalize() will replace
* sge_daemonize() for multithreaded applications.
*
* sge_daemonize_prepare() must be called before starting any thread.
*
*
* INPUTS
* void - none
*
* RESULT
* int - true on success, false on error
*
* SEE ALSO
* uti/os/sge_daemonize_finalize()
*******************************************************************************/
bool sge_daemonize_prepare(sge_gdi_ctx_class_t *ctx) {
pid_t pid;
#if !(defined(__hpux) || defined(WIN32) || defined(INTERIX) || defined(__CYGWIN__))
int fd;
#endif
#if defined(__sgi) || defined(ALPHA) || defined(HP1164)
# if defined(ALPHA)
extern int getdomainname(char *, int);
# endif
char domname[256];
#endif
int is_daemonized = ctx->is_daemonized(ctx);
DENTER(TOP_LAYER, "sge_daemonize_prepare");
#ifndef NO_SGE_COMPILE_DEBUG
if (TRACEON) {
DRETURN(false);
}
#endif
if (is_daemonized) {
DRETURN(true);
}
/* create pipe */
if ( pipe(fd_pipe) < 0) {
CRITICAL((SGE_EVENT, SFNMAX, MSG_UTI_DAEMONIZE_CANT_PIPE));
DRETURN(false);
}
if ( fcntl(fd_pipe[0], F_SETFL, O_NONBLOCK) != 0) {
CRITICAL((SGE_EVENT, SFNMAX, MSG_UTI_DAEMONIZE_CANT_FCNTL_PIPE));
DRETURN(false);
}
/* close all fd's except pipe and first 3 */
{
int keep_open[5];
keep_open[0] = 0;
keep_open[1] = 1;
keep_open[2] = 2;
keep_open[3] = fd_pipe[0];
keep_open[4] = fd_pipe[1];
sge_close_all_fds(keep_open, 5);
}
/* first fork */
pid=fork();
if (pid <0) {
CRITICAL((SGE_EVENT, MSG_PROC_FIRSTFORKFAILED_S , strerror(errno)));
DEXIT;
exit(1);
}
if (pid > 0) {
char line[256];
int line_p = 0;
int retries = 60;
int exit_status = SGE_DAEMONIZE_TIMEOUT;
int back;
int errno_value = 0;
/* close send pipe */
close(fd_pipe[1]);
/* check pipe for message from child */
while (line_p < 4 && retries-- > 0) {
errno = 0;
back = read(fd_pipe[0], &line[line_p], 1);
errno_value = errno;
if (back > 0) {
line_p++;
} else {
if (back != -1) {
if (errno_value != EAGAIN ) {
retries=0;
exit_status = SGE_DAEMONIZE_DEAD_CHILD;
}
}
DPRINTF(("back=%d errno=%d\n",back,errno_value));
sleep(1);
}
}
if (line_p >= 4) {
line[3] = 0;
exit_status = atoi(line);
DPRINTF(("received: \"%d\"\n", exit_status));
}
switch(exit_status) {
case SGE_DEAMONIZE_OK:
INFO((SGE_EVENT, SFNMAX, MSG_UTI_DAEMONIZE_OK));
break;
case SGE_DAEMONIZE_DEAD_CHILD:
WARNING((SGE_EVENT, SFNMAX, MSG_UTI_DAEMONIZE_DEAD_CHILD));
break;
case SGE_DAEMONIZE_TIMEOUT:
WARNING((SGE_EVENT, SFNMAX, MSG_UTI_DAEMONIZE_TIMEOUT));
break;
}
/* close read pipe */
close(fd_pipe[0]);
DEXIT;
exit(exit_status); /* parent exit */
}
/* child */
SETPGRP;
#if !(defined(__hpux) || defined(WIN32) || defined(INTERIX) || defined(__CYGWIN__))
if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
/* disassociate contolling tty */
ioctl(fd, TIOCNOTTY, (char *) NULL);
close(fd);
}
#endif
/* second fork */
pid=fork();
if (pid < 0) {
CRITICAL((SGE_EVENT, MSG_PROC_SECONDFORKFAILED_S , strerror(errno)));
DEXIT;
exit(1);
}
if ( pid > 0) {
/* close read and write pipe for second child and exit */
close(fd_pipe[0]);
close(fd_pipe[1]);
exit(0);
}
/* child of child */
/* close read pipe */
close(fd_pipe[0]);
#if defined(__sgi) || defined(ALPHA) || defined(HP1164)
/* The yp library may have open sockets
when closing all fds also the socket fd of the yp library gets closed
when called again yp library functions are confused since they
assume fds are already open. Thus we shutdown the yp library regularly
before closing all sockets */
getdomainname(domname, sizeof(domname));
yp_unbind(domname);
#endif
DRETURN(true);
}
/****** gdi/ctx/sge_daemonize_finalize() ****************************************
* NAME
* sge_daemonize_finalize() -- finalize daemonize process
*
* SYNOPSIS
* int sge_daemonize_finalize(fd_set *keep_open)
*
* FUNCTION
* report successful daemonizing to the parent process and close
* all file descriptors. Set file descirptors 0, 1 and 2 to /dev/null
*
* sge_daemonize_prepare() and sge_daemonize_finalize() will replace
* sge_daemonize() for multithreades applications.
*
* sge_daemonize_finalize() must be called by the thread who have called
* sge_daemonize_prepare().
*
* INPUTS
* fd_set *keep_open - file descriptor set to keep open
*
* RESULT
* int - true on success
*
* SEE ALSO
* uti/os/sge_daemonize_prepare()
*******************************************************************************/
bool sge_daemonize_finalize(sge_gdi_ctx_class_t *ctx)
{
int failed_fd;
char tmp_buffer[4];
int is_daemonized = ctx->is_daemonized(ctx);
DENTER(TOP_LAYER, "sge_daemonize_finalize");
/* don't call this function twice */
if (is_daemonized) {
DRETURN(true);
}
/* The response id has 4 byte, send it to father process */
snprintf(tmp_buffer, 4, "%3d", SGE_DEAMONIZE_OK );
if (write(fd_pipe[1], tmp_buffer, 4) != 4) {
dstring ds = DSTRING_INIT;
CRITICAL((SGE_EVENT, MSG_FILE_CANNOT_WRITE_SS, "fd_pipe[1]", sge_strerror(errno, &ds)));
sge_dstring_free(&ds);
}
sleep(2); /* give father time to read the status */
/* close write pipe */
close(fd_pipe[1]);
/* close first three file descriptors */
#ifndef __INSURE__
close(0);
close(1);
close(2);
/* new descriptors acquired for stdin, stdout, stderr should be 0,1,2 */
failed_fd = sge_occupy_first_three();
if (failed_fd != -1) {
CRITICAL((SGE_EVENT, MSG_CANNOT_REDIRECT_STDINOUTERR_I, failed_fd));
SGE_EXIT(NULL, 0);
}
#endif
SETPGRP;
/* now have finished daemonizing */
ctx->set_daemonized(ctx, true);
DRETURN(true);
}
/****** gdi/ctx/sge_daemonize() ************************************************
* NAME
* sge_daemonize() -- Daemonize the current application
*
* SYNOPSIS
* int sge_daemonize(fd_set *keep_open)
*
* FUNCTION
* Daemonize the current application. Throws ourself into the
* background and dissassociates from any controlling ttys.
* Don't close filedescriptors mentioned in 'keep_open'.
*
* INPUTS
* fd_set *keep_open - bitmask
* args optional args
*
* RESULT
* int - Successfull?
* 1 - Yes
* 0 - No
*
* NOTES
* MT-NOTES: sge_daemonize() is not MT safe
******************************************************************************/
int sge_daemonize(int *keep_open, unsigned long nr_of_fds, sge_gdi_ctx_class_t *ctx)
{
#if !(defined(__hpux) || defined(WIN32) || defined(INTERIX) || defined(__CYGWIN__))
int fd;
#endif
#if defined(__sgi) || defined(ALPHA) || defined(HP1164)
# if defined(ALPHA)
extern int getdomainname(char *, int);
# endif
char domname[256];
#endif
pid_t pid;
int failed_fd;
DENTER(TOP_LAYER, "sge_daemonize");
#ifndef NO_SGE_COMPILE_DEBUG
if (TRACEON) {
DRETURN(0);
}
#endif
if (ctx && ctx->is_daemonized(ctx)) {
DRETURN(1);
}
if ((pid=fork())!= 0) { /* 1st child not pgrp leader */
if (pid<0) {
CRITICAL((SGE_EVENT, MSG_PROC_FIRSTFORKFAILED_S , strerror(errno)));
}
exit(0);
}
SETPGRP;
#if !(defined(__hpux) || defined(WIN32) || defined(INTERIX) || defined(__CYGWIN__))
if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
/* disassociate contolling tty */
ioctl(fd, TIOCNOTTY, (char *) NULL);
close(fd);
}
#endif
if ((pid=fork())!= 0) {
if (pid<0) {
CRITICAL((SGE_EVENT, MSG_PROC_SECONDFORKFAILED_S , strerror(errno)));
}
exit(0);
}
#if defined(__sgi) || defined(ALPHA) || defined(HP1164)
/* The yp library may have open sockets
when closing all fds also the socket fd of the yp library gets closed
when called again yp library functions are confused since they
assume fds are already open. Thus we shutdown the yp library regularly
before closing all sockets */
getdomainname(domname, sizeof(domname));
yp_unbind(domname);
#endif
/* close all file descriptors */
sge_close_all_fds(keep_open, nr_of_fds);
/* new descriptors acquired for stdin, stdout, stderr should be 0,1,2 */
failed_fd = sge_occupy_first_three();
if (failed_fd != -1) {
CRITICAL((SGE_EVENT, MSG_CANNOT_REDIRECT_STDINOUTERR_I, failed_fd));
SGE_EXIT(NULL, 0);
}
SETPGRP;
if (ctx) ctx->set_daemonized(ctx, true);
DRETURN(1);
}
|