api.c 106 KB

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