api.c 114 KB

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