api.c 100 KB

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