api.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  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 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. *
  10. * Note: the code always includes GPU support even if there are no GPUs
  11. * this simplifies handling multiple other device code being included
  12. * depending on compile options
  13. */
  14. #include "config.h"
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdbool.h>
  20. #include <stdint.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include "compat.h"
  24. #include "miner.h"
  25. #include "driver-cpu.h" /* for algo_names[], TODO: re-factor dependency */
  26. #if defined(unix) || defined(__APPLE__)
  27. #include <errno.h>
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #define SOCKETTYPE int
  32. #define SOCKETFAIL(a) ((a) < 0)
  33. #define INVSOCK -1
  34. #define INVINETADDR -1
  35. #define CLOSESOCKET close
  36. #define SOCKERRMSG strerror(errno)
  37. #endif
  38. #ifdef WIN32
  39. #include <ws2tcpip.h>
  40. #include <winsock2.h>
  41. #define SOCKETTYPE SOCKET
  42. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  43. #define INVSOCK INVALID_SOCKET
  44. #define INVINETADDR INADDR_NONE
  45. #define CLOSESOCKET closesocket
  46. static char WSAbuf[1024];
  47. struct WSAERRORS {
  48. int id;
  49. char *code;
  50. } WSAErrors[] = {
  51. { 0, "No error" },
  52. { WSAEINTR, "Interrupted system call" },
  53. { WSAEBADF, "Bad file number" },
  54. { WSAEACCES, "Permission denied" },
  55. { WSAEFAULT, "Bad address" },
  56. { WSAEINVAL, "Invalid argument" },
  57. { WSAEMFILE, "Too many open sockets" },
  58. { WSAEWOULDBLOCK, "Operation would block" },
  59. { WSAEINPROGRESS, "Operation now in progress" },
  60. { WSAEALREADY, "Operation already in progress" },
  61. { WSAENOTSOCK, "Socket operation on non-socket" },
  62. { WSAEDESTADDRREQ, "Destination address required" },
  63. { WSAEMSGSIZE, "Message too long" },
  64. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  65. { WSAENOPROTOOPT, "Bad protocol option" },
  66. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  67. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  68. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  69. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  70. { WSAEAFNOSUPPORT, "Address family not supported" },
  71. { WSAEADDRINUSE, "Address already in use" },
  72. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  73. { WSAENETDOWN, "Network is down" },
  74. { WSAENETUNREACH, "Network is unreachable" },
  75. { WSAENETRESET, "Net connection reset" },
  76. { WSAECONNABORTED, "Software caused connection abort" },
  77. { WSAECONNRESET, "Connection reset by peer" },
  78. { WSAENOBUFS, "No buffer space available" },
  79. { WSAEISCONN, "Socket is already connected" },
  80. { WSAENOTCONN, "Socket is not connected" },
  81. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  82. { WSAETOOMANYREFS, "Too many references, can't splice" },
  83. { WSAETIMEDOUT, "Connection timed out" },
  84. { WSAECONNREFUSED, "Connection refused" },
  85. { WSAELOOP, "Too many levels of symbolic links" },
  86. { WSAENAMETOOLONG, "File name too long" },
  87. { WSAEHOSTDOWN, "Host is down" },
  88. { WSAEHOSTUNREACH, "No route to host" },
  89. { WSAENOTEMPTY, "Directory not empty" },
  90. { WSAEPROCLIM, "Too many processes" },
  91. { WSAEUSERS, "Too many users" },
  92. { WSAEDQUOT, "Disc quota exceeded" },
  93. { WSAESTALE, "Stale NFS file handle" },
  94. { WSAEREMOTE, "Too many levels of remote in path" },
  95. { WSASYSNOTREADY, "Network system is unavailable" },
  96. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  97. { WSANOTINITIALISED, "WSAStartup not yet called" },
  98. { WSAEDISCON, "Graceful shutdown in progress" },
  99. { WSAHOST_NOT_FOUND, "Host not found" },
  100. { WSANO_DATA, "No host data of that type was found" },
  101. { -1, "Unknown error code" }
  102. };
  103. static char *WSAErrorMsg()
  104. {
  105. int i;
  106. int id = WSAGetLastError();
  107. /* Assume none of them are actually -1 */
  108. for (i = 0; WSAErrors[i].id != -1; i++)
  109. if (WSAErrors[i].id == id)
  110. break;
  111. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  112. return &(WSAbuf[0]);
  113. }
  114. #define SOCKERRMSG WSAErrorMsg()
  115. #ifndef SHUT_RDWR
  116. #define SHUT_RDWR SD_BOTH
  117. #endif
  118. #ifndef in_addr_t
  119. #define in_addr_t uint32_t
  120. #endif
  121. #endif
  122. // Big enough for largest API request
  123. // though a PC with 100s of PGAs/CPUs may exceed the size ...
  124. // Current code assumes it can socket send this size also
  125. #define MYBUFSIZ 65432 // TODO: intercept before it's exceeded
  126. // Number of requests to queue - normally would be small
  127. // However lots of PGA's may mean more
  128. #define QUEUE 100
  129. static char *io_buffer = NULL;
  130. static char *msg_buffer = NULL;
  131. static SOCKETTYPE sock = INVSOCK;
  132. static const char *UNAVAILABLE = " - API will not be available";
  133. //static const char *BLANK = "";
  134. static const char *COMMA = ",";
  135. static const char SEPARATOR = '|';
  136. static const char GPUSEP = ',';
  137. static const char *APIVERSION = "1.7";
  138. static const char *DEAD = "Dead";
  139. static const char *SICK = "Sick";
  140. static const char *NOSTART = "NoStart";
  141. static const char *DISABLED = "Disabled";
  142. static const char *ALIVE = "Alive";
  143. static const char *UNKNOWN = "Unknown";
  144. #define _DYNAMIC "D"
  145. static const char *DYNAMIC = _DYNAMIC;
  146. static const char *YES = "Y";
  147. static const char *NO = "N";
  148. static const char *DEVICECODE = ""
  149. #ifdef HAVE_OPENCL
  150. "GPU "
  151. #endif
  152. #ifdef USE_BITFORCE
  153. "BFL "
  154. #endif
  155. #ifdef USE_ICARUS
  156. "ICA "
  157. #endif
  158. #ifdef WANT_CPUMINE
  159. "CPU "
  160. #endif
  161. "";
  162. static const char *OSINFO =
  163. #if defined(__linux)
  164. "Linux";
  165. #else
  166. #if defined(__APPLE__)
  167. "Apple";
  168. #else
  169. #if defined (WIN32)
  170. "Windows";
  171. #else
  172. #if defined(unix)
  173. "Unix";
  174. #else
  175. "Unknown";
  176. #endif
  177. #endif
  178. #endif
  179. #endif
  180. #define _DEVS "DEVS"
  181. #define _POOLS "POOLS"
  182. #define _SUMMARY "SUMMARY"
  183. #define _STATUS "STATUS"
  184. #define _VERSION "VERSION"
  185. #define _MINECON "CONFIG"
  186. #define _GPU "GPU"
  187. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  188. #define _PGA "PGA"
  189. #endif
  190. #ifdef WANT_CPUMINE
  191. #define _CPU "CPU"
  192. #endif
  193. #define _GPUS "GPUS"
  194. #define _PGAS "PGAS"
  195. #define _CPUS "CPUS"
  196. #define _NOTIFY "NOTIFY"
  197. #define _BYE "BYE"
  198. static const char ISJSON = '{';
  199. #define JSON0 "{"
  200. #define JSON1 "\""
  201. #define JSON2 "\":["
  202. #define JSON3 "]"
  203. #define JSON4 ",\"id\":1}"
  204. #define JSON_START JSON0
  205. #define JSON_DEVS JSON1 _DEVS JSON2
  206. #define JSON_POOLS JSON1 _POOLS JSON2
  207. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  208. #define JSON_STATUS JSON1 _STATUS JSON2
  209. #define JSON_VERSION JSON1 _VERSION JSON2
  210. #define JSON_MINECON JSON1 _MINECON JSON2
  211. #define JSON_GPU JSON1 _GPU JSON2
  212. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  213. #define JSON_PGA JSON1 _PGA JSON2
  214. #endif
  215. #ifdef WANT_CPUMINE
  216. #define JSON_CPU JSON1 _CPU JSON2
  217. #endif
  218. #define JSON_GPUS JSON1 _GPUS JSON2
  219. #define JSON_PGAS JSON1 _PGAS JSON2
  220. #define JSON_CPUS JSON1 _CPUS JSON2
  221. #define JSON_NOTIFY JSON1 _NOTIFY JSON2
  222. #define JSON_BYE JSON1 _BYE JSON1
  223. #define JSON_CLOSE JSON3
  224. #define JSON_END JSON4
  225. static const char *JSON_COMMAND = "command";
  226. static const char *JSON_PARAMETER = "parameter";
  227. #define MSG_INVGPU 1
  228. #define MSG_ALRENA 2
  229. #define MSG_ALRDIS 3
  230. #define MSG_GPUMRE 4
  231. #define MSG_GPUREN 5
  232. #define MSG_GPUNON 6
  233. #define MSG_POOL 7
  234. #define MSG_NOPOOL 8
  235. #define MSG_DEVS 9
  236. #define MSG_NODEVS 10
  237. #define MSG_SUMM 11
  238. #define MSG_GPUDIS 12
  239. #define MSG_GPUREI 13
  240. #define MSG_INVCMD 14
  241. #define MSG_MISID 15
  242. #define MSG_GPUDEV 17
  243. #ifdef WANT_CPUMINE
  244. #define MSG_CPUNON 16
  245. #define MSG_CPUDEV 18
  246. #define MSG_INVCPU 19
  247. #endif
  248. #define MSG_NUMGPU 20
  249. #define MSG_NUMCPU 21
  250. #define MSG_VERSION 22
  251. #define MSG_INVJSON 23
  252. #define MSG_MISCMD 24
  253. #define MSG_MISPID 25
  254. #define MSG_INVPID 26
  255. #define MSG_SWITCHP 27
  256. #define MSG_MISVAL 28
  257. #define MSG_NOADL 29
  258. #define MSG_NOGPUADL 30
  259. #define MSG_INVINT 31
  260. #define MSG_GPUINT 32
  261. #define MSG_MINECON 33
  262. #define MSG_GPUMERR 34
  263. #define MSG_GPUMEM 35
  264. #define MSG_GPUEERR 36
  265. #define MSG_GPUENG 37
  266. #define MSG_GPUVERR 38
  267. #define MSG_GPUVDDC 39
  268. #define MSG_GPUFERR 40
  269. #define MSG_GPUFAN 41
  270. #define MSG_MISFN 42
  271. #define MSG_BADFN 43
  272. #define MSG_SAVED 44
  273. #define MSG_ACCDENY 45
  274. #define MSG_ACCOK 46
  275. #define MSG_ENAPOOL 47
  276. #define MSG_DISPOOL 48
  277. #define MSG_ALRENAP 49
  278. #define MSG_ALRDISP 50
  279. #define MSG_DISLASTP 51
  280. #define MSG_MISPDP 52
  281. #define MSG_INVPDP 53
  282. #define MSG_TOOMANYP 54
  283. #define MSG_ADDPOOL 55
  284. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  285. #define MSG_PGANON 56
  286. #define MSG_PGADEV 57
  287. #define MSG_INVPGA 58
  288. #endif
  289. #define MSG_NUMPGA 59
  290. #define MSG_NOTIFY 60
  291. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  292. #define MSG_PGALRENA 61
  293. #define MSG_PGALRDIS 62
  294. #define MSG_PGAENA 63
  295. #define MSG_PGADIS 64
  296. #define MSG_PGAUNW 65
  297. #endif
  298. #define MSG_REMLASTP 66
  299. #define MSG_ACTPOOL 67
  300. #define MSG_REMPOOL 68
  301. enum code_severity {
  302. SEVERITY_ERR,
  303. SEVERITY_WARN,
  304. SEVERITY_INFO,
  305. SEVERITY_SUCC,
  306. SEVERITY_FAIL
  307. };
  308. enum code_parameters {
  309. PARAM_GPU,
  310. PARAM_PGA,
  311. PARAM_CPU,
  312. PARAM_GPUMAX,
  313. PARAM_PGAMAX,
  314. PARAM_CPUMAX,
  315. PARAM_PMAX,
  316. PARAM_POOLMAX,
  317. // Single generic case: have the code resolve it - see below
  318. PARAM_DMAX,
  319. PARAM_CMD,
  320. PARAM_POOL,
  321. PARAM_STR,
  322. PARAM_BOTH,
  323. PARAM_NONE
  324. };
  325. struct CODES {
  326. const enum code_severity severity;
  327. const int code;
  328. const enum code_parameters params;
  329. const char *description;
  330. } codes[] = {
  331. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  332. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  333. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  334. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  335. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  336. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  337. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  338. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  339. { SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX, "%d GPU(s)"
  340. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  341. " - %d PGA(s)"
  342. #endif
  343. #ifdef WANT_CPUMINE
  344. " - %d CPU(s)"
  345. #endif
  346. },
  347. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
  348. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  349. "/PGAs"
  350. #endif
  351. #ifdef WANT_CPUMINE
  352. "/CPUs"
  353. #endif
  354. },
  355. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  356. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  357. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  358. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  359. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  360. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  361. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  362. { SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
  363. { SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
  364. { SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
  365. { SEVERITY_INFO, MSG_PGALRENA,PARAM_PGA, "PGA %d already enabled" },
  366. { SEVERITY_INFO, MSG_PGALRDIS,PARAM_PGA, "PGA %d already disabled" },
  367. { SEVERITY_INFO, MSG_PGAENA, PARAM_PGA, "PGA %d sent enable message" },
  368. { SEVERITY_INFO, MSG_PGADIS, PARAM_PGA, "PGA %d set disable flag" },
  369. { SEVERITY_ERR, MSG_PGAUNW, PARAM_PGA, "PGA %d is not flagged WELL, cannot enable" },
  370. #endif
  371. #ifdef WANT_CPUMINE
  372. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  373. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  374. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  375. #endif
  376. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  377. { SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
  378. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  379. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "CGMiner versions" },
  380. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  381. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  382. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  383. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  384. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  385. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  386. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  387. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  388. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  389. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  390. { SEVERITY_SUCC, MSG_MINECON, PARAM_NONE, "CGMiner config" },
  391. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  392. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported succeess" },
  393. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  394. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported succeess" },
  395. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  396. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported succeess" },
  397. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  398. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported succeess" },
  399. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  400. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  401. { SEVERITY_ERR, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  402. { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
  403. { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
  404. { SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
  405. { SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
  406. { SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
  407. { SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
  408. { SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
  409. { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
  410. { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
  411. { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
  412. { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
  413. { SEVERITY_ERR, MSG_REMLASTP,PARAM_POOL, "Cannot remove last pool %d:'%s'" },
  414. { SEVERITY_ERR, MSG_ACTPOOL, PARAM_POOL, "Cannot remove active pool %d:'%s'" },
  415. { SEVERITY_SUCC, MSG_REMPOOL, PARAM_BOTH, "Removed pool %d:'%s'" },
  416. { SEVERITY_SUCC, MSG_NOTIFY, PARAM_NONE, "Notify" },
  417. { SEVERITY_FAIL, 0, 0, NULL }
  418. };
  419. static int my_thr_id = 0;
  420. static int bye = 0;
  421. static bool ping = true;
  422. static time_t when = 0; // when the request occurred
  423. struct IP4ACCESS {
  424. in_addr_t ip;
  425. in_addr_t mask;
  426. bool writemode;
  427. };
  428. static struct IP4ACCESS *ipaccess = NULL;
  429. static int ips = 0;
  430. #ifdef USE_BITFORCE
  431. extern struct device_api bitforce_api;
  432. #endif
  433. #ifdef USE_ICARUS
  434. extern struct device_api icarus_api;
  435. #endif
  436. // This is only called when expected to be needed (rarely)
  437. // i.e. strings outside of the codes control (input from the user)
  438. static char *escape_string(char *str, bool isjson)
  439. {
  440. char *buf, *ptr;
  441. int count;
  442. count = 0;
  443. for (ptr = str; *ptr; ptr++) {
  444. switch (*ptr) {
  445. case ',':
  446. case '|':
  447. case '=':
  448. if (!isjson)
  449. count++;
  450. break;
  451. case '"':
  452. if (isjson)
  453. count++;
  454. break;
  455. case '\\':
  456. count++;
  457. break;
  458. }
  459. }
  460. if (count == 0)
  461. return str;
  462. buf = malloc(strlen(str) + count + 1);
  463. if (unlikely(!buf))
  464. quit(1, "Failed to malloc escape buf");
  465. ptr = buf;
  466. while (*str)
  467. switch (*str) {
  468. case ',':
  469. case '|':
  470. case '=':
  471. if (!isjson)
  472. *(ptr++) = '\\';
  473. *(ptr++) = *(str++);
  474. break;
  475. case '"':
  476. if (isjson)
  477. *(ptr++) = '\\';
  478. *(ptr++) = *(str++);
  479. break;
  480. case '\\':
  481. *(ptr++) = '\\';
  482. *(ptr++) = *(str++);
  483. break;
  484. default:
  485. *(ptr++) = *(str++);
  486. break;
  487. }
  488. *ptr = '\0';
  489. return buf;
  490. }
  491. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  492. static int numpgas()
  493. {
  494. int count = 0;
  495. int i;
  496. for (i = 0; i < total_devices; i++) {
  497. #ifdef USE_BITFORCE
  498. if (devices[i]->api == &bitforce_api)
  499. count++;
  500. #endif
  501. #ifdef USE_ICARUS
  502. if (devices[i]->api == &icarus_api)
  503. count++;
  504. #endif
  505. }
  506. return count;
  507. }
  508. static int pgadevice(int pgaid)
  509. {
  510. int count = 0;
  511. int i;
  512. for (i = 0; i < total_devices; i++) {
  513. #ifdef USE_BITFORCE
  514. if (devices[i]->api == &bitforce_api)
  515. count++;
  516. #endif
  517. #ifdef USE_ICARUS
  518. if (devices[i]->api == &icarus_api)
  519. count++;
  520. #endif
  521. if (count == (pgaid + 1))
  522. return i;
  523. }
  524. return -1;
  525. }
  526. #endif
  527. // All replies (except BYE) start with a message
  528. // thus for JSON, message() inserts JSON_START at the front
  529. // and send_result() adds JSON_END at the end
  530. static char *message(int messageid, int paramid, char *param2, bool isjson)
  531. {
  532. char severity;
  533. char *ptr;
  534. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  535. int pga;
  536. #endif
  537. #ifdef WANT_CPUMINE
  538. int cpu;
  539. #endif
  540. int i;
  541. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  542. if (codes[i].code == messageid) {
  543. switch (codes[i].severity) {
  544. case SEVERITY_WARN:
  545. severity = 'W';
  546. break;
  547. case SEVERITY_INFO:
  548. severity = 'I';
  549. break;
  550. case SEVERITY_SUCC:
  551. severity = 'S';
  552. break;
  553. case SEVERITY_ERR:
  554. default:
  555. severity = 'E';
  556. break;
  557. }
  558. if (isjson)
  559. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"When\":%lu,\"Code\":%d,\"Msg\":\"", severity, (unsigned long)when, messageid);
  560. else
  561. sprintf(msg_buffer, _STATUS "=%c,When=%lu,Code=%d,Msg=", severity, (unsigned long)when, messageid);
  562. ptr = msg_buffer + strlen(msg_buffer);
  563. switch(codes[i].params) {
  564. case PARAM_GPU:
  565. case PARAM_PGA:
  566. case PARAM_CPU:
  567. sprintf(ptr, codes[i].description, paramid);
  568. break;
  569. case PARAM_POOL:
  570. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  571. break;
  572. case PARAM_GPUMAX:
  573. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  574. break;
  575. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  576. case PARAM_PGAMAX:
  577. pga = numpgas();
  578. sprintf(ptr, codes[i].description, paramid, pga - 1);
  579. break;
  580. #endif
  581. #ifdef WANT_CPUMINE
  582. case PARAM_CPUMAX:
  583. if (opt_n_threads > 0)
  584. cpu = num_processors;
  585. else
  586. cpu = 0;
  587. sprintf(ptr, codes[i].description, paramid, cpu - 1);
  588. break;
  589. #endif
  590. case PARAM_PMAX:
  591. sprintf(ptr, codes[i].description, total_pools);
  592. break;
  593. case PARAM_POOLMAX:
  594. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  595. break;
  596. case PARAM_DMAX:
  597. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  598. pga = numpgas();
  599. #endif
  600. #ifdef WANT_CPUMINE
  601. if (opt_n_threads > 0)
  602. cpu = num_processors;
  603. else
  604. cpu = 0;
  605. #endif
  606. sprintf(ptr, codes[i].description, nDevs
  607. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  608. , pga
  609. #endif
  610. #ifdef WANT_CPUMINE
  611. , cpu
  612. #endif
  613. );
  614. break;
  615. case PARAM_CMD:
  616. sprintf(ptr, codes[i].description, JSON_COMMAND);
  617. break;
  618. case PARAM_STR:
  619. sprintf(ptr, codes[i].description, param2);
  620. break;
  621. case PARAM_BOTH:
  622. sprintf(ptr, codes[i].description, paramid, param2);
  623. break;
  624. case PARAM_NONE:
  625. default:
  626. strcpy(ptr, codes[i].description);
  627. }
  628. ptr = msg_buffer + strlen(msg_buffer);
  629. if (isjson)
  630. sprintf(ptr, "\",\"Description\":\"%s\"}" JSON_CLOSE, opt_api_description);
  631. else
  632. sprintf(ptr, ",Description=%s%c", opt_api_description, SEPARATOR);
  633. return msg_buffer;
  634. }
  635. }
  636. if (isjson)
  637. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"When\":%lu,\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE,
  638. (unsigned long)when, messageid, opt_api_description);
  639. else
  640. sprintf(msg_buffer, _STATUS "=F,When=%lu,Code=-1,Msg=%d,Description=%s%c",
  641. (unsigned long)when, messageid, opt_api_description, SEPARATOR);
  642. return msg_buffer;
  643. }
  644. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  645. {
  646. if (isjson)
  647. sprintf(io_buffer, "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE,
  648. message(MSG_VERSION, 0, NULL, isjson),
  649. VERSION, APIVERSION);
  650. else
  651. sprintf(io_buffer, "%s" _VERSION ",CGMiner=%s,API=%s%c",
  652. message(MSG_VERSION, 0, NULL, isjson),
  653. VERSION, APIVERSION, SEPARATOR);
  654. }
  655. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  656. {
  657. char buf[BUFSIZ];
  658. int pgacount = 0;
  659. int cpucount = 0;
  660. char *adlinuse = (char *)NO;
  661. #ifdef HAVE_ADL
  662. const char *adl = YES;
  663. int i;
  664. for (i = 0; i < nDevs; i++) {
  665. if (gpus[i].has_adl) {
  666. adlinuse = (char *)YES;
  667. break;
  668. }
  669. }
  670. #else
  671. const char *adl = NO;
  672. #endif
  673. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  674. pgacount = numpgas();
  675. #endif
  676. #ifdef WANT_CPUMINE
  677. cpucount = opt_n_threads > 0 ? num_processors : 0;
  678. #endif
  679. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  680. if (isjson)
  681. sprintf(buf, "," JSON_MINECON "{\"GPU Count\":%d,\"PGA Count\":%d,\"CPU Count\":%d,\"Pool Count\":%d,\"ADL\":\"%s\",\"ADL in use\":\"%s\",\"Strategy\":\"%s\",\"Log Interval\":%d,\"Device Code\":\"%s\",\"OS\":\"%s\"}" JSON_CLOSE, nDevs, pgacount, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, opt_log_interval, DEVICECODE, OSINFO);
  682. else
  683. sprintf(buf, _MINECON ",GPU Count=%d,PGA Count=%d,CPU Count=%d,Pool Count=%d,ADL=%s,ADL in use=%s,Strategy=%s,Log Interval=%d,Device Code=%s,OS=%s%c", nDevs, pgacount, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, opt_log_interval, DEVICECODE, OSINFO, SEPARATOR);
  684. strcat(io_buffer, buf);
  685. }
  686. static const char*
  687. bool2str(bool b)
  688. {
  689. return b ? YES : NO;
  690. }
  691. static const char*
  692. status2str(enum alive status)
  693. {
  694. switch (status) {
  695. case LIFE_WELL:
  696. return ALIVE;
  697. case LIFE_SICK:
  698. return SICK;
  699. case LIFE_DEAD:
  700. return DEAD;
  701. case LIFE_NOSTART:
  702. return NOSTART;
  703. default:
  704. return UNKNOWN;
  705. }
  706. }
  707. #ifdef HAVE_OPENCL
  708. extern struct device_api opencl_api;
  709. #endif
  710. static void devstatus_an(char *buf, struct cgpu_info *cgpu, bool isjson)
  711. {
  712. tailsprintf(buf, isjson
  713. ? "{\"%s\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f"
  714. : "%s=%d,Enabled=%s,Status=%s,Temperature=%.2f",
  715. cgpu->api->name, cgpu->device_id,
  716. bool2str(cgpu->deven != DEV_DISABLED),
  717. status2str(cgpu->status),
  718. cgpu->temp
  719. );
  720. #ifdef HAVE_OPENCL
  721. if (cgpu->api == &opencl_api) {
  722. float gt, gv;
  723. int ga, gf, gp, gc, gm, pt;
  724. #ifdef HAVE_ADL
  725. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  726. #endif
  727. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  728. tailsprintf(buf, isjson
  729. ? ",\"Fan Speed\":%d,\"Fan Percent\":%d,\"GPU Clock\":%d,\"Memory Clock\":%d,\"GPU Voltage\":%.3f,\"GPU Activity\":%d,\"Powertune\":%d"
  730. : ",Fan Speed=%d,Fan Percent=%d,GPU Clock=%d,Memory Clock=%d,GPU Voltage=%.3f,GPU Activity=%d,Powertune=%d",
  731. gf, gp, gc, gm, gv, ga, pt
  732. );
  733. }
  734. #endif
  735. tailsprintf(buf, isjson
  736. ? ",\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f"
  737. : ",MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f",
  738. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  739. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  740. cgpu->utility
  741. );
  742. #ifdef HAVE_OPENCL
  743. if (cgpu->api == &opencl_api) {
  744. char intensity[20];
  745. if (cgpu->dynamic)
  746. strcpy(intensity, DYNAMIC);
  747. else
  748. sprintf(intensity, "%d", cgpu->intensity);
  749. tailsprintf(buf, isjson
  750. ? ",\"Intensity\":\"%s\""
  751. : ",Intensity=%s",
  752. intensity
  753. );
  754. }
  755. #endif
  756. tailsprintf(buf, isjson
  757. ? ",\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f"
  758. : ",Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f",
  759. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  760. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  761. tailsprintf(buf, isjson ? ",\"Driver\":\"%s\"" : ",Driver=%s", cgpu->api->dname);
  762. if (cgpu->kname)
  763. tailsprintf(buf, isjson ? ",\"Kernel\":\"%s\"" : ",Kernel=%s", cgpu->kname);
  764. if (cgpu->name)
  765. tailsprintf(buf, isjson ? ",\"Model\":\"%s\"" : ",Model=%s", cgpu->name);
  766. if (cgpu->device_path)
  767. tailsprintf(buf, isjson ? ",\"Device Path\":\"%s\"" : ",Device Path=%s", cgpu->device_path);
  768. if (isjson)
  769. tailsprintf(buf, "}");
  770. else
  771. tailsprintf(buf, "%c", SEPARATOR);
  772. }
  773. static void gpustatus(int gpu, bool isjson)
  774. {
  775. if (gpu < 0 || gpu >= nDevs)
  776. return;
  777. devstatus_an(io_buffer, &gpus[gpu], isjson);
  778. }
  779. static void pgastatus(int pga, bool isjson)
  780. {
  781. int dev = pgadevice(pga);
  782. if (dev < 0) // Should never happen
  783. return;
  784. devstatus_an(io_buffer, devices[dev], isjson);
  785. }
  786. static void cpustatus(int cpu, bool isjson)
  787. {
  788. if (opt_n_threads <= 0 || cpu < 0 || cpu >= num_processors)
  789. return;
  790. devstatus_an(io_buffer, &cpus[cpu], isjson);
  791. }
  792. static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  793. {
  794. int devcount = 0;
  795. int i;
  796. if (total_devices == 0) {
  797. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  798. return;
  799. }
  800. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  801. if (isjson) {
  802. strcat(io_buffer, COMMA);
  803. strcat(io_buffer, JSON_DEVS);
  804. }
  805. for (i = 0; i < nDevs; i++) {
  806. if (isjson && devcount > 0)
  807. strcat(io_buffer, COMMA);
  808. gpustatus(i, isjson);
  809. devcount++;
  810. }
  811. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  812. int numpga = numpgas();
  813. if (numpga > 0)
  814. for (i = 0; i < numpga; i++) {
  815. if (isjson && devcount > 0)
  816. strcat(io_buffer, COMMA);
  817. pgastatus(i, isjson);
  818. devcount++;
  819. }
  820. #endif
  821. #ifdef WANT_CPUMINE
  822. if (opt_n_threads > 0)
  823. for (i = 0; i < num_processors; i++) {
  824. if (isjson && devcount > 0)
  825. strcat(io_buffer, COMMA);
  826. cpustatus(i, isjson);
  827. devcount++;
  828. }
  829. #endif
  830. if (isjson)
  831. strcat(io_buffer, JSON_CLOSE);
  832. }
  833. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  834. {
  835. int id;
  836. if (nDevs == 0) {
  837. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  838. return;
  839. }
  840. if (param == NULL || *param == '\0') {
  841. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  842. return;
  843. }
  844. id = atoi(param);
  845. if (id < 0 || id >= nDevs) {
  846. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  847. return;
  848. }
  849. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  850. if (isjson) {
  851. strcat(io_buffer, COMMA);
  852. strcat(io_buffer, JSON_GPU);
  853. }
  854. gpustatus(id, isjson);
  855. if (isjson)
  856. strcat(io_buffer, JSON_CLOSE);
  857. }
  858. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  859. static void pgadev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  860. {
  861. int numpga = numpgas();
  862. int id;
  863. if (numpga == 0) {
  864. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  865. return;
  866. }
  867. if (param == NULL || *param == '\0') {
  868. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  869. return;
  870. }
  871. id = atoi(param);
  872. if (id < 0 || id >= numpga) {
  873. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  874. return;
  875. }
  876. strcpy(io_buffer, message(MSG_PGADEV, id, NULL, isjson));
  877. if (isjson) {
  878. strcat(io_buffer, COMMA);
  879. strcat(io_buffer, JSON_PGA);
  880. }
  881. pgastatus(id, isjson);
  882. if (isjson)
  883. strcat(io_buffer, JSON_CLOSE);
  884. }
  885. static void pgaenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  886. {
  887. int numpga = numpgas();
  888. struct thr_info *thr;
  889. int pga;
  890. int id;
  891. int i;
  892. if (numpga == 0) {
  893. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  894. return;
  895. }
  896. if (param == NULL || *param == '\0') {
  897. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  898. return;
  899. }
  900. id = atoi(param);
  901. if (id < 0 || id >= numpga) {
  902. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  903. return;
  904. }
  905. int dev = pgadevice(id);
  906. if (dev < 0) { // Should never happen
  907. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  908. return;
  909. }
  910. struct cgpu_info *cgpu = devices[dev];
  911. if (cgpu->deven != DEV_DISABLED) {
  912. strcpy(io_buffer, message(MSG_PGALRENA, id, NULL, isjson));
  913. return;
  914. }
  915. if (cgpu->status != LIFE_WELL) {
  916. strcpy(io_buffer, message(MSG_PGAUNW, id, NULL, isjson));
  917. return;
  918. }
  919. for (i = 0; i < mining_threads; i++) {
  920. pga = thr_info[i].cgpu->device_id;
  921. if (pga == dev) {
  922. thr = &thr_info[i];
  923. cgpu->deven = DEV_ENABLED;
  924. tq_push(thr->q, &ping);
  925. }
  926. }
  927. strcpy(io_buffer, message(MSG_PGAENA, id, NULL, isjson));
  928. }
  929. static void pgadisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  930. {
  931. int numpga = numpgas();
  932. int id;
  933. if (numpga == 0) {
  934. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  935. return;
  936. }
  937. if (param == NULL || *param == '\0') {
  938. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  939. return;
  940. }
  941. id = atoi(param);
  942. if (id < 0 || id >= numpga) {
  943. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  944. return;
  945. }
  946. int dev = pgadevice(id);
  947. if (dev < 0) { // Should never happen
  948. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  949. return;
  950. }
  951. struct cgpu_info *cgpu = devices[dev];
  952. if (cgpu->deven == DEV_DISABLED) {
  953. strcpy(io_buffer, message(MSG_PGALRDIS, id, NULL, isjson));
  954. return;
  955. }
  956. cgpu->deven = DEV_DISABLED;
  957. strcpy(io_buffer, message(MSG_PGADIS, id, NULL, isjson));
  958. }
  959. #endif
  960. #ifdef WANT_CPUMINE
  961. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  962. {
  963. int id;
  964. if (opt_n_threads == 0) {
  965. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  966. return;
  967. }
  968. if (param == NULL || *param == '\0') {
  969. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  970. return;
  971. }
  972. id = atoi(param);
  973. if (id < 0 || id >= num_processors) {
  974. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  975. return;
  976. }
  977. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  978. if (isjson) {
  979. strcat(io_buffer, COMMA);
  980. strcat(io_buffer, JSON_CPU);
  981. }
  982. cpustatus(id, isjson);
  983. if (isjson)
  984. strcat(io_buffer, JSON_CLOSE);
  985. }
  986. #endif
  987. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  988. {
  989. char buf[BUFSIZ];
  990. char *status, *lp;
  991. char *rpc_url;
  992. char *rpc_user;
  993. int i;
  994. if (total_pools == 0) {
  995. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  996. return;
  997. }
  998. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  999. if (isjson) {
  1000. strcat(io_buffer, COMMA);
  1001. strcat(io_buffer, JSON_POOLS);
  1002. }
  1003. for (i = 0; i < total_pools; i++) {
  1004. struct pool *pool = pools[i];
  1005. if (!pool->enabled)
  1006. status = (char *)DISABLED;
  1007. else {
  1008. if (pool->idle)
  1009. status = (char *)DEAD;
  1010. else
  1011. status = (char *)ALIVE;
  1012. }
  1013. if (pool->hdr_path)
  1014. lp = (char *)YES;
  1015. else
  1016. lp = (char *)NO;
  1017. rpc_url = escape_string(pool->rpc_url, isjson);
  1018. rpc_user = escape_string(pool->rpc_user, isjson);
  1019. if (isjson)
  1020. 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,\"User\":\"%s\"}",
  1021. (i > 0) ? COMMA : "",
  1022. i, rpc_url, status, pool->prio, lp,
  1023. pool->getwork_requested,
  1024. pool->accepted, pool->rejected,
  1025. pool->discarded_work,
  1026. pool->stale_shares,
  1027. pool->getfail_occasions,
  1028. pool->remotefail_occasions,
  1029. rpc_user);
  1030. else
  1031. 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,User=%s%c",
  1032. i, rpc_url, status, pool->prio, lp,
  1033. pool->getwork_requested,
  1034. pool->accepted, pool->rejected,
  1035. pool->discarded_work,
  1036. pool->stale_shares,
  1037. pool->getfail_occasions,
  1038. pool->remotefail_occasions,
  1039. rpc_user, SEPARATOR);
  1040. strcat(io_buffer, buf);
  1041. if (rpc_url != pool->rpc_url)
  1042. free(rpc_url);
  1043. rpc_url = NULL;
  1044. if (rpc_user != pool->rpc_user)
  1045. free(rpc_user);
  1046. rpc_user = NULL;
  1047. }
  1048. if (isjson)
  1049. strcat(io_buffer, JSON_CLOSE);
  1050. }
  1051. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1052. {
  1053. double utility, mhs;
  1054. #ifdef WANT_CPUMINE
  1055. char *algo = (char *)(algo_names[opt_algo]);
  1056. if (algo == NULL)
  1057. algo = "(null)";
  1058. #endif
  1059. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1060. mhs = total_mhashes_done / total_secs;
  1061. #ifdef WANT_CPUMINE
  1062. if (isjson)
  1063. 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,\"Total MH\":%.4f}" JSON_CLOSE,
  1064. message(MSG_SUMM, 0, NULL, isjson),
  1065. total_secs, algo, mhs, found_blocks,
  1066. total_getworks, total_accepted, total_rejected,
  1067. hw_errors, utility, total_discarded, total_stale,
  1068. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1069. else
  1070. 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,Total MH=%.4f%c",
  1071. message(MSG_SUMM, 0, NULL, isjson),
  1072. total_secs, algo, mhs, found_blocks,
  1073. total_getworks, total_accepted, total_rejected,
  1074. hw_errors, utility, total_discarded, total_stale,
  1075. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  1076. #else
  1077. if (isjson)
  1078. 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,\"Total MH\":%.4f}" JSON_CLOSE,
  1079. message(MSG_SUMM, 0, NULL, isjson),
  1080. total_secs, mhs, found_blocks,
  1081. total_getworks, total_accepted, total_rejected,
  1082. hw_errors, utility, total_discarded, total_stale,
  1083. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1084. else
  1085. 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,Total MH=%.4f%c",
  1086. message(MSG_SUMM, 0, NULL, isjson),
  1087. total_secs, mhs, found_blocks,
  1088. total_getworks, total_accepted, total_rejected,
  1089. hw_errors, utility, total_discarded, total_stale,
  1090. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  1091. #endif
  1092. }
  1093. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1094. {
  1095. struct thr_info *thr;
  1096. int gpu;
  1097. int id;
  1098. int i;
  1099. if (gpu_threads == 0) {
  1100. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1101. return;
  1102. }
  1103. if (param == NULL || *param == '\0') {
  1104. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1105. return;
  1106. }
  1107. id = atoi(param);
  1108. if (id < 0 || id >= nDevs) {
  1109. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1110. return;
  1111. }
  1112. if (gpus[id].deven != DEV_DISABLED) {
  1113. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  1114. return;
  1115. }
  1116. for (i = 0; i < gpu_threads; i++) {
  1117. gpu = thr_info[i].cgpu->device_id;
  1118. if (gpu == id) {
  1119. thr = &thr_info[i];
  1120. if (thr->cgpu->status != LIFE_WELL) {
  1121. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  1122. return;
  1123. }
  1124. gpus[id].deven = DEV_ENABLED;
  1125. tq_push(thr->q, &ping);
  1126. }
  1127. }
  1128. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  1129. }
  1130. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1131. {
  1132. int id;
  1133. if (nDevs == 0) {
  1134. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1135. return;
  1136. }
  1137. if (param == NULL || *param == '\0') {
  1138. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1139. return;
  1140. }
  1141. id = atoi(param);
  1142. if (id < 0 || id >= nDevs) {
  1143. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1144. return;
  1145. }
  1146. if (gpus[id].deven == DEV_DISABLED) {
  1147. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  1148. return;
  1149. }
  1150. gpus[id].deven = DEV_DISABLED;
  1151. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  1152. }
  1153. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1154. {
  1155. int id;
  1156. if (nDevs == 0) {
  1157. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1158. return;
  1159. }
  1160. if (param == NULL || *param == '\0') {
  1161. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1162. return;
  1163. }
  1164. id = atoi(param);
  1165. if (id < 0 || id >= nDevs) {
  1166. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1167. return;
  1168. }
  1169. reinit_device(&gpus[id]);
  1170. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  1171. }
  1172. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1173. {
  1174. char buf[BUFSIZ];
  1175. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  1176. if (isjson)
  1177. sprintf(buf, "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE, nDevs);
  1178. else
  1179. sprintf(buf, _GPUS ",Count=%d%c", nDevs, SEPARATOR);
  1180. strcat(io_buffer, buf);
  1181. }
  1182. static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1183. {
  1184. char buf[BUFSIZ];
  1185. int count = 0;
  1186. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  1187. count = numpgas();
  1188. #endif
  1189. strcpy(io_buffer, message(MSG_NUMPGA, 0, NULL, isjson));
  1190. if (isjson)
  1191. sprintf(buf, "," JSON_PGAS "{\"Count\":%d}" JSON_CLOSE, count);
  1192. else
  1193. sprintf(buf, _PGAS ",Count=%d%c", count, SEPARATOR);
  1194. strcat(io_buffer, buf);
  1195. }
  1196. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1197. {
  1198. char buf[BUFSIZ];
  1199. int count = 0;
  1200. #ifdef WANT_CPUMINE
  1201. count = opt_n_threads > 0 ? num_processors : 0;
  1202. #endif
  1203. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  1204. if (isjson)
  1205. sprintf(buf, "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE, count);
  1206. else
  1207. sprintf(buf, _CPUS ",Count=%d%c", count, SEPARATOR);
  1208. strcat(io_buffer, buf);
  1209. }
  1210. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1211. {
  1212. struct pool *pool;
  1213. int id;
  1214. if (total_pools == 0) {
  1215. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1216. return;
  1217. }
  1218. if (param == NULL || *param == '\0') {
  1219. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1220. return;
  1221. }
  1222. id = atoi(param);
  1223. if (id < 0 || id >= total_pools) {
  1224. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1225. return;
  1226. }
  1227. pool = pools[id];
  1228. pool->enabled = true;
  1229. switch_pools(pool);
  1230. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  1231. }
  1232. static void copyadvanceafter(char ch, char **param, char **buf)
  1233. {
  1234. #define src_p (*param)
  1235. #define dst_b (*buf)
  1236. while (*src_p && *src_p != ch) {
  1237. if (*src_p == '\\' && *(src_p+1) != '\0')
  1238. src_p++;
  1239. *(dst_b++) = *(src_p++);
  1240. }
  1241. if (*src_p)
  1242. src_p++;
  1243. *(dst_b++) = '\0';
  1244. }
  1245. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1246. {
  1247. char *ptr, *buf;
  1248. ptr = buf = malloc(strlen(param)+1);
  1249. if (unlikely(!buf))
  1250. quit(1, "Failed to malloc pooldetails buf");
  1251. *url = buf;
  1252. // copy url
  1253. copyadvanceafter(',', &param, &buf);
  1254. if (!(*param)) // missing user
  1255. goto exitsama;
  1256. *user = buf;
  1257. // copy user
  1258. copyadvanceafter(',', &param, &buf);
  1259. if (!*param) // missing pass
  1260. goto exitsama;
  1261. *pass = buf;
  1262. // copy pass
  1263. copyadvanceafter(',', &param, &buf);
  1264. return true;
  1265. exitsama:
  1266. free(ptr);
  1267. return false;
  1268. }
  1269. static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1270. {
  1271. char *url, *user, *pass;
  1272. char *ptr;
  1273. if (param == NULL || *param == '\0') {
  1274. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  1275. return;
  1276. }
  1277. if (!pooldetails(param, &url, &user, &pass)) {
  1278. ptr = escape_string(param, isjson);
  1279. strcpy(io_buffer, message(MSG_INVPDP, 0, ptr, isjson));
  1280. if (ptr != param)
  1281. free(ptr);
  1282. ptr = NULL;
  1283. return;
  1284. }
  1285. if (add_pool_details(true, url, user, pass) == ADD_POOL_MAXIMUM) {
  1286. strcpy(io_buffer, message(MSG_TOOMANYP, MAX_POOLS, NULL, isjson));
  1287. return;
  1288. }
  1289. ptr = escape_string(url, isjson);
  1290. strcpy(io_buffer, message(MSG_ADDPOOL, 0, ptr, isjson));
  1291. if (ptr != url)
  1292. free(ptr);
  1293. ptr = NULL;
  1294. }
  1295. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1296. {
  1297. struct pool *pool;
  1298. int id;
  1299. if (total_pools == 0) {
  1300. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1301. return;
  1302. }
  1303. if (param == NULL || *param == '\0') {
  1304. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1305. return;
  1306. }
  1307. id = atoi(param);
  1308. if (id < 0 || id >= total_pools) {
  1309. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1310. return;
  1311. }
  1312. pool = pools[id];
  1313. if (pool->enabled == true) {
  1314. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  1315. return;
  1316. }
  1317. pool->enabled = true;
  1318. if (pool->prio < current_pool()->prio)
  1319. switch_pools(pool);
  1320. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  1321. }
  1322. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1323. {
  1324. struct pool *pool;
  1325. int id;
  1326. if (total_pools == 0) {
  1327. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1328. return;
  1329. }
  1330. if (param == NULL || *param == '\0') {
  1331. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1332. return;
  1333. }
  1334. id = atoi(param);
  1335. if (id < 0 || id >= total_pools) {
  1336. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1337. return;
  1338. }
  1339. pool = pools[id];
  1340. if (pool->enabled == false) {
  1341. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  1342. return;
  1343. }
  1344. if (active_pools() <= 1) {
  1345. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  1346. return;
  1347. }
  1348. pool->enabled = false;
  1349. if (pool == current_pool())
  1350. switch_pools(NULL);
  1351. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  1352. }
  1353. static void removepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1354. {
  1355. struct pool *pool;
  1356. char *rpc_url;
  1357. bool dofree = false;
  1358. int id;
  1359. if (total_pools == 0) {
  1360. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1361. return;
  1362. }
  1363. if (param == NULL || *param == '\0') {
  1364. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1365. return;
  1366. }
  1367. id = atoi(param);
  1368. if (id < 0 || id >= total_pools) {
  1369. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1370. return;
  1371. }
  1372. if (total_pools <= 1) {
  1373. strcpy(io_buffer, message(MSG_REMLASTP, id, NULL, isjson));
  1374. return;
  1375. }
  1376. pool = pools[id];
  1377. if (pool == current_pool())
  1378. switch_pools(NULL);
  1379. if (pool == current_pool()) {
  1380. strcpy(io_buffer, message(MSG_ACTPOOL, id, NULL, isjson));
  1381. return;
  1382. }
  1383. pool->enabled = false;
  1384. rpc_url = escape_string(pool->rpc_url, isjson);
  1385. if (rpc_url != pool->rpc_url)
  1386. dofree = true;
  1387. remove_pool(pool);
  1388. strcpy(io_buffer, message(MSG_REMPOOL, id, rpc_url, isjson));
  1389. if (dofree)
  1390. free(rpc_url);
  1391. rpc_url = NULL;
  1392. }
  1393. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  1394. {
  1395. int id;
  1396. char *gpusep;
  1397. if (nDevs == 0) {
  1398. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1399. return false;
  1400. }
  1401. if (param == NULL || *param == '\0') {
  1402. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1403. return false;
  1404. }
  1405. gpusep = strchr(param, GPUSEP);
  1406. if (gpusep == NULL) {
  1407. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  1408. return false;
  1409. }
  1410. *(gpusep++) = '\0';
  1411. id = atoi(param);
  1412. if (id < 0 || id >= nDevs) {
  1413. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1414. return false;
  1415. }
  1416. *gpu = id;
  1417. *value = gpusep;
  1418. return true;
  1419. }
  1420. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1421. {
  1422. int id;
  1423. char *value;
  1424. int intensity;
  1425. char intensitystr[7];
  1426. if (!splitgpuvalue(param, &id, &value, isjson))
  1427. return;
  1428. if (!strncasecmp(value, DYNAMIC, 1)) {
  1429. gpus[id].dynamic = true;
  1430. strcpy(intensitystr, DYNAMIC);
  1431. }
  1432. else {
  1433. intensity = atoi(value);
  1434. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1435. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1436. return;
  1437. }
  1438. gpus[id].dynamic = false;
  1439. gpus[id].intensity = intensity;
  1440. sprintf(intensitystr, "%d", intensity);
  1441. }
  1442. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1443. }
  1444. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1445. {
  1446. #ifdef HAVE_ADL
  1447. int id;
  1448. char *value;
  1449. int clock;
  1450. if (!splitgpuvalue(param, &id, &value, isjson))
  1451. return;
  1452. clock = atoi(value);
  1453. if (set_memoryclock(id, clock))
  1454. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  1455. else
  1456. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  1457. #else
  1458. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1459. #endif
  1460. }
  1461. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1462. {
  1463. #ifdef HAVE_ADL
  1464. int id;
  1465. char *value;
  1466. int clock;
  1467. if (!splitgpuvalue(param, &id, &value, isjson))
  1468. return;
  1469. clock = atoi(value);
  1470. if (set_engineclock(id, clock))
  1471. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  1472. else
  1473. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  1474. #else
  1475. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1476. #endif
  1477. }
  1478. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1479. {
  1480. #ifdef HAVE_ADL
  1481. int id;
  1482. char *value;
  1483. int fan;
  1484. if (!splitgpuvalue(param, &id, &value, isjson))
  1485. return;
  1486. fan = atoi(value);
  1487. if (set_fanspeed(id, fan))
  1488. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  1489. else
  1490. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  1491. #else
  1492. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1493. #endif
  1494. }
  1495. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1496. {
  1497. #ifdef HAVE_ADL
  1498. int id;
  1499. char *value;
  1500. float vddc;
  1501. if (!splitgpuvalue(param, &id, &value, isjson))
  1502. return;
  1503. vddc = atof(value);
  1504. if (set_vddc(id, vddc))
  1505. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  1506. else
  1507. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  1508. #else
  1509. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1510. #endif
  1511. }
  1512. static void send_result(SOCKETTYPE c, bool isjson);
  1513. void doquit(SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1514. {
  1515. if (isjson)
  1516. strcpy(io_buffer, JSON_START JSON_BYE);
  1517. else
  1518. strcpy(io_buffer, _BYE);
  1519. send_result(c, isjson);
  1520. *io_buffer = '\0';
  1521. bye = 1;
  1522. PTH(&thr_info[my_thr_id]) = 0L;
  1523. kill_work();
  1524. }
  1525. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1526. {
  1527. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  1528. }
  1529. void notifystatus(int device, struct cgpu_info *cgpu, bool isjson)
  1530. {
  1531. char buf[BUFSIZ];
  1532. char *reason;
  1533. if (cgpu->device_last_not_well == 0)
  1534. reason = REASON_NONE;
  1535. else
  1536. switch(cgpu->device_not_well_reason) {
  1537. case REASON_THREAD_FAIL_INIT:
  1538. reason = REASON_THREAD_FAIL_INIT_STR;
  1539. break;
  1540. case REASON_THREAD_ZERO_HASH:
  1541. reason = REASON_THREAD_ZERO_HASH_STR;
  1542. break;
  1543. case REASON_THREAD_FAIL_QUEUE:
  1544. reason = REASON_THREAD_FAIL_QUEUE_STR;
  1545. break;
  1546. case REASON_DEV_SICK_IDLE_60:
  1547. reason = REASON_DEV_SICK_IDLE_60_STR;
  1548. break;
  1549. case REASON_DEV_DEAD_IDLE_600:
  1550. reason = REASON_DEV_DEAD_IDLE_600_STR;
  1551. break;
  1552. case REASON_DEV_NOSTART:
  1553. reason = REASON_DEV_NOSTART_STR;
  1554. break;
  1555. case REASON_DEV_OVER_HEAT:
  1556. reason = REASON_DEV_OVER_HEAT_STR;
  1557. break;
  1558. case REASON_DEV_THERMAL_CUTOFF:
  1559. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  1560. break;
  1561. default:
  1562. reason = REASON_UNKNOWN_STR;
  1563. break;
  1564. }
  1565. // ALL counters (and only counters) must start the name with a '*'
  1566. // Simplifies future external support for adding new counters
  1567. if (isjson)
  1568. sprintf(buf, "%s{\"NOTIFY\":%d,\"Name\":\"%s\",\"ID\":%d,\"Last Well\":%lu,\"Last Not Well\":%lu,\"Reason Not Well\":\"%s\",\"*Thread Fail Init\":%d,\"*Thread Zero Hash\":%d,\"*Thread Fail Queue\":%d,\"*Dev Sick Idle 60s\":%d,\"*Dev Dead Idle 600s\":%d,\"*Dev Nostart\":%d,\"*Dev Over Heat\":%d,\"*Dev Thermal Cutoff\":%d}" JSON_CLOSE,
  1569. device > 0 ? "," : "", device, cgpu->api->name, cgpu->device_id,
  1570. cgpu->device_last_well, cgpu->device_last_not_well, reason,
  1571. cgpu->thread_fail_init_count, cgpu->thread_zero_hash_count,
  1572. cgpu->thread_fail_queue_count, cgpu->dev_sick_idle_60_count,
  1573. cgpu->dev_dead_idle_600_count, cgpu->dev_nostart_count,
  1574. cgpu->dev_over_heat_count, cgpu->dev_thermal_cutoff_count);
  1575. else
  1576. sprintf(buf, "NOTIFY=%d,Name=%s,ID=%d,Last Well=%lu,Last Not Well=%lu,Reason Not Well=%s,*Thread Fail Init=%d,*Thread Zero Hash=%d,*Thread Fail Queue=%d,*Dev Sick Idle 60s=%d,*Dev Dead Idle 600s=%d,*Dev Nostart=%d,*Dev Over Heat=%d,*Dev Thermal Cutoff=%d%c",
  1577. device, cgpu->api->name, cgpu->device_id,
  1578. cgpu->device_last_well, cgpu->device_last_not_well, reason,
  1579. cgpu->thread_fail_init_count, cgpu->thread_zero_hash_count,
  1580. cgpu->thread_fail_queue_count, cgpu->dev_sick_idle_60_count,
  1581. cgpu->dev_dead_idle_600_count, cgpu->dev_nostart_count,
  1582. cgpu->dev_over_heat_count, cgpu->dev_thermal_cutoff_count, SEPARATOR);
  1583. strcat(io_buffer, buf);
  1584. }
  1585. static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1586. {
  1587. int i;
  1588. if (total_devices == 0) {
  1589. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1590. return;
  1591. }
  1592. strcpy(io_buffer, message(MSG_NOTIFY, 0, NULL, isjson));
  1593. if (isjson) {
  1594. strcat(io_buffer, COMMA);
  1595. strcat(io_buffer, JSON_NOTIFY);
  1596. }
  1597. for (i = 0; i < total_devices; i++)
  1598. notifystatus(i, devices[i], isjson);
  1599. if (isjson)
  1600. strcat(io_buffer, JSON_CLOSE);
  1601. }
  1602. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1603. {
  1604. FILE *fcfg;
  1605. char *ptr;
  1606. if (param == NULL || *param == '\0') {
  1607. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  1608. return;
  1609. }
  1610. fcfg = fopen(param, "w");
  1611. if (!fcfg) {
  1612. ptr = escape_string(param, isjson);
  1613. strcpy(io_buffer, message(MSG_BADFN, 0, ptr, isjson));
  1614. if (ptr != param)
  1615. free(ptr);
  1616. ptr = NULL;
  1617. return;
  1618. }
  1619. write_config(fcfg);
  1620. fclose(fcfg);
  1621. ptr = escape_string(param, isjson);
  1622. strcpy(io_buffer, message(MSG_SAVED, 0, ptr, isjson));
  1623. if (ptr != param)
  1624. free(ptr);
  1625. ptr = NULL;
  1626. }
  1627. struct CMDS {
  1628. char *name;
  1629. void (*func)(SOCKETTYPE, char *, bool);
  1630. bool requires_writemode;
  1631. } cmds[] = {
  1632. { "version", apiversion, false },
  1633. { "config", minerconfig, false },
  1634. { "devs", devstatus, false },
  1635. { "pools", poolstatus, false },
  1636. { "summary", summary, false },
  1637. { "gpuenable", gpuenable, true },
  1638. { "gpudisable", gpudisable, true },
  1639. { "gpurestart", gpurestart, true },
  1640. { "gpu", gpudev, false },
  1641. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  1642. { "pga", pgadev, false },
  1643. { "pgaenable", pgaenable, true },
  1644. { "pgadisable", pgadisable, true },
  1645. #endif
  1646. #ifdef WANT_CPUMINE
  1647. { "cpu", cpudev, false },
  1648. #endif
  1649. { "gpucount", gpucount, false },
  1650. { "pgacount", pgacount, false },
  1651. { "cpucount", cpucount, false },
  1652. { "switchpool", switchpool, true },
  1653. { "addpool", addpool, true },
  1654. { "enablepool", enablepool, true },
  1655. { "disablepool", disablepool, true },
  1656. { "removepool", removepool, true },
  1657. { "gpuintensity", gpuintensity, true },
  1658. { "gpumem", gpumem, true },
  1659. { "gpuengine", gpuengine, true },
  1660. { "gpufan", gpufan, true },
  1661. { "gpuvddc", gpuvddc, true },
  1662. { "save", dosave, true },
  1663. { "quit", doquit, true },
  1664. { "privileged", privileged, true },
  1665. { "notify", notify, false },
  1666. { NULL, NULL, false }
  1667. };
  1668. static void send_result(SOCKETTYPE c, bool isjson)
  1669. {
  1670. int n;
  1671. int len;
  1672. if (isjson)
  1673. strcat(io_buffer, JSON_END);
  1674. len = strlen(io_buffer);
  1675. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : "");
  1676. // ignore failure - it's closed immediately anyway
  1677. n = send(c, io_buffer, len+1, 0);
  1678. if (opt_debug) {
  1679. if (SOCKETFAIL(n))
  1680. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  1681. else
  1682. applog(LOG_DEBUG, "API: sent %d", n);
  1683. }
  1684. }
  1685. static void tidyup(void *arg)
  1686. {
  1687. bye = 1;
  1688. if (sock != INVSOCK) {
  1689. shutdown(sock, SHUT_RDWR);
  1690. CLOSESOCKET(sock);
  1691. sock = INVSOCK;
  1692. }
  1693. if (ipaccess != NULL) {
  1694. free(ipaccess);
  1695. ipaccess = NULL;
  1696. }
  1697. if (msg_buffer != NULL) {
  1698. free(msg_buffer);
  1699. msg_buffer = NULL;
  1700. }
  1701. if (io_buffer != NULL) {
  1702. free(io_buffer);
  1703. io_buffer = NULL;
  1704. }
  1705. }
  1706. /*
  1707. * Interpret [R|W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  1708. * special case of 0/0 allows /0 (means all IP addresses)
  1709. */
  1710. #define ALLIP4 "0/0"
  1711. /*
  1712. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  1713. */
  1714. static void setup_ipaccess()
  1715. {
  1716. char *buf, *ptr, *comma, *slash, *dot;
  1717. int ipcount, mask, octet, i;
  1718. bool writemode;
  1719. buf = malloc(strlen(opt_api_allow) + 1);
  1720. if (unlikely(!buf))
  1721. quit(1, "Failed to malloc ipaccess buf");
  1722. strcpy(buf, opt_api_allow);
  1723. ipcount = 1;
  1724. ptr = buf;
  1725. while (*ptr)
  1726. if (*(ptr++) == ',')
  1727. ipcount++;
  1728. // possibly more than needed, but never less
  1729. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  1730. if (unlikely(!ipaccess))
  1731. quit(1, "Failed to calloc ipaccess");
  1732. ips = 0;
  1733. ptr = buf;
  1734. while (ptr && *ptr) {
  1735. while (*ptr == ' ' || *ptr == '\t')
  1736. ptr++;
  1737. if (*ptr == ',') {
  1738. ptr++;
  1739. continue;
  1740. }
  1741. comma = strchr(ptr, ',');
  1742. if (comma)
  1743. *(comma++) = '\0';
  1744. writemode = false;
  1745. if (isalpha(*ptr) && *(ptr+1) == ':') {
  1746. if (tolower(*ptr) == 'w')
  1747. writemode = true;
  1748. ptr += 2;
  1749. }
  1750. ipaccess[ips].writemode = writemode;
  1751. if (strcmp(ptr, ALLIP4) == 0)
  1752. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  1753. else {
  1754. slash = strchr(ptr, '/');
  1755. if (!slash)
  1756. ipaccess[ips].mask = 0xffffffff;
  1757. else {
  1758. *(slash++) = '\0';
  1759. mask = atoi(slash);
  1760. if (mask < 1 || mask > 32)
  1761. goto popipo; // skip invalid/zero
  1762. ipaccess[ips].mask = 0;
  1763. while (mask-- >= 0) {
  1764. octet = 1 << (mask % 8);
  1765. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  1766. }
  1767. }
  1768. ipaccess[ips].ip = 0; // missing default to '.0'
  1769. for (i = 0; ptr && (i < 4); i++) {
  1770. dot = strchr(ptr, '.');
  1771. if (dot)
  1772. *(dot++) = '\0';
  1773. octet = atoi(ptr);
  1774. if (octet < 0 || octet > 0xff)
  1775. goto popipo; // skip invalid
  1776. ipaccess[ips].ip |= (octet << (i * 8));
  1777. ptr = dot;
  1778. }
  1779. ipaccess[ips].ip &= ipaccess[ips].mask;
  1780. }
  1781. ips++;
  1782. popipo:
  1783. ptr = comma;
  1784. }
  1785. free(buf);
  1786. }
  1787. void api(int api_thr_id)
  1788. {
  1789. char buf[BUFSIZ];
  1790. char param_buf[BUFSIZ];
  1791. const char *localaddr = "127.0.0.1";
  1792. SOCKETTYPE c;
  1793. int n, bound;
  1794. char *connectaddr;
  1795. char *binderror;
  1796. time_t bindstart;
  1797. short int port = opt_api_port;
  1798. struct sockaddr_in serv;
  1799. struct sockaddr_in cli;
  1800. socklen_t clisiz;
  1801. char *cmd;
  1802. char *param;
  1803. bool addrok;
  1804. bool writemode;
  1805. json_error_t json_err;
  1806. json_t *json_config;
  1807. json_t *json_val;
  1808. bool isjson;
  1809. bool did;
  1810. int i;
  1811. pthread_cleanup_push(tidyup, NULL);
  1812. my_thr_id = api_thr_id;
  1813. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1814. sleep(opt_log_interval);
  1815. if (!opt_api_listen) {
  1816. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1817. return;
  1818. }
  1819. if (opt_api_allow) {
  1820. setup_ipaccess();
  1821. if (ips == 0) {
  1822. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  1823. return;
  1824. }
  1825. }
  1826. sock = socket(AF_INET, SOCK_STREAM, 0);
  1827. if (sock == INVSOCK) {
  1828. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1829. return;
  1830. }
  1831. memset(&serv, 0, sizeof(serv));
  1832. serv.sin_family = AF_INET;
  1833. if (!opt_api_allow && !opt_api_network) {
  1834. serv.sin_addr.s_addr = inet_addr(localaddr);
  1835. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  1836. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1837. return;
  1838. }
  1839. }
  1840. serv.sin_port = htons(port);
  1841. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1842. bound = 0;
  1843. bindstart = time(NULL);
  1844. while (bound == 0) {
  1845. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1846. binderror = SOCKERRMSG;
  1847. if ((time(NULL) - bindstart) > 61)
  1848. break;
  1849. else {
  1850. applog(LOG_WARNING, "API bind to port %d failed - trying again in 15sec", port);
  1851. sleep(15);
  1852. }
  1853. }
  1854. else
  1855. bound = 1;
  1856. }
  1857. if (bound == 0) {
  1858. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1859. return;
  1860. }
  1861. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1862. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1863. CLOSESOCKET(sock);
  1864. return;
  1865. }
  1866. if (opt_api_allow)
  1867. applog(LOG_WARNING, "API running in IP access mode");
  1868. else {
  1869. if (opt_api_network)
  1870. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1871. else
  1872. applog(LOG_WARNING, "API running in local access mode");
  1873. }
  1874. io_buffer = malloc(MYBUFSIZ+1);
  1875. msg_buffer = malloc(MYBUFSIZ+1);
  1876. while (bye == 0) {
  1877. clisiz = sizeof(cli);
  1878. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1879. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1880. goto die;
  1881. }
  1882. connectaddr = inet_ntoa(cli.sin_addr);
  1883. addrok = false;
  1884. writemode = false;
  1885. if (opt_api_allow) {
  1886. for (i = 0; i < ips; i++) {
  1887. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  1888. addrok = true;
  1889. writemode = ipaccess[i].writemode;
  1890. break;
  1891. }
  1892. }
  1893. } else {
  1894. if (opt_api_network)
  1895. addrok = true;
  1896. else
  1897. addrok = (strcmp(connectaddr, localaddr) == 0);
  1898. }
  1899. if (opt_debug)
  1900. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1901. if (addrok) {
  1902. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1903. if (SOCKETFAIL(n))
  1904. buf[0] = '\0';
  1905. else
  1906. buf[n] = '\0';
  1907. if (opt_debug) {
  1908. if (SOCKETFAIL(n))
  1909. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  1910. else
  1911. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  1912. }
  1913. if (!SOCKETFAIL(n)) {
  1914. // the time of the request in now
  1915. when = time(NULL);
  1916. did = false;
  1917. if (*buf != ISJSON) {
  1918. isjson = false;
  1919. param = strchr(buf, SEPARATOR);
  1920. if (param != NULL)
  1921. *(param++) = '\0';
  1922. cmd = buf;
  1923. }
  1924. else {
  1925. isjson = true;
  1926. param = NULL;
  1927. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  1928. json_config = json_loadb(buf, n, 0, &json_err);
  1929. #elif JANSSON_MAJOR_VERSION > 1
  1930. json_config = json_loads(buf, 0, &json_err);
  1931. #else
  1932. json_config = json_loads(buf, &json_err);
  1933. #endif
  1934. if (!json_is_object(json_config)) {
  1935. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1936. send_result(c, isjson);
  1937. did = true;
  1938. }
  1939. else {
  1940. json_val = json_object_get(json_config, JSON_COMMAND);
  1941. if (json_val == NULL) {
  1942. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1943. send_result(c, isjson);
  1944. did = true;
  1945. }
  1946. else {
  1947. if (!json_is_string(json_val)) {
  1948. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1949. send_result(c, isjson);
  1950. did = true;
  1951. }
  1952. else {
  1953. cmd = (char *)json_string_value(json_val);
  1954. json_val = json_object_get(json_config, JSON_PARAMETER);
  1955. if (json_is_string(json_val))
  1956. param = (char *)json_string_value(json_val);
  1957. else if (json_is_integer(json_val)) {
  1958. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1959. param = param_buf;
  1960. } else if (json_is_real(json_val)) {
  1961. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1962. param = param_buf;
  1963. }
  1964. }
  1965. }
  1966. }
  1967. }
  1968. if (!did)
  1969. for (i = 0; cmds[i].name != NULL; i++) {
  1970. if (strcmp(cmd, cmds[i].name) == 0) {
  1971. if (cmds[i].requires_writemode && !writemode) {
  1972. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  1973. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  1974. }
  1975. else
  1976. (cmds[i].func)(c, param, isjson);
  1977. send_result(c, isjson);
  1978. did = true;
  1979. break;
  1980. }
  1981. }
  1982. if (!did) {
  1983. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1984. send_result(c, isjson);
  1985. }
  1986. }
  1987. }
  1988. CLOSESOCKET(c);
  1989. }
  1990. die:
  1991. pthread_cleanup_pop(true);
  1992. }