api.c 91 KB

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