api.c 114 KB

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