api.c 124 KB

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