api.c 85 KB

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