api.c 110 KB

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