api.c 96 KB

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