api.c 87 KB

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