api.c 37 KB

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