api.c 106 KB

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