api.c 107 KB

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