api.c 92 KB

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