api.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619
  1. /*
  2. * Copyright 2011-2012 Andrew Smith
  3. * Copyright 2011-2012 Con Kolivas
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. *
  10. * Note: the code always includes GPU support even if there are no GPUs
  11. * this simplifies handling multiple other device code being included
  12. * depending on compile options
  13. */
  14. #include "config.h"
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdbool.h>
  20. #include <stdint.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include "compat.h"
  24. #include "miner.h"
  25. #include "driver-cpu.h" /* for algo_names[], TODO: re-factor dependency */
  26. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX) || defined(USE_MODMINER)
  27. #define HAVE_AN_FPGA 1
  28. #endif
  29. #if defined(unix) || defined(__APPLE__)
  30. #include <errno.h>
  31. #include <sys/socket.h>
  32. #include <netinet/in.h>
  33. #include <arpa/inet.h>
  34. #define SOCKETTYPE int
  35. #define SOCKETFAIL(a) ((a) < 0)
  36. #define INVSOCK -1
  37. #define INVINETADDR -1
  38. #define CLOSESOCKET close
  39. #define SOCKERRMSG strerror(errno)
  40. #endif
  41. #ifdef WIN32
  42. #include <ws2tcpip.h>
  43. #include <winsock2.h>
  44. #define SOCKETTYPE SOCKET
  45. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  46. #define INVSOCK INVALID_SOCKET
  47. #define INVINETADDR INADDR_NONE
  48. #define CLOSESOCKET closesocket
  49. static char WSAbuf[1024];
  50. struct WSAERRORS {
  51. int id;
  52. char *code;
  53. } WSAErrors[] = {
  54. { 0, "No error" },
  55. { WSAEINTR, "Interrupted system call" },
  56. { WSAEBADF, "Bad file number" },
  57. { WSAEACCES, "Permission denied" },
  58. { WSAEFAULT, "Bad address" },
  59. { WSAEINVAL, "Invalid argument" },
  60. { WSAEMFILE, "Too many open sockets" },
  61. { WSAEWOULDBLOCK, "Operation would block" },
  62. { WSAEINPROGRESS, "Operation now in progress" },
  63. { WSAEALREADY, "Operation already in progress" },
  64. { WSAENOTSOCK, "Socket operation on non-socket" },
  65. { WSAEDESTADDRREQ, "Destination address required" },
  66. { WSAEMSGSIZE, "Message too long" },
  67. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  68. { WSAENOPROTOOPT, "Bad protocol option" },
  69. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  70. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  71. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  72. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  73. { WSAEAFNOSUPPORT, "Address family not supported" },
  74. { WSAEADDRINUSE, "Address already in use" },
  75. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  76. { WSAENETDOWN, "Network is down" },
  77. { WSAENETUNREACH, "Network is unreachable" },
  78. { WSAENETRESET, "Net connection reset" },
  79. { WSAECONNABORTED, "Software caused connection abort" },
  80. { WSAECONNRESET, "Connection reset by peer" },
  81. { WSAENOBUFS, "No buffer space available" },
  82. { WSAEISCONN, "Socket is already connected" },
  83. { WSAENOTCONN, "Socket is not connected" },
  84. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  85. { WSAETOOMANYREFS, "Too many references, can't splice" },
  86. { WSAETIMEDOUT, "Connection timed out" },
  87. { WSAECONNREFUSED, "Connection refused" },
  88. { WSAELOOP, "Too many levels of symbolic links" },
  89. { WSAENAMETOOLONG, "File name too long" },
  90. { WSAEHOSTDOWN, "Host is down" },
  91. { WSAEHOSTUNREACH, "No route to host" },
  92. { WSAENOTEMPTY, "Directory not empty" },
  93. { WSAEPROCLIM, "Too many processes" },
  94. { WSAEUSERS, "Too many users" },
  95. { WSAEDQUOT, "Disc quota exceeded" },
  96. { WSAESTALE, "Stale NFS file handle" },
  97. { WSAEREMOTE, "Too many levels of remote in path" },
  98. { WSASYSNOTREADY, "Network system is unavailable" },
  99. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  100. { WSANOTINITIALISED, "WSAStartup not yet called" },
  101. { WSAEDISCON, "Graceful shutdown in progress" },
  102. { WSAHOST_NOT_FOUND, "Host not found" },
  103. { WSANO_DATA, "No host data of that type was found" },
  104. { -1, "Unknown error code" }
  105. };
  106. static char *WSAErrorMsg()
  107. {
  108. int i;
  109. int id = WSAGetLastError();
  110. /* Assume none of them are actually -1 */
  111. for (i = 0; WSAErrors[i].id != -1; i++)
  112. if (WSAErrors[i].id == id)
  113. break;
  114. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  115. return &(WSAbuf[0]);
  116. }
  117. #define SOCKERRMSG WSAErrorMsg()
  118. #ifndef SHUT_RDWR
  119. #define SHUT_RDWR SD_BOTH
  120. #endif
  121. #ifndef in_addr_t
  122. #define in_addr_t uint32_t
  123. #endif
  124. #endif
  125. // Big enough for largest API request
  126. // though a PC with 100s of PGAs/CPUs may exceed the size ...
  127. // Current code assumes it can socket send this size also
  128. #define MYBUFSIZ 65432 // TODO: intercept before it's exceeded
  129. // BUFSIZ varies on Windows and Linux
  130. #define TMPBUFSIZ 8192
  131. // Number of requests to queue - normally would be small
  132. // However lots of PGA's may mean more
  133. #define QUEUE 100
  134. static char *io_buffer = NULL;
  135. static char *msg_buffer = NULL;
  136. static SOCKETTYPE sock = INVSOCK;
  137. static const char *UNAVAILABLE = " - API will not be available";
  138. static const char *BLANK = "";
  139. static const char *COMMA = ",";
  140. static const char SEPARATOR = '|';
  141. #define SEPSTR "|"
  142. static const char GPUSEP = ',';
  143. static const char *APIVERSION = "1.12";
  144. static const char *DEAD = "Dead";
  145. static const char *SICK = "Sick";
  146. static const char *NOSTART = "NoStart";
  147. static const char *DISABLED = "Disabled";
  148. static const char *ALIVE = "Alive";
  149. static const char *REJECTING = "Rejecting";
  150. static const char *UNKNOWN = "Unknown";
  151. #define _DYNAMIC "D"
  152. static const char *DYNAMIC = _DYNAMIC;
  153. static const char *YES = "Y";
  154. static const char *NO = "N";
  155. static const char *DEVICECODE = ""
  156. #ifdef HAVE_OPENCL
  157. "GPU "
  158. #endif
  159. #ifdef USE_BITFORCE
  160. "BFL "
  161. #endif
  162. #ifdef USE_ICARUS
  163. "ICA "
  164. #endif
  165. #ifdef USE_ZTEX
  166. "ZTX "
  167. #endif
  168. #ifdef USE_MODMINER
  169. "MMQ "
  170. #endif
  171. #ifdef WANT_CPUMINE
  172. "CPU "
  173. #endif
  174. "";
  175. static const char *OSINFO =
  176. #if defined(__linux)
  177. "Linux";
  178. #else
  179. #if defined(__APPLE__)
  180. "Apple";
  181. #else
  182. #if defined (WIN32)
  183. "Windows";
  184. #else
  185. #if defined(unix)
  186. "Unix";
  187. #else
  188. "Unknown";
  189. #endif
  190. #endif
  191. #endif
  192. #endif
  193. #define _DEVS "DEVS"
  194. #define _POOLS "POOLS"
  195. #define _SUMMARY "SUMMARY"
  196. #define _STATUS "STATUS"
  197. #define _VERSION "VERSION"
  198. #define _MINECON "CONFIG"
  199. #define _GPU "GPU"
  200. #ifdef HAVE_AN_FPGA
  201. #define _PGA "PGA"
  202. #endif
  203. #ifdef WANT_CPUMINE
  204. #define _CPU "CPU"
  205. #endif
  206. #define _GPUS "GPUS"
  207. #define _PGAS "PGAS"
  208. #define _CPUS "CPUS"
  209. #define _NOTIFY "NOTIFY"
  210. #define _DEVDETAILS "DEVDETAILS"
  211. #define _BYE "BYE"
  212. #define _RESTART "RESTART"
  213. #define _MINESTATS "STATS"
  214. static const char ISJSON = '{';
  215. #define JSON0 "{"
  216. #define JSON1 "\""
  217. #define JSON2 "\":["
  218. #define JSON3 "]"
  219. #define JSON4 ",\"id\":1}"
  220. #define JSON_START JSON0
  221. #define JSON_DEVS JSON1 _DEVS JSON2
  222. #define JSON_POOLS JSON1 _POOLS JSON2
  223. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  224. #define JSON_STATUS JSON1 _STATUS JSON2
  225. #define JSON_VERSION JSON1 _VERSION JSON2
  226. #define JSON_MINECON JSON1 _MINECON JSON2
  227. #define JSON_GPU JSON1 _GPU JSON2
  228. #ifdef HAVE_AN_FPGA
  229. #define JSON_PGA JSON1 _PGA JSON2
  230. #endif
  231. #ifdef WANT_CPUMINE
  232. #define JSON_CPU JSON1 _CPU JSON2
  233. #endif
  234. #define JSON_GPUS JSON1 _GPUS JSON2
  235. #define JSON_PGAS JSON1 _PGAS JSON2
  236. #define JSON_CPUS JSON1 _CPUS JSON2
  237. #define JSON_NOTIFY JSON1 _NOTIFY JSON2
  238. #define JSON_DEVDETAILS JSON1 _DEVDETAILS JSON2
  239. #define JSON_BYE JSON1 _BYE JSON1
  240. #define JSON_RESTART JSON1 _RESTART JSON1
  241. #define JSON_CLOSE JSON3
  242. #define JSON_MINESTATS JSON1 _MINESTATS JSON2
  243. #define JSON_END JSON4
  244. static const char *JSON_COMMAND = "command";
  245. static const char *JSON_PARAMETER = "parameter";
  246. #define MSG_INVGPU 1
  247. #define MSG_ALRENA 2
  248. #define MSG_ALRDIS 3
  249. #define MSG_GPUMRE 4
  250. #define MSG_GPUREN 5
  251. #define MSG_GPUNON 6
  252. #define MSG_POOL 7
  253. #define MSG_NOPOOL 8
  254. #define MSG_DEVS 9
  255. #define MSG_NODEVS 10
  256. #define MSG_SUMM 11
  257. #define MSG_GPUDIS 12
  258. #define MSG_GPUREI 13
  259. #define MSG_INVCMD 14
  260. #define MSG_MISID 15
  261. #define MSG_GPUDEV 17
  262. #ifdef WANT_CPUMINE
  263. #define MSG_CPUNON 16
  264. #define MSG_CPUDEV 18
  265. #define MSG_INVCPU 19
  266. #endif
  267. #define MSG_NUMGPU 20
  268. #define MSG_NUMCPU 21
  269. #define MSG_VERSION 22
  270. #define MSG_INVJSON 23
  271. #define MSG_MISCMD 24
  272. #define MSG_MISPID 25
  273. #define MSG_INVPID 26
  274. #define MSG_SWITCHP 27
  275. #define MSG_MISVAL 28
  276. #define MSG_NOADL 29
  277. #define MSG_NOGPUADL 30
  278. #define MSG_INVINT 31
  279. #define MSG_GPUINT 32
  280. #define MSG_MINECON 33
  281. #define MSG_GPUMERR 34
  282. #define MSG_GPUMEM 35
  283. #define MSG_GPUEERR 36
  284. #define MSG_GPUENG 37
  285. #define MSG_GPUVERR 38
  286. #define MSG_GPUVDDC 39
  287. #define MSG_GPUFERR 40
  288. #define MSG_GPUFAN 41
  289. #define MSG_MISFN 42
  290. #define MSG_BADFN 43
  291. #define MSG_SAVED 44
  292. #define MSG_ACCDENY 45
  293. #define MSG_ACCOK 46
  294. #define MSG_ENAPOOL 47
  295. #define MSG_DISPOOL 48
  296. #define MSG_ALRENAP 49
  297. #define MSG_ALRDISP 50
  298. #define MSG_DISLASTP 51
  299. #define MSG_MISPDP 52
  300. #define MSG_INVPDP 53
  301. #define MSG_TOOMANYP 54
  302. #define MSG_ADDPOOL 55
  303. #ifdef HAVE_AN_FPGA
  304. #define MSG_PGANON 56
  305. #define MSG_PGADEV 57
  306. #define MSG_INVPGA 58
  307. #endif
  308. #define MSG_NUMPGA 59
  309. #define MSG_NOTIFY 60
  310. #ifdef HAVE_AN_FPGA
  311. #define MSG_PGALRENA 61
  312. #define MSG_PGALRDIS 62
  313. #define MSG_PGAENA 63
  314. #define MSG_PGADIS 64
  315. #define MSG_PGAUNW 65
  316. #endif
  317. #define MSG_REMLASTP 66
  318. #define MSG_ACTPOOL 67
  319. #define MSG_REMPOOL 68
  320. #define MSG_DEVDETAILS 69
  321. #define MSG_MINESTATS 70
  322. enum code_severity {
  323. SEVERITY_ERR,
  324. SEVERITY_WARN,
  325. SEVERITY_INFO,
  326. SEVERITY_SUCC,
  327. SEVERITY_FAIL
  328. };
  329. enum code_parameters {
  330. PARAM_GPU,
  331. PARAM_PGA,
  332. PARAM_CPU,
  333. PARAM_GPUMAX,
  334. PARAM_PGAMAX,
  335. PARAM_CPUMAX,
  336. PARAM_PMAX,
  337. PARAM_POOLMAX,
  338. // Single generic case: have the code resolve it - see below
  339. PARAM_DMAX,
  340. PARAM_CMD,
  341. PARAM_POOL,
  342. PARAM_STR,
  343. PARAM_BOTH,
  344. PARAM_NONE
  345. };
  346. struct CODES {
  347. const enum code_severity severity;
  348. const int code;
  349. const enum code_parameters params;
  350. const char *description;
  351. } codes[] = {
  352. #ifdef HAVE_OPENCL
  353. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  354. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  355. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  356. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  357. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  358. #endif
  359. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  360. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  361. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  362. { SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX,
  363. #ifdef HAVE_OPENCL
  364. "%d GPU(s)"
  365. #endif
  366. #if defined(HAVE_AN_FPGA) && defined(HAVE_OPENCL)
  367. " - "
  368. #endif
  369. #ifdef HAVE_AN_FPGA
  370. "%d PGA(s)"
  371. #endif
  372. #if defined(WANT_CPUMINE) && (defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA))
  373. " - "
  374. #endif
  375. #ifdef WANT_CPUMINE
  376. "%d CPU(s)"
  377. #endif
  378. },
  379. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
  380. #ifdef HAVE_AN_FPGA
  381. "/PGAs"
  382. #endif
  383. #ifdef WANT_CPUMINE
  384. "/CPUs"
  385. #endif
  386. },
  387. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  388. #ifdef HAVE_OPENCL
  389. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  390. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  391. #endif
  392. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  393. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  394. #ifdef HAVE_OPENCL
  395. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  396. #endif
  397. #ifdef HAVE_AN_FPGA
  398. { SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
  399. { SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
  400. { SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
  401. { SEVERITY_INFO, MSG_PGALRENA,PARAM_PGA, "PGA %d already enabled" },
  402. { SEVERITY_INFO, MSG_PGALRDIS,PARAM_PGA, "PGA %d already disabled" },
  403. { SEVERITY_INFO, MSG_PGAENA, PARAM_PGA, "PGA %d sent enable message" },
  404. { SEVERITY_INFO, MSG_PGADIS, PARAM_PGA, "PGA %d set disable flag" },
  405. { SEVERITY_ERR, MSG_PGAUNW, PARAM_PGA, "PGA %d is not flagged WELL, cannot enable" },
  406. #endif
  407. #ifdef WANT_CPUMINE
  408. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  409. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  410. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  411. #endif
  412. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  413. { SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
  414. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  415. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "BFGMiner versions" },
  416. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  417. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  418. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  419. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  420. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  421. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  422. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  423. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  424. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  425. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  426. { SEVERITY_SUCC, MSG_MINECON, PARAM_NONE, "BFGMiner config" },
  427. #ifdef HAVE_OPENCL
  428. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  429. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported success" },
  430. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  431. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported success" },
  432. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  433. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported success" },
  434. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  435. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported success" },
  436. #endif
  437. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  438. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  439. { SEVERITY_SUCC, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  440. { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
  441. { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
  442. { SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
  443. { SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
  444. { SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
  445. { SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
  446. { SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
  447. { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
  448. { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
  449. { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
  450. { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
  451. { SEVERITY_ERR, MSG_REMLASTP,PARAM_POOL, "Cannot remove last pool %d:'%s'" },
  452. { SEVERITY_ERR, MSG_ACTPOOL, PARAM_POOL, "Cannot remove active pool %d:'%s'" },
  453. { SEVERITY_SUCC, MSG_REMPOOL, PARAM_BOTH, "Removed pool %d:'%s'" },
  454. { SEVERITY_SUCC, MSG_NOTIFY, PARAM_NONE, "Notify" },
  455. { SEVERITY_SUCC, MSG_DEVDETAILS,PARAM_NONE, "Device Details" },
  456. { SEVERITY_SUCC, MSG_MINESTATS,PARAM_NONE, "CGMiner stats" },
  457. { SEVERITY_FAIL, 0, 0, NULL }
  458. };
  459. static int my_thr_id = 0;
  460. static bool bye;
  461. static bool ping = true;
  462. // Used to control quit restart access to shutdown variables
  463. static pthread_mutex_t quit_restart_lock;
  464. static bool do_a_quit;
  465. static bool do_a_restart;
  466. static time_t when = 0; // when the request occurred
  467. struct IP4ACCESS {
  468. in_addr_t ip;
  469. in_addr_t mask;
  470. bool writemode;
  471. };
  472. static struct IP4ACCESS *ipaccess = NULL;
  473. static int ips = 0;
  474. #ifdef USE_BITFORCE
  475. extern struct device_api bitforce_api;
  476. #endif
  477. #ifdef USE_ICARUS
  478. extern struct device_api icarus_api;
  479. #endif
  480. #ifdef USE_ZTEX
  481. extern struct device_api ztex_api;
  482. #endif
  483. #ifdef USE_MODMINER
  484. extern struct device_api modminer_api;
  485. #endif
  486. // This is only called when expected to be needed (rarely)
  487. // i.e. strings outside of the codes control (input from the user)
  488. static char *escape_string(char *str, bool isjson)
  489. {
  490. char *buf, *ptr;
  491. int count;
  492. count = 0;
  493. for (ptr = str; *ptr; ptr++) {
  494. switch (*ptr) {
  495. case ',':
  496. case '|':
  497. case '=':
  498. if (!isjson)
  499. count++;
  500. break;
  501. case '"':
  502. if (isjson)
  503. count++;
  504. break;
  505. case '\\':
  506. count++;
  507. break;
  508. }
  509. }
  510. if (count == 0)
  511. return str;
  512. buf = malloc(strlen(str) + count + 1);
  513. if (unlikely(!buf))
  514. quit(1, "Failed to malloc escape buf");
  515. ptr = buf;
  516. while (*str)
  517. switch (*str) {
  518. case ',':
  519. case '|':
  520. case '=':
  521. if (!isjson)
  522. *(ptr++) = '\\';
  523. *(ptr++) = *(str++);
  524. break;
  525. case '"':
  526. if (isjson)
  527. *(ptr++) = '\\';
  528. *(ptr++) = *(str++);
  529. break;
  530. case '\\':
  531. *(ptr++) = '\\';
  532. *(ptr++) = *(str++);
  533. break;
  534. default:
  535. *(ptr++) = *(str++);
  536. break;
  537. }
  538. *ptr = '\0';
  539. return buf;
  540. }
  541. #ifdef HAVE_AN_FPGA
  542. static int numpgas()
  543. {
  544. int count = 0;
  545. int i;
  546. for (i = 0; i < total_devices; i++) {
  547. #ifdef USE_BITFORCE
  548. if (devices[i]->api == &bitforce_api)
  549. count++;
  550. #endif
  551. #ifdef USE_ICARUS
  552. if (devices[i]->api == &icarus_api)
  553. count++;
  554. #endif
  555. #ifdef USE_ZTEX
  556. if (devices[i]->api == &ztex_api)
  557. count++;
  558. #endif
  559. #ifdef USE_MODMINER
  560. if (devices[i]->api == &modminer_api)
  561. count++;
  562. #endif
  563. }
  564. return count;
  565. }
  566. static int pgadevice(int pgaid)
  567. {
  568. int count = 0;
  569. int i;
  570. for (i = 0; i < total_devices; i++) {
  571. #ifdef USE_BITFORCE
  572. if (devices[i]->api == &bitforce_api)
  573. count++;
  574. #endif
  575. #ifdef USE_ICARUS
  576. if (devices[i]->api == &icarus_api)
  577. count++;
  578. #endif
  579. #ifdef USE_ZTEX
  580. if (devices[i]->api == &ztex_api)
  581. count++;
  582. #endif
  583. #ifdef USE_MODMINER
  584. if (devices[i]->api == &modminer_api)
  585. count++;
  586. #endif
  587. if (count == (pgaid + 1))
  588. return i;
  589. }
  590. return -1;
  591. }
  592. #endif
  593. // All replies (except BYE and RESTART) start with a message
  594. // thus for JSON, message() inserts JSON_START at the front
  595. // and send_result() adds JSON_END at the end
  596. static char *message(int messageid, int paramid, char *param2, bool isjson)
  597. {
  598. char severity;
  599. char *ptr;
  600. #ifdef HAVE_AN_FPGA
  601. int pga;
  602. #endif
  603. #ifdef WANT_CPUMINE
  604. int cpu;
  605. #endif
  606. int i;
  607. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  608. if (codes[i].code == messageid) {
  609. switch (codes[i].severity) {
  610. case SEVERITY_WARN:
  611. severity = 'W';
  612. break;
  613. case SEVERITY_INFO:
  614. severity = 'I';
  615. break;
  616. case SEVERITY_SUCC:
  617. severity = 'S';
  618. break;
  619. case SEVERITY_ERR:
  620. default:
  621. severity = 'E';
  622. break;
  623. }
  624. sprintf(msg_buffer, isjson
  625. ? JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"When\":%lu,\"Code\":%d,\"Msg\":\""
  626. : _STATUS "=%c,When=%lu,Code=%d,Msg=",
  627. severity, (unsigned long)when, messageid);
  628. ptr = msg_buffer + strlen(msg_buffer);
  629. switch(codes[i].params) {
  630. case PARAM_GPU:
  631. case PARAM_PGA:
  632. case PARAM_CPU:
  633. sprintf(ptr, codes[i].description, paramid);
  634. break;
  635. case PARAM_POOL:
  636. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  637. break;
  638. #ifdef HAVE_OPENCL
  639. case PARAM_GPUMAX:
  640. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  641. break;
  642. #endif
  643. #ifdef HAVE_AN_FPGA
  644. case PARAM_PGAMAX:
  645. pga = numpgas();
  646. sprintf(ptr, codes[i].description, paramid, pga - 1);
  647. break;
  648. #endif
  649. #ifdef WANT_CPUMINE
  650. case PARAM_CPUMAX:
  651. if (opt_n_threads > 0)
  652. cpu = num_processors;
  653. else
  654. cpu = 0;
  655. sprintf(ptr, codes[i].description, paramid, cpu - 1);
  656. break;
  657. #endif
  658. case PARAM_PMAX:
  659. sprintf(ptr, codes[i].description, total_pools);
  660. break;
  661. case PARAM_POOLMAX:
  662. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  663. break;
  664. case PARAM_DMAX:
  665. #ifdef HAVE_AN_FPGA
  666. pga = numpgas();
  667. #endif
  668. #ifdef WANT_CPUMINE
  669. if (opt_n_threads > 0)
  670. cpu = num_processors;
  671. else
  672. cpu = 0;
  673. #endif
  674. sprintf(ptr, codes[i].description
  675. #ifdef HAVE_OPENCL
  676. , nDevs
  677. #endif
  678. #ifdef HAVE_AN_FPGA
  679. , pga
  680. #endif
  681. #ifdef WANT_CPUMINE
  682. , cpu
  683. #endif
  684. );
  685. break;
  686. case PARAM_CMD:
  687. sprintf(ptr, codes[i].description, JSON_COMMAND);
  688. break;
  689. case PARAM_STR:
  690. sprintf(ptr, codes[i].description, param2);
  691. break;
  692. case PARAM_BOTH:
  693. sprintf(ptr, codes[i].description, paramid, param2);
  694. break;
  695. case PARAM_NONE:
  696. default:
  697. strcpy(ptr, codes[i].description);
  698. }
  699. ptr = msg_buffer + strlen(msg_buffer);
  700. sprintf(ptr, isjson
  701. ? "\",\"Description\":\"%s\"}" JSON_CLOSE
  702. : ",Description=%s" SEPSTR,
  703. opt_api_description);
  704. return msg_buffer;
  705. }
  706. }
  707. sprintf(msg_buffer, isjson
  708. ? JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"When\":%lu,\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE
  709. : _STATUS "=F,When=%lu,Code=-1,Msg=%d,Description=%s" SEPSTR,
  710. (unsigned long)when, messageid, opt_api_description);
  711. return msg_buffer;
  712. }
  713. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  714. {
  715. sprintf(io_buffer, isjson
  716. ? "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE
  717. : "%s" _VERSION ",CGMiner=%s,API=%s" SEPSTR,
  718. message(MSG_VERSION, 0, NULL, isjson),
  719. VERSION, APIVERSION);
  720. }
  721. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  722. {
  723. char buf[TMPBUFSIZ];
  724. int gpucount = 0;
  725. int pgacount = 0;
  726. int cpucount = 0;
  727. char *adlinuse = (char *)NO;
  728. #ifdef HAVE_ADL
  729. const char *adl = YES;
  730. int i;
  731. for (i = 0; i < nDevs; i++) {
  732. if (gpus[i].has_adl) {
  733. adlinuse = (char *)YES;
  734. break;
  735. }
  736. }
  737. #else
  738. const char *adl = NO;
  739. #endif
  740. #ifdef HAVE_OPENCL
  741. gpucount = nDevs;
  742. #endif
  743. #ifdef HAVE_AN_FPGA
  744. pgacount = numpgas();
  745. #endif
  746. #ifdef WANT_CPUMINE
  747. cpucount = opt_n_threads > 0 ? num_processors : 0;
  748. #endif
  749. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  750. sprintf(buf, isjson
  751. ? "," JSON_MINECON "{\"GPU Count\":%d,\"PGA Count\":%d,\"CPU Count\":%d,\"Pool Count\":%d,\"ADL\":\"%s\",\"ADL in use\":\"%s\",\"Strategy\":\"%s\",\"Log Interval\":%d,\"Device Code\":\"%s\",\"OS\":\"%s\"}" JSON_CLOSE
  752. : _MINECON ",GPU Count=%d,PGA Count=%d,CPU Count=%d,Pool Count=%d,ADL=%s,ADL in use=%s,Strategy=%s,Log Interval=%d,Device Code=%s,OS=%s" SEPSTR,
  753. gpucount, pgacount, cpucount, total_pools, adl, adlinuse,
  754. strategies[pool_strategy].s, opt_log_interval, DEVICECODE, OSINFO);
  755. strcat(io_buffer, buf);
  756. }
  757. static const char*
  758. bool2str(bool b)
  759. {
  760. return b ? YES : NO;
  761. }
  762. static const char*
  763. status2str(enum alive status)
  764. {
  765. switch (status) {
  766. case LIFE_WELL:
  767. return ALIVE;
  768. case LIFE_SICK:
  769. return SICK;
  770. case LIFE_DEAD:
  771. return DEAD;
  772. case LIFE_NOSTART:
  773. return NOSTART;
  774. default:
  775. return UNKNOWN;
  776. }
  777. }
  778. static void
  779. append_kv(char *buf, json_t*info, bool isjson)
  780. {
  781. json_t *value;
  782. const char *key, *tmpl = isjson ? ",\"%s\":%s" : ",%s=%s";
  783. char *vdump;
  784. void *it;
  785. for (it = json_object_iter(info); it; it = json_object_iter_next(info, it)) {
  786. key = json_object_iter_key(it);
  787. value = json_object_iter_value(it);
  788. if (isjson || !json_is_string(value))
  789. vdump = json_dumps(value, JSON_COMPACT | JSON_ENCODE_ANY);
  790. else
  791. vdump = strdup(json_string_value(value));
  792. tailsprintf(buf, tmpl, key, vdump);
  793. free(vdump);
  794. }
  795. }
  796. static void
  797. devdetail_an(char *buf, struct cgpu_info *cgpu, bool isjson)
  798. {
  799. tailsprintf(buf, isjson
  800. ? "{\"%s\":%d,Driver=%s"
  801. : "%s=%d,Driver=%s",
  802. cgpu->api->name, cgpu->device_id,
  803. cgpu->api->dname
  804. );
  805. if (cgpu->kname)
  806. tailsprintf(buf, isjson ? ",\"Kernel\":\"%s\"" : ",Kernel=%s", cgpu->kname);
  807. if (cgpu->name)
  808. tailsprintf(buf, isjson ? ",\"Model\":\"%s\"" : ",Model=%s", cgpu->name);
  809. if (cgpu->device_path)
  810. tailsprintf(buf, isjson ? ",\"Device Path\":\"%s\"" : ",Device Path=%s", cgpu->device_path);
  811. if (cgpu->api->get_extra_device_detail) {
  812. json_t *info = cgpu->api->get_extra_device_detail(cgpu);
  813. append_kv(buf, info, isjson);
  814. json_decref(info);
  815. }
  816. tailsprintf(buf, "%c", isjson ? '}' : SEPARATOR);
  817. }
  818. static void devstatus_an(char *buf, struct cgpu_info *cgpu, bool isjson)
  819. {
  820. tailsprintf(buf, isjson
  821. ? "{\"%s\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f"
  822. : "%s=%d,Enabled=%s,Status=%s,Temperature=%.2f,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f",
  823. cgpu->api->name, cgpu->device_id,
  824. bool2str(cgpu->deven != DEV_DISABLED),
  825. status2str(cgpu->status),
  826. cgpu->temp,
  827. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  828. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  829. cgpu->utility,
  830. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  831. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes
  832. );
  833. if (cgpu->api->get_extra_device_status) {
  834. json_t *info = cgpu->api->get_extra_device_status(cgpu);
  835. append_kv(buf, info, isjson);
  836. json_decref(info);
  837. }
  838. tailsprintf(buf, "%c", isjson ? '}' : SEPARATOR);
  839. }
  840. #ifdef HAVE_OPENCL
  841. static void gpustatus(int gpu, bool isjson)
  842. {
  843. if (gpu < 0 || gpu >= nDevs)
  844. return;
  845. devstatus_an(io_buffer, &gpus[gpu], isjson);
  846. }
  847. #endif
  848. #ifdef HAVE_AN_FPGA
  849. static void pgastatus(int pga, bool isjson)
  850. {
  851. int dev = pgadevice(pga);
  852. if (dev < 0) // Should never happen
  853. return;
  854. devstatus_an(io_buffer, devices[dev], isjson);
  855. }
  856. #endif
  857. __maybe_unused
  858. static void cpustatus(int cpu, bool isjson)
  859. {
  860. if (opt_n_threads <= 0 || cpu < 0 || cpu >= num_processors)
  861. return;
  862. devstatus_an(io_buffer, &cpus[cpu], isjson);
  863. }
  864. static void
  865. devinfo_internal(void (*func)(char*, struct cgpu_info*, bool),
  866. __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  867. {
  868. int i;
  869. if (total_devices == 0) {
  870. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  871. return;
  872. }
  873. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  874. if (isjson) {
  875. strcat(io_buffer, COMMA);
  876. strcat(io_buffer, JSON_DEVS);
  877. }
  878. for (i = 0; i < total_devices; ++i) {
  879. if (isjson && i)
  880. strcat(io_buffer, COMMA);
  881. func(io_buffer, devices[i], isjson);
  882. }
  883. if (isjson)
  884. strcat(io_buffer, JSON_CLOSE);
  885. }
  886. static void
  887. devdetail(SOCKETTYPE c, char *param, bool isjson)
  888. {
  889. return devinfo_internal(devdetail_an, c, param, isjson);
  890. }
  891. static void
  892. devstatus(SOCKETTYPE c, char *param, bool isjson)
  893. {
  894. return devinfo_internal(devstatus_an, c, param, isjson);
  895. }
  896. #ifdef HAVE_OPENCL
  897. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  898. {
  899. int id;
  900. if (nDevs == 0) {
  901. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  902. return;
  903. }
  904. if (param == NULL || *param == '\0') {
  905. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  906. return;
  907. }
  908. id = atoi(param);
  909. if (id < 0 || id >= nDevs) {
  910. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  911. return;
  912. }
  913. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  914. if (isjson) {
  915. strcat(io_buffer, COMMA);
  916. strcat(io_buffer, JSON_GPU);
  917. }
  918. gpustatus(id, isjson);
  919. if (isjson)
  920. strcat(io_buffer, JSON_CLOSE);
  921. }
  922. #endif
  923. #ifdef HAVE_AN_FPGA
  924. static void pgadev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  925. {
  926. int numpga = numpgas();
  927. int id;
  928. if (numpga == 0) {
  929. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  930. return;
  931. }
  932. if (param == NULL || *param == '\0') {
  933. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  934. return;
  935. }
  936. id = atoi(param);
  937. if (id < 0 || id >= numpga) {
  938. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  939. return;
  940. }
  941. strcpy(io_buffer, message(MSG_PGADEV, id, NULL, isjson));
  942. if (isjson) {
  943. strcat(io_buffer, COMMA);
  944. strcat(io_buffer, JSON_PGA);
  945. }
  946. pgastatus(id, isjson);
  947. if (isjson)
  948. strcat(io_buffer, JSON_CLOSE);
  949. }
  950. static void pgaenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  951. {
  952. int numpga = numpgas();
  953. struct thr_info *thr;
  954. int pga;
  955. int id;
  956. int i;
  957. if (numpga == 0) {
  958. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  959. return;
  960. }
  961. if (param == NULL || *param == '\0') {
  962. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  963. return;
  964. }
  965. id = atoi(param);
  966. if (id < 0 || id >= numpga) {
  967. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  968. return;
  969. }
  970. int dev = pgadevice(id);
  971. if (dev < 0) { // Should never happen
  972. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  973. return;
  974. }
  975. struct cgpu_info *cgpu = devices[dev];
  976. if (cgpu->deven != DEV_DISABLED) {
  977. strcpy(io_buffer, message(MSG_PGALRENA, id, NULL, isjson));
  978. return;
  979. }
  980. if (cgpu->status != LIFE_WELL) {
  981. strcpy(io_buffer, message(MSG_PGAUNW, id, NULL, isjson));
  982. return;
  983. }
  984. for (i = 0; i < mining_threads; i++) {
  985. pga = thr_info[i].cgpu->device_id;
  986. if (pga == dev) {
  987. thr = &thr_info[i];
  988. cgpu->deven = DEV_ENABLED;
  989. tq_push(thr->q, &ping);
  990. }
  991. }
  992. strcpy(io_buffer, message(MSG_PGAENA, id, NULL, isjson));
  993. }
  994. static void pgadisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  995. {
  996. int numpga = numpgas();
  997. int id;
  998. if (numpga == 0) {
  999. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  1000. return;
  1001. }
  1002. if (param == NULL || *param == '\0') {
  1003. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1004. return;
  1005. }
  1006. id = atoi(param);
  1007. if (id < 0 || id >= numpga) {
  1008. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1009. return;
  1010. }
  1011. int dev = pgadevice(id);
  1012. if (dev < 0) { // Should never happen
  1013. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1014. return;
  1015. }
  1016. struct cgpu_info *cgpu = devices[dev];
  1017. if (cgpu->deven == DEV_DISABLED) {
  1018. strcpy(io_buffer, message(MSG_PGALRDIS, id, NULL, isjson));
  1019. return;
  1020. }
  1021. cgpu->deven = DEV_DISABLED;
  1022. strcpy(io_buffer, message(MSG_PGADIS, id, NULL, isjson));
  1023. }
  1024. #endif
  1025. #ifdef WANT_CPUMINE
  1026. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1027. {
  1028. int id;
  1029. if (opt_n_threads == 0) {
  1030. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  1031. return;
  1032. }
  1033. if (param == NULL || *param == '\0') {
  1034. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1035. return;
  1036. }
  1037. id = atoi(param);
  1038. if (id < 0 || id >= num_processors) {
  1039. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  1040. return;
  1041. }
  1042. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  1043. if (isjson) {
  1044. strcat(io_buffer, COMMA);
  1045. strcat(io_buffer, JSON_CPU);
  1046. }
  1047. cpustatus(id, isjson);
  1048. if (isjson)
  1049. strcat(io_buffer, JSON_CLOSE);
  1050. }
  1051. #endif
  1052. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1053. {
  1054. char buf[TMPBUFSIZ];
  1055. char *status, *lp;
  1056. char *rpc_url;
  1057. char *rpc_user;
  1058. int i;
  1059. if (total_pools == 0) {
  1060. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1061. return;
  1062. }
  1063. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  1064. if (isjson) {
  1065. strcat(io_buffer, COMMA);
  1066. strcat(io_buffer, JSON_POOLS);
  1067. }
  1068. for (i = 0; i < total_pools; i++) {
  1069. struct pool *pool = pools[i];
  1070. switch (pool->enabled) {
  1071. case POOL_DISABLED:
  1072. status = (char *)DISABLED;
  1073. break;
  1074. case POOL_REJECTING:
  1075. status = (char *)REJECTING;
  1076. break;
  1077. case POOL_ENABLED:
  1078. if (pool->idle)
  1079. status = (char *)DEAD;
  1080. else
  1081. status = (char *)ALIVE;
  1082. break;
  1083. default:
  1084. status = (char *)UNKNOWN;
  1085. break;
  1086. }
  1087. if (pool->hdr_path)
  1088. lp = (char *)YES;
  1089. else
  1090. lp = (char *)NO;
  1091. rpc_url = escape_string(pool->rpc_url, isjson);
  1092. rpc_user = escape_string(pool->rpc_user, isjson);
  1093. sprintf(buf, isjson
  1094. ? "%s{\"POOL\":%d,\"URL\":\"%s\",\"Status\":\"%s\",\"Priority\":%d,\"Long Poll\":\"%s\",\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Remote Failures\":%d,\"User\":\"%s\",\"Last Share Time\":%lu}"
  1095. : "%sPOOL=%d,URL=%s,Status=%s,Priority=%d,Long Poll=%s,Getworks=%d,Accepted=%d,Rejected=%d,Discarded=%d,Stale=%d,Get Failures=%d,Remote Failures=%d,User=%s,Last Share Time=%lu" SEPSTR,
  1096. (isjson && (i > 0)) ? COMMA : BLANK,
  1097. i, rpc_url, status, pool->prio, lp,
  1098. pool->getwork_requested,
  1099. pool->accepted, pool->rejected,
  1100. pool->discarded_work,
  1101. pool->stale_shares,
  1102. pool->getfail_occasions,
  1103. pool->remotefail_occasions,
  1104. rpc_user, pool->last_share_time);
  1105. strcat(io_buffer, buf);
  1106. if (rpc_url != pool->rpc_url)
  1107. free(rpc_url);
  1108. rpc_url = NULL;
  1109. if (rpc_user != pool->rpc_user)
  1110. free(rpc_user);
  1111. rpc_user = NULL;
  1112. }
  1113. if (isjson)
  1114. strcat(io_buffer, JSON_CLOSE);
  1115. }
  1116. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1117. {
  1118. double utility, mhs;
  1119. #ifdef WANT_CPUMINE
  1120. char *algo = (char *)(algo_names[opt_algo]);
  1121. if (algo == NULL)
  1122. algo = "(null)";
  1123. #endif
  1124. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1125. mhs = total_mhashes_done / total_secs;
  1126. #ifdef WANT_CPUMINE
  1127. sprintf(io_buffer, isjson
  1128. ? "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"Algorithm\":\"%s\",\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u,\"Total MH\":%.4f}" JSON_CLOSE
  1129. : "%s" _SUMMARY ",Elapsed=%.0f,Algorithm=%s,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u,Total MH=%.4f" SEPSTR,
  1130. message(MSG_SUMM, 0, NULL, isjson),
  1131. total_secs, algo, mhs, found_blocks,
  1132. total_getworks, total_accepted, total_rejected,
  1133. hw_errors, utility, total_discarded, total_stale,
  1134. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1135. #else
  1136. sprintf(io_buffer, isjson
  1137. ? "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u,\"Total MH\":%.4f}" JSON_CLOSE
  1138. : "%s" _SUMMARY ",Elapsed=%.0f,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u,Total MH=%.4f" SEPSTR,
  1139. message(MSG_SUMM, 0, NULL, isjson),
  1140. total_secs, mhs, found_blocks,
  1141. total_getworks, total_accepted, total_rejected,
  1142. hw_errors, utility, total_discarded, total_stale,
  1143. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1144. #endif
  1145. }
  1146. #ifdef HAVE_OPENCL
  1147. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1148. {
  1149. struct thr_info *thr;
  1150. int gpu;
  1151. int id;
  1152. int i;
  1153. if (gpu_threads == 0) {
  1154. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1155. return;
  1156. }
  1157. if (param == NULL || *param == '\0') {
  1158. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1159. return;
  1160. }
  1161. id = atoi(param);
  1162. if (id < 0 || id >= nDevs) {
  1163. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1164. return;
  1165. }
  1166. if (gpus[id].deven != DEV_DISABLED) {
  1167. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  1168. return;
  1169. }
  1170. for (i = 0; i < gpu_threads; i++) {
  1171. gpu = thr_info[i].cgpu->device_id;
  1172. if (gpu == id) {
  1173. thr = &thr_info[i];
  1174. if (thr->cgpu->status != LIFE_WELL) {
  1175. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  1176. return;
  1177. }
  1178. gpus[id].deven = DEV_ENABLED;
  1179. tq_push(thr->q, &ping);
  1180. }
  1181. }
  1182. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  1183. }
  1184. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1185. {
  1186. int id;
  1187. if (nDevs == 0) {
  1188. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1189. return;
  1190. }
  1191. if (param == NULL || *param == '\0') {
  1192. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1193. return;
  1194. }
  1195. id = atoi(param);
  1196. if (id < 0 || id >= nDevs) {
  1197. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1198. return;
  1199. }
  1200. if (gpus[id].deven == DEV_DISABLED) {
  1201. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  1202. return;
  1203. }
  1204. gpus[id].deven = DEV_DISABLED;
  1205. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  1206. }
  1207. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1208. {
  1209. int id;
  1210. if (nDevs == 0) {
  1211. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1212. return;
  1213. }
  1214. if (param == NULL || *param == '\0') {
  1215. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1216. return;
  1217. }
  1218. id = atoi(param);
  1219. if (id < 0 || id >= nDevs) {
  1220. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1221. return;
  1222. }
  1223. reinit_device(&gpus[id]);
  1224. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  1225. }
  1226. #endif
  1227. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1228. {
  1229. char buf[TMPBUFSIZ];
  1230. int numgpu = 0;
  1231. #ifdef HAVE_OPENCL
  1232. numgpu = nDevs;
  1233. #endif
  1234. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  1235. sprintf(buf, isjson
  1236. ? "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE
  1237. : _GPUS ",Count=%d" SEPSTR,
  1238. numgpu);
  1239. strcat(io_buffer, buf);
  1240. }
  1241. static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1242. {
  1243. char buf[TMPBUFSIZ];
  1244. int count = 0;
  1245. #ifdef HAVE_AN_FPGA
  1246. count = numpgas();
  1247. #endif
  1248. strcpy(io_buffer, message(MSG_NUMPGA, 0, NULL, isjson));
  1249. sprintf(buf, isjson
  1250. ? "," JSON_PGAS "{\"Count\":%d}" JSON_CLOSE
  1251. : _PGAS ",Count=%d" SEPSTR,
  1252. count);
  1253. strcat(io_buffer, buf);
  1254. }
  1255. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1256. {
  1257. char buf[TMPBUFSIZ];
  1258. int count = 0;
  1259. #ifdef WANT_CPUMINE
  1260. count = opt_n_threads > 0 ? num_processors : 0;
  1261. #endif
  1262. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  1263. sprintf(buf, isjson
  1264. ? "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE
  1265. : _CPUS ",Count=%d" SEPSTR,
  1266. count);
  1267. strcat(io_buffer, buf);
  1268. }
  1269. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1270. {
  1271. struct pool *pool;
  1272. int id;
  1273. if (total_pools == 0) {
  1274. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1275. return;
  1276. }
  1277. if (param == NULL || *param == '\0') {
  1278. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1279. return;
  1280. }
  1281. id = atoi(param);
  1282. if (id < 0 || id >= total_pools) {
  1283. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1284. return;
  1285. }
  1286. pool = pools[id];
  1287. pool->enabled = POOL_ENABLED;
  1288. switch_pools(pool);
  1289. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  1290. }
  1291. static void copyadvanceafter(char ch, char **param, char **buf)
  1292. {
  1293. #define src_p (*param)
  1294. #define dst_b (*buf)
  1295. while (*src_p && *src_p != ch) {
  1296. if (*src_p == '\\' && *(src_p+1) != '\0')
  1297. src_p++;
  1298. *(dst_b++) = *(src_p++);
  1299. }
  1300. if (*src_p)
  1301. src_p++;
  1302. *(dst_b++) = '\0';
  1303. }
  1304. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1305. {
  1306. char *ptr, *buf;
  1307. ptr = buf = malloc(strlen(param)+1);
  1308. if (unlikely(!buf))
  1309. quit(1, "Failed to malloc pooldetails buf");
  1310. *url = buf;
  1311. // copy url
  1312. copyadvanceafter(',', &param, &buf);
  1313. if (!(*param)) // missing user
  1314. goto exitsama;
  1315. *user = buf;
  1316. // copy user
  1317. copyadvanceafter(',', &param, &buf);
  1318. if (!*param) // missing pass
  1319. goto exitsama;
  1320. *pass = buf;
  1321. // copy pass
  1322. copyadvanceafter(',', &param, &buf);
  1323. return true;
  1324. exitsama:
  1325. free(ptr);
  1326. return false;
  1327. }
  1328. static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1329. {
  1330. char *url, *user, *pass;
  1331. char *ptr;
  1332. if (param == NULL || *param == '\0') {
  1333. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  1334. return;
  1335. }
  1336. if (!pooldetails(param, &url, &user, &pass)) {
  1337. ptr = escape_string(param, isjson);
  1338. strcpy(io_buffer, message(MSG_INVPDP, 0, ptr, isjson));
  1339. if (ptr != param)
  1340. free(ptr);
  1341. ptr = NULL;
  1342. return;
  1343. }
  1344. if (add_pool_details(true, url, user, pass) == ADD_POOL_MAXIMUM) {
  1345. strcpy(io_buffer, message(MSG_TOOMANYP, MAX_POOLS, NULL, isjson));
  1346. return;
  1347. }
  1348. ptr = escape_string(url, isjson);
  1349. strcpy(io_buffer, message(MSG_ADDPOOL, 0, ptr, isjson));
  1350. if (ptr != url)
  1351. free(ptr);
  1352. ptr = NULL;
  1353. }
  1354. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1355. {
  1356. struct pool *pool;
  1357. int id;
  1358. if (total_pools == 0) {
  1359. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1360. return;
  1361. }
  1362. if (param == NULL || *param == '\0') {
  1363. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1364. return;
  1365. }
  1366. id = atoi(param);
  1367. if (id < 0 || id >= total_pools) {
  1368. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1369. return;
  1370. }
  1371. pool = pools[id];
  1372. if (pool->enabled == POOL_ENABLED) {
  1373. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  1374. return;
  1375. }
  1376. pool->enabled = POOL_ENABLED;
  1377. if (pool->prio < current_pool()->prio)
  1378. switch_pools(pool);
  1379. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  1380. }
  1381. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1382. {
  1383. struct pool *pool;
  1384. int id;
  1385. if (total_pools == 0) {
  1386. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1387. return;
  1388. }
  1389. if (param == NULL || *param == '\0') {
  1390. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1391. return;
  1392. }
  1393. id = atoi(param);
  1394. if (id < 0 || id >= total_pools) {
  1395. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1396. return;
  1397. }
  1398. pool = pools[id];
  1399. if (pool->enabled == POOL_DISABLED) {
  1400. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  1401. return;
  1402. }
  1403. if (active_pools() <= 1) {
  1404. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  1405. return;
  1406. }
  1407. pool->enabled = POOL_DISABLED;
  1408. if (pool == current_pool())
  1409. switch_pools(NULL);
  1410. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  1411. }
  1412. static void removepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1413. {
  1414. struct pool *pool;
  1415. char *rpc_url;
  1416. bool dofree = false;
  1417. int id;
  1418. if (total_pools == 0) {
  1419. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1420. return;
  1421. }
  1422. if (param == NULL || *param == '\0') {
  1423. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1424. return;
  1425. }
  1426. id = atoi(param);
  1427. if (id < 0 || id >= total_pools) {
  1428. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1429. return;
  1430. }
  1431. if (total_pools <= 1) {
  1432. strcpy(io_buffer, message(MSG_REMLASTP, id, NULL, isjson));
  1433. return;
  1434. }
  1435. pool = pools[id];
  1436. if (pool == current_pool())
  1437. switch_pools(NULL);
  1438. if (pool == current_pool()) {
  1439. strcpy(io_buffer, message(MSG_ACTPOOL, id, NULL, isjson));
  1440. return;
  1441. }
  1442. pool->enabled = POOL_DISABLED;
  1443. rpc_url = escape_string(pool->rpc_url, isjson);
  1444. if (rpc_url != pool->rpc_url)
  1445. dofree = true;
  1446. remove_pool(pool);
  1447. strcpy(io_buffer, message(MSG_REMPOOL, id, rpc_url, isjson));
  1448. if (dofree)
  1449. free(rpc_url);
  1450. rpc_url = NULL;
  1451. }
  1452. #ifdef HAVE_OPENCL
  1453. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  1454. {
  1455. int id;
  1456. char *gpusep;
  1457. if (nDevs == 0) {
  1458. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1459. return false;
  1460. }
  1461. if (param == NULL || *param == '\0') {
  1462. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1463. return false;
  1464. }
  1465. gpusep = strchr(param, GPUSEP);
  1466. if (gpusep == NULL) {
  1467. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  1468. return false;
  1469. }
  1470. *(gpusep++) = '\0';
  1471. id = atoi(param);
  1472. if (id < 0 || id >= nDevs) {
  1473. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1474. return false;
  1475. }
  1476. *gpu = id;
  1477. *value = gpusep;
  1478. return true;
  1479. }
  1480. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1481. {
  1482. int id;
  1483. char *value;
  1484. int intensity;
  1485. char intensitystr[7];
  1486. if (!splitgpuvalue(param, &id, &value, isjson))
  1487. return;
  1488. if (!strncasecmp(value, DYNAMIC, 1)) {
  1489. gpus[id].dynamic = true;
  1490. strcpy(intensitystr, DYNAMIC);
  1491. }
  1492. else {
  1493. intensity = atoi(value);
  1494. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1495. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1496. return;
  1497. }
  1498. gpus[id].dynamic = false;
  1499. gpus[id].intensity = intensity;
  1500. sprintf(intensitystr, "%d", intensity);
  1501. }
  1502. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1503. }
  1504. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1505. {
  1506. #ifdef HAVE_ADL
  1507. int id;
  1508. char *value;
  1509. int clock;
  1510. if (!splitgpuvalue(param, &id, &value, isjson))
  1511. return;
  1512. clock = atoi(value);
  1513. if (set_memoryclock(id, clock))
  1514. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  1515. else
  1516. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  1517. #else
  1518. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1519. #endif
  1520. }
  1521. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1522. {
  1523. #ifdef HAVE_ADL
  1524. int id;
  1525. char *value;
  1526. int clock;
  1527. if (!splitgpuvalue(param, &id, &value, isjson))
  1528. return;
  1529. clock = atoi(value);
  1530. if (set_engineclock(id, clock))
  1531. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  1532. else
  1533. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  1534. #else
  1535. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1536. #endif
  1537. }
  1538. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1539. {
  1540. #ifdef HAVE_ADL
  1541. int id;
  1542. char *value;
  1543. int fan;
  1544. if (!splitgpuvalue(param, &id, &value, isjson))
  1545. return;
  1546. fan = atoi(value);
  1547. if (set_fanspeed(id, fan))
  1548. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  1549. else
  1550. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  1551. #else
  1552. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1553. #endif
  1554. }
  1555. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1556. {
  1557. #ifdef HAVE_ADL
  1558. int id;
  1559. char *value;
  1560. float vddc;
  1561. if (!splitgpuvalue(param, &id, &value, isjson))
  1562. return;
  1563. vddc = atof(value);
  1564. if (set_vddc(id, vddc))
  1565. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  1566. else
  1567. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  1568. #else
  1569. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1570. #endif
  1571. }
  1572. #endif
  1573. void doquit(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1574. {
  1575. if (isjson)
  1576. strcpy(io_buffer, JSON_START JSON_BYE);
  1577. else
  1578. strcpy(io_buffer, _BYE);
  1579. bye = true;
  1580. do_a_quit = true;
  1581. }
  1582. void dorestart(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1583. {
  1584. if (isjson)
  1585. strcpy(io_buffer, JSON_START JSON_RESTART);
  1586. else
  1587. strcpy(io_buffer, _RESTART);
  1588. bye = true;
  1589. do_a_restart = true;
  1590. }
  1591. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1592. {
  1593. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  1594. }
  1595. void notifystatus(int device, struct cgpu_info *cgpu, bool isjson)
  1596. {
  1597. char buf[TMPBUFSIZ];
  1598. char *reason;
  1599. if (cgpu->device_last_not_well == 0)
  1600. reason = REASON_NONE;
  1601. else
  1602. switch(cgpu->device_not_well_reason) {
  1603. case REASON_THREAD_FAIL_INIT:
  1604. reason = REASON_THREAD_FAIL_INIT_STR;
  1605. break;
  1606. case REASON_THREAD_ZERO_HASH:
  1607. reason = REASON_THREAD_ZERO_HASH_STR;
  1608. break;
  1609. case REASON_THREAD_FAIL_QUEUE:
  1610. reason = REASON_THREAD_FAIL_QUEUE_STR;
  1611. break;
  1612. case REASON_DEV_SICK_IDLE_60:
  1613. reason = REASON_DEV_SICK_IDLE_60_STR;
  1614. break;
  1615. case REASON_DEV_DEAD_IDLE_600:
  1616. reason = REASON_DEV_DEAD_IDLE_600_STR;
  1617. break;
  1618. case REASON_DEV_NOSTART:
  1619. reason = REASON_DEV_NOSTART_STR;
  1620. break;
  1621. case REASON_DEV_OVER_HEAT:
  1622. reason = REASON_DEV_OVER_HEAT_STR;
  1623. break;
  1624. case REASON_DEV_THERMAL_CUTOFF:
  1625. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  1626. break;
  1627. default:
  1628. reason = REASON_UNKNOWN_STR;
  1629. break;
  1630. }
  1631. // ALL counters (and only counters) must start the name with a '*'
  1632. // Simplifies future external support for adding new counters
  1633. sprintf(buf, isjson
  1634. ? "%s{\"NOTIFY\":%d,\"Name\":\"%s\",\"ID\":%d,\"Last Well\":%lu,\"Last Not Well\":%lu,\"Reason Not Well\":\"%s\",\"*Thread Fail Init\":%d,\"*Thread Zero Hash\":%d,\"*Thread Fail Queue\":%d,\"*Dev Sick Idle 60s\":%d,\"*Dev Dead Idle 600s\":%d,\"*Dev Nostart\":%d,\"*Dev Over Heat\":%d,\"*Dev Thermal Cutoff\":%d}"
  1635. : "%sNOTIFY=%d,Name=%s,ID=%d,Last Well=%lu,Last Not Well=%lu,Reason Not Well=%s,*Thread Fail Init=%d,*Thread Zero Hash=%d,*Thread Fail Queue=%d,*Dev Sick Idle 60s=%d,*Dev Dead Idle 600s=%d,*Dev Nostart=%d,*Dev Over Heat=%d,*Dev Thermal Cutoff=%d" SEPSTR,
  1636. (isjson && (device > 0)) ? COMMA : BLANK,
  1637. device, cgpu->api->name, cgpu->device_id,
  1638. cgpu->device_last_well, cgpu->device_last_not_well, reason,
  1639. cgpu->thread_fail_init_count, cgpu->thread_zero_hash_count,
  1640. cgpu->thread_fail_queue_count, cgpu->dev_sick_idle_60_count,
  1641. cgpu->dev_dead_idle_600_count, cgpu->dev_nostart_count,
  1642. cgpu->dev_over_heat_count, cgpu->dev_thermal_cutoff_count);
  1643. strcat(io_buffer, buf);
  1644. }
  1645. static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1646. {
  1647. int i;
  1648. if (total_devices == 0) {
  1649. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1650. return;
  1651. }
  1652. strcpy(io_buffer, message(MSG_NOTIFY, 0, NULL, isjson));
  1653. if (isjson) {
  1654. strcat(io_buffer, COMMA);
  1655. strcat(io_buffer, JSON_NOTIFY);
  1656. }
  1657. for (i = 0; i < total_devices; i++)
  1658. notifystatus(i, devices[i], isjson);
  1659. if (isjson)
  1660. strcat(io_buffer, JSON_CLOSE);
  1661. }
  1662. static void devdetails(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1663. {
  1664. char buf[TMPBUFSIZ];
  1665. struct cgpu_info *cgpu;
  1666. int i;
  1667. if (total_devices == 0) {
  1668. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1669. return;
  1670. }
  1671. strcpy(io_buffer, message(MSG_DEVDETAILS, 0, NULL, isjson));
  1672. if (isjson) {
  1673. strcat(io_buffer, COMMA);
  1674. strcat(io_buffer, JSON_DEVDETAILS);
  1675. }
  1676. for (i = 0; i < total_devices; i++) {
  1677. cgpu = devices[i];
  1678. sprintf(buf, isjson
  1679. ? "%s{\"DEVDETAILS\":%d,\"Name\":\"%s\",\"ID\":%d,\"Driver\":\"%s\",\"Kernel\":\"%s\",\"Model\":\"%s\",\"Device Path\":\"%s\"}"
  1680. : "%sDEVDETAILS=%d,Name=%s,ID=%d,Driver=%s,Kernel=%s,Model=%s,Device Path=%s" SEPSTR,
  1681. (isjson && (i > 0)) ? COMMA : BLANK,
  1682. i, cgpu->api->name, cgpu->device_id,
  1683. cgpu->api->dname, cgpu->kname ? : BLANK,
  1684. cgpu->name ? : BLANK, cgpu->device_path ? : BLANK);
  1685. strcat(io_buffer, buf);
  1686. }
  1687. if (isjson)
  1688. strcat(io_buffer, JSON_CLOSE);
  1689. }
  1690. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1691. {
  1692. char filename[PATH_MAX];
  1693. FILE *fcfg;
  1694. char *ptr;
  1695. if (param == NULL || *param == '\0') {
  1696. default_save_file(filename);
  1697. param = filename;
  1698. }
  1699. fcfg = fopen(param, "w");
  1700. if (!fcfg) {
  1701. ptr = escape_string(param, isjson);
  1702. strcpy(io_buffer, message(MSG_BADFN, 0, ptr, isjson));
  1703. if (ptr != param)
  1704. free(ptr);
  1705. ptr = NULL;
  1706. return;
  1707. }
  1708. write_config(fcfg);
  1709. fclose(fcfg);
  1710. ptr = escape_string(param, isjson);
  1711. strcpy(io_buffer, message(MSG_SAVED, 0, ptr, isjson));
  1712. if (ptr != param)
  1713. free(ptr);
  1714. ptr = NULL;
  1715. }
  1716. static int itemstats(int i, char *id, struct cgminer_stats *stats, struct cgminer_pool_stats *pool_stats, char *extra, bool isjson)
  1717. {
  1718. char buf[TMPBUFSIZ];
  1719. if (stats->getwork_calls || (extra != NULL && *extra))
  1720. {
  1721. if (extra == NULL)
  1722. extra = (char *)BLANK;
  1723. sprintf(buf, isjson
  1724. ? "%s{\"STATS\":%d,\"ID\":\"%s\",\"Elapsed\":%.0f,\"Calls\":%d,\"Wait\":%ld.%06ld,\"Max\":%ld.%06ld,\"Min\":%ld.%06ld"
  1725. : "%sSTATS=%d,ID=%s,Elapsed=%.0f,Calls=%d,Wait=%ld.%06ld,Max=%ld.%06ld,Min=%ld.%06ld",
  1726. (isjson && (i > 0)) ? COMMA : BLANK,
  1727. i, id, total_secs, stats->getwork_calls,
  1728. stats->getwork_wait.tv_sec, stats->getwork_wait.tv_usec,
  1729. stats->getwork_wait_max.tv_sec, stats->getwork_wait_max.tv_usec,
  1730. stats->getwork_wait_min.tv_sec, stats->getwork_wait_min.tv_usec);
  1731. strcat(io_buffer, buf);
  1732. if (pool_stats) {
  1733. sprintf(buf, isjson
  1734. ? ",\"Pool Calls\":%d,\"Pool Attempts\":%d,\"Pool Wait\":%ld.%06ld,\"Pool Max\":%ld.%06ld,\"Pool Min\":%ld.%06ld"
  1735. : ",Pool Calls=%d,Pool Attempts=%d,Pool Wait=%ld.%06ld,Pool Max=%ld.%06ld,Pool Min=%ld.%06ld",
  1736. pool_stats->getwork_calls, pool_stats->getwork_attempts,
  1737. pool_stats->getwork_wait.tv_sec, pool_stats->getwork_wait.tv_usec,
  1738. pool_stats->getwork_wait_max.tv_sec, pool_stats->getwork_wait_max.tv_usec,
  1739. pool_stats->getwork_wait_min.tv_sec, pool_stats->getwork_wait_min.tv_usec);
  1740. strcat(io_buffer, buf);
  1741. }
  1742. sprintf(buf, isjson
  1743. ? "%s}"
  1744. : "%s" SEPSTR,
  1745. extra);
  1746. strcat(io_buffer, buf);
  1747. i++;
  1748. }
  1749. return i;
  1750. }
  1751. static void minerstats(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1752. {
  1753. char extra[TMPBUFSIZ];
  1754. char id[20];
  1755. int i, j;
  1756. strcpy(io_buffer, message(MSG_MINESTATS, 0, NULL, isjson));
  1757. if (isjson) {
  1758. strcat(io_buffer, COMMA);
  1759. strcat(io_buffer, JSON_MINESTATS);
  1760. }
  1761. i = 0;
  1762. for (j = 0; j < total_devices; j++) {
  1763. struct cgpu_info *cgpu = devices[j];
  1764. extra[0] = '\0';
  1765. if (cgpu->api->get_extra_device_perf_stats) {
  1766. json_t *info = cgpu->api->get_extra_device_perf_stats(cgpu);
  1767. append_kv(extra, info, isjson);
  1768. json_decref(info);
  1769. }
  1770. sprintf(id, "%s%d", cgpu->api->name, cgpu->device_id);
  1771. i = itemstats(i, id, &(cgpu->cgminer_stats), NULL, extra, isjson);
  1772. }
  1773. for (j = 0; j < total_pools; j++) {
  1774. struct pool *pool = pools[j];
  1775. sprintf(id, "POOL%d", j);
  1776. i = itemstats(i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, isjson);
  1777. }
  1778. if (isjson)
  1779. strcat(io_buffer, JSON_CLOSE);
  1780. }
  1781. struct CMDS {
  1782. char *name;
  1783. void (*func)(SOCKETTYPE, char *, bool);
  1784. bool requires_writemode;
  1785. } cmds[] = {
  1786. { "version", apiversion, false },
  1787. { "config", minerconfig, false },
  1788. { "devs", devstatus, false },
  1789. { "devdetail", devdetail, false },
  1790. { "pools", poolstatus, false },
  1791. { "summary", summary, false },
  1792. #ifdef HAVE_OPENCL
  1793. { "gpuenable", gpuenable, true },
  1794. { "gpudisable", gpudisable, true },
  1795. { "gpurestart", gpurestart, true },
  1796. { "gpu", gpudev, false },
  1797. #endif
  1798. #ifdef HAVE_AN_FPGA
  1799. { "pga", pgadev, false },
  1800. { "pgaenable", pgaenable, true },
  1801. { "pgadisable", pgadisable, true },
  1802. #endif
  1803. #ifdef WANT_CPUMINE
  1804. { "cpu", cpudev, false },
  1805. #endif
  1806. { "gpucount", gpucount, false },
  1807. { "pgacount", pgacount, false },
  1808. { "cpucount", cpucount, false },
  1809. { "switchpool", switchpool, true },
  1810. { "addpool", addpool, true },
  1811. { "enablepool", enablepool, true },
  1812. { "disablepool", disablepool, true },
  1813. { "removepool", removepool, true },
  1814. #ifdef HAVE_OPENCL
  1815. { "gpuintensity", gpuintensity, true },
  1816. { "gpumem", gpumem, true },
  1817. { "gpuengine", gpuengine, true },
  1818. { "gpufan", gpufan, true },
  1819. { "gpuvddc", gpuvddc, true },
  1820. #endif
  1821. { "save", dosave, true },
  1822. { "quit", doquit, true },
  1823. { "privileged", privileged, true },
  1824. { "notify", notify, false },
  1825. { "devdetails", devdetails, false },
  1826. { "restart", dorestart, true },
  1827. { "stats", minerstats, false },
  1828. { NULL, NULL, false }
  1829. };
  1830. static void send_result(SOCKETTYPE c, bool isjson)
  1831. {
  1832. int n;
  1833. int len;
  1834. if (isjson)
  1835. strcat(io_buffer, JSON_END);
  1836. len = strlen(io_buffer);
  1837. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : BLANK);
  1838. // ignore failure - it's closed immediately anyway
  1839. n = send(c, io_buffer, len+1, 0);
  1840. if (opt_debug) {
  1841. if (SOCKETFAIL(n))
  1842. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  1843. else
  1844. applog(LOG_DEBUG, "API: sent %d", n);
  1845. }
  1846. }
  1847. static void tidyup(__maybe_unused void *arg)
  1848. {
  1849. mutex_lock(&quit_restart_lock);
  1850. bye = true;
  1851. if (sock != INVSOCK) {
  1852. shutdown(sock, SHUT_RDWR);
  1853. CLOSESOCKET(sock);
  1854. sock = INVSOCK;
  1855. }
  1856. if (ipaccess != NULL) {
  1857. free(ipaccess);
  1858. ipaccess = NULL;
  1859. }
  1860. if (msg_buffer != NULL) {
  1861. free(msg_buffer);
  1862. msg_buffer = NULL;
  1863. }
  1864. if (io_buffer != NULL) {
  1865. free(io_buffer);
  1866. io_buffer = NULL;
  1867. }
  1868. mutex_unlock(&quit_restart_lock);
  1869. }
  1870. /*
  1871. * Interpret [R|W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  1872. * special case of 0/0 allows /0 (means all IP addresses)
  1873. */
  1874. #define ALLIP4 "0/0"
  1875. /*
  1876. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  1877. */
  1878. static void setup_ipaccess()
  1879. {
  1880. char *buf, *ptr, *comma, *slash, *dot;
  1881. int ipcount, mask, octet, i;
  1882. bool writemode;
  1883. buf = malloc(strlen(opt_api_allow) + 1);
  1884. if (unlikely(!buf))
  1885. quit(1, "Failed to malloc ipaccess buf");
  1886. strcpy(buf, opt_api_allow);
  1887. ipcount = 1;
  1888. ptr = buf;
  1889. while (*ptr)
  1890. if (*(ptr++) == ',')
  1891. ipcount++;
  1892. // possibly more than needed, but never less
  1893. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  1894. if (unlikely(!ipaccess))
  1895. quit(1, "Failed to calloc ipaccess");
  1896. ips = 0;
  1897. ptr = buf;
  1898. while (ptr && *ptr) {
  1899. while (*ptr == ' ' || *ptr == '\t')
  1900. ptr++;
  1901. if (*ptr == ',') {
  1902. ptr++;
  1903. continue;
  1904. }
  1905. comma = strchr(ptr, ',');
  1906. if (comma)
  1907. *(comma++) = '\0';
  1908. writemode = false;
  1909. if (isalpha(*ptr) && *(ptr+1) == ':') {
  1910. if (tolower(*ptr) == 'w')
  1911. writemode = true;
  1912. ptr += 2;
  1913. }
  1914. ipaccess[ips].writemode = writemode;
  1915. if (strcmp(ptr, ALLIP4) == 0)
  1916. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  1917. else {
  1918. slash = strchr(ptr, '/');
  1919. if (!slash)
  1920. ipaccess[ips].mask = 0xffffffff;
  1921. else {
  1922. *(slash++) = '\0';
  1923. mask = atoi(slash);
  1924. if (mask < 1 || mask > 32)
  1925. goto popipo; // skip invalid/zero
  1926. ipaccess[ips].mask = 0;
  1927. while (mask-- >= 0) {
  1928. octet = 1 << (mask % 8);
  1929. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  1930. }
  1931. }
  1932. ipaccess[ips].ip = 0; // missing default to '.0'
  1933. for (i = 0; ptr && (i < 4); i++) {
  1934. dot = strchr(ptr, '.');
  1935. if (dot)
  1936. *(dot++) = '\0';
  1937. octet = atoi(ptr);
  1938. if (octet < 0 || octet > 0xff)
  1939. goto popipo; // skip invalid
  1940. ipaccess[ips].ip |= (octet << (i * 8));
  1941. ptr = dot;
  1942. }
  1943. ipaccess[ips].ip &= ipaccess[ips].mask;
  1944. }
  1945. ips++;
  1946. popipo:
  1947. ptr = comma;
  1948. }
  1949. free(buf);
  1950. }
  1951. static void *quit_thread(__maybe_unused void *userdata)
  1952. {
  1953. // allow thread creator to finish whatever it's doing
  1954. mutex_lock(&quit_restart_lock);
  1955. mutex_unlock(&quit_restart_lock);
  1956. if (opt_debug)
  1957. applog(LOG_DEBUG, "API: killing cgminer");
  1958. kill_work();
  1959. return NULL;
  1960. }
  1961. static void *restart_thread(__maybe_unused void *userdata)
  1962. {
  1963. // allow thread creator to finish whatever it's doing
  1964. mutex_lock(&quit_restart_lock);
  1965. mutex_unlock(&quit_restart_lock);
  1966. if (opt_debug)
  1967. applog(LOG_DEBUG, "API: restarting cgminer");
  1968. app_restart();
  1969. return NULL;
  1970. }
  1971. void api(int api_thr_id)
  1972. {
  1973. struct thr_info bye_thr;
  1974. char buf[TMPBUFSIZ];
  1975. char param_buf[TMPBUFSIZ];
  1976. const char *localaddr = "127.0.0.1";
  1977. SOCKETTYPE c;
  1978. int n, bound;
  1979. char *connectaddr;
  1980. char *binderror;
  1981. time_t bindstart;
  1982. short int port = opt_api_port;
  1983. struct sockaddr_in serv;
  1984. struct sockaddr_in cli;
  1985. socklen_t clisiz;
  1986. char *cmd;
  1987. char *param;
  1988. bool addrok;
  1989. bool writemode;
  1990. json_error_t json_err;
  1991. json_t *json_config;
  1992. json_t *json_val;
  1993. bool isjson;
  1994. bool did;
  1995. int i;
  1996. mutex_init(&quit_restart_lock);
  1997. pthread_cleanup_push(tidyup, NULL);
  1998. my_thr_id = api_thr_id;
  1999. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  2000. sleep(opt_log_interval);
  2001. if (!opt_api_listen) {
  2002. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  2003. return;
  2004. }
  2005. if (opt_api_allow) {
  2006. setup_ipaccess();
  2007. if (ips == 0) {
  2008. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  2009. return;
  2010. }
  2011. }
  2012. sock = socket(AF_INET, SOCK_STREAM, 0);
  2013. if (sock == INVSOCK) {
  2014. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2015. return;
  2016. }
  2017. memset(&serv, 0, sizeof(serv));
  2018. serv.sin_family = AF_INET;
  2019. if (!opt_api_allow && !opt_api_network) {
  2020. serv.sin_addr.s_addr = inet_addr(localaddr);
  2021. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  2022. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2023. return;
  2024. }
  2025. }
  2026. serv.sin_port = htons(port);
  2027. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  2028. bound = 0;
  2029. bindstart = time(NULL);
  2030. while (bound == 0) {
  2031. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  2032. binderror = SOCKERRMSG;
  2033. if ((time(NULL) - bindstart) > 61)
  2034. break;
  2035. else {
  2036. applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
  2037. sleep(30);
  2038. }
  2039. } else
  2040. bound = 1;
  2041. }
  2042. if (bound == 0) {
  2043. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  2044. return;
  2045. }
  2046. if (SOCKETFAIL(listen(sock, QUEUE))) {
  2047. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2048. CLOSESOCKET(sock);
  2049. return;
  2050. }
  2051. if (opt_api_allow)
  2052. applog(LOG_WARNING, "API running in IP access mode");
  2053. else {
  2054. if (opt_api_network)
  2055. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  2056. else
  2057. applog(LOG_WARNING, "API running in local access mode");
  2058. }
  2059. io_buffer = malloc(MYBUFSIZ+1);
  2060. msg_buffer = malloc(MYBUFSIZ+1);
  2061. while (!bye) {
  2062. clisiz = sizeof(cli);
  2063. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  2064. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2065. goto die;
  2066. }
  2067. connectaddr = inet_ntoa(cli.sin_addr);
  2068. addrok = false;
  2069. writemode = false;
  2070. if (opt_api_allow) {
  2071. for (i = 0; i < ips; i++) {
  2072. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  2073. addrok = true;
  2074. writemode = ipaccess[i].writemode;
  2075. break;
  2076. }
  2077. }
  2078. } else {
  2079. if (opt_api_network)
  2080. addrok = true;
  2081. else
  2082. addrok = (strcmp(connectaddr, localaddr) == 0);
  2083. }
  2084. if (opt_debug)
  2085. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  2086. if (addrok) {
  2087. n = recv(c, &buf[0], TMPBUFSIZ-1, 0);
  2088. if (SOCKETFAIL(n))
  2089. buf[0] = '\0';
  2090. else
  2091. buf[n] = '\0';
  2092. if (opt_debug) {
  2093. if (SOCKETFAIL(n))
  2094. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  2095. else
  2096. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  2097. }
  2098. if (!SOCKETFAIL(n)) {
  2099. // the time of the request in now
  2100. when = time(NULL);
  2101. did = false;
  2102. if (*buf != ISJSON) {
  2103. isjson = false;
  2104. param = strchr(buf, SEPARATOR);
  2105. if (param != NULL)
  2106. *(param++) = '\0';
  2107. cmd = buf;
  2108. }
  2109. else {
  2110. isjson = true;
  2111. param = NULL;
  2112. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  2113. json_config = json_loadb(buf, n, 0, &json_err);
  2114. #elif JANSSON_MAJOR_VERSION > 1
  2115. json_config = json_loads(buf, 0, &json_err);
  2116. #else
  2117. json_config = json_loads(buf, &json_err);
  2118. #endif
  2119. if (!json_is_object(json_config)) {
  2120. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  2121. send_result(c, isjson);
  2122. did = true;
  2123. }
  2124. else {
  2125. json_val = json_object_get(json_config, JSON_COMMAND);
  2126. if (json_val == NULL) {
  2127. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  2128. send_result(c, isjson);
  2129. did = true;
  2130. }
  2131. else {
  2132. if (!json_is_string(json_val)) {
  2133. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  2134. send_result(c, isjson);
  2135. did = true;
  2136. }
  2137. else {
  2138. cmd = (char *)json_string_value(json_val);
  2139. json_val = json_object_get(json_config, JSON_PARAMETER);
  2140. if (json_is_string(json_val))
  2141. param = (char *)json_string_value(json_val);
  2142. else if (json_is_integer(json_val)) {
  2143. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  2144. param = param_buf;
  2145. } else if (json_is_real(json_val)) {
  2146. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  2147. param = param_buf;
  2148. }
  2149. }
  2150. }
  2151. }
  2152. }
  2153. if (!did)
  2154. for (i = 0; cmds[i].name != NULL; i++) {
  2155. if (strcmp(cmd, cmds[i].name) == 0) {
  2156. if (cmds[i].requires_writemode && !writemode) {
  2157. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  2158. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  2159. }
  2160. else
  2161. (cmds[i].func)(c, param, isjson);
  2162. send_result(c, isjson);
  2163. did = true;
  2164. break;
  2165. }
  2166. }
  2167. if (!did) {
  2168. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  2169. send_result(c, isjson);
  2170. }
  2171. }
  2172. }
  2173. CLOSESOCKET(c);
  2174. }
  2175. die:
  2176. /* Blank line fix for older compilers since pthread_cleanup_pop is a
  2177. * macro that gets confused by a label existing immediately before it
  2178. */
  2179. ;
  2180. pthread_cleanup_pop(true);
  2181. if (opt_debug)
  2182. applog(LOG_DEBUG, "API: terminating due to: %s",
  2183. do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
  2184. mutex_lock(&quit_restart_lock);
  2185. if (do_a_restart) {
  2186. if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
  2187. mutex_unlock(&quit_restart_lock);
  2188. quit(1, "API failed to initiate a restart - aborting");
  2189. }
  2190. pthread_detach(bye_thr.pth);
  2191. } else if (do_a_quit) {
  2192. if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
  2193. mutex_unlock(&quit_restart_lock);
  2194. quit(1, "API failed to initiate a clean quit - aborting");
  2195. }
  2196. pthread_detach(bye_thr.pth);
  2197. }
  2198. mutex_unlock(&quit_restart_lock);
  2199. }