api.c 132 KB

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