api.c 124 KB

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