api.c 106 KB

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