api.c 82 KB

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