api.c 100 KB

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