api.c 109 KB

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