api.c 58 KB

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