api.c 132 KB

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