api.c 115 KB

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