api.c 91 KB

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