api.c 115 KB

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