api.c 101 KB

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