api.c 40 KB

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