api.c 65 KB

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