api.c 88 KB

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