api.c 83 KB

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