1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
|
/* -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 99; -*- */
/* vim: set ts=4 sw=4 et tw=99: */
/*
This file is part of Icecream.
Copyright (c) 2004 Stephan Kulow <coolo@suse.de>
2002, 2003 by Martin Pool <mbp@samba.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//#define ICECC_DEBUG 1
#ifndef _GNU_SOURCE
// getopt_long
#define _GNU_SOURCE 1
#endif
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <getopt.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <pwd.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/utsname.h>
#ifdef HAVE_ARPA_NAMESER_H
# include <arpa/nameser.h>
#endif
#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif
#include <arpa/inet.h>
#ifdef HAVE_RESOLV_H
# include <resolv.h>
#endif
#include <netdb.h>
#ifndef RUSAGE_SELF
# define RUSAGE_SELF (0)
#endif
#ifndef RUSAGE_CHILDREN
# define RUSAGE_CHILDREN (-1)
#endif
#ifdef HAVE_LIBCAP_NG
# include <cap-ng.h>
#endif
#include <archive.h>
#include <deque>
#include <map>
#include <algorithm>
#include <set>
#include <fstream>
#include <string>
#include "ncpus.h"
#include "exitcode.h"
#include "serve.h"
#include "workit.h"
#include "logging.h"
#include <comm.h>
#include "load.h"
#include "environment.h"
#include "platform.h"
#include "util.h"
#include "getifaddrs.h"
static std::string pidFilePath;
static volatile sig_atomic_t exit_main_loop = 0;
#ifndef __attribute_warn_unused_result__
#define __attribute_warn_unused_result__
#endif
using namespace std;
struct Client {
public:
/*
* UNKNOWN: Client was just created - not supposed to be long term
* GOTNATIVE: Client asked us for the native env - this is the first step
* PENDING_USE_CS: We have a CS from scheduler and need to tell the client
* as soon as there is a spot available on the local machine
* JOBDONE: This was compiled by a local client and we got a jobdone - awaiting END
* LINKJOB: This is a local job (aka link job) by a local client we told the scheduler about
* and await the finish of it
* TOINSTALL: We're receiving an environment transfer and wait for it to complete.
* WAITINSTALL: Client is waiting for the environment transfer unpacking child to finish.
* TOCOMPILE: We're supposed to compile it ourselves
* WAITFORCS: Client asked for a CS and we asked the scheduler - waiting for its answer
* WAITCOMPILE: Client got a CS and will ask him now (it's not me)
* CLIENTWORK: Client is busy working and we reserve the spot (job_id is set if it's a scheduler job)
* WAITFORCHILD: Client is waiting for the compile job to finish.
* WAITCREATEENV: We're waiting for icecc-create-env to finish.
*/
enum Status { UNKNOWN, GOTNATIVE, PENDING_USE_CS, JOBDONE, LINKJOB, TOINSTALL, WAITINSTALL, TOCOMPILE,
WAITFORCS, WAITCOMPILE, CLIENTWORK, WAITFORCHILD, WAITCREATEENV,
LASTSTATE = WAITCREATEENV
} status;
Client() {
job_id = 0;
channel = nullptr;
job = nullptr;
usecsmsg = nullptr;
client_id = 0;
status = UNKNOWN;
pipe_from_child = -1;
pipe_to_child = -1;
child_pid = -1;
}
static string status_str(Status status) {
switch (status) {
case UNKNOWN:
return "unknown";
case GOTNATIVE:
return "gotnative";
case PENDING_USE_CS:
return "pending_use_cs";
case JOBDONE:
return "jobdone";
case LINKJOB:
return "linkjob";
case TOINSTALL:
return "toinstall";
case WAITINSTALL:
return "waitinstall";
case TOCOMPILE:
return "tocompile";
case WAITFORCS:
return "waitforcs";
case CLIENTWORK:
return "clientwork";
case WAITCOMPILE:
return "waitcompile";
case WAITFORCHILD:
return "waitforchild";
case WAITCREATEENV:
return "waitcreateenv";
}
assert(false);
return string(); // shutup gcc
}
~Client() {
status = (Status) - 1;
delete channel;
channel = nullptr;
delete usecsmsg;
usecsmsg = nullptr;
delete job;
job = nullptr;
if (pipe_from_child >= 0) {
if (-1 == close(pipe_from_child) && (errno != EBADF)){
log_perror("Failed to close pipe from child process");
}
}
if (pipe_to_child >= 0) {
if (-1 == close(pipe_to_child) && (errno != EBADF)){
log_perror("Failed to close pipe to child process");
}
}
}
uint32_t job_id;
string outfile; // only useful for LINKJOB or TOINSTALL/WAITINSTALL
MsgChannel *channel;
UseCSMsg *usecsmsg;
CompileJob *job;
int client_id;
// pipe from child process with end status, only valid if WAITFORCHILD or TOINSTALL/WAITINSTALL
int pipe_from_child;
// pipe to child process, only valid if TOINSTALL/WAITINSTALL
int pipe_to_child;
pid_t child_pid;
string pending_create_env; // only for WAITCREATEENV
string dump() const {
string ret = status_str(status) + " " + channel->dump();
switch (status) {
case LINKJOB:
return ret + " ClientID: " + toString(client_id) + " " + outfile + " PID: " + toString(child_pid);
case TOINSTALL:
case WAITINSTALL:
return ret + " ClientID: " + toString(client_id) + " " + outfile + " PID: " + toString(child_pid);
case WAITFORCHILD:
return ret + " ClientID: " + toString(client_id) + " PID: " + toString(child_pid) + " PFD: " + toString(pipe_from_child);
case WAITCREATEENV:
return ret + " " + toString(client_id) + " " + pending_create_env;
default:
if (job_id) {
string jobs;
if (usecsmsg) {
jobs = " CompileServer: " + usecsmsg->hostname;
}
return ret + " ClientID: " + toString(client_id) + " Job ID: " + toString(job_id) + jobs;
} else {
return ret + " ClientID: " + toString(client_id);
}
}
return ret;
}
};
class Clients : public map<MsgChannel*, Client*>
{
public:
Clients() {
active_processes = 0;
}
unsigned int active_processes;
Client *find_by_client_id(int id) const {
for (auto it : *this)
if (it.second->client_id == id) {
return it.second;
}
return nullptr;
}
Client *find_by_channel(MsgChannel *c) const {
const_iterator it = find(c);
if (it == end()) {
return nullptr;
}
return it->second;
}
Client *find_by_pid(pid_t pid) const {
for (auto it : *this)
if (it.second->child_pid == pid) {
return it.second;
}
return nullptr;
}
Client *first() {
iterator it = begin();
if (it == end()) {
return nullptr;
}
Client *cl = it->second;
return cl;
}
string dump_status(Client::Status s) const {
int count = 0;
for (auto it : *this) {
if (it.second->status == s) {
count++;
}
}
if (count) {
return toString(count) + " " + Client::status_str(s) + ", ";
}
return string();
}
string dump_per_status() const {
string s;
for (Client::Status i = Client::UNKNOWN; i <= Client::LASTSTATE;
i = Client::Status(int(i) + 1)) {
s += dump_status(i);
}
return s;
}
Client *get_earliest_client(Client::Status s) const {
// TODO: possibly speed this up in adding some sorted lists
Client *client = nullptr;
int min_client_id = 0;
for (auto it : *this) {
if (it.second->status == s && (!min_client_id || min_client_id > it.second->client_id)) {
client = it.second;
min_client_id = client->client_id;
}
}
return client;
}
};
static int set_new_pgrp()
{
/* If we're a session group leader, then we are not able to call
* setpgid(). However, setsid will implicitly have put us into a new
* process group, so we don't have to do anything. */
/* Does everyone have getpgrp()? It's in POSIX.1. We used to call
* getpgid(0), but that is not available on BSD/OS. */
int pgrp_id = getpgrp();
if (-1 == pgrp_id){
log_perror("Failed to get process group ID");
return EXIT_DISTCC_FAILED;
}
if (pgrp_id == getpid()) {
trace() << "already a process group leader\n";
return 0;
}
if (setpgid(0, 0) == 0) {
trace() << "entered process group\n";
return 0;
}
trace() << "setpgid(0, 0) failed: " << strerror(errno) << endl;
return EXIT_DISTCC_FAILED;
}
static void dcc_daemon_terminate(int);
/**
* Catch all relevant termination signals. Set up in parent and also
* applies to children.
**/
void dcc_daemon_catch_signals()
{
/* SIGALRM is caught to allow for built-in timeouts when running test
* cases. */
signal(SIGTERM, &dcc_daemon_terminate);
signal(SIGINT, &dcc_daemon_terminate);
signal(SIGALRM, &dcc_daemon_terminate);
}
pid_t dcc_master_pid;
/**
* Called when a daemon gets a fatal signal.
*
* Some cleanup is done only if we're the master/parent daemon.
**/
static void dcc_daemon_terminate(int whichsig)
{
/**
* This is a signal handler. don't do stupid stuff.
* Don't call printf. and especially don't call the log_*() functions.
*/
if (exit_main_loop > 1) {
// The > 1 is because we get one more signal from the kill(0,...) below.
// hmm, we got killed already twice. try better
static const char msg[] = "forced exit.\n";
ignore_result(write(STDERR_FILENO, msg, strlen( msg )));
_exit(1);
}
// make BSD happy
signal(whichsig, dcc_daemon_terminate);
bool am_parent = (getpid() == dcc_master_pid);
if (am_parent && exit_main_loop == 0) {
/* kill whole group */
kill(0, whichsig);
/* Remove pid file */
unlink(pidFilePath.c_str());
}
++exit_main_loop;
}
void usage(const char *reason = nullptr)
{
if (reason) {
cerr << reason << endl;
}
cerr << "usage: iceccd [-n <netname>] [-m <max_processes>] [--no-remote] [-d|--daemonize] [-l logfile] [-s <schedulerhost[:port]>]"
" [-v[v[v]]] [-u|--user-uid <user_uid>] [-b <env-basedir>] [--cache-limit <MB>] [-N <node_name>] [-i|--interface <net_interface>] [-p|--port <port>]" << endl;
exit(1);
}
struct timeval last_stat;
// Initial rlimit for a compile job, measured in megabytes. Will vary with
// the amount of available memory.
int mem_limit = 100;
// Minimum rlimit for a compile job, measured in megabytes.
const int min_mem_limit = 100;
unsigned int max_kids = 0;
size_t cache_size_limit = 256 * 1024 * 1024;
struct NativeEnvironment {
string name; // the hash
// Timestamps for files including compiler binaries, if they have changed since the time
// the native env was built, it needs to be rebuilt.
map<string, time_t> filetimes;
time_t last_use;
size_t size; // tarball size
int create_env_pipe; // if in progress of creating the environment
NativeEnvironment() : last_use( 0 ), size( 0 ), create_env_pipe( 0 ) {}
};
struct ReceivedEnvironment {
ReceivedEnvironment() : last_use( 0 ), size( 0 ) {}
time_t last_use;
size_t size; // directory size
};
struct Daemon {
Clients clients;
// Installed environments received from other nodes. The key is
// (job->targetPlatform() + "/" job->environmentVersion()).
map<string, ReceivedEnvironment> received_environments;
// Map of native environments, the basic one(s) containing just the compiler
// and possibly more containing additional files (such as compiler plugins).
// The key is the compiler name and a concatenated list of the additional files
// (or just the compiler name for the basic ones).
map<string, NativeEnvironment> native_environments;
string envbasedir;
uid_t user_uid;
gid_t user_gid;
int warn_icecc_user_errno;
int tcp_listen_fd;
int tcp_listen_local_fd; // if tcp_listen is bound to a specific network interface, this one is bound to lo interface
int unix_listen_fd;
string machine_name;
string nodename;
bool noremote;
bool custom_nodename;
size_t cache_size;
map<int, MsgChannel *> fd2chan;
int new_client_id;
string remote_name;
time_t next_scheduler_connect;
unsigned long icecream_load;
struct timeval icecream_usage;
int current_load;
int num_cpus;
MsgChannel *scheduler;
DiscoverSched *discover;
string netname;
string schedname;
int scheduler_port;
string daemon_interface;
int daemon_port;
unsigned int supported_features;
int max_scheduler_pong;
int max_scheduler_ping;
unsigned int current_kids;
Daemon() {
warn_icecc_user_errno = 0;
if (getuid() == 0) {
struct passwd *pw = getpwnam("icecc");
if (pw) {
user_uid = pw->pw_uid;
user_gid = pw->pw_gid;
} else {
warn_icecc_user_errno = errno ? errno : ENOENT; // apparently errno can be 0 on error here
user_uid = 65534;
user_gid = 65533;
}
} else {
user_uid = getuid();
user_gid = getgid();
}
envbasedir = "/var/tmp/icecc-envs";
tcp_listen_fd = -1;
tcp_listen_local_fd = -1;
unix_listen_fd = -1;
new_client_id = 0;
next_scheduler_connect = 0;
cache_size = 0;
noremote = false;
custom_nodename = false;
icecream_load = 0;
icecream_usage.tv_sec = icecream_usage.tv_usec = 0;
current_load = - 1000;
num_cpus = 0;
scheduler = nullptr;
discover = nullptr;
scheduler_port = 8765;
daemon_interface = "";
daemon_port = 10245;
max_scheduler_pong = MAX_SCHEDULER_PONG;
max_scheduler_ping = MAX_SCHEDULER_PING;
current_kids = 0;
}
~Daemon() {
delete discover;
}
bool reannounce_environments() __attribute_warn_unused_result__;
void answer_client_requests();
bool handle_transfer_env(Client *client, EnvTransferMsg *msg) __attribute_warn_unused_result__;
bool handle_env_install_child_done(Client *client);
bool finish_transfer_env(Client *client, bool cancel = false);
bool handle_get_native_env(Client *client, GetNativeEnvMsg *msg) __attribute_warn_unused_result__;
bool finish_get_native_env(Client *client, string env_key);
void handle_old_request();
bool handle_compile_file(Client *client, Msg *msg) __attribute_warn_unused_result__;
bool handle_activity(Client *client) __attribute_warn_unused_result__;
bool handle_file_chunk_env(Client *client, Msg *msg) __attribute_warn_unused_result__;
void handle_end(Client *client, int exitcode);
int scheduler_get_internals() __attribute_warn_unused_result__;
void clear_children();
int scheduler_use_cs(UseCSMsg *msg) __attribute_warn_unused_result__;
int scheduler_no_cs(NoCSMsg *msg) __attribute_warn_unused_result__;
bool handle_get_cs(Client *client, Msg *msg) __attribute_warn_unused_result__;
bool handle_local_job(Client *client, Msg *msg) __attribute_warn_unused_result__;
bool handle_job_done(Client *cl, JobDoneMsg *m) __attribute_warn_unused_result__;
bool handle_compile_done(Client *client) __attribute_warn_unused_result__;
bool handle_verify_env(Client *client, VerifyEnvMsg *msg) __attribute_warn_unused_result__;
bool handle_blacklist_host_env(Client *client, Msg *msg) __attribute_warn_unused_result__;
int handle_cs_conf(ConfCSMsg *msg);
string dump_internals() const;
string determine_nodename();
void determine_system();
void determine_supported_features();
bool maybe_stats(bool force_check = false);
bool send_scheduler(const Msg &msg) __attribute_warn_unused_result__;
void close_scheduler();
bool reconnect();
int working_loop();
bool setup_listen_fds();
bool setup_listen_tcp_fd( int& fd, const string& interface );
bool setup_listen_unix_fd();
void check_cache_size(const string &new_env);
void remove_native_environment(const string& env_key);
void remove_environment(const string& env_key);
bool create_env_finished(string env_key);
};
bool Daemon::setup_listen_fds()
{
tcp_listen_fd = -1;
tcp_listen_local_fd = -1;
unix_listen_fd = -1;
if (!noremote) { // if we only listen to local clients, there is no point in going TCP
if( !setup_listen_tcp_fd( tcp_listen_fd, daemon_interface ))
return false;
// We should always listen on the loopback interface, so if we're binding only
// to a specific interface, bind also to the loopback.
if( !daemon_interface.empty()) {
if( !setup_listen_tcp_fd( tcp_listen_local_fd, "lo" ))
return false;
}
}
if( !setup_listen_unix_fd())
return false;
return true;
}
bool Daemon::setup_listen_tcp_fd( int& fd, const string& interface )
{
if( !interface.empty())
trace() << "starting to listen on interface " << interface << endl;
else
trace() << "starting to listen on all interfaces" << endl;
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
log_perror("Failed to create TCP listen socket.");
return false;
}
int optval = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
log_perror("Failed to set 'Reuse Address(SO_REUSEADDR)' option on TCP Listen Socket");
return false;
}
struct sockaddr_in myaddr;
if (!build_address_for_interface(myaddr, interface, daemon_port)) {
return false;
}
int count = 5;
while (count) {
if (::bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
log_perror("Failed to bind address to TCP listen socket");
sleep(2);
if (!--count) {
return false;
}
continue;
} else {
break;
}
}
if (listen(fd, 1024) < 0) {
log_perror("Failed to set TCP socket for listening to incoming connections");
return false;
}
fcntl(fd, F_SETFD, FD_CLOEXEC);
return true;
}
bool Daemon::setup_listen_unix_fd()
{
if ((unix_listen_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
log_perror("Failed to create a Unix scoket for listening");
return false;
}
struct sockaddr_un myaddr;
memset(&myaddr, 0, sizeof(myaddr));
myaddr.sun_family = AF_UNIX;
bool reset_umask = false;
mode_t old_umask = 0;
if (getenv("ICECC_TEST_SOCKET") == nullptr) {
#ifdef HAVE_LIBCAP_NG
// We run as system daemon (UID has been already changed).
if (capng_have_capability( CAPNG_EFFECTIVE, CAP_SYS_CHROOT )) {
#else
if (getuid() == 0) {
#endif
string default_socket = "/var/run/icecc/iceccd.socket";
strncpy(myaddr.sun_path, default_socket.c_str() , sizeof(myaddr.sun_path) - 1);
myaddr.sun_path[sizeof(myaddr.sun_path) - 1] = '\0';
if(default_socket.length() > sizeof(myaddr.sun_path) - 1) {
log_error() << "default socket path too long for sun_path" << endl;
}
if (-1 == unlink(myaddr.sun_path) && errno != ENOENT){
log_perror("unlink failed") << "\t" << myaddr.sun_path << endl;
}
old_umask = umask(0);
reset_umask = true;
} else { // Started by user.
if( getenv( "HOME" )) {
string socket_path = getenv("HOME");
socket_path.append("/.iceccd.socket");
strncpy(myaddr.sun_path, socket_path.c_str(), sizeof(myaddr.sun_path) - 1);
myaddr.sun_path[sizeof(myaddr.sun_path) - 1] = '\0';
if(socket_path.length() > sizeof(myaddr.sun_path) - 1) {
log_error() << "$HOME/.iceccd.socket path too long for sun_path" << endl;
}
if (-1 == unlink(myaddr.sun_path) && errno != ENOENT){
log_perror("unlink failed") << "\t" << myaddr.sun_path << endl;
}
} else {
log_error() << "launched by user, but $HOME not set" << endl;
return false;
}
}
} else {
string test_socket = getenv("ICECC_TEST_SOCKET");
strncpy(myaddr.sun_path, test_socket.c_str(), sizeof(myaddr.sun_path) - 1);
myaddr.sun_path[sizeof(myaddr.sun_path) - 1] = '\0';
if(test_socket.length() > sizeof(myaddr.sun_path) - 1) {
log_error() << "$ICECC_TEST_SOCKET path too long for sun_path" << endl;
}
if (-1 == unlink(myaddr.sun_path) && errno != ENOENT){
log_perror("unlink failed") << "\t" << myaddr.sun_path << endl;
}
old_umask = umask(0);
reset_umask = true;
}
if (::bind(unix_listen_fd, (struct sockaddr*)&myaddr, sizeof(myaddr)) < 0) {
log_perror("Failed to bind address to unix listen socket");
if (reset_umask) {
umask(old_umask);
}
return false;
}
if (reset_umask) {
umask(old_umask);
}
if (listen(unix_listen_fd, 1024) < 0) {
log_perror("Failed to set unix socket for listening");
return false;
}
fcntl(unix_listen_fd, F_SETFD, FD_CLOEXEC);
return true;
}
void Daemon::determine_system()
{
struct utsname uname_buf;
if (uname(&uname_buf)) {
log_perror("uname call failed. Unable to determine system node name and platform");
return;
}
if (nodename.length() && (nodename != uname_buf.nodename)) {
custom_nodename = true;
}
if (!custom_nodename) {
nodename = uname_buf.nodename;
}
machine_name = determine_platform();
}
string Daemon::determine_nodename()
{
if (custom_nodename && !nodename.empty()) {
return nodename;
}
// perhaps our host name changed due to network change?
struct utsname uname_buf;
if (!uname(&uname_buf)) {
nodename = uname_buf.nodename;
}
return nodename;
}
void Daemon::determine_supported_features()
{
supported_features = 0;
struct archive* a = archive_read_new();
static bool test_disable = false;
// Make one of the two remotes in tests say it doesn't support xz/zstd tarballs.
if( getenv( "ICECC_TESTS" ) != nullptr && nodename == "remoteice2" )
test_disable = true;
(void)test_disable;
#ifdef HAVE_LIBARCHIVE_XZ
if( !test_disable && archive_read_support_filter_xz(a) >= ARCHIVE_WARN ) // includes ARCHIVE_OK
supported_features = supported_features | NODE_FEATURE_ENV_XZ;
#endif
#ifdef HAVE_LIBARCHIVE_ZSTD
if( !test_disable && archive_read_support_filter_zstd(a) >= ARCHIVE_WARN ) // includes ARCHIVE_OK
supported_features = supported_features | NODE_FEATURE_ENV_ZSTD;
#endif
// sanity checks
if( archive_read_support_filter_gzip(a) < ARCHIVE_WARN ) // error
log_error() << "No support for uncompressing gzip available." << endl;
if( archive_read_support_format_tar(a) < ARCHIVE_WARN ) // error
log_error() << "No support for unpacking tar available." << endl;
archive_read_free(a);
}
bool Daemon::send_scheduler(const Msg& msg)
{
if (!scheduler) {
log_error() << "scheduler dead ?!" << endl;
return false;
}
if (!scheduler->send_msg(msg)) {
log_error() << "sending message to scheduler failed.." << endl;
close_scheduler();
return false;
}
return true;
}
bool Daemon::reannounce_environments()
{
log_info() << "reannounce_environments " << endl;
LoginMsg lmsg(0, nodename, "", supported_features);
lmsg.envs = available_environments(envbasedir);
return send_scheduler(lmsg);
}
void Daemon::close_scheduler()
{
if (!scheduler) {
return;
}
delete scheduler;
scheduler = nullptr;
delete discover;
discover = nullptr;
next_scheduler_connect = time(nullptr) + 20 + (rand() & 31);
static bool fast_reconnect = getenv( "ICECC_TESTS" ) != nullptr;
if( fast_reconnect )
next_scheduler_connect = time(nullptr) + 3;
}
bool Daemon::maybe_stats(bool force_check)
{
struct timeval now;
gettimeofday(&now, nullptr);
time_t diff_sent = (now.tv_sec - last_stat.tv_sec) * 1000 + (now.tv_usec - last_stat.tv_usec) / 1000;
if (diff_sent >= max_scheduler_pong * 1000 || force_check) {
StatsMsg msg;
unsigned int memory_fillgrade;
unsigned long idleLoad = 0;
unsigned long niceLoad = 0;
fill_stats(idleLoad, niceLoad, memory_fillgrade, &msg, clients.active_processes);
time_t diff_stat = (now.tv_sec - last_stat.tv_sec) * 1000 + (now.tv_usec - last_stat.tv_usec) / 1000;
last_stat = now;
/* icecream_load contains time in milliseconds we have used for icecream */
/* idle time could have been used for icecream, so claim it */
icecream_load += idleLoad * diff_stat / 1000;
/* add the time of our childrens, but only the time since the last run */
struct rusage ru;
if (!getrusage(RUSAGE_CHILDREN, &ru)) {
uint32_t ice_msec = ((ru.ru_utime.tv_sec - icecream_usage.tv_sec) * 1000
+ (ru.ru_utime.tv_usec - icecream_usage.tv_usec) / 1000) / num_cpus;
/* heuristics when no child terminated yet: account 25% of total nice as our clients */
if (!ice_msec && current_kids) {
ice_msec = (niceLoad * diff_stat) / (4 * 1000);
}
icecream_load += ice_msec * diff_stat / 1000;
icecream_usage.tv_sec = ru.ru_utime.tv_sec;
icecream_usage.tv_usec = ru.ru_utime.tv_usec;
}
unsigned int idle_average = icecream_load;
if (diff_sent) {
idle_average = icecream_load * 1000 / diff_sent;
}
if (idle_average > 1000)
idle_average = 1000;
msg.load = std::max((1000 - idle_average), memory_fillgrade);
#ifdef HAVE_SYS_VFS_H
struct statfs buf;
int ret = statfs(envbasedir.c_str(), &buf);
// Require at least 25MiB of free disk space per build.
if (!ret && long(buf.f_bavail) < ((long(max_kids + 1 - current_kids) * 25 * 1024 * 1024) / buf.f_bsize)) {
msg.load = 1000;
}
#endif
mem_limit = std::max(int(msg.freeMem / std::min(std::max(max_kids, 1U), 4U)), min_mem_limit);
if (abs(int(msg.load) - current_load) >= 100
|| (msg.load == 1000 && current_load != 1000)
|| (msg.load != 1000 && current_load == 1000)) {
if (!send_scheduler(msg)) {
return false;
}
}
icecream_load = 0;
current_load = msg.load;
}
return true;
}
string Daemon::dump_internals() const
{
string result;
result += "Node Name: " + nodename + "\n";
result += " Remote name: " + remote_name + "\n";
for (auto it : fd2chan) {
result += " fd2chan[" + toString(it.first) + "] = " + it.second->dump() + "\n";
}
for (auto client : clients) {
result += " client " + toString(client.second->client_id) + ": " + client.second->dump() + "\n";
}
if (cache_size) {
result += " Cache Size: " + toString(cache_size) + "\n";
}
result += " Architecture: " + machine_name + "\n";
for (const auto & native_environment : native_environments) {
result += " NativeEnv (" + native_environment.first + "): " + native_environment.second.name
+ ", size " + toString(native_environment.second.size) + (native_environment.second.create_env_pipe ? " (creating)" : "" ) + "\n";
}
if (!received_environments.empty()) {
result += " Now: " + toString(time(nullptr)) + "\n";
for (const auto& it : received_environments )
result += " ReceivedEnv[" + it.first + "] last_use " + toString(it.second.last_use)
+ ", size " + toString(it.second.size) + "\n";
}
result += " Current kids: " + toString(current_kids) + " (max: " + toString(max_kids) + ")\n";
result += " Supported features: " + supported_features_to_string(supported_features) + "\n";
if (scheduler) {
result += " Scheduler protocol: " + toString(scheduler->protocol) + "\n";
}
StatsMsg msg;
unsigned int memory_fillgrade = 0;
unsigned long idleLoad = 0;
unsigned long niceLoad = 0;
fill_stats(idleLoad, niceLoad, memory_fillgrade, &msg, clients.active_processes);
result += " cpu: " + toString(idleLoad) + " idle, "
+ toString(niceLoad) + " nice\n";
result += " load: " + toString(msg.loadAvg1 / 1000.) + ", icecream_load: "
+ toString(icecream_load) + "\n";
result += " memory: " + toString(memory_fillgrade)
+ " (free: " + toString(msg.freeMem) + ")\n";
return result;
}
int Daemon::scheduler_get_internals()
{
trace() << "handle_get_internals " << dump_internals() << endl;
return send_scheduler(StatusTextMsg(dump_internals())) ? 0 : 1;
}
int Daemon::scheduler_use_cs(UseCSMsg *msg)
{
Client *c = clients.find_by_client_id(msg->client_id);
trace() << "scheduler_use_cs " << msg->job_id << " " << msg->client_id
<< " " << c << " " << msg->hostname << " " << remote_name << endl;
if (!c) {
if (send_scheduler(JobDoneMsg(msg->job_id, 107, JobDoneMsg::FROM_SUBMITTER, clients.size()))) {
return 0;
}
return 1;
}
if (msg->hostname == remote_name && int(msg->port) == daemon_port) {
c->usecsmsg = new UseCSMsg(msg->host_platform, "127.0.0.1", daemon_port, msg->job_id, true, 1,
msg->matched_job_id);
c->status = Client::PENDING_USE_CS;
} else {
c->usecsmsg = new UseCSMsg(msg->host_platform, msg->hostname, msg->port,
msg->job_id, true, 1, msg->matched_job_id);
if (!c->channel->send_msg(*msg)) {
handle_end(c, 143);
return 0;
}
c->status = Client::WAITCOMPILE;
}
c->job_id = msg->job_id;
return 0;
}
int Daemon::scheduler_no_cs(NoCSMsg *msg)
{
Client *c = clients.find_by_client_id(msg->client_id);
trace() << "scheduler_no_cs " << msg->job_id << " " << msg->client_id
<< " " << c << " " << endl;
if (!c) {
if (send_scheduler(JobDoneMsg(msg->job_id, 107, JobDoneMsg::FROM_SUBMITTER, clients.size()))) {
return 0;
}
return 1;
}
c->usecsmsg = new UseCSMsg(string(), "127.0.0.1", daemon_port, msg->job_id, true, 1, 0);
c->status = Client::PENDING_USE_CS;
c->job_id = msg->job_id;
return 0;
}
bool Daemon::handle_transfer_env(Client *client, EnvTransferMsg *emsg)
{
log_info() << "handle_transfer_env, client status " << Client::status_str(client->status) << endl;
assert(client->status != Client::TOINSTALL &&
client->status != Client::WAITINSTALL &&
client->status != Client::TOCOMPILE &&
client->status != Client::WAITCOMPILE);
assert(client->pipe_from_child < 0);
assert(client->pipe_to_child < 0);
string target = emsg->target;
if (target.empty()) {
target = machine_name;
}
int pipe_from_child = -1;
int pipe_to_child = -1;
FileChunkMsg *fmsg = nullptr;
pid_t pid = start_install_environment(envbasedir, target, emsg->name, client->channel,
pipe_to_child, pipe_from_child, fmsg, user_uid, user_gid, nice_level);
if( pid <= 0 ) {
delete fmsg;
remove_environment_files(envbasedir, target + "/" + emsg->name);
handle_end(client, 144);
return false;
}
client->status = Client::TOINSTALL;
client->outfile = target + "/" + emsg->name;
current_kids++;
trace() << "PID of child thread running untaring environment: " << pid << endl;
client->pipe_to_child = pipe_to_child;
client->pipe_from_child = pipe_from_child;
client->child_pid = pid;
if (!handle_file_chunk_env(client, fmsg)) {
delete fmsg;
return false;
}
delete fmsg;
return true;
}
bool Daemon::handle_file_chunk_env(Client *client, Msg *msg)
{
/* this sucks, we can block when we're writing
the file chunk to the child, but we can't let the child
handle MsgChannel itself due to MsgChannel's stupid
caching layer inbetween, which causes us to lose partial
data after the M_END msg of the env transfer. */
assert(client);
assert(client->status == Client::TOINSTALL || client->status == Client::WAITINSTALL);
assert(client->pipe_to_child >= 0);
if (msg->type == M_FILE_CHUNK) {
FileChunkMsg *fcmsg = static_cast<FileChunkMsg *>(msg);
ssize_t len = fcmsg->len;
off_t off = 0;
while (len) {
ssize_t bytes = write(client->pipe_to_child, fcmsg->buffer + off, len);
if (bytes < 0 && errno == EINTR) {
continue;
}
if (bytes < 0 && errno == EPIPE) {
// Broken pipe may mean the unpacking has failed, but it also may
// mean the child has already finished successfully (it seems to happen,
// maybe some tar implementations add needless trailing bytes?).
// Wait for the child to finish to find out whether it was ok.
return true;
}
if (bytes == -1) {
log_perror("write to transfer env pipe failed.");
handle_end(client, 137);
return false;
}
len -= bytes;
off += bytes;
}
return true;
}
if (msg->type == M_END) {
trace() << "received end of environment, waiting for child" << endl;
close(client->pipe_to_child);
client->pipe_to_child = -1;
if( client->child_pid >= 0 ) {
// Transfer done, wait for handle_transfer_env_child_done() to finish the handling.
client->status = Client::WAITINSTALL; // Ignore further messages until child finishes.
return true;
}
// Transfer done, child done, finish.
return finish_transfer_env( client );
}
// unexpected message type
log_error() << "protocol error while receiving environment (" << msg->type << ")" << endl;
handle_end(client, 138);
return false;
}
bool Daemon::handle_env_install_child_done(Client *client)
{
assert(client->status == Client::TOINSTALL || client->status == Client::WAITINSTALL);
assert(client->child_pid >= 0);
assert(client->pipe_from_child >= 0);
bool success = false;
for (;;) {
char resultByte;
ssize_t n = ::read(client->pipe_from_child, &resultByte, 1);
if (n == -1 && errno == EINTR)
continue;
// The child at the end of start_install_environment() writes status on success.
if (n == 1 && resultByte == 0 )
success = true;
break;
}
log_info() << "handle_env_install_child_done PID " << client->child_pid << " for " << client->outfile
<< " status: " << ( success ? "success" : "failed" ) << endl;
client->child_pid = -1;
assert(current_kids > 0);
current_kids--;
if (client->pipe_from_child >= 0) {
close(client->pipe_from_child);
client->pipe_from_child = -1;
}
if( !success )
return finish_transfer_env( client, true ); // cancel
if( client->pipe_to_child >= 0 ) {
// we still haven't received M_END message, wait for that
assert( client->status == Client::TOINSTALL );
return true;
}
// Child done, transfer done, finish.
return finish_transfer_env( client );
}
bool Daemon::finish_transfer_env(Client *client, bool cancel)
{
log_info() << "finish_transfer_env for " << client->outfile
<< ( cancel ? " (cancel)" : "" ) << endl;
assert(client->outfile.size());
assert(client->status == Client::TOINSTALL || client->status == Client::WAITINSTALL);
if (client->pipe_from_child >= 0) {
assert( cancel ); // If not cancelled, this is closed by handle_env_install_child_done().
close(client->pipe_from_child);
client->pipe_from_child = -1;
}
if (client->pipe_to_child >= 0) {
assert( cancel ); // If not cancelled, this is closed by handle_file_chunk_env().
close(client->pipe_to_child);
client->pipe_to_child = -1;
}
if (client->child_pid >= 0 ) {
assert( cancel ); // If not cancelled, this is handled by handle_env_install_child_done().
kill( client->child_pid, SIGTERM );
int status;
trace() << "finish_transfer_env kill and waiting for child PID " << client->child_pid <<endl;
while (waitpid(client->child_pid, &status, 0) < 0 && errno == EINTR)
;
client->child_pid = -1;
assert(current_kids > 0);
current_kids--;
}
size_t installed_size = 0;
if( !cancel ) {
installed_size = finalize_install_environment(envbasedir, client->outfile,
user_uid, user_gid);
log_info() << "installed_size: " << installed_size << endl;
}
if( installed_size == 0 )
remove_environment_files(envbasedir, client->outfile);
client->status = Client::UNKNOWN;
string current = client->outfile;
client->outfile.clear();
if (installed_size) {
cache_size += installed_size;
received_environments[current].last_use = time(nullptr);
received_environments[current].size = installed_size;
log_info() << "installed " << current << " size: " << installed_size
<< " all: " << cache_size << endl;
}
check_cache_size(current);
bool r = reannounce_environments(); // do that before the file compiles
if (!maybe_stats(true)) { // update stats in case our disk is too full to accept more jobs
r = false;
}
return r;
}
void Daemon::check_cache_size(const string &new_env)
{
time_t now = time(nullptr);
while (cache_size > cache_size_limit) {
string oldest_received;
string oldest_native;
// I don't dare to use (time_t)-1
time_t oldest_time = time(nullptr) + 90000;
for (const auto& it : received_environments ) {
trace() << "considering cached environment: " << it.first << " " << it.second.last_use << " " << oldest_time << endl;
if (access(string(envbasedir + "/target=" + it.first + "/usr/bin/as").c_str(), X_OK) != 0) {
trace() << string(envbasedir + "/target=" + it.first + "/usr/bin/as") << " is missing, removing environment" << endl;
// force removing this one
oldest_time = 0;
oldest_received = it.first;
break;
}
// ignore recently used envs (they might be in use _right_ now)
int keep_timeout = 200;
if (it.second.last_use < oldest_time && now - it.second.last_use > keep_timeout) {
bool env_currently_in_use = false;
for (Clients::const_iterator it2 = clients.begin(); it2 != clients.end(); ++it2) {
if (it2->second->status == Client::TOCOMPILE
|| it2->second->status == Client::TOINSTALL
|| it2->second->status == Client::WAITINSTALL
|| it2->second->status == Client::WAITFORCHILD) {
assert(it2->second->job);
string envforjob = it2->second->job->targetPlatform() + "/"
+ it2->second->job->environmentVersion();
if (envforjob == it.first) {
env_currently_in_use = true;
}
}
}
if (!env_currently_in_use) {
oldest_time = it.second.last_use;
oldest_received = it.first;
}
}
}
for (const auto& it : native_environments ) {
trace() << "considering native environment: " << it.first << " " << it.second.last_use << " " << oldest_time << endl;
if (!it.second.name.empty() && access(it.second.name.c_str(), R_OK) != 0) {
trace() << it.second.name << " is missing, removing environment" << endl;
// force removing this one
oldest_time = 0;
oldest_native = it.first;
break;
}
// ignore recently used envs (they might be in use _right_ now)
int keep_timeout = 200;
// Allow removing native environments only after a longer period,
// unless there are many native environments.
if (native_environments.size() < 5) {
keep_timeout = 24 * 60 * 60; // 1 day
}
if (it.second.create_env_pipe) {
keep_timeout = 365 * 24 * 60 * 60; // do not remove if it's still being created
}
if (it.second.last_use < oldest_time && now - it.second.last_use > keep_timeout) {
oldest_time = it.second.last_use;
oldest_native = it.first;
}
}
if ((oldest_received.empty() || oldest_received == new_env)
&& (oldest_native.empty() || oldest_native == new_env)) {
break;
}
if (!oldest_native.empty())
remove_native_environment(oldest_native);
else
remove_environment(oldest_received);
}
}
void Daemon::remove_native_environment(const string& env_key)
{
assert(!env_key.empty());
remove_native_environment_files(env_key);
const NativeEnvironment &env = native_environments[env_key];
trace() << "removing " << env.name << " " << env.size << endl;
if (env.create_env_pipe) {
if ((-1 == close(env.create_env_pipe)) && (errno != EBADF)){
log_perror("close failed");
}
// TODO kill the still running icecc-create-env process?
}
assert( cache_size >= env.size );
cache_size -= env.size;
native_environments.erase(env_key);
}
void Daemon::remove_environment(const string& env_key)
{
assert(!env_key.empty());
remove_environment_files(envbasedir, env_key);
const ReceivedEnvironment& env = received_environments[env_key];
trace() << "removing " << envbasedir << "/target=" << env_key << " " << env.size << endl;
assert( cache_size >= env.size );
cache_size -= env.size;
received_environments.erase(env_key);
}
bool Daemon::handle_get_native_env(Client *client, GetNativeEnvMsg *msg)
{
string env_key;
map<string, time_t> filetimes;
struct stat st;
string compiler = msg->compiler;
// Older clients passed simply "gcc" or "clang" and not a binary.
if( !IS_PROTOCOL_41(client->channel) && compiler.find('/') == string::npos)
compiler = "/usr/bin/" + compiler;
string ccompiler = get_c_compiler(compiler);
string cppcompiler = get_cpp_compiler(compiler);
trace() << "get_native_env for " << msg->compiler
<< " (" << ccompiler << "," << cppcompiler << ")" << endl;
if (stat(ccompiler.c_str(), &st) != 0) {
log_error() << "Compiler binary " << ccompiler << " for environment not found." << endl;
client->channel->send_msg(EndMsg());
handle_end(client, 122);
return false;
}
filetimes[ccompiler] = st.st_mtime;
if (stat(cppcompiler.c_str(), &st) == 0) {
// C++ compiler is optional.
filetimes[cppcompiler] = st.st_mtime;
}
env_key = msg->compression + ":" + ccompiler;
for (list<string>::const_iterator it = msg->extrafiles.begin();
it != msg->extrafiles.end(); ++it) {
env_key += ':';
env_key += *it;
if (stat(it->c_str(), &st) != 0) {
log_error() << "Extra file " << *it << " for environment not found." << endl;
client->channel->send_msg(EndMsg());
handle_end(client, 122);
return false;
}
filetimes[*it] = st.st_mtime;
}
if (native_environments[env_key].name.length()) {
const NativeEnvironment &env = native_environments[env_key];
if (env.filetimes != filetimes || access(env.name.c_str(), R_OK) != 0) {
trace() << "native_env needs rebuild" << endl;
remove_native_environment(env.name);
}
}
trace() << "get_native_env " << native_environments[env_key].name
<< " (" << env_key << ")" << endl;
client->status = Client::WAITCREATEENV;
client->pending_create_env = env_key;
if (native_environments[env_key].name.length()) { // already available
return finish_get_native_env(client, env_key);
} else {
NativeEnvironment &env = native_environments[env_key]; // also inserts it
if (!env.create_env_pipe) { // start creating it only if not already in progress
env.filetimes = filetimes;
trace() << "start_create_env " << env_key << endl;
env.create_env_pipe = start_create_env(envbasedir, user_uid, user_gid, ccompiler,
msg->extrafiles, msg->compression);
} else {
trace() << "waiting for already running create_env " << env_key << endl;
}
}
return true;
}
bool Daemon::finish_get_native_env(Client *client, string env_key)
{
assert(client->status == Client::WAITCREATEENV);
assert(client->pending_create_env == env_key);
UseNativeEnvMsg m(native_environments[env_key].name);
if (!client->channel->send_msg(m)) {
handle_end(client, 138);
return false;
}
native_environments[env_key].last_use = time(nullptr);
client->status = Client::GOTNATIVE;
client->pending_create_env.clear();
return true;
}
bool Daemon::create_env_finished(string env_key)
{
assert(native_environments.count(env_key));
NativeEnvironment &env = native_environments[env_key];
trace() << "create_env_finished " << env_key << endl;
assert(env.create_env_pipe);
size_t installed_size = finish_create_env(env.create_env_pipe, envbasedir, env.name);
env.create_env_pipe = 0;
// we only clean out cache on next target install
cache_size += installed_size;
trace() << "cache_size = " << cache_size << endl;
if (!installed_size) {
bool repeat = true;
while(repeat) {
repeat = false;
for (Clients::const_iterator it = clients.begin(); it != clients.end(); ++it) {
if (it->second->pending_create_env == env_key) {
it->second->channel->send_msg(EndMsg());
handle_end(it->second, 121);
// The handle_end call invalidates our iterator, so break out of the loop,
// but try again just in case, until there's no match.
repeat = true;
break;
}
}
}
return false;
}
env.last_use = time(nullptr);
env.size = installed_size;
check_cache_size(env.name);
for (Clients::const_iterator it = clients.begin(); it != clients.end(); ++it) {
if (it->second->pending_create_env == env_key)
finish_get_native_env(it->second, env_key);
}
return true;
}
bool Daemon::handle_job_done(Client *cl, JobDoneMsg *m)
{
if (cl->status == Client::CLIENTWORK) {
clients.active_processes--;
}
cl->status = Client::JOBDONE;
JobDoneMsg *msg = static_cast<JobDoneMsg *>(m);
trace() << "handle_job_done " << msg->job_id << " " << msg->exitcode << endl;
if (!m->is_from_server()
&& (m->user_msec + m->sys_msec) <= m->real_msec) {
icecream_load += (m->user_msec + m->sys_msec) / num_cpus;
}
assert(msg->job_id == cl->job_id);
cl->job_id = 0; // the scheduler doesn't have it anymore
msg->client_count = clients.size();
return send_scheduler(*msg);
}
void Daemon::handle_old_request()
{
while ((current_kids + clients.active_processes) < std::max((unsigned int)1, max_kids)) {
Client *client = clients.get_earliest_client(Client::LINKJOB);
if (client) {
trace() << "send JobLocalBeginMsg to client" << endl;
if (!client->channel->send_msg(JobLocalBeginMsg())) {
log_warning() << "can't send start message to client" << endl;
handle_end(client, 112);
} else {
client->status = Client::CLIENTWORK;
clients.active_processes++;
trace() << "pushed local job " << client->client_id << endl;
if (!send_scheduler(JobLocalBeginMsg(client->client_id, client->outfile))) {
return;
}
}
continue;
}
client = clients.get_earliest_client(Client::PENDING_USE_CS);
if (client) {
trace() << "pending " << client->dump() << endl;
if (client->channel->send_msg(*client->usecsmsg)) {
client->status = Client::CLIENTWORK;
/* we make sure we reserve a spot and the rest is done if the
* client contacts as back with a Compile request */
clients.active_processes++;
} else {
handle_end(client, 129);
}
continue;
}
/* we don't want to handle TOCOMPILE jobs as long as our load
is too high */
if (current_load >= 1000) {
break;
}
client = clients.get_earliest_client(Client::TOCOMPILE);
if (client) {
CompileJob *job = client->job;
assert(job);
int sock = -1;
pid_t pid = -1;
trace() << "request for job " << job->jobID() << endl;
string envforjob = job->targetPlatform() + "/" + job->environmentVersion();
received_environments[envforjob].last_use = time(nullptr);
pid = handle_connection(envbasedir, job, client->channel, sock, mem_limit, user_uid, user_gid);
trace() << "handle connection returned " << pid << endl;
if (pid > 0) {
current_kids++;
client->status = Client::WAITFORCHILD;
client->pipe_from_child = sock;
client->child_pid = pid;
if (!send_scheduler(JobBeginMsg(job->jobID(), clients.size()))) {
log_info() << "failed sending scheduler about " << job->jobID() << endl;
}
} else {
handle_end(client, 117);
}
continue;
}
break;
}
}
bool Daemon::handle_compile_done(Client *client)
{
assert(client->status == Client::WAITFORCHILD);
assert(client->child_pid > 0);
assert(client->pipe_from_child >= 0);
JobDoneMsg *msg = new JobDoneMsg(client->job->jobID(), -1, JobDoneMsg::FROM_SERVER, clients.size());
assert(msg);
assert(current_kids > 0);
current_kids--;
unsigned int job_stat[8];
int end_status = 151;
if (read(client->pipe_from_child, job_stat, sizeof(job_stat)) == sizeof(job_stat)) {
msg->in_uncompressed = job_stat[JobStatistics::in_uncompressed];
msg->in_compressed = job_stat[JobStatistics::in_compressed];
msg->out_compressed = msg->out_uncompressed = job_stat[JobStatistics::out_uncompressed];
end_status = msg->exitcode = job_stat[JobStatistics::exit_code];
msg->real_msec = job_stat[JobStatistics::real_msec];
msg->user_msec = job_stat[JobStatistics::user_msec];
msg->sys_msec = job_stat[JobStatistics::sys_msec];
msg->pfaults = job_stat[JobStatistics::sys_pfaults];
}
close(client->pipe_from_child);
client->pipe_from_child = -1;
string envforjob = client->job->targetPlatform() + "/" + client->job->environmentVersion();
received_environments[envforjob].last_use = time(nullptr);
if(end_status == EXIT_COMPILER_MISSING) { // Environment damaged?
remove_environment(envforjob);
if( !reannounce_environments())
log_warning() << "failed reannounce environments after failed compile " << client->job->jobID() << endl;
}
if(!send_scheduler(*msg))
log_warning() << "failed sending scheduler about compile done " << client->job->jobID() << endl;
handle_end(client, end_status);
delete msg;
return false;
}
bool Daemon::handle_compile_file(Client *client, Msg *msg)
{
CompileJob *job = dynamic_cast<CompileFileMsg *>(msg)->takeJob();
assert(client);
assert(job);
client->job = job;
if (client->status == Client::CLIENTWORK) {
assert(job->environmentVersion() == "__client");
if (!send_scheduler(JobBeginMsg(job->jobID(), clients.size()))) {
trace() << "can't reach scheduler to tell him about compile file job "
<< job->jobID() << endl;
return false;
}
// no scheduler is not an error case!
} else {
client->status = Client::TOCOMPILE;
}
return true;
}
bool Daemon::handle_verify_env(Client *client, VerifyEnvMsg *msg)
{
assert(msg);
bool ok = verify_env(client->channel, envbasedir, msg->target, msg->environment, user_uid, user_gid);
trace() << "Verify environment done, " << (ok ? "success" : "failure") << ", environment " << msg->environment
<< " (" << msg->target << ")" << endl;
VerifyEnvResultMsg resultmsg(ok);
if (!client->channel->send_msg(resultmsg)) {
log_error() << "sending verify end result failed.." << endl;
return false;
}
return true;
}
bool Daemon::handle_blacklist_host_env(Client *client, Msg *msg)
{
// just forward
assert(dynamic_cast<BlacklistHostEnvMsg *>(msg));
assert(client);
(void)client;
if (!scheduler) {
return false;
}
return send_scheduler(*msg);
}
void Daemon::handle_end(Client *client, int exitcode)
{
trace() << "handle_end " << client->client_id << " " << client->channel->name << endl;
#ifdef ICECC_DEBUG
trace() << "handle_end " << client->dump() << endl;
trace() << dump_internals() << endl;
#endif
fd2chan.erase(client->channel->fd);
if (client->status == Client::TOINSTALL || client->status == Client::WAITINSTALL) {
finish_transfer_env(client, true);
}
if (client->status == Client::CLIENTWORK) {
clients.active_processes--;
}
if (client->status == Client::WAITCOMPILE && exitcode == 119) {
/* the client sent us a real good bye, so forget about the scheduler */
client->job_id = 0;
}
/* Delete from the clients map before send_scheduler, which causes a
double deletion. */
if (!clients.erase(client->channel)) {
log_error() << "client can't be erased: " << client->channel << endl;
flush_debug();
log_error() << dump_internals() << endl;
flush_debug();
assert(false);
}
if (scheduler && client->status != Client::WAITFORCHILD) {
int job_id = client->job_id;
bool use_client_id = false;
if (client->status == Client::TOCOMPILE) {
job_id = client->job->jobID();
}
if (client->status == Client::WAITFORCS) {
// We don't know the job id, because we haven't received a reply
// from the scheduler yet. Use client_id to identify the job,
// the scheduler will use it for matching.
use_client_id = true;
assert( client->client_id > 0 );
}
if (job_id > 0 || use_client_id) {
JobDoneMsg::from_type flag = JobDoneMsg::FROM_SUBMITTER;
switch (client->status) {
case Client::TOCOMPILE:
flag = JobDoneMsg::FROM_SERVER;
break;
case Client::UNKNOWN:
case Client::GOTNATIVE:
case Client::JOBDONE:
case Client::WAITFORCHILD:
case Client::LINKJOB:
case Client::TOINSTALL:
case Client::WAITINSTALL:
case Client::WAITCREATEENV:
assert(false); // should not have a job_id
break;
case Client::WAITCOMPILE:
case Client::PENDING_USE_CS:
case Client::CLIENTWORK:
case Client::WAITFORCS:
flag = JobDoneMsg::FROM_SUBMITTER;
break;
}
trace() << "scheduler->send_msg( JobDoneMsg( " << client->dump() << ", " << exitcode << "))\n";
JobDoneMsg msg(job_id, exitcode, flag, clients.size());
if( use_client_id ) {
msg.set_unknown_job_client_id( client->client_id );
}
if (!send_scheduler(msg)) {
trace() << "failed to reach scheduler for remote job done msg!" << endl;
}
} else if (client->status == Client::CLIENTWORK) {
// Clientwork && !job_id == LINK
trace() << "scheduler->send_msg( JobLocalDoneMsg( " << client->client_id << ") );\n";
if (!send_scheduler(JobLocalDoneMsg(client->client_id))) {
trace() << "failed to reach scheduler for local job done msg!" << endl;
}
}
}
delete client;
}
void Daemon::clear_children()
{
while (!clients.empty()) {
Client *cl = clients.first();
handle_end(cl, 116);
}
while (current_kids > 0) {
int status;
pid_t child;
while ((child = waitpid(-1, &status, 0)) < 0 && errno == EINTR) {}
current_kids--;
}
// they should be all in clients too
assert(fd2chan.empty());
fd2chan.clear();
new_client_id = 0;
trace() << "cleared children\n";
}
bool Daemon::handle_get_cs(Client *client, Msg *msg)
{
GetCSMsg *umsg = dynamic_cast<GetCSMsg *>(msg);
assert(client);
client->status = Client::WAITFORCS;
umsg->client_id = client->client_id;
trace() << "handle_get_cs " << umsg->client_id << endl;
if (!scheduler) {
/* now the thing is this: if there is no scheduler
there is no point in trying to ask him. So we just
redefine this as local job */
client->usecsmsg = new UseCSMsg(umsg->target, "127.0.0.1", daemon_port,
umsg->client_id, true, 1, 0);
client->status = Client::PENDING_USE_CS;
client->job_id = umsg->client_id;
return true;
}
umsg->client_count = clients.size();
return send_scheduler(*umsg);
}
int Daemon::handle_cs_conf(ConfCSMsg *msg)
{
max_scheduler_pong = msg->max_scheduler_pong;
max_scheduler_ping = msg->max_scheduler_ping;
return 0;
}
bool Daemon::handle_local_job(Client *client, Msg *msg)
{
client->status = Client::LINKJOB;
client->outfile = dynamic_cast<JobLocalBeginMsg *>(msg)->outfile;
return true;
}
bool Daemon::handle_activity(Client *client)
{
assert(client->status != Client::TOCOMPILE && client->status != Client::WAITINSTALL);
Msg *msg = client->channel->get_msg(0, true);
if (!msg) {
handle_end(client, 118);
return false;
}
bool ret = false;
if (client->status == Client::TOINSTALL) {
ret = handle_file_chunk_env(client, msg);
delete msg;
return ret;
}
switch (msg->type) {
case M_GET_NATIVE_ENV:
ret = handle_get_native_env(client, dynamic_cast<GetNativeEnvMsg *>(msg));
break;
case M_COMPILE_FILE:
ret = handle_compile_file(client, msg);
break;
case M_TRANFER_ENV:
ret = handle_transfer_env(client, dynamic_cast<EnvTransferMsg*>(msg));
break;
case M_GET_CS:
ret = handle_get_cs(client, msg);
break;
case M_END:
handle_end(client, 119);
ret = false;
break;
case M_JOB_LOCAL_BEGIN:
ret = handle_local_job(client, msg);
break;
case M_JOB_DONE:
ret = handle_job_done(client, dynamic_cast<JobDoneMsg *>(msg));
break;
case M_VERIFY_ENV:
ret = handle_verify_env(client, dynamic_cast<VerifyEnvMsg *>(msg));
break;
case M_BLACKLIST_HOST_ENV:
ret = handle_blacklist_host_env(client, msg);
break;
default:
log_error() << "protocol error " << msg->type << " on client "
<< client->dump() << endl;
client->channel->send_msg(EndMsg());
handle_end(client, 120);
ret = false;
}
delete msg;
return ret;
}
void Daemon::answer_client_requests()
{
#ifdef ICECC_DEBUG
if (clients.size() + current_kids) {
log_info() << dump_internals() << endl;
}
log_info() << "clients " << clients.dump_per_status() << " " << current_kids
<< " (" << max_kids << ")" << endl;
#endif
/* reap zombies */
int status;
while (waitpid(-1, &status, WNOHANG) < 0 && errno == EINTR) {}
handle_old_request();
/* collect the stats after the children exited icecream_load */
if (scheduler) {
maybe_stats();
}
vector< pollfd > pollfds;
pollfds.reserve( fd2chan.size() + 6 );
pollfd pfd; // tmp varible
if (tcp_listen_fd != -1) {
pfd.fd = tcp_listen_fd;
pfd.events = POLLIN;
pollfds.push_back(pfd);
}
if (tcp_listen_local_fd != -1) {
pfd.fd = tcp_listen_local_fd;
pfd.events = POLLIN;
pollfds.push_back(pfd);
}
pfd.fd = unix_listen_fd;
pfd.events = POLLIN;
pollfds.push_back(pfd);
for (map<int, MsgChannel *>::const_iterator it = fd2chan.begin();
it != fd2chan.end();) {
int i = it->first;
MsgChannel *c = it->second;
++it;
/* don't select on a fd that we're currently not interested in.
Avoids that we wake up on an event we're not handling anyway */
Client *client = clients.find_by_channel(c);
assert(client);
int current_status = client->status;
bool ignore_channel = current_status == Client::WAITFORCHILD ||
current_status == Client::WAITINSTALL;
/* when the remote host is full with work, the wait time for it to free up and
fork a child to compile could be long. If the input is ready to read, we will read
them and save it for the child; otherwise the write on the client side would be blocked */
if ( current_status == Client::TOCOMPILE ||
(!ignore_channel && (!c->has_msg() || handle_activity(client)))) {
pfd.fd = i;
pfd.events = POLLIN;
pollfds.push_back(pfd);
}
if ((current_status == Client::WAITFORCHILD
|| current_status == Client::TOINSTALL
|| current_status == Client::WAITINSTALL)
&& client->pipe_from_child != -1) {
pfd.fd = client->pipe_from_child;
pfd.events = POLLIN;
pollfds.push_back(pfd);
}
}
if (scheduler) {
pfd.fd = scheduler->fd;
pfd.events = POLLIN;
pollfds.push_back(pfd);
} else if (discover && discover->listen_fd() >= 0) {
/* We don't explicitely check for discover->get_fd() being in
the selected set below. If it's set, we simply will return
and our call will make sure we try to get the scheduler. */
pfd.fd = discover->listen_fd();
pfd.events = POLLIN;
pollfds.push_back(pfd);
}
for (map<string, NativeEnvironment>::const_iterator it = native_environments.begin();
it != native_environments.end(); ++it) {
if (it->second.create_env_pipe) {
pfd.fd = it->second.create_env_pipe;
pfd.events = POLLIN;
pollfds.push_back(pfd);
}
}
int ret = poll(pollfds.data(), pollfds.size(), max_scheduler_pong * 1000);
if (ret < 0 && errno != EINTR) {
log_perror("poll");
close_scheduler();
return;
}
// Reset debug if needed, but only if we aren't waiting for any child processes to finish,
// otherwise their debug output could end up reset in the middle (and flush log marks used
// by tests could be written out before debug output from children).
if( current_kids == 0 ) {
reset_debug_if_needed();
}
if (ret > 0) {
bool had_scheduler = scheduler;
if (scheduler && pollfd_is_set(pollfds, scheduler->fd, POLLIN)) {
while (!scheduler->read_a_bit() || scheduler->has_msg()) {
Msg *msg = scheduler->get_msg(0, true);
if (!msg) {
log_warning() << "scheduler closed connection" << endl;
close_scheduler();
clear_children();
return;
}
ret = 0;
switch (msg->type) {
case M_PING:
if (!IS_PROTOCOL_27(scheduler)) {
ret = !send_scheduler(PingMsg());
}
break;
case M_USE_CS:
ret = scheduler_use_cs(static_cast<UseCSMsg *>(msg));
break;
case M_NO_CS:
ret = scheduler_no_cs(static_cast<NoCSMsg *>(msg));
break;
case M_GET_INTERNALS:
ret = scheduler_get_internals();
break;
case M_CS_CONF:
ret = handle_cs_conf(static_cast<ConfCSMsg *>(msg));
break;
default:
log_error() << "unknown scheduler type " << (char)msg->type << endl;
ret = 1;
}
delete msg;
if (ret) {
close_scheduler();
return;
}
}
}
int listen_fd = -1;
if (tcp_listen_fd != -1 && pollfd_is_set(pollfds, tcp_listen_fd, POLLIN)) {
listen_fd = tcp_listen_fd;
}
if (tcp_listen_local_fd != -1 && pollfd_is_set(pollfds, tcp_listen_local_fd, POLLIN)) {
listen_fd = tcp_listen_local_fd;
}
if (pollfd_is_set(pollfds, unix_listen_fd, POLLIN)) {
listen_fd = unix_listen_fd;
}
if (listen_fd != -1) {
struct sockaddr cli_addr;
socklen_t cli_len = sizeof cli_addr;
int acc_fd = accept(listen_fd, &cli_addr, &cli_len);
if (acc_fd < 0) {
log_perror("accept error");
}
if (acc_fd == -1 && errno != EINTR) {
log_perror("accept failed:");
return;
}
MsgChannel *c = Service::createChannel(acc_fd, &cli_addr, cli_len);
if (!c) {
return;
}
Client *client = new Client;
client->client_id = ++new_client_id;
client->channel = c;
clients[c] = client;
fd2chan[c->fd] = c;
trace() << "accepted " << c->fd << " " << c->name << " as " << client->client_id << endl;
while (!c->read_a_bit() || c->has_msg()) {
if (!handle_activity(client)) {
break;
}
if (client->status == Client::TOCOMPILE
|| client->status == Client::WAITFORCHILD
|| client->status == Client::WAITINSTALL) {
break;
}
}
} else {
for (map<int, MsgChannel *>::const_iterator it = fd2chan.begin();
it != fd2chan.end();) {
int i = it->first;
MsgChannel *c = it->second;
Client *client = clients.find_by_channel(c);
assert(client);
++it;
if (client->status == Client::WAITFORCHILD
&& client->pipe_from_child >= 0
&& pollfd_is_set(pollfds, client->pipe_from_child, POLLIN)) {
if (!handle_compile_done(client)) {
return;
}
}
if ((client->status == Client::TOINSTALL || client->status == Client::WAITINSTALL)
&& client->pipe_from_child >= 0
&& pollfd_is_set(pollfds, client->pipe_from_child, POLLIN)) {
if (!handle_env_install_child_done(client)) {
return;
}
}
if (pollfd_is_set(pollfds, i, POLLIN)) {
if( client->status == Client::TOCOMPILE )
{
/* read as the preprocessed input is ready but don't process it and leave it to the child
if we didn't read it now, the client would be blocked and timed out */
c->read_a_bit();
}
else
{
assert(client->status != Client::TOCOMPILE && client->status != Client::WAITINSTALL);
while (!c->read_a_bit() || c->has_msg()) {
if (!handle_activity(client)) {
break;
}
if (client->status == Client::TOCOMPILE
|| client->status == Client::WAITFORCHILD
|| client->status == Client::WAITINSTALL) {
break;
}
}
}
}
}
for (map<string, NativeEnvironment>::iterator it = native_environments.begin();
it != native_environments.end(); ) {
if (it->second.create_env_pipe && pollfd_is_set(pollfds, it->second.create_env_pipe, POLLIN)) {
if(!create_env_finished(it->first))
{
native_environments.erase(it++);
continue;
}
}
++it;
}
}
if (had_scheduler && !scheduler) {
clear_children();
return;
}
}
}
bool Daemon::reconnect()
{
if (scheduler) {
return true;
}
if (!discover && next_scheduler_connect > time(nullptr)) {
trace() << "Delaying reconnect." << endl;
return false;
}
#ifdef ICECC_DEBUG
trace() << "reconn " << dump_internals() << endl;
#endif
if (!discover || (nullptr == (scheduler = discover->try_get_scheduler()) && discover->timed_out())) {
delete discover;
discover = new DiscoverSched(netname, max_scheduler_pong, schedname, scheduler_port);
}
if (!scheduler) {
log_warning() << "scheduler not yet found/selected." << endl;
return false;
}
delete discover;
discover = nullptr;
sockaddr_in name;
socklen_t len = sizeof(name);
int error = getsockname(scheduler->fd, (struct sockaddr*)&name, &len);
if (!error) {
remote_name = inet_ntoa(name.sin_addr);
} else {
remote_name = string();
}
log_info() << "Connected to scheduler (I am known as " << remote_name << ")" << endl;
current_load = -1000;
gettimeofday(&last_stat, nullptr);
icecream_load = 0;
LoginMsg lmsg(daemon_port, determine_nodename(), machine_name, supported_features);
lmsg.envs = available_environments(envbasedir);
lmsg.max_kids = max_kids;
lmsg.noremote = noremote;
return send_scheduler(lmsg);
}
int Daemon::working_loop()
{
for (;;) {
reconnect();
answer_client_requests();
if (exit_main_loop) {
close_scheduler();
clear_children();
break;
}
}
return 0;
}
int main(int argc, char **argv)
{
int max_processes = -1;
srand(time(nullptr) + getpid());
Daemon d;
int debug_level = Error;
string logfile;
bool detach = false;
nice_level = 5; // defined in serve.h
while (true) {
int option_index = 0;
static const struct option long_options[] = {
{ "netname", 1, nullptr, 'n' },
{ "max-processes", 1, nullptr, 'm' },
{ "help", 0, nullptr, 'h' },
{ "daemonize", 0, nullptr, 'd'},
{ "log-file", 1, nullptr, 'l'},
{ "nice", 1, nullptr, 0},
{ "name", 1, nullptr, 'N'},
{ "scheduler-host", 1, nullptr, 's' },
{ "env-basedir", 1, nullptr, 'b' },
{ "user-uid", 1, nullptr, 'u'},
{ "cache-limit", 1, nullptr, 0},
{ "no-remote", 0, nullptr, 0},
{ "interface", 1, nullptr, 'i'},
{ "port", 1, nullptr, 'p'},
{ nullptr, 0, nullptr, 0 }
};
const int c = getopt_long(argc, argv, "N:n:m:l:s:hvdb:u:i:p:", long_options, &option_index);
if (c == -1) {
break; // eoo
}
switch (c) {
case 0: {
string optname = long_options[option_index].name;
if (optname == "nice") {
if (optarg && *optarg) {
errno = 0;
int tnice = atoi(optarg);
if (!errno) {
nice_level = tnice;
}
} else {
usage("Error: --nice requires argument");
}
} else if (optname == "name") {
if (optarg && *optarg) {
d.nodename = optarg;
} else {
usage("Error: --name requires argument");
}
} else if (optname == "cache-limit") {
if (optarg && *optarg) {
errno = 0;
int mb = atoi(optarg);
if (!errno) {
cache_size_limit = mb * 1024 * 1024;
}
} else {
usage("Error: --cache-limit requires argument");
}
} else if (optname == "no-remote") {
d.noremote = true;
}
}
break;
case 'd':
detach = true;
break;
case 'N':
if (optarg && *optarg) {
d.nodename = optarg;
} else {
usage("Error: -N requires argument");
}
break;
case 'l':
if (optarg && *optarg) {
logfile = optarg;
} else {
usage("Error: -l requires argument");
}
break;
case 'v':
if (debug_level < MaxVerboseLevel) {
debug_level++;
}
break;
case 'n':
if (optarg && *optarg) {
d.netname = optarg;
} else {
usage("Error: -n requires argument");
}
break;
case 'm':
if (optarg && *optarg) {
max_processes = atoi(optarg);
} else {
usage("Error: -m requires argument");
}
break;
case 's':
if (optarg && *optarg) {
string scheduler = optarg;
size_t colon = scheduler.rfind( ':' );
if( colon == string::npos ) {
d.schedname = scheduler;
} else {
d.schedname = scheduler.substr(0, colon);
d.scheduler_port = atoi( scheduler.substr( colon + 1 ).c_str());
if( d.scheduler_port == 0 ) {
usage("Error: -s requires valid port if hostname includes colon");
}
}
} else {
usage("Error: -s requires hostname argument");
}
break;
case 'b':
if (optarg && *optarg) {
d.envbasedir = optarg;
}
break;
case 'u':
if (optarg && *optarg) {
struct passwd *pw = getpwnam(optarg);
if (!pw) {
usage("Error: -u requires a valid username");
} else {
d.user_uid = pw->pw_uid;
d.user_gid = pw->pw_gid;
d.warn_icecc_user_errno = 0;
if (!d.user_gid || !d.user_uid) {
usage("Error: -u <username> must not be root");
}
}
} else {
usage("Error: -u requires a valid username");
}
break;
case 'i':
if (optarg && *optarg) {
string daemon_interface = optarg;
if (daemon_interface.empty()) {
usage("Error: Invalid network interface specified");
}
d.daemon_interface = daemon_interface;
} else {
usage("Error: -i requires argument");
}
break;
case 'p':
if (optarg && *optarg) {
d.daemon_port = atoi(optarg);
if (0 == d.daemon_port) {
usage("Error: Invalid port specified");
}
} else {
usage("Error: -p requires argument");
}
break;
default:
usage();
}
}
if (d.warn_icecc_user_errno != 0) {
log_errno("No icecc user on system. Falling back to nobody.", d.warn_icecc_user_errno);
}
umask(022);
bool remote_disabled = false;
if (getuid() == 0) {
if (!logfile.length() && detach) {
mkdir("/var/log/icecc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
chmod("/var/log/icecc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
ignore_result(chown("/var/log/icecc", d.user_uid, d.user_gid));
logfile = "/var/log/icecc/iceccd.log";
}
mkdir("/var/run/icecc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
chmod("/var/run/icecc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
ignore_result(chown("/var/run/icecc", d.user_uid, d.user_gid));
#ifdef HAVE_LIBCAP_NG
capng_clear(CAPNG_SELECT_BOTH);
capng_update(CAPNG_ADD, (capng_type_t)(CAPNG_EFFECTIVE | CAPNG_PERMITTED), CAP_SYS_CHROOT);
int r = capng_change_id(d.user_uid, d.user_gid,
(capng_flags_t)(CAPNG_DROP_SUPP_GRP | CAPNG_CLEAR_BOUNDING));
if (r) {
log_error() << "Error: capng_change_id failed: " << r << endl;
exit(EXIT_SETUID_FAILED);
}
#endif
} else {
#ifdef HAVE_LIBCAP_NG
// It's possible to have the capability even without being root.
if (!capng_have_capability( CAPNG_EFFECTIVE, CAP_SYS_CHROOT )) {
#else
{
#endif
d.noremote = true;
remote_disabled = true;
}
}
setup_debug(debug_level, logfile);
log_info() << "ICECREAM daemon " VERSION " starting up (nice level "
<< nice_level << ") " << endl;
if (remote_disabled)
log_warning() << "Cannot use chroot, no remote jobs accepted." << endl;
if (d.noremote)
d.daemon_port = 0;
d.determine_system();
if (chdir("/") != 0) {
log_error() << "failed to switch to root directory: "
<< strerror(errno) << endl;
exit(EXIT_DISTCC_FAILED);
}
if (detach)
if (daemon(0, 0)) {
log_perror("Failed to run as a daemon.");
exit(EXIT_DISTCC_FAILED);
}
if (dcc_ncpus(&d.num_cpus) == 0) {
log_info() << d.num_cpus << " CPU(s) online on this server" << endl;
}
if (max_processes < 0) {
max_kids = d.num_cpus;
} else {
max_kids = max_processes;
}
log_info() << "allowing up to " << max_kids << " active jobs" << endl;
d.determine_supported_features();
log_info() << "supported features: " << supported_features_to_string(d.supported_features) << endl;
int ret;
/* Still create a new process group, even if not detached */
trace() << "not detaching\n";
if ((ret = set_new_pgrp()) != 0) {
return ret;
}
/* Don't catch signals until we've detached or created a process group. */
dcc_daemon_catch_signals();
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
log_warning() << "signal(SIGPIPE, ignore) failed: " << strerror(errno) << endl;
exit(EXIT_DISTCC_FAILED);
}
if (signal(SIGCHLD, SIG_DFL) == SIG_ERR) {
log_warning() << "signal(SIGCHLD) failed: " << strerror(errno) << endl;
exit(EXIT_DISTCC_FAILED);
}
/* This is called in the master daemon, whether that is detached or
* not. */
dcc_master_pid = getpid();
ofstream pidFile;
string progName = argv[0];
progName = find_basename(progName);
pidFilePath = string(RUNDIR) + string("/") + progName + string(".pid");
pidFile.open(pidFilePath.c_str());
pidFile << dcc_master_pid << endl;
pidFile.close();
if (!cleanup_cache(d.envbasedir, d.user_uid, d.user_gid)) {
return 1;
}
list<string> nl = get_netnames(200, d.scheduler_port);
trace() << "Netnames:" << endl;
for (list<string>::const_iterator it = nl.begin(); it != nl.end(); ++it) {
trace() << *it << endl;
}
if (!d.setup_listen_fds()) { // error
return 1;
}
return d.working_loop();
}
|