api.c 90 KB

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