api.c 104 KB

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