api.c 98 KB

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