api.c 115 KB

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