api.c 95 KB

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