api.c 115 KB

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