api.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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,\"Last Share Pool\":%d,\"Last Share Time\":%lu}",
  495. cpu, cgpu->total_mhashes / total_secs,
  496. opt_log_interval, cgpu->rolling,
  497. cgpu->accepted, cgpu->rejected,
  498. cgpu->utility,
  499. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  500. (unsigned long)(cgpu->last_share_pool_time));
  501. else
  502. sprintf(buf, "CPU=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Utility=%.2f,Last Share Pool=%d,Last Share Time=%lu%c",
  503. cpu, cgpu->total_mhashes / total_secs,
  504. opt_log_interval, cgpu->rolling,
  505. cgpu->accepted, cgpu->rejected,
  506. cgpu->utility,
  507. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  508. (unsigned long)(cgpu->last_share_pool_time), SEPARATOR);
  509. strcat(io_buffer, buf);
  510. }
  511. }
  512. #endif
  513. static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  514. {
  515. int i;
  516. if (nDevs == 0 && opt_n_threads == 0) {
  517. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  518. return;
  519. }
  520. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  521. if (isjson) {
  522. strcat(io_buffer, COMMA);
  523. strcat(io_buffer, JSON_DEVS);
  524. }
  525. for (i = 0; i < nDevs; i++) {
  526. if (isjson && i > 0)
  527. strcat(io_buffer, COMMA);
  528. gpustatus(i, isjson);
  529. }
  530. #ifdef WANT_CPUMINE
  531. if (opt_n_threads > 0)
  532. for (i = 0; i < num_processors; i++) {
  533. if (isjson && (i > 0 || nDevs > 0))
  534. strcat(io_buffer, COMMA);
  535. cpustatus(i, isjson);
  536. }
  537. #endif
  538. if (isjson)
  539. strcat(io_buffer, JSON_CLOSE);
  540. }
  541. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  542. {
  543. int id;
  544. if (nDevs == 0) {
  545. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  546. return;
  547. }
  548. if (param == NULL || *param == '\0') {
  549. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  550. return;
  551. }
  552. id = atoi(param);
  553. if (id < 0 || id >= nDevs) {
  554. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  555. return;
  556. }
  557. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  558. if (isjson) {
  559. strcat(io_buffer, COMMA);
  560. strcat(io_buffer, JSON_GPU);
  561. }
  562. gpustatus(id, isjson);
  563. if (isjson)
  564. strcat(io_buffer, JSON_CLOSE);
  565. }
  566. #ifdef WANT_CPUMINE
  567. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  568. {
  569. int id;
  570. if (opt_n_threads == 0) {
  571. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  572. return;
  573. }
  574. if (param == NULL || *param == '\0') {
  575. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  576. return;
  577. }
  578. id = atoi(param);
  579. if (id < 0 || id >= num_processors) {
  580. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  581. return;
  582. }
  583. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  584. if (isjson) {
  585. strcat(io_buffer, COMMA);
  586. strcat(io_buffer, JSON_CPU);
  587. }
  588. cpustatus(id, isjson);
  589. if (isjson)
  590. strcat(io_buffer, JSON_CLOSE);
  591. }
  592. #endif
  593. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  594. {
  595. char buf[BUFSIZ];
  596. char *status, *lp;
  597. int i;
  598. if (total_pools == 0) {
  599. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  600. return;
  601. }
  602. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  603. if (isjson) {
  604. strcat(io_buffer, COMMA);
  605. strcat(io_buffer, JSON_POOLS);
  606. }
  607. for (i = 0; i < total_pools; i++) {
  608. struct pool *pool = pools[i];
  609. if (!pool->enabled)
  610. status = (char *)DISABLED;
  611. else {
  612. if (pool->idle)
  613. status = (char *)DEAD;
  614. else
  615. status = (char *)ALIVE;
  616. }
  617. if (pool->hdr_path)
  618. lp = (char *)YES;
  619. else
  620. lp = (char *)NO;
  621. if (isjson)
  622. 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}",
  623. (i > 0) ? COMMA : "",
  624. i, pool->rpc_url, status, pool->prio, lp,
  625. pool->getwork_requested,
  626. pool->accepted, pool->rejected,
  627. pool->discarded_work,
  628. pool->stale_shares,
  629. pool->getfail_occasions,
  630. pool->remotefail_occasions);
  631. else
  632. 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",
  633. i, pool->rpc_url, status, pool->prio, lp,
  634. pool->getwork_requested,
  635. pool->accepted, pool->rejected,
  636. pool->discarded_work,
  637. pool->stale_shares,
  638. pool->getfail_occasions,
  639. pool->remotefail_occasions, SEPARATOR);
  640. strcat(io_buffer, buf);
  641. }
  642. if (isjson)
  643. strcat(io_buffer, JSON_CLOSE);
  644. }
  645. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  646. {
  647. double utility, mhs;
  648. #ifdef WANT_CPUMINE
  649. char *algo = (char *)(algo_names[opt_algo]);
  650. if (algo == NULL)
  651. algo = "(null)";
  652. #endif
  653. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  654. mhs = total_mhashes_done / total_secs;
  655. #ifdef WANT_CPUMINE
  656. if (isjson)
  657. 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,
  658. message(MSG_SUMM, 0, NULL, isjson),
  659. total_secs, algo, mhs, found_blocks,
  660. total_getworks, total_accepted, total_rejected,
  661. hw_errors, utility, total_discarded, total_stale,
  662. total_go, local_work, total_ro, new_blocks);
  663. else
  664. 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",
  665. message(MSG_SUMM, 0, NULL, isjson),
  666. total_secs, algo, mhs, found_blocks,
  667. total_getworks, total_accepted, total_rejected,
  668. hw_errors, utility, total_discarded, total_stale,
  669. total_go, local_work, total_ro, new_blocks, SEPARATOR);
  670. #else
  671. if (isjson)
  672. 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,
  673. message(MSG_SUMM, 0, NULL, isjson),
  674. total_secs, mhs, found_blocks,
  675. total_getworks, total_accepted, total_rejected,
  676. hw_errors, utility, total_discarded, total_stale,
  677. total_go, local_work, total_ro, new_blocks);
  678. else
  679. 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",
  680. message(MSG_SUMM, 0, NULL, isjson),
  681. total_secs, mhs, found_blocks,
  682. total_getworks, total_accepted, total_rejected,
  683. hw_errors, utility, total_discarded, total_stale,
  684. total_go, local_work, total_ro, new_blocks, SEPARATOR);
  685. #endif
  686. }
  687. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  688. {
  689. struct thr_info *thr;
  690. int gpu;
  691. int id;
  692. int i;
  693. if (gpu_threads == 0) {
  694. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  695. return;
  696. }
  697. if (param == NULL || *param == '\0') {
  698. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  699. return;
  700. }
  701. id = atoi(param);
  702. if (id < 0 || id >= nDevs) {
  703. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  704. return;
  705. }
  706. if (gpus[id].enabled) {
  707. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  708. return;
  709. }
  710. for (i = 0; i < gpu_threads; i++) {
  711. gpu = thr_info[i].cgpu->device_id;
  712. if (gpu == id) {
  713. thr = &thr_info[i];
  714. if (thr->cgpu->status != LIFE_WELL) {
  715. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  716. return;
  717. }
  718. gpus[id].enabled = true;
  719. tq_push(thr->q, &ping);
  720. }
  721. }
  722. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  723. }
  724. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  725. {
  726. int id;
  727. if (nDevs == 0) {
  728. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  729. return;
  730. }
  731. if (param == NULL || *param == '\0') {
  732. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  733. return;
  734. }
  735. id = atoi(param);
  736. if (id < 0 || id >= nDevs) {
  737. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  738. return;
  739. }
  740. if (!gpus[id].enabled) {
  741. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  742. return;
  743. }
  744. gpus[id].enabled = false;
  745. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  746. }
  747. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  748. {
  749. int id;
  750. if (nDevs == 0) {
  751. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  752. return;
  753. }
  754. if (param == NULL || *param == '\0') {
  755. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  756. return;
  757. }
  758. id = atoi(param);
  759. if (id < 0 || id >= nDevs) {
  760. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  761. return;
  762. }
  763. reinit_device(&gpus[id]);
  764. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  765. }
  766. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  767. {
  768. char buf[BUFSIZ];
  769. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  770. if (isjson)
  771. sprintf(buf, "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE, nDevs);
  772. else
  773. sprintf(buf, _GPUS ",Count=%d%c", nDevs, SEPARATOR);
  774. strcat(io_buffer, buf);
  775. }
  776. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  777. {
  778. char buf[BUFSIZ];
  779. int count = 0;
  780. #ifdef WANT_CPUMINE
  781. count = opt_n_threads > 0 ? num_processors : 0;
  782. #endif
  783. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  784. if (isjson)
  785. sprintf(buf, "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE, count);
  786. else
  787. sprintf(buf, _CPUS ",Count=%d%c", count, SEPARATOR);
  788. strcat(io_buffer, buf);
  789. }
  790. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  791. {
  792. struct pool *pool;
  793. int id;
  794. if (total_pools == 0) {
  795. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  796. return;
  797. }
  798. if (param == NULL || *param == '\0') {
  799. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  800. return;
  801. }
  802. id = atoi(param);
  803. if (id < 0 || id >= total_pools) {
  804. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  805. return;
  806. }
  807. pool = pools[id];
  808. pool->enabled = true;
  809. switch_pools(pool);
  810. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  811. }
  812. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  813. {
  814. int id;
  815. char *gpusep;
  816. if (nDevs == 0) {
  817. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  818. return false;
  819. }
  820. if (param == NULL || *param == '\0') {
  821. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  822. return false;
  823. }
  824. gpusep = strchr(param, GPUSEP);
  825. if (gpusep == NULL) {
  826. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  827. return false;
  828. }
  829. *(gpusep++) = '\0';
  830. id = atoi(param);
  831. if (id < 0 || id >= nDevs) {
  832. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  833. return false;
  834. }
  835. *gpu = id;
  836. *value = gpusep;
  837. return true;
  838. }
  839. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  840. {
  841. int id;
  842. char *value;
  843. int intensity;
  844. char intensitystr[7];
  845. if (!splitgpuvalue(param, &id, &value, isjson))
  846. return;
  847. if (!strncasecmp(value, DYNAMIC, 1)) {
  848. gpus[id].dynamic = true;
  849. strcpy(intensitystr, DYNAMIC);
  850. }
  851. else {
  852. intensity = atoi(value);
  853. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  854. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  855. return;
  856. }
  857. gpus[id].dynamic = false;
  858. gpus[id].intensity = intensity;
  859. sprintf(intensitystr, "%d", intensity);
  860. }
  861. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  862. }
  863. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  864. {
  865. #ifdef HAVE_ADL
  866. int id;
  867. char *value;
  868. int clock;
  869. if (!splitgpuvalue(param, &id, &value, isjson))
  870. return;
  871. clock = atoi(value);
  872. if (set_memoryclock(id, clock))
  873. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  874. else
  875. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  876. #else
  877. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  878. #endif
  879. }
  880. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  881. {
  882. #ifdef HAVE_ADL
  883. int id;
  884. char *value;
  885. int clock;
  886. if (!splitgpuvalue(param, &id, &value, isjson))
  887. return;
  888. clock = atoi(value);
  889. if (set_engineclock(id, clock))
  890. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  891. else
  892. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  893. #else
  894. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  895. #endif
  896. }
  897. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  898. {
  899. #ifdef HAVE_ADL
  900. int id;
  901. char *value;
  902. int fan;
  903. if (!splitgpuvalue(param, &id, &value, isjson))
  904. return;
  905. fan = atoi(value);
  906. if (set_fanspeed(id, fan))
  907. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  908. else
  909. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  910. #else
  911. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  912. #endif
  913. }
  914. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  915. {
  916. #ifdef HAVE_ADL
  917. int id;
  918. char *value;
  919. float vddc;
  920. if (!splitgpuvalue(param, &id, &value, isjson))
  921. return;
  922. vddc = atof(value);
  923. if (set_vddc(id, vddc))
  924. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  925. else
  926. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  927. #else
  928. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  929. #endif
  930. }
  931. static void send_result(SOCKETTYPE c, bool isjson);
  932. void doquit(SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  933. {
  934. if (isjson)
  935. strcpy(io_buffer, JSON_START JSON_BYE);
  936. else
  937. strcpy(io_buffer, _BYE);
  938. send_result(c, isjson);
  939. *io_buffer = '\0';
  940. bye = 1;
  941. kill_work();
  942. }
  943. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  944. {
  945. FILE *fcfg;
  946. if (param == NULL || *param == '\0') {
  947. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  948. return;
  949. }
  950. fcfg = fopen(param, "w");
  951. if (!fcfg) {
  952. strcpy(io_buffer, message(MSG_BADFN, 0, param, isjson));
  953. return;
  954. }
  955. write_config(fcfg);
  956. fclose(fcfg);
  957. strcpy(io_buffer, message(MSG_SAVED, 0, param, isjson));
  958. }
  959. struct CMDS {
  960. char *name;
  961. void (*func)(SOCKETTYPE, char *, bool);
  962. } cmds[] = {
  963. { "version", apiversion },
  964. { "config", minerconfig },
  965. { "devs", devstatus },
  966. { "pools", poolstatus },
  967. { "summary", summary },
  968. { "gpuenable", gpuenable },
  969. { "gpudisable", gpudisable },
  970. { "gpurestart", gpurestart },
  971. { "gpu", gpudev },
  972. #ifdef WANT_CPUMINE
  973. { "cpu", cpudev },
  974. #endif
  975. { "gpucount", gpucount },
  976. { "cpucount", cpucount },
  977. { "switchpool", switchpool },
  978. { "gpuintensity", gpuintensity },
  979. { "gpumem", gpumem},
  980. { "gpuengine", gpuengine},
  981. { "gpufan", gpufan},
  982. { "gpuvddc", gpuvddc},
  983. { "save", dosave },
  984. { "quit", doquit },
  985. { NULL, NULL }
  986. };
  987. static void send_result(SOCKETTYPE c, bool isjson)
  988. {
  989. int n;
  990. int len;
  991. if (isjson)
  992. strcat(io_buffer, JSON_END);
  993. len = strlen(io_buffer);
  994. if (opt_debug)
  995. applog(LOG_DEBUG, "DBG: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : "");
  996. // ignore failure - it's closed immediately anyway
  997. n = send(c, io_buffer, len+1, 0);
  998. if (opt_debug) {
  999. if (SOCKETFAIL(n))
  1000. applog(LOG_DEBUG, "DBG: send failed: %s", SOCKERRMSG);
  1001. else
  1002. applog(LOG_DEBUG, "DBG: sent %d", n);
  1003. }
  1004. }
  1005. static void tidyup()
  1006. {
  1007. bye = 1;
  1008. if (sock != INVSOCK) {
  1009. shutdown(sock, SHUT_RDWR);
  1010. CLOSESOCKET(sock);
  1011. sock = INVSOCK;
  1012. }
  1013. if (msg_buffer != NULL) {
  1014. free(msg_buffer);
  1015. msg_buffer = NULL;
  1016. }
  1017. if (io_buffer != NULL) {
  1018. free(io_buffer);
  1019. io_buffer = NULL;
  1020. }
  1021. }
  1022. void api(void)
  1023. {
  1024. char buf[BUFSIZ];
  1025. char param_buf[BUFSIZ];
  1026. const char *localaddr = "127.0.0.1";
  1027. SOCKETTYPE c;
  1028. int n, bound;
  1029. char *connectaddr;
  1030. char *binderror;
  1031. time_t bindstart;
  1032. short int port = opt_api_port;
  1033. struct sockaddr_in serv;
  1034. struct sockaddr_in cli;
  1035. socklen_t clisiz;
  1036. char *cmd;
  1037. char *param;
  1038. bool addrok;
  1039. json_error_t json_err;
  1040. json_t *json_config;
  1041. json_t *json_val;
  1042. bool isjson;
  1043. bool did;
  1044. int i;
  1045. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1046. sleep(opt_log_interval);
  1047. if (!opt_api_listen) {
  1048. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1049. return;
  1050. }
  1051. sock = socket(AF_INET, SOCK_STREAM, 0);
  1052. if (sock == INVSOCK) {
  1053. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1054. return;
  1055. }
  1056. memset(&serv, 0, sizeof(serv));
  1057. serv.sin_family = AF_INET;
  1058. if (!opt_api_network) {
  1059. serv.sin_addr.s_addr = inet_addr(localaddr);
  1060. if (serv.sin_addr.s_addr == INVINETADDR) {
  1061. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1062. return;
  1063. }
  1064. }
  1065. serv.sin_port = htons(port);
  1066. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1067. bound = 0;
  1068. bindstart = time(NULL);
  1069. while (bound == 0) {
  1070. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1071. binderror = SOCKERRMSG;
  1072. if ((time(NULL) - bindstart) > 61)
  1073. break;
  1074. else {
  1075. applog(LOG_WARNING, "API bind to port %d failed - trying again in 15sec", port);
  1076. sleep(15);
  1077. }
  1078. }
  1079. else
  1080. bound = 1;
  1081. }
  1082. if (bound == 0) {
  1083. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1084. return;
  1085. }
  1086. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1087. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1088. CLOSESOCKET(sock);
  1089. return;
  1090. }
  1091. if (opt_api_network)
  1092. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1093. else
  1094. applog(LOG_WARNING, "API running in restricted access mode");
  1095. io_buffer = malloc(MYBUFSIZ+1);
  1096. msg_buffer = malloc(MYBUFSIZ+1);
  1097. while (bye == 0) {
  1098. clisiz = sizeof(cli);
  1099. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1100. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1101. goto die;
  1102. }
  1103. if (opt_api_network)
  1104. addrok = true;
  1105. else {
  1106. connectaddr = inet_ntoa(cli.sin_addr);
  1107. addrok = (strcmp(connectaddr, localaddr) == 0);
  1108. }
  1109. if (opt_debug) {
  1110. connectaddr = inet_ntoa(cli.sin_addr);
  1111. applog(LOG_DEBUG, "DBG: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1112. }
  1113. if (addrok) {
  1114. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1115. if (SOCKETFAIL(n))
  1116. buf[0] = '\0';
  1117. else
  1118. buf[n] = '\0';
  1119. if (opt_debug) {
  1120. if (SOCKETFAIL(n))
  1121. applog(LOG_DEBUG, "DBG: recv failed: %s", SOCKERRMSG);
  1122. else
  1123. applog(LOG_DEBUG, "DBG: recv command: (%d) '%s'", n, buf);
  1124. }
  1125. if (!SOCKETFAIL(n)) {
  1126. did = false;
  1127. if (*buf != ISJSON) {
  1128. isjson = false;
  1129. param = strchr(buf, SEPARATOR);
  1130. if (param != NULL)
  1131. *(param++) = '\0';
  1132. cmd = buf;
  1133. }
  1134. else {
  1135. isjson = true;
  1136. param = NULL;
  1137. json_config = json_loadb(buf, n, 0, &json_err);
  1138. if (!json_is_object(json_config)) {
  1139. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1140. send_result(c, isjson);
  1141. did = true;
  1142. }
  1143. else {
  1144. json_val = json_object_get(json_config, JSON_COMMAND);
  1145. if (json_val == NULL) {
  1146. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1147. send_result(c, isjson);
  1148. did = true;
  1149. }
  1150. else {
  1151. if (!json_is_string(json_val)) {
  1152. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1153. send_result(c, isjson);
  1154. did = true;
  1155. }
  1156. else {
  1157. cmd = (char *)json_string_value(json_val);
  1158. json_val = json_object_get(json_config, JSON_PARAMETER);
  1159. if (json_is_string(json_val))
  1160. param = (char *)json_string_value(json_val);
  1161. else if (json_is_integer(json_val)) {
  1162. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1163. param = param_buf;
  1164. } else if (json_is_real(json_val)) {
  1165. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1166. param = param_buf;
  1167. }
  1168. }
  1169. }
  1170. }
  1171. }
  1172. if (!did)
  1173. for (i = 0; cmds[i].name != NULL; i++) {
  1174. if (strcmp(cmd, cmds[i].name) == 0) {
  1175. (cmds[i].func)(c, param, isjson);
  1176. send_result(c, isjson);
  1177. did = true;
  1178. break;
  1179. }
  1180. }
  1181. if (!did) {
  1182. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1183. send_result(c, isjson);
  1184. }
  1185. }
  1186. }
  1187. CLOSESOCKET(c);
  1188. }
  1189. die:
  1190. tidyup();
  1191. }