cpu-miner.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * Copyright 2010 Jeff Garzik
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "cpuminer-config.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdbool.h>
  14. #include <stdint.h>
  15. #include <unistd.h>
  16. #include <sys/time.h>
  17. #include <time.h>
  18. #ifndef WIN32
  19. #include <sys/resource.h>
  20. #endif
  21. #include <getopt.h>
  22. #include <jansson.h>
  23. #include <curl/curl.h>
  24. #include "compat.h"
  25. #include "miner.h"
  26. #define PROGRAM_NAME "minerd"
  27. #define DEF_RPC_URL "http://127.0.0.1:8332/"
  28. #define DEF_RPC_USERNAME "rpcuser"
  29. #define DEF_RPC_PASSWORD "rpcpass"
  30. #define DEF_RPC_USERPASS DEF_RPC_USERNAME ":" DEF_RPC_PASSWORD
  31. enum workio_commands {
  32. WC_GET_WORK,
  33. WC_SUBMIT_WORK,
  34. };
  35. struct workio_cmd {
  36. enum workio_commands cmd;
  37. struct thr_info *thr;
  38. union {
  39. struct work *work;
  40. } u;
  41. };
  42. enum sha256_algos {
  43. ALGO_C, /* plain C */
  44. ALGO_4WAY, /* parallel SSE2 */
  45. ALGO_VIA, /* VIA padlock */
  46. ALGO_CRYPTOPP, /* Crypto++ (C) */
  47. ALGO_CRYPTOPP_ASM32, /* Crypto++ 32-bit assembly */
  48. ALGO_SSE2_64, /* SSE2 for x86_64 */
  49. };
  50. static const char *algo_names[] = {
  51. [ALGO_C] = "c",
  52. #ifdef WANT_SSE2_4WAY
  53. [ALGO_4WAY] = "4way",
  54. #endif
  55. #ifdef WANT_VIA_PADLOCK
  56. [ALGO_VIA] = "via",
  57. #endif
  58. [ALGO_CRYPTOPP] = "cryptopp",
  59. #ifdef WANT_CRYPTOPP_ASM32
  60. [ALGO_CRYPTOPP_ASM32] = "cryptopp_asm32",
  61. #endif
  62. #ifdef WANT_X8664_SSE2
  63. [ALGO_SSE2_64] = "sse2_64",
  64. #endif
  65. };
  66. bool opt_debug = false;
  67. bool opt_protocol = false;
  68. bool want_longpoll = true;
  69. bool have_longpoll = false;
  70. bool use_syslog = false;
  71. static bool opt_quiet = false;
  72. static int opt_retries = 10;
  73. static int opt_fail_pause = 30;
  74. int opt_scantime = 5;
  75. static json_t *opt_config;
  76. static const bool opt_time = true;
  77. static enum sha256_algos opt_algo = ALGO_C;
  78. static int opt_n_threads = 1;
  79. static char *rpc_url;
  80. static char *rpc_userpass;
  81. static char *rpc_user, *rpc_pass;
  82. struct thr_info *thr_info;
  83. static int work_thr_id;
  84. int longpoll_thr_id;
  85. struct work_restart *work_restart = NULL;
  86. pthread_mutex_t time_lock;
  87. struct option_help {
  88. const char *name;
  89. const char *helptext;
  90. };
  91. static struct option_help options_help[] = {
  92. { "help",
  93. "(-h) Display this help text" },
  94. { "config FILE",
  95. "(-c FILE) JSON-format configuration file (default: none)\n"
  96. "See example-cfg.json for an example configuration." },
  97. { "algo XXX",
  98. "(-a XXX) Specify sha256 implementation:\n"
  99. "\tc\t\tLinux kernel sha256, implemented in C (default)"
  100. #ifdef WANT_SSE2_4WAY
  101. "\n\t4way\t\ttcatm's 4-way SSE2 implementation"
  102. #endif
  103. #ifdef WANT_VIA_PADLOCK
  104. "\n\tvia\t\tVIA padlock implementation"
  105. #endif
  106. "\n\tcryptopp\tCrypto++ C/C++ implementation"
  107. #ifdef WANT_CRYPTOPP_ASM32
  108. "\n\tcryptopp_asm32\tCrypto++ 32-bit assembler implementation"
  109. #endif
  110. #ifdef WANT_X8664_SSE2
  111. "\n\tsse2_64\t\tSSE2 implementation for x86_64 machines"
  112. #endif
  113. },
  114. { "quiet",
  115. "(-q) Disable per-thread hashmeter output (default: off)" },
  116. { "debug",
  117. "(-D) Enable debug output (default: off)" },
  118. { "no-longpoll",
  119. "Disable X-Long-Polling support (default: enabled)" },
  120. { "protocol-dump",
  121. "(-P) Verbose dump of protocol-level activities (default: off)" },
  122. { "retries N",
  123. "(-r N) Number of times to retry, if JSON-RPC call fails\n"
  124. "\t(default: 10; use -1 for \"never\")" },
  125. { "retry-pause N",
  126. "(-R N) Number of seconds to pause, between retries\n"
  127. "\t(default: 30)" },
  128. { "scantime N",
  129. "(-s N) Upper bound on time spent scanning current work,\n"
  130. "\tin seconds. (default: 5)" },
  131. #ifdef HAVE_SYSLOG_H
  132. { "syslog",
  133. "Use system log for output messages (default: standard error)" },
  134. #endif
  135. { "threads N",
  136. "(-t N) Number of miner threads (default: 1)" },
  137. { "url URL",
  138. "URL for bitcoin JSON-RPC server "
  139. "(default: " DEF_RPC_URL ")" },
  140. { "userpass USERNAME:PASSWORD",
  141. "Username:Password pair for bitcoin JSON-RPC server "
  142. "(default: " DEF_RPC_USERPASS ")" },
  143. { "user USERNAME",
  144. "(-u USERNAME) Username for bitcoin JSON-RPC server "
  145. "(default: " DEF_RPC_USERNAME ")" },
  146. { "pass PASSWORD",
  147. "(-p PASSWORD) Password for bitcoin JSON-RPC server "
  148. "(default: " DEF_RPC_PASSWORD ")" },
  149. };
  150. static struct option options[] = {
  151. { "algo", 1, NULL, 'a' },
  152. { "config", 1, NULL, 'c' },
  153. { "debug", 0, NULL, 'D' },
  154. { "help", 0, NULL, 'h' },
  155. { "no-longpoll", 0, NULL, 1003 },
  156. { "pass", 1, NULL, 'p' },
  157. { "protocol-dump", 0, NULL, 'P' },
  158. { "quiet", 0, NULL, 'q' },
  159. { "threads", 1, NULL, 't' },
  160. { "retries", 1, NULL, 'r' },
  161. { "retry-pause", 1, NULL, 'R' },
  162. { "scantime", 1, NULL, 's' },
  163. #ifdef HAVE_SYSLOG_H
  164. { "syslog", 0, NULL, 1004 },
  165. #endif
  166. { "url", 1, NULL, 1001 },
  167. { "user", 1, NULL, 'u' },
  168. { "userpass", 1, NULL, 1002 },
  169. { }
  170. };
  171. struct work {
  172. unsigned char data[128];
  173. unsigned char hash1[64];
  174. unsigned char midstate[32];
  175. unsigned char target[32];
  176. unsigned char hash[32];
  177. };
  178. static bool jobj_binary(const json_t *obj, const char *key,
  179. void *buf, size_t buflen)
  180. {
  181. const char *hexstr;
  182. json_t *tmp;
  183. tmp = json_object_get(obj, key);
  184. if (!tmp) {
  185. applog(LOG_ERR, "JSON key '%s' not found", key);
  186. return false;
  187. }
  188. hexstr = json_string_value(tmp);
  189. if (!hexstr) {
  190. applog(LOG_ERR, "JSON key '%s' is not a string", key);
  191. return false;
  192. }
  193. if (!hex2bin(buf, hexstr, buflen))
  194. return false;
  195. return true;
  196. }
  197. static bool work_decode(const json_t *val, struct work *work)
  198. {
  199. if (!jobj_binary(val, "midstate",
  200. work->midstate, sizeof(work->midstate))) {
  201. applog(LOG_ERR, "JSON inval midstate");
  202. goto err_out;
  203. }
  204. if (!jobj_binary(val, "data", work->data, sizeof(work->data))) {
  205. applog(LOG_ERR, "JSON inval data");
  206. goto err_out;
  207. }
  208. if (!jobj_binary(val, "hash1", work->hash1, sizeof(work->hash1))) {
  209. applog(LOG_ERR, "JSON inval hash1");
  210. goto err_out;
  211. }
  212. if (!jobj_binary(val, "target", work->target, sizeof(work->target))) {
  213. applog(LOG_ERR, "JSON inval target");
  214. goto err_out;
  215. }
  216. memset(work->hash, 0, sizeof(work->hash));
  217. return true;
  218. err_out:
  219. return false;
  220. }
  221. static bool submit_upstream_work(CURL *curl, const struct work *work)
  222. {
  223. char *hexstr = NULL;
  224. json_t *val, *res;
  225. char s[345];
  226. bool rc = false;
  227. /* build hex string */
  228. hexstr = bin2hex(work->data, sizeof(work->data));
  229. if (!hexstr) {
  230. applog(LOG_ERR, "submit_upstream_work OOM");
  231. goto out;
  232. }
  233. /* build JSON-RPC request */
  234. sprintf(s,
  235. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  236. hexstr);
  237. if (opt_debug)
  238. applog(LOG_DEBUG, "DBG: sending RPC call: %s", s);
  239. /* issue JSON-RPC request */
  240. val = json_rpc_call(curl, rpc_url, rpc_userpass, s, false, false);
  241. if (!val) {
  242. applog(LOG_ERR, "submit_upstream_work json_rpc_call failed");
  243. goto out;
  244. }
  245. res = json_object_get(val, "result");
  246. applog(LOG_INFO, "PROOF OF WORK RESULT: %s",
  247. json_is_true(res) ? "true (yay!!!)" : "false (booooo)");
  248. json_decref(val);
  249. rc = true;
  250. out:
  251. free(hexstr);
  252. return rc;
  253. }
  254. static const char *rpc_req =
  255. "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
  256. static bool get_upstream_work(CURL *curl, struct work *work)
  257. {
  258. json_t *val;
  259. bool rc;
  260. val = json_rpc_call(curl, rpc_url, rpc_userpass, rpc_req,
  261. want_longpoll, false);
  262. if (!val)
  263. return false;
  264. rc = work_decode(json_object_get(val, "result"), work);
  265. json_decref(val);
  266. return rc;
  267. }
  268. static void workio_cmd_free(struct workio_cmd *wc)
  269. {
  270. if (!wc)
  271. return;
  272. switch (wc->cmd) {
  273. case WC_SUBMIT_WORK:
  274. free(wc->u.work);
  275. break;
  276. default: /* do nothing */
  277. break;
  278. }
  279. memset(wc, 0, sizeof(*wc)); /* poison */
  280. free(wc);
  281. }
  282. static bool workio_get_work(struct workio_cmd *wc, CURL *curl)
  283. {
  284. struct work *ret_work;
  285. int failures = 0;
  286. ret_work = calloc(1, sizeof(*ret_work));
  287. if (!ret_work)
  288. return false;
  289. /* obtain new work from bitcoin via JSON-RPC */
  290. while (!get_upstream_work(curl, ret_work)) {
  291. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  292. applog(LOG_ERR, "json_rpc_call failed, terminating workio thread");
  293. free(ret_work);
  294. return false;
  295. }
  296. /* pause, then restart work-request loop */
  297. applog(LOG_ERR, "json_rpc_call failed, retry after %d seconds",
  298. opt_fail_pause);
  299. sleep(opt_fail_pause);
  300. }
  301. /* send work to requesting thread */
  302. if (!tq_push(wc->thr->q, ret_work))
  303. free(ret_work);
  304. return true;
  305. }
  306. static bool workio_submit_work(struct workio_cmd *wc, CURL *curl)
  307. {
  308. int failures = 0;
  309. /* submit solution to bitcoin via JSON-RPC */
  310. while (!submit_upstream_work(curl, wc->u.work)) {
  311. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  312. applog(LOG_ERR, "...terminating workio thread");
  313. return false;
  314. }
  315. /* pause, then restart work-request loop */
  316. applog(LOG_ERR, "...retry after %d seconds",
  317. opt_fail_pause);
  318. sleep(opt_fail_pause);
  319. }
  320. return true;
  321. }
  322. static void *workio_thread(void *userdata)
  323. {
  324. struct thr_info *mythr = userdata;
  325. CURL *curl;
  326. bool ok = true;
  327. curl = curl_easy_init();
  328. if (!curl) {
  329. applog(LOG_ERR, "CURL initialization failed");
  330. return NULL;
  331. }
  332. while (ok) {
  333. struct workio_cmd *wc;
  334. /* wait for workio_cmd sent to us, on our queue */
  335. wc = tq_pop(mythr->q, NULL);
  336. if (!wc) {
  337. ok = false;
  338. break;
  339. }
  340. /* process workio_cmd */
  341. switch (wc->cmd) {
  342. case WC_GET_WORK:
  343. ok = workio_get_work(wc, curl);
  344. break;
  345. case WC_SUBMIT_WORK:
  346. ok = workio_submit_work(wc, curl);
  347. break;
  348. default: /* should never happen */
  349. ok = false;
  350. break;
  351. }
  352. workio_cmd_free(wc);
  353. }
  354. tq_freeze(mythr->q);
  355. curl_easy_cleanup(curl);
  356. return NULL;
  357. }
  358. static void hashmeter(int thr_id, const struct timeval *diff,
  359. unsigned long hashes_done)
  360. {
  361. double khashes, secs;
  362. khashes = hashes_done / 1000.0;
  363. secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
  364. if (!opt_quiet)
  365. applog(LOG_INFO, "thread %d: %lu hashes, %.2f khash/sec",
  366. thr_id, hashes_done,
  367. khashes / secs);
  368. }
  369. static bool get_work(struct thr_info *thr, struct work *work)
  370. {
  371. struct workio_cmd *wc;
  372. struct work *work_heap;
  373. /* fill out work request message */
  374. wc = calloc(1, sizeof(*wc));
  375. if (!wc)
  376. return false;
  377. wc->cmd = WC_GET_WORK;
  378. wc->thr = thr;
  379. /* send work request to workio thread */
  380. if (!tq_push(thr_info[work_thr_id].q, wc)) {
  381. workio_cmd_free(wc);
  382. return false;
  383. }
  384. /* wait for response, a unit of work */
  385. work_heap = tq_pop(thr->q, NULL);
  386. if (!work_heap)
  387. return false;
  388. /* copy returned work into storage provided by caller */
  389. memcpy(work, work_heap, sizeof(*work));
  390. free(work_heap);
  391. return true;
  392. }
  393. static bool submit_work(struct thr_info *thr, const struct work *work_in)
  394. {
  395. struct workio_cmd *wc;
  396. /* fill out work request message */
  397. wc = calloc(1, sizeof(*wc));
  398. if (!wc)
  399. return false;
  400. wc->u.work = malloc(sizeof(*work_in));
  401. if (!wc->u.work)
  402. goto err_out;
  403. wc->cmd = WC_SUBMIT_WORK;
  404. wc->thr = thr;
  405. memcpy(wc->u.work, work_in, sizeof(*work_in));
  406. /* send solution to workio thread */
  407. if (!tq_push(thr_info[work_thr_id].q, wc))
  408. goto err_out;
  409. return true;
  410. err_out:
  411. workio_cmd_free(wc);
  412. return false;
  413. }
  414. static void *miner_thread(void *userdata)
  415. {
  416. struct thr_info *mythr = userdata;
  417. int thr_id = mythr->id;
  418. uint32_t max_nonce = 0xffffff;
  419. while (1) {
  420. struct work work __attribute__((aligned(128)));
  421. unsigned long hashes_done;
  422. struct timeval tv_start, tv_end, diff;
  423. uint64_t max64;
  424. bool rc;
  425. /* obtain new work from internal workio thread */
  426. if (!get_work(mythr, &work)) {
  427. applog(LOG_ERR, "work retrieval failed, exiting "
  428. "mining thread %d", mythr->id);
  429. goto out;
  430. }
  431. hashes_done = 0;
  432. gettimeofday(&tv_start, NULL);
  433. /* scan nonces for a proof-of-work hash */
  434. switch (opt_algo) {
  435. case ALGO_C:
  436. rc = scanhash_c(thr_id, work.midstate, work.data + 64,
  437. work.hash1, work.hash, work.target,
  438. max_nonce, &hashes_done);
  439. break;
  440. #ifdef WANT_X8664_SSE2
  441. case ALGO_SSE2_64: {
  442. unsigned int rc5 =
  443. scanhash_sse2_64(thr_id, work.midstate, work.data + 64,
  444. work.hash1, work.hash,
  445. work.target,
  446. max_nonce, &hashes_done);
  447. rc = (rc5 == -1) ? false : true;
  448. }
  449. break;
  450. #endif
  451. #ifdef WANT_SSE2_4WAY
  452. case ALGO_4WAY: {
  453. unsigned int rc4 =
  454. ScanHash_4WaySSE2(thr_id, work.midstate, work.data + 64,
  455. work.hash1, work.hash,
  456. work.target,
  457. max_nonce, &hashes_done);
  458. rc = (rc4 == -1) ? false : true;
  459. }
  460. break;
  461. #endif
  462. #ifdef WANT_VIA_PADLOCK
  463. case ALGO_VIA:
  464. rc = scanhash_via(thr_id, work.data, work.target,
  465. max_nonce, &hashes_done);
  466. break;
  467. #endif
  468. case ALGO_CRYPTOPP:
  469. rc = scanhash_cryptopp(thr_id, work.midstate, work.data + 64,
  470. work.hash1, work.hash, work.target,
  471. max_nonce, &hashes_done);
  472. break;
  473. #ifdef WANT_CRYPTOPP_ASM32
  474. case ALGO_CRYPTOPP_ASM32:
  475. rc = scanhash_asm32(thr_id, work.midstate, work.data + 64,
  476. work.hash1, work.hash, work.target,
  477. max_nonce, &hashes_done);
  478. break;
  479. #endif
  480. default:
  481. /* should never happen */
  482. goto out;
  483. }
  484. /* record scanhash elapsed time */
  485. gettimeofday(&tv_end, NULL);
  486. timeval_subtract(&diff, &tv_end, &tv_start);
  487. hashmeter(thr_id, &diff, hashes_done);
  488. /* adjust max_nonce to meet target scan time */
  489. if (diff.tv_usec > 500000)
  490. diff.tv_sec++;
  491. if (diff.tv_sec > 0) {
  492. max64 =
  493. ((uint64_t)hashes_done * opt_scantime) / diff.tv_sec;
  494. if (max64 > 0xfffffffaULL)
  495. max64 = 0xfffffffaULL;
  496. max_nonce = max64;
  497. }
  498. /* if nonce found, submit work */
  499. if (rc && !submit_work(mythr, &work))
  500. break;
  501. }
  502. out:
  503. tq_freeze(mythr->q);
  504. return NULL;
  505. }
  506. static void restart_threads(void)
  507. {
  508. int i;
  509. for (i = 0; i < opt_n_threads; i++)
  510. work_restart[i].restart = 1;
  511. }
  512. static void *longpoll_thread(void *userdata)
  513. {
  514. struct thr_info *mythr = userdata;
  515. CURL *curl = NULL;
  516. char *copy_start, *hdr_path, *lp_url = NULL;
  517. bool need_slash = false;
  518. int failures = 0;
  519. hdr_path = tq_pop(mythr->q, NULL);
  520. if (!hdr_path)
  521. goto out;
  522. /* full URL */
  523. if (strstr(hdr_path, "://")) {
  524. lp_url = hdr_path;
  525. hdr_path = NULL;
  526. }
  527. /* absolute path, on current server */
  528. else {
  529. copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path;
  530. if (rpc_url[strlen(rpc_url) - 1] != '/')
  531. need_slash = true;
  532. lp_url = malloc(strlen(rpc_url) + strlen(copy_start) + 2);
  533. if (!lp_url)
  534. goto out;
  535. sprintf(lp_url, "%s%s%s", rpc_url, need_slash ? "/" : "", copy_start);
  536. }
  537. applog(LOG_INFO, "Long-polling activated for %s", lp_url);
  538. curl = curl_easy_init();
  539. if (!curl) {
  540. applog(LOG_ERR, "CURL initialization failed");
  541. goto out;
  542. }
  543. while (1) {
  544. json_t *val;
  545. val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
  546. false, true);
  547. if (val) {
  548. failures = 0;
  549. json_decref(val);
  550. applog(LOG_INFO, "LONGPOLL detected new block");
  551. restart_threads();
  552. } else {
  553. if (failures++ < 10) {
  554. sleep(30);
  555. applog(LOG_ERR,
  556. "longpoll failed, sleeping for 30s");
  557. } else {
  558. applog(LOG_ERR,
  559. "longpoll failed, ending thread");
  560. goto out;
  561. }
  562. }
  563. }
  564. out:
  565. free(hdr_path);
  566. free(lp_url);
  567. tq_freeze(mythr->q);
  568. if (curl)
  569. curl_easy_cleanup(curl);
  570. return NULL;
  571. }
  572. static void show_usage(void)
  573. {
  574. int i;
  575. printf("minerd version %s\n\n", VERSION);
  576. printf("Usage:\tminerd [options]\n\nSupported options:\n");
  577. for (i = 0; i < ARRAY_SIZE(options_help); i++) {
  578. struct option_help *h;
  579. h = &options_help[i];
  580. printf("--%s\n%s\n\n", h->name, h->helptext);
  581. }
  582. exit(1);
  583. }
  584. static void parse_arg (int key, char *arg)
  585. {
  586. int v, i;
  587. switch(key) {
  588. case 'a':
  589. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  590. if (algo_names[i] &&
  591. !strcmp(arg, algo_names[i])) {
  592. opt_algo = i;
  593. break;
  594. }
  595. }
  596. if (i == ARRAY_SIZE(algo_names))
  597. show_usage();
  598. break;
  599. case 'c': {
  600. json_error_t err;
  601. if (opt_config)
  602. json_decref(opt_config);
  603. opt_config = json_load_file(arg, &err);
  604. if (!json_is_object(opt_config)) {
  605. applog(LOG_ERR, "JSON decode of %s failed", arg);
  606. show_usage();
  607. }
  608. break;
  609. }
  610. case 'q':
  611. opt_quiet = true;
  612. break;
  613. case 'D':
  614. opt_debug = true;
  615. break;
  616. case 'p':
  617. free(rpc_pass);
  618. rpc_pass = strdup(arg);
  619. break;
  620. case 'P':
  621. opt_protocol = true;
  622. break;
  623. case 'r':
  624. v = atoi(arg);
  625. if (v < -1 || v > 9999) /* sanity check */
  626. show_usage();
  627. opt_retries = v;
  628. break;
  629. case 'R':
  630. v = atoi(arg);
  631. if (v < 1 || v > 9999) /* sanity check */
  632. show_usage();
  633. opt_fail_pause = v;
  634. break;
  635. case 's':
  636. v = atoi(arg);
  637. if (v < 1 || v > 9999) /* sanity check */
  638. show_usage();
  639. opt_scantime = v;
  640. break;
  641. case 't':
  642. v = atoi(arg);
  643. if (v < 1 || v > 9999) /* sanity check */
  644. show_usage();
  645. opt_n_threads = v;
  646. break;
  647. case 'u':
  648. free(rpc_user);
  649. rpc_user = strdup(arg);
  650. break;
  651. case 1001: /* --url */
  652. if (strncmp(arg, "http://", 7) &&
  653. strncmp(arg, "https://", 8))
  654. show_usage();
  655. free(rpc_url);
  656. rpc_url = strdup(arg);
  657. break;
  658. case 1002: /* --userpass */
  659. if (!strchr(arg, ':'))
  660. show_usage();
  661. free(rpc_userpass);
  662. rpc_userpass = strdup(arg);
  663. break;
  664. case 1003:
  665. want_longpoll = false;
  666. break;
  667. case 1004:
  668. use_syslog = true;
  669. break;
  670. default:
  671. show_usage();
  672. }
  673. }
  674. static void parse_config(void)
  675. {
  676. int i;
  677. json_t *val;
  678. if (!json_is_object(opt_config))
  679. return;
  680. for (i = 0; i < ARRAY_SIZE(options); i++) {
  681. if (!options[i].name)
  682. break;
  683. if (!strcmp(options[i].name, "config"))
  684. continue;
  685. val = json_object_get(opt_config, options[i].name);
  686. if (!val)
  687. continue;
  688. if (options[i].has_arg && json_is_string(val)) {
  689. char *s = strdup(json_string_value(val));
  690. if (!s)
  691. break;
  692. parse_arg(options[i].val, s);
  693. free(s);
  694. } else if (!options[i].has_arg && json_is_true(val))
  695. parse_arg(options[i].val, "");
  696. else
  697. applog(LOG_ERR, "JSON option %s invalid",
  698. options[i].name);
  699. }
  700. }
  701. static void parse_cmdline(int argc, char *argv[])
  702. {
  703. int key;
  704. while (1) {
  705. key = getopt_long(argc, argv, "a:c:qDPr:s:t:h?", options, NULL);
  706. if (key < 0)
  707. break;
  708. parse_arg(key, optarg);
  709. }
  710. parse_config();
  711. }
  712. int main (int argc, char *argv[])
  713. {
  714. struct thr_info *thr;
  715. int i;
  716. rpc_url = strdup(DEF_RPC_URL);
  717. /* parse command line */
  718. parse_cmdline(argc, argv);
  719. if (!rpc_userpass) {
  720. if (!rpc_user || !rpc_pass) {
  721. applog(LOG_ERR, "No login credentials supplied");
  722. return 1;
  723. }
  724. rpc_userpass = malloc(strlen(rpc_user) + strlen(rpc_pass) + 2);
  725. if (!rpc_userpass)
  726. return 1;
  727. sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass);
  728. }
  729. pthread_mutex_init(&time_lock, NULL);
  730. #ifdef HAVE_SYSLOG_H
  731. if (use_syslog)
  732. openlog("cpuminer", LOG_PID, LOG_USER);
  733. #endif
  734. /* set our priority to the highest (aka "nicest, least intrusive") */
  735. if (setpriority(PRIO_PROCESS, 0, 19))
  736. perror("setpriority");
  737. work_restart = calloc(opt_n_threads, sizeof(*work_restart));
  738. if (!work_restart)
  739. return 1;
  740. thr_info = calloc(opt_n_threads + 2, sizeof(*thr));
  741. if (!thr_info)
  742. return 1;
  743. /* init workio thread info */
  744. work_thr_id = opt_n_threads;
  745. thr = &thr_info[work_thr_id];
  746. thr->id = work_thr_id;
  747. thr->q = tq_new();
  748. if (!thr->q)
  749. return 1;
  750. /* start work I/O thread */
  751. if (pthread_create(&thr->pth, NULL, workio_thread, thr)) {
  752. applog(LOG_ERR, "workio thread create failed");
  753. return 1;
  754. }
  755. /* init longpoll thread info */
  756. if (want_longpoll) {
  757. longpoll_thr_id = opt_n_threads + 1;
  758. thr = &thr_info[longpoll_thr_id];
  759. thr->id = longpoll_thr_id;
  760. thr->q = tq_new();
  761. if (!thr->q)
  762. return 1;
  763. /* start longpoll thread */
  764. if (pthread_create(&thr->pth, NULL, longpoll_thread, thr)) {
  765. applog(LOG_ERR, "longpoll thread create failed");
  766. return 1;
  767. }
  768. } else
  769. longpoll_thr_id = -1;
  770. /* start mining threads */
  771. for (i = 0; i < opt_n_threads; i++) {
  772. thr = &thr_info[i];
  773. thr->id = i;
  774. thr->q = tq_new();
  775. if (!thr->q)
  776. return 1;
  777. if (pthread_create(&thr->pth, NULL, miner_thread, thr)) {
  778. applog(LOG_ERR, "thread %d create failed", i);
  779. return 1;
  780. }
  781. sleep(1); /* don't pound RPC server all at once */
  782. }
  783. applog(LOG_INFO, "%d miner threads started, "
  784. "using SHA256 '%s' algorithm.",
  785. opt_n_threads,
  786. algo_names[opt_algo]);
  787. /* main loop - simply wait for workio thread to exit */
  788. pthread_join(thr_info[work_thr_id].pth, NULL);
  789. applog(LOG_INFO, "workio thread dead, exiting.");
  790. return 0;
  791. }