api.c 64 KB

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