api.c 80 KB

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