api.c 51 KB

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