api.c 83 KB

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