api.c 92 KB

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