api.c 64 KB

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