api.c 131 KB

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