api.c 124 KB

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