api.c 115 KB

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