api.c 107 KB

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