api.c 114 KB

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