api.c 55 KB

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