api.c 100 KB

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