api.c 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028
  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. goto foundit;
  1056. }
  1057. mutex_unlock(&devices_lock);
  1058. return -1;
  1059. foundit:
  1060. mutex_unlock(&devices_lock);
  1061. return i;
  1062. }
  1063. #endif
  1064. // All replies (except BYE and RESTART) start with a message
  1065. // thus for JSON, message() inserts JSON_START at the front
  1066. // and send_result() adds JSON_END at the end
  1067. static void message(struct io_data *io_data, int messageid, int paramid, char *param2, bool isjson)
  1068. {
  1069. struct api_data *root = NULL;
  1070. char buf[TMPBUFSIZ];
  1071. char buf2[TMPBUFSIZ];
  1072. char severity[2];
  1073. #ifdef HAVE_AN_FPGA
  1074. int pga;
  1075. #endif
  1076. #ifdef WANT_CPUMINE
  1077. int cpu;
  1078. #endif
  1079. int i;
  1080. io_reinit(io_data);
  1081. if (isjson)
  1082. io_put(io_data, JSON_START JSON_STATUS);
  1083. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  1084. if (codes[i].code == messageid) {
  1085. switch (codes[i].severity) {
  1086. case SEVERITY_WARN:
  1087. severity[0] = 'W';
  1088. break;
  1089. case SEVERITY_INFO:
  1090. severity[0] = 'I';
  1091. break;
  1092. case SEVERITY_SUCC:
  1093. severity[0] = 'S';
  1094. break;
  1095. case SEVERITY_ERR:
  1096. default:
  1097. severity[0] = 'E';
  1098. break;
  1099. }
  1100. severity[1] = '\0';
  1101. switch(codes[i].params) {
  1102. case PARAM_GPU:
  1103. case PARAM_PGA:
  1104. case PARAM_CPU:
  1105. case PARAM_PID:
  1106. sprintf(buf, codes[i].description, paramid);
  1107. break;
  1108. case PARAM_POOL:
  1109. sprintf(buf, codes[i].description, paramid, pools[paramid]->rpc_url);
  1110. break;
  1111. #ifdef HAVE_OPENCL
  1112. case PARAM_GPUMAX:
  1113. sprintf(buf, codes[i].description, paramid, nDevs - 1);
  1114. break;
  1115. #endif
  1116. #ifdef HAVE_AN_FPGA
  1117. case PARAM_PGAMAX:
  1118. pga = numpgas();
  1119. sprintf(buf, codes[i].description, paramid, pga - 1);
  1120. break;
  1121. #endif
  1122. #ifdef WANT_CPUMINE
  1123. case PARAM_CPUMAX:
  1124. if (opt_n_threads > 0)
  1125. cpu = num_processors;
  1126. else
  1127. cpu = 0;
  1128. sprintf(buf, codes[i].description, paramid, cpu - 1);
  1129. break;
  1130. #endif
  1131. case PARAM_PMAX:
  1132. sprintf(buf, codes[i].description, total_pools);
  1133. break;
  1134. case PARAM_POOLMAX:
  1135. sprintf(buf, codes[i].description, paramid, total_pools - 1);
  1136. break;
  1137. case PARAM_DMAX:
  1138. #ifdef HAVE_AN_FPGA
  1139. pga = numpgas();
  1140. #endif
  1141. #ifdef WANT_CPUMINE
  1142. if (opt_n_threads > 0)
  1143. cpu = num_processors;
  1144. else
  1145. cpu = 0;
  1146. #endif
  1147. sprintf(buf, codes[i].description
  1148. #ifdef HAVE_OPENCL
  1149. , nDevs
  1150. #endif
  1151. #ifdef HAVE_AN_FPGA
  1152. , pga
  1153. #endif
  1154. #ifdef WANT_CPUMINE
  1155. , cpu
  1156. #endif
  1157. );
  1158. break;
  1159. case PARAM_CMD:
  1160. sprintf(buf, codes[i].description, JSON_COMMAND);
  1161. break;
  1162. case PARAM_STR:
  1163. sprintf(buf, codes[i].description, param2);
  1164. break;
  1165. case PARAM_BOTH:
  1166. sprintf(buf, codes[i].description, paramid, param2);
  1167. break;
  1168. case PARAM_BOOL:
  1169. sprintf(buf, codes[i].description, paramid ? TRUESTR : FALSESTR);
  1170. break;
  1171. case PARAM_SET:
  1172. sprintf(buf, codes[i].description, param2, paramid);
  1173. break;
  1174. case PARAM_NONE:
  1175. default:
  1176. strcpy(buf, codes[i].description);
  1177. }
  1178. root = api_add_string(root, _STATUS, severity, false);
  1179. root = api_add_time(root, "When", &when, false);
  1180. root = api_add_int(root, "Code", &messageid, false);
  1181. root = api_add_escape(root, "Msg", buf, false);
  1182. root = api_add_escape(root, "Description", opt_api_description, false);
  1183. root = print_data(root, buf2, isjson, false);
  1184. io_add(io_data, buf2);
  1185. if (isjson)
  1186. io_add(io_data, JSON_CLOSE);
  1187. return;
  1188. }
  1189. }
  1190. root = api_add_string(root, _STATUS, "F", false);
  1191. root = api_add_time(root, "When", &when, false);
  1192. int id = -1;
  1193. root = api_add_int(root, "Code", &id, false);
  1194. sprintf(buf, "%d", messageid);
  1195. root = api_add_escape(root, "Msg", buf, false);
  1196. root = api_add_escape(root, "Description", opt_api_description, false);
  1197. root = print_data(root, buf2, isjson, false);
  1198. io_add(io_data, buf2);
  1199. if (isjson)
  1200. io_add(io_data, JSON_CLOSE);
  1201. }
  1202. static void apiversion(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1203. {
  1204. struct api_data *root = NULL;
  1205. char buf[TMPBUFSIZ];
  1206. bool io_open;
  1207. message(io_data, MSG_VERSION, 0, NULL, isjson);
  1208. io_open = io_add(io_data, isjson ? COMSTR JSON_VERSION : _VERSION COMSTR);
  1209. root = api_add_string(root, "CGMiner", VERSION, false);
  1210. root = api_add_const(root, "API", APIVERSION, false);
  1211. root = print_data(root, buf, isjson, false);
  1212. io_add(io_data, buf);
  1213. if (isjson && io_open)
  1214. io_close(io_data);
  1215. }
  1216. static void minerconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1217. {
  1218. struct api_data *root = NULL;
  1219. char buf[TMPBUFSIZ];
  1220. bool io_open;
  1221. int gpucount = 0;
  1222. int pgacount = 0;
  1223. int cpucount = 0;
  1224. char *adlinuse = (char *)NO;
  1225. #ifdef HAVE_ADL
  1226. const char *adl = YES;
  1227. int i;
  1228. for (i = 0; i < nDevs; i++) {
  1229. if (gpus[i].has_adl) {
  1230. adlinuse = (char *)YES;
  1231. break;
  1232. }
  1233. }
  1234. #else
  1235. const char *adl = NO;
  1236. #endif
  1237. #ifdef HAVE_OPENCL
  1238. gpucount = nDevs;
  1239. #endif
  1240. #ifdef HAVE_AN_FPGA
  1241. pgacount = numpgas();
  1242. #endif
  1243. #ifdef WANT_CPUMINE
  1244. cpucount = opt_n_threads > 0 ? num_processors : 0;
  1245. #endif
  1246. message(io_data, MSG_MINECONFIG, 0, NULL, isjson);
  1247. io_open = io_add(io_data, isjson ? COMSTR JSON_MINECONFIG : _MINECONFIG COMSTR);
  1248. root = api_add_int(root, "GPU Count", &gpucount, false);
  1249. root = api_add_int(root, "PGA Count", &pgacount, false);
  1250. root = api_add_int(root, "CPU Count", &cpucount, false);
  1251. root = api_add_int(root, "Pool Count", &total_pools, false);
  1252. root = api_add_const(root, "ADL", (char *)adl, false);
  1253. root = api_add_string(root, "ADL in use", adlinuse, false);
  1254. root = api_add_const(root, "Strategy", strategies[pool_strategy].s, false);
  1255. root = api_add_int(root, "Log Interval", &opt_log_interval, false);
  1256. root = api_add_const(root, "Device Code", DEVICECODE, false);
  1257. root = api_add_const(root, "OS", OSINFO, false);
  1258. root = api_add_bool(root, "Failover-Only", &opt_fail_only, false);
  1259. root = api_add_int(root, "ScanTime", &opt_scantime, false);
  1260. root = api_add_int(root, "Queue", &opt_queue, false);
  1261. root = api_add_int(root, "Expiry", &opt_expiry, false);
  1262. root = print_data(root, buf, isjson, false);
  1263. io_add(io_data, buf);
  1264. if (isjson && io_open)
  1265. io_close(io_data);
  1266. }
  1267. #if defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA)
  1268. static const char *status2str(enum alive status)
  1269. {
  1270. switch (status) {
  1271. case LIFE_WELL:
  1272. return ALIVE;
  1273. case LIFE_SICK:
  1274. return SICK;
  1275. case LIFE_DEAD:
  1276. return DEAD;
  1277. case LIFE_NOSTART:
  1278. return NOSTART;
  1279. case LIFE_INIT:
  1280. return INIT;
  1281. default:
  1282. return UNKNOWN;
  1283. }
  1284. }
  1285. #endif
  1286. #ifdef HAVE_OPENCL
  1287. static void gpustatus(struct io_data *io_data, int gpu, bool isjson, bool precom)
  1288. {
  1289. struct api_data *root = NULL;
  1290. char intensity[20];
  1291. char buf[TMPBUFSIZ];
  1292. char *enabled;
  1293. char *status;
  1294. float gt, gv;
  1295. int ga, gf, gp, gc, gm, pt;
  1296. if (gpu >= 0 && gpu < nDevs) {
  1297. struct cgpu_info *cgpu = &gpus[gpu];
  1298. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  1299. #ifdef HAVE_ADL
  1300. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  1301. #endif
  1302. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  1303. if (cgpu->deven != DEV_DISABLED)
  1304. enabled = (char *)YES;
  1305. else
  1306. enabled = (char *)NO;
  1307. status = (char *)status2str(cgpu->status);
  1308. if (cgpu->dynamic)
  1309. strcpy(intensity, DYNAMIC);
  1310. else
  1311. sprintf(intensity, "%d", cgpu->intensity);
  1312. root = api_add_int(root, "GPU", &gpu, false);
  1313. root = api_add_string(root, "Enabled", enabled, false);
  1314. root = api_add_string(root, "Status", status, false);
  1315. root = api_add_temp(root, "Temperature", &gt, false);
  1316. root = api_add_int(root, "Fan Speed", &gf, false);
  1317. root = api_add_int(root, "Fan Percent", &gp, false);
  1318. root = api_add_int(root, "GPU Clock", &gc, false);
  1319. root = api_add_int(root, "Memory Clock", &gm, false);
  1320. root = api_add_volts(root, "GPU Voltage", &gv, false);
  1321. root = api_add_int(root, "GPU Activity", &ga, false);
  1322. root = api_add_int(root, "Powertune", &pt, false);
  1323. double mhs = cgpu->total_mhashes / total_secs;
  1324. root = api_add_mhs(root, "MHS av", &mhs, false);
  1325. char mhsname[27];
  1326. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1327. root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
  1328. root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
  1329. root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
  1330. root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
  1331. root = api_add_utility(root, "Utility", &(cgpu->utility), false);
  1332. root = api_add_string(root, "Intensity", intensity, false);
  1333. int last_share_pool = cgpu->last_share_pool_time > 0 ?
  1334. cgpu->last_share_pool : -1;
  1335. root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
  1336. root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
  1337. root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
  1338. root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
  1339. root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
  1340. root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
  1341. root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
  1342. root = print_data(root, buf, isjson, precom);
  1343. io_add(io_data, buf);
  1344. }
  1345. }
  1346. #endif
  1347. #ifdef HAVE_AN_FPGA
  1348. static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom)
  1349. {
  1350. struct api_data *root = NULL;
  1351. char buf[TMPBUFSIZ];
  1352. char *enabled;
  1353. char *status;
  1354. int numpga = numpgas();
  1355. if (numpga > 0 && pga >= 0 && pga < numpga) {
  1356. int dev = pgadevice(pga);
  1357. if (dev < 0) // Should never happen
  1358. return;
  1359. struct cgpu_info *cgpu = get_devices(dev);
  1360. double frequency = 0;
  1361. float temp = cgpu->temp;
  1362. #ifdef USE_ZTEX
  1363. if (cgpu->drv->drv_id == DRIVER_ZTEX && cgpu->device_ztex)
  1364. frequency = cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1);
  1365. #endif
  1366. #ifdef USE_MODMINER
  1367. if (cgpu->drv->drv_id == DRIVER_MODMINER)
  1368. frequency = cgpu->clock;
  1369. #endif
  1370. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  1371. if (cgpu->deven != DEV_DISABLED)
  1372. enabled = (char *)YES;
  1373. else
  1374. enabled = (char *)NO;
  1375. status = (char *)status2str(cgpu->status);
  1376. root = api_add_int(root, "PGA", &pga, false);
  1377. root = api_add_string(root, "Name", cgpu->drv->name, false);
  1378. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  1379. root = api_add_string(root, "Enabled", enabled, false);
  1380. root = api_add_string(root, "Status", status, false);
  1381. root = api_add_temp(root, "Temperature", &temp, false);
  1382. double mhs = cgpu->total_mhashes / total_secs;
  1383. root = api_add_mhs(root, "MHS av", &mhs, false);
  1384. char mhsname[27];
  1385. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1386. root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
  1387. root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
  1388. root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
  1389. root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
  1390. root = api_add_utility(root, "Utility", &(cgpu->utility), false);
  1391. int last_share_pool = cgpu->last_share_pool_time > 0 ?
  1392. cgpu->last_share_pool : -1;
  1393. root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
  1394. root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
  1395. root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
  1396. root = api_add_freq(root, "Frequency", &frequency, false);
  1397. root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
  1398. root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
  1399. root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
  1400. root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
  1401. #if defined(USE_MODMINER) || defined(USE_BITFORCE)
  1402. root = api_add_bool(root, "No Device", &(cgpu->usbinfo.nodev), false);
  1403. #endif
  1404. root = print_data(root, buf, isjson, precom);
  1405. io_add(io_data, buf);
  1406. }
  1407. }
  1408. #endif
  1409. #ifdef WANT_CPUMINE
  1410. static void cpustatus(struct io_data *io_data, int cpu, bool isjson, bool precom)
  1411. {
  1412. struct api_data *root = NULL;
  1413. char buf[TMPBUFSIZ];
  1414. if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
  1415. struct cgpu_info *cgpu = &cpus[cpu];
  1416. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  1417. root = api_add_int(root, "CPU", &cpu, false);
  1418. double mhs = cgpu->total_mhashes / total_secs;
  1419. root = api_add_mhs(root, "MHS av", &mhs, false);
  1420. char mhsname[27];
  1421. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1422. root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
  1423. root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
  1424. root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
  1425. root = api_add_utility(root, "Utility", &(cgpu->utility), false);
  1426. int last_share_pool = cgpu->last_share_pool_time > 0 ?
  1427. cgpu->last_share_pool : -1;
  1428. root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
  1429. root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
  1430. root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
  1431. root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
  1432. root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
  1433. root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
  1434. root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
  1435. root = print_data(root, buf, isjson, precom);
  1436. io_add(io_data, buf);
  1437. }
  1438. }
  1439. #endif
  1440. static void devstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1441. {
  1442. bool io_open = false;
  1443. int devcount = 0;
  1444. int numgpu = 0;
  1445. int numpga = 0;
  1446. int i;
  1447. #ifdef HAVE_OPENCL
  1448. numgpu = nDevs;
  1449. #endif
  1450. #ifdef HAVE_AN_FPGA
  1451. numpga = numpgas();
  1452. #endif
  1453. if (numgpu == 0 && opt_n_threads == 0 && numpga == 0) {
  1454. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  1455. return;
  1456. }
  1457. message(io_data, MSG_DEVS, 0, NULL, isjson);
  1458. if (isjson)
  1459. io_open = io_add(io_data, COMSTR JSON_DEVS);
  1460. #ifdef HAVE_OPENCL
  1461. for (i = 0; i < nDevs; i++) {
  1462. gpustatus(io_data, i, isjson, isjson && devcount > 0);
  1463. devcount++;
  1464. }
  1465. #endif
  1466. #ifdef HAVE_AN_FPGA
  1467. if (numpga > 0) {
  1468. for (i = 0; i < numpga; i++) {
  1469. pgastatus(io_data, i, isjson, isjson && devcount > 0);
  1470. devcount++;
  1471. }
  1472. }
  1473. #endif
  1474. #ifdef WANT_CPUMINE
  1475. if (opt_n_threads > 0) {
  1476. for (i = 0; i < num_processors; i++) {
  1477. cpustatus(io_data, i, isjson, isjson && devcount > 0);
  1478. devcount++;
  1479. }
  1480. }
  1481. #endif
  1482. if (isjson && io_open)
  1483. io_close(io_data);
  1484. }
  1485. #ifdef HAVE_OPENCL
  1486. static void gpudev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1487. {
  1488. bool io_open = false;
  1489. int id;
  1490. if (nDevs == 0) {
  1491. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1492. return;
  1493. }
  1494. if (param == NULL || *param == '\0') {
  1495. message(io_data, MSG_MISID, 0, NULL, isjson);
  1496. return;
  1497. }
  1498. id = atoi(param);
  1499. if (id < 0 || id >= nDevs) {
  1500. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1501. return;
  1502. }
  1503. message(io_data, MSG_GPUDEV, id, NULL, isjson);
  1504. if (isjson)
  1505. io_open = io_add(io_data, COMSTR JSON_GPU);
  1506. gpustatus(io_data, id, isjson, false);
  1507. if (isjson && io_open)
  1508. io_close(io_data);
  1509. }
  1510. #endif
  1511. #ifdef HAVE_AN_FPGA
  1512. static void pgadev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1513. {
  1514. bool io_open = false;
  1515. int numpga = numpgas();
  1516. int id;
  1517. if (numpga == 0) {
  1518. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1519. return;
  1520. }
  1521. if (param == NULL || *param == '\0') {
  1522. message(io_data, MSG_MISID, 0, NULL, isjson);
  1523. return;
  1524. }
  1525. id = atoi(param);
  1526. if (id < 0 || id >= numpga) {
  1527. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1528. return;
  1529. }
  1530. message(io_data, MSG_PGADEV, id, NULL, isjson);
  1531. if (isjson)
  1532. io_open = io_add(io_data, COMSTR JSON_PGA);
  1533. pgastatus(io_data, id, isjson, false);
  1534. if (isjson && io_open)
  1535. io_close(io_data);
  1536. }
  1537. static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1538. {
  1539. struct cgpu_info *cgpu;
  1540. int numpga = numpgas();
  1541. struct thr_info *thr;
  1542. int pga;
  1543. int id;
  1544. int i;
  1545. if (numpga == 0) {
  1546. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1547. return;
  1548. }
  1549. if (param == NULL || *param == '\0') {
  1550. message(io_data, MSG_MISID, 0, NULL, isjson);
  1551. return;
  1552. }
  1553. id = atoi(param);
  1554. if (id < 0 || id >= numpga) {
  1555. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1556. return;
  1557. }
  1558. int dev = pgadevice(id);
  1559. if (dev < 0) { // Should never happen
  1560. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1561. return;
  1562. }
  1563. cgpu = get_devices(dev);
  1564. applog(LOG_DEBUG, "API: request to pgaenable pgaid %d device %d %s%u",
  1565. id, dev, cgpu->drv->name, cgpu->device_id);
  1566. if (cgpu->deven != DEV_DISABLED) {
  1567. message(io_data, MSG_PGALRENA, id, NULL, isjson);
  1568. return;
  1569. }
  1570. #if 0 /* A DISABLED device wont change status FIXME: should disabling make it WELL? */
  1571. if (cgpu->status != LIFE_WELL) {
  1572. message(io_data, MSG_PGAUNW, id, NULL, isjson);
  1573. return;
  1574. }
  1575. #endif
  1576. #if defined(USE_MODMINER) || defined(USE_BITFORCE)
  1577. if (cgpu->usbinfo.nodev) {
  1578. message(io_data, MSG_USBNODEV, id, NULL, isjson);
  1579. return;
  1580. }
  1581. #endif
  1582. for (i = 0; i < mining_threads; i++) {
  1583. thr = get_thread(i);
  1584. pga = thr->cgpu->cgminer_id;
  1585. if (pga == dev) {
  1586. cgpu->deven = DEV_ENABLED;
  1587. applog(LOG_DEBUG, "API: pushing ping (%d) to thread %d", ping, thr->id);
  1588. tq_push(thr->q, &ping);
  1589. }
  1590. }
  1591. message(io_data, MSG_PGAENA, id, NULL, isjson);
  1592. }
  1593. static void pgadisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1594. {
  1595. struct cgpu_info *cgpu;
  1596. int numpga = numpgas();
  1597. int id;
  1598. if (numpga == 0) {
  1599. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1600. return;
  1601. }
  1602. if (param == NULL || *param == '\0') {
  1603. message(io_data, MSG_MISID, 0, NULL, isjson);
  1604. return;
  1605. }
  1606. id = atoi(param);
  1607. if (id < 0 || id >= numpga) {
  1608. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1609. return;
  1610. }
  1611. int dev = pgadevice(id);
  1612. if (dev < 0) { // Should never happen
  1613. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1614. return;
  1615. }
  1616. cgpu = get_devices(dev);
  1617. applog(LOG_DEBUG, "API: request to pgadisable pgaid %d device %d %s%u",
  1618. id, dev, cgpu->drv->name, cgpu->device_id);
  1619. if (cgpu->deven == DEV_DISABLED) {
  1620. message(io_data, MSG_PGALRDIS, id, NULL, isjson);
  1621. return;
  1622. }
  1623. cgpu->deven = DEV_DISABLED;
  1624. message(io_data, MSG_PGADIS, id, NULL, isjson);
  1625. }
  1626. static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1627. {
  1628. struct cgpu_info *cgpu;
  1629. struct device_drv *drv;
  1630. int numpga = numpgas();
  1631. int id;
  1632. if (numpga == 0) {
  1633. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1634. return;
  1635. }
  1636. if (param == NULL || *param == '\0') {
  1637. message(io_data, MSG_MISID, 0, NULL, isjson);
  1638. return;
  1639. }
  1640. id = atoi(param);
  1641. if (id < 0 || id >= numpga) {
  1642. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1643. return;
  1644. }
  1645. int dev = pgadevice(id);
  1646. if (dev < 0) { // Should never happen
  1647. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1648. return;
  1649. }
  1650. cgpu = get_devices(dev);
  1651. drv = cgpu->drv;
  1652. if (!drv->identify_device)
  1653. message(io_data, MSG_PGANOID, id, NULL, isjson);
  1654. else {
  1655. drv->identify_device(cgpu);
  1656. message(io_data, MSG_PGAIDENT, id, NULL, isjson);
  1657. }
  1658. }
  1659. #endif
  1660. #ifdef WANT_CPUMINE
  1661. static void cpudev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1662. {
  1663. bool io_open = false;
  1664. int id;
  1665. if (opt_n_threads == 0) {
  1666. message(io_data, MSG_CPUNON, 0, NULL, isjson);
  1667. return;
  1668. }
  1669. if (param == NULL || *param == '\0') {
  1670. message(io_data, MSG_MISID, 0, NULL, isjson);
  1671. return;
  1672. }
  1673. id = atoi(param);
  1674. if (id < 0 || id >= num_processors) {
  1675. message(io_data, MSG_INVCPU, id, NULL, isjson);
  1676. return;
  1677. }
  1678. message(io_data, MSG_CPUDEV, id, NULL, isjson);
  1679. if (isjson)
  1680. io_open = io_add(io_data, COMSTR JSON_CPU);
  1681. cpustatus(io_data, id, isjson, false);
  1682. if (isjson && io_open)
  1683. io_close(io_data);
  1684. }
  1685. #endif
  1686. static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1687. {
  1688. struct api_data *root = NULL;
  1689. char buf[TMPBUFSIZ];
  1690. bool io_open = false;
  1691. char *status, *lp;
  1692. int i;
  1693. if (total_pools == 0) {
  1694. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1695. return;
  1696. }
  1697. message(io_data, MSG_POOL, 0, NULL, isjson);
  1698. if (isjson)
  1699. io_open = io_add(io_data, COMSTR JSON_POOLS);
  1700. for (i = 0; i < total_pools; i++) {
  1701. struct pool *pool = pools[i];
  1702. if (pool->removed)
  1703. continue;
  1704. switch (pool->enabled) {
  1705. case POOL_DISABLED:
  1706. status = (char *)DISABLED;
  1707. break;
  1708. case POOL_REJECTING:
  1709. status = (char *)REJECTING;
  1710. break;
  1711. case POOL_ENABLED:
  1712. if (pool->idle)
  1713. status = (char *)DEAD;
  1714. else
  1715. status = (char *)ALIVE;
  1716. break;
  1717. default:
  1718. status = (char *)UNKNOWN;
  1719. break;
  1720. }
  1721. if (pool->hdr_path)
  1722. lp = (char *)YES;
  1723. else
  1724. lp = (char *)NO;
  1725. root = api_add_int(root, "POOL", &i, false);
  1726. root = api_add_escape(root, "URL", pool->rpc_url, false);
  1727. root = api_add_string(root, "Status", status, false);
  1728. root = api_add_int(root, "Priority", &(pool->prio), false);
  1729. root = api_add_string(root, "Long Poll", lp, false);
  1730. root = api_add_uint(root, "Getworks", &(pool->getwork_requested), false);
  1731. root = api_add_int(root, "Accepted", &(pool->accepted), false);
  1732. root = api_add_int(root, "Rejected", &(pool->rejected), false);
  1733. root = api_add_uint(root, "Discarded", &(pool->discarded_work), false);
  1734. root = api_add_uint(root, "Stale", &(pool->stale_shares), false);
  1735. root = api_add_uint(root, "Get Failures", &(pool->getfail_occasions), false);
  1736. root = api_add_uint(root, "Remote Failures", &(pool->remotefail_occasions), false);
  1737. root = api_add_escape(root, "User", pool->rpc_user, false);
  1738. root = api_add_time(root, "Last Share Time", &(pool->last_share_time), false);
  1739. root = api_add_int(root, "Diff1 Shares", &(pool->diff1), false);
  1740. if (pool->rpc_proxy) {
  1741. root = api_add_const(root, "Proxy Type", proxytype(pool->rpc_proxytype), false);
  1742. root = api_add_escape(root, "Proxy", pool->rpc_proxy, false);
  1743. } else {
  1744. root = api_add_const(root, "Proxy Type", BLANK, false);
  1745. root = api_add_const(root, "Proxy", BLANK, false);
  1746. }
  1747. root = api_add_diff(root, "Difficulty Accepted", &(pool->diff_accepted), false);
  1748. root = api_add_diff(root, "Difficulty Rejected", &(pool->diff_rejected), false);
  1749. root = api_add_diff(root, "Difficulty Stale", &(pool->diff_stale), false);
  1750. root = api_add_diff(root, "Last Share Difficulty", &(pool->last_share_diff), false);
  1751. root = api_add_bool(root, "Has Stratum", &(pool->has_stratum), false);
  1752. root = api_add_bool(root, "Stratum Active", &(pool->stratum_active), false);
  1753. if (pool->stratum_active)
  1754. root = api_add_escape(root, "Stratum URL", pool->stratum_url, false);
  1755. else
  1756. root = api_add_const(root, "Stratum URL", BLANK, false);
  1757. root = api_add_bool(root, "Has GBT", &(pool->has_gbt), false);
  1758. root = api_add_uint64(root, "Best Share", &(pool->best_diff), true);
  1759. root = print_data(root, buf, isjson, isjson && (i > 0));
  1760. io_add(io_data, buf);
  1761. }
  1762. if (isjson && io_open)
  1763. io_close(io_data);
  1764. }
  1765. static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1766. {
  1767. struct api_data *root = NULL;
  1768. char buf[TMPBUFSIZ];
  1769. bool io_open;
  1770. double utility, mhs, work_utility;
  1771. #ifdef WANT_CPUMINE
  1772. char *algo = (char *)(algo_names[opt_algo]);
  1773. if (algo == NULL)
  1774. algo = (char *)NULLSTR;
  1775. #endif
  1776. message(io_data, MSG_SUMM, 0, NULL, isjson);
  1777. io_open = io_add(io_data, isjson ? COMSTR JSON_SUMMARY : _SUMMARY COMSTR);
  1778. // stop hashmeter() changing some while copying
  1779. mutex_lock(&hash_lock);
  1780. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1781. mhs = total_mhashes_done / total_secs;
  1782. work_utility = total_diff1 / ( total_secs ? total_secs : 1 ) * 60;
  1783. root = api_add_elapsed(root, "Elapsed", &(total_secs), true);
  1784. #ifdef WANT_CPUMINE
  1785. root = api_add_string(root, "Algorithm", algo, false);
  1786. #endif
  1787. root = api_add_mhs(root, "MHS av", &(mhs), false);
  1788. root = api_add_uint(root, "Found Blocks", &(found_blocks), true);
  1789. root = api_add_int(root, "Getworks", &(total_getworks), true);
  1790. root = api_add_int(root, "Accepted", &(total_accepted), true);
  1791. root = api_add_int(root, "Rejected", &(total_rejected), true);
  1792. root = api_add_int(root, "Hardware Errors", &(hw_errors), true);
  1793. root = api_add_utility(root, "Utility", &(utility), false);
  1794. root = api_add_int(root, "Discarded", &(total_discarded), true);
  1795. root = api_add_int(root, "Stale", &(total_stale), true);
  1796. root = api_add_uint(root, "Get Failures", &(total_go), true);
  1797. root = api_add_uint(root, "Local Work", &(local_work), true);
  1798. root = api_add_uint(root, "Remote Failures", &(total_ro), true);
  1799. root = api_add_uint(root, "Network Blocks", &(new_blocks), true);
  1800. root = api_add_mhtotal(root, "Total MH", &(total_mhashes_done), true);
  1801. root = api_add_utility(root, "Work Utility", &(work_utility), false);
  1802. root = api_add_diff(root, "Difficulty Accepted", &(total_diff_accepted), true);
  1803. root = api_add_diff(root, "Difficulty Rejected", &(total_diff_rejected), true);
  1804. root = api_add_diff(root, "Difficulty Stale", &(total_diff_stale), true);
  1805. root = api_add_uint64(root, "Best Share", &(best_diff), true);
  1806. mutex_unlock(&hash_lock);
  1807. root = print_data(root, buf, isjson, false);
  1808. io_add(io_data, buf);
  1809. if (isjson && io_open)
  1810. io_close(io_data);
  1811. }
  1812. #ifdef HAVE_OPENCL
  1813. static void gpuenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1814. {
  1815. struct thr_info *thr;
  1816. int gpu;
  1817. int id;
  1818. int i;
  1819. if (gpu_threads == 0) {
  1820. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1821. return;
  1822. }
  1823. if (param == NULL || *param == '\0') {
  1824. message(io_data, MSG_MISID, 0, NULL, isjson);
  1825. return;
  1826. }
  1827. id = atoi(param);
  1828. if (id < 0 || id >= nDevs) {
  1829. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1830. return;
  1831. }
  1832. applog(LOG_DEBUG, "API: request to gpuenable gpuid %d %s%u",
  1833. id, gpus[id].drv->name, gpus[id].device_id);
  1834. if (gpus[id].deven != DEV_DISABLED) {
  1835. message(io_data, MSG_ALRENA, id, NULL, isjson);
  1836. return;
  1837. }
  1838. for (i = 0; i < gpu_threads; i++) {
  1839. thr = get_thread(i);
  1840. gpu = thr->cgpu->device_id;
  1841. if (gpu == id) {
  1842. if (thr->cgpu->status != LIFE_WELL) {
  1843. message(io_data, MSG_GPUMRE, id, NULL, isjson);
  1844. return;
  1845. }
  1846. gpus[id].deven = DEV_ENABLED;
  1847. applog(LOG_DEBUG, "API: pushing ping (%d) to thread %d", ping, thr->id);
  1848. tq_push(thr->q, &ping);
  1849. }
  1850. }
  1851. message(io_data, MSG_GPUREN, id, NULL, isjson);
  1852. }
  1853. static void gpudisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1854. {
  1855. int id;
  1856. if (nDevs == 0) {
  1857. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1858. return;
  1859. }
  1860. if (param == NULL || *param == '\0') {
  1861. message(io_data, MSG_MISID, 0, NULL, isjson);
  1862. return;
  1863. }
  1864. id = atoi(param);
  1865. if (id < 0 || id >= nDevs) {
  1866. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1867. return;
  1868. }
  1869. applog(LOG_DEBUG, "API: request to gpudisable gpuid %d %s%u",
  1870. id, gpus[id].drv->name, gpus[id].device_id);
  1871. if (gpus[id].deven == DEV_DISABLED) {
  1872. message(io_data, MSG_ALRDIS, id, NULL, isjson);
  1873. return;
  1874. }
  1875. gpus[id].deven = DEV_DISABLED;
  1876. message(io_data, MSG_GPUDIS, id, NULL, isjson);
  1877. }
  1878. static void gpurestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1879. {
  1880. int id;
  1881. if (nDevs == 0) {
  1882. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1883. return;
  1884. }
  1885. if (param == NULL || *param == '\0') {
  1886. message(io_data, MSG_MISID, 0, NULL, isjson);
  1887. return;
  1888. }
  1889. id = atoi(param);
  1890. if (id < 0 || id >= nDevs) {
  1891. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1892. return;
  1893. }
  1894. reinit_device(&gpus[id]);
  1895. message(io_data, MSG_GPUREI, id, NULL, isjson);
  1896. }
  1897. #endif
  1898. static void gpucount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1899. {
  1900. struct api_data *root = NULL;
  1901. char buf[TMPBUFSIZ];
  1902. bool io_open;
  1903. int numgpu = 0;
  1904. #ifdef HAVE_OPENCL
  1905. numgpu = nDevs;
  1906. #endif
  1907. message(io_data, MSG_NUMGPU, 0, NULL, isjson);
  1908. io_open = io_add(io_data, isjson ? COMSTR JSON_GPUS : _GPUS COMSTR);
  1909. root = api_add_int(root, "Count", &numgpu, false);
  1910. root = print_data(root, buf, isjson, false);
  1911. io_add(io_data, buf);
  1912. if (isjson && io_open)
  1913. io_close(io_data);
  1914. }
  1915. static void pgacount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1916. {
  1917. struct api_data *root = NULL;
  1918. char buf[TMPBUFSIZ];
  1919. bool io_open;
  1920. int count = 0;
  1921. #ifdef HAVE_AN_FPGA
  1922. count = numpgas();
  1923. #endif
  1924. message(io_data, MSG_NUMPGA, 0, NULL, isjson);
  1925. io_open = io_add(io_data, isjson ? COMSTR JSON_PGAS : _PGAS COMSTR);
  1926. root = api_add_int(root, "Count", &count, false);
  1927. root = print_data(root, buf, isjson, false);
  1928. io_add(io_data, buf);
  1929. if (isjson && io_open)
  1930. io_close(io_data);
  1931. }
  1932. static void cpucount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1933. {
  1934. struct api_data *root = NULL;
  1935. char buf[TMPBUFSIZ];
  1936. bool io_open;
  1937. int count = 0;
  1938. #ifdef WANT_CPUMINE
  1939. count = opt_n_threads > 0 ? num_processors : 0;
  1940. #endif
  1941. message(io_data, MSG_NUMCPU, 0, NULL, isjson);
  1942. io_open = io_add(io_data, isjson ? COMSTR JSON_CPUS : _CPUS COMSTR);
  1943. root = api_add_int(root, "Count", &count, false);
  1944. root = print_data(root, buf, isjson, false);
  1945. io_add(io_data, buf);
  1946. if (isjson && io_open)
  1947. io_close(io_data);
  1948. }
  1949. static void switchpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1950. {
  1951. struct pool *pool;
  1952. int id;
  1953. if (total_pools == 0) {
  1954. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1955. return;
  1956. }
  1957. if (param == NULL || *param == '\0') {
  1958. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1959. return;
  1960. }
  1961. id = atoi(param);
  1962. if (id < 0 || id >= total_pools) {
  1963. message(io_data, MSG_INVPID, id, NULL, isjson);
  1964. return;
  1965. }
  1966. pool = pools[id];
  1967. pool->enabled = POOL_ENABLED;
  1968. switch_pools(pool);
  1969. message(io_data, MSG_SWITCHP, id, NULL, isjson);
  1970. }
  1971. static void copyadvanceafter(char ch, char **param, char **buf)
  1972. {
  1973. #define src_p (*param)
  1974. #define dst_b (*buf)
  1975. while (*src_p && *src_p != ch) {
  1976. if (*src_p == '\\' && *(src_p+1) != '\0')
  1977. src_p++;
  1978. *(dst_b++) = *(src_p++);
  1979. }
  1980. if (*src_p)
  1981. src_p++;
  1982. *(dst_b++) = '\0';
  1983. }
  1984. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1985. {
  1986. char *ptr, *buf;
  1987. ptr = buf = malloc(strlen(param)+1);
  1988. if (unlikely(!buf))
  1989. quit(1, "Failed to malloc pooldetails buf");
  1990. *url = buf;
  1991. // copy url
  1992. copyadvanceafter(',', &param, &buf);
  1993. if (!(*param)) // missing user
  1994. goto exitsama;
  1995. *user = buf;
  1996. // copy user
  1997. copyadvanceafter(',', &param, &buf);
  1998. if (!*param) // missing pass
  1999. goto exitsama;
  2000. *pass = buf;
  2001. // copy pass
  2002. copyadvanceafter(',', &param, &buf);
  2003. return true;
  2004. exitsama:
  2005. free(ptr);
  2006. return false;
  2007. }
  2008. static void addpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2009. {
  2010. char *url, *user, *pass;
  2011. struct pool *pool;
  2012. char *ptr;
  2013. if (param == NULL || *param == '\0') {
  2014. message(io_data, MSG_MISPDP, 0, NULL, isjson);
  2015. return;
  2016. }
  2017. if (!pooldetails(param, &url, &user, &pass)) {
  2018. ptr = escape_string(param, isjson);
  2019. message(io_data, MSG_INVPDP, 0, ptr, isjson);
  2020. if (ptr != param)
  2021. free(ptr);
  2022. ptr = NULL;
  2023. return;
  2024. }
  2025. pool = add_pool();
  2026. detect_stratum(pool, url);
  2027. add_pool_details(pool, true, url, user, pass);
  2028. ptr = escape_string(url, isjson);
  2029. message(io_data, MSG_ADDPOOL, 0, ptr, isjson);
  2030. if (ptr != url)
  2031. free(ptr);
  2032. ptr = NULL;
  2033. }
  2034. static void enablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2035. {
  2036. struct pool *pool;
  2037. int id;
  2038. if (total_pools == 0) {
  2039. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2040. return;
  2041. }
  2042. if (param == NULL || *param == '\0') {
  2043. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2044. return;
  2045. }
  2046. id = atoi(param);
  2047. if (id < 0 || id >= total_pools) {
  2048. message(io_data, MSG_INVPID, id, NULL, isjson);
  2049. return;
  2050. }
  2051. pool = pools[id];
  2052. if (pool->enabled == POOL_ENABLED) {
  2053. message(io_data, MSG_ALRENAP, id, NULL, isjson);
  2054. return;
  2055. }
  2056. pool->enabled = POOL_ENABLED;
  2057. if (pool->prio < current_pool()->prio)
  2058. switch_pools(pool);
  2059. message(io_data, MSG_ENAPOOL, id, NULL, isjson);
  2060. }
  2061. static void poolpriority(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2062. {
  2063. char *ptr, *next;
  2064. int i, pr, prio = 0;
  2065. // TODO: all cgminer code needs a mutex added everywhere for change
  2066. // access to total_pools and also parts of the pools[] array,
  2067. // just copying total_pools here wont solve that
  2068. if (total_pools == 0) {
  2069. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2070. return;
  2071. }
  2072. if (param == NULL || *param == '\0') {
  2073. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2074. return;
  2075. }
  2076. bool pools_changed[total_pools];
  2077. int new_prio[total_pools];
  2078. for (i = 0; i < total_pools; ++i)
  2079. pools_changed[i] = false;
  2080. next = param;
  2081. while (next && *next) {
  2082. ptr = next;
  2083. next = strchr(ptr, ',');
  2084. if (next)
  2085. *(next++) = '\0';
  2086. i = atoi(ptr);
  2087. if (i < 0 || i >= total_pools) {
  2088. message(io_data, MSG_INVPID, i, NULL, isjson);
  2089. return;
  2090. }
  2091. if (pools_changed[i]) {
  2092. message(io_data, MSG_DUPPID, i, NULL, isjson);
  2093. return;
  2094. }
  2095. pools_changed[i] = true;
  2096. new_prio[i] = prio++;
  2097. }
  2098. // Only change them if no errors
  2099. for (i = 0; i < total_pools; i++) {
  2100. if (pools_changed[i])
  2101. pools[i]->prio = new_prio[i];
  2102. }
  2103. // In priority order, cycle through the unchanged pools and append them
  2104. for (pr = 0; pr < total_pools; pr++)
  2105. for (i = 0; i < total_pools; i++) {
  2106. if (!pools_changed[i] && pools[i]->prio == pr) {
  2107. pools[i]->prio = prio++;
  2108. pools_changed[i] = true;
  2109. break;
  2110. }
  2111. }
  2112. if (current_pool()->prio)
  2113. switch_pools(NULL);
  2114. message(io_data, MSG_POOLPRIO, 0, NULL, isjson);
  2115. }
  2116. static void disablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2117. {
  2118. struct pool *pool;
  2119. int id;
  2120. if (total_pools == 0) {
  2121. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2122. return;
  2123. }
  2124. if (param == NULL || *param == '\0') {
  2125. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2126. return;
  2127. }
  2128. id = atoi(param);
  2129. if (id < 0 || id >= total_pools) {
  2130. message(io_data, MSG_INVPID, id, NULL, isjson);
  2131. return;
  2132. }
  2133. pool = pools[id];
  2134. if (pool->enabled == POOL_DISABLED) {
  2135. message(io_data, MSG_ALRDISP, id, NULL, isjson);
  2136. return;
  2137. }
  2138. if (enabled_pools <= 1) {
  2139. message(io_data, MSG_DISLASTP, id, NULL, isjson);
  2140. return;
  2141. }
  2142. pool->enabled = POOL_DISABLED;
  2143. if (pool == current_pool())
  2144. switch_pools(NULL);
  2145. message(io_data, MSG_DISPOOL, id, NULL, isjson);
  2146. }
  2147. static void removepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2148. {
  2149. struct pool *pool;
  2150. char *rpc_url;
  2151. bool dofree = false;
  2152. int id;
  2153. if (total_pools == 0) {
  2154. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2155. return;
  2156. }
  2157. if (param == NULL || *param == '\0') {
  2158. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2159. return;
  2160. }
  2161. id = atoi(param);
  2162. if (id < 0 || id >= total_pools) {
  2163. message(io_data, MSG_INVPID, id, NULL, isjson);
  2164. return;
  2165. }
  2166. if (total_pools <= 1) {
  2167. message(io_data, MSG_REMLASTP, id, NULL, isjson);
  2168. return;
  2169. }
  2170. pool = pools[id];
  2171. if (pool == current_pool())
  2172. switch_pools(NULL);
  2173. if (pool == current_pool()) {
  2174. message(io_data, MSG_ACTPOOL, id, NULL, isjson);
  2175. return;
  2176. }
  2177. pool->enabled = POOL_DISABLED;
  2178. rpc_url = escape_string(pool->rpc_url, isjson);
  2179. if (rpc_url != pool->rpc_url)
  2180. dofree = true;
  2181. remove_pool(pool);
  2182. message(io_data, MSG_REMPOOL, id, rpc_url, isjson);
  2183. if (dofree)
  2184. free(rpc_url);
  2185. rpc_url = NULL;
  2186. }
  2187. #ifdef HAVE_OPENCL
  2188. static bool splitgpuvalue(struct io_data *io_data, char *param, int *gpu, char **value, bool isjson)
  2189. {
  2190. int id;
  2191. char *gpusep;
  2192. if (nDevs == 0) {
  2193. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  2194. return false;
  2195. }
  2196. if (param == NULL || *param == '\0') {
  2197. message(io_data, MSG_MISID, 0, NULL, isjson);
  2198. return false;
  2199. }
  2200. gpusep = strchr(param, GPUSEP);
  2201. if (gpusep == NULL) {
  2202. message(io_data, MSG_MISVAL, 0, NULL, isjson);
  2203. return false;
  2204. }
  2205. *(gpusep++) = '\0';
  2206. id = atoi(param);
  2207. if (id < 0 || id >= nDevs) {
  2208. message(io_data, MSG_INVGPU, id, NULL, isjson);
  2209. return false;
  2210. }
  2211. *gpu = id;
  2212. *value = gpusep;
  2213. return true;
  2214. }
  2215. static void gpuintensity(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2216. {
  2217. int id;
  2218. char *value;
  2219. int intensity;
  2220. char intensitystr[7];
  2221. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2222. return;
  2223. if (!strncasecmp(value, DYNAMIC, 1)) {
  2224. gpus[id].dynamic = true;
  2225. strcpy(intensitystr, DYNAMIC);
  2226. }
  2227. else {
  2228. intensity = atoi(value);
  2229. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  2230. message(io_data, MSG_INVINT, 0, value, isjson);
  2231. return;
  2232. }
  2233. gpus[id].dynamic = false;
  2234. gpus[id].intensity = intensity;
  2235. sprintf(intensitystr, "%d", intensity);
  2236. }
  2237. message(io_data, MSG_GPUINT, id, intensitystr, isjson);
  2238. }
  2239. static void gpumem(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2240. {
  2241. #ifdef HAVE_ADL
  2242. int id;
  2243. char *value;
  2244. int clock;
  2245. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2246. return;
  2247. clock = atoi(value);
  2248. if (set_memoryclock(id, clock))
  2249. message(io_data, MSG_GPUMERR, id, value, isjson);
  2250. else
  2251. message(io_data, MSG_GPUMEM, id, value, isjson);
  2252. #else
  2253. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2254. #endif
  2255. }
  2256. static void gpuengine(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2257. {
  2258. #ifdef HAVE_ADL
  2259. int id;
  2260. char *value;
  2261. int clock;
  2262. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2263. return;
  2264. clock = atoi(value);
  2265. if (set_engineclock(id, clock))
  2266. message(io_data, MSG_GPUEERR, id, value, isjson);
  2267. else
  2268. message(io_data, MSG_GPUENG, id, value, isjson);
  2269. #else
  2270. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2271. #endif
  2272. }
  2273. static void gpufan(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2274. {
  2275. #ifdef HAVE_ADL
  2276. int id;
  2277. char *value;
  2278. int fan;
  2279. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2280. return;
  2281. fan = atoi(value);
  2282. if (set_fanspeed(id, fan))
  2283. message(io_data, MSG_GPUFERR, id, value, isjson);
  2284. else
  2285. message(io_data, MSG_GPUFAN, id, value, isjson);
  2286. #else
  2287. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2288. #endif
  2289. }
  2290. static void gpuvddc(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2291. {
  2292. #ifdef HAVE_ADL
  2293. int id;
  2294. char *value;
  2295. float vddc;
  2296. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2297. return;
  2298. vddc = atof(value);
  2299. if (set_vddc(id, vddc))
  2300. message(io_data, MSG_GPUVERR, id, value, isjson);
  2301. else
  2302. message(io_data, MSG_GPUVDDC, id, value, isjson);
  2303. #else
  2304. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2305. #endif
  2306. }
  2307. #endif
  2308. void doquit(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2309. {
  2310. if (isjson)
  2311. io_put(io_data, JSON_START JSON_BYE);
  2312. else
  2313. io_put(io_data, _BYE);
  2314. bye = true;
  2315. do_a_quit = true;
  2316. }
  2317. void dorestart(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_RESTART);
  2321. else
  2322. io_put(io_data, _RESTART);
  2323. bye = true;
  2324. do_a_restart = true;
  2325. }
  2326. void privileged(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2327. {
  2328. message(io_data, MSG_ACCOK, 0, NULL, isjson);
  2329. }
  2330. void notifystatus(struct io_data *io_data, int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
  2331. {
  2332. struct api_data *root = NULL;
  2333. char buf[TMPBUFSIZ];
  2334. char *reason;
  2335. if (cgpu->device_last_not_well == 0)
  2336. reason = REASON_NONE;
  2337. else
  2338. switch(cgpu->device_not_well_reason) {
  2339. case REASON_THREAD_FAIL_INIT:
  2340. reason = REASON_THREAD_FAIL_INIT_STR;
  2341. break;
  2342. case REASON_THREAD_ZERO_HASH:
  2343. reason = REASON_THREAD_ZERO_HASH_STR;
  2344. break;
  2345. case REASON_THREAD_FAIL_QUEUE:
  2346. reason = REASON_THREAD_FAIL_QUEUE_STR;
  2347. break;
  2348. case REASON_DEV_SICK_IDLE_60:
  2349. reason = REASON_DEV_SICK_IDLE_60_STR;
  2350. break;
  2351. case REASON_DEV_DEAD_IDLE_600:
  2352. reason = REASON_DEV_DEAD_IDLE_600_STR;
  2353. break;
  2354. case REASON_DEV_NOSTART:
  2355. reason = REASON_DEV_NOSTART_STR;
  2356. break;
  2357. case REASON_DEV_OVER_HEAT:
  2358. reason = REASON_DEV_OVER_HEAT_STR;
  2359. break;
  2360. case REASON_DEV_THERMAL_CUTOFF:
  2361. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  2362. break;
  2363. case REASON_DEV_COMMS_ERROR:
  2364. reason = REASON_DEV_COMMS_ERROR_STR;
  2365. break;
  2366. default:
  2367. reason = REASON_UNKNOWN_STR;
  2368. break;
  2369. }
  2370. // ALL counters (and only counters) must start the name with a '*'
  2371. // Simplifies future external support for identifying new counters
  2372. root = api_add_int(root, "NOTIFY", &device, false);
  2373. root = api_add_string(root, "Name", cgpu->drv->name, false);
  2374. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  2375. root = api_add_time(root, "Last Well", &(cgpu->device_last_well), false);
  2376. root = api_add_time(root, "Last Not Well", &(cgpu->device_last_not_well), false);
  2377. root = api_add_string(root, "Reason Not Well", reason, false);
  2378. root = api_add_int(root, "*Thread Fail Init", &(cgpu->thread_fail_init_count), false);
  2379. root = api_add_int(root, "*Thread Zero Hash", &(cgpu->thread_zero_hash_count), false);
  2380. root = api_add_int(root, "*Thread Fail Queue", &(cgpu->thread_fail_queue_count), false);
  2381. root = api_add_int(root, "*Dev Sick Idle 60s", &(cgpu->dev_sick_idle_60_count), false);
  2382. root = api_add_int(root, "*Dev Dead Idle 600s", &(cgpu->dev_dead_idle_600_count), false);
  2383. root = api_add_int(root, "*Dev Nostart", &(cgpu->dev_nostart_count), false);
  2384. root = api_add_int(root, "*Dev Over Heat", &(cgpu->dev_over_heat_count), false);
  2385. root = api_add_int(root, "*Dev Thermal Cutoff", &(cgpu->dev_thermal_cutoff_count), false);
  2386. root = api_add_int(root, "*Dev Comms Error", &(cgpu->dev_comms_error_count), false);
  2387. root = api_add_int(root, "*Dev Throttle", &(cgpu->dev_throttle_count), false);
  2388. root = print_data(root, buf, isjson, isjson && (device > 0));
  2389. io_add(io_data, buf);
  2390. }
  2391. static void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
  2392. {
  2393. struct cgpu_info *cgpu;
  2394. bool io_open = false;
  2395. int i;
  2396. if (total_devices == 0) {
  2397. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  2398. return;
  2399. }
  2400. message(io_data, MSG_NOTIFY, 0, NULL, isjson);
  2401. if (isjson)
  2402. io_open = io_add(io_data, COMSTR JSON_NOTIFY);
  2403. for (i = 0; i < total_devices; i++) {
  2404. cgpu = get_devices(i);
  2405. notifystatus(io_data, i, cgpu, isjson, group);
  2406. }
  2407. if (isjson && io_open)
  2408. io_close(io_data);
  2409. }
  2410. static void devdetails(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2411. {
  2412. struct api_data *root = NULL;
  2413. char buf[TMPBUFSIZ];
  2414. bool io_open = false;
  2415. struct cgpu_info *cgpu;
  2416. int i;
  2417. if (total_devices == 0) {
  2418. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  2419. return;
  2420. }
  2421. message(io_data, MSG_DEVDETAILS, 0, NULL, isjson);
  2422. if (isjson)
  2423. io_open = io_add(io_data, COMSTR JSON_DEVDETAILS);
  2424. for (i = 0; i < total_devices; i++) {
  2425. cgpu = get_devices(i);
  2426. root = api_add_int(root, "DEVDETAILS", &i, false);
  2427. root = api_add_string(root, "Name", cgpu->drv->name, false);
  2428. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  2429. root = api_add_string(root, "Driver", cgpu->drv->dname, false);
  2430. root = api_add_const(root, "Kernel", cgpu->kname ? : BLANK, false);
  2431. root = api_add_const(root, "Model", cgpu->name ? : BLANK, false);
  2432. root = api_add_const(root, "Device Path", cgpu->device_path ? : BLANK, false);
  2433. root = print_data(root, buf, isjson, isjson && (i > 0));
  2434. io_add(io_data, buf);
  2435. }
  2436. if (isjson && io_open)
  2437. io_close(io_data);
  2438. }
  2439. void dosave(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2440. {
  2441. char filename[PATH_MAX];
  2442. FILE *fcfg;
  2443. char *ptr;
  2444. if (param == NULL || *param == '\0') {
  2445. default_save_file(filename);
  2446. param = filename;
  2447. }
  2448. fcfg = fopen(param, "w");
  2449. if (!fcfg) {
  2450. ptr = escape_string(param, isjson);
  2451. message(io_data, MSG_BADFN, 0, ptr, isjson);
  2452. if (ptr != param)
  2453. free(ptr);
  2454. ptr = NULL;
  2455. return;
  2456. }
  2457. write_config(fcfg);
  2458. fclose(fcfg);
  2459. ptr = escape_string(param, isjson);
  2460. message(io_data, MSG_SAVED, 0, ptr, isjson);
  2461. if (ptr != param)
  2462. free(ptr);
  2463. ptr = NULL;
  2464. }
  2465. 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)
  2466. {
  2467. struct api_data *root = NULL;
  2468. char buf[TMPBUFSIZ];
  2469. root = api_add_int(root, "STATS", &i, false);
  2470. root = api_add_string(root, "ID", id, false);
  2471. root = api_add_elapsed(root, "Elapsed", &(total_secs), false);
  2472. root = api_add_uint32(root, "Calls", &(stats->getwork_calls), false);
  2473. root = api_add_timeval(root, "Wait", &(stats->getwork_wait), false);
  2474. root = api_add_timeval(root, "Max", &(stats->getwork_wait_max), false);
  2475. root = api_add_timeval(root, "Min", &(stats->getwork_wait_min), false);
  2476. if (pool_stats) {
  2477. root = api_add_uint32(root, "Pool Calls", &(pool_stats->getwork_calls), false);
  2478. root = api_add_uint32(root, "Pool Attempts", &(pool_stats->getwork_attempts), false);
  2479. root = api_add_timeval(root, "Pool Wait", &(pool_stats->getwork_wait), false);
  2480. root = api_add_timeval(root, "Pool Max", &(pool_stats->getwork_wait_max), false);
  2481. root = api_add_timeval(root, "Pool Min", &(pool_stats->getwork_wait_min), false);
  2482. root = api_add_double(root, "Pool Av", &(pool_stats->getwork_wait_rolling), false);
  2483. root = api_add_bool(root, "Work Had Roll Time", &(pool_stats->hadrolltime), false);
  2484. root = api_add_bool(root, "Work Can Roll", &(pool_stats->canroll), false);
  2485. root = api_add_bool(root, "Work Had Expire", &(pool_stats->hadexpire), false);
  2486. root = api_add_uint32(root, "Work Roll Time", &(pool_stats->rolltime), false);
  2487. root = api_add_diff(root, "Work Diff", &(pool_stats->last_diff), false);
  2488. root = api_add_diff(root, "Min Diff", &(pool_stats->min_diff), false);
  2489. root = api_add_diff(root, "Max Diff", &(pool_stats->max_diff), false);
  2490. root = api_add_uint32(root, "Min Diff Count", &(pool_stats->min_diff_count), false);
  2491. root = api_add_uint32(root, "Max Diff Count", &(pool_stats->max_diff_count), false);
  2492. root = api_add_uint64(root, "Times Sent", &(pool_stats->times_sent), false);
  2493. root = api_add_uint64(root, "Bytes Sent", &(pool_stats->bytes_sent), false);
  2494. root = api_add_uint64(root, "Times Recv", &(pool_stats->times_received), false);
  2495. root = api_add_uint64(root, "Bytes Recv", &(pool_stats->bytes_received), false);
  2496. root = api_add_uint64(root, "Net Bytes Sent", &(pool_stats->net_bytes_sent), false);
  2497. root = api_add_uint64(root, "Net Bytes Recv", &(pool_stats->net_bytes_received), false);
  2498. }
  2499. if (extra)
  2500. root = api_add_extra(root, extra);
  2501. root = print_data(root, buf, isjson, isjson && (i > 0));
  2502. io_add(io_data, buf);
  2503. return ++i;
  2504. }
  2505. static void minerstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2506. {
  2507. struct cgpu_info *cgpu;
  2508. bool io_open = false;
  2509. struct api_data *extra;
  2510. char id[20];
  2511. int i, j;
  2512. message(io_data, MSG_MINESTATS, 0, NULL, isjson);
  2513. if (isjson)
  2514. io_open = io_add(io_data, COMSTR JSON_MINESTATS);
  2515. i = 0;
  2516. for (j = 0; j < total_devices; j++) {
  2517. cgpu = get_devices(j);
  2518. if (cgpu && cgpu->drv) {
  2519. if (cgpu->drv->get_api_stats)
  2520. extra = cgpu->drv->get_api_stats(cgpu);
  2521. else
  2522. extra = NULL;
  2523. sprintf(id, "%s%d", cgpu->drv->name, cgpu->device_id);
  2524. i = itemstats(io_data, i, id, &(cgpu->cgminer_stats), NULL, extra, isjson);
  2525. }
  2526. }
  2527. for (j = 0; j < total_pools; j++) {
  2528. struct pool *pool = pools[j];
  2529. sprintf(id, "POOL%d", j);
  2530. i = itemstats(io_data, i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, isjson);
  2531. }
  2532. if (isjson && io_open)
  2533. io_close(io_data);
  2534. }
  2535. static void failoveronly(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2536. {
  2537. if (param == NULL || *param == '\0') {
  2538. message(io_data, MSG_MISBOOL, 0, NULL, isjson);
  2539. return;
  2540. }
  2541. *param = tolower(*param);
  2542. if (*param != 't' && *param != 'f') {
  2543. message(io_data, MSG_INVBOOL, 0, NULL, isjson);
  2544. return;
  2545. }
  2546. bool tf = (*param == 't');
  2547. opt_fail_only = tf;
  2548. message(io_data, MSG_FOO, tf, NULL, isjson);
  2549. }
  2550. static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2551. {
  2552. struct api_data *root = NULL;
  2553. char buf[TMPBUFSIZ];
  2554. bool io_open;
  2555. message(io_data, MSG_MINECOIN, 0, NULL, isjson);
  2556. io_open = io_add(io_data, isjson ? COMSTR JSON_MINECOIN : _MINECOIN COMSTR);
  2557. #ifdef USE_SCRYPT
  2558. if (opt_scrypt)
  2559. root = api_add_const(root, "Hash Method", SCRYPTSTR, false);
  2560. else
  2561. #endif
  2562. root = api_add_const(root, "Hash Method", SHA256STR, false);
  2563. mutex_lock(&ch_lock);
  2564. if (current_fullhash && *current_fullhash) {
  2565. root = api_add_timeval(root, "Current Block Time", &block_timeval, true);
  2566. root = api_add_string(root, "Current Block Hash", current_fullhash, true);
  2567. } else {
  2568. struct timeval t = {0,0};
  2569. root = api_add_timeval(root, "Current Block Time", &t, true);
  2570. root = api_add_const(root, "Current Block Hash", BLANK, false);
  2571. }
  2572. mutex_unlock(&ch_lock);
  2573. root = api_add_bool(root, "LP", &have_longpoll, false);
  2574. root = print_data(root, buf, isjson, false);
  2575. io_add(io_data, buf);
  2576. if (isjson && io_open)
  2577. io_close(io_data);
  2578. }
  2579. static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2580. {
  2581. struct api_data *root = NULL;
  2582. char buf[TMPBUFSIZ];
  2583. bool io_open;
  2584. if (param == NULL)
  2585. param = (char *)BLANK;
  2586. else
  2587. *param = tolower(*param);
  2588. switch(*param) {
  2589. case 's':
  2590. opt_realquiet = true;
  2591. break;
  2592. case 'q':
  2593. opt_quiet ^= true;
  2594. break;
  2595. case 'v':
  2596. opt_log_output ^= true;
  2597. if (opt_log_output)
  2598. opt_quiet = false;
  2599. break;
  2600. case 'd':
  2601. opt_debug ^= true;
  2602. opt_log_output = opt_debug;
  2603. if (opt_debug)
  2604. opt_quiet = false;
  2605. break;
  2606. case 'r':
  2607. opt_protocol ^= true;
  2608. if (opt_protocol)
  2609. opt_quiet = false;
  2610. break;
  2611. case 'p':
  2612. want_per_device_stats ^= true;
  2613. opt_log_output = want_per_device_stats;
  2614. break;
  2615. case 'n':
  2616. opt_log_output = false;
  2617. opt_debug = false;
  2618. opt_quiet = false;
  2619. opt_protocol = false;
  2620. want_per_device_stats = false;
  2621. opt_worktime = false;
  2622. break;
  2623. case 'w':
  2624. opt_worktime ^= true;
  2625. break;
  2626. #ifdef _MEMORY_DEBUG
  2627. case 'y':
  2628. cgmemspeedup();
  2629. break;
  2630. case 'z':
  2631. cgmemrpt();
  2632. break;
  2633. #endif
  2634. default:
  2635. // anything else just reports the settings
  2636. break;
  2637. }
  2638. message(io_data, MSG_DEBUGSET, 0, NULL, isjson);
  2639. io_open = io_add(io_data, isjson ? COMSTR JSON_DEBUGSET : _DEBUGSET COMSTR);
  2640. root = api_add_bool(root, "Silent", &opt_realquiet, false);
  2641. root = api_add_bool(root, "Quiet", &opt_quiet, false);
  2642. root = api_add_bool(root, "Verbose", &opt_log_output, false);
  2643. root = api_add_bool(root, "Debug", &opt_debug, false);
  2644. root = api_add_bool(root, "RPCProto", &opt_protocol, false);
  2645. root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
  2646. root = api_add_bool(root, "WorkTime", &opt_worktime, false);
  2647. root = print_data(root, buf, isjson, false);
  2648. io_add(io_data, buf);
  2649. if (isjson && io_open)
  2650. io_close(io_data);
  2651. }
  2652. static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2653. {
  2654. char *comma;
  2655. int value;
  2656. if (param == NULL || *param == '\0') {
  2657. message(io_data, MSG_CONPAR, 0, NULL, isjson);
  2658. return;
  2659. }
  2660. comma = strchr(param, ',');
  2661. if (!comma) {
  2662. message(io_data, MSG_CONVAL, 0, param, isjson);
  2663. return;
  2664. }
  2665. *(comma++) = '\0';
  2666. value = atoi(comma);
  2667. if (value < 0 || value > 9999) {
  2668. message(io_data, MSG_INVNUM, value, param, isjson);
  2669. return;
  2670. }
  2671. if (strcasecmp(param, "queue") == 0)
  2672. opt_queue = value;
  2673. else if (strcasecmp(param, "scantime") == 0)
  2674. opt_scantime = value;
  2675. else if (strcasecmp(param, "expiry") == 0)
  2676. opt_expiry = value;
  2677. else {
  2678. message(io_data, MSG_UNKCON, 0, param, isjson);
  2679. return;
  2680. }
  2681. message(io_data, MSG_SETCONFIG, value, param, isjson);
  2682. }
  2683. static void usbstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2684. {
  2685. struct api_data *root = NULL;
  2686. #if defined(USE_MODMINER) || defined(USE_BITFORCE)
  2687. char buf[TMPBUFSIZ];
  2688. bool io_open = false;
  2689. int count = 0;
  2690. root = api_usb_stats(&count);
  2691. #endif
  2692. if (!root) {
  2693. message(io_data, MSG_NOUSTA, 0, NULL, isjson);
  2694. return;
  2695. }
  2696. #if defined(USE_MODMINER) || defined(USE_BITFORCE)
  2697. message(io_data, MSG_USBSTA, 0, NULL, isjson);
  2698. if (isjson)
  2699. io_open = io_add(io_data, COMSTR JSON_USBSTATS);
  2700. root = print_data(root, buf, isjson, false);
  2701. io_add(io_data, buf);
  2702. while (42) {
  2703. root = api_usb_stats(&count);
  2704. if (!root)
  2705. break;
  2706. root = print_data(root, buf, isjson, isjson);
  2707. io_add(io_data, buf);
  2708. }
  2709. if (isjson && io_open)
  2710. io_close(io_data);
  2711. #endif
  2712. }
  2713. #ifdef HAVE_AN_FPGA
  2714. static void pgaset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2715. {
  2716. struct cgpu_info *cgpu;
  2717. struct device_drv *drv;
  2718. char buf[TMPBUFSIZ];
  2719. int numpga = numpgas();
  2720. if (numpga == 0) {
  2721. message(io_data, MSG_PGANON, 0, NULL, isjson);
  2722. return;
  2723. }
  2724. if (param == NULL || *param == '\0') {
  2725. message(io_data, MSG_MISID, 0, NULL, isjson);
  2726. return;
  2727. }
  2728. char *opt = strchr(param, ',');
  2729. if (opt)
  2730. *(opt++) = '\0';
  2731. if (!opt || !*opt) {
  2732. message(io_data, MSG_MISPGAOPT, 0, NULL, isjson);
  2733. return;
  2734. }
  2735. int id = atoi(param);
  2736. if (id < 0 || id >= numpga) {
  2737. message(io_data, MSG_INVPGA, id, NULL, isjson);
  2738. return;
  2739. }
  2740. int dev = pgadevice(id);
  2741. if (dev < 0) { // Should never happen
  2742. message(io_data, MSG_INVPGA, id, NULL, isjson);
  2743. return;
  2744. }
  2745. cgpu = get_devices(dev);
  2746. drv = cgpu->drv;
  2747. char *set = strchr(opt, ',');
  2748. if (set)
  2749. *(set++) = '\0';
  2750. if (!drv->set_device)
  2751. message(io_data, MSG_PGANOSET, id, NULL, isjson);
  2752. else {
  2753. char *ret = drv->set_device(cgpu, opt, set, buf);
  2754. if (ret) {
  2755. if (strcasecmp(opt, "help") == 0)
  2756. message(io_data, MSG_PGAHELP, id, ret, isjson);
  2757. else
  2758. message(io_data, MSG_PGASETERR, id, ret, isjson);
  2759. } else
  2760. message(io_data, MSG_PGASETOK, id, NULL, isjson);
  2761. }
  2762. }
  2763. #endif
  2764. static void dozero(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2765. {
  2766. if (param == NULL || *param == '\0') {
  2767. message(io_data, MSG_ZERMIS, 0, NULL, isjson);
  2768. return;
  2769. }
  2770. char *sum = strchr(param, ',');
  2771. if (sum)
  2772. *(sum++) = '\0';
  2773. if (!sum || !*sum) {
  2774. message(io_data, MSG_MISBOOL, 0, NULL, isjson);
  2775. return;
  2776. }
  2777. bool all = false;
  2778. bool bs = false;
  2779. if (strcasecmp(param, "all") == 0)
  2780. all = true;
  2781. else if (strcasecmp(param, "bestshare") == 0)
  2782. bs = true;
  2783. if (all == false && bs == false) {
  2784. message(io_data, MSG_ZERINV, 0, param, isjson);
  2785. return;
  2786. }
  2787. *sum = tolower(*sum);
  2788. if (*sum != 't' && *sum != 'f') {
  2789. message(io_data, MSG_INVBOOL, 0, NULL, isjson);
  2790. return;
  2791. }
  2792. bool dosum = (*sum == 't');
  2793. if (dosum)
  2794. print_summary();
  2795. if (all)
  2796. zero_stats();
  2797. if (bs)
  2798. zero_bestshare();
  2799. if (dosum)
  2800. message(io_data, MSG_ZERSUM, 0, all ? "All" : "BestShare", isjson);
  2801. else
  2802. message(io_data, MSG_ZERNOSUM, 0, all ? "All" : "BestShare", isjson);
  2803. }
  2804. static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group);
  2805. struct CMDS {
  2806. char *name;
  2807. void (*func)(struct io_data *, SOCKETTYPE, char *, bool, char);
  2808. bool iswritemode;
  2809. } cmds[] = {
  2810. { "version", apiversion, false },
  2811. { "config", minerconfig, false },
  2812. { "devs", devstatus, false },
  2813. { "pools", poolstatus, false },
  2814. { "summary", summary, false },
  2815. #ifdef HAVE_OPENCL
  2816. { "gpuenable", gpuenable, true },
  2817. { "gpudisable", gpudisable, true },
  2818. { "gpurestart", gpurestart, true },
  2819. { "gpu", gpudev, false },
  2820. #endif
  2821. #ifdef HAVE_AN_FPGA
  2822. { "pga", pgadev, false },
  2823. { "pgaenable", pgaenable, true },
  2824. { "pgadisable", pgadisable, true },
  2825. { "pgaidentify", pgaidentify, true },
  2826. #endif
  2827. #ifdef WANT_CPUMINE
  2828. { "cpu", cpudev, false },
  2829. #endif
  2830. { "gpucount", gpucount, false },
  2831. { "pgacount", pgacount, false },
  2832. { "cpucount", cpucount, false },
  2833. { "switchpool", switchpool, true },
  2834. { "addpool", addpool, true },
  2835. { "poolpriority", poolpriority, true },
  2836. { "enablepool", enablepool, true },
  2837. { "disablepool", disablepool, true },
  2838. { "removepool", removepool, true },
  2839. #ifdef HAVE_OPENCL
  2840. { "gpuintensity", gpuintensity, true },
  2841. { "gpumem", gpumem, true },
  2842. { "gpuengine", gpuengine, true },
  2843. { "gpufan", gpufan, true },
  2844. { "gpuvddc", gpuvddc, true },
  2845. #endif
  2846. { "save", dosave, true },
  2847. { "quit", doquit, true },
  2848. { "privileged", privileged, true },
  2849. { "notify", notify, false },
  2850. { "devdetails", devdetails, false },
  2851. { "restart", dorestart, true },
  2852. { "stats", minerstats, false },
  2853. { "check", checkcommand, false },
  2854. { "failover-only", failoveronly, true },
  2855. { "coin", minecoin, false },
  2856. { "debug", debugstate, true },
  2857. { "setconfig", setconfig, true },
  2858. { "usbstats", usbstats, false },
  2859. #ifdef HAVE_AN_FPGA
  2860. { "pgaset", pgaset, true },
  2861. #endif
  2862. { "zero", dozero, true },
  2863. { NULL, NULL, false }
  2864. };
  2865. static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
  2866. {
  2867. struct api_data *root = NULL;
  2868. char buf[TMPBUFSIZ];
  2869. bool io_open;
  2870. char cmdbuf[100];
  2871. bool found, access;
  2872. int i;
  2873. if (param == NULL || *param == '\0') {
  2874. message(io_data, MSG_MISCHK, 0, NULL, isjson);
  2875. return;
  2876. }
  2877. found = false;
  2878. access = false;
  2879. for (i = 0; cmds[i].name != NULL; i++) {
  2880. if (strcmp(cmds[i].name, param) == 0) {
  2881. found = true;
  2882. sprintf(cmdbuf, "|%s|", param);
  2883. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  2884. access = true;
  2885. break;
  2886. }
  2887. }
  2888. message(io_data, MSG_CHECK, 0, NULL, isjson);
  2889. io_open = io_add(io_data, isjson ? COMSTR JSON_CHECK : _CHECK COMSTR);
  2890. root = api_add_const(root, "Exists", found ? YES : NO, false);
  2891. root = api_add_const(root, "Access", access ? YES : NO, false);
  2892. root = print_data(root, buf, isjson, false);
  2893. io_add(io_data, buf);
  2894. if (isjson && io_open)
  2895. io_close(io_data);
  2896. }
  2897. static void send_result(struct io_data *io_data, SOCKETTYPE c, bool isjson)
  2898. {
  2899. char buf[SOCKBUFSIZ + sizeof(JSON_CLOSE) + sizeof(JSON_END)];
  2900. int len;
  2901. int n;
  2902. strcpy(buf, io_data->ptr);
  2903. if (io_data->close)
  2904. strcat(buf, JSON_CLOSE);
  2905. if (isjson) {
  2906. if (io_data->full)
  2907. strcat(buf, JSON_END_TRUNCATED);
  2908. else
  2909. strcat(buf, JSON_END);
  2910. }
  2911. len = strlen(buf);
  2912. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, buf, len > 10 ? "..." : BLANK);
  2913. // ignore failure - it's closed immediately anyway
  2914. n = send(c, buf, len+1, 0);
  2915. if (opt_debug) {
  2916. if (SOCKETFAIL(n))
  2917. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  2918. else
  2919. applog(LOG_DEBUG, "API: sent %d", n);
  2920. }
  2921. }
  2922. static void tidyup(__maybe_unused void *arg)
  2923. {
  2924. mutex_lock(&quit_restart_lock);
  2925. bye = true;
  2926. if (sock != INVSOCK) {
  2927. shutdown(sock, SHUT_RDWR);
  2928. CLOSESOCKET(sock);
  2929. sock = INVSOCK;
  2930. }
  2931. if (ipaccess != NULL) {
  2932. free(ipaccess);
  2933. ipaccess = NULL;
  2934. }
  2935. io_free();
  2936. mutex_unlock(&quit_restart_lock);
  2937. }
  2938. /*
  2939. * Interpret --api-groups G:cmd1:cmd2:cmd3,P:cmd4,*,...
  2940. */
  2941. static void setup_groups()
  2942. {
  2943. char *api_groups = opt_api_groups ? opt_api_groups : (char *)BLANK;
  2944. char *buf, *ptr, *next, *colon;
  2945. char group;
  2946. char commands[TMPBUFSIZ];
  2947. char cmdbuf[100];
  2948. char *cmd;
  2949. bool addstar, did;
  2950. int i;
  2951. buf = malloc(strlen(api_groups) + 1);
  2952. if (unlikely(!buf))
  2953. quit(1, "Failed to malloc ipgroups buf");
  2954. strcpy(buf, api_groups);
  2955. next = buf;
  2956. // for each group defined
  2957. while (next && *next) {
  2958. ptr = next;
  2959. next = strchr(ptr, ',');
  2960. if (next)
  2961. *(next++) = '\0';
  2962. // Validate the group
  2963. if (*(ptr+1) != ':') {
  2964. colon = strchr(ptr, ':');
  2965. if (colon)
  2966. *colon = '\0';
  2967. applog(LOG_WARNING, "API invalid group name '%s'", ptr);
  2968. quit(1, INVAPIGROUPS);
  2969. }
  2970. group = GROUP(*ptr);
  2971. if (!VALIDGROUP(group)) {
  2972. applog(LOG_WARNING, "API invalid group name '%c'", *ptr);
  2973. quit(1, INVAPIGROUPS);
  2974. }
  2975. if (group == PRIVGROUP) {
  2976. applog(LOG_WARNING, "API group name can't be '%c'", PRIVGROUP);
  2977. quit(1, INVAPIGROUPS);
  2978. }
  2979. if (group == NOPRIVGROUP) {
  2980. applog(LOG_WARNING, "API group name can't be '%c'", NOPRIVGROUP);
  2981. quit(1, INVAPIGROUPS);
  2982. }
  2983. if (apigroups[GROUPOFFSET(group)].commands != NULL) {
  2984. applog(LOG_WARNING, "API duplicate group name '%c'", *ptr);
  2985. quit(1, INVAPIGROUPS);
  2986. }
  2987. ptr += 2;
  2988. // Validate the command list (and handle '*')
  2989. cmd = &(commands[0]);
  2990. *(cmd++) = SEPARATOR;
  2991. *cmd = '\0';
  2992. addstar = false;
  2993. while (ptr && *ptr) {
  2994. colon = strchr(ptr, ':');
  2995. if (colon)
  2996. *(colon++) = '\0';
  2997. if (strcmp(ptr, "*") == 0)
  2998. addstar = true;
  2999. else {
  3000. did = false;
  3001. for (i = 0; cmds[i].name != NULL; i++) {
  3002. if (strcasecmp(ptr, cmds[i].name) == 0) {
  3003. did = true;
  3004. break;
  3005. }
  3006. }
  3007. if (did) {
  3008. // skip duplicates
  3009. sprintf(cmdbuf, "|%s|", cmds[i].name);
  3010. if (strstr(commands, cmdbuf) == NULL) {
  3011. strcpy(cmd, cmds[i].name);
  3012. cmd += strlen(cmds[i].name);
  3013. *(cmd++) = SEPARATOR;
  3014. *cmd = '\0';
  3015. }
  3016. } else {
  3017. applog(LOG_WARNING, "API unknown command '%s' in group '%c'", ptr, group);
  3018. quit(1, INVAPIGROUPS);
  3019. }
  3020. }
  3021. ptr = colon;
  3022. }
  3023. // * = allow all non-iswritemode commands
  3024. if (addstar) {
  3025. for (i = 0; cmds[i].name != NULL; i++) {
  3026. if (cmds[i].iswritemode == false) {
  3027. // skip duplicates
  3028. sprintf(cmdbuf, "|%s|", cmds[i].name);
  3029. if (strstr(commands, cmdbuf) == NULL) {
  3030. strcpy(cmd, cmds[i].name);
  3031. cmd += strlen(cmds[i].name);
  3032. *(cmd++) = SEPARATOR;
  3033. *cmd = '\0';
  3034. }
  3035. }
  3036. }
  3037. }
  3038. ptr = apigroups[GROUPOFFSET(group)].commands = malloc(strlen(commands) + 1);
  3039. if (unlikely(!ptr))
  3040. quit(1, "Failed to malloc group commands buf");
  3041. strcpy(ptr, commands);
  3042. }
  3043. // Now define R (NOPRIVGROUP) as all non-iswritemode commands
  3044. cmd = &(commands[0]);
  3045. *(cmd++) = SEPARATOR;
  3046. *cmd = '\0';
  3047. for (i = 0; cmds[i].name != NULL; i++) {
  3048. if (cmds[i].iswritemode == false) {
  3049. strcpy(cmd, cmds[i].name);
  3050. cmd += strlen(cmds[i].name);
  3051. *(cmd++) = SEPARATOR;
  3052. *cmd = '\0';
  3053. }
  3054. }
  3055. ptr = apigroups[GROUPOFFSET(NOPRIVGROUP)].commands = malloc(strlen(commands) + 1);
  3056. if (unlikely(!ptr))
  3057. quit(1, "Failed to malloc noprivgroup commands buf");
  3058. strcpy(ptr, commands);
  3059. // W (PRIVGROUP) is handled as a special case since it simply means all commands
  3060. free(buf);
  3061. return;
  3062. }
  3063. /*
  3064. * Interpret [W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  3065. * special case of 0/0 allows /0 (means all IP addresses)
  3066. */
  3067. #define ALLIP4 "0/0"
  3068. /*
  3069. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  3070. */
  3071. static void setup_ipaccess()
  3072. {
  3073. char *buf, *ptr, *comma, *slash, *dot;
  3074. int ipcount, mask, octet, i;
  3075. char group;
  3076. buf = malloc(strlen(opt_api_allow) + 1);
  3077. if (unlikely(!buf))
  3078. quit(1, "Failed to malloc ipaccess buf");
  3079. strcpy(buf, opt_api_allow);
  3080. ipcount = 1;
  3081. ptr = buf;
  3082. while (*ptr)
  3083. if (*(ptr++) == ',')
  3084. ipcount++;
  3085. // possibly more than needed, but never less
  3086. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  3087. if (unlikely(!ipaccess))
  3088. quit(1, "Failed to calloc ipaccess");
  3089. ips = 0;
  3090. ptr = buf;
  3091. while (ptr && *ptr) {
  3092. while (*ptr == ' ' || *ptr == '\t')
  3093. ptr++;
  3094. if (*ptr == ',') {
  3095. ptr++;
  3096. continue;
  3097. }
  3098. comma = strchr(ptr, ',');
  3099. if (comma)
  3100. *(comma++) = '\0';
  3101. group = NOPRIVGROUP;
  3102. if (isalpha(*ptr) && *(ptr+1) == ':') {
  3103. if (DEFINEDGROUP(*ptr))
  3104. group = GROUP(*ptr);
  3105. ptr += 2;
  3106. }
  3107. ipaccess[ips].group = group;
  3108. if (strcmp(ptr, ALLIP4) == 0)
  3109. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  3110. else {
  3111. slash = strchr(ptr, '/');
  3112. if (!slash)
  3113. ipaccess[ips].mask = 0xffffffff;
  3114. else {
  3115. *(slash++) = '\0';
  3116. mask = atoi(slash);
  3117. if (mask < 1 || mask > 32)
  3118. goto popipo; // skip invalid/zero
  3119. ipaccess[ips].mask = 0;
  3120. while (mask-- >= 0) {
  3121. octet = 1 << (mask % 8);
  3122. ipaccess[ips].mask |= (octet << (24 - (8 * (mask >> 3))));
  3123. }
  3124. }
  3125. ipaccess[ips].ip = 0; // missing default to '.0'
  3126. for (i = 0; ptr && (i < 4); i++) {
  3127. dot = strchr(ptr, '.');
  3128. if (dot)
  3129. *(dot++) = '\0';
  3130. octet = atoi(ptr);
  3131. if (octet < 0 || octet > 0xff)
  3132. goto popipo; // skip invalid
  3133. ipaccess[ips].ip |= (octet << (24 - (i * 8)));
  3134. ptr = dot;
  3135. }
  3136. ipaccess[ips].ip &= ipaccess[ips].mask;
  3137. }
  3138. ips++;
  3139. popipo:
  3140. ptr = comma;
  3141. }
  3142. free(buf);
  3143. }
  3144. static void *quit_thread(__maybe_unused void *userdata)
  3145. {
  3146. // allow thread creator to finish whatever it's doing
  3147. mutex_lock(&quit_restart_lock);
  3148. mutex_unlock(&quit_restart_lock);
  3149. if (opt_debug)
  3150. applog(LOG_DEBUG, "API: killing cgminer");
  3151. kill_work();
  3152. return NULL;
  3153. }
  3154. static void *restart_thread(__maybe_unused void *userdata)
  3155. {
  3156. // allow thread creator to finish whatever it's doing
  3157. mutex_lock(&quit_restart_lock);
  3158. mutex_unlock(&quit_restart_lock);
  3159. if (opt_debug)
  3160. applog(LOG_DEBUG, "API: restarting cgminer");
  3161. app_restart();
  3162. return NULL;
  3163. }
  3164. void api(int api_thr_id)
  3165. {
  3166. struct io_data *io_data;
  3167. struct thr_info bye_thr;
  3168. char buf[TMPBUFSIZ];
  3169. char param_buf[TMPBUFSIZ];
  3170. const char *localaddr = "127.0.0.1";
  3171. SOCKETTYPE c;
  3172. int n, bound;
  3173. char *connectaddr;
  3174. char *binderror;
  3175. time_t bindstart;
  3176. short int port = opt_api_port;
  3177. struct sockaddr_in serv;
  3178. struct sockaddr_in cli;
  3179. socklen_t clisiz;
  3180. char cmdbuf[100];
  3181. char *cmd;
  3182. char *param;
  3183. bool addrok;
  3184. char group;
  3185. json_error_t json_err;
  3186. json_t *json_config;
  3187. json_t *json_val;
  3188. bool isjson;
  3189. bool did;
  3190. int i;
  3191. if (!opt_api_listen) {
  3192. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  3193. return;
  3194. }
  3195. io_data = sock_io_new();
  3196. mutex_init(&quit_restart_lock);
  3197. pthread_cleanup_push(tidyup, NULL);
  3198. my_thr_id = api_thr_id;
  3199. setup_groups();
  3200. if (opt_api_allow) {
  3201. setup_ipaccess();
  3202. if (ips == 0) {
  3203. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  3204. return;
  3205. }
  3206. }
  3207. /* This should be done before curl in needed
  3208. * to ensure curl has already called WSAStartup() in windows */
  3209. nmsleep(opt_log_interval*1000);
  3210. sock = socket(AF_INET, SOCK_STREAM, 0);
  3211. if (sock == INVSOCK) {
  3212. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3213. return;
  3214. }
  3215. memset(&serv, 0, sizeof(serv));
  3216. serv.sin_family = AF_INET;
  3217. if (!opt_api_allow && !opt_api_network) {
  3218. serv.sin_addr.s_addr = inet_addr(localaddr);
  3219. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  3220. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3221. return;
  3222. }
  3223. }
  3224. serv.sin_port = htons(port);
  3225. #ifndef WIN32
  3226. // On linux with SO_REUSEADDR, bind will get the port if the previous
  3227. // socket is closed (even if it is still in TIME_WAIT) but fail if
  3228. // another program has it open - which is what we want
  3229. int optval = 1;
  3230. // If it doesn't work, we don't really care - just show a debug message
  3231. if (SOCKETFAIL(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval))))
  3232. applog(LOG_DEBUG, "API setsockopt SO_REUSEADDR failed (ignored): %s", SOCKERRMSG);
  3233. #else
  3234. // On windows a 2nd program can bind to a port>1024 already in use unless
  3235. // SO_EXCLUSIVEADDRUSE is used - however then the bind to a closed port
  3236. // in TIME_WAIT will fail until the timeout - so we leave the options alone
  3237. #endif
  3238. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  3239. bound = 0;
  3240. bindstart = time(NULL);
  3241. while (bound == 0) {
  3242. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  3243. binderror = SOCKERRMSG;
  3244. if ((time(NULL) - bindstart) > 61)
  3245. break;
  3246. else {
  3247. applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
  3248. nmsleep(30000);
  3249. }
  3250. } else
  3251. bound = 1;
  3252. }
  3253. if (bound == 0) {
  3254. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  3255. return;
  3256. }
  3257. if (SOCKETFAIL(listen(sock, QUEUE))) {
  3258. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3259. CLOSESOCKET(sock);
  3260. return;
  3261. }
  3262. if (opt_api_allow)
  3263. applog(LOG_WARNING, "API running in IP access mode on port %d", port);
  3264. else {
  3265. if (opt_api_network)
  3266. applog(LOG_WARNING, "API running in UNRESTRICTED read access mode on port %d", port);
  3267. else
  3268. applog(LOG_WARNING, "API running in local read access mode on port %d", port);
  3269. }
  3270. while (!bye) {
  3271. clisiz = sizeof(cli);
  3272. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  3273. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3274. goto die;
  3275. }
  3276. connectaddr = inet_ntoa(cli.sin_addr);
  3277. addrok = false;
  3278. group = NOPRIVGROUP;
  3279. if (opt_api_allow) {
  3280. int client_ip = htonl(cli.sin_addr.s_addr);
  3281. for (i = 0; i < ips; i++) {
  3282. if ((client_ip & ipaccess[i].mask) == ipaccess[i].ip) {
  3283. addrok = true;
  3284. group = ipaccess[i].group;
  3285. break;
  3286. }
  3287. }
  3288. } else {
  3289. if (opt_api_network)
  3290. addrok = true;
  3291. else
  3292. addrok = (strcmp(connectaddr, localaddr) == 0);
  3293. }
  3294. if (opt_debug)
  3295. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  3296. if (addrok) {
  3297. n = recv(c, &buf[0], TMPBUFSIZ-1, 0);
  3298. if (SOCKETFAIL(n))
  3299. buf[0] = '\0';
  3300. else
  3301. buf[n] = '\0';
  3302. if (opt_debug) {
  3303. if (SOCKETFAIL(n))
  3304. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  3305. else
  3306. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  3307. }
  3308. if (!SOCKETFAIL(n)) {
  3309. // the time of the request in now
  3310. when = time(NULL);
  3311. io_reinit(io_data);
  3312. did = false;
  3313. if (*buf != ISJSON) {
  3314. isjson = false;
  3315. param = strchr(buf, SEPARATOR);
  3316. if (param != NULL)
  3317. *(param++) = '\0';
  3318. cmd = buf;
  3319. }
  3320. else {
  3321. isjson = true;
  3322. param = NULL;
  3323. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  3324. json_config = json_loadb(buf, n, 0, &json_err);
  3325. #elif JANSSON_MAJOR_VERSION > 1
  3326. json_config = json_loads(buf, 0, &json_err);
  3327. #else
  3328. json_config = json_loads(buf, &json_err);
  3329. #endif
  3330. if (!json_is_object(json_config)) {
  3331. message(io_data, MSG_INVJSON, 0, NULL, isjson);
  3332. send_result(io_data, c, isjson);
  3333. did = true;
  3334. }
  3335. else {
  3336. json_val = json_object_get(json_config, JSON_COMMAND);
  3337. if (json_val == NULL) {
  3338. message(io_data, MSG_MISCMD, 0, NULL, isjson);
  3339. send_result(io_data, c, isjson);
  3340. did = true;
  3341. }
  3342. else {
  3343. if (!json_is_string(json_val)) {
  3344. message(io_data, MSG_INVCMD, 0, NULL, isjson);
  3345. send_result(io_data, c, isjson);
  3346. did = true;
  3347. }
  3348. else {
  3349. cmd = (char *)json_string_value(json_val);
  3350. json_val = json_object_get(json_config, JSON_PARAMETER);
  3351. if (json_is_string(json_val))
  3352. param = (char *)json_string_value(json_val);
  3353. else if (json_is_integer(json_val)) {
  3354. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  3355. param = param_buf;
  3356. } else if (json_is_real(json_val)) {
  3357. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  3358. param = param_buf;
  3359. }
  3360. }
  3361. }
  3362. }
  3363. }
  3364. if (!did)
  3365. for (i = 0; cmds[i].name != NULL; i++) {
  3366. if (strcmp(cmd, cmds[i].name) == 0) {
  3367. sprintf(cmdbuf, "|%s|", cmd);
  3368. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  3369. (cmds[i].func)(io_data, c, param, isjson, group);
  3370. else {
  3371. message(io_data, MSG_ACCDENY, 0, cmds[i].name, isjson);
  3372. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  3373. }
  3374. send_result(io_data, c, isjson);
  3375. did = true;
  3376. break;
  3377. }
  3378. }
  3379. if (!did) {
  3380. message(io_data, MSG_INVCMD, 0, NULL, isjson);
  3381. send_result(io_data, c, isjson);
  3382. }
  3383. }
  3384. }
  3385. CLOSESOCKET(c);
  3386. }
  3387. die:
  3388. /* Blank line fix for older compilers since pthread_cleanup_pop is a
  3389. * macro that gets confused by a label existing immediately before it
  3390. */
  3391. ;
  3392. pthread_cleanup_pop(true);
  3393. if (opt_debug)
  3394. applog(LOG_DEBUG, "API: terminating due to: %s",
  3395. do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
  3396. mutex_lock(&quit_restart_lock);
  3397. if (do_a_restart) {
  3398. if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
  3399. mutex_unlock(&quit_restart_lock);
  3400. quit(1, "API failed to initiate a restart - aborting");
  3401. }
  3402. pthread_detach(bye_thr.pth);
  3403. } else if (do_a_quit) {
  3404. if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
  3405. mutex_unlock(&quit_restart_lock);
  3406. quit(1, "API failed to initiate a clean quit - aborting");
  3407. }
  3408. pthread_detach(bye_thr.pth);
  3409. }
  3410. mutex_unlock(&quit_restart_lock);
  3411. }