api.c 115 KB

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