api.c 123 KB

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