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