api.c 83 KB

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