api.c 109 KB

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