api.c 114 KB

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