Hash :
89b6989f
Author :
Date :
2012-09-19T00:54:55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720
/*
* Copyright 2011-2012 Andrew Smith
* Copyright 2011-2012 Con Kolivas
*
* 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 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*
* Note: the code always includes GPU support even if there are no GPUs
* this simplifies handling multiple other device code being included
* depending on compile options
*/
#include "config.h"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include "compat.h"
#include "miner.h"
#include "driver-cpu.h" /* for algo_names[], TODO: re-factor dependency */
#if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX) || defined(USE_MODMINER)
#define HAVE_AN_FPGA 1
#endif
#if defined(unix) || defined(__APPLE__)
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SOCKETTYPE int
#define SOCKETFAIL(a) ((a) < 0)
#define INVSOCK -1
#define INVINETADDR -1
#define CLOSESOCKET close
#define SOCKERRMSG strerror(errno)
#endif
#ifdef WIN32
#include <ws2tcpip.h>
#include <winsock2.h>
#define SOCKETTYPE SOCKET
#define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
#define INVSOCK INVALID_SOCKET
#define INVINETADDR INADDR_NONE
#define CLOSESOCKET closesocket
static char WSAbuf[1024];
struct WSAERRORS {
int id;
char *code;
} WSAErrors[] = {
{ 0, "No error" },
{ WSAEINTR, "Interrupted system call" },
{ WSAEBADF, "Bad file number" },
{ WSAEACCES, "Permission denied" },
{ WSAEFAULT, "Bad address" },
{ WSAEINVAL, "Invalid argument" },
{ WSAEMFILE, "Too many open sockets" },
{ WSAEWOULDBLOCK, "Operation would block" },
{ WSAEINPROGRESS, "Operation now in progress" },
{ WSAEALREADY, "Operation already in progress" },
{ WSAENOTSOCK, "Socket operation on non-socket" },
{ WSAEDESTADDRREQ, "Destination address required" },
{ WSAEMSGSIZE, "Message too long" },
{ WSAEPROTOTYPE, "Protocol wrong type for socket" },
{ WSAENOPROTOOPT, "Bad protocol option" },
{ WSAEPROTONOSUPPORT, "Protocol not supported" },
{ WSAESOCKTNOSUPPORT, "Socket type not supported" },
{ WSAEOPNOTSUPP, "Operation not supported on socket" },
{ WSAEPFNOSUPPORT, "Protocol family not supported" },
{ WSAEAFNOSUPPORT, "Address family not supported" },
{ WSAEADDRINUSE, "Address already in use" },
{ WSAEADDRNOTAVAIL, "Can't assign requested address" },
{ WSAENETDOWN, "Network is down" },
{ WSAENETUNREACH, "Network is unreachable" },
{ WSAENETRESET, "Net connection reset" },
{ WSAECONNABORTED, "Software caused connection abort" },
{ WSAECONNRESET, "Connection reset by peer" },
{ WSAENOBUFS, "No buffer space available" },
{ WSAEISCONN, "Socket is already connected" },
{ WSAENOTCONN, "Socket is not connected" },
{ WSAESHUTDOWN, "Can't send after socket shutdown" },
{ WSAETOOMANYREFS, "Too many references, can't splice" },
{ WSAETIMEDOUT, "Connection timed out" },
{ WSAECONNREFUSED, "Connection refused" },
{ WSAELOOP, "Too many levels of symbolic links" },
{ WSAENAMETOOLONG, "File name too long" },
{ WSAEHOSTDOWN, "Host is down" },
{ WSAEHOSTUNREACH, "No route to host" },
{ WSAENOTEMPTY, "Directory not empty" },
{ WSAEPROCLIM, "Too many processes" },
{ WSAEUSERS, "Too many users" },
{ WSAEDQUOT, "Disc quota exceeded" },
{ WSAESTALE, "Stale NFS file handle" },
{ WSAEREMOTE, "Too many levels of remote in path" },
{ WSASYSNOTREADY, "Network system is unavailable" },
{ WSAVERNOTSUPPORTED, "Winsock version out of range" },
{ WSANOTINITIALISED, "WSAStartup not yet called" },
{ WSAEDISCON, "Graceful shutdown in progress" },
{ WSAHOST_NOT_FOUND, "Host not found" },
{ WSANO_DATA, "No host data of that type was found" },
{ -1, "Unknown error code" }
};
static char *WSAErrorMsg()
{
int i;
int id = WSAGetLastError();
/* Assume none of them are actually -1 */
for (i = 0; WSAErrors[i].id != -1; i++)
if (WSAErrors[i].id == id)
break;
sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
return &(WSAbuf[0]);
}
#define SOCKERRMSG WSAErrorMsg()
#ifndef SHUT_RDWR
#define SHUT_RDWR SD_BOTH
#endif
#ifndef in_addr_t
#define in_addr_t uint32_t
#endif
#endif
// Big enough for largest API request
// though a PC with 100s of PGAs/CPUs may exceed the size ...
// Current code assumes it can socket send this size also
#define MYBUFSIZ 65432 // TODO: intercept before it's exceeded
// BUFSIZ varies on Windows and Linux
#define TMPBUFSIZ 8192
// Number of requests to queue - normally would be small
// However lots of PGA's may mean more
#define QUEUE 100
static char *io_buffer = NULL;
static char *msg_buffer = NULL;
static SOCKETTYPE sock = INVSOCK;
static const char *UNAVAILABLE = " - API will not be available";
static const char *INVAPIGROUPS = "Invalid --api-groups parameter";
static const char *BLANK = "";
static const char *COMMA = ",";
static const char SEPARATOR = '|';
#define SEPSTR "|"
static const char GPUSEP = ',';
static const char *APIVERSION = "1.19";
static const char *DEAD = "Dead";
#if defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA)
static const char *SICK = "Sick";
static const char *NOSTART = "NoStart";
static const char *INIT = "Initialising";
#endif
static const char *DISABLED = "Disabled";
static const char *ALIVE = "Alive";
static const char *REJECTING = "Rejecting";
static const char *UNKNOWN = "Unknown";
#define _DYNAMIC "D"
#ifdef HAVE_OPENCL
static const char *DYNAMIC = _DYNAMIC;
#endif
static const char *YES = "Y";
static const char *NO = "N";
static const char *NULLSTR = "(null)";
static const char *TRUESTR = "true";
static const char *FALSESTR = "false";
#ifdef USE_SCRYPT
static const char *SCRYPTSTR = "scrypt";
#endif
static const char *SHA256STR = "sha256";
static const char *DEVICECODE = ""
#ifdef HAVE_OPENCL
"GPU "
#endif
#ifdef USE_BITFORCE
"BFL "
#endif
#ifdef USE_ICARUS
"ICA "
#endif
#ifdef USE_ZTEX
"ZTX "
#endif
#ifdef USE_MODMINER
"MMQ "
#endif
#ifdef WANT_CPUMINE
"CPU "
#endif
"";
static const char *OSINFO =
#if defined(__linux)
"Linux";
#else
#if defined(__APPLE__)
"Apple";
#else
#if defined (WIN32)
"Windows";
#else
#if defined(unix)
"Unix";
#else
"Unknown";
#endif
#endif
#endif
#endif
#define _DEVS "DEVS"
#define _POOLS "POOLS"
#define _SUMMARY "SUMMARY"
#define _STATUS "STATUS"
#define _VERSION "VERSION"
#define _MINECONFIG "CONFIG"
#define _GPU "GPU"
#ifdef HAVE_AN_FPGA
#define _PGA "PGA"
#endif
#ifdef WANT_CPUMINE
#define _CPU "CPU"
#endif
#define _GPUS "GPUS"
#define _PGAS "PGAS"
#define _CPUS "CPUS"
#define _NOTIFY "NOTIFY"
#define _DEVDETAILS "DEVDETAILS"
#define _BYE "BYE"
#define _RESTART "RESTART"
#define _MINESTATS "STATS"
#define _CHECK "CHECK"
#define _MINECOIN "COIN"
#define _DEBUGSET "DEBUG"
#define _SETCONFIG "SETCONFIG"
static const char ISJSON = '{';
#define JSON0 "{"
#define JSON1 "\""
#define JSON2 "\":["
#define JSON3 "]"
#define JSON4 ",\"id\":1"
#define JSON5 "}"
#define JSON_START JSON0
#define JSON_DEVS JSON1 _DEVS JSON2
#define JSON_POOLS JSON1 _POOLS JSON2
#define JSON_SUMMARY JSON1 _SUMMARY JSON2
#define JSON_STATUS JSON1 _STATUS JSON2
#define JSON_VERSION JSON1 _VERSION JSON2
#define JSON_MINECONFIG JSON1 _MINECONFIG JSON2
#define JSON_GPU JSON1 _GPU JSON2
#ifdef HAVE_AN_FPGA
#define JSON_PGA JSON1 _PGA JSON2
#endif
#ifdef WANT_CPUMINE
#define JSON_CPU JSON1 _CPU JSON2
#endif
#define JSON_GPUS JSON1 _GPUS JSON2
#define JSON_PGAS JSON1 _PGAS JSON2
#define JSON_CPUS JSON1 _CPUS JSON2
#define JSON_NOTIFY JSON1 _NOTIFY JSON2
#define JSON_DEVDETAILS JSON1 _DEVDETAILS JSON2
#define JSON_BYE JSON1 _BYE JSON1
#define JSON_RESTART JSON1 _RESTART JSON1
#define JSON_CLOSE JSON3
#define JSON_MINESTATS JSON1 _MINESTATS JSON2
#define JSON_CHECK JSON1 _CHECK JSON2
#define JSON_MINECOIN JSON1 _MINECOIN JSON2
#define JSON_DEBUGSET JSON1 _DEBUGSET JSON2
#define JSON_SETCONFIG JSON1 _SETCONFIG JSON2
#define JSON_END JSON4 JSON5
static const char *JSON_COMMAND = "command";
static const char *JSON_PARAMETER = "parameter";
#define MSG_INVGPU 1
#define MSG_ALRENA 2
#define MSG_ALRDIS 3
#define MSG_GPUMRE 4
#define MSG_GPUREN 5
#define MSG_GPUNON 6
#define MSG_POOL 7
#define MSG_NOPOOL 8
#define MSG_DEVS 9
#define MSG_NODEVS 10
#define MSG_SUMM 11
#define MSG_GPUDIS 12
#define MSG_GPUREI 13
#define MSG_INVCMD 14
#define MSG_MISID 15
#define MSG_GPUDEV 17
#ifdef WANT_CPUMINE
#define MSG_CPUNON 16
#define MSG_CPUDEV 18
#define MSG_INVCPU 19
#endif
#define MSG_NUMGPU 20
#define MSG_NUMCPU 21
#define MSG_VERSION 22
#define MSG_INVJSON 23
#define MSG_MISCMD 24
#define MSG_MISPID 25
#define MSG_INVPID 26
#define MSG_SWITCHP 27
#define MSG_MISVAL 28
#define MSG_NOADL 29
#define MSG_NOGPUADL 30
#define MSG_INVINT 31
#define MSG_GPUINT 32
#define MSG_MINECONFIG 33
#define MSG_GPUMERR 34
#define MSG_GPUMEM 35
#define MSG_GPUEERR 36
#define MSG_GPUENG 37
#define MSG_GPUVERR 38
#define MSG_GPUVDDC 39
#define MSG_GPUFERR 40
#define MSG_GPUFAN 41
#define MSG_MISFN 42
#define MSG_BADFN 43
#define MSG_SAVED 44
#define MSG_ACCDENY 45
#define MSG_ACCOK 46
#define MSG_ENAPOOL 47
#define MSG_DISPOOL 48
#define MSG_ALRENAP 49
#define MSG_ALRDISP 50
#define MSG_DISLASTP 51
#define MSG_MISPDP 52
#define MSG_INVPDP 53
#define MSG_TOOMANYP 54
#define MSG_ADDPOOL 55
#ifdef HAVE_AN_FPGA
#define MSG_PGANON 56
#define MSG_PGADEV 57
#define MSG_INVPGA 58
#endif
#define MSG_NUMPGA 59
#define MSG_NOTIFY 60
#ifdef HAVE_AN_FPGA
#define MSG_PGALRENA 61
#define MSG_PGALRDIS 62
#define MSG_PGAENA 63
#define MSG_PGADIS 64
#define MSG_PGAUNW 65
#endif
#define MSG_REMLASTP 66
#define MSG_ACTPOOL 67
#define MSG_REMPOOL 68
#define MSG_DEVDETAILS 69
#define MSG_MINESTATS 70
#define MSG_MISCHK 71
#define MSG_CHECK 72
#define MSG_POOLPRIO 73
#define MSG_DUPPID 74
#define MSG_MISBOOL 75
#define MSG_INVBOOL 76
#define MSG_FOO 77
#define MSG_MINECOIN 78
#define MSG_DEBUGSET 79
#define MSG_PGAIDENT 80
#define MSG_PGANOID 81
#define MSG_SETCONFIG 82
#define MSG_UNKCON 83
#define MSG_INVNUM 84
#define MSG_CONPAR 85
#define MSG_CONVAL 86
enum code_severity {
SEVERITY_ERR,
SEVERITY_WARN,
SEVERITY_INFO,
SEVERITY_SUCC,
SEVERITY_FAIL
};
enum code_parameters {
PARAM_GPU,
PARAM_PGA,
PARAM_CPU,
PARAM_PID,
PARAM_GPUMAX,
PARAM_PGAMAX,
PARAM_CPUMAX,
PARAM_PMAX,
PARAM_POOLMAX,
// Single generic case: have the code resolve it - see below
PARAM_DMAX,
PARAM_CMD,
PARAM_POOL,
PARAM_STR,
PARAM_BOTH,
PARAM_BOOL,
PARAM_SET,
PARAM_NONE
};
struct CODES {
const enum code_severity severity;
const int code;
const enum code_parameters params;
const char *description;
} codes[] = {
#ifdef HAVE_OPENCL
{ SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
{ SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
{ SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
{ SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
{ SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
#endif
{ SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
{ SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
{ SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
{ SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX,
#ifdef HAVE_OPENCL
"%d GPU(s)"
#endif
#if defined(HAVE_AN_FPGA) && defined(HAVE_OPENCL)
" - "
#endif
#ifdef HAVE_AN_FPGA
"%d PGA(s)"
#endif
#if defined(WANT_CPUMINE) && (defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA))
" - "
#endif
#ifdef WANT_CPUMINE
"%d CPU(s)"
#endif
},
{ SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
#ifdef HAVE_AN_FPGA
"/PGAs"
#endif
#ifdef WANT_CPUMINE
"/CPUs"
#endif
},
{ SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
#ifdef HAVE_OPENCL
{ SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
{ SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
#endif
{ SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
{ SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
#ifdef HAVE_OPENCL
{ SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
#endif
#ifdef HAVE_AN_FPGA
{ SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
{ SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
{ SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
{ SEVERITY_INFO, MSG_PGALRENA,PARAM_PGA, "PGA %d already enabled" },
{ SEVERITY_INFO, MSG_PGALRDIS,PARAM_PGA, "PGA %d already disabled" },
{ SEVERITY_INFO, MSG_PGAENA, PARAM_PGA, "PGA %d sent enable message" },
{ SEVERITY_INFO, MSG_PGADIS, PARAM_PGA, "PGA %d set disable flag" },
{ SEVERITY_ERR, MSG_PGAUNW, PARAM_PGA, "PGA %d is not flagged WELL, cannot enable" },
#endif
#ifdef WANT_CPUMINE
{ SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
{ SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
{ SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
#endif
{ SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
{ SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
{ SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
{ SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "CGMiner versions" },
{ SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
{ SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
{ SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
{ SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
{ SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
{ SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
{ SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
{ SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
{ SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
{ SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
{ SEVERITY_SUCC, MSG_MINECONFIG,PARAM_NONE, "CGMiner config" },
#ifdef HAVE_OPENCL
{ SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
{ SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported success" },
{ SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
{ SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported success" },
{ SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
{ SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported success" },
{ SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
{ SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported success" },
#endif
{ SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
{ SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
{ SEVERITY_SUCC, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
{ SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
{ SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
{ SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
{ SEVERITY_SUCC, MSG_POOLPRIO,PARAM_NONE, "Changed pool priorities" },
{ SEVERITY_ERR, MSG_DUPPID, PARAM_PID, "Duplicate pool specified %d" },
{ SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
{ SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
{ SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
{ SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
{ SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
{ SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
{ SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
{ SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
{ SEVERITY_ERR, MSG_REMLASTP,PARAM_POOL, "Cannot remove last pool %d:'%s'" },
{ SEVERITY_ERR, MSG_ACTPOOL, PARAM_POOL, "Cannot remove active pool %d:'%s'" },
{ SEVERITY_SUCC, MSG_REMPOOL, PARAM_BOTH, "Removed pool %d:'%s'" },
{ SEVERITY_SUCC, MSG_NOTIFY, PARAM_NONE, "Notify" },
{ SEVERITY_SUCC, MSG_DEVDETAILS,PARAM_NONE, "Device Details" },
{ SEVERITY_SUCC, MSG_MINESTATS,PARAM_NONE, "CGMiner stats" },
{ SEVERITY_ERR, MSG_MISCHK, PARAM_NONE, "Missing check cmd" },
{ SEVERITY_SUCC, MSG_CHECK, PARAM_NONE, "Check command" },
{ SEVERITY_ERR, MSG_MISBOOL, PARAM_NONE, "Missing parameter: true/false" },
{ SEVERITY_ERR, MSG_INVBOOL, PARAM_NONE, "Invalid parameter should be true or false" },
{ SEVERITY_SUCC, MSG_FOO, PARAM_BOOL, "Failover-Only set to %s" },
{ SEVERITY_SUCC, MSG_MINECOIN,PARAM_NONE, "CGMiner coin" },
{ SEVERITY_SUCC, MSG_DEBUGSET,PARAM_STR, "Debug settings" },
#ifdef HAVE_AN_FPGA
{ SEVERITY_SUCC, MSG_PGAIDENT,PARAM_PGA, "Identify command sent to PGA%d" },
{ SEVERITY_WARN, MSG_PGANOID, PARAM_PGA, "PGA%d does not support identify" },
#endif
{ SEVERITY_SUCC, MSG_SETCONFIG,PARAM_SET, "Set config '%s' to %d" },
{ SEVERITY_ERR, MSG_UNKCON, PARAM_STR, "Unknown config '%s'" },
{ SEVERITY_ERR, MSG_INVNUM, PARAM_BOTH, "Invalid number (%d) for '%s' range is 0-9999" },
{ SEVERITY_ERR, MSG_CONPAR, PARAM_NONE, "Missing config parameters 'name,N'" },
{ SEVERITY_ERR, MSG_CONVAL, PARAM_STR, "Missing config value N for '%s,N'" },
{ SEVERITY_FAIL, 0, 0, NULL }
};
static int my_thr_id = 0;
static bool bye;
#if defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA)
static bool ping = true;
#endif
// Used to control quit restart access to shutdown variables
static pthread_mutex_t quit_restart_lock;
static bool do_a_quit;
static bool do_a_restart;
static time_t when = 0; // when the request occurred
struct IP4ACCESS {
in_addr_t ip;
in_addr_t mask;
char group;
};
#define GROUP(g) (toupper(g))
#define PRIVGROUP GROUP('W')
#define NOPRIVGROUP GROUP('R')
#define ISPRIVGROUP(g) (GROUP(g) == PRIVGROUP)
#define GROUPOFFSET(g) (GROUP(g) - GROUP('A'))
#define VALIDGROUP(g) (GROUP(g) >= GROUP('A') && GROUP(g) <= GROUP('Z'))
#define COMMANDS(g) (apigroups[GROUPOFFSET(g)].commands)
#define DEFINEDGROUP(g) (ISPRIVGROUP(g) || COMMANDS(g) != NULL)
struct APIGROUPS {
// This becomes a string like: "|cmd1|cmd2|cmd3|" so it's quick to search
char *commands;
} apigroups['Z' - 'A' + 1]; // only A=0 to Z=25 (R: noprivs, W: allprivs)
static struct IP4ACCESS *ipaccess = NULL;
static int ips = 0;
#ifdef USE_BITFORCE
extern struct device_api bitforce_api;
#endif
#ifdef USE_ICARUS
extern struct device_api icarus_api;
#endif
#ifdef USE_ZTEX
extern struct device_api ztex_api;
#endif
#ifdef USE_MODMINER
extern struct device_api modminer_api;
#endif
// This is only called when expected to be needed (rarely)
// i.e. strings outside of the codes control (input from the user)
static char *escape_string(char *str, bool isjson)
{
char *buf, *ptr;
int count;
count = 0;
for (ptr = str; *ptr; ptr++) {
switch (*ptr) {
case ',':
case '|':
case '=':
if (!isjson)
count++;
break;
case '"':
if (isjson)
count++;
break;
case '\\':
count++;
break;
}
}
if (count == 0)
return str;
buf = malloc(strlen(str) + count + 1);
if (unlikely(!buf))
quit(1, "Failed to malloc escape buf");
ptr = buf;
while (*str)
switch (*str) {
case ',':
case '|':
case '=':
if (!isjson)
*(ptr++) = '\\';
*(ptr++) = *(str++);
break;
case '"':
if (isjson)
*(ptr++) = '\\';
*(ptr++) = *(str++);
break;
case '\\':
*(ptr++) = '\\';
*(ptr++) = *(str++);
break;
default:
*(ptr++) = *(str++);
break;
}
*ptr = '\0';
return buf;
}
static struct api_data *api_add_extra(struct api_data *root, struct api_data *extra)
{
struct api_data *tmp;
if (root) {
if (extra) {
// extra tail
tmp = extra->prev;
// extra prev = root tail
extra->prev = root->prev;
// root tail next = extra
root->prev->next = extra;
// extra tail next = root
tmp->next = root;
// root prev = extra tail
root->prev = tmp;
}
} else
root = extra;
return root;
}
static struct api_data *api_add_data_full(struct api_data *root, char *name, enum api_data_type type, void *data, bool copy_data)
{
struct api_data *api_data;
api_data = (struct api_data *)malloc(sizeof(struct api_data));
api_data->name = name;
api_data->type = type;
if (root == NULL) {
root = api_data;
root->prev = root;
root->next = root;
}
else {
api_data->prev = root->prev;
root->prev = api_data;
api_data->next = root;
api_data->prev->next = api_data;
}
api_data->data_was_malloc = copy_data;
// Avoid crashing on bad data
if (data == NULL) {
api_data->type = type = API_CONST;
data = (void *)NULLSTR;
api_data->data_was_malloc = copy_data = false;
}
if (!copy_data)
api_data->data = data;
else
switch(type) {
case API_ESCAPE:
case API_STRING:
case API_CONST:
api_data->data = (void *)malloc(strlen((char *)data) + 1);
strcpy((char*)(api_data->data), (char *)data);
break;
case API_INT:
api_data->data = (void *)malloc(sizeof(int));
*((int *)(api_data->data)) = *((int *)data);
break;
case API_UINT:
api_data->data = (void *)malloc(sizeof(unsigned int));
*((unsigned int *)(api_data->data)) = *((unsigned int *)data);
break;
case API_UINT32:
api_data->data = (void *)malloc(sizeof(uint32_t));
*((uint32_t *)(api_data->data)) = *((uint32_t *)data);
break;
case API_UINT64:
api_data->data = (void *)malloc(sizeof(uint64_t));
*((uint64_t *)(api_data->data)) = *((uint64_t *)data);
break;
case API_DOUBLE:
case API_ELAPSED:
case API_MHS:
case API_MHTOTAL:
case API_UTILITY:
case API_FREQ:
case API_HS:
case API_DIFF:
api_data->data = (void *)malloc(sizeof(double));
*((double *)(api_data->data)) = *((double *)data);
break;
case API_BOOL:
api_data->data = (void *)malloc(sizeof(bool));
*((bool *)(api_data->data)) = *((bool *)data);
break;
case API_TIMEVAL:
api_data->data = (void *)malloc(sizeof(struct timeval));
memcpy(api_data->data, data, sizeof(struct timeval));
break;
case API_TIME:
api_data->data = (void *)malloc(sizeof(time_t));
*(time_t *)(api_data->data) = *((time_t *)data);
break;
case API_VOLTS:
case API_TEMP:
api_data->data = (void *)malloc(sizeof(float));
*((float *)(api_data->data)) = *((float *)data);
break;
default:
applog(LOG_ERR, "API: unknown1 data type %d ignored", type);
api_data->type = API_STRING;
api_data->data_was_malloc = false;
api_data->data = (void *)UNKNOWN;
break;
}
return root;
}
struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data)
{
return api_add_data_full(root, name, API_ESCAPE, (void *)data, copy_data);
}
struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data)
{
return api_add_data_full(root, name, API_STRING, (void *)data, copy_data);
}
struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data)
{
return api_add_data_full(root, name, API_CONST, (void *)data, copy_data);
}
struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data)
{
return api_add_data_full(root, name, API_INT, (void *)data, copy_data);
}
struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data)
{
return api_add_data_full(root, name, API_UINT, (void *)data, copy_data);
}
struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data)
{
return api_add_data_full(root, name, API_UINT32, (void *)data, copy_data);
}
struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data)
{
return api_add_data_full(root, name, API_UINT64, (void *)data, copy_data);
}
struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_DOUBLE, (void *)data, copy_data);
}
struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_ELAPSED, (void *)data, copy_data);
}
struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data)
{
return api_add_data_full(root, name, API_BOOL, (void *)data, copy_data);
}
struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data)
{
return api_add_data_full(root, name, API_TIMEVAL, (void *)data, copy_data);
}
struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data)
{
return api_add_data_full(root, name, API_TIME, (void *)data, copy_data);
}
struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_MHS, (void *)data, copy_data);
}
struct api_data *api_add_mhtotal(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_MHTOTAL, (void *)data, copy_data);
}
struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data)
{
return api_add_data_full(root, name, API_TEMP, (void *)data, copy_data);
}
struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_UTILITY, (void *)data, copy_data);
}
struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_FREQ, (void *)data, copy_data);
}
struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data)
{
return api_add_data_full(root, name, API_VOLTS, (void *)data, copy_data);
}
struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_HS, (void *)data, copy_data);
}
struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_DIFF, (void *)data, copy_data);
}
static struct api_data *print_data(struct api_data *root, char *buf, bool isjson)
{
struct api_data *tmp;
bool first = true;
char *original, *escape;
char *quote;
if (isjson) {
strcpy(buf, JSON0);
buf = strchr(buf, '\0');
quote = JSON1;
} else
quote = (char *)BLANK;
while (root) {
if (!first)
*(buf++) = *COMMA;
else
first = false;
sprintf(buf, "%s%s%s%s", quote, root->name, quote, isjson ? ":" : "=");
buf = strchr(buf, '\0');
switch(root->type) {
case API_STRING:
case API_CONST:
sprintf(buf, "%s%s%s", quote, (char *)(root->data), quote);
break;
case API_ESCAPE:
original = (char *)(root->data);
escape = escape_string((char *)(root->data), isjson);
sprintf(buf, "%s%s%s", quote, escape, quote);
if (escape != original)
free(escape);
break;
case API_INT:
sprintf(buf, "%d", *((int *)(root->data)));
break;
case API_UINT:
sprintf(buf, "%u", *((unsigned int *)(root->data)));
break;
case API_UINT32:
sprintf(buf, "%"PRIu32, *((uint32_t *)(root->data)));
break;
case API_UINT64:
sprintf(buf, "%"PRIu64, *((uint64_t *)(root->data)));
break;
case API_TIME:
sprintf(buf, "%lu", *((unsigned long *)(root->data)));
break;
case API_DOUBLE:
sprintf(buf, "%f", *((double *)(root->data)));
break;
case API_ELAPSED:
sprintf(buf, "%.0f", *((double *)(root->data)));
break;
case API_UTILITY:
case API_FREQ:
case API_MHS:
sprintf(buf, "%.2f", *((double *)(root->data)));
break;
case API_VOLTS:
sprintf(buf, "%.3f", *((float *)(root->data)));
break;
case API_MHTOTAL:
sprintf(buf, "%.4f", *((double *)(root->data)));
break;
case API_HS:
sprintf(buf, "%.15f", *((double *)(root->data)));
break;
case API_DIFF:
sprintf(buf, "%.8f", *((double *)(root->data)));
break;
case API_BOOL:
sprintf(buf, "%s", *((bool *)(root->data)) ? TRUESTR : FALSESTR);
break;
case API_TIMEVAL:
sprintf(buf, "%ld.%06ld",
((struct timeval *)(root->data))->tv_sec,
((struct timeval *)(root->data))->tv_usec);
break;
case API_TEMP:
sprintf(buf, "%.2f", *((float *)(root->data)));
break;
default:
applog(LOG_ERR, "API: unknown2 data type %d ignored", root->type);
sprintf(buf, "%s%s%s", quote, UNKNOWN, quote);
break;
}
buf = strchr(buf, '\0');
if (root->data_was_malloc)
free(root->data);
if (root->next == root) {
free(root);
root = NULL;
} else {
tmp = root;
root = tmp->next;
root->prev = tmp->prev;
root->prev->next = root;
free(tmp);
}
}
strcpy(buf, isjson ? JSON5 : SEPSTR);
return root;
}
#ifdef HAVE_AN_FPGA
static int numpgas()
{
int count = 0;
int i;
for (i = 0; i < total_devices; i++) {
#ifdef USE_BITFORCE
if (devices[i]->api == &bitforce_api)
count++;
#endif
#ifdef USE_ICARUS
if (devices[i]->api == &icarus_api)
count++;
#endif
#ifdef USE_ZTEX
if (devices[i]->api == &ztex_api)
count++;
#endif
#ifdef USE_MODMINER
if (devices[i]->api == &modminer_api)
count++;
#endif
}
return count;
}
static int pgadevice(int pgaid)
{
int count = 0;
int i;
for (i = 0; i < total_devices; i++) {
#ifdef USE_BITFORCE
if (devices[i]->api == &bitforce_api)
count++;
#endif
#ifdef USE_ICARUS
if (devices[i]->api == &icarus_api)
count++;
#endif
#ifdef USE_ZTEX
if (devices[i]->api == &ztex_api)
count++;
#endif
#ifdef USE_MODMINER
if (devices[i]->api == &modminer_api)
count++;
#endif
if (count == (pgaid + 1))
return i;
}
return -1;
}
#endif
// All replies (except BYE and RESTART) start with a message
// thus for JSON, message() inserts JSON_START at the front
// and send_result() adds JSON_END at the end
static char *message(int messageid, int paramid, char *param2, bool isjson)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
char severity[2];
char *ptr;
#ifdef HAVE_AN_FPGA
int pga;
#endif
#ifdef WANT_CPUMINE
int cpu;
#endif
int i;
if (!isjson)
msg_buffer[0] = '\0';
else
strcpy(msg_buffer, JSON_START JSON_STATUS);
ptr = strchr(msg_buffer, '\0');
for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
if (codes[i].code == messageid) {
switch (codes[i].severity) {
case SEVERITY_WARN:
severity[0] = 'W';
break;
case SEVERITY_INFO:
severity[0] = 'I';
break;
case SEVERITY_SUCC:
severity[0] = 'S';
break;
case SEVERITY_ERR:
default:
severity[0] = 'E';
break;
}
severity[1] = '\0';
switch(codes[i].params) {
case PARAM_GPU:
case PARAM_PGA:
case PARAM_CPU:
case PARAM_PID:
sprintf(buf, codes[i].description, paramid);
break;
case PARAM_POOL:
sprintf(buf, codes[i].description, paramid, pools[paramid]->rpc_url);
break;
#ifdef HAVE_OPENCL
case PARAM_GPUMAX:
sprintf(buf, codes[i].description, paramid, nDevs - 1);
break;
#endif
#ifdef HAVE_AN_FPGA
case PARAM_PGAMAX:
pga = numpgas();
sprintf(buf, codes[i].description, paramid, pga - 1);
break;
#endif
#ifdef WANT_CPUMINE
case PARAM_CPUMAX:
if (opt_n_threads > 0)
cpu = num_processors;
else
cpu = 0;
sprintf(buf, codes[i].description, paramid, cpu - 1);
break;
#endif
case PARAM_PMAX:
sprintf(buf, codes[i].description, total_pools);
break;
case PARAM_POOLMAX:
sprintf(buf, codes[i].description, paramid, total_pools - 1);
break;
case PARAM_DMAX:
#ifdef HAVE_AN_FPGA
pga = numpgas();
#endif
#ifdef WANT_CPUMINE
if (opt_n_threads > 0)
cpu = num_processors;
else
cpu = 0;
#endif
sprintf(buf, codes[i].description
#ifdef HAVE_OPENCL
, nDevs
#endif
#ifdef HAVE_AN_FPGA
, pga
#endif
#ifdef WANT_CPUMINE
, cpu
#endif
);
break;
case PARAM_CMD:
sprintf(buf, codes[i].description, JSON_COMMAND);
break;
case PARAM_STR:
sprintf(buf, codes[i].description, param2);
break;
case PARAM_BOTH:
sprintf(buf, codes[i].description, paramid, param2);
break;
case PARAM_BOOL:
sprintf(buf, codes[i].description, paramid ? TRUESTR : FALSESTR);
break;
case PARAM_SET:
sprintf(buf, codes[i].description, param2, paramid);
break;
case PARAM_NONE:
default:
strcpy(buf, codes[i].description);
}
root = api_add_string(root, _STATUS, severity, false);
root = api_add_time(root, "When", &when, false);
root = api_add_int(root, "Code", &messageid, false);
root = api_add_escape(root, "Msg", buf, false);
root = api_add_escape(root, "Description", opt_api_description, false);
root = print_data(root, ptr, isjson);
if (isjson)
strcat(ptr, JSON_CLOSE);
return msg_buffer;
}
}
root = api_add_string(root, _STATUS, "F", false);
root = api_add_time(root, "When", &when, false);
int id = -1;
root = api_add_int(root, "Code", &id, false);
sprintf(buf, "%d", messageid);
root = api_add_escape(root, "Msg", buf, false);
root = api_add_escape(root, "Description", opt_api_description, false);
root = print_data(root, ptr, isjson);
if (isjson)
strcat(ptr, JSON_CLOSE);
return msg_buffer;
}
static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
sprintf(io_buffer, isjson
? "%s," JSON_VERSION
: "%s" _VERSION ",",
message(MSG_VERSION, 0, NULL, isjson));
root = api_add_string(root, "CGMiner", VERSION, false);
root = api_add_const(root, "API", APIVERSION, false);
root = print_data(root, buf, isjson);
if (isjson)
strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
int gpucount = 0;
int pgacount = 0;
int cpucount = 0;
char *adlinuse = (char *)NO;
#ifdef HAVE_ADL
const char *adl = YES;
int i;
for (i = 0; i < nDevs; i++) {
if (gpus[i].has_adl) {
adlinuse = (char *)YES;
break;
}
}
#else
const char *adl = NO;
#endif
#ifdef HAVE_OPENCL
gpucount = nDevs;
#endif
#ifdef HAVE_AN_FPGA
pgacount = numpgas();
#endif
#ifdef WANT_CPUMINE
cpucount = opt_n_threads > 0 ? num_processors : 0;
#endif
sprintf(io_buffer, isjson
? "%s," JSON_MINECONFIG
: "%s" _MINECONFIG ",",
message(MSG_MINECONFIG, 0, NULL, isjson));
root = api_add_int(root, "GPU Count", &gpucount, false);
root = api_add_int(root, "PGA Count", &pgacount, false);
root = api_add_int(root, "CPU Count", &cpucount, false);
root = api_add_int(root, "Pool Count", &total_pools, false);
root = api_add_const(root, "ADL", (char *)adl, false);
root = api_add_string(root, "ADL in use", adlinuse, false);
root = api_add_const(root, "Strategy", strategies[pool_strategy].s, false);
root = api_add_int(root, "Log Interval", &opt_log_interval, false);
root = api_add_const(root, "Device Code", DEVICECODE, false);
root = api_add_const(root, "OS", OSINFO, false);
root = api_add_bool(root, "Failover-Only", &opt_fail_only, false);
root = api_add_int(root, "ScanTime", &opt_scantime, false);
root = api_add_int(root, "Queue", &opt_queue, false);
root = api_add_int(root, "Expiry", &opt_expiry, false);
root = print_data(root, buf, isjson);
if (isjson)
strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
#if defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA)
static const char *status2str(enum alive status)
{
switch (status) {
case LIFE_WELL:
return ALIVE;
case LIFE_SICK:
return SICK;
case LIFE_DEAD:
return DEAD;
case LIFE_NOSTART:
return NOSTART;
case LIFE_INIT:
return INIT;
default:
return UNKNOWN;
}
}
#endif
#ifdef HAVE_OPENCL
static void gpustatus(int gpu, bool isjson)
{
struct api_data *root = NULL;
char intensity[20];
char buf[TMPBUFSIZ];
char *enabled;
char *status;
float gt, gv;
int ga, gf, gp, gc, gm, pt;
if (gpu >= 0 && gpu < nDevs) {
struct cgpu_info *cgpu = &gpus[gpu];
cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
#ifdef HAVE_ADL
if (!gpu_stats(gpu, >, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
#endif
gt = gv = gm = gc = ga = gf = gp = pt = 0;
if (cgpu->deven != DEV_DISABLED)
enabled = (char *)YES;
else
enabled = (char *)NO;
status = (char *)status2str(cgpu->status);
if (cgpu->dynamic)
strcpy(intensity, DYNAMIC);
else
sprintf(intensity, "%d", cgpu->intensity);
root = api_add_int(root, "GPU", &gpu, false);
root = api_add_string(root, "Enabled", enabled, false);
root = api_add_string(root, "Status", status, false);
root = api_add_temp(root, "Temperature", >, false);
root = api_add_int(root, "Fan Speed", &gf, false);
root = api_add_int(root, "Fan Percent", &gp, false);
root = api_add_int(root, "GPU Clock", &gc, false);
root = api_add_int(root, "Memory Clock", &gm, false);
root = api_add_volts(root, "GPU Voltage", &gv, false);
root = api_add_int(root, "GPU Activity", &ga, false);
root = api_add_int(root, "Powertune", &pt, false);
double mhs = cgpu->total_mhashes / total_secs;
root = api_add_mhs(root, "MHS av", &mhs, false);
char mhsname[27];
sprintf(mhsname, "MHS %ds", opt_log_interval);
root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
root = api_add_utility(root, "Utility", &(cgpu->utility), false);
root = api_add_string(root, "Intensity", intensity, false);
int last_share_pool = cgpu->last_share_pool_time > 0 ?
cgpu->last_share_pool : -1;
root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
}
}
#endif
#ifdef HAVE_AN_FPGA
static void pgastatus(int pga, bool isjson)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
char *enabled;
char *status;
int numpga = numpgas();
if (numpga > 0 && pga >= 0 && pga < numpga) {
int dev = pgadevice(pga);
if (dev < 0) // Should never happen
return;
struct cgpu_info *cgpu = devices[dev];
double frequency = 0;
float temp = cgpu->temp;
#ifdef USE_ZTEX
if (cgpu->api == &ztex_api && cgpu->device_ztex)
frequency = cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1);
#endif
#ifdef USE_MODMINER
// TODO: a modminer has up to 4 devices but only 1 set of data for all ...
// except 4 sets of data for temp/clock
// So this should change in the future to just find the single temp/clock
// if the modminer code splits the device into seperate devices later
// For now, just display the highest temp and the average clock
if (cgpu->api == &modminer_api) {
int tc = cgpu->threads;
int i;
temp = 0;
if (tc > 4)
tc = 4;
for (i = 0; i < tc; i++) {
struct thr_info *thr = cgpu->thr[i];
struct modminer_fpga_state *state = thr->cgpu_data;
if (state->temp > temp)
temp = state->temp;
frequency += state->clock;
}
frequency /= (tc ? tc : 1);
}
#endif
cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
if (cgpu->deven != DEV_DISABLED)
enabled = (char *)YES;
else
enabled = (char *)NO;
status = (char *)status2str(cgpu->status);
root = api_add_int(root, "PGA", &pga, false);
root = api_add_string(root, "Name", cgpu->api->name, false);
root = api_add_int(root, "ID", &(cgpu->device_id), false);
root = api_add_string(root, "Enabled", enabled, false);
root = api_add_string(root, "Status", status, false);
root = api_add_temp(root, "Temperature", &temp, false);
double mhs = cgpu->total_mhashes / total_secs;
root = api_add_mhs(root, "MHS av", &mhs, false);
char mhsname[27];
sprintf(mhsname, "MHS %ds", opt_log_interval);
root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
root = api_add_utility(root, "Utility", &(cgpu->utility), false);
int last_share_pool = cgpu->last_share_pool_time > 0 ?
cgpu->last_share_pool : -1;
root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
root = api_add_freq(root, "Frequency", &frequency, false);
root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
}
}
#endif
#ifdef WANT_CPUMINE
static void cpustatus(int cpu, bool isjson)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
struct cgpu_info *cgpu = &cpus[cpu];
cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
root = api_add_int(root, "CPU", &cpu, false);
double mhs = cgpu->total_mhashes / total_secs;
root = api_add_mhs(root, "MHS av", &mhs, false);
char mhsname[27];
sprintf(mhsname, "MHS %ds", opt_log_interval);
root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
root = api_add_utility(root, "Utility", &(cgpu->utility), false);
int last_share_pool = cgpu->last_share_pool_time > 0 ?
cgpu->last_share_pool : -1;
root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
}
}
#endif
static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
int devcount = 0;
int numgpu = 0;
int numpga = 0;
int i;
#ifdef HAVE_OPENCL
numgpu = nDevs;
#endif
#ifdef HAVE_AN_FPGA
numpga = numpgas();
#endif
if (numgpu == 0 && opt_n_threads == 0 && numpga == 0) {
strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
return;
}
strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
if (isjson) {
strcat(io_buffer, COMMA);
strcat(io_buffer, JSON_DEVS);
}
#ifdef HAVE_OPENCL
for (i = 0; i < nDevs; i++) {
if (isjson && devcount > 0)
strcat(io_buffer, COMMA);
gpustatus(i, isjson);
devcount++;
}
#endif
#ifdef HAVE_AN_FPGA
if (numpga > 0) {
for (i = 0; i < numpga; i++) {
if (isjson && devcount > 0)
strcat(io_buffer, COMMA);
pgastatus(i, isjson);
devcount++;
}
}
#endif
#ifdef WANT_CPUMINE
if (opt_n_threads > 0) {
for (i = 0; i < num_processors; i++) {
if (isjson && devcount > 0)
strcat(io_buffer, COMMA);
cpustatus(i, isjson);
devcount++;
}
}
#endif
if (isjson)
strcat(io_buffer, JSON_CLOSE);
}
#ifdef HAVE_OPENCL
static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
int id;
if (nDevs == 0) {
strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= nDevs) {
strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
return;
}
strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
if (isjson) {
strcat(io_buffer, COMMA);
strcat(io_buffer, JSON_GPU);
}
gpustatus(id, isjson);
if (isjson)
strcat(io_buffer, JSON_CLOSE);
}
#endif
#ifdef HAVE_AN_FPGA
static void pgadev(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
int numpga = numpgas();
int id;
if (numpga == 0) {
strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= numpga) {
strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
return;
}
strcpy(io_buffer, message(MSG_PGADEV, id, NULL, isjson));
if (isjson) {
strcat(io_buffer, COMMA);
strcat(io_buffer, JSON_PGA);
}
pgastatus(id, isjson);
if (isjson)
strcat(io_buffer, JSON_CLOSE);
}
static void pgaenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
int numpga = numpgas();
struct thr_info *thr;
int pga;
int id;
int i;
if (numpga == 0) {
strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= numpga) {
strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
return;
}
int dev = pgadevice(id);
if (dev < 0) { // Should never happen
strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
return;
}
struct cgpu_info *cgpu = devices[dev];
if (cgpu->deven != DEV_DISABLED) {
strcpy(io_buffer, message(MSG_PGALRENA, id, NULL, isjson));
return;
}
#if 0 /* A DISABLED device wont change status FIXME: should disabling make it WELL? */
if (cgpu->status != LIFE_WELL) {
strcpy(io_buffer, message(MSG_PGAUNW, id, NULL, isjson));
return;
}
#endif
for (i = 0; i < mining_threads; i++) {
pga = thr_info[i].cgpu->device_id;
if (pga == dev) {
thr = &thr_info[i];
cgpu->deven = DEV_ENABLED;
tq_push(thr->q, &ping);
}
}
strcpy(io_buffer, message(MSG_PGAENA, id, NULL, isjson));
}
static void pgadisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
int numpga = numpgas();
int id;
if (numpga == 0) {
strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= numpga) {
strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
return;
}
int dev = pgadevice(id);
if (dev < 0) { // Should never happen
strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
return;
}
struct cgpu_info *cgpu = devices[dev];
if (cgpu->deven == DEV_DISABLED) {
strcpy(io_buffer, message(MSG_PGALRDIS, id, NULL, isjson));
return;
}
cgpu->deven = DEV_DISABLED;
strcpy(io_buffer, message(MSG_PGADIS, id, NULL, isjson));
}
static void pgaidentify(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
int numpga = numpgas();
int id;
if (numpga == 0) {
strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= numpga) {
strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
return;
}
int dev = pgadevice(id);
if (dev < 0) { // Should never happen
strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
return;
}
struct cgpu_info *cgpu = devices[dev];
struct device_api *api = cgpu->api;
if (!api->identify_device)
strcpy(io_buffer, message(MSG_PGANOID, id, NULL, isjson));
else {
api->identify_device(cgpu);
strcpy(io_buffer, message(MSG_PGAIDENT, id, NULL, isjson));
}
}
#endif
#ifdef WANT_CPUMINE
static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
int id;
if (opt_n_threads == 0) {
strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= num_processors) {
strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
return;
}
strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
if (isjson) {
strcat(io_buffer, COMMA);
strcat(io_buffer, JSON_CPU);
}
cpustatus(id, isjson);
if (isjson)
strcat(io_buffer, JSON_CLOSE);
}
#endif
static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
char *status, *lp;
int i;
if (total_pools == 0) {
strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
return;
}
strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
if (isjson) {
strcat(io_buffer, COMMA);
strcat(io_buffer, JSON_POOLS);
}
for (i = 0; i < total_pools; i++) {
struct pool *pool = pools[i];
switch (pool->enabled) {
case POOL_DISABLED:
status = (char *)DISABLED;
break;
case POOL_REJECTING:
status = (char *)REJECTING;
break;
case POOL_ENABLED:
if (pool->idle)
status = (char *)DEAD;
else
status = (char *)ALIVE;
break;
default:
status = (char *)UNKNOWN;
break;
}
if (pool->hdr_path)
lp = (char *)YES;
else
lp = (char *)NO;
root = api_add_int(root, "POOL", &i, false);
root = api_add_escape(root, "URL", pool->rpc_url, false);
root = api_add_string(root, "Status", status, false);
root = api_add_int(root, "Priority", &(pool->prio), false);
root = api_add_string(root, "Long Poll", lp, false);
root = api_add_uint(root, "Getworks", &(pool->getwork_requested), false);
root = api_add_int(root, "Accepted", &(pool->accepted), false);
root = api_add_int(root, "Rejected", &(pool->rejected), false);
root = api_add_uint(root, "Discarded", &(pool->discarded_work), false);
root = api_add_uint(root, "Stale", &(pool->stale_shares), false);
root = api_add_uint(root, "Get Failures", &(pool->getfail_occasions), false);
root = api_add_uint(root, "Remote Failures", &(pool->remotefail_occasions), false);
root = api_add_escape(root, "User", pool->rpc_user, false);
root = api_add_time(root, "Last Share Time", &(pool->last_share_time), false);
root = api_add_int(root, "Diff1 Work", &(pool->diff1), false);
if (pool->rpc_proxy) {
root = api_add_const(root, "Proxy Type", proxytype(pool->rpc_proxytype), false);
root = api_add_escape(root, "Proxy", pool->rpc_proxy, false);
} else {
root = api_add_const(root, "Proxy Type", BLANK, false);
root = api_add_const(root, "Proxy", BLANK, false);
}
root = api_add_diff(root, "Difficulty Accepted", &(pool->diff_accepted), false);
root = api_add_diff(root, "Difficulty Rejected", &(pool->diff_rejected), false);
root = api_add_diff(root, "Difficulty Stale", &(pool->diff_stale), false);
if (isjson && (i > 0))
strcat(io_buffer, COMMA);
root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
}
if (isjson)
strcat(io_buffer, JSON_CLOSE);
}
static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
double utility, mhs, work_utility;
#ifdef WANT_CPUMINE
char *algo = (char *)(algo_names[opt_algo]);
if (algo == NULL)
algo = (char *)NULLSTR;
#endif
utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
mhs = total_mhashes_done / total_secs;
work_utility = total_diff1 / ( total_secs ? total_secs : 1 ) * 60;
sprintf(io_buffer, isjson
? "%s," JSON_SUMMARY
: "%s" _SUMMARY ",",
message(MSG_SUMM, 0, NULL, isjson));
root = api_add_elapsed(root, "Elapsed", &(total_secs), false);
#ifdef WANT_CPUMINE
root = api_add_string(root, "Algorithm", algo, false);
#endif
root = api_add_mhs(root, "MHS av", &(mhs), false);
root = api_add_uint(root, "Found Blocks", &(found_blocks), false);
root = api_add_int(root, "Getworks", &(total_getworks), false);
root = api_add_int(root, "Accepted", &(total_accepted), false);
root = api_add_int(root, "Rejected", &(total_rejected), false);
root = api_add_int(root, "Hardware Errors", &(hw_errors), false);
root = api_add_utility(root, "Utility", &(utility), false);
root = api_add_int(root, "Discarded", &(total_discarded), false);
root = api_add_int(root, "Stale", &(total_stale), false);
root = api_add_uint(root, "Get Failures", &(total_go), false);
root = api_add_uint(root, "Local Work", &(local_work), false);
root = api_add_uint(root, "Remote Failures", &(total_ro), false);
root = api_add_uint(root, "Network Blocks", &(new_blocks), false);
root = api_add_mhtotal(root, "Total MH", &(total_mhashes_done), false);
root = api_add_utility(root, "Work Utility", &(work_utility), false);
root = api_add_diff(root, "Difficulty Accepted", &(total_diff_accepted), false);
root = api_add_diff(root, "Difficulty Rejected", &(total_diff_rejected), false);
root = api_add_diff(root, "Difficulty Stale", &(total_diff_stale), false);
root = print_data(root, buf, isjson);
if (isjson)
strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
#ifdef HAVE_OPENCL
static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
struct thr_info *thr;
int gpu;
int id;
int i;
if (gpu_threads == 0) {
strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= nDevs) {
strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
return;
}
if (gpus[id].deven != DEV_DISABLED) {
strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
return;
}
for (i = 0; i < gpu_threads; i++) {
gpu = thr_info[i].cgpu->device_id;
if (gpu == id) {
thr = &thr_info[i];
if (thr->cgpu->status != LIFE_WELL) {
strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
return;
}
gpus[id].deven = DEV_ENABLED;
tq_push(thr->q, &ping);
}
}
strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
}
static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
int id;
if (nDevs == 0) {
strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= nDevs) {
strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
return;
}
if (gpus[id].deven == DEV_DISABLED) {
strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
return;
}
gpus[id].deven = DEV_DISABLED;
strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
}
static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
int id;
if (nDevs == 0) {
strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= nDevs) {
strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
return;
}
reinit_device(&gpus[id]);
strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
}
#endif
static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
int numgpu = 0;
#ifdef HAVE_OPENCL
numgpu = nDevs;
#endif
sprintf(io_buffer, isjson
? "%s," JSON_GPUS
: "%s" _GPUS ",",
message(MSG_NUMGPU, 0, NULL, isjson));
root = api_add_int(root, "Count", &numgpu, false);
root = print_data(root, buf, isjson);
if (isjson)
strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
int count = 0;
#ifdef HAVE_AN_FPGA
count = numpgas();
#endif
sprintf(io_buffer, isjson
? "%s," JSON_PGAS
: "%s" _PGAS ",",
message(MSG_NUMPGA, 0, NULL, isjson));
root = api_add_int(root, "Count", &count, false);
root = print_data(root, buf, isjson);
if (isjson)
strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
int count = 0;
#ifdef WANT_CPUMINE
count = opt_n_threads > 0 ? num_processors : 0;
#endif
sprintf(io_buffer, isjson
? "%s," JSON_CPUS
: "%s" _CPUS ",",
message(MSG_NUMCPU, 0, NULL, isjson));
root = api_add_int(root, "Count", &count, false);
root = print_data(root, buf, isjson);
if (isjson)
strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
struct pool *pool;
int id;
if (total_pools == 0) {
strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= total_pools) {
strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
return;
}
pool = pools[id];
pool->enabled = POOL_ENABLED;
switch_pools(pool);
strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
}
static void copyadvanceafter(char ch, char **param, char **buf)
{
#define src_p (*param)
#define dst_b (*buf)
while (*src_p && *src_p != ch) {
if (*src_p == '\\' && *(src_p+1) != '\0')
src_p++;
*(dst_b++) = *(src_p++);
}
if (*src_p)
src_p++;
*(dst_b++) = '\0';
}
static bool pooldetails(char *param, char **url, char **user, char **pass)
{
char *ptr, *buf;
ptr = buf = malloc(strlen(param)+1);
if (unlikely(!buf))
quit(1, "Failed to malloc pooldetails buf");
*url = buf;
// copy url
copyadvanceafter(',', ¶m, &buf);
if (!(*param)) // missing user
goto exitsama;
*user = buf;
// copy user
copyadvanceafter(',', ¶m, &buf);
if (!*param) // missing pass
goto exitsama;
*pass = buf;
// copy pass
copyadvanceafter(',', ¶m, &buf);
return true;
exitsama:
free(ptr);
return false;
}
static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
char *url, *user, *pass;
char *ptr;
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
return;
}
if (!pooldetails(param, &url, &user, &pass)) {
ptr = escape_string(param, isjson);
strcpy(io_buffer, message(MSG_INVPDP, 0, ptr, isjson));
if (ptr != param)
free(ptr);
ptr = NULL;
return;
}
add_pool_details(true, url, user, pass);
ptr = escape_string(url, isjson);
strcpy(io_buffer, message(MSG_ADDPOOL, 0, ptr, isjson));
if (ptr != url)
free(ptr);
ptr = NULL;
}
static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
struct pool *pool;
int id;
if (total_pools == 0) {
strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= total_pools) {
strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
return;
}
pool = pools[id];
if (pool->enabled == POOL_ENABLED) {
strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
return;
}
pool->enabled = POOL_ENABLED;
if (pool->prio < current_pool()->prio)
switch_pools(pool);
strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
}
static void poolpriority(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
char *ptr, *next;
int i, pr, prio = 0;
// TODO: all cgminer code needs a mutex added everywhere for change
// access to total_pools and also parts of the pools[] array,
// just copying total_pools here wont solve that
if (total_pools == 0) {
strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
return;
}
bool pools_changed[total_pools];
int new_prio[total_pools];
for (i = 0; i < total_pools; ++i)
pools_changed[i] = false;
next = param;
while (next && *next) {
ptr = next;
next = strchr(ptr, ',');
if (next)
*(next++) = '\0';
i = atoi(ptr);
if (i < 0 || i >= total_pools) {
strcpy(io_buffer, message(MSG_INVPID, i, NULL, isjson));
return;
}
if (pools_changed[i]) {
strcpy(io_buffer, message(MSG_DUPPID, i, NULL, isjson));
return;
}
pools_changed[i] = true;
new_prio[i] = prio++;
}
// Only change them if no errors
for (i = 0; i < total_pools; i++) {
if (pools_changed[i])
pools[i]->prio = new_prio[i];
}
// In priority order, cycle through the unchanged pools and append them
for (pr = 0; pr < total_pools; pr++)
for (i = 0; i < total_pools; i++) {
if (!pools_changed[i] && pools[i]->prio == pr) {
pools[i]->prio = prio++;
pools_changed[i] = true;
break;
}
}
if (current_pool()->prio)
switch_pools(NULL);
strcpy(io_buffer, message(MSG_POOLPRIO, 0, NULL, isjson));
}
static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
struct pool *pool;
int id;
if (total_pools == 0) {
strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= total_pools) {
strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
return;
}
pool = pools[id];
if (pool->enabled == POOL_DISABLED) {
strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
return;
}
if (enabled_pools <= 1) {
strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
return;
}
pool->enabled = POOL_DISABLED;
if (pool == current_pool())
switch_pools(NULL);
strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
}
static void removepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
struct pool *pool;
char *rpc_url;
bool dofree = false;
int id;
if (total_pools == 0) {
strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
return;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
return;
}
id = atoi(param);
if (id < 0 || id >= total_pools) {
strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
return;
}
if (total_pools <= 1) {
strcpy(io_buffer, message(MSG_REMLASTP, id, NULL, isjson));
return;
}
pool = pools[id];
if (pool == current_pool())
switch_pools(NULL);
if (pool == current_pool()) {
strcpy(io_buffer, message(MSG_ACTPOOL, id, NULL, isjson));
return;
}
pool->enabled = POOL_DISABLED;
rpc_url = escape_string(pool->rpc_url, isjson);
if (rpc_url != pool->rpc_url)
dofree = true;
remove_pool(pool);
strcpy(io_buffer, message(MSG_REMPOOL, id, rpc_url, isjson));
if (dofree)
free(rpc_url);
rpc_url = NULL;
}
#ifdef HAVE_OPENCL
static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
{
int id;
char *gpusep;
if (nDevs == 0) {
strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
return false;
}
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
return false;
}
gpusep = strchr(param, GPUSEP);
if (gpusep == NULL) {
strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
return false;
}
*(gpusep++) = '\0';
id = atoi(param);
if (id < 0 || id >= nDevs) {
strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
return false;
}
*gpu = id;
*value = gpusep;
return true;
}
static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
int id;
char *value;
int intensity;
char intensitystr[7];
if (!splitgpuvalue(param, &id, &value, isjson))
return;
if (!strncasecmp(value, DYNAMIC, 1)) {
gpus[id].dynamic = true;
strcpy(intensitystr, DYNAMIC);
}
else {
intensity = atoi(value);
if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
return;
}
gpus[id].dynamic = false;
gpus[id].intensity = intensity;
sprintf(intensitystr, "%d", intensity);
}
strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
}
static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
#ifdef HAVE_ADL
int id;
char *value;
int clock;
if (!splitgpuvalue(param, &id, &value, isjson))
return;
clock = atoi(value);
if (set_memoryclock(id, clock))
strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
else
strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
#else
strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
#endif
}
static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
#ifdef HAVE_ADL
int id;
char *value;
int clock;
if (!splitgpuvalue(param, &id, &value, isjson))
return;
clock = atoi(value);
if (set_engineclock(id, clock))
strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
else
strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
#else
strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
#endif
}
static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
#ifdef HAVE_ADL
int id;
char *value;
int fan;
if (!splitgpuvalue(param, &id, &value, isjson))
return;
fan = atoi(value);
if (set_fanspeed(id, fan))
strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
else
strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
#else
strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
#endif
}
static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
#ifdef HAVE_ADL
int id;
char *value;
float vddc;
if (!splitgpuvalue(param, &id, &value, isjson))
return;
vddc = atof(value);
if (set_vddc(id, vddc))
strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
else
strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
#else
strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
#endif
}
#endif
void doquit(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
if (isjson)
strcpy(io_buffer, JSON_START JSON_BYE);
else
strcpy(io_buffer, _BYE);
bye = true;
do_a_quit = true;
}
void dorestart(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
if (isjson)
strcpy(io_buffer, JSON_START JSON_RESTART);
else
strcpy(io_buffer, _RESTART);
bye = true;
do_a_restart = true;
}
void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
}
void notifystatus(int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
char *reason;
if (cgpu->device_last_not_well == 0)
reason = REASON_NONE;
else
switch(cgpu->device_not_well_reason) {
case REASON_THREAD_FAIL_INIT:
reason = REASON_THREAD_FAIL_INIT_STR;
break;
case REASON_THREAD_ZERO_HASH:
reason = REASON_THREAD_ZERO_HASH_STR;
break;
case REASON_THREAD_FAIL_QUEUE:
reason = REASON_THREAD_FAIL_QUEUE_STR;
break;
case REASON_DEV_SICK_IDLE_60:
reason = REASON_DEV_SICK_IDLE_60_STR;
break;
case REASON_DEV_DEAD_IDLE_600:
reason = REASON_DEV_DEAD_IDLE_600_STR;
break;
case REASON_DEV_NOSTART:
reason = REASON_DEV_NOSTART_STR;
break;
case REASON_DEV_OVER_HEAT:
reason = REASON_DEV_OVER_HEAT_STR;
break;
case REASON_DEV_THERMAL_CUTOFF:
reason = REASON_DEV_THERMAL_CUTOFF_STR;
break;
case REASON_DEV_COMMS_ERROR:
reason = REASON_DEV_COMMS_ERROR_STR;
break;
default:
reason = REASON_UNKNOWN_STR;
break;
}
// ALL counters (and only counters) must start the name with a '*'
// Simplifies future external support for identifying new counters
root = api_add_int(root, "NOTIFY", &device, false);
root = api_add_string(root, "Name", cgpu->api->name, false);
root = api_add_int(root, "ID", &(cgpu->device_id), false);
root = api_add_time(root, "Last Well", &(cgpu->device_last_well), false);
root = api_add_time(root, "Last Not Well", &(cgpu->device_last_not_well), false);
root = api_add_string(root, "Reason Not Well", reason, false);
root = api_add_int(root, "*Thread Fail Init", &(cgpu->thread_fail_init_count), false);
root = api_add_int(root, "*Thread Zero Hash", &(cgpu->thread_zero_hash_count), false);
root = api_add_int(root, "*Thread Fail Queue", &(cgpu->thread_fail_queue_count), false);
root = api_add_int(root, "*Dev Sick Idle 60s", &(cgpu->dev_sick_idle_60_count), false);
root = api_add_int(root, "*Dev Dead Idle 600s", &(cgpu->dev_dead_idle_600_count), false);
root = api_add_int(root, "*Dev Nostart", &(cgpu->dev_nostart_count), false);
root = api_add_int(root, "*Dev Over Heat", &(cgpu->dev_over_heat_count), false);
root = api_add_int(root, "*Dev Thermal Cutoff", &(cgpu->dev_thermal_cutoff_count), false);
root = api_add_int(root, "*Dev Comms Error", &(cgpu->dev_comms_error_count), false);
root = api_add_int(root, "*Dev Throttle", &(cgpu->dev_throttle_count), false);
if (isjson && (device > 0))
strcat(io_buffer, COMMA);
root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
}
static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
{
int i;
if (total_devices == 0) {
strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
return;
}
strcpy(io_buffer, message(MSG_NOTIFY, 0, NULL, isjson));
if (isjson) {
strcat(io_buffer, COMMA);
strcat(io_buffer, JSON_NOTIFY);
}
for (i = 0; i < total_devices; i++)
notifystatus(i, devices[i], isjson, group);
if (isjson)
strcat(io_buffer, JSON_CLOSE);
}
static void devdetails(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
struct cgpu_info *cgpu;
int i;
if (total_devices == 0) {
strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
return;
}
strcpy(io_buffer, message(MSG_DEVDETAILS, 0, NULL, isjson));
if (isjson) {
strcat(io_buffer, COMMA);
strcat(io_buffer, JSON_DEVDETAILS);
}
for (i = 0; i < total_devices; i++) {
cgpu = devices[i];
root = api_add_int(root, "DEVDETAILS", &i, false);
root = api_add_string(root, "Name", cgpu->api->name, false);
root = api_add_int(root, "ID", &(cgpu->device_id), false);
root = api_add_string(root, "Driver", cgpu->api->dname, false);
root = api_add_const(root, "Kernel", cgpu->kname ? : BLANK, false);
root = api_add_const(root, "Model", cgpu->name ? : BLANK, false);
root = api_add_const(root, "Device Path", cgpu->device_path ? : BLANK, false);
if (isjson && (i > 0))
strcat(io_buffer, COMMA);
root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
}
if (isjson)
strcat(io_buffer, JSON_CLOSE);
}
void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
char filename[PATH_MAX];
FILE *fcfg;
char *ptr;
if (param == NULL || *param == '\0') {
default_save_file(filename);
param = filename;
}
fcfg = fopen(param, "w");
if (!fcfg) {
ptr = escape_string(param, isjson);
strcpy(io_buffer, message(MSG_BADFN, 0, ptr, isjson));
if (ptr != param)
free(ptr);
ptr = NULL;
return;
}
write_config(fcfg);
fclose(fcfg);
ptr = escape_string(param, isjson);
strcpy(io_buffer, message(MSG_SAVED, 0, ptr, isjson));
if (ptr != param)
free(ptr);
ptr = NULL;
}
static int itemstats(int i, char *id, struct cgminer_stats *stats, struct cgminer_pool_stats *pool_stats, struct api_data *extra, bool isjson)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
root = api_add_int(root, "STATS", &i, false);
root = api_add_string(root, "ID", id, false);
root = api_add_elapsed(root, "Elapsed", &(total_secs), false);
root = api_add_uint32(root, "Calls", &(stats->getwork_calls), false);
root = api_add_timeval(root, "Wait", &(stats->getwork_wait), false);
root = api_add_timeval(root, "Max", &(stats->getwork_wait_max), false);
root = api_add_timeval(root, "Min", &(stats->getwork_wait_min), false);
if (pool_stats) {
root = api_add_uint32(root, "Pool Calls", &(pool_stats->getwork_calls), false);
root = api_add_uint32(root, "Pool Attempts", &(pool_stats->getwork_attempts), false);
root = api_add_timeval(root, "Pool Wait", &(pool_stats->getwork_wait), false);
root = api_add_timeval(root, "Pool Max", &(pool_stats->getwork_wait_max), false);
root = api_add_timeval(root, "Pool Min", &(pool_stats->getwork_wait_min), false);
root = api_add_double(root, "Pool Av", &(pool_stats->getwork_wait_rolling), false);
root = api_add_bool(root, "Work Had Roll Time", &(pool_stats->hadrolltime), false);
root = api_add_bool(root, "Work Can Roll", &(pool_stats->canroll), false);
root = api_add_bool(root, "Work Had Expire", &(pool_stats->hadexpire), false);
root = api_add_uint32(root, "Work Roll Time", &(pool_stats->rolltime), false);
}
if (extra)
root = api_add_extra(root, extra);
if (isjson && (i > 0))
strcat(io_buffer, COMMA);
root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
return ++i;
}
static void minerstats(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
struct api_data *extra;
char id[20];
int i, j;
strcpy(io_buffer, message(MSG_MINESTATS, 0, NULL, isjson));
if (isjson) {
strcat(io_buffer, COMMA);
strcat(io_buffer, JSON_MINESTATS);
}
i = 0;
for (j = 0; j < total_devices; j++) {
struct cgpu_info *cgpu = devices[j];
if (cgpu && cgpu->api) {
if (cgpu->api->get_api_stats)
extra = cgpu->api->get_api_stats(cgpu);
else
extra = NULL;
sprintf(id, "%s%d", cgpu->api->name, cgpu->device_id);
i = itemstats(i, id, &(cgpu->cgminer_stats), NULL, extra, isjson);
}
}
for (j = 0; j < total_pools; j++) {
struct pool *pool = pools[j];
sprintf(id, "POOL%d", j);
i = itemstats(i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, isjson);
}
if (isjson)
strcat(io_buffer, JSON_CLOSE);
}
static void failoveronly(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISBOOL, 0, NULL, isjson));
return;
}
*param = tolower(*param);
if (*param != 't' && *param != 'f') {
strcpy(io_buffer, message(MSG_INVBOOL, 0, NULL, isjson));
return;
}
bool tf = (*param == 't');
opt_fail_only = tf;
strcpy(io_buffer, message(MSG_FOO, tf, NULL, isjson));
}
static void minecoin(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
sprintf(io_buffer, isjson
? "%s," JSON_MINECOIN
: "%s" _MINECOIN ",",
message(MSG_MINECOIN, 0, NULL, isjson));
#ifdef USE_SCRYPT
if (opt_scrypt)
root = api_add_const(root, "Hash Method", SCRYPTSTR, false);
else
#endif
root = api_add_const(root, "Hash Method", SHA256STR, false);
mutex_lock(&ch_lock);
if (current_fullhash && *current_fullhash) {
root = api_add_timeval(root, "Current Block Time", &block_timeval, true);
root = api_add_string(root, "Current Block Hash", current_fullhash, true);
} else {
struct timeval t = {0,0};
root = api_add_timeval(root, "Current Block Time", &t, true);
root = api_add_const(root, "Current Block Hash", BLANK, false);
}
mutex_unlock(&ch_lock);
root = api_add_bool(root, "LP", &have_longpoll, false);
root = print_data(root, buf, isjson);
if (isjson)
strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
static void debugstate(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
if (param == NULL)
param = (char *)BLANK;
else
*param = tolower(*param);
switch(*param) {
case 's':
opt_realquiet = true;
break;
case 'q':
opt_quiet ^= true;
break;
case 'v':
opt_log_output ^= true;
if (opt_log_output)
opt_quiet = false;
break;
case 'd':
opt_debug ^= true;
opt_log_output = opt_debug;
if (opt_debug)
opt_quiet = false;
break;
case 'r':
opt_protocol ^= true;
if (opt_protocol)
opt_quiet = false;
break;
case 'p':
want_per_device_stats ^= true;
opt_log_output = want_per_device_stats;
break;
case 'n':
opt_log_output = false;
opt_debug = false;
opt_quiet = false;
opt_protocol = false;
want_per_device_stats = false;
opt_worktime = false;
break;
case 'w':
opt_worktime ^= true;
break;
default:
// anything else just reports the settings
break;
}
sprintf(io_buffer, isjson
? "%s," JSON_DEBUGSET
: "%s" _DEBUGSET ",",
message(MSG_DEBUGSET, 0, NULL, isjson));
root = api_add_bool(root, "Silent", &opt_realquiet, false);
root = api_add_bool(root, "Quiet", &opt_quiet, false);
root = api_add_bool(root, "Verbose", &opt_log_output, false);
root = api_add_bool(root, "Debug", &opt_debug, false);
root = api_add_bool(root, "RPCProto", &opt_protocol, false);
root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
root = api_add_bool(root, "WorkTime", &opt_worktime, false);
root = print_data(root, buf, isjson);
if (isjson)
strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
static void setconfig(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
{
char *comma;
int value;
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_CONPAR, 0, NULL, isjson));
return;
}
comma = strchr(param, ',');
if (!comma) {
strcpy(io_buffer, message(MSG_CONVAL, 0, param, isjson));
return;
}
*(comma++) = '\0';
value = atoi(comma);
if (value < 0 || value > 9999) {
strcpy(io_buffer, message(MSG_INVNUM, value, param, isjson));
return;
}
if (strcasecmp(param, "queue") == 0)
opt_queue = value;
else if (strcasecmp(param, "scantime") == 0)
opt_scantime = value;
else if (strcasecmp(param, "expiry") == 0)
opt_expiry = value;
else {
strcpy(io_buffer, message(MSG_UNKCON, 0, param, isjson));
return;
}
strcpy(io_buffer, message(MSG_SETCONFIG, value, param, isjson));
}
static void checkcommand(__maybe_unused SOCKETTYPE c, char *param, bool isjson, char group);
struct CMDS {
char *name;
void (*func)(SOCKETTYPE, char *, bool, char);
bool iswritemode;
} cmds[] = {
{ "version", apiversion, false },
{ "config", minerconfig, false },
{ "devs", devstatus, false },
{ "pools", poolstatus, false },
{ "summary", summary, false },
#ifdef HAVE_OPENCL
{ "gpuenable", gpuenable, true },
{ "gpudisable", gpudisable, true },
{ "gpurestart", gpurestart, true },
{ "gpu", gpudev, false },
#endif
#ifdef HAVE_AN_FPGA
{ "pga", pgadev, false },
{ "pgaenable", pgaenable, true },
{ "pgadisable", pgadisable, true },
{ "pgaidentify", pgaidentify, true },
#endif
#ifdef WANT_CPUMINE
{ "cpu", cpudev, false },
#endif
{ "gpucount", gpucount, false },
{ "pgacount", pgacount, false },
{ "cpucount", cpucount, false },
{ "switchpool", switchpool, true },
{ "addpool", addpool, true },
{ "poolpriority", poolpriority, true },
{ "enablepool", enablepool, true },
{ "disablepool", disablepool, true },
{ "removepool", removepool, true },
#ifdef HAVE_OPENCL
{ "gpuintensity", gpuintensity, true },
{ "gpumem", gpumem, true },
{ "gpuengine", gpuengine, true },
{ "gpufan", gpufan, true },
{ "gpuvddc", gpuvddc, true },
#endif
{ "save", dosave, true },
{ "quit", doquit, true },
{ "privileged", privileged, true },
{ "notify", notify, false },
{ "devdetails", devdetails, false },
{ "restart", dorestart, true },
{ "stats", minerstats, false },
{ "check", checkcommand, false },
{ "failover-only", failoveronly, true },
{ "coin", minecoin, false },
{ "debug", debugstate, true },
{ "setconfig", setconfig, true },
{ NULL, NULL, false }
};
static void checkcommand(__maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
char cmdbuf[100];
bool found, access;
int i;
if (param == NULL || *param == '\0') {
strcpy(io_buffer, message(MSG_MISCHK, 0, NULL, isjson));
return;
}
found = false;
access = false;
for (i = 0; cmds[i].name != NULL; i++) {
if (strcmp(cmds[i].name, param) == 0) {
found = true;
sprintf(cmdbuf, "|%s|", param);
if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
access = true;
break;
}
}
sprintf(io_buffer, isjson
? "%s," JSON_CHECK
: "%s" _CHECK ",",
message(MSG_CHECK, 0, NULL, isjson));
root = api_add_const(root, "Exists", found ? YES : NO, false);
root = api_add_const(root, "Access", access ? YES : NO, false);
root = print_data(root, buf, isjson);
if (isjson)
strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
static void send_result(SOCKETTYPE c, bool isjson)
{
int n;
int len;
if (isjson)
strcat(io_buffer, JSON_END);
len = strlen(io_buffer);
applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : BLANK);
// ignore failure - it's closed immediately anyway
n = send(c, io_buffer, len+1, 0);
if (opt_debug) {
if (SOCKETFAIL(n))
applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
else
applog(LOG_DEBUG, "API: sent %d", n);
}
}
static void tidyup(__maybe_unused void *arg)
{
mutex_lock(&quit_restart_lock);
bye = true;
if (sock != INVSOCK) {
shutdown(sock, SHUT_RDWR);
CLOSESOCKET(sock);
sock = INVSOCK;
}
if (ipaccess != NULL) {
free(ipaccess);
ipaccess = NULL;
}
if (msg_buffer != NULL) {
free(msg_buffer);
msg_buffer = NULL;
}
if (io_buffer != NULL) {
free(io_buffer);
io_buffer = NULL;
}
mutex_unlock(&quit_restart_lock);
}
/*
* Interpret --api-groups G:cmd1:cmd2:cmd3,P:cmd4,*,...
*/
static void setup_groups()
{
char *api_groups = opt_api_groups ? opt_api_groups : (char *)BLANK;
char *buf, *ptr, *next, *colon;
char group;
char commands[TMPBUFSIZ];
char cmdbuf[100];
char *cmd;
bool addstar, did;
int i;
buf = malloc(strlen(api_groups) + 1);
if (unlikely(!buf))
quit(1, "Failed to malloc ipgroups buf");
strcpy(buf, api_groups);
next = buf;
// for each group defined
while (next && *next) {
ptr = next;
next = strchr(ptr, ',');
if (next)
*(next++) = '\0';
// Validate the group
if (*(ptr+1) != ':') {
colon = strchr(ptr, ':');
if (colon)
*colon = '\0';
applog(LOG_WARNING, "API invalid group name '%s'", ptr);
quit(1, INVAPIGROUPS);
}
group = GROUP(*ptr);
if (!VALIDGROUP(group)) {
applog(LOG_WARNING, "API invalid group name '%c'", *ptr);
quit(1, INVAPIGROUPS);
}
if (group == PRIVGROUP) {
applog(LOG_WARNING, "API group name can't be '%c'", PRIVGROUP);
quit(1, INVAPIGROUPS);
}
if (group == NOPRIVGROUP) {
applog(LOG_WARNING, "API group name can't be '%c'", NOPRIVGROUP);
quit(1, INVAPIGROUPS);
}
if (apigroups[GROUPOFFSET(group)].commands != NULL) {
applog(LOG_WARNING, "API duplicate group name '%c'", *ptr);
quit(1, INVAPIGROUPS);
}
ptr += 2;
// Validate the command list (and handle '*')
cmd = &(commands[0]);
*(cmd++) = SEPARATOR;
*cmd = '\0';
addstar = false;
while (ptr && *ptr) {
colon = strchr(ptr, ':');
if (colon)
*(colon++) = '\0';
if (strcmp(ptr, "*") == 0)
addstar = true;
else {
did = false;
for (i = 0; cmds[i].name != NULL; i++) {
if (strcasecmp(ptr, cmds[i].name) == 0) {
did = true;
break;
}
}
if (did) {
// skip duplicates
sprintf(cmdbuf, "|%s|", cmds[i].name);
if (strstr(commands, cmdbuf) == NULL) {
strcpy(cmd, cmds[i].name);
cmd += strlen(cmds[i].name);
*(cmd++) = SEPARATOR;
*cmd = '\0';
}
} else {
applog(LOG_WARNING, "API unknown command '%s' in group '%c'", ptr, group);
quit(1, INVAPIGROUPS);
}
}
ptr = colon;
}
// * = allow all non-iswritemode commands
if (addstar) {
for (i = 0; cmds[i].name != NULL; i++) {
if (cmds[i].iswritemode == false) {
// skip duplicates
sprintf(cmdbuf, "|%s|", cmds[i].name);
if (strstr(commands, cmdbuf) == NULL) {
strcpy(cmd, cmds[i].name);
cmd += strlen(cmds[i].name);
*(cmd++) = SEPARATOR;
*cmd = '\0';
}
}
}
}
ptr = apigroups[GROUPOFFSET(group)].commands = malloc(strlen(commands) + 1);
if (unlikely(!ptr))
quit(1, "Failed to malloc group commands buf");
strcpy(ptr, commands);
}
// Now define R (NOPRIVGROUP) as all non-iswritemode commands
cmd = &(commands[0]);
*(cmd++) = SEPARATOR;
*cmd = '\0';
for (i = 0; cmds[i].name != NULL; i++) {
if (cmds[i].iswritemode == false) {
strcpy(cmd, cmds[i].name);
cmd += strlen(cmds[i].name);
*(cmd++) = SEPARATOR;
*cmd = '\0';
}
}
ptr = apigroups[GROUPOFFSET(NOPRIVGROUP)].commands = malloc(strlen(commands) + 1);
if (unlikely(!ptr))
quit(1, "Failed to malloc noprivgroup commands buf");
strcpy(ptr, commands);
// W (PRIVGROUP) is handled as a special case since it simply means all commands
free(buf);
return;
}
/*
* Interpret [W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
* special case of 0/0 allows /0 (means all IP addresses)
*/
#define ALLIP4 "0/0"
/*
* N.B. IP4 addresses are by Definition 32bit big endian on all platforms
*/
static void setup_ipaccess()
{
char *buf, *ptr, *comma, *slash, *dot;
int ipcount, mask, octet, i;
char group;
buf = malloc(strlen(opt_api_allow) + 1);
if (unlikely(!buf))
quit(1, "Failed to malloc ipaccess buf");
strcpy(buf, opt_api_allow);
ipcount = 1;
ptr = buf;
while (*ptr)
if (*(ptr++) == ',')
ipcount++;
// possibly more than needed, but never less
ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
if (unlikely(!ipaccess))
quit(1, "Failed to calloc ipaccess");
ips = 0;
ptr = buf;
while (ptr && *ptr) {
while (*ptr == ' ' || *ptr == '\t')
ptr++;
if (*ptr == ',') {
ptr++;
continue;
}
comma = strchr(ptr, ',');
if (comma)
*(comma++) = '\0';
group = NOPRIVGROUP;
if (isalpha(*ptr) && *(ptr+1) == ':') {
if (DEFINEDGROUP(*ptr))
group = GROUP(*ptr);
ptr += 2;
}
ipaccess[ips].group = group;
if (strcmp(ptr, ALLIP4) == 0)
ipaccess[ips].ip = ipaccess[ips].mask = 0;
else {
slash = strchr(ptr, '/');
if (!slash)
ipaccess[ips].mask = 0xffffffff;
else {
*(slash++) = '\0';
mask = atoi(slash);
if (mask < 1 || mask > 32)
goto popipo; // skip invalid/zero
ipaccess[ips].mask = 0;
while (mask-- >= 0) {
octet = 1 << (mask % 8);
ipaccess[ips].mask |= (octet << (24 - (8 * (mask >> 3))));
}
}
ipaccess[ips].ip = 0; // missing default to '.0'
for (i = 0; ptr && (i < 4); i++) {
dot = strchr(ptr, '.');
if (dot)
*(dot++) = '\0';
octet = atoi(ptr);
if (octet < 0 || octet > 0xff)
goto popipo; // skip invalid
ipaccess[ips].ip |= (octet << (24 - (i * 8)));
ptr = dot;
}
ipaccess[ips].ip &= ipaccess[ips].mask;
}
ips++;
popipo:
ptr = comma;
}
free(buf);
}
static void *quit_thread(__maybe_unused void *userdata)
{
// allow thread creator to finish whatever it's doing
mutex_lock(&quit_restart_lock);
mutex_unlock(&quit_restart_lock);
if (opt_debug)
applog(LOG_DEBUG, "API: killing cgminer");
kill_work();
return NULL;
}
static void *restart_thread(__maybe_unused void *userdata)
{
// allow thread creator to finish whatever it's doing
mutex_lock(&quit_restart_lock);
mutex_unlock(&quit_restart_lock);
if (opt_debug)
applog(LOG_DEBUG, "API: restarting cgminer");
app_restart();
return NULL;
}
void api(int api_thr_id)
{
struct thr_info bye_thr;
char buf[TMPBUFSIZ];
char param_buf[TMPBUFSIZ];
const char *localaddr = "127.0.0.1";
SOCKETTYPE c;
int n, bound;
char *connectaddr;
char *binderror;
time_t bindstart;
short int port = opt_api_port;
struct sockaddr_in serv;
struct sockaddr_in cli;
socklen_t clisiz;
char cmdbuf[100];
char *cmd;
char *param;
bool addrok;
char group;
json_error_t json_err;
json_t *json_config;
json_t *json_val;
bool isjson;
bool did;
int i;
mutex_init(&quit_restart_lock);
pthread_cleanup_push(tidyup, NULL);
my_thr_id = api_thr_id;
if (!opt_api_listen) {
applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
return;
}
setup_groups();
if (opt_api_allow) {
setup_ipaccess();
if (ips == 0) {
applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
return;
}
}
/* This should be done before curl in needed
* to ensure curl has already called WSAStartup() in windows */
sleep(opt_log_interval);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVSOCK) {
applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
return;
}
memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
if (!opt_api_allow && !opt_api_network) {
serv.sin_addr.s_addr = inet_addr(localaddr);
if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
return;
}
}
serv.sin_port = htons(port);
#ifndef WIN32
// On linux with SO_REUSEADDR, bind will get the port if the previous
// socket is closed (even if it is still in TIME_WAIT) but fail if
// another program has it open - which is what we want
int optval = 1;
// If it doesn't work, we don't really care - just show a debug message
if (SOCKETFAIL(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval))))
applog(LOG_DEBUG, "API setsockopt SO_REUSEADDR failed (ignored): %s", SOCKERRMSG);
#else
// On windows a 2nd program can bind to a port>1024 already in use unless
// SO_EXCLUSIVEADDRUSE is used - however then the bind to a closed port
// in TIME_WAIT will fail until the timeout - so we leave the options alone
#endif
// try for more than 1 minute ... in case the old one hasn't completely gone yet
bound = 0;
bindstart = time(NULL);
while (bound == 0) {
if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
binderror = SOCKERRMSG;
if ((time(NULL) - bindstart) > 61)
break;
else {
applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
sleep(30);
}
} else
bound = 1;
}
if (bound == 0) {
applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
return;
}
if (SOCKETFAIL(listen(sock, QUEUE))) {
applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
CLOSESOCKET(sock);
return;
}
if (opt_api_allow)
applog(LOG_WARNING, "API running in IP access mode on port %d", port);
else {
if (opt_api_network)
applog(LOG_WARNING, "API running in UNRESTRICTED read access mode on port %d", port);
else
applog(LOG_WARNING, "API running in local read access mode on port %d", port);
}
io_buffer = malloc(MYBUFSIZ+1);
msg_buffer = malloc(MYBUFSIZ+1);
while (!bye) {
clisiz = sizeof(cli);
if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
goto die;
}
connectaddr = inet_ntoa(cli.sin_addr);
addrok = false;
group = NOPRIVGROUP;
if (opt_api_allow) {
int client_ip = htonl(cli.sin_addr.s_addr);
for (i = 0; i < ips; i++) {
if ((client_ip & ipaccess[i].mask) == ipaccess[i].ip) {
addrok = true;
group = ipaccess[i].group;
break;
}
}
} else {
if (opt_api_network)
addrok = true;
else
addrok = (strcmp(connectaddr, localaddr) == 0);
}
if (opt_debug)
applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
if (addrok) {
n = recv(c, &buf[0], TMPBUFSIZ-1, 0);
if (SOCKETFAIL(n))
buf[0] = '\0';
else
buf[n] = '\0';
if (opt_debug) {
if (SOCKETFAIL(n))
applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
else
applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
}
if (!SOCKETFAIL(n)) {
// the time of the request in now
when = time(NULL);
did = false;
if (*buf != ISJSON) {
isjson = false;
param = strchr(buf, SEPARATOR);
if (param != NULL)
*(param++) = '\0';
cmd = buf;
}
else {
isjson = true;
param = NULL;
#if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
json_config = json_loadb(buf, n, 0, &json_err);
#elif JANSSON_MAJOR_VERSION > 1
json_config = json_loads(buf, 0, &json_err);
#else
json_config = json_loads(buf, &json_err);
#endif
if (!json_is_object(json_config)) {
strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
send_result(c, isjson);
did = true;
}
else {
json_val = json_object_get(json_config, JSON_COMMAND);
if (json_val == NULL) {
strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
send_result(c, isjson);
did = true;
}
else {
if (!json_is_string(json_val)) {
strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
send_result(c, isjson);
did = true;
}
else {
cmd = (char *)json_string_value(json_val);
json_val = json_object_get(json_config, JSON_PARAMETER);
if (json_is_string(json_val))
param = (char *)json_string_value(json_val);
else if (json_is_integer(json_val)) {
sprintf(param_buf, "%d", (int)json_integer_value(json_val));
param = param_buf;
} else if (json_is_real(json_val)) {
sprintf(param_buf, "%f", (double)json_real_value(json_val));
param = param_buf;
}
}
}
}
}
if (!did)
for (i = 0; cmds[i].name != NULL; i++) {
if (strcmp(cmd, cmds[i].name) == 0) {
sprintf(cmdbuf, "|%s|", cmd);
if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
(cmds[i].func)(c, param, isjson, group);
else {
strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
}
send_result(c, isjson);
did = true;
break;
}
}
if (!did) {
strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
send_result(c, isjson);
}
}
}
CLOSESOCKET(c);
}
die:
/* Blank line fix for older compilers since pthread_cleanup_pop is a
* macro that gets confused by a label existing immediately before it
*/
;
pthread_cleanup_pop(true);
if (opt_debug)
applog(LOG_DEBUG, "API: terminating due to: %s",
do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
mutex_lock(&quit_restart_lock);
if (do_a_restart) {
if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
mutex_unlock(&quit_restart_lock);
quit(1, "API failed to initiate a restart - aborting");
}
pthread_detach(bye_thr.pth);
} else if (do_a_quit) {
if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
mutex_unlock(&quit_restart_lock);
quit(1, "API failed to initiate a clean quit - aborting");
}
pthread_detach(bye_thr.pth);
}
mutex_unlock(&quit_restart_lock);
}