api.c 107 KB

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