api.c 58 KB

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