api.c 98 KB

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