api.c 71 KB

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