api.c 132 KB

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