api.c 57 KB

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