api.c 81 KB

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