api.c 111 KB

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