api.c 132 KB

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