api.c 111 KB

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