api.c 82 KB

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