api.c 58 KB

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