api.c 111 KB

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