api.c 106 KB

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