forked from ghostlander/nsgminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
miner.c
8735 lines (7492 loc) · 230 KB
/
miner.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2011-2012 Con Kolivas
* Copyright 2011-2013 Luke Dashjr
* Copyright 2012-2013 Andrew Smith
* Copyright 2010 Jeff Garzik
* Copyright 2015-2017 John Doering
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "config.h"
#ifdef WIN32
#define FD_SETSIZE 4096
#endif
#ifdef HAVE_CURSES
#include <curses.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>
#include <stdarg.h>
#include <assert.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#ifndef WIN32
#include <sys/resource.h>
#include <sys/socket.h>
#else
#include <winsock2.h>
#endif
#include <ccan/opt/opt.h>
#include <jansson.h>
#include <curl/curl.h>
#include <libgen.h>
#include <sha2.h>
#include <blkmaker.h>
#include <blkmaker_jansson.h>
#include <blktemplate.h>
#if defined(USE_NEOSCRYPT) || defined(USE_SCRYPT)
#include "neoscrypt.h"
#endif
#include "compat.h"
#include "miner.h"
#include "findnonce.h"
#include "driver-cpu.h"
#include "driver-opencl.h"
#include "bench_block.h"
#ifdef HAVE_ADL
#include "adl.h"
#endif
bool opt_neoscrypt = false;
bool opt_scrypt = false;
bool opt_sha256d = false;
#ifdef USE_X6500
#include "ft232r.h"
#endif
#if defined(unix) || defined(__APPLE__)
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
#endif
#if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_MODMINER) || defined(USE_X6500) || defined(USE_ZTEX)
# define USE_FPGA
# define USE_FPGA_SERIAL
#endif
struct strategies strategies[] = {
{ "Failover" },
{ "Round Robin" },
{ "Rotate" },
{ "Load Balance" },
{ "Balance" },
};
static char packagename[256];
bool opt_protocol;
static bool opt_benchmark;
static bool want_longpoll = true;
static bool want_gbt = true;
static bool want_getwork = true;
#if BLKMAKER_VERSION > 1
struct _cbscript_t {
char *data;
size_t sz;
};
static struct _cbscript_t opt_coinbase_script;
static uint32_t template_nonce;
#endif
#if BLKMAKER_VERSION > 0
char *opt_coinbase_sig;
#endif
static bool want_stratum = true;
bool have_longpoll;
int opt_skip_checks;
bool want_per_device_stats;
bool use_syslog;
bool opt_quiet;
bool opt_realquiet;
bool opt_loginput;
bool opt_compact;
const int opt_cutofftemp = 95;
int opt_hysteresis = 3;
static int opt_retries = -1;
int opt_fail_pause = 5;
int opt_log_interval = 5;
int opt_queue = 1;
int opt_scantime = 60;
int opt_expiry = 120;
int opt_expiry_lp = 3600;
int opt_bench_algo = -1;
static const bool opt_time = true;
unsigned long long global_hashrate;
#ifdef HAVE_OPENCL
int opt_dynamic_interval = 7;
uint opencl_devnum;
int nDevs;
int opt_g_threads = 1;
int gpu_threads;
#endif
bool opt_restart = true;
static bool opt_nogpu;
struct list_head scan_devices;
bool opt_force_dev_init;
static signed int devices_enabled;
static bool opt_removedisabled;
int total_devices;
struct cgpu_info **devices;
bool have_opencl;
int opt_n_threads = -1;
int mining_threads;
int num_processors;
#ifdef HAVE_CURSES
bool use_curses = true;
#else
bool use_curses;
#endif
#ifndef HAVE_LIBUSB
const
#endif
bool have_libusb;
static bool opt_submit_stale = true;
static int opt_shares;
static int opt_submit_threads = 0x40;
bool opt_fail_only;
bool opt_autofan;
bool opt_autoengine;
bool opt_noadl;
bool opt_nonvml;
#if HAVE_ADL
bool adl_active = false;
#endif
#if HAVE_NVML
bool nvml_active = false;
#endif
char *opt_api_allow = NULL;
char *opt_api_groups;
char *opt_api_description = PACKAGE_STRING;
int opt_api_port = 4028;
bool opt_api_listen;
bool opt_api_network;
bool opt_delaynet;
bool opt_disable_pool;
char *opt_icarus_options = NULL;
char *opt_icarus_timing = NULL;
bool opt_worktime;
char *opt_kernel_path;
char *cgminer_path;
#if defined(USE_BITFORCE)
bool opt_bfl_noncerange;
#endif
#define QUIET (opt_quiet || opt_realquiet)
struct thr_info *thr_info;
static int gwsched_thr_id;
static int stage_thr_id;
static int watchpool_thr_id;
static int watchdog_thr_id;
#ifdef HAVE_CURSES
static int input_thr_id;
#endif
int gpur_thr_id;
static int api_thr_id;
static int total_threads;
pthread_mutex_t hash_lock;
static pthread_mutex_t qd_lock;
static pthread_mutex_t *stgd_lock;
pthread_mutex_t console_lock;
pthread_mutex_t ch_lock;
static pthread_rwlock_t blk_lock;
static pthread_mutex_t sshare_lock;
pthread_rwlock_t netacc_lock;
static pthread_mutex_t lp_lock;
static pthread_cond_t lp_cond;
pthread_mutex_t restart_lock;
pthread_cond_t restart_cond;
pthread_cond_t gws_cond;
bool shutting_down;
double total_mhashes_done;
static struct timeval total_tv_start, total_tv_end;
static struct timeval miner_started;
pthread_mutex_t control_lock;
pthread_mutex_t stats_lock;
static pthread_mutex_t submitting_lock;
static int total_submitting;
static struct list_head submit_waiting;
notifier_t submit_waiting_notifier;
int hw_errors;
int total_accepted, total_rejected;
int total_getworks, total_stale, total_discarded;
uint64_t total_bytes_xfer;
double total_diff_accepted, total_diff_rejected, total_diff_stale;
static int staged_rollable;
unsigned int new_blocks;
unsigned int found_blocks;
unsigned int local_work;
unsigned int total_go, total_ro;
struct pool **pools;
static struct pool *currentpool = NULL;
int total_pools, enabled_pools;
enum pool_strategy pool_strategy = POOL_FAILOVER;
int opt_rotate_period;
static int total_urls, total_users, total_passes;
static
#ifndef HAVE_CURSES
const
#endif
bool curses_active;
static char current_block[20];
static char current_hash[40];
static uint32_t current_block_id;
static char datestamp[40];
static char blocktime[32];
struct timeval block_timeval;
ullong current_diff = 0xFFFFFFFFFFFFFFFFULL;
static char best_share[8] = "0";
static char block_diff[8];
uint64_t best_diff = 0;
static bool known_blkheight_current;
static uint32_t known_blkheight;
static uint32_t known_blkheight_blkid;
struct block {
char hash[20];
UT_hash_handle hh;
int block_no;
};
static struct block *blocks = NULL;
int swork_id;
/* For creating a hash database of stratum shares submitted that have not had
* a response yet */
struct stratum_share {
UT_hash_handle hh;
bool block;
struct work *work;
int id;
};
static struct stratum_share *stratum_shares = NULL;
char *opt_socks_proxy = NULL;
static const char def_conf[] = "nsgminer.conf";
static bool config_loaded;
static int include_count;
#define JSON_INCLUDE_CONF "include"
#define JSON_LOAD_ERROR "JSON decode of file '%s' failed\n %s"
#define JSON_LOAD_ERROR_LEN strlen(JSON_LOAD_ERROR)
#define JSON_MAX_DEPTH 10
#define JSON_MAX_DEPTH_ERR "Too many levels of JSON includes (limit 10) or a loop"
#if defined(unix) || defined(__APPLE__)
static char *opt_stderr_cmd = NULL;
static int forkpid;
#endif // defined(unix)
bool ping = true;
struct sigaction termhandler, inthandler;
struct thread_q *getq;
static int total_work;
struct work *staged_work = NULL;
struct schedtime {
bool enable;
struct tm tm;
};
struct schedtime schedstart;
struct schedtime schedstop;
bool sched_paused;
static bool time_before(struct tm *tm1, struct tm *tm2)
{
if (tm1->tm_hour < tm2->tm_hour)
return true;
if (tm1->tm_hour == tm2->tm_hour && tm1->tm_min < tm2->tm_min)
return true;
return false;
}
static bool should_run(void)
{
struct timeval tv;
struct tm tm;
bool within_range;
if (!schedstart.enable && !schedstop.enable)
return true;
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &tm);
// NOTE: This is delicately balanced so that should_run is always false if schedstart==schedstop
if (time_before(&schedstop.tm, &schedstart.tm))
within_range = (time_before(&tm, &schedstop.tm) || !time_before(&tm, &schedstart.tm));
else
within_range = (time_before(&tm, &schedstop.tm) && !time_before(&tm, &schedstart.tm));
if (within_range && !schedstop.enable)
/* This is a once off event with no stop time set */
schedstart.enable = false;
return within_range;
}
void get_datestamp(char *f, struct timeval *tv)
{
struct tm _tm;
struct tm *tm = &_tm;
localtime_r(&tv->tv_sec, tm);
sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d]",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
}
void get_timestamp(char *f, struct timeval *tv)
{
struct tm _tm;
struct tm *tm = &_tm;
localtime_r(&tv->tv_sec, tm);
sprintf(f, "[%02d:%02d:%02d]",
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
}
static void applog_and_exit(const char *fmt, ...) FORMAT_SYNTAX_CHECK(printf, 1, 2);
static void applog_and_exit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vapplog(LOG_ERR, fmt, ap);
va_end(ap);
exit(1);
}
static pthread_mutex_t sharelog_lock;
static FILE *sharelog_file = NULL;
static void sharelog(const char*disposition, const struct work*work)
{
char *target, *hash, *data;
struct cgpu_info *cgpu;
unsigned long int t;
struct pool *pool;
int thr_id, rv;
char s[1024];
size_t ret;
if (!sharelog_file)
return;
thr_id = work->thr_id;
cgpu = thr_info[thr_id].cgpu;
pool = work->pool;
t = (unsigned long int)(work->tv_work_found.tv_sec);
target = bin2hex(work->target, sizeof(work->target));
hash = bin2hex(work->hash, sizeof(work->hash));
data = bin2hex(work->data, sizeof(work->data));
// timestamp,disposition,target,pool,dev,thr,sharehash,sharedata
rv = snprintf(s, sizeof(s), "%lu,%s,%s,%s,%s%u,%u,%s,%s\n", t, disposition, target, pool->rpc_url, cgpu->api->name, cgpu->device_id, thr_id, hash, data);
free(target);
free(hash);
free(data);
if (rv >= (int)(sizeof(s)))
s[sizeof(s) - 1] = '\0';
else if (rv < 0) {
applog(LOG_ERR, "sharelog printf error");
return;
}
mutex_lock(&sharelog_lock);
ret = fwrite(s, rv, 1, sharelog_file);
fflush(sharelog_file);
mutex_unlock(&sharelog_lock);
if (ret != 1)
applog(LOG_ERR, "sharelog fwrite error");
}
static char *getwork_req = "{\"method\": \"getwork\", \"params\": [], \"id\":0}\n";
/* Return value is ignored if not called from add_pool_details */
struct pool *add_pool(void)
{
struct pool *pool;
pool = calloc(sizeof(struct pool), 1);
if (!pool)
quit(1, "Failed to malloc pool in add_pool");
pool->pool_no = pool->prio = total_pools;
mutex_init(&pool->last_work_lock);
mutex_init(&pool->pool_lock);
if (unlikely(pthread_cond_init(&pool->cr_cond, NULL)))
quit(1, "Failed to pthread_cond_init in add_pool");
mutex_init(&pool->stratum_lock);
INIT_LIST_HEAD(&pool->curlring);
pool->swork.transparency_time = (time_t)-1;
/* Make sure the pool doesn't think we've been idle since time 0 */
pool->tv_idle.tv_sec = ~0UL;
gettimeofday(&pool->cgminer_stats.start_tv, NULL);
pool->rpc_proxy = NULL;
pool->sock = INVSOCK;
pool->lp_socket = CURL_SOCKET_BAD;
pools = realloc(pools, sizeof(struct pool *) * (total_pools + 2));
pools[total_pools++] = pool;
return pool;
}
/* Pool variant of test and set */
static bool pool_tset(struct pool *pool, bool *var)
{
bool ret;
mutex_lock(&pool->pool_lock);
ret = *var;
*var = true;
mutex_unlock(&pool->pool_lock);
return ret;
}
bool pool_tclear(struct pool *pool, bool *var)
{
bool ret;
mutex_lock(&pool->pool_lock);
ret = *var;
*var = false;
mutex_unlock(&pool->pool_lock);
return ret;
}
struct pool *current_pool(void)
{
struct pool *pool;
mutex_lock(&control_lock);
pool = currentpool;
mutex_unlock(&control_lock);
return pool;
}
char *set_int_range(const char *arg, int *i, int min, int max)
{
char *err = opt_set_intval(arg, i);
if (err)
return err;
if (*i < min || *i > max)
return "Value out of range";
return NULL;
}
static char *set_int_0_to_9999(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 9999);
}
static char *set_int_1_to_65535(const char *arg, int *i)
{
return set_int_range(arg, i, 1, 65535);
}
static char *set_int_0_to_10(const char *arg, int *i)
{
return set_int_range(arg, i, 0, 10);
}
static char *set_int_1_to_10(const char *arg, int *i)
{
return set_int_range(arg, i, 1, 10);
}
char *set_strdup(const char *arg, char **p)
{
*p = strdup((char *)arg);
return NULL;
}
#if BLKMAKER_VERSION > 1
static char *set_b58addr(const char *arg, struct _cbscript_t *p)
{
size_t scriptsz = blkmk_address_to_script(NULL, 0, arg);
if (!scriptsz)
return "Invalid address";
char *script = malloc(scriptsz);
if (blkmk_address_to_script(script, scriptsz, arg) != scriptsz) {
free(script);
return "Failed to convert address to script";
}
p->data = script;
p->sz = scriptsz;
return NULL;
}
#endif
#ifdef HAVE_LIBUDEV
#include <libudev.h>
#endif
static
char* add_serial_all(char*arg, char*p) {
size_t pLen = p - arg;
char dev[pLen + PATH_MAX];
memcpy(dev, arg, pLen);
char *devp = &dev[pLen];
#ifdef HAVE_LIBUDEV
struct udev *udev = udev_new();
struct udev_enumerate *enumerate = udev_enumerate_new(udev);
struct udev_list_entry *list_entry;
udev_enumerate_add_match_subsystem(enumerate, "tty");
udev_enumerate_add_match_property(enumerate, "ID_SERIAL", "*");
udev_enumerate_scan_devices(enumerate);
udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
struct udev_device *device = udev_device_new_from_syspath(
udev_enumerate_get_udev(enumerate),
udev_list_entry_get_name(list_entry)
);
if (!device)
continue;
const char *devpath = udev_device_get_devnode(device);
if (devpath) {
strcpy(devp, devpath);
applog(LOG_DEBUG, "scan-serial: libudev all-adding %s", dev);
string_elist_add(dev, &scan_devices);
}
udev_device_unref(device);
}
udev_enumerate_unref(enumerate);
udev_unref(udev);
#elif defined(WIN32)
size_t bufLen = 0x10; // temp!
tryagain: ;
char buf[bufLen];
if (!QueryDosDevice(NULL, buf, bufLen)) {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
bufLen *= 2;
applog(LOG_DEBUG, "scan-serial: QueryDosDevice returned insufficent buffer error; enlarging to %lx", (unsigned long)bufLen);
goto tryagain;
}
return "scan-serial: Error occurred trying to enumerate COM ports with QueryDosDevice";
}
size_t tLen;
memcpy(devp, "\\\\.\\", 4);
devp = &devp[4];
for (char *t = buf; *t; t += tLen) {
tLen = strlen(t) + 1;
if (strncmp("COM", t, 3))
continue;
memcpy(devp, t, tLen);
applog(LOG_DEBUG, "scan-serial: QueryDosDevice all-adding %s", dev);
string_elist_add(dev, &scan_devices);
}
#else
DIR *D;
struct dirent *de;
const char devdir[] = "/dev";
const size_t devdirlen = sizeof(devdir) - 1;
char *devpath = devp;
char *devfile = devpath + devdirlen + 1;
D = opendir(devdir);
if (!D)
return "scan-serial 'all' is not supported on this platform";
memcpy(devpath, devdir, devdirlen);
devpath[devdirlen] = '/';
while ( (de = readdir(D)) ) {
if (strncmp(de->d_name, "tty", 3))
continue;
if (strncmp(&de->d_name[3], "USB", 3) && strncmp(&de->d_name[3], "ACM", 3))
continue;
strcpy(devfile, de->d_name);
applog(LOG_DEBUG, "scan-serial: /dev glob all-adding %s", dev);
string_elist_add(dev, &scan_devices);
}
closedir(D);
return NULL;
#endif
return NULL;
}
#ifdef USE_FPGA_SERIAL
static char *add_serial(char *arg)
{
char *p = strchr(arg, ':');
if (p)
++p;
else
p = arg;
if (!strcasecmp(p, "all")) {
return add_serial_all(arg, p);
}
string_elist_add(arg, &scan_devices);
return NULL;
}
#endif
static char *set_devices(char *arg)
{
int i = strtol(arg, &arg, 0);
if (*arg) {
if (*arg == '?') {
devices_enabled = -1;
return NULL;
}
return "Invalid device number";
}
if (i < 0 || i >= (int)(sizeof(devices_enabled) * 8) - 1)
return "Invalid device number";
devices_enabled |= 1 << i;
return NULL;
}
static char *set_balance(enum pool_strategy *strategy)
{
*strategy = POOL_BALANCE;
return NULL;
}
static char *set_loadbalance(enum pool_strategy *strategy)
{
*strategy = POOL_LOADBALANCE;
return NULL;
}
static char *set_rotate(const char *arg, int *i)
{
pool_strategy = POOL_ROTATE;
return set_int_range(arg, i, 0, 9999);
}
static char *set_rr(enum pool_strategy *strategy)
{
*strategy = POOL_ROUNDROBIN;
return NULL;
}
/* Detect that url is for a stratum protocol either via the presence of
* stratum+tcp or by detecting a stratum server response */
bool detect_stratum(struct pool *pool, char *url)
{
if (!extract_sockaddr(pool, url))
return false;
if (!strncasecmp(url, "stratum+tcp://", 14)) {
pool->rpc_url = strdup(url);
pool->has_stratum = true;
pool->stratum_url = pool->sockaddr_url;
return true;
}
return false;
}
static char *set_url(char *arg)
{
struct pool *pool;
total_urls++;
if (total_urls > total_pools)
add_pool();
pool = pools[total_urls - 1];
if (detect_stratum(pool, arg))
return NULL;
opt_set_charp(arg, &pool->rpc_url);
if (strncmp(arg, "http://", 7) &&
strncmp(arg, "https://", 8)) {
char *httpinput;
httpinput = malloc(255);
if (!httpinput)
quit(1, "Failed to malloc httpinput");
strcpy(httpinput, "http://");
strncat(httpinput, arg, 248);
pool->rpc_url = httpinput;
}
return NULL;
}
static char *set_user(const char *arg)
{
struct pool *pool;
total_users++;
if (total_users > total_pools)
add_pool();
pool = pools[total_users - 1];
opt_set_charp(arg, &pool->rpc_user);
return NULL;
}
static char *set_pass(const char *arg)
{
struct pool *pool;
total_passes++;
if (total_passes > total_pools)
add_pool();
pool = pools[total_passes - 1];
opt_set_charp(arg, &pool->rpc_pass);
return NULL;
}
static char *set_userpass(const char *arg)
{
struct pool *pool;
char *updup;
if (total_users != total_passes)
return "User + pass options must be balanced before userpass";
++total_users;
++total_passes;
if (total_users > total_pools)
add_pool();
pool = pools[total_users - 1];
updup = strdup(arg);
opt_set_charp(arg, &pool->rpc_userpass);
pool->rpc_user = strtok(updup, ":");
if (!pool->rpc_user)
return "Failed to find : delimited user info";
pool->rpc_pass = strtok(NULL, ":");
if (!pool->rpc_pass)
pool->rpc_pass = "";
return NULL;
}
static char *set_pool_priority(const char *arg)
{
struct pool *pool;
if (!total_pools)
return "Usage of --pool-priority before pools are defined does not make sense";
pool = pools[total_pools - 1];
opt_set_intval(arg, &pool->prio);
return NULL;
}
static char *set_pool_proxy(const char *arg)
{
struct pool *pool;
if (!total_pools)
return "Usage of --pool-proxy before pools are defined does not make sense";
if (!our_curl_supports_proxy_uris())
return "Your installed cURL library does not support proxy URIs. At least version 7.21.7 is required.";
pool = pools[total_pools - 1];
opt_set_charp(arg, &pool->rpc_proxy);
return NULL;
}
static char *enable_debug(bool *flag)
{
*flag = true;
opt_debug_console = true;
/* Turn on verbose output, too. */
opt_log_output = true;
return NULL;
}
static char *set_schedtime(const char *arg, struct schedtime *st)
{
if (sscanf(arg, "%d:%d", &st->tm.tm_hour, &st->tm.tm_min) != 2)
{
if (strcasecmp(arg, "now"))
return "Invalid time set, should be HH:MM";
} else
schedstop.tm.tm_sec = 0;
if (st->tm.tm_hour > 23 || st->tm.tm_min > 59 || st->tm.tm_hour < 0 || st->tm.tm_min < 0)
return "Invalid time set.";
st->enable = true;
return NULL;
}
static char* set_sharelog(char *arg)
{
char *r = "";
long int i = strtol(arg, &r, 10);
if ((!*r) && i >= 0 && i <= INT_MAX) {
sharelog_file = fdopen((int)i, "a");
if (!sharelog_file)
applog(LOG_ERR, "Failed to open fd %u for share log", (unsigned int)i);
} else if (!strcmp(arg, "-")) {
sharelog_file = stdout;
if (!sharelog_file)
applog(LOG_ERR, "Standard output missing for share log");
} else {
sharelog_file = fopen(arg, "a");
if (!sharelog_file)
applog(LOG_ERR, "Failed to open %s for share log", arg);
}
return NULL;
}
static char *temp_cutoff_str = "";
static char *temp_target_str = "";
char *set_temp_cutoff(char *arg)
{
int val;
if (!(arg && arg[0]))
return "Invalid parameters for set temp cutoff";
val = atoi(arg);
if (val < 0 || val > 200)
return "Invalid value passed to set temp cutoff";
temp_cutoff_str = arg;
return NULL;
}
char *set_temp_target(char *arg)
{
int val;
if (!(arg && arg[0]))
return "Invalid parameters for set temp target";
val = atoi(arg);
if (val < 0 || val > 200)
return "Invalid value passed to set temp target";
temp_target_str = arg;
return NULL;
}
// For a single element string, this always returns the number (for all calls)
// For multi-element strings, it returns each element as a number in order, and 0 when there are no more
static int temp_strtok(char *base, char **n)
{
char *i = *n;
char *p = strchr(i, ',');
if (p) {
p[0] = '\0';
*n = &p[1];
}
else
if (base != i)
*n = strchr(i, '\0');
return atoi(i);
}
static void load_temp_config()
{
int i, val = 0, target_off;
char *cutoff_n, *target_n;
struct cgpu_info *cgpu;
cutoff_n = temp_cutoff_str;
target_n = temp_target_str;
for (i = 0; i < total_devices; ++i) {
cgpu = devices[i];
// cutoff default may be specified by driver during probe; otherwise, opt_cutofftemp (const)
if (!cgpu->cutofftemp)
cgpu->cutofftemp = opt_cutofftemp;
// target default may be specified by driver, and is moved with offset; otherwise, offset minus 6
if (cgpu->targettemp)
target_off = cgpu->targettemp - cgpu->cutofftemp;
else
target_off = -6;
val = temp_strtok(temp_cutoff_str, &cutoff_n);
if (val < 0 || val > 200)
quit(1, "Invalid value passed to set temp cutoff");
if (val)
cgpu->cutofftemp = val;
val = temp_strtok(temp_target_str, &target_n);
if (val < 0 || val > 200)
quit(1, "Invalid value passed to set temp target");
if (val)
cgpu->targettemp = val;
else
cgpu->targettemp = cgpu->cutofftemp + target_off;
applog(LOG_DEBUG, "%s %u: Set temperature config: target=%d cutoff=%d",
cgpu->api->name, cgpu->device_id,