api.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152
  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 *INVAPIGROUPS = "Invalid --api-groups parameter";
  139. static const char *BLANK = "";
  140. static const char *COMMA = ",";
  141. static const char SEPARATOR = '|';
  142. #define SEPSTR "|"
  143. static const char GPUSEP = ',';
  144. static const char *APIVERSION = "1.13";
  145. static const char *DEAD = "Dead";
  146. static const char *SICK = "Sick";
  147. static const char *NOSTART = "NoStart";
  148. static const char *DISABLED = "Disabled";
  149. static const char *ALIVE = "Alive";
  150. static const char *REJECTING = "Rejecting";
  151. static const char *UNKNOWN = "Unknown";
  152. #define _DYNAMIC "D"
  153. static const char *DYNAMIC = _DYNAMIC;
  154. static const char *YES = "Y";
  155. static const char *NO = "N";
  156. static const char *DEVICECODE = ""
  157. #ifdef HAVE_OPENCL
  158. "GPU "
  159. #endif
  160. #ifdef USE_BITFORCE
  161. "BFL "
  162. #endif
  163. #ifdef USE_ICARUS
  164. "ICA "
  165. #endif
  166. #ifdef USE_ZTEX
  167. "ZTX "
  168. #endif
  169. #ifdef USE_MODMINER
  170. "MMQ "
  171. #endif
  172. #ifdef WANT_CPUMINE
  173. "CPU "
  174. #endif
  175. "";
  176. static const char *OSINFO =
  177. #if defined(__linux)
  178. "Linux";
  179. #else
  180. #if defined(__APPLE__)
  181. "Apple";
  182. #else
  183. #if defined (WIN32)
  184. "Windows";
  185. #else
  186. #if defined(unix)
  187. "Unix";
  188. #else
  189. "Unknown";
  190. #endif
  191. #endif
  192. #endif
  193. #endif
  194. #define _DEVS "DEVS"
  195. #define _POOLS "POOLS"
  196. #define _SUMMARY "SUMMARY"
  197. #define _STATUS "STATUS"
  198. #define _VERSION "VERSION"
  199. #define _MINECON "CONFIG"
  200. #define _GPU "GPU"
  201. #ifdef HAVE_AN_FPGA
  202. #define _PGA "PGA"
  203. #endif
  204. #ifdef WANT_CPUMINE
  205. #define _CPU "CPU"
  206. #endif
  207. #define _GPUS "GPUS"
  208. #define _PGAS "PGAS"
  209. #define _CPUS "CPUS"
  210. #define _NOTIFY "NOTIFY"
  211. #define _DEVDETAILS "DEVDETAILS"
  212. #define _BYE "BYE"
  213. #define _RESTART "RESTART"
  214. #define _MINESTATS "STATS"
  215. #define _CHECK "CHECK"
  216. static const char ISJSON = '{';
  217. #define JSON0 "{"
  218. #define JSON1 "\""
  219. #define JSON2 "\":["
  220. #define JSON3 "]"
  221. #define JSON4 ",\"id\":1}"
  222. #define JSON_START JSON0
  223. #define JSON_DEVS JSON1 _DEVS JSON2
  224. #define JSON_POOLS JSON1 _POOLS JSON2
  225. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  226. #define JSON_STATUS JSON1 _STATUS JSON2
  227. #define JSON_VERSION JSON1 _VERSION JSON2
  228. #define JSON_MINECON JSON1 _MINECON JSON2
  229. #define JSON_GPU JSON1 _GPU JSON2
  230. #ifdef HAVE_AN_FPGA
  231. #define JSON_PGA JSON1 _PGA JSON2
  232. #endif
  233. #ifdef WANT_CPUMINE
  234. #define JSON_CPU JSON1 _CPU JSON2
  235. #endif
  236. #define JSON_GPUS JSON1 _GPUS JSON2
  237. #define JSON_PGAS JSON1 _PGAS JSON2
  238. #define JSON_CPUS JSON1 _CPUS JSON2
  239. #define JSON_NOTIFY JSON1 _NOTIFY JSON2
  240. #define JSON_DEVDETAILS JSON1 _DEVDETAILS JSON2
  241. #define JSON_BYE JSON1 _BYE JSON1
  242. #define JSON_RESTART JSON1 _RESTART JSON1
  243. #define JSON_CLOSE JSON3
  244. #define JSON_MINESTATS JSON1 _MINESTATS JSON2
  245. #define JSON_CHECK JSON1 _CHECK JSON2
  246. #define JSON_END JSON4
  247. static const char *JSON_COMMAND = "command";
  248. static const char *JSON_PARAMETER = "parameter";
  249. #define MSG_INVGPU 1
  250. #define MSG_ALRENA 2
  251. #define MSG_ALRDIS 3
  252. #define MSG_GPUMRE 4
  253. #define MSG_GPUREN 5
  254. #define MSG_GPUNON 6
  255. #define MSG_POOL 7
  256. #define MSG_NOPOOL 8
  257. #define MSG_DEVS 9
  258. #define MSG_NODEVS 10
  259. #define MSG_SUMM 11
  260. #define MSG_GPUDIS 12
  261. #define MSG_GPUREI 13
  262. #define MSG_INVCMD 14
  263. #define MSG_MISID 15
  264. #define MSG_GPUDEV 17
  265. #ifdef WANT_CPUMINE
  266. #define MSG_CPUNON 16
  267. #define MSG_CPUDEV 18
  268. #define MSG_INVCPU 19
  269. #endif
  270. #define MSG_NUMGPU 20
  271. #define MSG_NUMCPU 21
  272. #define MSG_VERSION 22
  273. #define MSG_INVJSON 23
  274. #define MSG_MISCMD 24
  275. #define MSG_MISPID 25
  276. #define MSG_INVPID 26
  277. #define MSG_SWITCHP 27
  278. #define MSG_MISVAL 28
  279. #define MSG_NOADL 29
  280. #define MSG_NOGPUADL 30
  281. #define MSG_INVINT 31
  282. #define MSG_GPUINT 32
  283. #define MSG_MINECON 33
  284. #define MSG_GPUMERR 34
  285. #define MSG_GPUMEM 35
  286. #define MSG_GPUEERR 36
  287. #define MSG_GPUENG 37
  288. #define MSG_GPUVERR 38
  289. #define MSG_GPUVDDC 39
  290. #define MSG_GPUFERR 40
  291. #define MSG_GPUFAN 41
  292. #define MSG_MISFN 42
  293. #define MSG_BADFN 43
  294. #define MSG_SAVED 44
  295. #define MSG_ACCDENY 45
  296. #define MSG_ACCOK 46
  297. #define MSG_ENAPOOL 47
  298. #define MSG_DISPOOL 48
  299. #define MSG_ALRENAP 49
  300. #define MSG_ALRDISP 50
  301. #define MSG_DISLASTP 51
  302. #define MSG_MISPDP 52
  303. #define MSG_INVPDP 53
  304. #define MSG_TOOMANYP 54
  305. #define MSG_ADDPOOL 55
  306. #ifdef HAVE_AN_FPGA
  307. #define MSG_PGANON 56
  308. #define MSG_PGADEV 57
  309. #define MSG_INVPGA 58
  310. #endif
  311. #define MSG_NUMPGA 59
  312. #define MSG_NOTIFY 60
  313. #ifdef HAVE_AN_FPGA
  314. #define MSG_PGALRENA 61
  315. #define MSG_PGALRDIS 62
  316. #define MSG_PGAENA 63
  317. #define MSG_PGADIS 64
  318. #define MSG_PGAUNW 65
  319. #endif
  320. #define MSG_REMLASTP 66
  321. #define MSG_ACTPOOL 67
  322. #define MSG_REMPOOL 68
  323. #define MSG_DEVDETAILS 69
  324. #define MSG_MINESTATS 70
  325. #define MSG_MISCHK 71
  326. #define MSG_CHECK 72
  327. enum code_severity {
  328. SEVERITY_ERR,
  329. SEVERITY_WARN,
  330. SEVERITY_INFO,
  331. SEVERITY_SUCC,
  332. SEVERITY_FAIL
  333. };
  334. enum code_parameters {
  335. PARAM_GPU,
  336. PARAM_PGA,
  337. PARAM_CPU,
  338. PARAM_GPUMAX,
  339. PARAM_PGAMAX,
  340. PARAM_CPUMAX,
  341. PARAM_PMAX,
  342. PARAM_POOLMAX,
  343. // Single generic case: have the code resolve it - see below
  344. PARAM_DMAX,
  345. PARAM_CMD,
  346. PARAM_POOL,
  347. PARAM_STR,
  348. PARAM_BOTH,
  349. PARAM_NONE
  350. };
  351. struct CODES {
  352. const enum code_severity severity;
  353. const int code;
  354. const enum code_parameters params;
  355. const char *description;
  356. } codes[] = {
  357. #ifdef HAVE_OPENCL
  358. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  359. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  360. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  361. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  362. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  363. #endif
  364. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  365. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  366. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  367. { SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX,
  368. #ifdef HAVE_OPENCL
  369. "%d GPU(s)"
  370. #endif
  371. #if defined(HAVE_AN_FPGA) && defined(HAVE_OPENCL)
  372. " - "
  373. #endif
  374. #ifdef HAVE_AN_FPGA
  375. "%d PGA(s)"
  376. #endif
  377. #if defined(WANT_CPUMINE) && (defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA))
  378. " - "
  379. #endif
  380. #ifdef WANT_CPUMINE
  381. "%d CPU(s)"
  382. #endif
  383. },
  384. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
  385. #ifdef HAVE_AN_FPGA
  386. "/PGAs"
  387. #endif
  388. #ifdef WANT_CPUMINE
  389. "/CPUs"
  390. #endif
  391. },
  392. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  393. #ifdef HAVE_OPENCL
  394. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  395. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  396. #endif
  397. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  398. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  399. #ifdef HAVE_OPENCL
  400. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  401. #endif
  402. #ifdef HAVE_AN_FPGA
  403. { SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
  404. { SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
  405. { SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
  406. { SEVERITY_INFO, MSG_PGALRENA,PARAM_PGA, "PGA %d already enabled" },
  407. { SEVERITY_INFO, MSG_PGALRDIS,PARAM_PGA, "PGA %d already disabled" },
  408. { SEVERITY_INFO, MSG_PGAENA, PARAM_PGA, "PGA %d sent enable message" },
  409. { SEVERITY_INFO, MSG_PGADIS, PARAM_PGA, "PGA %d set disable flag" },
  410. { SEVERITY_ERR, MSG_PGAUNW, PARAM_PGA, "PGA %d is not flagged WELL, cannot enable" },
  411. #endif
  412. #ifdef WANT_CPUMINE
  413. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  414. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  415. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  416. #endif
  417. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  418. { SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
  419. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  420. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "CGMiner versions" },
  421. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  422. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  423. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  424. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  425. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  426. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  427. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  428. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  429. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  430. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  431. { SEVERITY_SUCC, MSG_MINECON, PARAM_NONE, "CGMiner config" },
  432. #ifdef HAVE_OPENCL
  433. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  434. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported success" },
  435. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  436. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported success" },
  437. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  438. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported success" },
  439. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  440. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported success" },
  441. #endif
  442. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  443. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  444. { SEVERITY_SUCC, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  445. { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
  446. { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
  447. { SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
  448. { SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
  449. { SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
  450. { SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
  451. { SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
  452. { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
  453. { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
  454. { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
  455. { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
  456. { SEVERITY_ERR, MSG_REMLASTP,PARAM_POOL, "Cannot remove last pool %d:'%s'" },
  457. { SEVERITY_ERR, MSG_ACTPOOL, PARAM_POOL, "Cannot remove active pool %d:'%s'" },
  458. { SEVERITY_SUCC, MSG_REMPOOL, PARAM_BOTH, "Removed pool %d:'%s'" },
  459. { SEVERITY_SUCC, MSG_NOTIFY, PARAM_NONE, "Notify" },
  460. { SEVERITY_SUCC, MSG_DEVDETAILS,PARAM_NONE, "Device Details" },
  461. { SEVERITY_SUCC, MSG_MINESTATS,PARAM_NONE, "CGMiner stats" },
  462. { SEVERITY_ERR, MSG_MISCHK, PARAM_NONE, "Missing check cmd" },
  463. { SEVERITY_SUCC, MSG_CHECK, PARAM_NONE, "Check command" },
  464. { SEVERITY_FAIL, 0, 0, NULL }
  465. };
  466. static int my_thr_id = 0;
  467. static bool bye;
  468. static bool ping = true;
  469. // Used to control quit restart access to shutdown variables
  470. static pthread_mutex_t quit_restart_lock;
  471. static bool do_a_quit;
  472. static bool do_a_restart;
  473. static time_t when = 0; // when the request occurred
  474. struct IP4ACCESS {
  475. in_addr_t ip;
  476. in_addr_t mask;
  477. char group;
  478. };
  479. #define GROUP(g) (toupper(g))
  480. #define PRIVGROUP GROUP('W')
  481. #define NOPRIVGROUP GROUP('R')
  482. #define ISPRIVGROUP(g) (GROUP(g) == PRIVGROUP)
  483. #define GROUPOFFSET(g) (GROUP(g) - GROUP('A'))
  484. #define VALIDGROUP(g) (GROUP(g) >= GROUP('A') && GROUP(g) <= GROUP('Z'))
  485. #define COMMANDS(g) (apigroups[GROUPOFFSET(g)].commands)
  486. #define DEFINEDGROUP(g) (ISPRIVGROUP(g) || COMMANDS(g) != NULL)
  487. struct APIGROUPS {
  488. // This becomes a string like: "|cmd1|cmd2|cmd3|" so it's quick to search
  489. char *commands;
  490. } apigroups['Z' - 'A' + 1]; // only A=0 to Z=25 (R: noprivs, W: allprivs)
  491. static struct IP4ACCESS *ipaccess = NULL;
  492. static int ips = 0;
  493. #ifdef USE_BITFORCE
  494. extern struct device_api bitforce_api;
  495. #endif
  496. #ifdef USE_ICARUS
  497. extern struct device_api icarus_api;
  498. #endif
  499. #ifdef USE_ZTEX
  500. extern struct device_api ztex_api;
  501. #endif
  502. #ifdef USE_MODMINER
  503. extern struct device_api modminer_api;
  504. #endif
  505. // This is only called when expected to be needed (rarely)
  506. // i.e. strings outside of the codes control (input from the user)
  507. static char *escape_string(char *str, bool isjson)
  508. {
  509. char *buf, *ptr;
  510. int count;
  511. count = 0;
  512. for (ptr = str; *ptr; ptr++) {
  513. switch (*ptr) {
  514. case ',':
  515. case '|':
  516. case '=':
  517. if (!isjson)
  518. count++;
  519. break;
  520. case '"':
  521. if (isjson)
  522. count++;
  523. break;
  524. case '\\':
  525. count++;
  526. break;
  527. }
  528. }
  529. if (count == 0)
  530. return str;
  531. buf = malloc(strlen(str) + count + 1);
  532. if (unlikely(!buf))
  533. quit(1, "Failed to malloc escape buf");
  534. ptr = buf;
  535. while (*str)
  536. switch (*str) {
  537. case ',':
  538. case '|':
  539. case '=':
  540. if (!isjson)
  541. *(ptr++) = '\\';
  542. *(ptr++) = *(str++);
  543. break;
  544. case '"':
  545. if (isjson)
  546. *(ptr++) = '\\';
  547. *(ptr++) = *(str++);
  548. break;
  549. case '\\':
  550. *(ptr++) = '\\';
  551. *(ptr++) = *(str++);
  552. break;
  553. default:
  554. *(ptr++) = *(str++);
  555. break;
  556. }
  557. *ptr = '\0';
  558. return buf;
  559. }
  560. struct api_data *api_add_data_full(struct api_data *root, char *name, enum api_data_type type, void *data, bool copy_data)
  561. {
  562. struct api_data *api_data;
  563. api_data = (struct api_data *)malloc(sizeof(struct api_data));
  564. api_data->type = type;
  565. api_data->name = name;
  566. if (root == NULL) {
  567. root = api_data;
  568. root->prev = root;
  569. root->next = root;
  570. }
  571. else {
  572. api_data->prev = root->prev;
  573. root->prev = api_data;
  574. api_data->next = root;
  575. api_data->prev->next = api_data;
  576. }
  577. api_data->data_was_malloc = copy_data;
  578. if (!copy_data)
  579. api_data->data = data;
  580. else
  581. switch(type) {
  582. case API_ESCAPE:
  583. case API_STRING:
  584. api_data->data = (void *)malloc(strlen((char *)data) + 1);
  585. strcpy((char*)(api_data->data), (char *)data);
  586. break;
  587. case API_INT:
  588. api_data->data = (void *)malloc(sizeof(int));
  589. *((int *)(api_data->data)) = *((int *)data);
  590. break;
  591. case API_UINT:
  592. api_data->data = (void *)malloc(sizeof(unsigned int));
  593. *((unsigned int *)(api_data->data)) = *((unsigned int *)data);
  594. break;
  595. case API_UINT64:
  596. api_data->data = (void *)malloc(sizeof(uint64_t));
  597. *((uint64_t *)(api_data->data)) = *((uint64_t *)data);
  598. break;
  599. case API_ULONG:
  600. api_data->data = (void *)malloc(sizeof(unsigned long));
  601. *((unsigned long *)(api_data->data)) = *((unsigned long *)data);
  602. break;
  603. case API_DOUBLE:
  604. case API_MHS:
  605. case API_MHTOTAL:
  606. case API_UTILITY:
  607. case API_VOLTS:
  608. case API_HS:
  609. api_data->data = (void *)malloc(sizeof(double));
  610. *((double *)(api_data->data)) = *((double *)data);
  611. break;
  612. case API_BOOL:
  613. api_data->data = (void *)malloc(sizeof(bool));
  614. *((bool *)(api_data->data)) = *((bool *)data);
  615. break;
  616. case API_TIMEVAL:
  617. api_data->data = (void *)malloc(sizeof(struct timeval));
  618. memcpy(api_data->data, data, sizeof(struct timeval));
  619. break;
  620. case API_TIME:
  621. api_data->data = (void *)malloc(sizeof(time_t));
  622. *(time_t *)(api_data->data) = *((time_t *)data);
  623. break;
  624. case API_TEMP:
  625. api_data->data = (void *)malloc(sizeof(float));
  626. *((float *)(api_data->data)) = *((float *)data);
  627. break;
  628. default:
  629. applog(LOG_ERR, "API: unknown1 data type %d ignored", type);
  630. api_data->type = API_STRING;
  631. api_data->data_was_malloc = false;
  632. api_data->data = (void *)UNKNOWN;
  633. break;
  634. }
  635. return root;
  636. }
  637. static struct api_data *print_data(struct api_data *root, char *buf, bool isjson)
  638. {
  639. struct api_data *tmp;
  640. bool first = true;
  641. char *quote;
  642. if (isjson) {
  643. strcpy(buf, JSON0);
  644. quote = JSON1;
  645. } else {
  646. strcpy(buf, COMMA);
  647. quote = (char *)BLANK;
  648. }
  649. buf = strchr(buf, '\0');
  650. while (root) {
  651. if (!first)
  652. *(buf++) = *COMMA;
  653. else
  654. first = false;
  655. sprintf(buf, "%s%s%s%s", quote, root->name, quote, isjson ? ":" : "=");
  656. buf = strchr(buf, '\0');
  657. switch(root->type) {
  658. case API_STRING:
  659. sprintf(buf, "%s%s%s", quote, (char *)(root->data), quote);
  660. break;
  661. case API_ESCAPE:
  662. sprintf(buf, "%s%s%s",
  663. quote,
  664. escape_string((char *)(root->data), isjson),
  665. quote);
  666. break;
  667. case API_INT:
  668. sprintf(buf, "%d", *((int *)(root->data)));
  669. break;
  670. case API_UINT:
  671. sprintf(buf, "%u", *((unsigned int *)(root->data)));
  672. break;
  673. case API_UINT64:
  674. sprintf(buf, "%"PRIu64, *((uint64_t *)(root->data)));
  675. break;
  676. case API_ULONG:
  677. case API_TIME:
  678. sprintf(buf, "%lu", *((unsigned long *)(root->data)));
  679. break;
  680. case API_DOUBLE:
  681. sprintf(buf, "%f", *((double *)(root->data)));
  682. break;
  683. case API_UTILITY:
  684. case API_MHS:
  685. sprintf(buf, "%.2f", *((double *)(root->data)));
  686. break;
  687. case API_VOLTS:
  688. sprintf(buf, "%.3f", *((double *)(root->data)));
  689. break;
  690. case API_MHTOTAL:
  691. sprintf(buf, "%.4f", *((double *)(root->data)));
  692. break;
  693. case API_HS:
  694. sprintf(buf, "%.15f", *((double *)(root->data)));
  695. break;
  696. case API_BOOL:
  697. sprintf(buf, "%s", *((bool *)(root->data)) ? "true" : "false");
  698. break;
  699. case API_TIMEVAL:
  700. sprintf(buf, "%ld.%06ld",
  701. ((struct timeval *)(root->data))->tv_sec,
  702. ((struct timeval *)(root->data))->tv_usec);
  703. break;
  704. case API_TEMP:
  705. sprintf(buf, "%.2f", *((float *)(root->data)));
  706. break;
  707. default:
  708. applog(LOG_ERR, "API: unknown2 data type %d ignored", root->type);
  709. sprintf(buf, "%s%s%s", quote, UNKNOWN, quote);
  710. break;
  711. }
  712. buf = strchr(buf, '\0');
  713. if (root->data_was_malloc)
  714. free(root->data);
  715. if (root->next == root) {
  716. free(root);
  717. root = NULL;
  718. } else {
  719. tmp = root;
  720. root = tmp->next;
  721. root->prev = tmp->prev;
  722. root->prev->next = root;
  723. free(tmp);
  724. }
  725. }
  726. if (isjson)
  727. strcat(buf, "}");
  728. return root;
  729. }
  730. #ifdef HAVE_AN_FPGA
  731. static int numpgas()
  732. {
  733. int count = 0;
  734. int i;
  735. for (i = 0; i < total_devices; i++) {
  736. #ifdef USE_BITFORCE
  737. if (devices[i]->api == &bitforce_api)
  738. count++;
  739. #endif
  740. #ifdef USE_ICARUS
  741. if (devices[i]->api == &icarus_api)
  742. count++;
  743. #endif
  744. #ifdef USE_ZTEX
  745. if (devices[i]->api == &ztex_api)
  746. count++;
  747. #endif
  748. #ifdef USE_MODMINER
  749. if (devices[i]->api == &modminer_api)
  750. count++;
  751. #endif
  752. }
  753. return count;
  754. }
  755. static int pgadevice(int pgaid)
  756. {
  757. int count = 0;
  758. int i;
  759. for (i = 0; i < total_devices; i++) {
  760. #ifdef USE_BITFORCE
  761. if (devices[i]->api == &bitforce_api)
  762. count++;
  763. #endif
  764. #ifdef USE_ICARUS
  765. if (devices[i]->api == &icarus_api)
  766. count++;
  767. #endif
  768. #ifdef USE_ZTEX
  769. if (devices[i]->api == &ztex_api)
  770. count++;
  771. #endif
  772. #ifdef USE_MODMINER
  773. if (devices[i]->api == &modminer_api)
  774. count++;
  775. #endif
  776. if (count == (pgaid + 1))
  777. return i;
  778. }
  779. return -1;
  780. }
  781. #endif
  782. // All replies (except BYE and RESTART) start with a message
  783. // thus for JSON, message() inserts JSON_START at the front
  784. // and send_result() adds JSON_END at the end
  785. static char *message(int messageid, int paramid, char *param2, bool isjson)
  786. {
  787. char severity;
  788. char *ptr;
  789. #ifdef HAVE_AN_FPGA
  790. int pga;
  791. #endif
  792. #ifdef WANT_CPUMINE
  793. int cpu;
  794. #endif
  795. int i;
  796. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  797. if (codes[i].code == messageid) {
  798. switch (codes[i].severity) {
  799. case SEVERITY_WARN:
  800. severity = 'W';
  801. break;
  802. case SEVERITY_INFO:
  803. severity = 'I';
  804. break;
  805. case SEVERITY_SUCC:
  806. severity = 'S';
  807. break;
  808. case SEVERITY_ERR:
  809. default:
  810. severity = 'E';
  811. break;
  812. }
  813. sprintf(msg_buffer, isjson
  814. ? JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"When\":%lu,\"Code\":%d,\"Msg\":\""
  815. : _STATUS "=%c,When=%lu,Code=%d,Msg=",
  816. severity, (unsigned long)when, messageid);
  817. ptr = msg_buffer + strlen(msg_buffer);
  818. switch(codes[i].params) {
  819. case PARAM_GPU:
  820. case PARAM_PGA:
  821. case PARAM_CPU:
  822. sprintf(ptr, codes[i].description, paramid);
  823. break;
  824. case PARAM_POOL:
  825. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  826. break;
  827. #ifdef HAVE_OPENCL
  828. case PARAM_GPUMAX:
  829. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  830. break;
  831. #endif
  832. #ifdef HAVE_AN_FPGA
  833. case PARAM_PGAMAX:
  834. pga = numpgas();
  835. sprintf(ptr, codes[i].description, paramid, pga - 1);
  836. break;
  837. #endif
  838. #ifdef WANT_CPUMINE
  839. case PARAM_CPUMAX:
  840. if (opt_n_threads > 0)
  841. cpu = num_processors;
  842. else
  843. cpu = 0;
  844. sprintf(ptr, codes[i].description, paramid, cpu - 1);
  845. break;
  846. #endif
  847. case PARAM_PMAX:
  848. sprintf(ptr, codes[i].description, total_pools);
  849. break;
  850. case PARAM_POOLMAX:
  851. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  852. break;
  853. case PARAM_DMAX:
  854. #ifdef HAVE_AN_FPGA
  855. pga = numpgas();
  856. #endif
  857. #ifdef WANT_CPUMINE
  858. if (opt_n_threads > 0)
  859. cpu = num_processors;
  860. else
  861. cpu = 0;
  862. #endif
  863. sprintf(ptr, codes[i].description
  864. #ifdef HAVE_OPENCL
  865. , nDevs
  866. #endif
  867. #ifdef HAVE_AN_FPGA
  868. , pga
  869. #endif
  870. #ifdef WANT_CPUMINE
  871. , cpu
  872. #endif
  873. );
  874. break;
  875. case PARAM_CMD:
  876. sprintf(ptr, codes[i].description, JSON_COMMAND);
  877. break;
  878. case PARAM_STR:
  879. sprintf(ptr, codes[i].description, param2);
  880. break;
  881. case PARAM_BOTH:
  882. sprintf(ptr, codes[i].description, paramid, param2);
  883. break;
  884. case PARAM_NONE:
  885. default:
  886. strcpy(ptr, codes[i].description);
  887. }
  888. ptr = msg_buffer + strlen(msg_buffer);
  889. sprintf(ptr, isjson
  890. ? "\",\"Description\":\"%s\"}" JSON_CLOSE
  891. : ",Description=%s" SEPSTR,
  892. opt_api_description);
  893. return msg_buffer;
  894. }
  895. }
  896. sprintf(msg_buffer, isjson
  897. ? JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"When\":%lu,\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE
  898. : _STATUS "=F,When=%lu,Code=-1,Msg=%d,Description=%s" SEPSTR,
  899. (unsigned long)when, messageid, opt_api_description);
  900. return msg_buffer;
  901. }
  902. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  903. {
  904. struct api_data *root = NULL;
  905. char buf[TMPBUFSIZ];
  906. sprintf(io_buffer, isjson
  907. ? "%s," JSON_VERSION
  908. : "%s" _VERSION,
  909. message(MSG_VERSION, 0, NULL, isjson));
  910. root = api_add_data(root, "CGMiner", API_STRING, VERSION);
  911. root = api_add_data(root, "API", API_STRING, APIVERSION);
  912. /*
  913. sprintf(io_buffer, isjson
  914. ? "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE
  915. : "%s" _VERSION ",CGMiner=%s,API=%s" SEPSTR,
  916. message(MSG_VERSION, 0, NULL, isjson),
  917. VERSION, APIVERSION);
  918. */
  919. root = print_data(root, buf, isjson);
  920. strcat(io_buffer, buf);
  921. strcat(io_buffer, isjson ? JSON_CLOSE : SEPSTR);
  922. }
  923. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  924. {
  925. struct api_data *root = NULL;
  926. char buf[TMPBUFSIZ];
  927. int gpucount = 0;
  928. int pgacount = 0;
  929. int cpucount = 0;
  930. char *adlinuse = (char *)NO;
  931. #ifdef HAVE_ADL
  932. const char *adl = YES;
  933. int i;
  934. for (i = 0; i < nDevs; i++) {
  935. if (gpus[i].has_adl) {
  936. adlinuse = (char *)YES;
  937. break;
  938. }
  939. }
  940. #else
  941. const char *adl = NO;
  942. #endif
  943. #ifdef HAVE_OPENCL
  944. gpucount = nDevs;
  945. #endif
  946. #ifdef HAVE_AN_FPGA
  947. pgacount = numpgas();
  948. #endif
  949. #ifdef WANT_CPUMINE
  950. cpucount = opt_n_threads > 0 ? num_processors : 0;
  951. #endif
  952. sprintf(io_buffer, isjson
  953. ? "%s," JSON_MINECON
  954. : "%s" _MINECON ",",
  955. message(MSG_MINECON, 0, NULL, isjson));
  956. root = api_add_data(root, "GPU Count", API_INT, &gpucount);
  957. root = api_add_data(root, "PGA Count", API_INT, &pgacount);
  958. root = api_add_data(root, "CPU Count", API_INT, &cpucount);
  959. root = api_add_data(root, "Pool Count", API_INT, &total_pools);
  960. root = api_add_data(root, "ADL", API_STRING, adl);
  961. root = api_add_data(root, "ADL in use", API_STRING, adlinuse);
  962. root = api_add_data(root, "Strategy", API_STRING, strategies[pool_strategy].s);
  963. root = api_add_data(root, "Log Interval", API_INT, &opt_log_interval);
  964. root = api_add_data(root, "Device Code", API_STRING, DEVICECODE);
  965. root = api_add_data(root, "OS", API_STRING, OSINFO);
  966. /*
  967. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  968. sprintf(buf, isjson
  969. ? "," 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
  970. : _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,
  971. gpucount, pgacount, cpucount, total_pools, adl, adlinuse,
  972. strategies[pool_strategy].s, opt_log_interval, DEVICECODE, OSINFO);
  973. strcat(io_buffer, buf);
  974. */
  975. root = print_data(root, buf, isjson);
  976. strcat(io_buffer, buf);
  977. strcat(io_buffer, isjson ? JSON_CLOSE : SEPSTR);
  978. }
  979. #ifdef HAVE_OPENCL
  980. static void gpustatus(int gpu, bool isjson)
  981. {
  982. struct api_data *root = NULL;
  983. char intensity[20];
  984. char buf[TMPBUFSIZ];
  985. char *enabled;
  986. char *status;
  987. float gt, gv;
  988. int ga, gf, gp, gc, gm, pt;
  989. if (gpu >= 0 && gpu < nDevs) {
  990. struct cgpu_info *cgpu = &gpus[gpu];
  991. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  992. #ifdef HAVE_ADL
  993. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  994. #endif
  995. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  996. if (cgpu->deven != DEV_DISABLED)
  997. enabled = (char *)YES;
  998. else
  999. enabled = (char *)NO;
  1000. if (cgpu->status == LIFE_DEAD)
  1001. status = (char *)DEAD;
  1002. else if (cgpu->status == LIFE_SICK)
  1003. status = (char *)SICK;
  1004. else if (cgpu->status == LIFE_NOSTART)
  1005. status = (char *)NOSTART;
  1006. else
  1007. status = (char *)ALIVE;
  1008. if (cgpu->dynamic)
  1009. strcpy(intensity, DYNAMIC);
  1010. else
  1011. sprintf(intensity, "%d", cgpu->intensity);
  1012. root = api_add_data(root, "GPU", API_INT, &gpu);
  1013. root = api_add_data(root, "Enabled", API_STRING, &enabled);
  1014. root = api_add_data(root, "Status", API_STRING, &status);
  1015. root = api_add_data(root, "Temperature", API_TEMP, &gt);
  1016. root = api_add_data(root, "Fan Speed", API_INT, &gf);
  1017. root = api_add_data(root, "Fan Percent", API_INT, &gp);
  1018. root = api_add_data(root, "GPU Clock", API_INT, &gc);
  1019. root = api_add_data(root, "Memory Clock", API_INT, &gm);
  1020. root = api_add_data(root, "GPU Voltage", API_VOLTS, &gv);
  1021. root = api_add_data(root, "GPU Activity", API_INT, &ga);
  1022. root = api_add_data(root, "Powertune", API_INT, &pt);
  1023. double mhs = cgpu->total_mhashes / total_secs;
  1024. root = api_add_data(root, "MHS av", API_MHS, &mhs);
  1025. char mhsname[27];
  1026. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1027. root = api_add_data(root, mhsname, API_MHS, &(cgpu->rolling));
  1028. root = api_add_data(root, "Accepted", API_INT, &(cgpu->accepted));
  1029. root = api_add_data(root, "Rejected", API_INT, &(cgpu->rejected));
  1030. root = api_add_data(root, "Hardware Errors", API_INT, &(cgpu->hw_errors));
  1031. root = api_add_data(root, "Utility", API_UTILITY, &(cgpu->utility));
  1032. root = api_add_data(root, "Intensity", API_STRING, intensity);
  1033. unsigned long last_share_pool = cgpu->last_share_pool_time > 0 ? cgpu->last_share_pool : -1;
  1034. root = api_add_data(root, "Last Share Pool", API_INT, &last_share_pool);
  1035. root = api_add_data(root, "Last Share Time", API_ULONG, &(cgpu->last_share_pool_time));
  1036. root = api_add_data(root, "Total MH", API_MHTOTAL, &(cgpu->total_mhashes));
  1037. /*
  1038. sprintf(buf, isjson
  1039. ? "{\"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}"
  1040. : "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,
  1041. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  1042. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  1043. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  1044. cgpu->utility, intensity,
  1045. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  1046. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  1047. strcat(io_buffer, buf);
  1048. */
  1049. root = print_data(root, buf, isjson);
  1050. strcat(io_buffer, buf);
  1051. strcat(io_buffer, isjson ? BLANK : SEPSTR);
  1052. }
  1053. }
  1054. #endif
  1055. #ifdef HAVE_AN_FPGA
  1056. static void pgastatus(int pga, bool isjson)
  1057. {
  1058. char buf[TMPBUFSIZ];
  1059. char *enabled;
  1060. char *status;
  1061. int numpga = numpgas();
  1062. if (numpga > 0 && pga >= 0 && pga < numpga) {
  1063. int dev = pgadevice(pga);
  1064. if (dev < 0) // Should never happen
  1065. return;
  1066. struct cgpu_info *cgpu = devices[dev];
  1067. double frequency = 0;
  1068. float temp = cgpu->temp;
  1069. #ifdef USE_ZTEX
  1070. if (cgpu->api == &ztex_api && cgpu->device_ztex)
  1071. frequency = cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1);
  1072. #endif
  1073. #ifdef USE_MODMINER
  1074. // TODO: a modminer has up to 4 devices but only 1 set of data for all ...
  1075. // except 4 sets of data for temp/clock
  1076. // So this should change in the future to just find the single temp/clock
  1077. // if the modminer code splits the device into seperate devices later
  1078. // For now, just display the highest temp and the average clock
  1079. if (cgpu->api == &modminer_api) {
  1080. int tc = cgpu->threads;
  1081. int i;
  1082. temp = 0;
  1083. if (tc > 4)
  1084. tc = 4;
  1085. for (i = 0; i < tc; i++) {
  1086. struct thr_info *thr = cgpu->thr[i];
  1087. struct modminer_fpga_state *state = thr->cgpu_data;
  1088. if (state->temp > temp)
  1089. temp = state->temp;
  1090. frequency += state->clock;
  1091. }
  1092. frequency /= (tc ? tc : 1);
  1093. }
  1094. #endif
  1095. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  1096. if (cgpu->deven != DEV_DISABLED)
  1097. enabled = (char *)YES;
  1098. else
  1099. enabled = (char *)NO;
  1100. if (cgpu->status == LIFE_DEAD)
  1101. status = (char *)DEAD;
  1102. else if (cgpu->status == LIFE_SICK)
  1103. status = (char *)SICK;
  1104. else if (cgpu->status == LIFE_NOSTART)
  1105. status = (char *)NOSTART;
  1106. else
  1107. status = (char *)ALIVE;
  1108. sprintf(buf, isjson
  1109. ? "{\"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}"
  1110. : "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,
  1111. pga, cgpu->api->name, cgpu->device_id,
  1112. enabled, status, temp,
  1113. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  1114. cgpu->accepted, cgpu->rejected, cgpu->hw_errors, cgpu->utility,
  1115. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  1116. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, frequency);
  1117. strcat(io_buffer, buf);
  1118. }
  1119. }
  1120. #endif
  1121. #ifdef WANT_CPUMINE
  1122. static void cpustatus(int cpu, bool isjson)
  1123. {
  1124. char buf[TMPBUFSIZ];
  1125. if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
  1126. struct cgpu_info *cgpu = &cpus[cpu];
  1127. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  1128. sprintf(buf, isjson
  1129. ? "{\"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}"
  1130. : "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,
  1131. cpu, cgpu->total_mhashes / total_secs,
  1132. opt_log_interval, cgpu->rolling,
  1133. cgpu->accepted, cgpu->rejected,
  1134. cgpu->utility,
  1135. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  1136. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  1137. strcat(io_buffer, buf);
  1138. }
  1139. }
  1140. #endif
  1141. static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1142. {
  1143. int devcount = 0;
  1144. int numgpu = 0;
  1145. int numpga = 0;
  1146. int i;
  1147. #ifdef HAVE_OPENCL
  1148. numgpu = nDevs;
  1149. #endif
  1150. #ifdef HAVE_AN_FPGA
  1151. numpga = numpgas();
  1152. #endif
  1153. if (numgpu == 0 && opt_n_threads == 0 && numpga == 0) {
  1154. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1155. return;
  1156. }
  1157. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  1158. if (isjson) {
  1159. strcat(io_buffer, COMMA);
  1160. strcat(io_buffer, JSON_DEVS);
  1161. }
  1162. #ifdef HAVE_OPENCL
  1163. for (i = 0; i < nDevs; i++) {
  1164. if (isjson && devcount > 0)
  1165. strcat(io_buffer, COMMA);
  1166. gpustatus(i, isjson);
  1167. devcount++;
  1168. }
  1169. #endif
  1170. #ifdef HAVE_AN_FPGA
  1171. if (numpga > 0)
  1172. for (i = 0; i < numpga; i++) {
  1173. if (isjson && devcount > 0)
  1174. strcat(io_buffer, COMMA);
  1175. pgastatus(i, isjson);
  1176. devcount++;
  1177. }
  1178. #endif
  1179. #ifdef WANT_CPUMINE
  1180. if (opt_n_threads > 0)
  1181. for (i = 0; i < num_processors; i++) {
  1182. if (isjson && devcount > 0)
  1183. strcat(io_buffer, COMMA);
  1184. cpustatus(i, isjson);
  1185. devcount++;
  1186. }
  1187. #endif
  1188. if (isjson)
  1189. strcat(io_buffer, JSON_CLOSE);
  1190. }
  1191. #ifdef HAVE_OPENCL
  1192. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1193. {
  1194. int id;
  1195. if (nDevs == 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. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  1209. if (isjson) {
  1210. strcat(io_buffer, COMMA);
  1211. strcat(io_buffer, JSON_GPU);
  1212. }
  1213. gpustatus(id, isjson);
  1214. if (isjson)
  1215. strcat(io_buffer, JSON_CLOSE);
  1216. }
  1217. #endif
  1218. #ifdef HAVE_AN_FPGA
  1219. static void pgadev(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1220. {
  1221. int numpga = numpgas();
  1222. int id;
  1223. if (numpga == 0) {
  1224. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  1225. return;
  1226. }
  1227. if (param == NULL || *param == '\0') {
  1228. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1229. return;
  1230. }
  1231. id = atoi(param);
  1232. if (id < 0 || id >= numpga) {
  1233. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1234. return;
  1235. }
  1236. strcpy(io_buffer, message(MSG_PGADEV, id, NULL, isjson));
  1237. if (isjson) {
  1238. strcat(io_buffer, COMMA);
  1239. strcat(io_buffer, JSON_PGA);
  1240. }
  1241. pgastatus(id, isjson);
  1242. if (isjson)
  1243. strcat(io_buffer, JSON_CLOSE);
  1244. }
  1245. static void pgaenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1246. {
  1247. int numpga = numpgas();
  1248. struct thr_info *thr;
  1249. int pga;
  1250. int id;
  1251. int i;
  1252. if (numpga == 0) {
  1253. strcpy(io_buffer, message(MSG_PGANON, 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 >= numpga) {
  1262. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1263. return;
  1264. }
  1265. int dev = pgadevice(id);
  1266. if (dev < 0) { // Should never happen
  1267. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1268. return;
  1269. }
  1270. struct cgpu_info *cgpu = devices[dev];
  1271. if (cgpu->deven != DEV_DISABLED) {
  1272. strcpy(io_buffer, message(MSG_PGALRENA, id, NULL, isjson));
  1273. return;
  1274. }
  1275. #if 0 /* A DISABLED device wont change status FIXME: should disabling make it WELL? */
  1276. if (cgpu->status != LIFE_WELL) {
  1277. strcpy(io_buffer, message(MSG_PGAUNW, id, NULL, isjson));
  1278. return;
  1279. }
  1280. #endif
  1281. for (i = 0; i < mining_threads; i++) {
  1282. pga = thr_info[i].cgpu->device_id;
  1283. if (pga == dev) {
  1284. thr = &thr_info[i];
  1285. cgpu->deven = DEV_ENABLED;
  1286. tq_push(thr->q, &ping);
  1287. }
  1288. }
  1289. strcpy(io_buffer, message(MSG_PGAENA, id, NULL, isjson));
  1290. }
  1291. static void pgadisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1292. {
  1293. int numpga = numpgas();
  1294. int id;
  1295. if (numpga == 0) {
  1296. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  1297. return;
  1298. }
  1299. if (param == NULL || *param == '\0') {
  1300. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1301. return;
  1302. }
  1303. id = atoi(param);
  1304. if (id < 0 || id >= numpga) {
  1305. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1306. return;
  1307. }
  1308. int dev = pgadevice(id);
  1309. if (dev < 0) { // Should never happen
  1310. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1311. return;
  1312. }
  1313. struct cgpu_info *cgpu = devices[dev];
  1314. if (cgpu->deven == DEV_DISABLED) {
  1315. strcpy(io_buffer, message(MSG_PGALRDIS, id, NULL, isjson));
  1316. return;
  1317. }
  1318. cgpu->deven = DEV_DISABLED;
  1319. strcpy(io_buffer, message(MSG_PGADIS, id, NULL, isjson));
  1320. }
  1321. #endif
  1322. #ifdef WANT_CPUMINE
  1323. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1324. {
  1325. int id;
  1326. if (opt_n_threads == 0) {
  1327. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  1328. return;
  1329. }
  1330. if (param == NULL || *param == '\0') {
  1331. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1332. return;
  1333. }
  1334. id = atoi(param);
  1335. if (id < 0 || id >= num_processors) {
  1336. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  1337. return;
  1338. }
  1339. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  1340. if (isjson) {
  1341. strcat(io_buffer, COMMA);
  1342. strcat(io_buffer, JSON_CPU);
  1343. }
  1344. cpustatus(id, isjson);
  1345. if (isjson)
  1346. strcat(io_buffer, JSON_CLOSE);
  1347. }
  1348. #endif
  1349. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1350. {
  1351. char buf[TMPBUFSIZ];
  1352. char *status, *lp;
  1353. char *rpc_url;
  1354. char *rpc_user;
  1355. int i;
  1356. if (total_pools == 0) {
  1357. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1358. return;
  1359. }
  1360. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  1361. if (isjson) {
  1362. strcat(io_buffer, COMMA);
  1363. strcat(io_buffer, JSON_POOLS);
  1364. }
  1365. for (i = 0; i < total_pools; i++) {
  1366. struct pool *pool = pools[i];
  1367. switch (pool->enabled) {
  1368. case POOL_DISABLED:
  1369. status = (char *)DISABLED;
  1370. break;
  1371. case POOL_REJECTING:
  1372. status = (char *)REJECTING;
  1373. break;
  1374. case POOL_ENABLED:
  1375. if (pool->idle)
  1376. status = (char *)DEAD;
  1377. else
  1378. status = (char *)ALIVE;
  1379. break;
  1380. default:
  1381. status = (char *)UNKNOWN;
  1382. break;
  1383. }
  1384. if (pool->hdr_path)
  1385. lp = (char *)YES;
  1386. else
  1387. lp = (char *)NO;
  1388. rpc_url = escape_string(pool->rpc_url, isjson);
  1389. rpc_user = escape_string(pool->rpc_user, isjson);
  1390. sprintf(buf, isjson
  1391. ? "%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}"
  1392. : "%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,
  1393. (isjson && (i > 0)) ? COMMA : BLANK,
  1394. i, rpc_url, status, pool->prio, lp,
  1395. pool->getwork_requested,
  1396. pool->accepted, pool->rejected,
  1397. pool->discarded_work,
  1398. pool->stale_shares,
  1399. pool->getfail_occasions,
  1400. pool->remotefail_occasions,
  1401. rpc_user, pool->last_share_time);
  1402. strcat(io_buffer, buf);
  1403. if (rpc_url != pool->rpc_url)
  1404. free(rpc_url);
  1405. rpc_url = NULL;
  1406. if (rpc_user != pool->rpc_user)
  1407. free(rpc_user);
  1408. rpc_user = NULL;
  1409. }
  1410. if (isjson)
  1411. strcat(io_buffer, JSON_CLOSE);
  1412. }
  1413. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1414. {
  1415. double utility, mhs;
  1416. #ifdef WANT_CPUMINE
  1417. char *algo = (char *)(algo_names[opt_algo]);
  1418. if (algo == NULL)
  1419. algo = "(null)";
  1420. #endif
  1421. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1422. mhs = total_mhashes_done / total_secs;
  1423. #ifdef WANT_CPUMINE
  1424. sprintf(io_buffer, isjson
  1425. ? "%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
  1426. : "%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,
  1427. message(MSG_SUMM, 0, NULL, isjson),
  1428. total_secs, algo, mhs, found_blocks,
  1429. total_getworks, total_accepted, total_rejected,
  1430. hw_errors, utility, total_discarded, total_stale,
  1431. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1432. #else
  1433. sprintf(io_buffer, isjson
  1434. ? "%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
  1435. : "%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,
  1436. message(MSG_SUMM, 0, NULL, isjson),
  1437. total_secs, mhs, found_blocks,
  1438. total_getworks, total_accepted, total_rejected,
  1439. hw_errors, utility, total_discarded, total_stale,
  1440. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1441. #endif
  1442. }
  1443. #ifdef HAVE_OPENCL
  1444. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1445. {
  1446. struct thr_info *thr;
  1447. int gpu;
  1448. int id;
  1449. int i;
  1450. if (gpu_threads == 0) {
  1451. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1452. return;
  1453. }
  1454. if (param == NULL || *param == '\0') {
  1455. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1456. return;
  1457. }
  1458. id = atoi(param);
  1459. if (id < 0 || id >= nDevs) {
  1460. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1461. return;
  1462. }
  1463. if (gpus[id].deven != DEV_DISABLED) {
  1464. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  1465. return;
  1466. }
  1467. for (i = 0; i < gpu_threads; i++) {
  1468. gpu = thr_info[i].cgpu->device_id;
  1469. if (gpu == id) {
  1470. thr = &thr_info[i];
  1471. if (thr->cgpu->status != LIFE_WELL) {
  1472. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  1473. return;
  1474. }
  1475. gpus[id].deven = DEV_ENABLED;
  1476. tq_push(thr->q, &ping);
  1477. }
  1478. }
  1479. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  1480. }
  1481. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1482. {
  1483. int id;
  1484. if (nDevs == 0) {
  1485. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1486. return;
  1487. }
  1488. if (param == NULL || *param == '\0') {
  1489. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1490. return;
  1491. }
  1492. id = atoi(param);
  1493. if (id < 0 || id >= nDevs) {
  1494. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1495. return;
  1496. }
  1497. if (gpus[id].deven == DEV_DISABLED) {
  1498. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  1499. return;
  1500. }
  1501. gpus[id].deven = DEV_DISABLED;
  1502. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  1503. }
  1504. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1505. {
  1506. int id;
  1507. if (nDevs == 0) {
  1508. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1509. return;
  1510. }
  1511. if (param == NULL || *param == '\0') {
  1512. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1513. return;
  1514. }
  1515. id = atoi(param);
  1516. if (id < 0 || id >= nDevs) {
  1517. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1518. return;
  1519. }
  1520. reinit_device(&gpus[id]);
  1521. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  1522. }
  1523. #endif
  1524. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1525. {
  1526. char buf[TMPBUFSIZ];
  1527. int numgpu = 0;
  1528. #ifdef HAVE_OPENCL
  1529. numgpu = nDevs;
  1530. #endif
  1531. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  1532. sprintf(buf, isjson
  1533. ? "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE
  1534. : _GPUS ",Count=%d" SEPSTR,
  1535. numgpu);
  1536. strcat(io_buffer, buf);
  1537. }
  1538. static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1539. {
  1540. char buf[TMPBUFSIZ];
  1541. int count = 0;
  1542. #ifdef HAVE_AN_FPGA
  1543. count = numpgas();
  1544. #endif
  1545. strcpy(io_buffer, message(MSG_NUMPGA, 0, NULL, isjson));
  1546. sprintf(buf, isjson
  1547. ? "," JSON_PGAS "{\"Count\":%d}" JSON_CLOSE
  1548. : _PGAS ",Count=%d" SEPSTR,
  1549. count);
  1550. strcat(io_buffer, buf);
  1551. }
  1552. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1553. {
  1554. char buf[TMPBUFSIZ];
  1555. int count = 0;
  1556. #ifdef WANT_CPUMINE
  1557. count = opt_n_threads > 0 ? num_processors : 0;
  1558. #endif
  1559. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  1560. sprintf(buf, isjson
  1561. ? "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE
  1562. : _CPUS ",Count=%d" SEPSTR,
  1563. count);
  1564. strcat(io_buffer, buf);
  1565. }
  1566. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1567. {
  1568. struct pool *pool;
  1569. int id;
  1570. if (total_pools == 0) {
  1571. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1572. return;
  1573. }
  1574. if (param == NULL || *param == '\0') {
  1575. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1576. return;
  1577. }
  1578. id = atoi(param);
  1579. if (id < 0 || id >= total_pools) {
  1580. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1581. return;
  1582. }
  1583. pool = pools[id];
  1584. pool->enabled = POOL_ENABLED;
  1585. switch_pools(pool);
  1586. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  1587. }
  1588. static void copyadvanceafter(char ch, char **param, char **buf)
  1589. {
  1590. #define src_p (*param)
  1591. #define dst_b (*buf)
  1592. while (*src_p && *src_p != ch) {
  1593. if (*src_p == '\\' && *(src_p+1) != '\0')
  1594. src_p++;
  1595. *(dst_b++) = *(src_p++);
  1596. }
  1597. if (*src_p)
  1598. src_p++;
  1599. *(dst_b++) = '\0';
  1600. }
  1601. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1602. {
  1603. char *ptr, *buf;
  1604. ptr = buf = malloc(strlen(param)+1);
  1605. if (unlikely(!buf))
  1606. quit(1, "Failed to malloc pooldetails buf");
  1607. *url = buf;
  1608. // copy url
  1609. copyadvanceafter(',', &param, &buf);
  1610. if (!(*param)) // missing user
  1611. goto exitsama;
  1612. *user = buf;
  1613. // copy user
  1614. copyadvanceafter(',', &param, &buf);
  1615. if (!*param) // missing pass
  1616. goto exitsama;
  1617. *pass = buf;
  1618. // copy pass
  1619. copyadvanceafter(',', &param, &buf);
  1620. return true;
  1621. exitsama:
  1622. free(ptr);
  1623. return false;
  1624. }
  1625. static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1626. {
  1627. char *url, *user, *pass;
  1628. char *ptr;
  1629. if (param == NULL || *param == '\0') {
  1630. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  1631. return;
  1632. }
  1633. if (!pooldetails(param, &url, &user, &pass)) {
  1634. ptr = escape_string(param, isjson);
  1635. strcpy(io_buffer, message(MSG_INVPDP, 0, ptr, isjson));
  1636. if (ptr != param)
  1637. free(ptr);
  1638. ptr = NULL;
  1639. return;
  1640. }
  1641. add_pool_details(true, url, user, pass);
  1642. ptr = escape_string(url, isjson);
  1643. strcpy(io_buffer, message(MSG_ADDPOOL, 0, ptr, isjson));
  1644. if (ptr != url)
  1645. free(ptr);
  1646. ptr = NULL;
  1647. }
  1648. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1649. {
  1650. struct pool *pool;
  1651. int id;
  1652. if (total_pools == 0) {
  1653. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1654. return;
  1655. }
  1656. if (param == NULL || *param == '\0') {
  1657. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1658. return;
  1659. }
  1660. id = atoi(param);
  1661. if (id < 0 || id >= total_pools) {
  1662. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1663. return;
  1664. }
  1665. pool = pools[id];
  1666. if (pool->enabled == POOL_ENABLED) {
  1667. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  1668. return;
  1669. }
  1670. pool->enabled = POOL_ENABLED;
  1671. if (pool->prio < current_pool()->prio)
  1672. switch_pools(pool);
  1673. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  1674. }
  1675. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1676. {
  1677. struct pool *pool;
  1678. int id;
  1679. if (total_pools == 0) {
  1680. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1681. return;
  1682. }
  1683. if (param == NULL || *param == '\0') {
  1684. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1685. return;
  1686. }
  1687. id = atoi(param);
  1688. if (id < 0 || id >= total_pools) {
  1689. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1690. return;
  1691. }
  1692. pool = pools[id];
  1693. if (pool->enabled == POOL_DISABLED) {
  1694. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  1695. return;
  1696. }
  1697. if (active_pools() <= 1) {
  1698. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  1699. return;
  1700. }
  1701. pool->enabled = POOL_DISABLED;
  1702. if (pool == current_pool())
  1703. switch_pools(NULL);
  1704. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  1705. }
  1706. static void removepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1707. {
  1708. struct pool *pool;
  1709. char *rpc_url;
  1710. bool dofree = false;
  1711. int id;
  1712. if (total_pools == 0) {
  1713. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1714. return;
  1715. }
  1716. if (param == NULL || *param == '\0') {
  1717. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1718. return;
  1719. }
  1720. id = atoi(param);
  1721. if (id < 0 || id >= total_pools) {
  1722. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1723. return;
  1724. }
  1725. if (total_pools <= 1) {
  1726. strcpy(io_buffer, message(MSG_REMLASTP, id, NULL, isjson));
  1727. return;
  1728. }
  1729. pool = pools[id];
  1730. if (pool == current_pool())
  1731. switch_pools(NULL);
  1732. if (pool == current_pool()) {
  1733. strcpy(io_buffer, message(MSG_ACTPOOL, id, NULL, isjson));
  1734. return;
  1735. }
  1736. pool->enabled = POOL_DISABLED;
  1737. rpc_url = escape_string(pool->rpc_url, isjson);
  1738. if (rpc_url != pool->rpc_url)
  1739. dofree = true;
  1740. remove_pool(pool);
  1741. strcpy(io_buffer, message(MSG_REMPOOL, id, rpc_url, isjson));
  1742. if (dofree)
  1743. free(rpc_url);
  1744. rpc_url = NULL;
  1745. }
  1746. #ifdef HAVE_OPENCL
  1747. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  1748. {
  1749. int id;
  1750. char *gpusep;
  1751. if (nDevs == 0) {
  1752. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1753. return false;
  1754. }
  1755. if (param == NULL || *param == '\0') {
  1756. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1757. return false;
  1758. }
  1759. gpusep = strchr(param, GPUSEP);
  1760. if (gpusep == NULL) {
  1761. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  1762. return false;
  1763. }
  1764. *(gpusep++) = '\0';
  1765. id = atoi(param);
  1766. if (id < 0 || id >= nDevs) {
  1767. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1768. return false;
  1769. }
  1770. *gpu = id;
  1771. *value = gpusep;
  1772. return true;
  1773. }
  1774. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1775. {
  1776. int id;
  1777. char *value;
  1778. int intensity;
  1779. char intensitystr[7];
  1780. if (!splitgpuvalue(param, &id, &value, isjson))
  1781. return;
  1782. if (!strncasecmp(value, DYNAMIC, 1)) {
  1783. gpus[id].dynamic = true;
  1784. strcpy(intensitystr, DYNAMIC);
  1785. }
  1786. else {
  1787. intensity = atoi(value);
  1788. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1789. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1790. return;
  1791. }
  1792. gpus[id].dynamic = false;
  1793. gpus[id].intensity = intensity;
  1794. sprintf(intensitystr, "%d", intensity);
  1795. }
  1796. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1797. }
  1798. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1799. {
  1800. #ifdef HAVE_ADL
  1801. int id;
  1802. char *value;
  1803. int clock;
  1804. if (!splitgpuvalue(param, &id, &value, isjson))
  1805. return;
  1806. clock = atoi(value);
  1807. if (set_memoryclock(id, clock))
  1808. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  1809. else
  1810. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  1811. #else
  1812. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1813. #endif
  1814. }
  1815. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1816. {
  1817. #ifdef HAVE_ADL
  1818. int id;
  1819. char *value;
  1820. int clock;
  1821. if (!splitgpuvalue(param, &id, &value, isjson))
  1822. return;
  1823. clock = atoi(value);
  1824. if (set_engineclock(id, clock))
  1825. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  1826. else
  1827. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  1828. #else
  1829. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1830. #endif
  1831. }
  1832. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1833. {
  1834. #ifdef HAVE_ADL
  1835. int id;
  1836. char *value;
  1837. int fan;
  1838. if (!splitgpuvalue(param, &id, &value, isjson))
  1839. return;
  1840. fan = atoi(value);
  1841. if (set_fanspeed(id, fan))
  1842. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  1843. else
  1844. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  1845. #else
  1846. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1847. #endif
  1848. }
  1849. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1850. {
  1851. #ifdef HAVE_ADL
  1852. int id;
  1853. char *value;
  1854. float vddc;
  1855. if (!splitgpuvalue(param, &id, &value, isjson))
  1856. return;
  1857. vddc = atof(value);
  1858. if (set_vddc(id, vddc))
  1859. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  1860. else
  1861. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  1862. #else
  1863. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1864. #endif
  1865. }
  1866. #endif
  1867. void doquit(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1868. {
  1869. if (isjson)
  1870. strcpy(io_buffer, JSON_START JSON_BYE);
  1871. else
  1872. strcpy(io_buffer, _BYE);
  1873. bye = true;
  1874. do_a_quit = true;
  1875. }
  1876. void dorestart(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1877. {
  1878. if (isjson)
  1879. strcpy(io_buffer, JSON_START JSON_RESTART);
  1880. else
  1881. strcpy(io_buffer, _RESTART);
  1882. bye = true;
  1883. do_a_restart = true;
  1884. }
  1885. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1886. {
  1887. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  1888. }
  1889. void notifystatus(int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
  1890. {
  1891. char buf[TMPBUFSIZ];
  1892. char *reason;
  1893. if (cgpu->device_last_not_well == 0)
  1894. reason = REASON_NONE;
  1895. else
  1896. switch(cgpu->device_not_well_reason) {
  1897. case REASON_THREAD_FAIL_INIT:
  1898. reason = REASON_THREAD_FAIL_INIT_STR;
  1899. break;
  1900. case REASON_THREAD_ZERO_HASH:
  1901. reason = REASON_THREAD_ZERO_HASH_STR;
  1902. break;
  1903. case REASON_THREAD_FAIL_QUEUE:
  1904. reason = REASON_THREAD_FAIL_QUEUE_STR;
  1905. break;
  1906. case REASON_DEV_SICK_IDLE_60:
  1907. reason = REASON_DEV_SICK_IDLE_60_STR;
  1908. break;
  1909. case REASON_DEV_DEAD_IDLE_600:
  1910. reason = REASON_DEV_DEAD_IDLE_600_STR;
  1911. break;
  1912. case REASON_DEV_NOSTART:
  1913. reason = REASON_DEV_NOSTART_STR;
  1914. break;
  1915. case REASON_DEV_OVER_HEAT:
  1916. reason = REASON_DEV_OVER_HEAT_STR;
  1917. break;
  1918. case REASON_DEV_THERMAL_CUTOFF:
  1919. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  1920. break;
  1921. default:
  1922. reason = REASON_UNKNOWN_STR;
  1923. break;
  1924. }
  1925. // ALL counters (and only counters) must start the name with a '*'
  1926. // Simplifies future external support for adding new counters
  1927. sprintf(buf, isjson
  1928. ? "%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}"
  1929. : "%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,
  1930. (isjson && (device > 0)) ? COMMA : BLANK,
  1931. device, cgpu->api->name, cgpu->device_id,
  1932. cgpu->device_last_well, cgpu->device_last_not_well, reason,
  1933. cgpu->thread_fail_init_count, cgpu->thread_zero_hash_count,
  1934. cgpu->thread_fail_queue_count, cgpu->dev_sick_idle_60_count,
  1935. cgpu->dev_dead_idle_600_count, cgpu->dev_nostart_count,
  1936. cgpu->dev_over_heat_count, cgpu->dev_thermal_cutoff_count);
  1937. strcat(io_buffer, buf);
  1938. }
  1939. static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
  1940. {
  1941. int i;
  1942. if (total_devices == 0) {
  1943. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1944. return;
  1945. }
  1946. strcpy(io_buffer, message(MSG_NOTIFY, 0, NULL, isjson));
  1947. if (isjson) {
  1948. strcat(io_buffer, COMMA);
  1949. strcat(io_buffer, JSON_NOTIFY);
  1950. }
  1951. for (i = 0; i < total_devices; i++)
  1952. notifystatus(i, devices[i], isjson, group);
  1953. if (isjson)
  1954. strcat(io_buffer, JSON_CLOSE);
  1955. }
  1956. static void devdetails(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1957. {
  1958. char buf[TMPBUFSIZ];
  1959. struct cgpu_info *cgpu;
  1960. int i;
  1961. if (total_devices == 0) {
  1962. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1963. return;
  1964. }
  1965. strcpy(io_buffer, message(MSG_DEVDETAILS, 0, NULL, isjson));
  1966. if (isjson) {
  1967. strcat(io_buffer, COMMA);
  1968. strcat(io_buffer, JSON_DEVDETAILS);
  1969. }
  1970. for (i = 0; i < total_devices; i++) {
  1971. cgpu = devices[i];
  1972. sprintf(buf, isjson
  1973. ? "%s{\"DEVDETAILS\":%d,\"Name\":\"%s\",\"ID\":%d,\"Driver\":\"%s\",\"Kernel\":\"%s\",\"Model\":\"%s\",\"Device Path\":\"%s\"}"
  1974. : "%sDEVDETAILS=%d,Name=%s,ID=%d,Driver=%s,Kernel=%s,Model=%s,Device Path=%s" SEPSTR,
  1975. (isjson && (i > 0)) ? COMMA : BLANK,
  1976. i, cgpu->api->name, cgpu->device_id,
  1977. cgpu->api->dname, cgpu->kname ? : BLANK,
  1978. cgpu->name ? : BLANK, cgpu->device_path ? : BLANK);
  1979. strcat(io_buffer, buf);
  1980. }
  1981. if (isjson)
  1982. strcat(io_buffer, JSON_CLOSE);
  1983. }
  1984. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1985. {
  1986. char filename[PATH_MAX];
  1987. FILE *fcfg;
  1988. char *ptr;
  1989. if (param == NULL || *param == '\0') {
  1990. default_save_file(filename);
  1991. param = filename;
  1992. }
  1993. fcfg = fopen(param, "w");
  1994. if (!fcfg) {
  1995. ptr = escape_string(param, isjson);
  1996. strcpy(io_buffer, message(MSG_BADFN, 0, ptr, isjson));
  1997. if (ptr != param)
  1998. free(ptr);
  1999. ptr = NULL;
  2000. return;
  2001. }
  2002. write_config(fcfg);
  2003. fclose(fcfg);
  2004. ptr = escape_string(param, isjson);
  2005. strcpy(io_buffer, message(MSG_SAVED, 0, ptr, isjson));
  2006. if (ptr != param)
  2007. free(ptr);
  2008. ptr = NULL;
  2009. }
  2010. static int itemstats(int i, char *id, struct cgminer_stats *stats, struct cgminer_pool_stats *pool_stats, char *extra, bool isjson)
  2011. {
  2012. char buf[TMPBUFSIZ];
  2013. if (stats->getwork_calls || (extra != NULL && *extra))
  2014. {
  2015. if (extra == NULL)
  2016. extra = (char *)BLANK;
  2017. sprintf(buf, isjson
  2018. ? "%s{\"STATS\":%d,\"ID\":\"%s\",\"Elapsed\":%.0f,\"Calls\":%d,\"Wait\":%ld.%06ld,\"Max\":%ld.%06ld,\"Min\":%ld.%06ld"
  2019. : "%sSTATS=%d,ID=%s,Elapsed=%.0f,Calls=%d,Wait=%ld.%06ld,Max=%ld.%06ld,Min=%ld.%06ld",
  2020. (isjson && (i > 0)) ? COMMA : BLANK,
  2021. i, id, total_secs, stats->getwork_calls,
  2022. stats->getwork_wait.tv_sec, stats->getwork_wait.tv_usec,
  2023. stats->getwork_wait_max.tv_sec, stats->getwork_wait_max.tv_usec,
  2024. stats->getwork_wait_min.tv_sec, stats->getwork_wait_min.tv_usec);
  2025. strcat(io_buffer, buf);
  2026. if (pool_stats) {
  2027. sprintf(buf, isjson
  2028. ? ",\"Pool Calls\":%d,\"Pool Attempts\":%d,\"Pool Wait\":%ld.%06ld,\"Pool Max\":%ld.%06ld,\"Pool Min\":%ld.%06ld,\"Pool Av\":%f"
  2029. : ",Pool Calls=%d,Pool Attempts=%d,Pool Wait=%ld.%06ld,Pool Max=%ld.%06ld,Pool Min=%ld.%06ld,Pool Av=%f",
  2030. pool_stats->getwork_calls, pool_stats->getwork_attempts,
  2031. pool_stats->getwork_wait.tv_sec, pool_stats->getwork_wait.tv_usec,
  2032. pool_stats->getwork_wait_max.tv_sec, pool_stats->getwork_wait_max.tv_usec,
  2033. pool_stats->getwork_wait_min.tv_sec, pool_stats->getwork_wait_min.tv_usec,
  2034. pool_stats->getwork_wait_rolling);
  2035. strcat(io_buffer, buf);
  2036. }
  2037. sprintf(buf, isjson
  2038. ? "%s%s}"
  2039. : "%s%s" SEPSTR,
  2040. *extra ? COMMA : BLANK, extra);
  2041. strcat(io_buffer, buf);
  2042. i++;
  2043. }
  2044. return i;
  2045. }
  2046. static void minerstats(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2047. {
  2048. char extra[TMPBUFSIZ];
  2049. char id[20];
  2050. int i, j;
  2051. strcpy(io_buffer, message(MSG_MINESTATS, 0, NULL, isjson));
  2052. if (isjson) {
  2053. strcat(io_buffer, COMMA);
  2054. strcat(io_buffer, JSON_MINESTATS);
  2055. }
  2056. i = 0;
  2057. for (j = 0; j < total_devices; j++) {
  2058. struct cgpu_info *cgpu = devices[j];
  2059. if (cgpu && cgpu->api) {
  2060. if (cgpu->api->get_api_stats)
  2061. cgpu->api->get_api_stats(extra, cgpu, isjson);
  2062. else
  2063. extra[0] = '\0';
  2064. sprintf(id, "%s%d", cgpu->api->name, cgpu->device_id);
  2065. i = itemstats(i, id, &(cgpu->cgminer_stats), NULL, extra, isjson);
  2066. }
  2067. }
  2068. for (j = 0; j < total_pools; j++) {
  2069. struct pool *pool = pools[j];
  2070. sprintf(id, "POOL%d", j);
  2071. i = itemstats(i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, isjson);
  2072. }
  2073. if (isjson)
  2074. strcat(io_buffer, JSON_CLOSE);
  2075. }
  2076. static void checkcommand(__maybe_unused SOCKETTYPE c, char *param, bool isjson, char group);
  2077. struct CMDS {
  2078. char *name;
  2079. void (*func)(SOCKETTYPE, char *, bool, char);
  2080. bool iswritemode;
  2081. } cmds[] = {
  2082. { "version", apiversion, false },
  2083. { "config", minerconfig, false },
  2084. { "devs", devstatus, false },
  2085. { "pools", poolstatus, false },
  2086. { "summary", summary, false },
  2087. #ifdef HAVE_OPENCL
  2088. { "gpuenable", gpuenable, true },
  2089. { "gpudisable", gpudisable, true },
  2090. { "gpurestart", gpurestart, true },
  2091. { "gpu", gpudev, false },
  2092. #endif
  2093. #ifdef HAVE_AN_FPGA
  2094. { "pga", pgadev, false },
  2095. { "pgaenable", pgaenable, true },
  2096. { "pgadisable", pgadisable, true },
  2097. #endif
  2098. #ifdef WANT_CPUMINE
  2099. { "cpu", cpudev, false },
  2100. #endif
  2101. { "gpucount", gpucount, false },
  2102. { "pgacount", pgacount, false },
  2103. { "cpucount", cpucount, false },
  2104. { "switchpool", switchpool, true },
  2105. { "addpool", addpool, true },
  2106. { "enablepool", enablepool, true },
  2107. { "disablepool", disablepool, true },
  2108. { "removepool", removepool, true },
  2109. #ifdef HAVE_OPENCL
  2110. { "gpuintensity", gpuintensity, true },
  2111. { "gpumem", gpumem, true },
  2112. { "gpuengine", gpuengine, true },
  2113. { "gpufan", gpufan, true },
  2114. { "gpuvddc", gpuvddc, true },
  2115. #endif
  2116. { "save", dosave, true },
  2117. { "quit", doquit, true },
  2118. { "privileged", privileged, true },
  2119. { "notify", notify, false },
  2120. { "devdetails", devdetails, false },
  2121. { "restart", dorestart, true },
  2122. { "stats", minerstats, false },
  2123. { "check", checkcommand, false },
  2124. { NULL, NULL, false }
  2125. };
  2126. static void checkcommand(__maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
  2127. {
  2128. char buf[TMPBUFSIZ];
  2129. char cmdbuf[100];
  2130. bool found, access;
  2131. int i;
  2132. if (param == NULL || *param == '\0') {
  2133. strcpy(io_buffer, message(MSG_MISCHK, 0, NULL, isjson));
  2134. return;
  2135. }
  2136. found = false;
  2137. access = false;
  2138. for (i = 0; cmds[i].name != NULL; i++) {
  2139. if (strcmp(cmds[i].name, param) == 0) {
  2140. found = true;
  2141. sprintf(cmdbuf, "|%s|", param);
  2142. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  2143. access = true;
  2144. break;
  2145. }
  2146. }
  2147. strcpy(io_buffer, message(MSG_CHECK, 0, NULL, isjson));
  2148. sprintf(buf, isjson
  2149. ? "," JSON_CHECK "{\"Exists\":\"%s\",\"Access\":\"%s\"}" JSON_CLOSE
  2150. : _CHECK ",Exists=%s,Access=%s" SEPSTR,
  2151. found ? YES : NO,
  2152. access ? YES : NO);
  2153. strcat(io_buffer, buf);
  2154. }
  2155. static void send_result(SOCKETTYPE c, bool isjson)
  2156. {
  2157. int n;
  2158. int len;
  2159. if (isjson)
  2160. strcat(io_buffer, JSON_END);
  2161. len = strlen(io_buffer);
  2162. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : BLANK);
  2163. // ignore failure - it's closed immediately anyway
  2164. n = send(c, io_buffer, len+1, 0);
  2165. if (opt_debug) {
  2166. if (SOCKETFAIL(n))
  2167. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  2168. else
  2169. applog(LOG_DEBUG, "API: sent %d", n);
  2170. }
  2171. }
  2172. static void tidyup(__maybe_unused void *arg)
  2173. {
  2174. mutex_lock(&quit_restart_lock);
  2175. bye = true;
  2176. if (sock != INVSOCK) {
  2177. shutdown(sock, SHUT_RDWR);
  2178. CLOSESOCKET(sock);
  2179. sock = INVSOCK;
  2180. }
  2181. if (ipaccess != NULL) {
  2182. free(ipaccess);
  2183. ipaccess = NULL;
  2184. }
  2185. if (msg_buffer != NULL) {
  2186. free(msg_buffer);
  2187. msg_buffer = NULL;
  2188. }
  2189. if (io_buffer != NULL) {
  2190. free(io_buffer);
  2191. io_buffer = NULL;
  2192. }
  2193. mutex_unlock(&quit_restart_lock);
  2194. }
  2195. /*
  2196. * Interpret --api-groups G:cmd1:cmd2:cmd3,P:cmd4,*,...
  2197. */
  2198. static void setup_groups()
  2199. {
  2200. char *api_groups = opt_api_groups ? opt_api_groups : (char *)BLANK;
  2201. char *buf, *ptr, *next, *colon;
  2202. char group;
  2203. char commands[TMPBUFSIZ];
  2204. char cmdbuf[100];
  2205. char *cmd;
  2206. bool addstar, did;
  2207. int i;
  2208. buf = malloc(strlen(api_groups) + 1);
  2209. if (unlikely(!buf))
  2210. quit(1, "Failed to malloc ipgroups buf");
  2211. strcpy(buf, api_groups);
  2212. next = buf;
  2213. // for each group defined
  2214. while (next && *next) {
  2215. ptr = next;
  2216. next = strchr(ptr, ',');
  2217. if (next)
  2218. *(next++) = '\0';
  2219. // Validate the group
  2220. if (*(ptr+1) != ':') {
  2221. colon = strchr(ptr, ':');
  2222. if (colon)
  2223. *colon = '\0';
  2224. applog(LOG_WARNING, "API invalid group name '%s'", ptr);
  2225. quit(1, INVAPIGROUPS);
  2226. }
  2227. group = GROUP(*ptr);
  2228. if (!VALIDGROUP(group)) {
  2229. applog(LOG_WARNING, "API invalid group name '%c'", *ptr);
  2230. quit(1, INVAPIGROUPS);
  2231. }
  2232. if (group == PRIVGROUP) {
  2233. applog(LOG_WARNING, "API group name can't be '%c'", PRIVGROUP);
  2234. quit(1, INVAPIGROUPS);
  2235. }
  2236. if (group == NOPRIVGROUP) {
  2237. applog(LOG_WARNING, "API group name can't be '%c'", NOPRIVGROUP);
  2238. quit(1, INVAPIGROUPS);
  2239. }
  2240. if (apigroups[GROUPOFFSET(group)].commands != NULL) {
  2241. applog(LOG_WARNING, "API duplicate group name '%c'", *ptr);
  2242. quit(1, INVAPIGROUPS);
  2243. }
  2244. ptr += 2;
  2245. // Validate the command list (and handle '*')
  2246. cmd = &(commands[0]);
  2247. *(cmd++) = SEPARATOR;
  2248. *cmd = '\0';
  2249. addstar = false;
  2250. while (ptr && *ptr) {
  2251. colon = strchr(ptr, ':');
  2252. if (colon)
  2253. *(colon++) = '\0';
  2254. if (strcmp(ptr, "*") == 0)
  2255. addstar = true;
  2256. else {
  2257. did = false;
  2258. for (i = 0; cmds[i].name != NULL; i++) {
  2259. if (strcasecmp(ptr, cmds[i].name) == 0) {
  2260. did = true;
  2261. break;
  2262. }
  2263. }
  2264. if (did) {
  2265. // skip duplicates
  2266. sprintf(cmdbuf, "|%s|", cmds[i].name);
  2267. if (strstr(commands, cmdbuf) == NULL) {
  2268. strcpy(cmd, cmds[i].name);
  2269. cmd += strlen(cmds[i].name);
  2270. *(cmd++) = SEPARATOR;
  2271. *cmd = '\0';
  2272. }
  2273. } else {
  2274. applog(LOG_WARNING, "API unknown command '%s' in group '%c'", ptr, group);
  2275. quit(1, INVAPIGROUPS);
  2276. }
  2277. }
  2278. ptr = colon;
  2279. }
  2280. // * = allow all non-iswritemode commands
  2281. if (addstar) {
  2282. for (i = 0; cmds[i].name != NULL; i++) {
  2283. if (cmds[i].iswritemode == false) {
  2284. // skip duplicates
  2285. sprintf(cmdbuf, "|%s|", cmds[i].name);
  2286. if (strstr(commands, cmdbuf) == NULL) {
  2287. strcpy(cmd, cmds[i].name);
  2288. cmd += strlen(cmds[i].name);
  2289. *(cmd++) = SEPARATOR;
  2290. *cmd = '\0';
  2291. }
  2292. }
  2293. }
  2294. }
  2295. ptr = apigroups[GROUPOFFSET(group)].commands = malloc(strlen(commands) + 1);
  2296. if (unlikely(!ptr))
  2297. quit(1, "Failed to malloc group commands buf");
  2298. strcpy(ptr, commands);
  2299. }
  2300. // Now define R (NOPRIVGROUP) as all non-iswritemode commands
  2301. cmd = &(commands[0]);
  2302. *(cmd++) = SEPARATOR;
  2303. *cmd = '\0';
  2304. for (i = 0; cmds[i].name != NULL; i++) {
  2305. if (cmds[i].iswritemode == false) {
  2306. strcpy(cmd, cmds[i].name);
  2307. cmd += strlen(cmds[i].name);
  2308. *(cmd++) = SEPARATOR;
  2309. *cmd = '\0';
  2310. }
  2311. }
  2312. ptr = apigroups[GROUPOFFSET(NOPRIVGROUP)].commands = malloc(strlen(commands) + 1);
  2313. if (unlikely(!ptr))
  2314. quit(1, "Failed to malloc noprivgroup commands buf");
  2315. strcpy(ptr, commands);
  2316. // W (PRIVGROUP) is handled as a special case since it simply means all commands
  2317. free(buf);
  2318. return;
  2319. }
  2320. /*
  2321. * Interpret [W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  2322. * special case of 0/0 allows /0 (means all IP addresses)
  2323. */
  2324. #define ALLIP4 "0/0"
  2325. /*
  2326. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  2327. */
  2328. static void setup_ipaccess()
  2329. {
  2330. char *buf, *ptr, *comma, *slash, *dot;
  2331. int ipcount, mask, octet, i;
  2332. char group;
  2333. buf = malloc(strlen(opt_api_allow) + 1);
  2334. if (unlikely(!buf))
  2335. quit(1, "Failed to malloc ipaccess buf");
  2336. strcpy(buf, opt_api_allow);
  2337. ipcount = 1;
  2338. ptr = buf;
  2339. while (*ptr)
  2340. if (*(ptr++) == ',')
  2341. ipcount++;
  2342. // possibly more than needed, but never less
  2343. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  2344. if (unlikely(!ipaccess))
  2345. quit(1, "Failed to calloc ipaccess");
  2346. ips = 0;
  2347. ptr = buf;
  2348. while (ptr && *ptr) {
  2349. while (*ptr == ' ' || *ptr == '\t')
  2350. ptr++;
  2351. if (*ptr == ',') {
  2352. ptr++;
  2353. continue;
  2354. }
  2355. comma = strchr(ptr, ',');
  2356. if (comma)
  2357. *(comma++) = '\0';
  2358. group = NOPRIVGROUP;
  2359. if (isalpha(*ptr) && *(ptr+1) == ':') {
  2360. if (DEFINEDGROUP(*ptr))
  2361. group = GROUP(*ptr);
  2362. ptr += 2;
  2363. }
  2364. ipaccess[ips].group = group;
  2365. if (strcmp(ptr, ALLIP4) == 0)
  2366. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  2367. else {
  2368. slash = strchr(ptr, '/');
  2369. if (!slash)
  2370. ipaccess[ips].mask = 0xffffffff;
  2371. else {
  2372. *(slash++) = '\0';
  2373. mask = atoi(slash);
  2374. if (mask < 1 || mask > 32)
  2375. goto popipo; // skip invalid/zero
  2376. ipaccess[ips].mask = 0;
  2377. while (mask-- >= 0) {
  2378. octet = 1 << (mask % 8);
  2379. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  2380. }
  2381. }
  2382. ipaccess[ips].ip = 0; // missing default to '.0'
  2383. for (i = 0; ptr && (i < 4); i++) {
  2384. dot = strchr(ptr, '.');
  2385. if (dot)
  2386. *(dot++) = '\0';
  2387. octet = atoi(ptr);
  2388. if (octet < 0 || octet > 0xff)
  2389. goto popipo; // skip invalid
  2390. ipaccess[ips].ip |= (octet << (i * 8));
  2391. ptr = dot;
  2392. }
  2393. ipaccess[ips].ip &= ipaccess[ips].mask;
  2394. }
  2395. ips++;
  2396. popipo:
  2397. ptr = comma;
  2398. }
  2399. free(buf);
  2400. }
  2401. static void *quit_thread(__maybe_unused void *userdata)
  2402. {
  2403. // allow thread creator to finish whatever it's doing
  2404. mutex_lock(&quit_restart_lock);
  2405. mutex_unlock(&quit_restart_lock);
  2406. if (opt_debug)
  2407. applog(LOG_DEBUG, "API: killing cgminer");
  2408. kill_work();
  2409. return NULL;
  2410. }
  2411. static void *restart_thread(__maybe_unused void *userdata)
  2412. {
  2413. // allow thread creator to finish whatever it's doing
  2414. mutex_lock(&quit_restart_lock);
  2415. mutex_unlock(&quit_restart_lock);
  2416. if (opt_debug)
  2417. applog(LOG_DEBUG, "API: restarting cgminer");
  2418. app_restart();
  2419. return NULL;
  2420. }
  2421. void api(int api_thr_id)
  2422. {
  2423. struct thr_info bye_thr;
  2424. char buf[TMPBUFSIZ];
  2425. char param_buf[TMPBUFSIZ];
  2426. const char *localaddr = "127.0.0.1";
  2427. SOCKETTYPE c;
  2428. int n, bound;
  2429. char *connectaddr;
  2430. char *binderror;
  2431. time_t bindstart;
  2432. short int port = opt_api_port;
  2433. struct sockaddr_in serv;
  2434. struct sockaddr_in cli;
  2435. socklen_t clisiz;
  2436. char cmdbuf[100];
  2437. char *cmd;
  2438. char *param;
  2439. bool addrok;
  2440. char group;
  2441. json_error_t json_err;
  2442. json_t *json_config;
  2443. json_t *json_val;
  2444. bool isjson;
  2445. bool did;
  2446. int i;
  2447. mutex_init(&quit_restart_lock);
  2448. pthread_cleanup_push(tidyup, NULL);
  2449. my_thr_id = api_thr_id;
  2450. if (!opt_api_listen) {
  2451. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  2452. return;
  2453. }
  2454. setup_groups();
  2455. if (opt_api_allow) {
  2456. setup_ipaccess();
  2457. if (ips == 0) {
  2458. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  2459. return;
  2460. }
  2461. }
  2462. /* This should be done before curl in needed
  2463. * to ensure curl has already called WSAStartup() in windows */
  2464. sleep(opt_log_interval);
  2465. sock = socket(AF_INET, SOCK_STREAM, 0);
  2466. if (sock == INVSOCK) {
  2467. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2468. return;
  2469. }
  2470. memset(&serv, 0, sizeof(serv));
  2471. serv.sin_family = AF_INET;
  2472. if (!opt_api_allow && !opt_api_network) {
  2473. serv.sin_addr.s_addr = inet_addr(localaddr);
  2474. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  2475. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2476. return;
  2477. }
  2478. }
  2479. serv.sin_port = htons(port);
  2480. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  2481. bound = 0;
  2482. bindstart = time(NULL);
  2483. while (bound == 0) {
  2484. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  2485. binderror = SOCKERRMSG;
  2486. if ((time(NULL) - bindstart) > 61)
  2487. break;
  2488. else {
  2489. applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
  2490. sleep(30);
  2491. }
  2492. } else
  2493. bound = 1;
  2494. }
  2495. if (bound == 0) {
  2496. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  2497. return;
  2498. }
  2499. if (SOCKETFAIL(listen(sock, QUEUE))) {
  2500. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2501. CLOSESOCKET(sock);
  2502. return;
  2503. }
  2504. if (opt_api_allow)
  2505. applog(LOG_WARNING, "API running in IP access mode");
  2506. else {
  2507. if (opt_api_network)
  2508. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  2509. else
  2510. applog(LOG_WARNING, "API running in local access mode");
  2511. }
  2512. io_buffer = malloc(MYBUFSIZ+1);
  2513. msg_buffer = malloc(MYBUFSIZ+1);
  2514. while (!bye) {
  2515. clisiz = sizeof(cli);
  2516. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  2517. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2518. goto die;
  2519. }
  2520. connectaddr = inet_ntoa(cli.sin_addr);
  2521. addrok = false;
  2522. group = NOPRIVGROUP;
  2523. if (opt_api_allow) {
  2524. for (i = 0; i < ips; i++) {
  2525. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  2526. addrok = true;
  2527. group = ipaccess[i].group;
  2528. break;
  2529. }
  2530. }
  2531. } else {
  2532. if (opt_api_network)
  2533. addrok = true;
  2534. else
  2535. addrok = (strcmp(connectaddr, localaddr) == 0);
  2536. }
  2537. if (opt_debug)
  2538. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  2539. if (addrok) {
  2540. n = recv(c, &buf[0], TMPBUFSIZ-1, 0);
  2541. if (SOCKETFAIL(n))
  2542. buf[0] = '\0';
  2543. else
  2544. buf[n] = '\0';
  2545. if (opt_debug) {
  2546. if (SOCKETFAIL(n))
  2547. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  2548. else
  2549. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  2550. }
  2551. if (!SOCKETFAIL(n)) {
  2552. // the time of the request in now
  2553. when = time(NULL);
  2554. did = false;
  2555. if (*buf != ISJSON) {
  2556. isjson = false;
  2557. param = strchr(buf, SEPARATOR);
  2558. if (param != NULL)
  2559. *(param++) = '\0';
  2560. cmd = buf;
  2561. }
  2562. else {
  2563. isjson = true;
  2564. param = NULL;
  2565. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  2566. json_config = json_loadb(buf, n, 0, &json_err);
  2567. #elif JANSSON_MAJOR_VERSION > 1
  2568. json_config = json_loads(buf, 0, &json_err);
  2569. #else
  2570. json_config = json_loads(buf, &json_err);
  2571. #endif
  2572. if (!json_is_object(json_config)) {
  2573. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  2574. send_result(c, isjson);
  2575. did = true;
  2576. }
  2577. else {
  2578. json_val = json_object_get(json_config, JSON_COMMAND);
  2579. if (json_val == NULL) {
  2580. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  2581. send_result(c, isjson);
  2582. did = true;
  2583. }
  2584. else {
  2585. if (!json_is_string(json_val)) {
  2586. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  2587. send_result(c, isjson);
  2588. did = true;
  2589. }
  2590. else {
  2591. cmd = (char *)json_string_value(json_val);
  2592. json_val = json_object_get(json_config, JSON_PARAMETER);
  2593. if (json_is_string(json_val))
  2594. param = (char *)json_string_value(json_val);
  2595. else if (json_is_integer(json_val)) {
  2596. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  2597. param = param_buf;
  2598. } else if (json_is_real(json_val)) {
  2599. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  2600. param = param_buf;
  2601. }
  2602. }
  2603. }
  2604. }
  2605. }
  2606. if (!did)
  2607. for (i = 0; cmds[i].name != NULL; i++) {
  2608. if (strcmp(cmd, cmds[i].name) == 0) {
  2609. sprintf(cmdbuf, "|%s|", cmd);
  2610. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  2611. (cmds[i].func)(c, param, isjson, group);
  2612. else {
  2613. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  2614. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  2615. }
  2616. send_result(c, isjson);
  2617. did = true;
  2618. break;
  2619. }
  2620. }
  2621. if (!did) {
  2622. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  2623. send_result(c, isjson);
  2624. }
  2625. }
  2626. }
  2627. CLOSESOCKET(c);
  2628. }
  2629. die:
  2630. /* Blank line fix for older compilers since pthread_cleanup_pop is a
  2631. * macro that gets confused by a label existing immediately before it
  2632. */
  2633. ;
  2634. pthread_cleanup_pop(true);
  2635. if (opt_debug)
  2636. applog(LOG_DEBUG, "API: terminating due to: %s",
  2637. do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
  2638. mutex_lock(&quit_restart_lock);
  2639. if (do_a_restart) {
  2640. if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
  2641. mutex_unlock(&quit_restart_lock);
  2642. quit(1, "API failed to initiate a restart - aborting");
  2643. }
  2644. pthread_detach(bye_thr.pth);
  2645. } else if (do_a_quit) {
  2646. if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
  2647. mutex_unlock(&quit_restart_lock);
  2648. quit(1, "API failed to initiate a clean quit - aborting");
  2649. }
  2650. pthread_detach(bye_thr.pth);
  2651. }
  2652. mutex_unlock(&quit_restart_lock);
  2653. }