api.c 35 KB

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