api.c 40 KB

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