api.c 97 KB

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