api.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336
  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, "CGMiner", VERSION, false);
  1115. root = api_add_const(root, "API", APIVERSION, false);
  1116. root = print_data(root, buf, isjson, false);
  1117. io_add(io_data, buf);
  1118. if (isjson && io_open)
  1119. io_close(io_data);
  1120. }
  1121. static void minerconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1122. {
  1123. struct api_data *root = NULL;
  1124. char buf[TMPBUFSIZ];
  1125. bool io_open;
  1126. struct driver_registration *reg, *regtmp;
  1127. int pgacount = 0;
  1128. char *adlinuse = (char *)NO;
  1129. #ifdef HAVE_ADL
  1130. const char *adl = YES;
  1131. int i;
  1132. for (i = 0; i < nDevs; i++) {
  1133. struct opencl_device_data * const data = gpus[i].device_data;
  1134. if (data->has_adl) {
  1135. adlinuse = (char *)YES;
  1136. break;
  1137. }
  1138. }
  1139. #else
  1140. const char *adl = NO;
  1141. #endif
  1142. #ifdef HAVE_AN_FPGA
  1143. pgacount = numpgas();
  1144. #endif
  1145. message(io_data, MSG_MINECONFIG, 0, NULL, isjson);
  1146. io_open = io_add(io_data, isjson ? COMSTR JSON_MINECONFIG : _MINECONFIG COMSTR);
  1147. root = api_add_int(root, "PGA Count", &pgacount, false);
  1148. root = api_add_int(root, "Pool Count", &total_pools, false);
  1149. root = api_add_const(root, "ADL", (char *)adl, false);
  1150. root = api_add_string(root, "ADL in use", adlinuse, false);
  1151. root = api_add_const(root, "Strategy", strategies[pool_strategy].s, false);
  1152. root = api_add_int(root, "Log Interval", &opt_log_interval, false);
  1153. strcpy(buf, ""
  1154. #ifdef USE_LIBMICROHTTPD
  1155. " SGW"
  1156. #endif
  1157. #ifdef USE_LIBEVENT
  1158. " SSM"
  1159. #endif
  1160. );
  1161. BFG_FOREACH_DRIVER_BY_DNAME(reg, regtmp)
  1162. {
  1163. const struct device_drv * const drv = reg->drv;
  1164. tailsprintf(buf, sizeof(buf), " %s", drv->name);
  1165. }
  1166. root = api_add_const(root, "Device Code", &buf[1], true);
  1167. root = api_add_const(root, "OS", OSINFO, false);
  1168. root = api_add_bool(root, "Failover-Only", &opt_fail_only, false);
  1169. root = api_add_int(root, "ScanTime", &opt_scantime, false);
  1170. root = api_add_int(root, "Queue", &opt_queue, false);
  1171. root = api_add_int(root, "Expiry", &opt_expiry, false);
  1172. #if BLKMAKER_VERSION > 0
  1173. root = api_add_string(root, "Coinbase-Sig", opt_coinbase_sig, true);
  1174. #endif
  1175. root = print_data(root, buf, isjson, false);
  1176. io_add(io_data, buf);
  1177. if (isjson && io_open)
  1178. io_close(io_data);
  1179. }
  1180. static const char*
  1181. bool2str(bool b)
  1182. {
  1183. return b ? YES : NO;
  1184. }
  1185. static const char *status2str(enum alive status)
  1186. {
  1187. switch (status) {
  1188. case LIFE_WELL:
  1189. return ALIVE;
  1190. case LIFE_SICK:
  1191. return SICK;
  1192. case LIFE_DEAD:
  1193. case LIFE_DEAD2:
  1194. return DEAD;
  1195. case LIFE_NOSTART:
  1196. return NOSTART;
  1197. case LIFE_INIT:
  1198. case LIFE_INIT2:
  1199. return INIT;
  1200. case LIFE_WAIT:
  1201. return WAIT;
  1202. case LIFE_MIXED:
  1203. return "Mixed";
  1204. default:
  1205. return UNKNOWN;
  1206. }
  1207. }
  1208. static
  1209. struct api_data *api_add_device_identifier(struct api_data *root, struct cgpu_info *cgpu)
  1210. {
  1211. root = api_add_string(root, "Name", cgpu->drv->name, false);
  1212. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  1213. if (per_proc)
  1214. root = api_add_int(root, "ProcID", &(cgpu->proc_id), false);
  1215. return root;
  1216. }
  1217. static
  1218. int find_index_by_cgpu(struct cgpu_info *cgpu)
  1219. {
  1220. if (per_proc)
  1221. return cgpu->cgminer_id;
  1222. int n = 0, i;
  1223. // Quickly traverse the devices array backward until we reach the 0th device, counting as we go
  1224. rd_lock(&devices_lock);
  1225. while (true)
  1226. {
  1227. i = cgpu->device->cgminer_id;
  1228. if (!i)
  1229. break;
  1230. cgpu = devices[--i];
  1231. ++n;
  1232. }
  1233. rd_unlock(&devices_lock);
  1234. return n;
  1235. }
  1236. static void devdetail_an(struct io_data *io_data, struct cgpu_info *cgpu, bool isjson, bool precom)
  1237. {
  1238. struct api_data *root = NULL;
  1239. char buf[TMPBUFSIZ];
  1240. int n;
  1241. cgpu_utility(cgpu);
  1242. n = find_index_by_cgpu(cgpu);
  1243. root = api_add_int(root, "DEVDETAILS", &n, true);
  1244. root = api_add_device_identifier(root, cgpu);
  1245. if (!per_proc)
  1246. root = api_add_int(root, "Processors", &cgpu->procs, false);
  1247. root = api_add_string(root, "Driver", cgpu->drv->dname, false);
  1248. if (cgpu->kname)
  1249. root = api_add_string(root, "Kernel", cgpu->kname, false);
  1250. if (cgpu->name)
  1251. root = api_add_string(root, "Model", cgpu->name, false);
  1252. if (cgpu->dev_manufacturer)
  1253. root = api_add_string(root, "Manufacturer", cgpu->dev_manufacturer, false);
  1254. if (cgpu->dev_product)
  1255. root = api_add_string(root, "Product", cgpu->dev_product, false);
  1256. if (cgpu->dev_serial)
  1257. root = api_add_string(root, "Serial", cgpu->dev_serial, false);
  1258. if (cgpu->device_path)
  1259. root = api_add_string(root, "Device Path", cgpu->device_path, false);
  1260. root = api_add_int(root, "Target Temperature", &cgpu->targettemp, false);
  1261. root = api_add_int(root, "Cutoff Temperature", &cgpu->cutofftemp, false);
  1262. if (cgpu->drv->get_api_extra_device_detail)
  1263. root = api_add_extra(root, cgpu->drv->get_api_extra_device_detail(cgpu));
  1264. root = print_data(root, buf, isjson, precom);
  1265. io_add(io_data, buf);
  1266. }
  1267. static
  1268. void devstatus_an(struct io_data *io_data, struct cgpu_info *cgpu, bool isjson, bool precom)
  1269. {
  1270. struct cgpu_info *proc;
  1271. struct api_data *root = NULL;
  1272. char buf[TMPBUFSIZ];
  1273. int n;
  1274. n = find_index_by_cgpu(cgpu);
  1275. double runtime = cgpu_runtime(cgpu);
  1276. bool enabled = false;
  1277. double total_mhashes = 0, rolling = 0, utility = 0;
  1278. enum alive status = cgpu->status;
  1279. float temp = -1;
  1280. int accepted = 0, rejected = 0, stale = 0, hw_errors = 0;
  1281. double diff1 = 0, bad_diff1 = 0;
  1282. double diff_accepted = 0, diff_rejected = 0, diff_stale = 0;
  1283. int last_share_pool = -1;
  1284. time_t last_share_pool_time = -1, last_device_valid_work = -1;
  1285. double last_share_diff = -1;
  1286. int procs = per_proc ? 1 : cgpu->procs, i;
  1287. for (i = 0, proc = cgpu; i < procs; ++i, proc = proc->next_proc)
  1288. {
  1289. cgpu_utility(proc);
  1290. if (proc->deven != DEV_DISABLED)
  1291. enabled = true;
  1292. total_mhashes += proc->total_mhashes;
  1293. rolling += proc->rolling;
  1294. utility += proc->utility;
  1295. accepted += proc->accepted;
  1296. rejected += proc->rejected;
  1297. stale += proc->stale;
  1298. hw_errors += proc->hw_errors;
  1299. diff1 += proc->diff1;
  1300. diff_accepted += proc->diff_accepted;
  1301. diff_rejected += proc->diff_rejected;
  1302. diff_stale += proc->diff_stale;
  1303. bad_diff1 += proc->bad_diff1;
  1304. if (status != proc->status)
  1305. status = LIFE_MIXED;
  1306. if (proc->temp > temp)
  1307. temp = proc->temp;
  1308. if (proc->last_share_pool_time > last_share_pool_time)
  1309. {
  1310. last_share_pool_time = proc->last_share_pool_time;
  1311. last_share_pool = proc->last_share_pool;
  1312. last_share_diff = proc->last_share_diff;
  1313. }
  1314. if (proc->last_device_valid_work > last_device_valid_work)
  1315. last_device_valid_work = proc->last_device_valid_work;
  1316. if (per_proc)
  1317. break;
  1318. }
  1319. root = api_add_int(root, "PGA", &n, true);
  1320. root = api_add_device_identifier(root, cgpu);
  1321. root = api_add_string(root, "Enabled", bool2str(enabled), false);
  1322. root = api_add_string(root, "Status", status2str(status), false);
  1323. if (temp > 0)
  1324. root = api_add_temp(root, "Temperature", &temp, false);
  1325. root = api_add_elapsed(root, "Device Elapsed", &runtime, false);
  1326. double mhs = total_mhashes / runtime;
  1327. root = api_add_mhs(root, "MHS av", &mhs, false);
  1328. char mhsname[27];
  1329. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1330. root = api_add_mhs(root, mhsname, &rolling, false);
  1331. root = api_add_int(root, "Accepted", &accepted, false);
  1332. root = api_add_int(root, "Rejected", &rejected, false);
  1333. root = api_add_int(root, "Hardware Errors", &hw_errors, false);
  1334. root = api_add_utility(root, "Utility", &utility, false);
  1335. root = api_add_int(root, "Stale", &stale, false);
  1336. if (last_share_pool != -1)
  1337. {
  1338. root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
  1339. root = api_add_time(root, "Last Share Time", &last_share_pool_time, false);
  1340. }
  1341. root = api_add_mhtotal(root, "Total MH", &total_mhashes, false);
  1342. double work_utility = diff1 / runtime * 60;
  1343. root = api_add_diff(root, "Diff1 Work", &diff1, false);
  1344. root = api_add_utility(root, "Work Utility", &work_utility, false);
  1345. root = api_add_diff(root, "Difficulty Accepted", &diff_accepted, false);
  1346. root = api_add_diff(root, "Difficulty Rejected", &diff_rejected, false);
  1347. root = api_add_diff(root, "Difficulty Stale", &diff_stale, false);
  1348. if (last_share_diff > 0)
  1349. root = api_add_diff(root, "Last Share Difficulty", &last_share_diff, false);
  1350. if (last_device_valid_work != -1)
  1351. root = api_add_time(root, "Last Valid Work", &last_device_valid_work, false);
  1352. double hwp = (bad_diff1 + diff1) ?
  1353. (double)(bad_diff1) / (double)(bad_diff1 + diff1) : 0;
  1354. root = api_add_percent(root, "Device Hardware%", &hwp, false);
  1355. double rejp = diff1 ?
  1356. (double)(diff_rejected) / (double)(diff1) : 0;
  1357. root = api_add_percent(root, "Device Rejected%", &rejp, false);
  1358. if ((per_proc || cgpu->procs <= 1) && cgpu->drv->get_api_extra_device_status)
  1359. root = api_add_extra(root, cgpu->drv->get_api_extra_device_status(cgpu));
  1360. root = print_data(root, buf, isjson, precom);
  1361. io_add(io_data, buf);
  1362. }
  1363. #ifdef HAVE_OPENCL
  1364. static void gpustatus(struct io_data *io_data, int gpu, bool isjson, bool precom)
  1365. {
  1366. if (gpu < 0 || gpu >= nDevs)
  1367. return;
  1368. devstatus_an(io_data, &gpus[gpu], isjson, precom);
  1369. }
  1370. #endif
  1371. #ifdef HAVE_AN_FPGA
  1372. static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom)
  1373. {
  1374. int dev = pgadevice(pga);
  1375. if (dev < 0) // Should never happen
  1376. return;
  1377. devstatus_an(io_data, get_devices(dev), isjson, precom);
  1378. }
  1379. #endif
  1380. #ifdef WANT_CPUMINE
  1381. static void cpustatus(struct io_data *io_data, int cpu, bool isjson, bool precom)
  1382. {
  1383. if (opt_n_threads <= 0 || cpu < 0 || cpu >= num_processors)
  1384. return;
  1385. devstatus_an(io_data, &cpus[cpu], isjson, precom);
  1386. }
  1387. #endif
  1388. static void
  1389. 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)
  1390. {
  1391. struct cgpu_info *cgpu;
  1392. bool io_open = false;
  1393. int i;
  1394. if (total_devices == 0) {
  1395. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  1396. return;
  1397. }
  1398. message(io_data, msg, 0, NULL, isjson);
  1399. if (isjson)
  1400. io_open = io_add(io_data, COMSTR JSON_DEVS);
  1401. for (i = 0; i < total_devices; ++i) {
  1402. cgpu = get_devices(i);
  1403. if (per_proc || cgpu->device == cgpu)
  1404. func(io_data, cgpu, isjson, isjson && i > 0);
  1405. }
  1406. if (isjson && io_open)
  1407. io_close(io_data);
  1408. }
  1409. static void devdetail(struct io_data *io_data, SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1410. {
  1411. return devinfo_internal(devdetail_an, MSG_DEVDETAILS, io_data, c, param, isjson, group);
  1412. }
  1413. static void devstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1414. {
  1415. return devinfo_internal(devstatus_an, MSG_DEVS, io_data, c, param, isjson, group);
  1416. }
  1417. #ifdef HAVE_OPENCL
  1418. static void gpudev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1419. {
  1420. bool io_open = false;
  1421. int id;
  1422. if (nDevs == 0) {
  1423. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1424. return;
  1425. }
  1426. if (param == NULL || *param == '\0') {
  1427. message(io_data, MSG_MISID, 0, NULL, isjson);
  1428. return;
  1429. }
  1430. id = atoi(param);
  1431. if (id < 0 || id >= nDevs) {
  1432. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1433. return;
  1434. }
  1435. message(io_data, MSG_GPUDEV, id, NULL, isjson);
  1436. if (isjson)
  1437. io_open = io_add(io_data, COMSTR JSON_GPU);
  1438. gpustatus(io_data, id, isjson, false);
  1439. if (isjson && io_open)
  1440. io_close(io_data);
  1441. }
  1442. #endif
  1443. static void devscan(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1444. {
  1445. int n;
  1446. bool io_open = false;
  1447. applog(LOG_DEBUG, "RPC: request to scan %s for devices",
  1448. param);
  1449. if (param && !param[0])
  1450. param = NULL;
  1451. n = scan_serial(param);
  1452. message(io_data, MSG_DEVSCAN, n, NULL, isjson);
  1453. io_open = io_add(io_data, isjson ? COMSTR JSON_DEVS : _DEVS COMSTR);
  1454. n = total_devices - n;
  1455. for (int i = n; i < total_devices; ++i)
  1456. devdetail_an(io_data, get_devices(i), isjson, i > n);
  1457. if (isjson && io_open)
  1458. io_close(io_data);
  1459. }
  1460. #ifdef HAVE_AN_FPGA
  1461. static
  1462. 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)
  1463. {
  1464. int numpga = numpgas();
  1465. if (numpga == 0) {
  1466. message(io_data, MSG_PGANON, 0, NULL, isjson);
  1467. return NULL;
  1468. }
  1469. if (param == NULL || *param == '\0') {
  1470. message(io_data, MSG_MISID, 0, NULL, isjson);
  1471. return NULL;
  1472. }
  1473. *id_p = atoi(param);
  1474. if (*id_p < 0 || *id_p >= numpga) {
  1475. message(io_data, MSG_INVPGA, *id_p, NULL, isjson);
  1476. return NULL;
  1477. }
  1478. *dev_p = pgadevice(*id_p);
  1479. if (*dev_p < 0) { // Should never happen
  1480. message(io_data, MSG_INVPGA, *id_p, NULL, isjson);
  1481. return NULL;
  1482. }
  1483. return get_devices(*dev_p);
  1484. }
  1485. static void pgadev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1486. {
  1487. bool io_open = false;
  1488. int id, dev;
  1489. if (!get_pga_cgpu(io_data, c, param, isjson, group, &id, &dev))
  1490. return;
  1491. message(io_data, MSG_PGADEV, id, NULL, isjson);
  1492. if (isjson)
  1493. io_open = io_add(io_data, COMSTR JSON_PGA);
  1494. pgastatus(io_data, id, isjson, false);
  1495. if (isjson && io_open)
  1496. io_close(io_data);
  1497. }
  1498. static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1499. {
  1500. struct cgpu_info *cgpu, *proc;
  1501. int id, dev;
  1502. bool already;
  1503. cgpu = get_pga_cgpu(io_data, c, param, isjson, group, &id, &dev);
  1504. if (!cgpu)
  1505. return;
  1506. applog(LOG_DEBUG, "API: request to pgaenable %s id %d device %d %s",
  1507. per_proc ? "proc" : "dev", id, dev, cgpu->proc_repr_ns);
  1508. already = true;
  1509. int procs = per_proc ? 1 : cgpu->procs, i;
  1510. for (i = 0, proc = cgpu; i < procs; ++i, proc = proc->next_proc)
  1511. {
  1512. if (proc->deven == DEV_DISABLED)
  1513. {
  1514. proc_enable(proc);
  1515. already = false;
  1516. }
  1517. }
  1518. if (already)
  1519. {
  1520. message(io_data, MSG_PGALRENA, id, NULL, isjson);
  1521. return;
  1522. }
  1523. #if 0 /* A DISABLED device wont change status FIXME: should disabling make it WELL? */
  1524. if (cgpu->status != LIFE_WELL) {
  1525. message(io_data, MSG_PGAUNW, id, NULL, isjson);
  1526. return;
  1527. }
  1528. #endif
  1529. message(io_data, MSG_PGAENA, id, NULL, isjson);
  1530. }
  1531. static void pgadisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1532. {
  1533. struct cgpu_info *cgpu, *proc;
  1534. int id, dev;
  1535. bool already;
  1536. cgpu = get_pga_cgpu(io_data, c, param, isjson, group, &id, &dev);
  1537. if (!cgpu)
  1538. return;
  1539. applog(LOG_DEBUG, "API: request to pgadisable %s id %d device %d %s",
  1540. per_proc ? "proc" : "dev", id, dev, cgpu->proc_repr_ns);
  1541. already = true;
  1542. int procs = per_proc ? 1 : cgpu->procs, i;
  1543. for (i = 0, proc = cgpu; i < procs; ++i, proc = proc->next_proc)
  1544. {
  1545. if (proc->deven != DEV_DISABLED)
  1546. {
  1547. cgpu->deven = DEV_DISABLED;
  1548. already = false;
  1549. }
  1550. }
  1551. if (already)
  1552. {
  1553. message(io_data, MSG_PGALRDIS, id, NULL, isjson);
  1554. return;
  1555. }
  1556. message(io_data, MSG_PGADIS, id, NULL, isjson);
  1557. }
  1558. static void pgarestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1559. {
  1560. struct cgpu_info *cgpu;
  1561. int id, dev;
  1562. cgpu = get_pga_cgpu(io_data, c, param, isjson, group, &id, &dev);
  1563. if (!cgpu)
  1564. return;
  1565. applog(LOG_DEBUG, "API: request to pgarestart dev id %d device %d %s",
  1566. id, dev, cgpu->dev_repr);
  1567. reinit_device(cgpu);
  1568. message(io_data, MSG_PGAREI, id, NULL, isjson);
  1569. }
  1570. static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1571. {
  1572. struct cgpu_info *cgpu;
  1573. struct device_drv *drv;
  1574. int id, dev;
  1575. cgpu = get_pga_cgpu(io_data, c, param, isjson, group, &id, &dev);
  1576. if (!cgpu)
  1577. return;
  1578. drv = cgpu->drv;
  1579. if (drv->identify_device && drv->identify_device(cgpu))
  1580. message(io_data, MSG_PGAIDENT, id, NULL, isjson);
  1581. else
  1582. message(io_data, MSG_PGANOID, id, NULL, isjson);
  1583. }
  1584. #endif
  1585. #ifdef WANT_CPUMINE
  1586. static void cpudev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1587. {
  1588. bool io_open = false;
  1589. int id;
  1590. if (opt_n_threads == 0) {
  1591. message(io_data, MSG_CPUNON, 0, NULL, isjson);
  1592. return;
  1593. }
  1594. if (param == NULL || *param == '\0') {
  1595. message(io_data, MSG_MISID, 0, NULL, isjson);
  1596. return;
  1597. }
  1598. id = atoi(param);
  1599. if (id < 0 || id >= num_processors) {
  1600. message(io_data, MSG_INVCPU, id, NULL, isjson);
  1601. return;
  1602. }
  1603. message(io_data, MSG_CPUDEV, id, NULL, isjson);
  1604. if (isjson)
  1605. io_open = io_add(io_data, COMSTR JSON_CPU);
  1606. cpustatus(io_data, id, isjson, false);
  1607. if (isjson && io_open)
  1608. io_close(io_data);
  1609. }
  1610. #endif
  1611. static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1612. {
  1613. struct api_data *root = NULL;
  1614. char buf[TMPBUFSIZ];
  1615. bool io_open = false;
  1616. char *status, *lp;
  1617. int i;
  1618. if (total_pools == 0) {
  1619. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1620. return;
  1621. }
  1622. message(io_data, MSG_POOL, 0, NULL, isjson);
  1623. if (isjson)
  1624. io_open = io_add(io_data, COMSTR JSON_POOLS);
  1625. for (i = 0; i < total_pools; i++) {
  1626. struct pool *pool = pools[i];
  1627. if (pool->removed)
  1628. continue;
  1629. switch (pool->enabled) {
  1630. case POOL_DISABLED:
  1631. status = (char *)DISABLED;
  1632. break;
  1633. case POOL_REJECTING:
  1634. status = (char *)REJECTING;
  1635. break;
  1636. case POOL_ENABLED:
  1637. if (pool->idle)
  1638. status = (char *)DEAD;
  1639. else
  1640. status = (char *)ALIVE;
  1641. break;
  1642. default:
  1643. status = (char *)UNKNOWN;
  1644. break;
  1645. }
  1646. if (pool->hdr_path)
  1647. lp = (char *)YES;
  1648. else
  1649. lp = (char *)NO;
  1650. root = api_add_int(root, "POOL", &i, false);
  1651. root = api_add_escape(root, "URL", pool->rpc_url, false);
  1652. root = api_add_string(root, "Status", status, false);
  1653. root = api_add_int(root, "Priority", &(pool->prio), false);
  1654. root = api_add_int(root, "Quota", &pool->quota, false);
  1655. root = api_add_string(root, "Long Poll", lp, false);
  1656. root = api_add_uint(root, "Getworks", &(pool->getwork_requested), false);
  1657. root = api_add_int(root, "Accepted", &(pool->accepted), false);
  1658. root = api_add_int(root, "Rejected", &(pool->rejected), false);
  1659. root = api_add_int(root, "Works", &pool->works, false);
  1660. root = api_add_uint(root, "Discarded", &(pool->discarded_work), false);
  1661. root = api_add_uint(root, "Stale", &(pool->stale_shares), false);
  1662. root = api_add_uint(root, "Get Failures", &(pool->getfail_occasions), false);
  1663. root = api_add_uint(root, "Remote Failures", &(pool->remotefail_occasions), false);
  1664. root = api_add_escape(root, "User", pool->rpc_user, false);
  1665. root = api_add_time(root, "Last Share Time", &(pool->last_share_time), false);
  1666. root = api_add_diff(root, "Diff1 Shares", &(pool->diff1), false);
  1667. if (pool->rpc_proxy) {
  1668. root = api_add_escape(root, "Proxy", pool->rpc_proxy, false);
  1669. } else {
  1670. root = api_add_const(root, "Proxy", BLANK, false);
  1671. }
  1672. root = api_add_diff(root, "Difficulty Accepted", &(pool->diff_accepted), false);
  1673. root = api_add_diff(root, "Difficulty Rejected", &(pool->diff_rejected), false);
  1674. root = api_add_diff(root, "Difficulty Stale", &(pool->diff_stale), false);
  1675. root = api_add_diff(root, "Last Share Difficulty", &(pool->last_share_diff), false);
  1676. root = api_add_bool(root, "Has Stratum", &(pool->has_stratum), false);
  1677. root = api_add_bool(root, "Stratum Active", &(pool->stratum_active), false);
  1678. if (pool->stratum_active)
  1679. root = api_add_escape(root, "Stratum URL", pool->stratum_url, false);
  1680. else
  1681. root = api_add_const(root, "Stratum URL", BLANK, false);
  1682. root = api_add_uint64(root, "Best Share", &(pool->best_diff), true);
  1683. if (pool->admin_msg)
  1684. root = api_add_escape(root, "Message", pool->admin_msg, true);
  1685. double rejp = (pool->diff_accepted + pool->diff_rejected + pool->diff_stale) ?
  1686. (double)(pool->diff_rejected) / (double)(pool->diff_accepted + pool->diff_rejected + pool->diff_stale) : 0;
  1687. root = api_add_percent(root, "Pool Rejected%", &rejp, false);
  1688. double stalep = (pool->diff_accepted + pool->diff_rejected + pool->diff_stale) ?
  1689. (double)(pool->diff_stale) / (double)(pool->diff_accepted + pool->diff_rejected + pool->diff_stale) : 0;
  1690. root = api_add_percent(root, "Pool Stale%", &stalep, false);
  1691. root = print_data(root, buf, isjson, isjson && (i > 0));
  1692. io_add(io_data, buf);
  1693. }
  1694. if (isjson && io_open)
  1695. io_close(io_data);
  1696. }
  1697. static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1698. {
  1699. struct api_data *root = NULL;
  1700. char buf[TMPBUFSIZ];
  1701. bool io_open;
  1702. double utility, mhs, work_utility;
  1703. #ifdef WANT_CPUMINE
  1704. char *algo = (char *)(algo_names[opt_algo]);
  1705. if (algo == NULL)
  1706. algo = (char *)NULLSTR;
  1707. #endif
  1708. message(io_data, MSG_SUMM, 0, NULL, isjson);
  1709. io_open = io_add(io_data, isjson ? COMSTR JSON_SUMMARY : _SUMMARY COMSTR);
  1710. // stop hashmeter() changing some while copying
  1711. mutex_lock(&hash_lock);
  1712. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1713. mhs = total_mhashes_done / total_secs;
  1714. work_utility = total_diff1 / ( total_secs ? total_secs : 1 ) * 60;
  1715. root = api_add_elapsed(root, "Elapsed", &(total_secs), true);
  1716. #ifdef WANT_CPUMINE
  1717. if (opt_n_threads)
  1718. root = api_add_string(root, "Algorithm", algo, false);
  1719. #endif
  1720. root = api_add_mhs(root, "MHS av", &(mhs), false);
  1721. char mhsname[27];
  1722. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1723. root = api_add_mhs(root, mhsname, &(total_rolling), false);
  1724. root = api_add_uint(root, "Found Blocks", &(found_blocks), true);
  1725. root = api_add_int(root, "Getworks", &(total_getworks), true);
  1726. root = api_add_int(root, "Accepted", &(total_accepted), true);
  1727. root = api_add_int(root, "Rejected", &(total_rejected), true);
  1728. root = api_add_int(root, "Hardware Errors", &(hw_errors), true);
  1729. root = api_add_utility(root, "Utility", &(utility), false);
  1730. root = api_add_int(root, "Discarded", &(total_discarded), true);
  1731. root = api_add_int(root, "Stale", &(total_stale), true);
  1732. root = api_add_uint(root, "Get Failures", &(total_go), true);
  1733. root = api_add_uint(root, "Local Work", &(local_work), true);
  1734. root = api_add_uint(root, "Remote Failures", &(total_ro), true);
  1735. root = api_add_uint(root, "Network Blocks", &(new_blocks), true);
  1736. root = api_add_mhtotal(root, "Total MH", &(total_mhashes_done), true);
  1737. root = api_add_diff(root, "Diff1 Work", &total_diff1, true);
  1738. root = api_add_utility(root, "Work Utility", &(work_utility), false);
  1739. root = api_add_diff(root, "Difficulty Accepted", &(total_diff_accepted), true);
  1740. root = api_add_diff(root, "Difficulty Rejected", &(total_diff_rejected), true);
  1741. root = api_add_diff(root, "Difficulty Stale", &(total_diff_stale), true);
  1742. root = api_add_uint64(root, "Best Share", &(best_diff), true);
  1743. double hwp = (total_bad_diff1 + total_diff1) ?
  1744. (double)(total_bad_diff1) / (double)(total_bad_diff1 + total_diff1) : 0;
  1745. root = api_add_percent(root, "Device Hardware%", &hwp, false);
  1746. double rejp = total_diff1 ?
  1747. (double)(total_diff_rejected) / (double)(total_diff1) : 0;
  1748. root = api_add_percent(root, "Device Rejected%", &rejp, false);
  1749. double prejp = (total_diff_accepted + total_diff_rejected + total_diff_stale) ?
  1750. (double)(total_diff_rejected) / (double)(total_diff_accepted + total_diff_rejected + total_diff_stale) : 0;
  1751. root = api_add_percent(root, "Pool Rejected%", &prejp, false);
  1752. double stalep = (total_diff_accepted + total_diff_rejected + total_diff_stale) ?
  1753. (double)(total_diff_stale) / (double)(total_diff_accepted + total_diff_rejected + total_diff_stale) : 0;
  1754. root = api_add_percent(root, "Pool Stale%", &stalep, false);
  1755. root = api_add_time(root, "Last getwork", &last_getwork, false);
  1756. mutex_unlock(&hash_lock);
  1757. root = print_data(root, buf, isjson, false);
  1758. io_add(io_data, buf);
  1759. if (isjson && io_open)
  1760. io_close(io_data);
  1761. }
  1762. #ifdef HAVE_OPENCL
  1763. static void gpuenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1764. {
  1765. int id;
  1766. if (!nDevs) {
  1767. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1768. return;
  1769. }
  1770. if (param == NULL || *param == '\0') {
  1771. message(io_data, MSG_MISID, 0, NULL, isjson);
  1772. return;
  1773. }
  1774. id = atoi(param);
  1775. if (id < 0 || id >= nDevs) {
  1776. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1777. return;
  1778. }
  1779. applog(LOG_DEBUG, "API: request to gpuenable gpuid %d %s",
  1780. id, gpus[id].proc_repr_ns);
  1781. if (gpus[id].deven != DEV_DISABLED) {
  1782. message(io_data, MSG_ALRENA, id, NULL, isjson);
  1783. return;
  1784. }
  1785. if (gpus[id].status != LIFE_WELL)
  1786. {
  1787. message(io_data, MSG_GPUMRE, id, NULL, isjson);
  1788. return;
  1789. }
  1790. proc_enable(&gpus[id]);
  1791. message(io_data, MSG_GPUREN, id, NULL, isjson);
  1792. }
  1793. static void gpudisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1794. {
  1795. int id;
  1796. if (nDevs == 0) {
  1797. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1798. return;
  1799. }
  1800. if (param == NULL || *param == '\0') {
  1801. message(io_data, MSG_MISID, 0, NULL, isjson);
  1802. return;
  1803. }
  1804. id = atoi(param);
  1805. if (id < 0 || id >= nDevs) {
  1806. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1807. return;
  1808. }
  1809. applog(LOG_DEBUG, "API: request to gpudisable gpuid %d %s",
  1810. id, gpus[id].proc_repr_ns);
  1811. if (gpus[id].deven == DEV_DISABLED) {
  1812. message(io_data, MSG_ALRDIS, id, NULL, isjson);
  1813. return;
  1814. }
  1815. gpus[id].deven = DEV_DISABLED;
  1816. message(io_data, MSG_GPUDIS, id, NULL, isjson);
  1817. }
  1818. static void gpurestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1819. {
  1820. int id;
  1821. if (nDevs == 0) {
  1822. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  1823. return;
  1824. }
  1825. if (param == NULL || *param == '\0') {
  1826. message(io_data, MSG_MISID, 0, NULL, isjson);
  1827. return;
  1828. }
  1829. id = atoi(param);
  1830. if (id < 0 || id >= nDevs) {
  1831. message(io_data, MSG_INVGPU, id, NULL, isjson);
  1832. return;
  1833. }
  1834. reinit_device(&gpus[id]);
  1835. message(io_data, MSG_GPUREI, id, NULL, isjson);
  1836. }
  1837. #endif
  1838. static void gpucount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1839. {
  1840. struct api_data *root = NULL;
  1841. char buf[TMPBUFSIZ];
  1842. bool io_open;
  1843. int numgpu = 0;
  1844. #ifdef HAVE_OPENCL
  1845. numgpu = nDevs;
  1846. #endif
  1847. message(io_data, MSG_NUMGPU, 0, NULL, isjson);
  1848. io_open = io_add(io_data, isjson ? COMSTR JSON_GPUS : _GPUS COMSTR);
  1849. root = api_add_int(root, "Count", &numgpu, false);
  1850. root = print_data(root, buf, isjson, false);
  1851. io_add(io_data, buf);
  1852. if (isjson && io_open)
  1853. io_close(io_data);
  1854. }
  1855. static void pgacount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1856. {
  1857. struct api_data *root = NULL;
  1858. char buf[TMPBUFSIZ];
  1859. bool io_open;
  1860. int count = 0;
  1861. #ifdef HAVE_AN_FPGA
  1862. count = numpgas();
  1863. #endif
  1864. message(io_data, MSG_NUMPGA, 0, NULL, isjson);
  1865. io_open = io_add(io_data, isjson ? COMSTR JSON_PGAS : _PGAS COMSTR);
  1866. root = api_add_int(root, "Count", &count, false);
  1867. root = print_data(root, buf, isjson, false);
  1868. io_add(io_data, buf);
  1869. if (isjson && io_open)
  1870. io_close(io_data);
  1871. }
  1872. #ifdef WANT_CPUMINE
  1873. static void cpuenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1874. {
  1875. int id;
  1876. if (opt_n_threads == 0) {
  1877. message(io_data, MSG_CPUNON, 0, NULL, isjson);
  1878. return;
  1879. }
  1880. if (param == NULL || *param == '\0') {
  1881. message(io_data, MSG_MISID, 0, NULL, isjson);
  1882. return;
  1883. }
  1884. id = atoi(param);
  1885. if (id < 0 || id >= opt_n_threads) {
  1886. message(io_data, MSG_INVCPU, id, NULL, isjson);
  1887. return;
  1888. }
  1889. applog(LOG_DEBUG, "API: request to cpuenable cpuid %d %s",
  1890. id, cpus[id].proc_repr_ns);
  1891. if (cpus[id].deven != DEV_DISABLED) {
  1892. message(io_data, MSG_ALRENAC, id, NULL, isjson);
  1893. return;
  1894. }
  1895. if (cpus[id].status != LIFE_WELL)
  1896. {
  1897. message(io_data, MSG_CPUMRE, id, NULL, isjson);
  1898. return;
  1899. }
  1900. proc_enable(&cpus[id]);
  1901. message(io_data, MSG_CPUREN, id, NULL, isjson);
  1902. }
  1903. static void cpudisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1904. {
  1905. int id;
  1906. if (opt_n_threads == 0) {
  1907. message(io_data, MSG_CPUNON, 0, NULL, isjson);
  1908. return;
  1909. }
  1910. if (param == NULL || *param == '\0') {
  1911. message(io_data, MSG_MISID, 0, NULL, isjson);
  1912. return;
  1913. }
  1914. id = atoi(param);
  1915. if (id < 0 || id >= opt_n_threads) {
  1916. message(io_data, MSG_INVCPU, id, NULL, isjson);
  1917. return;
  1918. }
  1919. applog(LOG_DEBUG, "API: request to cpudisable cpuid %d %s",
  1920. id, cpus[id].proc_repr_ns);
  1921. if (cpus[id].deven == DEV_DISABLED) {
  1922. message(io_data, MSG_ALRDISC, id, NULL, isjson);
  1923. return;
  1924. }
  1925. cpus[id].deven = DEV_DISABLED;
  1926. message(io_data, MSG_CPUDIS, id, NULL, isjson);
  1927. }
  1928. static void cpurestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1929. {
  1930. int id;
  1931. if (opt_n_threads == 0) {
  1932. message(io_data, MSG_CPUNON, 0, NULL, isjson);
  1933. return;
  1934. }
  1935. if (param == NULL || *param == '\0') {
  1936. message(io_data, MSG_MISID, 0, NULL, isjson);
  1937. return;
  1938. }
  1939. id = atoi(param);
  1940. if (id < 0 || id >= opt_n_threads) {
  1941. message(io_data, MSG_INVCPU, id, NULL, isjson);
  1942. return;
  1943. }
  1944. reinit_device(&cpus[id]);
  1945. message(io_data, MSG_CPUREI, id, NULL, isjson);
  1946. }
  1947. #endif
  1948. static void cpucount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1949. {
  1950. struct api_data *root = NULL;
  1951. char buf[TMPBUFSIZ];
  1952. bool io_open;
  1953. int count = 0;
  1954. #ifdef WANT_CPUMINE
  1955. count = opt_n_threads > 0 ? num_processors : 0;
  1956. #endif
  1957. message(io_data, MSG_NUMCPU, 0, NULL, isjson);
  1958. io_open = io_add(io_data, isjson ? COMSTR JSON_CPUS : _CPUS COMSTR);
  1959. root = api_add_int(root, "Count", &count, false);
  1960. root = print_data(root, buf, isjson, false);
  1961. io_add(io_data, buf);
  1962. if (isjson && io_open)
  1963. io_close(io_data);
  1964. }
  1965. static void switchpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1966. {
  1967. struct pool *pool;
  1968. int id;
  1969. if (total_pools == 0) {
  1970. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  1971. return;
  1972. }
  1973. if (param == NULL || *param == '\0') {
  1974. message(io_data, MSG_MISPID, 0, NULL, isjson);
  1975. return;
  1976. }
  1977. id = atoi(param);
  1978. cg_rlock(&control_lock);
  1979. if (id < 0 || id >= total_pools) {
  1980. cg_runlock(&control_lock);
  1981. message(io_data, MSG_INVPID, id, NULL, isjson);
  1982. return;
  1983. }
  1984. pool = pools[id];
  1985. pool->enabled = POOL_ENABLED;
  1986. cg_runlock(&control_lock);
  1987. switch_pools(pool);
  1988. message(io_data, MSG_SWITCHP, id, NULL, isjson);
  1989. }
  1990. static void copyadvanceafter(char ch, char **param, char **buf)
  1991. {
  1992. #define src_p (*param)
  1993. #define dst_b (*buf)
  1994. while (*src_p && *src_p != ch) {
  1995. if (*src_p == '\\' && *(src_p+1) != '\0')
  1996. src_p++;
  1997. *(dst_b++) = *(src_p++);
  1998. }
  1999. if (*src_p)
  2000. src_p++;
  2001. *(dst_b++) = '\0';
  2002. }
  2003. static bool pooldetails(char *param, char **url, char **user, char **pass)
  2004. {
  2005. char *ptr, *buf;
  2006. ptr = buf = malloc(strlen(param)+1);
  2007. if (unlikely(!buf))
  2008. quit(1, "Failed to malloc pooldetails buf");
  2009. *url = buf;
  2010. // copy url
  2011. copyadvanceafter(',', &param, &buf);
  2012. if (!(*param)) // missing user
  2013. goto exitsama;
  2014. *user = buf;
  2015. // copy user
  2016. copyadvanceafter(',', &param, &buf);
  2017. if (!*param) // missing pass
  2018. goto exitsama;
  2019. *pass = buf;
  2020. // copy pass
  2021. copyadvanceafter(',', &param, &buf);
  2022. return true;
  2023. exitsama:
  2024. free(ptr);
  2025. return false;
  2026. }
  2027. static void addpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2028. {
  2029. char *url, *user, *pass;
  2030. struct pool *pool;
  2031. char *ptr;
  2032. if (param == NULL || *param == '\0') {
  2033. message(io_data, MSG_MISPDP, 0, NULL, isjson);
  2034. return;
  2035. }
  2036. if (!pooldetails(param, &url, &user, &pass)) {
  2037. ptr = escape_string(param, isjson);
  2038. message(io_data, MSG_INVPDP, 0, ptr, isjson);
  2039. if (ptr != param)
  2040. free(ptr);
  2041. ptr = NULL;
  2042. return;
  2043. }
  2044. pool = add_pool();
  2045. detect_stratum(pool, url);
  2046. add_pool_details(pool, true, url, user, pass);
  2047. ptr = escape_string(url, isjson);
  2048. message(io_data, MSG_ADDPOOL, 0, ptr, isjson);
  2049. if (ptr != url)
  2050. free(ptr);
  2051. ptr = NULL;
  2052. }
  2053. static void enablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2054. {
  2055. struct pool *pool;
  2056. int id;
  2057. if (total_pools == 0) {
  2058. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2059. return;
  2060. }
  2061. if (param == NULL || *param == '\0') {
  2062. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2063. return;
  2064. }
  2065. id = atoi(param);
  2066. if (id < 0 || id >= total_pools) {
  2067. message(io_data, MSG_INVPID, id, NULL, isjson);
  2068. return;
  2069. }
  2070. pool = pools[id];
  2071. if (pool->enabled == POOL_ENABLED) {
  2072. message(io_data, MSG_ALRENAP, id, NULL, isjson);
  2073. return;
  2074. }
  2075. pool->enabled = POOL_ENABLED;
  2076. if (pool->prio < current_pool()->prio)
  2077. switch_pools(pool);
  2078. message(io_data, MSG_ENAPOOL, id, NULL, isjson);
  2079. }
  2080. static void poolpriority(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2081. {
  2082. int i;
  2083. switch (prioritize_pools(param, &i)) {
  2084. case MSG_NOPOOL:
  2085. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2086. return;
  2087. case MSG_MISPID:
  2088. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2089. return;
  2090. case MSG_INVPID:
  2091. message(io_data, MSG_INVPID, i, NULL, isjson);
  2092. return;
  2093. case MSG_DUPPID:
  2094. message(io_data, MSG_DUPPID, i, NULL, isjson);
  2095. return;
  2096. case MSG_POOLPRIO:
  2097. default:
  2098. message(io_data, MSG_POOLPRIO, 0, NULL, isjson);
  2099. return;
  2100. }
  2101. }
  2102. static void poolquota(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2103. {
  2104. struct pool *pool;
  2105. int quota, id;
  2106. char *comma;
  2107. if (total_pools == 0) {
  2108. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2109. return;
  2110. }
  2111. if (param == NULL || *param == '\0') {
  2112. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2113. return;
  2114. }
  2115. comma = strchr(param, ',');
  2116. if (!comma) {
  2117. message(io_data, MSG_CONVAL, 0, param, isjson);
  2118. return;
  2119. }
  2120. *(comma++) = '\0';
  2121. id = atoi(param);
  2122. if (id < 0 || id >= total_pools) {
  2123. message(io_data, MSG_INVPID, id, NULL, isjson);
  2124. return;
  2125. }
  2126. pool = pools[id];
  2127. quota = atoi(comma);
  2128. if (quota < 0) {
  2129. message(io_data, MSG_INVNEG, quota, pool->rpc_url, isjson);
  2130. return;
  2131. }
  2132. pool->quota = quota;
  2133. adjust_quota_gcd();
  2134. message(io_data, MSG_SETQUOTA, quota, pool->rpc_url, isjson);
  2135. }
  2136. static void disablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2137. {
  2138. struct pool *pool;
  2139. int id;
  2140. if (total_pools == 0) {
  2141. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2142. return;
  2143. }
  2144. if (param == NULL || *param == '\0') {
  2145. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2146. return;
  2147. }
  2148. id = atoi(param);
  2149. if (id < 0 || id >= total_pools) {
  2150. message(io_data, MSG_INVPID, id, NULL, isjson);
  2151. return;
  2152. }
  2153. pool = pools[id];
  2154. if (pool->enabled == POOL_DISABLED) {
  2155. message(io_data, MSG_ALRDISP, id, NULL, isjson);
  2156. return;
  2157. }
  2158. if (enabled_pools <= 1) {
  2159. message(io_data, MSG_DISLASTP, id, NULL, isjson);
  2160. return;
  2161. }
  2162. pool->enabled = POOL_DISABLED;
  2163. if (pool == current_pool())
  2164. switch_pools(NULL);
  2165. message(io_data, MSG_DISPOOL, id, NULL, isjson);
  2166. }
  2167. static void removepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2168. {
  2169. struct pool *pool;
  2170. char *rpc_url;
  2171. bool dofree = false;
  2172. int id;
  2173. if (total_pools == 0) {
  2174. message(io_data, MSG_NOPOOL, 0, NULL, isjson);
  2175. return;
  2176. }
  2177. if (param == NULL || *param == '\0') {
  2178. message(io_data, MSG_MISPID, 0, NULL, isjson);
  2179. return;
  2180. }
  2181. id = atoi(param);
  2182. if (id < 0 || id >= total_pools) {
  2183. message(io_data, MSG_INVPID, id, NULL, isjson);
  2184. return;
  2185. }
  2186. if (total_pools <= 1) {
  2187. message(io_data, MSG_REMLASTP, id, NULL, isjson);
  2188. return;
  2189. }
  2190. pool = pools[id];
  2191. if (pool == current_pool())
  2192. switch_pools(NULL);
  2193. if (pool == current_pool()) {
  2194. message(io_data, MSG_ACTPOOL, id, NULL, isjson);
  2195. return;
  2196. }
  2197. pool->enabled = POOL_DISABLED;
  2198. rpc_url = escape_string(pool->rpc_url, isjson);
  2199. if (rpc_url != pool->rpc_url)
  2200. dofree = true;
  2201. remove_pool(pool);
  2202. message(io_data, MSG_REMPOOL, id, rpc_url, isjson);
  2203. if (dofree)
  2204. free(rpc_url);
  2205. rpc_url = NULL;
  2206. }
  2207. #ifdef HAVE_OPENCL
  2208. static bool splitgpuvalue(struct io_data *io_data, char *param, int *gpu, char **value, bool isjson)
  2209. {
  2210. int id;
  2211. char *gpusep;
  2212. if (nDevs == 0) {
  2213. message(io_data, MSG_GPUNON, 0, NULL, isjson);
  2214. return false;
  2215. }
  2216. if (param == NULL || *param == '\0') {
  2217. message(io_data, MSG_MISID, 0, NULL, isjson);
  2218. return false;
  2219. }
  2220. gpusep = strchr(param, GPUSEP);
  2221. if (gpusep == NULL) {
  2222. message(io_data, MSG_MISVAL, 0, NULL, isjson);
  2223. return false;
  2224. }
  2225. *(gpusep++) = '\0';
  2226. id = atoi(param);
  2227. if (id < 0 || id >= nDevs) {
  2228. message(io_data, MSG_INVGPU, id, NULL, isjson);
  2229. return false;
  2230. }
  2231. *gpu = id;
  2232. *value = gpusep;
  2233. return true;
  2234. }
  2235. static void gpuintensity(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2236. {
  2237. int id;
  2238. char *value;
  2239. char intensitystr[7];
  2240. char buf[TMPBUFSIZ];
  2241. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2242. return;
  2243. struct cgpu_info * const cgpu = &gpus[id];
  2244. struct opencl_device_data * const data = gpus[id].device_data;
  2245. enum bfg_set_device_replytype success;
  2246. proc_set_device(cgpu, "intensity", value, buf, &success);
  2247. if (success == SDR_OK)
  2248. {
  2249. if (data->dynamic)
  2250. strcpy(intensitystr, DYNAMIC);
  2251. else
  2252. snprintf(intensitystr, sizeof(intensitystr), "%g", oclthreads_to_intensity(data->oclthreads, !opt_scrypt));
  2253. }
  2254. else
  2255. {
  2256. message(io_data, MSG_INVINT, 0, value, isjson);
  2257. return;
  2258. }
  2259. message(io_data, MSG_GPUINT, id, intensitystr, isjson);
  2260. }
  2261. static void gpumem(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2262. {
  2263. #ifdef HAVE_ADL
  2264. int id;
  2265. char *value;
  2266. char buf[TMPBUFSIZ];
  2267. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2268. return;
  2269. struct cgpu_info * const cgpu = &gpus[id];
  2270. enum bfg_set_device_replytype success;
  2271. proc_set_device(cgpu, "memclock", value, buf, &success);
  2272. if (success != SDR_OK)
  2273. message(io_data, MSG_GPUMERR, id, value, isjson);
  2274. else
  2275. message(io_data, MSG_GPUMEM, id, value, isjson);
  2276. #else
  2277. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2278. #endif
  2279. }
  2280. static void gpuengine(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2281. {
  2282. #ifdef HAVE_ADL
  2283. int id;
  2284. char *value;
  2285. char buf[TMPBUFSIZ];
  2286. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2287. return;
  2288. struct cgpu_info * const cgpu = &gpus[id];
  2289. enum bfg_set_device_replytype success;
  2290. proc_set_device(cgpu, "clock", value, buf, &success);
  2291. if (success != SDR_OK)
  2292. message(io_data, MSG_GPUEERR, id, value, isjson);
  2293. else
  2294. message(io_data, MSG_GPUENG, id, value, isjson);
  2295. #else
  2296. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2297. #endif
  2298. }
  2299. static void gpufan(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2300. {
  2301. #ifdef HAVE_ADL
  2302. int id;
  2303. char *value;
  2304. char buf[TMPBUFSIZ];
  2305. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2306. return;
  2307. struct cgpu_info * const cgpu = &gpus[id];
  2308. enum bfg_set_device_replytype success;
  2309. proc_set_device(cgpu, "fan", value, buf, &success);
  2310. if (success != SDR_OK)
  2311. message(io_data, MSG_GPUFERR, id, value, isjson);
  2312. else
  2313. message(io_data, MSG_GPUFAN, id, value, isjson);
  2314. #else
  2315. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2316. #endif
  2317. }
  2318. static void gpuvddc(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2319. {
  2320. #ifdef HAVE_ADL
  2321. int id;
  2322. char *value;
  2323. char buf[TMPBUFSIZ];
  2324. if (!splitgpuvalue(io_data, param, &id, &value, isjson))
  2325. return;
  2326. struct cgpu_info * const cgpu = &gpus[id];
  2327. enum bfg_set_device_replytype success;
  2328. proc_set_device(cgpu, "voltage", value, buf, &success);
  2329. if (success != SDR_OK)
  2330. message(io_data, MSG_GPUVERR, id, value, isjson);
  2331. else
  2332. message(io_data, MSG_GPUVDDC, id, value, isjson);
  2333. #else
  2334. message(io_data, MSG_NOADL, 0, NULL, isjson);
  2335. #endif
  2336. }
  2337. #endif
  2338. void doquit(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2339. {
  2340. if (isjson)
  2341. io_put(io_data, JSON_START JSON_BYE);
  2342. else
  2343. io_put(io_data, _BYE);
  2344. bye = true;
  2345. do_a_quit = true;
  2346. }
  2347. void dorestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2348. {
  2349. if (isjson)
  2350. io_put(io_data, JSON_START JSON_RESTART);
  2351. else
  2352. io_put(io_data, _RESTART);
  2353. bye = true;
  2354. do_a_restart = true;
  2355. }
  2356. void privileged(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2357. {
  2358. message(io_data, MSG_ACCOK, 0, NULL, isjson);
  2359. }
  2360. void notifystatus(struct io_data *io_data, int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
  2361. {
  2362. struct cgpu_info *proc;
  2363. struct api_data *root = NULL;
  2364. char buf[TMPBUFSIZ];
  2365. char *reason;
  2366. time_t last_not_well = 0;
  2367. enum dev_reason uninitialised_var(enum_reason);
  2368. int thread_fail_init_count = 0, thread_zero_hash_count = 0, thread_fail_queue_count = 0;
  2369. int dev_sick_idle_60_count = 0, dev_dead_idle_600_count = 0;
  2370. int dev_nostart_count = 0, dev_over_heat_count = 0, dev_thermal_cutoff_count = 0, dev_comms_error_count = 0, dev_throttle_count = 0;
  2371. int procs = per_proc ? 1 : cgpu->procs, i;
  2372. for (i = 0, proc = cgpu; i < procs; ++i, proc = proc->next_proc)
  2373. {
  2374. if (proc->device_last_not_well > last_not_well)
  2375. {
  2376. last_not_well = proc->device_last_not_well;
  2377. enum_reason = proc->device_not_well_reason;
  2378. thread_fail_init_count += proc->thread_fail_init_count;
  2379. thread_zero_hash_count += proc->thread_zero_hash_count;
  2380. thread_fail_queue_count += proc->thread_fail_queue_count;
  2381. dev_sick_idle_60_count += proc->dev_sick_idle_60_count;
  2382. dev_dead_idle_600_count += proc->dev_dead_idle_600_count;
  2383. dev_nostart_count += proc->dev_nostart_count;
  2384. dev_over_heat_count += proc->dev_over_heat_count;
  2385. dev_thermal_cutoff_count += proc->dev_thermal_cutoff_count;
  2386. dev_comms_error_count += proc->dev_comms_error_count;
  2387. dev_throttle_count += proc->dev_throttle_count;
  2388. }
  2389. if (per_proc)
  2390. break;
  2391. }
  2392. if (last_not_well == 0)
  2393. reason = REASON_NONE;
  2394. else
  2395. switch (enum_reason)
  2396. {
  2397. case REASON_THREAD_FAIL_INIT:
  2398. reason = REASON_THREAD_FAIL_INIT_STR;
  2399. break;
  2400. case REASON_THREAD_ZERO_HASH:
  2401. reason = REASON_THREAD_ZERO_HASH_STR;
  2402. break;
  2403. case REASON_THREAD_FAIL_QUEUE:
  2404. reason = REASON_THREAD_FAIL_QUEUE_STR;
  2405. break;
  2406. case REASON_DEV_SICK_IDLE_60:
  2407. reason = REASON_DEV_SICK_IDLE_60_STR;
  2408. break;
  2409. case REASON_DEV_DEAD_IDLE_600:
  2410. reason = REASON_DEV_DEAD_IDLE_600_STR;
  2411. break;
  2412. case REASON_DEV_NOSTART:
  2413. reason = REASON_DEV_NOSTART_STR;
  2414. break;
  2415. case REASON_DEV_OVER_HEAT:
  2416. reason = REASON_DEV_OVER_HEAT_STR;
  2417. break;
  2418. case REASON_DEV_THERMAL_CUTOFF:
  2419. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  2420. break;
  2421. case REASON_DEV_COMMS_ERROR:
  2422. reason = REASON_DEV_COMMS_ERROR_STR;
  2423. break;
  2424. default:
  2425. reason = REASON_UNKNOWN_STR;
  2426. break;
  2427. }
  2428. // ALL counters (and only counters) must start the name with a '*'
  2429. // Simplifies future external support for identifying new counters
  2430. root = api_add_int(root, "NOTIFY", &device, false);
  2431. root = api_add_device_identifier(root, cgpu);
  2432. if (per_proc)
  2433. root = api_add_time(root, "Last Well", &(cgpu->device_last_well), false);
  2434. root = api_add_time(root, "Last Not Well", &last_not_well, false);
  2435. root = api_add_string(root, "Reason Not Well", reason, false);
  2436. root = api_add_int(root, "*Thread Fail Init", &thread_fail_init_count, false);
  2437. root = api_add_int(root, "*Thread Zero Hash", &thread_zero_hash_count, false);
  2438. root = api_add_int(root, "*Thread Fail Queue", &thread_fail_queue_count, false);
  2439. root = api_add_int(root, "*Dev Sick Idle 60s", &dev_sick_idle_60_count, false);
  2440. root = api_add_int(root, "*Dev Dead Idle 600s", &dev_dead_idle_600_count, false);
  2441. root = api_add_int(root, "*Dev Nostart", &dev_nostart_count, false);
  2442. root = api_add_int(root, "*Dev Over Heat", &dev_over_heat_count, false);
  2443. root = api_add_int(root, "*Dev Thermal Cutoff", &dev_thermal_cutoff_count, false);
  2444. root = api_add_int(root, "*Dev Comms Error", &dev_comms_error_count, false);
  2445. root = api_add_int(root, "*Dev Throttle", &dev_throttle_count, false);
  2446. root = print_data(root, buf, isjson, isjson && (device > 0));
  2447. io_add(io_data, buf);
  2448. }
  2449. static
  2450. void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
  2451. {
  2452. struct cgpu_info *cgpu;
  2453. bool io_open = false;
  2454. int i, n = 0;
  2455. if (total_devices == 0) {
  2456. message(io_data, MSG_NODEVS, 0, NULL, isjson);
  2457. return;
  2458. }
  2459. message(io_data, MSG_NOTIFY, 0, NULL, isjson);
  2460. if (isjson)
  2461. io_open = io_add(io_data, COMSTR JSON_NOTIFY);
  2462. for (i = 0; i < total_devices; i++) {
  2463. cgpu = get_devices(i);
  2464. if (cgpu->device == cgpu || per_proc)
  2465. notifystatus(io_data, n++, cgpu, isjson, group);
  2466. }
  2467. if (isjson && io_open)
  2468. io_close(io_data);
  2469. }
  2470. void dosave(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2471. {
  2472. char filename[PATH_MAX];
  2473. FILE *fcfg;
  2474. char *ptr;
  2475. if (param == NULL || *param == '\0') {
  2476. default_save_file(filename);
  2477. param = filename;
  2478. }
  2479. fcfg = fopen(param, "w");
  2480. if (!fcfg) {
  2481. ptr = escape_string(param, isjson);
  2482. message(io_data, MSG_BADFN, 0, ptr, isjson);
  2483. if (ptr != param)
  2484. free(ptr);
  2485. ptr = NULL;
  2486. return;
  2487. }
  2488. write_config(fcfg);
  2489. fclose(fcfg);
  2490. ptr = escape_string(param, isjson);
  2491. message(io_data, MSG_SAVED, 0, ptr, isjson);
  2492. if (ptr != param)
  2493. free(ptr);
  2494. ptr = NULL;
  2495. }
  2496. 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)
  2497. {
  2498. struct api_data *root = NULL;
  2499. char buf[TMPBUFSIZ];
  2500. double elapsed;
  2501. root = api_add_int(root, "STATS", &i, false);
  2502. root = api_add_string(root, "ID", id, false);
  2503. elapsed = stats_elapsed(stats);
  2504. root = api_add_elapsed(root, "Elapsed", &elapsed, false);
  2505. root = api_add_uint32(root, "Calls", &(stats->getwork_calls), false);
  2506. root = api_add_timeval(root, "Wait", &(stats->getwork_wait), false);
  2507. root = api_add_timeval(root, "Max", &(stats->getwork_wait_max), false);
  2508. root = api_add_timeval(root, "Min", &(stats->getwork_wait_min), false);
  2509. if (pool_stats) {
  2510. root = api_add_uint32(root, "Pool Calls", &(pool_stats->getwork_calls), false);
  2511. root = api_add_uint32(root, "Pool Attempts", &(pool_stats->getwork_attempts), false);
  2512. root = api_add_timeval(root, "Pool Wait", &(pool_stats->getwork_wait), false);
  2513. root = api_add_timeval(root, "Pool Max", &(pool_stats->getwork_wait_max), false);
  2514. root = api_add_timeval(root, "Pool Min", &(pool_stats->getwork_wait_min), false);
  2515. root = api_add_double(root, "Pool Av", &(pool_stats->getwork_wait_rolling), false);
  2516. root = api_add_bool(root, "Work Had Roll Time", &(pool_stats->hadrolltime), false);
  2517. root = api_add_bool(root, "Work Can Roll", &(pool_stats->canroll), false);
  2518. root = api_add_bool(root, "Work Had Expire", &(pool_stats->hadexpire), false);
  2519. root = api_add_uint32(root, "Work Roll Time", &(pool_stats->rolltime), false);
  2520. root = api_add_diff(root, "Work Diff", &(pool_stats->last_diff), false);
  2521. root = api_add_diff(root, "Min Diff", &(pool_stats->min_diff), false);
  2522. root = api_add_diff(root, "Max Diff", &(pool_stats->max_diff), false);
  2523. root = api_add_uint32(root, "Min Diff Count", &(pool_stats->min_diff_count), false);
  2524. root = api_add_uint32(root, "Max Diff Count", &(pool_stats->max_diff_count), false);
  2525. root = api_add_uint64(root, "Times Sent", &(pool_stats->times_sent), false);
  2526. root = api_add_uint64(root, "Bytes Sent", &(pool_stats->bytes_sent), false);
  2527. root = api_add_uint64(root, "Times Recv", &(pool_stats->times_received), false);
  2528. root = api_add_uint64(root, "Bytes Recv", &(pool_stats->bytes_received), false);
  2529. root = api_add_uint64(root, "Net Bytes Sent", &(pool_stats->net_bytes_sent), false);
  2530. root = api_add_uint64(root, "Net Bytes Recv", &(pool_stats->net_bytes_received), false);
  2531. }
  2532. if (extra)
  2533. root = api_add_extra(root, extra);
  2534. root = print_data(root, buf, isjson, isjson && (i > 0));
  2535. io_add(io_data, buf);
  2536. return ++i;
  2537. }
  2538. static void minerstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2539. {
  2540. struct cgpu_info *cgpu;
  2541. bool io_open = false;
  2542. struct api_data *extra;
  2543. char id[20];
  2544. int i, j;
  2545. message(io_data, MSG_MINESTATS, 0, NULL, isjson);
  2546. if (isjson)
  2547. io_open = io_add(io_data, COMSTR JSON_MINESTATS);
  2548. i = 0;
  2549. for (j = 0; j < total_devices; j++) {
  2550. cgpu = get_devices(j);
  2551. if (cgpu && cgpu->drv) {
  2552. if (cgpu->drv->get_api_stats)
  2553. extra = cgpu->drv->get_api_stats(cgpu);
  2554. else
  2555. extra = NULL;
  2556. i = itemstats(io_data, i, cgpu->proc_repr_ns, &(cgpu->cgminer_stats), NULL, extra, isjson);
  2557. }
  2558. }
  2559. for (j = 0; j < total_pools; j++) {
  2560. struct pool *pool = pools[j];
  2561. sprintf(id, "POOL%d", j);
  2562. i = itemstats(io_data, i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, isjson);
  2563. }
  2564. if (isjson && io_open)
  2565. io_close(io_data);
  2566. }
  2567. static void failoveronly(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2568. {
  2569. if (param == NULL || *param == '\0') {
  2570. message(io_data, MSG_MISBOOL, 0, NULL, isjson);
  2571. return;
  2572. }
  2573. *param = tolower(*param);
  2574. if (*param != 't' && *param != 'f') {
  2575. message(io_data, MSG_INVBOOL, 0, NULL, isjson);
  2576. return;
  2577. }
  2578. bool tf = (*param == 't');
  2579. opt_fail_only = tf;
  2580. message(io_data, MSG_FOO, tf, NULL, isjson);
  2581. }
  2582. static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2583. {
  2584. struct api_data *root = NULL;
  2585. char buf[TMPBUFSIZ];
  2586. bool io_open;
  2587. message(io_data, MSG_MINECOIN, 0, NULL, isjson);
  2588. io_open = io_add(io_data, isjson ? COMSTR JSON_MINECOIN : _MINECOIN COMSTR);
  2589. #ifdef USE_SCRYPT
  2590. if (opt_scrypt)
  2591. root = api_add_const(root, "Hash Method", SCRYPTSTR, false);
  2592. else
  2593. #endif
  2594. root = api_add_const(root, "Hash Method", SHA256STR, false);
  2595. cg_rlock(&ch_lock);
  2596. if (current_fullhash && *current_fullhash) {
  2597. root = api_add_time(root, "Current Block Time", &block_time, true);
  2598. root = api_add_string(root, "Current Block Hash", current_fullhash, true);
  2599. } else {
  2600. time_t t = 0;
  2601. root = api_add_time(root, "Current Block Time", &t, true);
  2602. root = api_add_const(root, "Current Block Hash", BLANK, false);
  2603. }
  2604. cg_runlock(&ch_lock);
  2605. root = api_add_bool(root, "LP", &have_longpoll, false);
  2606. root = api_add_diff(root, "Network Difficulty", &current_diff, true);
  2607. root = print_data(root, buf, isjson, false);
  2608. io_add(io_data, buf);
  2609. if (isjson && io_open)
  2610. io_close(io_data);
  2611. }
  2612. static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2613. {
  2614. struct api_data *root = NULL;
  2615. char buf[TMPBUFSIZ];
  2616. bool io_open;
  2617. if (param == NULL)
  2618. param = (char *)BLANK;
  2619. else
  2620. *param = tolower(*param);
  2621. switch(*param) {
  2622. case 's':
  2623. opt_realquiet = true;
  2624. break;
  2625. case 'q':
  2626. opt_quiet ^= true;
  2627. break;
  2628. case 'v':
  2629. opt_log_output ^= true;
  2630. if (opt_log_output)
  2631. opt_quiet = false;
  2632. break;
  2633. case 'd':
  2634. opt_debug ^= true;
  2635. opt_log_output = opt_debug;
  2636. if (opt_debug)
  2637. opt_quiet = false;
  2638. break;
  2639. case 'r':
  2640. opt_protocol ^= true;
  2641. if (opt_protocol)
  2642. opt_quiet = false;
  2643. break;
  2644. case 'p':
  2645. want_per_device_stats ^= true;
  2646. opt_log_output = want_per_device_stats;
  2647. break;
  2648. case 'n':
  2649. opt_log_output = false;
  2650. opt_debug = false;
  2651. opt_quiet = false;
  2652. opt_protocol = false;
  2653. want_per_device_stats = false;
  2654. opt_worktime = false;
  2655. break;
  2656. case 'w':
  2657. opt_worktime ^= true;
  2658. break;
  2659. #ifdef _MEMORY_DEBUG
  2660. case 'y':
  2661. cgmemspeedup();
  2662. break;
  2663. case 'z':
  2664. cgmemrpt();
  2665. break;
  2666. #endif
  2667. default:
  2668. // anything else just reports the settings
  2669. break;
  2670. }
  2671. message(io_data, MSG_DEBUGSET, 0, NULL, isjson);
  2672. io_open = io_add(io_data, isjson ? COMSTR JSON_DEBUGSET : _DEBUGSET COMSTR);
  2673. root = api_add_bool(root, "Silent", &opt_realquiet, false);
  2674. root = api_add_bool(root, "Quiet", &opt_quiet, false);
  2675. root = api_add_bool(root, "Verbose", &opt_log_output, false);
  2676. root = api_add_bool(root, "Debug", &opt_debug, false);
  2677. root = api_add_bool(root, "RPCProto", &opt_protocol, false);
  2678. root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
  2679. root = api_add_bool(root, "WorkTime", &opt_worktime, false);
  2680. root = print_data(root, buf, isjson, false);
  2681. io_add(io_data, buf);
  2682. if (isjson && io_open)
  2683. io_close(io_data);
  2684. }
  2685. extern void stratumsrv_change_port();
  2686. static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2687. {
  2688. char *comma;
  2689. int value;
  2690. if (param == NULL || *param == '\0') {
  2691. message(io_data, MSG_CONPAR, 0, NULL, isjson);
  2692. return;
  2693. }
  2694. comma = strchr(param, ',');
  2695. if (!comma) {
  2696. message(io_data, MSG_CONVAL, 0, param, isjson);
  2697. return;
  2698. }
  2699. *(comma++) = '\0';
  2700. #if BLKMAKER_VERSION > 0
  2701. if (strcasecmp(param, "coinbase-sig") == 0) {
  2702. free(opt_coinbase_sig);
  2703. opt_coinbase_sig = strdup(comma);
  2704. message(io_data, MSG_SETCONFIG, 1, param, isjson);
  2705. return;
  2706. }
  2707. #endif
  2708. value = atoi(comma);
  2709. if (value < 0 || value > 9999) {
  2710. message(io_data, MSG_INVNUM, value, param, isjson);
  2711. return;
  2712. }
  2713. if (strcasecmp(param, "queue") == 0)
  2714. opt_queue = value;
  2715. else if (strcasecmp(param, "scantime") == 0)
  2716. opt_scantime = value;
  2717. else if (strcasecmp(param, "expiry") == 0)
  2718. opt_expiry = value;
  2719. #ifdef USE_LIBMICROHTTPD
  2720. else if (strcasecmp(param, "http-port") == 0)
  2721. {
  2722. httpsrv_stop();
  2723. httpsrv_port = value;
  2724. if (httpsrv_port != -1)
  2725. httpsrv_start(httpsrv_port);
  2726. }
  2727. #endif
  2728. #ifdef USE_LIBEVENT
  2729. else if (strcasecmp(param, "stratum-port") == 0)
  2730. {
  2731. stratumsrv_port = value;
  2732. stratumsrv_change_port();
  2733. }
  2734. #endif
  2735. else {
  2736. message(io_data, MSG_UNKCON, 0, param, isjson);
  2737. return;
  2738. }
  2739. message(io_data, MSG_SETCONFIG, value, param, isjson);
  2740. }
  2741. #ifdef HAVE_AN_FPGA
  2742. static void pgaset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2743. {
  2744. struct cgpu_info *cgpu;
  2745. char buf[TMPBUFSIZ];
  2746. int numpga = numpgas();
  2747. if (numpga == 0) {
  2748. message(io_data, MSG_PGANON, 0, NULL, isjson);
  2749. return;
  2750. }
  2751. if (param == NULL || *param == '\0') {
  2752. message(io_data, MSG_MISID, 0, NULL, isjson);
  2753. return;
  2754. }
  2755. char *opt = strchr(param, ',');
  2756. if (opt)
  2757. *(opt++) = '\0';
  2758. if (!opt || !*opt) {
  2759. message(io_data, MSG_MISPGAOPT, 0, NULL, isjson);
  2760. return;
  2761. }
  2762. int id = atoi(param);
  2763. if (id < 0 || id >= numpga) {
  2764. message(io_data, MSG_INVPGA, id, NULL, isjson);
  2765. return;
  2766. }
  2767. int dev = pgadevice(id);
  2768. if (dev < 0) { // Should never happen
  2769. message(io_data, MSG_INVPGA, id, NULL, isjson);
  2770. return;
  2771. }
  2772. cgpu = get_devices(dev);
  2773. char *set = strchr(opt, ',');
  2774. if (set)
  2775. *(set++) = '\0';
  2776. enum bfg_set_device_replytype success;
  2777. const char *ret = proc_set_device(cgpu, opt, set, buf, &success);
  2778. switch (success)
  2779. {
  2780. case SDR_HELP:
  2781. message(io_data, MSG_PGAHELP, id, ret, isjson);
  2782. break;
  2783. case SDR_OK:
  2784. if (ret)
  2785. message(io_data, MSG_PGASETOK | USE_ALTMSG, id, ret, isjson);
  2786. else
  2787. message(io_data, MSG_PGASETOK, id, NULL, isjson);
  2788. break;
  2789. case SDR_UNKNOWN:
  2790. case SDR_ERR:
  2791. message(io_data, MSG_PGASETERR, id, ret, isjson);
  2792. break;
  2793. case SDR_AUTO:
  2794. case SDR_NOSUPP:
  2795. message(io_data, MSG_PGANOSET, id, NULL, isjson);
  2796. }
  2797. }
  2798. #endif
  2799. static void dozero(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2800. {
  2801. if (param == NULL || *param == '\0') {
  2802. message(io_data, MSG_ZERMIS, 0, NULL, isjson);
  2803. return;
  2804. }
  2805. char *sum = strchr(param, ',');
  2806. if (sum)
  2807. *(sum++) = '\0';
  2808. if (!sum || !*sum) {
  2809. message(io_data, MSG_MISBOOL, 0, NULL, isjson);
  2810. return;
  2811. }
  2812. bool all = false;
  2813. bool bs = false;
  2814. if (strcasecmp(param, "all") == 0)
  2815. all = true;
  2816. else if (strcasecmp(param, "bestshare") == 0)
  2817. bs = true;
  2818. if (all == false && bs == false) {
  2819. message(io_data, MSG_ZERINV, 0, param, isjson);
  2820. return;
  2821. }
  2822. *sum = tolower(*sum);
  2823. if (*sum != 't' && *sum != 'f') {
  2824. message(io_data, MSG_INVBOOL, 0, NULL, isjson);
  2825. return;
  2826. }
  2827. bool dosum = (*sum == 't');
  2828. if (dosum)
  2829. print_summary();
  2830. if (all)
  2831. zero_stats();
  2832. if (bs)
  2833. zero_bestshare();
  2834. if (dosum)
  2835. message(io_data, MSG_ZERSUM, 0, all ? "All" : "BestShare", isjson);
  2836. else
  2837. message(io_data, MSG_ZERNOSUM, 0, all ? "All" : "BestShare", isjson);
  2838. }
  2839. static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group);
  2840. struct CMDS {
  2841. char *name;
  2842. void (*func)(struct io_data *, SOCKETTYPE, char *, bool, char);
  2843. bool iswritemode;
  2844. bool joinable;
  2845. } cmds[] = {
  2846. { "version", apiversion, false, true },
  2847. { "config", minerconfig, false, true },
  2848. { "devscan", devscan, true, false },
  2849. { "devs", devstatus, false, true },
  2850. { "procs", devstatus, false, true },
  2851. { "pools", poolstatus, false, true },
  2852. { "summary", summary, false, true },
  2853. #ifdef HAVE_OPENCL
  2854. { "gpuenable", gpuenable, true, false },
  2855. { "gpudisable", gpudisable, true, false },
  2856. { "gpurestart", gpurestart, true, false },
  2857. { "gpu", gpudev, false, false },
  2858. #endif
  2859. #ifdef HAVE_AN_FPGA
  2860. { "pga", pgadev, false, false },
  2861. { "pgaenable", pgaenable, true, false },
  2862. { "pgadisable", pgadisable, true, false },
  2863. { "pgarestart", pgarestart, true, false },
  2864. { "pgaidentify", pgaidentify, true, false },
  2865. { "proc", pgadev, false, false },
  2866. { "procenable", pgaenable, true, false },
  2867. { "procdisable", pgadisable, true, false },
  2868. { "procidentify", pgaidentify, true, false },
  2869. #endif
  2870. #ifdef WANT_CPUMINE
  2871. { "cpuenable", cpuenable, true, false },
  2872. { "cpudisable", cpudisable, true, false },
  2873. { "cpurestart", cpurestart, true, false },
  2874. { "cpu", cpudev, false, false },
  2875. #endif
  2876. { "gpucount", gpucount, false, true },
  2877. { "pgacount", pgacount, false, true },
  2878. { "proccount", pgacount, false, true },
  2879. { "cpucount", cpucount, false, true },
  2880. { "switchpool", switchpool, true, false },
  2881. { "addpool", addpool, true, false },
  2882. { "poolpriority", poolpriority, true, false },
  2883. { "poolquota", poolquota, true, false },
  2884. { "enablepool", enablepool, true, false },
  2885. { "disablepool", disablepool, true, false },
  2886. { "removepool", removepool, true, false },
  2887. #ifdef HAVE_OPENCL
  2888. { "gpuintensity", gpuintensity, true, false },
  2889. { "gpumem", gpumem, true, false },
  2890. { "gpuengine", gpuengine, true, false },
  2891. { "gpufan", gpufan, true, false },
  2892. { "gpuvddc", gpuvddc, true, false },
  2893. #endif
  2894. { "save", dosave, true, false },
  2895. { "quit", doquit, true, false },
  2896. { "privileged", privileged, true, false },
  2897. { "notify", notify, false, true },
  2898. { "procnotify", notify, false, true },
  2899. { "devdetails", devdetail, false, true },
  2900. { "procdetails", devdetail, false, true },
  2901. { "restart", dorestart, true, false },
  2902. { "stats", minerstats, false, true },
  2903. { "check", checkcommand, false, false },
  2904. { "failover-only", failoveronly, true, false },
  2905. { "coin", minecoin, false, true },
  2906. { "debug", debugstate, true, false },
  2907. { "setconfig", setconfig, true, false },
  2908. #ifdef HAVE_AN_FPGA
  2909. { "pgaset", pgaset, true, false },
  2910. { "procset", pgaset, true, false },
  2911. #endif
  2912. { "zero", dozero, true, false },
  2913. { NULL, NULL, false, false }
  2914. };
  2915. static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
  2916. {
  2917. struct api_data *root = NULL;
  2918. char buf[TMPBUFSIZ];
  2919. bool io_open;
  2920. char cmdbuf[100];
  2921. bool found, access;
  2922. int i;
  2923. if (param == NULL || *param == '\0') {
  2924. message(io_data, MSG_MISCHK, 0, NULL, isjson);
  2925. return;
  2926. }
  2927. found = false;
  2928. access = false;
  2929. for (i = 0; cmds[i].name != NULL; i++) {
  2930. if (strcmp(cmds[i].name, param) == 0) {
  2931. found = true;
  2932. sprintf(cmdbuf, "|%s|", param);
  2933. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  2934. access = true;
  2935. break;
  2936. }
  2937. }
  2938. message(io_data, MSG_CHECK, 0, NULL, isjson);
  2939. io_open = io_add(io_data, isjson ? COMSTR JSON_CHECK : _CHECK COMSTR);
  2940. root = api_add_const(root, "Exists", found ? YES : NO, false);
  2941. root = api_add_const(root, "Access", access ? YES : NO, false);
  2942. root = print_data(root, buf, isjson, false);
  2943. io_add(io_data, buf);
  2944. if (isjson && io_open)
  2945. io_close(io_data);
  2946. }
  2947. static void head_join(struct io_data *io_data, char *cmdptr, bool isjson, bool *firstjoin)
  2948. {
  2949. char *ptr;
  2950. if (*firstjoin) {
  2951. if (isjson)
  2952. io_add(io_data, JSON0);
  2953. *firstjoin = false;
  2954. } else {
  2955. if (isjson)
  2956. io_add(io_data, JSON_BETWEEN_JOIN);
  2957. }
  2958. // External supplied string
  2959. ptr = escape_string(cmdptr, isjson);
  2960. if (isjson) {
  2961. io_add(io_data, JSON1);
  2962. io_add(io_data, ptr);
  2963. io_add(io_data, JSON2);
  2964. } else {
  2965. io_add(io_data, JOIN_CMD);
  2966. io_add(io_data, ptr);
  2967. io_add(io_data, BETWEEN_JOIN);
  2968. }
  2969. if (ptr != cmdptr)
  2970. free(ptr);
  2971. }
  2972. static void tail_join(struct io_data *io_data, bool isjson)
  2973. {
  2974. if (io_data->close) {
  2975. io_add(io_data, JSON_CLOSE);
  2976. io_data->close = false;
  2977. }
  2978. if (isjson) {
  2979. io_add(io_data, JSON_END);
  2980. io_add(io_data, JSON3);
  2981. }
  2982. }
  2983. static void send_result(struct io_data *io_data, SOCKETTYPE c, bool isjson)
  2984. {
  2985. if (io_data->close)
  2986. io_add(io_data, JSON_CLOSE);
  2987. if (isjson)
  2988. io_add(io_data, JSON_END);
  2989. // Null-terminate reply, including sending the \0 on the socket
  2990. bytes_append(&io_data->data, "", 1);
  2991. applog(LOG_DEBUG, "API: send reply: (%ld) '%.10s%s'",
  2992. (long)bytes_len(&io_data->data),
  2993. bytes_buf(&io_data->data),
  2994. bytes_len(&io_data->data) > 10 ? "..." : BLANK);
  2995. io_flush(io_data, true);
  2996. if (bytes_len(&io_data->data))
  2997. applog(LOG_WARNING, "RPC: Timed out with %ld bytes left to send",
  2998. (long)bytes_len(&io_data->data));
  2999. }
  3000. static void tidyup(__maybe_unused void *arg)
  3001. {
  3002. mutex_lock(&quit_restart_lock);
  3003. SOCKETTYPE *apisock = (SOCKETTYPE *)arg;
  3004. bye = true;
  3005. if (*apisock != INVSOCK) {
  3006. shutdown(*apisock, SHUT_RDWR);
  3007. CLOSESOCKET(*apisock);
  3008. *apisock = INVSOCK;
  3009. free(apisock);
  3010. }
  3011. if (ipaccess != NULL) {
  3012. free(ipaccess);
  3013. ipaccess = NULL;
  3014. }
  3015. io_free();
  3016. mutex_unlock(&quit_restart_lock);
  3017. }
  3018. /*
  3019. * Interpret --api-groups G:cmd1:cmd2:cmd3,P:cmd4,*,...
  3020. */
  3021. static void setup_groups()
  3022. {
  3023. char *api_groups = opt_api_groups ? opt_api_groups : (char *)BLANK;
  3024. char *buf, *ptr, *next, *colon;
  3025. char group;
  3026. char commands[TMPBUFSIZ];
  3027. char cmdbuf[100];
  3028. char *cmd;
  3029. bool addstar, did;
  3030. int i;
  3031. buf = malloc(strlen(api_groups) + 1);
  3032. if (unlikely(!buf))
  3033. quit(1, "Failed to malloc ipgroups buf");
  3034. strcpy(buf, api_groups);
  3035. next = buf;
  3036. // for each group defined
  3037. while (next && *next) {
  3038. ptr = next;
  3039. next = strchr(ptr, ',');
  3040. if (next)
  3041. *(next++) = '\0';
  3042. // Validate the group
  3043. if (*(ptr+1) != ':') {
  3044. colon = strchr(ptr, ':');
  3045. if (colon)
  3046. *colon = '\0';
  3047. quit(1, "API invalid group name '%s'", ptr);
  3048. }
  3049. group = GROUP(*ptr);
  3050. if (!VALIDGROUP(group))
  3051. quit(1, "API invalid group name '%c'", *ptr);
  3052. if (group == PRIVGROUP)
  3053. quit(1, "API group name can't be '%c'", PRIVGROUP);
  3054. if (group == NOPRIVGROUP)
  3055. quit(1, "API group name can't be '%c'", NOPRIVGROUP);
  3056. if (apigroups[GROUPOFFSET(group)].commands != NULL)
  3057. quit(1, "API duplicate group name '%c'", *ptr);
  3058. ptr += 2;
  3059. // Validate the command list (and handle '*')
  3060. cmd = &(commands[0]);
  3061. *(cmd++) = SEPARATOR;
  3062. *cmd = '\0';
  3063. addstar = false;
  3064. while (ptr && *ptr) {
  3065. colon = strchr(ptr, ':');
  3066. if (colon)
  3067. *(colon++) = '\0';
  3068. if (strcmp(ptr, "*") == 0)
  3069. addstar = true;
  3070. else {
  3071. did = false;
  3072. for (i = 0; cmds[i].name != NULL; i++) {
  3073. if (strcasecmp(ptr, cmds[i].name) == 0) {
  3074. did = true;
  3075. break;
  3076. }
  3077. }
  3078. if (did) {
  3079. // skip duplicates
  3080. sprintf(cmdbuf, "|%s|", cmds[i].name);
  3081. if (strstr(commands, cmdbuf) == NULL) {
  3082. strcpy(cmd, cmds[i].name);
  3083. cmd += strlen(cmds[i].name);
  3084. *(cmd++) = SEPARATOR;
  3085. *cmd = '\0';
  3086. }
  3087. } else {
  3088. quit(1, "API unknown command '%s' in group '%c'", ptr, group);
  3089. }
  3090. }
  3091. ptr = colon;
  3092. }
  3093. // * = allow all non-iswritemode commands
  3094. if (addstar) {
  3095. for (i = 0; cmds[i].name != NULL; i++) {
  3096. if (cmds[i].iswritemode == false) {
  3097. // skip duplicates
  3098. sprintf(cmdbuf, "|%s|", cmds[i].name);
  3099. if (strstr(commands, cmdbuf) == NULL) {
  3100. strcpy(cmd, cmds[i].name);
  3101. cmd += strlen(cmds[i].name);
  3102. *(cmd++) = SEPARATOR;
  3103. *cmd = '\0';
  3104. }
  3105. }
  3106. }
  3107. }
  3108. ptr = apigroups[GROUPOFFSET(group)].commands = malloc(strlen(commands) + 1);
  3109. if (unlikely(!ptr))
  3110. quit(1, "Failed to malloc group commands buf");
  3111. strcpy(ptr, commands);
  3112. }
  3113. // Now define R (NOPRIVGROUP) as all non-iswritemode commands
  3114. cmd = &(commands[0]);
  3115. *(cmd++) = SEPARATOR;
  3116. *cmd = '\0';
  3117. for (i = 0; cmds[i].name != NULL; i++) {
  3118. if (cmds[i].iswritemode == false) {
  3119. strcpy(cmd, cmds[i].name);
  3120. cmd += strlen(cmds[i].name);
  3121. *(cmd++) = SEPARATOR;
  3122. *cmd = '\0';
  3123. }
  3124. }
  3125. ptr = apigroups[GROUPOFFSET(NOPRIVGROUP)].commands = malloc(strlen(commands) + 1);
  3126. if (unlikely(!ptr))
  3127. quit(1, "Failed to malloc noprivgroup commands buf");
  3128. strcpy(ptr, commands);
  3129. // W (PRIVGROUP) is handled as a special case since it simply means all commands
  3130. free(buf);
  3131. return;
  3132. }
  3133. /*
  3134. * Interpret [W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  3135. * special case of 0/0 allows /0 (means all IP addresses)
  3136. */
  3137. #define ALLIP4 "0/0"
  3138. /*
  3139. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  3140. */
  3141. static void setup_ipaccess()
  3142. {
  3143. char *buf, *ptr, *comma, *slash, *dot;
  3144. int ipcount, mask, octet, i;
  3145. char group;
  3146. buf = malloc(strlen(opt_api_allow) + 1);
  3147. if (unlikely(!buf))
  3148. quit(1, "Failed to malloc ipaccess buf");
  3149. strcpy(buf, opt_api_allow);
  3150. ipcount = 1;
  3151. ptr = buf;
  3152. while (*ptr)
  3153. if (*(ptr++) == ',')
  3154. ipcount++;
  3155. // possibly more than needed, but never less
  3156. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  3157. if (unlikely(!ipaccess))
  3158. quit(1, "Failed to calloc ipaccess");
  3159. ips = 0;
  3160. ptr = buf;
  3161. while (ptr && *ptr) {
  3162. while (*ptr == ' ' || *ptr == '\t')
  3163. ptr++;
  3164. if (*ptr == ',') {
  3165. ptr++;
  3166. continue;
  3167. }
  3168. comma = strchr(ptr, ',');
  3169. if (comma)
  3170. *(comma++) = '\0';
  3171. group = NOPRIVGROUP;
  3172. if (VALIDGROUP(*ptr) && *(ptr+1) == ':') {
  3173. if (DEFINEDGROUP(*ptr))
  3174. group = GROUP(*ptr);
  3175. ptr += 2;
  3176. }
  3177. ipaccess[ips].group = group;
  3178. if (strcmp(ptr, ALLIP4) == 0)
  3179. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  3180. else {
  3181. slash = strchr(ptr, '/');
  3182. if (!slash)
  3183. ipaccess[ips].mask = 0xffffffff;
  3184. else {
  3185. *(slash++) = '\0';
  3186. mask = atoi(slash);
  3187. if (mask < 1 || mask > 32)
  3188. goto popipo; // skip invalid/zero
  3189. ipaccess[ips].mask = 0;
  3190. while (mask-- >= 0) {
  3191. octet = 1 << (mask % 8);
  3192. ipaccess[ips].mask |= (octet << (24 - (8 * (mask >> 3))));
  3193. }
  3194. }
  3195. ipaccess[ips].ip = 0; // missing default to '.0'
  3196. for (i = 0; ptr && (i < 4); i++) {
  3197. dot = strchr(ptr, '.');
  3198. if (dot)
  3199. *(dot++) = '\0';
  3200. octet = atoi(ptr);
  3201. if (octet < 0 || octet > 0xff)
  3202. goto popipo; // skip invalid
  3203. ipaccess[ips].ip |= (octet << (24 - (i * 8)));
  3204. ptr = dot;
  3205. }
  3206. ipaccess[ips].ip &= ipaccess[ips].mask;
  3207. }
  3208. ips++;
  3209. popipo:
  3210. ptr = comma;
  3211. }
  3212. free(buf);
  3213. }
  3214. static void *quit_thread(__maybe_unused void *userdata)
  3215. {
  3216. RenameThread("rpc_quit");
  3217. // allow thread creator to finish whatever it's doing
  3218. mutex_lock(&quit_restart_lock);
  3219. mutex_unlock(&quit_restart_lock);
  3220. if (opt_debug)
  3221. applog(LOG_DEBUG, "API: killing BFGMiner");
  3222. kill_work();
  3223. return NULL;
  3224. }
  3225. static void *restart_thread(__maybe_unused void *userdata)
  3226. {
  3227. RenameThread("rpc_restart");
  3228. // allow thread creator to finish whatever it's doing
  3229. mutex_lock(&quit_restart_lock);
  3230. mutex_unlock(&quit_restart_lock);
  3231. if (opt_debug)
  3232. applog(LOG_DEBUG, "API: restarting BFGMiner");
  3233. app_restart();
  3234. return NULL;
  3235. }
  3236. static bool check_connect(struct sockaddr_in *cli, char **connectaddr, char *group)
  3237. {
  3238. bool addrok = false;
  3239. int i;
  3240. *connectaddr = inet_ntoa(cli->sin_addr);
  3241. *group = NOPRIVGROUP;
  3242. if (opt_api_allow) {
  3243. int client_ip = htonl(cli->sin_addr.s_addr);
  3244. for (i = 0; i < ips; i++) {
  3245. if ((client_ip & ipaccess[i].mask) == ipaccess[i].ip) {
  3246. addrok = true;
  3247. *group = ipaccess[i].group;
  3248. break;
  3249. }
  3250. }
  3251. } else {
  3252. if (opt_api_network)
  3253. addrok = true;
  3254. else
  3255. addrok = (strcmp(*connectaddr, localaddr) == 0);
  3256. }
  3257. return addrok;
  3258. }
  3259. static void mcast()
  3260. {
  3261. struct sockaddr_in listen;
  3262. struct ip_mreq grp;
  3263. struct sockaddr_in came_from;
  3264. time_t bindstart;
  3265. const char *binderror;
  3266. SOCKETTYPE mcast_sock;
  3267. SOCKETTYPE reply_sock;
  3268. socklen_t came_from_siz;
  3269. char *connectaddr;
  3270. ssize_t rep;
  3271. int bound;
  3272. int count;
  3273. int reply_port;
  3274. bool addrok;
  3275. char group;
  3276. char expect[] = "cgminer-"; // first 8 bytes constant
  3277. char *expect_code;
  3278. size_t expect_code_len;
  3279. char buf[1024];
  3280. char replybuf[1024];
  3281. memset(&grp, 0, sizeof(grp));
  3282. grp.imr_multiaddr.s_addr = inet_addr(opt_api_mcast_addr);
  3283. if (grp.imr_multiaddr.s_addr == INADDR_NONE)
  3284. quit(1, "Invalid Multicast Address");
  3285. grp.imr_interface.s_addr = INADDR_ANY;
  3286. mcast_sock = socket(AF_INET, SOCK_DGRAM, 0);
  3287. int optval = 1;
  3288. if (SOCKETFAIL(setsockopt(mcast_sock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval)))) {
  3289. applog(LOG_ERR, "API mcast setsockopt SO_REUSEADDR failed (%s)%s", SOCKERRMSG, MUNAVAILABLE);
  3290. goto die;
  3291. }
  3292. memset(&listen, 0, sizeof(listen));
  3293. listen.sin_family = AF_INET;
  3294. listen.sin_addr.s_addr = INADDR_ANY;
  3295. listen.sin_port = htons(opt_api_mcast_port);
  3296. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  3297. bound = 0;
  3298. bindstart = time(NULL);
  3299. while (bound == 0) {
  3300. if (SOCKETFAIL(bind(mcast_sock, (struct sockaddr *)(&listen), sizeof(listen)))) {
  3301. binderror = SOCKERRMSG;
  3302. if ((time(NULL) - bindstart) > 61)
  3303. break;
  3304. else
  3305. cgsleep_ms(30000);
  3306. } else
  3307. bound = 1;
  3308. }
  3309. if (bound == 0) {
  3310. applog(LOG_ERR, "API mcast bind to port %d failed (%s)%s", opt_api_port, binderror, MUNAVAILABLE);
  3311. goto die;
  3312. }
  3313. if (SOCKETFAIL(setsockopt(mcast_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)(&grp), sizeof(grp)))) {
  3314. applog(LOG_ERR, "API mcast join failed (%s)%s", SOCKERRMSG, MUNAVAILABLE);
  3315. goto die;
  3316. }
  3317. expect_code_len = sizeof(expect) + strlen(opt_api_mcast_code);
  3318. expect_code = malloc(expect_code_len+1);
  3319. if (!expect_code)
  3320. quit(1, "Failed to malloc mcast expect_code");
  3321. snprintf(expect_code, expect_code_len+1, "%s%s-", expect, opt_api_mcast_code);
  3322. count = 0;
  3323. while (80085) {
  3324. cgsleep_ms(1000);
  3325. count++;
  3326. came_from_siz = sizeof(came_from);
  3327. if (SOCKETFAIL(rep = recvfrom(mcast_sock, buf, sizeof(buf) - 1,
  3328. 0, (struct sockaddr *)(&came_from), &came_from_siz))) {
  3329. applog(LOG_DEBUG, "API mcast failed count=%d (%s) (%d)",
  3330. count, SOCKERRMSG, (int)mcast_sock);
  3331. continue;
  3332. }
  3333. addrok = check_connect(&came_from, &connectaddr, &group);
  3334. applog(LOG_DEBUG, "API mcast from %s - %s",
  3335. connectaddr, addrok ? "Accepted" : "Ignored");
  3336. if (!addrok)
  3337. continue;
  3338. buf[rep] = '\0';
  3339. if (rep > 0 && buf[rep-1] == '\n')
  3340. buf[--rep] = '\0';
  3341. applog(LOG_DEBUG, "API mcast request rep=%d (%s) from %s:%d",
  3342. (int)rep, buf,
  3343. inet_ntoa(came_from.sin_addr),
  3344. ntohs(came_from.sin_port));
  3345. if ((size_t)rep > expect_code_len && memcmp(buf, expect_code, expect_code_len) == 0) {
  3346. reply_port = atoi(&buf[expect_code_len]);
  3347. if (reply_port < 1 || reply_port > 65535) {
  3348. applog(LOG_DEBUG, "API mcast request ignored - invalid port (%s)",
  3349. &buf[expect_code_len]);
  3350. } else {
  3351. applog(LOG_DEBUG, "API mcast request OK port %s=%d",
  3352. &buf[expect_code_len], reply_port);
  3353. came_from.sin_port = htons(reply_port);
  3354. reply_sock = socket(AF_INET, SOCK_DGRAM, 0);
  3355. snprintf(replybuf, sizeof(replybuf),
  3356. "cgm-%s-%d-%s",
  3357. opt_api_mcast_code,
  3358. opt_api_port, opt_api_mcast_des);
  3359. rep = sendto(reply_sock, replybuf, strlen(replybuf)+1,
  3360. 0, (struct sockaddr *)(&came_from),
  3361. sizeof(came_from));
  3362. if (SOCKETFAIL(rep)) {
  3363. applog(LOG_DEBUG, "API mcast send reply failed (%s) (%d)",
  3364. SOCKERRMSG, (int)reply_sock);
  3365. } else {
  3366. applog(LOG_DEBUG, "API mcast send reply (%s) succeeded (%d) (%d)",
  3367. replybuf, (int)rep, (int)reply_sock);
  3368. }
  3369. CLOSESOCKET(reply_sock);
  3370. }
  3371. } else
  3372. applog(LOG_DEBUG, "API mcast request was no good");
  3373. }
  3374. die:
  3375. CLOSESOCKET(mcast_sock);
  3376. }
  3377. static void *mcast_thread(void *userdata)
  3378. {
  3379. pthread_detach(pthread_self());
  3380. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  3381. RenameThread("api_mcast");
  3382. mcast();
  3383. return NULL;
  3384. }
  3385. void mcast_init()
  3386. {
  3387. struct thr_info *thr;
  3388. thr = calloc(1, sizeof(*thr));
  3389. if (!thr)
  3390. quit(1, "Failed to calloc mcast thr");
  3391. if (thr_info_create(thr, NULL, mcast_thread, thr))
  3392. quit(1, "API mcast thread create failed");
  3393. }
  3394. void api(int api_thr_id)
  3395. {
  3396. struct io_data *io_data;
  3397. struct thr_info bye_thr;
  3398. char buf[TMPBUFSIZ];
  3399. char param_buf[TMPBUFSIZ];
  3400. SOCKETTYPE c;
  3401. int n, bound;
  3402. char *connectaddr;
  3403. const char *binderror;
  3404. struct timeval bindstart;
  3405. short int port = opt_api_port;
  3406. struct sockaddr_in serv;
  3407. struct sockaddr_in cli;
  3408. socklen_t clisiz;
  3409. char cmdbuf[100];
  3410. char *cmd = NULL, *cmdptr, *cmdsbuf;
  3411. char *param;
  3412. bool addrok;
  3413. char group;
  3414. json_error_t json_err;
  3415. json_t *json_config;
  3416. json_t *json_val;
  3417. bool isjson;
  3418. bool did, isjoin, firstjoin;
  3419. int i;
  3420. SOCKETTYPE *apisock;
  3421. if (!opt_api_listen) {
  3422. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  3423. return;
  3424. }
  3425. apisock = malloc(sizeof(*apisock));
  3426. *apisock = INVSOCK;
  3427. rpc_io_data =
  3428. io_data = sock_io_new();
  3429. mutex_init(&quit_restart_lock);
  3430. pthread_cleanup_push(tidyup, (void *)apisock);
  3431. my_thr_id = api_thr_id;
  3432. setup_groups();
  3433. if (opt_api_allow) {
  3434. setup_ipaccess();
  3435. if (ips == 0) {
  3436. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  3437. return;
  3438. }
  3439. }
  3440. *apisock = socket(AF_INET, SOCK_STREAM, 0);
  3441. if (*apisock == INVSOCK) {
  3442. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3443. return;
  3444. }
  3445. memset(&serv, 0, sizeof(serv));
  3446. serv.sin_family = AF_INET;
  3447. if (!opt_api_allow && !opt_api_network) {
  3448. serv.sin_addr.s_addr = inet_addr(localaddr);
  3449. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  3450. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3451. return;
  3452. }
  3453. }
  3454. serv.sin_port = htons(port);
  3455. #ifndef WIN32
  3456. // On linux with SO_REUSEADDR, bind will get the port if the previous
  3457. // socket is closed (even if it is still in TIME_WAIT) but fail if
  3458. // another program has it open - which is what we want
  3459. int optval = 1;
  3460. // If it doesn't work, we don't really care - just show a debug message
  3461. if (SOCKETFAIL(setsockopt(*apisock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval))))
  3462. applog(LOG_DEBUG, "API setsockopt SO_REUSEADDR failed (ignored): %s", SOCKERRMSG);
  3463. #else
  3464. // On windows a 2nd program can bind to a port>1024 already in use unless
  3465. // SO_EXCLUSIVEADDRUSE is used - however then the bind to a closed port
  3466. // in TIME_WAIT will fail until the timeout - so we leave the options alone
  3467. #endif
  3468. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  3469. bound = 0;
  3470. cgtime(&bindstart);
  3471. while (bound == 0) {
  3472. if (SOCKETFAIL(bind(*apisock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  3473. binderror = SOCKERRMSG;
  3474. if (timer_elapsed(&bindstart, NULL) > 61)
  3475. break;
  3476. else {
  3477. applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
  3478. cgsleep_ms(30000);
  3479. }
  3480. } else
  3481. bound = 1;
  3482. }
  3483. if (bound == 0) {
  3484. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  3485. return;
  3486. }
  3487. if (SOCKETFAIL(listen(*apisock, QUEUE))) {
  3488. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3489. CLOSESOCKET(*apisock);
  3490. return;
  3491. }
  3492. if (opt_api_allow)
  3493. applog(LOG_WARNING, "API running in IP access mode on port %d", port);
  3494. else {
  3495. if (opt_api_network)
  3496. applog(LOG_WARNING, "API running in UNRESTRICTED read access mode on port %d", port);
  3497. else
  3498. applog(LOG_WARNING, "API running in local read access mode on port %d", port);
  3499. }
  3500. if (opt_api_mcast)
  3501. mcast_init();
  3502. while (!bye) {
  3503. clisiz = sizeof(cli);
  3504. if (SOCKETFAIL(c = accept(*apisock, (struct sockaddr *)(&cli), &clisiz))) {
  3505. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  3506. goto die;
  3507. }
  3508. addrok = check_connect(&cli, &connectaddr, &group);
  3509. applog(LOG_DEBUG, "API: connection from %s - %s",
  3510. connectaddr, addrok ? "Accepted" : "Ignored");
  3511. if (addrok) {
  3512. n = recv(c, &buf[0], TMPBUFSIZ-1, 0);
  3513. if (SOCKETFAIL(n))
  3514. buf[0] = '\0';
  3515. else
  3516. buf[n] = '\0';
  3517. if (opt_debug) {
  3518. if (SOCKETFAIL(n))
  3519. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  3520. else
  3521. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  3522. }
  3523. firstjoin = isjoin = false;
  3524. if (!SOCKETFAIL(n)) {
  3525. // the time of the request in now
  3526. when = time(NULL);
  3527. io_reinit(io_data);
  3528. io_data->sock = c;
  3529. did = false;
  3530. if (*buf != ISJSON) {
  3531. isjson = false;
  3532. param = strchr(buf, SEPARATOR);
  3533. if (param != NULL)
  3534. *(param++) = '\0';
  3535. cmd = buf;
  3536. }
  3537. else {
  3538. isjson = true;
  3539. param = NULL;
  3540. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  3541. json_config = json_loadb(buf, n, 0, &json_err);
  3542. #elif JANSSON_MAJOR_VERSION > 1
  3543. json_config = json_loads(buf, 0, &json_err);
  3544. #else
  3545. json_config = json_loads(buf, &json_err);
  3546. #endif
  3547. if (!json_is_object(json_config)) {
  3548. message(io_data, MSG_INVJSON, 0, NULL, isjson);
  3549. send_result(io_data, c, isjson);
  3550. did = true;
  3551. }
  3552. else {
  3553. json_val = json_object_get(json_config, JSON_COMMAND);
  3554. if (json_val == NULL) {
  3555. message(io_data, MSG_MISCMD, 0, NULL, isjson);
  3556. send_result(io_data, c, isjson);
  3557. did = true;
  3558. }
  3559. else {
  3560. if (!json_is_string(json_val)) {
  3561. message(io_data, MSG_INVCMD, 0, NULL, isjson);
  3562. send_result(io_data, c, isjson);
  3563. did = true;
  3564. }
  3565. else {
  3566. cmd = (char *)json_string_value(json_val);
  3567. json_val = json_object_get(json_config, JSON_PARAMETER);
  3568. if (json_is_string(json_val))
  3569. param = (char *)json_string_value(json_val);
  3570. else if (json_is_integer(json_val)) {
  3571. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  3572. param = param_buf;
  3573. } else if (json_is_real(json_val)) {
  3574. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  3575. param = param_buf;
  3576. }
  3577. }
  3578. }
  3579. }
  3580. }
  3581. if (!did) {
  3582. if (strchr(cmd, CMDJOIN)) {
  3583. firstjoin = isjoin = true;
  3584. // cmd + leading '|' + '\0'
  3585. cmdsbuf = malloc(strlen(cmd) + 2);
  3586. if (!cmdsbuf)
  3587. quithere(1, "OOM cmdsbuf");
  3588. strcpy(cmdsbuf, "|");
  3589. param = NULL;
  3590. }
  3591. cmdptr = cmd;
  3592. do {
  3593. did = false;
  3594. if (isjoin) {
  3595. cmd = strchr(cmdptr, CMDJOIN);
  3596. if (cmd)
  3597. *(cmd++) = '\0';
  3598. if (!*cmdptr)
  3599. goto inochi;
  3600. }
  3601. for (i = 0; cmds[i].name != NULL; i++) {
  3602. if (strcmp(cmdptr, cmds[i].name) == 0) {
  3603. sprintf(cmdbuf, "|%s|", cmdptr);
  3604. if (isjoin) {
  3605. if (strstr(cmdsbuf, cmdbuf)) {
  3606. did = true;
  3607. break;
  3608. }
  3609. strcat(cmdsbuf, cmdptr);
  3610. strcat(cmdsbuf, "|");
  3611. head_join(io_data, cmdptr, isjson, &firstjoin);
  3612. if (!cmds[i].joinable) {
  3613. message(io_data, MSG_ACCDENY, 0, cmds[i].name, isjson);
  3614. did = true;
  3615. tail_join(io_data, isjson);
  3616. break;
  3617. }
  3618. }
  3619. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  3620. {
  3621. per_proc = !strncmp(cmds[i].name, "proc", 4);
  3622. (cmds[i].func)(io_data, c, param, isjson, group);
  3623. }
  3624. else {
  3625. message(io_data, MSG_ACCDENY, 0, cmds[i].name, isjson);
  3626. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  3627. }
  3628. did = true;
  3629. if (!isjoin)
  3630. send_result(io_data, c, isjson);
  3631. else
  3632. tail_join(io_data, isjson);
  3633. break;
  3634. }
  3635. }
  3636. if (!did) {
  3637. if (isjoin)
  3638. head_join(io_data, cmdptr, isjson, &firstjoin);
  3639. message(io_data, MSG_INVCMD, 0, NULL, isjson);
  3640. if (isjoin)
  3641. tail_join(io_data, isjson);
  3642. else
  3643. send_result(io_data, c, isjson);
  3644. }
  3645. inochi:
  3646. if (isjoin)
  3647. cmdptr = cmd;
  3648. } while (isjoin && cmdptr);
  3649. }
  3650. if (isjson)
  3651. json_decref(json_config);
  3652. if (isjoin)
  3653. send_result(io_data, c, isjson);
  3654. }
  3655. }
  3656. CLOSESOCKET(c);
  3657. }
  3658. die:
  3659. /* Blank line fix for older compilers since pthread_cleanup_pop is a
  3660. * macro that gets confused by a label existing immediately before it
  3661. */
  3662. ;
  3663. pthread_cleanup_pop(true);
  3664. if (opt_debug)
  3665. applog(LOG_DEBUG, "API: terminating due to: %s",
  3666. do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
  3667. mutex_lock(&quit_restart_lock);
  3668. if (do_a_restart) {
  3669. if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
  3670. mutex_unlock(&quit_restart_lock);
  3671. quit(1, "API failed to initiate a restart - aborting");
  3672. }
  3673. pthread_detach(bye_thr.pth);
  3674. } else if (do_a_quit) {
  3675. if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
  3676. mutex_unlock(&quit_restart_lock);
  3677. quit(1, "API failed to initiate a clean quit - aborting");
  3678. }
  3679. pthread_detach(bye_thr.pth);
  3680. }
  3681. mutex_unlock(&quit_restart_lock);
  3682. }