api.c 65 KB

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