api.c 89 KB

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