api.c 109 KB

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