api.c 39 KB

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