api.c 114 KB

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