api.c 114 KB

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