api.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852
  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. root = api_add_string(root, "Coinbase-Sig", opt_coinbase_sig, true);
  1203. root = print_data(root, buf, isjson, false);
  1204. io_add(io_data, buf);
  1205. if (isjson && io_open)
  1206. io_close(io_data);
  1207. }
  1208. static const char*
  1209. bool2str(bool b)
  1210. {
  1211. return b ? YES : NO;
  1212. }
  1213. static const char *status2str(enum alive status)
  1214. {
  1215. switch (status) {
  1216. case LIFE_WELL:
  1217. return ALIVE;
  1218. case LIFE_SICK:
  1219. return SICK;
  1220. case LIFE_DEAD:
  1221. case LIFE_DEAD2:
  1222. return DEAD;
  1223. case LIFE_NOSTART:
  1224. return NOSTART;
  1225. case LIFE_INIT:
  1226. case LIFE_INIT2:
  1227. return INIT;
  1228. case LIFE_WAIT:
  1229. return WAIT;
  1230. default:
  1231. return UNKNOWN;
  1232. }
  1233. }
  1234. static
  1235. struct api_data *api_add_device_identifier(struct api_data *root, struct cgpu_info *cgpu)
  1236. {
  1237. root = api_add_string(root, "Name", cgpu->drv->name, false);
  1238. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  1239. root = api_add_int(root, "ProcID", &(cgpu->proc_id), false);
  1240. return root;
  1241. }
  1242. static void devdetail_an(struct io_data *io_data, struct cgpu_info *cgpu, bool isjson, bool precom)
  1243. {
  1244. struct api_data *root = NULL;
  1245. char buf[TMPBUFSIZ];
  1246. int n = 0, i;
  1247. cgpu_utility(cgpu);
  1248. rd_lock(&devices_lock);
  1249. for (i = 0; i < total_devices; ++i) {
  1250. if (devices[i] == cgpu)
  1251. break;
  1252. if (cgpu->devtype == devices[i]->devtype)
  1253. ++n;
  1254. }
  1255. rd_unlock(&devices_lock);
  1256. root = api_add_int(root, (char*)cgpu->devtype, &n, true);
  1257. root = api_add_device_identifier(root, cgpu);
  1258. root = api_add_string(root, "Driver", cgpu->drv->dname, false);
  1259. if (cgpu->kname)
  1260. root = api_add_string(root, "Kernel", cgpu->kname, false);
  1261. if (cgpu->name)
  1262. root = api_add_string(root, "Model", cgpu->name, false);
  1263. if (cgpu->device_path)
  1264. root = api_add_string(root, "Device Path", cgpu->device_path, false);
  1265. if (cgpu->drv->get_api_extra_device_detail)
  1266. root = api_add_extra(root, cgpu->drv->get_api_extra_device_detail(cgpu));
  1267. root = print_data(root, buf, isjson, precom);
  1268. io_add(io_data, buf);
  1269. }
  1270. static void devstatus_an(struct io_data *io_data, struct cgpu_info *cgpu, bool isjson, bool precom)
  1271. {
  1272. struct api_data *root = NULL;
  1273. char buf[TMPBUFSIZ];
  1274. int n = 0, i;
  1275. cgpu_utility(cgpu);
  1276. rd_lock(&devices_lock);
  1277. for (i = 0; i < total_devices; ++i) {
  1278. if (devices[i] == cgpu)
  1279. break;
  1280. if (cgpu->devtype == devices[i]->devtype)
  1281. ++n;
  1282. }
  1283. rd_unlock(&devices_lock);
  1284. root = api_add_int(root, (char*)cgpu->devtype, &n, true);
  1285. root = api_add_device_identifier(root, cgpu);
  1286. root = api_add_string(root, "Enabled", bool2str(cgpu->deven != DEV_DISABLED), false);
  1287. root = api_add_string(root, "Status", status2str(cgpu->status), false);
  1288. if (cgpu->temp)
  1289. root = api_add_temp(root, "Temperature", &cgpu->temp, false);
  1290. double mhs = cgpu->total_mhashes / cgpu_runtime(cgpu);
  1291. root = api_add_mhs(root, "MHS av", &mhs, false);
  1292. char mhsname[27];
  1293. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1294. root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
  1295. root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
  1296. root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
  1297. root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
  1298. root = api_add_utility(root, "Utility", &(cgpu->utility), false);
  1299. int last_share_pool = cgpu->last_share_pool_time > 0 ?
  1300. cgpu->last_share_pool : -1;
  1301. root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
  1302. root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
  1303. root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
  1304. root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
  1305. root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
  1306. root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
  1307. root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
  1308. root = api_add_time(root, "Last Valid Work", &(cgpu->last_device_valid_work), false);
  1309. if (cgpu->drv->get_api_extra_device_status)
  1310. root = api_add_extra(root, cgpu->drv->get_api_extra_device_status(cgpu));
  1311. root = print_data(root, buf, isjson, precom);
  1312. io_add(io_data, buf);
  1313. }
  1314. #ifdef HAVE_OPENCL
  1315. static void gpustatus(struct io_data *io_data, int gpu, bool isjson, bool precom)
  1316. {
  1317. if (gpu < 0 || gpu >= nDevs)
  1318. return;
  1319. devstatus_an(io_data, &gpus[gpu], isjson, precom);
  1320. }
  1321. #endif
  1322. #ifdef HAVE_AN_FPGA
  1323. static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom)
  1324. {
  1325. int dev = pgadevice(pga);
  1326. if (dev < 0) // Should never happen
  1327. return;
  1328. devstatus_an(io_data, get_devices(dev), isjson, precom);
  1329. }
  1330. #endif
  1331. #ifdef WANT_CPUMINE
  1332. static void cpustatus(struct io_data *io_data, int cpu, bool isjson, bool precom)
  1333. {
  1334. if (opt_n_threads <= 0 || cpu < 0 || cpu >= num_processors)
  1335. return;
  1336. devstatus_an(io_data, &cpus[cpu], isjson, precom);
  1337. }
  1338. #endif
  1339. static void
  1340. 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)
  1341. {
  1342. bool io_open = false;
  1343. int i;
  1344. if (total_devices == 0) {
  1345. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  1346. return;
  1347. }
  1348. message(io_data, MSG_DEVS, 0, NULL, isjson);
  1349. if (isjson)
  1350. io_open = io_add(io_data, COMSTR JSON_DEVS);
  1351. for (i = 0; i < total_devices; ++i) {
  1352. func(io_data, get_devices(i), isjson, isjson && i > 0);
  1353. }
  1354. if (isjson && io_open)
  1355. io_close(io_data);
  1356. }
  1357. static void devdetail(struct io_data *io_data, SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1358. {
  1359. return devinfo_internal(devdetail_an, io_data, c, param, isjson, group);
  1360. }
  1361. static void devstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1362. {
  1363. return devinfo_internal(devstatus_an, io_data, c, param, isjson, group);
  1364. }
  1365. #ifdef HAVE_OPENCL
  1366. static void gpudev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1367. {
  1368. bool io_open = false;
  1369. int id;
  1370. if (nDevs == 0) {
  1371. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1372. return;
  1373. }
  1374. if (param == NULL || *param == '\0') {
  1375. message(io_data, MSG_MISID, 0, NULL, isjson);
  1376. return;
  1377. }
  1378. id = atoi(param);
  1379. if (id < 0 || id >= nDevs) {
  1380. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1381. return;
  1382. }
  1383. message(io_data, MSG_GPUDEV, id, NULL, isjson);
  1384. if (isjson)
  1385. io_open = io_add(io_data, COMSTR JSON_GPU);
  1386. gpustatus(io_data, id, isjson, false);
  1387. if (isjson && io_open)
  1388. io_close(io_data);
  1389. }
  1390. #endif
  1391. static void devscan(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1392. {
  1393. int n;
  1394. bool io_open = false;
  1395. applog(LOG_DEBUG, "RPC: request to scan %s for devices",
  1396. param);
  1397. if (param && !param[0])
  1398. param = NULL;
  1399. n = scan_serial(param);
  1400. message(io_data, MSG_DEVSCAN, n, NULL, isjson);
  1401. io_open = io_add(io_data, isjson ? COMSTR JSON_DEVS : _DEVS COMSTR);
  1402. n = total_devices - n;
  1403. for (int i = n; i < total_devices; ++i)
  1404. devdetail_an(io_data, get_devices(i), isjson, i > n);
  1405. if (isjson && io_open)
  1406. io_close(io_data);
  1407. }
  1408. #ifdef HAVE_AN_FPGA
  1409. static void pgadev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1410. {
  1411. bool io_open = false;
  1412. int numpga = numpgas();
  1413. int id;
  1414. if (numpga == 0) {
  1415. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1416. return;
  1417. }
  1418. if (param == NULL || *param == '\0') {
  1419. message(io_data, MSG_MISID, 0, NULL, isjson);
  1420. return;
  1421. }
  1422. id = atoi(param);
  1423. if (id < 0 || id >= numpga) {
  1424. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1425. return;
  1426. }
  1427. message(io_data, MSG_PGADEV, id, NULL, isjson);
  1428. if (isjson)
  1429. io_open = io_add(io_data, COMSTR JSON_PGA);
  1430. pgastatus(io_data, id, isjson, false);
  1431. if (isjson && io_open)
  1432. io_close(io_data);
  1433. }
  1434. static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1435. {
  1436. struct cgpu_info *cgpu;
  1437. int numpga = numpgas();
  1438. int id;
  1439. if (numpga == 0) {
  1440. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1441. return;
  1442. }
  1443. if (param == NULL || *param == '\0') {
  1444. message(io_data, MSG_MISID, 0, NULL, isjson);
  1445. return;
  1446. }
  1447. id = atoi(param);
  1448. if (id < 0 || id >= numpga) {
  1449. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1450. return;
  1451. }
  1452. int dev = pgadevice(id);
  1453. if (dev < 0) { // Should never happen
  1454. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1455. return;
  1456. }
  1457. cgpu = get_devices(dev);
  1458. applog(LOG_DEBUG, "API: request to pgaenable pgaid %d device %d %s",
  1459. id, dev, cgpu->proc_repr_ns);
  1460. if (cgpu->deven != DEV_DISABLED) {
  1461. message(io_data, MSG_PGALRENA, id, NULL, isjson);
  1462. return;
  1463. }
  1464. #if 0 /* A DISABLED device wont change status FIXME: should disabling make it WELL? */
  1465. if (cgpu->status != LIFE_WELL) {
  1466. message(io_data, MSG_PGAUNW, id, NULL, isjson);
  1467. return;
  1468. }
  1469. #endif
  1470. proc_enable(cgpu);
  1471. message(io_data, MSG_PGAENA, id, NULL, isjson);
  1472. }
  1473. static void pgadisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1474. {
  1475. struct cgpu_info *cgpu;
  1476. int numpga = numpgas();
  1477. int id;
  1478. if (numpga == 0) {
  1479. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1480. return;
  1481. }
  1482. if (param == NULL || *param == '\0') {
  1483. message(io_data, MSG_MISID, 0, NULL, isjson);
  1484. return;
  1485. }
  1486. id = atoi(param);
  1487. if (id < 0 || id >= numpga) {
  1488. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1489. return;
  1490. }
  1491. int dev = pgadevice(id);
  1492. if (dev < 0) { // Should never happen
  1493. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1494. return;
  1495. }
  1496. cgpu = get_devices(dev);
  1497. applog(LOG_DEBUG, "API: request to pgadisable pgaid %d device %d %s",
  1498. id, dev, cgpu->proc_repr_ns);
  1499. if (cgpu->deven == DEV_DISABLED) {
  1500. message(io_data, MSG_PGALRDIS, id, NULL, isjson);
  1501. return;
  1502. }
  1503. cgpu->deven = DEV_DISABLED;
  1504. message(io_data, MSG_PGADIS, id, NULL, isjson);
  1505. }
  1506. static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1507. {
  1508. struct cgpu_info *cgpu;
  1509. struct device_drv *drv;
  1510. int numpga = numpgas();
  1511. int id;
  1512. if (numpga == 0) {
  1513. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1514. return;
  1515. }
  1516. if (param == NULL || *param == '\0') {
  1517. message(io_data, MSG_MISID, 0, NULL, isjson);
  1518. return;
  1519. }
  1520. id = atoi(param);
  1521. if (id < 0 || id >= numpga) {
  1522. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1523. return;
  1524. }
  1525. int dev = pgadevice(id);
  1526. if (dev < 0) { // Should never happen
  1527. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1528. return;
  1529. }
  1530. cgpu = get_devices(dev);
  1531. drv = cgpu->drv;
  1532. if (drv->identify_device && drv->identify_device(cgpu))
  1533. message(io_data, MSG_PGAIDENT, id, NULL, isjson);
  1534. else
  1535. message(io_data, MSG_PGANOID, id, NULL, isjson);
  1536. }
  1537. #endif
  1538. #ifdef WANT_CPUMINE
  1539. static void cpudev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1540. {
  1541. bool io_open = false;
  1542. int id;
  1543. if (opt_n_threads == 0) {
  1544. message(io_data, MSG_CPUNON, 0, NULL, isjson);
  1545. return;
  1546. }
  1547. if (param == NULL || *param == '\0') {
  1548. message(io_data, MSG_MISID, 0, NULL, isjson);
  1549. return;
  1550. }
  1551. id = atoi(param);
  1552. if (id < 0 || id >= num_processors) {
  1553. message(io_data, MSG_INVCPU, id, NULL, isjson);
  1554. return;
  1555. }
  1556. message(io_data, MSG_CPUDEV, id, NULL, isjson);
  1557. if (isjson)
  1558. io_open = io_add(io_data, COMSTR JSON_CPU);
  1559. cpustatus(io_data, id, isjson, false);
  1560. if (isjson && io_open)
  1561. io_close(io_data);
  1562. }
  1563. #endif
  1564. static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1565. {
  1566. struct api_data *root = NULL;
  1567. char buf[TMPBUFSIZ];
  1568. bool io_open = false;
  1569. char *status, *lp;
  1570. int i;
  1571. if (total_pools == 0) {
  1572. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1573. return;
  1574. }
  1575. message(io_data, MSG_POOL, 0, NULL, isjson);
  1576. if (isjson)
  1577. io_open = io_add(io_data, COMSTR JSON_POOLS);
  1578. for (i = 0; i < total_pools; i++) {
  1579. struct pool *pool = pools[i];
  1580. if (pool->removed)
  1581. continue;
  1582. switch (pool->enabled) {
  1583. case POOL_DISABLED:
  1584. status = (char *)DISABLED;
  1585. break;
  1586. case POOL_REJECTING:
  1587. status = (char *)REJECTING;
  1588. break;
  1589. case POOL_ENABLED:
  1590. if (pool->idle)
  1591. status = (char *)DEAD;
  1592. else
  1593. status = (char *)ALIVE;
  1594. break;
  1595. default:
  1596. status = (char *)UNKNOWN;
  1597. break;
  1598. }
  1599. if (pool->hdr_path)
  1600. lp = (char *)YES;
  1601. else
  1602. lp = (char *)NO;
  1603. root = api_add_int(root, "POOL", &i, false);
  1604. root = api_add_escape(root, "URL", pool->rpc_url, false);
  1605. root = api_add_string(root, "Status", status, false);
  1606. root = api_add_int(root, "Priority", &(pool->prio), false);
  1607. root = api_add_string(root, "Long Poll", lp, false);
  1608. root = api_add_uint(root, "Getworks", &(pool->getwork_requested), false);
  1609. root = api_add_int(root, "Accepted", &(pool->accepted), false);
  1610. root = api_add_int(root, "Rejected", &(pool->rejected), false);
  1611. root = api_add_uint(root, "Discarded", &(pool->discarded_work), false);
  1612. root = api_add_uint(root, "Stale", &(pool->stale_shares), false);
  1613. root = api_add_uint(root, "Get Failures", &(pool->getfail_occasions), false);
  1614. root = api_add_uint(root, "Remote Failures", &(pool->remotefail_occasions), false);
  1615. root = api_add_escape(root, "User", pool->rpc_user, false);
  1616. root = api_add_time(root, "Last Share Time", &(pool->last_share_time), false);
  1617. root = api_add_int(root, "Diff1 Shares", &(pool->diff1), false);
  1618. if (pool->rpc_proxy) {
  1619. root = api_add_escape(root, "Proxy", pool->rpc_proxy, false);
  1620. } else {
  1621. root = api_add_const(root, "Proxy", BLANK, false);
  1622. }
  1623. root = api_add_diff(root, "Difficulty Accepted", &(pool->diff_accepted), false);
  1624. root = api_add_diff(root, "Difficulty Rejected", &(pool->diff_rejected), false);
  1625. root = api_add_diff(root, "Difficulty Stale", &(pool->diff_stale), false);
  1626. root = api_add_diff(root, "Last Share Difficulty", &(pool->last_share_diff), false);
  1627. root = api_add_bool(root, "Has Stratum", &(pool->has_stratum), false);
  1628. root = api_add_bool(root, "Stratum Active", &(pool->stratum_active), false);
  1629. if (pool->stratum_active)
  1630. root = api_add_escape(root, "Stratum URL", pool->stratum_url, false);
  1631. else
  1632. root = api_add_const(root, "Stratum URL", BLANK, false);
  1633. root = api_add_uint64(root, "Best Share", &(pool->best_diff), true);
  1634. if (pool->admin_msg)
  1635. root = api_add_escape(root, "Message", pool->admin_msg, true);
  1636. root = print_data(root, buf, isjson, isjson && (i > 0));
  1637. io_add(io_data, buf);
  1638. }
  1639. if (isjson && io_open)
  1640. io_close(io_data);
  1641. }
  1642. static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1643. {
  1644. struct api_data *root = NULL;
  1645. char buf[TMPBUFSIZ];
  1646. bool io_open;
  1647. double utility, mhs, work_utility;
  1648. #ifdef WANT_CPUMINE
  1649. char *algo = (char *)(algo_names[opt_algo]);
  1650. if (algo == NULL)
  1651. algo = (char *)NULLSTR;
  1652. #endif
  1653. message(io_data, MSG_SUMM, 0, NULL, isjson);
  1654. io_open = io_add(io_data, isjson ? COMSTR JSON_SUMMARY : _SUMMARY COMSTR);
  1655. // stop hashmeter() changing some while copying
  1656. mutex_lock(&hash_lock);
  1657. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1658. mhs = total_mhashes_done / total_secs;
  1659. work_utility = total_diff1 / ( total_secs ? total_secs : 1 ) * 60;
  1660. root = api_add_elapsed(root, "Elapsed", &(total_secs), true);
  1661. #ifdef WANT_CPUMINE
  1662. if (opt_n_threads)
  1663. root = api_add_string(root, "Algorithm", algo, false);
  1664. #endif
  1665. root = api_add_mhs(root, "MHS av", &(mhs), false);
  1666. root = api_add_uint(root, "Found Blocks", &(found_blocks), true);
  1667. root = api_add_int(root, "Getworks", &(total_getworks), true);
  1668. root = api_add_int(root, "Accepted", &(total_accepted), true);
  1669. root = api_add_int(root, "Rejected", &(total_rejected), true);
  1670. root = api_add_int(root, "Hardware Errors", &(hw_errors), true);
  1671. root = api_add_utility(root, "Utility", &(utility), false);
  1672. root = api_add_int(root, "Discarded", &(total_discarded), true);
  1673. root = api_add_int(root, "Stale", &(total_stale), true);
  1674. root = api_add_uint(root, "Get Failures", &(total_go), true);
  1675. root = api_add_uint(root, "Local Work", &(local_work), true);
  1676. root = api_add_uint(root, "Remote Failures", &(total_ro), true);
  1677. root = api_add_uint(root, "Network Blocks", &(new_blocks), true);
  1678. root = api_add_mhtotal(root, "Total MH", &(total_mhashes_done), true);
  1679. root = api_add_utility(root, "Work Utility", &(work_utility), false);
  1680. root = api_add_diff(root, "Difficulty Accepted", &(total_diff_accepted), true);
  1681. root = api_add_diff(root, "Difficulty Rejected", &(total_diff_rejected), true);
  1682. root = api_add_diff(root, "Difficulty Stale", &(total_diff_stale), true);
  1683. root = api_add_uint64(root, "Best Share", &(best_diff), true);
  1684. mutex_unlock(&hash_lock);
  1685. root = print_data(root, buf, isjson, false);
  1686. io_add(io_data, buf);
  1687. if (isjson && io_open)
  1688. io_close(io_data);
  1689. }
  1690. #ifdef HAVE_OPENCL
  1691. static void gpuenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1692. {
  1693. int id;
  1694. if (gpu_threads == 0) {
  1695. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1696. return;
  1697. }
  1698. if (param == NULL || *param == '\0') {
  1699. message(io_data, MSG_MISID, 0, NULL, isjson);
  1700. return;
  1701. }
  1702. id = atoi(param);
  1703. if (id < 0 || id >= nDevs) {
  1704. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1705. return;
  1706. }
  1707. applog(LOG_DEBUG, "API: request to gpuenable gpuid %d %s",
  1708. id, gpus[id].proc_repr_ns);
  1709. if (gpus[id].deven != DEV_DISABLED) {
  1710. message(io_data, MSG_ALRENA, id, NULL, isjson);
  1711. return;
  1712. }
  1713. if (gpus[id].status != LIFE_WELL)
  1714. {
  1715. message(io_data, MSG_GPUMRE, id, NULL, isjson);
  1716. return;
  1717. }
  1718. proc_enable(&gpus[id]);
  1719. message(io_data, MSG_GPUREN, id, NULL, isjson);
  1720. }
  1721. static void gpudisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1722. {
  1723. int id;
  1724. if (nDevs == 0) {
  1725. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1726. return;
  1727. }
  1728. if (param == NULL || *param == '\0') {
  1729. message(io_data, MSG_MISID, 0, NULL, isjson);
  1730. return;
  1731. }
  1732. id = atoi(param);
  1733. if (id < 0 || id >= nDevs) {
  1734. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1735. return;
  1736. }
  1737. applog(LOG_DEBUG, "API: request to gpudisable gpuid %d %s",
  1738. id, gpus[id].proc_repr_ns);
  1739. if (gpus[id].deven == DEV_DISABLED) {
  1740. message(io_data, MSG_ALRDIS, id, NULL, isjson);
  1741. return;
  1742. }
  1743. gpus[id].deven = DEV_DISABLED;
  1744. message(io_data, MSG_GPUDIS, id, NULL, isjson);
  1745. }
  1746. static void gpurestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1747. {
  1748. int id;
  1749. if (nDevs == 0) {
  1750. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1751. return;
  1752. }
  1753. if (param == NULL || *param == '\0') {
  1754. message(io_data, MSG_MISID, 0, NULL, isjson);
  1755. return;
  1756. }
  1757. id = atoi(param);
  1758. if (id < 0 || id >= nDevs) {
  1759. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1760. return;
  1761. }
  1762. reinit_device(&gpus[id]);
  1763. message(io_data, MSG_GPUREI, id, NULL, isjson);
  1764. }
  1765. #endif
  1766. static void gpucount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1767. {
  1768. struct api_data *root = NULL;
  1769. char buf[TMPBUFSIZ];
  1770. bool io_open;
  1771. int numgpu = 0;
  1772. #ifdef HAVE_OPENCL
  1773. numgpu = nDevs;
  1774. #endif
  1775. message(io_data, MSG_NUMGPU, 0, NULL, isjson);
  1776. io_open = io_add(io_data, isjson ? COMSTR JSON_GPUS : _GPUS COMSTR);
  1777. root = api_add_int(root, "Count", &numgpu, false);
  1778. root = print_data(root, buf, isjson, false);
  1779. io_add(io_data, buf);
  1780. if (isjson && io_open)
  1781. io_close(io_data);
  1782. }
  1783. static void pgacount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1784. {
  1785. struct api_data *root = NULL;
  1786. char buf[TMPBUFSIZ];
  1787. bool io_open;
  1788. int count = 0;
  1789. #ifdef HAVE_AN_FPGA
  1790. count = numpgas();
  1791. #endif
  1792. message(io_data, MSG_NUMPGA, 0, NULL, isjson);
  1793. io_open = io_add(io_data, isjson ? COMSTR JSON_PGAS : _PGAS COMSTR);
  1794. root = api_add_int(root, "Count", &count, false);
  1795. root = print_data(root, buf, isjson, false);
  1796. io_add(io_data, buf);
  1797. if (isjson && io_open)
  1798. io_close(io_data);
  1799. }
  1800. static void cpucount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1801. {
  1802. struct api_data *root = NULL;
  1803. char buf[TMPBUFSIZ];
  1804. bool io_open;
  1805. int count = 0;
  1806. #ifdef WANT_CPUMINE
  1807. count = opt_n_threads > 0 ? num_processors : 0;
  1808. #endif
  1809. message(io_data, MSG_NUMCPU, 0, NULL, isjson);
  1810. io_open = io_add(io_data, isjson ? COMSTR JSON_CPUS : _CPUS COMSTR);
  1811. root = api_add_int(root, "Count", &count, false);
  1812. root = print_data(root, buf, isjson, false);
  1813. io_add(io_data, buf);
  1814. if (isjson && io_open)
  1815. io_close(io_data);
  1816. }
  1817. static void switchpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1818. {
  1819. struct pool *pool;
  1820. int id;
  1821. if (total_pools == 0) {
  1822. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1823. return;
  1824. }
  1825. if (param == NULL || *param == '\0') {
  1826. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1827. return;
  1828. }
  1829. id = atoi(param);
  1830. cg_rlock(&control_lock);
  1831. if (id < 0 || id >= total_pools) {
  1832. cg_runlock(&control_lock);
  1833. message(io_data, MSG_INVPID, id, NULL, isjson);
  1834. return;
  1835. }
  1836. pool = pools[id];
  1837. pool->enabled = POOL_ENABLED;
  1838. cg_runlock(&control_lock);
  1839. switch_pools(pool);
  1840. message(io_data, MSG_SWITCHP, id, NULL, isjson);
  1841. }
  1842. static void copyadvanceafter(char ch, char **param, char **buf)
  1843. {
  1844. #define src_p (*param)
  1845. #define dst_b (*buf)
  1846. while (*src_p && *src_p != ch) {
  1847. if (*src_p == '\\' && *(src_p+1) != '\0')
  1848. src_p++;
  1849. *(dst_b++) = *(src_p++);
  1850. }
  1851. if (*src_p)
  1852. src_p++;
  1853. *(dst_b++) = '\0';
  1854. }
  1855. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1856. {
  1857. char *ptr, *buf;
  1858. ptr = buf = malloc(strlen(param)+1);
  1859. if (unlikely(!buf))
  1860. quit(1, "Failed to malloc pooldetails buf");
  1861. *url = buf;
  1862. // copy url
  1863. copyadvanceafter(',', &param, &buf);
  1864. if (!(*param)) // missing user
  1865. goto exitsama;
  1866. *user = buf;
  1867. // copy user
  1868. copyadvanceafter(',', &param, &buf);
  1869. if (!*param) // missing pass
  1870. goto exitsama;
  1871. *pass = buf;
  1872. // copy pass
  1873. copyadvanceafter(',', &param, &buf);
  1874. return true;
  1875. exitsama:
  1876. free(ptr);
  1877. return false;
  1878. }
  1879. static void addpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1880. {
  1881. char *url, *user, *pass;
  1882. struct pool *pool;
  1883. char *ptr;
  1884. if (param == NULL || *param == '\0') {
  1885. message(io_data, MSG_MISPDP, 0, NULL, isjson);
  1886. return;
  1887. }
  1888. if (!pooldetails(param, &url, &user, &pass)) {
  1889. ptr = escape_string(param, isjson);
  1890. message(io_data, MSG_INVPDP, 0, ptr, isjson);
  1891. if (ptr != param)
  1892. free(ptr);
  1893. ptr = NULL;
  1894. return;
  1895. }
  1896. pool = add_pool();
  1897. detect_stratum(pool, url);
  1898. add_pool_details(pool, true, url, user, pass);
  1899. ptr = escape_string(url, isjson);
  1900. message(io_data, MSG_ADDPOOL, 0, ptr, isjson);
  1901. if (ptr != url)
  1902. free(ptr);
  1903. ptr = NULL;
  1904. }
  1905. static void enablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1906. {
  1907. struct pool *pool;
  1908. int id;
  1909. if (total_pools == 0) {
  1910. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1911. return;
  1912. }
  1913. if (param == NULL || *param == '\0') {
  1914. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1915. return;
  1916. }
  1917. id = atoi(param);
  1918. if (id < 0 || id >= total_pools) {
  1919. message(io_data, MSG_INVPID, id, NULL, isjson);
  1920. return;
  1921. }
  1922. pool = pools[id];
  1923. if (pool->enabled == POOL_ENABLED) {
  1924. message(io_data, MSG_ALRENAP, id, NULL, isjson);
  1925. return;
  1926. }
  1927. pool->enabled = POOL_ENABLED;
  1928. if (pool->prio < current_pool()->prio)
  1929. switch_pools(pool);
  1930. message(io_data, MSG_ENAPOOL, id, NULL, isjson);
  1931. }
  1932. static void poolpriority(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1933. {
  1934. int i;
  1935. switch (prioritize_pools(param, &i)) {
  1936. case MSG_NOPOOL:
  1937. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1938. return;
  1939. case MSG_MISPID:
  1940. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1941. return;
  1942. case MSG_INVPID:
  1943. message(io_data, MSG_INVPID, i, NULL, isjson);
  1944. return;
  1945. case MSG_DUPPID:
  1946. message(io_data, MSG_DUPPID, i, NULL, isjson);
  1947. return;
  1948. case MSG_POOLPRIO:
  1949. default:
  1950. message(io_data, MSG_POOLPRIO, 0, NULL, isjson);
  1951. return;
  1952. }
  1953. }
  1954. static void disablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1955. {
  1956. struct pool *pool;
  1957. int id;
  1958. if (total_pools == 0) {
  1959. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1960. return;
  1961. }
  1962. if (param == NULL || *param == '\0') {
  1963. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1964. return;
  1965. }
  1966. id = atoi(param);
  1967. if (id < 0 || id >= total_pools) {
  1968. message(io_data, MSG_INVPID, id, NULL, isjson);
  1969. return;
  1970. }
  1971. pool = pools[id];
  1972. if (pool->enabled == POOL_DISABLED) {
  1973. message(io_data, MSG_ALRDISP, id, NULL, isjson);
  1974. return;
  1975. }
  1976. if (enabled_pools <= 1) {
  1977. message(io_data, MSG_DISLASTP, id, NULL, isjson);
  1978. return;
  1979. }
  1980. pool->enabled = POOL_DISABLED;
  1981. if (pool == current_pool())
  1982. switch_pools(NULL);
  1983. message(io_data, MSG_DISPOOL, id, NULL, isjson);
  1984. }
  1985. static void removepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1986. {
  1987. struct pool *pool;
  1988. char *rpc_url;
  1989. bool dofree = false;
  1990. int id;
  1991. if (total_pools == 0) {
  1992. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1993. return;
  1994. }
  1995. if (param == NULL || *param == '\0') {
  1996. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1997. return;
  1998. }
  1999. id = atoi(param);
  2000. if (id < 0 || id >= total_pools) {
  2001. message(io_data, MSG_INVPID, id, NULL, isjson);
  2002. return;
  2003. }
  2004. if (total_pools <= 1) {
  2005. message(io_data, MSG_REMLASTP, id, NULL, isjson);
  2006. return;
  2007. }
  2008. pool = pools[id];
  2009. if (pool == current_pool())
  2010. switch_pools(NULL);
  2011. if (pool == current_pool()) {
  2012. message(io_data, MSG_ACTPOOL, id, NULL, isjson);
  2013. return;
  2014. }
  2015. pool->enabled = POOL_DISABLED;
  2016. rpc_url = escape_string(pool->rpc_url, isjson);
  2017. if (rpc_url != pool->rpc_url)
  2018. dofree = true;
  2019. remove_pool(pool);
  2020. message(io_data, MSG_REMPOOL, id, rpc_url, isjson);
  2021. if (dofree)
  2022. free(rpc_url);
  2023. rpc_url = NULL;
  2024. }
  2025. #ifdef HAVE_OPENCL
  2026. static bool splitgpuvalue(struct io_data *io_data, char *param, int *gpu, char **value, bool isjson)
  2027. {
  2028. int id;
  2029. char *gpusep;
  2030. if (nDevs == 0) {
  2031. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  2032. return false;
  2033. }
  2034. if (param == NULL || *param == '\0') {
  2035. message(io_data, MSG_MISID, 0, NULL, isjson);
  2036. return false;
  2037. }
  2038. gpusep = strchr(param, GPUSEP);
  2039. if (gpusep == NULL) {
  2040. message(io_data, MSG_MISVAL, 0, NULL, isjson);
  2041. return false;
  2042. }
  2043. *(gpusep++) = '\0';
  2044. id = atoi(param);
  2045. if (id < 0 || id >= nDevs) {
  2046. message(io_data, MSG_INVGPU, id, NULL, isjson);
  2047. return false;
  2048. }
  2049. *gpu = id;
  2050. *value = gpusep;
  2051. return true;
  2052. }
  2053. static void gpuintensity(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2054. {
  2055. int id;
  2056. char *value;
  2057. int intensity;
  2058. char intensitystr[7];
  2059. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2060. return;
  2061. if (!strncasecmp(value, DYNAMIC, 1)) {
  2062. gpus[id].dynamic = true;
  2063. strcpy(intensitystr, DYNAMIC);
  2064. }
  2065. else {
  2066. intensity = atoi(value);
  2067. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  2068. message(io_data, MSG_INVINT, 0, value, isjson);
  2069. return;
  2070. }
  2071. gpus[id].dynamic = false;
  2072. gpus[id].intensity = intensity;
  2073. sprintf(intensitystr, "%d", intensity);
  2074. }
  2075. message(io_data, MSG_GPUINT, id, intensitystr, isjson);
  2076. }
  2077. static void gpumem(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2078. {
  2079. #ifdef HAVE_ADL
  2080. int id;
  2081. char *value;
  2082. int clock;
  2083. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2084. return;
  2085. clock = atoi(value);
  2086. if (set_memoryclock(id, clock))
  2087. message(io_data, MSG_GPUMERR, id, value, isjson);
  2088. else
  2089. message(io_data, MSG_GPUMEM, id, value, isjson);
  2090. #else
  2091. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2092. #endif
  2093. }
  2094. static void gpuengine(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2095. {
  2096. #ifdef HAVE_ADL
  2097. int id;
  2098. char *value;
  2099. int clock;
  2100. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2101. return;
  2102. clock = atoi(value);
  2103. if (set_engineclock(id, clock))
  2104. message(io_data, MSG_GPUEERR, id, value, isjson);
  2105. else
  2106. message(io_data, MSG_GPUENG, id, value, isjson);
  2107. #else
  2108. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2109. #endif
  2110. }
  2111. static void gpufan(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2112. {
  2113. #ifdef HAVE_ADL
  2114. int id;
  2115. char *value;
  2116. int fan;
  2117. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2118. return;
  2119. fan = atoi(value);
  2120. if (set_fanspeed(id, fan))
  2121. message(io_data, MSG_GPUFERR, id, value, isjson);
  2122. else
  2123. message(io_data, MSG_GPUFAN, id, value, isjson);
  2124. #else
  2125. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2126. #endif
  2127. }
  2128. static void gpuvddc(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2129. {
  2130. #ifdef HAVE_ADL
  2131. int id;
  2132. char *value;
  2133. float vddc;
  2134. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2135. return;
  2136. vddc = atof(value);
  2137. if (set_vddc(id, vddc))
  2138. message(io_data, MSG_GPUVERR, id, value, isjson);
  2139. else
  2140. message(io_data, MSG_GPUVDDC, id, value, isjson);
  2141. #else
  2142. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2143. #endif
  2144. }
  2145. #endif
  2146. void doquit(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2147. {
  2148. if (isjson)
  2149. io_put(io_data, JSON_START JSON_BYE);
  2150. else
  2151. io_put(io_data, _BYE);
  2152. bye = true;
  2153. do_a_quit = true;
  2154. }
  2155. void dorestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2156. {
  2157. if (isjson)
  2158. io_put(io_data, JSON_START JSON_RESTART);
  2159. else
  2160. io_put(io_data, _RESTART);
  2161. bye = true;
  2162. do_a_restart = true;
  2163. }
  2164. void privileged(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2165. {
  2166. message(io_data, MSG_ACCOK, 0, NULL, isjson);
  2167. }
  2168. void notifystatus(struct io_data *io_data, int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
  2169. {
  2170. struct api_data *root = NULL;
  2171. char buf[TMPBUFSIZ];
  2172. char *reason;
  2173. if (cgpu->device_last_not_well == 0)
  2174. reason = REASON_NONE;
  2175. else
  2176. switch(cgpu->device_not_well_reason) {
  2177. case REASON_THREAD_FAIL_INIT:
  2178. reason = REASON_THREAD_FAIL_INIT_STR;
  2179. break;
  2180. case REASON_THREAD_ZERO_HASH:
  2181. reason = REASON_THREAD_ZERO_HASH_STR;
  2182. break;
  2183. case REASON_THREAD_FAIL_QUEUE:
  2184. reason = REASON_THREAD_FAIL_QUEUE_STR;
  2185. break;
  2186. case REASON_DEV_SICK_IDLE_60:
  2187. reason = REASON_DEV_SICK_IDLE_60_STR;
  2188. break;
  2189. case REASON_DEV_DEAD_IDLE_600:
  2190. reason = REASON_DEV_DEAD_IDLE_600_STR;
  2191. break;
  2192. case REASON_DEV_NOSTART:
  2193. reason = REASON_DEV_NOSTART_STR;
  2194. break;
  2195. case REASON_DEV_OVER_HEAT:
  2196. reason = REASON_DEV_OVER_HEAT_STR;
  2197. break;
  2198. case REASON_DEV_THERMAL_CUTOFF:
  2199. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  2200. break;
  2201. case REASON_DEV_COMMS_ERROR:
  2202. reason = REASON_DEV_COMMS_ERROR_STR;
  2203. break;
  2204. default:
  2205. reason = REASON_UNKNOWN_STR;
  2206. break;
  2207. }
  2208. // ALL counters (and only counters) must start the name with a '*'
  2209. // Simplifies future external support for identifying new counters
  2210. root = api_add_int(root, "NOTIFY", &device, false);
  2211. root = api_add_device_identifier(root, cgpu);
  2212. root = api_add_time(root, "Last Well", &(cgpu->device_last_well), false);
  2213. root = api_add_time(root, "Last Not Well", &(cgpu->device_last_not_well), false);
  2214. root = api_add_string(root, "Reason Not Well", reason, false);
  2215. root = api_add_int(root, "*Thread Fail Init", &(cgpu->thread_fail_init_count), false);
  2216. root = api_add_int(root, "*Thread Zero Hash", &(cgpu->thread_zero_hash_count), false);
  2217. root = api_add_int(root, "*Thread Fail Queue", &(cgpu->thread_fail_queue_count), false);
  2218. root = api_add_int(root, "*Dev Sick Idle 60s", &(cgpu->dev_sick_idle_60_count), false);
  2219. root = api_add_int(root, "*Dev Dead Idle 600s", &(cgpu->dev_dead_idle_600_count), false);
  2220. root = api_add_int(root, "*Dev Nostart", &(cgpu->dev_nostart_count), false);
  2221. root = api_add_int(root, "*Dev Over Heat", &(cgpu->dev_over_heat_count), false);
  2222. root = api_add_int(root, "*Dev Thermal Cutoff", &(cgpu->dev_thermal_cutoff_count), false);
  2223. root = api_add_int(root, "*Dev Comms Error", &(cgpu->dev_comms_error_count), false);
  2224. root = api_add_int(root, "*Dev Throttle", &(cgpu->dev_throttle_count), false);
  2225. root = print_data(root, buf, isjson, isjson && (device > 0));
  2226. io_add(io_data, buf);
  2227. }
  2228. static void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
  2229. {
  2230. struct cgpu_info *cgpu;
  2231. bool io_open = false;
  2232. int i;
  2233. if (total_devices == 0) {
  2234. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  2235. return;
  2236. }
  2237. message(io_data, MSG_NOTIFY, 0, NULL, isjson);
  2238. if (isjson)
  2239. io_open = io_add(io_data, COMSTR JSON_NOTIFY);
  2240. for (i = 0; i < total_devices; i++) {
  2241. cgpu = get_devices(i);
  2242. notifystatus(io_data, i, cgpu, isjson, group);
  2243. }
  2244. if (isjson && io_open)
  2245. io_close(io_data);
  2246. }
  2247. static void devdetails(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2248. {
  2249. struct api_data *root = NULL;
  2250. char buf[TMPBUFSIZ];
  2251. bool io_open = false;
  2252. struct cgpu_info *cgpu;
  2253. int i;
  2254. if (total_devices == 0) {
  2255. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  2256. return;
  2257. }
  2258. message(io_data, MSG_DEVDETAILS, 0, NULL, isjson);
  2259. if (isjson)
  2260. io_open = io_add(io_data, COMSTR JSON_DEVDETAILS);
  2261. for (i = 0; i < total_devices; i++) {
  2262. cgpu = get_devices(i);
  2263. root = api_add_int(root, "DEVDETAILS", &i, false);
  2264. root = api_add_device_identifier(root, cgpu);
  2265. root = api_add_string(root, "Driver", cgpu->drv->dname, false);
  2266. root = api_add_const(root, "Kernel", cgpu->kname ? : BLANK, false);
  2267. root = api_add_const(root, "Model", cgpu->name ? : BLANK, false);
  2268. root = api_add_const(root, "Device Path", cgpu->device_path ? : BLANK, false);
  2269. root = print_data(root, buf, isjson, isjson && (i > 0));
  2270. io_add(io_data, buf);
  2271. }
  2272. if (isjson && io_open)
  2273. io_close(io_data);
  2274. }
  2275. void dosave(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2276. {
  2277. char filename[PATH_MAX];
  2278. FILE *fcfg;
  2279. char *ptr;
  2280. if (param == NULL || *param == '\0') {
  2281. default_save_file(filename);
  2282. param = filename;
  2283. }
  2284. fcfg = fopen(param, "w");
  2285. if (!fcfg) {
  2286. ptr = escape_string(param, isjson);
  2287. message(io_data, MSG_BADFN, 0, ptr, isjson);
  2288. if (ptr != param)
  2289. free(ptr);
  2290. ptr = NULL;
  2291. return;
  2292. }
  2293. write_config(fcfg);
  2294. fclose(fcfg);
  2295. ptr = escape_string(param, isjson);
  2296. message(io_data, MSG_SAVED, 0, ptr, isjson);
  2297. if (ptr != param)
  2298. free(ptr);
  2299. ptr = NULL;
  2300. }
  2301. 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)
  2302. {
  2303. struct api_data *root = NULL;
  2304. char buf[TMPBUFSIZ];
  2305. double elapsed;
  2306. root = api_add_int(root, "STATS", &i, false);
  2307. root = api_add_string(root, "ID", id, false);
  2308. elapsed = stats_elapsed(stats);
  2309. root = api_add_elapsed(root, "Elapsed", &elapsed, false);
  2310. root = api_add_uint32(root, "Calls", &(stats->getwork_calls), false);
  2311. root = api_add_timeval(root, "Wait", &(stats->getwork_wait), false);
  2312. root = api_add_timeval(root, "Max", &(stats->getwork_wait_max), false);
  2313. root = api_add_timeval(root, "Min", &(stats->getwork_wait_min), false);
  2314. if (pool_stats) {
  2315. root = api_add_uint32(root, "Pool Calls", &(pool_stats->getwork_calls), false);
  2316. root = api_add_uint32(root, "Pool Attempts", &(pool_stats->getwork_attempts), false);
  2317. root = api_add_timeval(root, "Pool Wait", &(pool_stats->getwork_wait), false);
  2318. root = api_add_timeval(root, "Pool Max", &(pool_stats->getwork_wait_max), false);
  2319. root = api_add_timeval(root, "Pool Min", &(pool_stats->getwork_wait_min), false);
  2320. root = api_add_double(root, "Pool Av", &(pool_stats->getwork_wait_rolling), false);
  2321. root = api_add_bool(root, "Work Had Roll Time", &(pool_stats->hadrolltime), false);
  2322. root = api_add_bool(root, "Work Can Roll", &(pool_stats->canroll), false);
  2323. root = api_add_bool(root, "Work Had Expire", &(pool_stats->hadexpire), false);
  2324. root = api_add_uint32(root, "Work Roll Time", &(pool_stats->rolltime), false);
  2325. root = api_add_diff(root, "Work Diff", &(pool_stats->last_diff), false);
  2326. root = api_add_diff(root, "Min Diff", &(pool_stats->min_diff), false);
  2327. root = api_add_diff(root, "Max Diff", &(pool_stats->max_diff), false);
  2328. root = api_add_uint32(root, "Min Diff Count", &(pool_stats->min_diff_count), false);
  2329. root = api_add_uint32(root, "Max Diff Count", &(pool_stats->max_diff_count), false);
  2330. root = api_add_uint64(root, "Times Sent", &(pool_stats->times_sent), false);
  2331. root = api_add_uint64(root, "Bytes Sent", &(pool_stats->bytes_sent), false);
  2332. root = api_add_uint64(root, "Times Recv", &(pool_stats->times_received), false);
  2333. root = api_add_uint64(root, "Bytes Recv", &(pool_stats->bytes_received), false);
  2334. root = api_add_uint64(root, "Net Bytes Sent", &(pool_stats->net_bytes_sent), false);
  2335. root = api_add_uint64(root, "Net Bytes Recv", &(pool_stats->net_bytes_received), false);
  2336. }
  2337. if (extra)
  2338. root = api_add_extra(root, extra);
  2339. root = print_data(root, buf, isjson, isjson && (i > 0));
  2340. io_add(io_data, buf);
  2341. return ++i;
  2342. }
  2343. static void minerstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2344. {
  2345. struct cgpu_info *cgpu;
  2346. bool io_open = false;
  2347. struct api_data *extra;
  2348. char id[20];
  2349. int i, j;
  2350. message(io_data, MSG_MINESTATS, 0, NULL, isjson);
  2351. if (isjson)
  2352. io_open = io_add(io_data, COMSTR JSON_MINESTATS);
  2353. i = 0;
  2354. for (j = 0; j < total_devices; j++) {
  2355. cgpu = get_devices(j);
  2356. if (cgpu && cgpu->drv) {
  2357. if (cgpu->drv->get_api_stats)
  2358. extra = cgpu->drv->get_api_stats(cgpu);
  2359. else
  2360. extra = NULL;
  2361. i = itemstats(io_data, i, cgpu->proc_repr_ns, &(cgpu->cgminer_stats), NULL, extra, isjson);
  2362. }
  2363. }
  2364. for (j = 0; j < total_pools; j++) {
  2365. struct pool *pool = pools[j];
  2366. sprintf(id, "POOL%d", j);
  2367. i = itemstats(io_data, i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, isjson);
  2368. }
  2369. if (isjson && io_open)
  2370. io_close(io_data);
  2371. }
  2372. static void failoveronly(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2373. {
  2374. if (param == NULL || *param == '\0') {
  2375. message(io_data, MSG_MISBOOL, 0, NULL, isjson);
  2376. return;
  2377. }
  2378. *param = tolower(*param);
  2379. if (*param != 't' && *param != 'f') {
  2380. message(io_data, MSG_INVBOOL, 0, NULL, isjson);
  2381. return;
  2382. }
  2383. bool tf = (*param == 't');
  2384. opt_fail_only = tf;
  2385. message(io_data, MSG_FOO, tf, NULL, isjson);
  2386. }
  2387. static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2388. {
  2389. struct api_data *root = NULL;
  2390. char buf[TMPBUFSIZ];
  2391. bool io_open;
  2392. message(io_data, MSG_MINECOIN, 0, NULL, isjson);
  2393. io_open = io_add(io_data, isjson ? COMSTR JSON_MINECOIN : _MINECOIN COMSTR);
  2394. #ifdef USE_SCRYPT
  2395. if (opt_scrypt)
  2396. root = api_add_const(root, "Hash Method", SCRYPTSTR, false);
  2397. else
  2398. #endif
  2399. root = api_add_const(root, "Hash Method", SHA256STR, false);
  2400. cg_rlock(&ch_lock);
  2401. if (current_fullhash && *current_fullhash) {
  2402. root = api_add_timeval(root, "Current Block Time", &block_timeval, true);
  2403. root = api_add_string(root, "Current Block Hash", current_fullhash, true);
  2404. } else {
  2405. struct timeval t = {0,0};
  2406. root = api_add_timeval(root, "Current Block Time", &t, true);
  2407. root = api_add_const(root, "Current Block Hash", BLANK, false);
  2408. }
  2409. cg_runlock(&ch_lock);
  2410. root = api_add_bool(root, "LP", &have_longpoll, false);
  2411. root = api_add_diff(root, "Network Difficulty", &current_diff, true);
  2412. root = print_data(root, buf, isjson, false);
  2413. io_add(io_data, buf);
  2414. if (isjson && io_open)
  2415. io_close(io_data);
  2416. }
  2417. static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2418. {
  2419. struct api_data *root = NULL;
  2420. char buf[TMPBUFSIZ];
  2421. bool io_open;
  2422. if (param == NULL)
  2423. param = (char *)BLANK;
  2424. else
  2425. *param = tolower(*param);
  2426. switch(*param) {
  2427. case 's':
  2428. opt_realquiet = true;
  2429. break;
  2430. case 'q':
  2431. opt_quiet ^= true;
  2432. break;
  2433. case 'v':
  2434. opt_log_output ^= true;
  2435. if (opt_log_output)
  2436. opt_quiet = false;
  2437. break;
  2438. case 'd':
  2439. opt_debug ^= true;
  2440. opt_log_output = opt_debug;
  2441. if (opt_debug)
  2442. opt_quiet = false;
  2443. break;
  2444. case 'r':
  2445. opt_protocol ^= true;
  2446. if (opt_protocol)
  2447. opt_quiet = false;
  2448. break;
  2449. case 'p':
  2450. want_per_device_stats ^= true;
  2451. opt_log_output = want_per_device_stats;
  2452. break;
  2453. case 'n':
  2454. opt_log_output = false;
  2455. opt_debug = false;
  2456. opt_quiet = false;
  2457. opt_protocol = false;
  2458. want_per_device_stats = false;
  2459. opt_worktime = false;
  2460. break;
  2461. case 'w':
  2462. opt_worktime ^= true;
  2463. break;
  2464. #ifdef _MEMORY_DEBUG
  2465. case 'y':
  2466. cgmemspeedup();
  2467. break;
  2468. case 'z':
  2469. cgmemrpt();
  2470. break;
  2471. #endif
  2472. default:
  2473. // anything else just reports the settings
  2474. break;
  2475. }
  2476. message(io_data, MSG_DEBUGSET, 0, NULL, isjson);
  2477. io_open = io_add(io_data, isjson ? COMSTR JSON_DEBUGSET : _DEBUGSET COMSTR);
  2478. root = api_add_bool(root, "Silent", &opt_realquiet, false);
  2479. root = api_add_bool(root, "Quiet", &opt_quiet, false);
  2480. root = api_add_bool(root, "Verbose", &opt_log_output, false);
  2481. root = api_add_bool(root, "Debug", &opt_debug, false);
  2482. root = api_add_bool(root, "RPCProto", &opt_protocol, false);
  2483. root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
  2484. root = api_add_bool(root, "WorkTime", &opt_worktime, false);
  2485. root = print_data(root, buf, isjson, false);
  2486. io_add(io_data, buf);
  2487. if (isjson && io_open)
  2488. io_close(io_data);
  2489. }
  2490. static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2491. {
  2492. char *comma;
  2493. int value;
  2494. if (param == NULL || *param == '\0') {
  2495. message(io_data, MSG_CONPAR, 0, NULL, isjson);
  2496. return;
  2497. }
  2498. comma = strchr(param, ',');
  2499. if (!comma) {
  2500. message(io_data, MSG_CONVAL, 0, param, isjson);
  2501. return;
  2502. }
  2503. *(comma++) = '\0';
  2504. if (strcasecmp(param, "coinbase-sig") == 0) {
  2505. free(opt_coinbase_sig);
  2506. opt_coinbase_sig = strdup(comma);
  2507. message(io_data, MSG_SETCONFIG, 1, param, isjson);
  2508. return;
  2509. }
  2510. value = atoi(comma);
  2511. if (value < 0 || value > 9999) {
  2512. message(io_data, MSG_INVNUM, value, param, isjson);
  2513. return;
  2514. }
  2515. if (strcasecmp(param, "queue") == 0)
  2516. opt_queue = value;
  2517. else if (strcasecmp(param, "scantime") == 0)
  2518. opt_scantime = value;
  2519. else if (strcasecmp(param, "expiry") == 0)
  2520. opt_expiry = value;
  2521. else {
  2522. message(io_data, MSG_UNKCON, 0, param, isjson);
  2523. return;
  2524. }
  2525. message(io_data, MSG_SETCONFIG, value, param, isjson);
  2526. }
  2527. #ifdef HAVE_AN_FPGA
  2528. static void pgaset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2529. {
  2530. struct cgpu_info *cgpu;
  2531. struct device_drv *drv;
  2532. char buf[TMPBUFSIZ];
  2533. int numpga = numpgas();
  2534. if (numpga == 0) {
  2535. message(io_data, MSG_PGANON, 0, NULL, isjson);
  2536. return;
  2537. }
  2538. if (param == NULL || *param == '\0') {
  2539. message(io_data, MSG_MISID, 0, NULL, isjson);
  2540. return;
  2541. }
  2542. char *opt = strchr(param, ',');
  2543. if (opt)
  2544. *(opt++) = '\0';
  2545. if (!opt || !*opt) {
  2546. message(io_data, MSG_MISPGAOPT, 0, NULL, isjson);
  2547. return;
  2548. }
  2549. int id = atoi(param);
  2550. if (id < 0 || id >= numpga) {
  2551. message(io_data, MSG_INVPGA, id, NULL, isjson);
  2552. return;
  2553. }
  2554. int dev = pgadevice(id);
  2555. if (dev < 0) { // Should never happen
  2556. message(io_data, MSG_INVPGA, id, NULL, isjson);
  2557. return;
  2558. }
  2559. cgpu = get_devices(dev);
  2560. drv = cgpu->drv;
  2561. char *set = strchr(opt, ',');
  2562. if (set)
  2563. *(set++) = '\0';
  2564. if (!drv->set_device)
  2565. message(io_data, MSG_PGANOSET, id, NULL, isjson);
  2566. else {
  2567. char *ret = drv->set_device(cgpu, opt, set, buf);
  2568. if (ret) {
  2569. if (strcasecmp(opt, "help") == 0)
  2570. message(io_data, MSG_PGAHELP, id, ret, isjson);
  2571. else
  2572. message(io_data, MSG_PGASETERR, id, ret, isjson);
  2573. } else
  2574. message(io_data, MSG_PGASETOK, id, NULL, isjson);
  2575. }
  2576. }
  2577. #endif
  2578. static void dozero(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2579. {
  2580. if (param == NULL || *param == '\0') {
  2581. message(io_data, MSG_ZERMIS, 0, NULL, isjson);
  2582. return;
  2583. }
  2584. char *sum = strchr(param, ',');
  2585. if (sum)
  2586. *(sum++) = '\0';
  2587. if (!sum || !*sum) {
  2588. message(io_data, MSG_MISBOOL, 0, NULL, isjson);
  2589. return;
  2590. }
  2591. bool all = false;
  2592. bool bs = false;
  2593. if (strcasecmp(param, "all") == 0)
  2594. all = true;
  2595. else if (strcasecmp(param, "bestshare") == 0)
  2596. bs = true;
  2597. if (all == false && bs == false) {
  2598. message(io_data, MSG_ZERINV, 0, param, isjson);
  2599. return;
  2600. }
  2601. *sum = tolower(*sum);
  2602. if (*sum != 't' && *sum != 'f') {
  2603. message(io_data, MSG_INVBOOL, 0, NULL, isjson);
  2604. return;
  2605. }
  2606. bool dosum = (*sum == 't');
  2607. if (dosum)
  2608. print_summary();
  2609. if (all)
  2610. zero_stats();
  2611. if (bs)
  2612. zero_bestshare();
  2613. if (dosum)
  2614. message(io_data, MSG_ZERSUM, 0, all ? "All" : "BestShare", isjson);
  2615. else
  2616. message(io_data, MSG_ZERNOSUM, 0, all ? "All" : "BestShare", isjson);
  2617. }
  2618. static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group);
  2619. struct CMDS {
  2620. char *name;
  2621. void (*func)(struct io_data *, SOCKETTYPE, char *, bool, char);
  2622. bool iswritemode;
  2623. } cmds[] = {
  2624. { "version", apiversion, false },
  2625. { "config", minerconfig, false },
  2626. { "devscan", devscan, false },
  2627. { "devs", devstatus, false },
  2628. { "devdetail", devdetail, false },
  2629. { "pools", poolstatus, false },
  2630. { "summary", summary, false },
  2631. #ifdef HAVE_OPENCL
  2632. { "gpuenable", gpuenable, true },
  2633. { "gpudisable", gpudisable, true },
  2634. { "gpurestart", gpurestart, true },
  2635. { "gpu", gpudev, false },
  2636. #endif
  2637. #ifdef HAVE_AN_FPGA
  2638. { "pga", pgadev, false },
  2639. { "pgaenable", pgaenable, true },
  2640. { "pgadisable", pgadisable, true },
  2641. { "pgaidentify", pgaidentify, true },
  2642. #endif
  2643. #ifdef WANT_CPUMINE
  2644. { "cpu", cpudev, false },
  2645. #endif
  2646. { "gpucount", gpucount, false },
  2647. { "pgacount", pgacount, false },
  2648. { "cpucount", cpucount, false },
  2649. { "switchpool", switchpool, true },
  2650. { "addpool", addpool, true },
  2651. { "poolpriority", poolpriority, true },
  2652. { "enablepool", enablepool, true },
  2653. { "disablepool", disablepool, true },
  2654. { "removepool", removepool, true },
  2655. #ifdef HAVE_OPENCL
  2656. { "gpuintensity", gpuintensity, true },
  2657. { "gpumem", gpumem, true },
  2658. { "gpuengine", gpuengine, true },
  2659. { "gpufan", gpufan, true },
  2660. { "gpuvddc", gpuvddc, true },
  2661. #endif
  2662. { "save", dosave, true },
  2663. { "quit", doquit, true },
  2664. { "privileged", privileged, true },
  2665. { "notify", notify, false },
  2666. { "devdetails", devdetails, false },
  2667. { "restart", dorestart, true },
  2668. { "stats", minerstats, false },
  2669. { "check", checkcommand, false },
  2670. { "failover-only", failoveronly, true },
  2671. { "coin", minecoin, false },
  2672. { "debug", debugstate, true },
  2673. { "setconfig", setconfig, true },
  2674. #ifdef HAVE_AN_FPGA
  2675. { "pgaset", pgaset, true },
  2676. #endif
  2677. { "zero", dozero, true },
  2678. { NULL, NULL, false }
  2679. };
  2680. static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
  2681. {
  2682. struct api_data *root = NULL;
  2683. char buf[TMPBUFSIZ];
  2684. bool io_open;
  2685. char cmdbuf[100];
  2686. bool found, access;
  2687. int i;
  2688. if (param == NULL || *param == '\0') {
  2689. message(io_data, MSG_MISCHK, 0, NULL, isjson);
  2690. return;
  2691. }
  2692. found = false;
  2693. access = false;
  2694. for (i = 0; cmds[i].name != NULL; i++) {
  2695. if (strcmp(cmds[i].name, param) == 0) {
  2696. found = true;
  2697. sprintf(cmdbuf, "|%s|", param);
  2698. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  2699. access = true;
  2700. break;
  2701. }
  2702. }
  2703. message(io_data, MSG_CHECK, 0, NULL, isjson);
  2704. io_open = io_add(io_data, isjson ? COMSTR JSON_CHECK : _CHECK COMSTR);
  2705. root = api_add_const(root, "Exists", found ? YES : NO, false);
  2706. root = api_add_const(root, "Access", access ? YES : NO, false);
  2707. root = print_data(root, buf, isjson, false);
  2708. io_add(io_data, buf);
  2709. if (isjson && io_open)
  2710. io_close(io_data);
  2711. }
  2712. static void send_result(struct io_data *io_data, SOCKETTYPE c, bool isjson)
  2713. {
  2714. char buf[SOCKBUFSIZ + sizeof(JSON_CLOSE) + sizeof(JSON_END)];
  2715. int count, res, tosend, len, n;
  2716. strcpy(buf, io_data->ptr);
  2717. if (io_data->close)
  2718. strcat(buf, JSON_CLOSE);
  2719. if (isjson) {
  2720. if (io_data->full)
  2721. strcat(buf, JSON_END_TRUNCATED);
  2722. else
  2723. strcat(buf, JSON_END);
  2724. }
  2725. len = strlen(buf);
  2726. tosend = len+1;
  2727. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", tosend, buf, len > 10 ? "..." : BLANK);
  2728. count = 0;
  2729. while (count++ < 5 && tosend > 0) {
  2730. // allow 50ms per attempt
  2731. struct timeval timeout = {0, 50000};
  2732. fd_set wd;
  2733. FD_ZERO(&wd);
  2734. FD_SET(c, &wd);
  2735. if ((res = select(c + 1, NULL, &wd, NULL, &timeout)) < 1) {
  2736. applog(LOG_WARNING, "API: send select failed (%d)", res);
  2737. return;
  2738. }
  2739. n = send(c, buf, tosend, 0);
  2740. if (SOCKETFAIL(n)) {
  2741. if (sock_blocks())
  2742. continue;
  2743. applog(LOG_WARNING, "API: send (%d) failed: %s", tosend, SOCKERRMSG);
  2744. return;
  2745. } else {
  2746. if (count <= 1) {
  2747. if (n == tosend)
  2748. applog(LOG_DEBUG, "API: sent all of %d first go", tosend);
  2749. else
  2750. applog(LOG_DEBUG, "API: sent %d of %d first go", n, tosend);
  2751. } else {
  2752. if (n == tosend)
  2753. applog(LOG_DEBUG, "API: sent all of remaining %d (count=%d)", tosend, count);
  2754. else
  2755. applog(LOG_DEBUG, "API: sent %d of remaining %d (count=%d)", n, tosend, count);
  2756. }
  2757. tosend -= n;
  2758. }
  2759. }
  2760. }
  2761. static void tidyup(__maybe_unused void *arg)
  2762. {
  2763. mutex_lock(&quit_restart_lock);
  2764. SOCKETTYPE *apisock = (SOCKETTYPE *)arg;
  2765. bye = true;
  2766. if (*apisock != INVSOCK) {
  2767. shutdown(*apisock, SHUT_RDWR);
  2768. CLOSESOCKET(*apisock);
  2769. *apisock = INVSOCK;
  2770. }
  2771. if (ipaccess != NULL) {
  2772. free(ipaccess);
  2773. ipaccess = NULL;
  2774. }
  2775. io_free();
  2776. mutex_unlock(&quit_restart_lock);
  2777. }
  2778. /*
  2779. * Interpret --api-groups G:cmd1:cmd2:cmd3,P:cmd4,*,...
  2780. */
  2781. static void setup_groups()
  2782. {
  2783. char *api_groups = opt_api_groups ? opt_api_groups : (char *)BLANK;
  2784. char *buf, *ptr, *next, *colon;
  2785. char group;
  2786. char commands[TMPBUFSIZ];
  2787. char cmdbuf[100];
  2788. char *cmd;
  2789. bool addstar, did;
  2790. int i;
  2791. buf = malloc(strlen(api_groups) + 1);
  2792. if (unlikely(!buf))
  2793. quit(1, "Failed to malloc ipgroups buf");
  2794. strcpy(buf, api_groups);
  2795. next = buf;
  2796. // for each group defined
  2797. while (next && *next) {
  2798. ptr = next;
  2799. next = strchr(ptr, ',');
  2800. if (next)
  2801. *(next++) = '\0';
  2802. // Validate the group
  2803. if (*(ptr+1) != ':') {
  2804. colon = strchr(ptr, ':');
  2805. if (colon)
  2806. *colon = '\0';
  2807. quit(1, "API invalid group name '%s'", ptr);
  2808. }
  2809. group = GROUP(*ptr);
  2810. if (!VALIDGROUP(group))
  2811. quit(1, "API invalid group name '%c'", *ptr);
  2812. if (group == PRIVGROUP)
  2813. quit(1, "API group name can't be '%c'", PRIVGROUP);
  2814. if (group == NOPRIVGROUP)
  2815. quit(1, "API group name can't be '%c'", NOPRIVGROUP);
  2816. if (apigroups[GROUPOFFSET(group)].commands != NULL)
  2817. quit(1, "API duplicate group name '%c'", *ptr);
  2818. ptr += 2;
  2819. // Validate the command list (and handle '*')
  2820. cmd = &(commands[0]);
  2821. *(cmd++) = SEPARATOR;
  2822. *cmd = '\0';
  2823. addstar = false;
  2824. while (ptr && *ptr) {
  2825. colon = strchr(ptr, ':');
  2826. if (colon)
  2827. *(colon++) = '\0';
  2828. if (strcmp(ptr, "*") == 0)
  2829. addstar = true;
  2830. else {
  2831. did = false;
  2832. for (i = 0; cmds[i].name != NULL; i++) {
  2833. if (strcasecmp(ptr, cmds[i].name) == 0) {
  2834. did = true;
  2835. break;
  2836. }
  2837. }
  2838. if (did) {
  2839. // skip duplicates
  2840. sprintf(cmdbuf, "|%s|", cmds[i].name);
  2841. if (strstr(commands, cmdbuf) == NULL) {
  2842. strcpy(cmd, cmds[i].name);
  2843. cmd += strlen(cmds[i].name);
  2844. *(cmd++) = SEPARATOR;
  2845. *cmd = '\0';
  2846. }
  2847. } else {
  2848. quit(1, "API unknown command '%s' in group '%c'", ptr, group);
  2849. }
  2850. }
  2851. ptr = colon;
  2852. }
  2853. // * = allow all non-iswritemode commands
  2854. if (addstar) {
  2855. for (i = 0; cmds[i].name != NULL; i++) {
  2856. if (cmds[i].iswritemode == false) {
  2857. // skip duplicates
  2858. sprintf(cmdbuf, "|%s|", cmds[i].name);
  2859. if (strstr(commands, cmdbuf) == NULL) {
  2860. strcpy(cmd, cmds[i].name);
  2861. cmd += strlen(cmds[i].name);
  2862. *(cmd++) = SEPARATOR;
  2863. *cmd = '\0';
  2864. }
  2865. }
  2866. }
  2867. }
  2868. ptr = apigroups[GROUPOFFSET(group)].commands = malloc(strlen(commands) + 1);
  2869. if (unlikely(!ptr))
  2870. quit(1, "Failed to malloc group commands buf");
  2871. strcpy(ptr, commands);
  2872. }
  2873. // Now define R (NOPRIVGROUP) as all non-iswritemode commands
  2874. cmd = &(commands[0]);
  2875. *(cmd++) = SEPARATOR;
  2876. *cmd = '\0';
  2877. for (i = 0; cmds[i].name != NULL; i++) {
  2878. if (cmds[i].iswritemode == false) {
  2879. strcpy(cmd, cmds[i].name);
  2880. cmd += strlen(cmds[i].name);
  2881. *(cmd++) = SEPARATOR;
  2882. *cmd = '\0';
  2883. }
  2884. }
  2885. ptr = apigroups[GROUPOFFSET(NOPRIVGROUP)].commands = malloc(strlen(commands) + 1);
  2886. if (unlikely(!ptr))
  2887. quit(1, "Failed to malloc noprivgroup commands buf");
  2888. strcpy(ptr, commands);
  2889. // W (PRIVGROUP) is handled as a special case since it simply means all commands
  2890. free(buf);
  2891. return;
  2892. }
  2893. /*
  2894. * Interpret [W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  2895. * special case of 0/0 allows /0 (means all IP addresses)
  2896. */
  2897. #define ALLIP4 "0/0"
  2898. /*
  2899. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  2900. */
  2901. static void setup_ipaccess()
  2902. {
  2903. char *buf, *ptr, *comma, *slash, *dot;
  2904. int ipcount, mask, octet, i;
  2905. char group;
  2906. buf = malloc(strlen(opt_api_allow) + 1);
  2907. if (unlikely(!buf))
  2908. quit(1, "Failed to malloc ipaccess buf");
  2909. strcpy(buf, opt_api_allow);
  2910. ipcount = 1;
  2911. ptr = buf;
  2912. while (*ptr)
  2913. if (*(ptr++) == ',')
  2914. ipcount++;
  2915. // possibly more than needed, but never less
  2916. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  2917. if (unlikely(!ipaccess))
  2918. quit(1, "Failed to calloc ipaccess");
  2919. ips = 0;
  2920. ptr = buf;
  2921. while (ptr && *ptr) {
  2922. while (*ptr == ' ' || *ptr == '\t')
  2923. ptr++;
  2924. if (*ptr == ',') {
  2925. ptr++;
  2926. continue;
  2927. }
  2928. comma = strchr(ptr, ',');
  2929. if (comma)
  2930. *(comma++) = '\0';
  2931. group = NOPRIVGROUP;
  2932. if (isalpha(*ptr) && *(ptr+1) == ':') {
  2933. if (DEFINEDGROUP(*ptr))
  2934. group = GROUP(*ptr);
  2935. ptr += 2;
  2936. }
  2937. ipaccess[ips].group = group;
  2938. if (strcmp(ptr, ALLIP4) == 0)
  2939. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  2940. else {
  2941. slash = strchr(ptr, '/');
  2942. if (!slash)
  2943. ipaccess[ips].mask = 0xffffffff;
  2944. else {
  2945. *(slash++) = '\0';
  2946. mask = atoi(slash);
  2947. if (mask < 1 || mask > 32)
  2948. goto popipo; // skip invalid/zero
  2949. ipaccess[ips].mask = 0;
  2950. while (mask-- >= 0) {
  2951. octet = 1 << (mask % 8);
  2952. ipaccess[ips].mask |= (octet << (24 - (8 * (mask >> 3))));
  2953. }
  2954. }
  2955. ipaccess[ips].ip = 0; // missing default to '.0'
  2956. for (i = 0; ptr && (i < 4); i++) {
  2957. dot = strchr(ptr, '.');
  2958. if (dot)
  2959. *(dot++) = '\0';
  2960. octet = atoi(ptr);
  2961. if (octet < 0 || octet > 0xff)
  2962. goto popipo; // skip invalid
  2963. ipaccess[ips].ip |= (octet << (24 - (i * 8)));
  2964. ptr = dot;
  2965. }
  2966. ipaccess[ips].ip &= ipaccess[ips].mask;
  2967. }
  2968. ips++;
  2969. popipo:
  2970. ptr = comma;
  2971. }
  2972. free(buf);
  2973. }
  2974. static void *quit_thread(__maybe_unused void *userdata)
  2975. {
  2976. RenameThread("rpc_quit");
  2977. // allow thread creator to finish whatever it's doing
  2978. mutex_lock(&quit_restart_lock);
  2979. mutex_unlock(&quit_restart_lock);
  2980. if (opt_debug)
  2981. applog(LOG_DEBUG, "API: killing BFGMiner");
  2982. kill_work();
  2983. return NULL;
  2984. }
  2985. static void *restart_thread(__maybe_unused void *userdata)
  2986. {
  2987. RenameThread("rpc_restart");
  2988. // allow thread creator to finish whatever it's doing
  2989. mutex_lock(&quit_restart_lock);
  2990. mutex_unlock(&quit_restart_lock);
  2991. if (opt_debug)
  2992. applog(LOG_DEBUG, "API: restarting BFGMiner");
  2993. app_restart();
  2994. return NULL;
  2995. }
  2996. void api(int api_thr_id)
  2997. {
  2998. struct io_data *io_data;
  2999. struct thr_info bye_thr;
  3000. char buf[TMPBUFSIZ];
  3001. char param_buf[TMPBUFSIZ];
  3002. const char *localaddr = "127.0.0.1";
  3003. SOCKETTYPE c;
  3004. int n, bound;
  3005. char *connectaddr;
  3006. const char *binderror;
  3007. time_t bindstart;
  3008. short int port = opt_api_port;
  3009. struct sockaddr_in serv;
  3010. struct sockaddr_in cli;
  3011. socklen_t clisiz;
  3012. char cmdbuf[100];
  3013. char *cmd;
  3014. char *param;
  3015. bool addrok;
  3016. char group;
  3017. json_error_t json_err;
  3018. json_t *json_config;
  3019. json_t *json_val;
  3020. bool isjson;
  3021. bool did;
  3022. int i;
  3023. SOCKETTYPE *apisock;
  3024. apisock = malloc(sizeof(*apisock));
  3025. *apisock = INVSOCK;
  3026. if (!opt_api_listen) {
  3027. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  3028. return;
  3029. }
  3030. io_data = sock_io_new();
  3031. mutex_init(&quit_restart_lock);
  3032. pthread_cleanup_push(tidyup, (void *)apisock);
  3033. my_thr_id = api_thr_id;
  3034. setup_groups();
  3035. if (opt_api_allow) {
  3036. setup_ipaccess();
  3037. if (ips == 0) {
  3038. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  3039. return;
  3040. }
  3041. }
  3042. /* This should be done before curl in needed
  3043. * to ensure curl has already called WSAStartup() in windows */
  3044. nmsleep(opt_log_interval*1000);
  3045. *apisock = socket(AF_INET, SOCK_STREAM, 0);
  3046. if (*apisock == INVSOCK) {
  3047. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3048. return;
  3049. }
  3050. memset(&serv, 0, sizeof(serv));
  3051. serv.sin_family = AF_INET;
  3052. if (!opt_api_allow && !opt_api_network) {
  3053. serv.sin_addr.s_addr = inet_addr(localaddr);
  3054. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  3055. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3056. return;
  3057. }
  3058. }
  3059. serv.sin_port = htons(port);
  3060. #ifndef WIN32
  3061. // On linux with SO_REUSEADDR, bind will get the port if the previous
  3062. // socket is closed (even if it is still in TIME_WAIT) but fail if
  3063. // another program has it open - which is what we want
  3064. int optval = 1;
  3065. // If it doesn't work, we don't really care - just show a debug message
  3066. if (SOCKETFAIL(setsockopt(*apisock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval))))
  3067. applog(LOG_DEBUG, "API setsockopt SO_REUSEADDR failed (ignored): %s", SOCKERRMSG);
  3068. #else
  3069. // On windows a 2nd program can bind to a port>1024 already in use unless
  3070. // SO_EXCLUSIVEADDRUSE is used - however then the bind to a closed port
  3071. // in TIME_WAIT will fail until the timeout - so we leave the options alone
  3072. #endif
  3073. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  3074. bound = 0;
  3075. bindstart = time(NULL);
  3076. while (bound == 0) {
  3077. if (SOCKETFAIL(bind(*apisock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  3078. binderror = SOCKERRMSG;
  3079. if ((time(NULL) - bindstart) > 61)
  3080. break;
  3081. else {
  3082. applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
  3083. nmsleep(30000);
  3084. }
  3085. } else
  3086. bound = 1;
  3087. }
  3088. if (bound == 0) {
  3089. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  3090. return;
  3091. }
  3092. if (SOCKETFAIL(listen(*apisock, QUEUE))) {
  3093. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3094. CLOSESOCKET(*apisock);
  3095. return;
  3096. }
  3097. if (opt_api_allow)
  3098. applog(LOG_WARNING, "API running in IP access mode on port %d", port);
  3099. else {
  3100. if (opt_api_network)
  3101. applog(LOG_WARNING, "API running in UNRESTRICTED read access mode on port %d", port);
  3102. else
  3103. applog(LOG_WARNING, "API running in local read access mode on port %d", port);
  3104. }
  3105. while (!bye) {
  3106. clisiz = sizeof(cli);
  3107. if (SOCKETFAIL(c = accept(*apisock, (struct sockaddr *)(&cli), &clisiz))) {
  3108. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3109. goto die;
  3110. }
  3111. connectaddr = inet_ntoa(cli.sin_addr);
  3112. addrok = false;
  3113. group = NOPRIVGROUP;
  3114. if (opt_api_allow) {
  3115. int client_ip = htonl(cli.sin_addr.s_addr);
  3116. for (i = 0; i < ips; i++) {
  3117. if ((client_ip & ipaccess[i].mask) == ipaccess[i].ip) {
  3118. addrok = true;
  3119. group = ipaccess[i].group;
  3120. break;
  3121. }
  3122. }
  3123. } else {
  3124. if (opt_api_network)
  3125. addrok = true;
  3126. else
  3127. addrok = (strcmp(connectaddr, localaddr) == 0);
  3128. }
  3129. if (opt_debug)
  3130. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  3131. if (addrok) {
  3132. n = recv(c, &buf[0], TMPBUFSIZ-1, 0);
  3133. if (SOCKETFAIL(n))
  3134. buf[0] = '\0';
  3135. else
  3136. buf[n] = '\0';
  3137. if (opt_debug) {
  3138. if (SOCKETFAIL(n))
  3139. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  3140. else
  3141. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  3142. }
  3143. if (!SOCKETFAIL(n)) {
  3144. // the time of the request in now
  3145. when = time(NULL);
  3146. io_reinit(io_data);
  3147. did = false;
  3148. if (*buf != ISJSON) {
  3149. isjson = false;
  3150. param = strchr(buf, SEPARATOR);
  3151. if (param != NULL)
  3152. *(param++) = '\0';
  3153. cmd = buf;
  3154. }
  3155. else {
  3156. isjson = true;
  3157. param = NULL;
  3158. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  3159. json_config = json_loadb(buf, n, 0, &json_err);
  3160. #elif JANSSON_MAJOR_VERSION > 1
  3161. json_config = json_loads(buf, 0, &json_err);
  3162. #else
  3163. json_config = json_loads(buf, &json_err);
  3164. #endif
  3165. if (!json_is_object(json_config)) {
  3166. message(io_data, MSG_INVJSON, 0, NULL, isjson);
  3167. send_result(io_data, c, isjson);
  3168. did = true;
  3169. }
  3170. else {
  3171. json_val = json_object_get(json_config, JSON_COMMAND);
  3172. if (json_val == NULL) {
  3173. message(io_data, MSG_MISCMD, 0, NULL, isjson);
  3174. send_result(io_data, c, isjson);
  3175. did = true;
  3176. }
  3177. else {
  3178. if (!json_is_string(json_val)) {
  3179. message(io_data, MSG_INVCMD, 0, NULL, isjson);
  3180. send_result(io_data, c, isjson);
  3181. did = true;
  3182. }
  3183. else {
  3184. cmd = (char *)json_string_value(json_val);
  3185. json_val = json_object_get(json_config, JSON_PARAMETER);
  3186. if (json_is_string(json_val))
  3187. param = (char *)json_string_value(json_val);
  3188. else if (json_is_integer(json_val)) {
  3189. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  3190. param = param_buf;
  3191. } else if (json_is_real(json_val)) {
  3192. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  3193. param = param_buf;
  3194. }
  3195. }
  3196. }
  3197. }
  3198. }
  3199. if (!did)
  3200. for (i = 0; cmds[i].name != NULL; i++) {
  3201. if (strcmp(cmd, cmds[i].name) == 0) {
  3202. sprintf(cmdbuf, "|%s|", cmd);
  3203. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  3204. (cmds[i].func)(io_data, c, param, isjson, group);
  3205. else {
  3206. message(io_data, MSG_ACCDENY, 0, cmds[i].name, isjson);
  3207. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  3208. }
  3209. send_result(io_data, c, isjson);
  3210. did = true;
  3211. break;
  3212. }
  3213. }
  3214. if (isjson)
  3215. json_decref(json_config);
  3216. if (!did) {
  3217. message(io_data, MSG_INVCMD, 0, NULL, isjson);
  3218. send_result(io_data, c, isjson);
  3219. }
  3220. }
  3221. }
  3222. CLOSESOCKET(c);
  3223. }
  3224. die:
  3225. /* Blank line fix for older compilers since pthread_cleanup_pop is a
  3226. * macro that gets confused by a label existing immediately before it
  3227. */
  3228. ;
  3229. pthread_cleanup_pop(true);
  3230. if (opt_debug)
  3231. applog(LOG_DEBUG, "API: terminating due to: %s",
  3232. do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
  3233. mutex_lock(&quit_restart_lock);
  3234. if (do_a_restart) {
  3235. if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
  3236. mutex_unlock(&quit_restart_lock);
  3237. quit(1, "API failed to initiate a restart - aborting");
  3238. }
  3239. pthread_detach(bye_thr.pth);
  3240. } else if (do_a_quit) {
  3241. if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
  3242. mutex_unlock(&quit_restart_lock);
  3243. quit(1, "API failed to initiate a clean quit - aborting");
  3244. }
  3245. pthread_detach(bye_thr.pth);
  3246. }
  3247. mutex_unlock(&quit_restart_lock);
  3248. }