api.c 91 KB

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