cpu-miner.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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 USERNAME) 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. copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path;
  523. if (rpc_url[strlen(rpc_url) - 1] != '/')
  524. need_slash = true;
  525. lp_url = malloc(strlen(rpc_url) + strlen(copy_start) + 2);
  526. if (!lp_url)
  527. goto out;
  528. sprintf(lp_url, "%s%s%s", rpc_url, need_slash ? "/" : "", copy_start);
  529. applog(LOG_INFO, "Long-polling activated for %s", lp_url);
  530. curl = curl_easy_init();
  531. if (!curl) {
  532. applog(LOG_ERR, "CURL initialization failed");
  533. goto out;
  534. }
  535. while (1) {
  536. json_t *val;
  537. val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
  538. false, true);
  539. if (val) {
  540. failures = 0;
  541. json_decref(val);
  542. applog(LOG_INFO, "LONGPOLL detected new block");
  543. restart_threads();
  544. } else {
  545. if (failures++ < 10) {
  546. sleep(30);
  547. applog(LOG_ERR,
  548. "longpoll failed, sleeping for 30s\n");
  549. } else {
  550. applog(LOG_ERR,
  551. "longpoll failed, ending thread\n");
  552. goto out;
  553. }
  554. }
  555. }
  556. out:
  557. free(hdr_path);
  558. free(lp_url);
  559. tq_freeze(mythr->q);
  560. if (curl)
  561. curl_easy_cleanup(curl);
  562. return NULL;
  563. }
  564. static void show_usage(void)
  565. {
  566. int i;
  567. printf("minerd version %s\n\n", VERSION);
  568. printf("Usage:\tminerd [options]\n\nSupported options:\n");
  569. for (i = 0; i < ARRAY_SIZE(options_help); i++) {
  570. struct option_help *h;
  571. h = &options_help[i];
  572. printf("--%s\n%s\n\n", h->name, h->helptext);
  573. }
  574. exit(1);
  575. }
  576. static void parse_arg (int key, char *arg)
  577. {
  578. int v, i;
  579. switch(key) {
  580. case 'a':
  581. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  582. if (algo_names[i] &&
  583. !strcmp(arg, algo_names[i])) {
  584. opt_algo = i;
  585. break;
  586. }
  587. }
  588. if (i == ARRAY_SIZE(algo_names))
  589. show_usage();
  590. break;
  591. case 'c': {
  592. json_error_t err;
  593. if (opt_config)
  594. json_decref(opt_config);
  595. opt_config = json_load_file(arg, &err);
  596. if (!json_is_object(opt_config)) {
  597. applog(LOG_ERR, "JSON decode of %s failed", arg);
  598. show_usage();
  599. }
  600. break;
  601. }
  602. case 'q':
  603. opt_quiet = true;
  604. break;
  605. case 'D':
  606. opt_debug = true;
  607. break;
  608. case 'p':
  609. free(rpc_pass);
  610. rpc_pass = strdup(arg);
  611. break;
  612. case 'P':
  613. opt_protocol = true;
  614. break;
  615. case 'r':
  616. v = atoi(arg);
  617. if (v < -1 || v > 9999) /* sanity check */
  618. show_usage();
  619. opt_retries = v;
  620. break;
  621. case 'R':
  622. v = atoi(arg);
  623. if (v < 1 || v > 9999) /* sanity check */
  624. show_usage();
  625. opt_fail_pause = v;
  626. break;
  627. case 's':
  628. v = atoi(arg);
  629. if (v < 1 || v > 9999) /* sanity check */
  630. show_usage();
  631. opt_scantime = v;
  632. break;
  633. case 't':
  634. v = atoi(arg);
  635. if (v < 1 || v > 9999) /* sanity check */
  636. show_usage();
  637. opt_n_threads = v;
  638. break;
  639. case 'u':
  640. free(rpc_user);
  641. rpc_user = strdup(arg);
  642. break;
  643. case 1001: /* --url */
  644. if (strncmp(arg, "http://", 7) &&
  645. strncmp(arg, "https://", 8))
  646. show_usage();
  647. free(rpc_url);
  648. rpc_url = strdup(arg);
  649. break;
  650. case 1002: /* --userpass */
  651. if (!strchr(arg, ':'))
  652. show_usage();
  653. free(rpc_userpass);
  654. rpc_userpass = strdup(arg);
  655. break;
  656. case 1003:
  657. want_longpoll = false;
  658. break;
  659. case 1004:
  660. use_syslog = true;
  661. break;
  662. default:
  663. show_usage();
  664. }
  665. }
  666. static void parse_config(void)
  667. {
  668. int i;
  669. json_t *val;
  670. if (!json_is_object(opt_config))
  671. return;
  672. for (i = 0; i < ARRAY_SIZE(options); i++) {
  673. if (!options[i].name)
  674. break;
  675. if (!strcmp(options[i].name, "config"))
  676. continue;
  677. val = json_object_get(opt_config, options[i].name);
  678. if (!val)
  679. continue;
  680. if (options[i].has_arg && json_is_string(val)) {
  681. char *s = strdup(json_string_value(val));
  682. if (!s)
  683. break;
  684. parse_arg(options[i].val, s);
  685. free(s);
  686. } else if (!options[i].has_arg && json_is_true(val))
  687. parse_arg(options[i].val, "");
  688. else
  689. applog(LOG_ERR, "JSON option %s invalid",
  690. options[i].name);
  691. }
  692. }
  693. static void parse_cmdline(int argc, char *argv[])
  694. {
  695. int key;
  696. while (1) {
  697. key = getopt_long(argc, argv, "a:c:qDPr:s:t:h?", options, NULL);
  698. if (key < 0)
  699. break;
  700. parse_arg(key, optarg);
  701. }
  702. parse_config();
  703. }
  704. int main (int argc, char *argv[])
  705. {
  706. struct thr_info *thr;
  707. int i;
  708. rpc_url = strdup(DEF_RPC_URL);
  709. rpc_userpass = strdup(DEF_RPC_USERPASS);
  710. /* parse command line */
  711. parse_cmdline(argc, argv);
  712. if (!rpc_userpass && rpc_user && rpc_pass) {
  713. rpc_userpass = malloc(strlen(rpc_user) + strlen(rpc_pass) + 2);
  714. if (!rpc_userpass)
  715. return 1;
  716. sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass);
  717. }
  718. pthread_mutex_init(&time_lock, NULL);
  719. #ifdef HAVE_SYSLOG_H
  720. if (use_syslog)
  721. openlog("cpuminer", LOG_PID, LOG_USER);
  722. #endif
  723. /* set our priority to the highest (aka "nicest, least intrusive") */
  724. if (setpriority(PRIO_PROCESS, 0, 19))
  725. perror("setpriority");
  726. work_restart = calloc(opt_n_threads, sizeof(*work_restart));
  727. if (!work_restart)
  728. return 1;
  729. thr_info = calloc(opt_n_threads + 2, sizeof(*thr));
  730. if (!thr_info)
  731. return 1;
  732. /* init workio thread info */
  733. work_thr_id = opt_n_threads;
  734. thr = &thr_info[work_thr_id];
  735. thr->id = work_thr_id;
  736. thr->q = tq_new();
  737. if (!thr->q)
  738. return 1;
  739. /* start work I/O thread */
  740. if (pthread_create(&thr->pth, NULL, workio_thread, thr)) {
  741. applog(LOG_ERR, "workio thread create failed");
  742. return 1;
  743. }
  744. /* init longpoll thread info */
  745. if (want_longpoll) {
  746. longpoll_thr_id = opt_n_threads + 1;
  747. thr = &thr_info[longpoll_thr_id];
  748. thr->id = longpoll_thr_id;
  749. thr->q = tq_new();
  750. if (!thr->q)
  751. return 1;
  752. /* start longpoll thread */
  753. if (pthread_create(&thr->pth, NULL, longpoll_thread, thr)) {
  754. applog(LOG_ERR, "longpoll thread create failed");
  755. return 1;
  756. }
  757. } else
  758. longpoll_thr_id = -1;
  759. /* start mining threads */
  760. for (i = 0; i < opt_n_threads; i++) {
  761. thr = &thr_info[i];
  762. thr->id = i;
  763. thr->q = tq_new();
  764. if (!thr->q)
  765. return 1;
  766. if (pthread_create(&thr->pth, NULL, miner_thread, thr)) {
  767. applog(LOG_ERR, "thread %d create failed", i);
  768. return 1;
  769. }
  770. sleep(1); /* don't pound RPC server all at once */
  771. }
  772. applog(LOG_INFO, "%d miner threads started, "
  773. "using SHA256 '%s' algorithm.\n",
  774. opt_n_threads,
  775. algo_names[opt_algo]);
  776. /* main loop - simply wait for workio thread to exit */
  777. pthread_join(thr_info[work_thr_id].pth, NULL);
  778. applog(LOG_INFO, "workio thread dead, exiting.");
  779. return 0;
  780. }