api.c 58 KB

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