api.c 70 KB

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