api.c 106 KB

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