api.c 109 KB

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