api.c 102 KB

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