api.c 98 KB

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