api.c 115 KB

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