api.c 99 KB

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