api.c 110 KB

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