api.c 88 KB

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