api.c 41 KB

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