util.c 15 KB

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