api.c 94 KB

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