api.c 123 KB

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