api.c 131 KB

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