api.c 80 KB

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