api.c 99 KB

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