api.c 103 KB

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