api.c 99 KB

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