api.c 112 KB

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