util.c 15 KB

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