api.c 40 KB

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