api.c 52 KB

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