forked from luke-jr/bfgminer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.c
4425 lines (3763 loc) · 117 KB
/
api.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-2014 Andrew Smith
* Copyright 2011-2014 Con Kolivas
* Copyright 2012-2015 Luke Dashjr
*
* 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.
*
* Note: the code always includes GPU support even if there are no GPUs
* this simplifies handling multiple other device code being included
* depending on compile options
*/
#define _MEMORY_DEBUG_MASTER 1
#include "config.h"
#include <math.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <uthash.h>
#include <utlist.h>
#include "compat.h"
#include "deviceapi.h"
#ifdef USE_LIBMICROHTTPD
#include "httpsrv.h"
#endif
#include "miner.h"
#include "util.h"
#include "driver-cpu.h" /* for algo_names[], TODO: re-factor dependency */
#include "driver-opencl.h"
#define HAVE_AN_FPGA 1
// Max amount of data to buffer before sending on the socket
#define RPC_SOCKBUFSIZ 0x10000
// BUFSIZ varies on Windows and Linux
#define TMPBUFSIZ 8192
// Number of requests to queue - normally would be small
// However lots of PGA's may mean more
#define QUEUE 100
static const char *UNAVAILABLE = " - API will not be available";
static const char *MUNAVAILABLE = " - API multicast listener will not be available";
static const char *BLANK = "";
static const char *COMMA = ",";
#define COMSTR ","
static const char SEPARATOR = '|';
#define SEPSTR "|"
static const char GPUSEP = ',';
#define CMDJOIN '+'
#define JOIN_CMD "CMD="
#define BETWEEN_JOIN SEPSTR
static const char *APIVERSION = "3.1";
static const char *DEAD = "Dead";
static const char *SICK = "Sick";
static const char *NOSTART = "NoStart";
static const char *INIT = "Initialising";
static const char *WAIT = "Waiting";
static const char *DISABLED = "Disabled";
static const char *ALIVE = "Alive";
static const char *REJECTING = "Rejecting";
static const char *UNKNOWN = "Unknown";
#define _DYNAMIC "D"
#ifdef USE_OPENCL
static const char *DYNAMIC = _DYNAMIC;
#endif
static const char *YES = "Y";
static const char *NO = "N";
static const char *NULLSTR = "(null)";
static const char *TRUESTR = "true";
static const char *FALSESTR = "false";
#ifdef USE_SCRYPT
static const char *SCRYPTSTR = "scrypt";
#endif
#ifdef USE_SHA256D
static const char *SHA256STR = "sha256";
#endif
static const char *OSINFO =
#if defined(__linux)
"Linux";
#else
#if defined(__APPLE__)
"Apple";
#else
#if defined (__CYGWIN__)
"Cygwin";
#elif defined (WIN32)
"Windows";
#else
#if defined(unix)
"Unix";
#else
"Unknown";
#endif
#endif
#endif
#endif
#define _DEVS "DEVS"
#define _POOLS "POOLS"
#define _SUMMARY "SUMMARY"
#define _STATUS "STATUS"
#define _VERSION "VERSION"
#define _MINECONFIG "CONFIG"
#define _GPU "GPU"
#ifdef HAVE_AN_FPGA
#define _PGA "PGA"
#endif
#ifdef USE_CPUMINING
#define _CPU "CPU"
#endif
#define _GPUS "GPUS"
#define _PGAS "PGAS"
#define _CPUS "CPUS"
#define _NOTIFY "NOTIFY"
#define _DEVDETAILS "DEVDETAILS"
#define _BYE "BYE"
#define _RESTART "RESTART"
#define _MINESTATS "STATS"
#define _CHECK "CHECK"
#define _MINECOIN "COIN"
#define _DEBUGSET "DEBUG"
#define _SETCONFIG "SETCONFIG"
static const char ISJSON = '{';
#define JSON0 "{"
#define JSON1 "\""
#define JSON2 "\":["
#define JSON3 "]"
#define JSON4 ",\"id\":1"
// If anyone cares, id=0 for truncated output
#define JSON4_TRUNCATED ",\"id\":0"
#define JSON5 "}"
#define JSON_START JSON0
#define JSON_DEVS JSON1 _DEVS JSON2
#define JSON_POOLS JSON1 _POOLS JSON2
#define JSON_SUMMARY JSON1 _SUMMARY JSON2
#define JSON_STATUS JSON1 _STATUS JSON2
#define JSON_VERSION JSON1 _VERSION JSON2
#define JSON_MINECONFIG JSON1 _MINECONFIG JSON2
#define JSON_GPU JSON1 _GPU JSON2
#ifdef HAVE_AN_FPGA
#define JSON_PGA JSON1 _PGA JSON2
#endif
#ifdef USE_CPUMINING
#define JSON_CPU JSON1 _CPU JSON2
#endif
#define JSON_GPUS JSON1 _GPUS JSON2
#define JSON_PGAS JSON1 _PGAS JSON2
#define JSON_CPUS JSON1 _CPUS JSON2
#define JSON_NOTIFY JSON1 _NOTIFY JSON2
#define JSON_CLOSE JSON3
#define JSON_MINESTATS JSON1 _MINESTATS JSON2
#define JSON_CHECK JSON1 _CHECK JSON2
#define JSON_DEBUGSET JSON1 _DEBUGSET JSON2
#define JSON_SETCONFIG JSON1 _SETCONFIG JSON2
#define JSON_END JSON4 JSON5
#define JSON_END_TRUNCATED JSON4_TRUNCATED JSON5
#define JSON_BETWEEN_JOIN ","
static const char *JSON_COMMAND = "command";
static const char *JSON_PARAMETER = "parameter";
#define MSG_INVGPU 1
#define MSG_ALRENA 2
#define MSG_ALRDIS 3
#define MSG_GPUMRE 4
#define MSG_GPUREN 5
#define MSG_GPUNON 6
#define MSG_POOL 7
#define MSG_NOPOOL 8
#define MSG_DEVS 9
#define MSG_NODEVS 10
#define MSG_SUMM 11
#define MSG_GPUDIS 12
#define MSG_GPUREI 13
#define MSG_INVCMD 14
#define MSG_MISID 15
#define MSG_GPUDEV 17
#ifdef USE_CPUMINING
#define MSG_CPUNON 16
#define MSG_CPUDEV 18
#define MSG_INVCPU 19
#define MSG_ALRENAC 98
#define MSG_ALRDISC 99
#define MSG_CPUMRE 100
#define MSG_CPUREN 101
#define MSG_CPUDIS 102
#define MSG_CPUREI 103
#endif
#define MSG_NUMGPU 20
#define MSG_NUMCPU 21
#define MSG_VERSION 22
#define MSG_INVJSON 23
#define MSG_MISCMD 24
#define MSG_MISPID 25
#define MSG_INVPID 26
#define MSG_SWITCHP 27
#define MSG_MISVAL 28
#define MSG_NOADL 29
#define MSG_NOGPUADL 30
#define MSG_INVINT 31
#define MSG_GPUINT 32
#define MSG_MINECONFIG 33
#define MSG_GPUMERR 34
#define MSG_GPUMEM 35
#define MSG_GPUEERR 36
#define MSG_GPUENG 37
#define MSG_GPUVERR 38
#define MSG_GPUVDDC 39
#define MSG_GPUFERR 40
#define MSG_GPUFAN 41
#define MSG_MISFN 42
#define MSG_BADFN 43
#define MSG_SAVED 44
#define MSG_ACCDENY 45
#define MSG_ACCOK 46
#define MSG_ENAPOOL 47
#define MSG_DISPOOL 48
#define MSG_ALRENAP 49
#define MSG_ALRDISP 50
#define MSG_DISLASTP 51
#define MSG_MISPDP 52
#define MSG_INVPDP 53
#define MSG_TOOMANYP 54
#define MSG_ADDPOOL 55
#ifdef HAVE_AN_FPGA
#define MSG_PGANON 56
#define MSG_PGADEV 57
#define MSG_INVPGA 58
#endif
#define MSG_NUMPGA 59
#define MSG_NOTIFY 60
#ifdef HAVE_AN_FPGA
#define MSG_PGALRENA 61
#define MSG_PGALRDIS 62
#define MSG_PGAENA 63
#define MSG_PGADIS 64
#define MSG_PGAREI 0x101
#define MSG_PGAUNW 65
#endif
#define MSG_REMLASTP 66
#define MSG_ACTPOOL 67
#define MSG_REMPOOL 68
#define MSG_DEVDETAILS 69
#define MSG_MINESTATS 70
#define MSG_MISCHK 71
#define MSG_CHECK 72
#define MSG_POOLPRIO 73
#define MSG_DUPPID 74
#define MSG_MISBOOL 75
#define MSG_INVBOOL 76
#define MSG_FOO 77
#define MSG_MINECOIN 78
#define MSG_DEBUGSET 79
#define MSG_PGAIDENT 80
#define MSG_PGANOID 81
#define MSG_SETCONFIG 82
#define MSG_UNKCON 83
#define MSG_INVNUM 84
#define MSG_CONPAR 85
#define MSG_CONVAL 86
#ifdef HAVE_AN_FPGA
#define MSG_MISPGAOPT 89
#define MSG_PGANOSET 90
#define MSG_PGAHELP 91
#define MSG_PGASETOK 92
#define MSG_PGASETERR 93
#endif
#define MSG_ZERMIS 94
#define MSG_ZERINV 95
#define MSG_ZERSUM 96
#define MSG_ZERNOSUM 97
#define MSG_DEVSCAN 0x100
#define MSG_BYE 0x101
#define MSG_INVNEG 121
#define MSG_SETQUOTA 122
#define MSG_INVSTRATEGY 0x102
#define USE_ALTMSG 0x4000
enum code_severity {
SEVERITY_ERR,
SEVERITY_WARN,
SEVERITY_INFO,
SEVERITY_SUCC,
SEVERITY_FAIL
};
enum code_parameters {
PARAM_COUNT,
PARAM_GPU,
PARAM_PGA,
PARAM_CPU,
PARAM_PID,
PARAM_GPUMAX,
PARAM_PGAMAX,
PARAM_CPUMAX,
PARAM_PMAX,
PARAM_POOLMAX,
// Single generic case: have the code resolve it - see below
PARAM_DMAX,
PARAM_CMD,
PARAM_POOL,
PARAM_STR,
PARAM_BOTH,
PARAM_BOOL,
PARAM_SET,
PARAM_NONE
};
struct CODES {
const enum code_severity severity;
const int code;
const enum code_parameters params;
const char *description;
} codes[] = {
#ifdef USE_OPENCL
{ SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
{ SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
{ SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
{ SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
{ SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
#endif
{ SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
{ SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
{ SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
{ SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX,
"%d PGA(s)"
},
{ SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No PGAs"
},
{ SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
#ifdef USE_OPENCL
{ SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
{ SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
#endif
{ SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
{ SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
#ifdef USE_OPENCL
{ SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
#endif
#ifdef HAVE_AN_FPGA
{ SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
{ SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
{ SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
{ SEVERITY_INFO, MSG_PGALRENA,PARAM_PGA, "PGA %d already enabled" },
{ SEVERITY_INFO, MSG_PGALRDIS,PARAM_PGA, "PGA %d already disabled" },
{ SEVERITY_INFO, MSG_PGAENA, PARAM_PGA, "PGA %d sent enable message" },
{ SEVERITY_INFO, MSG_PGADIS, PARAM_PGA, "PGA %d set disable flag" },
{ SEVERITY_ERR, MSG_PGAUNW, PARAM_PGA, "PGA %d is not flagged WELL, cannot enable" },
#endif
#ifdef USE_CPUMINING
{ SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
{ SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
{ SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
{ SEVERITY_INFO, MSG_ALRENAC, PARAM_CPU, "CPU %d already enabled" },
{ SEVERITY_INFO, MSG_ALRDISC, PARAM_CPU, "CPU %d already disabled" },
{ SEVERITY_WARN, MSG_CPUMRE, PARAM_CPU, "CPU %d must be restarted first" },
{ SEVERITY_INFO, MSG_CPUREN, PARAM_CPU, "CPU %d sent enable message" },
{ SEVERITY_INFO, MSG_CPUDIS, PARAM_CPU, "CPU %d set disable flag" },
{ SEVERITY_INFO, MSG_CPUREI, PARAM_CPU, "CPU %d restart attempted" },
#endif
{ SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
{ SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
{ SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
{ SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "BFGMiner versions" },
{ SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
{ SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
{ SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
{ SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
{ SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
{ SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
{ SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
{ SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
{ SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range -10 - 31" },
{ SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
{ SEVERITY_SUCC, MSG_MINECONFIG,PARAM_NONE, "BFGMiner config" },
#ifdef USE_OPENCL
{ SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
{ SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported success" },
{ SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
{ SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported success" },
{ SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
{ SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported success" },
{ SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
{ SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported success" },
#endif
{ SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
{ SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
{ SEVERITY_SUCC, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
{ SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
{ SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
{ SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
{ SEVERITY_SUCC, MSG_POOLPRIO,PARAM_NONE, "Changed pool priorities" },
{ SEVERITY_ERR, MSG_DUPPID, PARAM_PID, "Duplicate pool specified %d" },
{ SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
{ SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
{ SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
{ SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
{ SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
{ SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
{ SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
{ SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
{ SEVERITY_ERR, MSG_REMLASTP,PARAM_POOL, "Cannot remove last pool %d:'%s'" },
{ SEVERITY_ERR, MSG_ACTPOOL, PARAM_POOL, "Cannot remove active pool %d:'%s'" },
{ SEVERITY_SUCC, MSG_REMPOOL, PARAM_BOTH, "Removed pool %d:'%s'" },
{ SEVERITY_SUCC, MSG_NOTIFY, PARAM_NONE, "Notify" },
{ SEVERITY_SUCC, MSG_DEVDETAILS,PARAM_NONE, "Device Details" },
{ SEVERITY_SUCC, MSG_MINESTATS,PARAM_NONE, "BFGMiner stats" },
{ SEVERITY_ERR, MSG_MISCHK, PARAM_NONE, "Missing check cmd" },
{ SEVERITY_SUCC, MSG_CHECK, PARAM_NONE, "Check command" },
{ SEVERITY_ERR, MSG_MISBOOL, PARAM_NONE, "Missing parameter: true/false" },
{ SEVERITY_ERR, MSG_INVBOOL, PARAM_NONE, "Invalid parameter should be true or false" },
{ SEVERITY_SUCC, MSG_FOO, PARAM_BOOL, "Failover-Only set to %s" },
{ SEVERITY_SUCC, MSG_MINECOIN,PARAM_NONE, "BFGMiner coin" },
{ SEVERITY_SUCC, MSG_DEBUGSET,PARAM_NONE, "Debug settings" },
#ifdef HAVE_AN_FPGA
{ SEVERITY_SUCC, MSG_PGAIDENT,PARAM_PGA, "Identify command sent to PGA%d" },
{ SEVERITY_WARN, MSG_PGANOID, PARAM_PGA, "PGA%d does not support identify" },
#endif
{ SEVERITY_SUCC, MSG_SETCONFIG,PARAM_SET, "Set config '%s' to %d" },
{ SEVERITY_ERR, MSG_UNKCON, PARAM_STR, "Unknown config '%s'" },
{ SEVERITY_ERR, MSG_INVNUM, PARAM_BOTH, "Invalid number (%d) for '%s' range is 0-9999" },
{ SEVERITY_ERR, MSG_INVNEG, PARAM_BOTH, "Invalid negative number (%d) for '%s'" },
{ SEVERITY_ERR, MSG_INVSTRATEGY, PARAM_STR, "Invalid strategy for '%s'" },
{ SEVERITY_SUCC, MSG_SETQUOTA,PARAM_SET, "Set pool '%s' to quota %d'" },
{ SEVERITY_ERR, MSG_CONPAR, PARAM_NONE, "Missing config parameters 'name,N'" },
{ SEVERITY_ERR, MSG_CONVAL, PARAM_STR, "Missing config value N for '%s,N'" },
#ifdef HAVE_AN_FPGA
{ SEVERITY_ERR, MSG_MISPGAOPT, PARAM_NONE, "Missing option after PGA number" },
{ SEVERITY_WARN, MSG_PGANOSET, PARAM_PGA, "PGA %d does not support pgaset" },
{ SEVERITY_INFO, MSG_PGAHELP, PARAM_BOTH, "PGA %d set help: %s" },
{ SEVERITY_SUCC, MSG_PGASETOK, PARAM_PGA, "PGA %d set OK" },
{ SEVERITY_SUCC, MSG_PGASETOK | USE_ALTMSG, PARAM_BOTH, "PGA %d set OK: %s" },
{ SEVERITY_ERR, MSG_PGASETERR, PARAM_BOTH, "PGA %d set failed: %s" },
#endif
{ SEVERITY_ERR, MSG_ZERMIS, PARAM_NONE, "Missing zero parameters" },
{ SEVERITY_ERR, MSG_ZERINV, PARAM_STR, "Invalid zero parameter '%s'" },
{ SEVERITY_SUCC, MSG_ZERSUM, PARAM_STR, "Zeroed %s stats with summary" },
{ SEVERITY_SUCC, MSG_ZERNOSUM, PARAM_STR, "Zeroed %s stats without summary" },
{ SEVERITY_SUCC, MSG_DEVSCAN, PARAM_COUNT, "Added %d new device(s)" },
{ SEVERITY_SUCC, MSG_BYE, PARAM_STR, "%s" },
{ SEVERITY_FAIL, 0, 0, NULL }
};
static const char *localaddr = "127.0.0.1";
static int my_thr_id = 0;
static bool bye;
// Used to control quit restart access to shutdown variables
static pthread_mutex_t quit_restart_lock;
static bool do_a_quit;
static bool do_a_restart;
static time_t when = 0; // when the request occurred
static bool per_proc;
struct IP4ACCESS {
in_addr_t ip;
in_addr_t mask;
char group;
};
#define GROUP(g) (toupper(g))
#define PRIVGROUP GROUP('W')
#define NOPRIVGROUP GROUP('R')
#define ISPRIVGROUP(g) (GROUP(g) == PRIVGROUP)
#define GROUPOFFSET(g) (GROUP(g) - GROUP('A'))
#define VALIDGROUP(g) (GROUP(g) >= GROUP('A') && GROUP(g) <= GROUP('Z'))
#define COMMANDS(g) (apigroups[GROUPOFFSET(g)].commands)
#define DEFINEDGROUP(g) (ISPRIVGROUP(g) || COMMANDS(g) != NULL)
struct APIGROUPS {
// This becomes a string like: "|cmd1|cmd2|cmd3|" so it's quick to search
char *commands;
} apigroups['Z' - 'A' + 1]; // only A=0 to Z=25 (R: noprivs, W: allprivs)
static struct IP4ACCESS *ipaccess = NULL;
static int ips = 0;
struct io_data {
bytes_t data;
SOCKETTYPE sock;
// Whether to add various things
bool close;
};
static struct io_data *rpc_io_data;
static void io_reinit(struct io_data *io_data)
{
bytes_reset(&io_data->data);
io_data->close = false;
}
static
struct io_data *sock_io_new()
{
struct io_data *io_data = malloc(sizeof(struct io_data));
bytes_init(&io_data->data);
io_data->sock = INVSOCK;
io_reinit(io_data);
return io_data;
}
static
size_t io_flush(struct io_data *io_data, bool complete)
{
size_t sent = 0, tosend = bytes_len(&io_data->data);
ssize_t n;
struct timeval timeout = {0, complete ? 50000: 0}, tv;
fd_set wd;
int count = 0;
while (tosend)
{
FD_ZERO(&wd);
FD_SET(io_data->sock, &wd);
tv = timeout;
if (select(io_data->sock + 1, NULL, &wd, NULL, &tv) < 1)
break;
n = send(io_data->sock, (void*)&bytes_buf(&io_data->data)[sent], tosend, 0);
if (SOCKETFAIL(n))
{
if (!sock_blocks())
applog(LOG_WARNING, "API: send (%lu) failed: %s", (unsigned long)tosend, SOCKERRMSG);
break;
}
if (count <= 1)
{
if (n == tosend)
applog(LOG_DEBUG, "API: sent all of %lu first go", (unsigned long)tosend);
else
applog(LOG_DEBUG, "API: sent %ld of %lu first go", (long)n, (unsigned long)tosend);
}
else
{
if (n == tosend)
applog(LOG_DEBUG, "API: sent all of remaining %lu (count=%d)", (unsigned long)tosend, count);
else
applog(LOG_DEBUG, "API: sent %ld of remaining %lu (count=%d)", (long)n, (unsigned long)tosend, count);
}
sent += n;
tosend -= n;
}
bytes_shift(&io_data->data, sent);
return sent;
}
static bool io_add(struct io_data *io_data, char *buf)
{
size_t len = strlen(buf);
if (bytes_len(&io_data->data) + len > RPC_SOCKBUFSIZ)
io_flush(io_data, false);
bytes_append(&io_data->data, buf, len);
return true;
}
static void io_close(struct io_data *io_data)
{
io_data->close = true;
}
static void io_free()
{
bytes_free(&rpc_io_data->data);
free(rpc_io_data);
rpc_io_data = NULL;
}
// This is only called when expected to be needed (rarely)
// i.e. strings outside of the codes control (input from the user)
static char *escape_string(char *str, bool isjson)
{
char *buf, *ptr;
int count;
count = 0;
for (ptr = str; *ptr; ptr++) {
switch (*ptr) {
case ',':
case '|':
case '=':
if (!isjson)
count++;
break;
case '"':
if (isjson)
count++;
break;
case '\\':
count++;
break;
}
}
if (count == 0)
return str;
buf = malloc(strlen(str) + count + 1);
if (unlikely(!buf))
quit(1, "Failed to malloc escape buf");
ptr = buf;
while (*str)
switch (*str) {
case ',':
case '|':
case '=':
if (!isjson)
*(ptr++) = '\\';
*(ptr++) = *(str++);
break;
case '"':
if (isjson)
*(ptr++) = '\\';
*(ptr++) = *(str++);
break;
case '\\':
*(ptr++) = '\\';
*(ptr++) = *(str++);
break;
default:
*(ptr++) = *(str++);
break;
}
*ptr = '\0';
return buf;
}
static struct api_data *api_add_extra(struct api_data *root, struct api_data *extra)
{
struct api_data *tmp;
if (root) {
if (extra) {
// extra tail
tmp = extra->prev;
// extra prev = root tail
extra->prev = root->prev;
// root tail next = extra
root->prev->next = extra;
// extra tail next = root
tmp->next = root;
// root prev = extra tail
root->prev = tmp;
}
} else
root = extra;
return root;
}
static
struct api_data *api_add_data_full(struct api_data *root, const char * const name, enum api_data_type type, const void *data, bool copy_data)
{
struct api_data *api_data;
api_data = (struct api_data *)malloc(sizeof(struct api_data));
api_data->name = strdup(name);
api_data->type = type;
if (root == NULL) {
root = api_data;
root->prev = root;
root->next = root;
}
else {
api_data->prev = root->prev;
root->prev = api_data;
api_data->next = root;
api_data->prev->next = api_data;
}
api_data->data_was_malloc = copy_data;
// Avoid crashing on bad data
if (data == NULL) {
api_data->type = type = API_CONST;
data = NULLSTR;
api_data->data_was_malloc = copy_data = false;
}
if (!copy_data)
{
api_data->data = data;
if (type == API_JSON)
json_incref((json_t *)data);
}
else
{
size_t datalen = 0;
switch(type) {
case API_ESCAPE:
case API_STRING:
case API_CONST:
datalen = strlen(data) + 1;
break;
case API_UINT8:
datalen = sizeof(uint8_t);
break;
case API_INT16:
datalen = sizeof(int16_t);
break;
case API_UINT16:
datalen = sizeof(uint16_t);
break;
case API_INT:
datalen = sizeof(int);
break;
case API_UINT:
datalen = sizeof(unsigned int);
break;
case API_UINT32:
datalen = sizeof(uint32_t);
break;
case API_UINT64:
datalen = sizeof(uint64_t);
break;
case API_DOUBLE:
case API_ELAPSED:
case API_MHS:
case API_MHTOTAL:
case API_UTILITY:
case API_FREQ:
case API_HS:
case API_DIFF:
case API_PERCENT:
datalen = sizeof(double);
break;
case API_BOOL:
datalen = sizeof(bool);
break;
case API_TIMEVAL:
datalen = sizeof(struct timeval);
break;
case API_TIME:
datalen = sizeof(time_t);
break;
case API_VOLTS:
case API_TEMP:
datalen = sizeof(float);
break;
case API_JSON:
api_data->data_was_malloc = false;
api_data->data = json_deep_copy((json_t *)data);
break;
default:
applog(LOG_ERR, "API: unknown1 data type %d ignored", type);
api_data->type = API_STRING;
api_data->data_was_malloc = false;
api_data->data = UNKNOWN;
break;
}
if (datalen)
{
void * const copied_data = malloc(datalen);
memcpy(copied_data, data, datalen);
api_data->data = copied_data;
}
}
return root;
}
struct api_data *api_add_escape(struct api_data * const root, const char * const name, const char * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_ESCAPE, data, copy_data);
}
struct api_data *api_add_string(struct api_data * const root, const char * const name, const char * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_STRING, data, copy_data);
}
struct api_data *api_add_const(struct api_data * const root, const char * const name, const char * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_CONST, data, copy_data);
}
struct api_data *api_add_uint8(struct api_data * const root, const char * const name, const uint8_t * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_UINT8, data, copy_data);
}
struct api_data *api_add_int16(struct api_data * const root, const char * const name, const uint16_t * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_INT16, data, copy_data);
}
struct api_data *api_add_uint16(struct api_data * const root, const char * const name, const uint16_t * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_UINT16, data, copy_data);
}
struct api_data *api_add_int(struct api_data * const root, const char * const name, const int * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_INT, data, copy_data);
}
struct api_data *api_add_uint(struct api_data * const root, const char * const name, const unsigned int * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_UINT, data, copy_data);
}
struct api_data *api_add_uint32(struct api_data * const root, const char * const name, const uint32_t * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_UINT32, data, copy_data);
}
struct api_data *api_add_uint64(struct api_data * const root, const char * const name, const uint64_t * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_UINT64, data, copy_data);
}
struct api_data *api_add_double(struct api_data * const root, const char * const name, const double * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_DOUBLE, data, copy_data);
}
struct api_data *api_add_elapsed(struct api_data * const root, const char * const name, const double * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_ELAPSED, data, copy_data);
}
struct api_data *api_add_bool(struct api_data * const root, const char * const name, const bool * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_BOOL, data, copy_data);
}
struct api_data *api_add_timeval(struct api_data * const root, const char * const name, const struct timeval * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_TIMEVAL, data, copy_data);
}
struct api_data *api_add_time(struct api_data * const root, const char * const name, const time_t * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_TIME, data, copy_data);
}
struct api_data *api_add_mhs(struct api_data * const root, const char * const name, const double * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_MHS, data, copy_data);
}
struct api_data *api_add_mhtotal(struct api_data * const root, const char * const name, const double * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_MHTOTAL, data, copy_data);
}
struct api_data *api_add_temp(struct api_data * const root, const char * const name, const float * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_TEMP, data, copy_data);
}
struct api_data *api_add_utility(struct api_data * const root, const char * const name, const double * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_UTILITY, data, copy_data);
}
struct api_data *api_add_freq(struct api_data * const root, const char * const name, const double * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_FREQ, data, copy_data);
}
struct api_data *api_add_volts(struct api_data * const root, const char * const name, const float * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_VOLTS, data, copy_data);
}
struct api_data *api_add_hs(struct api_data * const root, const char * const name, const double * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_HS, data, copy_data);
}
struct api_data *api_add_diff(struct api_data * const root, const char * const name, const double * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_DIFF, data, copy_data);
}
// json_t is not const since we generally increase the refcount
struct api_data *api_add_json(struct api_data * const root, const char * const name, json_t * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_JSON, data, copy_data);
}
struct api_data *api_add_percent(struct api_data * const root, const char * const name, const double * const data, const bool copy_data)
{
return api_add_data_full(root, name, API_PERCENT, data, copy_data);
}
static struct api_data *print_data(struct api_data *root, char *buf, bool isjson, bool precom)
{
struct api_data *tmp;
bool first = true;
char *original, *escape;
char *quote;
*buf = '\0';
if (precom) {
*(buf++) = *COMMA;
*buf = '\0';
}
if (isjson) {
strcpy(buf, JSON0);
buf = strchr(buf, '\0');
quote = JSON1;
} else
quote = (char *)BLANK;
while (root) {
if (!first)
*(buf++) = *COMMA;
else
first = false;
sprintf(buf, "%s%s%s%s", quote, root->name, quote, isjson ? ":" : "=");
buf = strchr(buf, '\0');
switch(root->type) {
case API_STRING:
case API_CONST:
sprintf(buf, "%s%s%s", quote, (char *)(root->data), quote);
break;
case API_ESCAPE:
original = (char *)(root->data);
escape = escape_string((char *)(root->data), isjson);
sprintf(buf, "%s%s%s", quote, escape, quote);
if (escape != original)
free(escape);
break;
case API_UINT8:
sprintf(buf, "%u", *(uint8_t *)root->data);
break;
case API_INT16:
sprintf(buf, "%d", *(int16_t *)root->data);
break;
case API_UINT16:
sprintf(buf, "%u", *(uint16_t *)root->data);
break;
case API_INT:
sprintf(buf, "%d", *((int *)(root->data)));