api.c 109 KB

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