api.c 115 KB

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