api.c 92 KB

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