api.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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 bye = 0;
  314. static bool ping = true;
  315. struct IP4ACCESS {
  316. in_addr_t ip;
  317. in_addr_t mask;
  318. bool writemode;
  319. };
  320. static struct IP4ACCESS *ipaccess = NULL;
  321. static int ips = 0;
  322. // All replies (except BYE) start with a message
  323. // thus for JSON, message() inserts JSON_START at the front
  324. // and send_result() adds JSON_END at the end
  325. static char *message(int messageid, int paramid, char *param2, bool isjson)
  326. {
  327. char severity;
  328. char *ptr;
  329. #ifdef WANT_CPUMINE
  330. int cpu;
  331. #endif
  332. int i;
  333. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  334. if (codes[i].code == messageid) {
  335. switch (codes[i].severity) {
  336. case SEVERITY_WARN:
  337. severity = 'W';
  338. break;
  339. case SEVERITY_INFO:
  340. severity = 'I';
  341. break;
  342. case SEVERITY_SUCC:
  343. severity = 'S';
  344. break;
  345. case SEVERITY_ERR:
  346. default:
  347. severity = 'E';
  348. break;
  349. }
  350. if (isjson)
  351. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"Code\":%d,\"Msg\":\"", severity, messageid);
  352. else
  353. sprintf(msg_buffer, _STATUS "=%c,Code=%d,Msg=", severity, messageid);
  354. ptr = msg_buffer + strlen(msg_buffer);
  355. switch(codes[i].params) {
  356. case PARAM_GPU:
  357. case PARAM_CPU:
  358. sprintf(ptr, codes[i].description, paramid);
  359. break;
  360. case PARAM_POOL:
  361. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  362. break;
  363. case PARAM_GPUMAX:
  364. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  365. break;
  366. case PARAM_PMAX:
  367. sprintf(ptr, codes[i].description, total_pools);
  368. break;
  369. case PARAM_POOLMAX:
  370. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  371. break;
  372. #ifdef WANT_CPUMINE
  373. case PARAM_GCMAX:
  374. if (opt_n_threads > 0)
  375. cpu = num_processors;
  376. else
  377. cpu = 0;
  378. sprintf(ptr, codes[i].description, nDevs, cpu);
  379. break;
  380. #else
  381. case PARAM_GMAX:
  382. sprintf(ptr, codes[i].description, nDevs);
  383. break;
  384. #endif
  385. case PARAM_CMD:
  386. sprintf(ptr, codes[i].description, JSON_COMMAND);
  387. break;
  388. case PARAM_STR:
  389. sprintf(ptr, codes[i].description, param2);
  390. break;
  391. case PARAM_BOTH:
  392. sprintf(ptr, codes[i].description, paramid, param2);
  393. break;
  394. case PARAM_NONE:
  395. default:
  396. strcpy(ptr, codes[i].description);
  397. }
  398. ptr = msg_buffer + strlen(msg_buffer);
  399. if (isjson)
  400. sprintf(ptr, "\",\"Description\":\"%s\"}" JSON_CLOSE, opt_api_description);
  401. else
  402. sprintf(ptr, ",Description=%s%c", opt_api_description, SEPARATOR);
  403. return msg_buffer;
  404. }
  405. }
  406. if (isjson)
  407. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE,
  408. messageid, opt_api_description);
  409. else
  410. sprintf(msg_buffer, _STATUS "=F,Code=-1,Msg=%d,Description=%s%c",
  411. messageid, opt_api_description, SEPARATOR);
  412. return msg_buffer;
  413. }
  414. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  415. {
  416. if (isjson)
  417. sprintf(io_buffer, "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE,
  418. message(MSG_VERSION, 0, NULL, isjson),
  419. VERSION, APIVERSION);
  420. else
  421. sprintf(io_buffer, "%s" _VERSION ",CGMiner=%s,API=%s%c",
  422. message(MSG_VERSION, 0, NULL, isjson),
  423. VERSION, APIVERSION, SEPARATOR);
  424. }
  425. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  426. {
  427. char buf[BUFSIZ];
  428. int cpucount = 0;
  429. char *adlinuse = (char *)NO;
  430. #ifdef HAVE_ADL
  431. const char *adl = YES;
  432. int i;
  433. for (i = 0; i < nDevs; i++) {
  434. if (gpus[i].has_adl) {
  435. adlinuse = (char *)YES;
  436. break;
  437. }
  438. }
  439. #else
  440. const char *adl = NO;
  441. #endif
  442. #ifdef WANT_CPUMINE
  443. cpucount = opt_n_threads > 0 ? num_processors : 0;
  444. #endif
  445. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  446. if (isjson)
  447. 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);
  448. else
  449. 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);
  450. strcat(io_buffer, buf);
  451. }
  452. static void gpustatus(int gpu, bool isjson)
  453. {
  454. char intensity[20];
  455. char buf[BUFSIZ];
  456. char *enabled;
  457. char *status;
  458. float gt, gv;
  459. int ga, gf, gp, gc, gm, pt;
  460. if (gpu >= 0 && gpu < nDevs) {
  461. struct cgpu_info *cgpu = &gpus[gpu];
  462. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  463. #ifdef HAVE_ADL
  464. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  465. #endif
  466. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  467. if (cgpu->deven != DEV_DISABLED)
  468. enabled = (char *)YES;
  469. else
  470. enabled = (char *)NO;
  471. if (cgpu->status == LIFE_DEAD)
  472. status = (char *)DEAD;
  473. else if (cgpu->status == LIFE_SICK)
  474. status = (char *)SICK;
  475. else if (cgpu->status == LIFE_NOSTART)
  476. status = (char *)NOSTART;
  477. else
  478. status = (char *)ALIVE;
  479. if (cgpu->dynamic)
  480. strcpy(intensity, DYNAMIC);
  481. else
  482. sprintf(intensity, "%d", cgpu->intensity);
  483. if (isjson)
  484. 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}",
  485. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  486. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  487. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  488. cgpu->utility, intensity,
  489. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  490. (unsigned long)(cgpu->last_share_pool_time));
  491. else
  492. 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",
  493. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  494. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  495. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  496. cgpu->utility, intensity,
  497. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  498. (unsigned long)(cgpu->last_share_pool_time), SEPARATOR);
  499. strcat(io_buffer, buf);
  500. }
  501. }
  502. #ifdef WANT_CPUMINE
  503. static void cpustatus(int cpu, bool isjson)
  504. {
  505. char buf[BUFSIZ];
  506. if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
  507. struct cgpu_info *cgpu = &cpus[cpu];
  508. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  509. if (isjson)
  510. sprintf(buf, "{\"CPU\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Utility\":%.2f,\"Last Share Pool\":%d,\"Last Share Time\":%lu}",
  511. cpu, cgpu->total_mhashes / total_secs,
  512. opt_log_interval, cgpu->rolling,
  513. cgpu->accepted, cgpu->rejected,
  514. cgpu->utility,
  515. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  516. (unsigned long)(cgpu->last_share_pool_time));
  517. else
  518. 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",
  519. cpu, cgpu->total_mhashes / total_secs,
  520. opt_log_interval, cgpu->rolling,
  521. cgpu->accepted, cgpu->rejected,
  522. cgpu->utility,
  523. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  524. (unsigned long)(cgpu->last_share_pool_time), SEPARATOR);
  525. strcat(io_buffer, buf);
  526. }
  527. }
  528. #endif
  529. static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  530. {
  531. int i;
  532. if (nDevs == 0 && opt_n_threads == 0) {
  533. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  534. return;
  535. }
  536. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  537. if (isjson) {
  538. strcat(io_buffer, COMMA);
  539. strcat(io_buffer, JSON_DEVS);
  540. }
  541. for (i = 0; i < nDevs; i++) {
  542. if (isjson && i > 0)
  543. strcat(io_buffer, COMMA);
  544. gpustatus(i, isjson);
  545. }
  546. #ifdef WANT_CPUMINE
  547. if (opt_n_threads > 0)
  548. for (i = 0; i < num_processors; i++) {
  549. if (isjson && (i > 0 || nDevs > 0))
  550. strcat(io_buffer, COMMA);
  551. cpustatus(i, isjson);
  552. }
  553. #endif
  554. if (isjson)
  555. strcat(io_buffer, JSON_CLOSE);
  556. }
  557. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  558. {
  559. int id;
  560. if (nDevs == 0) {
  561. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  562. return;
  563. }
  564. if (param == NULL || *param == '\0') {
  565. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  566. return;
  567. }
  568. id = atoi(param);
  569. if (id < 0 || id >= nDevs) {
  570. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  571. return;
  572. }
  573. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  574. if (isjson) {
  575. strcat(io_buffer, COMMA);
  576. strcat(io_buffer, JSON_GPU);
  577. }
  578. gpustatus(id, isjson);
  579. if (isjson)
  580. strcat(io_buffer, JSON_CLOSE);
  581. }
  582. #ifdef WANT_CPUMINE
  583. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  584. {
  585. int id;
  586. if (opt_n_threads == 0) {
  587. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  588. return;
  589. }
  590. if (param == NULL || *param == '\0') {
  591. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  592. return;
  593. }
  594. id = atoi(param);
  595. if (id < 0 || id >= num_processors) {
  596. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  597. return;
  598. }
  599. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  600. if (isjson) {
  601. strcat(io_buffer, COMMA);
  602. strcat(io_buffer, JSON_CPU);
  603. }
  604. cpustatus(id, isjson);
  605. if (isjson)
  606. strcat(io_buffer, JSON_CLOSE);
  607. }
  608. #endif
  609. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  610. {
  611. char buf[BUFSIZ];
  612. char *status, *lp;
  613. int i;
  614. if (total_pools == 0) {
  615. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  616. return;
  617. }
  618. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  619. if (isjson) {
  620. strcat(io_buffer, COMMA);
  621. strcat(io_buffer, JSON_POOLS);
  622. }
  623. for (i = 0; i < total_pools; i++) {
  624. struct pool *pool = pools[i];
  625. if (!pool->enabled)
  626. status = (char *)DISABLED;
  627. else {
  628. if (pool->idle)
  629. status = (char *)DEAD;
  630. else
  631. status = (char *)ALIVE;
  632. }
  633. if (pool->hdr_path)
  634. lp = (char *)YES;
  635. else
  636. lp = (char *)NO;
  637. if (isjson)
  638. 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}",
  639. (i > 0) ? COMMA : "",
  640. i, pool->rpc_url, status, pool->prio, lp,
  641. pool->getwork_requested,
  642. pool->accepted, pool->rejected,
  643. pool->discarded_work,
  644. pool->stale_shares,
  645. pool->getfail_occasions,
  646. pool->remotefail_occasions);
  647. else
  648. 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",
  649. i, pool->rpc_url, status, pool->prio, lp,
  650. pool->getwork_requested,
  651. pool->accepted, pool->rejected,
  652. pool->discarded_work,
  653. pool->stale_shares,
  654. pool->getfail_occasions,
  655. pool->remotefail_occasions, SEPARATOR);
  656. strcat(io_buffer, buf);
  657. }
  658. if (isjson)
  659. strcat(io_buffer, JSON_CLOSE);
  660. }
  661. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  662. {
  663. double utility, mhs;
  664. #ifdef WANT_CPUMINE
  665. char *algo = (char *)(algo_names[opt_algo]);
  666. if (algo == NULL)
  667. algo = "(null)";
  668. #endif
  669. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  670. mhs = total_mhashes_done / total_secs;
  671. #ifdef WANT_CPUMINE
  672. if (isjson)
  673. 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,
  674. message(MSG_SUMM, 0, NULL, isjson),
  675. total_secs, algo, mhs, found_blocks,
  676. total_getworks, total_accepted, total_rejected,
  677. hw_errors, utility, total_discarded, total_stale,
  678. total_go, local_work, total_ro, new_blocks);
  679. else
  680. 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",
  681. message(MSG_SUMM, 0, NULL, isjson),
  682. total_secs, algo, mhs, found_blocks,
  683. total_getworks, total_accepted, total_rejected,
  684. hw_errors, utility, total_discarded, total_stale,
  685. total_go, local_work, total_ro, new_blocks, SEPARATOR);
  686. #else
  687. if (isjson)
  688. 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,
  689. message(MSG_SUMM, 0, NULL, isjson),
  690. total_secs, mhs, found_blocks,
  691. total_getworks, total_accepted, total_rejected,
  692. hw_errors, utility, total_discarded, total_stale,
  693. total_go, local_work, total_ro, new_blocks);
  694. else
  695. 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",
  696. message(MSG_SUMM, 0, NULL, isjson),
  697. total_secs, mhs, found_blocks,
  698. total_getworks, total_accepted, total_rejected,
  699. hw_errors, utility, total_discarded, total_stale,
  700. total_go, local_work, total_ro, new_blocks, SEPARATOR);
  701. #endif
  702. }
  703. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  704. {
  705. struct thr_info *thr;
  706. int gpu;
  707. int id;
  708. int i;
  709. if (gpu_threads == 0) {
  710. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  711. return;
  712. }
  713. if (param == NULL || *param == '\0') {
  714. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  715. return;
  716. }
  717. id = atoi(param);
  718. if (id < 0 || id >= nDevs) {
  719. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  720. return;
  721. }
  722. if (gpus[id].deven != DEV_DISABLED) {
  723. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  724. return;
  725. }
  726. for (i = 0; i < gpu_threads; i++) {
  727. gpu = thr_info[i].cgpu->device_id;
  728. if (gpu == id) {
  729. thr = &thr_info[i];
  730. if (thr->cgpu->status != LIFE_WELL) {
  731. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  732. return;
  733. }
  734. gpus[id].deven = DEV_ENABLED;
  735. tq_push(thr->q, &ping);
  736. }
  737. }
  738. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  739. }
  740. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  741. {
  742. int id;
  743. if (nDevs == 0) {
  744. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  745. return;
  746. }
  747. if (param == NULL || *param == '\0') {
  748. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  749. return;
  750. }
  751. id = atoi(param);
  752. if (id < 0 || id >= nDevs) {
  753. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  754. return;
  755. }
  756. if (gpus[id].deven == DEV_DISABLED) {
  757. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  758. return;
  759. }
  760. gpus[id].deven = DEV_DISABLED;
  761. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  762. }
  763. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  764. {
  765. int id;
  766. if (nDevs == 0) {
  767. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  768. return;
  769. }
  770. if (param == NULL || *param == '\0') {
  771. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  772. return;
  773. }
  774. id = atoi(param);
  775. if (id < 0 || id >= nDevs) {
  776. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  777. return;
  778. }
  779. reinit_device(&gpus[id]);
  780. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  781. }
  782. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  783. {
  784. char buf[BUFSIZ];
  785. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  786. if (isjson)
  787. sprintf(buf, "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE, nDevs);
  788. else
  789. sprintf(buf, _GPUS ",Count=%d%c", nDevs, SEPARATOR);
  790. strcat(io_buffer, buf);
  791. }
  792. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  793. {
  794. char buf[BUFSIZ];
  795. int count = 0;
  796. #ifdef WANT_CPUMINE
  797. count = opt_n_threads > 0 ? num_processors : 0;
  798. #endif
  799. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  800. if (isjson)
  801. sprintf(buf, "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE, count);
  802. else
  803. sprintf(buf, _CPUS ",Count=%d%c", count, SEPARATOR);
  804. strcat(io_buffer, buf);
  805. }
  806. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  807. {
  808. struct pool *pool;
  809. int id;
  810. if (total_pools == 0) {
  811. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  812. return;
  813. }
  814. if (param == NULL || *param == '\0') {
  815. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  816. return;
  817. }
  818. id = atoi(param);
  819. if (id < 0 || id >= total_pools) {
  820. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  821. return;
  822. }
  823. pool = pools[id];
  824. pool->enabled = true;
  825. switch_pools(pool);
  826. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  827. }
  828. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  829. {
  830. int id;
  831. char *gpusep;
  832. if (nDevs == 0) {
  833. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  834. return false;
  835. }
  836. if (param == NULL || *param == '\0') {
  837. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  838. return false;
  839. }
  840. gpusep = strchr(param, GPUSEP);
  841. if (gpusep == NULL) {
  842. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  843. return false;
  844. }
  845. *(gpusep++) = '\0';
  846. id = atoi(param);
  847. if (id < 0 || id >= nDevs) {
  848. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  849. return false;
  850. }
  851. *gpu = id;
  852. *value = gpusep;
  853. return true;
  854. }
  855. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  856. {
  857. int id;
  858. char *value;
  859. int intensity;
  860. char intensitystr[7];
  861. if (!splitgpuvalue(param, &id, &value, isjson))
  862. return;
  863. if (!strncasecmp(value, DYNAMIC, 1)) {
  864. gpus[id].dynamic = true;
  865. strcpy(intensitystr, DYNAMIC);
  866. }
  867. else {
  868. intensity = atoi(value);
  869. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  870. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  871. return;
  872. }
  873. gpus[id].dynamic = false;
  874. gpus[id].intensity = intensity;
  875. sprintf(intensitystr, "%d", intensity);
  876. }
  877. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  878. }
  879. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  880. {
  881. #ifdef HAVE_ADL
  882. int id;
  883. char *value;
  884. int clock;
  885. if (!splitgpuvalue(param, &id, &value, isjson))
  886. return;
  887. clock = atoi(value);
  888. if (set_memoryclock(id, clock))
  889. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  890. else
  891. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  892. #else
  893. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  894. #endif
  895. }
  896. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  897. {
  898. #ifdef HAVE_ADL
  899. int id;
  900. char *value;
  901. int clock;
  902. if (!splitgpuvalue(param, &id, &value, isjson))
  903. return;
  904. clock = atoi(value);
  905. if (set_engineclock(id, clock))
  906. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  907. else
  908. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  909. #else
  910. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  911. #endif
  912. }
  913. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  914. {
  915. #ifdef HAVE_ADL
  916. int id;
  917. char *value;
  918. int fan;
  919. if (!splitgpuvalue(param, &id, &value, isjson))
  920. return;
  921. fan = atoi(value);
  922. if (set_fanspeed(id, fan))
  923. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  924. else
  925. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  926. #else
  927. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  928. #endif
  929. }
  930. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  931. {
  932. #ifdef HAVE_ADL
  933. int id;
  934. char *value;
  935. float vddc;
  936. if (!splitgpuvalue(param, &id, &value, isjson))
  937. return;
  938. vddc = atof(value);
  939. if (set_vddc(id, vddc))
  940. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  941. else
  942. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  943. #else
  944. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  945. #endif
  946. }
  947. static void send_result(SOCKETTYPE c, bool isjson);
  948. void doquit(SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  949. {
  950. if (isjson)
  951. strcpy(io_buffer, JSON_START JSON_BYE);
  952. else
  953. strcpy(io_buffer, _BYE);
  954. send_result(c, isjson);
  955. *io_buffer = '\0';
  956. bye = 1;
  957. kill_work();
  958. }
  959. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  960. {
  961. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  962. }
  963. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  964. {
  965. FILE *fcfg;
  966. if (param == NULL || *param == '\0') {
  967. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  968. return;
  969. }
  970. fcfg = fopen(param, "w");
  971. if (!fcfg) {
  972. strcpy(io_buffer, message(MSG_BADFN, 0, param, isjson));
  973. return;
  974. }
  975. write_config(fcfg);
  976. fclose(fcfg);
  977. strcpy(io_buffer, message(MSG_SAVED, 0, param, isjson));
  978. }
  979. struct CMDS {
  980. char *name;
  981. void (*func)(SOCKETTYPE, char *, bool);
  982. bool requires_writemode;
  983. } cmds[] = {
  984. { "version", apiversion, false },
  985. { "config", minerconfig, false },
  986. { "devs", devstatus, false },
  987. { "pools", poolstatus, false },
  988. { "summary", summary, false },
  989. { "gpuenable", gpuenable, true },
  990. { "gpudisable", gpudisable, true },
  991. { "gpurestart", gpurestart, true },
  992. { "gpu", gpudev, false },
  993. #ifdef WANT_CPUMINE
  994. { "cpu", cpudev, false },
  995. #endif
  996. { "gpucount", gpucount, false },
  997. { "cpucount", cpucount, false },
  998. { "switchpool", switchpool, true },
  999. { "gpuintensity", gpuintensity, true },
  1000. { "gpumem", gpumem, true },
  1001. { "gpuengine", gpuengine, true },
  1002. { "gpufan", gpufan, true },
  1003. { "gpuvddc", gpuvddc, true },
  1004. { "save", dosave, true },
  1005. { "quit", doquit, true },
  1006. { "privileged", privileged, true },
  1007. { NULL, NULL, false }
  1008. };
  1009. static void send_result(SOCKETTYPE c, bool isjson)
  1010. {
  1011. int n;
  1012. int len;
  1013. if (isjson)
  1014. strcat(io_buffer, JSON_END);
  1015. len = strlen(io_buffer);
  1016. applog(LOG_DEBUG, "DBG: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : "");
  1017. // ignore failure - it's closed immediately anyway
  1018. n = send(c, io_buffer, len+1, 0);
  1019. if (opt_debug) {
  1020. if (SOCKETFAIL(n))
  1021. applog(LOG_DEBUG, "DBG: send failed: %s", SOCKERRMSG);
  1022. else
  1023. applog(LOG_DEBUG, "DBG: sent %d", n);
  1024. }
  1025. }
  1026. static void tidyup()
  1027. {
  1028. bye = 1;
  1029. if (sock != INVSOCK) {
  1030. shutdown(sock, SHUT_RDWR);
  1031. CLOSESOCKET(sock);
  1032. sock = INVSOCK;
  1033. }
  1034. if (ipaccess != NULL) {
  1035. free(ipaccess);
  1036. ipaccess = NULL;
  1037. }
  1038. if (msg_buffer != NULL) {
  1039. free(msg_buffer);
  1040. msg_buffer = NULL;
  1041. }
  1042. if (io_buffer != NULL) {
  1043. free(io_buffer);
  1044. io_buffer = NULL;
  1045. }
  1046. }
  1047. /*
  1048. * Interpret [R|W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  1049. * special case of 0/0 allows /0 (means all IP addresses)
  1050. */
  1051. #define ALLIP4 "0/0"
  1052. /*
  1053. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  1054. */
  1055. static void setup_ipaccess()
  1056. {
  1057. char *buf, *ptr, *comma, *slash, *dot;
  1058. int ipcount, mask, octet, i;
  1059. bool writemode;
  1060. buf = malloc(strlen(opt_api_allow) + 1);
  1061. if (unlikely(!buf))
  1062. quit(1, "Failed to malloc ipaccess buf");
  1063. strcpy(buf, opt_api_allow);
  1064. ipcount = 1;
  1065. ptr = buf;
  1066. while (*ptr)
  1067. if (*(ptr++) == ',')
  1068. ipcount++;
  1069. // possibly more than needed, but never less
  1070. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  1071. if (unlikely(!ipaccess))
  1072. quit(1, "Failed to calloc ipaccess");
  1073. ips = 0;
  1074. ptr = buf;
  1075. while (ptr && *ptr) {
  1076. while (*ptr == ' ' || *ptr == '\t')
  1077. ptr++;
  1078. if (*ptr == ',') {
  1079. ptr++;
  1080. continue;
  1081. }
  1082. comma = strchr(ptr, ',');
  1083. if (comma)
  1084. *(comma++) = '\0';
  1085. writemode = false;
  1086. if (isalpha(*ptr) && *(ptr+1) == ':') {
  1087. if (tolower(*ptr) == 'w')
  1088. writemode = true;
  1089. ptr += 2;
  1090. }
  1091. ipaccess[ips].writemode = writemode;
  1092. if (strcmp(ptr, ALLIP4) == 0)
  1093. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  1094. else {
  1095. slash = strchr(ptr, '/');
  1096. if (!slash)
  1097. ipaccess[ips].mask = 0xffffffff;
  1098. else {
  1099. *(slash++) = '\0';
  1100. mask = atoi(slash);
  1101. if (mask < 1 || mask > 32)
  1102. goto popipo; // skip invalid/zero
  1103. ipaccess[ips].mask = 0;
  1104. while (mask-- >= 0) {
  1105. octet = 1 << (mask % 8);
  1106. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  1107. }
  1108. }
  1109. ipaccess[ips].ip = 0; // missing default to '.0'
  1110. for (i = 0; ptr && (i < 4); i++) {
  1111. dot = strchr(ptr, '.');
  1112. if (dot)
  1113. *(dot++) = '\0';
  1114. octet = atoi(ptr);
  1115. if (octet < 0 || octet > 0xff)
  1116. goto popipo; // skip invalid
  1117. ipaccess[ips].ip |= (octet << (i * 8));
  1118. ptr = dot;
  1119. }
  1120. ipaccess[ips].ip &= ipaccess[ips].mask;
  1121. }
  1122. ips++;
  1123. popipo:
  1124. ptr = comma;
  1125. }
  1126. free(buf);
  1127. }
  1128. void api(void)
  1129. {
  1130. char buf[BUFSIZ];
  1131. char param_buf[BUFSIZ];
  1132. const char *localaddr = "127.0.0.1";
  1133. SOCKETTYPE c;
  1134. int n, bound;
  1135. char *connectaddr;
  1136. char *binderror;
  1137. time_t bindstart;
  1138. short int port = opt_api_port;
  1139. struct sockaddr_in serv;
  1140. struct sockaddr_in cli;
  1141. socklen_t clisiz;
  1142. char *cmd;
  1143. char *param;
  1144. bool addrok;
  1145. bool writemode;
  1146. json_error_t json_err;
  1147. json_t *json_config;
  1148. json_t *json_val;
  1149. bool isjson;
  1150. bool did;
  1151. int i;
  1152. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1153. sleep(opt_log_interval);
  1154. if (!opt_api_listen) {
  1155. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1156. return;
  1157. }
  1158. if (opt_api_allow) {
  1159. setup_ipaccess();
  1160. if (ips == 0) {
  1161. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  1162. return;
  1163. }
  1164. }
  1165. sock = socket(AF_INET, SOCK_STREAM, 0);
  1166. if (sock == INVSOCK) {
  1167. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1168. return;
  1169. }
  1170. memset(&serv, 0, sizeof(serv));
  1171. serv.sin_family = AF_INET;
  1172. if (!opt_api_allow && !opt_api_network) {
  1173. serv.sin_addr.s_addr = inet_addr(localaddr);
  1174. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  1175. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1176. return;
  1177. }
  1178. }
  1179. serv.sin_port = htons(port);
  1180. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1181. bound = 0;
  1182. bindstart = time(NULL);
  1183. while (bound == 0) {
  1184. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1185. binderror = SOCKERRMSG;
  1186. if ((time(NULL) - bindstart) > 61)
  1187. break;
  1188. else {
  1189. applog(LOG_WARNING, "API bind to port %d failed - trying again in 15sec", port);
  1190. sleep(15);
  1191. }
  1192. }
  1193. else
  1194. bound = 1;
  1195. }
  1196. if (bound == 0) {
  1197. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1198. return;
  1199. }
  1200. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1201. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1202. CLOSESOCKET(sock);
  1203. return;
  1204. }
  1205. if (opt_api_allow)
  1206. applog(LOG_WARNING, "API running in IP access mode");
  1207. else {
  1208. if (opt_api_network)
  1209. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1210. else
  1211. applog(LOG_WARNING, "API running in local access mode");
  1212. }
  1213. io_buffer = malloc(MYBUFSIZ+1);
  1214. msg_buffer = malloc(MYBUFSIZ+1);
  1215. while (bye == 0) {
  1216. clisiz = sizeof(cli);
  1217. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1218. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1219. goto die;
  1220. }
  1221. connectaddr = inet_ntoa(cli.sin_addr);
  1222. addrok = false;
  1223. writemode = false;
  1224. if (opt_api_allow) {
  1225. for (i = 0; i < ips; i++) {
  1226. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  1227. addrok = true;
  1228. writemode = ipaccess[i].writemode;
  1229. break;
  1230. }
  1231. }
  1232. } else {
  1233. if (opt_api_network)
  1234. addrok = true;
  1235. else
  1236. addrok = (strcmp(connectaddr, localaddr) == 0);
  1237. }
  1238. if (opt_debug)
  1239. applog(LOG_DEBUG, "DBG: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1240. if (addrok) {
  1241. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1242. if (SOCKETFAIL(n))
  1243. buf[0] = '\0';
  1244. else
  1245. buf[n] = '\0';
  1246. if (opt_debug) {
  1247. if (SOCKETFAIL(n))
  1248. applog(LOG_DEBUG, "DBG: recv failed: %s", SOCKERRMSG);
  1249. else
  1250. applog(LOG_DEBUG, "DBG: recv command: (%d) '%s'", n, buf);
  1251. }
  1252. if (!SOCKETFAIL(n)) {
  1253. did = false;
  1254. if (*buf != ISJSON) {
  1255. isjson = false;
  1256. param = strchr(buf, SEPARATOR);
  1257. if (param != NULL)
  1258. *(param++) = '\0';
  1259. cmd = buf;
  1260. }
  1261. else {
  1262. isjson = true;
  1263. param = NULL;
  1264. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  1265. json_config = json_loadb(buf, n, 0, &json_err);
  1266. #elif JANSSON_MAJOR_VERSION > 1
  1267. json_config = json_loads(buf, 0, &json_err);
  1268. #else
  1269. json_config = json_loads(buf, &json_err);
  1270. #endif
  1271. if (!json_is_object(json_config)) {
  1272. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1273. send_result(c, isjson);
  1274. did = true;
  1275. }
  1276. else {
  1277. json_val = json_object_get(json_config, JSON_COMMAND);
  1278. if (json_val == NULL) {
  1279. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1280. send_result(c, isjson);
  1281. did = true;
  1282. }
  1283. else {
  1284. if (!json_is_string(json_val)) {
  1285. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1286. send_result(c, isjson);
  1287. did = true;
  1288. }
  1289. else {
  1290. cmd = (char *)json_string_value(json_val);
  1291. json_val = json_object_get(json_config, JSON_PARAMETER);
  1292. if (json_is_string(json_val))
  1293. param = (char *)json_string_value(json_val);
  1294. else if (json_is_integer(json_val)) {
  1295. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1296. param = param_buf;
  1297. } else if (json_is_real(json_val)) {
  1298. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1299. param = param_buf;
  1300. }
  1301. }
  1302. }
  1303. }
  1304. }
  1305. if (!did)
  1306. for (i = 0; cmds[i].name != NULL; i++) {
  1307. if (strcmp(cmd, cmds[i].name) == 0) {
  1308. if (cmds[i].requires_writemode && !writemode) {
  1309. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  1310. applog(LOG_DEBUG, "DBG: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  1311. }
  1312. else
  1313. (cmds[i].func)(c, param, isjson);
  1314. send_result(c, isjson);
  1315. did = true;
  1316. break;
  1317. }
  1318. }
  1319. if (!did) {
  1320. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1321. send_result(c, isjson);
  1322. }
  1323. }
  1324. }
  1325. CLOSESOCKET(c);
  1326. }
  1327. die:
  1328. tidyup();
  1329. }