api.c 100 KB

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