api.c 96 KB

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