api.c 110 KB

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