api.c 57 KB

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