api.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  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 "device-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. static const char GPUSEP = ',';
  137. static const char *APIVERSION = "1.6";
  138. static const char *DEAD = "Dead";
  139. static const char *SICK = "Sick";
  140. static const char *NOSTART = "NoStart";
  141. static const char *DISABLED = "Disabled";
  142. static const char *ALIVE = "Alive";
  143. #define _DYNAMIC "D"
  144. static const char *DYNAMIC = _DYNAMIC;
  145. static const char *YES = "Y";
  146. static const char *NO = "N";
  147. static const char *DEVICECODE = ""
  148. #ifdef HAVE_OPENCL
  149. "GPU "
  150. #endif
  151. #ifdef USE_BITFORCE
  152. "BFL "
  153. #endif
  154. #ifdef USE_ICARUS
  155. "ICA "
  156. #endif
  157. #ifdef WANT_CPUMINE
  158. "CPU "
  159. #endif
  160. "";
  161. static const char *OSINFO =
  162. #if defined(__linux)
  163. "Linux";
  164. #else
  165. #if defined(__APPLE__)
  166. "Apple";
  167. #else
  168. #if defined (WIN32)
  169. "Windows";
  170. #else
  171. #if defined(unix)
  172. "Unix";
  173. #else
  174. "Unknown";
  175. #endif
  176. #endif
  177. #endif
  178. #endif
  179. #define _DEVS "DEVS"
  180. #define _POOLS "POOLS"
  181. #define _SUMMARY "SUMMARY"
  182. #define _STATUS "STATUS"
  183. #define _VERSION "VERSION"
  184. #define _MINECON "CONFIG"
  185. #define _GPU "GPU"
  186. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  187. #define _PGA "PGA"
  188. #endif
  189. #ifdef WANT_CPUMINE
  190. #define _CPU "CPU"
  191. #endif
  192. #define _GPUS "GPUS"
  193. #define _PGAS "PGAS"
  194. #define _CPUS "CPUS"
  195. #define _BYE "BYE"
  196. static const char ISJSON = '{';
  197. #define JSON0 "{"
  198. #define JSON1 "\""
  199. #define JSON2 "\":["
  200. #define JSON3 "]"
  201. #define JSON4 ",\"id\":1}"
  202. #define JSON_START JSON0
  203. #define JSON_DEVS JSON1 _DEVS JSON2
  204. #define JSON_POOLS JSON1 _POOLS JSON2
  205. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  206. #define JSON_STATUS JSON1 _STATUS JSON2
  207. #define JSON_VERSION JSON1 _VERSION JSON2
  208. #define JSON_MINECON JSON1 _MINECON JSON2
  209. #define JSON_GPU JSON1 _GPU JSON2
  210. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  211. #define JSON_PGA JSON1 _PGA JSON2
  212. #endif
  213. #ifdef WANT_CPUMINE
  214. #define JSON_CPU JSON1 _CPU JSON2
  215. #endif
  216. #define JSON_GPUS JSON1 _GPUS JSON2
  217. #define JSON_PGAS JSON1 _PGAS JSON2
  218. #define JSON_CPUS JSON1 _CPUS JSON2
  219. #define JSON_BYE JSON1 _BYE JSON1
  220. #define JSON_CLOSE JSON3
  221. #define JSON_END JSON4
  222. static const char *JSON_COMMAND = "command";
  223. static const char *JSON_PARAMETER = "parameter";
  224. #define MSG_INVGPU 1
  225. #define MSG_ALRENA 2
  226. #define MSG_ALRDIS 3
  227. #define MSG_GPUMRE 4
  228. #define MSG_GPUREN 5
  229. #define MSG_GPUNON 6
  230. #define MSG_POOL 7
  231. #define MSG_NOPOOL 8
  232. #define MSG_DEVS 9
  233. #define MSG_NODEVS 10
  234. #define MSG_SUMM 11
  235. #define MSG_GPUDIS 12
  236. #define MSG_GPUREI 13
  237. #define MSG_INVCMD 14
  238. #define MSG_MISID 15
  239. #define MSG_GPUDEV 17
  240. #ifdef WANT_CPUMINE
  241. #define MSG_CPUNON 16
  242. #define MSG_CPUDEV 18
  243. #define MSG_INVCPU 19
  244. #endif
  245. #define MSG_NUMGPU 20
  246. #define MSG_NUMCPU 21
  247. #define MSG_VERSION 22
  248. #define MSG_INVJSON 23
  249. #define MSG_MISCMD 24
  250. #define MSG_MISPID 25
  251. #define MSG_INVPID 26
  252. #define MSG_SWITCHP 27
  253. #define MSG_MISVAL 28
  254. #define MSG_NOADL 29
  255. #define MSG_NOGPUADL 30
  256. #define MSG_INVINT 31
  257. #define MSG_GPUINT 32
  258. #define MSG_MINECON 33
  259. #define MSG_GPUMERR 34
  260. #define MSG_GPUMEM 35
  261. #define MSG_GPUEERR 36
  262. #define MSG_GPUENG 37
  263. #define MSG_GPUVERR 38
  264. #define MSG_GPUVDDC 39
  265. #define MSG_GPUFERR 40
  266. #define MSG_GPUFAN 41
  267. #define MSG_MISFN 42
  268. #define MSG_BADFN 43
  269. #define MSG_SAVED 44
  270. #define MSG_ACCDENY 45
  271. #define MSG_ACCOK 46
  272. #define MSG_ENAPOOL 47
  273. #define MSG_DISPOOL 48
  274. #define MSG_ALRENAP 49
  275. #define MSG_ALRDISP 50
  276. #define MSG_DISLASTP 51
  277. #define MSG_MISPDP 52
  278. #define MSG_INVPDP 53
  279. #define MSG_TOOMANYP 54
  280. #define MSG_ADDPOOL 55
  281. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  282. #define MSG_PGANON 56
  283. #define MSG_PGADEV 57
  284. #define MSG_INVPGA 58
  285. #endif
  286. #define MSG_NUMPGA 59
  287. enum code_severity {
  288. SEVERITY_ERR,
  289. SEVERITY_WARN,
  290. SEVERITY_INFO,
  291. SEVERITY_SUCC,
  292. SEVERITY_FAIL
  293. };
  294. enum code_parameters {
  295. PARAM_GPU,
  296. PARAM_PGA,
  297. PARAM_CPU,
  298. PARAM_GPUMAX,
  299. PARAM_PGAMAX,
  300. PARAM_CPUMAX,
  301. PARAM_PMAX,
  302. PARAM_POOLMAX,
  303. // Single generic case: have the code resolve it - see below
  304. PARAM_DMAX,
  305. PARAM_CMD,
  306. PARAM_POOL,
  307. PARAM_STR,
  308. PARAM_BOTH,
  309. PARAM_NONE
  310. };
  311. struct CODES {
  312. const enum code_severity severity;
  313. const int code;
  314. const enum code_parameters params;
  315. const char *description;
  316. } codes[] = {
  317. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  318. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  319. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  320. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  321. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  322. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  323. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  324. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  325. { SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX, "%d GPU(s)"
  326. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  327. " - %d PGA(s)"
  328. #endif
  329. #ifdef WANT_CPUMINE
  330. " - %d CPU(s)"
  331. #endif
  332. },
  333. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
  334. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  335. "/PGAs"
  336. #endif
  337. #ifdef WANT_CPUMINE
  338. "/CPUs"
  339. #endif
  340. },
  341. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  342. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  343. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  344. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  345. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  346. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  347. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  348. { SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
  349. { SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
  350. { SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
  351. #endif
  352. #ifdef WANT_CPUMINE
  353. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  354. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  355. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  356. #endif
  357. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  358. { SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
  359. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  360. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "CGMiner versions" },
  361. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  362. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  363. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  364. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  365. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  366. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  367. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  368. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  369. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  370. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  371. { SEVERITY_SUCC, MSG_MINECON, PARAM_NONE, "CGMiner config" },
  372. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  373. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported succeess" },
  374. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  375. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported succeess" },
  376. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  377. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported succeess" },
  378. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  379. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported succeess" },
  380. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  381. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  382. { SEVERITY_ERR, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  383. { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
  384. { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
  385. { SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
  386. { SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
  387. { SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
  388. { SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
  389. { SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
  390. { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
  391. { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
  392. { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
  393. { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
  394. { SEVERITY_FAIL, 0, 0, NULL }
  395. };
  396. static int my_thr_id = 0;
  397. static int bye = 0;
  398. static bool ping = true;
  399. static time_t when = 0; // when the request occurred
  400. struct IP4ACCESS {
  401. in_addr_t ip;
  402. in_addr_t mask;
  403. bool writemode;
  404. };
  405. static struct IP4ACCESS *ipaccess = NULL;
  406. static int ips = 0;
  407. #ifdef USE_BITFORCE
  408. extern struct device_api bitforce_api;
  409. #endif
  410. #ifdef USE_ICARUS
  411. extern struct device_api icarus_api;
  412. #endif
  413. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  414. static int numpgas()
  415. {
  416. int count = 0;
  417. int i;
  418. for (i = 0; i < total_devices; i++) {
  419. #ifdef USE_BITFORCE
  420. if (devices[i]->api == &bitforce_api)
  421. count++;
  422. #endif
  423. #ifdef USE_ICARUS
  424. if (devices[i]->api == &icarus_api)
  425. count++;
  426. #endif
  427. }
  428. return count;
  429. }
  430. static int pgadevice(int pgaid)
  431. {
  432. int count = 0;
  433. int i;
  434. for (i = 0; i < total_devices; i++) {
  435. #ifdef USE_BITFORCE
  436. if (devices[i]->api == &bitforce_api)
  437. count++;
  438. #endif
  439. #ifdef USE_ICARUS
  440. if (devices[i]->api == &icarus_api)
  441. count++;
  442. #endif
  443. if (count == (pgaid + 1))
  444. return i;
  445. }
  446. return -1;
  447. }
  448. #endif
  449. // All replies (except BYE) start with a message
  450. // thus for JSON, message() inserts JSON_START at the front
  451. // and send_result() adds JSON_END at the end
  452. static char *message(int messageid, int paramid, char *param2, bool isjson)
  453. {
  454. char severity;
  455. char *ptr;
  456. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  457. int pga;
  458. #endif
  459. #ifdef WANT_CPUMINE
  460. int cpu;
  461. #endif
  462. int i;
  463. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  464. if (codes[i].code == messageid) {
  465. switch (codes[i].severity) {
  466. case SEVERITY_WARN:
  467. severity = 'W';
  468. break;
  469. case SEVERITY_INFO:
  470. severity = 'I';
  471. break;
  472. case SEVERITY_SUCC:
  473. severity = 'S';
  474. break;
  475. case SEVERITY_ERR:
  476. default:
  477. severity = 'E';
  478. break;
  479. }
  480. if (isjson)
  481. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"When\":%lu,\"Code\":%d,\"Msg\":\"", severity, (unsigned long)when, messageid);
  482. else
  483. sprintf(msg_buffer, _STATUS "=%c,When=%lu,Code=%d,Msg=", severity, (unsigned long)when, messageid);
  484. ptr = msg_buffer + strlen(msg_buffer);
  485. switch(codes[i].params) {
  486. case PARAM_GPU:
  487. case PARAM_PGA:
  488. case PARAM_CPU:
  489. sprintf(ptr, codes[i].description, paramid);
  490. break;
  491. case PARAM_POOL:
  492. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  493. break;
  494. case PARAM_GPUMAX:
  495. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  496. break;
  497. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  498. case PARAM_PGAMAX:
  499. pga = numpgas();
  500. sprintf(ptr, codes[i].description, paramid, pga - 1);
  501. break;
  502. #endif
  503. #ifdef WANT_CPUMINE
  504. case PARAM_CPUMAX:
  505. if (opt_n_threads > 0)
  506. cpu = num_processors;
  507. else
  508. cpu = 0;
  509. sprintf(ptr, codes[i].description, paramid, cpu - 1);
  510. break;
  511. #endif
  512. case PARAM_PMAX:
  513. sprintf(ptr, codes[i].description, total_pools);
  514. break;
  515. case PARAM_POOLMAX:
  516. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  517. break;
  518. case PARAM_DMAX:
  519. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  520. pga = numpgas();
  521. #endif
  522. #ifdef WANT_CPUMINE
  523. if (opt_n_threads > 0)
  524. cpu = num_processors;
  525. else
  526. cpu = 0;
  527. #endif
  528. sprintf(ptr, codes[i].description, nDevs
  529. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  530. , pga
  531. #endif
  532. #ifdef WANT_CPUMINE
  533. , cpu
  534. #endif
  535. );
  536. break;
  537. case PARAM_CMD:
  538. sprintf(ptr, codes[i].description, JSON_COMMAND);
  539. break;
  540. case PARAM_STR:
  541. sprintf(ptr, codes[i].description, param2);
  542. break;
  543. case PARAM_BOTH:
  544. sprintf(ptr, codes[i].description, paramid, param2);
  545. break;
  546. case PARAM_NONE:
  547. default:
  548. strcpy(ptr, codes[i].description);
  549. }
  550. ptr = msg_buffer + strlen(msg_buffer);
  551. if (isjson)
  552. sprintf(ptr, "\",\"Description\":\"%s\"}" JSON_CLOSE, opt_api_description);
  553. else
  554. sprintf(ptr, ",Description=%s%c", opt_api_description, SEPARATOR);
  555. return msg_buffer;
  556. }
  557. }
  558. if (isjson)
  559. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"When\":%lu,\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE,
  560. (unsigned long)when, messageid, opt_api_description);
  561. else
  562. sprintf(msg_buffer, _STATUS "=F,When=%lu,Code=-1,Msg=%d,Description=%s%c",
  563. (unsigned long)when, messageid, opt_api_description, SEPARATOR);
  564. return msg_buffer;
  565. }
  566. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  567. {
  568. if (isjson)
  569. sprintf(io_buffer, "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE,
  570. message(MSG_VERSION, 0, NULL, isjson),
  571. VERSION, APIVERSION);
  572. else
  573. sprintf(io_buffer, "%s" _VERSION ",CGMiner=%s,API=%s%c",
  574. message(MSG_VERSION, 0, NULL, isjson),
  575. VERSION, APIVERSION, SEPARATOR);
  576. }
  577. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  578. {
  579. char buf[BUFSIZ];
  580. int pgacount = 0;
  581. int cpucount = 0;
  582. char *adlinuse = (char *)NO;
  583. #ifdef HAVE_ADL
  584. const char *adl = YES;
  585. int i;
  586. for (i = 0; i < nDevs; i++) {
  587. if (gpus[i].has_adl) {
  588. adlinuse = (char *)YES;
  589. break;
  590. }
  591. }
  592. #else
  593. const char *adl = NO;
  594. #endif
  595. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  596. pgacount = numpgas();
  597. #endif
  598. #ifdef WANT_CPUMINE
  599. cpucount = opt_n_threads > 0 ? num_processors : 0;
  600. #endif
  601. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  602. if (isjson)
  603. sprintf(buf, "," 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, nDevs, pgacount, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, opt_log_interval, OSINFO, DEVICECODE);
  604. else
  605. sprintf(buf, _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%c", nDevs, pgacount, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, opt_log_interval, DEVICECODE, OSINFO, SEPARATOR);
  606. strcat(io_buffer, buf);
  607. }
  608. static void gpustatus(int gpu, bool isjson)
  609. {
  610. char intensity[20];
  611. char buf[BUFSIZ];
  612. char *enabled;
  613. char *status;
  614. float gt, gv;
  615. int ga, gf, gp, gc, gm, pt;
  616. if (gpu >= 0 && gpu < nDevs) {
  617. struct cgpu_info *cgpu = &gpus[gpu];
  618. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  619. #ifdef HAVE_ADL
  620. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  621. #endif
  622. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  623. if (cgpu->deven != DEV_DISABLED)
  624. enabled = (char *)YES;
  625. else
  626. enabled = (char *)NO;
  627. if (cgpu->status == LIFE_DEAD)
  628. status = (char *)DEAD;
  629. else if (cgpu->status == LIFE_SICK)
  630. status = (char *)SICK;
  631. else if (cgpu->status == LIFE_NOSTART)
  632. status = (char *)NOSTART;
  633. else
  634. status = (char *)ALIVE;
  635. if (cgpu->dynamic)
  636. strcpy(intensity, DYNAMIC);
  637. else
  638. sprintf(intensity, "%d", cgpu->intensity);
  639. if (isjson)
  640. sprintf(buf, "{\"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}",
  641. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  642. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  643. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  644. cgpu->utility, intensity,
  645. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  646. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  647. else
  648. sprintf(buf, "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%c",
  649. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  650. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  651. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  652. cgpu->utility, intensity,
  653. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  654. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, SEPARATOR);
  655. strcat(io_buffer, buf);
  656. }
  657. }
  658. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  659. static void pgastatus(int pga, bool isjson)
  660. {
  661. char buf[BUFSIZ];
  662. char *enabled;
  663. char *status;
  664. int numpga = numpgas();
  665. if (numpga > 0 && pga >= 0 && pga < numpga) {
  666. int dev = pgadevice(pga);
  667. if (dev < 0) // Should never happen
  668. return;
  669. struct cgpu_info *cgpu = devices[dev];
  670. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  671. if (cgpu->deven != DEV_DISABLED)
  672. enabled = (char *)YES;
  673. else
  674. enabled = (char *)NO;
  675. if (cgpu->status == LIFE_DEAD)
  676. status = (char *)DEAD;
  677. else if (cgpu->status == LIFE_SICK)
  678. status = (char *)SICK;
  679. else if (cgpu->status == LIFE_NOSTART)
  680. status = (char *)NOSTART;
  681. else
  682. status = (char *)ALIVE;
  683. if (isjson)
  684. sprintf(buf, "{\"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}",
  685. pga, cgpu->api->name, cgpu->device_id,
  686. enabled, status, cgpu->temp,
  687. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  688. cgpu->accepted, cgpu->rejected, cgpu->hw_errors, cgpu->utility,
  689. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  690. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  691. else
  692. sprintf(buf, "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%c",
  693. pga, cgpu->api->name, cgpu->device_id,
  694. enabled, status, cgpu->temp,
  695. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  696. cgpu->accepted, cgpu->rejected, cgpu->hw_errors, cgpu->utility,
  697. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  698. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, SEPARATOR);
  699. strcat(io_buffer, buf);
  700. }
  701. }
  702. #endif
  703. #ifdef WANT_CPUMINE
  704. static void cpustatus(int cpu, bool isjson)
  705. {
  706. char buf[BUFSIZ];
  707. if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
  708. struct cgpu_info *cgpu = &cpus[cpu];
  709. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  710. if (isjson)
  711. sprintf(buf, "{\"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}",
  712. cpu, cgpu->total_mhashes / total_secs,
  713. opt_log_interval, cgpu->rolling,
  714. cgpu->accepted, cgpu->rejected,
  715. cgpu->utility,
  716. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  717. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  718. else
  719. sprintf(buf, "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%c",
  720. cpu, cgpu->total_mhashes / total_secs,
  721. opt_log_interval, cgpu->rolling,
  722. cgpu->accepted, cgpu->rejected,
  723. cgpu->utility,
  724. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  725. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, SEPARATOR);
  726. strcat(io_buffer, buf);
  727. }
  728. }
  729. #endif
  730. static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  731. {
  732. int devcount = 0;
  733. int i;
  734. if (nDevs == 0 && opt_n_threads == 0) {
  735. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  736. return;
  737. }
  738. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  739. if (isjson) {
  740. strcat(io_buffer, COMMA);
  741. strcat(io_buffer, JSON_DEVS);
  742. }
  743. for (i = 0; i < nDevs; i++) {
  744. if (isjson && devcount > 0)
  745. strcat(io_buffer, COMMA);
  746. gpustatus(i, isjson);
  747. devcount++;
  748. }
  749. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  750. int numpga = numpgas();
  751. if (numpga > 0)
  752. for (i = 0; i < numpga; i++) {
  753. if (isjson && devcount > 0)
  754. strcat(io_buffer, COMMA);
  755. pgastatus(i, isjson);
  756. devcount++;
  757. }
  758. #endif
  759. #ifdef WANT_CPUMINE
  760. if (opt_n_threads > 0)
  761. for (i = 0; i < num_processors; i++) {
  762. if (isjson && devcount > 0)
  763. strcat(io_buffer, COMMA);
  764. cpustatus(i, isjson);
  765. devcount++;
  766. }
  767. #endif
  768. if (isjson)
  769. strcat(io_buffer, JSON_CLOSE);
  770. }
  771. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  772. {
  773. int id;
  774. if (nDevs == 0) {
  775. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  776. return;
  777. }
  778. if (param == NULL || *param == '\0') {
  779. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  780. return;
  781. }
  782. id = atoi(param);
  783. if (id < 0 || id >= nDevs) {
  784. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  785. return;
  786. }
  787. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  788. if (isjson) {
  789. strcat(io_buffer, COMMA);
  790. strcat(io_buffer, JSON_GPU);
  791. }
  792. gpustatus(id, isjson);
  793. if (isjson)
  794. strcat(io_buffer, JSON_CLOSE);
  795. }
  796. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  797. static void pgadev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  798. {
  799. int numpga = numpgas();
  800. int id;
  801. if (numpga == 0) {
  802. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  803. return;
  804. }
  805. if (param == NULL || *param == '\0') {
  806. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  807. return;
  808. }
  809. id = atoi(param);
  810. if (id < 0 || id >= numpga) {
  811. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  812. return;
  813. }
  814. strcpy(io_buffer, message(MSG_PGADEV, id, NULL, isjson));
  815. if (isjson) {
  816. strcat(io_buffer, COMMA);
  817. strcat(io_buffer, JSON_PGA);
  818. }
  819. pgastatus(id, isjson);
  820. if (isjson)
  821. strcat(io_buffer, JSON_CLOSE);
  822. }
  823. #endif
  824. #ifdef WANT_CPUMINE
  825. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  826. {
  827. int id;
  828. if (opt_n_threads == 0) {
  829. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  830. return;
  831. }
  832. if (param == NULL || *param == '\0') {
  833. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  834. return;
  835. }
  836. id = atoi(param);
  837. if (id < 0 || id >= num_processors) {
  838. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  839. return;
  840. }
  841. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  842. if (isjson) {
  843. strcat(io_buffer, COMMA);
  844. strcat(io_buffer, JSON_CPU);
  845. }
  846. cpustatus(id, isjson);
  847. if (isjson)
  848. strcat(io_buffer, JSON_CLOSE);
  849. }
  850. #endif
  851. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  852. {
  853. char buf[BUFSIZ];
  854. char *status, *lp;
  855. int i;
  856. if (total_pools == 0) {
  857. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  858. return;
  859. }
  860. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  861. if (isjson) {
  862. strcat(io_buffer, COMMA);
  863. strcat(io_buffer, JSON_POOLS);
  864. }
  865. for (i = 0; i < total_pools; i++) {
  866. struct pool *pool = pools[i];
  867. if (!pool->enabled)
  868. status = (char *)DISABLED;
  869. else {
  870. if (pool->idle)
  871. status = (char *)DEAD;
  872. else
  873. status = (char *)ALIVE;
  874. }
  875. if (pool->hdr_path)
  876. lp = (char *)YES;
  877. else
  878. lp = (char *)NO;
  879. if (isjson)
  880. sprintf(buf, "%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}",
  881. (i > 0) ? COMMA : "",
  882. i, pool->rpc_url, status, pool->prio, lp,
  883. pool->getwork_requested,
  884. pool->accepted, pool->rejected,
  885. pool->discarded_work,
  886. pool->stale_shares,
  887. pool->getfail_occasions,
  888. pool->remotefail_occasions);
  889. else
  890. sprintf(buf, "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%c",
  891. i, pool->rpc_url, status, pool->prio, lp,
  892. pool->getwork_requested,
  893. pool->accepted, pool->rejected,
  894. pool->discarded_work,
  895. pool->stale_shares,
  896. pool->getfail_occasions,
  897. pool->remotefail_occasions, SEPARATOR);
  898. strcat(io_buffer, buf);
  899. }
  900. if (isjson)
  901. strcat(io_buffer, JSON_CLOSE);
  902. }
  903. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  904. {
  905. double utility, mhs;
  906. #ifdef WANT_CPUMINE
  907. char *algo = (char *)(algo_names[opt_algo]);
  908. if (algo == NULL)
  909. algo = "(null)";
  910. #endif
  911. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  912. mhs = total_mhashes_done / total_secs;
  913. #ifdef WANT_CPUMINE
  914. if (isjson)
  915. sprintf(io_buffer, "%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,
  916. message(MSG_SUMM, 0, NULL, isjson),
  917. total_secs, algo, mhs, found_blocks,
  918. total_getworks, total_accepted, total_rejected,
  919. hw_errors, utility, total_discarded, total_stale,
  920. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  921. else
  922. sprintf(io_buffer, "%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%c",
  923. message(MSG_SUMM, 0, NULL, isjson),
  924. total_secs, algo, mhs, found_blocks,
  925. total_getworks, total_accepted, total_rejected,
  926. hw_errors, utility, total_discarded, total_stale,
  927. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  928. #else
  929. if (isjson)
  930. sprintf(io_buffer, "%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,
  931. message(MSG_SUMM, 0, NULL, isjson),
  932. total_secs, mhs, found_blocks,
  933. total_getworks, total_accepted, total_rejected,
  934. hw_errors, utility, total_discarded, total_stale,
  935. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  936. else
  937. sprintf(io_buffer, "%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%c",
  938. message(MSG_SUMM, 0, NULL, isjson),
  939. total_secs, mhs, found_blocks,
  940. total_getworks, total_accepted, total_rejected,
  941. hw_errors, utility, total_discarded, total_stale,
  942. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  943. #endif
  944. }
  945. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  946. {
  947. struct thr_info *thr;
  948. int gpu;
  949. int id;
  950. int i;
  951. if (gpu_threads == 0) {
  952. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  953. return;
  954. }
  955. if (param == NULL || *param == '\0') {
  956. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  957. return;
  958. }
  959. id = atoi(param);
  960. if (id < 0 || id >= nDevs) {
  961. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  962. return;
  963. }
  964. if (gpus[id].deven != DEV_DISABLED) {
  965. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  966. return;
  967. }
  968. for (i = 0; i < gpu_threads; i++) {
  969. gpu = thr_info[i].cgpu->device_id;
  970. if (gpu == id) {
  971. thr = &thr_info[i];
  972. if (thr->cgpu->status != LIFE_WELL) {
  973. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  974. return;
  975. }
  976. gpus[id].deven = DEV_ENABLED;
  977. tq_push(thr->q, &ping);
  978. }
  979. }
  980. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  981. }
  982. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  983. {
  984. int id;
  985. if (nDevs == 0) {
  986. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  987. return;
  988. }
  989. if (param == NULL || *param == '\0') {
  990. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  991. return;
  992. }
  993. id = atoi(param);
  994. if (id < 0 || id >= nDevs) {
  995. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  996. return;
  997. }
  998. if (gpus[id].deven == DEV_DISABLED) {
  999. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  1000. return;
  1001. }
  1002. gpus[id].deven = DEV_DISABLED;
  1003. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  1004. }
  1005. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1006. {
  1007. int id;
  1008. if (nDevs == 0) {
  1009. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1010. return;
  1011. }
  1012. if (param == NULL || *param == '\0') {
  1013. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1014. return;
  1015. }
  1016. id = atoi(param);
  1017. if (id < 0 || id >= nDevs) {
  1018. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1019. return;
  1020. }
  1021. reinit_device(&gpus[id]);
  1022. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  1023. }
  1024. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1025. {
  1026. char buf[BUFSIZ];
  1027. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  1028. if (isjson)
  1029. sprintf(buf, "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE, nDevs);
  1030. else
  1031. sprintf(buf, _GPUS ",Count=%d%c", nDevs, SEPARATOR);
  1032. strcat(io_buffer, buf);
  1033. }
  1034. static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1035. {
  1036. char buf[BUFSIZ];
  1037. int count = 0;
  1038. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  1039. count = numpgas();
  1040. #endif
  1041. strcpy(io_buffer, message(MSG_NUMPGA, 0, NULL, isjson));
  1042. if (isjson)
  1043. sprintf(buf, "," JSON_PGAS "{\"Count\":%d}" JSON_CLOSE, count);
  1044. else
  1045. sprintf(buf, _PGAS ",Count=%d%c", count, SEPARATOR);
  1046. strcat(io_buffer, buf);
  1047. }
  1048. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1049. {
  1050. char buf[BUFSIZ];
  1051. int count = 0;
  1052. #ifdef WANT_CPUMINE
  1053. count = opt_n_threads > 0 ? num_processors : 0;
  1054. #endif
  1055. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  1056. if (isjson)
  1057. sprintf(buf, "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE, count);
  1058. else
  1059. sprintf(buf, _CPUS ",Count=%d%c", count, SEPARATOR);
  1060. strcat(io_buffer, buf);
  1061. }
  1062. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1063. {
  1064. struct pool *pool;
  1065. int id;
  1066. if (total_pools == 0) {
  1067. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1068. return;
  1069. }
  1070. if (param == NULL || *param == '\0') {
  1071. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1072. return;
  1073. }
  1074. id = atoi(param);
  1075. if (id < 0 || id >= total_pools) {
  1076. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1077. return;
  1078. }
  1079. pool = pools[id];
  1080. pool->enabled = true;
  1081. switch_pools(pool);
  1082. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  1083. }
  1084. static void copyadvanceafter(char ch, char **param, char **buf)
  1085. {
  1086. #define src_p (*param)
  1087. #define dst_b (*buf)
  1088. while (*src_p && *src_p != ch) {
  1089. if (*src_p == '\\' && *(src_p+1) != '\0')
  1090. src_p++;
  1091. *(dst_b++) = *(src_p++);
  1092. }
  1093. if (*src_p)
  1094. src_p++;
  1095. *(dst_b++) = '\0';
  1096. }
  1097. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1098. {
  1099. char *ptr, *buf;
  1100. ptr = buf = malloc(strlen(param)+1);
  1101. if (unlikely(!buf))
  1102. quit(1, "Failed to malloc pooldetails buf");
  1103. *url = buf;
  1104. // copy url
  1105. copyadvanceafter(',', &param, &buf);
  1106. if (!(*param)) // missing user
  1107. goto exitsama;
  1108. *user = buf;
  1109. // copy user
  1110. copyadvanceafter(',', &param, &buf);
  1111. if (!*param) // missing pass
  1112. goto exitsama;
  1113. *pass = buf;
  1114. // copy pass
  1115. copyadvanceafter(',', &param, &buf);
  1116. return true;
  1117. exitsama:
  1118. free(ptr);
  1119. return false;
  1120. }
  1121. static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1122. {
  1123. char *url, *user, *pass;
  1124. if (param == NULL || *param == '\0') {
  1125. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  1126. return;
  1127. }
  1128. if (!pooldetails(param, &url, &user, &pass)) {
  1129. strcpy(io_buffer, message(MSG_INVPDP, 0, param, isjson));
  1130. return;
  1131. }
  1132. if (add_pool_details(true, url, user, pass) == ADD_POOL_MAXIMUM) {
  1133. strcpy(io_buffer, message(MSG_TOOMANYP, MAX_POOLS, NULL, isjson));
  1134. return;
  1135. }
  1136. strcpy(io_buffer, message(MSG_ADDPOOL, 0, url, isjson));
  1137. }
  1138. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1139. {
  1140. struct pool *pool;
  1141. int id;
  1142. if (total_pools == 0) {
  1143. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1144. return;
  1145. }
  1146. if (param == NULL || *param == '\0') {
  1147. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1148. return;
  1149. }
  1150. id = atoi(param);
  1151. if (id < 0 || id >= total_pools) {
  1152. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1153. return;
  1154. }
  1155. pool = pools[id];
  1156. if (pool->enabled == true) {
  1157. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  1158. return;
  1159. }
  1160. pool->enabled = true;
  1161. if (pool->prio < current_pool()->prio)
  1162. switch_pools(pool);
  1163. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  1164. }
  1165. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1166. {
  1167. struct pool *pool;
  1168. int id;
  1169. if (total_pools == 0) {
  1170. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1171. return;
  1172. }
  1173. if (param == NULL || *param == '\0') {
  1174. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1175. return;
  1176. }
  1177. id = atoi(param);
  1178. if (id < 0 || id >= total_pools) {
  1179. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1180. return;
  1181. }
  1182. pool = pools[id];
  1183. if (pool->enabled == false) {
  1184. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  1185. return;
  1186. }
  1187. if (active_pools() <= 1) {
  1188. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  1189. return;
  1190. }
  1191. pool->enabled = false;
  1192. if (pool == current_pool())
  1193. switch_pools(NULL);
  1194. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  1195. }
  1196. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  1197. {
  1198. int id;
  1199. char *gpusep;
  1200. if (nDevs == 0) {
  1201. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1202. return false;
  1203. }
  1204. if (param == NULL || *param == '\0') {
  1205. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1206. return false;
  1207. }
  1208. gpusep = strchr(param, GPUSEP);
  1209. if (gpusep == NULL) {
  1210. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  1211. return false;
  1212. }
  1213. *(gpusep++) = '\0';
  1214. id = atoi(param);
  1215. if (id < 0 || id >= nDevs) {
  1216. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1217. return false;
  1218. }
  1219. *gpu = id;
  1220. *value = gpusep;
  1221. return true;
  1222. }
  1223. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1224. {
  1225. int id;
  1226. char *value;
  1227. int intensity;
  1228. char intensitystr[7];
  1229. if (!splitgpuvalue(param, &id, &value, isjson))
  1230. return;
  1231. if (!strncasecmp(value, DYNAMIC, 1)) {
  1232. gpus[id].dynamic = true;
  1233. strcpy(intensitystr, DYNAMIC);
  1234. }
  1235. else {
  1236. intensity = atoi(value);
  1237. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1238. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1239. return;
  1240. }
  1241. gpus[id].dynamic = false;
  1242. gpus[id].intensity = intensity;
  1243. sprintf(intensitystr, "%d", intensity);
  1244. }
  1245. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1246. }
  1247. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1248. {
  1249. #ifdef HAVE_ADL
  1250. int id;
  1251. char *value;
  1252. int clock;
  1253. if (!splitgpuvalue(param, &id, &value, isjson))
  1254. return;
  1255. clock = atoi(value);
  1256. if (set_memoryclock(id, clock))
  1257. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  1258. else
  1259. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  1260. #else
  1261. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1262. #endif
  1263. }
  1264. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1265. {
  1266. #ifdef HAVE_ADL
  1267. int id;
  1268. char *value;
  1269. int clock;
  1270. if (!splitgpuvalue(param, &id, &value, isjson))
  1271. return;
  1272. clock = atoi(value);
  1273. if (set_engineclock(id, clock))
  1274. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  1275. else
  1276. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  1277. #else
  1278. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1279. #endif
  1280. }
  1281. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1282. {
  1283. #ifdef HAVE_ADL
  1284. int id;
  1285. char *value;
  1286. int fan;
  1287. if (!splitgpuvalue(param, &id, &value, isjson))
  1288. return;
  1289. fan = atoi(value);
  1290. if (set_fanspeed(id, fan))
  1291. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  1292. else
  1293. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  1294. #else
  1295. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1296. #endif
  1297. }
  1298. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1299. {
  1300. #ifdef HAVE_ADL
  1301. int id;
  1302. char *value;
  1303. float vddc;
  1304. if (!splitgpuvalue(param, &id, &value, isjson))
  1305. return;
  1306. vddc = atof(value);
  1307. if (set_vddc(id, vddc))
  1308. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  1309. else
  1310. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  1311. #else
  1312. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1313. #endif
  1314. }
  1315. static void send_result(SOCKETTYPE c, bool isjson);
  1316. void doquit(SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1317. {
  1318. if (isjson)
  1319. strcpy(io_buffer, JSON_START JSON_BYE);
  1320. else
  1321. strcpy(io_buffer, _BYE);
  1322. send_result(c, isjson);
  1323. *io_buffer = '\0';
  1324. bye = 1;
  1325. PTH(&thr_info[my_thr_id]) = 0L;
  1326. kill_work();
  1327. }
  1328. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1329. {
  1330. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  1331. }
  1332. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1333. {
  1334. FILE *fcfg;
  1335. if (param == NULL || *param == '\0') {
  1336. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  1337. return;
  1338. }
  1339. fcfg = fopen(param, "w");
  1340. if (!fcfg) {
  1341. strcpy(io_buffer, message(MSG_BADFN, 0, param, isjson));
  1342. return;
  1343. }
  1344. write_config(fcfg);
  1345. fclose(fcfg);
  1346. strcpy(io_buffer, message(MSG_SAVED, 0, param, isjson));
  1347. }
  1348. struct CMDS {
  1349. char *name;
  1350. void (*func)(SOCKETTYPE, char *, bool);
  1351. bool requires_writemode;
  1352. } cmds[] = {
  1353. { "version", apiversion, false },
  1354. { "config", minerconfig, false },
  1355. { "devs", devstatus, false },
  1356. { "pools", poolstatus, false },
  1357. { "summary", summary, false },
  1358. { "gpuenable", gpuenable, true },
  1359. { "gpudisable", gpudisable, true },
  1360. { "gpurestart", gpurestart, true },
  1361. { "gpu", gpudev, false },
  1362. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  1363. { "pga", pgadev, false },
  1364. #endif
  1365. #ifdef WANT_CPUMINE
  1366. { "cpu", cpudev, false },
  1367. #endif
  1368. { "gpucount", gpucount, false },
  1369. { "pgacount", pgacount, false },
  1370. { "cpucount", cpucount, false },
  1371. { "switchpool", switchpool, true },
  1372. { "addpool", addpool, true },
  1373. { "enablepool", enablepool, true },
  1374. { "disablepool", disablepool, true },
  1375. { "gpuintensity", gpuintensity, true },
  1376. { "gpumem", gpumem, true },
  1377. { "gpuengine", gpuengine, true },
  1378. { "gpufan", gpufan, true },
  1379. { "gpuvddc", gpuvddc, true },
  1380. { "save", dosave, true },
  1381. { "quit", doquit, true },
  1382. { "privileged", privileged, true },
  1383. { NULL, NULL, false }
  1384. };
  1385. static void send_result(SOCKETTYPE c, bool isjson)
  1386. {
  1387. int n;
  1388. int len;
  1389. if (isjson)
  1390. strcat(io_buffer, JSON_END);
  1391. len = strlen(io_buffer);
  1392. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : "");
  1393. // ignore failure - it's closed immediately anyway
  1394. n = send(c, io_buffer, len+1, 0);
  1395. if (opt_debug) {
  1396. if (SOCKETFAIL(n))
  1397. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  1398. else
  1399. applog(LOG_DEBUG, "API: sent %d", n);
  1400. }
  1401. }
  1402. static void tidyup()
  1403. {
  1404. bye = 1;
  1405. if (sock != INVSOCK) {
  1406. shutdown(sock, SHUT_RDWR);
  1407. CLOSESOCKET(sock);
  1408. sock = INVSOCK;
  1409. }
  1410. if (ipaccess != NULL) {
  1411. free(ipaccess);
  1412. ipaccess = NULL;
  1413. }
  1414. if (msg_buffer != NULL) {
  1415. free(msg_buffer);
  1416. msg_buffer = NULL;
  1417. }
  1418. if (io_buffer != NULL) {
  1419. free(io_buffer);
  1420. io_buffer = NULL;
  1421. }
  1422. }
  1423. /*
  1424. * Interpret [R|W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  1425. * special case of 0/0 allows /0 (means all IP addresses)
  1426. */
  1427. #define ALLIP4 "0/0"
  1428. /*
  1429. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  1430. */
  1431. static void setup_ipaccess()
  1432. {
  1433. char *buf, *ptr, *comma, *slash, *dot;
  1434. int ipcount, mask, octet, i;
  1435. bool writemode;
  1436. buf = malloc(strlen(opt_api_allow) + 1);
  1437. if (unlikely(!buf))
  1438. quit(1, "Failed to malloc ipaccess buf");
  1439. strcpy(buf, opt_api_allow);
  1440. ipcount = 1;
  1441. ptr = buf;
  1442. while (*ptr)
  1443. if (*(ptr++) == ',')
  1444. ipcount++;
  1445. // possibly more than needed, but never less
  1446. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  1447. if (unlikely(!ipaccess))
  1448. quit(1, "Failed to calloc ipaccess");
  1449. ips = 0;
  1450. ptr = buf;
  1451. while (ptr && *ptr) {
  1452. while (*ptr == ' ' || *ptr == '\t')
  1453. ptr++;
  1454. if (*ptr == ',') {
  1455. ptr++;
  1456. continue;
  1457. }
  1458. comma = strchr(ptr, ',');
  1459. if (comma)
  1460. *(comma++) = '\0';
  1461. writemode = false;
  1462. if (isalpha(*ptr) && *(ptr+1) == ':') {
  1463. if (tolower(*ptr) == 'w')
  1464. writemode = true;
  1465. ptr += 2;
  1466. }
  1467. ipaccess[ips].writemode = writemode;
  1468. if (strcmp(ptr, ALLIP4) == 0)
  1469. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  1470. else {
  1471. slash = strchr(ptr, '/');
  1472. if (!slash)
  1473. ipaccess[ips].mask = 0xffffffff;
  1474. else {
  1475. *(slash++) = '\0';
  1476. mask = atoi(slash);
  1477. if (mask < 1 || mask > 32)
  1478. goto popipo; // skip invalid/zero
  1479. ipaccess[ips].mask = 0;
  1480. while (mask-- >= 0) {
  1481. octet = 1 << (mask % 8);
  1482. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  1483. }
  1484. }
  1485. ipaccess[ips].ip = 0; // missing default to '.0'
  1486. for (i = 0; ptr && (i < 4); i++) {
  1487. dot = strchr(ptr, '.');
  1488. if (dot)
  1489. *(dot++) = '\0';
  1490. octet = atoi(ptr);
  1491. if (octet < 0 || octet > 0xff)
  1492. goto popipo; // skip invalid
  1493. ipaccess[ips].ip |= (octet << (i * 8));
  1494. ptr = dot;
  1495. }
  1496. ipaccess[ips].ip &= ipaccess[ips].mask;
  1497. }
  1498. ips++;
  1499. popipo:
  1500. ptr = comma;
  1501. }
  1502. free(buf);
  1503. }
  1504. void api(int api_thr_id)
  1505. {
  1506. char buf[BUFSIZ];
  1507. char param_buf[BUFSIZ];
  1508. const char *localaddr = "127.0.0.1";
  1509. SOCKETTYPE c;
  1510. int n, bound;
  1511. char *connectaddr;
  1512. char *binderror;
  1513. time_t bindstart;
  1514. short int port = opt_api_port;
  1515. struct sockaddr_in serv;
  1516. struct sockaddr_in cli;
  1517. socklen_t clisiz;
  1518. char *cmd;
  1519. char *param;
  1520. bool addrok;
  1521. bool writemode;
  1522. json_error_t json_err;
  1523. json_t *json_config;
  1524. json_t *json_val;
  1525. bool isjson;
  1526. bool did;
  1527. int i;
  1528. my_thr_id = api_thr_id;
  1529. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1530. sleep(opt_log_interval);
  1531. if (!opt_api_listen) {
  1532. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1533. return;
  1534. }
  1535. if (opt_api_allow) {
  1536. setup_ipaccess();
  1537. if (ips == 0) {
  1538. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  1539. return;
  1540. }
  1541. }
  1542. sock = socket(AF_INET, SOCK_STREAM, 0);
  1543. if (sock == INVSOCK) {
  1544. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1545. return;
  1546. }
  1547. memset(&serv, 0, sizeof(serv));
  1548. serv.sin_family = AF_INET;
  1549. if (!opt_api_allow && !opt_api_network) {
  1550. serv.sin_addr.s_addr = inet_addr(localaddr);
  1551. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  1552. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1553. return;
  1554. }
  1555. }
  1556. serv.sin_port = htons(port);
  1557. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1558. bound = 0;
  1559. bindstart = time(NULL);
  1560. while (bound == 0) {
  1561. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1562. binderror = SOCKERRMSG;
  1563. if ((time(NULL) - bindstart) > 61)
  1564. break;
  1565. else {
  1566. applog(LOG_WARNING, "API bind to port %d failed - trying again in 15sec", port);
  1567. sleep(15);
  1568. }
  1569. }
  1570. else
  1571. bound = 1;
  1572. }
  1573. if (bound == 0) {
  1574. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1575. return;
  1576. }
  1577. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1578. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1579. CLOSESOCKET(sock);
  1580. return;
  1581. }
  1582. if (opt_api_allow)
  1583. applog(LOG_WARNING, "API running in IP access mode");
  1584. else {
  1585. if (opt_api_network)
  1586. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1587. else
  1588. applog(LOG_WARNING, "API running in local access mode");
  1589. }
  1590. io_buffer = malloc(MYBUFSIZ+1);
  1591. msg_buffer = malloc(MYBUFSIZ+1);
  1592. while (bye == 0) {
  1593. clisiz = sizeof(cli);
  1594. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1595. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1596. goto die;
  1597. }
  1598. connectaddr = inet_ntoa(cli.sin_addr);
  1599. addrok = false;
  1600. writemode = false;
  1601. if (opt_api_allow) {
  1602. for (i = 0; i < ips; i++) {
  1603. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  1604. addrok = true;
  1605. writemode = ipaccess[i].writemode;
  1606. break;
  1607. }
  1608. }
  1609. } else {
  1610. if (opt_api_network)
  1611. addrok = true;
  1612. else
  1613. addrok = (strcmp(connectaddr, localaddr) == 0);
  1614. }
  1615. if (opt_debug)
  1616. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1617. if (addrok) {
  1618. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1619. if (SOCKETFAIL(n))
  1620. buf[0] = '\0';
  1621. else
  1622. buf[n] = '\0';
  1623. if (opt_debug) {
  1624. if (SOCKETFAIL(n))
  1625. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  1626. else
  1627. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  1628. }
  1629. if (!SOCKETFAIL(n)) {
  1630. // the time of the request in now
  1631. when = time(NULL);
  1632. did = false;
  1633. if (*buf != ISJSON) {
  1634. isjson = false;
  1635. param = strchr(buf, SEPARATOR);
  1636. if (param != NULL)
  1637. *(param++) = '\0';
  1638. cmd = buf;
  1639. }
  1640. else {
  1641. isjson = true;
  1642. param = NULL;
  1643. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  1644. json_config = json_loadb(buf, n, 0, &json_err);
  1645. #elif JANSSON_MAJOR_VERSION > 1
  1646. json_config = json_loads(buf, 0, &json_err);
  1647. #else
  1648. json_config = json_loads(buf, &json_err);
  1649. #endif
  1650. if (!json_is_object(json_config)) {
  1651. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1652. send_result(c, isjson);
  1653. did = true;
  1654. }
  1655. else {
  1656. json_val = json_object_get(json_config, JSON_COMMAND);
  1657. if (json_val == NULL) {
  1658. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1659. send_result(c, isjson);
  1660. did = true;
  1661. }
  1662. else {
  1663. if (!json_is_string(json_val)) {
  1664. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1665. send_result(c, isjson);
  1666. did = true;
  1667. }
  1668. else {
  1669. cmd = (char *)json_string_value(json_val);
  1670. json_val = json_object_get(json_config, JSON_PARAMETER);
  1671. if (json_is_string(json_val))
  1672. param = (char *)json_string_value(json_val);
  1673. else if (json_is_integer(json_val)) {
  1674. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1675. param = param_buf;
  1676. } else if (json_is_real(json_val)) {
  1677. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1678. param = param_buf;
  1679. }
  1680. }
  1681. }
  1682. }
  1683. }
  1684. if (!did)
  1685. for (i = 0; cmds[i].name != NULL; i++) {
  1686. if (strcmp(cmd, cmds[i].name) == 0) {
  1687. if (cmds[i].requires_writemode && !writemode) {
  1688. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  1689. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  1690. }
  1691. else
  1692. (cmds[i].func)(c, param, isjson);
  1693. send_result(c, isjson);
  1694. did = true;
  1695. break;
  1696. }
  1697. }
  1698. if (!did) {
  1699. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1700. send_result(c, isjson);
  1701. }
  1702. }
  1703. }
  1704. CLOSESOCKET(c);
  1705. }
  1706. die:
  1707. tidyup();
  1708. }