api.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. /*
  2. * Copyright 2011-2012 Andrew Smith
  3. * Copyright 2011-2012 Con Kolivas
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include "compat.h"
  20. #include "miner.h"
  21. #include "device-cpu.h" /* for algo_names[], TODO: re-factor dependency */
  22. #if defined(unix) || defined(__APPLE__)
  23. #include <errno.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #include <arpa/inet.h>
  27. #define SOCKETTYPE int
  28. #define SOCKETFAIL(a) ((a) < 0)
  29. #define INVSOCK -1
  30. #define INVINETADDR -1
  31. #define CLOSESOCKET close
  32. #define SOCKERRMSG strerror(errno)
  33. #endif
  34. #ifdef WIN32
  35. #include <ws2tcpip.h>
  36. #include <winsock2.h>
  37. #define SOCKETTYPE SOCKET
  38. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  39. #define INVSOCK INVALID_SOCKET
  40. #define INVINETADDR INADDR_NONE
  41. #define CLOSESOCKET closesocket
  42. static char WSAbuf[1024];
  43. struct WSAERRORS {
  44. int id;
  45. char *code;
  46. } WSAErrors[] = {
  47. { 0, "No error" },
  48. { WSAEINTR, "Interrupted system call" },
  49. { WSAEBADF, "Bad file number" },
  50. { WSAEACCES, "Permission denied" },
  51. { WSAEFAULT, "Bad address" },
  52. { WSAEINVAL, "Invalid argument" },
  53. { WSAEMFILE, "Too many open sockets" },
  54. { WSAEWOULDBLOCK, "Operation would block" },
  55. { WSAEINPROGRESS, "Operation now in progress" },
  56. { WSAEALREADY, "Operation already in progress" },
  57. { WSAENOTSOCK, "Socket operation on non-socket" },
  58. { WSAEDESTADDRREQ, "Destination address required" },
  59. { WSAEMSGSIZE, "Message too long" },
  60. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  61. { WSAENOPROTOOPT, "Bad protocol option" },
  62. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  63. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  64. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  65. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  66. { WSAEAFNOSUPPORT, "Address family not supported" },
  67. { WSAEADDRINUSE, "Address already in use" },
  68. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  69. { WSAENETDOWN, "Network is down" },
  70. { WSAENETUNREACH, "Network is unreachable" },
  71. { WSAENETRESET, "Net connection reset" },
  72. { WSAECONNABORTED, "Software caused connection abort" },
  73. { WSAECONNRESET, "Connection reset by peer" },
  74. { WSAENOBUFS, "No buffer space available" },
  75. { WSAEISCONN, "Socket is already connected" },
  76. { WSAENOTCONN, "Socket is not connected" },
  77. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  78. { WSAETOOMANYREFS, "Too many references, can't splice" },
  79. { WSAETIMEDOUT, "Connection timed out" },
  80. { WSAECONNREFUSED, "Connection refused" },
  81. { WSAELOOP, "Too many levels of symbolic links" },
  82. { WSAENAMETOOLONG, "File name too long" },
  83. { WSAEHOSTDOWN, "Host is down" },
  84. { WSAEHOSTUNREACH, "No route to host" },
  85. { WSAENOTEMPTY, "Directory not empty" },
  86. { WSAEPROCLIM, "Too many processes" },
  87. { WSAEUSERS, "Too many users" },
  88. { WSAEDQUOT, "Disc quota exceeded" },
  89. { WSAESTALE, "Stale NFS file handle" },
  90. { WSAEREMOTE, "Too many levels of remote in path" },
  91. { WSASYSNOTREADY, "Network system is unavailable" },
  92. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  93. { WSANOTINITIALISED, "WSAStartup not yet called" },
  94. { WSAEDISCON, "Graceful shutdown in progress" },
  95. { WSAHOST_NOT_FOUND, "Host not found" },
  96. { WSANO_DATA, "No host data of that type was found" },
  97. { -1, "Unknown error code" }
  98. };
  99. static char *WSAErrorMsg()
  100. {
  101. int i;
  102. int id = WSAGetLastError();
  103. /* Assume none of them are actually -1 */
  104. for (i = 0; WSAErrors[i].id != -1; i++)
  105. if (WSAErrors[i].id == id)
  106. break;
  107. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  108. return &(WSAbuf[0]);
  109. }
  110. #define SOCKERRMSG WSAErrorMsg()
  111. #ifndef SHUT_RDWR
  112. #define SHUT_RDWR SD_BOTH
  113. #endif
  114. #ifndef in_addr_t
  115. #define in_addr_t uint32_t
  116. #endif
  117. #endif
  118. // Big enough for largest API request
  119. // though a PC with 100s of CPUs may exceed the size ...
  120. // Current code assumes it can socket send this size also
  121. #define MYBUFSIZ 32768
  122. // Number of requests to queue - normally would be small
  123. #define QUEUE 10
  124. static char *io_buffer = NULL;
  125. static char *msg_buffer = NULL;
  126. static SOCKETTYPE sock = INVSOCK;
  127. static const char *UNAVAILABLE = " - API will not be available";
  128. //static const char *BLANK = "";
  129. static const char *COMMA = ",";
  130. static const char SEPARATOR = '|';
  131. static const char GPUSEP = ',';
  132. static const char *APIVERSION = "1.2";
  133. static const char *DEAD = "Dead";
  134. static const char *SICK = "Sick";
  135. static const char *NOSTART = "NoStart";
  136. static const char *DISABLED = "Disabled";
  137. static const char *ALIVE = "Alive";
  138. #define _DYNAMIC "D"
  139. static const char *DYNAMIC = _DYNAMIC;
  140. static const char *YES = "Y";
  141. static const char *NO = "N";
  142. #define _DEVS "DEVS"
  143. #define _POOLS "POOLS"
  144. #define _SUMMARY "SUMMARY"
  145. #define _STATUS "STATUS"
  146. #define _VERSION "VERSION"
  147. #define _MINECON "CONFIG"
  148. #ifdef WANT_CPUMINE
  149. #define _CPU "CPU"
  150. #endif
  151. #define _GPU "GPU"
  152. #define _CPUS "CPUS"
  153. #define _GPUS "GPUS"
  154. #define _BYE "BYE"
  155. static const char ISJSON = '{';
  156. #define JSON0 "{"
  157. #define JSON1 "\""
  158. #define JSON2 "\":["
  159. #define JSON3 "]"
  160. #define JSON4 ",\"id\":1}"
  161. #define JSON_START JSON0
  162. #define JSON_DEVS JSON1 _DEVS JSON2
  163. #define JSON_POOLS JSON1 _POOLS JSON2
  164. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  165. #define JSON_STATUS JSON1 _STATUS JSON2
  166. #define JSON_VERSION JSON1 _VERSION JSON2
  167. #define JSON_MINECON JSON1 _MINECON JSON2
  168. #define JSON_GPU JSON1 _GPU JSON2
  169. #ifdef WANT_CPUMINE
  170. #define JSON_CPU JSON1 _CPU JSON2
  171. #endif
  172. #define JSON_GPUS JSON1 _GPUS JSON2
  173. #define JSON_CPUS JSON1 _CPUS JSON2
  174. #define JSON_BYE JSON1 _BYE JSON1
  175. #define JSON_CLOSE JSON3
  176. #define JSON_END JSON4
  177. static const char *JSON_COMMAND = "command";
  178. static const char *JSON_PARAMETER = "parameter";
  179. #define MSG_INVGPU 1
  180. #define MSG_ALRENA 2
  181. #define MSG_ALRDIS 3
  182. #define MSG_GPUMRE 4
  183. #define MSG_GPUREN 5
  184. #define MSG_GPUNON 6
  185. #define MSG_POOL 7
  186. #define MSG_NOPOOL 8
  187. #define MSG_DEVS 9
  188. #define MSG_NODEVS 10
  189. #define MSG_SUMM 11
  190. #define MSG_GPUDIS 12
  191. #define MSG_GPUREI 13
  192. #define MSG_INVCMD 14
  193. #define MSG_MISID 15
  194. #define MSG_GPUDEV 17
  195. #ifdef WANT_CPUMINE
  196. #define MSG_CPUNON 16
  197. #define MSG_CPUDEV 18
  198. #define MSG_INVCPU 19
  199. #endif
  200. #define MSG_NUMGPU 20
  201. #define MSG_NUMCPU 21
  202. #define MSG_VERSION 22
  203. #define MSG_INVJSON 23
  204. #define MSG_MISCMD 24
  205. #define MSG_MISPID 25
  206. #define MSG_INVPID 26
  207. #define MSG_SWITCHP 27
  208. #define MSG_MISVAL 28
  209. #define MSG_NOADL 29
  210. #define MSG_NOGPUADL 30
  211. #define MSG_INVINT 31
  212. #define MSG_GPUINT 32
  213. #define MSG_MINECON 33
  214. #define MSG_GPUMERR 34
  215. #define MSG_GPUMEM 35
  216. #define MSG_GPUEERR 36
  217. #define MSG_GPUENG 37
  218. #define MSG_GPUVERR 38
  219. #define MSG_GPUVDDC 39
  220. #define MSG_GPUFERR 40
  221. #define MSG_GPUFAN 41
  222. #define MSG_MISFN 42
  223. #define MSG_BADFN 43
  224. #define MSG_SAVED 44
  225. #define MSG_ACCDENY 45
  226. #define MSG_ACCOK 46
  227. enum code_severity {
  228. SEVERITY_ERR,
  229. SEVERITY_WARN,
  230. SEVERITY_INFO,
  231. SEVERITY_SUCC,
  232. SEVERITY_FAIL
  233. };
  234. enum code_parameters {
  235. PARAM_GPU,
  236. PARAM_CPU,
  237. PARAM_GPUMAX,
  238. PARAM_CPUMAX,
  239. PARAM_PMAX,
  240. PARAM_POOLMAX,
  241. #ifdef WANT_CPUMINE
  242. PARAM_GCMAX,
  243. #else
  244. PARAM_GMAX,
  245. #endif
  246. PARAM_CMD,
  247. PARAM_POOL,
  248. PARAM_STR,
  249. PARAM_BOTH,
  250. PARAM_NONE
  251. };
  252. struct CODES {
  253. const enum code_severity severity;
  254. const int code;
  255. const enum code_parameters params;
  256. const char *description;
  257. } codes[] = {
  258. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  259. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  260. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  261. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  262. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  263. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  264. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  265. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  266. #ifdef WANT_CPUMINE
  267. { SEVERITY_SUCC, MSG_DEVS, PARAM_GCMAX, "%d GPU(s) - %d CPU(s)" },
  268. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs/CPUs" },
  269. #else
  270. { SEVERITY_SUCC, MSG_DEVS, PARAM_GMAX, "%d GPU(s)" },
  271. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs" },
  272. #endif
  273. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  274. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  275. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  276. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  277. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  278. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  279. #ifdef WANT_CPUMINE
  280. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  281. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  282. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  283. #endif
  284. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  285. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  286. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "CGMiner versions" },
  287. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  288. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  289. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  290. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  291. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  292. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  293. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  294. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  295. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  296. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  297. { SEVERITY_SUCC, MSG_MINECON, PARAM_NONE, "CGMiner config" },
  298. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  299. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported succeess" },
  300. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  301. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported succeess" },
  302. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  303. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported succeess" },
  304. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  305. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported succeess" },
  306. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  307. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  308. { SEVERITY_ERR, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  309. { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
  310. { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
  311. { SEVERITY_FAIL, 0, 0, NULL }
  312. };
  313. static int my_thr_id = 0;
  314. static int bye = 0;
  315. static bool ping = true;
  316. struct IP4ACCESS {
  317. in_addr_t ip;
  318. in_addr_t mask;
  319. bool writemode;
  320. };
  321. static struct IP4ACCESS *ipaccess = NULL;
  322. static int ips = 0;
  323. // All replies (except BYE) start with a message
  324. // thus for JSON, message() inserts JSON_START at the front
  325. // and send_result() adds JSON_END at the end
  326. static char *message(int messageid, int paramid, char *param2, bool isjson)
  327. {
  328. char severity;
  329. char *ptr;
  330. #ifdef WANT_CPUMINE
  331. int cpu;
  332. #endif
  333. int i;
  334. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  335. if (codes[i].code == messageid) {
  336. switch (codes[i].severity) {
  337. case SEVERITY_WARN:
  338. severity = 'W';
  339. break;
  340. case SEVERITY_INFO:
  341. severity = 'I';
  342. break;
  343. case SEVERITY_SUCC:
  344. severity = 'S';
  345. break;
  346. case SEVERITY_ERR:
  347. default:
  348. severity = 'E';
  349. break;
  350. }
  351. if (isjson)
  352. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"Code\":%d,\"Msg\":\"", severity, messageid);
  353. else
  354. sprintf(msg_buffer, _STATUS "=%c,Code=%d,Msg=", severity, messageid);
  355. ptr = msg_buffer + strlen(msg_buffer);
  356. switch(codes[i].params) {
  357. case PARAM_GPU:
  358. case PARAM_CPU:
  359. sprintf(ptr, codes[i].description, paramid);
  360. break;
  361. case PARAM_POOL:
  362. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  363. break;
  364. case PARAM_GPUMAX:
  365. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  366. break;
  367. case PARAM_PMAX:
  368. sprintf(ptr, codes[i].description, total_pools);
  369. break;
  370. case PARAM_POOLMAX:
  371. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  372. break;
  373. #ifdef WANT_CPUMINE
  374. case PARAM_GCMAX:
  375. if (opt_n_threads > 0)
  376. cpu = num_processors;
  377. else
  378. cpu = 0;
  379. sprintf(ptr, codes[i].description, nDevs, cpu);
  380. break;
  381. #else
  382. case PARAM_GMAX:
  383. sprintf(ptr, codes[i].description, nDevs);
  384. break;
  385. #endif
  386. case PARAM_CMD:
  387. sprintf(ptr, codes[i].description, JSON_COMMAND);
  388. break;
  389. case PARAM_STR:
  390. sprintf(ptr, codes[i].description, param2);
  391. break;
  392. case PARAM_BOTH:
  393. sprintf(ptr, codes[i].description, paramid, param2);
  394. break;
  395. case PARAM_NONE:
  396. default:
  397. strcpy(ptr, codes[i].description);
  398. }
  399. ptr = msg_buffer + strlen(msg_buffer);
  400. if (isjson)
  401. sprintf(ptr, "\",\"Description\":\"%s\"}" JSON_CLOSE, opt_api_description);
  402. else
  403. sprintf(ptr, ",Description=%s%c", opt_api_description, SEPARATOR);
  404. return msg_buffer;
  405. }
  406. }
  407. if (isjson)
  408. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE,
  409. messageid, opt_api_description);
  410. else
  411. sprintf(msg_buffer, _STATUS "=F,Code=-1,Msg=%d,Description=%s%c",
  412. messageid, opt_api_description, SEPARATOR);
  413. return msg_buffer;
  414. }
  415. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  416. {
  417. if (isjson)
  418. sprintf(io_buffer, "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE,
  419. message(MSG_VERSION, 0, NULL, isjson),
  420. VERSION, APIVERSION);
  421. else
  422. sprintf(io_buffer, "%s" _VERSION ",CGMiner=%s,API=%s%c",
  423. message(MSG_VERSION, 0, NULL, isjson),
  424. VERSION, APIVERSION, SEPARATOR);
  425. }
  426. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  427. {
  428. char buf[BUFSIZ];
  429. int cpucount = 0;
  430. char *adlinuse = (char *)NO;
  431. #ifdef HAVE_ADL
  432. const char *adl = YES;
  433. int i;
  434. for (i = 0; i < nDevs; i++) {
  435. if (gpus[i].has_adl) {
  436. adlinuse = (char *)YES;
  437. break;
  438. }
  439. }
  440. #else
  441. const char *adl = NO;
  442. #endif
  443. #ifdef WANT_CPUMINE
  444. cpucount = opt_n_threads > 0 ? num_processors : 0;
  445. #endif
  446. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  447. if (isjson)
  448. sprintf(buf, "," JSON_MINECON "{\"GPU Count\":%d,\"CPU Count\":%d,\"Pool Count\":%d,\"ADL\":\"%s\",\"ADL in use\":\"%s\",\"Strategy\":\"%s\",\"Log Interval\":\"%d\"}" JSON_CLOSE, nDevs, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, opt_log_interval);
  449. else
  450. sprintf(buf, _MINECON ",GPU Count=%d,CPU Count=%d,Pool Count=%d,ADL=%s,ADL in use=%s,Strategy=%s,Log Interval=%d%c", nDevs, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, opt_log_interval, SEPARATOR);
  451. strcat(io_buffer, buf);
  452. }
  453. static void gpustatus(int gpu, bool isjson)
  454. {
  455. char intensity[20];
  456. char buf[BUFSIZ];
  457. char *enabled;
  458. char *status;
  459. float gt, gv;
  460. int ga, gf, gp, gc, gm, pt;
  461. if (gpu >= 0 && gpu < nDevs) {
  462. struct cgpu_info *cgpu = &gpus[gpu];
  463. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  464. #ifdef HAVE_ADL
  465. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  466. #endif
  467. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  468. if (cgpu->deven != DEV_DISABLED)
  469. enabled = (char *)YES;
  470. else
  471. enabled = (char *)NO;
  472. if (cgpu->status == LIFE_DEAD)
  473. status = (char *)DEAD;
  474. else if (cgpu->status == LIFE_SICK)
  475. status = (char *)SICK;
  476. else if (cgpu->status == LIFE_NOSTART)
  477. status = (char *)NOSTART;
  478. else
  479. status = (char *)ALIVE;
  480. if (cgpu->dynamic)
  481. strcpy(intensity, DYNAMIC);
  482. else
  483. sprintf(intensity, "%d", cgpu->intensity);
  484. if (isjson)
  485. sprintf(buf, "{\"GPU\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f,\"Fan Speed\":%d,\"Fan Percent\":%d,\"GPU Clock\":%d,\"Memory Clock\":%d,\"GPU Voltage\":%.3f,\"GPU Activity\":%d,\"Powertune\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Intensity\":\"%s\",\"Last Share Pool\":%d,\"Last Share Time\":%lu}",
  486. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  487. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  488. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  489. cgpu->utility, intensity,
  490. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  491. (unsigned long)(cgpu->last_share_pool_time));
  492. else
  493. sprintf(buf, "GPU=%d,Enabled=%s,Status=%s,Temperature=%.2f,Fan Speed=%d,Fan Percent=%d,GPU Clock=%d,Memory Clock=%d,GPU Voltage=%.3f,GPU Activity=%d,Powertune=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Intensity=%s,Last Share Pool=%d,Last Share Time=%lu%c",
  494. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  495. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  496. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  497. cgpu->utility, intensity,
  498. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  499. (unsigned long)(cgpu->last_share_pool_time), SEPARATOR);
  500. strcat(io_buffer, buf);
  501. }
  502. }
  503. #ifdef WANT_CPUMINE
  504. static void cpustatus(int cpu, bool isjson)
  505. {
  506. char buf[BUFSIZ];
  507. if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
  508. struct cgpu_info *cgpu = &cpus[cpu];
  509. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  510. if (isjson)
  511. sprintf(buf, "{\"CPU\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Utility\":%.2f,\"Last Share Pool\":%d,\"Last Share Time\":%lu}",
  512. cpu, cgpu->total_mhashes / total_secs,
  513. opt_log_interval, cgpu->rolling,
  514. cgpu->accepted, cgpu->rejected,
  515. cgpu->utility,
  516. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  517. (unsigned long)(cgpu->last_share_pool_time));
  518. else
  519. sprintf(buf, "CPU=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Utility=%.2f,Last Share Pool=%d,Last Share Time=%lu%c",
  520. cpu, cgpu->total_mhashes / total_secs,
  521. opt_log_interval, cgpu->rolling,
  522. cgpu->accepted, cgpu->rejected,
  523. cgpu->utility,
  524. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  525. (unsigned long)(cgpu->last_share_pool_time), SEPARATOR);
  526. strcat(io_buffer, buf);
  527. }
  528. }
  529. #endif
  530. static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  531. {
  532. int i;
  533. if (nDevs == 0 && opt_n_threads == 0) {
  534. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  535. return;
  536. }
  537. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  538. if (isjson) {
  539. strcat(io_buffer, COMMA);
  540. strcat(io_buffer, JSON_DEVS);
  541. }
  542. for (i = 0; i < nDevs; i++) {
  543. if (isjson && i > 0)
  544. strcat(io_buffer, COMMA);
  545. gpustatus(i, isjson);
  546. }
  547. #ifdef WANT_CPUMINE
  548. if (opt_n_threads > 0)
  549. for (i = 0; i < num_processors; i++) {
  550. if (isjson && (i > 0 || nDevs > 0))
  551. strcat(io_buffer, COMMA);
  552. cpustatus(i, isjson);
  553. }
  554. #endif
  555. if (isjson)
  556. strcat(io_buffer, JSON_CLOSE);
  557. }
  558. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  559. {
  560. int id;
  561. if (nDevs == 0) {
  562. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  563. return;
  564. }
  565. if (param == NULL || *param == '\0') {
  566. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  567. return;
  568. }
  569. id = atoi(param);
  570. if (id < 0 || id >= nDevs) {
  571. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  572. return;
  573. }
  574. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  575. if (isjson) {
  576. strcat(io_buffer, COMMA);
  577. strcat(io_buffer, JSON_GPU);
  578. }
  579. gpustatus(id, isjson);
  580. if (isjson)
  581. strcat(io_buffer, JSON_CLOSE);
  582. }
  583. #ifdef WANT_CPUMINE
  584. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  585. {
  586. int id;
  587. if (opt_n_threads == 0) {
  588. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  589. return;
  590. }
  591. if (param == NULL || *param == '\0') {
  592. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  593. return;
  594. }
  595. id = atoi(param);
  596. if (id < 0 || id >= num_processors) {
  597. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  598. return;
  599. }
  600. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  601. if (isjson) {
  602. strcat(io_buffer, COMMA);
  603. strcat(io_buffer, JSON_CPU);
  604. }
  605. cpustatus(id, isjson);
  606. if (isjson)
  607. strcat(io_buffer, JSON_CLOSE);
  608. }
  609. #endif
  610. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  611. {
  612. char buf[BUFSIZ];
  613. char *status, *lp;
  614. int i;
  615. if (total_pools == 0) {
  616. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  617. return;
  618. }
  619. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  620. if (isjson) {
  621. strcat(io_buffer, COMMA);
  622. strcat(io_buffer, JSON_POOLS);
  623. }
  624. for (i = 0; i < total_pools; i++) {
  625. struct pool *pool = pools[i];
  626. if (!pool->enabled)
  627. status = (char *)DISABLED;
  628. else {
  629. if (pool->idle)
  630. status = (char *)DEAD;
  631. else
  632. status = (char *)ALIVE;
  633. }
  634. if (pool->hdr_path)
  635. lp = (char *)YES;
  636. else
  637. lp = (char *)NO;
  638. if (isjson)
  639. sprintf(buf, "%s{\"POOL\":%d,\"URL\":\"%s\",\"Status\":\"%s\",\"Priority\":%d,\"Long Poll\":\"%s\",\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Remote Failures\":%d}",
  640. (i > 0) ? COMMA : "",
  641. i, pool->rpc_url, status, pool->prio, lp,
  642. pool->getwork_requested,
  643. pool->accepted, pool->rejected,
  644. pool->discarded_work,
  645. pool->stale_shares,
  646. pool->getfail_occasions,
  647. pool->remotefail_occasions);
  648. else
  649. sprintf(buf, "POOL=%d,URL=%s,Status=%s,Priority=%d,Long Poll=%s,Getworks=%d,Accepted=%d,Rejected=%d,Discarded=%d,Stale=%d,Get Failures=%d,Remote Failures=%d%c",
  650. i, pool->rpc_url, status, pool->prio, lp,
  651. pool->getwork_requested,
  652. pool->accepted, pool->rejected,
  653. pool->discarded_work,
  654. pool->stale_shares,
  655. pool->getfail_occasions,
  656. pool->remotefail_occasions, SEPARATOR);
  657. strcat(io_buffer, buf);
  658. }
  659. if (isjson)
  660. strcat(io_buffer, JSON_CLOSE);
  661. }
  662. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  663. {
  664. double utility, mhs;
  665. #ifdef WANT_CPUMINE
  666. char *algo = (char *)(algo_names[opt_algo]);
  667. if (algo == NULL)
  668. algo = "(null)";
  669. #endif
  670. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  671. mhs = total_mhashes_done / total_secs;
  672. #ifdef WANT_CPUMINE
  673. if (isjson)
  674. sprintf(io_buffer, "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"Algorithm\":\"%s\",\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u}" JSON_CLOSE,
  675. message(MSG_SUMM, 0, NULL, isjson),
  676. total_secs, algo, mhs, found_blocks,
  677. total_getworks, total_accepted, total_rejected,
  678. hw_errors, utility, total_discarded, total_stale,
  679. total_go, local_work, total_ro, new_blocks);
  680. else
  681. sprintf(io_buffer, "%s" _SUMMARY ",Elapsed=%.0f,Algorithm=%s,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u%c",
  682. message(MSG_SUMM, 0, NULL, isjson),
  683. total_secs, algo, mhs, found_blocks,
  684. total_getworks, total_accepted, total_rejected,
  685. hw_errors, utility, total_discarded, total_stale,
  686. total_go, local_work, total_ro, new_blocks, SEPARATOR);
  687. #else
  688. if (isjson)
  689. sprintf(io_buffer, "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u}" JSON_CLOSE,
  690. message(MSG_SUMM, 0, NULL, isjson),
  691. total_secs, mhs, found_blocks,
  692. total_getworks, total_accepted, total_rejected,
  693. hw_errors, utility, total_discarded, total_stale,
  694. total_go, local_work, total_ro, new_blocks);
  695. else
  696. sprintf(io_buffer, "%s" _SUMMARY ",Elapsed=%.0f,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u%c",
  697. message(MSG_SUMM, 0, NULL, isjson),
  698. total_secs, mhs, found_blocks,
  699. total_getworks, total_accepted, total_rejected,
  700. hw_errors, utility, total_discarded, total_stale,
  701. total_go, local_work, total_ro, new_blocks, SEPARATOR);
  702. #endif
  703. }
  704. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  705. {
  706. struct thr_info *thr;
  707. int gpu;
  708. int id;
  709. int i;
  710. if (gpu_threads == 0) {
  711. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  712. return;
  713. }
  714. if (param == NULL || *param == '\0') {
  715. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  716. return;
  717. }
  718. id = atoi(param);
  719. if (id < 0 || id >= nDevs) {
  720. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  721. return;
  722. }
  723. if (gpus[id].deven != DEV_DISABLED) {
  724. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  725. return;
  726. }
  727. for (i = 0; i < gpu_threads; i++) {
  728. gpu = thr_info[i].cgpu->device_id;
  729. if (gpu == id) {
  730. thr = &thr_info[i];
  731. if (thr->cgpu->status != LIFE_WELL) {
  732. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  733. return;
  734. }
  735. gpus[id].deven = DEV_ENABLED;
  736. tq_push(thr->q, &ping);
  737. }
  738. }
  739. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  740. }
  741. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  742. {
  743. int id;
  744. if (nDevs == 0) {
  745. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  746. return;
  747. }
  748. if (param == NULL || *param == '\0') {
  749. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  750. return;
  751. }
  752. id = atoi(param);
  753. if (id < 0 || id >= nDevs) {
  754. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  755. return;
  756. }
  757. if (gpus[id].deven == DEV_DISABLED) {
  758. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  759. return;
  760. }
  761. gpus[id].deven = DEV_DISABLED;
  762. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  763. }
  764. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  765. {
  766. int id;
  767. if (nDevs == 0) {
  768. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  769. return;
  770. }
  771. if (param == NULL || *param == '\0') {
  772. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  773. return;
  774. }
  775. id = atoi(param);
  776. if (id < 0 || id >= nDevs) {
  777. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  778. return;
  779. }
  780. reinit_device(&gpus[id]);
  781. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  782. }
  783. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  784. {
  785. char buf[BUFSIZ];
  786. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  787. if (isjson)
  788. sprintf(buf, "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE, nDevs);
  789. else
  790. sprintf(buf, _GPUS ",Count=%d%c", nDevs, SEPARATOR);
  791. strcat(io_buffer, buf);
  792. }
  793. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  794. {
  795. char buf[BUFSIZ];
  796. int count = 0;
  797. #ifdef WANT_CPUMINE
  798. count = opt_n_threads > 0 ? num_processors : 0;
  799. #endif
  800. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  801. if (isjson)
  802. sprintf(buf, "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE, count);
  803. else
  804. sprintf(buf, _CPUS ",Count=%d%c", count, SEPARATOR);
  805. strcat(io_buffer, buf);
  806. }
  807. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  808. {
  809. struct pool *pool;
  810. int id;
  811. if (total_pools == 0) {
  812. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  813. return;
  814. }
  815. if (param == NULL || *param == '\0') {
  816. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  817. return;
  818. }
  819. id = atoi(param);
  820. if (id < 0 || id >= total_pools) {
  821. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  822. return;
  823. }
  824. pool = pools[id];
  825. pool->enabled = true;
  826. switch_pools(pool);
  827. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  828. }
  829. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  830. {
  831. int id;
  832. char *gpusep;
  833. if (nDevs == 0) {
  834. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  835. return false;
  836. }
  837. if (param == NULL || *param == '\0') {
  838. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  839. return false;
  840. }
  841. gpusep = strchr(param, GPUSEP);
  842. if (gpusep == NULL) {
  843. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  844. return false;
  845. }
  846. *(gpusep++) = '\0';
  847. id = atoi(param);
  848. if (id < 0 || id >= nDevs) {
  849. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  850. return false;
  851. }
  852. *gpu = id;
  853. *value = gpusep;
  854. return true;
  855. }
  856. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  857. {
  858. int id;
  859. char *value;
  860. int intensity;
  861. char intensitystr[7];
  862. if (!splitgpuvalue(param, &id, &value, isjson))
  863. return;
  864. if (!strncasecmp(value, DYNAMIC, 1)) {
  865. gpus[id].dynamic = true;
  866. strcpy(intensitystr, DYNAMIC);
  867. }
  868. else {
  869. intensity = atoi(value);
  870. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  871. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  872. return;
  873. }
  874. gpus[id].dynamic = false;
  875. gpus[id].intensity = intensity;
  876. sprintf(intensitystr, "%d", intensity);
  877. }
  878. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  879. }
  880. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  881. {
  882. #ifdef HAVE_ADL
  883. int id;
  884. char *value;
  885. int clock;
  886. if (!splitgpuvalue(param, &id, &value, isjson))
  887. return;
  888. clock = atoi(value);
  889. if (set_memoryclock(id, clock))
  890. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  891. else
  892. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  893. #else
  894. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  895. #endif
  896. }
  897. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  898. {
  899. #ifdef HAVE_ADL
  900. int id;
  901. char *value;
  902. int clock;
  903. if (!splitgpuvalue(param, &id, &value, isjson))
  904. return;
  905. clock = atoi(value);
  906. if (set_engineclock(id, clock))
  907. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  908. else
  909. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  910. #else
  911. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  912. #endif
  913. }
  914. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  915. {
  916. #ifdef HAVE_ADL
  917. int id;
  918. char *value;
  919. int fan;
  920. if (!splitgpuvalue(param, &id, &value, isjson))
  921. return;
  922. fan = atoi(value);
  923. if (set_fanspeed(id, fan))
  924. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  925. else
  926. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  927. #else
  928. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  929. #endif
  930. }
  931. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  932. {
  933. #ifdef HAVE_ADL
  934. int id;
  935. char *value;
  936. float vddc;
  937. if (!splitgpuvalue(param, &id, &value, isjson))
  938. return;
  939. vddc = atof(value);
  940. if (set_vddc(id, vddc))
  941. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  942. else
  943. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  944. #else
  945. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  946. #endif
  947. }
  948. static void send_result(SOCKETTYPE c, bool isjson);
  949. void doquit(SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  950. {
  951. if (isjson)
  952. strcpy(io_buffer, JSON_START JSON_BYE);
  953. else
  954. strcpy(io_buffer, _BYE);
  955. send_result(c, isjson);
  956. *io_buffer = '\0';
  957. bye = 1;
  958. PTH(&thr_info[my_thr_id]) = 0L;
  959. kill_work();
  960. }
  961. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  962. {
  963. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  964. }
  965. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  966. {
  967. FILE *fcfg;
  968. if (param == NULL || *param == '\0') {
  969. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  970. return;
  971. }
  972. fcfg = fopen(param, "w");
  973. if (!fcfg) {
  974. strcpy(io_buffer, message(MSG_BADFN, 0, param, isjson));
  975. return;
  976. }
  977. write_config(fcfg);
  978. fclose(fcfg);
  979. strcpy(io_buffer, message(MSG_SAVED, 0, param, isjson));
  980. }
  981. struct CMDS {
  982. char *name;
  983. void (*func)(SOCKETTYPE, char *, bool);
  984. bool requires_writemode;
  985. } cmds[] = {
  986. { "version", apiversion, false },
  987. { "config", minerconfig, false },
  988. { "devs", devstatus, false },
  989. { "pools", poolstatus, false },
  990. { "summary", summary, false },
  991. { "gpuenable", gpuenable, true },
  992. { "gpudisable", gpudisable, true },
  993. { "gpurestart", gpurestart, true },
  994. { "gpu", gpudev, false },
  995. #ifdef WANT_CPUMINE
  996. { "cpu", cpudev, false },
  997. #endif
  998. { "gpucount", gpucount, false },
  999. { "cpucount", cpucount, false },
  1000. { "switchpool", switchpool, true },
  1001. { "gpuintensity", gpuintensity, true },
  1002. { "gpumem", gpumem, true },
  1003. { "gpuengine", gpuengine, true },
  1004. { "gpufan", gpufan, true },
  1005. { "gpuvddc", gpuvddc, true },
  1006. { "save", dosave, true },
  1007. { "quit", doquit, true },
  1008. { "privileged", privileged, true },
  1009. { NULL, NULL, false }
  1010. };
  1011. static void send_result(SOCKETTYPE c, bool isjson)
  1012. {
  1013. int n;
  1014. int len;
  1015. if (isjson)
  1016. strcat(io_buffer, JSON_END);
  1017. len = strlen(io_buffer);
  1018. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : "");
  1019. // ignore failure - it's closed immediately anyway
  1020. n = send(c, io_buffer, len+1, 0);
  1021. if (opt_debug) {
  1022. if (SOCKETFAIL(n))
  1023. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  1024. else
  1025. applog(LOG_DEBUG, "API: sent %d", n);
  1026. }
  1027. }
  1028. static void tidyup()
  1029. {
  1030. bye = 1;
  1031. if (sock != INVSOCK) {
  1032. shutdown(sock, SHUT_RDWR);
  1033. CLOSESOCKET(sock);
  1034. sock = INVSOCK;
  1035. }
  1036. if (ipaccess != NULL) {
  1037. free(ipaccess);
  1038. ipaccess = NULL;
  1039. }
  1040. if (msg_buffer != NULL) {
  1041. free(msg_buffer);
  1042. msg_buffer = NULL;
  1043. }
  1044. if (io_buffer != NULL) {
  1045. free(io_buffer);
  1046. io_buffer = NULL;
  1047. }
  1048. }
  1049. /*
  1050. * Interpret [R|W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  1051. * special case of 0/0 allows /0 (means all IP addresses)
  1052. */
  1053. #define ALLIP4 "0/0"
  1054. /*
  1055. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  1056. */
  1057. static void setup_ipaccess()
  1058. {
  1059. char *buf, *ptr, *comma, *slash, *dot;
  1060. int ipcount, mask, octet, i;
  1061. bool writemode;
  1062. buf = malloc(strlen(opt_api_allow) + 1);
  1063. if (unlikely(!buf))
  1064. quit(1, "Failed to malloc ipaccess buf");
  1065. strcpy(buf, opt_api_allow);
  1066. ipcount = 1;
  1067. ptr = buf;
  1068. while (*ptr)
  1069. if (*(ptr++) == ',')
  1070. ipcount++;
  1071. // possibly more than needed, but never less
  1072. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  1073. if (unlikely(!ipaccess))
  1074. quit(1, "Failed to calloc ipaccess");
  1075. ips = 0;
  1076. ptr = buf;
  1077. while (ptr && *ptr) {
  1078. while (*ptr == ' ' || *ptr == '\t')
  1079. ptr++;
  1080. if (*ptr == ',') {
  1081. ptr++;
  1082. continue;
  1083. }
  1084. comma = strchr(ptr, ',');
  1085. if (comma)
  1086. *(comma++) = '\0';
  1087. writemode = false;
  1088. if (isalpha(*ptr) && *(ptr+1) == ':') {
  1089. if (tolower(*ptr) == 'w')
  1090. writemode = true;
  1091. ptr += 2;
  1092. }
  1093. ipaccess[ips].writemode = writemode;
  1094. if (strcmp(ptr, ALLIP4) == 0)
  1095. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  1096. else {
  1097. slash = strchr(ptr, '/');
  1098. if (!slash)
  1099. ipaccess[ips].mask = 0xffffffff;
  1100. else {
  1101. *(slash++) = '\0';
  1102. mask = atoi(slash);
  1103. if (mask < 1 || mask > 32)
  1104. goto popipo; // skip invalid/zero
  1105. ipaccess[ips].mask = 0;
  1106. while (mask-- >= 0) {
  1107. octet = 1 << (mask % 8);
  1108. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  1109. }
  1110. }
  1111. ipaccess[ips].ip = 0; // missing default to '.0'
  1112. for (i = 0; ptr && (i < 4); i++) {
  1113. dot = strchr(ptr, '.');
  1114. if (dot)
  1115. *(dot++) = '\0';
  1116. octet = atoi(ptr);
  1117. if (octet < 0 || octet > 0xff)
  1118. goto popipo; // skip invalid
  1119. ipaccess[ips].ip |= (octet << (i * 8));
  1120. ptr = dot;
  1121. }
  1122. ipaccess[ips].ip &= ipaccess[ips].mask;
  1123. }
  1124. ips++;
  1125. popipo:
  1126. ptr = comma;
  1127. }
  1128. free(buf);
  1129. }
  1130. void api(int api_thr_id)
  1131. {
  1132. char buf[BUFSIZ];
  1133. char param_buf[BUFSIZ];
  1134. const char *localaddr = "127.0.0.1";
  1135. SOCKETTYPE c;
  1136. int n, bound;
  1137. char *connectaddr;
  1138. char *binderror;
  1139. time_t bindstart;
  1140. short int port = opt_api_port;
  1141. struct sockaddr_in serv;
  1142. struct sockaddr_in cli;
  1143. socklen_t clisiz;
  1144. char *cmd;
  1145. char *param;
  1146. bool addrok;
  1147. bool writemode;
  1148. json_error_t json_err;
  1149. json_t *json_config;
  1150. json_t *json_val;
  1151. bool isjson;
  1152. bool did;
  1153. int i;
  1154. my_thr_id = api_thr_id;
  1155. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1156. sleep(opt_log_interval);
  1157. if (!opt_api_listen) {
  1158. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1159. return;
  1160. }
  1161. if (opt_api_allow) {
  1162. setup_ipaccess();
  1163. if (ips == 0) {
  1164. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  1165. return;
  1166. }
  1167. }
  1168. sock = socket(AF_INET, SOCK_STREAM, 0);
  1169. if (sock == INVSOCK) {
  1170. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1171. return;
  1172. }
  1173. memset(&serv, 0, sizeof(serv));
  1174. serv.sin_family = AF_INET;
  1175. if (!opt_api_allow && !opt_api_network) {
  1176. serv.sin_addr.s_addr = inet_addr(localaddr);
  1177. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  1178. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1179. return;
  1180. }
  1181. }
  1182. serv.sin_port = htons(port);
  1183. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1184. bound = 0;
  1185. bindstart = time(NULL);
  1186. while (bound == 0) {
  1187. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1188. binderror = SOCKERRMSG;
  1189. if ((time(NULL) - bindstart) > 61)
  1190. break;
  1191. else {
  1192. applog(LOG_WARNING, "API bind to port %d failed - trying again in 15sec", port);
  1193. sleep(15);
  1194. }
  1195. }
  1196. else
  1197. bound = 1;
  1198. }
  1199. if (bound == 0) {
  1200. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1201. return;
  1202. }
  1203. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1204. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1205. CLOSESOCKET(sock);
  1206. return;
  1207. }
  1208. if (opt_api_allow)
  1209. applog(LOG_WARNING, "API running in IP access mode");
  1210. else {
  1211. if (opt_api_network)
  1212. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1213. else
  1214. applog(LOG_WARNING, "API running in local access mode");
  1215. }
  1216. io_buffer = malloc(MYBUFSIZ+1);
  1217. msg_buffer = malloc(MYBUFSIZ+1);
  1218. while (bye == 0) {
  1219. clisiz = sizeof(cli);
  1220. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1221. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1222. goto die;
  1223. }
  1224. connectaddr = inet_ntoa(cli.sin_addr);
  1225. addrok = false;
  1226. writemode = false;
  1227. if (opt_api_allow) {
  1228. for (i = 0; i < ips; i++) {
  1229. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  1230. addrok = true;
  1231. writemode = ipaccess[i].writemode;
  1232. break;
  1233. }
  1234. }
  1235. } else {
  1236. if (opt_api_network)
  1237. addrok = true;
  1238. else
  1239. addrok = (strcmp(connectaddr, localaddr) == 0);
  1240. }
  1241. if (opt_debug)
  1242. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1243. if (addrok) {
  1244. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1245. if (SOCKETFAIL(n))
  1246. buf[0] = '\0';
  1247. else
  1248. buf[n] = '\0';
  1249. if (opt_debug) {
  1250. if (SOCKETFAIL(n))
  1251. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  1252. else
  1253. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  1254. }
  1255. if (!SOCKETFAIL(n)) {
  1256. did = false;
  1257. if (*buf != ISJSON) {
  1258. isjson = false;
  1259. param = strchr(buf, SEPARATOR);
  1260. if (param != NULL)
  1261. *(param++) = '\0';
  1262. cmd = buf;
  1263. }
  1264. else {
  1265. isjson = true;
  1266. param = NULL;
  1267. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  1268. json_config = json_loadb(buf, n, 0, &json_err);
  1269. #elif JANSSON_MAJOR_VERSION > 1
  1270. json_config = json_loads(buf, 0, &json_err);
  1271. #else
  1272. json_config = json_loads(buf, &json_err);
  1273. #endif
  1274. if (!json_is_object(json_config)) {
  1275. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1276. send_result(c, isjson);
  1277. did = true;
  1278. }
  1279. else {
  1280. json_val = json_object_get(json_config, JSON_COMMAND);
  1281. if (json_val == NULL) {
  1282. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1283. send_result(c, isjson);
  1284. did = true;
  1285. }
  1286. else {
  1287. if (!json_is_string(json_val)) {
  1288. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1289. send_result(c, isjson);
  1290. did = true;
  1291. }
  1292. else {
  1293. cmd = (char *)json_string_value(json_val);
  1294. json_val = json_object_get(json_config, JSON_PARAMETER);
  1295. if (json_is_string(json_val))
  1296. param = (char *)json_string_value(json_val);
  1297. else if (json_is_integer(json_val)) {
  1298. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1299. param = param_buf;
  1300. } else if (json_is_real(json_val)) {
  1301. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1302. param = param_buf;
  1303. }
  1304. }
  1305. }
  1306. }
  1307. }
  1308. if (!did)
  1309. for (i = 0; cmds[i].name != NULL; i++) {
  1310. if (strcmp(cmd, cmds[i].name) == 0) {
  1311. if (cmds[i].requires_writemode && !writemode) {
  1312. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  1313. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  1314. }
  1315. else
  1316. (cmds[i].func)(c, param, isjson);
  1317. send_result(c, isjson);
  1318. did = true;
  1319. break;
  1320. }
  1321. }
  1322. if (!did) {
  1323. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1324. send_result(c, isjson);
  1325. }
  1326. }
  1327. }
  1328. CLOSESOCKET(c);
  1329. }
  1330. die:
  1331. tidyup();
  1332. }