util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Copyright 2011 Con Kolivas
  3. * Copyright 2010 Jeff Garzik
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #define _GNU_SOURCE
  11. #include "config.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <jansson.h>
  18. #include <curl/curl.h>
  19. #include <time.h>
  20. #include <curses.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #ifndef WIN32
  25. # include <sys/socket.h>
  26. # include <netinet/in.h>
  27. # include <netinet/tcp.h>
  28. #else
  29. # include <winsock2.h>
  30. # include <mstcpip.h>
  31. #endif
  32. #include "miner.h"
  33. #include "elist.h"
  34. #if JANSSON_MAJOR_VERSION >= 2
  35. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  36. #else
  37. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  38. #endif
  39. bool successful_connect = false;
  40. struct timeval nettime;
  41. struct data_buffer {
  42. void *buf;
  43. size_t len;
  44. };
  45. struct upload_buffer {
  46. const void *buf;
  47. size_t len;
  48. };
  49. struct header_info {
  50. char *lp_path;
  51. bool has_rolltime;
  52. };
  53. struct tq_ent {
  54. void *data;
  55. struct list_head q_node;
  56. };
  57. void vapplog(int prio, const char *fmt, va_list ap)
  58. {
  59. extern bool use_curses;
  60. #ifdef HAVE_SYSLOG_H
  61. if (use_syslog) {
  62. vsyslog(prio, fmt, ap);
  63. }
  64. #else
  65. if (0) {}
  66. #endif
  67. else if (opt_log_output || prio <= LOG_NOTICE) {
  68. char *f;
  69. int len;
  70. struct timeval tv = { };
  71. struct tm tm;
  72. gettimeofday(&tv, NULL);
  73. localtime_r(&tv.tv_sec, &tm);
  74. len = 40 + strlen(fmt) + 22;
  75. f = alloca(len);
  76. sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d] %s\n",
  77. tm.tm_year + 1900,
  78. tm.tm_mon + 1,
  79. tm.tm_mday,
  80. tm.tm_hour,
  81. tm.tm_min,
  82. tm.tm_sec,
  83. fmt);
  84. /* Only output to stderr if it's not going to the screen as well */
  85. if (!isatty(fileno((FILE *)stderr))) {
  86. va_list apc;
  87. va_copy(apc, ap);
  88. vfprintf(stderr, f, apc); /* atomic write to stderr */
  89. fflush(stderr);
  90. }
  91. if (use_curses)
  92. log_curses(prio, f, ap);
  93. else {
  94. int len = strlen(f);
  95. strcpy(f + len - 1, " \n");
  96. log_curses(prio, f, ap);
  97. }
  98. }
  99. }
  100. void applog(int prio, const char *fmt, ...)
  101. {
  102. va_list ap;
  103. va_start(ap, fmt);
  104. vapplog(prio, fmt, ap);
  105. va_end(ap);
  106. }
  107. static void databuf_free(struct data_buffer *db)
  108. {
  109. if (!db)
  110. return;
  111. free(db->buf);
  112. memset(db, 0, sizeof(*db));
  113. }
  114. static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
  115. void *user_data)
  116. {
  117. struct data_buffer *db = user_data;
  118. size_t len = size * nmemb;
  119. size_t oldlen, newlen;
  120. void *newmem;
  121. static const unsigned char zero = 0;
  122. oldlen = db->len;
  123. newlen = oldlen + len;
  124. newmem = realloc(db->buf, newlen + 1);
  125. if (!newmem)
  126. return 0;
  127. db->buf = newmem;
  128. db->len = newlen;
  129. memcpy(db->buf + oldlen, ptr, len);
  130. memcpy(db->buf + newlen, &zero, 1); /* null terminate */
  131. return len;
  132. }
  133. static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
  134. void *user_data)
  135. {
  136. struct upload_buffer *ub = user_data;
  137. int len = size * nmemb;
  138. if (len > ub->len)
  139. len = ub->len;
  140. if (len) {
  141. memcpy(ptr, ub->buf, len);
  142. ub->buf += len;
  143. ub->len -= len;
  144. }
  145. return len;
  146. }
  147. static size_t resp_hdr_cb(void *ptr, size_t size, size_t nmemb, void *user_data)
  148. {
  149. struct header_info *hi = user_data;
  150. size_t remlen, slen, ptrlen = size * nmemb;
  151. char *rem, *val = NULL, *key = NULL;
  152. void *tmp;
  153. val = calloc(1, ptrlen);
  154. key = calloc(1, ptrlen);
  155. if (!key || !val)
  156. goto out;
  157. tmp = memchr(ptr, ':', ptrlen);
  158. if (!tmp || (tmp == ptr)) /* skip empty keys / blanks */
  159. goto out;
  160. slen = tmp - ptr;
  161. if ((slen + 1) == ptrlen) /* skip key w/ no value */
  162. goto out;
  163. memcpy(key, ptr, slen); /* store & nul term key */
  164. key[slen] = 0;
  165. rem = ptr + slen + 1; /* trim value's leading whitespace */
  166. remlen = ptrlen - slen - 1;
  167. while ((remlen > 0) && (isspace(*rem))) {
  168. remlen--;
  169. rem++;
  170. }
  171. memcpy(val, rem, remlen); /* store value, trim trailing ws */
  172. val[remlen] = 0;
  173. while ((*val) && (isspace(val[strlen(val) - 1]))) {
  174. val[strlen(val) - 1] = 0;
  175. }
  176. if (!*val) /* skip blank value */
  177. goto out;
  178. if (opt_protocol)
  179. applog(LOG_DEBUG, "HTTP hdr(%s): %s", key, val);
  180. if (!strcasecmp("X-Roll-Ntime", key)) {
  181. if (!strncasecmp("N", val, 1)) {
  182. if (opt_debug)
  183. applog(LOG_DEBUG, "X-Roll-Ntime: N found");
  184. } else {
  185. if (opt_debug)
  186. applog(LOG_DEBUG, "X-Roll-Ntime found");
  187. hi->has_rolltime = true;
  188. }
  189. }
  190. if (!strcasecmp("X-Long-Polling", key)) {
  191. hi->lp_path = val; /* steal memory reference */
  192. val = NULL;
  193. }
  194. out:
  195. free(key);
  196. free(val);
  197. return ptrlen;
  198. }
  199. #ifdef CURL_HAS_SOCKOPT
  200. int json_rpc_call_sockopt_cb(void *userdata, curl_socket_t fd, curlsocktype purpose)
  201. {
  202. int keepalive = 1;
  203. int tcp_keepcnt = 5;
  204. int tcp_keepidle = 120;
  205. int tcp_keepintvl = 120;
  206. #ifndef WIN32
  207. if (unlikely(setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive))))
  208. return 1;
  209. # ifdef __linux
  210. if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &tcp_keepcnt, sizeof(tcp_keepcnt))))
  211. return 1;
  212. if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &tcp_keepidle, sizeof(tcp_keepidle))))
  213. return 1;
  214. if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &tcp_keepintvl, sizeof(tcp_keepintvl))))
  215. return 1;
  216. # endif /* __linux */
  217. # ifdef __APPLE_CC__
  218. if (unlikely(setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &tcp_keepintvl, sizeof(tcp_keepintvl))))
  219. return 1;
  220. # endif /* __APPLE_CC__ */
  221. #else /* WIN32 */
  222. struct tcp_keepalive vals;
  223. vals.onoff = 1;
  224. vals.keepalivetime = tcp_keepidle * 1000;
  225. vals.keepaliveinterval = tcp_keepintvl * 1000;
  226. DWORD outputBytes;
  227. if (unlikely(WSAIoctl(fd, SIO_KEEPALIVE_VALS, &vals, sizeof(vals), NULL, 0, &outputBytes, NULL, NULL)))
  228. return 1;
  229. #endif /* WIN32 */
  230. return 0;
  231. }
  232. #endif
  233. static void last_nettime(struct timeval *last)
  234. {
  235. rd_lock(&netacc_lock);
  236. last->tv_sec = nettime.tv_sec;
  237. last->tv_usec = nettime.tv_usec;
  238. rd_unlock(&netacc_lock);
  239. }
  240. static void set_nettime(void)
  241. {
  242. wr_lock(&netacc_lock);
  243. gettimeofday(&nettime, NULL);
  244. wr_unlock(&netacc_lock);
  245. }
  246. json_t *json_rpc_call(CURL *curl, const char *url,
  247. const char *userpass, const char *rpc_req,
  248. bool probe, bool longpoll, bool *rolltime,
  249. struct pool *pool, bool share)
  250. {
  251. json_t *val, *err_val, *res_val;
  252. int rc;
  253. struct data_buffer all_data = { };
  254. struct upload_buffer upload_data;
  255. json_error_t err = { };
  256. struct curl_slist *headers = NULL;
  257. char len_hdr[64], user_agent_hdr[128];
  258. char curl_err_str[CURL_ERROR_SIZE];
  259. long timeout = longpoll ? (60 * 60) : 60;
  260. struct header_info hi = { };
  261. bool probing = false;
  262. /* it is assumed that 'curl' is freshly [re]initialised at this pt */
  263. curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
  264. #if 0 /* Disable curl debugging since it spews to stderr */
  265. if (opt_protocol)
  266. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  267. #endif
  268. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  269. curl_easy_setopt(curl, CURLOPT_URL, url);
  270. curl_easy_setopt(curl, CURLOPT_ENCODING, "");
  271. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  272. /* Shares are staggered already and delays in submission can be costly
  273. * so do not delay them */
  274. if (!opt_delaynet || share)
  275. curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
  276. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
  277. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
  278. curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
  279. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_data);
  280. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
  281. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  282. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, resp_hdr_cb);
  283. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &hi);
  284. curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);
  285. if (opt_socks_proxy) {
  286. curl_easy_setopt(curl, CURLOPT_PROXY, opt_socks_proxy);
  287. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  288. }
  289. if (userpass) {
  290. curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
  291. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  292. }
  293. #ifdef CURL_HAS_SOCKOPT
  294. if (longpoll)
  295. curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, json_rpc_call_sockopt_cb);
  296. #endif
  297. curl_easy_setopt(curl, CURLOPT_POST, 1);
  298. if (opt_protocol)
  299. applog(LOG_DEBUG, "JSON protocol request:\n%s", rpc_req);
  300. upload_data.buf = rpc_req;
  301. upload_data.len = strlen(rpc_req);
  302. sprintf(len_hdr, "Content-Length: %lu",
  303. (unsigned long) upload_data.len);
  304. sprintf(user_agent_hdr, "User-Agent: %s", PACKAGE_STRING);
  305. headers = curl_slist_append(headers,
  306. "Content-type: application/json");
  307. headers = curl_slist_append(headers,
  308. "X-Mining-Extensions: longpoll midstate rollntime");
  309. headers = curl_slist_append(headers, len_hdr);
  310. headers = curl_slist_append(headers, user_agent_hdr);
  311. headers = curl_slist_append(headers, "Expect:"); /* disable Expect hdr*/
  312. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  313. if (opt_delaynet) {
  314. /* Don't delay share submission, but still track the nettime */
  315. if (!share) {
  316. long long now_msecs, last_msecs;
  317. struct timeval now, last;
  318. gettimeofday(&now, NULL);
  319. last_nettime(&last);
  320. now_msecs = (long long)now.tv_sec * 1000;
  321. now_msecs += now.tv_usec / 1000;
  322. last_msecs = (long long)last.tv_sec * 1000;
  323. last_msecs += last.tv_usec / 1000;
  324. if (now_msecs > last_msecs && now_msecs - last_msecs < 250) {
  325. struct timespec rgtp;
  326. rgtp.tv_sec = 0;
  327. rgtp.tv_nsec = (250 - (now_msecs - last_msecs)) * 1000000;
  328. nanosleep(&rgtp, NULL);
  329. }
  330. }
  331. set_nettime();
  332. }
  333. rc = curl_easy_perform(curl);
  334. if (rc) {
  335. applog(LOG_INFO, "HTTP request failed: %s", curl_err_str);
  336. goto err_out;
  337. }
  338. if (!all_data.buf) {
  339. if (opt_debug)
  340. applog(LOG_DEBUG, "Empty data received in json_rpc_call.");
  341. goto err_out;
  342. }
  343. if (probing) {
  344. pool->probed = true;
  345. /* If X-Long-Polling was found, activate long polling */
  346. if (hi.lp_path)
  347. pool->hdr_path = hi.lp_path;
  348. else
  349. pool->hdr_path = NULL;
  350. }
  351. *rolltime = hi.has_rolltime;
  352. val = JSON_LOADS(all_data.buf, &err);
  353. if (!val) {
  354. applog(LOG_INFO, "JSON decode failed(%d): %s", err.line, err.text);
  355. if (opt_protocol)
  356. applog(LOG_DEBUG, "JSON protocol response:\n%s", all_data.buf);
  357. goto err_out;
  358. }
  359. if (opt_protocol) {
  360. char *s = json_dumps(val, JSON_INDENT(3));
  361. applog(LOG_DEBUG, "JSON protocol response:\n%s", s);
  362. free(s);
  363. }
  364. /* JSON-RPC valid response returns a non-null 'result',
  365. * and a null 'error'.
  366. */
  367. res_val = json_object_get(val, "result");
  368. err_val = json_object_get(val, "error");
  369. if (!res_val || json_is_null(res_val) ||
  370. (err_val && !json_is_null(err_val))) {
  371. char *s;
  372. if (err_val)
  373. s = json_dumps(err_val, JSON_INDENT(3));
  374. else
  375. s = strdup("(unknown reason)");
  376. applog(LOG_INFO, "JSON-RPC call failed: %s", s);
  377. free(s);
  378. goto err_out;
  379. }
  380. successful_connect = true;
  381. databuf_free(&all_data);
  382. curl_slist_free_all(headers);
  383. curl_easy_reset(curl);
  384. return val;
  385. err_out:
  386. databuf_free(&all_data);
  387. curl_slist_free_all(headers);
  388. curl_easy_reset(curl);
  389. if (!successful_connect)
  390. applog(LOG_DEBUG, "Failed to connect in json_rpc_call");
  391. curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
  392. return NULL;
  393. }
  394. char *bin2hex(const unsigned char *p, size_t len)
  395. {
  396. int i;
  397. char *s = malloc((len * 2) + 1);
  398. if (!s)
  399. return NULL;
  400. for (i = 0; i < len; i++)
  401. sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);
  402. return s;
  403. }
  404. bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
  405. {
  406. while (*hexstr && len) {
  407. char hex_byte[3];
  408. unsigned int v;
  409. if (!hexstr[1]) {
  410. applog(LOG_ERR, "hex2bin str truncated");
  411. return false;
  412. }
  413. hex_byte[0] = hexstr[0];
  414. hex_byte[1] = hexstr[1];
  415. hex_byte[2] = 0;
  416. if (sscanf(hex_byte, "%x", &v) != 1) {
  417. applog(LOG_ERR, "hex2bin sscanf '%s' failed", hex_byte);
  418. return false;
  419. }
  420. *p = (unsigned char) v;
  421. p++;
  422. hexstr += 2;
  423. len--;
  424. }
  425. return (len == 0 && *hexstr == 0) ? true : false;
  426. }
  427. /* Subtract the `struct timeval' values X and Y,
  428. storing the result in RESULT.
  429. Return 1 if the difference is negative, otherwise 0. */
  430. int
  431. timeval_subtract (
  432. struct timeval *result, struct timeval *x, struct timeval *y)
  433. {
  434. /* Perform the carry for the later subtraction by updating Y. */
  435. if (x->tv_usec < y->tv_usec) {
  436. int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  437. y->tv_usec -= 1000000 * nsec;
  438. y->tv_sec += nsec;
  439. }
  440. if (x->tv_usec - y->tv_usec > 1000000) {
  441. int nsec = (x->tv_usec - y->tv_usec) / 1000000;
  442. y->tv_usec += 1000000 * nsec;
  443. y->tv_sec -= nsec;
  444. }
  445. /* Compute the time remaining to wait.
  446. `tv_usec' is certainly positive. */
  447. result->tv_sec = x->tv_sec - y->tv_sec;
  448. result->tv_usec = x->tv_usec - y->tv_usec;
  449. /* Return 1 if result is negative. */
  450. return x->tv_sec < y->tv_sec;
  451. }
  452. bool fulltest(const unsigned char *hash, const unsigned char *target)
  453. {
  454. unsigned char hash_swap[32], target_swap[32];
  455. uint32_t *hash32 = (uint32_t *) hash_swap;
  456. uint32_t *target32 = (uint32_t *) target_swap;
  457. int i;
  458. bool rc = true;
  459. char *hash_str, *target_str;
  460. swap256(hash_swap, hash);
  461. swap256(target_swap, target);
  462. for (i = 0; i < 32/4; i++) {
  463. uint32_t h32tmp = swab32(hash32[i]);
  464. uint32_t t32tmp = target32[i];
  465. target32[i] = swab32(target32[i]); /* for printing */
  466. if (h32tmp > t32tmp) {
  467. rc = false;
  468. break;
  469. }
  470. if (h32tmp < t32tmp) {
  471. rc = true;
  472. break;
  473. }
  474. }
  475. if (opt_debug) {
  476. hash_str = bin2hex(hash_swap, 32);
  477. target_str = bin2hex(target_swap, 32);
  478. applog(LOG_DEBUG, " Proof: %s\nTarget: %s\nTrgVal? %s",
  479. hash_str,
  480. target_str,
  481. rc ? "YES (hash < target)" :
  482. "no (false positive; hash > target)");
  483. free(hash_str);
  484. free(target_str);
  485. }
  486. return rc;
  487. }
  488. struct thread_q *tq_new(void)
  489. {
  490. struct thread_q *tq;
  491. tq = calloc(1, sizeof(*tq));
  492. if (!tq)
  493. return NULL;
  494. INIT_LIST_HEAD(&tq->q);
  495. pthread_mutex_init(&tq->mutex, NULL);
  496. pthread_cond_init(&tq->cond, NULL);
  497. return tq;
  498. }
  499. void tq_free(struct thread_q *tq)
  500. {
  501. struct tq_ent *ent, *iter;
  502. if (!tq)
  503. return;
  504. list_for_each_entry_safe(ent, iter, &tq->q, q_node) {
  505. list_del(&ent->q_node);
  506. free(ent);
  507. }
  508. pthread_cond_destroy(&tq->cond);
  509. pthread_mutex_destroy(&tq->mutex);
  510. memset(tq, 0, sizeof(*tq)); /* poison */
  511. free(tq);
  512. }
  513. static void tq_freezethaw(struct thread_q *tq, bool frozen)
  514. {
  515. mutex_lock(&tq->mutex);
  516. tq->frozen = frozen;
  517. pthread_cond_signal(&tq->cond);
  518. mutex_unlock(&tq->mutex);
  519. }
  520. void tq_freeze(struct thread_q *tq)
  521. {
  522. tq_freezethaw(tq, true);
  523. }
  524. void tq_thaw(struct thread_q *tq)
  525. {
  526. tq_freezethaw(tq, false);
  527. }
  528. bool tq_push(struct thread_q *tq, void *data)
  529. {
  530. struct tq_ent *ent;
  531. bool rc = true;
  532. ent = calloc(1, sizeof(*ent));
  533. if (!ent)
  534. return false;
  535. ent->data = data;
  536. INIT_LIST_HEAD(&ent->q_node);
  537. mutex_lock(&tq->mutex);
  538. if (!tq->frozen) {
  539. list_add_tail(&ent->q_node, &tq->q);
  540. } else {
  541. free(ent);
  542. rc = false;
  543. }
  544. pthread_cond_signal(&tq->cond);
  545. mutex_unlock(&tq->mutex);
  546. return rc;
  547. }
  548. void *tq_pop(struct thread_q *tq, const struct timespec *abstime)
  549. {
  550. struct tq_ent *ent;
  551. void *rval = NULL;
  552. int rc;
  553. mutex_lock(&tq->mutex);
  554. if (!list_empty(&tq->q))
  555. goto pop;
  556. if (abstime)
  557. rc = pthread_cond_timedwait(&tq->cond, &tq->mutex, abstime);
  558. else
  559. rc = pthread_cond_wait(&tq->cond, &tq->mutex);
  560. if (rc)
  561. goto out;
  562. if (list_empty(&tq->q))
  563. goto out;
  564. pop:
  565. ent = list_entry(tq->q.next, struct tq_ent, q_node);
  566. rval = ent->data;
  567. list_del(&ent->q_node);
  568. free(ent);
  569. out:
  570. mutex_unlock(&tq->mutex);
  571. return rval;
  572. }
  573. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg)
  574. {
  575. int ret;
  576. ret = pthread_create(&thr->pth, attr, start, arg);
  577. return ret;
  578. }
  579. void thr_info_cancel(struct thr_info *thr)
  580. {
  581. if (!thr)
  582. return;
  583. if (thr->q)
  584. tq_freeze(thr->q);
  585. if (PTH(thr) != 0L) {
  586. pthread_cancel(thr->pth);
  587. PTH(thr) = 0L;
  588. }
  589. }
  590. bool get_dondata(char **url, char **userpass)
  591. {
  592. struct data_buffer all_data = { };
  593. char curl_err_str[CURL_ERROR_SIZE];
  594. CURL *curl = curl_easy_init();
  595. int rc;
  596. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  597. curl_easy_setopt(curl, CURLOPT_ENCODING, "");
  598. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  599. if (!opt_delaynet)
  600. curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
  601. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
  602. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
  603. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
  604. curl_easy_setopt(curl, CURLOPT_URL, "http://vds.kolivas.org/url");
  605. rc = curl_easy_perform(curl);
  606. if (rc) {
  607. applog(LOG_INFO, "HTTP request failed: %s", curl_err_str);
  608. goto err_out;
  609. }
  610. if (!all_data.buf)
  611. goto err_out;
  612. *url = strtok(all_data.buf, "\n");
  613. all_data.buf = NULL;
  614. databuf_free(&all_data);
  615. curl_easy_setopt(curl, CURLOPT_URL, "http://vds.kolivas.org/userpass");
  616. rc = curl_easy_perform(curl);
  617. if (rc) {
  618. applog(LOG_INFO, "HTTP request failed: %s", curl_err_str);
  619. goto err_out;
  620. }
  621. if (!all_data.buf)
  622. goto err_out;
  623. *userpass = strtok(all_data.buf, "\n");
  624. all_data.buf = NULL;
  625. databuf_free(&all_data);
  626. applog(LOG_INFO, "Donation URL: %s Userpass: %s", *url, *userpass);
  627. curl_easy_cleanup(curl);
  628. return true;
  629. err_out:
  630. databuf_free(&all_data);
  631. *url = NULL;
  632. *userpass = NULL;
  633. curl_easy_cleanup(curl);
  634. return false;
  635. }