api.c 111 KB

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