api.c 112 KB

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