api.c 111 KB

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