api.c 83 KB

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