api.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459
  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(unix) || defined(__APPLE__)
  27. #include <errno.h>
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #define SOCKETTYPE int
  32. #define SOCKETFAIL(a) ((a) < 0)
  33. #define INVSOCK -1
  34. #define INVINETADDR -1
  35. #define CLOSESOCKET close
  36. #define SOCKERRMSG strerror(errno)
  37. #endif
  38. #ifdef WIN32
  39. #include <ws2tcpip.h>
  40. #include <winsock2.h>
  41. #define SOCKETTYPE SOCKET
  42. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  43. #define INVSOCK INVALID_SOCKET
  44. #define INVINETADDR INADDR_NONE
  45. #define CLOSESOCKET closesocket
  46. static char WSAbuf[1024];
  47. struct WSAERRORS {
  48. int id;
  49. char *code;
  50. } WSAErrors[] = {
  51. { 0, "No error" },
  52. { WSAEINTR, "Interrupted system call" },
  53. { WSAEBADF, "Bad file number" },
  54. { WSAEACCES, "Permission denied" },
  55. { WSAEFAULT, "Bad address" },
  56. { WSAEINVAL, "Invalid argument" },
  57. { WSAEMFILE, "Too many open sockets" },
  58. { WSAEWOULDBLOCK, "Operation would block" },
  59. { WSAEINPROGRESS, "Operation now in progress" },
  60. { WSAEALREADY, "Operation already in progress" },
  61. { WSAENOTSOCK, "Socket operation on non-socket" },
  62. { WSAEDESTADDRREQ, "Destination address required" },
  63. { WSAEMSGSIZE, "Message too long" },
  64. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  65. { WSAENOPROTOOPT, "Bad protocol option" },
  66. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  67. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  68. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  69. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  70. { WSAEAFNOSUPPORT, "Address family not supported" },
  71. { WSAEADDRINUSE, "Address already in use" },
  72. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  73. { WSAENETDOWN, "Network is down" },
  74. { WSAENETUNREACH, "Network is unreachable" },
  75. { WSAENETRESET, "Net connection reset" },
  76. { WSAECONNABORTED, "Software caused connection abort" },
  77. { WSAECONNRESET, "Connection reset by peer" },
  78. { WSAENOBUFS, "No buffer space available" },
  79. { WSAEISCONN, "Socket is already connected" },
  80. { WSAENOTCONN, "Socket is not connected" },
  81. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  82. { WSAETOOMANYREFS, "Too many references, can't splice" },
  83. { WSAETIMEDOUT, "Connection timed out" },
  84. { WSAECONNREFUSED, "Connection refused" },
  85. { WSAELOOP, "Too many levels of symbolic links" },
  86. { WSAENAMETOOLONG, "File name too long" },
  87. { WSAEHOSTDOWN, "Host is down" },
  88. { WSAEHOSTUNREACH, "No route to host" },
  89. { WSAENOTEMPTY, "Directory not empty" },
  90. { WSAEPROCLIM, "Too many processes" },
  91. { WSAEUSERS, "Too many users" },
  92. { WSAEDQUOT, "Disc quota exceeded" },
  93. { WSAESTALE, "Stale NFS file handle" },
  94. { WSAEREMOTE, "Too many levels of remote in path" },
  95. { WSASYSNOTREADY, "Network system is unavailable" },
  96. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  97. { WSANOTINITIALISED, "WSAStartup not yet called" },
  98. { WSAEDISCON, "Graceful shutdown in progress" },
  99. { WSAHOST_NOT_FOUND, "Host not found" },
  100. { WSANO_DATA, "No host data of that type was found" },
  101. { -1, "Unknown error code" }
  102. };
  103. static char *WSAErrorMsg()
  104. {
  105. int i;
  106. int id = WSAGetLastError();
  107. /* Assume none of them are actually -1 */
  108. for (i = 0; WSAErrors[i].id != -1; i++)
  109. if (WSAErrors[i].id == id)
  110. break;
  111. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  112. return &(WSAbuf[0]);
  113. }
  114. #define SOCKERRMSG WSAErrorMsg()
  115. #ifndef SHUT_RDWR
  116. #define SHUT_RDWR SD_BOTH
  117. #endif
  118. #ifndef in_addr_t
  119. #define in_addr_t uint32_t
  120. #endif
  121. #endif
  122. // Big enough for largest API request
  123. // though a PC with 100s of PGAs/CPUs may exceed the size ...
  124. // Current code assumes it can socket send this size also
  125. #define MYBUFSIZ 65432 // TODO: intercept before it's exceeded
  126. // Number of requests to queue - normally would be small
  127. // However lots of PGA's may mean more
  128. #define QUEUE 100
  129. static char *io_buffer = NULL;
  130. static char *msg_buffer = NULL;
  131. static SOCKETTYPE sock = INVSOCK;
  132. static const char *UNAVAILABLE = " - API will not be available";
  133. static const char *BLANK = "";
  134. static const char *COMMA = ",";
  135. static const char SEPARATOR = '|';
  136. #define SEPSTR "|"
  137. static const char GPUSEP = ',';
  138. static const char *APIVERSION = "1.10";
  139. static const char *DEAD = "Dead";
  140. static const char *SICK = "Sick";
  141. static const char *NOSTART = "NoStart";
  142. static const char *DISABLED = "Disabled";
  143. static const char *ALIVE = "Alive";
  144. static const char *UNKNOWN = "Unknown";
  145. #define _DYNAMIC "D"
  146. static const char *DYNAMIC = _DYNAMIC;
  147. static const char *YES = "Y";
  148. static const char *NO = "N";
  149. static const char *DEVICECODE = ""
  150. #ifdef HAVE_OPENCL
  151. "GPU "
  152. #endif
  153. #ifdef USE_BITFORCE
  154. "BFL "
  155. #endif
  156. #ifdef USE_ICARUS
  157. "ICA "
  158. #endif
  159. #ifdef USE_ZTEX
  160. "ZTX "
  161. #endif
  162. #ifdef WANT_CPUMINE
  163. "CPU "
  164. #endif
  165. "";
  166. static const char *OSINFO =
  167. #if defined(__linux)
  168. "Linux";
  169. #else
  170. #if defined(__APPLE__)
  171. "Apple";
  172. #else
  173. #if defined (WIN32)
  174. "Windows";
  175. #else
  176. #if defined(unix)
  177. "Unix";
  178. #else
  179. "Unknown";
  180. #endif
  181. #endif
  182. #endif
  183. #endif
  184. #define _DEVS "DEVS"
  185. #define _POOLS "POOLS"
  186. #define _SUMMARY "SUMMARY"
  187. #define _STATUS "STATUS"
  188. #define _VERSION "VERSION"
  189. #define _MINECON "CONFIG"
  190. #define _GPU "GPU"
  191. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  192. #define _PGA "PGA"
  193. #endif
  194. #ifdef WANT_CPUMINE
  195. #define _CPU "CPU"
  196. #endif
  197. #define _GPUS "GPUS"
  198. #define _PGAS "PGAS"
  199. #define _CPUS "CPUS"
  200. #define _NOTIFY "NOTIFY"
  201. #define _DEVDETAILS "DEVDETAILS"
  202. #define _BYE "BYE"
  203. #define _RESTART "RESTART"
  204. static const char ISJSON = '{';
  205. #define JSON0 "{"
  206. #define JSON1 "\""
  207. #define JSON2 "\":["
  208. #define JSON3 "]"
  209. #define JSON4 ",\"id\":1}"
  210. #define JSON_START JSON0
  211. #define JSON_DEVS JSON1 _DEVS JSON2
  212. #define JSON_POOLS JSON1 _POOLS JSON2
  213. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  214. #define JSON_STATUS JSON1 _STATUS JSON2
  215. #define JSON_VERSION JSON1 _VERSION JSON2
  216. #define JSON_MINECON JSON1 _MINECON JSON2
  217. #define JSON_GPU JSON1 _GPU JSON2
  218. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  219. #define JSON_PGA JSON1 _PGA JSON2
  220. #endif
  221. #ifdef WANT_CPUMINE
  222. #define JSON_CPU JSON1 _CPU JSON2
  223. #endif
  224. #define JSON_GPUS JSON1 _GPUS JSON2
  225. #define JSON_PGAS JSON1 _PGAS JSON2
  226. #define JSON_CPUS JSON1 _CPUS JSON2
  227. #define JSON_NOTIFY JSON1 _NOTIFY JSON2
  228. #define JSON_DEVDETAILS JSON1 _DEVDETAILS JSON2
  229. #define JSON_BYE JSON1 _BYE JSON1
  230. #define JSON_RESTART JSON1 _RESTART JSON1
  231. #define JSON_CLOSE JSON3
  232. #define JSON_END JSON4
  233. static const char *JSON_COMMAND = "command";
  234. static const char *JSON_PARAMETER = "parameter";
  235. #define MSG_INVGPU 1
  236. #define MSG_ALRENA 2
  237. #define MSG_ALRDIS 3
  238. #define MSG_GPUMRE 4
  239. #define MSG_GPUREN 5
  240. #define MSG_GPUNON 6
  241. #define MSG_POOL 7
  242. #define MSG_NOPOOL 8
  243. #define MSG_DEVS 9
  244. #define MSG_NODEVS 10
  245. #define MSG_SUMM 11
  246. #define MSG_GPUDIS 12
  247. #define MSG_GPUREI 13
  248. #define MSG_INVCMD 14
  249. #define MSG_MISID 15
  250. #define MSG_GPUDEV 17
  251. #ifdef WANT_CPUMINE
  252. #define MSG_CPUNON 16
  253. #define MSG_CPUDEV 18
  254. #define MSG_INVCPU 19
  255. #endif
  256. #define MSG_NUMGPU 20
  257. #define MSG_NUMCPU 21
  258. #define MSG_VERSION 22
  259. #define MSG_INVJSON 23
  260. #define MSG_MISCMD 24
  261. #define MSG_MISPID 25
  262. #define MSG_INVPID 26
  263. #define MSG_SWITCHP 27
  264. #define MSG_MISVAL 28
  265. #define MSG_NOADL 29
  266. #define MSG_NOGPUADL 30
  267. #define MSG_INVINT 31
  268. #define MSG_GPUINT 32
  269. #define MSG_MINECON 33
  270. #define MSG_GPUMERR 34
  271. #define MSG_GPUMEM 35
  272. #define MSG_GPUEERR 36
  273. #define MSG_GPUENG 37
  274. #define MSG_GPUVERR 38
  275. #define MSG_GPUVDDC 39
  276. #define MSG_GPUFERR 40
  277. #define MSG_GPUFAN 41
  278. #define MSG_MISFN 42
  279. #define MSG_BADFN 43
  280. #define MSG_SAVED 44
  281. #define MSG_ACCDENY 45
  282. #define MSG_ACCOK 46
  283. #define MSG_ENAPOOL 47
  284. #define MSG_DISPOOL 48
  285. #define MSG_ALRENAP 49
  286. #define MSG_ALRDISP 50
  287. #define MSG_DISLASTP 51
  288. #define MSG_MISPDP 52
  289. #define MSG_INVPDP 53
  290. #define MSG_TOOMANYP 54
  291. #define MSG_ADDPOOL 55
  292. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  293. #define MSG_PGANON 56
  294. #define MSG_PGADEV 57
  295. #define MSG_INVPGA 58
  296. #endif
  297. #define MSG_NUMPGA 59
  298. #define MSG_NOTIFY 60
  299. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  300. #define MSG_PGALRENA 61
  301. #define MSG_PGALRDIS 62
  302. #define MSG_PGAENA 63
  303. #define MSG_PGADIS 64
  304. #define MSG_PGAUNW 65
  305. #endif
  306. #define MSG_REMLASTP 66
  307. #define MSG_ACTPOOL 67
  308. #define MSG_REMPOOL 68
  309. #define MSG_DEVDETAILS 69
  310. enum code_severity {
  311. SEVERITY_ERR,
  312. SEVERITY_WARN,
  313. SEVERITY_INFO,
  314. SEVERITY_SUCC,
  315. SEVERITY_FAIL
  316. };
  317. enum code_parameters {
  318. PARAM_GPU,
  319. PARAM_PGA,
  320. PARAM_CPU,
  321. PARAM_GPUMAX,
  322. PARAM_PGAMAX,
  323. PARAM_CPUMAX,
  324. PARAM_PMAX,
  325. PARAM_POOLMAX,
  326. // Single generic case: have the code resolve it - see below
  327. PARAM_DMAX,
  328. PARAM_CMD,
  329. PARAM_POOL,
  330. PARAM_STR,
  331. PARAM_BOTH,
  332. PARAM_NONE
  333. };
  334. struct CODES {
  335. const enum code_severity severity;
  336. const int code;
  337. const enum code_parameters params;
  338. const char *description;
  339. } codes[] = {
  340. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  341. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  342. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  343. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  344. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  345. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  346. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  347. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  348. { SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX, "%d GPU(s)"
  349. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  350. " - %d PGA(s)"
  351. #endif
  352. #ifdef WANT_CPUMINE
  353. " - %d CPU(s)"
  354. #endif
  355. },
  356. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
  357. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  358. "/PGAs"
  359. #endif
  360. #ifdef WANT_CPUMINE
  361. "/CPUs"
  362. #endif
  363. },
  364. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  365. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  366. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  367. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  368. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  369. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  370. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  371. { SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
  372. { SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
  373. { SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
  374. { SEVERITY_INFO, MSG_PGALRENA,PARAM_PGA, "PGA %d already enabled" },
  375. { SEVERITY_INFO, MSG_PGALRDIS,PARAM_PGA, "PGA %d already disabled" },
  376. { SEVERITY_INFO, MSG_PGAENA, PARAM_PGA, "PGA %d sent enable message" },
  377. { SEVERITY_INFO, MSG_PGADIS, PARAM_PGA, "PGA %d set disable flag" },
  378. { SEVERITY_ERR, MSG_PGAUNW, PARAM_PGA, "PGA %d is not flagged WELL, cannot enable" },
  379. #endif
  380. #ifdef WANT_CPUMINE
  381. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  382. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  383. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  384. #endif
  385. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  386. { SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
  387. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  388. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "BFGMiner versions" },
  389. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  390. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  391. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  392. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  393. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  394. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  395. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  396. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  397. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  398. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  399. { SEVERITY_SUCC, MSG_MINECON, PARAM_NONE, "BFGMiner config" },
  400. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  401. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported succeess" },
  402. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  403. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported succeess" },
  404. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  405. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported succeess" },
  406. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  407. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported succeess" },
  408. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  409. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  410. { SEVERITY_ERR, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  411. { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
  412. { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
  413. { SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
  414. { SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
  415. { SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
  416. { SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
  417. { SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
  418. { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
  419. { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
  420. { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
  421. { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
  422. { SEVERITY_ERR, MSG_REMLASTP,PARAM_POOL, "Cannot remove last pool %d:'%s'" },
  423. { SEVERITY_ERR, MSG_ACTPOOL, PARAM_POOL, "Cannot remove active pool %d:'%s'" },
  424. { SEVERITY_SUCC, MSG_REMPOOL, PARAM_BOTH, "Removed pool %d:'%s'" },
  425. { SEVERITY_SUCC, MSG_NOTIFY, PARAM_NONE, "Notify" },
  426. { SEVERITY_SUCC, MSG_DEVDETAILS,PARAM_NONE, "Device Details" },
  427. { SEVERITY_FAIL, 0, 0, NULL }
  428. };
  429. static int my_thr_id = 0;
  430. static bool bye;
  431. static bool ping = true;
  432. // Used to control quit restart access to shutdown variables
  433. static pthread_mutex_t quit_restart_lock;
  434. static bool do_a_quit;
  435. static bool do_a_restart;
  436. static time_t when = 0; // when the request occurred
  437. struct IP4ACCESS {
  438. in_addr_t ip;
  439. in_addr_t mask;
  440. bool writemode;
  441. };
  442. static struct IP4ACCESS *ipaccess = NULL;
  443. static int ips = 0;
  444. #ifdef USE_BITFORCE
  445. extern struct device_api bitforce_api;
  446. #endif
  447. #ifdef USE_ICARUS
  448. extern struct device_api icarus_api;
  449. #endif
  450. #ifdef USE_ZTEX
  451. extern struct device_api ztex_api;
  452. #endif
  453. // This is only called when expected to be needed (rarely)
  454. // i.e. strings outside of the codes control (input from the user)
  455. static char *escape_string(char *str, bool isjson)
  456. {
  457. char *buf, *ptr;
  458. int count;
  459. count = 0;
  460. for (ptr = str; *ptr; ptr++) {
  461. switch (*ptr) {
  462. case ',':
  463. case '|':
  464. case '=':
  465. if (!isjson)
  466. count++;
  467. break;
  468. case '"':
  469. if (isjson)
  470. count++;
  471. break;
  472. case '\\':
  473. count++;
  474. break;
  475. }
  476. }
  477. if (count == 0)
  478. return str;
  479. buf = malloc(strlen(str) + count + 1);
  480. if (unlikely(!buf))
  481. quit(1, "Failed to malloc escape buf");
  482. ptr = buf;
  483. while (*str)
  484. switch (*str) {
  485. case ',':
  486. case '|':
  487. case '=':
  488. if (!isjson)
  489. *(ptr++) = '\\';
  490. *(ptr++) = *(str++);
  491. break;
  492. case '"':
  493. if (isjson)
  494. *(ptr++) = '\\';
  495. *(ptr++) = *(str++);
  496. break;
  497. case '\\':
  498. *(ptr++) = '\\';
  499. *(ptr++) = *(str++);
  500. break;
  501. default:
  502. *(ptr++) = *(str++);
  503. break;
  504. }
  505. *ptr = '\0';
  506. return buf;
  507. }
  508. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  509. static int numpgas()
  510. {
  511. int count = 0;
  512. int i;
  513. for (i = 0; i < total_devices; i++) {
  514. #ifdef USE_BITFORCE
  515. if (devices[i]->api == &bitforce_api)
  516. count++;
  517. #endif
  518. #ifdef USE_ICARUS
  519. if (devices[i]->api == &icarus_api)
  520. count++;
  521. #endif
  522. #ifdef USE_ZTEX
  523. if (devices[i]->api == &ztex_api)
  524. count++;
  525. #endif
  526. }
  527. return count;
  528. }
  529. static int pgadevice(int pgaid)
  530. {
  531. int count = 0;
  532. int i;
  533. for (i = 0; i < total_devices; i++) {
  534. #ifdef USE_BITFORCE
  535. if (devices[i]->api == &bitforce_api)
  536. count++;
  537. #endif
  538. #ifdef USE_ICARUS
  539. if (devices[i]->api == &icarus_api)
  540. count++;
  541. #endif
  542. #ifdef USE_ZTEX
  543. if (devices[i]->api == &ztex_api)
  544. count++;
  545. #endif
  546. if (count == (pgaid + 1))
  547. return i;
  548. }
  549. return -1;
  550. }
  551. #endif
  552. // All replies (except BYE and RESTART) start with a message
  553. // thus for JSON, message() inserts JSON_START at the front
  554. // and send_result() adds JSON_END at the end
  555. static char *message(int messageid, int paramid, char *param2, bool isjson)
  556. {
  557. char severity;
  558. char *ptr;
  559. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  560. int pga;
  561. #endif
  562. #ifdef WANT_CPUMINE
  563. int cpu;
  564. #endif
  565. int i;
  566. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  567. if (codes[i].code == messageid) {
  568. switch (codes[i].severity) {
  569. case SEVERITY_WARN:
  570. severity = 'W';
  571. break;
  572. case SEVERITY_INFO:
  573. severity = 'I';
  574. break;
  575. case SEVERITY_SUCC:
  576. severity = 'S';
  577. break;
  578. case SEVERITY_ERR:
  579. default:
  580. severity = 'E';
  581. break;
  582. }
  583. sprintf(msg_buffer, isjson
  584. ? JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"When\":%lu,\"Code\":%d,\"Msg\":\""
  585. : _STATUS "=%c,When=%lu,Code=%d,Msg=",
  586. severity, (unsigned long)when, messageid);
  587. ptr = msg_buffer + strlen(msg_buffer);
  588. switch(codes[i].params) {
  589. case PARAM_GPU:
  590. case PARAM_PGA:
  591. case PARAM_CPU:
  592. sprintf(ptr, codes[i].description, paramid);
  593. break;
  594. case PARAM_POOL:
  595. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  596. break;
  597. case PARAM_GPUMAX:
  598. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  599. break;
  600. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  601. case PARAM_PGAMAX:
  602. pga = numpgas();
  603. sprintf(ptr, codes[i].description, paramid, pga - 1);
  604. break;
  605. #endif
  606. #ifdef WANT_CPUMINE
  607. case PARAM_CPUMAX:
  608. if (opt_n_threads > 0)
  609. cpu = num_processors;
  610. else
  611. cpu = 0;
  612. sprintf(ptr, codes[i].description, paramid, cpu - 1);
  613. break;
  614. #endif
  615. case PARAM_PMAX:
  616. sprintf(ptr, codes[i].description, total_pools);
  617. break;
  618. case PARAM_POOLMAX:
  619. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  620. break;
  621. case PARAM_DMAX:
  622. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  623. pga = numpgas();
  624. #endif
  625. #ifdef WANT_CPUMINE
  626. if (opt_n_threads > 0)
  627. cpu = num_processors;
  628. else
  629. cpu = 0;
  630. #endif
  631. sprintf(ptr, codes[i].description, nDevs
  632. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  633. , pga
  634. #endif
  635. #ifdef WANT_CPUMINE
  636. , cpu
  637. #endif
  638. );
  639. break;
  640. case PARAM_CMD:
  641. sprintf(ptr, codes[i].description, JSON_COMMAND);
  642. break;
  643. case PARAM_STR:
  644. sprintf(ptr, codes[i].description, param2);
  645. break;
  646. case PARAM_BOTH:
  647. sprintf(ptr, codes[i].description, paramid, param2);
  648. break;
  649. case PARAM_NONE:
  650. default:
  651. strcpy(ptr, codes[i].description);
  652. }
  653. ptr = msg_buffer + strlen(msg_buffer);
  654. sprintf(ptr, isjson
  655. ? "\",\"Description\":\"%s\"}" JSON_CLOSE
  656. : ",Description=%s" SEPSTR,
  657. opt_api_description);
  658. return msg_buffer;
  659. }
  660. }
  661. sprintf(msg_buffer, isjson
  662. ? JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"When\":%lu,\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE
  663. : _STATUS "=F,When=%lu,Code=-1,Msg=%d,Description=%s" SEPSTR,
  664. (unsigned long)when, messageid, opt_api_description);
  665. return msg_buffer;
  666. }
  667. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  668. {
  669. sprintf(io_buffer, isjson
  670. ? "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE
  671. : "%s" _VERSION ",CGMiner=%s,API=%s" SEPSTR,
  672. message(MSG_VERSION, 0, NULL, isjson),
  673. VERSION, APIVERSION);
  674. }
  675. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  676. {
  677. char buf[BUFSIZ];
  678. int pgacount = 0;
  679. int cpucount = 0;
  680. char *adlinuse = (char *)NO;
  681. #ifdef HAVE_ADL
  682. const char *adl = YES;
  683. int i;
  684. for (i = 0; i < nDevs; i++) {
  685. if (gpus[i].has_adl) {
  686. adlinuse = (char *)YES;
  687. break;
  688. }
  689. }
  690. #else
  691. const char *adl = NO;
  692. #endif
  693. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  694. pgacount = numpgas();
  695. #endif
  696. #ifdef WANT_CPUMINE
  697. cpucount = opt_n_threads > 0 ? num_processors : 0;
  698. #endif
  699. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  700. sprintf(buf, isjson
  701. ? "," JSON_MINECON "{\"GPU Count\":%d,\"PGA Count\":%d,\"CPU Count\":%d,\"Pool Count\":%d,\"ADL\":\"%s\",\"ADL in use\":\"%s\",\"Strategy\":\"%s\",\"Log Interval\":%d,\"Device Code\":\"%s\",\"OS\":\"%s\"}" JSON_CLOSE
  702. : _MINECON ",GPU Count=%d,PGA Count=%d,CPU Count=%d,Pool Count=%d,ADL=%s,ADL in use=%s,Strategy=%s,Log Interval=%d,Device Code=%s,OS=%s" SEPSTR,
  703. nDevs, pgacount, cpucount, total_pools, adl, adlinuse,
  704. strategies[pool_strategy].s, opt_log_interval, DEVICECODE, OSINFO);
  705. strcat(io_buffer, buf);
  706. }
  707. static const char*
  708. bool2str(bool b)
  709. {
  710. return b ? YES : NO;
  711. }
  712. static const char*
  713. status2str(enum alive status)
  714. {
  715. switch (status) {
  716. case LIFE_WELL:
  717. return ALIVE;
  718. case LIFE_SICK:
  719. return SICK;
  720. case LIFE_DEAD:
  721. return DEAD;
  722. case LIFE_NOSTART:
  723. return NOSTART;
  724. default:
  725. return UNKNOWN;
  726. }
  727. }
  728. static void
  729. append_kv(char *buf, json_t*info, bool isjson)
  730. {
  731. json_t *value;
  732. const char *key, *tmpl = isjson ? ",\"%s\":%s" : ",%s=%s";
  733. char *vdump;
  734. void *it;
  735. for (it = json_object_iter(info); it; it = json_object_iter_next(info, it)) {
  736. key = json_object_iter_key(it);
  737. value = json_object_iter_value(it);
  738. if (isjson || !json_is_string(value))
  739. vdump = json_dumps(value, JSON_COMPACT | JSON_ENCODE_ANY);
  740. else
  741. vdump = strdup(json_string_value(value));
  742. tailsprintf(buf, tmpl, key, vdump);
  743. free(vdump);
  744. }
  745. }
  746. static void
  747. devdetail_an(char *buf, struct cgpu_info *cgpu, bool isjson)
  748. {
  749. tailsprintf(buf, isjson
  750. ? "{\"%s\":%d,Driver=%s"
  751. : "%s=%d,Driver=%s",
  752. cgpu->api->name, cgpu->device_id,
  753. cgpu->api->dname
  754. );
  755. if (cgpu->kname)
  756. tailsprintf(buf, isjson ? ",\"Kernel\":\"%s\"" : ",Kernel=%s", cgpu->kname);
  757. if (cgpu->name)
  758. tailsprintf(buf, isjson ? ",\"Model\":\"%s\"" : ",Model=%s", cgpu->name);
  759. if (cgpu->device_path)
  760. tailsprintf(buf, isjson ? ",\"Device Path\":\"%s\"" : ",Device Path=%s", cgpu->device_path);
  761. if (cgpu->api->get_extra_device_detail) {
  762. json_t *info = cgpu->api->get_extra_device_detail(cgpu);
  763. append_kv(buf, info, isjson);
  764. json_decref(info);
  765. }
  766. tailsprintf(buf, "%c", isjson ? '}' : SEPARATOR);
  767. }
  768. static void devstatus_an(char *buf, struct cgpu_info *cgpu, bool isjson)
  769. {
  770. tailsprintf(buf, isjson
  771. ? "{\"%s\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f"
  772. : "%s=%d,Enabled=%s,Status=%s,Temperature=%.2f,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f",
  773. cgpu->api->name, cgpu->device_id,
  774. bool2str(cgpu->deven != DEV_DISABLED),
  775. status2str(cgpu->status),
  776. cgpu->temp,
  777. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  778. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  779. cgpu->utility,
  780. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  781. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes
  782. );
  783. if (cgpu->api->get_extra_device_status) {
  784. json_t *info = cgpu->api->get_extra_device_status(cgpu);
  785. append_kv(buf, info, isjson);
  786. json_decref(info);
  787. }
  788. tailsprintf(buf, "%c", isjson ? '}' : SEPARATOR);
  789. }
  790. static void gpustatus(int gpu, bool isjson)
  791. {
  792. if (gpu < 0 || gpu >= nDevs)
  793. return;
  794. devstatus_an(io_buffer, &gpus[gpu], isjson);
  795. }
  796. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  797. static void pgastatus(int pga, bool isjson)
  798. {
  799. int dev = pgadevice(pga);
  800. if (dev < 0) // Should never happen
  801. return;
  802. devstatus_an(io_buffer, devices[dev], isjson);
  803. }
  804. #endif
  805. __maybe_unused
  806. static void cpustatus(int cpu, bool isjson)
  807. {
  808. if (opt_n_threads <= 0 || cpu < 0 || cpu >= num_processors)
  809. return;
  810. devstatus_an(io_buffer, &cpus[cpu], isjson);
  811. }
  812. static void
  813. devinfo_internal(void (*func)(char*, struct cgpu_info*, bool),
  814. __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  815. {
  816. int i;
  817. if (total_devices == 0) {
  818. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  819. return;
  820. }
  821. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  822. if (isjson) {
  823. strcat(io_buffer, COMMA);
  824. strcat(io_buffer, JSON_DEVS);
  825. }
  826. for (i = 0; i < total_devices; ++i) {
  827. if (isjson && i)
  828. strcat(io_buffer, COMMA);
  829. func(io_buffer, devices[i], isjson);
  830. }
  831. if (isjson)
  832. strcat(io_buffer, JSON_CLOSE);
  833. }
  834. static void
  835. devdetail(SOCKETTYPE c, char *param, bool isjson)
  836. {
  837. return devinfo_internal(devdetail_an, c, param, isjson);
  838. }
  839. static void
  840. devstatus(SOCKETTYPE c, char *param, bool isjson)
  841. {
  842. return devinfo_internal(devstatus_an, c, param, isjson);
  843. }
  844. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  845. {
  846. int id;
  847. if (nDevs == 0) {
  848. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  849. return;
  850. }
  851. if (param == NULL || *param == '\0') {
  852. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  853. return;
  854. }
  855. id = atoi(param);
  856. if (id < 0 || id >= nDevs) {
  857. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  858. return;
  859. }
  860. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  861. if (isjson) {
  862. strcat(io_buffer, COMMA);
  863. strcat(io_buffer, JSON_GPU);
  864. }
  865. gpustatus(id, isjson);
  866. if (isjson)
  867. strcat(io_buffer, JSON_CLOSE);
  868. }
  869. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  870. static void pgadev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  871. {
  872. int numpga = numpgas();
  873. int id;
  874. if (numpga == 0) {
  875. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  876. return;
  877. }
  878. if (param == NULL || *param == '\0') {
  879. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  880. return;
  881. }
  882. id = atoi(param);
  883. if (id < 0 || id >= numpga) {
  884. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  885. return;
  886. }
  887. strcpy(io_buffer, message(MSG_PGADEV, id, NULL, isjson));
  888. if (isjson) {
  889. strcat(io_buffer, COMMA);
  890. strcat(io_buffer, JSON_PGA);
  891. }
  892. pgastatus(id, isjson);
  893. if (isjson)
  894. strcat(io_buffer, JSON_CLOSE);
  895. }
  896. static void pgaenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  897. {
  898. int numpga = numpgas();
  899. struct thr_info *thr;
  900. int pga;
  901. int id;
  902. int i;
  903. if (numpga == 0) {
  904. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  905. return;
  906. }
  907. if (param == NULL || *param == '\0') {
  908. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  909. return;
  910. }
  911. id = atoi(param);
  912. if (id < 0 || id >= numpga) {
  913. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  914. return;
  915. }
  916. int dev = pgadevice(id);
  917. if (dev < 0) { // Should never happen
  918. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  919. return;
  920. }
  921. struct cgpu_info *cgpu = devices[dev];
  922. if (cgpu->deven != DEV_DISABLED) {
  923. strcpy(io_buffer, message(MSG_PGALRENA, id, NULL, isjson));
  924. return;
  925. }
  926. if (cgpu->status != LIFE_WELL) {
  927. strcpy(io_buffer, message(MSG_PGAUNW, id, NULL, isjson));
  928. return;
  929. }
  930. for (i = 0; i < mining_threads; i++) {
  931. pga = thr_info[i].cgpu->device_id;
  932. if (pga == dev) {
  933. thr = &thr_info[i];
  934. cgpu->deven = DEV_ENABLED;
  935. tq_push(thr->q, &ping);
  936. }
  937. }
  938. strcpy(io_buffer, message(MSG_PGAENA, id, NULL, isjson));
  939. }
  940. static void pgadisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  941. {
  942. int numpga = numpgas();
  943. int id;
  944. if (numpga == 0) {
  945. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  946. return;
  947. }
  948. if (param == NULL || *param == '\0') {
  949. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  950. return;
  951. }
  952. id = atoi(param);
  953. if (id < 0 || id >= numpga) {
  954. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  955. return;
  956. }
  957. int dev = pgadevice(id);
  958. if (dev < 0) { // Should never happen
  959. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  960. return;
  961. }
  962. struct cgpu_info *cgpu = devices[dev];
  963. if (cgpu->deven == DEV_DISABLED) {
  964. strcpy(io_buffer, message(MSG_PGALRDIS, id, NULL, isjson));
  965. return;
  966. }
  967. cgpu->deven = DEV_DISABLED;
  968. strcpy(io_buffer, message(MSG_PGADIS, id, NULL, isjson));
  969. }
  970. #endif
  971. #ifdef WANT_CPUMINE
  972. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  973. {
  974. int id;
  975. if (opt_n_threads == 0) {
  976. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  977. return;
  978. }
  979. if (param == NULL || *param == '\0') {
  980. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  981. return;
  982. }
  983. id = atoi(param);
  984. if (id < 0 || id >= num_processors) {
  985. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  986. return;
  987. }
  988. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  989. if (isjson) {
  990. strcat(io_buffer, COMMA);
  991. strcat(io_buffer, JSON_CPU);
  992. }
  993. cpustatus(id, isjson);
  994. if (isjson)
  995. strcat(io_buffer, JSON_CLOSE);
  996. }
  997. #endif
  998. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  999. {
  1000. char buf[BUFSIZ];
  1001. char *status, *lp;
  1002. char *rpc_url;
  1003. char *rpc_user;
  1004. int i;
  1005. if (total_pools == 0) {
  1006. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1007. return;
  1008. }
  1009. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  1010. if (isjson) {
  1011. strcat(io_buffer, COMMA);
  1012. strcat(io_buffer, JSON_POOLS);
  1013. }
  1014. for (i = 0; i < total_pools; i++) {
  1015. struct pool *pool = pools[i];
  1016. if (!pool->enabled)
  1017. status = (char *)DISABLED;
  1018. else {
  1019. if (pool->idle)
  1020. status = (char *)DEAD;
  1021. else
  1022. status = (char *)ALIVE;
  1023. }
  1024. if (pool->hdr_path)
  1025. lp = (char *)YES;
  1026. else
  1027. lp = (char *)NO;
  1028. rpc_url = escape_string(pool->rpc_url, isjson);
  1029. rpc_user = escape_string(pool->rpc_user, isjson);
  1030. sprintf(buf, isjson
  1031. ? "%s{\"POOL\":%d,\"URL\":\"%s\",\"Status\":\"%s\",\"Priority\":%d,\"Long Poll\":\"%s\",\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Remote Failures\":%d,\"User\":\"%s\",\"Last Share Time\":%lu}"
  1032. : "%sPOOL=%d,URL=%s,Status=%s,Priority=%d,Long Poll=%s,Getworks=%d,Accepted=%d,Rejected=%d,Discarded=%d,Stale=%d,Get Failures=%d,Remote Failures=%d,User=%s,Last Share Time=%lu" SEPSTR,
  1033. (isjson && (i > 0)) ? COMMA : BLANK,
  1034. i, rpc_url, status, pool->prio, lp,
  1035. pool->getwork_requested,
  1036. pool->accepted, pool->rejected,
  1037. pool->discarded_work,
  1038. pool->stale_shares,
  1039. pool->getfail_occasions,
  1040. pool->remotefail_occasions,
  1041. rpc_user, pool->last_share_time);
  1042. strcat(io_buffer, buf);
  1043. if (rpc_url != pool->rpc_url)
  1044. free(rpc_url);
  1045. rpc_url = NULL;
  1046. if (rpc_user != pool->rpc_user)
  1047. free(rpc_user);
  1048. rpc_user = NULL;
  1049. }
  1050. if (isjson)
  1051. strcat(io_buffer, JSON_CLOSE);
  1052. }
  1053. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1054. {
  1055. double utility, mhs;
  1056. #ifdef WANT_CPUMINE
  1057. char *algo = (char *)(algo_names[opt_algo]);
  1058. if (algo == NULL)
  1059. algo = "(null)";
  1060. #endif
  1061. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1062. mhs = total_mhashes_done / total_secs;
  1063. #ifdef WANT_CPUMINE
  1064. sprintf(io_buffer, isjson
  1065. ? "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"Algorithm\":\"%s\",\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u,\"Total MH\":%.4f}" JSON_CLOSE
  1066. : "%s" _SUMMARY ",Elapsed=%.0f,Algorithm=%s,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u,Total MH=%.4f" SEPSTR,
  1067. message(MSG_SUMM, 0, NULL, isjson),
  1068. total_secs, algo, mhs, found_blocks,
  1069. total_getworks, total_accepted, total_rejected,
  1070. hw_errors, utility, total_discarded, total_stale,
  1071. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1072. #else
  1073. sprintf(io_buffer, isjson
  1074. ? "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u,\"Total MH\":%.4f}" JSON_CLOSE
  1075. : "%s" _SUMMARY ",Elapsed=%.0f,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u,Total MH=%.4f" SEPSTR,
  1076. message(MSG_SUMM, 0, NULL, isjson),
  1077. total_secs, mhs, found_blocks,
  1078. total_getworks, total_accepted, total_rejected,
  1079. hw_errors, utility, total_discarded, total_stale,
  1080. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1081. #endif
  1082. }
  1083. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1084. {
  1085. struct thr_info *thr;
  1086. int gpu;
  1087. int id;
  1088. int i;
  1089. if (gpu_threads == 0) {
  1090. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1091. return;
  1092. }
  1093. if (param == NULL || *param == '\0') {
  1094. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1095. return;
  1096. }
  1097. id = atoi(param);
  1098. if (id < 0 || id >= nDevs) {
  1099. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1100. return;
  1101. }
  1102. if (gpus[id].deven != DEV_DISABLED) {
  1103. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  1104. return;
  1105. }
  1106. for (i = 0; i < gpu_threads; i++) {
  1107. gpu = thr_info[i].cgpu->device_id;
  1108. if (gpu == id) {
  1109. thr = &thr_info[i];
  1110. if (thr->cgpu->status != LIFE_WELL) {
  1111. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  1112. return;
  1113. }
  1114. gpus[id].deven = DEV_ENABLED;
  1115. tq_push(thr->q, &ping);
  1116. }
  1117. }
  1118. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  1119. }
  1120. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1121. {
  1122. int id;
  1123. if (nDevs == 0) {
  1124. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1125. return;
  1126. }
  1127. if (param == NULL || *param == '\0') {
  1128. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1129. return;
  1130. }
  1131. id = atoi(param);
  1132. if (id < 0 || id >= nDevs) {
  1133. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1134. return;
  1135. }
  1136. if (gpus[id].deven == DEV_DISABLED) {
  1137. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  1138. return;
  1139. }
  1140. gpus[id].deven = DEV_DISABLED;
  1141. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  1142. }
  1143. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1144. {
  1145. int id;
  1146. if (nDevs == 0) {
  1147. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1148. return;
  1149. }
  1150. if (param == NULL || *param == '\0') {
  1151. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1152. return;
  1153. }
  1154. id = atoi(param);
  1155. if (id < 0 || id >= nDevs) {
  1156. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1157. return;
  1158. }
  1159. reinit_device(&gpus[id]);
  1160. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  1161. }
  1162. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1163. {
  1164. char buf[BUFSIZ];
  1165. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  1166. sprintf(buf, isjson
  1167. ? "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE
  1168. : _GPUS ",Count=%d" SEPSTR,
  1169. nDevs);
  1170. strcat(io_buffer, buf);
  1171. }
  1172. static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1173. {
  1174. char buf[BUFSIZ];
  1175. int count = 0;
  1176. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  1177. count = numpgas();
  1178. #endif
  1179. strcpy(io_buffer, message(MSG_NUMPGA, 0, NULL, isjson));
  1180. sprintf(buf, isjson
  1181. ? "," JSON_PGAS "{\"Count\":%d}" JSON_CLOSE
  1182. : _PGAS ",Count=%d" SEPSTR,
  1183. count);
  1184. strcat(io_buffer, buf);
  1185. }
  1186. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1187. {
  1188. char buf[BUFSIZ];
  1189. int count = 0;
  1190. #ifdef WANT_CPUMINE
  1191. count = opt_n_threads > 0 ? num_processors : 0;
  1192. #endif
  1193. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  1194. sprintf(buf, isjson
  1195. ? "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE
  1196. : _CPUS ",Count=%d" SEPSTR,
  1197. count);
  1198. strcat(io_buffer, buf);
  1199. }
  1200. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1201. {
  1202. struct pool *pool;
  1203. int id;
  1204. if (total_pools == 0) {
  1205. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1206. return;
  1207. }
  1208. if (param == NULL || *param == '\0') {
  1209. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1210. return;
  1211. }
  1212. id = atoi(param);
  1213. if (id < 0 || id >= total_pools) {
  1214. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1215. return;
  1216. }
  1217. pool = pools[id];
  1218. pool->enabled = true;
  1219. switch_pools(pool);
  1220. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  1221. }
  1222. static void copyadvanceafter(char ch, char **param, char **buf)
  1223. {
  1224. #define src_p (*param)
  1225. #define dst_b (*buf)
  1226. while (*src_p && *src_p != ch) {
  1227. if (*src_p == '\\' && *(src_p+1) != '\0')
  1228. src_p++;
  1229. *(dst_b++) = *(src_p++);
  1230. }
  1231. if (*src_p)
  1232. src_p++;
  1233. *(dst_b++) = '\0';
  1234. }
  1235. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1236. {
  1237. char *ptr, *buf;
  1238. ptr = buf = malloc(strlen(param)+1);
  1239. if (unlikely(!buf))
  1240. quit(1, "Failed to malloc pooldetails buf");
  1241. *url = buf;
  1242. // copy url
  1243. copyadvanceafter(',', &param, &buf);
  1244. if (!(*param)) // missing user
  1245. goto exitsama;
  1246. *user = buf;
  1247. // copy user
  1248. copyadvanceafter(',', &param, &buf);
  1249. if (!*param) // missing pass
  1250. goto exitsama;
  1251. *pass = buf;
  1252. // copy pass
  1253. copyadvanceafter(',', &param, &buf);
  1254. return true;
  1255. exitsama:
  1256. free(ptr);
  1257. return false;
  1258. }
  1259. static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1260. {
  1261. char *url, *user, *pass;
  1262. char *ptr;
  1263. if (param == NULL || *param == '\0') {
  1264. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  1265. return;
  1266. }
  1267. if (!pooldetails(param, &url, &user, &pass)) {
  1268. ptr = escape_string(param, isjson);
  1269. strcpy(io_buffer, message(MSG_INVPDP, 0, ptr, isjson));
  1270. if (ptr != param)
  1271. free(ptr);
  1272. ptr = NULL;
  1273. return;
  1274. }
  1275. if (add_pool_details(true, url, user, pass) == ADD_POOL_MAXIMUM) {
  1276. strcpy(io_buffer, message(MSG_TOOMANYP, MAX_POOLS, NULL, isjson));
  1277. return;
  1278. }
  1279. ptr = escape_string(url, isjson);
  1280. strcpy(io_buffer, message(MSG_ADDPOOL, 0, ptr, isjson));
  1281. if (ptr != url)
  1282. free(ptr);
  1283. ptr = NULL;
  1284. }
  1285. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1286. {
  1287. struct pool *pool;
  1288. int id;
  1289. if (total_pools == 0) {
  1290. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1291. return;
  1292. }
  1293. if (param == NULL || *param == '\0') {
  1294. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1295. return;
  1296. }
  1297. id = atoi(param);
  1298. if (id < 0 || id >= total_pools) {
  1299. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1300. return;
  1301. }
  1302. pool = pools[id];
  1303. if (pool->enabled == true) {
  1304. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  1305. return;
  1306. }
  1307. pool->enabled = true;
  1308. if (pool->prio < current_pool()->prio)
  1309. switch_pools(pool);
  1310. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  1311. }
  1312. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1313. {
  1314. struct pool *pool;
  1315. int id;
  1316. if (total_pools == 0) {
  1317. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1318. return;
  1319. }
  1320. if (param == NULL || *param == '\0') {
  1321. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1322. return;
  1323. }
  1324. id = atoi(param);
  1325. if (id < 0 || id >= total_pools) {
  1326. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1327. return;
  1328. }
  1329. pool = pools[id];
  1330. if (pool->enabled == false) {
  1331. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  1332. return;
  1333. }
  1334. if (active_pools() <= 1) {
  1335. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  1336. return;
  1337. }
  1338. pool->enabled = false;
  1339. if (pool == current_pool())
  1340. switch_pools(NULL);
  1341. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  1342. }
  1343. static void removepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1344. {
  1345. struct pool *pool;
  1346. char *rpc_url;
  1347. bool dofree = false;
  1348. int id;
  1349. if (total_pools == 0) {
  1350. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1351. return;
  1352. }
  1353. if (param == NULL || *param == '\0') {
  1354. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1355. return;
  1356. }
  1357. id = atoi(param);
  1358. if (id < 0 || id >= total_pools) {
  1359. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1360. return;
  1361. }
  1362. if (total_pools <= 1) {
  1363. strcpy(io_buffer, message(MSG_REMLASTP, id, NULL, isjson));
  1364. return;
  1365. }
  1366. pool = pools[id];
  1367. if (pool == current_pool())
  1368. switch_pools(NULL);
  1369. if (pool == current_pool()) {
  1370. strcpy(io_buffer, message(MSG_ACTPOOL, id, NULL, isjson));
  1371. return;
  1372. }
  1373. pool->enabled = false;
  1374. rpc_url = escape_string(pool->rpc_url, isjson);
  1375. if (rpc_url != pool->rpc_url)
  1376. dofree = true;
  1377. remove_pool(pool);
  1378. strcpy(io_buffer, message(MSG_REMPOOL, id, rpc_url, isjson));
  1379. if (dofree)
  1380. free(rpc_url);
  1381. rpc_url = NULL;
  1382. }
  1383. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  1384. {
  1385. int id;
  1386. char *gpusep;
  1387. if (nDevs == 0) {
  1388. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1389. return false;
  1390. }
  1391. if (param == NULL || *param == '\0') {
  1392. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1393. return false;
  1394. }
  1395. gpusep = strchr(param, GPUSEP);
  1396. if (gpusep == NULL) {
  1397. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  1398. return false;
  1399. }
  1400. *(gpusep++) = '\0';
  1401. id = atoi(param);
  1402. if (id < 0 || id >= nDevs) {
  1403. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1404. return false;
  1405. }
  1406. *gpu = id;
  1407. *value = gpusep;
  1408. return true;
  1409. }
  1410. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1411. {
  1412. int id;
  1413. char *value;
  1414. int intensity;
  1415. char intensitystr[7];
  1416. if (!splitgpuvalue(param, &id, &value, isjson))
  1417. return;
  1418. if (!strncasecmp(value, DYNAMIC, 1)) {
  1419. gpus[id].dynamic = true;
  1420. strcpy(intensitystr, DYNAMIC);
  1421. }
  1422. else {
  1423. intensity = atoi(value);
  1424. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1425. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1426. return;
  1427. }
  1428. gpus[id].dynamic = false;
  1429. gpus[id].intensity = intensity;
  1430. sprintf(intensitystr, "%d", intensity);
  1431. }
  1432. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1433. }
  1434. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1435. {
  1436. #ifdef HAVE_ADL
  1437. int id;
  1438. char *value;
  1439. int clock;
  1440. if (!splitgpuvalue(param, &id, &value, isjson))
  1441. return;
  1442. clock = atoi(value);
  1443. if (set_memoryclock(id, clock))
  1444. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  1445. else
  1446. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  1447. #else
  1448. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1449. #endif
  1450. }
  1451. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1452. {
  1453. #ifdef HAVE_ADL
  1454. int id;
  1455. char *value;
  1456. int clock;
  1457. if (!splitgpuvalue(param, &id, &value, isjson))
  1458. return;
  1459. clock = atoi(value);
  1460. if (set_engineclock(id, clock))
  1461. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  1462. else
  1463. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  1464. #else
  1465. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1466. #endif
  1467. }
  1468. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1469. {
  1470. #ifdef HAVE_ADL
  1471. int id;
  1472. char *value;
  1473. int fan;
  1474. if (!splitgpuvalue(param, &id, &value, isjson))
  1475. return;
  1476. fan = atoi(value);
  1477. if (set_fanspeed(id, fan))
  1478. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  1479. else
  1480. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  1481. #else
  1482. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1483. #endif
  1484. }
  1485. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1486. {
  1487. #ifdef HAVE_ADL
  1488. int id;
  1489. char *value;
  1490. float vddc;
  1491. if (!splitgpuvalue(param, &id, &value, isjson))
  1492. return;
  1493. vddc = atof(value);
  1494. if (set_vddc(id, vddc))
  1495. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  1496. else
  1497. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  1498. #else
  1499. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1500. #endif
  1501. }
  1502. void doquit(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1503. {
  1504. if (isjson)
  1505. strcpy(io_buffer, JSON_START JSON_BYE);
  1506. else
  1507. strcpy(io_buffer, _BYE);
  1508. bye = true;
  1509. do_a_quit = true;
  1510. }
  1511. void dorestart(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1512. {
  1513. if (isjson)
  1514. strcpy(io_buffer, JSON_START JSON_RESTART);
  1515. else
  1516. strcpy(io_buffer, _RESTART);
  1517. bye = true;
  1518. do_a_restart = true;
  1519. }
  1520. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1521. {
  1522. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  1523. }
  1524. void notifystatus(int device, struct cgpu_info *cgpu, bool isjson)
  1525. {
  1526. char buf[BUFSIZ];
  1527. char *reason;
  1528. if (cgpu->device_last_not_well == 0)
  1529. reason = REASON_NONE;
  1530. else
  1531. switch(cgpu->device_not_well_reason) {
  1532. case REASON_THREAD_FAIL_INIT:
  1533. reason = REASON_THREAD_FAIL_INIT_STR;
  1534. break;
  1535. case REASON_THREAD_ZERO_HASH:
  1536. reason = REASON_THREAD_ZERO_HASH_STR;
  1537. break;
  1538. case REASON_THREAD_FAIL_QUEUE:
  1539. reason = REASON_THREAD_FAIL_QUEUE_STR;
  1540. break;
  1541. case REASON_DEV_SICK_IDLE_60:
  1542. reason = REASON_DEV_SICK_IDLE_60_STR;
  1543. break;
  1544. case REASON_DEV_DEAD_IDLE_600:
  1545. reason = REASON_DEV_DEAD_IDLE_600_STR;
  1546. break;
  1547. case REASON_DEV_NOSTART:
  1548. reason = REASON_DEV_NOSTART_STR;
  1549. break;
  1550. case REASON_DEV_OVER_HEAT:
  1551. reason = REASON_DEV_OVER_HEAT_STR;
  1552. break;
  1553. case REASON_DEV_THERMAL_CUTOFF:
  1554. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  1555. break;
  1556. default:
  1557. reason = REASON_UNKNOWN_STR;
  1558. break;
  1559. }
  1560. // ALL counters (and only counters) must start the name with a '*'
  1561. // Simplifies future external support for adding new counters
  1562. sprintf(buf, isjson
  1563. ? "%s{\"NOTIFY\":%d,\"Name\":\"%s\",\"ID\":%d,\"Last Well\":%lu,\"Last Not Well\":%lu,\"Reason Not Well\":\"%s\",\"*Thread Fail Init\":%d,\"*Thread Zero Hash\":%d,\"*Thread Fail Queue\":%d,\"*Dev Sick Idle 60s\":%d,\"*Dev Dead Idle 600s\":%d,\"*Dev Nostart\":%d,\"*Dev Over Heat\":%d,\"*Dev Thermal Cutoff\":%d}"
  1564. : "%sNOTIFY=%d,Name=%s,ID=%d,Last Well=%lu,Last Not Well=%lu,Reason Not Well=%s,*Thread Fail Init=%d,*Thread Zero Hash=%d,*Thread Fail Queue=%d,*Dev Sick Idle 60s=%d,*Dev Dead Idle 600s=%d,*Dev Nostart=%d,*Dev Over Heat=%d,*Dev Thermal Cutoff=%d" SEPSTR,
  1565. (isjson && (device > 0)) ? COMMA : BLANK,
  1566. device, cgpu->api->name, cgpu->device_id,
  1567. cgpu->device_last_well, cgpu->device_last_not_well, reason,
  1568. cgpu->thread_fail_init_count, cgpu->thread_zero_hash_count,
  1569. cgpu->thread_fail_queue_count, cgpu->dev_sick_idle_60_count,
  1570. cgpu->dev_dead_idle_600_count, cgpu->dev_nostart_count,
  1571. cgpu->dev_over_heat_count, cgpu->dev_thermal_cutoff_count);
  1572. strcat(io_buffer, buf);
  1573. }
  1574. static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1575. {
  1576. int i;
  1577. if (total_devices == 0) {
  1578. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1579. return;
  1580. }
  1581. strcpy(io_buffer, message(MSG_NOTIFY, 0, NULL, isjson));
  1582. if (isjson) {
  1583. strcat(io_buffer, COMMA);
  1584. strcat(io_buffer, JSON_NOTIFY);
  1585. }
  1586. for (i = 0; i < total_devices; i++)
  1587. notifystatus(i, devices[i], isjson);
  1588. if (isjson)
  1589. strcat(io_buffer, JSON_CLOSE);
  1590. }
  1591. static void devdetails(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1592. {
  1593. char buf[BUFSIZ];
  1594. struct cgpu_info *cgpu;
  1595. int i;
  1596. if (total_devices == 0) {
  1597. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1598. return;
  1599. }
  1600. strcpy(io_buffer, message(MSG_DEVDETAILS, 0, NULL, isjson));
  1601. if (isjson) {
  1602. strcat(io_buffer, COMMA);
  1603. strcat(io_buffer, JSON_DEVDETAILS);
  1604. }
  1605. for (i = 0; i < total_devices; i++) {
  1606. cgpu = devices[i];
  1607. sprintf(buf, isjson
  1608. ? "%s{\"DEVDETAILS\":%d,\"Name\":\"%s\",\"ID\":%d,\"Driver\":\"%s\",\"Kernel\":\"%s\",\"Model\":\"%s\",\"Device Path\":\"%s\"}"
  1609. : "%sDEVDETAILS=%d,Name=%s,ID=%d,Driver=%s,Kernel=%s,Model=%s,Device Path=%s" SEPSTR,
  1610. (isjson && (i > 0)) ? COMMA : BLANK,
  1611. i, cgpu->api->name, cgpu->device_id,
  1612. cgpu->api->dname, cgpu->kname ? : BLANK,
  1613. cgpu->name ? : BLANK, cgpu->device_path ? : BLANK);
  1614. strcat(io_buffer, buf);
  1615. }
  1616. if (isjson)
  1617. strcat(io_buffer, JSON_CLOSE);
  1618. }
  1619. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1620. {
  1621. FILE *fcfg;
  1622. char *ptr;
  1623. if (param == NULL || *param == '\0') {
  1624. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  1625. return;
  1626. }
  1627. fcfg = fopen(param, "w");
  1628. if (!fcfg) {
  1629. ptr = escape_string(param, isjson);
  1630. strcpy(io_buffer, message(MSG_BADFN, 0, ptr, isjson));
  1631. if (ptr != param)
  1632. free(ptr);
  1633. ptr = NULL;
  1634. return;
  1635. }
  1636. write_config(fcfg);
  1637. fclose(fcfg);
  1638. ptr = escape_string(param, isjson);
  1639. strcpy(io_buffer, message(MSG_SAVED, 0, ptr, isjson));
  1640. if (ptr != param)
  1641. free(ptr);
  1642. ptr = NULL;
  1643. }
  1644. struct CMDS {
  1645. char *name;
  1646. void (*func)(SOCKETTYPE, char *, bool);
  1647. bool requires_writemode;
  1648. } cmds[] = {
  1649. { "version", apiversion, false },
  1650. { "config", minerconfig, false },
  1651. { "devs", devstatus, false },
  1652. { "devdetail", devdetail, false },
  1653. { "pools", poolstatus, false },
  1654. { "summary", summary, false },
  1655. { "gpuenable", gpuenable, true },
  1656. { "gpudisable", gpudisable, true },
  1657. { "gpurestart", gpurestart, true },
  1658. { "gpu", gpudev, false },
  1659. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  1660. { "pga", pgadev, false },
  1661. { "pgaenable", pgaenable, true },
  1662. { "pgadisable", pgadisable, true },
  1663. #endif
  1664. #ifdef WANT_CPUMINE
  1665. { "cpu", cpudev, false },
  1666. #endif
  1667. { "gpucount", gpucount, false },
  1668. { "pgacount", pgacount, false },
  1669. { "cpucount", cpucount, false },
  1670. { "switchpool", switchpool, true },
  1671. { "addpool", addpool, true },
  1672. { "enablepool", enablepool, true },
  1673. { "disablepool", disablepool, true },
  1674. { "removepool", removepool, true },
  1675. { "gpuintensity", gpuintensity, true },
  1676. { "gpumem", gpumem, true },
  1677. { "gpuengine", gpuengine, true },
  1678. { "gpufan", gpufan, true },
  1679. { "gpuvddc", gpuvddc, true },
  1680. { "save", dosave, true },
  1681. { "quit", doquit, true },
  1682. { "privileged", privileged, true },
  1683. { "notify", notify, false },
  1684. { "devdetails", devdetails, false },
  1685. { "restart", dorestart, true },
  1686. { NULL, NULL, false }
  1687. };
  1688. static void send_result(SOCKETTYPE c, bool isjson)
  1689. {
  1690. int n;
  1691. int len;
  1692. if (isjson)
  1693. strcat(io_buffer, JSON_END);
  1694. len = strlen(io_buffer);
  1695. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : BLANK);
  1696. // ignore failure - it's closed immediately anyway
  1697. n = send(c, io_buffer, len+1, 0);
  1698. if (opt_debug) {
  1699. if (SOCKETFAIL(n))
  1700. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  1701. else
  1702. applog(LOG_DEBUG, "API: sent %d", n);
  1703. }
  1704. }
  1705. static void tidyup(__maybe_unused void *arg)
  1706. {
  1707. mutex_lock(&quit_restart_lock);
  1708. bye = true;
  1709. if (sock != INVSOCK) {
  1710. shutdown(sock, SHUT_RDWR);
  1711. CLOSESOCKET(sock);
  1712. sock = INVSOCK;
  1713. }
  1714. if (ipaccess != NULL) {
  1715. free(ipaccess);
  1716. ipaccess = NULL;
  1717. }
  1718. if (msg_buffer != NULL) {
  1719. free(msg_buffer);
  1720. msg_buffer = NULL;
  1721. }
  1722. if (io_buffer != NULL) {
  1723. free(io_buffer);
  1724. io_buffer = NULL;
  1725. }
  1726. mutex_unlock(&quit_restart_lock);
  1727. }
  1728. /*
  1729. * Interpret [R|W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  1730. * special case of 0/0 allows /0 (means all IP addresses)
  1731. */
  1732. #define ALLIP4 "0/0"
  1733. /*
  1734. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  1735. */
  1736. static void setup_ipaccess()
  1737. {
  1738. char *buf, *ptr, *comma, *slash, *dot;
  1739. int ipcount, mask, octet, i;
  1740. bool writemode;
  1741. buf = malloc(strlen(opt_api_allow) + 1);
  1742. if (unlikely(!buf))
  1743. quit(1, "Failed to malloc ipaccess buf");
  1744. strcpy(buf, opt_api_allow);
  1745. ipcount = 1;
  1746. ptr = buf;
  1747. while (*ptr)
  1748. if (*(ptr++) == ',')
  1749. ipcount++;
  1750. // possibly more than needed, but never less
  1751. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  1752. if (unlikely(!ipaccess))
  1753. quit(1, "Failed to calloc ipaccess");
  1754. ips = 0;
  1755. ptr = buf;
  1756. while (ptr && *ptr) {
  1757. while (*ptr == ' ' || *ptr == '\t')
  1758. ptr++;
  1759. if (*ptr == ',') {
  1760. ptr++;
  1761. continue;
  1762. }
  1763. comma = strchr(ptr, ',');
  1764. if (comma)
  1765. *(comma++) = '\0';
  1766. writemode = false;
  1767. if (isalpha(*ptr) && *(ptr+1) == ':') {
  1768. if (tolower(*ptr) == 'w')
  1769. writemode = true;
  1770. ptr += 2;
  1771. }
  1772. ipaccess[ips].writemode = writemode;
  1773. if (strcmp(ptr, ALLIP4) == 0)
  1774. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  1775. else {
  1776. slash = strchr(ptr, '/');
  1777. if (!slash)
  1778. ipaccess[ips].mask = 0xffffffff;
  1779. else {
  1780. *(slash++) = '\0';
  1781. mask = atoi(slash);
  1782. if (mask < 1 || mask > 32)
  1783. goto popipo; // skip invalid/zero
  1784. ipaccess[ips].mask = 0;
  1785. while (mask-- >= 0) {
  1786. octet = 1 << (mask % 8);
  1787. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  1788. }
  1789. }
  1790. ipaccess[ips].ip = 0; // missing default to '.0'
  1791. for (i = 0; ptr && (i < 4); i++) {
  1792. dot = strchr(ptr, '.');
  1793. if (dot)
  1794. *(dot++) = '\0';
  1795. octet = atoi(ptr);
  1796. if (octet < 0 || octet > 0xff)
  1797. goto popipo; // skip invalid
  1798. ipaccess[ips].ip |= (octet << (i * 8));
  1799. ptr = dot;
  1800. }
  1801. ipaccess[ips].ip &= ipaccess[ips].mask;
  1802. }
  1803. ips++;
  1804. popipo:
  1805. ptr = comma;
  1806. }
  1807. free(buf);
  1808. }
  1809. static void *quit_thread(__maybe_unused void *userdata)
  1810. {
  1811. // allow thread creator to finish whatever it's doing
  1812. mutex_lock(&quit_restart_lock);
  1813. mutex_unlock(&quit_restart_lock);
  1814. if (opt_debug)
  1815. applog(LOG_DEBUG, "API: killing cgminer");
  1816. kill_work();
  1817. return NULL;
  1818. }
  1819. static void *restart_thread(__maybe_unused void *userdata)
  1820. {
  1821. // allow thread creator to finish whatever it's doing
  1822. mutex_lock(&quit_restart_lock);
  1823. mutex_unlock(&quit_restart_lock);
  1824. if (opt_debug)
  1825. applog(LOG_DEBUG, "API: restarting cgminer");
  1826. app_restart();
  1827. return NULL;
  1828. }
  1829. void api(int api_thr_id)
  1830. {
  1831. struct thr_info bye_thr;
  1832. char buf[BUFSIZ];
  1833. char param_buf[BUFSIZ];
  1834. const char *localaddr = "127.0.0.1";
  1835. SOCKETTYPE c;
  1836. int n, bound;
  1837. char *connectaddr;
  1838. char *binderror;
  1839. time_t bindstart;
  1840. short int port = opt_api_port;
  1841. struct sockaddr_in serv;
  1842. struct sockaddr_in cli;
  1843. socklen_t clisiz;
  1844. char *cmd;
  1845. char *param;
  1846. bool addrok;
  1847. bool writemode;
  1848. json_error_t json_err;
  1849. json_t *json_config;
  1850. json_t *json_val;
  1851. bool isjson;
  1852. bool did;
  1853. int i;
  1854. mutex_init(&quit_restart_lock);
  1855. pthread_cleanup_push(tidyup, NULL);
  1856. my_thr_id = api_thr_id;
  1857. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1858. sleep(opt_log_interval);
  1859. if (!opt_api_listen) {
  1860. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1861. return;
  1862. }
  1863. if (opt_api_allow) {
  1864. setup_ipaccess();
  1865. if (ips == 0) {
  1866. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  1867. return;
  1868. }
  1869. }
  1870. sock = socket(AF_INET, SOCK_STREAM, 0);
  1871. if (sock == INVSOCK) {
  1872. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1873. return;
  1874. }
  1875. memset(&serv, 0, sizeof(serv));
  1876. serv.sin_family = AF_INET;
  1877. if (!opt_api_allow && !opt_api_network) {
  1878. serv.sin_addr.s_addr = inet_addr(localaddr);
  1879. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  1880. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1881. return;
  1882. }
  1883. }
  1884. serv.sin_port = htons(port);
  1885. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1886. bound = 0;
  1887. bindstart = time(NULL);
  1888. while (bound == 0) {
  1889. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1890. binderror = SOCKERRMSG;
  1891. if ((time(NULL) - bindstart) > 61)
  1892. break;
  1893. else {
  1894. applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
  1895. sleep(30);
  1896. }
  1897. } else
  1898. bound = 1;
  1899. }
  1900. if (bound == 0) {
  1901. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1902. return;
  1903. }
  1904. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1905. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1906. CLOSESOCKET(sock);
  1907. return;
  1908. }
  1909. if (opt_api_allow)
  1910. applog(LOG_WARNING, "API running in IP access mode");
  1911. else {
  1912. if (opt_api_network)
  1913. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1914. else
  1915. applog(LOG_WARNING, "API running in local access mode");
  1916. }
  1917. io_buffer = malloc(MYBUFSIZ+1);
  1918. msg_buffer = malloc(MYBUFSIZ+1);
  1919. while (!bye) {
  1920. clisiz = sizeof(cli);
  1921. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1922. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1923. goto die;
  1924. }
  1925. connectaddr = inet_ntoa(cli.sin_addr);
  1926. addrok = false;
  1927. writemode = false;
  1928. if (opt_api_allow) {
  1929. for (i = 0; i < ips; i++) {
  1930. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  1931. addrok = true;
  1932. writemode = ipaccess[i].writemode;
  1933. break;
  1934. }
  1935. }
  1936. } else {
  1937. if (opt_api_network)
  1938. addrok = true;
  1939. else
  1940. addrok = (strcmp(connectaddr, localaddr) == 0);
  1941. }
  1942. if (opt_debug)
  1943. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1944. if (addrok) {
  1945. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1946. if (SOCKETFAIL(n))
  1947. buf[0] = '\0';
  1948. else
  1949. buf[n] = '\0';
  1950. if (opt_debug) {
  1951. if (SOCKETFAIL(n))
  1952. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  1953. else
  1954. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  1955. }
  1956. if (!SOCKETFAIL(n)) {
  1957. // the time of the request in now
  1958. when = time(NULL);
  1959. did = false;
  1960. if (*buf != ISJSON) {
  1961. isjson = false;
  1962. param = strchr(buf, SEPARATOR);
  1963. if (param != NULL)
  1964. *(param++) = '\0';
  1965. cmd = buf;
  1966. }
  1967. else {
  1968. isjson = true;
  1969. param = NULL;
  1970. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  1971. json_config = json_loadb(buf, n, 0, &json_err);
  1972. #elif JANSSON_MAJOR_VERSION > 1
  1973. json_config = json_loads(buf, 0, &json_err);
  1974. #else
  1975. json_config = json_loads(buf, &json_err);
  1976. #endif
  1977. if (!json_is_object(json_config)) {
  1978. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1979. send_result(c, isjson);
  1980. did = true;
  1981. }
  1982. else {
  1983. json_val = json_object_get(json_config, JSON_COMMAND);
  1984. if (json_val == NULL) {
  1985. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1986. send_result(c, isjson);
  1987. did = true;
  1988. }
  1989. else {
  1990. if (!json_is_string(json_val)) {
  1991. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1992. send_result(c, isjson);
  1993. did = true;
  1994. }
  1995. else {
  1996. cmd = (char *)json_string_value(json_val);
  1997. json_val = json_object_get(json_config, JSON_PARAMETER);
  1998. if (json_is_string(json_val))
  1999. param = (char *)json_string_value(json_val);
  2000. else if (json_is_integer(json_val)) {
  2001. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  2002. param = param_buf;
  2003. } else if (json_is_real(json_val)) {
  2004. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  2005. param = param_buf;
  2006. }
  2007. }
  2008. }
  2009. }
  2010. }
  2011. if (!did)
  2012. for (i = 0; cmds[i].name != NULL; i++) {
  2013. if (strcmp(cmd, cmds[i].name) == 0) {
  2014. if (cmds[i].requires_writemode && !writemode) {
  2015. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  2016. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  2017. }
  2018. else
  2019. (cmds[i].func)(c, param, isjson);
  2020. send_result(c, isjson);
  2021. did = true;
  2022. break;
  2023. }
  2024. }
  2025. if (!did) {
  2026. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  2027. send_result(c, isjson);
  2028. }
  2029. }
  2030. }
  2031. CLOSESOCKET(c);
  2032. }
  2033. die:
  2034. /* Blank line fix for older compilers since pthread_cleanup_pop is a
  2035. * macro that gets confused by a label existing immediately before it
  2036. */
  2037. ;
  2038. pthread_cleanup_pop(true);
  2039. if (opt_debug)
  2040. applog(LOG_DEBUG, "API: terminating due to: %s",
  2041. do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
  2042. mutex_lock(&quit_restart_lock);
  2043. if (do_a_restart) {
  2044. if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
  2045. mutex_unlock(&quit_restart_lock);
  2046. quit(1, "API failed to initiate a restart - aborting");
  2047. }
  2048. pthread_detach(bye_thr.pth);
  2049. } else if (do_a_quit) {
  2050. if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
  2051. mutex_unlock(&quit_restart_lock);
  2052. quit(1, "API failed to initiate a clean quit - aborting");
  2053. }
  2054. pthread_detach(bye_thr.pth);
  2055. }
  2056. mutex_unlock(&quit_restart_lock);
  2057. }