api.c 93 KB

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