api.c 92 KB

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