api.c 118 KB

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