api.c 86 KB

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