api.c 123 KB

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