api.c 115 KB

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