api.c 98 KB

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