api.c 116 KB

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