api.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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 2 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #include "compat.h"
  19. #include "miner.h"
  20. #if defined(unix) || defined(__APPLE__)
  21. #include <errno.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #define SOCKETTYPE int
  26. #define SOCKETFAIL(a) ((a) < 0)
  27. #define INVSOCK -1
  28. #define INVINETADDR -1
  29. #define CLOSESOCKET close
  30. #define SOCKERRMSG strerror(errno)
  31. #endif
  32. #ifdef WIN32
  33. #include <winsock2.h>
  34. #define SOCKETTYPE SOCKET
  35. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  36. #define INVSOCK INVALID_SOCKET
  37. #define INVINETADDR INADDR_NONE
  38. #define CLOSESOCKET closesocket
  39. static char WSAbuf[1024];
  40. struct WSAERRORS {
  41. int id;
  42. char *code;
  43. } WSAErrors[] = {
  44. { 0, "No error" },
  45. { WSAEINTR, "Interrupted system call" },
  46. { WSAEBADF, "Bad file number" },
  47. { WSAEACCES, "Permission denied" },
  48. { WSAEFAULT, "Bad address" },
  49. { WSAEINVAL, "Invalid argument" },
  50. { WSAEMFILE, "Too many open sockets" },
  51. { WSAEWOULDBLOCK, "Operation would block" },
  52. { WSAEINPROGRESS, "Operation now in progress" },
  53. { WSAEALREADY, "Operation already in progress" },
  54. { WSAENOTSOCK, "Socket operation on non-socket" },
  55. { WSAEDESTADDRREQ, "Destination address required" },
  56. { WSAEMSGSIZE, "Message too long" },
  57. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  58. { WSAENOPROTOOPT, "Bad protocol option" },
  59. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  60. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  61. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  62. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  63. { WSAEAFNOSUPPORT, "Address family not supported" },
  64. { WSAEADDRINUSE, "Address already in use" },
  65. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  66. { WSAENETDOWN, "Network is down" },
  67. { WSAENETUNREACH, "Network is unreachable" },
  68. { WSAENETRESET, "Net connection reset" },
  69. { WSAECONNABORTED, "Software caused connection abort" },
  70. { WSAECONNRESET, "Connection reset by peer" },
  71. { WSAENOBUFS, "No buffer space available" },
  72. { WSAEISCONN, "Socket is already connected" },
  73. { WSAENOTCONN, "Socket is not connected" },
  74. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  75. { WSAETOOMANYREFS, "Too many references, can't splice" },
  76. { WSAETIMEDOUT, "Connection timed out" },
  77. { WSAECONNREFUSED, "Connection refused" },
  78. { WSAELOOP, "Too many levels of symbolic links" },
  79. { WSAENAMETOOLONG, "File name too long" },
  80. { WSAEHOSTDOWN, "Host is down" },
  81. { WSAEHOSTUNREACH, "No route to host" },
  82. { WSAENOTEMPTY, "Directory not empty" },
  83. { WSAEPROCLIM, "Too many processes" },
  84. { WSAEUSERS, "Too many users" },
  85. { WSAEDQUOT, "Disc quota exceeded" },
  86. { WSAESTALE, "Stale NFS file handle" },
  87. { WSAEREMOTE, "Too many levels of remote in path" },
  88. { WSASYSNOTREADY, "Network system is unavailable" },
  89. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  90. { WSANOTINITIALISED, "WSAStartup not yet called" },
  91. { WSAEDISCON, "Graceful shutdown in progress" },
  92. { WSAHOST_NOT_FOUND, "Host not found" },
  93. { WSANO_DATA, "No host data of that type was found" },
  94. { -1, "Unknown error code" }
  95. };
  96. static char *WSAErrorMsg()
  97. {
  98. char *msg;
  99. int i;
  100. int id = WSAGetLastError();
  101. /* Assume none of them are actually -1 */
  102. for (i = 0; WSAErrors[i].id != -1; i++)
  103. if (WSAErrors[i].id == id)
  104. break;
  105. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  106. return &(WSAbuf[0]);
  107. }
  108. #define SOCKERRMSG WSAErrorMsg()
  109. #ifndef SHUT_RDWR
  110. #define SHUT_RDWR SD_BOTH
  111. #endif
  112. #endif
  113. // Big enough for largest API request
  114. // though a PC with 100s of CPUs may exceed the size ...
  115. // Current code assumes it can socket send this size also
  116. #define MYBUFSIZ 32768
  117. // Number of requests to queue - normally would be small
  118. #define QUEUE 10
  119. static char *io_buffer = NULL;
  120. static char *msg_buffer = NULL;
  121. static SOCKETTYPE sock = INVSOCK;
  122. static const char *UNAVAILABLE = " - API will not be available";
  123. //static const char *BLANK = "";
  124. static const char *COMMA = ",";
  125. static const char SEPARATOR = '|';
  126. static const char GPUSEP = ',';
  127. static const char *APIVERSION = "1.1";
  128. static const char *DEAD = "Dead";
  129. static const char *SICK = "Sick";
  130. static const char *NOSTART = "NoStart";
  131. static const char *DISABLED = "Disabled";
  132. static const char *ALIVE = "Alive";
  133. #define _DYNAMIC "D"
  134. static const char *DYNAMIC = _DYNAMIC;
  135. static const char *YES = "Y";
  136. static const char *NO = "N";
  137. #define _DEVS "DEVS"
  138. #define _POOLS "POOLS"
  139. #define _SUMMARY "SUMMARY"
  140. #define _STATUS "STATUS"
  141. #define _VERSION "VERSION"
  142. #define _MINECON "CONFIG"
  143. #ifdef WANT_CPUMINE
  144. #define _CPU "CPU"
  145. #endif
  146. #define _GPU "GPU"
  147. #define _CPUS "CPUS"
  148. #define _GPUS "GPUS"
  149. #define _BYE "BYE"
  150. static const char ISJSON = '{';
  151. #define JSON0 "{"
  152. #define JSON1 "\""
  153. #define JSON2 "\":["
  154. #define JSON3 "]"
  155. #define JSON4 ",\"id\":1}"
  156. #define JSON_START JSON0
  157. #define JSON_DEVS JSON1 _DEVS JSON2
  158. #define JSON_POOLS JSON1 _POOLS JSON2
  159. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  160. #define JSON_STATUS JSON1 _STATUS JSON2
  161. #define JSON_VERSION JSON1 _VERSION JSON2
  162. #define JSON_MINECON JSON1 _MINECON JSON2
  163. #define JSON_GPU JSON1 _GPU JSON2
  164. #ifdef WANT_CPUMINE
  165. #define JSON_CPU JSON1 _CPU JSON2
  166. #endif
  167. #define JSON_GPUS JSON1 _GPUS JSON2
  168. #define JSON_CPUS JSON1 _CPUS JSON2
  169. #define JSON_BYE JSON1 _BYE JSON1
  170. #define JSON_CLOSE JSON3
  171. #define JSON_END JSON4
  172. static const char *JSON_COMMAND = "command";
  173. static const char *JSON_PARAMETER = "parameter";
  174. #define MSG_INVGPU 1
  175. #define MSG_ALRENA 2
  176. #define MSG_ALRDIS 3
  177. #define MSG_GPUMRE 4
  178. #define MSG_GPUREN 5
  179. #define MSG_GPUNON 6
  180. #define MSG_POOL 7
  181. #define MSG_NOPOOL 8
  182. #define MSG_DEVS 9
  183. #define MSG_NODEVS 10
  184. #define MSG_SUMM 11
  185. #define MSG_GPUDIS 12
  186. #define MSG_GPUREI 13
  187. #define MSG_INVCMD 14
  188. #define MSG_MISID 15
  189. #define MSG_GPUDEV 17
  190. #ifdef WANT_CPUMINE
  191. #define MSG_CPUNON 16
  192. #define MSG_CPUDEV 18
  193. #define MSG_INVCPU 19
  194. #endif
  195. #define MSG_NUMGPU 20
  196. #define MSG_NUMCPU 21
  197. #define MSG_VERSION 22
  198. #define MSG_INVJSON 23
  199. #define MSG_MISCMD 24
  200. #define MSG_MISPID 25
  201. #define MSG_INVPID 26
  202. #define MSG_SWITCHP 27
  203. #define MSG_MISVAL 28
  204. #define MSG_NOADL 29
  205. #define MSG_NOGPUADL 30
  206. #define MSG_INVINT 31
  207. #define MSG_GPUINT 32
  208. #define MSG_MINECON 33
  209. #define MSG_GPUMERR 34
  210. #define MSG_GPUMEM 35
  211. #define MSG_GPUEERR 36
  212. #define MSG_GPUENG 37
  213. #define MSG_GPUVERR 38
  214. #define MSG_GPUVDDC 39
  215. #define MSG_GPUFERR 40
  216. #define MSG_GPUFAN 41
  217. #define MSG_MISFN 42
  218. #define MSG_BADFN 43
  219. #define MSG_SAVED 44
  220. enum code_severity {
  221. SEVERITY_ERR,
  222. SEVERITY_WARN,
  223. SEVERITY_INFO,
  224. SEVERITY_SUCC,
  225. SEVERITY_FAIL
  226. };
  227. enum code_parameters {
  228. PARAM_GPU,
  229. PARAM_CPU,
  230. PARAM_GPUMAX,
  231. PARAM_CPUMAX,
  232. PARAM_PMAX,
  233. PARAM_POOLMAX,
  234. #ifdef WANT_CPUMINE
  235. PARAM_GCMAX,
  236. #else
  237. PARAM_GMAX,
  238. #endif
  239. PARAM_CMD,
  240. PARAM_POOL,
  241. PARAM_STR,
  242. PARAM_BOTH,
  243. PARAM_NONE
  244. };
  245. struct CODES {
  246. const enum code_severity severity;
  247. const int code;
  248. const enum code_parameters params;
  249. const char *description;
  250. } codes[] = {
  251. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  252. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  253. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  254. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  255. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  256. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  257. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  258. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  259. #ifdef WANT_CPUMINE
  260. { SEVERITY_SUCC, MSG_DEVS, PARAM_GCMAX, "%d GPU(s) - %d CPU(s)" },
  261. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs/CPUs" },
  262. #else
  263. { SEVERITY_SUCC, MSG_DEVS, PARAM_GMAX, "%d GPU(s)" },
  264. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs" },
  265. #endif
  266. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  267. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  268. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  269. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  270. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  271. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  272. #ifdef WANT_CPUMINE
  273. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  274. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  275. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  276. #endif
  277. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  278. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  279. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "CGMiner versions" },
  280. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  281. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  282. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  283. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  284. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  285. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  286. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  287. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  288. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  289. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  290. { SEVERITY_SUCC, MSG_MINECON, PARAM_NONE, "CGMiner config" },
  291. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  292. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported succeess" },
  293. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  294. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported succeess" },
  295. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  296. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported succeess" },
  297. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  298. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported succeess" },
  299. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  300. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  301. { SEVERITY_ERR, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  302. { SEVERITY_FAIL, 0, 0, NULL }
  303. };
  304. static int bye = 0;
  305. static bool ping = true;
  306. // All replies (except BYE) start with a message
  307. // thus for JSON, message() inserts JSON_START at the front
  308. // and send_result() adds JSON_END at the end
  309. static char *message(int messageid, int paramid, char *param2, bool isjson)
  310. {
  311. char severity;
  312. char *ptr;
  313. #ifdef WANT_CPUMINE
  314. int cpu;
  315. #endif
  316. int i;
  317. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  318. if (codes[i].code == messageid) {
  319. switch (codes[i].severity) {
  320. case SEVERITY_WARN:
  321. severity = 'W';
  322. break;
  323. case SEVERITY_INFO:
  324. severity = 'I';
  325. break;
  326. case SEVERITY_SUCC:
  327. severity = 'S';
  328. break;
  329. case SEVERITY_ERR:
  330. default:
  331. severity = 'E';
  332. break;
  333. }
  334. if (isjson)
  335. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"Code\":%d,\"Msg\":\"", severity, messageid);
  336. else
  337. sprintf(msg_buffer, _STATUS "=%c,Code=%d,Msg=", severity, messageid);
  338. ptr = msg_buffer + strlen(msg_buffer);
  339. switch(codes[i].params) {
  340. case PARAM_GPU:
  341. case PARAM_CPU:
  342. sprintf(ptr, codes[i].description, paramid);
  343. break;
  344. case PARAM_POOL:
  345. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  346. break;
  347. case PARAM_GPUMAX:
  348. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  349. break;
  350. case PARAM_PMAX:
  351. sprintf(ptr, codes[i].description, total_pools);
  352. break;
  353. case PARAM_POOLMAX:
  354. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  355. break;
  356. #ifdef WANT_CPUMINE
  357. case PARAM_GCMAX:
  358. if (opt_n_threads > 0)
  359. cpu = num_processors;
  360. else
  361. cpu = 0;
  362. sprintf(ptr, codes[i].description, nDevs, cpu);
  363. break;
  364. #else
  365. case PARAM_GMAX:
  366. sprintf(ptr, codes[i].description, nDevs);
  367. break;
  368. #endif
  369. case PARAM_CMD:
  370. sprintf(ptr, codes[i].description, JSON_COMMAND);
  371. break;
  372. case PARAM_STR:
  373. sprintf(ptr, codes[i].description, param2);
  374. break;
  375. case PARAM_BOTH:
  376. sprintf(ptr, codes[i].description, paramid, param2);
  377. break;
  378. case PARAM_NONE:
  379. default:
  380. strcpy(ptr, codes[i].description);
  381. }
  382. ptr = msg_buffer + strlen(msg_buffer);
  383. if (isjson)
  384. sprintf(ptr, "\",\"Description\":\"%s\"}" JSON_CLOSE, opt_api_description);
  385. else
  386. sprintf(ptr, ",Description=%s%c", opt_api_description, SEPARATOR);
  387. return msg_buffer;
  388. }
  389. }
  390. if (isjson)
  391. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE,
  392. messageid, opt_api_description);
  393. else
  394. sprintf(msg_buffer, _STATUS "=F,Code=-1,Msg=%d,Description=%s%c",
  395. messageid, opt_api_description, SEPARATOR);
  396. return msg_buffer;
  397. }
  398. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  399. {
  400. if (isjson)
  401. sprintf(io_buffer, "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE,
  402. message(MSG_VERSION, 0, NULL, isjson),
  403. VERSION, APIVERSION);
  404. else
  405. sprintf(io_buffer, "%s" _VERSION ",CGMiner=%s,API=%s%c",
  406. message(MSG_VERSION, 0, NULL, isjson),
  407. VERSION, APIVERSION, SEPARATOR);
  408. }
  409. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  410. {
  411. char buf[BUFSIZ];
  412. int cpucount = 0;
  413. char *adlinuse = (char *)NO;
  414. #ifdef HAVE_ADL
  415. const char *adl = YES;
  416. int i;
  417. for (i = 0; i < nDevs; i++) {
  418. if (gpus[i].has_adl) {
  419. adlinuse = (char *)YES;
  420. break;
  421. }
  422. }
  423. #else
  424. const char *adl = NO;
  425. #endif
  426. #ifdef WANT_CPUMINE
  427. cpucount = opt_n_threads > 0 ? num_processors : 0;
  428. #endif
  429. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  430. if (isjson)
  431. sprintf(buf, "," JSON_MINECON "{\"GPU Count\":%d,\"CPU Count\":%d,\"Pool Count\":%d,\"ADL\":\"%s\",\"ADL in use\":\"%s\",\"Strategy\":\"%s\"}" JSON_CLOSE, nDevs, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s);
  432. else
  433. sprintf(buf, _MINECON ",GPU Count=%d,CPU Count=%d,Pool Count=%d,ADL=%s,ADL in use=%s,Strategy=%s%c", nDevs, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, SEPARATOR);
  434. strcat(io_buffer, buf);
  435. }
  436. static void gpustatus(int gpu, bool isjson)
  437. {
  438. char intensity[20];
  439. char buf[BUFSIZ];
  440. char *enabled;
  441. char *status;
  442. float gt, gv;
  443. int ga, gf, gp, gc, gm, pt;
  444. if (gpu >= 0 && gpu < nDevs) {
  445. struct cgpu_info *cgpu = &gpus[gpu];
  446. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  447. #ifdef HAVE_ADL
  448. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  449. #endif
  450. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  451. if (cgpu->enabled)
  452. enabled = (char *)YES;
  453. else
  454. enabled = (char *)NO;
  455. if (cgpu->status == LIFE_DEAD)
  456. status = (char *)DEAD;
  457. else if (cgpu->status == LIFE_SICK)
  458. status = (char *)SICK;
  459. else if (cgpu->status == LIFE_NOSTART)
  460. status = (char *)NOSTART;
  461. else
  462. status = (char *)ALIVE;
  463. if (cgpu->dynamic)
  464. strcpy(intensity, DYNAMIC);
  465. else
  466. sprintf(intensity, "%d", gpus->intensity);
  467. if (isjson)
  468. sprintf(buf, "{\"GPU\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f,\"Fan Speed\":%d,\"Fan Percent\":%d,\"GPU Clock\":%d,\"Memory Clock\":%d,\"GPU Voltage\":%.3f,\"GPU Activity\":%d,\"Powertune\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Intensity\":\"%s\",\"Last Share Pool\":%d,\"Last Share Time\":%lu}",
  469. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  470. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  471. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  472. cgpu->utility, intensity,
  473. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  474. (unsigned long)(cgpu->last_share_pool_time));
  475. else
  476. sprintf(buf, "GPU=%d,Enabled=%s,Status=%s,Temperature=%.2f,Fan Speed=%d,Fan Percent=%d,GPU Clock=%d,Memory Clock=%d,GPU Voltage=%.3f,GPU Activity=%d,Powertune=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Intensity=%s,Last Share Pool=%d,Last Share Time=%lu%c",
  477. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  478. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  479. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  480. cgpu->utility, intensity,
  481. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  482. (unsigned long)(cgpu->last_share_pool_time), SEPARATOR);
  483. strcat(io_buffer, buf);
  484. }
  485. }
  486. #ifdef WANT_CPUMINE
  487. static void cpustatus(int cpu, bool isjson)
  488. {
  489. char buf[BUFSIZ];
  490. if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
  491. struct cgpu_info *cgpu = &cpus[cpu];
  492. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  493. if (isjson)
  494. sprintf(buf, "{\"CPU\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Utility\":%.2f}",
  495. cpu, cgpu->total_mhashes / total_secs,
  496. opt_log_interval, cgpu->rolling,
  497. cgpu->accepted, cgpu->rejected,
  498. cgpu->utility);
  499. else
  500. sprintf(buf, "CPU=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Utility=%.2f%c",
  501. cpu, cgpu->total_mhashes / total_secs,
  502. opt_log_interval, cgpu->rolling,
  503. cgpu->accepted, cgpu->rejected,
  504. cgpu->utility, SEPARATOR);
  505. strcat(io_buffer, buf);
  506. }
  507. }
  508. #endif
  509. static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  510. {
  511. int i;
  512. if (nDevs == 0 && opt_n_threads == 0) {
  513. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  514. return;
  515. }
  516. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  517. if (isjson) {
  518. strcat(io_buffer, COMMA);
  519. strcat(io_buffer, JSON_DEVS);
  520. }
  521. for (i = 0; i < nDevs; i++) {
  522. if (isjson && i > 0)
  523. strcat(io_buffer, COMMA);
  524. gpustatus(i, isjson);
  525. }
  526. #ifdef WANT_CPUMINE
  527. if (opt_n_threads > 0)
  528. for (i = 0; i < num_processors; i++) {
  529. if (isjson && (i > 0 || nDevs > 0))
  530. strcat(io_buffer, COMMA);
  531. cpustatus(i, isjson);
  532. }
  533. #endif
  534. if (isjson)
  535. strcat(io_buffer, JSON_CLOSE);
  536. }
  537. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  538. {
  539. int id;
  540. if (nDevs == 0) {
  541. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  542. return;
  543. }
  544. if (param == NULL || *param == '\0') {
  545. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  546. return;
  547. }
  548. id = atoi(param);
  549. if (id < 0 || id >= nDevs) {
  550. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  551. return;
  552. }
  553. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  554. if (isjson) {
  555. strcat(io_buffer, COMMA);
  556. strcat(io_buffer, JSON_GPU);
  557. }
  558. gpustatus(id, isjson);
  559. if (isjson)
  560. strcat(io_buffer, JSON_CLOSE);
  561. }
  562. #ifdef WANT_CPUMINE
  563. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  564. {
  565. int id;
  566. if (opt_n_threads == 0) {
  567. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  568. return;
  569. }
  570. if (param == NULL || *param == '\0') {
  571. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  572. return;
  573. }
  574. id = atoi(param);
  575. if (id < 0 || id >= num_processors) {
  576. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  577. return;
  578. }
  579. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  580. if (isjson) {
  581. strcat(io_buffer, COMMA);
  582. strcat(io_buffer, JSON_CPU);
  583. }
  584. cpustatus(id, isjson);
  585. if (isjson)
  586. strcat(io_buffer, JSON_CLOSE);
  587. }
  588. #endif
  589. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  590. {
  591. char buf[BUFSIZ];
  592. char *status, *lp;
  593. int i;
  594. if (total_pools == 0) {
  595. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  596. return;
  597. }
  598. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  599. if (isjson) {
  600. strcat(io_buffer, COMMA);
  601. strcat(io_buffer, JSON_POOLS);
  602. }
  603. for (i = 0; i < total_pools; i++) {
  604. struct pool *pool = pools[i];
  605. if (!pool->enabled)
  606. status = (char *)DISABLED;
  607. else {
  608. if (pool->idle)
  609. status = (char *)DEAD;
  610. else
  611. status = (char *)ALIVE;
  612. }
  613. if (pool->hdr_path)
  614. lp = (char *)YES;
  615. else
  616. lp = (char *)NO;
  617. if (isjson)
  618. sprintf(buf, "%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}",
  619. (i > 0) ? COMMA : "",
  620. i, pool->rpc_url, status, pool->prio, lp,
  621. pool->getwork_requested,
  622. pool->accepted, pool->rejected,
  623. pool->discarded_work,
  624. pool->stale_shares,
  625. pool->getfail_occasions,
  626. pool->remotefail_occasions);
  627. else
  628. sprintf(buf, "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%c",
  629. i, pool->rpc_url, status, pool->prio, lp,
  630. pool->getwork_requested,
  631. pool->accepted, pool->rejected,
  632. pool->discarded_work,
  633. pool->stale_shares,
  634. pool->getfail_occasions,
  635. pool->remotefail_occasions, SEPARATOR);
  636. strcat(io_buffer, buf);
  637. }
  638. if (isjson)
  639. strcat(io_buffer, JSON_CLOSE);
  640. }
  641. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  642. {
  643. double utility, mhs;
  644. #ifdef WANT_CPUMINE
  645. char *algo = (char *)(algo_names[opt_algo]);
  646. if (algo == NULL)
  647. algo = "(null)";
  648. #endif
  649. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  650. mhs = total_mhashes_done / total_secs;
  651. #ifdef WANT_CPUMINE
  652. if (isjson)
  653. sprintf(io_buffer, "%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}" JSON_CLOSE,
  654. message(MSG_SUMM, 0, NULL, isjson),
  655. total_secs, algo, mhs, found_blocks,
  656. total_getworks, total_accepted, total_rejected,
  657. hw_errors, utility, total_discarded, total_stale,
  658. total_go, local_work, total_ro, new_blocks);
  659. else
  660. sprintf(io_buffer, "%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%c",
  661. message(MSG_SUMM, 0, NULL, isjson),
  662. total_secs, algo, mhs, found_blocks,
  663. total_getworks, total_accepted, total_rejected,
  664. hw_errors, utility, total_discarded, total_stale,
  665. total_go, local_work, total_ro, new_blocks, SEPARATOR);
  666. #else
  667. if (isjson)
  668. sprintf(io_buffer, "%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}" JSON_CLOSE,
  669. message(MSG_SUMM, 0, NULL, isjson),
  670. total_secs, mhs, found_blocks,
  671. total_getworks, total_accepted, total_rejected,
  672. hw_errors, utility, total_discarded, total_stale,
  673. total_go, local_work, total_ro, new_blocks);
  674. else
  675. sprintf(io_buffer, "%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%c",
  676. message(MSG_SUMM, 0, NULL, isjson),
  677. total_secs, mhs, found_blocks,
  678. total_getworks, total_accepted, total_rejected,
  679. hw_errors, utility, total_discarded, total_stale,
  680. total_go, local_work, total_ro, new_blocks, SEPARATOR);
  681. #endif
  682. }
  683. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  684. {
  685. struct thr_info *thr;
  686. int gpu;
  687. int id;
  688. int i;
  689. if (gpu_threads == 0) {
  690. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  691. return;
  692. }
  693. if (param == NULL || *param == '\0') {
  694. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  695. return;
  696. }
  697. id = atoi(param);
  698. if (id < 0 || id >= nDevs) {
  699. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  700. return;
  701. }
  702. if (gpus[id].enabled) {
  703. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  704. return;
  705. }
  706. for (i = 0; i < gpu_threads; i++) {
  707. gpu = thr_info[i].cgpu->device_id;
  708. if (gpu == id) {
  709. thr = &thr_info[i];
  710. if (thr->cgpu->status != LIFE_WELL) {
  711. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  712. return;
  713. }
  714. gpus[id].enabled = true;
  715. tq_push(thr->q, &ping);
  716. }
  717. }
  718. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  719. }
  720. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  721. {
  722. int id;
  723. if (nDevs == 0) {
  724. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  725. return;
  726. }
  727. if (param == NULL || *param == '\0') {
  728. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  729. return;
  730. }
  731. id = atoi(param);
  732. if (id < 0 || id >= nDevs) {
  733. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  734. return;
  735. }
  736. if (!gpus[id].enabled) {
  737. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  738. return;
  739. }
  740. gpus[id].enabled = false;
  741. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  742. }
  743. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  744. {
  745. int id;
  746. if (nDevs == 0) {
  747. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  748. return;
  749. }
  750. if (param == NULL || *param == '\0') {
  751. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  752. return;
  753. }
  754. id = atoi(param);
  755. if (id < 0 || id >= nDevs) {
  756. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  757. return;
  758. }
  759. reinit_device(&gpus[id]);
  760. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  761. }
  762. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  763. {
  764. char buf[BUFSIZ];
  765. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  766. if (isjson)
  767. sprintf(buf, "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE, nDevs);
  768. else
  769. sprintf(buf, _GPUS ",Count=%d%c", nDevs, SEPARATOR);
  770. strcat(io_buffer, buf);
  771. }
  772. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  773. {
  774. char buf[BUFSIZ];
  775. int count = 0;
  776. #ifdef WANT_CPUMINE
  777. count = opt_n_threads > 0 ? num_processors : 0;
  778. #endif
  779. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  780. if (isjson)
  781. sprintf(buf, "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE, count);
  782. else
  783. sprintf(buf, _CPUS ",Count=%d%c", count, SEPARATOR);
  784. strcat(io_buffer, buf);
  785. }
  786. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  787. {
  788. struct pool *pool;
  789. int id;
  790. if (total_pools == 0) {
  791. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  792. return;
  793. }
  794. if (param == NULL || *param == '\0') {
  795. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  796. return;
  797. }
  798. id = atoi(param);
  799. if (id < 0 || id >= total_pools) {
  800. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  801. return;
  802. }
  803. pool = pools[id];
  804. pool->enabled = true;
  805. switch_pools(pool);
  806. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  807. }
  808. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  809. {
  810. int id;
  811. char *gpusep;
  812. if (nDevs == 0) {
  813. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  814. return false;
  815. }
  816. if (param == NULL || *param == '\0') {
  817. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  818. return false;
  819. }
  820. gpusep = strchr(param, GPUSEP);
  821. if (gpusep == NULL) {
  822. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  823. return false;
  824. }
  825. *(gpusep++) = '\0';
  826. id = atoi(param);
  827. if (id < 0 || id >= nDevs) {
  828. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  829. return false;
  830. }
  831. *gpu = id;
  832. *value = gpusep;
  833. return true;
  834. }
  835. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  836. {
  837. int id;
  838. char *value;
  839. int intensity;
  840. char intensitystr[7];
  841. if (!splitgpuvalue(param, &id, &value, isjson))
  842. return;
  843. if (!strncasecmp(value, DYNAMIC, 1)) {
  844. gpus[id].dynamic = true;
  845. strcpy(intensitystr, DYNAMIC);
  846. }
  847. else {
  848. intensity = atoi(value);
  849. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  850. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  851. return;
  852. }
  853. gpus[id].dynamic = false;
  854. gpus[id].intensity = intensity;
  855. sprintf(intensitystr, "%d", intensity);
  856. }
  857. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  858. }
  859. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  860. {
  861. #ifdef HAVE_ADL
  862. int id;
  863. char *value;
  864. int clock;
  865. if (!splitgpuvalue(param, &id, &value, isjson))
  866. return;
  867. clock = atoi(value);
  868. if (set_memoryclock(id, clock))
  869. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  870. else
  871. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  872. #else
  873. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  874. #endif
  875. }
  876. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  877. {
  878. #ifdef HAVE_ADL
  879. int id;
  880. char *value;
  881. int clock;
  882. if (!splitgpuvalue(param, &id, &value, isjson))
  883. return;
  884. clock = atoi(value);
  885. if (set_engineclock(id, clock))
  886. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  887. else
  888. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  889. #else
  890. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  891. #endif
  892. }
  893. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  894. {
  895. #ifdef HAVE_ADL
  896. int id;
  897. char *value;
  898. int fan;
  899. if (!splitgpuvalue(param, &id, &value, isjson))
  900. return;
  901. fan = atoi(value);
  902. if (set_fanspeed(id, fan))
  903. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  904. else
  905. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  906. #else
  907. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  908. #endif
  909. }
  910. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  911. {
  912. #ifdef HAVE_ADL
  913. int id;
  914. char *value;
  915. float vddc;
  916. if (!splitgpuvalue(param, &id, &value, isjson))
  917. return;
  918. vddc = atof(value);
  919. if (set_vddc(id, vddc))
  920. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  921. else
  922. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  923. #else
  924. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  925. #endif
  926. }
  927. static void send_result(SOCKETTYPE c, bool isjson);
  928. void doquit(SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  929. {
  930. if (isjson)
  931. strcpy(io_buffer, JSON_START JSON_BYE);
  932. else
  933. strcpy(io_buffer, _BYE);
  934. send_result(c, isjson);
  935. *io_buffer = '\0';
  936. bye = 1;
  937. kill_work();
  938. }
  939. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  940. {
  941. FILE *fcfg;
  942. if (param == NULL || *param == '\0') {
  943. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  944. return;
  945. }
  946. fcfg = fopen(param, "w");
  947. if (!fcfg) {
  948. strcpy(io_buffer, message(MSG_BADFN, 0, param, isjson));
  949. return;
  950. }
  951. write_config(fcfg);
  952. fclose(fcfg);
  953. strcpy(io_buffer, message(MSG_SAVED, 0, param, isjson));
  954. }
  955. struct CMDS {
  956. char *name;
  957. void (*func)(SOCKETTYPE, char *, bool);
  958. } cmds[] = {
  959. { "version", apiversion },
  960. { "config", minerconfig },
  961. { "devs", devstatus },
  962. { "pools", poolstatus },
  963. { "summary", summary },
  964. { "gpuenable", gpuenable },
  965. { "gpudisable", gpudisable },
  966. { "gpurestart", gpurestart },
  967. { "gpu", gpudev },
  968. #ifdef WANT_CPUMINE
  969. { "cpu", cpudev },
  970. #endif
  971. { "gpucount", gpucount },
  972. { "cpucount", cpucount },
  973. { "switchpool", switchpool },
  974. { "gpuintensity", gpuintensity },
  975. { "gpumem", gpumem},
  976. { "gpuengine", gpuengine},
  977. { "gpufan", gpufan},
  978. { "gpuvddc", gpuvddc},
  979. { "save", dosave },
  980. { "quit", doquit },
  981. { NULL, NULL }
  982. };
  983. static void send_result(SOCKETTYPE c, bool isjson)
  984. {
  985. int n;
  986. int len;
  987. if (isjson)
  988. strcat(io_buffer, JSON_END);
  989. len = strlen(io_buffer);
  990. if (opt_debug)
  991. applog(LOG_DEBUG, "DBG: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : "");
  992. // ignore failure - it's closed immediately anyway
  993. n = send(c, io_buffer, len+1, 0);
  994. if (opt_debug) {
  995. if (SOCKETFAIL(n))
  996. applog(LOG_DEBUG, "DBG: send failed: %s", SOCKERRMSG);
  997. else
  998. applog(LOG_DEBUG, "DBG: sent %d", n);
  999. }
  1000. }
  1001. static void tidyup()
  1002. {
  1003. bye = 1;
  1004. if (sock != INVSOCK) {
  1005. shutdown(sock, SHUT_RDWR);
  1006. CLOSESOCKET(sock);
  1007. sock = INVSOCK;
  1008. }
  1009. if (msg_buffer != NULL) {
  1010. free(msg_buffer);
  1011. msg_buffer = NULL;
  1012. }
  1013. if (io_buffer != NULL) {
  1014. free(io_buffer);
  1015. io_buffer = NULL;
  1016. }
  1017. }
  1018. void api(void)
  1019. {
  1020. char buf[BUFSIZ];
  1021. char param_buf[BUFSIZ];
  1022. const char *localaddr = "127.0.0.1";
  1023. SOCKETTYPE c;
  1024. int n, bound;
  1025. char *connectaddr;
  1026. char *binderror;
  1027. time_t bindstart;
  1028. short int port = opt_api_port;
  1029. struct sockaddr_in serv;
  1030. struct sockaddr_in cli;
  1031. socklen_t clisiz;
  1032. char *cmd;
  1033. char *param;
  1034. bool addrok;
  1035. json_error_t json_err;
  1036. json_t *json_config;
  1037. json_t *json_val;
  1038. bool isjson;
  1039. bool did;
  1040. int i;
  1041. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1042. sleep(opt_log_interval);
  1043. if (!opt_api_listen) {
  1044. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1045. return;
  1046. }
  1047. sock = socket(AF_INET, SOCK_STREAM, 0);
  1048. if (sock == INVSOCK) {
  1049. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1050. return;
  1051. }
  1052. memset(&serv, 0, sizeof(serv));
  1053. serv.sin_family = AF_INET;
  1054. if (!opt_api_network) {
  1055. serv.sin_addr.s_addr = inet_addr(localaddr);
  1056. if (serv.sin_addr.s_addr == INVINETADDR) {
  1057. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1058. return;
  1059. }
  1060. }
  1061. serv.sin_port = htons(port);
  1062. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1063. bound = 0;
  1064. bindstart = time(NULL);
  1065. while (bound == 0) {
  1066. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1067. binderror = SOCKERRMSG;
  1068. if ((time(NULL) - bindstart) > 61)
  1069. break;
  1070. else {
  1071. applog(LOG_WARNING, "API bind to port %d failed - trying again in 15sec", port);
  1072. sleep(15);
  1073. }
  1074. }
  1075. else
  1076. bound = 1;
  1077. }
  1078. if (bound == 0) {
  1079. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1080. return;
  1081. }
  1082. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1083. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1084. CLOSESOCKET(sock);
  1085. return;
  1086. }
  1087. if (opt_api_network)
  1088. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1089. else
  1090. applog(LOG_WARNING, "API running in restricted access mode");
  1091. io_buffer = malloc(MYBUFSIZ+1);
  1092. msg_buffer = malloc(MYBUFSIZ+1);
  1093. while (bye == 0) {
  1094. clisiz = sizeof(cli);
  1095. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1096. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1097. goto die;
  1098. }
  1099. if (opt_api_network)
  1100. addrok = true;
  1101. else {
  1102. connectaddr = inet_ntoa(cli.sin_addr);
  1103. addrok = (strcmp(connectaddr, localaddr) == 0);
  1104. }
  1105. if (opt_debug) {
  1106. connectaddr = inet_ntoa(cli.sin_addr);
  1107. applog(LOG_DEBUG, "DBG: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1108. }
  1109. if (addrok) {
  1110. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1111. if (SOCKETFAIL(n))
  1112. buf[0] = '\0';
  1113. else
  1114. buf[n] = '\0';
  1115. if (opt_debug) {
  1116. if (SOCKETFAIL(n))
  1117. applog(LOG_DEBUG, "DBG: recv failed: %s", SOCKERRMSG);
  1118. else
  1119. applog(LOG_DEBUG, "DBG: recv command: (%d) '%s'", n, buf);
  1120. }
  1121. if (!SOCKETFAIL(n)) {
  1122. did = false;
  1123. if (*buf != ISJSON) {
  1124. isjson = false;
  1125. param = strchr(buf, SEPARATOR);
  1126. if (param != NULL)
  1127. *(param++) = '\0';
  1128. cmd = buf;
  1129. }
  1130. else {
  1131. isjson = true;
  1132. param = NULL;
  1133. json_config = json_loadb(buf, n, 0, &json_err);
  1134. if (!json_is_object(json_config)) {
  1135. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1136. send_result(c, isjson);
  1137. did = true;
  1138. }
  1139. else {
  1140. json_val = json_object_get(json_config, JSON_COMMAND);
  1141. if (json_val == NULL) {
  1142. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1143. send_result(c, isjson);
  1144. did = true;
  1145. }
  1146. else {
  1147. if (!json_is_string(json_val)) {
  1148. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1149. send_result(c, isjson);
  1150. did = true;
  1151. }
  1152. else {
  1153. cmd = (char *)json_string_value(json_val);
  1154. json_val = json_object_get(json_config, JSON_PARAMETER);
  1155. if (json_is_string(json_val))
  1156. param = (char *)json_string_value(json_val);
  1157. else if (json_is_integer(json_val)) {
  1158. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1159. param = param_buf;
  1160. } else if (json_is_real(json_val)) {
  1161. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1162. param = param_buf;
  1163. }
  1164. }
  1165. }
  1166. }
  1167. }
  1168. if (!did)
  1169. for (i = 0; cmds[i].name != NULL; i++) {
  1170. if (strcmp(cmd, cmds[i].name) == 0) {
  1171. (cmds[i].func)(c, param, isjson);
  1172. send_result(c, isjson);
  1173. did = true;
  1174. break;
  1175. }
  1176. }
  1177. if (!did) {
  1178. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1179. send_result(c, isjson);
  1180. }
  1181. }
  1182. }
  1183. CLOSESOCKET(c);
  1184. }
  1185. die:
  1186. tidyup();
  1187. }