api.c 100 KB

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