api.c 65 KB

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