-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.c
3981 lines (3329 loc) · 113 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-2013 Con Kolivas
*
* 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 <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include "compat.h"
#include "api.h"
#include "miner.h"
#include "pool.h"
#include "util.h"
#include "pool.h"
#include "algorithm.h"
#include "config_parser.h"
#ifdef WIN32
static char WSAbuf[1024];
struct WSAERRORS {
int id;
char *code;
} WSAErrors[] = {
{ 0, "No error" },
{ WSAEINTR, "Interrupted system call" },
{ WSAEBADF, "Bad file number" },
{ WSAEACCES, "Permission denied" },
{ WSAEFAULT, "Bad address" },
{ WSAEINVAL, "Invalid argument" },
{ WSAEMFILE, "Too many open sockets" },
{ WSAEWOULDBLOCK, "Operation would block" },
{ WSAEINPROGRESS, "Operation now in progress" },
{ WSAEALREADY, "Operation already in progress" },
{ WSAENOTSOCK, "Socket operation on non-socket" },
{ WSAEDESTADDRREQ, "Destination address required" },
{ WSAEMSGSIZE, "Message too long" },
{ WSAEPROTOTYPE, "Protocol wrong type for socket" },
{ WSAENOPROTOOPT, "Bad protocol option" },
{ WSAEPROTONOSUPPORT, "Protocol not supported" },
{ WSAESOCKTNOSUPPORT, "Socket type not supported" },
{ WSAEOPNOTSUPP, "Operation not supported on socket" },
{ WSAEPFNOSUPPORT, "Protocol family not supported" },
{ WSAEAFNOSUPPORT, "Address family not supported" },
{ WSAEADDRINUSE, "Address already in use" },
{ WSAEADDRNOTAVAIL, "Can't assign requested address" },
{ WSAENETDOWN, "Network is down" },
{ WSAENETUNREACH, "Network is unreachable" },
{ WSAENETRESET, "Net connection reset" },
{ WSAECONNABORTED, "Software caused connection abort" },
{ WSAECONNRESET, "Connection reset by peer" },
{ WSAENOBUFS, "No buffer space available" },
{ WSAEISCONN, "Socket is already connected" },
{ WSAENOTCONN, "Socket is not connected" },
{ WSAESHUTDOWN, "Can't send after socket shutdown" },
{ WSAETOOMANYREFS, "Too many references, can't splice" },
{ WSAETIMEDOUT, "Connection timed out" },
{ WSAECONNREFUSED, "Connection refused" },
{ WSAELOOP, "Too many levels of symbolic links" },
{ WSAENAMETOOLONG, "File name too long" },
{ WSAEHOSTDOWN, "Host is down" },
{ WSAEHOSTUNREACH, "No route to host" },
{ WSAENOTEMPTY, "Directory not empty" },
{ WSAEPROCLIM, "Too many processes" },
{ WSAEUSERS, "Too many users" },
{ WSAEDQUOT, "Disc quota exceeded" },
{ WSAESTALE, "Stale NFS file handle" },
{ WSAEREMOTE, "Too many levels of remote in path" },
{ WSASYSNOTREADY, "Network system is unavailable" },
{ WSAVERNOTSUPPORTED, "Winsock version out of range" },
{ WSANOTINITIALISED, "WSAStartup not yet called" },
{ WSAEDISCON, "Graceful shutdown in progress" },
{ WSAHOST_NOT_FOUND, "Host not found" },
{ WSANO_DATA, "No host data of that type was found" },
{ -1, "Unknown error code" }
};
char *WSAErrorMsg(void) {
int i;
int id = WSAGetLastError();
/* Assume none of them are actually -1 */
for (i = 0; WSAErrors[i].id != -1; i++)
if (WSAErrors[i].id == id)
break;
sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
return &(WSAbuf[0]);
}
#endif
struct CODES codes[] = {
{ 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" },
{ 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 GPU(s)" },
{ SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
},
{ SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
{ SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
{ SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
{ SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
{ SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
{ SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
{ SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
{ SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "SGMiner 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 " MIN_INTENSITY_STR " - " MAX_INTENSITY_STR },
{ SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
{ SEVERITY_ERR, MSG_INVXINT, PARAM_STR, "Invalid xintensity (%s) - must be range " MIN_XINTENSITY_STR " - " MAX_XINTENSITY_STR },
{ SEVERITY_INFO, MSG_GPUXINT, PARAM_BOTH, "GPU %d set new xintensity to %s" },
{ SEVERITY_ERR, MSG_INVRAWINT, PARAM_STR, "Invalid rawintensity (%s) - must be range " MIN_RAWINTENSITY_STR " - " MAX_RAWINTENSITY_STR },
{ SEVERITY_INFO, MSG_GPURAWINT, PARAM_BOTH, "GPU %d set new rawintensity to %s" },
{ SEVERITY_SUCC, MSG_MINECONFIG,PARAM_NONE, "sgminer config" },
{ 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" },
{ 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_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, "sgminer 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, "sgminer coin" },
{ SEVERITY_SUCC, MSG_DEBUGSET,PARAM_NONE, "Debug settings" },
{ 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_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'" },
{ SEVERITY_INFO, MSG_NOUSTA, PARAM_NONE, "No USB Statistics" },
{ 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_LOCKOK, PARAM_NONE, "Lock stats created" },
{ SEVERITY_WARN, MSG_LOCKDIS, PARAM_NONE, "Lock stats not enabled" },
{ SEVERITY_SUCC, MSG_CHSTRAT, PARAM_STR, "Multipool strategy changed to '%s'" },
{ SEVERITY_ERR, MSG_MISSTRAT, PARAM_NONE, "Missing multipool strategy" },
{ SEVERITY_ERR, MSG_INVSTRAT, PARAM_NONE, "Invalid multipool strategy %d" },
{ SEVERITY_ERR, MSG_MISSTRATINT, PARAM_NONE, "Missing rotate interval" },
{ SEVERITY_SUCC, MSG_PROFILE, PARAM_PRMAX, "%d Profile(s)" },
{ SEVERITY_ERR, MSG_NOPROFILE, PARAM_NONE, "No profiles" },
{ SEVERITY_ERR, MSG_PROFILEEXIST, PARAM_STR, "Profile '%s' already exists" },
{ SEVERITY_ERR, MSG_MISPRD, PARAM_NONE, "Missing addprofile details" },
{ SEVERITY_SUCC, MSG_ADDPROFILE, PARAM_STR, "Added profile '%s'" },
{ SEVERITY_ERR, MSG_MISPRID, PARAM_STR, "Profile name missing" },
{ SEVERITY_ERR, MSG_PRNOEXIST, PARAM_STR, "Profile '%s' doesn't exist" },
{ SEVERITY_ERR, MSG_PRISDEFAULT, PARAM_STR, "Profile '%s' is the default profile" },
{ SEVERITY_ERR, MSG_PRINUSE, PARAM_STR, "Profile '%s' is used by a pool" },
{ SEVERITY_SUCC, MSG_REMPROFILE, PARAM_BOTH, "Removed pool %d:'%s'" },
{ SEVERITY_SUCC, MSG_CHPOOLPR, PARAM_BOTH, "Changed pool %d to profile '%s'" },
{ SEVERITY_SUCC, MSG_BYE, PARAM_STR, "%s" },
{ SEVERITY_FAIL, 0, (enum code_parameters)0, NULL }
};
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 = ",";
static const char SEPARATOR = '|';
static const char GPUSEP = ',';
static const char *APIVERSION = "4.0";
static const char *DEAD = "Dead";
static const char *SICK = "Sick";
static const char *NOSTART = "NoStart";
static const char *INIT = "Initialising";
static const char *DISABLED = "Disabled";
static const char *ALIVE = "Alive";
static const char *REJECTING = "Rejecting";
static const char *UNKNOWN = "Unknown";
static const char *DYNAMIC = _DYNAMIC;
static __maybe_unused const char *NONE = "None";
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";
static const char *DEVICECODE = "GPU ";
static const char *OSINFO =
#if defined(__linux__)
"Linux";
#elif defined(__APPLE__)
"Apple";
#elif defined(WIN32)
"Windows";
#elif defined(__CYGWIN__)
"Cygwin";
#elif defined(__unix__)
"Unix";
#else
"Unknown";
#endif
static const char *JSON_COMMAND = "command";
static const char *JSON_PARAMETER = "parameter";
static const char ISJSON = '{';
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 struct IP4ACCESS *ipaccess = NULL;
static int ips = 0;
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 io_list *io_head = NULL;
static void io_reinit(struct io_data *io_data)
{
io_data->cur = io_data->ptr;
*(io_data->ptr) = '\0';
io_data->close = false;
}
static struct io_data *_io_new(size_t initial, bool socket_buf)
{
struct io_data *io_data;
struct io_list *io_list;
io_data = (struct io_data *)malloc(sizeof(*io_data));
io_data->ptr = (char *)malloc(initial);
io_data->siz = initial;
io_data->sock = socket_buf;
io_reinit(io_data);
io_list = (struct io_list *)malloc(sizeof(*io_list));
io_list->io_data = io_data;
if (io_head) {
io_list->next = io_head;
io_list->prev = io_head->prev;
io_list->next->prev = io_list;
io_list->prev->next = io_list;
} else {
io_list->prev = io_list;
io_list->next = io_list;
io_head = io_list;
}
return io_data;
}
bool io_add(struct io_data *io_data, char *buf)
{
size_t len, dif, tot;
len = strlen(buf);
dif = io_data->cur - io_data->ptr;
// send will always have enough space to add the JSON
tot = len + 1 + dif + sizeof(JSON_CLOSE) + sizeof(JSON_END);
if (tot > io_data->siz) {
size_t newsize = io_data->siz + (2 * SOCKBUFALLOCSIZ);
if (newsize < tot)
newsize = (2 + (size_t)((float)tot / (float)SOCKBUFALLOCSIZ)) * SOCKBUFALLOCSIZ;
io_data->ptr = (char *)realloc(io_data->ptr, newsize);
io_data->cur = io_data->ptr + dif;
io_data->siz = newsize;
}
memcpy(io_data->cur, buf, len + 1);
io_data->cur += len;
return true;
}
void io_close(struct io_data *io_data)
{
io_data->close = true;
}
void io_free()
{
struct io_list *io_list, *io_next;
if (io_head) {
io_list = io_head;
do {
io_next = io_list->next;
free(io_list->io_data->ptr);
free(io_list->io_data);
free(io_list);
io_list = io_next;
} while (io_list != io_head);
io_head = 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 = (char *)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, char *name, enum api_data_type type, 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 = (void *)NULLSTR;
api_data->data_was_malloc = copy_data = false;
}
if (!copy_data)
api_data->data = data;
else
switch(type) {
case API_ESCAPE:
case API_STRING:
case API_CONST:
api_data->data = (void *)malloc(strlen((char *)data) + 1);
strcpy((char*)(api_data->data), (char *)data);
break;
case API_UINT8:
/* Most OSs won't really alloc less than 4 */
api_data->data = malloc(4);
*(uint8_t *)api_data->data = *(uint8_t *)data;
break;
case API_UINT16:
/* Most OSs won't really alloc less than 4 */
api_data->data = malloc(4);
*(uint16_t *)api_data->data = *(uint16_t *)data;
break;
case API_INT:
api_data->data = (void *)malloc(sizeof(int));
*((int *)(api_data->data)) = *((int *)data);
break;
case API_UINT:
api_data->data = (void *)malloc(sizeof(unsigned int));
*((unsigned int *)(api_data->data)) = *((unsigned int *)data);
break;
case API_UINT32:
api_data->data = (void *)malloc(sizeof(uint32_t));
*((uint32_t *)(api_data->data)) = *((uint32_t *)data);
break;
case API_HEX32:
api_data->data = (void *)malloc(sizeof(uint32_t));
*((uint32_t *)(api_data->data)) = *((uint32_t *)data);
break;
case API_UINT64:
api_data->data = (void *)malloc(sizeof(uint64_t));
*((uint64_t *)(api_data->data)) = *((uint64_t *)data);
break;
case API_DOUBLE:
case API_ELAPSED:
case API_MHS:
case API_KHS:
case API_MHTOTAL:
case API_UTILITY:
case API_FREQ:
case API_HS:
case API_DIFF:
case API_PERCENT:
api_data->data = (void *)malloc(sizeof(double));
*((double *)(api_data->data)) = *((double *)data);
break;
case API_BOOL:
api_data->data = (void *)malloc(sizeof(bool));
*((bool *)(api_data->data)) = *((bool *)data);
break;
case API_TIMEVAL:
api_data->data = (void *)malloc(sizeof(struct timeval));
memcpy(api_data->data, data, sizeof(struct timeval));
break;
case API_TIME:
api_data->data = (void *)malloc(sizeof(time_t));
*(time_t *)(api_data->data) = *((time_t *)data);
break;
case API_VOLTS:
case API_TEMP:
case API_AVG:
api_data->data = (void *)malloc(sizeof(float));
*((float *)(api_data->data)) = *((float *)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 = (void *)UNKNOWN;
break;
}
return root;
}
struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data)
{
return api_add_data_full(root, name, API_ESCAPE, (void *)data, copy_data);
}
struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data)
{
return api_add_data_full(root, name, API_STRING, (void *)data, copy_data);
}
struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data)
{
return api_add_data_full(root, name, API_CONST, (void *)data, copy_data);
}
struct api_data *api_add_uint8(struct api_data *root, char *name, uint8_t *data, bool copy_data)
{
return api_add_data_full(root, name, API_UINT8, (void *)data, copy_data);
}
struct api_data *api_add_uint16(struct api_data *root, char *name, uint16_t *data, bool copy_data)
{
return api_add_data_full(root, name, API_UINT16, (void *)data, copy_data);
}
struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data)
{
return api_add_data_full(root, name, API_INT, (void *)data, copy_data);
}
struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data)
{
return api_add_data_full(root, name, API_UINT, (void *)data, copy_data);
}
struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data)
{
return api_add_data_full(root, name, API_UINT32, (void *)data, copy_data);
}
struct api_data *api_add_hex32(struct api_data *root, char *name, uint32_t *data, bool copy_data)
{
return api_add_data_full(root, name, API_HEX32, (void *)data, copy_data);
}
struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data)
{
return api_add_data_full(root, name, API_UINT64, (void *)data, copy_data);
}
struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_DOUBLE, (void *)data, copy_data);
}
struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_ELAPSED, (void *)data, copy_data);
}
struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data)
{
return api_add_data_full(root, name, API_BOOL, (void *)data, copy_data);
}
struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data)
{
return api_add_data_full(root, name, API_TIMEVAL, (void *)data, copy_data);
}
struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data)
{
return api_add_data_full(root, name, API_TIME, (void *)data, copy_data);
}
struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_MHS, (void *)data, copy_data);
}
struct api_data *api_add_khs(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_KHS, (void *)data, copy_data);
}
struct api_data *api_add_mhtotal(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_MHTOTAL, (void *)data, copy_data);
}
struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data)
{
return api_add_data_full(root, name, API_TEMP, (void *)data, copy_data);
}
struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_UTILITY, (void *)data, copy_data);
}
struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_FREQ, (void *)data, copy_data);
}
struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data)
{
return api_add_data_full(root, name, API_VOLTS, (void *)data, copy_data);
}
struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_HS, (void *)data, copy_data);
}
struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_DIFF, (void *)data, copy_data);
}
struct api_data *api_add_percent(struct api_data *root, char *name, double *data, bool copy_data)
{
return api_add_data_full(root, name, API_PERCENT, (void *)data, copy_data);
}
struct api_data *api_add_avg(struct api_data *root, char *name, float *data, bool copy_data)
{
return api_add_data_full(root, name, API_AVG, (void *)data, copy_data);
}
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_UINT16:
sprintf(buf, "%u", *(uint16_t *)root->data);
break;
case API_INT:
sprintf(buf, "%d", *((int *)(root->data)));
break;
case API_UINT:
sprintf(buf, "%u", *((unsigned int *)(root->data)));
break;
case API_UINT32:
sprintf(buf, "%"PRIu32, *((uint32_t *)(root->data)));
break;
case API_HEX32:
snprintf(buf, sizeof(buf), "0x%08x", *((uint32_t *)(root->data)));
break;
case API_UINT64:
sprintf(buf, "%"PRIu64, *((uint64_t *)(root->data)));
break;
case API_TIME:
sprintf(buf, "%lu", *((unsigned long *)(root->data)));
break;
case API_DOUBLE:
sprintf(buf, "%f", *((double *)(root->data)));
break;
case API_ELAPSED:
sprintf(buf, "%.0f", *((double *)(root->data)));
break;
case API_UTILITY:
case API_FREQ:
case API_MHS:
sprintf(buf, "%.4f", *((double *)(root->data)));
break;
case API_KHS:
sprintf(buf, "%.0f", *((double *)(root->data)));
break;
case API_VOLTS:
case API_AVG:
sprintf(buf, "%.3f", *((float *)(root->data)));
break;
case API_MHTOTAL:
sprintf(buf, "%.4f", *((double *)(root->data)));
break;
case API_HS:
sprintf(buf, "%.15f", *((double *)(root->data)));
break;
case API_DIFF:
sprintf(buf, "%.8f", *((double *)(root->data)));
break;
case API_BOOL:
sprintf(buf, "%s", *((bool *)(root->data)) ? TRUESTR : FALSESTR);
break;
case API_TIMEVAL:
sprintf(buf, "%"PRIu64".%06lu",
(uint64_t)((struct timeval *)(root->data))->tv_sec,
(unsigned long)((struct timeval *)(root->data))->tv_usec);
break;
case API_TEMP:
sprintf(buf, "%.2f", *((float *)(root->data)));
break;
case API_PERCENT:
sprintf(buf, "%.4f", *((double *)(root->data)) * 100.0);
break;
default:
applog(LOG_ERR, "API: unknown2 data type %d ignored", root->type);
sprintf(buf, "%s%s%s", quote, UNKNOWN, quote);
break;
}
buf = strchr(buf, '\0');
free(root->name);
if (root->data_was_malloc)
free(root->data);
if (root->next == root) {
free(root);
root = NULL;
} else {
tmp = root;
root = tmp->next;
root->prev = tmp->prev;
root->prev->next = root;
free(tmp);
}
}
strcpy(buf, isjson ? JSON5 : SEPSTR);
return root;
}
// All replies (except BYE and RESTART) start with a message
// thus for JSON, message() inserts JSON_START at the front
// and send_result() adds JSON_END at the end
void message(struct io_data *io_data, int messageid, int paramid, char *param2, bool isjson)
{
struct api_data *root = NULL;
char buf[TMPBUFSIZ];
char buf2[TMPBUFSIZ];
char severity[2];
int i;
if (isjson)
io_add(io_data, JSON_START JSON_STATUS);
for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
if (codes[i].code == messageid) {
switch (codes[i].severity) {
case SEVERITY_WARN:
severity[0] = 'W';
break;
case SEVERITY_INFO:
severity[0] = 'I';
break;
case SEVERITY_SUCC:
severity[0] = 'S';
break;
case SEVERITY_ERR:
default:
severity[0] = 'E';
break;
}
severity[1] = '\0';
switch(codes[i].params) {
case PARAM_GPU:
case PARAM_PID:
case PARAM_INT:
sprintf(buf, codes[i].description, paramid);
break;
case PARAM_POOL:
sprintf(buf, codes[i].description, paramid, pools[paramid]->rpc_url);
break;
case PARAM_GPUMAX:
sprintf(buf, codes[i].description, paramid, nDevs - 1);
break;
case PARAM_PMAX:
sprintf(buf, codes[i].description, total_pools);
break;
case PARAM_PRMAX:
sprintf(buf, codes[i].description, total_profiles);
break;
case PARAM_POOLMAX:
sprintf(buf, codes[i].description, paramid, total_pools - 1);
break;
case PARAM_DMAX:
sprintf(buf, codes[i].description, nDevs);
break;
case PARAM_CMD:
sprintf(buf, codes[i].description, JSON_COMMAND);
break;
case PARAM_STR:
sprintf(buf, codes[i].description, param2);
break;
case PARAM_BOTH:
sprintf(buf, codes[i].description, paramid, param2);
break;
case PARAM_BOOL:
sprintf(buf, codes[i].description, paramid ? TRUESTR : FALSESTR);
break;
case PARAM_SET:
sprintf(buf, codes[i].description, param2, paramid);
break;
case PARAM_NONE:
default:
strcpy(buf, codes[i].description);
}
root = api_add_string(root, _STATUS, severity, false);
root = api_add_time(root, "When", &when, false);
root = api_add_int(root, "Code", &messageid, false);
root = api_add_escape(root, "Msg", buf, false);
root = api_add_escape(root, "Description", opt_api_description, false);
root = print_data(root, buf2, isjson, false);
io_add(io_data, buf2);
if (isjson)
io_add(io_data, JSON_CLOSE);
return;
}
}
root = api_add_string(root, _STATUS, "F", false);
root = api_add_time(root, "When", &when, false);
int id = -1;
root = api_add_int(root, "Code", &id, false);
sprintf(buf, "%d", messageid);
root = api_add_escape(root, "Msg", buf, false);
root = api_add_escape(root, "Description", opt_api_description, false);
root = print_data(root, buf2, isjson, false);
io_add(io_data, buf2);
if (isjson)
io_add(io_data, JSON_CLOSE);
}
#if LOCK_TRACKING
static uint64_t lock_id = 1;
static LOCKLIST *lockhead;
static void lockmsgnow()
{
struct timeval now;
struct tm *tm;
time_t dt;
cgtime(&now);
dt = now.tv_sec;
tm = localtime(&dt);
LOCKMSG("%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);
}
static LOCKLIST *newlock(void *lock, enum cglock_typ typ, const char *file, const char *func, const int linenum)
{
LOCKLIST *list;
list = (LOCKLIST *)calloc(1, sizeof(*list));
if (!list)
quithere(1, "OOM list");
list->info = (LOCKINFO *)calloc(1, sizeof(*(list->info)));
if (!list->info)
quithere(1, "OOM info");
list->next = lockhead;
lockhead = list;
list->info->lock = lock;
list->info->typ = typ;
list->info->file = file;
list->info->func = func;
list->info->linenum = linenum;