api.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850
  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. #endif
  205. #define MSG_NUMGPU 20
  206. #define MSG_NUMCPU 21
  207. #define MSG_VERSION 22
  208. #define MSG_INVJSON 23
  209. #define MSG_MISCMD 24
  210. #define MSG_MISPID 25
  211. #define MSG_INVPID 26
  212. #define MSG_SWITCHP 27
  213. #define MSG_MISVAL 28
  214. #define MSG_NOADL 29
  215. #define MSG_NOGPUADL 30
  216. #define MSG_INVINT 31
  217. #define MSG_GPUINT 32
  218. #define MSG_MINECONFIG 33
  219. #define MSG_GPUMERR 34
  220. #define MSG_GPUMEM 35
  221. #define MSG_GPUEERR 36
  222. #define MSG_GPUENG 37
  223. #define MSG_GPUVERR 38
  224. #define MSG_GPUVDDC 39
  225. #define MSG_GPUFERR 40
  226. #define MSG_GPUFAN 41
  227. #define MSG_MISFN 42
  228. #define MSG_BADFN 43
  229. #define MSG_SAVED 44
  230. #define MSG_ACCDENY 45
  231. #define MSG_ACCOK 46
  232. #define MSG_ENAPOOL 47
  233. #define MSG_DISPOOL 48
  234. #define MSG_ALRENAP 49
  235. #define MSG_ALRDISP 50
  236. #define MSG_DISLASTP 51
  237. #define MSG_MISPDP 52
  238. #define MSG_INVPDP 53
  239. #define MSG_TOOMANYP 54
  240. #define MSG_ADDPOOL 55
  241. #ifdef HAVE_AN_FPGA
  242. #define MSG_PGANON 56
  243. #define MSG_PGADEV 57
  244. #define MSG_INVPGA 58
  245. #endif
  246. #define MSG_NUMPGA 59
  247. #define MSG_NOTIFY 60
  248. #ifdef HAVE_AN_FPGA
  249. #define MSG_PGALRENA 61
  250. #define MSG_PGALRDIS 62
  251. #define MSG_PGAENA 63
  252. #define MSG_PGADIS 64
  253. #define MSG_PGAUNW 65
  254. #endif
  255. #define MSG_REMLASTP 66
  256. #define MSG_ACTPOOL 67
  257. #define MSG_REMPOOL 68
  258. #define MSG_DEVDETAILS 69
  259. #define MSG_MINESTATS 70
  260. #define MSG_MISCHK 71
  261. #define MSG_CHECK 72
  262. #define MSG_POOLPRIO 73
  263. #define MSG_DUPPID 74
  264. #define MSG_MISBOOL 75
  265. #define MSG_INVBOOL 76
  266. #define MSG_FOO 77
  267. #define MSG_MINECOIN 78
  268. #define MSG_DEBUGSET 79
  269. #define MSG_PGAIDENT 80
  270. #define MSG_PGANOID 81
  271. #define MSG_SETCONFIG 82
  272. #define MSG_UNKCON 83
  273. #define MSG_INVNUM 84
  274. #define MSG_CONPAR 85
  275. #define MSG_CONVAL 86
  276. #ifdef HAVE_AN_FPGA
  277. #define MSG_MISPGAOPT 89
  278. #define MSG_PGANOSET 90
  279. #define MSG_PGAHELP 91
  280. #define MSG_PGASETOK 92
  281. #define MSG_PGASETERR 93
  282. #endif
  283. #define MSG_ZERMIS 94
  284. #define MSG_ZERINV 95
  285. #define MSG_ZERSUM 96
  286. #define MSG_ZERNOSUM 97
  287. #define MSG_DEVSCAN 0x100
  288. enum code_severity {
  289. SEVERITY_ERR,
  290. SEVERITY_WARN,
  291. SEVERITY_INFO,
  292. SEVERITY_SUCC,
  293. SEVERITY_FAIL
  294. };
  295. enum code_parameters {
  296. PARAM_COUNT,
  297. PARAM_GPU,
  298. PARAM_PGA,
  299. PARAM_CPU,
  300. PARAM_PID,
  301. PARAM_GPUMAX,
  302. PARAM_PGAMAX,
  303. PARAM_CPUMAX,
  304. PARAM_PMAX,
  305. PARAM_POOLMAX,
  306. // Single generic case: have the code resolve it - see below
  307. PARAM_DMAX,
  308. PARAM_CMD,
  309. PARAM_POOL,
  310. PARAM_STR,
  311. PARAM_BOTH,
  312. PARAM_BOOL,
  313. PARAM_SET,
  314. PARAM_NONE
  315. };
  316. struct CODES {
  317. const enum code_severity severity;
  318. const int code;
  319. const enum code_parameters params;
  320. const char *description;
  321. } codes[] = {
  322. #ifdef HAVE_OPENCL
  323. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  324. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  325. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  326. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  327. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  328. #endif
  329. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  330. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  331. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  332. { SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX,
  333. #ifdef HAVE_OPENCL
  334. "%d GPU(s)"
  335. #endif
  336. #if defined(HAVE_AN_FPGA) && defined(HAVE_OPENCL)
  337. " - "
  338. #endif
  339. #ifdef HAVE_AN_FPGA
  340. "%d PGA(s)"
  341. #endif
  342. #if defined(WANT_CPUMINE) && (defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA))
  343. " - "
  344. #endif
  345. #ifdef WANT_CPUMINE
  346. "%d CPU(s)"
  347. #endif
  348. },
  349. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
  350. #ifdef HAVE_AN_FPGA
  351. "/PGAs"
  352. #endif
  353. #ifdef WANT_CPUMINE
  354. "/CPUs"
  355. #endif
  356. },
  357. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  358. #ifdef HAVE_OPENCL
  359. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  360. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  361. #endif
  362. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  363. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  364. #ifdef HAVE_OPENCL
  365. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  366. #endif
  367. #ifdef HAVE_AN_FPGA
  368. { SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
  369. { SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
  370. { SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
  371. { SEVERITY_INFO, MSG_PGALRENA,PARAM_PGA, "PGA %d already enabled" },
  372. { SEVERITY_INFO, MSG_PGALRDIS,PARAM_PGA, "PGA %d already disabled" },
  373. { SEVERITY_INFO, MSG_PGAENA, PARAM_PGA, "PGA %d sent enable message" },
  374. { SEVERITY_INFO, MSG_PGADIS, PARAM_PGA, "PGA %d set disable flag" },
  375. { SEVERITY_ERR, MSG_PGAUNW, PARAM_PGA, "PGA %d is not flagged WELL, cannot enable" },
  376. #endif
  377. #ifdef WANT_CPUMINE
  378. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  379. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  380. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  381. #endif
  382. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  383. { SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
  384. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  385. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "BFGMiner versions" },
  386. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  387. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  388. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  389. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  390. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  391. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  392. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  393. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  394. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  395. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  396. { SEVERITY_SUCC, MSG_MINECONFIG,PARAM_NONE, "BFGMiner config" },
  397. #ifdef HAVE_OPENCL
  398. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  399. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported success" },
  400. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  401. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported success" },
  402. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  403. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported success" },
  404. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  405. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported success" },
  406. #endif
  407. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  408. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  409. { SEVERITY_SUCC, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  410. { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
  411. { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
  412. { SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
  413. { SEVERITY_SUCC, MSG_POOLPRIO,PARAM_NONE, "Changed pool priorities" },
  414. { SEVERITY_ERR, MSG_DUPPID, PARAM_PID, "Duplicate pool specified %d" },
  415. { SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
  416. { SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
  417. { SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
  418. { SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
  419. { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
  420. { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
  421. { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
  422. { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
  423. { SEVERITY_ERR, MSG_REMLASTP,PARAM_POOL, "Cannot remove last pool %d:'%s'" },
  424. { SEVERITY_ERR, MSG_ACTPOOL, PARAM_POOL, "Cannot remove active pool %d:'%s'" },
  425. { SEVERITY_SUCC, MSG_REMPOOL, PARAM_BOTH, "Removed pool %d:'%s'" },
  426. { SEVERITY_SUCC, MSG_NOTIFY, PARAM_NONE, "Notify" },
  427. { SEVERITY_SUCC, MSG_DEVDETAILS,PARAM_NONE, "Device Details" },
  428. { SEVERITY_SUCC, MSG_MINESTATS,PARAM_NONE, "BFGMiner stats" },
  429. { SEVERITY_ERR, MSG_MISCHK, PARAM_NONE, "Missing check cmd" },
  430. { SEVERITY_SUCC, MSG_CHECK, PARAM_NONE, "Check command" },
  431. { SEVERITY_ERR, MSG_MISBOOL, PARAM_NONE, "Missing parameter: true/false" },
  432. { SEVERITY_ERR, MSG_INVBOOL, PARAM_NONE, "Invalid parameter should be true or false" },
  433. { SEVERITY_SUCC, MSG_FOO, PARAM_BOOL, "Failover-Only set to %s" },
  434. { SEVERITY_SUCC, MSG_MINECOIN,PARAM_NONE, "BFGMiner coin" },
  435. { SEVERITY_SUCC, MSG_DEBUGSET,PARAM_NONE, "Debug settings" },
  436. #ifdef HAVE_AN_FPGA
  437. { SEVERITY_SUCC, MSG_PGAIDENT,PARAM_PGA, "Identify command sent to PGA%d" },
  438. { SEVERITY_WARN, MSG_PGANOID, PARAM_PGA, "PGA%d does not support identify" },
  439. #endif
  440. { SEVERITY_SUCC, MSG_SETCONFIG,PARAM_SET, "Set config '%s' to %d" },
  441. { SEVERITY_ERR, MSG_UNKCON, PARAM_STR, "Unknown config '%s'" },
  442. { SEVERITY_ERR, MSG_INVNUM, PARAM_BOTH, "Invalid number (%d) for '%s' range is 0-9999" },
  443. { SEVERITY_ERR, MSG_CONPAR, PARAM_NONE, "Missing config parameters 'name,N'" },
  444. { SEVERITY_ERR, MSG_CONVAL, PARAM_STR, "Missing config value N for '%s,N'" },
  445. #ifdef HAVE_AN_FPGA
  446. { SEVERITY_ERR, MSG_MISPGAOPT, PARAM_NONE, "Missing option after PGA number" },
  447. { SEVERITY_WARN, MSG_PGANOSET, PARAM_PGA, "PGA %d does not support pgaset" },
  448. { SEVERITY_INFO, MSG_PGAHELP, PARAM_BOTH, "PGA %d set help: %s" },
  449. { SEVERITY_SUCC, MSG_PGASETOK, PARAM_BOTH, "PGA %d set OK" },
  450. { SEVERITY_ERR, MSG_PGASETERR, PARAM_BOTH, "PGA %d set failed: %s" },
  451. #endif
  452. { SEVERITY_ERR, MSG_ZERMIS, PARAM_NONE, "Missing zero parameters" },
  453. { SEVERITY_ERR, MSG_ZERINV, PARAM_STR, "Invalid zero parameter '%s'" },
  454. { SEVERITY_SUCC, MSG_ZERSUM, PARAM_STR, "Zeroed %s stats with summary" },
  455. { SEVERITY_SUCC, MSG_ZERNOSUM, PARAM_STR, "Zeroed %s stats without summary" },
  456. { SEVERITY_SUCC, MSG_DEVSCAN, PARAM_COUNT, "Added %d new device(s)" },
  457. { SEVERITY_FAIL, 0, 0, NULL }
  458. };
  459. static int my_thr_id = 0;
  460. static bool bye;
  461. // Used to control quit restart access to shutdown variables
  462. static pthread_mutex_t quit_restart_lock;
  463. static bool do_a_quit;
  464. static bool do_a_restart;
  465. static time_t when = 0; // when the request occurred
  466. struct IP4ACCESS {
  467. in_addr_t ip;
  468. in_addr_t mask;
  469. char group;
  470. };
  471. #define GROUP(g) (toupper(g))
  472. #define PRIVGROUP GROUP('W')
  473. #define NOPRIVGROUP GROUP('R')
  474. #define ISPRIVGROUP(g) (GROUP(g) == PRIVGROUP)
  475. #define GROUPOFFSET(g) (GROUP(g) - GROUP('A'))
  476. #define VALIDGROUP(g) (GROUP(g) >= GROUP('A') && GROUP(g) <= GROUP('Z'))
  477. #define COMMANDS(g) (apigroups[GROUPOFFSET(g)].commands)
  478. #define DEFINEDGROUP(g) (ISPRIVGROUP(g) || COMMANDS(g) != NULL)
  479. struct APIGROUPS {
  480. // This becomes a string like: "|cmd1|cmd2|cmd3|" so it's quick to search
  481. char *commands;
  482. } apigroups['Z' - 'A' + 1]; // only A=0 to Z=25 (R: noprivs, W: allprivs)
  483. static struct IP4ACCESS *ipaccess = NULL;
  484. static int ips = 0;
  485. #ifdef HAVE_OPENCL
  486. extern struct device_drv opencl_api;
  487. #endif
  488. #ifdef WANT_CPUMINE
  489. extern struct device_drv cpu_drv;
  490. #endif
  491. struct io_data {
  492. size_t siz;
  493. char *ptr;
  494. char *cur;
  495. bool sock;
  496. bool full;
  497. bool close;
  498. };
  499. struct io_list {
  500. struct io_data *io_data;
  501. struct io_list *prev;
  502. struct io_list *next;
  503. };
  504. static struct io_list *io_head = NULL;
  505. #define io_new(init) _io_new(init, false)
  506. #define sock_io_new() _io_new(SOCKBUFSIZ, true)
  507. static void io_reinit(struct io_data *io_data)
  508. {
  509. io_data->cur = io_data->ptr;
  510. *(io_data->ptr) = '\0';
  511. io_data->full = false;
  512. io_data->close = false;
  513. }
  514. static struct io_data *_io_new(size_t initial, bool socket_buf)
  515. {
  516. struct io_data *io_data;
  517. struct io_list *io_list;
  518. io_data = malloc(sizeof(*io_data));
  519. io_data->ptr = malloc(initial);
  520. io_data->siz = initial;
  521. io_data->sock = socket_buf;
  522. io_reinit(io_data);
  523. io_list = malloc(sizeof(*io_list));
  524. io_list->io_data = io_data;
  525. if (io_head) {
  526. io_list->next = io_head;
  527. io_list->prev = io_head->prev;
  528. io_list->next->prev = io_list;
  529. io_list->prev->next = io_list;
  530. } else {
  531. io_list->prev = io_list;
  532. io_list->next = io_list;
  533. io_head = io_list;
  534. }
  535. return io_data;
  536. }
  537. static bool io_add(struct io_data *io_data, char *buf)
  538. {
  539. size_t len, dif, tot;
  540. if (io_data->full)
  541. return false;
  542. len = strlen(buf);
  543. dif = io_data->cur - io_data->ptr;
  544. tot = len + 1 + dif;
  545. if (tot > io_data->siz) {
  546. size_t new = io_data->siz * 2;
  547. if (new < tot)
  548. new = tot * 2;
  549. if (io_data->sock) {
  550. if (new > SOCKBUFSIZ) {
  551. if (tot > SOCKBUFSIZ) {
  552. io_data->full = true;
  553. return false;
  554. }
  555. new = SOCKBUFSIZ;
  556. }
  557. }
  558. io_data->ptr = realloc(io_data->ptr, new);
  559. io_data->cur = io_data->ptr + dif;
  560. io_data->siz = new;
  561. }
  562. memcpy(io_data->cur, buf, len + 1);
  563. io_data->cur += len;
  564. return true;
  565. }
  566. static bool io_put(struct io_data *io_data, char *buf)
  567. {
  568. io_reinit(io_data);
  569. return io_add(io_data, buf);
  570. }
  571. static void io_close(struct io_data *io_data)
  572. {
  573. io_data->close = true;
  574. }
  575. static void io_free()
  576. {
  577. struct io_list *io_list, *io_next;
  578. if (io_head) {
  579. io_list = io_head;
  580. do {
  581. io_next = io_list->next;
  582. free(io_list->io_data);
  583. free(io_list);
  584. io_list = io_next;
  585. } while (io_list != io_head);
  586. io_head = NULL;
  587. }
  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. root = api_add_string(root, "Coinbase-Sig", opt_coinbase_sig, true);
  1203. root = print_data(root, buf, isjson, false);
  1204. io_add(io_data, buf);
  1205. if (isjson && io_open)
  1206. io_close(io_data);
  1207. }
  1208. static const char*
  1209. bool2str(bool b)
  1210. {
  1211. return b ? YES : NO;
  1212. }
  1213. static const char *status2str(enum alive status)
  1214. {
  1215. switch (status) {
  1216. case LIFE_WELL:
  1217. return ALIVE;
  1218. case LIFE_SICK:
  1219. return SICK;
  1220. case LIFE_DEAD:
  1221. return DEAD;
  1222. case LIFE_NOSTART:
  1223. return NOSTART;
  1224. case LIFE_INIT:
  1225. return INIT;
  1226. case LIFE_WAIT:
  1227. return WAIT;
  1228. default:
  1229. return UNKNOWN;
  1230. }
  1231. }
  1232. static
  1233. struct api_data *api_add_device_identifier(struct api_data *root, struct cgpu_info *cgpu)
  1234. {
  1235. root = api_add_string(root, "Name", cgpu->drv->name, false);
  1236. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  1237. root = api_add_int(root, "ProcID", &(cgpu->proc_id), false);
  1238. return root;
  1239. }
  1240. static void devdetail_an(struct io_data *io_data, struct cgpu_info *cgpu, bool isjson, bool precom)
  1241. {
  1242. struct api_data *root = NULL;
  1243. char buf[TMPBUFSIZ];
  1244. int n = 0, i;
  1245. cgpu_utility(cgpu);
  1246. rd_lock(&devices_lock);
  1247. for (i = 0; i < total_devices; ++i) {
  1248. if (devices[i] == cgpu)
  1249. break;
  1250. if (cgpu->devtype == devices[i]->devtype)
  1251. ++n;
  1252. }
  1253. rd_unlock(&devices_lock);
  1254. root = api_add_int(root, (char*)cgpu->devtype, &n, true);
  1255. root = api_add_device_identifier(root, cgpu);
  1256. root = api_add_string(root, "Driver", cgpu->drv->dname, false);
  1257. if (cgpu->kname)
  1258. root = api_add_string(root, "Kernel", cgpu->kname, false);
  1259. if (cgpu->name)
  1260. root = api_add_string(root, "Model", cgpu->name, false);
  1261. if (cgpu->device_path)
  1262. root = api_add_string(root, "Device Path", cgpu->device_path, false);
  1263. if (cgpu->drv->get_api_extra_device_detail)
  1264. root = api_add_extra(root, cgpu->drv->get_api_extra_device_detail(cgpu));
  1265. root = print_data(root, buf, isjson, precom);
  1266. io_add(io_data, buf);
  1267. }
  1268. static void devstatus_an(struct io_data *io_data, struct cgpu_info *cgpu, bool isjson, bool precom)
  1269. {
  1270. struct api_data *root = NULL;
  1271. char buf[TMPBUFSIZ];
  1272. int n = 0, i;
  1273. cgpu_utility(cgpu);
  1274. rd_lock(&devices_lock);
  1275. for (i = 0; i < total_devices; ++i) {
  1276. if (devices[i] == cgpu)
  1277. break;
  1278. if (cgpu->devtype == devices[i]->devtype)
  1279. ++n;
  1280. }
  1281. rd_unlock(&devices_lock);
  1282. root = api_add_int(root, (char*)cgpu->devtype, &n, true);
  1283. root = api_add_device_identifier(root, cgpu);
  1284. root = api_add_string(root, "Enabled", bool2str(cgpu->deven != DEV_DISABLED), false);
  1285. root = api_add_string(root, "Status", status2str(cgpu->status), false);
  1286. if (cgpu->temp)
  1287. root = api_add_temp(root, "Temperature", &cgpu->temp, false);
  1288. double mhs = cgpu->total_mhashes / cgpu_runtime(cgpu);
  1289. root = api_add_mhs(root, "MHS av", &mhs, false);
  1290. char mhsname[27];
  1291. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1292. root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
  1293. root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
  1294. root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
  1295. root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
  1296. root = api_add_utility(root, "Utility", &(cgpu->utility), false);
  1297. int last_share_pool = cgpu->last_share_pool_time > 0 ?
  1298. cgpu->last_share_pool : -1;
  1299. root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
  1300. root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
  1301. root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
  1302. root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
  1303. root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
  1304. root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
  1305. root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
  1306. root = api_add_time(root, "Last Valid Work", &(cgpu->last_device_valid_work), false);
  1307. if (cgpu->drv->get_api_extra_device_status)
  1308. root = api_add_extra(root, cgpu->drv->get_api_extra_device_status(cgpu));
  1309. root = print_data(root, buf, isjson, precom);
  1310. io_add(io_data, buf);
  1311. }
  1312. #ifdef HAVE_OPENCL
  1313. static void gpustatus(struct io_data *io_data, int gpu, bool isjson, bool precom)
  1314. {
  1315. if (gpu < 0 || gpu >= nDevs)
  1316. return;
  1317. devstatus_an(io_data, &gpus[gpu], isjson, precom);
  1318. }
  1319. #endif
  1320. #ifdef HAVE_AN_FPGA
  1321. static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom)
  1322. {
  1323. int dev = pgadevice(pga);
  1324. if (dev < 0) // Should never happen
  1325. return;
  1326. devstatus_an(io_data, get_devices(dev), isjson, precom);
  1327. }
  1328. #endif
  1329. #ifdef WANT_CPUMINE
  1330. static void cpustatus(struct io_data *io_data, int cpu, bool isjson, bool precom)
  1331. {
  1332. if (opt_n_threads <= 0 || cpu < 0 || cpu >= num_processors)
  1333. return;
  1334. devstatus_an(io_data, &cpus[cpu], isjson, precom);
  1335. }
  1336. #endif
  1337. static void
  1338. 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)
  1339. {
  1340. bool io_open = false;
  1341. int i;
  1342. if (total_devices == 0) {
  1343. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  1344. return;
  1345. }
  1346. message(io_data, MSG_DEVS, 0, NULL, isjson);
  1347. if (isjson)
  1348. io_open = io_add(io_data, COMSTR JSON_DEVS);
  1349. for (i = 0; i < total_devices; ++i) {
  1350. func(io_data, get_devices(i), isjson, isjson && i > 0);
  1351. }
  1352. if (isjson && io_open)
  1353. io_close(io_data);
  1354. }
  1355. static void devdetail(struct io_data *io_data, SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1356. {
  1357. return devinfo_internal(devdetail_an, io_data, c, param, isjson, group);
  1358. }
  1359. static void devstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1360. {
  1361. return devinfo_internal(devstatus_an, io_data, c, param, isjson, group);
  1362. }
  1363. #ifdef HAVE_OPENCL
  1364. static void gpudev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1365. {
  1366. bool io_open = false;
  1367. int id;
  1368. if (nDevs == 0) {
  1369. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1370. return;
  1371. }
  1372. if (param == NULL || *param == '\0') {
  1373. message(io_data, MSG_MISID, 0, NULL, isjson);
  1374. return;
  1375. }
  1376. id = atoi(param);
  1377. if (id < 0 || id >= nDevs) {
  1378. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1379. return;
  1380. }
  1381. message(io_data, MSG_GPUDEV, id, NULL, isjson);
  1382. if (isjson)
  1383. io_open = io_add(io_data, COMSTR JSON_GPU);
  1384. gpustatus(io_data, id, isjson, false);
  1385. if (isjson && io_open)
  1386. io_close(io_data);
  1387. }
  1388. #endif
  1389. static void devscan(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1390. {
  1391. int n;
  1392. bool io_open = false;
  1393. applog(LOG_DEBUG, "RPC: request to scan %s for devices",
  1394. param);
  1395. if (param && !param[0])
  1396. param = NULL;
  1397. n = scan_serial(param);
  1398. message(io_data, MSG_DEVSCAN, n, NULL, isjson);
  1399. io_open = io_add(io_data, isjson ? COMSTR JSON_DEVS : _DEVS COMSTR);
  1400. n = total_devices - n;
  1401. for (int i = n; i < total_devices; ++i)
  1402. devdetail_an(io_data, get_devices(i), isjson, i > n);
  1403. if (isjson && io_open)
  1404. io_close(io_data);
  1405. }
  1406. #ifdef HAVE_AN_FPGA
  1407. static void pgadev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1408. {
  1409. bool io_open = false;
  1410. int numpga = numpgas();
  1411. int id;
  1412. if (numpga == 0) {
  1413. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1414. return;
  1415. }
  1416. if (param == NULL || *param == '\0') {
  1417. message(io_data, MSG_MISID, 0, NULL, isjson);
  1418. return;
  1419. }
  1420. id = atoi(param);
  1421. if (id < 0 || id >= numpga) {
  1422. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1423. return;
  1424. }
  1425. message(io_data, MSG_PGADEV, id, NULL, isjson);
  1426. if (isjson)
  1427. io_open = io_add(io_data, COMSTR JSON_PGA);
  1428. pgastatus(io_data, id, isjson, false);
  1429. if (isjson && io_open)
  1430. io_close(io_data);
  1431. }
  1432. static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1433. {
  1434. struct cgpu_info *cgpu;
  1435. int numpga = numpgas();
  1436. int id;
  1437. if (numpga == 0) {
  1438. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1439. return;
  1440. }
  1441. if (param == NULL || *param == '\0') {
  1442. message(io_data, MSG_MISID, 0, NULL, isjson);
  1443. return;
  1444. }
  1445. id = atoi(param);
  1446. if (id < 0 || id >= numpga) {
  1447. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1448. return;
  1449. }
  1450. int dev = pgadevice(id);
  1451. if (dev < 0) { // Should never happen
  1452. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1453. return;
  1454. }
  1455. cgpu = get_devices(dev);
  1456. applog(LOG_DEBUG, "API: request to pgaenable pgaid %d device %d %s",
  1457. id, dev, cgpu->proc_repr_ns);
  1458. if (cgpu->deven != DEV_DISABLED) {
  1459. message(io_data, MSG_PGALRENA, id, NULL, isjson);
  1460. return;
  1461. }
  1462. #if 0 /* A DISABLED device wont change status FIXME: should disabling make it WELL? */
  1463. if (cgpu->status != LIFE_WELL) {
  1464. message(io_data, MSG_PGAUNW, id, NULL, isjson);
  1465. return;
  1466. }
  1467. #endif
  1468. proc_enable(cgpu);
  1469. message(io_data, MSG_PGAENA, id, NULL, isjson);
  1470. }
  1471. static void pgadisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1472. {
  1473. struct cgpu_info *cgpu;
  1474. int numpga = numpgas();
  1475. int id;
  1476. if (numpga == 0) {
  1477. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1478. return;
  1479. }
  1480. if (param == NULL || *param == '\0') {
  1481. message(io_data, MSG_MISID, 0, NULL, isjson);
  1482. return;
  1483. }
  1484. id = atoi(param);
  1485. if (id < 0 || id >= numpga) {
  1486. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1487. return;
  1488. }
  1489. int dev = pgadevice(id);
  1490. if (dev < 0) { // Should never happen
  1491. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1492. return;
  1493. }
  1494. cgpu = get_devices(dev);
  1495. applog(LOG_DEBUG, "API: request to pgadisable pgaid %d device %d %s",
  1496. id, dev, cgpu->proc_repr_ns);
  1497. if (cgpu->deven == DEV_DISABLED) {
  1498. message(io_data, MSG_PGALRDIS, id, NULL, isjson);
  1499. return;
  1500. }
  1501. cgpu->deven = DEV_DISABLED;
  1502. message(io_data, MSG_PGADIS, id, NULL, isjson);
  1503. }
  1504. static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1505. {
  1506. struct cgpu_info *cgpu;
  1507. struct device_drv *drv;
  1508. int numpga = numpgas();
  1509. int id;
  1510. if (numpga == 0) {
  1511. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1512. return;
  1513. }
  1514. if (param == NULL || *param == '\0') {
  1515. message(io_data, MSG_MISID, 0, NULL, isjson);
  1516. return;
  1517. }
  1518. id = atoi(param);
  1519. if (id < 0 || id >= numpga) {
  1520. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1521. return;
  1522. }
  1523. int dev = pgadevice(id);
  1524. if (dev < 0) { // Should never happen
  1525. message(io_data, MSG_INVPGA, id, NULL, isjson);
  1526. return;
  1527. }
  1528. cgpu = get_devices(dev);
  1529. drv = cgpu->drv;
  1530. if (drv->identify_device && drv->identify_device(cgpu))
  1531. message(io_data, MSG_PGAIDENT, id, NULL, isjson);
  1532. else
  1533. message(io_data, MSG_PGANOID, id, NULL, isjson);
  1534. }
  1535. #endif
  1536. #ifdef WANT_CPUMINE
  1537. static void cpudev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1538. {
  1539. bool io_open = false;
  1540. int id;
  1541. if (opt_n_threads == 0) {
  1542. message(io_data, MSG_CPUNON, 0, NULL, isjson);
  1543. return;
  1544. }
  1545. if (param == NULL || *param == '\0') {
  1546. message(io_data, MSG_MISID, 0, NULL, isjson);
  1547. return;
  1548. }
  1549. id = atoi(param);
  1550. if (id < 0 || id >= num_processors) {
  1551. message(io_data, MSG_INVCPU, id, NULL, isjson);
  1552. return;
  1553. }
  1554. message(io_data, MSG_CPUDEV, id, NULL, isjson);
  1555. if (isjson)
  1556. io_open = io_add(io_data, COMSTR JSON_CPU);
  1557. cpustatus(io_data, id, isjson, false);
  1558. if (isjson && io_open)
  1559. io_close(io_data);
  1560. }
  1561. #endif
  1562. static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1563. {
  1564. struct api_data *root = NULL;
  1565. char buf[TMPBUFSIZ];
  1566. bool io_open = false;
  1567. char *status, *lp;
  1568. int i;
  1569. if (total_pools == 0) {
  1570. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1571. return;
  1572. }
  1573. message(io_data, MSG_POOL, 0, NULL, isjson);
  1574. if (isjson)
  1575. io_open = io_add(io_data, COMSTR JSON_POOLS);
  1576. for (i = 0; i < total_pools; i++) {
  1577. struct pool *pool = pools[i];
  1578. if (pool->removed)
  1579. continue;
  1580. switch (pool->enabled) {
  1581. case POOL_DISABLED:
  1582. status = (char *)DISABLED;
  1583. break;
  1584. case POOL_REJECTING:
  1585. status = (char *)REJECTING;
  1586. break;
  1587. case POOL_ENABLED:
  1588. if (pool->idle)
  1589. status = (char *)DEAD;
  1590. else
  1591. status = (char *)ALIVE;
  1592. break;
  1593. default:
  1594. status = (char *)UNKNOWN;
  1595. break;
  1596. }
  1597. if (pool->hdr_path)
  1598. lp = (char *)YES;
  1599. else
  1600. lp = (char *)NO;
  1601. root = api_add_int(root, "POOL", &i, false);
  1602. root = api_add_escape(root, "URL", pool->rpc_url, false);
  1603. root = api_add_string(root, "Status", status, false);
  1604. root = api_add_int(root, "Priority", &(pool->prio), false);
  1605. root = api_add_string(root, "Long Poll", lp, false);
  1606. root = api_add_uint(root, "Getworks", &(pool->getwork_requested), false);
  1607. root = api_add_int(root, "Accepted", &(pool->accepted), false);
  1608. root = api_add_int(root, "Rejected", &(pool->rejected), false);
  1609. root = api_add_uint(root, "Discarded", &(pool->discarded_work), false);
  1610. root = api_add_uint(root, "Stale", &(pool->stale_shares), false);
  1611. root = api_add_uint(root, "Get Failures", &(pool->getfail_occasions), false);
  1612. root = api_add_uint(root, "Remote Failures", &(pool->remotefail_occasions), false);
  1613. root = api_add_escape(root, "User", pool->rpc_user, false);
  1614. root = api_add_time(root, "Last Share Time", &(pool->last_share_time), false);
  1615. root = api_add_int(root, "Diff1 Shares", &(pool->diff1), false);
  1616. if (pool->rpc_proxy) {
  1617. root = api_add_escape(root, "Proxy", pool->rpc_proxy, false);
  1618. } else {
  1619. root = api_add_const(root, "Proxy", BLANK, false);
  1620. }
  1621. root = api_add_diff(root, "Difficulty Accepted", &(pool->diff_accepted), false);
  1622. root = api_add_diff(root, "Difficulty Rejected", &(pool->diff_rejected), false);
  1623. root = api_add_diff(root, "Difficulty Stale", &(pool->diff_stale), false);
  1624. root = api_add_diff(root, "Last Share Difficulty", &(pool->last_share_diff), false);
  1625. root = api_add_bool(root, "Has Stratum", &(pool->has_stratum), false);
  1626. root = api_add_bool(root, "Stratum Active", &(pool->stratum_active), false);
  1627. if (pool->stratum_active)
  1628. root = api_add_escape(root, "Stratum URL", pool->stratum_url, false);
  1629. else
  1630. root = api_add_const(root, "Stratum URL", BLANK, false);
  1631. root = api_add_uint64(root, "Best Share", &(pool->best_diff), true);
  1632. if (pool->admin_msg)
  1633. root = api_add_escape(root, "Message", pool->admin_msg, true);
  1634. root = print_data(root, buf, isjson, isjson && (i > 0));
  1635. io_add(io_data, buf);
  1636. }
  1637. if (isjson && io_open)
  1638. io_close(io_data);
  1639. }
  1640. static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1641. {
  1642. struct api_data *root = NULL;
  1643. char buf[TMPBUFSIZ];
  1644. bool io_open;
  1645. double utility, mhs, work_utility;
  1646. #ifdef WANT_CPUMINE
  1647. char *algo = (char *)(algo_names[opt_algo]);
  1648. if (algo == NULL)
  1649. algo = (char *)NULLSTR;
  1650. #endif
  1651. message(io_data, MSG_SUMM, 0, NULL, isjson);
  1652. io_open = io_add(io_data, isjson ? COMSTR JSON_SUMMARY : _SUMMARY COMSTR);
  1653. // stop hashmeter() changing some while copying
  1654. mutex_lock(&hash_lock);
  1655. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1656. mhs = total_mhashes_done / total_secs;
  1657. work_utility = total_diff1 / ( total_secs ? total_secs : 1 ) * 60;
  1658. root = api_add_elapsed(root, "Elapsed", &(total_secs), true);
  1659. #ifdef WANT_CPUMINE
  1660. if (opt_n_threads)
  1661. root = api_add_string(root, "Algorithm", algo, false);
  1662. #endif
  1663. root = api_add_mhs(root, "MHS av", &(mhs), false);
  1664. root = api_add_uint(root, "Found Blocks", &(found_blocks), true);
  1665. root = api_add_int(root, "Getworks", &(total_getworks), true);
  1666. root = api_add_int(root, "Accepted", &(total_accepted), true);
  1667. root = api_add_int(root, "Rejected", &(total_rejected), true);
  1668. root = api_add_int(root, "Hardware Errors", &(hw_errors), true);
  1669. root = api_add_utility(root, "Utility", &(utility), false);
  1670. root = api_add_int(root, "Discarded", &(total_discarded), true);
  1671. root = api_add_int(root, "Stale", &(total_stale), true);
  1672. root = api_add_uint(root, "Get Failures", &(total_go), true);
  1673. root = api_add_uint(root, "Local Work", &(local_work), true);
  1674. root = api_add_uint(root, "Remote Failures", &(total_ro), true);
  1675. root = api_add_uint(root, "Network Blocks", &(new_blocks), true);
  1676. root = api_add_mhtotal(root, "Total MH", &(total_mhashes_done), true);
  1677. root = api_add_utility(root, "Work Utility", &(work_utility), false);
  1678. root = api_add_diff(root, "Difficulty Accepted", &(total_diff_accepted), true);
  1679. root = api_add_diff(root, "Difficulty Rejected", &(total_diff_rejected), true);
  1680. root = api_add_diff(root, "Difficulty Stale", &(total_diff_stale), true);
  1681. root = api_add_uint64(root, "Best Share", &(best_diff), true);
  1682. mutex_unlock(&hash_lock);
  1683. root = print_data(root, buf, isjson, false);
  1684. io_add(io_data, buf);
  1685. if (isjson && io_open)
  1686. io_close(io_data);
  1687. }
  1688. #ifdef HAVE_OPENCL
  1689. static void gpuenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1690. {
  1691. int id;
  1692. if (gpu_threads == 0) {
  1693. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1694. return;
  1695. }
  1696. if (param == NULL || *param == '\0') {
  1697. message(io_data, MSG_MISID, 0, NULL, isjson);
  1698. return;
  1699. }
  1700. id = atoi(param);
  1701. if (id < 0 || id >= nDevs) {
  1702. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1703. return;
  1704. }
  1705. applog(LOG_DEBUG, "API: request to gpuenable gpuid %d %s",
  1706. id, gpus[id].proc_repr_ns);
  1707. if (gpus[id].deven != DEV_DISABLED) {
  1708. message(io_data, MSG_ALRENA, id, NULL, isjson);
  1709. return;
  1710. }
  1711. if (gpus[id].status != LIFE_WELL)
  1712. {
  1713. message(io_data, MSG_GPUMRE, id, NULL, isjson);
  1714. return;
  1715. }
  1716. proc_enable(&gpus[id]);
  1717. message(io_data, MSG_GPUREN, id, NULL, isjson);
  1718. }
  1719. static void gpudisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1720. {
  1721. int id;
  1722. if (nDevs == 0) {
  1723. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1724. return;
  1725. }
  1726. if (param == NULL || *param == '\0') {
  1727. message(io_data, MSG_MISID, 0, NULL, isjson);
  1728. return;
  1729. }
  1730. id = atoi(param);
  1731. if (id < 0 || id >= nDevs) {
  1732. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1733. return;
  1734. }
  1735. applog(LOG_DEBUG, "API: request to gpudisable gpuid %d %s",
  1736. id, gpus[id].proc_repr_ns);
  1737. if (gpus[id].deven == DEV_DISABLED) {
  1738. message(io_data, MSG_ALRDIS, id, NULL, isjson);
  1739. return;
  1740. }
  1741. gpus[id].deven = DEV_DISABLED;
  1742. message(io_data, MSG_GPUDIS, id, NULL, isjson);
  1743. }
  1744. static void gpurestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1745. {
  1746. int id;
  1747. if (nDevs == 0) {
  1748. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1749. return;
  1750. }
  1751. if (param == NULL || *param == '\0') {
  1752. message(io_data, MSG_MISID, 0, NULL, isjson);
  1753. return;
  1754. }
  1755. id = atoi(param);
  1756. if (id < 0 || id >= nDevs) {
  1757. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1758. return;
  1759. }
  1760. reinit_device(&gpus[id]);
  1761. message(io_data, MSG_GPUREI, id, NULL, isjson);
  1762. }
  1763. #endif
  1764. static void gpucount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1765. {
  1766. struct api_data *root = NULL;
  1767. char buf[TMPBUFSIZ];
  1768. bool io_open;
  1769. int numgpu = 0;
  1770. #ifdef HAVE_OPENCL
  1771. numgpu = nDevs;
  1772. #endif
  1773. message(io_data, MSG_NUMGPU, 0, NULL, isjson);
  1774. io_open = io_add(io_data, isjson ? COMSTR JSON_GPUS : _GPUS COMSTR);
  1775. root = api_add_int(root, "Count", &numgpu, false);
  1776. root = print_data(root, buf, isjson, false);
  1777. io_add(io_data, buf);
  1778. if (isjson && io_open)
  1779. io_close(io_data);
  1780. }
  1781. static void pgacount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1782. {
  1783. struct api_data *root = NULL;
  1784. char buf[TMPBUFSIZ];
  1785. bool io_open;
  1786. int count = 0;
  1787. #ifdef HAVE_AN_FPGA
  1788. count = numpgas();
  1789. #endif
  1790. message(io_data, MSG_NUMPGA, 0, NULL, isjson);
  1791. io_open = io_add(io_data, isjson ? COMSTR JSON_PGAS : _PGAS COMSTR);
  1792. root = api_add_int(root, "Count", &count, false);
  1793. root = print_data(root, buf, isjson, false);
  1794. io_add(io_data, buf);
  1795. if (isjson && io_open)
  1796. io_close(io_data);
  1797. }
  1798. static void cpucount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1799. {
  1800. struct api_data *root = NULL;
  1801. char buf[TMPBUFSIZ];
  1802. bool io_open;
  1803. int count = 0;
  1804. #ifdef WANT_CPUMINE
  1805. count = opt_n_threads > 0 ? num_processors : 0;
  1806. #endif
  1807. message(io_data, MSG_NUMCPU, 0, NULL, isjson);
  1808. io_open = io_add(io_data, isjson ? COMSTR JSON_CPUS : _CPUS COMSTR);
  1809. root = api_add_int(root, "Count", &count, false);
  1810. root = print_data(root, buf, isjson, false);
  1811. io_add(io_data, buf);
  1812. if (isjson && io_open)
  1813. io_close(io_data);
  1814. }
  1815. static void switchpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1816. {
  1817. struct pool *pool;
  1818. int id;
  1819. if (total_pools == 0) {
  1820. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1821. return;
  1822. }
  1823. if (param == NULL || *param == '\0') {
  1824. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1825. return;
  1826. }
  1827. id = atoi(param);
  1828. cg_rlock(&control_lock);
  1829. if (id < 0 || id >= total_pools) {
  1830. cg_runlock(&control_lock);
  1831. message(io_data, MSG_INVPID, id, NULL, isjson);
  1832. return;
  1833. }
  1834. pool = pools[id];
  1835. pool->enabled = POOL_ENABLED;
  1836. cg_runlock(&control_lock);
  1837. switch_pools(pool);
  1838. message(io_data, MSG_SWITCHP, id, NULL, isjson);
  1839. }
  1840. static void copyadvanceafter(char ch, char **param, char **buf)
  1841. {
  1842. #define src_p (*param)
  1843. #define dst_b (*buf)
  1844. while (*src_p && *src_p != ch) {
  1845. if (*src_p == '\\' && *(src_p+1) != '\0')
  1846. src_p++;
  1847. *(dst_b++) = *(src_p++);
  1848. }
  1849. if (*src_p)
  1850. src_p++;
  1851. *(dst_b++) = '\0';
  1852. }
  1853. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1854. {
  1855. char *ptr, *buf;
  1856. ptr = buf = malloc(strlen(param)+1);
  1857. if (unlikely(!buf))
  1858. quit(1, "Failed to malloc pooldetails buf");
  1859. *url = buf;
  1860. // copy url
  1861. copyadvanceafter(',', &param, &buf);
  1862. if (!(*param)) // missing user
  1863. goto exitsama;
  1864. *user = buf;
  1865. // copy user
  1866. copyadvanceafter(',', &param, &buf);
  1867. if (!*param) // missing pass
  1868. goto exitsama;
  1869. *pass = buf;
  1870. // copy pass
  1871. copyadvanceafter(',', &param, &buf);
  1872. return true;
  1873. exitsama:
  1874. free(ptr);
  1875. return false;
  1876. }
  1877. static void addpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1878. {
  1879. char *url, *user, *pass;
  1880. struct pool *pool;
  1881. char *ptr;
  1882. if (param == NULL || *param == '\0') {
  1883. message(io_data, MSG_MISPDP, 0, NULL, isjson);
  1884. return;
  1885. }
  1886. if (!pooldetails(param, &url, &user, &pass)) {
  1887. ptr = escape_string(param, isjson);
  1888. message(io_data, MSG_INVPDP, 0, ptr, isjson);
  1889. if (ptr != param)
  1890. free(ptr);
  1891. ptr = NULL;
  1892. return;
  1893. }
  1894. pool = add_pool();
  1895. detect_stratum(pool, url);
  1896. add_pool_details(pool, true, url, user, pass);
  1897. ptr = escape_string(url, isjson);
  1898. message(io_data, MSG_ADDPOOL, 0, ptr, isjson);
  1899. if (ptr != url)
  1900. free(ptr);
  1901. ptr = NULL;
  1902. }
  1903. static void enablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1904. {
  1905. struct pool *pool;
  1906. int id;
  1907. if (total_pools == 0) {
  1908. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1909. return;
  1910. }
  1911. if (param == NULL || *param == '\0') {
  1912. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1913. return;
  1914. }
  1915. id = atoi(param);
  1916. if (id < 0 || id >= total_pools) {
  1917. message(io_data, MSG_INVPID, id, NULL, isjson);
  1918. return;
  1919. }
  1920. pool = pools[id];
  1921. if (pool->enabled == POOL_ENABLED) {
  1922. message(io_data, MSG_ALRENAP, id, NULL, isjson);
  1923. return;
  1924. }
  1925. pool->enabled = POOL_ENABLED;
  1926. if (pool->prio < current_pool()->prio)
  1927. switch_pools(pool);
  1928. message(io_data, MSG_ENAPOOL, id, NULL, isjson);
  1929. }
  1930. static void poolpriority(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1931. {
  1932. int i;
  1933. switch (prioritize_pools(param, &i)) {
  1934. case MSG_NOPOOL:
  1935. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1936. return;
  1937. case MSG_MISPID:
  1938. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1939. return;
  1940. case MSG_INVPID:
  1941. message(io_data, MSG_INVPID, i, NULL, isjson);
  1942. return;
  1943. case MSG_DUPPID:
  1944. message(io_data, MSG_DUPPID, i, NULL, isjson);
  1945. return;
  1946. case MSG_POOLPRIO:
  1947. default:
  1948. message(io_data, MSG_POOLPRIO, 0, NULL, isjson);
  1949. return;
  1950. }
  1951. }
  1952. static void disablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1953. {
  1954. struct pool *pool;
  1955. int id;
  1956. if (total_pools == 0) {
  1957. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1958. return;
  1959. }
  1960. if (param == NULL || *param == '\0') {
  1961. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1962. return;
  1963. }
  1964. id = atoi(param);
  1965. if (id < 0 || id >= total_pools) {
  1966. message(io_data, MSG_INVPID, id, NULL, isjson);
  1967. return;
  1968. }
  1969. pool = pools[id];
  1970. if (pool->enabled == POOL_DISABLED) {
  1971. message(io_data, MSG_ALRDISP, id, NULL, isjson);
  1972. return;
  1973. }
  1974. if (enabled_pools <= 1) {
  1975. message(io_data, MSG_DISLASTP, id, NULL, isjson);
  1976. return;
  1977. }
  1978. pool->enabled = POOL_DISABLED;
  1979. if (pool == current_pool())
  1980. switch_pools(NULL);
  1981. message(io_data, MSG_DISPOOL, id, NULL, isjson);
  1982. }
  1983. static void removepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1984. {
  1985. struct pool *pool;
  1986. char *rpc_url;
  1987. bool dofree = false;
  1988. int id;
  1989. if (total_pools == 0) {
  1990. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1991. return;
  1992. }
  1993. if (param == NULL || *param == '\0') {
  1994. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1995. return;
  1996. }
  1997. id = atoi(param);
  1998. if (id < 0 || id >= total_pools) {
  1999. message(io_data, MSG_INVPID, id, NULL, isjson);
  2000. return;
  2001. }
  2002. if (total_pools <= 1) {
  2003. message(io_data, MSG_REMLASTP, id, NULL, isjson);
  2004. return;
  2005. }
  2006. pool = pools[id];
  2007. if (pool == current_pool())
  2008. switch_pools(NULL);
  2009. if (pool == current_pool()) {
  2010. message(io_data, MSG_ACTPOOL, id, NULL, isjson);
  2011. return;
  2012. }
  2013. pool->enabled = POOL_DISABLED;
  2014. rpc_url = escape_string(pool->rpc_url, isjson);
  2015. if (rpc_url != pool->rpc_url)
  2016. dofree = true;
  2017. remove_pool(pool);
  2018. message(io_data, MSG_REMPOOL, id, rpc_url, isjson);
  2019. if (dofree)
  2020. free(rpc_url);
  2021. rpc_url = NULL;
  2022. }
  2023. #ifdef HAVE_OPENCL
  2024. static bool splitgpuvalue(struct io_data *io_data, char *param, int *gpu, char **value, bool isjson)
  2025. {
  2026. int id;
  2027. char *gpusep;
  2028. if (nDevs == 0) {
  2029. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  2030. return false;
  2031. }
  2032. if (param == NULL || *param == '\0') {
  2033. message(io_data, MSG_MISID, 0, NULL, isjson);
  2034. return false;
  2035. }
  2036. gpusep = strchr(param, GPUSEP);
  2037. if (gpusep == NULL) {
  2038. message(io_data, MSG_MISVAL, 0, NULL, isjson);
  2039. return false;
  2040. }
  2041. *(gpusep++) = '\0';
  2042. id = atoi(param);
  2043. if (id < 0 || id >= nDevs) {
  2044. message(io_data, MSG_INVGPU, id, NULL, isjson);
  2045. return false;
  2046. }
  2047. *gpu = id;
  2048. *value = gpusep;
  2049. return true;
  2050. }
  2051. static void gpuintensity(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2052. {
  2053. int id;
  2054. char *value;
  2055. int intensity;
  2056. char intensitystr[7];
  2057. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2058. return;
  2059. if (!strncasecmp(value, DYNAMIC, 1)) {
  2060. gpus[id].dynamic = true;
  2061. strcpy(intensitystr, DYNAMIC);
  2062. }
  2063. else {
  2064. intensity = atoi(value);
  2065. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  2066. message(io_data, MSG_INVINT, 0, value, isjson);
  2067. return;
  2068. }
  2069. gpus[id].dynamic = false;
  2070. gpus[id].intensity = intensity;
  2071. sprintf(intensitystr, "%d", intensity);
  2072. }
  2073. message(io_data, MSG_GPUINT, id, intensitystr, isjson);
  2074. }
  2075. static void gpumem(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2076. {
  2077. #ifdef HAVE_ADL
  2078. int id;
  2079. char *value;
  2080. int clock;
  2081. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2082. return;
  2083. clock = atoi(value);
  2084. if (set_memoryclock(id, clock))
  2085. message(io_data, MSG_GPUMERR, id, value, isjson);
  2086. else
  2087. message(io_data, MSG_GPUMEM, id, value, isjson);
  2088. #else
  2089. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2090. #endif
  2091. }
  2092. static void gpuengine(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2093. {
  2094. #ifdef HAVE_ADL
  2095. int id;
  2096. char *value;
  2097. int clock;
  2098. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2099. return;
  2100. clock = atoi(value);
  2101. if (set_engineclock(id, clock))
  2102. message(io_data, MSG_GPUEERR, id, value, isjson);
  2103. else
  2104. message(io_data, MSG_GPUENG, id, value, isjson);
  2105. #else
  2106. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2107. #endif
  2108. }
  2109. static void gpufan(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2110. {
  2111. #ifdef HAVE_ADL
  2112. int id;
  2113. char *value;
  2114. int fan;
  2115. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2116. return;
  2117. fan = atoi(value);
  2118. if (set_fanspeed(id, fan))
  2119. message(io_data, MSG_GPUFERR, id, value, isjson);
  2120. else
  2121. message(io_data, MSG_GPUFAN, id, value, isjson);
  2122. #else
  2123. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2124. #endif
  2125. }
  2126. static void gpuvddc(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2127. {
  2128. #ifdef HAVE_ADL
  2129. int id;
  2130. char *value;
  2131. float vddc;
  2132. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2133. return;
  2134. vddc = atof(value);
  2135. if (set_vddc(id, vddc))
  2136. message(io_data, MSG_GPUVERR, id, value, isjson);
  2137. else
  2138. message(io_data, MSG_GPUVDDC, id, value, isjson);
  2139. #else
  2140. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2141. #endif
  2142. }
  2143. #endif
  2144. void doquit(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2145. {
  2146. if (isjson)
  2147. io_put(io_data, JSON_START JSON_BYE);
  2148. else
  2149. io_put(io_data, _BYE);
  2150. bye = true;
  2151. do_a_quit = true;
  2152. }
  2153. void dorestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2154. {
  2155. if (isjson)
  2156. io_put(io_data, JSON_START JSON_RESTART);
  2157. else
  2158. io_put(io_data, _RESTART);
  2159. bye = true;
  2160. do_a_restart = true;
  2161. }
  2162. void privileged(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2163. {
  2164. message(io_data, MSG_ACCOK, 0, NULL, isjson);
  2165. }
  2166. void notifystatus(struct io_data *io_data, int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
  2167. {
  2168. struct api_data *root = NULL;
  2169. char buf[TMPBUFSIZ];
  2170. char *reason;
  2171. if (cgpu->device_last_not_well == 0)
  2172. reason = REASON_NONE;
  2173. else
  2174. switch(cgpu->device_not_well_reason) {
  2175. case REASON_THREAD_FAIL_INIT:
  2176. reason = REASON_THREAD_FAIL_INIT_STR;
  2177. break;
  2178. case REASON_THREAD_ZERO_HASH:
  2179. reason = REASON_THREAD_ZERO_HASH_STR;
  2180. break;
  2181. case REASON_THREAD_FAIL_QUEUE:
  2182. reason = REASON_THREAD_FAIL_QUEUE_STR;
  2183. break;
  2184. case REASON_DEV_SICK_IDLE_60:
  2185. reason = REASON_DEV_SICK_IDLE_60_STR;
  2186. break;
  2187. case REASON_DEV_DEAD_IDLE_600:
  2188. reason = REASON_DEV_DEAD_IDLE_600_STR;
  2189. break;
  2190. case REASON_DEV_NOSTART:
  2191. reason = REASON_DEV_NOSTART_STR;
  2192. break;
  2193. case REASON_DEV_OVER_HEAT:
  2194. reason = REASON_DEV_OVER_HEAT_STR;
  2195. break;
  2196. case REASON_DEV_THERMAL_CUTOFF:
  2197. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  2198. break;
  2199. case REASON_DEV_COMMS_ERROR:
  2200. reason = REASON_DEV_COMMS_ERROR_STR;
  2201. break;
  2202. default:
  2203. reason = REASON_UNKNOWN_STR;
  2204. break;
  2205. }
  2206. // ALL counters (and only counters) must start the name with a '*'
  2207. // Simplifies future external support for identifying new counters
  2208. root = api_add_int(root, "NOTIFY", &device, false);
  2209. root = api_add_device_identifier(root, cgpu);
  2210. root = api_add_time(root, "Last Well", &(cgpu->device_last_well), false);
  2211. root = api_add_time(root, "Last Not Well", &(cgpu->device_last_not_well), false);
  2212. root = api_add_string(root, "Reason Not Well", reason, false);
  2213. root = api_add_int(root, "*Thread Fail Init", &(cgpu->thread_fail_init_count), false);
  2214. root = api_add_int(root, "*Thread Zero Hash", &(cgpu->thread_zero_hash_count), false);
  2215. root = api_add_int(root, "*Thread Fail Queue", &(cgpu->thread_fail_queue_count), false);
  2216. root = api_add_int(root, "*Dev Sick Idle 60s", &(cgpu->dev_sick_idle_60_count), false);
  2217. root = api_add_int(root, "*Dev Dead Idle 600s", &(cgpu->dev_dead_idle_600_count), false);
  2218. root = api_add_int(root, "*Dev Nostart", &(cgpu->dev_nostart_count), false);
  2219. root = api_add_int(root, "*Dev Over Heat", &(cgpu->dev_over_heat_count), false);
  2220. root = api_add_int(root, "*Dev Thermal Cutoff", &(cgpu->dev_thermal_cutoff_count), false);
  2221. root = api_add_int(root, "*Dev Comms Error", &(cgpu->dev_comms_error_count), false);
  2222. root = api_add_int(root, "*Dev Throttle", &(cgpu->dev_throttle_count), false);
  2223. root = print_data(root, buf, isjson, isjson && (device > 0));
  2224. io_add(io_data, buf);
  2225. }
  2226. static void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
  2227. {
  2228. struct cgpu_info *cgpu;
  2229. bool io_open = false;
  2230. int i;
  2231. if (total_devices == 0) {
  2232. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  2233. return;
  2234. }
  2235. message(io_data, MSG_NOTIFY, 0, NULL, isjson);
  2236. if (isjson)
  2237. io_open = io_add(io_data, COMSTR JSON_NOTIFY);
  2238. for (i = 0; i < total_devices; i++) {
  2239. cgpu = get_devices(i);
  2240. notifystatus(io_data, i, cgpu, isjson, group);
  2241. }
  2242. if (isjson && io_open)
  2243. io_close(io_data);
  2244. }
  2245. static void devdetails(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2246. {
  2247. struct api_data *root = NULL;
  2248. char buf[TMPBUFSIZ];
  2249. bool io_open = false;
  2250. struct cgpu_info *cgpu;
  2251. int i;
  2252. if (total_devices == 0) {
  2253. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  2254. return;
  2255. }
  2256. message(io_data, MSG_DEVDETAILS, 0, NULL, isjson);
  2257. if (isjson)
  2258. io_open = io_add(io_data, COMSTR JSON_DEVDETAILS);
  2259. for (i = 0; i < total_devices; i++) {
  2260. cgpu = get_devices(i);
  2261. root = api_add_int(root, "DEVDETAILS", &i, false);
  2262. root = api_add_device_identifier(root, cgpu);
  2263. root = api_add_string(root, "Driver", cgpu->drv->dname, false);
  2264. root = api_add_const(root, "Kernel", cgpu->kname ? : BLANK, false);
  2265. root = api_add_const(root, "Model", cgpu->name ? : BLANK, false);
  2266. root = api_add_const(root, "Device Path", cgpu->device_path ? : BLANK, false);
  2267. root = print_data(root, buf, isjson, isjson && (i > 0));
  2268. io_add(io_data, buf);
  2269. }
  2270. if (isjson && io_open)
  2271. io_close(io_data);
  2272. }
  2273. void dosave(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2274. {
  2275. char filename[PATH_MAX];
  2276. FILE *fcfg;
  2277. char *ptr;
  2278. if (param == NULL || *param == '\0') {
  2279. default_save_file(filename);
  2280. param = filename;
  2281. }
  2282. fcfg = fopen(param, "w");
  2283. if (!fcfg) {
  2284. ptr = escape_string(param, isjson);
  2285. message(io_data, MSG_BADFN, 0, ptr, isjson);
  2286. if (ptr != param)
  2287. free(ptr);
  2288. ptr = NULL;
  2289. return;
  2290. }
  2291. write_config(fcfg);
  2292. fclose(fcfg);
  2293. ptr = escape_string(param, isjson);
  2294. message(io_data, MSG_SAVED, 0, ptr, isjson);
  2295. if (ptr != param)
  2296. free(ptr);
  2297. ptr = NULL;
  2298. }
  2299. 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)
  2300. {
  2301. struct api_data *root = NULL;
  2302. char buf[TMPBUFSIZ];
  2303. double elapsed;
  2304. root = api_add_int(root, "STATS", &i, false);
  2305. root = api_add_string(root, "ID", id, false);
  2306. elapsed = stats_elapsed(stats);
  2307. root = api_add_elapsed(root, "Elapsed", &elapsed, false);
  2308. root = api_add_uint32(root, "Calls", &(stats->getwork_calls), false);
  2309. root = api_add_timeval(root, "Wait", &(stats->getwork_wait), false);
  2310. root = api_add_timeval(root, "Max", &(stats->getwork_wait_max), false);
  2311. root = api_add_timeval(root, "Min", &(stats->getwork_wait_min), false);
  2312. if (pool_stats) {
  2313. root = api_add_uint32(root, "Pool Calls", &(pool_stats->getwork_calls), false);
  2314. root = api_add_uint32(root, "Pool Attempts", &(pool_stats->getwork_attempts), false);
  2315. root = api_add_timeval(root, "Pool Wait", &(pool_stats->getwork_wait), false);
  2316. root = api_add_timeval(root, "Pool Max", &(pool_stats->getwork_wait_max), false);
  2317. root = api_add_timeval(root, "Pool Min", &(pool_stats->getwork_wait_min), false);
  2318. root = api_add_double(root, "Pool Av", &(pool_stats->getwork_wait_rolling), false);
  2319. root = api_add_bool(root, "Work Had Roll Time", &(pool_stats->hadrolltime), false);
  2320. root = api_add_bool(root, "Work Can Roll", &(pool_stats->canroll), false);
  2321. root = api_add_bool(root, "Work Had Expire", &(pool_stats->hadexpire), false);
  2322. root = api_add_uint32(root, "Work Roll Time", &(pool_stats->rolltime), false);
  2323. root = api_add_diff(root, "Work Diff", &(pool_stats->last_diff), false);
  2324. root = api_add_diff(root, "Min Diff", &(pool_stats->min_diff), false);
  2325. root = api_add_diff(root, "Max Diff", &(pool_stats->max_diff), false);
  2326. root = api_add_uint32(root, "Min Diff Count", &(pool_stats->min_diff_count), false);
  2327. root = api_add_uint32(root, "Max Diff Count", &(pool_stats->max_diff_count), false);
  2328. root = api_add_uint64(root, "Times Sent", &(pool_stats->times_sent), false);
  2329. root = api_add_uint64(root, "Bytes Sent", &(pool_stats->bytes_sent), false);
  2330. root = api_add_uint64(root, "Times Recv", &(pool_stats->times_received), false);
  2331. root = api_add_uint64(root, "Bytes Recv", &(pool_stats->bytes_received), false);
  2332. root = api_add_uint64(root, "Net Bytes Sent", &(pool_stats->net_bytes_sent), false);
  2333. root = api_add_uint64(root, "Net Bytes Recv", &(pool_stats->net_bytes_received), false);
  2334. }
  2335. if (extra)
  2336. root = api_add_extra(root, extra);
  2337. root = print_data(root, buf, isjson, isjson && (i > 0));
  2338. io_add(io_data, buf);
  2339. return ++i;
  2340. }
  2341. static void minerstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2342. {
  2343. struct cgpu_info *cgpu;
  2344. bool io_open = false;
  2345. struct api_data *extra;
  2346. char id[20];
  2347. int i, j;
  2348. message(io_data, MSG_MINESTATS, 0, NULL, isjson);
  2349. if (isjson)
  2350. io_open = io_add(io_data, COMSTR JSON_MINESTATS);
  2351. i = 0;
  2352. for (j = 0; j < total_devices; j++) {
  2353. cgpu = get_devices(j);
  2354. if (cgpu && cgpu->drv) {
  2355. if (cgpu->drv->get_api_stats)
  2356. extra = cgpu->drv->get_api_stats(cgpu);
  2357. else
  2358. extra = NULL;
  2359. i = itemstats(io_data, i, cgpu->proc_repr_ns, &(cgpu->cgminer_stats), NULL, extra, isjson);
  2360. }
  2361. }
  2362. for (j = 0; j < total_pools; j++) {
  2363. struct pool *pool = pools[j];
  2364. sprintf(id, "POOL%d", j);
  2365. i = itemstats(io_data, i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, isjson);
  2366. }
  2367. if (isjson && io_open)
  2368. io_close(io_data);
  2369. }
  2370. static void failoveronly(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2371. {
  2372. if (param == NULL || *param == '\0') {
  2373. message(io_data, MSG_MISBOOL, 0, NULL, isjson);
  2374. return;
  2375. }
  2376. *param = tolower(*param);
  2377. if (*param != 't' && *param != 'f') {
  2378. message(io_data, MSG_INVBOOL, 0, NULL, isjson);
  2379. return;
  2380. }
  2381. bool tf = (*param == 't');
  2382. opt_fail_only = tf;
  2383. message(io_data, MSG_FOO, tf, NULL, isjson);
  2384. }
  2385. static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2386. {
  2387. struct api_data *root = NULL;
  2388. char buf[TMPBUFSIZ];
  2389. bool io_open;
  2390. message(io_data, MSG_MINECOIN, 0, NULL, isjson);
  2391. io_open = io_add(io_data, isjson ? COMSTR JSON_MINECOIN : _MINECOIN COMSTR);
  2392. #ifdef USE_SCRYPT
  2393. if (opt_scrypt)
  2394. root = api_add_const(root, "Hash Method", SCRYPTSTR, false);
  2395. else
  2396. #endif
  2397. root = api_add_const(root, "Hash Method", SHA256STR, false);
  2398. cg_rlock(&ch_lock);
  2399. if (current_fullhash && *current_fullhash) {
  2400. root = api_add_timeval(root, "Current Block Time", &block_timeval, true);
  2401. root = api_add_string(root, "Current Block Hash", current_fullhash, true);
  2402. } else {
  2403. struct timeval t = {0,0};
  2404. root = api_add_timeval(root, "Current Block Time", &t, true);
  2405. root = api_add_const(root, "Current Block Hash", BLANK, false);
  2406. }
  2407. cg_runlock(&ch_lock);
  2408. root = api_add_bool(root, "LP", &have_longpoll, false);
  2409. root = api_add_diff(root, "Network Difficulty", &current_diff, true);
  2410. root = print_data(root, buf, isjson, false);
  2411. io_add(io_data, buf);
  2412. if (isjson && io_open)
  2413. io_close(io_data);
  2414. }
  2415. static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2416. {
  2417. struct api_data *root = NULL;
  2418. char buf[TMPBUFSIZ];
  2419. bool io_open;
  2420. if (param == NULL)
  2421. param = (char *)BLANK;
  2422. else
  2423. *param = tolower(*param);
  2424. switch(*param) {
  2425. case 's':
  2426. opt_realquiet = true;
  2427. break;
  2428. case 'q':
  2429. opt_quiet ^= true;
  2430. break;
  2431. case 'v':
  2432. opt_log_output ^= true;
  2433. if (opt_log_output)
  2434. opt_quiet = false;
  2435. break;
  2436. case 'd':
  2437. opt_debug ^= true;
  2438. opt_log_output = opt_debug;
  2439. if (opt_debug)
  2440. opt_quiet = false;
  2441. break;
  2442. case 'r':
  2443. opt_protocol ^= true;
  2444. if (opt_protocol)
  2445. opt_quiet = false;
  2446. break;
  2447. case 'p':
  2448. want_per_device_stats ^= true;
  2449. opt_log_output = want_per_device_stats;
  2450. break;
  2451. case 'n':
  2452. opt_log_output = false;
  2453. opt_debug = false;
  2454. opt_quiet = false;
  2455. opt_protocol = false;
  2456. want_per_device_stats = false;
  2457. opt_worktime = false;
  2458. break;
  2459. case 'w':
  2460. opt_worktime ^= true;
  2461. break;
  2462. #ifdef _MEMORY_DEBUG
  2463. case 'y':
  2464. cgmemspeedup();
  2465. break;
  2466. case 'z':
  2467. cgmemrpt();
  2468. break;
  2469. #endif
  2470. default:
  2471. // anything else just reports the settings
  2472. break;
  2473. }
  2474. message(io_data, MSG_DEBUGSET, 0, NULL, isjson);
  2475. io_open = io_add(io_data, isjson ? COMSTR JSON_DEBUGSET : _DEBUGSET COMSTR);
  2476. root = api_add_bool(root, "Silent", &opt_realquiet, false);
  2477. root = api_add_bool(root, "Quiet", &opt_quiet, false);
  2478. root = api_add_bool(root, "Verbose", &opt_log_output, false);
  2479. root = api_add_bool(root, "Debug", &opt_debug, false);
  2480. root = api_add_bool(root, "RPCProto", &opt_protocol, false);
  2481. root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
  2482. root = api_add_bool(root, "WorkTime", &opt_worktime, false);
  2483. root = print_data(root, buf, isjson, false);
  2484. io_add(io_data, buf);
  2485. if (isjson && io_open)
  2486. io_close(io_data);
  2487. }
  2488. static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2489. {
  2490. char *comma;
  2491. int value;
  2492. if (param == NULL || *param == '\0') {
  2493. message(io_data, MSG_CONPAR, 0, NULL, isjson);
  2494. return;
  2495. }
  2496. comma = strchr(param, ',');
  2497. if (!comma) {
  2498. message(io_data, MSG_CONVAL, 0, param, isjson);
  2499. return;
  2500. }
  2501. *(comma++) = '\0';
  2502. if (strcasecmp(param, "coinbase-sig") == 0) {
  2503. free(opt_coinbase_sig);
  2504. opt_coinbase_sig = strdup(comma);
  2505. message(io_data, MSG_SETCONFIG, 1, param, isjson);
  2506. return;
  2507. }
  2508. value = atoi(comma);
  2509. if (value < 0 || value > 9999) {
  2510. message(io_data, MSG_INVNUM, value, param, isjson);
  2511. return;
  2512. }
  2513. if (strcasecmp(param, "queue") == 0)
  2514. opt_queue = value;
  2515. else if (strcasecmp(param, "scantime") == 0)
  2516. opt_scantime = value;
  2517. else if (strcasecmp(param, "expiry") == 0)
  2518. opt_expiry = value;
  2519. else {
  2520. message(io_data, MSG_UNKCON, 0, param, isjson);
  2521. return;
  2522. }
  2523. message(io_data, MSG_SETCONFIG, value, param, isjson);
  2524. }
  2525. #ifdef HAVE_AN_FPGA
  2526. static void pgaset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2527. {
  2528. struct cgpu_info *cgpu;
  2529. struct device_drv *drv;
  2530. char buf[TMPBUFSIZ];
  2531. int numpga = numpgas();
  2532. if (numpga == 0) {
  2533. message(io_data, MSG_PGANON, 0, NULL, isjson);
  2534. return;
  2535. }
  2536. if (param == NULL || *param == '\0') {
  2537. message(io_data, MSG_MISID, 0, NULL, isjson);
  2538. return;
  2539. }
  2540. char *opt = strchr(param, ',');
  2541. if (opt)
  2542. *(opt++) = '\0';
  2543. if (!opt || !*opt) {
  2544. message(io_data, MSG_MISPGAOPT, 0, NULL, isjson);
  2545. return;
  2546. }
  2547. int id = atoi(param);
  2548. if (id < 0 || id >= numpga) {
  2549. message(io_data, MSG_INVPGA, id, NULL, isjson);
  2550. return;
  2551. }
  2552. int dev = pgadevice(id);
  2553. if (dev < 0) { // Should never happen
  2554. message(io_data, MSG_INVPGA, id, NULL, isjson);
  2555. return;
  2556. }
  2557. cgpu = get_devices(dev);
  2558. drv = cgpu->drv;
  2559. char *set = strchr(opt, ',');
  2560. if (set)
  2561. *(set++) = '\0';
  2562. if (!drv->set_device)
  2563. message(io_data, MSG_PGANOSET, id, NULL, isjson);
  2564. else {
  2565. char *ret = drv->set_device(cgpu, opt, set, buf);
  2566. if (ret) {
  2567. if (strcasecmp(opt, "help") == 0)
  2568. message(io_data, MSG_PGAHELP, id, ret, isjson);
  2569. else
  2570. message(io_data, MSG_PGASETERR, id, ret, isjson);
  2571. } else
  2572. message(io_data, MSG_PGASETOK, id, NULL, isjson);
  2573. }
  2574. }
  2575. #endif
  2576. static void dozero(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2577. {
  2578. if (param == NULL || *param == '\0') {
  2579. message(io_data, MSG_ZERMIS, 0, NULL, isjson);
  2580. return;
  2581. }
  2582. char *sum = strchr(param, ',');
  2583. if (sum)
  2584. *(sum++) = '\0';
  2585. if (!sum || !*sum) {
  2586. message(io_data, MSG_MISBOOL, 0, NULL, isjson);
  2587. return;
  2588. }
  2589. bool all = false;
  2590. bool bs = false;
  2591. if (strcasecmp(param, "all") == 0)
  2592. all = true;
  2593. else if (strcasecmp(param, "bestshare") == 0)
  2594. bs = true;
  2595. if (all == false && bs == false) {
  2596. message(io_data, MSG_ZERINV, 0, param, isjson);
  2597. return;
  2598. }
  2599. *sum = tolower(*sum);
  2600. if (*sum != 't' && *sum != 'f') {
  2601. message(io_data, MSG_INVBOOL, 0, NULL, isjson);
  2602. return;
  2603. }
  2604. bool dosum = (*sum == 't');
  2605. if (dosum)
  2606. print_summary();
  2607. if (all)
  2608. zero_stats();
  2609. if (bs)
  2610. zero_bestshare();
  2611. if (dosum)
  2612. message(io_data, MSG_ZERSUM, 0, all ? "All" : "BestShare", isjson);
  2613. else
  2614. message(io_data, MSG_ZERNOSUM, 0, all ? "All" : "BestShare", isjson);
  2615. }
  2616. static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group);
  2617. struct CMDS {
  2618. char *name;
  2619. void (*func)(struct io_data *, SOCKETTYPE, char *, bool, char);
  2620. bool iswritemode;
  2621. } cmds[] = {
  2622. { "version", apiversion, false },
  2623. { "config", minerconfig, false },
  2624. { "devscan", devscan, true },
  2625. { "devs", devstatus, false },
  2626. { "devdetail", devdetail, false },
  2627. { "pools", poolstatus, false },
  2628. { "summary", summary, false },
  2629. #ifdef HAVE_OPENCL
  2630. { "gpuenable", gpuenable, true },
  2631. { "gpudisable", gpudisable, true },
  2632. { "gpurestart", gpurestart, true },
  2633. { "gpu", gpudev, false },
  2634. #endif
  2635. #ifdef HAVE_AN_FPGA
  2636. { "pga", pgadev, false },
  2637. { "pgaenable", pgaenable, true },
  2638. { "pgadisable", pgadisable, true },
  2639. { "pgaidentify", pgaidentify, true },
  2640. #endif
  2641. #ifdef WANT_CPUMINE
  2642. { "cpu", cpudev, false },
  2643. #endif
  2644. { "gpucount", gpucount, false },
  2645. { "pgacount", pgacount, false },
  2646. { "cpucount", cpucount, false },
  2647. { "switchpool", switchpool, true },
  2648. { "addpool", addpool, true },
  2649. { "poolpriority", poolpriority, true },
  2650. { "enablepool", enablepool, true },
  2651. { "disablepool", disablepool, true },
  2652. { "removepool", removepool, true },
  2653. #ifdef HAVE_OPENCL
  2654. { "gpuintensity", gpuintensity, true },
  2655. { "gpumem", gpumem, true },
  2656. { "gpuengine", gpuengine, true },
  2657. { "gpufan", gpufan, true },
  2658. { "gpuvddc", gpuvddc, true },
  2659. #endif
  2660. { "save", dosave, true },
  2661. { "quit", doquit, true },
  2662. { "privileged", privileged, true },
  2663. { "notify", notify, false },
  2664. { "devdetails", devdetails, false },
  2665. { "restart", dorestart, true },
  2666. { "stats", minerstats, false },
  2667. { "check", checkcommand, false },
  2668. { "failover-only", failoveronly, true },
  2669. { "coin", minecoin, false },
  2670. { "debug", debugstate, true },
  2671. { "setconfig", setconfig, true },
  2672. #ifdef HAVE_AN_FPGA
  2673. { "pgaset", pgaset, true },
  2674. #endif
  2675. { "zero", dozero, true },
  2676. { NULL, NULL, false }
  2677. };
  2678. static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
  2679. {
  2680. struct api_data *root = NULL;
  2681. char buf[TMPBUFSIZ];
  2682. bool io_open;
  2683. char cmdbuf[100];
  2684. bool found, access;
  2685. int i;
  2686. if (param == NULL || *param == '\0') {
  2687. message(io_data, MSG_MISCHK, 0, NULL, isjson);
  2688. return;
  2689. }
  2690. found = false;
  2691. access = false;
  2692. for (i = 0; cmds[i].name != NULL; i++) {
  2693. if (strcmp(cmds[i].name, param) == 0) {
  2694. found = true;
  2695. sprintf(cmdbuf, "|%s|", param);
  2696. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  2697. access = true;
  2698. break;
  2699. }
  2700. }
  2701. message(io_data, MSG_CHECK, 0, NULL, isjson);
  2702. io_open = io_add(io_data, isjson ? COMSTR JSON_CHECK : _CHECK COMSTR);
  2703. root = api_add_const(root, "Exists", found ? YES : NO, false);
  2704. root = api_add_const(root, "Access", access ? YES : NO, false);
  2705. root = print_data(root, buf, isjson, false);
  2706. io_add(io_data, buf);
  2707. if (isjson && io_open)
  2708. io_close(io_data);
  2709. }
  2710. static void send_result(struct io_data *io_data, SOCKETTYPE c, bool isjson)
  2711. {
  2712. char buf[SOCKBUFSIZ + sizeof(JSON_CLOSE) + sizeof(JSON_END)];
  2713. int count, res, tosend, len, n;
  2714. strcpy(buf, io_data->ptr);
  2715. if (io_data->close)
  2716. strcat(buf, JSON_CLOSE);
  2717. if (isjson) {
  2718. if (io_data->full)
  2719. strcat(buf, JSON_END_TRUNCATED);
  2720. else
  2721. strcat(buf, JSON_END);
  2722. }
  2723. len = strlen(buf);
  2724. tosend = len+1;
  2725. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", tosend, buf, len > 10 ? "..." : BLANK);
  2726. count = 0;
  2727. while (count++ < 5 && tosend > 0) {
  2728. // allow 50ms per attempt
  2729. struct timeval timeout = {0, 50000};
  2730. fd_set wd;
  2731. FD_ZERO(&wd);
  2732. FD_SET(c, &wd);
  2733. if ((res = select(c + 1, NULL, &wd, NULL, &timeout)) < 1) {
  2734. applog(LOG_WARNING, "API: send select failed (%d)", res);
  2735. return;
  2736. }
  2737. n = send(c, buf, tosend, 0);
  2738. if (SOCKETFAIL(n)) {
  2739. if (sock_blocks())
  2740. continue;
  2741. applog(LOG_WARNING, "API: send (%d) failed: %s", tosend, SOCKERRMSG);
  2742. return;
  2743. } else {
  2744. if (count <= 1) {
  2745. if (n == tosend)
  2746. applog(LOG_DEBUG, "API: sent all of %d first go", tosend);
  2747. else
  2748. applog(LOG_DEBUG, "API: sent %d of %d first go", n, tosend);
  2749. } else {
  2750. if (n == tosend)
  2751. applog(LOG_DEBUG, "API: sent all of remaining %d (count=%d)", tosend, count);
  2752. else
  2753. applog(LOG_DEBUG, "API: sent %d of remaining %d (count=%d)", n, tosend, count);
  2754. }
  2755. tosend -= n;
  2756. }
  2757. }
  2758. }
  2759. static void tidyup(__maybe_unused void *arg)
  2760. {
  2761. mutex_lock(&quit_restart_lock);
  2762. SOCKETTYPE *apisock = (SOCKETTYPE *)arg;
  2763. bye = true;
  2764. if (*apisock != INVSOCK) {
  2765. shutdown(*apisock, SHUT_RDWR);
  2766. CLOSESOCKET(*apisock);
  2767. *apisock = INVSOCK;
  2768. }
  2769. if (ipaccess != NULL) {
  2770. free(ipaccess);
  2771. ipaccess = NULL;
  2772. }
  2773. io_free();
  2774. mutex_unlock(&quit_restart_lock);
  2775. }
  2776. /*
  2777. * Interpret --api-groups G:cmd1:cmd2:cmd3,P:cmd4,*,...
  2778. */
  2779. static void setup_groups()
  2780. {
  2781. char *api_groups = opt_api_groups ? opt_api_groups : (char *)BLANK;
  2782. char *buf, *ptr, *next, *colon;
  2783. char group;
  2784. char commands[TMPBUFSIZ];
  2785. char cmdbuf[100];
  2786. char *cmd;
  2787. bool addstar, did;
  2788. int i;
  2789. buf = malloc(strlen(api_groups) + 1);
  2790. if (unlikely(!buf))
  2791. quit(1, "Failed to malloc ipgroups buf");
  2792. strcpy(buf, api_groups);
  2793. next = buf;
  2794. // for each group defined
  2795. while (next && *next) {
  2796. ptr = next;
  2797. next = strchr(ptr, ',');
  2798. if (next)
  2799. *(next++) = '\0';
  2800. // Validate the group
  2801. if (*(ptr+1) != ':') {
  2802. colon = strchr(ptr, ':');
  2803. if (colon)
  2804. *colon = '\0';
  2805. quit(1, "API invalid group name '%s'", ptr);
  2806. }
  2807. group = GROUP(*ptr);
  2808. if (!VALIDGROUP(group))
  2809. quit(1, "API invalid group name '%c'", *ptr);
  2810. if (group == PRIVGROUP)
  2811. quit(1, "API group name can't be '%c'", PRIVGROUP);
  2812. if (group == NOPRIVGROUP)
  2813. quit(1, "API group name can't be '%c'", NOPRIVGROUP);
  2814. if (apigroups[GROUPOFFSET(group)].commands != NULL)
  2815. quit(1, "API duplicate group name '%c'", *ptr);
  2816. ptr += 2;
  2817. // Validate the command list (and handle '*')
  2818. cmd = &(commands[0]);
  2819. *(cmd++) = SEPARATOR;
  2820. *cmd = '\0';
  2821. addstar = false;
  2822. while (ptr && *ptr) {
  2823. colon = strchr(ptr, ':');
  2824. if (colon)
  2825. *(colon++) = '\0';
  2826. if (strcmp(ptr, "*") == 0)
  2827. addstar = true;
  2828. else {
  2829. did = false;
  2830. for (i = 0; cmds[i].name != NULL; i++) {
  2831. if (strcasecmp(ptr, cmds[i].name) == 0) {
  2832. did = true;
  2833. break;
  2834. }
  2835. }
  2836. if (did) {
  2837. // skip duplicates
  2838. sprintf(cmdbuf, "|%s|", cmds[i].name);
  2839. if (strstr(commands, cmdbuf) == NULL) {
  2840. strcpy(cmd, cmds[i].name);
  2841. cmd += strlen(cmds[i].name);
  2842. *(cmd++) = SEPARATOR;
  2843. *cmd = '\0';
  2844. }
  2845. } else {
  2846. quit(1, "API unknown command '%s' in group '%c'", ptr, group);
  2847. }
  2848. }
  2849. ptr = colon;
  2850. }
  2851. // * = allow all non-iswritemode commands
  2852. if (addstar) {
  2853. for (i = 0; cmds[i].name != NULL; i++) {
  2854. if (cmds[i].iswritemode == false) {
  2855. // skip duplicates
  2856. sprintf(cmdbuf, "|%s|", cmds[i].name);
  2857. if (strstr(commands, cmdbuf) == NULL) {
  2858. strcpy(cmd, cmds[i].name);
  2859. cmd += strlen(cmds[i].name);
  2860. *(cmd++) = SEPARATOR;
  2861. *cmd = '\0';
  2862. }
  2863. }
  2864. }
  2865. }
  2866. ptr = apigroups[GROUPOFFSET(group)].commands = malloc(strlen(commands) + 1);
  2867. if (unlikely(!ptr))
  2868. quit(1, "Failed to malloc group commands buf");
  2869. strcpy(ptr, commands);
  2870. }
  2871. // Now define R (NOPRIVGROUP) as all non-iswritemode commands
  2872. cmd = &(commands[0]);
  2873. *(cmd++) = SEPARATOR;
  2874. *cmd = '\0';
  2875. for (i = 0; cmds[i].name != NULL; i++) {
  2876. if (cmds[i].iswritemode == false) {
  2877. strcpy(cmd, cmds[i].name);
  2878. cmd += strlen(cmds[i].name);
  2879. *(cmd++) = SEPARATOR;
  2880. *cmd = '\0';
  2881. }
  2882. }
  2883. ptr = apigroups[GROUPOFFSET(NOPRIVGROUP)].commands = malloc(strlen(commands) + 1);
  2884. if (unlikely(!ptr))
  2885. quit(1, "Failed to malloc noprivgroup commands buf");
  2886. strcpy(ptr, commands);
  2887. // W (PRIVGROUP) is handled as a special case since it simply means all commands
  2888. free(buf);
  2889. return;
  2890. }
  2891. /*
  2892. * Interpret [W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  2893. * special case of 0/0 allows /0 (means all IP addresses)
  2894. */
  2895. #define ALLIP4 "0/0"
  2896. /*
  2897. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  2898. */
  2899. static void setup_ipaccess()
  2900. {
  2901. char *buf, *ptr, *comma, *slash, *dot;
  2902. int ipcount, mask, octet, i;
  2903. char group;
  2904. buf = malloc(strlen(opt_api_allow) + 1);
  2905. if (unlikely(!buf))
  2906. quit(1, "Failed to malloc ipaccess buf");
  2907. strcpy(buf, opt_api_allow);
  2908. ipcount = 1;
  2909. ptr = buf;
  2910. while (*ptr)
  2911. if (*(ptr++) == ',')
  2912. ipcount++;
  2913. // possibly more than needed, but never less
  2914. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  2915. if (unlikely(!ipaccess))
  2916. quit(1, "Failed to calloc ipaccess");
  2917. ips = 0;
  2918. ptr = buf;
  2919. while (ptr && *ptr) {
  2920. while (*ptr == ' ' || *ptr == '\t')
  2921. ptr++;
  2922. if (*ptr == ',') {
  2923. ptr++;
  2924. continue;
  2925. }
  2926. comma = strchr(ptr, ',');
  2927. if (comma)
  2928. *(comma++) = '\0';
  2929. group = NOPRIVGROUP;
  2930. if (isalpha(*ptr) && *(ptr+1) == ':') {
  2931. if (DEFINEDGROUP(*ptr))
  2932. group = GROUP(*ptr);
  2933. ptr += 2;
  2934. }
  2935. ipaccess[ips].group = group;
  2936. if (strcmp(ptr, ALLIP4) == 0)
  2937. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  2938. else {
  2939. slash = strchr(ptr, '/');
  2940. if (!slash)
  2941. ipaccess[ips].mask = 0xffffffff;
  2942. else {
  2943. *(slash++) = '\0';
  2944. mask = atoi(slash);
  2945. if (mask < 1 || mask > 32)
  2946. goto popipo; // skip invalid/zero
  2947. ipaccess[ips].mask = 0;
  2948. while (mask-- >= 0) {
  2949. octet = 1 << (mask % 8);
  2950. ipaccess[ips].mask |= (octet << (24 - (8 * (mask >> 3))));
  2951. }
  2952. }
  2953. ipaccess[ips].ip = 0; // missing default to '.0'
  2954. for (i = 0; ptr && (i < 4); i++) {
  2955. dot = strchr(ptr, '.');
  2956. if (dot)
  2957. *(dot++) = '\0';
  2958. octet = atoi(ptr);
  2959. if (octet < 0 || octet > 0xff)
  2960. goto popipo; // skip invalid
  2961. ipaccess[ips].ip |= (octet << (24 - (i * 8)));
  2962. ptr = dot;
  2963. }
  2964. ipaccess[ips].ip &= ipaccess[ips].mask;
  2965. }
  2966. ips++;
  2967. popipo:
  2968. ptr = comma;
  2969. }
  2970. free(buf);
  2971. }
  2972. static void *quit_thread(__maybe_unused void *userdata)
  2973. {
  2974. RenameThread("rpc_quit");
  2975. // allow thread creator to finish whatever it's doing
  2976. mutex_lock(&quit_restart_lock);
  2977. mutex_unlock(&quit_restart_lock);
  2978. if (opt_debug)
  2979. applog(LOG_DEBUG, "API: killing BFGMiner");
  2980. kill_work();
  2981. return NULL;
  2982. }
  2983. static void *restart_thread(__maybe_unused void *userdata)
  2984. {
  2985. RenameThread("rpc_restart");
  2986. // allow thread creator to finish whatever it's doing
  2987. mutex_lock(&quit_restart_lock);
  2988. mutex_unlock(&quit_restart_lock);
  2989. if (opt_debug)
  2990. applog(LOG_DEBUG, "API: restarting BFGMiner");
  2991. app_restart();
  2992. return NULL;
  2993. }
  2994. void api(int api_thr_id)
  2995. {
  2996. struct io_data *io_data;
  2997. struct thr_info bye_thr;
  2998. char buf[TMPBUFSIZ];
  2999. char param_buf[TMPBUFSIZ];
  3000. const char *localaddr = "127.0.0.1";
  3001. SOCKETTYPE c;
  3002. int n, bound;
  3003. char *connectaddr;
  3004. const char *binderror;
  3005. time_t bindstart;
  3006. short int port = opt_api_port;
  3007. struct sockaddr_in serv;
  3008. struct sockaddr_in cli;
  3009. socklen_t clisiz;
  3010. char cmdbuf[100];
  3011. char *cmd;
  3012. char *param;
  3013. bool addrok;
  3014. char group;
  3015. json_error_t json_err;
  3016. json_t *json_config;
  3017. json_t *json_val;
  3018. bool isjson;
  3019. bool did;
  3020. int i;
  3021. SOCKETTYPE *apisock;
  3022. apisock = malloc(sizeof(*apisock));
  3023. *apisock = INVSOCK;
  3024. if (!opt_api_listen) {
  3025. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  3026. return;
  3027. }
  3028. io_data = sock_io_new();
  3029. mutex_init(&quit_restart_lock);
  3030. pthread_cleanup_push(tidyup, (void *)apisock);
  3031. my_thr_id = api_thr_id;
  3032. setup_groups();
  3033. if (opt_api_allow) {
  3034. setup_ipaccess();
  3035. if (ips == 0) {
  3036. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  3037. return;
  3038. }
  3039. }
  3040. /* This should be done before curl in needed
  3041. * to ensure curl has already called WSAStartup() in windows */
  3042. nmsleep(opt_log_interval*1000);
  3043. *apisock = socket(AF_INET, SOCK_STREAM, 0);
  3044. if (*apisock == INVSOCK) {
  3045. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3046. return;
  3047. }
  3048. memset(&serv, 0, sizeof(serv));
  3049. serv.sin_family = AF_INET;
  3050. if (!opt_api_allow && !opt_api_network) {
  3051. serv.sin_addr.s_addr = inet_addr(localaddr);
  3052. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  3053. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3054. return;
  3055. }
  3056. }
  3057. serv.sin_port = htons(port);
  3058. #ifndef WIN32
  3059. // On linux with SO_REUSEADDR, bind will get the port if the previous
  3060. // socket is closed (even if it is still in TIME_WAIT) but fail if
  3061. // another program has it open - which is what we want
  3062. int optval = 1;
  3063. // If it doesn't work, we don't really care - just show a debug message
  3064. if (SOCKETFAIL(setsockopt(*apisock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval))))
  3065. applog(LOG_DEBUG, "API setsockopt SO_REUSEADDR failed (ignored): %s", SOCKERRMSG);
  3066. #else
  3067. // On windows a 2nd program can bind to a port>1024 already in use unless
  3068. // SO_EXCLUSIVEADDRUSE is used - however then the bind to a closed port
  3069. // in TIME_WAIT will fail until the timeout - so we leave the options alone
  3070. #endif
  3071. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  3072. bound = 0;
  3073. bindstart = time(NULL);
  3074. while (bound == 0) {
  3075. if (SOCKETFAIL(bind(*apisock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  3076. binderror = SOCKERRMSG;
  3077. if ((time(NULL) - bindstart) > 61)
  3078. break;
  3079. else {
  3080. applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
  3081. nmsleep(30000);
  3082. }
  3083. } else
  3084. bound = 1;
  3085. }
  3086. if (bound == 0) {
  3087. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  3088. return;
  3089. }
  3090. if (SOCKETFAIL(listen(*apisock, QUEUE))) {
  3091. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3092. CLOSESOCKET(*apisock);
  3093. return;
  3094. }
  3095. if (opt_api_allow)
  3096. applog(LOG_WARNING, "API running in IP access mode on port %d", port);
  3097. else {
  3098. if (opt_api_network)
  3099. applog(LOG_WARNING, "API running in UNRESTRICTED read access mode on port %d", port);
  3100. else
  3101. applog(LOG_WARNING, "API running in local read access mode on port %d", port);
  3102. }
  3103. while (!bye) {
  3104. clisiz = sizeof(cli);
  3105. if (SOCKETFAIL(c = accept(*apisock, (struct sockaddr *)(&cli), &clisiz))) {
  3106. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3107. goto die;
  3108. }
  3109. connectaddr = inet_ntoa(cli.sin_addr);
  3110. addrok = false;
  3111. group = NOPRIVGROUP;
  3112. if (opt_api_allow) {
  3113. int client_ip = htonl(cli.sin_addr.s_addr);
  3114. for (i = 0; i < ips; i++) {
  3115. if ((client_ip & ipaccess[i].mask) == ipaccess[i].ip) {
  3116. addrok = true;
  3117. group = ipaccess[i].group;
  3118. break;
  3119. }
  3120. }
  3121. } else {
  3122. if (opt_api_network)
  3123. addrok = true;
  3124. else
  3125. addrok = (strcmp(connectaddr, localaddr) == 0);
  3126. }
  3127. if (opt_debug)
  3128. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  3129. if (addrok) {
  3130. n = recv(c, &buf[0], TMPBUFSIZ-1, 0);
  3131. if (SOCKETFAIL(n))
  3132. buf[0] = '\0';
  3133. else
  3134. buf[n] = '\0';
  3135. if (opt_debug) {
  3136. if (SOCKETFAIL(n))
  3137. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  3138. else
  3139. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  3140. }
  3141. if (!SOCKETFAIL(n)) {
  3142. // the time of the request in now
  3143. when = time(NULL);
  3144. io_reinit(io_data);
  3145. did = false;
  3146. if (*buf != ISJSON) {
  3147. isjson = false;
  3148. param = strchr(buf, SEPARATOR);
  3149. if (param != NULL)
  3150. *(param++) = '\0';
  3151. cmd = buf;
  3152. }
  3153. else {
  3154. isjson = true;
  3155. param = NULL;
  3156. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  3157. json_config = json_loadb(buf, n, 0, &json_err);
  3158. #elif JANSSON_MAJOR_VERSION > 1
  3159. json_config = json_loads(buf, 0, &json_err);
  3160. #else
  3161. json_config = json_loads(buf, &json_err);
  3162. #endif
  3163. if (!json_is_object(json_config)) {
  3164. message(io_data, MSG_INVJSON, 0, NULL, isjson);
  3165. send_result(io_data, c, isjson);
  3166. did = true;
  3167. }
  3168. else {
  3169. json_val = json_object_get(json_config, JSON_COMMAND);
  3170. if (json_val == NULL) {
  3171. message(io_data, MSG_MISCMD, 0, NULL, isjson);
  3172. send_result(io_data, c, isjson);
  3173. did = true;
  3174. }
  3175. else {
  3176. if (!json_is_string(json_val)) {
  3177. message(io_data, MSG_INVCMD, 0, NULL, isjson);
  3178. send_result(io_data, c, isjson);
  3179. did = true;
  3180. }
  3181. else {
  3182. cmd = (char *)json_string_value(json_val);
  3183. json_val = json_object_get(json_config, JSON_PARAMETER);
  3184. if (json_is_string(json_val))
  3185. param = (char *)json_string_value(json_val);
  3186. else if (json_is_integer(json_val)) {
  3187. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  3188. param = param_buf;
  3189. } else if (json_is_real(json_val)) {
  3190. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  3191. param = param_buf;
  3192. }
  3193. }
  3194. }
  3195. }
  3196. }
  3197. if (!did)
  3198. for (i = 0; cmds[i].name != NULL; i++) {
  3199. if (strcmp(cmd, cmds[i].name) == 0) {
  3200. sprintf(cmdbuf, "|%s|", cmd);
  3201. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  3202. (cmds[i].func)(io_data, c, param, isjson, group);
  3203. else {
  3204. message(io_data, MSG_ACCDENY, 0, cmds[i].name, isjson);
  3205. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  3206. }
  3207. send_result(io_data, c, isjson);
  3208. did = true;
  3209. break;
  3210. }
  3211. }
  3212. if (isjson)
  3213. json_decref(json_config);
  3214. if (!did) {
  3215. message(io_data, MSG_INVCMD, 0, NULL, isjson);
  3216. send_result(io_data, c, isjson);
  3217. }
  3218. }
  3219. }
  3220. CLOSESOCKET(c);
  3221. }
  3222. die:
  3223. /* Blank line fix for older compilers since pthread_cleanup_pop is a
  3224. * macro that gets confused by a label existing immediately before it
  3225. */
  3226. ;
  3227. pthread_cleanup_pop(true);
  3228. if (opt_debug)
  3229. applog(LOG_DEBUG, "API: terminating due to: %s",
  3230. do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
  3231. mutex_lock(&quit_restart_lock);
  3232. if (do_a_restart) {
  3233. if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
  3234. mutex_unlock(&quit_restart_lock);
  3235. quit(1, "API failed to initiate a restart - aborting");
  3236. }
  3237. pthread_detach(bye_thr.pth);
  3238. } else if (do_a_quit) {
  3239. if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
  3240. mutex_unlock(&quit_restart_lock);
  3241. quit(1, "API failed to initiate a clean quit - aborting");
  3242. }
  3243. pthread_detach(bye_thr.pth);
  3244. }
  3245. mutex_unlock(&quit_restart_lock);
  3246. }