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
|
/*
* Copyright 1999-2006 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.
*/
#include "globus_xio.h"
#include "globus_io.h"
#include "globus_xio_tcp_driver.h"
#include "globus_i_gridftp_server.h"
#include "version.h"
#include "globus_i_gfs_ipc.h"
#ifndef TARGET_ARCH_WIN32
#include <grp.h>
#include <sys/wait.h>
#endif
#ifdef TARGET_ARCH_WIN32
#define S_ISLNK(x) 0
#define lstat(x,y) stat(x,y)
#define mkdir(x,y) mkdir(x)
#define chown(x,y,z) -1
#define symlink(x,y) -1
#define readlink(x,y,z) 0
#define realpath(x,y) strcpy(y,x)
#define scandir(a,b,c,d) 0
#define alphasort(x,y) 0
#endif
#ifdef TARGET_ARCH_WIN32
#define lstat(x,y) stat(x,y)
#define S_ISLNK(x) 0
#define getuid() 1
#define getpwuid(x) 0
#define initgroups(x,y) -1
#define getgroups(x,y) -1
#define setgroups(x,y) 0
#define setgid(x) 0
#define setuid(x) 0
#define sync() 0
#define fork() -1
#define setsid() -1
#define chroot(x) -1
#define globus_libc_getpwnam_r(a,b,c,d,e) -1
#define globus_libc_getpwuid_r(a,b,c,d,e) -1
#endif
#ifdef TARGET_ARCH_WIN32
#define getpwnam(x) 0
#define getgrgid(x) 0
#define getgrnam(x) 0
#define lstat(x,y) stat(x,y)
#define S_ISLNK(x) 0
#endif
#include <signal.h>
#include "globus_preload.h"
static globus_cond_t globus_l_gfs_cond;
static globus_mutex_t globus_l_gfs_mutex;
static globus_bool_t globus_l_gfs_terminated = GLOBUS_FALSE;
static unsigned int globus_l_gfs_outstanding = 0;
static globus_xio_driver_t globus_l_gfs_tcp_driver = GLOBUS_NULL;
static globus_xio_server_t globus_l_gfs_xio_server = GLOBUS_NULL;
static globus_bool_t globus_l_gfs_xio_server_accepting;
static globus_xio_attr_t globus_l_gfs_xio_attr;
static globus_bool_t globus_l_gfs_exit = GLOBUS_FALSE;
static globus_bool_t globus_l_gfs_sigint_caught = GLOBUS_FALSE;
static char ** globus_l_gfs_child_argv = NULL;
static int globus_l_gfs_child_argc = 0;
#ifndef BUILD_LITE
#define GLOBUS_L_GFS_SIGCHLD_DELAY 10
static globus_callback_handle_t globus_l_gfs_sigchld_periodic_handle =
GLOBUS_NULL_HANDLE;
#endif
static
globus_result_t
globus_l_gfs_open_new_server(
globus_xio_handle_t handle);
static
void
globus_l_gfs_server_closed(
void * user_arg,
globus_object_t * error);
static
void
globus_i_gfs_connection_closed();
static
void
globus_l_gfs_close_cb(
globus_xio_handle_t handle,
globus_result_t result,
void * user_arg);
static
void
globus_l_gfs_server_accept_cb(
globus_xio_server_t server,
globus_xio_handle_t handle,
globus_result_t result,
void * user_arg);
static
void
globus_l_gfs_sigchld(
void * user_arg);
static
void
globus_l_gfs_bad_signal_handler(
int signum)
{
GlobusGFSName(globus_l_gfs_bad_signal_handler);
GlobusGFSDebugEnter();
/*
globus_gfs_log_message(
GLOBUS_GFS_LOG_ERR,
_GSSL("an unexpected signal occured: %d\n"),
signum);
*/
if(!globus_l_gfs_exit)
{
signal(signum, SIG_DFL);
raise(signum);
}
else
{
exit(signum);
}
GlobusGFSDebugExit();
}
static
void
globus_l_gfs_server_close_cb(
globus_xio_server_t server,
void * user_arg)
{
globus_mutex_lock(&globus_l_gfs_mutex);
{
globus_l_gfs_outstanding--;
globus_l_gfs_xio_server = GLOBUS_NULL;
globus_cond_signal(&globus_l_gfs_cond);
}
globus_mutex_unlock(&globus_l_gfs_mutex);
}
static
void
globus_l_gfs_sigint(
void * user_arg)
{
globus_result_t res;
GlobusGFSName(globus_l_gfs_sigint);
GlobusGFSDebugEnter();
globus_gfs_log_message(
GLOBUS_GFS_LOG_ERR,
"Server is shutting down...\n");
globus_gfs_log_event(
GLOBUS_GFS_LOG_ERR,
GLOBUS_GFS_LOG_EVENT_START,
"cleanup",
0,
NULL);
globus_mutex_lock(&globus_l_gfs_mutex);
{
if(globus_l_gfs_sigint_caught)
{
globus_gfs_config_set_int(
"open_connections_count", 0);
globus_gfs_log_message(
GLOBUS_GFS_LOG_ERR,
"Forcing unclean shutdown.\n");
globus_gfs_log_event(
GLOBUS_GFS_LOG_ERR,
GLOBUS_GFS_LOG_EVENT_END,
"cleanup",
0,
"msg=\"Forcing unclean shutdown.\"");
}
if(globus_l_gfs_xio_server)
{
res = globus_xio_server_register_close(
globus_l_gfs_xio_server, globus_l_gfs_server_close_cb, NULL);
if(res == GLOBUS_SUCCESS)
{
globus_l_gfs_outstanding++;
}
else
{
globus_l_gfs_xio_server = GLOBUS_NULL;
}
}
globus_l_gfs_sigint_caught = GLOBUS_TRUE;
globus_l_gfs_terminated = GLOBUS_TRUE;
if(globus_gfs_config_get_int("open_connections_count") == 0)
{
globus_cond_signal(&globus_l_gfs_cond);
globus_gfs_log_event(
GLOBUS_GFS_LOG_ERR,
GLOBUS_GFS_LOG_EVENT_END,
"cleanup",
0,
NULL);
}
else
{
if(!globus_i_gfs_config_bool("data_node"))
{
globus_i_gfs_control_stop();
}
globus_i_gfs_ipc_stop();
if(globus_i_gfs_config_bool("daemon"))
{
globus_l_gfs_sigchld(user_arg);
}
}
}
globus_mutex_unlock(&globus_l_gfs_mutex);
GlobusGFSDebugExit();
}
static
void
globus_l_gfs_sighup(
void * user_arg)
{
int argc;
char ** argv;
GlobusGFSName(globus_l_gfs_sighup);
GlobusGFSDebugEnter();
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"Reloading config...\n");
argv = (char **) globus_i_gfs_config_get("argv");
argc = globus_i_gfs_config_int("argc");
globus_i_gfs_config_init(argc, argv, GLOBUS_FALSE);
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"Done reloading config.\n");
GlobusGFSDebugExit();
}
static
void
globus_l_gfs_sigchld(
void * user_arg)
{
int child_pid;
int child_status;
int child_rc;
GlobusGFSName(globus_l_gfs_sigchld);
GlobusGFSDebugEnter();
#ifndef TARGET_ARCH_WIN32
while(globus_gfs_config_get_int("open_connections_count") > 0 &&
(child_pid = waitpid(-1, &child_status, WNOHANG)) > 0)
{
if(WIFEXITED(child_status))
{
child_rc = WEXITSTATUS(child_status);
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"Child process %d ended with rc = %d\n",
child_pid,
child_rc);
globus_gfs_log_event(
GLOBUS_GFS_LOG_INFO,
GLOBUS_GFS_LOG_EVENT_END,
"child",
0,
"c.id=%d exitcode=%d",
child_pid,
child_rc);
}
else if(WIFSIGNALED(child_status))
{
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"Child process %d killed by signal %d\n",
child_pid,
WTERMSIG(child_status));
globus_gfs_log_event(
GLOBUS_GFS_LOG_INFO,
GLOBUS_GFS_LOG_EVENT_END,
"child",
0,
"c.id=%d signal=%d",
child_pid,
WTERMSIG(child_status));
}
globus_mutex_lock(&globus_l_gfs_mutex);
{
globus_i_gfs_connection_closed();
}
globus_mutex_unlock(&globus_l_gfs_mutex);
}
#endif
GlobusGFSDebugExit();
}
static
void
globus_l_gfs_signal_init()
{
GlobusGFSName(globus_l_gfs_signal_init);
GlobusGFSDebugEnter();
# ifdef SIGINT
globus_callback_register_signal_handler(
SIGINT,
GLOBUS_TRUE,
globus_l_gfs_sigint,
NULL);
# endif
# ifdef SIGHUP
if(globus_i_gfs_config_string("loaded_config"))
{
globus_callback_register_signal_handler(
SIGHUP,
GLOBUS_TRUE,
globus_l_gfs_sighup,
NULL);
}
# endif
# ifdef SIGKILL
{
/* XXX //signal(SIGKILL, globus_l_gfs_bad_signal_handler); */
}
# endif
# ifdef SIGSEGV
{
signal(SIGSEGV, globus_l_gfs_bad_signal_handler);
}
# endif
# ifdef SIGABRT
{
signal(SIGABRT, globus_l_gfs_bad_signal_handler);
}
# endif
# ifdef SIGBUS
{
signal(SIGBUS, globus_l_gfs_bad_signal_handler);
}
# endif
# ifdef SIGFPE
{
signal(SIGFPE, globus_l_gfs_bad_signal_handler);
}
# endif
# ifdef SIGILL
{
signal(SIGILL, globus_l_gfs_bad_signal_handler);
}
# endif
# ifdef SIGIOT
{
signal(SIGIOT, globus_l_gfs_bad_signal_handler);
}
# endif
# ifdef SIGPIPE
{
/* XXX //signal(SIGPIPE, globus_l_gfs_bad_signal_handler); */
}
# endif
# ifdef SIGEMT
{
signal(SIGEMT, globus_l_gfs_bad_signal_handler);
}
# endif
# ifdef SIGSYS
{
signal(SIGSYS, globus_l_gfs_bad_signal_handler);
}
# endif
# ifdef SIGTRAP
{
signal(SIGTRAP, globus_l_gfs_bad_signal_handler);
}
# endif
# ifdef SIGSTOP
{
/* XXX //signal(SIGSTOP, globus_l_gfs_bad_signal_handler); */
}
# endif
GlobusGFSDebugExit();
}
/* called locked */
static
globus_result_t
globus_l_gfs_spawn_child(
globus_xio_handle_t handle)
{
globus_result_t result;
pid_t child_pid;
globus_xio_system_socket_t socket_handle;
int rc;
GlobusGFSName(globus_l_gfs_spawn_child);
GlobusGFSDebugEnter();
result = globus_xio_handle_cntl(
handle,
globus_l_gfs_tcp_driver,
GLOBUS_XIO_TCP_GET_HANDLE,
&socket_handle);
if(result != GLOBUS_SUCCESS)
{
globus_gfs_log_result(
GLOBUS_GFS_LOG_ERR,
_GSSL("Could not get handle from daemon process"),
result);
goto error;
}
child_pid = fork();
if(child_pid == 0)
{
if(globus_l_gfs_xio_server)
{
result = globus_xio_server_register_close(
globus_l_gfs_xio_server, globus_l_gfs_server_close_cb, NULL);
if(result == GLOBUS_SUCCESS)
{
globus_l_gfs_outstanding++;
}
else
{
globus_l_gfs_xio_server = GLOBUS_NULL;
}
}
rc = dup2(socket_handle, STDIN_FILENO);
if(rc == -1)
{
result = GlobusGFSErrorSystemError("dup2", errno);
globus_gfs_log_result(
GLOBUS_GFS_LOG_ERR,
_GSSL("Could not open new handle for child process"),
result);
goto child_error;
}
close(socket_handle);
/* exec the process */
if(*globus_l_gfs_child_argv[0] == '/')
{
rc = execv(globus_l_gfs_child_argv[0], globus_l_gfs_child_argv);
}
else
{
rc = execvp(globus_l_gfs_child_argv[0], globus_l_gfs_child_argv);
}
if(rc == -1)
{
char * error_msg;
error_msg = globus_common_create_string("%s\n%s\n%s\n%s",
_GSSL("Could not exec child process."),
_GSSL("Please verify that a gridftp server is located at: "),
globus_l_gfs_child_argv[0],
_GSSL("Or try the -exec flag."));
result = GlobusGFSErrorSystemError("execv", errno);
globus_gfs_log_result(GLOBUS_GFS_LOG_ERR, error_msg, result);
goto child_close_error;
}
}
else if(child_pid == -1)
{
result = GlobusGFSErrorSystemError("fork", errno);
goto child_error;
}
else
{
globus_gfs_log_event(
GLOBUS_GFS_LOG_INFO,
GLOBUS_GFS_LOG_EVENT_START,
"child",
0,
"c.id=%d",
child_pid);
/* inc the connection count 2 here since we will dec it on this close
and on the death of the child process */
globus_gfs_config_inc_int("open_connections_count", 2);
globus_mutex_unlock(&globus_l_gfs_mutex);
result = globus_xio_register_close(
handle,
NULL,
globus_l_gfs_close_cb,
NULL);
if(result != GLOBUS_SUCCESS)
{
globus_l_gfs_close_cb(handle, result, NULL);
}
}
GlobusGFSDebugExit();
return GLOBUS_SUCCESS;
child_close_error:
close(STDIN_FILENO);
close(socket_handle);
child_error:
exit(1);
error:
GlobusGFSDebugExitWithError();
return result;
}
static
void
globus_i_gfs_connection_closed()
{
GlobusGFSName(globus_i_gfs_connection_closed);
GlobusGFSDebugEnter();
globus_gfs_config_inc_int("open_connections_count", -1);
if(globus_l_gfs_terminated || globus_i_gfs_config_bool("single"))
{
if(globus_gfs_config_get_int("open_connections_count") == 0)
{
globus_l_gfs_terminated = GLOBUS_TRUE;
globus_cond_signal(&globus_l_gfs_cond);
}
}
GlobusGFSDebugExit();
}
static
void
globus_l_gfs_close_cb(
globus_xio_handle_t handle,
globus_result_t result,
void * user_arg)
{
GlobusGFSName(globus_l_gfs_close_cb);
GlobusGFSDebugEnter();
globus_mutex_lock(&globus_l_gfs_mutex);
{
globus_i_gfs_connection_closed();
}
globus_mutex_unlock(&globus_l_gfs_mutex);
GlobusGFSDebugExit();
}
static
void
globus_l_gfs_ipc_closed(
void * user_arg,
globus_result_t result)
{
globus_xio_handle_t handle;
if(result != GLOBUS_SUCCESS)
{
/* XXX TODO log and error */
}
handle = (globus_xio_handle_t) user_arg;
globus_mutex_unlock(&globus_l_gfs_mutex);
{
result = globus_xio_register_close(
handle,
NULL,
globus_l_gfs_close_cb,
NULL);
}
globus_mutex_unlock(&globus_l_gfs_mutex);
if(result != GLOBUS_SUCCESS)
{
globus_l_gfs_close_cb(handle, result, NULL);
}
}
static
void
globus_l_gfs_new_server_cb(
globus_xio_handle_t handle,
globus_result_t result,
void * user_arg)
{
globus_xio_system_socket_t system_handle;
char * remote_ip;
char * remote_contact;
char * local_contact;
char * tmp_local_contact = NULL;
GlobusGFSName(globus_l_gfs_new_server_cb);
GlobusGFSDebugEnter();
globus_mutex_lock(&globus_l_gfs_mutex);
{
if(result != GLOBUS_SUCCESS || globus_l_gfs_terminated)
{
goto error;
}
result = globus_xio_handle_cntl(
handle,
globus_l_gfs_tcp_driver,
GLOBUS_XIO_TCP_GET_REMOTE_NUMERIC_CONTACT,
&remote_ip);
if(result != GLOBUS_SUCCESS)
{
char * tmp;
if(globus_i_gfs_config_bool("ssh") &&
(tmp = getenv("SSH_CLIENT")) != NULL)
{
remote_ip = strdup(tmp);
tmp = strchr(remote_ip, ' ');
if(tmp)
{
*tmp = ':';
tmp = strchr(remote_ip, ' ');
if(tmp)
{
*tmp = '\0';
}
}
remote_contact = strdup(remote_ip);
}
else
{
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"Couldn't get remote IP address. "
"Possibly using a non-tcp protocol.\n");
remote_ip = strdup("0.0.0.0");
remote_contact = strdup(remote_ip);
}
}
else
{
result = globus_xio_handle_cntl(
handle,
globus_l_gfs_tcp_driver,
GLOBUS_XIO_TCP_GET_REMOTE_CONTACT,
&remote_contact);
if(result != GLOBUS_SUCCESS)
{
globus_gfs_log_result(
GLOBUS_GFS_LOG_INFO,
"Couldn't resolve hostname from remote IP address",
result);
remote_contact = strdup(remote_ip);
}
}
if(!globus_i_gfs_config_allow_addr(remote_ip, GLOBUS_FALSE))
{
globus_gfs_log_message(
GLOBUS_GFS_LOG_WARN,
"Connection disallowed by configuration from: %s\n",
remote_ip);
goto error;
}
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"New connection from: %s\n", remote_contact);
globus_gfs_log_event(
GLOBUS_GFS_LOG_INFO,
GLOBUS_GFS_LOG_EVENT_START,
"session",
0,
"remotehost=%s", remote_contact);
result = globus_xio_handle_cntl(
handle,
globus_l_gfs_tcp_driver,
GLOBUS_XIO_TCP_GET_LOCAL_NUMERIC_CONTACT,
&local_contact);
if(result != GLOBUS_SUCCESS)
{
char * tmp;
if(globus_i_gfs_config_bool("ssh") &&
(tmp = getenv("SSH_CONNECTION")) != NULL)
{
local_contact = NULL;
tmp = strchr(tmp, ' ');
if(tmp)
{
tmp++;
tmp = strchr(tmp, ' ');
if(tmp)
{
tmp++;
local_contact = strdup(tmp);
}
}
if(local_contact && (tmp = strchr(local_contact, ' ')) != NULL)
{
*tmp = ':';
}
else
{
local_contact = strdup("0.0.0.0");
}
}
else
{
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"Couldn't get local contact. "
"Possibly using a non-tcp protocol.\n");
local_contact = strdup("0.0.0.0");
}
}
if(globus_i_gfs_config_string("contact_string") == NULL)
{
/* GLOBUS_XIO_TCP_GET_LOCAL_NUMERIC_CONTACT result */
if(result != GLOBUS_SUCCESS)
{
tmp_local_contact = strdup(local_contact);
}
else
{
result = globus_xio_handle_cntl(
handle,
globus_l_gfs_tcp_driver,
GLOBUS_XIO_TCP_GET_LOCAL_CONTACT,
&tmp_local_contact);
if(result != GLOBUS_SUCCESS)
{
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"Couldn't get local contact. "
"Possibly using a non-tcp protocol.\n");
tmp_local_contact = strdup(local_contact);
}
}
globus_gfs_config_set_ptr("contact_string", tmp_local_contact);
}
result = globus_xio_handle_cntl(
handle,
globus_l_gfs_tcp_driver,
GLOBUS_XIO_TCP_SET_NODELAY,
GLOBUS_TRUE);
result = globus_xio_handle_cntl(
handle,
globus_l_gfs_tcp_driver,
GLOBUS_XIO_TCP_GET_HANDLE,
&system_handle);
if(result != GLOBUS_SUCCESS)
{
goto error_start;
}
if(globus_i_gfs_config_bool("data_node"))
{
result = globus_gfs_ipc_handle_create(
&globus_gfs_ipc_default_iface,
system_handle,
globus_l_gfs_ipc_closed,
handle);
}
else
{
result = globus_i_gfs_control_start(
handle,
system_handle,
remote_contact,
local_contact,
globus_l_gfs_server_closed,
(void *) (intptr_t) system_handle);
}
if(result != GLOBUS_SUCCESS)
{
globus_gfs_log_result(
GLOBUS_GFS_LOG_ERR, "Connection failed", result);
goto error_start;
}
}
globus_mutex_unlock(&globus_l_gfs_mutex);
globus_free(remote_ip);
globus_free(local_contact);
globus_free(remote_contact);
GlobusGFSDebugExit();
return;
error_start:
globus_free(remote_ip);
globus_free(remote_contact);
globus_free(local_contact);
error:
result = globus_xio_register_close(
handle,
NULL,
globus_l_gfs_close_cb,
NULL);
globus_mutex_unlock(&globus_l_gfs_mutex);
if(result != GLOBUS_SUCCESS)
{
globus_l_gfs_close_cb(handle, result, NULL);
}
GlobusGFSDebugExitWithError();
}
static
void
globus_l_gfs_reject_close_cb(
globus_xio_handle_t handle,
globus_result_t result,
void * user_arg)
{
globus_mutex_lock(&globus_l_gfs_mutex);
{
if(globus_i_gfs_config_bool("inetd"))
{
globus_i_gfs_connection_closed();
}
else
{
globus_l_gfs_outstanding--;
}
}
globus_mutex_unlock(&globus_l_gfs_mutex);
}
static
void
globus_l_gfs_reject_write_cb(
globus_xio_handle_t handle,
globus_result_t result,
globus_byte_t * buffer,
globus_size_t len,
globus_size_t nbytes,
globus_xio_data_descriptor_t data_desc,
void * user_arg)
{
globus_mutex_lock(&globus_l_gfs_mutex);
{
if(user_arg)
{
globus_free(user_arg);
}
result = globus_xio_register_close(
handle,
NULL,
globus_l_gfs_reject_close_cb,
NULL);
if(result != GLOBUS_SUCCESS)
{
if(globus_i_gfs_config_bool("inetd"))
{
globus_i_gfs_connection_closed();
}
else
{
globus_l_gfs_outstanding--;
}
}
}
globus_mutex_unlock(&globus_l_gfs_mutex);
}
static
void
globus_l_gfs_reject_open_cb(
globus_xio_handle_t handle,
globus_result_t result,
void * user_arg)
{
char * sorry_msg;
char * tmp_msg;
int len;
if(globus_gfs_config_get_int("connections_max") == 0 ||
globus_i_gfs_config_bool("connections_disabled"))
{
if((tmp_msg = globus_gfs_config_get_string("offline_msg")) != NULL)
{
sorry_msg = globus_common_create_string("422 %s\r\n", tmp_msg);
}
else
{
sorry_msg = globus_libc_strdup(
"422 Service temporarily offline. Closing control connection.\r\n");
}
}
else
{
sorry_msg = globus_libc_strdup(
"421 Service busy: Connection limit exceeded. Please try again "
"later. Closing control connection.\r\n");
}
len = strlen(sorry_msg);
globus_mutex_lock(&globus_l_gfs_mutex);
{
if(result != GLOBUS_SUCCESS)
{
goto error;
}
result = globus_xio_register_write(
handle,
(globus_byte_t *) sorry_msg,
len,
len,
NULL,
globus_l_gfs_reject_write_cb,
sorry_msg);
if(result != GLOBUS_SUCCESS)
{
goto error;
}
}
globus_mutex_unlock(&globus_l_gfs_mutex);
return;
error:
globus_free(sorry_msg);
result = globus_xio_register_close(
handle,
NULL,
globus_l_gfs_reject_close_cb,
NULL);
if(result != GLOBUS_SUCCESS)
{
globus_l_gfs_outstanding--;
}
globus_mutex_unlock(&globus_l_gfs_mutex);
}
/* begin new server, this is called locked and it is assumed that
the application is not in the termintated state */
static
globus_result_t
globus_l_gfs_open_new_server(
globus_xio_handle_t handle)
{
globus_result_t result;
GlobusGFSName(globus_l_gfs_open_new_server);
GlobusGFSDebugEnter();
if(globus_gfs_config_get_int("connections_max") == 0 ||
globus_i_gfs_config_bool("connections_disabled"))
{
result = globus_xio_register_open(
handle,
NULL,
globus_l_gfs_xio_attr,
globus_l_gfs_reject_open_cb,
NULL);
if(result != GLOBUS_SUCCESS)
{
goto error_open;
}
globus_gfs_config_inc_int("open_connections_count", 1);
}
else
{
/* dont need the handle here, will get it in callback too */
result = globus_xio_register_open(
handle,
NULL,
globus_l_gfs_xio_attr,
globus_l_gfs_new_server_cb,
NULL);
if(result != GLOBUS_SUCCESS)
{
goto error_open;
}
globus_gfs_config_inc_int("open_connections_count", 1);
}
GlobusGFSDebugExit();
return GLOBUS_SUCCESS;
error_open:
GlobusGFSDebugExitWithError();
return result;
}
static
globus_result_t
globus_l_gfs_prepare_stack(
globus_xio_stack_t * stack)
{
globus_result_t result;
GlobusGFSName(globus_l_gfs_prepare_stack);
GlobusGFSDebugEnter();
result = globus_xio_stack_init(stack, NULL);
if(result != GLOBUS_SUCCESS)
{
goto error;
}
result = globus_xio_stack_push_driver(*stack, globus_l_gfs_tcp_driver);
if(result != GLOBUS_SUCCESS)
{
goto error;
}
GlobusGFSDebugExit();
return GLOBUS_SUCCESS;
error:
GlobusGFSDebugExitWithError();
return result;
}
/* begin a server instance from the channel already connected on stdin,
this IS called locked, it is assume that the server has not been
terminated */
static
globus_result_t
globus_l_gfs_convert_inetd_handle(void)
{
globus_result_t result;
globus_xio_stack_t stack;
globus_xio_handle_t handle;
globus_xio_system_socket_t stdin_socket = STDIN_FILENO;
GlobusGFSName(globus_l_gfs_convert_inetd_handle);
GlobusGFSDebugEnter();
result = globus_l_gfs_prepare_stack(&stack);
if(result != GLOBUS_SUCCESS)
{
goto error;
}
#ifdef WIN32
{
WSAPROTOCOL_INFO sock_info;
DWORD dwRead;
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (!ReadFile
(hStdin, &(sock_info), sizeof(WSAPROTOCOL_INFO), &dwRead, NULL)
|| dwRead != sizeof(WSAPROTOCOL_INFO)) {
printf("Error reading socket\n");
goto error_stack;
}
if ((stdin_socket = WSASocket(FROM_PROTOCOL_INFO,
FROM_PROTOCOL_INFO,
FROM_PROTOCOL_INFO,
&(sock_info), 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET) {
printf("Error in creating socket :%d\n",
WSAGetLastError());
goto error_stack;
}
}
#endif
result = globus_xio_attr_cntl(
globus_l_gfs_xio_attr,
globus_l_gfs_tcp_driver,
GLOBUS_XIO_TCP_SET_HANDLE,
(globus_xio_system_socket_t) stdin_socket);
if(result != GLOBUS_SUCCESS)
{
goto error_stack;
}
result = globus_xio_handle_create(&handle, stack);
if(result != GLOBUS_SUCCESS)
{
goto error_stack;
}
result = globus_l_gfs_open_new_server(handle);
if(result != GLOBUS_SUCCESS)
{
goto error_stack;
}
globus_xio_stack_destroy(stack);
GlobusGFSDebugExit();
return GLOBUS_SUCCESS;
error_stack:
globus_xio_stack_destroy(stack);
error:
GlobusGFSDebugExitWithError();
return result;
}
/* a new client has connected */
static
void
globus_l_gfs_server_accept_cb(
globus_xio_server_t server,
globus_xio_handle_t handle,
globus_result_t result,
void * user_arg)
{
GlobusGFSName(globus_l_gfs_server_accept_cb);
GlobusGFSDebugEnter();
globus_mutex_lock(&globus_l_gfs_mutex);
{
globus_l_gfs_outstanding--;
globus_l_gfs_xio_server_accepting = GLOBUS_FALSE;
if(result != GLOBUS_SUCCESS)
{
goto error_accept;
}
/* if too many already open */
if((globus_gfs_config_get_int("connections_max") != -1 &&
globus_gfs_config_get_int("open_connections_count")
>= globus_gfs_config_get_int("connections_max")) ||
globus_i_gfs_config_bool("connections_disabled"))
{
result = globus_xio_register_open(
handle,
NULL,
NULL,
globus_l_gfs_reject_open_cb,
NULL);
if(result != GLOBUS_SUCCESS)
{
goto error_accept;
}
globus_l_gfs_outstanding++;
}
else
{
/* if we fail to actually open a connection with either method we
do not fail, just log that the connection failed */
if(globus_i_gfs_config_bool("daemon"))
{
result = globus_l_gfs_spawn_child(handle);
if(result != GLOBUS_SUCCESS)
{
globus_gfs_log_result(
GLOBUS_GFS_LOG_ERR, "Could not spawn a child", result);
result = GLOBUS_SUCCESS;
/* if fork fails (cygwin sometimes),
* attempt to run non-forked */
if(globus_i_gfs_config_bool("fork_fallback"))
{
result = globus_l_gfs_open_new_server(handle);
if(result != GLOBUS_SUCCESS)
{
globus_gfs_log_result(
GLOBUS_GFS_LOG_ERR,
_GSSL("Could not open new handle"),
result);
result = GLOBUS_SUCCESS;
}
}
}
}
else
{
result = globus_l_gfs_open_new_server(handle);
if(result != GLOBUS_SUCCESS)
{
globus_gfs_log_result(
GLOBUS_GFS_LOG_ERR,
_GSSL("Could not open new handle"),
result);
result = GLOBUS_SUCCESS;
}
}
}
/* be sure to close handle on server proc and close server on
* client proc (close on exec)
*
* need to handle proc exits and decrement the open server count
* to keep the limit in effect.
*
* win32 will have to simulate fork/exec... should i just do that
* for unix too?
*/
if(globus_i_gfs_config_bool("single"))
{
result = globus_xio_server_register_close(
globus_l_gfs_xio_server, globus_l_gfs_server_close_cb, NULL);
if(result == GLOBUS_SUCCESS)
{
globus_l_gfs_outstanding++;
}
else
{
globus_l_gfs_xio_server = GLOBUS_NULL;
}
}
else if(!globus_l_gfs_terminated)
{
result = globus_xio_server_register_accept(
globus_l_gfs_xio_server,
globus_l_gfs_server_accept_cb,
NULL);
if(result != GLOBUS_SUCCESS)
{
goto error_register_accept;
}
globus_l_gfs_outstanding++;
globus_l_gfs_xio_server_accepting = GLOBUS_TRUE;
}
}
globus_mutex_unlock(&globus_l_gfs_mutex);
GlobusGFSDebugExit();
return;
error_register_accept:
error_accept:
globus_l_gfs_terminated = GLOBUS_TRUE;
if(globus_gfs_config_get_int("open_connections_count") == 0)
{
globus_cond_signal(&globus_l_gfs_cond);
}
globus_mutex_unlock(&globus_l_gfs_mutex);
GlobusGFSDebugExitWithError();
}
/* start up a daemon which will spawn server instances */
static
globus_result_t
globus_l_gfs_be_daemon()
{
char * contact_string;
char * interface;
globus_result_t result;
globus_xio_stack_t stack;
globus_xio_attr_t attr;
GlobusGFSName(globus_l_gfs_be_daemon);
GlobusGFSDebugEnter();
#ifndef TARGET_ARCH_WIN32
result = globus_callback_register_signal_handler(
SIGCHLD,
GLOBUS_TRUE,
globus_l_gfs_sigchld,
NULL);
if(result != GLOBUS_SUCCESS)
{
goto error;
}
#ifndef BUILD_LITE
/* when threaded add a periodic callback to simulate the SIGCHLD signal, since
* many versions of LinuxThreads don't seem to pass that to right thread */
{
globus_reltime_t delay;
GlobusTimeReltimeSet(
delay, GLOBUS_L_GFS_SIGCHLD_DELAY, 0);
result = globus_callback_register_periodic(
&globus_l_gfs_sigchld_periodic_handle,
&delay,
&delay,
globus_l_gfs_sigchld,
NULL);
if(result != GLOBUS_SUCCESS)
{
goto error;
}
}
#endif
#endif /* !WIN32 */
result = globus_l_gfs_prepare_stack(&stack);
if(result != GLOBUS_SUCCESS)
{
goto error;
}
result = globus_xio_attr_init(&attr);
if(result != GLOBUS_SUCCESS)
{
goto stack_error;
}
if((interface = globus_i_gfs_config_string("control_interface")) != NULL)
{
result = globus_xio_attr_cntl(
attr,
globus_l_gfs_tcp_driver,
GLOBUS_XIO_TCP_SET_INTERFACE,
interface);
if(result != GLOBUS_SUCCESS)
{
goto attr_error;
}
}
result = globus_xio_attr_cntl(
attr,
globus_l_gfs_tcp_driver,
GLOBUS_XIO_TCP_SET_PORT,
globus_i_gfs_config_int("port"));
if(result != GLOBUS_SUCCESS)
{
goto attr_error;
}
result = globus_xio_attr_cntl(
attr,
globus_l_gfs_tcp_driver,
GLOBUS_XIO_TCP_SET_REUSEADDR,
GLOBUS_TRUE);
if(result != GLOBUS_SUCCESS)
{
goto attr_error;
}
result = globus_xio_server_create(&globus_l_gfs_xio_server, attr, stack);
if(result != GLOBUS_SUCCESS)
{
goto attr_error;
}
if(globus_i_gfs_config_bool("chdir"))
{
char * chdir_to;
chdir_to = globus_i_gfs_config_string("chdir_to");
if(chdir_to != NULL)
{
chdir(chdir_to);
}
else
{
chdir("/");
}
}
result = globus_xio_server_get_contact_string(
globus_l_gfs_xio_server,
&contact_string);
if(result != GLOBUS_SUCCESS)
{
goto server_error;
}
globus_gfs_config_set_ptr("contact_string", contact_string);
if(globus_i_gfs_config_int("port") == 0)
{
globus_libc_printf(_GSSL("Server listening at %s\n"), contact_string);
fflush(stdout);
}
result = globus_xio_server_register_accept(
globus_l_gfs_xio_server,
globus_l_gfs_server_accept_cb,
NULL);
if(result != GLOBUS_SUCCESS)
{
goto contact_error;
}
globus_l_gfs_outstanding++;
globus_l_gfs_xio_server_accepting = GLOBUS_TRUE;
globus_xio_stack_destroy(stack);
globus_xio_attr_destroy(attr);
GlobusGFSDebugExit();
return GLOBUS_SUCCESS;
contact_error:
globus_free(contact_string);
server_error:
globus_xio_server_close(globus_l_gfs_xio_server);
attr_error:
globus_xio_attr_destroy(attr);
stack_error:
globus_xio_stack_destroy(stack);
error:
GlobusGFSDebugExitWithError();
return result;
}
static
void
globus_l_gfs_server_build_child_args(
globus_bool_t detach)
{
int i;
int j;
char ** prog_argv;
GlobusGFSName(globus_l_gfs_server_build_child_args);
/* exec the process */
prog_argv = (char **) globus_i_gfs_config_get("argv");
for(i = 0; prog_argv[i] != NULL; i++)
{
}
globus_l_gfs_child_argv = (char **)
globus_calloc(1, sizeof(char *) * (i + 10));
if(globus_l_gfs_child_argv == NULL)
{
globus_result_t result;
char * error_msg;
error_msg = globus_common_create_string("%s",
_GSSL("Small malloc failed. Unrecoverable error."));
result = GlobusGFSErrorSystemError("malloc", errno);
globus_gfs_log_result(GLOBUS_GFS_LOG_ERR, error_msg, result);
exit(1);
}
if((globus_l_gfs_child_argv[0]=globus_i_gfs_config_string("exec")) == NULL)
{
globus_l_gfs_child_argv[0] = globus_i_gfs_config_string("exec_name");
}
for(i = 1, j = 1; prog_argv[i] != NULL; i++)
{
char * arg = prog_argv[i];
while(*arg == '-')
{
arg++;
}
if(!detach)
{
if(!strcmp(arg, "s") || !strcmp(arg, "daemon"))
{
continue;
}
}
if(!strcmp(arg, "S") || !strcmp(arg, "detach"))
{
continue;
}
if(!strcmp(arg, "config_base_path"))
{
continue;
}
globus_l_gfs_child_argv[j++] = prog_argv[i];
}
if(detach)
{
globus_l_gfs_child_argv[j++] = "-no-detach";
}
else
{
globus_l_gfs_child_argv[j++] = "-inetd";
}
if(globus_i_gfs_config_string("config_base_path"))
{
globus_l_gfs_child_argv[j++] = "-config-base-path";
globus_l_gfs_child_argv[j++] =
globus_i_gfs_config_string("config_base_path");
}
globus_l_gfs_child_argv[j] = NULL;
globus_l_gfs_child_argc = j;
}
static
void
globus_l_gfs_server_detached()
{
int rc;
pid_t pid;
GlobusGFSName(globus_l_gfs_server_detached);
globus_l_gfs_server_build_child_args(GLOBUS_TRUE);
pid = fork();
if(pid < 0)
{
}
/* if parent just end */
else if(pid != 0)
{
char * pidfile = globus_i_gfs_config_string("pidfile");
if(pidfile != NULL)
{
globus_result_t result;
char * pidfile_evaled;
result = globus_eval_path(pidfile, &pidfile_evaled);
if(result == GLOBUS_SUCCESS)
{
mode_t oldmask;
oldmask = umask(022);
FILE * fh = fopen(pidfile_evaled, "w");
umask(oldmask);
if (fh != NULL)
{
fprintf(fh, "%ld\n", (long) pid);
fclose(fh);
}
}
}
exit(0);
}
/* if child */
else
{
setsid();
freopen("/dev/null", "w+", stdin);
freopen("/dev/null", "w+", stdout);
freopen("/dev/null", "w+", stderr);
/* exec the process */
if(*globus_l_gfs_child_argv[0] == '/')
{
rc = execv(globus_l_gfs_child_argv[0], globus_l_gfs_child_argv);
}
else
{
rc = execvp(globus_l_gfs_child_argv[0], globus_l_gfs_child_argv);
}
if(rc == -1)
{
globus_result_t result;
char * error_msg;
error_msg = globus_common_create_string("%s\n%s\n%s\n%s",
_GSSL("Could not exec process."),
_GSSL("Please verify that a gridftp server is located at: "),
globus_l_gfs_child_argv[0],
_GSSL("Or try the -exec flag."));
result = GlobusGFSErrorSystemError("execv", errno);
globus_gfs_log_result(GLOBUS_GFS_LOG_ERR, error_msg, result);
exit(1);
}
}
}
static
void
gfs_l_add_acls()
{
globus_gfs_acl_module_t * mod;
char * value;
char * name;
globus_bool_t cas_found = GLOBUS_FALSE;
globus_extension_handle_t ext_handle;
globus_list_t * list = NULL;
int rc;
value = globus_i_gfs_config_string("acl");
if(value != NULL)
{
list = globus_list_from_string(value, ',', NULL);
while(!globus_list_empty(list))
{
name = (char *) globus_list_remove(&list, list);
if(strncmp(name, "cas", 3) == 0)
{
cas_found = GLOBUS_TRUE;
}
else
{
mod = (globus_gfs_acl_module_t *)
globus_extension_lookup(
&ext_handle,
GLOBUS_GFS_ACL_REGISTRY,
(void *) name);
if(mod != NULL)
{
globus_gfs_acl_add_module(mod);
}
else
{
char buf[256];
snprintf(buf, 256, "globus_gridftp_server_acl_%s", name);
buf[255] = 0;
rc = globus_extension_activate(buf);
if(rc != 0)
{
/* log error */
}
else
{
mod = (globus_gfs_acl_module_t *)
globus_extension_lookup(
&ext_handle,
GLOBUS_GFS_ACL_REGISTRY,
(void *) name);
if(mod != NULL)
{
globus_gfs_acl_add_module(mod);
}
}
}
}
}
}
/* maintain older parameter */
if(cas_found || globus_i_gfs_config_bool("cas"))
{
globus_gfs_acl_add_module(&globus_gfs_acl_cas_module);
}
}
static
char *
globus_l_gfs_get_inetd_contact()
{
globus_result_t result;
globus_sockaddr_t sock_name;
globus_socklen_t sock_len;
char * contact_string;
GlobusGFSName(globus_l_xio_tcp_contact_string);
sock_len = sizeof(sock_name);
result = globus_xio_system_socket_getsockname(
STDIN_FILENO, (struct sockaddr *) &sock_name, &sock_len);
if(result != GLOBUS_SUCCESS)
{
goto error;
}
result = globus_libc_addr_to_contact_string(
&sock_name,
0,
&contact_string);
if(result != GLOBUS_SUCCESS)
{
goto error;
}
return contact_string;
error:
return NULL;
}
int
main(
int argc,
char ** argv)
{
char * tmp_s;
int rc = 0;
char * config;
globus_bool_t inetd;
char * cs;
globus_result_t result;
GlobusGFSName(main);
LTDL_SET_PRELOADED_SYMBOLS();
/* drop supplemental groups */
if(getuid() == 0 && setgroups(0, NULL))
{
fprintf(stderr,
"Error: unable to drop supplemental group privileges.\n");
return 1;
}
/* seed for libraries (xio random port range) */
srand(time(NULL) * (getpid() + 1));
/* parse and set envs from config file before loading modules */
rc = globus_i_gfs_config_init_envs(argc, argv);
if(rc != 0)
{
fprintf(stderr, "Failed to initialize configuration. Exiting.\n");
return 2;
}
/* activate globus stuff */
if((rc = globus_module_activate(GLOBUS_COMMON_MODULE)) != GLOBUS_SUCCESS)
{
fprintf(stderr,
"Error: Failed to initialize GLOBUS_COMMON:\n%s",
globus_error_print_friendly(globus_error_peek(rc)));
goto error_activate;
}
if ((rc = globus_module_activate(GLOBUS_XIO_MODULE)) != GLOBUS_SUCCESS)
{
fprintf(stderr,
"Error: Failed to initialize GLOBUS_XIO:\n%s",
globus_error_print_friendly(globus_error_peek(rc)));
goto error_activate;
}
if ((rc = globus_module_activate(
GLOBUS_GRIDFTP_SERVER_MODULE)) != GLOBUS_SUCCESS)
{
fprintf(stderr,
"Error: Failed to initialize GLOBUS_GRIDFTP_SERVER:\n%s",
globus_error_print_friendly(globus_error_peek(rc)));
goto error_activate;
}
if ((rc = globus_module_activate(GLOBUS_USAGE_MODULE)) != GLOBUS_SUCCESS)
{
fprintf(stderr,
"Error: Failed to initialize GLOBUS_USAGE_MODULE:\n%s",
globus_error_print_friendly(globus_error_peek(rc)));
goto error_activate;
}
/* init all the server modules */
rc = globus_i_gfs_config_init(argc, argv, GLOBUS_FALSE);
if(rc != 0)
{
fprintf(stderr, "Failed to initialize configuration. Exiting.\n");
return 2;
}
globus_i_gfs_log_open();
globus_l_gfs_signal_init();
globus_i_gfs_data_init();
globus_gfs_ipc_init(!globus_i_gfs_config_bool("data_node"));
globus_i_gfs_control_init();
globus_i_gfs_brain_init(globus_l_gfs_data_brain_ready, NULL);
/* initialize global variables */
globus_mutex_init(&globus_l_gfs_mutex, GLOBUS_NULL);
globus_cond_init(&globus_l_gfs_cond, GLOBUS_NULL);
globus_gfs_config_set_int("open_connections_count", 0);
globus_l_gfs_exit = globus_i_gfs_config_int("bad_signal_exit");
globus_l_gfs_xio_server = NULL;
/* if all the want is version info print and exit */
if(globus_i_gfs_config_bool("help"))
{
globus_i_gfs_config_display_usage();
rc = 0;
goto error_ver;
}
else if(globus_i_gfs_config_bool("version"))
{
globus_version_print(
local_package_name,
&local_version,
stderr,
GLOBUS_TRUE);
rc = 0;
goto error_ver;
}
else if(globus_i_gfs_config_bool("versions"))
{
globus_version_print(
local_package_name,
&local_version,
stderr,
GLOBUS_TRUE);
globus_module_print_activated_versions(
stderr,
GLOBUS_TRUE);
rc = 0;
goto error_ver;
}
#if !defined(BUILD_LITE) && defined(TARGET_ARCH_LINUX)
{
char buf[256];
buf[0] = '\0';
#if defined(_CS_GNU_LIBPTHREAD_VERSION)
confstr(_CS_GNU_LIBPTHREAD_VERSION, buf, sizeof(buf));
#endif
if((strstr(buf, "linuxthreads") || buf[0] == '\0') &&
(!globus_i_gfs_config_bool("ignore_bad_threads") &&
getuid() == 0))
{
fprintf(stderr,
"For security reasons, running as root with LinuxThreads \n"
"is not supported. Please use a non-threaded flavor, update \n"
"your libc libraries, and/or unset the LD_ASSUME_KERNEL\n"
"environment variable. \n"
"(confstr = %s)\n", buf);
rc = -1;
goto error_ver;
}
}
#endif
/* detach from the terminal if we need to */
if(globus_i_gfs_config_bool("detach"))
{
globus_l_gfs_server_detached();
}
/* XXX we're missing this */
if(globus_i_gfs_config_bool("detach"))
{
if(globus_i_gfs_config_bool("chdir"))
{
char * chdir_to;
chdir_to = globus_i_gfs_config_string("chdir_to");
if(chdir_to != NULL)
{
chdir(chdir_to);
}
else
{
chdir("/");
}
}
}
gfs_l_add_acls();
tmp_s = globus_i_gfs_config_string("extension");
if(tmp_s != NULL)
{
char * ext_name;
char * ext_args = NULL;
ext_name = strdup(tmp_s);
tmp_s = strchr(ext_name, ':');
if(tmp_s != NULL)
{
*tmp_s = '\0';
tmp_s++;
if(*tmp_s != '\0')
{
ext_args = strdup(tmp_s);
globus_gfs_config_set_ptr("extension_args", ext_args);
}
}
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"loading extension %s\n", ext_name);
rc = globus_extension_activate(ext_name);
if(rc != 0)
{
globus_gfs_log_message(
GLOBUS_GFS_LOG_ERR,
"failed to load loading extension %s\n", ext_name);
}
free(ext_name);
}
globus_mutex_lock(&globus_l_gfs_mutex);
{
result = globus_xio_driver_load("tcp", &globus_l_gfs_tcp_driver);
if(result != GLOBUS_SUCCESS)
{
rc = 1;
goto error_lock;
}
result = globus_xio_attr_init(&globus_l_gfs_xio_attr);
if(result != GLOBUS_SUCCESS)
{
rc = 1;
goto error_lock;
}
/* in theory we could have gotten a terminated already */
if(globus_l_gfs_terminated)
{
rc = 0;
goto error_lock;
}
config = globus_i_gfs_config_string("loaded_config");
inetd = globus_i_gfs_config_bool("inetd");
if(inetd)
{
if(globus_i_gfs_config_bool("ssh"))
{
struct stat statbuf;
rc = fstat(STDIN_FILENO, &statbuf);
if(rc == 0 && S_ISFIFO(statbuf.st_mode))
{
/* using pipe driver */
}
else /* if(S_ISSOCK(statbuf.st_mode)) */
{
freopen("/dev/null", "w+", stderr);
}
}
#ifndef WIN32
freopen("/dev/null", "w+", stderr);
#endif
}
if(config)
{
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"Configuration read from %s.\n", config);
}
else
{
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"No configuration file found.\n");
}
globus_gfs_log_event(
GLOBUS_GFS_LOG_INFO,
GLOBUS_GFS_LOG_EVENT_START,
"configuration",
0,
NULL);
globus_gfs_log_event(
GLOBUS_GFS_LOG_INFO,
GLOBUS_GFS_LOG_EVENT_END,
"configuration",
0,
"configfile=%s",
config ? config : "none");
if(inetd)
{
result = globus_l_gfs_convert_inetd_handle();
if(result != GLOBUS_SUCCESS)
{
rc = 1;
goto error_lock;
}
}
else
{
globus_l_gfs_server_build_child_args(GLOBUS_FALSE);
result = globus_l_gfs_be_daemon();
if(result != GLOBUS_SUCCESS)
{
rc = 1;
goto error_lock;
}
}
cs = globus_i_gfs_config_string("contact_string");
if(cs == NULL)
{
cs = globus_l_gfs_get_inetd_contact();
}
globus_gfs_log_message(
GLOBUS_GFS_LOG_INFO,
"Server started in %s mode.\n",
inetd ? (globus_i_gfs_config_bool("ssh") ? "ssh" : "inetd") : "daemon");
globus_gfs_log_event(
GLOBUS_GFS_LOG_INFO,
GLOBUS_GFS_LOG_EVENT_START,
NULL,
0,
"mode=%s%s%s",
inetd ? "inetd" : "daemon",
cs ? " contact=" : "",
cs ? cs : "");
/* run until we are done */
while(!globus_l_gfs_terminated ||
globus_gfs_config_get_int("open_connections_count") > 0 ||
globus_l_gfs_outstanding > 0)
{
globus_cond_wait(&globus_l_gfs_cond, &globus_l_gfs_mutex);
}
}
globus_mutex_unlock(&globus_l_gfs_mutex);
globus_xio_attr_destroy(globus_l_gfs_xio_attr);
globus_xio_driver_unload(globus_l_gfs_tcp_driver);
globus_gfs_log_event(
GLOBUS_GFS_LOG_ERR,
GLOBUS_GFS_LOG_EVENT_END,
NULL,
0,
NULL);
globus_i_gfs_log_close();
#ifndef BUILD_LITE
if(globus_l_gfs_sigchld_periodic_handle != GLOBUS_NULL_HANDLE)
{
globus_callback_unregister(
globus_l_gfs_sigchld_periodic_handle,
NULL,
NULL,
NULL);
globus_l_gfs_sigchld_periodic_handle = GLOBUS_NULL_HANDLE;
}
#endif
globus_module_deactivate_all();
GlobusGFSDebugExit();
return 0;
error_lock:
globus_gfs_log_result(
GLOBUS_GFS_LOG_ERR, _GSSL("Could not start server"), result);
globus_mutex_unlock(&globus_l_gfs_mutex);
error_ver:
globus_xio_attr_destroy(globus_l_gfs_xio_attr);
globus_xio_driver_unload(globus_l_gfs_tcp_driver);
globus_i_gfs_log_close();
error_activate:
globus_module_deactivate_all();
GlobusGFSDebugExitWithError();
return rc;
}
static
void
globus_l_gfs_server_closed(
void * user_arg,
globus_object_t * error)
{
GlobusGFSName(globus_l_gfs_server_closed);
GlobusGFSDebugEnter();
#ifdef WIN32
if(user_arg)
{
closesocket(user_arg);
}
#endif
globus_mutex_lock(&globus_l_gfs_mutex);
{
globus_i_gfs_connection_closed();
}
globus_mutex_unlock(&globus_l_gfs_mutex);
if(error != NULL)
{
char * tmp_str;
tmp_str = globus_error_print_friendly(error);
/* XXX find out why we get (false) error here */
globus_gfs_log_message(
GLOBUS_GFS_LOG_WARN,
"Control connection closed with error: %s\n",
tmp_str);
globus_free(tmp_str);
globus_object_free(error);
}
GlobusGFSDebugExit();
}
|