api.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758
  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)
  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. #ifdef HAVE_AN_ASIC
  1068. static int numascs(void)
  1069. {
  1070. int count = 0;
  1071. int i;
  1072. rd_lock(&devices_lock);
  1073. for (i = 0; i < total_devices; i++) {
  1074. #define DRIVER_ADD_COMMAND(X) \
  1075. if (devices[i]->drv->drv_id == DRIVER_##X) \
  1076. count++;
  1077. ASIC_PARSE_COMMANDS
  1078. #undef DRIVER_ADD_COMMAND
  1079. }
  1080. rd_unlock(&devices_lock);
  1081. return count;
  1082. }
  1083. static int ascdevice(int ascid)
  1084. {
  1085. int count = 0;
  1086. int i;
  1087. rd_lock(&devices_lock);
  1088. for (i = 0; i < total_devices; i++) {
  1089. #define DRIVER_ADD_COMMAND(X) \
  1090. if (devices[i]->drv->drv_id == DRIVER_##X) \
  1091. count++;
  1092. ASIC_PARSE_COMMANDS
  1093. #undef DRIVER_ADD_COMMAND
  1094. if (count == (ascid + 1))
  1095. goto foundit;
  1096. }
  1097. rd_unlock(&devices_lock);
  1098. return -1;
  1099. foundit:
  1100. rd_unlock(&devices_lock);
  1101. return i;
  1102. }
  1103. #endif
  1104. #ifdef HAVE_AN_FPGA
  1105. static int numpgas(void)
  1106. {
  1107. int count = 0;
  1108. int i;
  1109. rd_lock(&devices_lock);
  1110. for (i = 0; i < total_devices; i++) {
  1111. #define DRIVER_ADD_COMMAND(X) \
  1112. if (devices[i]->drv->drv_id == DRIVER_##X) \
  1113. count++;
  1114. FPGA_PARSE_COMMANDS
  1115. #undef DRIVER_ADD_COMMAND
  1116. }
  1117. rd_unlock(&devices_lock);
  1118. return count;
  1119. }
  1120. static int pgadevice(int pgaid)
  1121. {
  1122. int count = 0;
  1123. int i;
  1124. rd_lock(&devices_lock);
  1125. for (i = 0; i < total_devices; i++) {
  1126. #define DRIVER_ADD_COMMAND(X) \
  1127. if (devices[i]->drv->drv_id == DRIVER_##X) \
  1128. count++;
  1129. FPGA_PARSE_COMMANDS
  1130. #undef DRIVER_ADD_COMMAND
  1131. if (count == (pgaid + 1))
  1132. goto foundit;
  1133. }
  1134. rd_unlock(&devices_lock);
  1135. return -1;
  1136. foundit:
  1137. rd_unlock(&devices_lock);
  1138. return i;
  1139. }
  1140. #endif
  1141. // All replies (except BYE and RESTART) start with a message
  1142. // thus for JSON, message() inserts JSON_START at the front
  1143. // and send_result() adds JSON_END at the end
  1144. static void message(struct io_data *io_data, int messageid, int paramid, char *param2, bool isjson)
  1145. {
  1146. struct api_data *root = NULL;
  1147. char buf[TMPBUFSIZ];
  1148. char buf2[TMPBUFSIZ];
  1149. char severity[2];
  1150. #ifdef HAVE_AN_ASIC
  1151. int asc;
  1152. #endif
  1153. #ifdef HAVE_AN_FPGA
  1154. int pga;
  1155. #endif
  1156. int i;
  1157. io_reinit(io_data);
  1158. if (isjson)
  1159. io_put(io_data, JSON_START JSON_STATUS);
  1160. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  1161. if (codes[i].code == messageid) {
  1162. switch (codes[i].severity) {
  1163. case SEVERITY_WARN:
  1164. severity[0] = 'W';
  1165. break;
  1166. case SEVERITY_INFO:
  1167. severity[0] = 'I';
  1168. break;
  1169. case SEVERITY_SUCC:
  1170. severity[0] = 'S';
  1171. break;
  1172. case SEVERITY_ERR:
  1173. default:
  1174. severity[0] = 'E';
  1175. break;
  1176. }
  1177. severity[1] = '\0';
  1178. switch(codes[i].params) {
  1179. case PARAM_GPU:
  1180. case PARAM_PGA:
  1181. case PARAM_ASC:
  1182. case PARAM_PID:
  1183. case PARAM_INT:
  1184. sprintf(buf, codes[i].description, paramid);
  1185. break;
  1186. case PARAM_POOL:
  1187. sprintf(buf, codes[i].description, paramid, pools[paramid]->rpc_url);
  1188. break;
  1189. #ifdef HAVE_OPENCL
  1190. case PARAM_GPUMAX:
  1191. sprintf(buf, codes[i].description, paramid, nDevs - 1);
  1192. break;
  1193. #endif
  1194. #ifdef HAVE_AN_FPGA
  1195. case PARAM_PGAMAX:
  1196. pga = numpgas();
  1197. sprintf(buf, codes[i].description, paramid, pga - 1);
  1198. break;
  1199. #endif
  1200. #ifdef HAVE_AN_ASIC
  1201. case PARAM_ASCMAX:
  1202. asc = numascs();
  1203. sprintf(buf, codes[i].description, paramid, asc - 1);
  1204. break;
  1205. #endif
  1206. case PARAM_PMAX:
  1207. sprintf(buf, codes[i].description, total_pools);
  1208. break;
  1209. case PARAM_POOLMAX:
  1210. sprintf(buf, codes[i].description, paramid, total_pools - 1);
  1211. break;
  1212. case PARAM_DMAX:
  1213. #ifdef HAVE_AN_ASIC
  1214. asc = numascs();
  1215. #endif
  1216. #ifdef HAVE_AN_FPGA
  1217. pga = numpgas();
  1218. #endif
  1219. sprintf(buf, codes[i].description
  1220. #ifdef HAVE_OPENCL
  1221. , nDevs
  1222. #endif
  1223. #ifdef HAVE_AN_ASIC
  1224. , asc
  1225. #endif
  1226. #ifdef HAVE_AN_FPGA
  1227. , pga
  1228. #endif
  1229. );
  1230. break;
  1231. case PARAM_CMD:
  1232. sprintf(buf, codes[i].description, JSON_COMMAND);
  1233. break;
  1234. case PARAM_STR:
  1235. sprintf(buf, codes[i].description, param2);
  1236. break;
  1237. case PARAM_BOTH:
  1238. sprintf(buf, codes[i].description, paramid, param2);
  1239. break;
  1240. case PARAM_BOOL:
  1241. sprintf(buf, codes[i].description, paramid ? TRUESTR : FALSESTR);
  1242. break;
  1243. case PARAM_SET:
  1244. sprintf(buf, codes[i].description, param2, paramid);
  1245. break;
  1246. case PARAM_NONE:
  1247. default:
  1248. strcpy(buf, codes[i].description);
  1249. }
  1250. root = api_add_string(root, _STATUS, severity, false);
  1251. root = api_add_time(root, "When", &when, false);
  1252. root = api_add_int(root, "Code", &messageid, false);
  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. return;
  1260. }
  1261. }
  1262. root = api_add_string(root, _STATUS, "F", false);
  1263. root = api_add_time(root, "When", &when, false);
  1264. int id = -1;
  1265. root = api_add_int(root, "Code", &id, false);
  1266. sprintf(buf, "%d", messageid);
  1267. root = api_add_escape(root, "Msg", buf, false);
  1268. root = api_add_escape(root, "Description", opt_api_description, false);
  1269. root = print_data(root, buf2, isjson, false);
  1270. io_add(io_data, buf2);
  1271. if (isjson)
  1272. io_add(io_data, JSON_CLOSE);
  1273. }
  1274. static void apiversion(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. message(io_data, MSG_VERSION, 0, NULL, isjson);
  1280. io_open = io_add(io_data, isjson ? COMSTR JSON_VERSION : _VERSION COMSTR);
  1281. root = api_add_string(root, "CGMiner", VERSION, false);
  1282. root = api_add_const(root, "API", APIVERSION, false);
  1283. root = print_data(root, buf, isjson, false);
  1284. io_add(io_data, buf);
  1285. if (isjson && io_open)
  1286. io_close(io_data);
  1287. }
  1288. static void minerconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1289. {
  1290. struct api_data *root = NULL;
  1291. char buf[TMPBUFSIZ];
  1292. bool io_open;
  1293. int gpucount = 0;
  1294. int asccount = 0;
  1295. int pgacount = 0;
  1296. char *adlinuse = (char *)NO;
  1297. #ifdef HAVE_ADL
  1298. const char *adl = YES;
  1299. int i;
  1300. for (i = 0; i < nDevs; i++) {
  1301. if (gpus[i].has_adl) {
  1302. adlinuse = (char *)YES;
  1303. break;
  1304. }
  1305. }
  1306. #else
  1307. const char *adl = NO;
  1308. #endif
  1309. #ifdef HAVE_OPENCL
  1310. gpucount = nDevs;
  1311. #endif
  1312. #ifdef HAVE_AN_ASIC
  1313. asccount = numascs();
  1314. #endif
  1315. #ifdef HAVE_AN_FPGA
  1316. pgacount = numpgas();
  1317. #endif
  1318. message(io_data, MSG_MINECONFIG, 0, NULL, isjson);
  1319. io_open = io_add(io_data, isjson ? COMSTR JSON_MINECONFIG : _MINECONFIG COMSTR);
  1320. root = api_add_int(root, "GPU Count", &gpucount, false);
  1321. root = api_add_int(root, "ASC Count", &asccount, false);
  1322. root = api_add_int(root, "PGA Count", &pgacount, false);
  1323. root = api_add_int(root, "Pool Count", &total_pools, false);
  1324. root = api_add_const(root, "ADL", (char *)adl, false);
  1325. root = api_add_string(root, "ADL in use", adlinuse, false);
  1326. root = api_add_const(root, "Strategy", strategies[pool_strategy].s, false);
  1327. root = api_add_int(root, "Log Interval", &opt_log_interval, false);
  1328. root = api_add_const(root, "Device Code", DEVICECODE, false);
  1329. root = api_add_const(root, "OS", OSINFO, false);
  1330. root = api_add_bool(root, "Failover-Only", &opt_fail_only, false);
  1331. root = api_add_int(root, "ScanTime", &opt_scantime, false);
  1332. root = api_add_int(root, "Queue", &opt_queue, false);
  1333. root = api_add_int(root, "Expiry", &opt_expiry, false);
  1334. #ifdef USE_USBUTILS
  1335. if (hotplug_time == 0)
  1336. root = api_add_const(root, "Hotplug", DISABLED, false);
  1337. else
  1338. root = api_add_int(root, "Hotplug", &hotplug_time, false);
  1339. #else
  1340. root = api_add_const(root, "Hotplug", NONE, false);
  1341. #endif
  1342. root = print_data(root, buf, isjson, false);
  1343. io_add(io_data, buf);
  1344. if (isjson && io_open)
  1345. io_close(io_data);
  1346. }
  1347. #if defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA) || defined(HAVE_AN_ASIC)
  1348. static const char *status2str(enum alive status)
  1349. {
  1350. switch (status) {
  1351. case LIFE_WELL:
  1352. return ALIVE;
  1353. case LIFE_SICK:
  1354. return SICK;
  1355. case LIFE_DEAD:
  1356. return DEAD;
  1357. case LIFE_NOSTART:
  1358. return NOSTART;
  1359. case LIFE_INIT:
  1360. return INIT;
  1361. default:
  1362. return UNKNOWN;
  1363. }
  1364. }
  1365. #endif
  1366. #ifdef HAVE_OPENCL
  1367. static void gpustatus(struct io_data *io_data, int gpu, bool isjson, bool precom)
  1368. {
  1369. struct api_data *root = NULL;
  1370. char intensity[20];
  1371. char buf[TMPBUFSIZ];
  1372. char *enabled;
  1373. char *status;
  1374. float gt, gv;
  1375. int ga, gf, gp, gc, gm, pt;
  1376. if (gpu >= 0 && gpu < nDevs) {
  1377. struct cgpu_info *cgpu = &gpus[gpu];
  1378. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  1379. #ifdef HAVE_ADL
  1380. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  1381. #endif
  1382. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  1383. if (cgpu->deven != DEV_DISABLED)
  1384. enabled = (char *)YES;
  1385. else
  1386. enabled = (char *)NO;
  1387. status = (char *)status2str(cgpu->status);
  1388. if (cgpu->dynamic)
  1389. strcpy(intensity, DYNAMIC);
  1390. else
  1391. sprintf(intensity, "%d", cgpu->intensity);
  1392. root = api_add_int(root, "GPU", &gpu, false);
  1393. root = api_add_string(root, "Enabled", enabled, false);
  1394. root = api_add_string(root, "Status", status, false);
  1395. root = api_add_temp(root, "Temperature", &gt, false);
  1396. root = api_add_int(root, "Fan Speed", &gf, false);
  1397. root = api_add_int(root, "Fan Percent", &gp, false);
  1398. root = api_add_int(root, "GPU Clock", &gc, false);
  1399. root = api_add_int(root, "Memory Clock", &gm, false);
  1400. root = api_add_volts(root, "GPU Voltage", &gv, false);
  1401. root = api_add_int(root, "GPU Activity", &ga, false);
  1402. root = api_add_int(root, "Powertune", &pt, false);
  1403. double mhs = cgpu->total_mhashes / total_secs;
  1404. root = api_add_mhs(root, "MHS av", &mhs, false);
  1405. char mhsname[27];
  1406. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1407. root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
  1408. root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
  1409. root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
  1410. root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
  1411. root = api_add_utility(root, "Utility", &(cgpu->utility), false);
  1412. root = api_add_string(root, "Intensity", intensity, false);
  1413. int last_share_pool = cgpu->last_share_pool_time > 0 ?
  1414. cgpu->last_share_pool : -1;
  1415. root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
  1416. root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
  1417. root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
  1418. root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
  1419. root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
  1420. root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
  1421. root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
  1422. root = api_add_time(root, "Last Valid Work", &(cgpu->last_device_valid_work), false);
  1423. double hwp = (cgpu->hw_errors + cgpu->diff1) ?
  1424. (double)(cgpu->hw_errors) / (double)(cgpu->hw_errors + cgpu->diff1) : 0;
  1425. root = api_add_percent(root, "Device Hardware%", &hwp, false);
  1426. double rejp = cgpu->diff1 ?
  1427. (double)(cgpu->diff_rejected) / (double)(cgpu->diff1) : 0;
  1428. root = api_add_percent(root, "Device Rejected%", &rejp, false);
  1429. root = print_data(root, buf, isjson, precom);
  1430. io_add(io_data, buf);
  1431. }
  1432. }
  1433. #endif
  1434. #ifdef HAVE_AN_ASIC
  1435. static void ascstatus(struct io_data *io_data, int asc, bool isjson, bool precom)
  1436. {
  1437. struct api_data *root = NULL;
  1438. char buf[TMPBUFSIZ];
  1439. char *enabled;
  1440. char *status;
  1441. int numasc = numascs();
  1442. if (numasc > 0 && asc >= 0 && asc < numasc) {
  1443. int dev = ascdevice(asc);
  1444. if (dev < 0) // Should never happen
  1445. return;
  1446. struct cgpu_info *cgpu = get_devices(dev);
  1447. float temp = cgpu->temp;
  1448. struct timeval now;
  1449. double dev_runtime;
  1450. if (cgpu->dev_start_tv.tv_sec == 0)
  1451. dev_runtime = total_secs;
  1452. else {
  1453. cgtime(&now);
  1454. dev_runtime = tdiff(&now, &(cgpu->dev_start_tv));
  1455. }
  1456. if (dev_runtime < 1.0)
  1457. dev_runtime = 1.0;
  1458. cgpu->utility = cgpu->accepted / dev_runtime * 60;
  1459. if (cgpu->deven != DEV_DISABLED)
  1460. enabled = (char *)YES;
  1461. else
  1462. enabled = (char *)NO;
  1463. status = (char *)status2str(cgpu->status);
  1464. root = api_add_int(root, "ASC", &asc, false);
  1465. root = api_add_string(root, "Name", cgpu->drv->name, false);
  1466. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  1467. root = api_add_string(root, "Enabled", enabled, false);
  1468. root = api_add_string(root, "Status", status, false);
  1469. root = api_add_temp(root, "Temperature", &temp, false);
  1470. double mhs = cgpu->total_mhashes / dev_runtime;
  1471. root = api_add_mhs(root, "MHS av", &mhs, false);
  1472. char mhsname[27];
  1473. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1474. root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
  1475. root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
  1476. root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
  1477. root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
  1478. root = api_add_utility(root, "Utility", &(cgpu->utility), false);
  1479. int last_share_pool = cgpu->last_share_pool_time > 0 ?
  1480. cgpu->last_share_pool : -1;
  1481. root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
  1482. root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
  1483. root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
  1484. root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
  1485. root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
  1486. root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
  1487. root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
  1488. #ifdef USE_USBUTILS
  1489. root = api_add_bool(root, "No Device", &(cgpu->usbinfo.nodev), false);
  1490. #endif
  1491. root = api_add_time(root, "Last Valid Work", &(cgpu->last_device_valid_work), false);
  1492. double hwp = (cgpu->hw_errors + cgpu->diff1) ?
  1493. (double)(cgpu->hw_errors) / (double)(cgpu->hw_errors + cgpu->diff1) : 0;
  1494. root = api_add_percent(root, "Device Hardware%", &hwp, false);
  1495. double rejp = cgpu->diff1 ?
  1496. (double)(cgpu->diff_rejected) / (double)(cgpu->diff1) : 0;
  1497. root = api_add_percent(root, "Device Rejected%", &rejp, false);
  1498. root = print_data(root, buf, isjson, precom);
  1499. io_add(io_data, buf);
  1500. }
  1501. }
  1502. #endif
  1503. #ifdef HAVE_AN_FPGA
  1504. static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom)
  1505. {
  1506. struct api_data *root = NULL;
  1507. char buf[TMPBUFSIZ];
  1508. char *enabled;
  1509. char *status;
  1510. int numpga = numpgas();
  1511. if (numpga > 0 && pga >= 0 && pga < numpga) {
  1512. int dev = pgadevice(pga);
  1513. if (dev < 0) // Should never happen
  1514. return;
  1515. struct cgpu_info *cgpu = get_devices(dev);
  1516. double frequency = 0;
  1517. float temp = cgpu->temp;
  1518. struct timeval now;
  1519. double dev_runtime;
  1520. if (cgpu->dev_start_tv.tv_sec == 0)
  1521. dev_runtime = total_secs;
  1522. else {
  1523. cgtime(&now);
  1524. dev_runtime = tdiff(&now, &(cgpu->dev_start_tv));
  1525. }
  1526. if (dev_runtime < 1.0)
  1527. dev_runtime = 1.0;
  1528. #ifdef USE_ZTEX
  1529. if (cgpu->drv->drv_id == DRIVER_ztex && cgpu->device_ztex)
  1530. frequency = cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1);
  1531. #endif
  1532. #ifdef USE_MODMINER
  1533. if (cgpu->drv->drv_id == DRIVER_modminer)
  1534. frequency = cgpu->clock;
  1535. #endif
  1536. cgpu->utility = cgpu->accepted / dev_runtime * 60;
  1537. if (cgpu->deven != DEV_DISABLED)
  1538. enabled = (char *)YES;
  1539. else
  1540. enabled = (char *)NO;
  1541. status = (char *)status2str(cgpu->status);
  1542. root = api_add_int(root, "PGA", &pga, false);
  1543. root = api_add_string(root, "Name", cgpu->drv->name, false);
  1544. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  1545. root = api_add_string(root, "Enabled", enabled, false);
  1546. root = api_add_string(root, "Status", status, false);
  1547. root = api_add_temp(root, "Temperature", &temp, false);
  1548. double mhs = cgpu->total_mhashes / dev_runtime;
  1549. root = api_add_mhs(root, "MHS av", &mhs, false);
  1550. char mhsname[27];
  1551. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1552. root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
  1553. root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
  1554. root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
  1555. root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
  1556. root = api_add_utility(root, "Utility", &(cgpu->utility), false);
  1557. int last_share_pool = cgpu->last_share_pool_time > 0 ?
  1558. cgpu->last_share_pool : -1;
  1559. root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
  1560. root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
  1561. root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
  1562. root = api_add_freq(root, "Frequency", &frequency, false);
  1563. root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
  1564. root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
  1565. root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
  1566. root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
  1567. #ifdef USE_USBUTILS
  1568. root = api_add_bool(root, "No Device", &(cgpu->usbinfo.nodev), false);
  1569. #endif
  1570. root = api_add_time(root, "Last Valid Work", &(cgpu->last_device_valid_work), false);
  1571. double hwp = (cgpu->hw_errors + cgpu->diff1) ?
  1572. (double)(cgpu->hw_errors) / (double)(cgpu->hw_errors + cgpu->diff1) : 0;
  1573. root = api_add_percent(root, "Device Hardware%", &hwp, false);
  1574. double rejp = cgpu->diff1 ?
  1575. (double)(cgpu->diff_rejected) / (double)(cgpu->diff1) : 0;
  1576. root = api_add_percent(root, "Device Rejected%", &rejp, false);
  1577. root = print_data(root, buf, isjson, precom);
  1578. io_add(io_data, buf);
  1579. }
  1580. }
  1581. #endif
  1582. static void devstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1583. {
  1584. bool io_open = false;
  1585. int devcount = 0;
  1586. int numgpu = 0;
  1587. int numasc = 0;
  1588. int numpga = 0;
  1589. int i;
  1590. #ifdef HAVE_OPENCL
  1591. numgpu = nDevs;
  1592. #endif
  1593. #ifdef HAVE_AN_ASIC
  1594. numasc = numascs();
  1595. #endif
  1596. #ifdef HAVE_AN_FPGA
  1597. numpga = numpgas();
  1598. #endif
  1599. if (numgpu == 0 && numpga == 0 && numasc == 0) {
  1600. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  1601. return;
  1602. }
  1603. message(io_data, MSG_DEVS, 0, NULL, isjson);
  1604. if (isjson)
  1605. io_open = io_add(io_data, COMSTR JSON_DEVS);
  1606. #ifdef HAVE_OPENCL
  1607. for (i = 0; i < nDevs; i++) {
  1608. gpustatus(io_data, i, isjson, isjson && devcount > 0);
  1609. devcount++;
  1610. }
  1611. #endif
  1612. #ifdef HAVE_AN_ASIC
  1613. if (numasc > 0) {
  1614. for (i = 0; i < numasc; i++) {
  1615. ascstatus(io_data, i, isjson, isjson && devcount > 0);
  1616. devcount++;
  1617. }
  1618. }
  1619. #endif
  1620. #ifdef HAVE_AN_FPGA
  1621. if (numpga > 0) {
  1622. for (i = 0; i < numpga; i++) {
  1623. pgastatus(io_data, i, isjson, isjson && devcount > 0);
  1624. devcount++;
  1625. }
  1626. }
  1627. #endif
  1628. if (isjson && io_open)
  1629. io_close(io_data);
  1630. }
  1631. #ifdef HAVE_OPENCL
  1632. static void gpudev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1633. {
  1634. bool io_open = false;
  1635. int id;
  1636. if (nDevs == 0) {
  1637. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1638. return;
  1639. }
  1640. if (param == NULL || *param == '\0') {
  1641. message(io_data, MSG_MISID, 0, NULL, isjson);
  1642. return;
  1643. }
  1644. id = atoi(param);
  1645. if (id < 0 || id >= nDevs) {
  1646. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1647. return;
  1648. }
  1649. message(io_data, MSG_GPUDEV, id, NULL, isjson);
  1650. if (isjson)
  1651. io_open = io_add(io_data, COMSTR JSON_GPU);
  1652. gpustatus(io_data, id, isjson, false);
  1653. if (isjson && io_open)
  1654. io_close(io_data);
  1655. }
  1656. #endif
  1657. #ifdef HAVE_AN_FPGA
  1658. static void pgadev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1659. {
  1660. bool io_open = false;
  1661. int numpga = numpgas();
  1662. int id;
  1663. if (numpga == 0) {
  1664. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1665. return;
  1666. }
  1667. if (param == NULL || *param == '\0') {
  1668. message(io_data, MSG_MISID, 0, NULL, isjson);
  1669. return;
  1670. }
  1671. id = atoi(param);
  1672. if (id < 0 || id >= numpga) {
  1673. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1674. return;
  1675. }
  1676. message(io_data, MSG_PGADEV, id, NULL, isjson);
  1677. if (isjson)
  1678. io_open = io_add(io_data, COMSTR JSON_PGA);
  1679. pgastatus(io_data, id, isjson, false);
  1680. if (isjson && io_open)
  1681. io_close(io_data);
  1682. }
  1683. static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1684. {
  1685. struct cgpu_info *cgpu;
  1686. int numpga = numpgas();
  1687. struct thr_info *thr;
  1688. int pga;
  1689. int id;
  1690. int i;
  1691. if (numpga == 0) {
  1692. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1693. return;
  1694. }
  1695. if (param == NULL || *param == '\0') {
  1696. message(io_data, MSG_MISID, 0, NULL, isjson);
  1697. return;
  1698. }
  1699. id = atoi(param);
  1700. if (id < 0 || id >= numpga) {
  1701. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1702. return;
  1703. }
  1704. int dev = pgadevice(id);
  1705. if (dev < 0) { // Should never happen
  1706. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1707. return;
  1708. }
  1709. cgpu = get_devices(dev);
  1710. applog(LOG_DEBUG, "API: request to pgaenable pgaid %d device %d %s%u",
  1711. id, dev, cgpu->drv->name, cgpu->device_id);
  1712. if (cgpu->deven != DEV_DISABLED) {
  1713. message(io_data, MSG_PGALRENA, id, NULL, isjson);
  1714. return;
  1715. }
  1716. #if 0 /* A DISABLED device wont change status FIXME: should disabling make it WELL? */
  1717. if (cgpu->status != LIFE_WELL) {
  1718. message(io_data, MSG_PGAUNW, id, NULL, isjson);
  1719. return;
  1720. }
  1721. #endif
  1722. #ifdef USE_USBUTILS
  1723. if (cgpu->usbinfo.nodev) {
  1724. message(io_data, MSG_PGAUSBNODEV, id, NULL, isjson);
  1725. return;
  1726. }
  1727. #endif
  1728. for (i = 0; i < mining_threads; i++) {
  1729. thr = get_thread(i);
  1730. pga = thr->cgpu->cgminer_id;
  1731. if (pga == dev) {
  1732. cgpu->deven = DEV_ENABLED;
  1733. applog(LOG_DEBUG, "API: Pushing sem post to thread %d", thr->id);
  1734. cgsem_post(&thr->sem);
  1735. }
  1736. }
  1737. message(io_data, MSG_PGAENA, id, NULL, isjson);
  1738. }
  1739. static void pgadisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1740. {
  1741. struct cgpu_info *cgpu;
  1742. int numpga = numpgas();
  1743. int id;
  1744. if (numpga == 0) {
  1745. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1746. return;
  1747. }
  1748. if (param == NULL || *param == '\0') {
  1749. message(io_data, MSG_MISID, 0, NULL, isjson);
  1750. return;
  1751. }
  1752. id = atoi(param);
  1753. if (id < 0 || id >= numpga) {
  1754. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1755. return;
  1756. }
  1757. int dev = pgadevice(id);
  1758. if (dev < 0) { // Should never happen
  1759. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1760. return;
  1761. }
  1762. cgpu = get_devices(dev);
  1763. applog(LOG_DEBUG, "API: request to pgadisable pgaid %d device %d %s%u",
  1764. id, dev, cgpu->drv->name, cgpu->device_id);
  1765. if (cgpu->deven == DEV_DISABLED) {
  1766. message(io_data, MSG_PGALRDIS, id, NULL, isjson);
  1767. return;
  1768. }
  1769. cgpu->deven = DEV_DISABLED;
  1770. message(io_data, MSG_PGADIS, id, NULL, isjson);
  1771. }
  1772. static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1773. {
  1774. struct cgpu_info *cgpu;
  1775. struct device_drv *drv;
  1776. int numpga = numpgas();
  1777. int id;
  1778. if (numpga == 0) {
  1779. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1780. return;
  1781. }
  1782. if (param == NULL || *param == '\0') {
  1783. message(io_data, MSG_MISID, 0, NULL, isjson);
  1784. return;
  1785. }
  1786. id = atoi(param);
  1787. if (id < 0 || id >= numpga) {
  1788. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1789. return;
  1790. }
  1791. int dev = pgadevice(id);
  1792. if (dev < 0) { // Should never happen
  1793. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1794. return;
  1795. }
  1796. cgpu = get_devices(dev);
  1797. drv = cgpu->drv;
  1798. if (!drv->identify_device)
  1799. message(io_data, MSG_PGANOID, id, NULL, isjson);
  1800. else {
  1801. drv->identify_device(cgpu);
  1802. message(io_data, MSG_PGAIDENT, id, NULL, isjson);
  1803. }
  1804. }
  1805. #endif
  1806. static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1807. {
  1808. struct api_data *root = NULL;
  1809. char buf[TMPBUFSIZ];
  1810. bool io_open = false;
  1811. char *status, *lp;
  1812. int i;
  1813. if (total_pools == 0) {
  1814. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1815. return;
  1816. }
  1817. message(io_data, MSG_POOL, 0, NULL, isjson);
  1818. if (isjson)
  1819. io_open = io_add(io_data, COMSTR JSON_POOLS);
  1820. for (i = 0; i < total_pools; i++) {
  1821. struct pool *pool = pools[i];
  1822. if (pool->removed)
  1823. continue;
  1824. switch (pool->enabled) {
  1825. case POOL_DISABLED:
  1826. status = (char *)DISABLED;
  1827. break;
  1828. case POOL_REJECTING:
  1829. status = (char *)REJECTING;
  1830. break;
  1831. case POOL_ENABLED:
  1832. if (pool->idle)
  1833. status = (char *)DEAD;
  1834. else
  1835. status = (char *)ALIVE;
  1836. break;
  1837. default:
  1838. status = (char *)UNKNOWN;
  1839. break;
  1840. }
  1841. if (pool->hdr_path)
  1842. lp = (char *)YES;
  1843. else
  1844. lp = (char *)NO;
  1845. root = api_add_int(root, "POOL", &i, false);
  1846. root = api_add_escape(root, "URL", pool->rpc_url, false);
  1847. root = api_add_string(root, "Status", status, false);
  1848. root = api_add_int(root, "Priority", &(pool->prio), false);
  1849. root = api_add_int(root, "Quota", &pool->quota, false);
  1850. root = api_add_string(root, "Long Poll", lp, false);
  1851. root = api_add_uint(root, "Getworks", &(pool->getwork_requested), false);
  1852. root = api_add_int(root, "Accepted", &(pool->accepted), false);
  1853. root = api_add_int(root, "Rejected", &(pool->rejected), false);
  1854. root = api_add_uint(root, "Discarded", &(pool->discarded_work), false);
  1855. root = api_add_uint(root, "Stale", &(pool->stale_shares), false);
  1856. root = api_add_uint(root, "Get Failures", &(pool->getfail_occasions), false);
  1857. root = api_add_uint(root, "Remote Failures", &(pool->remotefail_occasions), false);
  1858. root = api_add_escape(root, "User", pool->rpc_user, false);
  1859. root = api_add_time(root, "Last Share Time", &(pool->last_share_time), false);
  1860. root = api_add_int(root, "Diff1 Shares", &(pool->diff1), false);
  1861. if (pool->rpc_proxy) {
  1862. root = api_add_const(root, "Proxy Type", proxytype(pool->rpc_proxytype), false);
  1863. root = api_add_escape(root, "Proxy", pool->rpc_proxy, false);
  1864. } else {
  1865. root = api_add_const(root, "Proxy Type", BLANK, false);
  1866. root = api_add_const(root, "Proxy", BLANK, false);
  1867. }
  1868. root = api_add_diff(root, "Difficulty Accepted", &(pool->diff_accepted), false);
  1869. root = api_add_diff(root, "Difficulty Rejected", &(pool->diff_rejected), false);
  1870. root = api_add_diff(root, "Difficulty Stale", &(pool->diff_stale), false);
  1871. root = api_add_diff(root, "Last Share Difficulty", &(pool->last_share_diff), false);
  1872. root = api_add_bool(root, "Has Stratum", &(pool->has_stratum), false);
  1873. root = api_add_bool(root, "Stratum Active", &(pool->stratum_active), false);
  1874. if (pool->stratum_active)
  1875. root = api_add_escape(root, "Stratum URL", pool->stratum_url, false);
  1876. else
  1877. root = api_add_const(root, "Stratum URL", BLANK, false);
  1878. root = api_add_bool(root, "Has GBT", &(pool->has_gbt), false);
  1879. root = api_add_uint64(root, "Best Share", &(pool->best_diff), true);
  1880. double rejp = (pool->diff_accepted + pool->diff_rejected + pool->diff_stale) ?
  1881. (double)(pool->diff_rejected) / (double)(pool->diff_accepted + pool->diff_rejected + pool->diff_stale) : 0;
  1882. root = api_add_percent(root, "Pool Rejected%", &rejp, false);
  1883. double stalep = (pool->diff_accepted + pool->diff_rejected + pool->diff_stale) ?
  1884. (double)(pool->diff_stale) / (double)(pool->diff_accepted + pool->diff_rejected + pool->diff_stale) : 0;
  1885. root = api_add_percent(root, "Pool Stale%", &stalep, false);
  1886. root = print_data(root, buf, isjson, isjson && (i > 0));
  1887. io_add(io_data, buf);
  1888. }
  1889. if (isjson && io_open)
  1890. io_close(io_data);
  1891. }
  1892. static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1893. {
  1894. struct api_data *root = NULL;
  1895. char buf[TMPBUFSIZ];
  1896. bool io_open;
  1897. double utility, mhs, work_utility;
  1898. message(io_data, MSG_SUMM, 0, NULL, isjson);
  1899. io_open = io_add(io_data, isjson ? COMSTR JSON_SUMMARY : _SUMMARY COMSTR);
  1900. // stop hashmeter() changing some while copying
  1901. mutex_lock(&hash_lock);
  1902. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1903. mhs = total_mhashes_done / total_secs;
  1904. work_utility = total_diff1 / ( total_secs ? total_secs : 1 ) * 60;
  1905. root = api_add_elapsed(root, "Elapsed", &(total_secs), true);
  1906. root = api_add_mhs(root, "MHS av", &(mhs), false);
  1907. root = api_add_uint(root, "Found Blocks", &(found_blocks), true);
  1908. root = api_add_int(root, "Getworks", &(total_getworks), true);
  1909. root = api_add_int(root, "Accepted", &(total_accepted), true);
  1910. root = api_add_int(root, "Rejected", &(total_rejected), true);
  1911. root = api_add_int(root, "Hardware Errors", &(hw_errors), true);
  1912. root = api_add_utility(root, "Utility", &(utility), false);
  1913. root = api_add_int(root, "Discarded", &(total_discarded), true);
  1914. root = api_add_int(root, "Stale", &(total_stale), true);
  1915. root = api_add_uint(root, "Get Failures", &(total_go), true);
  1916. root = api_add_uint(root, "Local Work", &(local_work), true);
  1917. root = api_add_uint(root, "Remote Failures", &(total_ro), true);
  1918. root = api_add_uint(root, "Network Blocks", &(new_blocks), true);
  1919. root = api_add_mhtotal(root, "Total MH", &(total_mhashes_done), true);
  1920. root = api_add_utility(root, "Work Utility", &(work_utility), false);
  1921. root = api_add_diff(root, "Difficulty Accepted", &(total_diff_accepted), true);
  1922. root = api_add_diff(root, "Difficulty Rejected", &(total_diff_rejected), true);
  1923. root = api_add_diff(root, "Difficulty Stale", &(total_diff_stale), true);
  1924. root = api_add_uint64(root, "Best Share", &(best_diff), true);
  1925. double hwp = (hw_errors + total_diff1) ?
  1926. (double)(hw_errors) / (double)(hw_errors + total_diff1) : 0;
  1927. root = api_add_percent(root, "Device Hardware%", &hwp, false);
  1928. double rejp = total_diff1 ?
  1929. (double)(total_diff_rejected) / (double)(total_diff1) : 0;
  1930. root = api_add_percent(root, "Device Rejected%", &rejp, false);
  1931. double prejp = (total_diff_accepted + total_diff_rejected + total_diff_stale) ?
  1932. (double)(total_diff_rejected) / (double)(total_diff_accepted + total_diff_rejected + total_diff_stale) : 0;
  1933. root = api_add_percent(root, "Pool Rejected%", &prejp, false);
  1934. double stalep = (total_diff_accepted + total_diff_rejected + total_diff_stale) ?
  1935. (double)(total_diff_stale) / (double)(total_diff_accepted + total_diff_rejected + total_diff_stale) : 0;
  1936. root = api_add_percent(root, "Pool Stale%", &stalep, false);
  1937. mutex_unlock(&hash_lock);
  1938. root = print_data(root, buf, isjson, false);
  1939. io_add(io_data, buf);
  1940. if (isjson && io_open)
  1941. io_close(io_data);
  1942. }
  1943. #ifdef HAVE_OPENCL
  1944. static void gpuenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1945. {
  1946. struct thr_info *thr;
  1947. int gpu;
  1948. int id;
  1949. int i;
  1950. if (gpu_threads == 0) {
  1951. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1952. return;
  1953. }
  1954. if (param == NULL || *param == '\0') {
  1955. message(io_data, MSG_MISID, 0, NULL, isjson);
  1956. return;
  1957. }
  1958. id = atoi(param);
  1959. if (id < 0 || id >= nDevs) {
  1960. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1961. return;
  1962. }
  1963. applog(LOG_DEBUG, "API: request to gpuenable gpuid %d %s%u",
  1964. id, gpus[id].drv->name, gpus[id].device_id);
  1965. if (gpus[id].deven != DEV_DISABLED) {
  1966. message(io_data, MSG_ALRENA, id, NULL, isjson);
  1967. return;
  1968. }
  1969. for (i = 0; i < gpu_threads; i++) {
  1970. thr = get_thread(i);
  1971. gpu = thr->cgpu->device_id;
  1972. if (gpu == id) {
  1973. if (thr->cgpu->status != LIFE_WELL) {
  1974. message(io_data, MSG_GPUMRE, id, NULL, isjson);
  1975. return;
  1976. }
  1977. gpus[id].deven = DEV_ENABLED;
  1978. applog(LOG_DEBUG, "API Pushing sem post to thread %d", thr->id);
  1979. cgsem_post(&thr->sem);
  1980. }
  1981. }
  1982. message(io_data, MSG_GPUREN, id, NULL, isjson);
  1983. }
  1984. static void gpudisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1985. {
  1986. int id;
  1987. if (nDevs == 0) {
  1988. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1989. return;
  1990. }
  1991. if (param == NULL || *param == '\0') {
  1992. message(io_data, MSG_MISID, 0, NULL, isjson);
  1993. return;
  1994. }
  1995. id = atoi(param);
  1996. if (id < 0 || id >= nDevs) {
  1997. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1998. return;
  1999. }
  2000. applog(LOG_DEBUG, "API: request to gpudisable gpuid %d %s%u",
  2001. id, gpus[id].drv->name, gpus[id].device_id);
  2002. if (gpus[id].deven == DEV_DISABLED) {
  2003. message(io_data, MSG_ALRDIS, id, NULL, isjson);
  2004. return;
  2005. }
  2006. gpus[id].deven = DEV_DISABLED;
  2007. message(io_data, MSG_GPUDIS, id, NULL, isjson);
  2008. }
  2009. static void gpurestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2010. {
  2011. int id;
  2012. if (nDevs == 0) {
  2013. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  2014. return;
  2015. }
  2016. if (param == NULL || *param == '\0') {
  2017. message(io_data, MSG_MISID, 0, NULL, isjson);
  2018. return;
  2019. }
  2020. id = atoi(param);
  2021. if (id < 0 || id >= nDevs) {
  2022. message(io_data, MSG_INVGPU, id, NULL, isjson);
  2023. return;
  2024. }
  2025. reinit_device(&gpus[id]);
  2026. message(io_data, MSG_GPUREI, id, NULL, isjson);
  2027. }
  2028. #endif
  2029. static void gpucount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2030. {
  2031. struct api_data *root = NULL;
  2032. char buf[TMPBUFSIZ];
  2033. bool io_open;
  2034. int numgpu = 0;
  2035. #ifdef HAVE_OPENCL
  2036. numgpu = nDevs;
  2037. #endif
  2038. message(io_data, MSG_NUMGPU, 0, NULL, isjson);
  2039. io_open = io_add(io_data, isjson ? COMSTR JSON_GPUS : _GPUS COMSTR);
  2040. root = api_add_int(root, "Count", &numgpu, false);
  2041. root = print_data(root, buf, isjson, false);
  2042. io_add(io_data, buf);
  2043. if (isjson && io_open)
  2044. io_close(io_data);
  2045. }
  2046. static void pgacount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2047. {
  2048. struct api_data *root = NULL;
  2049. char buf[TMPBUFSIZ];
  2050. bool io_open;
  2051. int count = 0;
  2052. #ifdef HAVE_AN_FPGA
  2053. count = numpgas();
  2054. #endif
  2055. message(io_data, MSG_NUMPGA, 0, NULL, isjson);
  2056. io_open = io_add(io_data, isjson ? COMSTR JSON_PGAS : _PGAS COMSTR);
  2057. root = api_add_int(root, "Count", &count, false);
  2058. root = print_data(root, buf, isjson, false);
  2059. io_add(io_data, buf);
  2060. if (isjson && io_open)
  2061. io_close(io_data);
  2062. }
  2063. static void switchpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2064. {
  2065. struct pool *pool;
  2066. int id;
  2067. if (total_pools == 0) {
  2068. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2069. return;
  2070. }
  2071. if (param == NULL || *param == '\0') {
  2072. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2073. return;
  2074. }
  2075. id = atoi(param);
  2076. cg_rlock(&control_lock);
  2077. if (id < 0 || id >= total_pools) {
  2078. cg_runlock(&control_lock);
  2079. message(io_data, MSG_INVPID, id, NULL, isjson);
  2080. return;
  2081. }
  2082. pool = pools[id];
  2083. pool->enabled = POOL_ENABLED;
  2084. cg_runlock(&control_lock);
  2085. switch_pools(pool);
  2086. message(io_data, MSG_SWITCHP, id, NULL, isjson);
  2087. }
  2088. static void copyadvanceafter(char ch, char **param, char **buf)
  2089. {
  2090. #define src_p (*param)
  2091. #define dst_b (*buf)
  2092. while (*src_p && *src_p != ch) {
  2093. if (*src_p == '\\' && *(src_p+1) != '\0')
  2094. src_p++;
  2095. *(dst_b++) = *(src_p++);
  2096. }
  2097. if (*src_p)
  2098. src_p++;
  2099. *(dst_b++) = '\0';
  2100. }
  2101. static bool pooldetails(char *param, char **url, char **user, char **pass)
  2102. {
  2103. char *ptr, *buf;
  2104. ptr = buf = malloc(strlen(param)+1);
  2105. if (unlikely(!buf))
  2106. quit(1, "Failed to malloc pooldetails buf");
  2107. *url = buf;
  2108. // copy url
  2109. copyadvanceafter(',', &param, &buf);
  2110. if (!(*param)) // missing user
  2111. goto exitsama;
  2112. *user = buf;
  2113. // copy user
  2114. copyadvanceafter(',', &param, &buf);
  2115. if (!*param) // missing pass
  2116. goto exitsama;
  2117. *pass = buf;
  2118. // copy pass
  2119. copyadvanceafter(',', &param, &buf);
  2120. return true;
  2121. exitsama:
  2122. free(ptr);
  2123. return false;
  2124. }
  2125. static void addpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2126. {
  2127. char *url, *user, *pass;
  2128. struct pool *pool;
  2129. char *ptr;
  2130. if (param == NULL || *param == '\0') {
  2131. message(io_data, MSG_MISPDP, 0, NULL, isjson);
  2132. return;
  2133. }
  2134. if (!pooldetails(param, &url, &user, &pass)) {
  2135. ptr = escape_string(param, isjson);
  2136. message(io_data, MSG_INVPDP, 0, ptr, isjson);
  2137. if (ptr != param)
  2138. free(ptr);
  2139. ptr = NULL;
  2140. return;
  2141. }
  2142. pool = add_pool();
  2143. detect_stratum(pool, url);
  2144. add_pool_details(pool, true, url, user, pass);
  2145. ptr = escape_string(url, isjson);
  2146. message(io_data, MSG_ADDPOOL, 0, ptr, isjson);
  2147. if (ptr != url)
  2148. free(ptr);
  2149. ptr = NULL;
  2150. }
  2151. static void enablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2152. {
  2153. struct pool *pool;
  2154. int id;
  2155. if (total_pools == 0) {
  2156. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2157. return;
  2158. }
  2159. if (param == NULL || *param == '\0') {
  2160. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2161. return;
  2162. }
  2163. id = atoi(param);
  2164. if (id < 0 || id >= total_pools) {
  2165. message(io_data, MSG_INVPID, id, NULL, isjson);
  2166. return;
  2167. }
  2168. pool = pools[id];
  2169. if (pool->enabled == POOL_ENABLED) {
  2170. message(io_data, MSG_ALRENAP, id, NULL, isjson);
  2171. return;
  2172. }
  2173. pool->enabled = POOL_ENABLED;
  2174. if (pool->prio < current_pool()->prio)
  2175. switch_pools(pool);
  2176. message(io_data, MSG_ENAPOOL, id, NULL, isjson);
  2177. }
  2178. static void poolpriority(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2179. {
  2180. char *ptr, *next;
  2181. int i, pr, prio = 0;
  2182. // TODO: all cgminer code needs a mutex added everywhere for change
  2183. // access to total_pools and also parts of the pools[] array,
  2184. // just copying total_pools here wont solve that
  2185. if (total_pools == 0) {
  2186. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2187. return;
  2188. }
  2189. if (param == NULL || *param == '\0') {
  2190. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2191. return;
  2192. }
  2193. bool pools_changed[total_pools];
  2194. int new_prio[total_pools];
  2195. for (i = 0; i < total_pools; ++i)
  2196. pools_changed[i] = false;
  2197. next = param;
  2198. while (next && *next) {
  2199. ptr = next;
  2200. next = strchr(ptr, ',');
  2201. if (next)
  2202. *(next++) = '\0';
  2203. i = atoi(ptr);
  2204. if (i < 0 || i >= total_pools) {
  2205. message(io_data, MSG_INVPID, i, NULL, isjson);
  2206. return;
  2207. }
  2208. if (pools_changed[i]) {
  2209. message(io_data, MSG_DUPPID, i, NULL, isjson);
  2210. return;
  2211. }
  2212. pools_changed[i] = true;
  2213. new_prio[i] = prio++;
  2214. }
  2215. // Only change them if no errors
  2216. for (i = 0; i < total_pools; i++) {
  2217. if (pools_changed[i])
  2218. pools[i]->prio = new_prio[i];
  2219. }
  2220. // In priority order, cycle through the unchanged pools and append them
  2221. for (pr = 0; pr < total_pools; pr++)
  2222. for (i = 0; i < total_pools; i++) {
  2223. if (!pools_changed[i] && pools[i]->prio == pr) {
  2224. pools[i]->prio = prio++;
  2225. pools_changed[i] = true;
  2226. break;
  2227. }
  2228. }
  2229. if (current_pool()->prio)
  2230. switch_pools(NULL);
  2231. message(io_data, MSG_POOLPRIO, 0, NULL, isjson);
  2232. }
  2233. static void poolquota(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2234. {
  2235. struct pool *pool;
  2236. int quota, id;
  2237. char *comma;
  2238. if (total_pools == 0) {
  2239. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2240. return;
  2241. }
  2242. if (param == NULL || *param == '\0') {
  2243. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2244. return;
  2245. }
  2246. comma = strchr(param, ',');
  2247. if (!comma) {
  2248. message(io_data, MSG_CONVAL, 0, param, isjson);
  2249. return;
  2250. }
  2251. *(comma++) = '\0';
  2252. id = atoi(param);
  2253. if (id < 0 || id >= total_pools) {
  2254. message(io_data, MSG_INVPID, id, NULL, isjson);
  2255. return;
  2256. }
  2257. pool = pools[id];
  2258. quota = atoi(comma);
  2259. if (quota < 0) {
  2260. message(io_data, MSG_INVNEG, quota, pool->rpc_url, isjson);
  2261. return;
  2262. }
  2263. pool->quota = quota;
  2264. adjust_quota_gcd();
  2265. message(io_data, MSG_SETQUOTA, quota, pool->rpc_url, isjson);
  2266. }
  2267. static void disablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2268. {
  2269. struct pool *pool;
  2270. int id;
  2271. if (total_pools == 0) {
  2272. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2273. return;
  2274. }
  2275. if (param == NULL || *param == '\0') {
  2276. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2277. return;
  2278. }
  2279. id = atoi(param);
  2280. if (id < 0 || id >= total_pools) {
  2281. message(io_data, MSG_INVPID, id, NULL, isjson);
  2282. return;
  2283. }
  2284. pool = pools[id];
  2285. if (pool->enabled == POOL_DISABLED) {
  2286. message(io_data, MSG_ALRDISP, id, NULL, isjson);
  2287. return;
  2288. }
  2289. if (enabled_pools <= 1) {
  2290. message(io_data, MSG_DISLASTP, id, NULL, isjson);
  2291. return;
  2292. }
  2293. pool->enabled = POOL_DISABLED;
  2294. if (pool == current_pool())
  2295. switch_pools(NULL);
  2296. message(io_data, MSG_DISPOOL, id, NULL, isjson);
  2297. }
  2298. static void removepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2299. {
  2300. struct pool *pool;
  2301. char *rpc_url;
  2302. bool dofree = false;
  2303. int id;
  2304. if (total_pools == 0) {
  2305. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2306. return;
  2307. }
  2308. if (param == NULL || *param == '\0') {
  2309. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2310. return;
  2311. }
  2312. id = atoi(param);
  2313. if (id < 0 || id >= total_pools) {
  2314. message(io_data, MSG_INVPID, id, NULL, isjson);
  2315. return;
  2316. }
  2317. if (total_pools <= 1) {
  2318. message(io_data, MSG_REMLASTP, id, NULL, isjson);
  2319. return;
  2320. }
  2321. pool = pools[id];
  2322. if (pool == current_pool())
  2323. switch_pools(NULL);
  2324. if (pool == current_pool()) {
  2325. message(io_data, MSG_ACTPOOL, id, NULL, isjson);
  2326. return;
  2327. }
  2328. pool->enabled = POOL_DISABLED;
  2329. rpc_url = escape_string(pool->rpc_url, isjson);
  2330. if (rpc_url != pool->rpc_url)
  2331. dofree = true;
  2332. remove_pool(pool);
  2333. message(io_data, MSG_REMPOOL, id, rpc_url, isjson);
  2334. if (dofree)
  2335. free(rpc_url);
  2336. rpc_url = NULL;
  2337. }
  2338. #ifdef HAVE_OPENCL
  2339. static bool splitgpuvalue(struct io_data *io_data, char *param, int *gpu, char **value, bool isjson)
  2340. {
  2341. int id;
  2342. char *gpusep;
  2343. if (nDevs == 0) {
  2344. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  2345. return false;
  2346. }
  2347. if (param == NULL || *param == '\0') {
  2348. message(io_data, MSG_MISID, 0, NULL, isjson);
  2349. return false;
  2350. }
  2351. gpusep = strchr(param, GPUSEP);
  2352. if (gpusep == NULL) {
  2353. message(io_data, MSG_MISVAL, 0, NULL, isjson);
  2354. return false;
  2355. }
  2356. *(gpusep++) = '\0';
  2357. id = atoi(param);
  2358. if (id < 0 || id >= nDevs) {
  2359. message(io_data, MSG_INVGPU, id, NULL, isjson);
  2360. return false;
  2361. }
  2362. *gpu = id;
  2363. *value = gpusep;
  2364. return true;
  2365. }
  2366. static void gpuintensity(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2367. {
  2368. int id;
  2369. char *value;
  2370. int intensity;
  2371. char intensitystr[7];
  2372. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2373. return;
  2374. if (!strncasecmp(value, DYNAMIC, 1)) {
  2375. gpus[id].dynamic = true;
  2376. strcpy(intensitystr, DYNAMIC);
  2377. }
  2378. else {
  2379. intensity = atoi(value);
  2380. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  2381. message(io_data, MSG_INVINT, 0, value, isjson);
  2382. return;
  2383. }
  2384. gpus[id].dynamic = false;
  2385. gpus[id].intensity = intensity;
  2386. sprintf(intensitystr, "%d", intensity);
  2387. }
  2388. message(io_data, MSG_GPUINT, id, intensitystr, isjson);
  2389. }
  2390. static void gpumem(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2391. {
  2392. #ifdef HAVE_ADL
  2393. int id;
  2394. char *value;
  2395. int clock;
  2396. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2397. return;
  2398. clock = atoi(value);
  2399. if (set_memoryclock(id, clock))
  2400. message(io_data, MSG_GPUMERR, id, value, isjson);
  2401. else
  2402. message(io_data, MSG_GPUMEM, id, value, isjson);
  2403. #else
  2404. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2405. #endif
  2406. }
  2407. static void gpuengine(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2408. {
  2409. #ifdef HAVE_ADL
  2410. int id;
  2411. char *value;
  2412. int clock;
  2413. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2414. return;
  2415. clock = atoi(value);
  2416. if (set_engineclock(id, clock))
  2417. message(io_data, MSG_GPUEERR, id, value, isjson);
  2418. else
  2419. message(io_data, MSG_GPUENG, id, value, isjson);
  2420. #else
  2421. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2422. #endif
  2423. }
  2424. static void gpufan(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2425. {
  2426. #ifdef HAVE_ADL
  2427. int id;
  2428. char *value;
  2429. int fan;
  2430. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2431. return;
  2432. fan = atoi(value);
  2433. if (set_fanspeed(id, fan))
  2434. message(io_data, MSG_GPUFERR, id, value, isjson);
  2435. else
  2436. message(io_data, MSG_GPUFAN, id, value, isjson);
  2437. #else
  2438. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2439. #endif
  2440. }
  2441. static void gpuvddc(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2442. {
  2443. #ifdef HAVE_ADL
  2444. int id;
  2445. char *value;
  2446. float vddc;
  2447. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2448. return;
  2449. vddc = atof(value);
  2450. if (set_vddc(id, vddc))
  2451. message(io_data, MSG_GPUVERR, id, value, isjson);
  2452. else
  2453. message(io_data, MSG_GPUVDDC, id, value, isjson);
  2454. #else
  2455. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2456. #endif
  2457. }
  2458. #endif
  2459. void doquit(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2460. {
  2461. if (isjson)
  2462. io_put(io_data, JSON_START JSON_BYE);
  2463. else
  2464. io_put(io_data, _BYE);
  2465. bye = true;
  2466. do_a_quit = true;
  2467. }
  2468. void dorestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2469. {
  2470. if (isjson)
  2471. io_put(io_data, JSON_START JSON_RESTART);
  2472. else
  2473. io_put(io_data, _RESTART);
  2474. bye = true;
  2475. do_a_restart = true;
  2476. }
  2477. void privileged(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2478. {
  2479. message(io_data, MSG_ACCOK, 0, NULL, isjson);
  2480. }
  2481. void notifystatus(struct io_data *io_data, int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
  2482. {
  2483. struct api_data *root = NULL;
  2484. char buf[TMPBUFSIZ];
  2485. char *reason;
  2486. if (cgpu->device_last_not_well == 0)
  2487. reason = REASON_NONE;
  2488. else
  2489. switch(cgpu->device_not_well_reason) {
  2490. case REASON_THREAD_FAIL_INIT:
  2491. reason = REASON_THREAD_FAIL_INIT_STR;
  2492. break;
  2493. case REASON_THREAD_ZERO_HASH:
  2494. reason = REASON_THREAD_ZERO_HASH_STR;
  2495. break;
  2496. case REASON_THREAD_FAIL_QUEUE:
  2497. reason = REASON_THREAD_FAIL_QUEUE_STR;
  2498. break;
  2499. case REASON_DEV_SICK_IDLE_60:
  2500. reason = REASON_DEV_SICK_IDLE_60_STR;
  2501. break;
  2502. case REASON_DEV_DEAD_IDLE_600:
  2503. reason = REASON_DEV_DEAD_IDLE_600_STR;
  2504. break;
  2505. case REASON_DEV_NOSTART:
  2506. reason = REASON_DEV_NOSTART_STR;
  2507. break;
  2508. case REASON_DEV_OVER_HEAT:
  2509. reason = REASON_DEV_OVER_HEAT_STR;
  2510. break;
  2511. case REASON_DEV_THERMAL_CUTOFF:
  2512. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  2513. break;
  2514. case REASON_DEV_COMMS_ERROR:
  2515. reason = REASON_DEV_COMMS_ERROR_STR;
  2516. break;
  2517. default:
  2518. reason = REASON_UNKNOWN_STR;
  2519. break;
  2520. }
  2521. // ALL counters (and only counters) must start the name with a '*'
  2522. // Simplifies future external support for identifying new counters
  2523. root = api_add_int(root, "NOTIFY", &device, false);
  2524. root = api_add_string(root, "Name", cgpu->drv->name, false);
  2525. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  2526. root = api_add_time(root, "Last Well", &(cgpu->device_last_well), false);
  2527. root = api_add_time(root, "Last Not Well", &(cgpu->device_last_not_well), false);
  2528. root = api_add_string(root, "Reason Not Well", reason, false);
  2529. root = api_add_int(root, "*Thread Fail Init", &(cgpu->thread_fail_init_count), false);
  2530. root = api_add_int(root, "*Thread Zero Hash", &(cgpu->thread_zero_hash_count), false);
  2531. root = api_add_int(root, "*Thread Fail Queue", &(cgpu->thread_fail_queue_count), false);
  2532. root = api_add_int(root, "*Dev Sick Idle 60s", &(cgpu->dev_sick_idle_60_count), false);
  2533. root = api_add_int(root, "*Dev Dead Idle 600s", &(cgpu->dev_dead_idle_600_count), false);
  2534. root = api_add_int(root, "*Dev Nostart", &(cgpu->dev_nostart_count), false);
  2535. root = api_add_int(root, "*Dev Over Heat", &(cgpu->dev_over_heat_count), false);
  2536. root = api_add_int(root, "*Dev Thermal Cutoff", &(cgpu->dev_thermal_cutoff_count), false);
  2537. root = api_add_int(root, "*Dev Comms Error", &(cgpu->dev_comms_error_count), false);
  2538. root = api_add_int(root, "*Dev Throttle", &(cgpu->dev_throttle_count), false);
  2539. root = print_data(root, buf, isjson, isjson && (device > 0));
  2540. io_add(io_data, buf);
  2541. }
  2542. static void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
  2543. {
  2544. struct cgpu_info *cgpu;
  2545. bool io_open = false;
  2546. int i;
  2547. if (total_devices == 0) {
  2548. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  2549. return;
  2550. }
  2551. message(io_data, MSG_NOTIFY, 0, NULL, isjson);
  2552. if (isjson)
  2553. io_open = io_add(io_data, COMSTR JSON_NOTIFY);
  2554. for (i = 0; i < total_devices; i++) {
  2555. cgpu = get_devices(i);
  2556. notifystatus(io_data, i, cgpu, isjson, group);
  2557. }
  2558. if (isjson && io_open)
  2559. io_close(io_data);
  2560. }
  2561. static void devdetails(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2562. {
  2563. struct api_data *root = NULL;
  2564. char buf[TMPBUFSIZ];
  2565. bool io_open = false;
  2566. struct cgpu_info *cgpu;
  2567. int i;
  2568. if (total_devices == 0) {
  2569. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  2570. return;
  2571. }
  2572. message(io_data, MSG_DEVDETAILS, 0, NULL, isjson);
  2573. if (isjson)
  2574. io_open = io_add(io_data, COMSTR JSON_DEVDETAILS);
  2575. for (i = 0; i < total_devices; i++) {
  2576. cgpu = get_devices(i);
  2577. root = api_add_int(root, "DEVDETAILS", &i, false);
  2578. root = api_add_string(root, "Name", cgpu->drv->name, false);
  2579. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  2580. root = api_add_string(root, "Driver", cgpu->drv->dname, false);
  2581. root = api_add_const(root, "Kernel", cgpu->kname ? : BLANK, false);
  2582. root = api_add_const(root, "Model", cgpu->name ? : BLANK, false);
  2583. root = api_add_const(root, "Device Path", cgpu->device_path ? : BLANK, false);
  2584. root = print_data(root, buf, isjson, isjson && (i > 0));
  2585. io_add(io_data, buf);
  2586. }
  2587. if (isjson && io_open)
  2588. io_close(io_data);
  2589. }
  2590. void dosave(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2591. {
  2592. char filename[PATH_MAX];
  2593. FILE *fcfg;
  2594. char *ptr;
  2595. if (param == NULL || *param == '\0') {
  2596. default_save_file(filename);
  2597. param = filename;
  2598. }
  2599. fcfg = fopen(param, "w");
  2600. if (!fcfg) {
  2601. ptr = escape_string(param, isjson);
  2602. message(io_data, MSG_BADFN, 0, ptr, isjson);
  2603. if (ptr != param)
  2604. free(ptr);
  2605. ptr = NULL;
  2606. return;
  2607. }
  2608. write_config(fcfg);
  2609. fclose(fcfg);
  2610. ptr = escape_string(param, isjson);
  2611. message(io_data, MSG_SAVED, 0, ptr, isjson);
  2612. if (ptr != param)
  2613. free(ptr);
  2614. ptr = NULL;
  2615. }
  2616. 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)
  2617. {
  2618. struct api_data *root = NULL;
  2619. char buf[TMPBUFSIZ];
  2620. root = api_add_int(root, "STATS", &i, false);
  2621. root = api_add_string(root, "ID", id, false);
  2622. root = api_add_elapsed(root, "Elapsed", &(total_secs), false);
  2623. root = api_add_uint32(root, "Calls", &(stats->getwork_calls), false);
  2624. root = api_add_timeval(root, "Wait", &(stats->getwork_wait), false);
  2625. root = api_add_timeval(root, "Max", &(stats->getwork_wait_max), false);
  2626. root = api_add_timeval(root, "Min", &(stats->getwork_wait_min), false);
  2627. if (pool_stats) {
  2628. root = api_add_uint32(root, "Pool Calls", &(pool_stats->getwork_calls), false);
  2629. root = api_add_uint32(root, "Pool Attempts", &(pool_stats->getwork_attempts), false);
  2630. root = api_add_timeval(root, "Pool Wait", &(pool_stats->getwork_wait), false);
  2631. root = api_add_timeval(root, "Pool Max", &(pool_stats->getwork_wait_max), false);
  2632. root = api_add_timeval(root, "Pool Min", &(pool_stats->getwork_wait_min), false);
  2633. root = api_add_double(root, "Pool Av", &(pool_stats->getwork_wait_rolling), false);
  2634. root = api_add_bool(root, "Work Had Roll Time", &(pool_stats->hadrolltime), false);
  2635. root = api_add_bool(root, "Work Can Roll", &(pool_stats->canroll), false);
  2636. root = api_add_bool(root, "Work Had Expire", &(pool_stats->hadexpire), false);
  2637. root = api_add_uint32(root, "Work Roll Time", &(pool_stats->rolltime), false);
  2638. root = api_add_diff(root, "Work Diff", &(pool_stats->last_diff), false);
  2639. root = api_add_diff(root, "Min Diff", &(pool_stats->min_diff), false);
  2640. root = api_add_diff(root, "Max Diff", &(pool_stats->max_diff), false);
  2641. root = api_add_uint32(root, "Min Diff Count", &(pool_stats->min_diff_count), false);
  2642. root = api_add_uint32(root, "Max Diff Count", &(pool_stats->max_diff_count), false);
  2643. root = api_add_uint64(root, "Times Sent", &(pool_stats->times_sent), false);
  2644. root = api_add_uint64(root, "Bytes Sent", &(pool_stats->bytes_sent), false);
  2645. root = api_add_uint64(root, "Times Recv", &(pool_stats->times_received), false);
  2646. root = api_add_uint64(root, "Bytes Recv", &(pool_stats->bytes_received), false);
  2647. root = api_add_uint64(root, "Net Bytes Sent", &(pool_stats->net_bytes_sent), false);
  2648. root = api_add_uint64(root, "Net Bytes Recv", &(pool_stats->net_bytes_received), false);
  2649. }
  2650. if (extra)
  2651. root = api_add_extra(root, extra);
  2652. if (cgpu) {
  2653. #ifdef USE_USBUTILS
  2654. char details[256];
  2655. if (cgpu->usbinfo.pipe_count)
  2656. snprintf(details, sizeof(details),
  2657. "%"PRIu64" %"PRIu64"/%"PRIu64"/%"PRIu64" %lu",
  2658. cgpu->usbinfo.pipe_count,
  2659. cgpu->usbinfo.clear_err_count,
  2660. cgpu->usbinfo.retry_err_count,
  2661. cgpu->usbinfo.clear_fail_count,
  2662. (unsigned long)(cgpu->usbinfo.last_pipe));
  2663. else
  2664. strcpy(details, "0");
  2665. root = api_add_string(root, "USB Pipe", details, true);
  2666. snprintf(details, sizeof(details),
  2667. "r%"PRIu64" %.6f w%"PRIu64" %.6f",
  2668. cgpu->usbinfo.read_delay_count,
  2669. cgpu->usbinfo.total_read_delay,
  2670. cgpu->usbinfo.write_delay_count,
  2671. cgpu->usbinfo.total_write_delay);
  2672. root = api_add_string(root, "USB Delay", details, true);
  2673. if (cgpu->usbinfo.usb_tmo[0].count == 0 &&
  2674. cgpu->usbinfo.usb_tmo[1].count == 0 &&
  2675. cgpu->usbinfo.usb_tmo[2].count == 0) {
  2676. snprintf(details, sizeof(details),
  2677. "%"PRIu64" 0", cgpu->usbinfo.tmo_count);
  2678. } else {
  2679. snprintf(details, sizeof(details),
  2680. "%"PRIu64" %d=%d/%d/%d/%"PRIu64"/%"PRIu64
  2681. " %d=%d/%d/%d/%"PRIu64"/%"PRIu64
  2682. " %d=%d/%d/%d/%"PRIu64"/%"PRIu64" ",
  2683. cgpu->usbinfo.tmo_count,
  2684. USB_TMO_0, cgpu->usbinfo.usb_tmo[0].count,
  2685. cgpu->usbinfo.usb_tmo[0].min_tmo,
  2686. cgpu->usbinfo.usb_tmo[0].max_tmo,
  2687. cgpu->usbinfo.usb_tmo[0].total_over,
  2688. cgpu->usbinfo.usb_tmo[0].total_tmo,
  2689. USB_TMO_1, cgpu->usbinfo.usb_tmo[1].count,
  2690. cgpu->usbinfo.usb_tmo[1].min_tmo,
  2691. cgpu->usbinfo.usb_tmo[1].max_tmo,
  2692. cgpu->usbinfo.usb_tmo[1].total_over,
  2693. cgpu->usbinfo.usb_tmo[1].total_tmo,
  2694. USB_TMO_2, cgpu->usbinfo.usb_tmo[2].count,
  2695. cgpu->usbinfo.usb_tmo[2].min_tmo,
  2696. cgpu->usbinfo.usb_tmo[2].max_tmo,
  2697. cgpu->usbinfo.usb_tmo[2].total_over,
  2698. cgpu->usbinfo.usb_tmo[2].total_tmo);
  2699. }
  2700. root = api_add_string(root, "USB tmo", details, true);
  2701. #endif
  2702. }
  2703. root = print_data(root, buf, isjson, isjson && (i > 0));
  2704. io_add(io_data, buf);
  2705. return ++i;
  2706. }
  2707. static void minerstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2708. {
  2709. struct cgpu_info *cgpu;
  2710. bool io_open = false;
  2711. struct api_data *extra;
  2712. char id[20];
  2713. int i, j;
  2714. message(io_data, MSG_MINESTATS, 0, NULL, isjson);
  2715. if (isjson)
  2716. io_open = io_add(io_data, COMSTR JSON_MINESTATS);
  2717. i = 0;
  2718. for (j = 0; j < total_devices; j++) {
  2719. cgpu = get_devices(j);
  2720. if (cgpu && cgpu->drv) {
  2721. if (cgpu->drv->get_api_stats)
  2722. extra = cgpu->drv->get_api_stats(cgpu);
  2723. else
  2724. extra = NULL;
  2725. sprintf(id, "%s%d", cgpu->drv->name, cgpu->device_id);
  2726. i = itemstats(io_data, i, id, &(cgpu->cgminer_stats), NULL, extra, cgpu, isjson);
  2727. }
  2728. }
  2729. for (j = 0; j < total_pools; j++) {
  2730. struct pool *pool = pools[j];
  2731. sprintf(id, "POOL%d", j);
  2732. i = itemstats(io_data, i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, NULL, isjson);
  2733. }
  2734. if (isjson && io_open)
  2735. io_close(io_data);
  2736. }
  2737. static void failoveronly(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2738. {
  2739. if (param == NULL || *param == '\0') {
  2740. message(io_data, MSG_MISBOOL, 0, NULL, isjson);
  2741. return;
  2742. }
  2743. *param = tolower(*param);
  2744. if (*param != 't' && *param != 'f') {
  2745. message(io_data, MSG_INVBOOL, 0, NULL, isjson);
  2746. return;
  2747. }
  2748. bool tf = (*param == 't');
  2749. opt_fail_only = tf;
  2750. message(io_data, MSG_FOO, tf, NULL, isjson);
  2751. }
  2752. static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2753. {
  2754. struct api_data *root = NULL;
  2755. char buf[TMPBUFSIZ];
  2756. bool io_open;
  2757. message(io_data, MSG_MINECOIN, 0, NULL, isjson);
  2758. io_open = io_add(io_data, isjson ? COMSTR JSON_MINECOIN : _MINECOIN COMSTR);
  2759. #ifdef USE_SCRYPT
  2760. if (opt_scrypt)
  2761. root = api_add_const(root, "Hash Method", SCRYPTSTR, false);
  2762. else
  2763. #endif
  2764. root = api_add_const(root, "Hash Method", SHA256STR, false);
  2765. cg_rlock(&ch_lock);
  2766. if (current_fullhash && *current_fullhash) {
  2767. root = api_add_timeval(root, "Current Block Time", &block_timeval, true);
  2768. root = api_add_string(root, "Current Block Hash", current_fullhash, true);
  2769. } else {
  2770. struct timeval t = {0,0};
  2771. root = api_add_timeval(root, "Current Block Time", &t, true);
  2772. root = api_add_const(root, "Current Block Hash", BLANK, false);
  2773. }
  2774. cg_runlock(&ch_lock);
  2775. root = api_add_bool(root, "LP", &have_longpoll, false);
  2776. root = api_add_diff(root, "Network Difficulty", &current_diff, true);
  2777. root = print_data(root, buf, isjson, false);
  2778. io_add(io_data, buf);
  2779. if (isjson && io_open)
  2780. io_close(io_data);
  2781. }
  2782. static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2783. {
  2784. struct api_data *root = NULL;
  2785. char buf[TMPBUFSIZ];
  2786. bool io_open;
  2787. if (param == NULL)
  2788. param = (char *)BLANK;
  2789. else
  2790. *param = tolower(*param);
  2791. switch(*param) {
  2792. case 's':
  2793. opt_realquiet = true;
  2794. break;
  2795. case 'q':
  2796. opt_quiet ^= true;
  2797. break;
  2798. case 'v':
  2799. opt_log_output ^= true;
  2800. if (opt_log_output)
  2801. opt_quiet = false;
  2802. break;
  2803. case 'd':
  2804. opt_debug ^= true;
  2805. opt_log_output = opt_debug;
  2806. if (opt_debug)
  2807. opt_quiet = false;
  2808. break;
  2809. case 'r':
  2810. opt_protocol ^= true;
  2811. if (opt_protocol)
  2812. opt_quiet = false;
  2813. break;
  2814. case 'p':
  2815. want_per_device_stats ^= true;
  2816. opt_log_output = want_per_device_stats;
  2817. break;
  2818. case 'n':
  2819. opt_log_output = false;
  2820. opt_debug = false;
  2821. opt_quiet = false;
  2822. opt_protocol = false;
  2823. want_per_device_stats = false;
  2824. opt_worktime = false;
  2825. break;
  2826. case 'w':
  2827. opt_worktime ^= true;
  2828. break;
  2829. #ifdef _MEMORY_DEBUG
  2830. case 'y':
  2831. cgmemspeedup();
  2832. break;
  2833. case 'z':
  2834. cgmemrpt();
  2835. break;
  2836. #endif
  2837. default:
  2838. // anything else just reports the settings
  2839. break;
  2840. }
  2841. message(io_data, MSG_DEBUGSET, 0, NULL, isjson);
  2842. io_open = io_add(io_data, isjson ? COMSTR JSON_DEBUGSET : _DEBUGSET COMSTR);
  2843. root = api_add_bool(root, "Silent", &opt_realquiet, false);
  2844. root = api_add_bool(root, "Quiet", &opt_quiet, false);
  2845. root = api_add_bool(root, "Verbose", &opt_log_output, false);
  2846. root = api_add_bool(root, "Debug", &opt_debug, false);
  2847. root = api_add_bool(root, "RPCProto", &opt_protocol, false);
  2848. root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
  2849. root = api_add_bool(root, "WorkTime", &opt_worktime, false);
  2850. root = print_data(root, buf, isjson, false);
  2851. io_add(io_data, buf);
  2852. if (isjson && io_open)
  2853. io_close(io_data);
  2854. }
  2855. static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2856. {
  2857. char *comma;
  2858. int value;
  2859. if (param == NULL || *param == '\0') {
  2860. message(io_data, MSG_CONPAR, 0, NULL, isjson);
  2861. return;
  2862. }
  2863. comma = strchr(param, ',');
  2864. if (!comma) {
  2865. message(io_data, MSG_CONVAL, 0, param, isjson);
  2866. return;
  2867. }
  2868. *(comma++) = '\0';
  2869. value = atoi(comma);
  2870. if (value < 0 || value > 9999) {
  2871. message(io_data, MSG_INVNUM, value, param, isjson);
  2872. return;
  2873. }
  2874. if (strcasecmp(param, "queue") == 0)
  2875. opt_queue = value;
  2876. else if (strcasecmp(param, "scantime") == 0)
  2877. opt_scantime = value;
  2878. else if (strcasecmp(param, "expiry") == 0)
  2879. opt_expiry = value;
  2880. else {
  2881. message(io_data, MSG_UNKCON, 0, param, isjson);
  2882. return;
  2883. }
  2884. message(io_data, MSG_SETCONFIG, value, param, isjson);
  2885. }
  2886. static void usbstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2887. {
  2888. struct api_data *root = NULL;
  2889. #ifdef USE_USBUTILS
  2890. char buf[TMPBUFSIZ];
  2891. bool io_open = false;
  2892. int count = 0;
  2893. root = api_usb_stats(&count);
  2894. #endif
  2895. if (!root) {
  2896. message(io_data, MSG_NOUSTA, 0, NULL, isjson);
  2897. return;
  2898. }
  2899. #ifdef USE_USBUTILS
  2900. message(io_data, MSG_USBSTA, 0, NULL, isjson);
  2901. if (isjson)
  2902. io_open = io_add(io_data, COMSTR JSON_USBSTATS);
  2903. root = print_data(root, buf, isjson, false);
  2904. io_add(io_data, buf);
  2905. while (42) {
  2906. root = api_usb_stats(&count);
  2907. if (!root)
  2908. break;
  2909. root = print_data(root, buf, isjson, isjson);
  2910. io_add(io_data, buf);
  2911. }
  2912. if (isjson && io_open)
  2913. io_close(io_data);
  2914. #endif
  2915. }
  2916. #ifdef HAVE_AN_FPGA
  2917. static void pgaset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2918. {
  2919. struct cgpu_info *cgpu;
  2920. struct device_drv *drv;
  2921. char buf[TMPBUFSIZ];
  2922. int numpga = numpgas();
  2923. if (numpga == 0) {
  2924. message(io_data, MSG_PGANON, 0, NULL, isjson);
  2925. return;
  2926. }
  2927. if (param == NULL || *param == '\0') {
  2928. message(io_data, MSG_MISID, 0, NULL, isjson);
  2929. return;
  2930. }
  2931. char *opt = strchr(param, ',');
  2932. if (opt)
  2933. *(opt++) = '\0';
  2934. if (!opt || !*opt) {
  2935. message(io_data, MSG_MISPGAOPT, 0, NULL, isjson);
  2936. return;
  2937. }
  2938. int id = atoi(param);
  2939. if (id < 0 || id >= numpga) {
  2940. message(io_data, MSG_INVPGA, id, NULL, isjson);
  2941. return;
  2942. }
  2943. int dev = pgadevice(id);
  2944. if (dev < 0) { // Should never happen
  2945. message(io_data, MSG_INVPGA, id, NULL, isjson);
  2946. return;
  2947. }
  2948. cgpu = get_devices(dev);
  2949. drv = cgpu->drv;
  2950. char *set = strchr(opt, ',');
  2951. if (set)
  2952. *(set++) = '\0';
  2953. if (!drv->set_device)
  2954. message(io_data, MSG_PGANOSET, id, NULL, isjson);
  2955. else {
  2956. char *ret = drv->set_device(cgpu, opt, set, buf);
  2957. if (ret) {
  2958. if (strcasecmp(opt, "help") == 0)
  2959. message(io_data, MSG_PGAHELP, id, ret, isjson);
  2960. else
  2961. message(io_data, MSG_PGASETERR, id, ret, isjson);
  2962. } else
  2963. message(io_data, MSG_PGASETOK, id, NULL, isjson);
  2964. }
  2965. }
  2966. #endif
  2967. static void dozero(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2968. {
  2969. if (param == NULL || *param == '\0') {
  2970. message(io_data, MSG_ZERMIS, 0, NULL, isjson);
  2971. return;
  2972. }
  2973. char *sum = strchr(param, ',');
  2974. if (sum)
  2975. *(sum++) = '\0';
  2976. if (!sum || !*sum) {
  2977. message(io_data, MSG_MISBOOL, 0, NULL, isjson);
  2978. return;
  2979. }
  2980. bool all = false;
  2981. bool bs = false;
  2982. if (strcasecmp(param, "all") == 0)
  2983. all = true;
  2984. else if (strcasecmp(param, "bestshare") == 0)
  2985. bs = true;
  2986. if (all == false && bs == false) {
  2987. message(io_data, MSG_ZERINV, 0, param, isjson);
  2988. return;
  2989. }
  2990. *sum = tolower(*sum);
  2991. if (*sum != 't' && *sum != 'f') {
  2992. message(io_data, MSG_INVBOOL, 0, NULL, isjson);
  2993. return;
  2994. }
  2995. bool dosum = (*sum == 't');
  2996. if (dosum)
  2997. print_summary();
  2998. if (all)
  2999. zero_stats();
  3000. if (bs)
  3001. zero_bestshare();
  3002. if (dosum)
  3003. message(io_data, MSG_ZERSUM, 0, all ? "All" : "BestShare", isjson);
  3004. else
  3005. message(io_data, MSG_ZERNOSUM, 0, all ? "All" : "BestShare", isjson);
  3006. }
  3007. static void dohotplug(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  3008. {
  3009. #ifdef USE_USBUTILS
  3010. int value;
  3011. if (param == NULL || *param == '\0') {
  3012. message(io_data, MSG_MISHPLG, 0, NULL, isjson);
  3013. return;
  3014. }
  3015. value = atoi(param);
  3016. if (value < 0 || value > 9999) {
  3017. message(io_data, MSG_INVHPLG, 0, param, isjson);
  3018. return;
  3019. }
  3020. hotplug_time = value;
  3021. if (value)
  3022. message(io_data, MSG_HOTPLUG, value, NULL, isjson);
  3023. else
  3024. message(io_data, MSG_DISHPLG, 0, NULL, isjson);
  3025. #else
  3026. message(io_data, MSG_NOHPLG, 0, NULL, isjson);
  3027. return;
  3028. #endif
  3029. }
  3030. #ifdef HAVE_AN_ASIC
  3031. static void ascdev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  3032. {
  3033. bool io_open = false;
  3034. int numasc = numascs();
  3035. int id;
  3036. if (numasc == 0) {
  3037. message(io_data, MSG_ASCNON, 0, NULL, isjson);
  3038. return;
  3039. }
  3040. if (param == NULL || *param == '\0') {
  3041. message(io_data, MSG_MISID, 0, NULL, isjson);
  3042. return;
  3043. }
  3044. id = atoi(param);
  3045. if (id < 0 || id >= numasc) {
  3046. message(io_data, MSG_INVASC, id, NULL, isjson);
  3047. return;
  3048. }
  3049. message(io_data, MSG_ASCDEV, id, NULL, isjson);
  3050. if (isjson)
  3051. io_open = io_add(io_data, COMSTR JSON_ASC);
  3052. ascstatus(io_data, id, isjson, false);
  3053. if (isjson && io_open)
  3054. io_close(io_data);
  3055. }
  3056. static void ascenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  3057. {
  3058. struct cgpu_info *cgpu;
  3059. int numasc = numascs();
  3060. struct thr_info *thr;
  3061. int asc;
  3062. int id;
  3063. int i;
  3064. if (numasc == 0) {
  3065. message(io_data, MSG_ASCNON, 0, NULL, isjson);
  3066. return;
  3067. }
  3068. if (param == NULL || *param == '\0') {
  3069. message(io_data, MSG_MISID, 0, NULL, isjson);
  3070. return;
  3071. }
  3072. id = atoi(param);
  3073. if (id < 0 || id >= numasc) {
  3074. message(io_data, MSG_INVASC, id, NULL, isjson);
  3075. return;
  3076. }
  3077. int dev = ascdevice(id);
  3078. if (dev < 0) { // Should never happen
  3079. message(io_data, MSG_INVASC, id, NULL, isjson);
  3080. return;
  3081. }
  3082. cgpu = get_devices(dev);
  3083. applog(LOG_DEBUG, "API: request to ascenable ascid %d device %d %s%u",
  3084. id, dev, cgpu->drv->name, cgpu->device_id);
  3085. if (cgpu->deven != DEV_DISABLED) {
  3086. message(io_data, MSG_ASCLRENA, id, NULL, isjson);
  3087. return;
  3088. }
  3089. #if 0 /* A DISABLED device wont change status FIXME: should disabling make it WELL? */
  3090. if (cgpu->status != LIFE_WELL) {
  3091. message(io_data, MSG_ASCUNW, id, NULL, isjson);
  3092. return;
  3093. }
  3094. #endif
  3095. #ifdef USE_USBUTILS
  3096. if (cgpu->usbinfo.nodev) {
  3097. message(io_data, MSG_ASCUSBNODEV, id, NULL, isjson);
  3098. return;
  3099. }
  3100. #endif
  3101. for (i = 0; i < mining_threads; i++) {
  3102. thr = get_thread(i);
  3103. asc = thr->cgpu->cgminer_id;
  3104. if (asc == dev) {
  3105. cgpu->deven = DEV_ENABLED;
  3106. applog(LOG_DEBUG, "API: Pushing sem post to thread %d", thr->id);
  3107. cgsem_post(&thr->sem);
  3108. }
  3109. }
  3110. message(io_data, MSG_ASCENA, id, NULL, isjson);
  3111. }
  3112. static void ascdisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  3113. {
  3114. struct cgpu_info *cgpu;
  3115. int numasc = numascs();
  3116. int id;
  3117. if (numasc == 0) {
  3118. message(io_data, MSG_ASCNON, 0, NULL, isjson);
  3119. return;
  3120. }
  3121. if (param == NULL || *param == '\0') {
  3122. message(io_data, MSG_MISID, 0, NULL, isjson);
  3123. return;
  3124. }
  3125. id = atoi(param);
  3126. if (id < 0 || id >= numasc) {
  3127. message(io_data, MSG_INVASC, id, NULL, isjson);
  3128. return;
  3129. }
  3130. int dev = ascdevice(id);
  3131. if (dev < 0) { // Should never happen
  3132. message(io_data, MSG_INVASC, id, NULL, isjson);
  3133. return;
  3134. }
  3135. cgpu = get_devices(dev);
  3136. applog(LOG_DEBUG, "API: request to ascdisable ascid %d device %d %s%u",
  3137. id, dev, cgpu->drv->name, cgpu->device_id);
  3138. if (cgpu->deven == DEV_DISABLED) {
  3139. message(io_data, MSG_ASCLRDIS, id, NULL, isjson);
  3140. return;
  3141. }
  3142. cgpu->deven = DEV_DISABLED;
  3143. message(io_data, MSG_ASCDIS, id, NULL, isjson);
  3144. }
  3145. static void ascidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  3146. {
  3147. struct cgpu_info *cgpu;
  3148. struct device_drv *drv;
  3149. int numasc = numascs();
  3150. int id;
  3151. if (numasc == 0) {
  3152. message(io_data, MSG_ASCNON, 0, NULL, isjson);
  3153. return;
  3154. }
  3155. if (param == NULL || *param == '\0') {
  3156. message(io_data, MSG_MISID, 0, NULL, isjson);
  3157. return;
  3158. }
  3159. id = atoi(param);
  3160. if (id < 0 || id >= numasc) {
  3161. message(io_data, MSG_INVASC, id, NULL, isjson);
  3162. return;
  3163. }
  3164. int dev = ascdevice(id);
  3165. if (dev < 0) { // Should never happen
  3166. message(io_data, MSG_INVASC, id, NULL, isjson);
  3167. return;
  3168. }
  3169. cgpu = get_devices(dev);
  3170. drv = cgpu->drv;
  3171. if (!drv->identify_device)
  3172. message(io_data, MSG_ASCNOID, id, NULL, isjson);
  3173. else {
  3174. drv->identify_device(cgpu);
  3175. message(io_data, MSG_ASCIDENT, id, NULL, isjson);
  3176. }
  3177. }
  3178. #endif
  3179. static void asccount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  3180. {
  3181. struct api_data *root = NULL;
  3182. char buf[TMPBUFSIZ];
  3183. bool io_open;
  3184. int count = 0;
  3185. #ifdef HAVE_AN_ASIC
  3186. count = numascs();
  3187. #endif
  3188. message(io_data, MSG_NUMASC, 0, NULL, isjson);
  3189. io_open = io_add(io_data, isjson ? COMSTR JSON_ASCS : _ASCS COMSTR);
  3190. root = api_add_int(root, "Count", &count, false);
  3191. root = print_data(root, buf, isjson, false);
  3192. io_add(io_data, buf);
  3193. if (isjson && io_open)
  3194. io_close(io_data);
  3195. }
  3196. #ifdef HAVE_AN_ASIC
  3197. static void ascset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  3198. {
  3199. struct cgpu_info *cgpu;
  3200. struct device_drv *drv;
  3201. char buf[TMPBUFSIZ];
  3202. int numasc = numascs();
  3203. if (numasc == 0) {
  3204. message(io_data, MSG_ASCNON, 0, NULL, isjson);
  3205. return;
  3206. }
  3207. if (param == NULL || *param == '\0') {
  3208. message(io_data, MSG_MISID, 0, NULL, isjson);
  3209. return;
  3210. }
  3211. char *opt = strchr(param, ',');
  3212. if (opt)
  3213. *(opt++) = '\0';
  3214. if (!opt || !*opt) {
  3215. message(io_data, MSG_MISASCOPT, 0, NULL, isjson);
  3216. return;
  3217. }
  3218. int id = atoi(param);
  3219. if (id < 0 || id >= numasc) {
  3220. message(io_data, MSG_INVASC, id, NULL, isjson);
  3221. return;
  3222. }
  3223. int dev = ascdevice(id);
  3224. if (dev < 0) { // Should never happen
  3225. message(io_data, MSG_INVASC, id, NULL, isjson);
  3226. return;
  3227. }
  3228. cgpu = get_devices(dev);
  3229. drv = cgpu->drv;
  3230. char *set = strchr(opt, ',');
  3231. if (set)
  3232. *(set++) = '\0';
  3233. if (!drv->set_device)
  3234. message(io_data, MSG_ASCNOSET, id, NULL, isjson);
  3235. else {
  3236. char *ret = drv->set_device(cgpu, opt, set, buf);
  3237. if (ret) {
  3238. if (strcasecmp(opt, "help") == 0)
  3239. message(io_data, MSG_ASCHELP, id, ret, isjson);
  3240. else
  3241. message(io_data, MSG_ASCSETERR, id, ret, isjson);
  3242. } else
  3243. message(io_data, MSG_ASCSETOK, id, NULL, isjson);
  3244. }
  3245. }
  3246. #endif
  3247. static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group);
  3248. struct CMDS {
  3249. char *name;
  3250. void (*func)(struct io_data *, SOCKETTYPE, char *, bool, char);
  3251. bool iswritemode;
  3252. } cmds[] = {
  3253. { "version", apiversion, false },
  3254. { "config", minerconfig, false },
  3255. { "devs", devstatus, false },
  3256. { "pools", poolstatus, false },
  3257. { "summary", summary, false },
  3258. #ifdef HAVE_OPENCL
  3259. { "gpuenable", gpuenable, true },
  3260. { "gpudisable", gpudisable, true },
  3261. { "gpurestart", gpurestart, true },
  3262. { "gpu", gpudev, false },
  3263. #endif
  3264. #ifdef HAVE_AN_FPGA
  3265. { "pga", pgadev, false },
  3266. { "pgaenable", pgaenable, true },
  3267. { "pgadisable", pgadisable, true },
  3268. { "pgaidentify", pgaidentify, true },
  3269. #endif
  3270. { "gpucount", gpucount, false },
  3271. { "pgacount", pgacount, false },
  3272. { "switchpool", switchpool, true },
  3273. { "addpool", addpool, true },
  3274. { "poolpriority", poolpriority, true },
  3275. { "poolquota", poolquota, true },
  3276. { "enablepool", enablepool, true },
  3277. { "disablepool", disablepool, true },
  3278. { "removepool", removepool, true },
  3279. #ifdef HAVE_OPENCL
  3280. { "gpuintensity", gpuintensity, true },
  3281. { "gpumem", gpumem, true },
  3282. { "gpuengine", gpuengine, true },
  3283. { "gpufan", gpufan, true },
  3284. { "gpuvddc", gpuvddc, true },
  3285. #endif
  3286. { "save", dosave, true },
  3287. { "quit", doquit, true },
  3288. { "privileged", privileged, true },
  3289. { "notify", notify, false },
  3290. { "devdetails", devdetails, false },
  3291. { "restart", dorestart, true },
  3292. { "stats", minerstats, false },
  3293. { "check", checkcommand, false },
  3294. { "failover-only", failoveronly, true },
  3295. { "coin", minecoin, false },
  3296. { "debug", debugstate, true },
  3297. { "setconfig", setconfig, true },
  3298. { "usbstats", usbstats, false },
  3299. #ifdef HAVE_AN_FPGA
  3300. { "pgaset", pgaset, true },
  3301. #endif
  3302. { "zero", dozero, true },
  3303. { "hotplug", dohotplug, true },
  3304. #ifdef HAVE_AN_ASIC
  3305. { "asc", ascdev, false },
  3306. { "ascenable", ascenable, true },
  3307. { "ascdisable", ascdisable, true },
  3308. { "ascidentify", ascidentify, true },
  3309. { "ascset", ascset, true },
  3310. #endif
  3311. { "asccount", asccount, false },
  3312. { NULL, NULL, false }
  3313. };
  3314. static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
  3315. {
  3316. struct api_data *root = NULL;
  3317. char buf[TMPBUFSIZ];
  3318. bool io_open;
  3319. char cmdbuf[100];
  3320. bool found, access;
  3321. int i;
  3322. if (param == NULL || *param == '\0') {
  3323. message(io_data, MSG_MISCHK, 0, NULL, isjson);
  3324. return;
  3325. }
  3326. found = false;
  3327. access = false;
  3328. for (i = 0; cmds[i].name != NULL; i++) {
  3329. if (strcmp(cmds[i].name, param) == 0) {
  3330. found = true;
  3331. sprintf(cmdbuf, "|%s|", param);
  3332. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  3333. access = true;
  3334. break;
  3335. }
  3336. }
  3337. message(io_data, MSG_CHECK, 0, NULL, isjson);
  3338. io_open = io_add(io_data, isjson ? COMSTR JSON_CHECK : _CHECK COMSTR);
  3339. root = api_add_const(root, "Exists", found ? YES : NO, false);
  3340. root = api_add_const(root, "Access", access ? YES : NO, false);
  3341. root = print_data(root, buf, isjson, false);
  3342. io_add(io_data, buf);
  3343. if (isjson && io_open)
  3344. io_close(io_data);
  3345. }
  3346. static void send_result(struct io_data *io_data, SOCKETTYPE c, bool isjson)
  3347. {
  3348. char buf[SOCKBUFSIZ + sizeof(JSON_CLOSE) + sizeof(JSON_END)];
  3349. int count, res, tosend, len, n;
  3350. strcpy(buf, io_data->ptr);
  3351. if (io_data->close)
  3352. strcat(buf, JSON_CLOSE);
  3353. if (isjson) {
  3354. if (io_data->full)
  3355. strcat(buf, JSON_END_TRUNCATED);
  3356. else
  3357. strcat(buf, JSON_END);
  3358. }
  3359. len = strlen(buf);
  3360. tosend = len+1;
  3361. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", tosend, buf, len > 10 ? "..." : BLANK);
  3362. count = 0;
  3363. while (count++ < 5 && tosend > 0) {
  3364. // allow 50ms per attempt
  3365. struct timeval timeout = {0, 50000};
  3366. fd_set wd;
  3367. FD_ZERO(&wd);
  3368. FD_SET(c, &wd);
  3369. if ((res = select(c + 1, NULL, &wd, NULL, &timeout)) < 1) {
  3370. applog(LOG_WARNING, "API: send select failed (%d)", res);
  3371. return;
  3372. }
  3373. n = send(c, buf, tosend, 0);
  3374. if (SOCKETFAIL(n)) {
  3375. if (sock_blocks())
  3376. continue;
  3377. applog(LOG_WARNING, "API: send (%d) failed: %s", tosend, SOCKERRMSG);
  3378. return;
  3379. } else {
  3380. if (count <= 1) {
  3381. if (n == tosend)
  3382. applog(LOG_DEBUG, "API: sent all of %d first go", tosend);
  3383. else
  3384. applog(LOG_DEBUG, "API: sent %d of %d first go", n, tosend);
  3385. } else {
  3386. if (n == tosend)
  3387. applog(LOG_DEBUG, "API: sent all of remaining %d (count=%d)", tosend, count);
  3388. else
  3389. applog(LOG_DEBUG, "API: sent %d of remaining %d (count=%d)", n, tosend, count);
  3390. }
  3391. tosend -= n;
  3392. }
  3393. }
  3394. }
  3395. static void tidyup(__maybe_unused void *arg)
  3396. {
  3397. mutex_lock(&quit_restart_lock);
  3398. SOCKETTYPE *apisock = (SOCKETTYPE *)arg;
  3399. bye = true;
  3400. if (*apisock != INVSOCK) {
  3401. shutdown(*apisock, SHUT_RDWR);
  3402. CLOSESOCKET(*apisock);
  3403. *apisock = INVSOCK;
  3404. }
  3405. if (ipaccess != NULL) {
  3406. free(ipaccess);
  3407. ipaccess = NULL;
  3408. }
  3409. io_free();
  3410. mutex_unlock(&quit_restart_lock);
  3411. }
  3412. /*
  3413. * Interpret --api-groups G:cmd1:cmd2:cmd3,P:cmd4,*,...
  3414. */
  3415. static void setup_groups()
  3416. {
  3417. char *api_groups = opt_api_groups ? opt_api_groups : (char *)BLANK;
  3418. char *buf, *ptr, *next, *colon;
  3419. char group;
  3420. char commands[TMPBUFSIZ];
  3421. char cmdbuf[100];
  3422. char *cmd;
  3423. bool addstar, did;
  3424. int i;
  3425. buf = malloc(strlen(api_groups) + 1);
  3426. if (unlikely(!buf))
  3427. quit(1, "Failed to malloc ipgroups buf");
  3428. strcpy(buf, api_groups);
  3429. next = buf;
  3430. // for each group defined
  3431. while (next && *next) {
  3432. ptr = next;
  3433. next = strchr(ptr, ',');
  3434. if (next)
  3435. *(next++) = '\0';
  3436. // Validate the group
  3437. if (*(ptr+1) != ':') {
  3438. colon = strchr(ptr, ':');
  3439. if (colon)
  3440. *colon = '\0';
  3441. quit(1, "API invalid group name '%s'", ptr);
  3442. }
  3443. group = GROUP(*ptr);
  3444. if (!VALIDGROUP(group))
  3445. quit(1, "API invalid group name '%c'", *ptr);
  3446. if (group == PRIVGROUP)
  3447. quit(1, "API group name can't be '%c'", PRIVGROUP);
  3448. if (group == NOPRIVGROUP)
  3449. quit(1, "API group name can't be '%c'", NOPRIVGROUP);
  3450. if (apigroups[GROUPOFFSET(group)].commands != NULL)
  3451. quit(1, "API duplicate group name '%c'", *ptr);
  3452. ptr += 2;
  3453. // Validate the command list (and handle '*')
  3454. cmd = &(commands[0]);
  3455. *(cmd++) = SEPARATOR;
  3456. *cmd = '\0';
  3457. addstar = false;
  3458. while (ptr && *ptr) {
  3459. colon = strchr(ptr, ':');
  3460. if (colon)
  3461. *(colon++) = '\0';
  3462. if (strcmp(ptr, "*") == 0)
  3463. addstar = true;
  3464. else {
  3465. did = false;
  3466. for (i = 0; cmds[i].name != NULL; i++) {
  3467. if (strcasecmp(ptr, cmds[i].name) == 0) {
  3468. did = true;
  3469. break;
  3470. }
  3471. }
  3472. if (did) {
  3473. // skip duplicates
  3474. sprintf(cmdbuf, "|%s|", cmds[i].name);
  3475. if (strstr(commands, cmdbuf) == NULL) {
  3476. strcpy(cmd, cmds[i].name);
  3477. cmd += strlen(cmds[i].name);
  3478. *(cmd++) = SEPARATOR;
  3479. *cmd = '\0';
  3480. }
  3481. } else {
  3482. quit(1, "API unknown command '%s' in group '%c'", ptr, group);
  3483. }
  3484. }
  3485. ptr = colon;
  3486. }
  3487. // * = allow all non-iswritemode commands
  3488. if (addstar) {
  3489. for (i = 0; cmds[i].name != NULL; i++) {
  3490. if (cmds[i].iswritemode == false) {
  3491. // skip duplicates
  3492. sprintf(cmdbuf, "|%s|", cmds[i].name);
  3493. if (strstr(commands, cmdbuf) == NULL) {
  3494. strcpy(cmd, cmds[i].name);
  3495. cmd += strlen(cmds[i].name);
  3496. *(cmd++) = SEPARATOR;
  3497. *cmd = '\0';
  3498. }
  3499. }
  3500. }
  3501. }
  3502. ptr = apigroups[GROUPOFFSET(group)].commands = malloc(strlen(commands) + 1);
  3503. if (unlikely(!ptr))
  3504. quit(1, "Failed to malloc group commands buf");
  3505. strcpy(ptr, commands);
  3506. }
  3507. // Now define R (NOPRIVGROUP) as all non-iswritemode commands
  3508. cmd = &(commands[0]);
  3509. *(cmd++) = SEPARATOR;
  3510. *cmd = '\0';
  3511. for (i = 0; cmds[i].name != NULL; i++) {
  3512. if (cmds[i].iswritemode == false) {
  3513. strcpy(cmd, cmds[i].name);
  3514. cmd += strlen(cmds[i].name);
  3515. *(cmd++) = SEPARATOR;
  3516. *cmd = '\0';
  3517. }
  3518. }
  3519. ptr = apigroups[GROUPOFFSET(NOPRIVGROUP)].commands = malloc(strlen(commands) + 1);
  3520. if (unlikely(!ptr))
  3521. quit(1, "Failed to malloc noprivgroup commands buf");
  3522. strcpy(ptr, commands);
  3523. // W (PRIVGROUP) is handled as a special case since it simply means all commands
  3524. free(buf);
  3525. return;
  3526. }
  3527. /*
  3528. * Interpret [W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  3529. * special case of 0/0 allows /0 (means all IP addresses)
  3530. */
  3531. #define ALLIP4 "0/0"
  3532. /*
  3533. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  3534. */
  3535. static void setup_ipaccess()
  3536. {
  3537. char *buf, *ptr, *comma, *slash, *dot;
  3538. int ipcount, mask, octet, i;
  3539. char group;
  3540. buf = malloc(strlen(opt_api_allow) + 1);
  3541. if (unlikely(!buf))
  3542. quit(1, "Failed to malloc ipaccess buf");
  3543. strcpy(buf, opt_api_allow);
  3544. ipcount = 1;
  3545. ptr = buf;
  3546. while (*ptr)
  3547. if (*(ptr++) == ',')
  3548. ipcount++;
  3549. // possibly more than needed, but never less
  3550. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  3551. if (unlikely(!ipaccess))
  3552. quit(1, "Failed to calloc ipaccess");
  3553. ips = 0;
  3554. ptr = buf;
  3555. while (ptr && *ptr) {
  3556. while (*ptr == ' ' || *ptr == '\t')
  3557. ptr++;
  3558. if (*ptr == ',') {
  3559. ptr++;
  3560. continue;
  3561. }
  3562. comma = strchr(ptr, ',');
  3563. if (comma)
  3564. *(comma++) = '\0';
  3565. group = NOPRIVGROUP;
  3566. if (isalpha(*ptr) && *(ptr+1) == ':') {
  3567. if (DEFINEDGROUP(*ptr))
  3568. group = GROUP(*ptr);
  3569. ptr += 2;
  3570. }
  3571. ipaccess[ips].group = group;
  3572. if (strcmp(ptr, ALLIP4) == 0)
  3573. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  3574. else {
  3575. slash = strchr(ptr, '/');
  3576. if (!slash)
  3577. ipaccess[ips].mask = 0xffffffff;
  3578. else {
  3579. *(slash++) = '\0';
  3580. mask = atoi(slash);
  3581. if (mask < 1 || mask > 32)
  3582. goto popipo; // skip invalid/zero
  3583. ipaccess[ips].mask = 0;
  3584. while (mask-- >= 0) {
  3585. octet = 1 << (mask % 8);
  3586. ipaccess[ips].mask |= (octet << (24 - (8 * (mask >> 3))));
  3587. }
  3588. }
  3589. ipaccess[ips].ip = 0; // missing default to '.0'
  3590. for (i = 0; ptr && (i < 4); i++) {
  3591. dot = strchr(ptr, '.');
  3592. if (dot)
  3593. *(dot++) = '\0';
  3594. octet = atoi(ptr);
  3595. if (octet < 0 || octet > 0xff)
  3596. goto popipo; // skip invalid
  3597. ipaccess[ips].ip |= (octet << (24 - (i * 8)));
  3598. ptr = dot;
  3599. }
  3600. ipaccess[ips].ip &= ipaccess[ips].mask;
  3601. }
  3602. ips++;
  3603. popipo:
  3604. ptr = comma;
  3605. }
  3606. free(buf);
  3607. }
  3608. static void *quit_thread(__maybe_unused void *userdata)
  3609. {
  3610. // allow thread creator to finish whatever it's doing
  3611. mutex_lock(&quit_restart_lock);
  3612. mutex_unlock(&quit_restart_lock);
  3613. if (opt_debug)
  3614. applog(LOG_DEBUG, "API: killing cgminer");
  3615. kill_work();
  3616. return NULL;
  3617. }
  3618. static void *restart_thread(__maybe_unused void *userdata)
  3619. {
  3620. // allow thread creator to finish whatever it's doing
  3621. mutex_lock(&quit_restart_lock);
  3622. mutex_unlock(&quit_restart_lock);
  3623. if (opt_debug)
  3624. applog(LOG_DEBUG, "API: restarting cgminer");
  3625. app_restart();
  3626. return NULL;
  3627. }
  3628. static bool check_connect(struct sockaddr_in *cli, char **connectaddr, char *group)
  3629. {
  3630. bool addrok = false;
  3631. int i;
  3632. *connectaddr = inet_ntoa(cli->sin_addr);
  3633. *group = NOPRIVGROUP;
  3634. if (opt_api_allow) {
  3635. int client_ip = htonl(cli->sin_addr.s_addr);
  3636. for (i = 0; i < ips; i++) {
  3637. if ((client_ip & ipaccess[i].mask) == ipaccess[i].ip) {
  3638. addrok = true;
  3639. *group = ipaccess[i].group;
  3640. break;
  3641. }
  3642. }
  3643. } else {
  3644. if (opt_api_network)
  3645. addrok = true;
  3646. else
  3647. addrok = (strcmp(*connectaddr, localaddr) == 0);
  3648. }
  3649. return addrok;
  3650. }
  3651. static void mcast()
  3652. {
  3653. struct sockaddr_in listen;
  3654. struct ip_mreq grp;
  3655. struct sockaddr_in came_from;
  3656. time_t bindstart;
  3657. char *binderror;
  3658. SOCKETTYPE mcast_sock;
  3659. SOCKETTYPE reply_sock;
  3660. socklen_t came_from_siz;
  3661. char *connectaddr;
  3662. ssize_t rep;
  3663. int bound;
  3664. int count;
  3665. int reply_port;
  3666. bool addrok;
  3667. char group;
  3668. char expect[] = "cgminer-"; // first 8 bytes constant
  3669. char *expect_code;
  3670. size_t expect_code_len;
  3671. char buf[1024];
  3672. char replybuf[1024];
  3673. memset(&grp, 0, sizeof(grp));
  3674. grp.imr_multiaddr.s_addr = inet_addr(opt_api_mcast_addr);
  3675. if (grp.imr_multiaddr.s_addr == INADDR_NONE)
  3676. quit(1, "Invalid Multicast Address");
  3677. grp.imr_interface.s_addr = INADDR_ANY;
  3678. mcast_sock = socket(AF_INET, SOCK_DGRAM, 0);
  3679. int optval = 1;
  3680. if (SOCKETFAIL(setsockopt(mcast_sock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval)))) {
  3681. applog(LOG_ERR, "API mcast setsockopt SO_REUSEADDR failed (%s)%s", SOCKERRMSG, MUNAVAILABLE);
  3682. goto die;
  3683. }
  3684. memset(&listen, 0, sizeof(listen));
  3685. listen.sin_family = AF_INET;
  3686. listen.sin_addr.s_addr = INADDR_ANY;
  3687. listen.sin_port = htons(opt_api_mcast_port);
  3688. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  3689. bound = 0;
  3690. bindstart = time(NULL);
  3691. while (bound == 0) {
  3692. if (SOCKETFAIL(bind(mcast_sock, (struct sockaddr *)(&listen), sizeof(listen)))) {
  3693. binderror = SOCKERRMSG;
  3694. if ((time(NULL) - bindstart) > 61)
  3695. break;
  3696. else
  3697. cgsleep_ms(30000);
  3698. } else
  3699. bound = 1;
  3700. }
  3701. if (bound == 0) {
  3702. applog(LOG_ERR, "API mcast bind to port %d failed (%s)%s", opt_api_port, binderror, MUNAVAILABLE);
  3703. goto die;
  3704. }
  3705. if (SOCKETFAIL(setsockopt(mcast_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)(&grp), sizeof(grp)))) {
  3706. applog(LOG_ERR, "API mcast join failed (%s)%s", SOCKERRMSG, MUNAVAILABLE);
  3707. goto die;
  3708. }
  3709. expect_code_len = sizeof(expect) + strlen(opt_api_mcast_code);
  3710. expect_code = malloc(expect_code_len+1);
  3711. if (!expect_code)
  3712. quit(1, "Failed to malloc mcast expect_code");
  3713. snprintf(expect_code, expect_code_len+1, "%s%s-", expect, opt_api_mcast_code);
  3714. count = 0;
  3715. while (80085) {
  3716. cgsleep_ms(1000);
  3717. count++;
  3718. came_from_siz = sizeof(came_from);
  3719. if (SOCKETFAIL(rep = recvfrom(mcast_sock, buf, sizeof(buf),
  3720. 0, (struct sockaddr *)(&came_from), &came_from_siz))) {
  3721. applog(LOG_DEBUG, "API mcast failed count=%d (%s) (%d)",
  3722. count, SOCKERRMSG, (int)mcast_sock);
  3723. continue;
  3724. }
  3725. addrok = check_connect(&came_from, &connectaddr, &group);
  3726. applog(LOG_DEBUG, "API mcast from %s - %s",
  3727. connectaddr, addrok ? "Accepted" : "Ignored");
  3728. if (!addrok)
  3729. continue;
  3730. buf[rep] = '\0';
  3731. if (rep > 0 && buf[rep-1] == '\n')
  3732. buf[--rep] = '\0';
  3733. applog(LOG_DEBUG, "API mcast request rep=%d (%s) from %s:%d",
  3734. (int)rep, buf,
  3735. inet_ntoa(came_from.sin_addr),
  3736. ntohs(came_from.sin_port));
  3737. if ((size_t)rep > expect_code_len && memcmp(buf, expect_code, expect_code_len) == 0) {
  3738. reply_port = atoi(&buf[expect_code_len]);
  3739. if (reply_port < 1 || reply_port > 65535) {
  3740. applog(LOG_DEBUG, "API mcast request ignored - invalid port (%s)",
  3741. &buf[expect_code_len]);
  3742. } else {
  3743. applog(LOG_DEBUG, "API mcast request OK port %s=%d",
  3744. &buf[expect_code_len], reply_port);
  3745. came_from.sin_port = htons(reply_port);
  3746. reply_sock = socket(AF_INET, SOCK_DGRAM, 0);
  3747. snprintf(replybuf, sizeof(replybuf),
  3748. "cgm-" API_MCAST_CODE "-%d-%s",
  3749. opt_api_port, opt_api_mcast_des);
  3750. rep = sendto(reply_sock, replybuf, strlen(replybuf)+1,
  3751. 0, (struct sockaddr *)(&came_from),
  3752. sizeof(came_from));
  3753. if (SOCKETFAIL(rep)) {
  3754. applog(LOG_DEBUG, "API mcast send reply failed (%s) (%d)",
  3755. SOCKERRMSG, (int)reply_sock);
  3756. } else {
  3757. applog(LOG_DEBUG, "API mcast send reply (%s) succeeded (%d) (%d)",
  3758. replybuf, (int)rep, (int)reply_sock);
  3759. }
  3760. CLOSESOCKET(reply_sock);
  3761. }
  3762. } else
  3763. applog(LOG_DEBUG, "API mcast request was no good");
  3764. }
  3765. die:
  3766. CLOSESOCKET(mcast_sock);
  3767. }
  3768. static void *mcast_thread(void *userdata)
  3769. {
  3770. struct thr_info *mythr = userdata;
  3771. pthread_detach(pthread_self());
  3772. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  3773. RenameThread("api_mcast");
  3774. mcast();
  3775. PTH(mythr) = 0L;
  3776. return NULL;
  3777. }
  3778. void mcast_init()
  3779. {
  3780. struct thr_info *thr;
  3781. thr = calloc(1, sizeof(*thr));
  3782. if (!thr)
  3783. quit(1, "Failed to calloc mcast thr");
  3784. if (thr_info_create(thr, NULL, mcast_thread, thr))
  3785. quit(1, "API mcast thread create failed");
  3786. }
  3787. void api(int api_thr_id)
  3788. {
  3789. struct io_data *io_data;
  3790. struct thr_info bye_thr;
  3791. char buf[TMPBUFSIZ];
  3792. char param_buf[TMPBUFSIZ];
  3793. SOCKETTYPE c;
  3794. int n, bound;
  3795. char *connectaddr;
  3796. char *binderror;
  3797. time_t bindstart;
  3798. short int port = opt_api_port;
  3799. struct sockaddr_in serv;
  3800. struct sockaddr_in cli;
  3801. socklen_t clisiz;
  3802. char cmdbuf[100];
  3803. char *cmd;
  3804. char *param;
  3805. bool addrok;
  3806. char group;
  3807. json_error_t json_err;
  3808. json_t *json_config;
  3809. json_t *json_val;
  3810. bool isjson;
  3811. bool did;
  3812. int i;
  3813. SOCKETTYPE *apisock;
  3814. apisock = malloc(sizeof(*apisock));
  3815. *apisock = INVSOCK;
  3816. if (!opt_api_listen) {
  3817. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  3818. return;
  3819. }
  3820. io_data = sock_io_new();
  3821. mutex_init(&quit_restart_lock);
  3822. pthread_cleanup_push(tidyup, (void *)apisock);
  3823. my_thr_id = api_thr_id;
  3824. setup_groups();
  3825. if (opt_api_allow) {
  3826. setup_ipaccess();
  3827. if (ips == 0) {
  3828. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  3829. return;
  3830. }
  3831. }
  3832. /* This should be done before curl in needed
  3833. * to ensure curl has already called WSAStartup() in windows */
  3834. cgsleep_ms(opt_log_interval*1000);
  3835. *apisock = socket(AF_INET, SOCK_STREAM, 0);
  3836. if (*apisock == INVSOCK) {
  3837. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3838. return;
  3839. }
  3840. memset(&serv, 0, sizeof(serv));
  3841. serv.sin_family = AF_INET;
  3842. if (!opt_api_allow && !opt_api_network) {
  3843. serv.sin_addr.s_addr = inet_addr(localaddr);
  3844. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  3845. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3846. return;
  3847. }
  3848. }
  3849. serv.sin_port = htons(port);
  3850. #ifndef WIN32
  3851. // On linux with SO_REUSEADDR, bind will get the port if the previous
  3852. // socket is closed (even if it is still in TIME_WAIT) but fail if
  3853. // another program has it open - which is what we want
  3854. int optval = 1;
  3855. // If it doesn't work, we don't really care - just show a debug message
  3856. if (SOCKETFAIL(setsockopt(*apisock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval))))
  3857. applog(LOG_DEBUG, "API setsockopt SO_REUSEADDR failed (ignored): %s", SOCKERRMSG);
  3858. #else
  3859. // On windows a 2nd program can bind to a port>1024 already in use unless
  3860. // SO_EXCLUSIVEADDRUSE is used - however then the bind to a closed port
  3861. // in TIME_WAIT will fail until the timeout - so we leave the options alone
  3862. #endif
  3863. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  3864. bound = 0;
  3865. bindstart = time(NULL);
  3866. while (bound == 0) {
  3867. if (SOCKETFAIL(bind(*apisock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  3868. binderror = SOCKERRMSG;
  3869. if ((time(NULL) - bindstart) > 61)
  3870. break;
  3871. else {
  3872. applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
  3873. cgsleep_ms(30000);
  3874. }
  3875. } else
  3876. bound = 1;
  3877. }
  3878. if (bound == 0) {
  3879. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  3880. return;
  3881. }
  3882. if (SOCKETFAIL(listen(*apisock, QUEUE))) {
  3883. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3884. CLOSESOCKET(*apisock);
  3885. return;
  3886. }
  3887. if (opt_api_allow)
  3888. applog(LOG_WARNING, "API running in IP access mode on port %d (%d)", port, (int)*apisock);
  3889. else {
  3890. if (opt_api_network)
  3891. applog(LOG_WARNING, "API running in UNRESTRICTED read access mode on port %d (%d)", port, (int)*apisock);
  3892. else
  3893. applog(LOG_WARNING, "API running in local read access mode on port %d (%d)", port, (int)*apisock);
  3894. }
  3895. if (opt_api_mcast)
  3896. mcast_init();
  3897. while (!bye) {
  3898. clisiz = sizeof(cli);
  3899. if (SOCKETFAIL(c = accept(*apisock, (struct sockaddr *)(&cli), &clisiz))) {
  3900. applog(LOG_ERR, "API failed (%s)%s (%d)", SOCKERRMSG, UNAVAILABLE, (int)*apisock);
  3901. goto die;
  3902. }
  3903. addrok = check_connect(&cli, &connectaddr, &group);
  3904. applog(LOG_DEBUG, "API: connection from %s - %s",
  3905. connectaddr, addrok ? "Accepted" : "Ignored");
  3906. if (addrok) {
  3907. n = recv(c, &buf[0], TMPBUFSIZ-1, 0);
  3908. if (SOCKETFAIL(n))
  3909. buf[0] = '\0';
  3910. else
  3911. buf[n] = '\0';
  3912. if (opt_debug) {
  3913. if (SOCKETFAIL(n))
  3914. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  3915. else
  3916. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  3917. }
  3918. if (!SOCKETFAIL(n)) {
  3919. // the time of the request in now
  3920. when = time(NULL);
  3921. io_reinit(io_data);
  3922. did = false;
  3923. if (*buf != ISJSON) {
  3924. isjson = false;
  3925. param = strchr(buf, SEPARATOR);
  3926. if (param != NULL)
  3927. *(param++) = '\0';
  3928. cmd = buf;
  3929. }
  3930. else {
  3931. isjson = true;
  3932. param = NULL;
  3933. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  3934. json_config = json_loadb(buf, n, 0, &json_err);
  3935. #elif JANSSON_MAJOR_VERSION > 1
  3936. json_config = json_loads(buf, 0, &json_err);
  3937. #else
  3938. json_config = json_loads(buf, &json_err);
  3939. #endif
  3940. if (!json_is_object(json_config)) {
  3941. message(io_data, MSG_INVJSON, 0, NULL, isjson);
  3942. send_result(io_data, c, isjson);
  3943. did = true;
  3944. }
  3945. else {
  3946. json_val = json_object_get(json_config, JSON_COMMAND);
  3947. if (json_val == NULL) {
  3948. message(io_data, MSG_MISCMD, 0, NULL, isjson);
  3949. send_result(io_data, c, isjson);
  3950. did = true;
  3951. }
  3952. else {
  3953. if (!json_is_string(json_val)) {
  3954. message(io_data, MSG_INVCMD, 0, NULL, isjson);
  3955. send_result(io_data, c, isjson);
  3956. did = true;
  3957. }
  3958. else {
  3959. cmd = (char *)json_string_value(json_val);
  3960. json_val = json_object_get(json_config, JSON_PARAMETER);
  3961. if (json_is_string(json_val))
  3962. param = (char *)json_string_value(json_val);
  3963. else if (json_is_integer(json_val)) {
  3964. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  3965. param = param_buf;
  3966. } else if (json_is_real(json_val)) {
  3967. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  3968. param = param_buf;
  3969. }
  3970. }
  3971. }
  3972. }
  3973. }
  3974. if (!did)
  3975. for (i = 0; cmds[i].name != NULL; i++) {
  3976. if (strcmp(cmd, cmds[i].name) == 0) {
  3977. sprintf(cmdbuf, "|%s|", cmd);
  3978. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  3979. (cmds[i].func)(io_data, c, param, isjson, group);
  3980. else {
  3981. message(io_data, MSG_ACCDENY, 0, cmds[i].name, isjson);
  3982. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  3983. }
  3984. send_result(io_data, c, isjson);
  3985. did = true;
  3986. break;
  3987. }
  3988. }
  3989. if (!did) {
  3990. message(io_data, MSG_INVCMD, 0, NULL, isjson);
  3991. send_result(io_data, c, isjson);
  3992. }
  3993. }
  3994. }
  3995. CLOSESOCKET(c);
  3996. }
  3997. die:
  3998. /* Blank line fix for older compilers since pthread_cleanup_pop is a
  3999. * macro that gets confused by a label existing immediately before it
  4000. */
  4001. ;
  4002. pthread_cleanup_pop(true);
  4003. if (opt_debug)
  4004. applog(LOG_DEBUG, "API: terminating due to: %s",
  4005. do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
  4006. mutex_lock(&quit_restart_lock);
  4007. if (do_a_restart) {
  4008. if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
  4009. mutex_unlock(&quit_restart_lock);
  4010. quit(1, "API failed to initiate a restart - aborting");
  4011. }
  4012. pthread_detach(bye_thr.pth);
  4013. } else if (do_a_quit) {
  4014. if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
  4015. mutex_unlock(&quit_restart_lock);
  4016. quit(1, "API failed to initiate a clean quit - aborting");
  4017. }
  4018. pthread_detach(bye_thr.pth);
  4019. }
  4020. mutex_unlock(&quit_restart_lock);
  4021. }