api.c 113 KB

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