api.c 131 KB

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