api.c 131 KB

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