api.c 95 KB

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