api.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  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, "BFGMiner 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, "BFGMiner 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. static void
  708. append_kv(char *buf, json_t*info, bool isjson)
  709. {
  710. json_t *value;
  711. const char *key, *tmpl = isjson ? ",\"%s\":%s" : ",%s=%s";
  712. char *vdump;
  713. void *it;
  714. for (it = json_object_iter(info); it; it = json_object_iter_next(info, it)) {
  715. key = json_object_iter_key(it);
  716. value = json_object_iter_value(it);
  717. if (isjson || !json_is_string(value))
  718. vdump = json_dumps(value, JSON_COMPACT | JSON_ENCODE_ANY);
  719. else
  720. vdump = strdup(json_string_value(value));
  721. tailsprintf(buf, tmpl, key, vdump);
  722. free(vdump);
  723. }
  724. }
  725. static void
  726. devdetail_an(char *buf, struct cgpu_info *cgpu, bool isjson)
  727. {
  728. tailsprintf(buf, isjson
  729. ? "{\"%s\":%d,Driver=%s"
  730. : "%s=%d,Driver=%s",
  731. cgpu->api->name, cgpu->device_id,
  732. cgpu->api->dname
  733. );
  734. if (cgpu->kname)
  735. tailsprintf(buf, isjson ? ",\"Kernel\":\"%s\"" : ",Kernel=%s", cgpu->kname);
  736. if (cgpu->name)
  737. tailsprintf(buf, isjson ? ",\"Model\":\"%s\"" : ",Model=%s", cgpu->name);
  738. if (cgpu->device_path)
  739. tailsprintf(buf, isjson ? ",\"Device Path\":\"%s\"" : ",Device Path=%s", cgpu->device_path);
  740. if (cgpu->api->get_extra_device_detail) {
  741. json_t *info = cgpu->api->get_extra_device_detail(cgpu);
  742. append_kv(buf, info, isjson);
  743. json_decref(info);
  744. }
  745. tailsprintf(buf, "%c", isjson ? '}' : SEPARATOR);
  746. }
  747. static void devstatus_an(char *buf, struct cgpu_info *cgpu, bool isjson)
  748. {
  749. tailsprintf(buf, isjson
  750. ? "{\"%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"
  751. : "%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",
  752. cgpu->api->name, cgpu->device_id,
  753. bool2str(cgpu->deven != DEV_DISABLED),
  754. status2str(cgpu->status),
  755. cgpu->temp,
  756. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  757. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  758. cgpu->utility,
  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. );
  762. if (cgpu->api->get_extra_device_status) {
  763. json_t *info = cgpu->api->get_extra_device_status(cgpu);
  764. append_kv(buf, info, isjson);
  765. json_decref(info);
  766. }
  767. tailsprintf(buf, "%c", isjson ? '}' : SEPARATOR);
  768. }
  769. static void gpustatus(int gpu, bool isjson)
  770. {
  771. if (gpu < 0 || gpu >= nDevs)
  772. return;
  773. devstatus_an(io_buffer, &gpus[gpu], isjson);
  774. }
  775. static void pgastatus(int pga, bool isjson)
  776. {
  777. int dev = pgadevice(pga);
  778. if (dev < 0) // Should never happen
  779. return;
  780. devstatus_an(io_buffer, devices[dev], isjson);
  781. }
  782. static void cpustatus(int cpu, bool isjson)
  783. {
  784. if (opt_n_threads <= 0 || cpu < 0 || cpu >= num_processors)
  785. return;
  786. devstatus_an(io_buffer, &cpus[cpu], isjson);
  787. }
  788. static void
  789. devinfo_internal(void (*func)(char*, struct cgpu_info*, bool),
  790. __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  791. {
  792. int i;
  793. if (total_devices == 0) {
  794. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  795. return;
  796. }
  797. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  798. if (isjson) {
  799. strcat(io_buffer, COMMA);
  800. strcat(io_buffer, JSON_DEVS);
  801. }
  802. for (i = 0; i < total_devices; ++i) {
  803. if (isjson && i)
  804. strcat(io_buffer, COMMA);
  805. func(io_buffer, devices[i], isjson);
  806. }
  807. if (isjson)
  808. strcat(io_buffer, JSON_CLOSE);
  809. }
  810. static void
  811. devdetail(SOCKETTYPE c, char *param, bool isjson)
  812. {
  813. return devinfo_internal(devdetail_an, c, param, isjson);
  814. }
  815. static void
  816. devstatus(SOCKETTYPE c, char *param, bool isjson)
  817. {
  818. return devinfo_internal(devstatus_an, c, param, isjson);
  819. }
  820. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  821. {
  822. int id;
  823. if (nDevs == 0) {
  824. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  825. return;
  826. }
  827. if (param == NULL || *param == '\0') {
  828. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  829. return;
  830. }
  831. id = atoi(param);
  832. if (id < 0 || id >= nDevs) {
  833. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  834. return;
  835. }
  836. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  837. if (isjson) {
  838. strcat(io_buffer, COMMA);
  839. strcat(io_buffer, JSON_GPU);
  840. }
  841. gpustatus(id, isjson);
  842. if (isjson)
  843. strcat(io_buffer, JSON_CLOSE);
  844. }
  845. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  846. static void pgadev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  847. {
  848. int numpga = numpgas();
  849. int id;
  850. if (numpga == 0) {
  851. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  852. return;
  853. }
  854. if (param == NULL || *param == '\0') {
  855. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  856. return;
  857. }
  858. id = atoi(param);
  859. if (id < 0 || id >= numpga) {
  860. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  861. return;
  862. }
  863. strcpy(io_buffer, message(MSG_PGADEV, id, NULL, isjson));
  864. if (isjson) {
  865. strcat(io_buffer, COMMA);
  866. strcat(io_buffer, JSON_PGA);
  867. }
  868. pgastatus(id, isjson);
  869. if (isjson)
  870. strcat(io_buffer, JSON_CLOSE);
  871. }
  872. static void pgaenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  873. {
  874. int numpga = numpgas();
  875. struct thr_info *thr;
  876. int pga;
  877. int id;
  878. int i;
  879. if (numpga == 0) {
  880. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  881. return;
  882. }
  883. if (param == NULL || *param == '\0') {
  884. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  885. return;
  886. }
  887. id = atoi(param);
  888. if (id < 0 || id >= numpga) {
  889. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  890. return;
  891. }
  892. int dev = pgadevice(id);
  893. if (dev < 0) { // Should never happen
  894. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  895. return;
  896. }
  897. struct cgpu_info *cgpu = devices[dev];
  898. if (cgpu->deven != DEV_DISABLED) {
  899. strcpy(io_buffer, message(MSG_PGALRENA, id, NULL, isjson));
  900. return;
  901. }
  902. if (cgpu->status != LIFE_WELL) {
  903. strcpy(io_buffer, message(MSG_PGAUNW, id, NULL, isjson));
  904. return;
  905. }
  906. for (i = 0; i < mining_threads; i++) {
  907. pga = thr_info[i].cgpu->device_id;
  908. if (pga == dev) {
  909. thr = &thr_info[i];
  910. cgpu->deven = DEV_ENABLED;
  911. tq_push(thr->q, &ping);
  912. }
  913. }
  914. strcpy(io_buffer, message(MSG_PGAENA, id, NULL, isjson));
  915. }
  916. static void pgadisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  917. {
  918. int numpga = numpgas();
  919. int id;
  920. if (numpga == 0) {
  921. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  922. return;
  923. }
  924. if (param == NULL || *param == '\0') {
  925. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  926. return;
  927. }
  928. id = atoi(param);
  929. if (id < 0 || id >= numpga) {
  930. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  931. return;
  932. }
  933. int dev = pgadevice(id);
  934. if (dev < 0) { // Should never happen
  935. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  936. return;
  937. }
  938. struct cgpu_info *cgpu = devices[dev];
  939. if (cgpu->deven == DEV_DISABLED) {
  940. strcpy(io_buffer, message(MSG_PGALRDIS, id, NULL, isjson));
  941. return;
  942. }
  943. cgpu->deven = DEV_DISABLED;
  944. strcpy(io_buffer, message(MSG_PGADIS, id, NULL, isjson));
  945. }
  946. #endif
  947. #ifdef WANT_CPUMINE
  948. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  949. {
  950. int id;
  951. if (opt_n_threads == 0) {
  952. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  953. return;
  954. }
  955. if (param == NULL || *param == '\0') {
  956. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  957. return;
  958. }
  959. id = atoi(param);
  960. if (id < 0 || id >= num_processors) {
  961. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  962. return;
  963. }
  964. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  965. if (isjson) {
  966. strcat(io_buffer, COMMA);
  967. strcat(io_buffer, JSON_CPU);
  968. }
  969. cpustatus(id, isjson);
  970. if (isjson)
  971. strcat(io_buffer, JSON_CLOSE);
  972. }
  973. #endif
  974. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  975. {
  976. char buf[BUFSIZ];
  977. char *status, *lp;
  978. char *rpc_url;
  979. char *rpc_user;
  980. int i;
  981. if (total_pools == 0) {
  982. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  983. return;
  984. }
  985. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  986. if (isjson) {
  987. strcat(io_buffer, COMMA);
  988. strcat(io_buffer, JSON_POOLS);
  989. }
  990. for (i = 0; i < total_pools; i++) {
  991. struct pool *pool = pools[i];
  992. if (!pool->enabled)
  993. status = (char *)DISABLED;
  994. else {
  995. if (pool->idle)
  996. status = (char *)DEAD;
  997. else
  998. status = (char *)ALIVE;
  999. }
  1000. if (pool->hdr_path)
  1001. lp = (char *)YES;
  1002. else
  1003. lp = (char *)NO;
  1004. rpc_url = escape_string(pool->rpc_url, isjson);
  1005. rpc_user = escape_string(pool->rpc_user, isjson);
  1006. if (isjson)
  1007. 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\"}",
  1008. (i > 0) ? COMMA : "",
  1009. i, rpc_url, status, pool->prio, lp,
  1010. pool->getwork_requested,
  1011. pool->accepted, pool->rejected,
  1012. pool->discarded_work,
  1013. pool->stale_shares,
  1014. pool->getfail_occasions,
  1015. pool->remotefail_occasions,
  1016. rpc_user);
  1017. else
  1018. 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",
  1019. i, rpc_url, status, pool->prio, lp,
  1020. pool->getwork_requested,
  1021. pool->accepted, pool->rejected,
  1022. pool->discarded_work,
  1023. pool->stale_shares,
  1024. pool->getfail_occasions,
  1025. pool->remotefail_occasions,
  1026. rpc_user, SEPARATOR);
  1027. strcat(io_buffer, buf);
  1028. if (rpc_url != pool->rpc_url)
  1029. free(rpc_url);
  1030. rpc_url = NULL;
  1031. if (rpc_user != pool->rpc_user)
  1032. free(rpc_user);
  1033. rpc_user = NULL;
  1034. }
  1035. if (isjson)
  1036. strcat(io_buffer, JSON_CLOSE);
  1037. }
  1038. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1039. {
  1040. double utility, mhs;
  1041. #ifdef WANT_CPUMINE
  1042. char *algo = (char *)(algo_names[opt_algo]);
  1043. if (algo == NULL)
  1044. algo = "(null)";
  1045. #endif
  1046. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1047. mhs = total_mhashes_done / total_secs;
  1048. #ifdef WANT_CPUMINE
  1049. if (isjson)
  1050. 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,
  1051. message(MSG_SUMM, 0, NULL, isjson),
  1052. total_secs, algo, mhs, found_blocks,
  1053. total_getworks, total_accepted, total_rejected,
  1054. hw_errors, utility, total_discarded, total_stale,
  1055. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1056. else
  1057. 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",
  1058. message(MSG_SUMM, 0, NULL, isjson),
  1059. total_secs, algo, mhs, found_blocks,
  1060. total_getworks, total_accepted, total_rejected,
  1061. hw_errors, utility, total_discarded, total_stale,
  1062. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  1063. #else
  1064. if (isjson)
  1065. 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,
  1066. message(MSG_SUMM, 0, NULL, isjson),
  1067. total_secs, mhs, found_blocks,
  1068. total_getworks, total_accepted, total_rejected,
  1069. hw_errors, utility, total_discarded, total_stale,
  1070. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1071. else
  1072. 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",
  1073. message(MSG_SUMM, 0, NULL, isjson),
  1074. total_secs, mhs, found_blocks,
  1075. total_getworks, total_accepted, total_rejected,
  1076. hw_errors, utility, total_discarded, total_stale,
  1077. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  1078. #endif
  1079. }
  1080. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1081. {
  1082. struct thr_info *thr;
  1083. int gpu;
  1084. int id;
  1085. int i;
  1086. if (gpu_threads == 0) {
  1087. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1088. return;
  1089. }
  1090. if (param == NULL || *param == '\0') {
  1091. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1092. return;
  1093. }
  1094. id = atoi(param);
  1095. if (id < 0 || id >= nDevs) {
  1096. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1097. return;
  1098. }
  1099. if (gpus[id].deven != DEV_DISABLED) {
  1100. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  1101. return;
  1102. }
  1103. for (i = 0; i < gpu_threads; i++) {
  1104. gpu = thr_info[i].cgpu->device_id;
  1105. if (gpu == id) {
  1106. thr = &thr_info[i];
  1107. if (thr->cgpu->status != LIFE_WELL) {
  1108. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  1109. return;
  1110. }
  1111. gpus[id].deven = DEV_ENABLED;
  1112. tq_push(thr->q, &ping);
  1113. }
  1114. }
  1115. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  1116. }
  1117. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1118. {
  1119. int id;
  1120. if (nDevs == 0) {
  1121. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1122. return;
  1123. }
  1124. if (param == NULL || *param == '\0') {
  1125. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1126. return;
  1127. }
  1128. id = atoi(param);
  1129. if (id < 0 || id >= nDevs) {
  1130. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1131. return;
  1132. }
  1133. if (gpus[id].deven == DEV_DISABLED) {
  1134. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  1135. return;
  1136. }
  1137. gpus[id].deven = DEV_DISABLED;
  1138. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  1139. }
  1140. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1141. {
  1142. int id;
  1143. if (nDevs == 0) {
  1144. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1145. return;
  1146. }
  1147. if (param == NULL || *param == '\0') {
  1148. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1149. return;
  1150. }
  1151. id = atoi(param);
  1152. if (id < 0 || id >= nDevs) {
  1153. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1154. return;
  1155. }
  1156. reinit_device(&gpus[id]);
  1157. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  1158. }
  1159. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1160. {
  1161. char buf[BUFSIZ];
  1162. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  1163. if (isjson)
  1164. sprintf(buf, "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE, nDevs);
  1165. else
  1166. sprintf(buf, _GPUS ",Count=%d%c", nDevs, SEPARATOR);
  1167. strcat(io_buffer, buf);
  1168. }
  1169. static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1170. {
  1171. char buf[BUFSIZ];
  1172. int count = 0;
  1173. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  1174. count = numpgas();
  1175. #endif
  1176. strcpy(io_buffer, message(MSG_NUMPGA, 0, NULL, isjson));
  1177. if (isjson)
  1178. sprintf(buf, "," JSON_PGAS "{\"Count\":%d}" JSON_CLOSE, count);
  1179. else
  1180. sprintf(buf, _PGAS ",Count=%d%c", count, SEPARATOR);
  1181. strcat(io_buffer, buf);
  1182. }
  1183. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1184. {
  1185. char buf[BUFSIZ];
  1186. int count = 0;
  1187. #ifdef WANT_CPUMINE
  1188. count = opt_n_threads > 0 ? num_processors : 0;
  1189. #endif
  1190. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  1191. if (isjson)
  1192. sprintf(buf, "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE, count);
  1193. else
  1194. sprintf(buf, _CPUS ",Count=%d%c", count, SEPARATOR);
  1195. strcat(io_buffer, buf);
  1196. }
  1197. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1198. {
  1199. struct pool *pool;
  1200. int id;
  1201. if (total_pools == 0) {
  1202. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1203. return;
  1204. }
  1205. if (param == NULL || *param == '\0') {
  1206. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1207. return;
  1208. }
  1209. id = atoi(param);
  1210. if (id < 0 || id >= total_pools) {
  1211. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1212. return;
  1213. }
  1214. pool = pools[id];
  1215. pool->enabled = true;
  1216. switch_pools(pool);
  1217. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  1218. }
  1219. static void copyadvanceafter(char ch, char **param, char **buf)
  1220. {
  1221. #define src_p (*param)
  1222. #define dst_b (*buf)
  1223. while (*src_p && *src_p != ch) {
  1224. if (*src_p == '\\' && *(src_p+1) != '\0')
  1225. src_p++;
  1226. *(dst_b++) = *(src_p++);
  1227. }
  1228. if (*src_p)
  1229. src_p++;
  1230. *(dst_b++) = '\0';
  1231. }
  1232. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1233. {
  1234. char *ptr, *buf;
  1235. ptr = buf = malloc(strlen(param)+1);
  1236. if (unlikely(!buf))
  1237. quit(1, "Failed to malloc pooldetails buf");
  1238. *url = buf;
  1239. // copy url
  1240. copyadvanceafter(',', &param, &buf);
  1241. if (!(*param)) // missing user
  1242. goto exitsama;
  1243. *user = buf;
  1244. // copy user
  1245. copyadvanceafter(',', &param, &buf);
  1246. if (!*param) // missing pass
  1247. goto exitsama;
  1248. *pass = buf;
  1249. // copy pass
  1250. copyadvanceafter(',', &param, &buf);
  1251. return true;
  1252. exitsama:
  1253. free(ptr);
  1254. return false;
  1255. }
  1256. static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1257. {
  1258. char *url, *user, *pass;
  1259. char *ptr;
  1260. if (param == NULL || *param == '\0') {
  1261. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  1262. return;
  1263. }
  1264. if (!pooldetails(param, &url, &user, &pass)) {
  1265. ptr = escape_string(param, isjson);
  1266. strcpy(io_buffer, message(MSG_INVPDP, 0, ptr, isjson));
  1267. if (ptr != param)
  1268. free(ptr);
  1269. ptr = NULL;
  1270. return;
  1271. }
  1272. if (add_pool_details(true, url, user, pass) == ADD_POOL_MAXIMUM) {
  1273. strcpy(io_buffer, message(MSG_TOOMANYP, MAX_POOLS, NULL, isjson));
  1274. return;
  1275. }
  1276. ptr = escape_string(url, isjson);
  1277. strcpy(io_buffer, message(MSG_ADDPOOL, 0, ptr, isjson));
  1278. if (ptr != url)
  1279. free(ptr);
  1280. ptr = NULL;
  1281. }
  1282. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1283. {
  1284. struct pool *pool;
  1285. int id;
  1286. if (total_pools == 0) {
  1287. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1288. return;
  1289. }
  1290. if (param == NULL || *param == '\0') {
  1291. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1292. return;
  1293. }
  1294. id = atoi(param);
  1295. if (id < 0 || id >= total_pools) {
  1296. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1297. return;
  1298. }
  1299. pool = pools[id];
  1300. if (pool->enabled == true) {
  1301. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  1302. return;
  1303. }
  1304. pool->enabled = true;
  1305. if (pool->prio < current_pool()->prio)
  1306. switch_pools(pool);
  1307. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  1308. }
  1309. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1310. {
  1311. struct pool *pool;
  1312. int id;
  1313. if (total_pools == 0) {
  1314. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1315. return;
  1316. }
  1317. if (param == NULL || *param == '\0') {
  1318. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1319. return;
  1320. }
  1321. id = atoi(param);
  1322. if (id < 0 || id >= total_pools) {
  1323. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1324. return;
  1325. }
  1326. pool = pools[id];
  1327. if (pool->enabled == false) {
  1328. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  1329. return;
  1330. }
  1331. if (active_pools() <= 1) {
  1332. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  1333. return;
  1334. }
  1335. pool->enabled = false;
  1336. if (pool == current_pool())
  1337. switch_pools(NULL);
  1338. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  1339. }
  1340. static void removepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1341. {
  1342. struct pool *pool;
  1343. char *rpc_url;
  1344. bool dofree = false;
  1345. int id;
  1346. if (total_pools == 0) {
  1347. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1348. return;
  1349. }
  1350. if (param == NULL || *param == '\0') {
  1351. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1352. return;
  1353. }
  1354. id = atoi(param);
  1355. if (id < 0 || id >= total_pools) {
  1356. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1357. return;
  1358. }
  1359. if (total_pools <= 1) {
  1360. strcpy(io_buffer, message(MSG_REMLASTP, id, NULL, isjson));
  1361. return;
  1362. }
  1363. pool = pools[id];
  1364. if (pool == current_pool())
  1365. switch_pools(NULL);
  1366. if (pool == current_pool()) {
  1367. strcpy(io_buffer, message(MSG_ACTPOOL, id, NULL, isjson));
  1368. return;
  1369. }
  1370. pool->enabled = false;
  1371. rpc_url = escape_string(pool->rpc_url, isjson);
  1372. if (rpc_url != pool->rpc_url)
  1373. dofree = true;
  1374. remove_pool(pool);
  1375. strcpy(io_buffer, message(MSG_REMPOOL, id, rpc_url, isjson));
  1376. if (dofree)
  1377. free(rpc_url);
  1378. rpc_url = NULL;
  1379. }
  1380. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  1381. {
  1382. int id;
  1383. char *gpusep;
  1384. if (nDevs == 0) {
  1385. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1386. return false;
  1387. }
  1388. if (param == NULL || *param == '\0') {
  1389. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1390. return false;
  1391. }
  1392. gpusep = strchr(param, GPUSEP);
  1393. if (gpusep == NULL) {
  1394. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  1395. return false;
  1396. }
  1397. *(gpusep++) = '\0';
  1398. id = atoi(param);
  1399. if (id < 0 || id >= nDevs) {
  1400. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1401. return false;
  1402. }
  1403. *gpu = id;
  1404. *value = gpusep;
  1405. return true;
  1406. }
  1407. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1408. {
  1409. int id;
  1410. char *value;
  1411. int intensity;
  1412. char intensitystr[7];
  1413. if (!splitgpuvalue(param, &id, &value, isjson))
  1414. return;
  1415. if (!strncasecmp(value, DYNAMIC, 1)) {
  1416. gpus[id].dynamic = true;
  1417. strcpy(intensitystr, DYNAMIC);
  1418. }
  1419. else {
  1420. intensity = atoi(value);
  1421. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1422. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1423. return;
  1424. }
  1425. gpus[id].dynamic = false;
  1426. gpus[id].intensity = intensity;
  1427. sprintf(intensitystr, "%d", intensity);
  1428. }
  1429. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1430. }
  1431. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1432. {
  1433. #ifdef HAVE_ADL
  1434. int id;
  1435. char *value;
  1436. int clock;
  1437. if (!splitgpuvalue(param, &id, &value, isjson))
  1438. return;
  1439. clock = atoi(value);
  1440. if (set_memoryclock(id, clock))
  1441. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  1442. else
  1443. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  1444. #else
  1445. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1446. #endif
  1447. }
  1448. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1449. {
  1450. #ifdef HAVE_ADL
  1451. int id;
  1452. char *value;
  1453. int clock;
  1454. if (!splitgpuvalue(param, &id, &value, isjson))
  1455. return;
  1456. clock = atoi(value);
  1457. if (set_engineclock(id, clock))
  1458. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  1459. else
  1460. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  1461. #else
  1462. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1463. #endif
  1464. }
  1465. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1466. {
  1467. #ifdef HAVE_ADL
  1468. int id;
  1469. char *value;
  1470. int fan;
  1471. if (!splitgpuvalue(param, &id, &value, isjson))
  1472. return;
  1473. fan = atoi(value);
  1474. if (set_fanspeed(id, fan))
  1475. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  1476. else
  1477. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  1478. #else
  1479. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1480. #endif
  1481. }
  1482. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1483. {
  1484. #ifdef HAVE_ADL
  1485. int id;
  1486. char *value;
  1487. float vddc;
  1488. if (!splitgpuvalue(param, &id, &value, isjson))
  1489. return;
  1490. vddc = atof(value);
  1491. if (set_vddc(id, vddc))
  1492. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  1493. else
  1494. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  1495. #else
  1496. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1497. #endif
  1498. }
  1499. static void send_result(SOCKETTYPE c, bool isjson);
  1500. void doquit(SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1501. {
  1502. if (isjson)
  1503. strcpy(io_buffer, JSON_START JSON_BYE);
  1504. else
  1505. strcpy(io_buffer, _BYE);
  1506. send_result(c, isjson);
  1507. *io_buffer = '\0';
  1508. bye = 1;
  1509. PTH(&thr_info[my_thr_id]) = 0L;
  1510. kill_work();
  1511. }
  1512. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1513. {
  1514. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  1515. }
  1516. void notifystatus(int device, struct cgpu_info *cgpu, bool isjson)
  1517. {
  1518. char buf[BUFSIZ];
  1519. char *reason;
  1520. if (cgpu->device_last_not_well == 0)
  1521. reason = REASON_NONE;
  1522. else
  1523. switch(cgpu->device_not_well_reason) {
  1524. case REASON_THREAD_FAIL_INIT:
  1525. reason = REASON_THREAD_FAIL_INIT_STR;
  1526. break;
  1527. case REASON_THREAD_ZERO_HASH:
  1528. reason = REASON_THREAD_ZERO_HASH_STR;
  1529. break;
  1530. case REASON_THREAD_FAIL_QUEUE:
  1531. reason = REASON_THREAD_FAIL_QUEUE_STR;
  1532. break;
  1533. case REASON_DEV_SICK_IDLE_60:
  1534. reason = REASON_DEV_SICK_IDLE_60_STR;
  1535. break;
  1536. case REASON_DEV_DEAD_IDLE_600:
  1537. reason = REASON_DEV_DEAD_IDLE_600_STR;
  1538. break;
  1539. case REASON_DEV_NOSTART:
  1540. reason = REASON_DEV_NOSTART_STR;
  1541. break;
  1542. case REASON_DEV_OVER_HEAT:
  1543. reason = REASON_DEV_OVER_HEAT_STR;
  1544. break;
  1545. case REASON_DEV_THERMAL_CUTOFF:
  1546. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  1547. break;
  1548. default:
  1549. reason = REASON_UNKNOWN_STR;
  1550. break;
  1551. }
  1552. // ALL counters (and only counters) must start the name with a '*'
  1553. // Simplifies future external support for adding new counters
  1554. if (isjson)
  1555. 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,
  1556. device > 0 ? "," : "", device, cgpu->api->name, cgpu->device_id,
  1557. cgpu->device_last_well, cgpu->device_last_not_well, reason,
  1558. cgpu->thread_fail_init_count, cgpu->thread_zero_hash_count,
  1559. cgpu->thread_fail_queue_count, cgpu->dev_sick_idle_60_count,
  1560. cgpu->dev_dead_idle_600_count, cgpu->dev_nostart_count,
  1561. cgpu->dev_over_heat_count, cgpu->dev_thermal_cutoff_count);
  1562. else
  1563. 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",
  1564. device, cgpu->api->name, cgpu->device_id,
  1565. cgpu->device_last_well, cgpu->device_last_not_well, reason,
  1566. cgpu->thread_fail_init_count, cgpu->thread_zero_hash_count,
  1567. cgpu->thread_fail_queue_count, cgpu->dev_sick_idle_60_count,
  1568. cgpu->dev_dead_idle_600_count, cgpu->dev_nostart_count,
  1569. cgpu->dev_over_heat_count, cgpu->dev_thermal_cutoff_count, SEPARATOR);
  1570. strcat(io_buffer, buf);
  1571. }
  1572. static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1573. {
  1574. int i;
  1575. if (total_devices == 0) {
  1576. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1577. return;
  1578. }
  1579. strcpy(io_buffer, message(MSG_NOTIFY, 0, NULL, isjson));
  1580. if (isjson) {
  1581. strcat(io_buffer, COMMA);
  1582. strcat(io_buffer, JSON_NOTIFY);
  1583. }
  1584. for (i = 0; i < total_devices; i++)
  1585. notifystatus(i, devices[i], isjson);
  1586. if (isjson)
  1587. strcat(io_buffer, JSON_CLOSE);
  1588. }
  1589. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1590. {
  1591. FILE *fcfg;
  1592. char *ptr;
  1593. if (param == NULL || *param == '\0') {
  1594. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  1595. return;
  1596. }
  1597. fcfg = fopen(param, "w");
  1598. if (!fcfg) {
  1599. ptr = escape_string(param, isjson);
  1600. strcpy(io_buffer, message(MSG_BADFN, 0, ptr, isjson));
  1601. if (ptr != param)
  1602. free(ptr);
  1603. ptr = NULL;
  1604. return;
  1605. }
  1606. write_config(fcfg);
  1607. fclose(fcfg);
  1608. ptr = escape_string(param, isjson);
  1609. strcpy(io_buffer, message(MSG_SAVED, 0, ptr, isjson));
  1610. if (ptr != param)
  1611. free(ptr);
  1612. ptr = NULL;
  1613. }
  1614. struct CMDS {
  1615. char *name;
  1616. void (*func)(SOCKETTYPE, char *, bool);
  1617. bool requires_writemode;
  1618. } cmds[] = {
  1619. { "version", apiversion, false },
  1620. { "config", minerconfig, false },
  1621. { "devs", devstatus, false },
  1622. { "devdetail", devdetail, false },
  1623. { "pools", poolstatus, false },
  1624. { "summary", summary, false },
  1625. { "gpuenable", gpuenable, true },
  1626. { "gpudisable", gpudisable, true },
  1627. { "gpurestart", gpurestart, true },
  1628. { "gpu", gpudev, false },
  1629. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  1630. { "pga", pgadev, false },
  1631. { "pgaenable", pgaenable, true },
  1632. { "pgadisable", pgadisable, true },
  1633. #endif
  1634. #ifdef WANT_CPUMINE
  1635. { "cpu", cpudev, false },
  1636. #endif
  1637. { "gpucount", gpucount, false },
  1638. { "pgacount", pgacount, false },
  1639. { "cpucount", cpucount, false },
  1640. { "switchpool", switchpool, true },
  1641. { "addpool", addpool, true },
  1642. { "enablepool", enablepool, true },
  1643. { "disablepool", disablepool, true },
  1644. { "removepool", removepool, true },
  1645. { "gpuintensity", gpuintensity, true },
  1646. { "gpumem", gpumem, true },
  1647. { "gpuengine", gpuengine, true },
  1648. { "gpufan", gpufan, true },
  1649. { "gpuvddc", gpuvddc, true },
  1650. { "save", dosave, true },
  1651. { "quit", doquit, true },
  1652. { "privileged", privileged, true },
  1653. { "notify", notify, false },
  1654. { NULL, NULL, false }
  1655. };
  1656. static void send_result(SOCKETTYPE c, bool isjson)
  1657. {
  1658. int n;
  1659. int len;
  1660. if (isjson)
  1661. strcat(io_buffer, JSON_END);
  1662. len = strlen(io_buffer);
  1663. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : "");
  1664. // ignore failure - it's closed immediately anyway
  1665. n = send(c, io_buffer, len+1, 0);
  1666. if (opt_debug) {
  1667. if (SOCKETFAIL(n))
  1668. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  1669. else
  1670. applog(LOG_DEBUG, "API: sent %d", n);
  1671. }
  1672. }
  1673. static void tidyup(__maybe_unused void *arg)
  1674. {
  1675. bye = 1;
  1676. if (sock != INVSOCK) {
  1677. shutdown(sock, SHUT_RDWR);
  1678. CLOSESOCKET(sock);
  1679. sock = INVSOCK;
  1680. }
  1681. if (ipaccess != NULL) {
  1682. free(ipaccess);
  1683. ipaccess = NULL;
  1684. }
  1685. if (msg_buffer != NULL) {
  1686. free(msg_buffer);
  1687. msg_buffer = NULL;
  1688. }
  1689. if (io_buffer != NULL) {
  1690. free(io_buffer);
  1691. io_buffer = NULL;
  1692. }
  1693. }
  1694. /*
  1695. * Interpret [R|W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  1696. * special case of 0/0 allows /0 (means all IP addresses)
  1697. */
  1698. #define ALLIP4 "0/0"
  1699. /*
  1700. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  1701. */
  1702. static void setup_ipaccess()
  1703. {
  1704. char *buf, *ptr, *comma, *slash, *dot;
  1705. int ipcount, mask, octet, i;
  1706. bool writemode;
  1707. buf = malloc(strlen(opt_api_allow) + 1);
  1708. if (unlikely(!buf))
  1709. quit(1, "Failed to malloc ipaccess buf");
  1710. strcpy(buf, opt_api_allow);
  1711. ipcount = 1;
  1712. ptr = buf;
  1713. while (*ptr)
  1714. if (*(ptr++) == ',')
  1715. ipcount++;
  1716. // possibly more than needed, but never less
  1717. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  1718. if (unlikely(!ipaccess))
  1719. quit(1, "Failed to calloc ipaccess");
  1720. ips = 0;
  1721. ptr = buf;
  1722. while (ptr && *ptr) {
  1723. while (*ptr == ' ' || *ptr == '\t')
  1724. ptr++;
  1725. if (*ptr == ',') {
  1726. ptr++;
  1727. continue;
  1728. }
  1729. comma = strchr(ptr, ',');
  1730. if (comma)
  1731. *(comma++) = '\0';
  1732. writemode = false;
  1733. if (isalpha(*ptr) && *(ptr+1) == ':') {
  1734. if (tolower(*ptr) == 'w')
  1735. writemode = true;
  1736. ptr += 2;
  1737. }
  1738. ipaccess[ips].writemode = writemode;
  1739. if (strcmp(ptr, ALLIP4) == 0)
  1740. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  1741. else {
  1742. slash = strchr(ptr, '/');
  1743. if (!slash)
  1744. ipaccess[ips].mask = 0xffffffff;
  1745. else {
  1746. *(slash++) = '\0';
  1747. mask = atoi(slash);
  1748. if (mask < 1 || mask > 32)
  1749. goto popipo; // skip invalid/zero
  1750. ipaccess[ips].mask = 0;
  1751. while (mask-- >= 0) {
  1752. octet = 1 << (mask % 8);
  1753. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  1754. }
  1755. }
  1756. ipaccess[ips].ip = 0; // missing default to '.0'
  1757. for (i = 0; ptr && (i < 4); i++) {
  1758. dot = strchr(ptr, '.');
  1759. if (dot)
  1760. *(dot++) = '\0';
  1761. octet = atoi(ptr);
  1762. if (octet < 0 || octet > 0xff)
  1763. goto popipo; // skip invalid
  1764. ipaccess[ips].ip |= (octet << (i * 8));
  1765. ptr = dot;
  1766. }
  1767. ipaccess[ips].ip &= ipaccess[ips].mask;
  1768. }
  1769. ips++;
  1770. popipo:
  1771. ptr = comma;
  1772. }
  1773. free(buf);
  1774. }
  1775. void api(int api_thr_id)
  1776. {
  1777. char buf[BUFSIZ];
  1778. char param_buf[BUFSIZ];
  1779. const char *localaddr = "127.0.0.1";
  1780. SOCKETTYPE c;
  1781. int n, bound;
  1782. char *connectaddr;
  1783. char *binderror;
  1784. time_t bindstart;
  1785. short int port = opt_api_port;
  1786. struct sockaddr_in serv;
  1787. struct sockaddr_in cli;
  1788. socklen_t clisiz;
  1789. char *cmd;
  1790. char *param;
  1791. bool addrok;
  1792. bool writemode;
  1793. json_error_t json_err;
  1794. json_t *json_config;
  1795. json_t *json_val;
  1796. bool isjson;
  1797. bool did;
  1798. int i;
  1799. pthread_cleanup_push(tidyup, NULL);
  1800. my_thr_id = api_thr_id;
  1801. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1802. sleep(opt_log_interval);
  1803. if (!opt_api_listen) {
  1804. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1805. return;
  1806. }
  1807. if (opt_api_allow) {
  1808. setup_ipaccess();
  1809. if (ips == 0) {
  1810. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  1811. return;
  1812. }
  1813. }
  1814. sock = socket(AF_INET, SOCK_STREAM, 0);
  1815. if (sock == INVSOCK) {
  1816. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1817. return;
  1818. }
  1819. memset(&serv, 0, sizeof(serv));
  1820. serv.sin_family = AF_INET;
  1821. if (!opt_api_allow && !opt_api_network) {
  1822. serv.sin_addr.s_addr = inet_addr(localaddr);
  1823. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  1824. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1825. return;
  1826. }
  1827. }
  1828. serv.sin_port = htons(port);
  1829. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1830. bound = 0;
  1831. bindstart = time(NULL);
  1832. while (bound == 0) {
  1833. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1834. binderror = SOCKERRMSG;
  1835. if ((time(NULL) - bindstart) > 61)
  1836. break;
  1837. else {
  1838. applog(LOG_WARNING, "API bind to port %d failed - trying again in 15sec", port);
  1839. sleep(15);
  1840. }
  1841. }
  1842. else
  1843. bound = 1;
  1844. }
  1845. if (bound == 0) {
  1846. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1847. return;
  1848. }
  1849. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1850. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1851. CLOSESOCKET(sock);
  1852. return;
  1853. }
  1854. if (opt_api_allow)
  1855. applog(LOG_WARNING, "API running in IP access mode");
  1856. else {
  1857. if (opt_api_network)
  1858. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1859. else
  1860. applog(LOG_WARNING, "API running in local access mode");
  1861. }
  1862. io_buffer = malloc(MYBUFSIZ+1);
  1863. msg_buffer = malloc(MYBUFSIZ+1);
  1864. while (bye == 0) {
  1865. clisiz = sizeof(cli);
  1866. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1867. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1868. goto die;
  1869. }
  1870. connectaddr = inet_ntoa(cli.sin_addr);
  1871. addrok = false;
  1872. writemode = false;
  1873. if (opt_api_allow) {
  1874. for (i = 0; i < ips; i++) {
  1875. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  1876. addrok = true;
  1877. writemode = ipaccess[i].writemode;
  1878. break;
  1879. }
  1880. }
  1881. } else {
  1882. if (opt_api_network)
  1883. addrok = true;
  1884. else
  1885. addrok = (strcmp(connectaddr, localaddr) == 0);
  1886. }
  1887. if (opt_debug)
  1888. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1889. if (addrok) {
  1890. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1891. if (SOCKETFAIL(n))
  1892. buf[0] = '\0';
  1893. else
  1894. buf[n] = '\0';
  1895. if (opt_debug) {
  1896. if (SOCKETFAIL(n))
  1897. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  1898. else
  1899. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  1900. }
  1901. if (!SOCKETFAIL(n)) {
  1902. // the time of the request in now
  1903. when = time(NULL);
  1904. did = false;
  1905. if (*buf != ISJSON) {
  1906. isjson = false;
  1907. param = strchr(buf, SEPARATOR);
  1908. if (param != NULL)
  1909. *(param++) = '\0';
  1910. cmd = buf;
  1911. }
  1912. else {
  1913. isjson = true;
  1914. param = NULL;
  1915. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  1916. json_config = json_loadb(buf, n, 0, &json_err);
  1917. #elif JANSSON_MAJOR_VERSION > 1
  1918. json_config = json_loads(buf, 0, &json_err);
  1919. #else
  1920. json_config = json_loads(buf, &json_err);
  1921. #endif
  1922. if (!json_is_object(json_config)) {
  1923. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1924. send_result(c, isjson);
  1925. did = true;
  1926. }
  1927. else {
  1928. json_val = json_object_get(json_config, JSON_COMMAND);
  1929. if (json_val == NULL) {
  1930. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1931. send_result(c, isjson);
  1932. did = true;
  1933. }
  1934. else {
  1935. if (!json_is_string(json_val)) {
  1936. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1937. send_result(c, isjson);
  1938. did = true;
  1939. }
  1940. else {
  1941. cmd = (char *)json_string_value(json_val);
  1942. json_val = json_object_get(json_config, JSON_PARAMETER);
  1943. if (json_is_string(json_val))
  1944. param = (char *)json_string_value(json_val);
  1945. else if (json_is_integer(json_val)) {
  1946. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1947. param = param_buf;
  1948. } else if (json_is_real(json_val)) {
  1949. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1950. param = param_buf;
  1951. }
  1952. }
  1953. }
  1954. }
  1955. }
  1956. if (!did)
  1957. for (i = 0; cmds[i].name != NULL; i++) {
  1958. if (strcmp(cmd, cmds[i].name) == 0) {
  1959. if (cmds[i].requires_writemode && !writemode) {
  1960. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  1961. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  1962. }
  1963. else
  1964. (cmds[i].func)(c, param, isjson);
  1965. send_result(c, isjson);
  1966. did = true;
  1967. break;
  1968. }
  1969. }
  1970. if (!did) {
  1971. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1972. send_result(c, isjson);
  1973. }
  1974. }
  1975. }
  1976. CLOSESOCKET(c);
  1977. }
  1978. die:
  1979. pthread_cleanup_pop(true);
  1980. }