api.c 62 KB

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