api.c 91 KB

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