api.c 59 KB

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