api.c 64 KB

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