api.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552
  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. char *ptr;
  1740. if (param == NULL || *param == '\0') {
  1741. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  1742. return;
  1743. }
  1744. if (!pooldetails(param, &url, &user, &pass)) {
  1745. ptr = escape_string(param, isjson);
  1746. strcpy(io_buffer, message(MSG_INVPDP, 0, ptr, isjson));
  1747. if (ptr != param)
  1748. free(ptr);
  1749. ptr = NULL;
  1750. return;
  1751. }
  1752. add_pool_details(true, url, user, pass);
  1753. ptr = escape_string(url, isjson);
  1754. strcpy(io_buffer, message(MSG_ADDPOOL, 0, ptr, isjson));
  1755. if (ptr != url)
  1756. free(ptr);
  1757. ptr = NULL;
  1758. }
  1759. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1760. {
  1761. struct pool *pool;
  1762. int id;
  1763. if (total_pools == 0) {
  1764. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1765. return;
  1766. }
  1767. if (param == NULL || *param == '\0') {
  1768. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1769. return;
  1770. }
  1771. id = atoi(param);
  1772. if (id < 0 || id >= total_pools) {
  1773. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1774. return;
  1775. }
  1776. pool = pools[id];
  1777. if (pool->enabled == POOL_ENABLED) {
  1778. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  1779. return;
  1780. }
  1781. pool->enabled = POOL_ENABLED;
  1782. if (pool->prio < current_pool()->prio)
  1783. switch_pools(pool);
  1784. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  1785. }
  1786. static void poolpriority(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1787. {
  1788. char *ptr, *next;
  1789. int i, pr, prio = 0;
  1790. // TODO: all cgminer code needs a mutex added everywhere for change
  1791. // access to total_pools and also parts of the pools[] array,
  1792. // just copying total_pools here wont solve that
  1793. if (total_pools == 0) {
  1794. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1795. return;
  1796. }
  1797. if (param == NULL || *param == '\0') {
  1798. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1799. return;
  1800. }
  1801. bool pools_changed[total_pools];
  1802. int new_prio[total_pools];
  1803. for (i = 0; i < total_pools; ++i)
  1804. pools_changed[i] = false;
  1805. next = param;
  1806. while (next && *next) {
  1807. ptr = next;
  1808. next = strchr(ptr, ',');
  1809. if (next)
  1810. *(next++) = '\0';
  1811. i = atoi(ptr);
  1812. if (i < 0 || i >= total_pools) {
  1813. strcpy(io_buffer, message(MSG_INVPID, i, NULL, isjson));
  1814. return;
  1815. }
  1816. if (pools_changed[i]) {
  1817. strcpy(io_buffer, message(MSG_DUPPID, i, NULL, isjson));
  1818. return;
  1819. }
  1820. pools_changed[i] = true;
  1821. new_prio[i] = prio++;
  1822. }
  1823. // Only change them if no errors
  1824. for (i = 0; i < total_pools; i++) {
  1825. if (pools_changed[i])
  1826. pools[i]->prio = new_prio[i];
  1827. }
  1828. // In priority order, cycle through the unchanged pools and append them
  1829. for (pr = 0; pr < total_pools; pr++)
  1830. for (i = 0; i < total_pools; i++) {
  1831. if (!pools_changed[i] && pools[i]->prio == pr) {
  1832. pools[i]->prio = prio++;
  1833. pools_changed[i] = true;
  1834. break;
  1835. }
  1836. }
  1837. if (current_pool()->prio)
  1838. switch_pools(NULL);
  1839. strcpy(io_buffer, message(MSG_POOLPRIO, 0, NULL, isjson));
  1840. }
  1841. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1842. {
  1843. struct pool *pool;
  1844. int id;
  1845. if (total_pools == 0) {
  1846. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1847. return;
  1848. }
  1849. if (param == NULL || *param == '\0') {
  1850. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1851. return;
  1852. }
  1853. id = atoi(param);
  1854. if (id < 0 || id >= total_pools) {
  1855. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1856. return;
  1857. }
  1858. pool = pools[id];
  1859. if (pool->enabled == POOL_DISABLED) {
  1860. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  1861. return;
  1862. }
  1863. if (enabled_pools <= 1) {
  1864. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  1865. return;
  1866. }
  1867. pool->enabled = POOL_DISABLED;
  1868. if (pool == current_pool())
  1869. switch_pools(NULL);
  1870. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  1871. }
  1872. static void removepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1873. {
  1874. struct pool *pool;
  1875. char *rpc_url;
  1876. bool dofree = false;
  1877. int id;
  1878. if (total_pools == 0) {
  1879. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1880. return;
  1881. }
  1882. if (param == NULL || *param == '\0') {
  1883. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1884. return;
  1885. }
  1886. id = atoi(param);
  1887. if (id < 0 || id >= total_pools) {
  1888. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1889. return;
  1890. }
  1891. if (total_pools <= 1) {
  1892. strcpy(io_buffer, message(MSG_REMLASTP, id, NULL, isjson));
  1893. return;
  1894. }
  1895. pool = pools[id];
  1896. if (pool == current_pool())
  1897. switch_pools(NULL);
  1898. if (pool == current_pool()) {
  1899. strcpy(io_buffer, message(MSG_ACTPOOL, id, NULL, isjson));
  1900. return;
  1901. }
  1902. pool->enabled = POOL_DISABLED;
  1903. rpc_url = escape_string(pool->rpc_url, isjson);
  1904. if (rpc_url != pool->rpc_url)
  1905. dofree = true;
  1906. remove_pool(pool);
  1907. strcpy(io_buffer, message(MSG_REMPOOL, id, rpc_url, isjson));
  1908. if (dofree)
  1909. free(rpc_url);
  1910. rpc_url = NULL;
  1911. }
  1912. #ifdef HAVE_OPENCL
  1913. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  1914. {
  1915. int id;
  1916. char *gpusep;
  1917. if (nDevs == 0) {
  1918. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1919. return false;
  1920. }
  1921. if (param == NULL || *param == '\0') {
  1922. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1923. return false;
  1924. }
  1925. gpusep = strchr(param, GPUSEP);
  1926. if (gpusep == NULL) {
  1927. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  1928. return false;
  1929. }
  1930. *(gpusep++) = '\0';
  1931. id = atoi(param);
  1932. if (id < 0 || id >= nDevs) {
  1933. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1934. return false;
  1935. }
  1936. *gpu = id;
  1937. *value = gpusep;
  1938. return true;
  1939. }
  1940. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1941. {
  1942. int id;
  1943. char *value;
  1944. int intensity;
  1945. char intensitystr[7];
  1946. if (!splitgpuvalue(param, &id, &value, isjson))
  1947. return;
  1948. if (!strncasecmp(value, DYNAMIC, 1)) {
  1949. gpus[id].dynamic = true;
  1950. strcpy(intensitystr, DYNAMIC);
  1951. }
  1952. else {
  1953. intensity = atoi(value);
  1954. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1955. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1956. return;
  1957. }
  1958. gpus[id].dynamic = false;
  1959. gpus[id].intensity = intensity;
  1960. sprintf(intensitystr, "%d", intensity);
  1961. }
  1962. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1963. }
  1964. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1965. {
  1966. #ifdef HAVE_ADL
  1967. int id;
  1968. char *value;
  1969. int clock;
  1970. if (!splitgpuvalue(param, &id, &value, isjson))
  1971. return;
  1972. clock = atoi(value);
  1973. if (set_memoryclock(id, clock))
  1974. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  1975. else
  1976. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  1977. #else
  1978. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1979. #endif
  1980. }
  1981. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1982. {
  1983. #ifdef HAVE_ADL
  1984. int id;
  1985. char *value;
  1986. int clock;
  1987. if (!splitgpuvalue(param, &id, &value, isjson))
  1988. return;
  1989. clock = atoi(value);
  1990. if (set_engineclock(id, clock))
  1991. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  1992. else
  1993. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  1994. #else
  1995. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1996. #endif
  1997. }
  1998. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1999. {
  2000. #ifdef HAVE_ADL
  2001. int id;
  2002. char *value;
  2003. int fan;
  2004. if (!splitgpuvalue(param, &id, &value, isjson))
  2005. return;
  2006. fan = atoi(value);
  2007. if (set_fanspeed(id, fan))
  2008. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  2009. else
  2010. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  2011. #else
  2012. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  2013. #endif
  2014. }
  2015. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2016. {
  2017. #ifdef HAVE_ADL
  2018. int id;
  2019. char *value;
  2020. float vddc;
  2021. if (!splitgpuvalue(param, &id, &value, isjson))
  2022. return;
  2023. vddc = atof(value);
  2024. if (set_vddc(id, vddc))
  2025. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  2026. else
  2027. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  2028. #else
  2029. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  2030. #endif
  2031. }
  2032. #endif
  2033. void doquit(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2034. {
  2035. if (isjson)
  2036. strcpy(io_buffer, JSON_START JSON_BYE);
  2037. else
  2038. strcpy(io_buffer, _BYE);
  2039. bye = true;
  2040. do_a_quit = true;
  2041. }
  2042. void dorestart(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2043. {
  2044. if (isjson)
  2045. strcpy(io_buffer, JSON_START JSON_RESTART);
  2046. else
  2047. strcpy(io_buffer, _RESTART);
  2048. bye = true;
  2049. do_a_restart = true;
  2050. }
  2051. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2052. {
  2053. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  2054. }
  2055. void notifystatus(int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
  2056. {
  2057. struct api_data *root = NULL;
  2058. char buf[TMPBUFSIZ];
  2059. char *reason;
  2060. if (cgpu->device_last_not_well == 0)
  2061. reason = REASON_NONE;
  2062. else
  2063. switch(cgpu->device_not_well_reason) {
  2064. case REASON_THREAD_FAIL_INIT:
  2065. reason = REASON_THREAD_FAIL_INIT_STR;
  2066. break;
  2067. case REASON_THREAD_ZERO_HASH:
  2068. reason = REASON_THREAD_ZERO_HASH_STR;
  2069. break;
  2070. case REASON_THREAD_FAIL_QUEUE:
  2071. reason = REASON_THREAD_FAIL_QUEUE_STR;
  2072. break;
  2073. case REASON_DEV_SICK_IDLE_60:
  2074. reason = REASON_DEV_SICK_IDLE_60_STR;
  2075. break;
  2076. case REASON_DEV_DEAD_IDLE_600:
  2077. reason = REASON_DEV_DEAD_IDLE_600_STR;
  2078. break;
  2079. case REASON_DEV_NOSTART:
  2080. reason = REASON_DEV_NOSTART_STR;
  2081. break;
  2082. case REASON_DEV_OVER_HEAT:
  2083. reason = REASON_DEV_OVER_HEAT_STR;
  2084. break;
  2085. case REASON_DEV_THERMAL_CUTOFF:
  2086. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  2087. break;
  2088. case REASON_DEV_COMMS_ERROR:
  2089. reason = REASON_DEV_COMMS_ERROR_STR;
  2090. break;
  2091. default:
  2092. reason = REASON_UNKNOWN_STR;
  2093. break;
  2094. }
  2095. // ALL counters (and only counters) must start the name with a '*'
  2096. // Simplifies future external support for identifying new counters
  2097. root = api_add_int(root, "NOTIFY", &device, false);
  2098. root = api_add_string(root, "Name", cgpu->api->name, false);
  2099. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  2100. root = api_add_time(root, "Last Well", &(cgpu->device_last_well), false);
  2101. root = api_add_time(root, "Last Not Well", &(cgpu->device_last_not_well), false);
  2102. root = api_add_string(root, "Reason Not Well", reason, false);
  2103. root = api_add_int(root, "*Thread Fail Init", &(cgpu->thread_fail_init_count), false);
  2104. root = api_add_int(root, "*Thread Zero Hash", &(cgpu->thread_zero_hash_count), false);
  2105. root = api_add_int(root, "*Thread Fail Queue", &(cgpu->thread_fail_queue_count), false);
  2106. root = api_add_int(root, "*Dev Sick Idle 60s", &(cgpu->dev_sick_idle_60_count), false);
  2107. root = api_add_int(root, "*Dev Dead Idle 600s", &(cgpu->dev_dead_idle_600_count), false);
  2108. root = api_add_int(root, "*Dev Nostart", &(cgpu->dev_nostart_count), false);
  2109. root = api_add_int(root, "*Dev Over Heat", &(cgpu->dev_over_heat_count), false);
  2110. root = api_add_int(root, "*Dev Thermal Cutoff", &(cgpu->dev_thermal_cutoff_count), false);
  2111. root = api_add_int(root, "*Dev Comms Error", &(cgpu->dev_comms_error_count), false);
  2112. root = api_add_int(root, "*Dev Throttle", &(cgpu->dev_throttle_count), false);
  2113. if (isjson && (device > 0))
  2114. strcat(io_buffer, COMMA);
  2115. root = print_data(root, buf, isjson);
  2116. strcat(io_buffer, buf);
  2117. }
  2118. static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
  2119. {
  2120. int i;
  2121. if (total_devices == 0) {
  2122. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  2123. return;
  2124. }
  2125. strcpy(io_buffer, message(MSG_NOTIFY, 0, NULL, isjson));
  2126. if (isjson) {
  2127. strcat(io_buffer, COMMA);
  2128. strcat(io_buffer, JSON_NOTIFY);
  2129. }
  2130. for (i = 0; i < total_devices; i++)
  2131. notifystatus(i, devices[i], isjson, group);
  2132. if (isjson)
  2133. strcat(io_buffer, JSON_CLOSE);
  2134. }
  2135. static void devdetails(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2136. {
  2137. struct api_data *root = NULL;
  2138. char buf[TMPBUFSIZ];
  2139. struct cgpu_info *cgpu;
  2140. int i;
  2141. if (total_devices == 0) {
  2142. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  2143. return;
  2144. }
  2145. strcpy(io_buffer, message(MSG_DEVDETAILS, 0, NULL, isjson));
  2146. if (isjson) {
  2147. strcat(io_buffer, COMMA);
  2148. strcat(io_buffer, JSON_DEVDETAILS);
  2149. }
  2150. for (i = 0; i < total_devices; i++) {
  2151. cgpu = devices[i];
  2152. root = api_add_int(root, "DEVDETAILS", &i, false);
  2153. root = api_add_string(root, "Name", cgpu->api->name, false);
  2154. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  2155. root = api_add_string(root, "Driver", cgpu->api->dname, false);
  2156. root = api_add_const(root, "Kernel", cgpu->kname ? : BLANK, false);
  2157. root = api_add_const(root, "Model", cgpu->name ? : BLANK, false);
  2158. root = api_add_const(root, "Device Path", cgpu->device_path ? : BLANK, false);
  2159. if (isjson && (i > 0))
  2160. strcat(io_buffer, COMMA);
  2161. root = print_data(root, buf, isjson);
  2162. strcat(io_buffer, buf);
  2163. }
  2164. if (isjson)
  2165. strcat(io_buffer, JSON_CLOSE);
  2166. }
  2167. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2168. {
  2169. char filename[PATH_MAX];
  2170. FILE *fcfg;
  2171. char *ptr;
  2172. if (param == NULL || *param == '\0') {
  2173. default_save_file(filename);
  2174. param = filename;
  2175. }
  2176. fcfg = fopen(param, "w");
  2177. if (!fcfg) {
  2178. ptr = escape_string(param, isjson);
  2179. strcpy(io_buffer, message(MSG_BADFN, 0, ptr, isjson));
  2180. if (ptr != param)
  2181. free(ptr);
  2182. ptr = NULL;
  2183. return;
  2184. }
  2185. write_config(fcfg);
  2186. fclose(fcfg);
  2187. ptr = escape_string(param, isjson);
  2188. strcpy(io_buffer, message(MSG_SAVED, 0, ptr, isjson));
  2189. if (ptr != param)
  2190. free(ptr);
  2191. ptr = NULL;
  2192. }
  2193. static int itemstats(int i, char *id, struct cgminer_stats *stats, struct cgminer_pool_stats *pool_stats, struct api_data *extra, bool isjson)
  2194. {
  2195. struct api_data *root = NULL;
  2196. char buf[TMPBUFSIZ];
  2197. root = api_add_int(root, "STATS", &i, false);
  2198. root = api_add_string(root, "ID", id, false);
  2199. root = api_add_elapsed(root, "Elapsed", &(total_secs), false);
  2200. root = api_add_uint32(root, "Calls", &(stats->getwork_calls), false);
  2201. root = api_add_timeval(root, "Wait", &(stats->getwork_wait), false);
  2202. root = api_add_timeval(root, "Max", &(stats->getwork_wait_max), false);
  2203. root = api_add_timeval(root, "Min", &(stats->getwork_wait_min), false);
  2204. if (pool_stats) {
  2205. root = api_add_uint32(root, "Pool Calls", &(pool_stats->getwork_calls), false);
  2206. root = api_add_uint32(root, "Pool Attempts", &(pool_stats->getwork_attempts), false);
  2207. root = api_add_timeval(root, "Pool Wait", &(pool_stats->getwork_wait), false);
  2208. root = api_add_timeval(root, "Pool Max", &(pool_stats->getwork_wait_max), false);
  2209. root = api_add_timeval(root, "Pool Min", &(pool_stats->getwork_wait_min), false);
  2210. root = api_add_double(root, "Pool Av", &(pool_stats->getwork_wait_rolling), false);
  2211. root = api_add_bool(root, "Work Had Roll Time", &(pool_stats->hadrolltime), false);
  2212. root = api_add_bool(root, "Work Can Roll", &(pool_stats->canroll), false);
  2213. root = api_add_bool(root, "Work Had Expire", &(pool_stats->hadexpire), false);
  2214. root = api_add_uint32(root, "Work Roll Time", &(pool_stats->rolltime), false);
  2215. root = api_add_diff(root, "Work Diff", &(pool_stats->last_diff), false);
  2216. root = api_add_diff(root, "Min Diff", &(pool_stats->min_diff), false);
  2217. root = api_add_diff(root, "Max Diff", &(pool_stats->max_diff), false);
  2218. root = api_add_uint32(root, "Min Diff Count", &(pool_stats->min_diff_count), false);
  2219. root = api_add_uint32(root, "Max Diff Count", &(pool_stats->max_diff_count), false);
  2220. }
  2221. if (extra)
  2222. root = api_add_extra(root, extra);
  2223. if (isjson && (i > 0))
  2224. strcat(io_buffer, COMMA);
  2225. root = print_data(root, buf, isjson);
  2226. strcat(io_buffer, buf);
  2227. return ++i;
  2228. }
  2229. static void minerstats(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2230. {
  2231. struct api_data *extra;
  2232. char id[20];
  2233. int i, j;
  2234. strcpy(io_buffer, message(MSG_MINESTATS, 0, NULL, isjson));
  2235. if (isjson) {
  2236. strcat(io_buffer, COMMA);
  2237. strcat(io_buffer, JSON_MINESTATS);
  2238. }
  2239. i = 0;
  2240. for (j = 0; j < total_devices; j++) {
  2241. struct cgpu_info *cgpu = devices[j];
  2242. if (cgpu && cgpu->api) {
  2243. if (cgpu->api->get_api_stats)
  2244. extra = cgpu->api->get_api_stats(cgpu);
  2245. else
  2246. extra = NULL;
  2247. sprintf(id, "%s%d", cgpu->api->name, cgpu->device_id);
  2248. i = itemstats(i, id, &(cgpu->cgminer_stats), NULL, extra, isjson);
  2249. }
  2250. }
  2251. for (j = 0; j < total_pools; j++) {
  2252. struct pool *pool = pools[j];
  2253. sprintf(id, "POOL%d", j);
  2254. i = itemstats(i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, isjson);
  2255. }
  2256. if (isjson)
  2257. strcat(io_buffer, JSON_CLOSE);
  2258. }
  2259. static void failoveronly(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2260. {
  2261. if (param == NULL || *param == '\0') {
  2262. strcpy(io_buffer, message(MSG_MISBOOL, 0, NULL, isjson));
  2263. return;
  2264. }
  2265. *param = tolower(*param);
  2266. if (*param != 't' && *param != 'f') {
  2267. strcpy(io_buffer, message(MSG_INVBOOL, 0, NULL, isjson));
  2268. return;
  2269. }
  2270. bool tf = (*param == 't');
  2271. opt_fail_only = tf;
  2272. strcpy(io_buffer, message(MSG_FOO, tf, NULL, isjson));
  2273. }
  2274. static void minecoin(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2275. {
  2276. struct api_data *root = NULL;
  2277. char buf[TMPBUFSIZ];
  2278. sprintf(io_buffer, isjson
  2279. ? "%s," JSON_MINECOIN
  2280. : "%s" _MINECOIN ",",
  2281. message(MSG_MINECOIN, 0, NULL, isjson));
  2282. #ifdef USE_SCRYPT
  2283. if (opt_scrypt)
  2284. root = api_add_const(root, "Hash Method", SCRYPTSTR, false);
  2285. else
  2286. #endif
  2287. root = api_add_const(root, "Hash Method", SHA256STR, false);
  2288. mutex_lock(&ch_lock);
  2289. if (current_fullhash && *current_fullhash) {
  2290. root = api_add_timeval(root, "Current Block Time", &block_timeval, true);
  2291. root = api_add_string(root, "Current Block Hash", current_fullhash, true);
  2292. } else {
  2293. struct timeval t = {0,0};
  2294. root = api_add_timeval(root, "Current Block Time", &t, true);
  2295. root = api_add_const(root, "Current Block Hash", BLANK, false);
  2296. }
  2297. mutex_unlock(&ch_lock);
  2298. root = api_add_bool(root, "LP", &have_longpoll, false);
  2299. root = print_data(root, buf, isjson);
  2300. if (isjson)
  2301. strcat(buf, JSON_CLOSE);
  2302. strcat(io_buffer, buf);
  2303. }
  2304. static void debugstate(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2305. {
  2306. struct api_data *root = NULL;
  2307. char buf[TMPBUFSIZ];
  2308. if (param == NULL)
  2309. param = (char *)BLANK;
  2310. else
  2311. *param = tolower(*param);
  2312. switch(*param) {
  2313. case 's':
  2314. opt_realquiet = true;
  2315. break;
  2316. case 'q':
  2317. opt_quiet ^= true;
  2318. break;
  2319. case 'v':
  2320. opt_log_output ^= true;
  2321. if (opt_log_output)
  2322. opt_quiet = false;
  2323. break;
  2324. case 'd':
  2325. opt_debug ^= true;
  2326. opt_log_output = opt_debug;
  2327. if (opt_debug)
  2328. opt_quiet = false;
  2329. break;
  2330. case 'r':
  2331. opt_protocol ^= true;
  2332. if (opt_protocol)
  2333. opt_quiet = false;
  2334. break;
  2335. case 'p':
  2336. want_per_device_stats ^= true;
  2337. opt_log_output = want_per_device_stats;
  2338. break;
  2339. case 'n':
  2340. opt_log_output = false;
  2341. opt_debug = false;
  2342. opt_quiet = false;
  2343. opt_protocol = false;
  2344. want_per_device_stats = false;
  2345. opt_worktime = false;
  2346. break;
  2347. case 'w':
  2348. opt_worktime ^= true;
  2349. break;
  2350. default:
  2351. // anything else just reports the settings
  2352. break;
  2353. }
  2354. sprintf(io_buffer, isjson
  2355. ? "%s," JSON_DEBUGSET
  2356. : "%s" _DEBUGSET ",",
  2357. message(MSG_DEBUGSET, 0, NULL, isjson));
  2358. root = api_add_bool(root, "Silent", &opt_realquiet, false);
  2359. root = api_add_bool(root, "Quiet", &opt_quiet, false);
  2360. root = api_add_bool(root, "Verbose", &opt_log_output, false);
  2361. root = api_add_bool(root, "Debug", &opt_debug, false);
  2362. root = api_add_bool(root, "RPCProto", &opt_protocol, false);
  2363. root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
  2364. root = api_add_bool(root, "WorkTime", &opt_worktime, false);
  2365. root = print_data(root, buf, isjson);
  2366. if (isjson)
  2367. strcat(buf, JSON_CLOSE);
  2368. strcat(io_buffer, buf);
  2369. }
  2370. static void setconfig(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2371. {
  2372. char *comma;
  2373. int value;
  2374. if (param == NULL || *param == '\0') {
  2375. strcpy(io_buffer, message(MSG_CONPAR, 0, NULL, isjson));
  2376. return;
  2377. }
  2378. comma = strchr(param, ',');
  2379. if (!comma) {
  2380. strcpy(io_buffer, message(MSG_CONVAL, 0, param, isjson));
  2381. return;
  2382. }
  2383. *(comma++) = '\0';
  2384. if (strcasecmp(param, "coinbase-sig") == 0) {
  2385. free(opt_coinbase_sig);
  2386. opt_coinbase_sig = strdup(comma);
  2387. strcpy(io_buffer, message(MSG_SETCONFIG, 1, param, isjson));
  2388. return;
  2389. }
  2390. value = atoi(comma);
  2391. if (value < 0 || value > 9999) {
  2392. strcpy(io_buffer, message(MSG_INVNUM, value, param, isjson));
  2393. return;
  2394. }
  2395. if (strcasecmp(param, "queue") == 0)
  2396. opt_queue = value;
  2397. else if (strcasecmp(param, "scantime") == 0)
  2398. opt_scantime = value;
  2399. else if (strcasecmp(param, "expiry") == 0)
  2400. opt_expiry = value;
  2401. else {
  2402. strcpy(io_buffer, message(MSG_UNKCON, 0, param, isjson));
  2403. return;
  2404. }
  2405. strcpy(io_buffer, message(MSG_SETCONFIG, value, param, isjson));
  2406. }
  2407. static void checkcommand(__maybe_unused SOCKETTYPE c, char *param, bool isjson, char group);
  2408. struct CMDS {
  2409. char *name;
  2410. void (*func)(SOCKETTYPE, char *, bool, char);
  2411. bool iswritemode;
  2412. } cmds[] = {
  2413. { "version", apiversion, false },
  2414. { "config", minerconfig, false },
  2415. { "devs", devstatus, false },
  2416. { "devdetail", devdetail, false },
  2417. { "pools", poolstatus, false },
  2418. { "summary", summary, false },
  2419. #ifdef HAVE_OPENCL
  2420. { "gpuenable", gpuenable, true },
  2421. { "gpudisable", gpudisable, true },
  2422. { "gpurestart", gpurestart, true },
  2423. { "gpu", gpudev, false },
  2424. #endif
  2425. #ifdef HAVE_AN_FPGA
  2426. { "pga", pgadev, false },
  2427. { "pgaenable", pgaenable, true },
  2428. { "pgadisable", pgadisable, true },
  2429. { "pgaidentify", pgaidentify, true },
  2430. #endif
  2431. #ifdef WANT_CPUMINE
  2432. { "cpu", cpudev, false },
  2433. #endif
  2434. { "gpucount", gpucount, false },
  2435. { "pgacount", pgacount, false },
  2436. { "cpucount", cpucount, false },
  2437. { "switchpool", switchpool, true },
  2438. { "addpool", addpool, true },
  2439. { "poolpriority", poolpriority, true },
  2440. { "enablepool", enablepool, true },
  2441. { "disablepool", disablepool, true },
  2442. { "removepool", removepool, true },
  2443. #ifdef HAVE_OPENCL
  2444. { "gpuintensity", gpuintensity, true },
  2445. { "gpumem", gpumem, true },
  2446. { "gpuengine", gpuengine, true },
  2447. { "gpufan", gpufan, true },
  2448. { "gpuvddc", gpuvddc, true },
  2449. #endif
  2450. { "save", dosave, true },
  2451. { "quit", doquit, true },
  2452. { "privileged", privileged, true },
  2453. { "notify", notify, false },
  2454. { "devdetails", devdetails, false },
  2455. { "restart", dorestart, true },
  2456. { "stats", minerstats, false },
  2457. { "check", checkcommand, false },
  2458. { "failover-only", failoveronly, true },
  2459. { "coin", minecoin, false },
  2460. { "debug", debugstate, true },
  2461. { "setconfig", setconfig, true },
  2462. { NULL, NULL, false }
  2463. };
  2464. static void checkcommand(__maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
  2465. {
  2466. struct api_data *root = NULL;
  2467. char buf[TMPBUFSIZ];
  2468. char cmdbuf[100];
  2469. bool found, access;
  2470. int i;
  2471. if (param == NULL || *param == '\0') {
  2472. strcpy(io_buffer, message(MSG_MISCHK, 0, NULL, isjson));
  2473. return;
  2474. }
  2475. found = false;
  2476. access = false;
  2477. for (i = 0; cmds[i].name != NULL; i++) {
  2478. if (strcmp(cmds[i].name, param) == 0) {
  2479. found = true;
  2480. sprintf(cmdbuf, "|%s|", param);
  2481. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  2482. access = true;
  2483. break;
  2484. }
  2485. }
  2486. sprintf(io_buffer, isjson
  2487. ? "%s," JSON_CHECK
  2488. : "%s" _CHECK ",",
  2489. message(MSG_CHECK, 0, NULL, isjson));
  2490. root = api_add_const(root, "Exists", found ? YES : NO, false);
  2491. root = api_add_const(root, "Access", access ? YES : NO, false);
  2492. root = print_data(root, buf, isjson);
  2493. if (isjson)
  2494. strcat(buf, JSON_CLOSE);
  2495. strcat(io_buffer, buf);
  2496. }
  2497. static void send_result(SOCKETTYPE c, bool isjson)
  2498. {
  2499. int n;
  2500. int len;
  2501. if (isjson)
  2502. strcat(io_buffer, JSON_END);
  2503. len = strlen(io_buffer);
  2504. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : BLANK);
  2505. // ignore failure - it's closed immediately anyway
  2506. n = send(c, io_buffer, len+1, 0);
  2507. if (opt_debug) {
  2508. if (SOCKETFAIL(n))
  2509. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  2510. else
  2511. applog(LOG_DEBUG, "API: sent %d", n);
  2512. }
  2513. }
  2514. static void tidyup(__maybe_unused void *arg)
  2515. {
  2516. mutex_lock(&quit_restart_lock);
  2517. bye = true;
  2518. if (sock != INVSOCK) {
  2519. shutdown(sock, SHUT_RDWR);
  2520. CLOSESOCKET(sock);
  2521. sock = INVSOCK;
  2522. }
  2523. if (ipaccess != NULL) {
  2524. free(ipaccess);
  2525. ipaccess = NULL;
  2526. }
  2527. if (msg_buffer != NULL) {
  2528. free(msg_buffer);
  2529. msg_buffer = NULL;
  2530. }
  2531. if (io_buffer != NULL) {
  2532. free(io_buffer);
  2533. io_buffer = NULL;
  2534. }
  2535. mutex_unlock(&quit_restart_lock);
  2536. }
  2537. /*
  2538. * Interpret --api-groups G:cmd1:cmd2:cmd3,P:cmd4,*,...
  2539. */
  2540. static void setup_groups()
  2541. {
  2542. char *api_groups = opt_api_groups ? opt_api_groups : (char *)BLANK;
  2543. char *buf, *ptr, *next, *colon;
  2544. char group;
  2545. char commands[TMPBUFSIZ];
  2546. char cmdbuf[100];
  2547. char *cmd;
  2548. bool addstar, did;
  2549. int i;
  2550. buf = malloc(strlen(api_groups) + 1);
  2551. if (unlikely(!buf))
  2552. quit(1, "Failed to malloc ipgroups buf");
  2553. strcpy(buf, api_groups);
  2554. next = buf;
  2555. // for each group defined
  2556. while (next && *next) {
  2557. ptr = next;
  2558. next = strchr(ptr, ',');
  2559. if (next)
  2560. *(next++) = '\0';
  2561. // Validate the group
  2562. if (*(ptr+1) != ':') {
  2563. colon = strchr(ptr, ':');
  2564. if (colon)
  2565. *colon = '\0';
  2566. applog(LOG_WARNING, "API invalid group name '%s'", ptr);
  2567. quit(1, INVAPIGROUPS);
  2568. }
  2569. group = GROUP(*ptr);
  2570. if (!VALIDGROUP(group)) {
  2571. applog(LOG_WARNING, "API invalid group name '%c'", *ptr);
  2572. quit(1, INVAPIGROUPS);
  2573. }
  2574. if (group == PRIVGROUP) {
  2575. applog(LOG_WARNING, "API group name can't be '%c'", PRIVGROUP);
  2576. quit(1, INVAPIGROUPS);
  2577. }
  2578. if (group == NOPRIVGROUP) {
  2579. applog(LOG_WARNING, "API group name can't be '%c'", NOPRIVGROUP);
  2580. quit(1, INVAPIGROUPS);
  2581. }
  2582. if (apigroups[GROUPOFFSET(group)].commands != NULL) {
  2583. applog(LOG_WARNING, "API duplicate group name '%c'", *ptr);
  2584. quit(1, INVAPIGROUPS);
  2585. }
  2586. ptr += 2;
  2587. // Validate the command list (and handle '*')
  2588. cmd = &(commands[0]);
  2589. *(cmd++) = SEPARATOR;
  2590. *cmd = '\0';
  2591. addstar = false;
  2592. while (ptr && *ptr) {
  2593. colon = strchr(ptr, ':');
  2594. if (colon)
  2595. *(colon++) = '\0';
  2596. if (strcmp(ptr, "*") == 0)
  2597. addstar = true;
  2598. else {
  2599. did = false;
  2600. for (i = 0; cmds[i].name != NULL; i++) {
  2601. if (strcasecmp(ptr, cmds[i].name) == 0) {
  2602. did = true;
  2603. break;
  2604. }
  2605. }
  2606. if (did) {
  2607. // skip duplicates
  2608. sprintf(cmdbuf, "|%s|", cmds[i].name);
  2609. if (strstr(commands, cmdbuf) == NULL) {
  2610. strcpy(cmd, cmds[i].name);
  2611. cmd += strlen(cmds[i].name);
  2612. *(cmd++) = SEPARATOR;
  2613. *cmd = '\0';
  2614. }
  2615. } else {
  2616. applog(LOG_WARNING, "API unknown command '%s' in group '%c'", ptr, group);
  2617. quit(1, INVAPIGROUPS);
  2618. }
  2619. }
  2620. ptr = colon;
  2621. }
  2622. // * = allow all non-iswritemode commands
  2623. if (addstar) {
  2624. for (i = 0; cmds[i].name != NULL; i++) {
  2625. if (cmds[i].iswritemode == false) {
  2626. // skip duplicates
  2627. sprintf(cmdbuf, "|%s|", cmds[i].name);
  2628. if (strstr(commands, cmdbuf) == NULL) {
  2629. strcpy(cmd, cmds[i].name);
  2630. cmd += strlen(cmds[i].name);
  2631. *(cmd++) = SEPARATOR;
  2632. *cmd = '\0';
  2633. }
  2634. }
  2635. }
  2636. }
  2637. ptr = apigroups[GROUPOFFSET(group)].commands = malloc(strlen(commands) + 1);
  2638. if (unlikely(!ptr))
  2639. quit(1, "Failed to malloc group commands buf");
  2640. strcpy(ptr, commands);
  2641. }
  2642. // Now define R (NOPRIVGROUP) as all non-iswritemode commands
  2643. cmd = &(commands[0]);
  2644. *(cmd++) = SEPARATOR;
  2645. *cmd = '\0';
  2646. for (i = 0; cmds[i].name != NULL; i++) {
  2647. if (cmds[i].iswritemode == false) {
  2648. strcpy(cmd, cmds[i].name);
  2649. cmd += strlen(cmds[i].name);
  2650. *(cmd++) = SEPARATOR;
  2651. *cmd = '\0';
  2652. }
  2653. }
  2654. ptr = apigroups[GROUPOFFSET(NOPRIVGROUP)].commands = malloc(strlen(commands) + 1);
  2655. if (unlikely(!ptr))
  2656. quit(1, "Failed to malloc noprivgroup commands buf");
  2657. strcpy(ptr, commands);
  2658. // W (PRIVGROUP) is handled as a special case since it simply means all commands
  2659. free(buf);
  2660. return;
  2661. }
  2662. /*
  2663. * Interpret [W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  2664. * special case of 0/0 allows /0 (means all IP addresses)
  2665. */
  2666. #define ALLIP4 "0/0"
  2667. /*
  2668. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  2669. */
  2670. static void setup_ipaccess()
  2671. {
  2672. char *buf, *ptr, *comma, *slash, *dot;
  2673. int ipcount, mask, octet, i;
  2674. char group;
  2675. buf = malloc(strlen(opt_api_allow) + 1);
  2676. if (unlikely(!buf))
  2677. quit(1, "Failed to malloc ipaccess buf");
  2678. strcpy(buf, opt_api_allow);
  2679. ipcount = 1;
  2680. ptr = buf;
  2681. while (*ptr)
  2682. if (*(ptr++) == ',')
  2683. ipcount++;
  2684. // possibly more than needed, but never less
  2685. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  2686. if (unlikely(!ipaccess))
  2687. quit(1, "Failed to calloc ipaccess");
  2688. ips = 0;
  2689. ptr = buf;
  2690. while (ptr && *ptr) {
  2691. while (*ptr == ' ' || *ptr == '\t')
  2692. ptr++;
  2693. if (*ptr == ',') {
  2694. ptr++;
  2695. continue;
  2696. }
  2697. comma = strchr(ptr, ',');
  2698. if (comma)
  2699. *(comma++) = '\0';
  2700. group = NOPRIVGROUP;
  2701. if (isalpha(*ptr) && *(ptr+1) == ':') {
  2702. if (DEFINEDGROUP(*ptr))
  2703. group = GROUP(*ptr);
  2704. ptr += 2;
  2705. }
  2706. ipaccess[ips].group = group;
  2707. if (strcmp(ptr, ALLIP4) == 0)
  2708. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  2709. else {
  2710. slash = strchr(ptr, '/');
  2711. if (!slash)
  2712. ipaccess[ips].mask = 0xffffffff;
  2713. else {
  2714. *(slash++) = '\0';
  2715. mask = atoi(slash);
  2716. if (mask < 1 || mask > 32)
  2717. goto popipo; // skip invalid/zero
  2718. ipaccess[ips].mask = 0;
  2719. while (mask-- >= 0) {
  2720. octet = 1 << (mask % 8);
  2721. ipaccess[ips].mask |= (octet << (24 - (8 * (mask >> 3))));
  2722. }
  2723. }
  2724. ipaccess[ips].ip = 0; // missing default to '.0'
  2725. for (i = 0; ptr && (i < 4); i++) {
  2726. dot = strchr(ptr, '.');
  2727. if (dot)
  2728. *(dot++) = '\0';
  2729. octet = atoi(ptr);
  2730. if (octet < 0 || octet > 0xff)
  2731. goto popipo; // skip invalid
  2732. ipaccess[ips].ip |= (octet << (24 - (i * 8)));
  2733. ptr = dot;
  2734. }
  2735. ipaccess[ips].ip &= ipaccess[ips].mask;
  2736. }
  2737. ips++;
  2738. popipo:
  2739. ptr = comma;
  2740. }
  2741. free(buf);
  2742. }
  2743. static void *quit_thread(__maybe_unused void *userdata)
  2744. {
  2745. rename_thr("bfg-rpc-quit");
  2746. // allow thread creator to finish whatever it's doing
  2747. mutex_lock(&quit_restart_lock);
  2748. mutex_unlock(&quit_restart_lock);
  2749. if (opt_debug)
  2750. applog(LOG_DEBUG, "API: killing BFGMiner");
  2751. kill_work();
  2752. return NULL;
  2753. }
  2754. static void *restart_thread(__maybe_unused void *userdata)
  2755. {
  2756. rename_thr("bfg-rpc-restart");
  2757. // allow thread creator to finish whatever it's doing
  2758. mutex_lock(&quit_restart_lock);
  2759. mutex_unlock(&quit_restart_lock);
  2760. if (opt_debug)
  2761. applog(LOG_DEBUG, "API: restarting BFGMiner");
  2762. app_restart();
  2763. return NULL;
  2764. }
  2765. void api(int api_thr_id)
  2766. {
  2767. struct thr_info bye_thr;
  2768. char buf[TMPBUFSIZ];
  2769. char param_buf[TMPBUFSIZ];
  2770. const char *localaddr = "127.0.0.1";
  2771. SOCKETTYPE c;
  2772. int n, bound;
  2773. char *connectaddr;
  2774. char *binderror;
  2775. time_t bindstart;
  2776. short int port = opt_api_port;
  2777. struct sockaddr_in serv;
  2778. struct sockaddr_in cli;
  2779. socklen_t clisiz;
  2780. char cmdbuf[100];
  2781. char *cmd;
  2782. char *param;
  2783. bool addrok;
  2784. char group;
  2785. json_error_t json_err;
  2786. json_t *json_config;
  2787. json_t *json_val;
  2788. bool isjson;
  2789. bool did;
  2790. int i;
  2791. mutex_init(&quit_restart_lock);
  2792. pthread_cleanup_push(tidyup, NULL);
  2793. my_thr_id = api_thr_id;
  2794. if (!opt_api_listen) {
  2795. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  2796. return;
  2797. }
  2798. setup_groups();
  2799. if (opt_api_allow) {
  2800. setup_ipaccess();
  2801. if (ips == 0) {
  2802. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  2803. return;
  2804. }
  2805. }
  2806. /* This should be done before curl in needed
  2807. * to ensure curl has already called WSAStartup() in windows */
  2808. sleep(opt_log_interval);
  2809. sock = socket(AF_INET, SOCK_STREAM, 0);
  2810. if (sock == INVSOCK) {
  2811. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2812. return;
  2813. }
  2814. memset(&serv, 0, sizeof(serv));
  2815. serv.sin_family = AF_INET;
  2816. if (!opt_api_allow && !opt_api_network) {
  2817. serv.sin_addr.s_addr = inet_addr(localaddr);
  2818. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  2819. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2820. return;
  2821. }
  2822. }
  2823. serv.sin_port = htons(port);
  2824. #ifndef WIN32
  2825. // On linux with SO_REUSEADDR, bind will get the port if the previous
  2826. // socket is closed (even if it is still in TIME_WAIT) but fail if
  2827. // another program has it open - which is what we want
  2828. int optval = 1;
  2829. // If it doesn't work, we don't really care - just show a debug message
  2830. if (SOCKETFAIL(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval))))
  2831. applog(LOG_DEBUG, "API setsockopt SO_REUSEADDR failed (ignored): %s", SOCKERRMSG);
  2832. #else
  2833. // On windows a 2nd program can bind to a port>1024 already in use unless
  2834. // SO_EXCLUSIVEADDRUSE is used - however then the bind to a closed port
  2835. // in TIME_WAIT will fail until the timeout - so we leave the options alone
  2836. #endif
  2837. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  2838. bound = 0;
  2839. bindstart = time(NULL);
  2840. while (bound == 0) {
  2841. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  2842. binderror = SOCKERRMSG;
  2843. if ((time(NULL) - bindstart) > 61)
  2844. break;
  2845. else {
  2846. applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
  2847. sleep(30);
  2848. }
  2849. } else
  2850. bound = 1;
  2851. }
  2852. if (bound == 0) {
  2853. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  2854. return;
  2855. }
  2856. if (SOCKETFAIL(listen(sock, QUEUE))) {
  2857. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2858. CLOSESOCKET(sock);
  2859. return;
  2860. }
  2861. if (opt_api_allow)
  2862. applog(LOG_WARNING, "API running in IP access mode on port %d", port);
  2863. else {
  2864. if (opt_api_network)
  2865. applog(LOG_WARNING, "API running in UNRESTRICTED read access mode on port %d", port);
  2866. else
  2867. applog(LOG_WARNING, "API running in local read access mode on port %d", port);
  2868. }
  2869. io_buffer = malloc(MYBUFSIZ+1);
  2870. msg_buffer = malloc(MYBUFSIZ+1);
  2871. while (!bye) {
  2872. clisiz = sizeof(cli);
  2873. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  2874. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2875. goto die;
  2876. }
  2877. connectaddr = inet_ntoa(cli.sin_addr);
  2878. addrok = false;
  2879. group = NOPRIVGROUP;
  2880. if (opt_api_allow) {
  2881. int client_ip = htonl(cli.sin_addr.s_addr);
  2882. for (i = 0; i < ips; i++) {
  2883. if ((client_ip & ipaccess[i].mask) == ipaccess[i].ip) {
  2884. addrok = true;
  2885. group = ipaccess[i].group;
  2886. break;
  2887. }
  2888. }
  2889. } else {
  2890. if (opt_api_network)
  2891. addrok = true;
  2892. else
  2893. addrok = (strcmp(connectaddr, localaddr) == 0);
  2894. }
  2895. if (opt_debug)
  2896. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  2897. if (addrok) {
  2898. n = recv(c, &buf[0], TMPBUFSIZ-1, 0);
  2899. if (SOCKETFAIL(n))
  2900. buf[0] = '\0';
  2901. else
  2902. buf[n] = '\0';
  2903. if (opt_debug) {
  2904. if (SOCKETFAIL(n))
  2905. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  2906. else
  2907. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  2908. }
  2909. if (!SOCKETFAIL(n)) {
  2910. // the time of the request in now
  2911. when = time(NULL);
  2912. did = false;
  2913. if (*buf != ISJSON) {
  2914. isjson = false;
  2915. param = strchr(buf, SEPARATOR);
  2916. if (param != NULL)
  2917. *(param++) = '\0';
  2918. cmd = buf;
  2919. }
  2920. else {
  2921. isjson = true;
  2922. param = NULL;
  2923. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  2924. json_config = json_loadb(buf, n, 0, &json_err);
  2925. #elif JANSSON_MAJOR_VERSION > 1
  2926. json_config = json_loads(buf, 0, &json_err);
  2927. #else
  2928. json_config = json_loads(buf, &json_err);
  2929. #endif
  2930. if (!json_is_object(json_config)) {
  2931. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  2932. send_result(c, isjson);
  2933. did = true;
  2934. }
  2935. else {
  2936. json_val = json_object_get(json_config, JSON_COMMAND);
  2937. if (json_val == NULL) {
  2938. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  2939. send_result(c, isjson);
  2940. did = true;
  2941. }
  2942. else {
  2943. if (!json_is_string(json_val)) {
  2944. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  2945. send_result(c, isjson);
  2946. did = true;
  2947. }
  2948. else {
  2949. cmd = (char *)json_string_value(json_val);
  2950. json_val = json_object_get(json_config, JSON_PARAMETER);
  2951. if (json_is_string(json_val))
  2952. param = (char *)json_string_value(json_val);
  2953. else if (json_is_integer(json_val)) {
  2954. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  2955. param = param_buf;
  2956. } else if (json_is_real(json_val)) {
  2957. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  2958. param = param_buf;
  2959. }
  2960. }
  2961. }
  2962. }
  2963. }
  2964. if (!did)
  2965. for (i = 0; cmds[i].name != NULL; i++) {
  2966. if (strcmp(cmd, cmds[i].name) == 0) {
  2967. sprintf(cmdbuf, "|%s|", cmd);
  2968. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  2969. (cmds[i].func)(c, param, isjson, group);
  2970. else {
  2971. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  2972. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  2973. }
  2974. send_result(c, isjson);
  2975. did = true;
  2976. break;
  2977. }
  2978. }
  2979. if (!did) {
  2980. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  2981. send_result(c, isjson);
  2982. }
  2983. }
  2984. }
  2985. CLOSESOCKET(c);
  2986. }
  2987. die:
  2988. /* Blank line fix for older compilers since pthread_cleanup_pop is a
  2989. * macro that gets confused by a label existing immediately before it
  2990. */
  2991. ;
  2992. pthread_cleanup_pop(true);
  2993. if (opt_debug)
  2994. applog(LOG_DEBUG, "API: terminating due to: %s",
  2995. do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
  2996. mutex_lock(&quit_restart_lock);
  2997. if (do_a_restart) {
  2998. if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
  2999. mutex_unlock(&quit_restart_lock);
  3000. quit(1, "API failed to initiate a restart - aborting");
  3001. }
  3002. pthread_detach(bye_thr.pth);
  3003. } else if (do_a_quit) {
  3004. if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
  3005. mutex_unlock(&quit_restart_lock);
  3006. quit(1, "API failed to initiate a clean quit - aborting");
  3007. }
  3008. pthread_detach(bye_thr.pth);
  3009. }
  3010. mutex_unlock(&quit_restart_lock);
  3011. }