api.c 89 KB

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