api.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571
  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 *INVAPIGROUPS = "Invalid --api-groups parameter";
  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.19";
  145. static const char *DEAD = "Dead";
  146. static const char *SICK = "Sick";
  147. static const char *NOSTART = "NoStart";
  148. static const char *INIT = "Initialising";
  149. static const char *WAIT = "Waiting";
  150. static const char *DISABLED = "Disabled";
  151. static const char *ALIVE = "Alive";
  152. static const char *REJECTING = "Rejecting";
  153. static const char *UNKNOWN = "Unknown";
  154. #define _DYNAMIC "D"
  155. #ifdef HAVE_OPENCL
  156. static const char *DYNAMIC = _DYNAMIC;
  157. #endif
  158. static const char *YES = "Y";
  159. static const char *NO = "N";
  160. static const char *NULLSTR = "(null)";
  161. static const char *TRUESTR = "true";
  162. static const char *FALSESTR = "false";
  163. #ifdef USE_SCRYPT
  164. static const char *SCRYPTSTR = "scrypt";
  165. #endif
  166. static const char *SHA256STR = "sha256";
  167. static const char *DEVICECODE = ""
  168. #ifdef HAVE_OPENCL
  169. "GPU "
  170. #endif
  171. #ifdef USE_BITFORCE
  172. "BFL "
  173. #endif
  174. #ifdef USE_ICARUS
  175. "ICA "
  176. #endif
  177. #ifdef USE_ZTEX
  178. "ZTX "
  179. #endif
  180. #ifdef USE_MODMINER
  181. "MMQ "
  182. #endif
  183. #ifdef WANT_CPUMINE
  184. "CPU "
  185. #endif
  186. "";
  187. static const char *OSINFO =
  188. #if defined(__linux)
  189. "Linux";
  190. #else
  191. #if defined(__APPLE__)
  192. "Apple";
  193. #else
  194. #if defined (WIN32)
  195. "Windows";
  196. #else
  197. #if defined(unix)
  198. "Unix";
  199. #else
  200. "Unknown";
  201. #endif
  202. #endif
  203. #endif
  204. #endif
  205. #define _DEVS "DEVS"
  206. #define _POOLS "POOLS"
  207. #define _SUMMARY "SUMMARY"
  208. #define _STATUS "STATUS"
  209. #define _VERSION "VERSION"
  210. #define _MINECONFIG "CONFIG"
  211. #define _GPU "GPU"
  212. #ifdef HAVE_AN_FPGA
  213. #define _PGA "PGA"
  214. #endif
  215. #ifdef WANT_CPUMINE
  216. #define _CPU "CPU"
  217. #endif
  218. #define _GPUS "GPUS"
  219. #define _PGAS "PGAS"
  220. #define _CPUS "CPUS"
  221. #define _NOTIFY "NOTIFY"
  222. #define _DEVDETAILS "DEVDETAILS"
  223. #define _BYE "BYE"
  224. #define _RESTART "RESTART"
  225. #define _MINESTATS "STATS"
  226. #define _CHECK "CHECK"
  227. #define _MINECOIN "COIN"
  228. #define _DEBUGSET "DEBUG"
  229. #define _SETCONFIG "SETCONFIG"
  230. static const char ISJSON = '{';
  231. #define JSON0 "{"
  232. #define JSON1 "\""
  233. #define JSON2 "\":["
  234. #define JSON3 "]"
  235. #define JSON4 ",\"id\":1"
  236. #define JSON5 "}"
  237. #define JSON_START JSON0
  238. #define JSON_DEVS JSON1 _DEVS JSON2
  239. #define JSON_POOLS JSON1 _POOLS JSON2
  240. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  241. #define JSON_STATUS JSON1 _STATUS JSON2
  242. #define JSON_VERSION JSON1 _VERSION JSON2
  243. #define JSON_MINECONFIG JSON1 _MINECONFIG JSON2
  244. #define JSON_GPU JSON1 _GPU JSON2
  245. #ifdef HAVE_AN_FPGA
  246. #define JSON_PGA JSON1 _PGA JSON2
  247. #endif
  248. #ifdef WANT_CPUMINE
  249. #define JSON_CPU JSON1 _CPU JSON2
  250. #endif
  251. #define JSON_GPUS JSON1 _GPUS JSON2
  252. #define JSON_PGAS JSON1 _PGAS JSON2
  253. #define JSON_CPUS JSON1 _CPUS JSON2
  254. #define JSON_NOTIFY JSON1 _NOTIFY JSON2
  255. #define JSON_DEVDETAILS JSON1 _DEVDETAILS JSON2
  256. #define JSON_BYE JSON1 _BYE JSON1
  257. #define JSON_RESTART JSON1 _RESTART JSON1
  258. #define JSON_CLOSE JSON3
  259. #define JSON_MINESTATS JSON1 _MINESTATS JSON2
  260. #define JSON_CHECK JSON1 _CHECK JSON2
  261. #define JSON_MINECOIN JSON1 _MINECOIN JSON2
  262. #define JSON_DEBUGSET JSON1 _DEBUGSET JSON2
  263. #define JSON_SETCONFIG JSON1 _SETCONFIG JSON2
  264. #define JSON_END JSON4 JSON5
  265. static const char *JSON_COMMAND = "command";
  266. static const char *JSON_PARAMETER = "parameter";
  267. #define MSG_INVGPU 1
  268. #define MSG_ALRENA 2
  269. #define MSG_ALRDIS 3
  270. #define MSG_GPUMRE 4
  271. #define MSG_GPUREN 5
  272. #define MSG_GPUNON 6
  273. #define MSG_POOL 7
  274. #define MSG_NOPOOL 8
  275. #define MSG_DEVS 9
  276. #define MSG_NODEVS 10
  277. #define MSG_SUMM 11
  278. #define MSG_GPUDIS 12
  279. #define MSG_GPUREI 13
  280. #define MSG_INVCMD 14
  281. #define MSG_MISID 15
  282. #define MSG_GPUDEV 17
  283. #ifdef WANT_CPUMINE
  284. #define MSG_CPUNON 16
  285. #define MSG_CPUDEV 18
  286. #define MSG_INVCPU 19
  287. #endif
  288. #define MSG_NUMGPU 20
  289. #define MSG_NUMCPU 21
  290. #define MSG_VERSION 22
  291. #define MSG_INVJSON 23
  292. #define MSG_MISCMD 24
  293. #define MSG_MISPID 25
  294. #define MSG_INVPID 26
  295. #define MSG_SWITCHP 27
  296. #define MSG_MISVAL 28
  297. #define MSG_NOADL 29
  298. #define MSG_NOGPUADL 30
  299. #define MSG_INVINT 31
  300. #define MSG_GPUINT 32
  301. #define MSG_MINECONFIG 33
  302. #define MSG_GPUMERR 34
  303. #define MSG_GPUMEM 35
  304. #define MSG_GPUEERR 36
  305. #define MSG_GPUENG 37
  306. #define MSG_GPUVERR 38
  307. #define MSG_GPUVDDC 39
  308. #define MSG_GPUFERR 40
  309. #define MSG_GPUFAN 41
  310. #define MSG_MISFN 42
  311. #define MSG_BADFN 43
  312. #define MSG_SAVED 44
  313. #define MSG_ACCDENY 45
  314. #define MSG_ACCOK 46
  315. #define MSG_ENAPOOL 47
  316. #define MSG_DISPOOL 48
  317. #define MSG_ALRENAP 49
  318. #define MSG_ALRDISP 50
  319. #define MSG_DISLASTP 51
  320. #define MSG_MISPDP 52
  321. #define MSG_INVPDP 53
  322. #define MSG_TOOMANYP 54
  323. #define MSG_ADDPOOL 55
  324. #ifdef HAVE_AN_FPGA
  325. #define MSG_PGANON 56
  326. #define MSG_PGADEV 57
  327. #define MSG_INVPGA 58
  328. #endif
  329. #define MSG_NUMPGA 59
  330. #define MSG_NOTIFY 60
  331. #ifdef HAVE_AN_FPGA
  332. #define MSG_PGALRENA 61
  333. #define MSG_PGALRDIS 62
  334. #define MSG_PGAENA 63
  335. #define MSG_PGADIS 64
  336. #define MSG_PGAUNW 65
  337. #endif
  338. #define MSG_REMLASTP 66
  339. #define MSG_ACTPOOL 67
  340. #define MSG_REMPOOL 68
  341. #define MSG_DEVDETAILS 69
  342. #define MSG_MINESTATS 70
  343. #define MSG_MISCHK 71
  344. #define MSG_CHECK 72
  345. #define MSG_POOLPRIO 73
  346. #define MSG_DUPPID 74
  347. #define MSG_MISBOOL 75
  348. #define MSG_INVBOOL 76
  349. #define MSG_FOO 77
  350. #define MSG_MINECOIN 78
  351. #define MSG_DEBUGSET 79
  352. #define MSG_SETCONFIG 82
  353. #define MSG_UNKCON 83
  354. #define MSG_INVNUM 84
  355. #define MSG_CONPAR 85
  356. #define MSG_CONVAL 86
  357. enum code_severity {
  358. SEVERITY_ERR,
  359. SEVERITY_WARN,
  360. SEVERITY_INFO,
  361. SEVERITY_SUCC,
  362. SEVERITY_FAIL
  363. };
  364. enum code_parameters {
  365. PARAM_GPU,
  366. PARAM_PGA,
  367. PARAM_CPU,
  368. PARAM_PID,
  369. PARAM_GPUMAX,
  370. PARAM_PGAMAX,
  371. PARAM_CPUMAX,
  372. PARAM_PMAX,
  373. PARAM_POOLMAX,
  374. // Single generic case: have the code resolve it - see below
  375. PARAM_DMAX,
  376. PARAM_CMD,
  377. PARAM_POOL,
  378. PARAM_STR,
  379. PARAM_BOTH,
  380. PARAM_BOOL,
  381. PARAM_SET,
  382. PARAM_NONE
  383. };
  384. struct CODES {
  385. const enum code_severity severity;
  386. const int code;
  387. const enum code_parameters params;
  388. const char *description;
  389. } codes[] = {
  390. #ifdef HAVE_OPENCL
  391. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  392. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  393. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  394. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  395. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  396. #endif
  397. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  398. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  399. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  400. { SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX,
  401. #ifdef HAVE_OPENCL
  402. "%d GPU(s)"
  403. #endif
  404. #if defined(HAVE_AN_FPGA) && defined(HAVE_OPENCL)
  405. " - "
  406. #endif
  407. #ifdef HAVE_AN_FPGA
  408. "%d PGA(s)"
  409. #endif
  410. #if defined(WANT_CPUMINE) && (defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA))
  411. " - "
  412. #endif
  413. #ifdef WANT_CPUMINE
  414. "%d CPU(s)"
  415. #endif
  416. },
  417. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
  418. #ifdef HAVE_AN_FPGA
  419. "/PGAs"
  420. #endif
  421. #ifdef WANT_CPUMINE
  422. "/CPUs"
  423. #endif
  424. },
  425. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  426. #ifdef HAVE_OPENCL
  427. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  428. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  429. #endif
  430. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  431. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  432. #ifdef HAVE_OPENCL
  433. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  434. #endif
  435. #ifdef HAVE_AN_FPGA
  436. { SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
  437. { SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
  438. { SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
  439. { SEVERITY_INFO, MSG_PGALRENA,PARAM_PGA, "PGA %d already enabled" },
  440. { SEVERITY_INFO, MSG_PGALRDIS,PARAM_PGA, "PGA %d already disabled" },
  441. { SEVERITY_INFO, MSG_PGAENA, PARAM_PGA, "PGA %d sent enable message" },
  442. { SEVERITY_INFO, MSG_PGADIS, PARAM_PGA, "PGA %d set disable flag" },
  443. { SEVERITY_ERR, MSG_PGAUNW, PARAM_PGA, "PGA %d is not flagged WELL, cannot enable" },
  444. #endif
  445. #ifdef WANT_CPUMINE
  446. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  447. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  448. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  449. #endif
  450. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  451. { SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
  452. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  453. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "BFGMiner versions" },
  454. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  455. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  456. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  457. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  458. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  459. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  460. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  461. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  462. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  463. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  464. { SEVERITY_SUCC, MSG_MINECONFIG,PARAM_NONE, "BFGMiner config" },
  465. #ifdef HAVE_OPENCL
  466. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  467. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported success" },
  468. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  469. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported success" },
  470. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  471. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported success" },
  472. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  473. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported success" },
  474. #endif
  475. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  476. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  477. { SEVERITY_SUCC, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  478. { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
  479. { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
  480. { SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
  481. { SEVERITY_SUCC, MSG_POOLPRIO,PARAM_NONE, "Changed pool priorities" },
  482. { SEVERITY_ERR, MSG_DUPPID, PARAM_PID, "Duplicate pool specified %d" },
  483. { SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
  484. { SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
  485. { SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
  486. { SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
  487. { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
  488. { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
  489. { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
  490. { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
  491. { SEVERITY_ERR, MSG_REMLASTP,PARAM_POOL, "Cannot remove last pool %d:'%s'" },
  492. { SEVERITY_ERR, MSG_ACTPOOL, PARAM_POOL, "Cannot remove active pool %d:'%s'" },
  493. { SEVERITY_SUCC, MSG_REMPOOL, PARAM_BOTH, "Removed pool %d:'%s'" },
  494. { SEVERITY_SUCC, MSG_NOTIFY, PARAM_NONE, "Notify" },
  495. { SEVERITY_SUCC, MSG_DEVDETAILS,PARAM_NONE, "Device Details" },
  496. { SEVERITY_SUCC, MSG_MINESTATS,PARAM_NONE, "BFGMiner stats" },
  497. { SEVERITY_ERR, MSG_MISCHK, PARAM_NONE, "Missing check cmd" },
  498. { SEVERITY_SUCC, MSG_CHECK, PARAM_NONE, "Check command" },
  499. { SEVERITY_ERR, MSG_MISBOOL, PARAM_NONE, "Missing parameter: true/false" },
  500. { SEVERITY_ERR, MSG_INVBOOL, PARAM_NONE, "Invalid parameter should be true or false" },
  501. { SEVERITY_SUCC, MSG_FOO, PARAM_BOOL, "Failover-Only set to %s" },
  502. { SEVERITY_SUCC, MSG_MINECOIN,PARAM_NONE, "BFGMiner coin" },
  503. { SEVERITY_SUCC, MSG_DEBUGSET,PARAM_STR, "Debug settings" },
  504. { SEVERITY_SUCC, MSG_SETCONFIG,PARAM_SET, "Set config '%s' to %d" },
  505. { SEVERITY_ERR, MSG_UNKCON, PARAM_STR, "Unknown config '%s'" },
  506. { SEVERITY_ERR, MSG_INVNUM, PARAM_BOTH, "Invalid number (%d) for '%s' range is 0-9999" },
  507. { SEVERITY_ERR, MSG_CONPAR, PARAM_NONE, "Missing config parameters 'name,N'" },
  508. { SEVERITY_ERR, MSG_CONVAL, PARAM_STR, "Missing config value N for '%s,N'" },
  509. { SEVERITY_FAIL, 0, 0, NULL }
  510. };
  511. static int my_thr_id = 0;
  512. static bool bye;
  513. static bool ping = true;
  514. // Used to control quit restart access to shutdown variables
  515. static pthread_mutex_t quit_restart_lock;
  516. static bool do_a_quit;
  517. static bool do_a_restart;
  518. static time_t when = 0; // when the request occurred
  519. struct IP4ACCESS {
  520. in_addr_t ip;
  521. in_addr_t mask;
  522. char group;
  523. };
  524. #define GROUP(g) (toupper(g))
  525. #define PRIVGROUP GROUP('W')
  526. #define NOPRIVGROUP GROUP('R')
  527. #define ISPRIVGROUP(g) (GROUP(g) == PRIVGROUP)
  528. #define GROUPOFFSET(g) (GROUP(g) - GROUP('A'))
  529. #define VALIDGROUP(g) (GROUP(g) >= GROUP('A') && GROUP(g) <= GROUP('Z'))
  530. #define COMMANDS(g) (apigroups[GROUPOFFSET(g)].commands)
  531. #define DEFINEDGROUP(g) (ISPRIVGROUP(g) || COMMANDS(g) != NULL)
  532. struct APIGROUPS {
  533. // This becomes a string like: "|cmd1|cmd2|cmd3|" so it's quick to search
  534. char *commands;
  535. } apigroups['Z' - 'A' + 1]; // only A=0 to Z=25 (R: noprivs, W: allprivs)
  536. static struct IP4ACCESS *ipaccess = NULL;
  537. static int ips = 0;
  538. #ifdef USE_BITFORCE
  539. extern struct device_api bitforce_api;
  540. #endif
  541. #ifdef USE_ICARUS
  542. extern struct device_api icarus_api;
  543. #endif
  544. #ifdef USE_ZTEX
  545. extern struct device_api ztex_api;
  546. #endif
  547. #ifdef USE_MODMINER
  548. extern struct device_api modminer_api;
  549. #endif
  550. // This is only called when expected to be needed (rarely)
  551. // i.e. strings outside of the codes control (input from the user)
  552. static char *escape_string(char *str, bool isjson)
  553. {
  554. char *buf, *ptr;
  555. int count;
  556. count = 0;
  557. for (ptr = str; *ptr; ptr++) {
  558. switch (*ptr) {
  559. case ',':
  560. case '|':
  561. case '=':
  562. if (!isjson)
  563. count++;
  564. break;
  565. case '"':
  566. if (isjson)
  567. count++;
  568. break;
  569. case '\\':
  570. count++;
  571. break;
  572. }
  573. }
  574. if (count == 0)
  575. return str;
  576. buf = malloc(strlen(str) + count + 1);
  577. if (unlikely(!buf))
  578. quit(1, "Failed to malloc escape buf");
  579. ptr = buf;
  580. while (*str)
  581. switch (*str) {
  582. case ',':
  583. case '|':
  584. case '=':
  585. if (!isjson)
  586. *(ptr++) = '\\';
  587. *(ptr++) = *(str++);
  588. break;
  589. case '"':
  590. if (isjson)
  591. *(ptr++) = '\\';
  592. *(ptr++) = *(str++);
  593. break;
  594. case '\\':
  595. *(ptr++) = '\\';
  596. *(ptr++) = *(str++);
  597. break;
  598. default:
  599. *(ptr++) = *(str++);
  600. break;
  601. }
  602. *ptr = '\0';
  603. return buf;
  604. }
  605. static struct api_data *api_add_extra(struct api_data *root, struct api_data *extra)
  606. {
  607. struct api_data *tmp;
  608. if (root) {
  609. if (extra) {
  610. // extra tail
  611. tmp = extra->prev;
  612. // extra prev = root tail
  613. extra->prev = root->prev;
  614. // root tail next = extra
  615. root->prev->next = extra;
  616. // extra tail next = root
  617. tmp->next = root;
  618. // root prev = extra tail
  619. root->prev = tmp;
  620. }
  621. } else
  622. root = extra;
  623. return root;
  624. }
  625. static struct api_data *api_add_data_full(struct api_data *root, char *name, enum api_data_type type, void *data, bool copy_data)
  626. {
  627. struct api_data *api_data;
  628. api_data = (struct api_data *)malloc(sizeof(struct api_data));
  629. api_data->name = name;
  630. api_data->type = type;
  631. if (root == NULL) {
  632. root = api_data;
  633. root->prev = root;
  634. root->next = root;
  635. }
  636. else {
  637. api_data->prev = root->prev;
  638. root->prev = api_data;
  639. api_data->next = root;
  640. api_data->prev->next = api_data;
  641. }
  642. api_data->data_was_malloc = copy_data;
  643. // Avoid crashing on bad data
  644. if (data == NULL) {
  645. api_data->type = type = API_CONST;
  646. data = (void *)NULLSTR;
  647. api_data->data_was_malloc = copy_data = false;
  648. }
  649. if (!copy_data)
  650. {
  651. api_data->data = data;
  652. if (type == API_JSON)
  653. json_incref((json_t *)data);
  654. }
  655. else
  656. switch(type) {
  657. case API_ESCAPE:
  658. case API_STRING:
  659. case API_CONST:
  660. api_data->data = (void *)malloc(strlen((char *)data) + 1);
  661. strcpy((char*)(api_data->data), (char *)data);
  662. break;
  663. case API_INT:
  664. api_data->data = (void *)malloc(sizeof(int));
  665. *((int *)(api_data->data)) = *((int *)data);
  666. break;
  667. case API_UINT:
  668. api_data->data = (void *)malloc(sizeof(unsigned int));
  669. *((unsigned int *)(api_data->data)) = *((unsigned int *)data);
  670. break;
  671. case API_UINT32:
  672. api_data->data = (void *)malloc(sizeof(uint32_t));
  673. *((uint32_t *)(api_data->data)) = *((uint32_t *)data);
  674. break;
  675. case API_UINT64:
  676. api_data->data = (void *)malloc(sizeof(uint64_t));
  677. *((uint64_t *)(api_data->data)) = *((uint64_t *)data);
  678. break;
  679. case API_DOUBLE:
  680. case API_ELAPSED:
  681. case API_MHS:
  682. case API_MHTOTAL:
  683. case API_UTILITY:
  684. case API_FREQ:
  685. case API_HS:
  686. api_data->data = (void *)malloc(sizeof(double));
  687. *((double *)(api_data->data)) = *((double *)data);
  688. break;
  689. case API_BOOL:
  690. api_data->data = (void *)malloc(sizeof(bool));
  691. *((bool *)(api_data->data)) = *((bool *)data);
  692. break;
  693. case API_TIMEVAL:
  694. api_data->data = (void *)malloc(sizeof(struct timeval));
  695. memcpy(api_data->data, data, sizeof(struct timeval));
  696. break;
  697. case API_TIME:
  698. api_data->data = (void *)malloc(sizeof(time_t));
  699. *(time_t *)(api_data->data) = *((time_t *)data);
  700. break;
  701. case API_VOLTS:
  702. case API_TEMP:
  703. api_data->data = (void *)malloc(sizeof(float));
  704. *((float *)(api_data->data)) = *((float *)data);
  705. break;
  706. case API_JSON:
  707. api_data->data_was_malloc = false;
  708. api_data->data = (void *)json_deep_copy((json_t *)data);
  709. break;
  710. default:
  711. applog(LOG_ERR, "API: unknown1 data type %d ignored", type);
  712. api_data->type = API_STRING;
  713. api_data->data_was_malloc = false;
  714. api_data->data = (void *)UNKNOWN;
  715. break;
  716. }
  717. return root;
  718. }
  719. struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data)
  720. {
  721. return api_add_data_full(root, name, API_ESCAPE, (void *)data, copy_data);
  722. }
  723. struct api_data *api_add_string(struct api_data *root, char *name, const char *data, bool copy_data)
  724. {
  725. return api_add_data_full(root, name, API_STRING, (void *)data, copy_data);
  726. }
  727. struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data)
  728. {
  729. return api_add_data_full(root, name, API_CONST, (void *)data, copy_data);
  730. }
  731. struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data)
  732. {
  733. return api_add_data_full(root, name, API_INT, (void *)data, copy_data);
  734. }
  735. struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data)
  736. {
  737. return api_add_data_full(root, name, API_UINT, (void *)data, copy_data);
  738. }
  739. struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data)
  740. {
  741. return api_add_data_full(root, name, API_UINT32, (void *)data, copy_data);
  742. }
  743. struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data)
  744. {
  745. return api_add_data_full(root, name, API_UINT64, (void *)data, copy_data);
  746. }
  747. struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data)
  748. {
  749. return api_add_data_full(root, name, API_DOUBLE, (void *)data, copy_data);
  750. }
  751. struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data)
  752. {
  753. return api_add_data_full(root, name, API_ELAPSED, (void *)data, copy_data);
  754. }
  755. struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data)
  756. {
  757. return api_add_data_full(root, name, API_BOOL, (void *)data, copy_data);
  758. }
  759. struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data)
  760. {
  761. return api_add_data_full(root, name, API_TIMEVAL, (void *)data, copy_data);
  762. }
  763. struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data)
  764. {
  765. return api_add_data_full(root, name, API_TIME, (void *)data, copy_data);
  766. }
  767. struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data)
  768. {
  769. return api_add_data_full(root, name, API_MHS, (void *)data, copy_data);
  770. }
  771. struct api_data *api_add_mhtotal(struct api_data *root, char *name, double *data, bool copy_data)
  772. {
  773. return api_add_data_full(root, name, API_MHTOTAL, (void *)data, copy_data);
  774. }
  775. struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data)
  776. {
  777. return api_add_data_full(root, name, API_TEMP, (void *)data, copy_data);
  778. }
  779. struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data)
  780. {
  781. return api_add_data_full(root, name, API_UTILITY, (void *)data, copy_data);
  782. }
  783. struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data)
  784. {
  785. return api_add_data_full(root, name, API_FREQ, (void *)data, copy_data);
  786. }
  787. struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data)
  788. {
  789. return api_add_data_full(root, name, API_VOLTS, (void *)data, copy_data);
  790. }
  791. struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data)
  792. {
  793. return api_add_data_full(root, name, API_HS, (void *)data, copy_data);
  794. }
  795. struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data)
  796. {
  797. return api_add_data_full(root, name, API_JSON, (void *)data, copy_data);
  798. }
  799. static struct api_data *print_data(struct api_data *root, char *buf, bool isjson)
  800. {
  801. struct api_data *tmp;
  802. bool first = true;
  803. char *original, *escape;
  804. char *quote;
  805. if (isjson) {
  806. strcpy(buf, JSON0);
  807. buf = strchr(buf, '\0');
  808. quote = JSON1;
  809. } else
  810. quote = (char *)BLANK;
  811. while (root) {
  812. if (!first)
  813. *(buf++) = *COMMA;
  814. else
  815. first = false;
  816. sprintf(buf, "%s%s%s%s", quote, root->name, quote, isjson ? ":" : "=");
  817. buf = strchr(buf, '\0');
  818. switch(root->type) {
  819. case API_STRING:
  820. case API_CONST:
  821. sprintf(buf, "%s%s%s", quote, (char *)(root->data), quote);
  822. break;
  823. case API_ESCAPE:
  824. original = (char *)(root->data);
  825. escape = escape_string((char *)(root->data), isjson);
  826. sprintf(buf, "%s%s%s", quote, escape, quote);
  827. if (escape != original)
  828. free(escape);
  829. break;
  830. case API_INT:
  831. sprintf(buf, "%d", *((int *)(root->data)));
  832. break;
  833. case API_UINT:
  834. sprintf(buf, "%u", *((unsigned int *)(root->data)));
  835. break;
  836. case API_UINT32:
  837. sprintf(buf, "%"PRIu32, *((uint32_t *)(root->data)));
  838. break;
  839. case API_UINT64:
  840. sprintf(buf, "%"PRIu64, *((uint64_t *)(root->data)));
  841. break;
  842. case API_TIME:
  843. sprintf(buf, "%lu", *((unsigned long *)(root->data)));
  844. break;
  845. case API_DOUBLE:
  846. sprintf(buf, "%f", *((double *)(root->data)));
  847. break;
  848. case API_ELAPSED:
  849. sprintf(buf, "%.0f", *((double *)(root->data)));
  850. break;
  851. case API_UTILITY:
  852. case API_FREQ:
  853. case API_MHS:
  854. sprintf(buf, "%.2f", *((double *)(root->data)));
  855. break;
  856. case API_VOLTS:
  857. sprintf(buf, "%.3f", *((float *)(root->data)));
  858. break;
  859. case API_MHTOTAL:
  860. sprintf(buf, "%.4f", *((double *)(root->data)));
  861. break;
  862. case API_HS:
  863. sprintf(buf, "%.15f", *((double *)(root->data)));
  864. break;
  865. case API_BOOL:
  866. sprintf(buf, "%s", *((bool *)(root->data)) ? TRUESTR : FALSESTR);
  867. break;
  868. case API_TIMEVAL:
  869. sprintf(buf, "%ld.%06ld",
  870. ((struct timeval *)(root->data))->tv_sec,
  871. ((struct timeval *)(root->data))->tv_usec);
  872. break;
  873. case API_TEMP:
  874. sprintf(buf, "%.2f", *((float *)(root->data)));
  875. break;
  876. case API_JSON:
  877. escape = json_dumps((json_t *)(root->data), JSON_COMPACT);
  878. strcpy(buf, escape);
  879. free(escape);
  880. break;
  881. default:
  882. applog(LOG_ERR, "API: unknown2 data type %d ignored", root->type);
  883. sprintf(buf, "%s%s%s", quote, UNKNOWN, quote);
  884. break;
  885. }
  886. buf = strchr(buf, '\0');
  887. if (root->type == API_JSON)
  888. json_decref((json_t *)root->data);
  889. if (root->data_was_malloc)
  890. free(root->data);
  891. if (root->next == root) {
  892. free(root);
  893. root = NULL;
  894. } else {
  895. tmp = root;
  896. root = tmp->next;
  897. root->prev = tmp->prev;
  898. root->prev->next = root;
  899. free(tmp);
  900. }
  901. }
  902. strcpy(buf, isjson ? JSON5 : SEPSTR);
  903. return root;
  904. }
  905. #ifdef HAVE_AN_FPGA
  906. static int numpgas()
  907. {
  908. int count = 0;
  909. int i;
  910. for (i = 0; i < total_devices; i++) {
  911. #ifdef USE_BITFORCE
  912. if (devices[i]->api == &bitforce_api)
  913. count++;
  914. #endif
  915. #ifdef USE_ICARUS
  916. if (devices[i]->api == &icarus_api)
  917. count++;
  918. #endif
  919. #ifdef USE_ZTEX
  920. if (devices[i]->api == &ztex_api)
  921. count++;
  922. #endif
  923. #ifdef USE_MODMINER
  924. if (devices[i]->api == &modminer_api)
  925. count++;
  926. #endif
  927. }
  928. return count;
  929. }
  930. static int pgadevice(int pgaid)
  931. {
  932. int count = 0;
  933. int i;
  934. for (i = 0; i < total_devices; i++) {
  935. #ifdef USE_BITFORCE
  936. if (devices[i]->api == &bitforce_api)
  937. count++;
  938. #endif
  939. #ifdef USE_ICARUS
  940. if (devices[i]->api == &icarus_api)
  941. count++;
  942. #endif
  943. #ifdef USE_ZTEX
  944. if (devices[i]->api == &ztex_api)
  945. count++;
  946. #endif
  947. #ifdef USE_MODMINER
  948. if (devices[i]->api == &modminer_api)
  949. count++;
  950. #endif
  951. if (count == (pgaid + 1))
  952. return i;
  953. }
  954. return -1;
  955. }
  956. #endif
  957. // All replies (except BYE and RESTART) start with a message
  958. // thus for JSON, message() inserts JSON_START at the front
  959. // and send_result() adds JSON_END at the end
  960. static char *message(int messageid, int paramid, char *param2, bool isjson)
  961. {
  962. struct api_data *root = NULL;
  963. char buf[TMPBUFSIZ];
  964. char severity[2];
  965. char *ptr;
  966. #ifdef HAVE_AN_FPGA
  967. int pga;
  968. #endif
  969. #ifdef WANT_CPUMINE
  970. int cpu;
  971. #endif
  972. int i;
  973. if (!isjson)
  974. msg_buffer[0] = '\0';
  975. else
  976. strcpy(msg_buffer, JSON_START JSON_STATUS);
  977. ptr = strchr(msg_buffer, '\0');
  978. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  979. if (codes[i].code == messageid) {
  980. switch (codes[i].severity) {
  981. case SEVERITY_WARN:
  982. severity[0] = 'W';
  983. break;
  984. case SEVERITY_INFO:
  985. severity[0] = 'I';
  986. break;
  987. case SEVERITY_SUCC:
  988. severity[0] = 'S';
  989. break;
  990. case SEVERITY_ERR:
  991. default:
  992. severity[0] = 'E';
  993. break;
  994. }
  995. severity[1] = '\0';
  996. switch(codes[i].params) {
  997. case PARAM_GPU:
  998. case PARAM_PGA:
  999. case PARAM_CPU:
  1000. case PARAM_PID:
  1001. sprintf(buf, codes[i].description, paramid);
  1002. break;
  1003. case PARAM_POOL:
  1004. sprintf(buf, codes[i].description, paramid, pools[paramid]->rpc_url);
  1005. break;
  1006. #ifdef HAVE_OPENCL
  1007. case PARAM_GPUMAX:
  1008. sprintf(buf, codes[i].description, paramid, nDevs - 1);
  1009. break;
  1010. #endif
  1011. #ifdef HAVE_AN_FPGA
  1012. case PARAM_PGAMAX:
  1013. pga = numpgas();
  1014. sprintf(buf, codes[i].description, paramid, pga - 1);
  1015. break;
  1016. #endif
  1017. #ifdef WANT_CPUMINE
  1018. case PARAM_CPUMAX:
  1019. if (opt_n_threads > 0)
  1020. cpu = num_processors;
  1021. else
  1022. cpu = 0;
  1023. sprintf(buf, codes[i].description, paramid, cpu - 1);
  1024. break;
  1025. #endif
  1026. case PARAM_PMAX:
  1027. sprintf(buf, codes[i].description, total_pools);
  1028. break;
  1029. case PARAM_POOLMAX:
  1030. sprintf(buf, codes[i].description, paramid, total_pools - 1);
  1031. break;
  1032. case PARAM_DMAX:
  1033. #ifdef HAVE_AN_FPGA
  1034. pga = numpgas();
  1035. #endif
  1036. #ifdef WANT_CPUMINE
  1037. if (opt_n_threads > 0)
  1038. cpu = num_processors;
  1039. else
  1040. cpu = 0;
  1041. #endif
  1042. sprintf(buf, codes[i].description
  1043. #ifdef HAVE_OPENCL
  1044. , nDevs
  1045. #endif
  1046. #ifdef HAVE_AN_FPGA
  1047. , pga
  1048. #endif
  1049. #ifdef WANT_CPUMINE
  1050. , cpu
  1051. #endif
  1052. );
  1053. break;
  1054. case PARAM_CMD:
  1055. sprintf(buf, codes[i].description, JSON_COMMAND);
  1056. break;
  1057. case PARAM_STR:
  1058. sprintf(buf, codes[i].description, param2);
  1059. break;
  1060. case PARAM_BOTH:
  1061. sprintf(buf, codes[i].description, paramid, param2);
  1062. break;
  1063. case PARAM_BOOL:
  1064. sprintf(buf, codes[i].description, paramid ? TRUESTR : FALSESTR);
  1065. break;
  1066. case PARAM_SET:
  1067. sprintf(buf, codes[i].description, param2, paramid);
  1068. break;
  1069. case PARAM_NONE:
  1070. default:
  1071. strcpy(buf, codes[i].description);
  1072. }
  1073. root = api_add_string(root, _STATUS, severity, false);
  1074. root = api_add_time(root, "When", &when, false);
  1075. root = api_add_int(root, "Code", &messageid, false);
  1076. root = api_add_escape(root, "Msg", buf, false);
  1077. root = api_add_escape(root, "Description", opt_api_description, false);
  1078. root = print_data(root, ptr, isjson);
  1079. if (isjson)
  1080. strcat(ptr, JSON_CLOSE);
  1081. return msg_buffer;
  1082. }
  1083. }
  1084. root = api_add_string(root, _STATUS, "F", false);
  1085. root = api_add_time(root, "When", &when, false);
  1086. int id = -1;
  1087. root = api_add_int(root, "Code", &id, false);
  1088. sprintf(buf, "%d", messageid);
  1089. root = api_add_escape(root, "Msg", buf, false);
  1090. root = api_add_escape(root, "Description", opt_api_description, false);
  1091. root = print_data(root, ptr, isjson);
  1092. if (isjson)
  1093. strcat(ptr, JSON_CLOSE);
  1094. return msg_buffer;
  1095. }
  1096. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1097. {
  1098. struct api_data *root = NULL;
  1099. char buf[TMPBUFSIZ];
  1100. sprintf(io_buffer, isjson
  1101. ? "%s," JSON_VERSION
  1102. : "%s" _VERSION ",",
  1103. message(MSG_VERSION, 0, NULL, isjson));
  1104. root = api_add_string(root, "CGMiner", VERSION, false);
  1105. root = api_add_const(root, "API", APIVERSION, false);
  1106. root = print_data(root, buf, isjson);
  1107. if (isjson)
  1108. strcat(buf, JSON_CLOSE);
  1109. strcat(io_buffer, buf);
  1110. }
  1111. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1112. {
  1113. struct api_data *root = NULL;
  1114. char buf[TMPBUFSIZ];
  1115. int gpucount = 0;
  1116. int pgacount = 0;
  1117. int cpucount = 0;
  1118. char *adlinuse = (char *)NO;
  1119. #ifdef HAVE_ADL
  1120. const char *adl = YES;
  1121. int i;
  1122. for (i = 0; i < nDevs; i++) {
  1123. if (gpus[i].has_adl) {
  1124. adlinuse = (char *)YES;
  1125. break;
  1126. }
  1127. }
  1128. #else
  1129. const char *adl = NO;
  1130. #endif
  1131. #ifdef HAVE_OPENCL
  1132. gpucount = nDevs;
  1133. #endif
  1134. #ifdef HAVE_AN_FPGA
  1135. pgacount = numpgas();
  1136. #endif
  1137. #ifdef WANT_CPUMINE
  1138. cpucount = opt_n_threads > 0 ? num_processors : 0;
  1139. #endif
  1140. sprintf(io_buffer, isjson
  1141. ? "%s," JSON_MINECONFIG
  1142. : "%s" _MINECONFIG ",",
  1143. message(MSG_MINECONFIG, 0, NULL, isjson));
  1144. root = api_add_int(root, "GPU Count", &gpucount, false);
  1145. root = api_add_int(root, "PGA Count", &pgacount, false);
  1146. root = api_add_int(root, "CPU Count", &cpucount, false);
  1147. root = api_add_int(root, "Pool Count", &total_pools, false);
  1148. root = api_add_const(root, "ADL", (char *)adl, false);
  1149. root = api_add_string(root, "ADL in use", adlinuse, false);
  1150. root = api_add_const(root, "Strategy", strategies[pool_strategy].s, false);
  1151. root = api_add_int(root, "Log Interval", &opt_log_interval, false);
  1152. root = api_add_const(root, "Device Code", DEVICECODE, false);
  1153. root = api_add_const(root, "OS", OSINFO, false);
  1154. root = api_add_bool(root, "Failover-Only", &opt_fail_only, false);
  1155. root = api_add_int(root, "ScanTime", &opt_scantime, false);
  1156. root = api_add_int(root, "Queue", &opt_queue, false);
  1157. root = api_add_int(root, "Expiry", &opt_expiry, false);
  1158. root = print_data(root, buf, isjson);
  1159. if (isjson)
  1160. strcat(buf, JSON_CLOSE);
  1161. strcat(io_buffer, buf);
  1162. }
  1163. static const char*
  1164. bool2str(bool b)
  1165. {
  1166. return b ? YES : NO;
  1167. }
  1168. static const char *status2str(enum alive status)
  1169. {
  1170. switch (status) {
  1171. case LIFE_WELL:
  1172. return ALIVE;
  1173. case LIFE_SICK:
  1174. return SICK;
  1175. case LIFE_DEAD:
  1176. return DEAD;
  1177. case LIFE_NOSTART:
  1178. return NOSTART;
  1179. case LIFE_INIT:
  1180. return INIT;
  1181. case LIFE_WAIT:
  1182. return WAIT;
  1183. default:
  1184. return UNKNOWN;
  1185. }
  1186. }
  1187. static void
  1188. devdetail_an(struct cgpu_info *cgpu, bool isjson)
  1189. {
  1190. struct api_data *root = NULL;
  1191. char buf[TMPBUFSIZ];
  1192. int n = 0, i;
  1193. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  1194. for (i = 0; i < total_devices; ++i) {
  1195. if (devices[i] == cgpu)
  1196. break;
  1197. if (cgpu->devtype == devices[i]->devtype)
  1198. ++n;
  1199. }
  1200. root = api_add_int(root, (char*)cgpu->devtype, &n, true);
  1201. root = api_add_string(root, "Driver", cgpu->api->dname, false);
  1202. if (cgpu->kname)
  1203. root = api_add_string(root, "Kernel", cgpu->kname, false);
  1204. if (cgpu->name)
  1205. root = api_add_string(root, "Model", cgpu->name, false);
  1206. if (cgpu->device_path)
  1207. root = api_add_string(root, "Device Path", cgpu->device_path, false);
  1208. if (cgpu->api->get_api_extra_device_detail)
  1209. root = api_add_extra(root, cgpu->api->get_api_extra_device_detail(cgpu));
  1210. root = print_data(root, buf, isjson);
  1211. strcat(io_buffer, buf);
  1212. }
  1213. static void devstatus_an(struct cgpu_info *cgpu, bool isjson)
  1214. {
  1215. struct api_data *root = NULL;
  1216. char buf[TMPBUFSIZ];
  1217. int n = 0, i;
  1218. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  1219. for (i = 0; i < total_devices; ++i) {
  1220. if (devices[i] == cgpu)
  1221. break;
  1222. if (cgpu->devtype == devices[i]->devtype)
  1223. ++n;
  1224. }
  1225. root = api_add_int(root, (char*)cgpu->devtype, &n, true);
  1226. root = api_add_string(root, "Name", cgpu->api->name, false);
  1227. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  1228. root = api_add_string(root, "Enabled", bool2str(cgpu->deven != DEV_DISABLED), false);
  1229. root = api_add_string(root, "Status", status2str(cgpu->status), false);
  1230. if (cgpu->temp)
  1231. root = api_add_temp(root, "Temperature", &cgpu->temp, false);
  1232. double mhs = cgpu->total_mhashes / total_secs;
  1233. root = api_add_mhs(root, "MHS av", &mhs, false);
  1234. char mhsname[27];
  1235. sprintf(mhsname, "MHS %ds", opt_log_interval);
  1236. root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
  1237. root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
  1238. root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
  1239. root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
  1240. root = api_add_utility(root, "Utility", &(cgpu->utility), false);
  1241. int last_share_pool = cgpu->last_share_pool_time > 0 ?
  1242. cgpu->last_share_pool : -1;
  1243. root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
  1244. root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
  1245. root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
  1246. root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
  1247. if (cgpu->api->get_api_extra_device_status)
  1248. root = api_add_extra(root, cgpu->api->get_api_extra_device_status(cgpu));
  1249. root = print_data(root, buf, isjson);
  1250. strcat(io_buffer, buf);
  1251. }
  1252. #ifdef HAVE_OPENCL
  1253. static void gpustatus(int gpu, bool isjson)
  1254. {
  1255. if (gpu < 0 || gpu >= nDevs)
  1256. return;
  1257. devstatus_an(&gpus[gpu], isjson);
  1258. }
  1259. #endif
  1260. #ifdef HAVE_AN_FPGA
  1261. static void pgastatus(int pga, bool isjson)
  1262. {
  1263. int dev = pgadevice(pga);
  1264. if (dev < 0) // Should never happen
  1265. return;
  1266. devstatus_an(devices[dev], isjson);
  1267. }
  1268. #endif
  1269. #ifdef WANT_CPUMINE
  1270. static void cpustatus(int cpu, bool isjson)
  1271. {
  1272. if (opt_n_threads <= 0 || cpu < 0 || cpu >= num_processors)
  1273. return;
  1274. devstatus_an(&cpus[cpu], isjson);
  1275. }
  1276. #endif
  1277. static void
  1278. devinfo_internal(void (*func)(struct cgpu_info*, bool), __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1279. {
  1280. int i;
  1281. if (total_devices == 0) {
  1282. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1283. return;
  1284. }
  1285. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  1286. if (isjson) {
  1287. strcat(io_buffer, COMMA);
  1288. strcat(io_buffer, JSON_DEVS);
  1289. }
  1290. for (i = 0; i < total_devices; ++i) {
  1291. if (isjson && i > 0)
  1292. strcat(io_buffer, COMMA);
  1293. func(devices[i], isjson);
  1294. }
  1295. if (isjson)
  1296. strcat(io_buffer, JSON_CLOSE);
  1297. }
  1298. static void
  1299. devdetail(SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1300. {
  1301. return devinfo_internal(devdetail_an, c, param, isjson, group);
  1302. }
  1303. static void
  1304. devstatus(SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1305. {
  1306. return devinfo_internal(devstatus_an, c, param, isjson, group);
  1307. }
  1308. #ifdef HAVE_OPENCL
  1309. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1310. {
  1311. int id;
  1312. if (nDevs == 0) {
  1313. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1314. return;
  1315. }
  1316. if (param == NULL || *param == '\0') {
  1317. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1318. return;
  1319. }
  1320. id = atoi(param);
  1321. if (id < 0 || id >= nDevs) {
  1322. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1323. return;
  1324. }
  1325. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  1326. if (isjson) {
  1327. strcat(io_buffer, COMMA);
  1328. strcat(io_buffer, JSON_GPU);
  1329. }
  1330. gpustatus(id, isjson);
  1331. if (isjson)
  1332. strcat(io_buffer, JSON_CLOSE);
  1333. }
  1334. #endif
  1335. #ifdef HAVE_AN_FPGA
  1336. static void pgadev(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1337. {
  1338. int numpga = numpgas();
  1339. int id;
  1340. if (numpga == 0) {
  1341. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  1342. return;
  1343. }
  1344. if (param == NULL || *param == '\0') {
  1345. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1346. return;
  1347. }
  1348. id = atoi(param);
  1349. if (id < 0 || id >= numpga) {
  1350. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1351. return;
  1352. }
  1353. strcpy(io_buffer, message(MSG_PGADEV, id, NULL, isjson));
  1354. if (isjson) {
  1355. strcat(io_buffer, COMMA);
  1356. strcat(io_buffer, JSON_PGA);
  1357. }
  1358. pgastatus(id, isjson);
  1359. if (isjson)
  1360. strcat(io_buffer, JSON_CLOSE);
  1361. }
  1362. static void pgaenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1363. {
  1364. int numpga = numpgas();
  1365. struct thr_info *thr;
  1366. int pga;
  1367. int id;
  1368. int i;
  1369. if (numpga == 0) {
  1370. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  1371. return;
  1372. }
  1373. if (param == NULL || *param == '\0') {
  1374. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1375. return;
  1376. }
  1377. id = atoi(param);
  1378. if (id < 0 || id >= numpga) {
  1379. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1380. return;
  1381. }
  1382. int dev = pgadevice(id);
  1383. if (dev < 0) { // Should never happen
  1384. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1385. return;
  1386. }
  1387. struct cgpu_info *cgpu = devices[dev];
  1388. if (cgpu->deven != DEV_DISABLED) {
  1389. strcpy(io_buffer, message(MSG_PGALRENA, id, NULL, isjson));
  1390. return;
  1391. }
  1392. #if 0 /* A DISABLED device wont change status FIXME: should disabling make it WELL? */
  1393. if (cgpu->status != LIFE_WELL) {
  1394. strcpy(io_buffer, message(MSG_PGAUNW, id, NULL, isjson));
  1395. return;
  1396. }
  1397. #endif
  1398. for (i = 0; i < mining_threads; i++) {
  1399. pga = thr_info[i].cgpu->device_id;
  1400. if (pga == dev) {
  1401. thr = &thr_info[i];
  1402. cgpu->deven = DEV_ENABLED;
  1403. tq_push(thr->q, &ping);
  1404. }
  1405. }
  1406. strcpy(io_buffer, message(MSG_PGAENA, id, NULL, isjson));
  1407. }
  1408. static void pgadisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1409. {
  1410. int numpga = numpgas();
  1411. int id;
  1412. if (numpga == 0) {
  1413. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  1414. return;
  1415. }
  1416. if (param == NULL || *param == '\0') {
  1417. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1418. return;
  1419. }
  1420. id = atoi(param);
  1421. if (id < 0 || id >= numpga) {
  1422. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1423. return;
  1424. }
  1425. int dev = pgadevice(id);
  1426. if (dev < 0) { // Should never happen
  1427. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  1428. return;
  1429. }
  1430. struct cgpu_info *cgpu = devices[dev];
  1431. if (cgpu->deven == DEV_DISABLED) {
  1432. strcpy(io_buffer, message(MSG_PGALRDIS, id, NULL, isjson));
  1433. return;
  1434. }
  1435. cgpu->deven = DEV_DISABLED;
  1436. strcpy(io_buffer, message(MSG_PGADIS, id, NULL, isjson));
  1437. }
  1438. #endif
  1439. #ifdef WANT_CPUMINE
  1440. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1441. {
  1442. int id;
  1443. if (opt_n_threads == 0) {
  1444. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  1445. return;
  1446. }
  1447. if (param == NULL || *param == '\0') {
  1448. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1449. return;
  1450. }
  1451. id = atoi(param);
  1452. if (id < 0 || id >= num_processors) {
  1453. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  1454. return;
  1455. }
  1456. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  1457. if (isjson) {
  1458. strcat(io_buffer, COMMA);
  1459. strcat(io_buffer, JSON_CPU);
  1460. }
  1461. cpustatus(id, isjson);
  1462. if (isjson)
  1463. strcat(io_buffer, JSON_CLOSE);
  1464. }
  1465. #endif
  1466. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1467. {
  1468. struct api_data *root = NULL;
  1469. char buf[TMPBUFSIZ];
  1470. char *status, *lp;
  1471. int i;
  1472. if (total_pools == 0) {
  1473. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1474. return;
  1475. }
  1476. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  1477. if (isjson) {
  1478. strcat(io_buffer, COMMA);
  1479. strcat(io_buffer, JSON_POOLS);
  1480. }
  1481. for (i = 0; i < total_pools; i++) {
  1482. struct pool *pool = pools[i];
  1483. switch (pool->enabled) {
  1484. case POOL_DISABLED:
  1485. status = (char *)DISABLED;
  1486. break;
  1487. case POOL_REJECTING:
  1488. status = (char *)REJECTING;
  1489. break;
  1490. case POOL_ENABLED:
  1491. if (pool->idle)
  1492. status = (char *)DEAD;
  1493. else
  1494. status = (char *)ALIVE;
  1495. break;
  1496. default:
  1497. status = (char *)UNKNOWN;
  1498. break;
  1499. }
  1500. if (pool->hdr_path)
  1501. lp = (char *)YES;
  1502. else
  1503. lp = (char *)NO;
  1504. root = api_add_int(root, "POOL", &i, false);
  1505. root = api_add_escape(root, "URL", pool->rpc_url, false);
  1506. root = api_add_string(root, "Status", status, false);
  1507. root = api_add_int(root, "Priority", &(pool->prio), false);
  1508. root = api_add_string(root, "Long Poll", lp, false);
  1509. root = api_add_uint(root, "Getworks", &(pool->getwork_requested), false);
  1510. root = api_add_int(root, "Accepted", &(pool->accepted), false);
  1511. root = api_add_int(root, "Rejected", &(pool->rejected), false);
  1512. root = api_add_uint(root, "Discarded", &(pool->discarded_work), false);
  1513. root = api_add_uint(root, "Stale", &(pool->stale_shares), false);
  1514. root = api_add_uint(root, "Get Failures", &(pool->getfail_occasions), false);
  1515. root = api_add_uint(root, "Remote Failures", &(pool->remotefail_occasions), false);
  1516. root = api_add_escape(root, "User", pool->rpc_user, false);
  1517. root = api_add_time(root, "Last Share Time", &(pool->last_share_time), false);
  1518. root = api_add_int(root, "Diff1 Work", &(pool->diff1), false);
  1519. if (pool->rpc_proxy) {
  1520. root = api_add_escape(root, "Proxy", pool->rpc_proxy, false);
  1521. } else {
  1522. root = api_add_const(root, "Proxy", BLANK, false);
  1523. }
  1524. if (isjson && (i > 0))
  1525. strcat(io_buffer, COMMA);
  1526. root = print_data(root, buf, isjson);
  1527. strcat(io_buffer, buf);
  1528. }
  1529. if (isjson)
  1530. strcat(io_buffer, JSON_CLOSE);
  1531. }
  1532. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1533. {
  1534. struct api_data *root = NULL;
  1535. char buf[TMPBUFSIZ];
  1536. double utility, mhs, work_utility;
  1537. #ifdef WANT_CPUMINE
  1538. char *algo = (char *)(algo_names[opt_algo]);
  1539. if (algo == NULL)
  1540. algo = (char *)NULLSTR;
  1541. #endif
  1542. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1543. mhs = total_mhashes_done / total_secs;
  1544. work_utility = total_diff1 / ( total_secs ? total_secs : 1 ) * 60;
  1545. sprintf(io_buffer, isjson
  1546. ? "%s," JSON_SUMMARY
  1547. : "%s" _SUMMARY ",",
  1548. message(MSG_SUMM, 0, NULL, isjson));
  1549. root = api_add_elapsed(root, "Elapsed", &(total_secs), false);
  1550. #ifdef WANT_CPUMINE
  1551. if (opt_n_threads)
  1552. root = api_add_string(root, "Algorithm", algo, false);
  1553. #endif
  1554. root = api_add_mhs(root, "MHS av", &(mhs), false);
  1555. root = api_add_uint(root, "Found Blocks", &(found_blocks), false);
  1556. root = api_add_int(root, "Getworks", &(total_getworks), false);
  1557. root = api_add_int(root, "Accepted", &(total_accepted), false);
  1558. root = api_add_int(root, "Rejected", &(total_rejected), false);
  1559. root = api_add_int(root, "Hardware Errors", &(hw_errors), false);
  1560. root = api_add_utility(root, "Utility", &(utility), false);
  1561. root = api_add_int(root, "Discarded", &(total_discarded), false);
  1562. root = api_add_int(root, "Stale", &(total_stale), false);
  1563. root = api_add_uint(root, "Get Failures", &(total_go), false);
  1564. root = api_add_uint(root, "Local Work", &(local_work), false);
  1565. root = api_add_uint(root, "Remote Failures", &(total_ro), false);
  1566. root = api_add_uint(root, "Network Blocks", &(new_blocks), false);
  1567. root = api_add_mhtotal(root, "Total MH", &(total_mhashes_done), false);
  1568. root = api_add_utility(root, "Work Utility", &(work_utility), false);
  1569. root = print_data(root, buf, isjson);
  1570. if (isjson)
  1571. strcat(buf, JSON_CLOSE);
  1572. strcat(io_buffer, buf);
  1573. }
  1574. #ifdef HAVE_OPENCL
  1575. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1576. {
  1577. struct thr_info *thr;
  1578. int gpu;
  1579. int id;
  1580. int i;
  1581. if (gpu_threads == 0) {
  1582. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1583. return;
  1584. }
  1585. if (param == NULL || *param == '\0') {
  1586. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1587. return;
  1588. }
  1589. id = atoi(param);
  1590. if (id < 0 || id >= nDevs) {
  1591. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1592. return;
  1593. }
  1594. if (gpus[id].deven != DEV_DISABLED) {
  1595. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  1596. return;
  1597. }
  1598. for (i = 0; i < gpu_threads; i++) {
  1599. gpu = thr_info[i].cgpu->device_id;
  1600. if (gpu == id) {
  1601. thr = &thr_info[i];
  1602. if (thr->cgpu->status != LIFE_WELL) {
  1603. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  1604. return;
  1605. }
  1606. gpus[id].deven = DEV_ENABLED;
  1607. tq_push(thr->q, &ping);
  1608. }
  1609. }
  1610. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  1611. }
  1612. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1613. {
  1614. int id;
  1615. if (nDevs == 0) {
  1616. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1617. return;
  1618. }
  1619. if (param == NULL || *param == '\0') {
  1620. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1621. return;
  1622. }
  1623. id = atoi(param);
  1624. if (id < 0 || id >= nDevs) {
  1625. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1626. return;
  1627. }
  1628. if (gpus[id].deven == DEV_DISABLED) {
  1629. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  1630. return;
  1631. }
  1632. gpus[id].deven = DEV_DISABLED;
  1633. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  1634. }
  1635. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1636. {
  1637. int id;
  1638. if (nDevs == 0) {
  1639. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1640. return;
  1641. }
  1642. if (param == NULL || *param == '\0') {
  1643. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1644. return;
  1645. }
  1646. id = atoi(param);
  1647. if (id < 0 || id >= nDevs) {
  1648. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1649. return;
  1650. }
  1651. reinit_device(&gpus[id]);
  1652. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  1653. }
  1654. #endif
  1655. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1656. {
  1657. struct api_data *root = NULL;
  1658. char buf[TMPBUFSIZ];
  1659. int numgpu = 0;
  1660. #ifdef HAVE_OPENCL
  1661. numgpu = nDevs;
  1662. #endif
  1663. sprintf(io_buffer, isjson
  1664. ? "%s," JSON_GPUS
  1665. : "%s" _GPUS ",",
  1666. message(MSG_NUMGPU, 0, NULL, isjson));
  1667. root = api_add_int(root, "Count", &numgpu, false);
  1668. root = print_data(root, buf, isjson);
  1669. if (isjson)
  1670. strcat(buf, JSON_CLOSE);
  1671. strcat(io_buffer, buf);
  1672. }
  1673. static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1674. {
  1675. struct api_data *root = NULL;
  1676. char buf[TMPBUFSIZ];
  1677. int count = 0;
  1678. #ifdef HAVE_AN_FPGA
  1679. count = numpgas();
  1680. #endif
  1681. sprintf(io_buffer, isjson
  1682. ? "%s," JSON_PGAS
  1683. : "%s" _PGAS ",",
  1684. message(MSG_NUMPGA, 0, NULL, isjson));
  1685. root = api_add_int(root, "Count", &count, false);
  1686. root = print_data(root, buf, isjson);
  1687. if (isjson)
  1688. strcat(buf, JSON_CLOSE);
  1689. strcat(io_buffer, buf);
  1690. }
  1691. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1692. {
  1693. struct api_data *root = NULL;
  1694. char buf[TMPBUFSIZ];
  1695. int count = 0;
  1696. #ifdef WANT_CPUMINE
  1697. count = opt_n_threads > 0 ? num_processors : 0;
  1698. #endif
  1699. sprintf(io_buffer, isjson
  1700. ? "%s," JSON_CPUS
  1701. : "%s" _CPUS ",",
  1702. message(MSG_NUMCPU, 0, NULL, isjson));
  1703. root = api_add_int(root, "Count", &count, false);
  1704. root = print_data(root, buf, isjson);
  1705. if (isjson)
  1706. strcat(buf, JSON_CLOSE);
  1707. strcat(io_buffer, buf);
  1708. }
  1709. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1710. {
  1711. struct pool *pool;
  1712. int id;
  1713. if (total_pools == 0) {
  1714. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1715. return;
  1716. }
  1717. if (param == NULL || *param == '\0') {
  1718. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1719. return;
  1720. }
  1721. id = atoi(param);
  1722. if (id < 0 || id >= total_pools) {
  1723. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1724. return;
  1725. }
  1726. pool = pools[id];
  1727. pool->enabled = POOL_ENABLED;
  1728. switch_pools(pool);
  1729. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  1730. }
  1731. static void copyadvanceafter(char ch, char **param, char **buf)
  1732. {
  1733. #define src_p (*param)
  1734. #define dst_b (*buf)
  1735. while (*src_p && *src_p != ch) {
  1736. if (*src_p == '\\' && *(src_p+1) != '\0')
  1737. src_p++;
  1738. *(dst_b++) = *(src_p++);
  1739. }
  1740. if (*src_p)
  1741. src_p++;
  1742. *(dst_b++) = '\0';
  1743. }
  1744. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1745. {
  1746. char *ptr, *buf;
  1747. ptr = buf = malloc(strlen(param)+1);
  1748. if (unlikely(!buf))
  1749. quit(1, "Failed to malloc pooldetails buf");
  1750. *url = buf;
  1751. // copy url
  1752. copyadvanceafter(',', &param, &buf);
  1753. if (!(*param)) // missing user
  1754. goto exitsama;
  1755. *user = buf;
  1756. // copy user
  1757. copyadvanceafter(',', &param, &buf);
  1758. if (!*param) // missing pass
  1759. goto exitsama;
  1760. *pass = buf;
  1761. // copy pass
  1762. copyadvanceafter(',', &param, &buf);
  1763. return true;
  1764. exitsama:
  1765. free(ptr);
  1766. return false;
  1767. }
  1768. static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1769. {
  1770. char *url, *user, *pass;
  1771. char *ptr;
  1772. if (param == NULL || *param == '\0') {
  1773. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  1774. return;
  1775. }
  1776. if (!pooldetails(param, &url, &user, &pass)) {
  1777. ptr = escape_string(param, isjson);
  1778. strcpy(io_buffer, message(MSG_INVPDP, 0, ptr, isjson));
  1779. if (ptr != param)
  1780. free(ptr);
  1781. ptr = NULL;
  1782. return;
  1783. }
  1784. add_pool_details(true, url, user, pass);
  1785. ptr = escape_string(url, isjson);
  1786. strcpy(io_buffer, message(MSG_ADDPOOL, 0, ptr, isjson));
  1787. if (ptr != url)
  1788. free(ptr);
  1789. ptr = NULL;
  1790. }
  1791. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1792. {
  1793. struct pool *pool;
  1794. int id;
  1795. if (total_pools == 0) {
  1796. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1797. return;
  1798. }
  1799. if (param == NULL || *param == '\0') {
  1800. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1801. return;
  1802. }
  1803. id = atoi(param);
  1804. if (id < 0 || id >= total_pools) {
  1805. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1806. return;
  1807. }
  1808. pool = pools[id];
  1809. if (pool->enabled == POOL_ENABLED) {
  1810. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  1811. return;
  1812. }
  1813. pool->enabled = POOL_ENABLED;
  1814. if (pool->prio < current_pool()->prio)
  1815. switch_pools(pool);
  1816. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  1817. }
  1818. static void poolpriority(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1819. {
  1820. char *ptr, *next;
  1821. int i, pr, prio = 0;
  1822. // TODO: all cgminer code needs a mutex added everywhere for change
  1823. // access to total_pools and also parts of the pools[] array,
  1824. // just copying total_pools here wont solve that
  1825. if (total_pools == 0) {
  1826. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1827. return;
  1828. }
  1829. if (param == NULL || *param == '\0') {
  1830. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1831. return;
  1832. }
  1833. bool pools_changed[total_pools];
  1834. int new_prio[total_pools];
  1835. for (i = 0; i < total_pools; ++i)
  1836. pools_changed[i] = false;
  1837. next = param;
  1838. while (next && *next) {
  1839. ptr = next;
  1840. next = strchr(ptr, ',');
  1841. if (next)
  1842. *(next++) = '\0';
  1843. i = atoi(ptr);
  1844. if (i < 0 || i >= total_pools) {
  1845. strcpy(io_buffer, message(MSG_INVPID, i, NULL, isjson));
  1846. return;
  1847. }
  1848. if (pools_changed[i]) {
  1849. strcpy(io_buffer, message(MSG_DUPPID, i, NULL, isjson));
  1850. return;
  1851. }
  1852. pools_changed[i] = true;
  1853. new_prio[i] = prio++;
  1854. }
  1855. // Only change them if no errors
  1856. for (i = 0; i < total_pools; i++) {
  1857. if (pools_changed[i])
  1858. pools[i]->prio = new_prio[i];
  1859. }
  1860. // In priority order, cycle through the unchanged pools and append them
  1861. for (pr = 0; pr < total_pools; pr++)
  1862. for (i = 0; i < total_pools; i++) {
  1863. if (!pools_changed[i] && pools[i]->prio == pr) {
  1864. pools[i]->prio = prio++;
  1865. pools_changed[i] = true;
  1866. break;
  1867. }
  1868. }
  1869. if (current_pool()->prio)
  1870. switch_pools(NULL);
  1871. strcpy(io_buffer, message(MSG_POOLPRIO, 0, NULL, isjson));
  1872. }
  1873. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1874. {
  1875. struct pool *pool;
  1876. int id;
  1877. if (total_pools == 0) {
  1878. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1879. return;
  1880. }
  1881. if (param == NULL || *param == '\0') {
  1882. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1883. return;
  1884. }
  1885. id = atoi(param);
  1886. if (id < 0 || id >= total_pools) {
  1887. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1888. return;
  1889. }
  1890. pool = pools[id];
  1891. if (pool->enabled == POOL_DISABLED) {
  1892. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  1893. return;
  1894. }
  1895. if (enabled_pools <= 1) {
  1896. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  1897. return;
  1898. }
  1899. pool->enabled = POOL_DISABLED;
  1900. if (pool == current_pool())
  1901. switch_pools(NULL);
  1902. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  1903. }
  1904. static void removepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1905. {
  1906. struct pool *pool;
  1907. char *rpc_url;
  1908. bool dofree = false;
  1909. int id;
  1910. if (total_pools == 0) {
  1911. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1912. return;
  1913. }
  1914. if (param == NULL || *param == '\0') {
  1915. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1916. return;
  1917. }
  1918. id = atoi(param);
  1919. if (id < 0 || id >= total_pools) {
  1920. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1921. return;
  1922. }
  1923. if (total_pools <= 1) {
  1924. strcpy(io_buffer, message(MSG_REMLASTP, id, NULL, isjson));
  1925. return;
  1926. }
  1927. pool = pools[id];
  1928. if (pool == current_pool())
  1929. switch_pools(NULL);
  1930. if (pool == current_pool()) {
  1931. strcpy(io_buffer, message(MSG_ACTPOOL, id, NULL, isjson));
  1932. return;
  1933. }
  1934. pool->enabled = POOL_DISABLED;
  1935. rpc_url = escape_string(pool->rpc_url, isjson);
  1936. if (rpc_url != pool->rpc_url)
  1937. dofree = true;
  1938. remove_pool(pool);
  1939. strcpy(io_buffer, message(MSG_REMPOOL, id, rpc_url, isjson));
  1940. if (dofree)
  1941. free(rpc_url);
  1942. rpc_url = NULL;
  1943. }
  1944. #ifdef HAVE_OPENCL
  1945. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  1946. {
  1947. int id;
  1948. char *gpusep;
  1949. if (nDevs == 0) {
  1950. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1951. return false;
  1952. }
  1953. if (param == NULL || *param == '\0') {
  1954. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1955. return false;
  1956. }
  1957. gpusep = strchr(param, GPUSEP);
  1958. if (gpusep == NULL) {
  1959. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  1960. return false;
  1961. }
  1962. *(gpusep++) = '\0';
  1963. id = atoi(param);
  1964. if (id < 0 || id >= nDevs) {
  1965. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1966. return false;
  1967. }
  1968. *gpu = id;
  1969. *value = gpusep;
  1970. return true;
  1971. }
  1972. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  1973. {
  1974. int id;
  1975. char *value;
  1976. int intensity;
  1977. char intensitystr[7];
  1978. if (!splitgpuvalue(param, &id, &value, isjson))
  1979. return;
  1980. if (!strncasecmp(value, DYNAMIC, 1)) {
  1981. gpus[id].dynamic = true;
  1982. strcpy(intensitystr, DYNAMIC);
  1983. }
  1984. else {
  1985. intensity = atoi(value);
  1986. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1987. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1988. return;
  1989. }
  1990. gpus[id].dynamic = false;
  1991. gpus[id].intensity = intensity;
  1992. sprintf(intensitystr, "%d", intensity);
  1993. }
  1994. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1995. }
  1996. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  1997. {
  1998. #ifdef HAVE_ADL
  1999. int id;
  2000. char *value;
  2001. int clock;
  2002. if (!splitgpuvalue(param, &id, &value, isjson))
  2003. return;
  2004. clock = atoi(value);
  2005. if (set_memoryclock(id, clock))
  2006. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  2007. else
  2008. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  2009. #else
  2010. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  2011. #endif
  2012. }
  2013. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2014. {
  2015. #ifdef HAVE_ADL
  2016. int id;
  2017. char *value;
  2018. int clock;
  2019. if (!splitgpuvalue(param, &id, &value, isjson))
  2020. return;
  2021. clock = atoi(value);
  2022. if (set_engineclock(id, clock))
  2023. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  2024. else
  2025. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  2026. #else
  2027. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  2028. #endif
  2029. }
  2030. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2031. {
  2032. #ifdef HAVE_ADL
  2033. int id;
  2034. char *value;
  2035. int fan;
  2036. if (!splitgpuvalue(param, &id, &value, isjson))
  2037. return;
  2038. fan = atoi(value);
  2039. if (set_fanspeed(id, fan))
  2040. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  2041. else
  2042. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  2043. #else
  2044. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  2045. #endif
  2046. }
  2047. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2048. {
  2049. #ifdef HAVE_ADL
  2050. int id;
  2051. char *value;
  2052. float vddc;
  2053. if (!splitgpuvalue(param, &id, &value, isjson))
  2054. return;
  2055. vddc = atof(value);
  2056. if (set_vddc(id, vddc))
  2057. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  2058. else
  2059. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  2060. #else
  2061. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  2062. #endif
  2063. }
  2064. #endif
  2065. void doquit(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2066. {
  2067. if (isjson)
  2068. strcpy(io_buffer, JSON_START JSON_BYE);
  2069. else
  2070. strcpy(io_buffer, _BYE);
  2071. bye = true;
  2072. do_a_quit = true;
  2073. }
  2074. void dorestart(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2075. {
  2076. if (isjson)
  2077. strcpy(io_buffer, JSON_START JSON_RESTART);
  2078. else
  2079. strcpy(io_buffer, _RESTART);
  2080. bye = true;
  2081. do_a_restart = true;
  2082. }
  2083. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2084. {
  2085. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  2086. }
  2087. void notifystatus(int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
  2088. {
  2089. struct api_data *root = NULL;
  2090. char buf[TMPBUFSIZ];
  2091. char *reason;
  2092. if (cgpu->device_last_not_well == 0)
  2093. reason = REASON_NONE;
  2094. else
  2095. switch(cgpu->device_not_well_reason) {
  2096. case REASON_THREAD_FAIL_INIT:
  2097. reason = REASON_THREAD_FAIL_INIT_STR;
  2098. break;
  2099. case REASON_THREAD_ZERO_HASH:
  2100. reason = REASON_THREAD_ZERO_HASH_STR;
  2101. break;
  2102. case REASON_THREAD_FAIL_QUEUE:
  2103. reason = REASON_THREAD_FAIL_QUEUE_STR;
  2104. break;
  2105. case REASON_DEV_SICK_IDLE_60:
  2106. reason = REASON_DEV_SICK_IDLE_60_STR;
  2107. break;
  2108. case REASON_DEV_DEAD_IDLE_600:
  2109. reason = REASON_DEV_DEAD_IDLE_600_STR;
  2110. break;
  2111. case REASON_DEV_NOSTART:
  2112. reason = REASON_DEV_NOSTART_STR;
  2113. break;
  2114. case REASON_DEV_OVER_HEAT:
  2115. reason = REASON_DEV_OVER_HEAT_STR;
  2116. break;
  2117. case REASON_DEV_THERMAL_CUTOFF:
  2118. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  2119. break;
  2120. case REASON_DEV_COMMS_ERROR:
  2121. reason = REASON_DEV_COMMS_ERROR_STR;
  2122. break;
  2123. default:
  2124. reason = REASON_UNKNOWN_STR;
  2125. break;
  2126. }
  2127. // ALL counters (and only counters) must start the name with a '*'
  2128. // Simplifies future external support for identifying new counters
  2129. root = api_add_int(root, "NOTIFY", &device, false);
  2130. root = api_add_string(root, "Name", cgpu->api->name, false);
  2131. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  2132. root = api_add_time(root, "Last Well", &(cgpu->device_last_well), false);
  2133. root = api_add_time(root, "Last Not Well", &(cgpu->device_last_not_well), false);
  2134. root = api_add_string(root, "Reason Not Well", reason, false);
  2135. root = api_add_int(root, "*Thread Fail Init", &(cgpu->thread_fail_init_count), false);
  2136. root = api_add_int(root, "*Thread Zero Hash", &(cgpu->thread_zero_hash_count), false);
  2137. root = api_add_int(root, "*Thread Fail Queue", &(cgpu->thread_fail_queue_count), false);
  2138. root = api_add_int(root, "*Dev Sick Idle 60s", &(cgpu->dev_sick_idle_60_count), false);
  2139. root = api_add_int(root, "*Dev Dead Idle 600s", &(cgpu->dev_dead_idle_600_count), false);
  2140. root = api_add_int(root, "*Dev Nostart", &(cgpu->dev_nostart_count), false);
  2141. root = api_add_int(root, "*Dev Over Heat", &(cgpu->dev_over_heat_count), false);
  2142. root = api_add_int(root, "*Dev Thermal Cutoff", &(cgpu->dev_thermal_cutoff_count), false);
  2143. root = api_add_int(root, "*Dev Comms Error", &(cgpu->dev_comms_error_count), false);
  2144. if (isjson && (device > 0))
  2145. strcat(io_buffer, COMMA);
  2146. root = print_data(root, buf, isjson);
  2147. strcat(io_buffer, buf);
  2148. }
  2149. static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
  2150. {
  2151. int i;
  2152. if (total_devices == 0) {
  2153. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  2154. return;
  2155. }
  2156. strcpy(io_buffer, message(MSG_NOTIFY, 0, NULL, isjson));
  2157. if (isjson) {
  2158. strcat(io_buffer, COMMA);
  2159. strcat(io_buffer, JSON_NOTIFY);
  2160. }
  2161. for (i = 0; i < total_devices; i++)
  2162. notifystatus(i, devices[i], isjson, group);
  2163. if (isjson)
  2164. strcat(io_buffer, JSON_CLOSE);
  2165. }
  2166. static void devdetails(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2167. {
  2168. struct api_data *root = NULL;
  2169. char buf[TMPBUFSIZ];
  2170. struct cgpu_info *cgpu;
  2171. int i;
  2172. if (total_devices == 0) {
  2173. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  2174. return;
  2175. }
  2176. strcpy(io_buffer, message(MSG_DEVDETAILS, 0, NULL, isjson));
  2177. if (isjson) {
  2178. strcat(io_buffer, COMMA);
  2179. strcat(io_buffer, JSON_DEVDETAILS);
  2180. }
  2181. for (i = 0; i < total_devices; i++) {
  2182. cgpu = devices[i];
  2183. root = api_add_int(root, "DEVDETAILS", &i, false);
  2184. root = api_add_string(root, "Name", cgpu->api->name, false);
  2185. root = api_add_int(root, "ID", &(cgpu->device_id), false);
  2186. root = api_add_string(root, "Driver", cgpu->api->dname, false);
  2187. root = api_add_const(root, "Kernel", cgpu->kname ? : BLANK, false);
  2188. root = api_add_const(root, "Model", cgpu->name ? : BLANK, false);
  2189. root = api_add_const(root, "Device Path", cgpu->device_path ? : BLANK, false);
  2190. if (isjson && (i > 0))
  2191. strcat(io_buffer, COMMA);
  2192. root = print_data(root, buf, isjson);
  2193. strcat(io_buffer, buf);
  2194. }
  2195. if (isjson)
  2196. strcat(io_buffer, JSON_CLOSE);
  2197. }
  2198. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2199. {
  2200. char filename[PATH_MAX];
  2201. FILE *fcfg;
  2202. char *ptr;
  2203. if (param == NULL || *param == '\0') {
  2204. default_save_file(filename);
  2205. param = filename;
  2206. }
  2207. fcfg = fopen(param, "w");
  2208. if (!fcfg) {
  2209. ptr = escape_string(param, isjson);
  2210. strcpy(io_buffer, message(MSG_BADFN, 0, ptr, isjson));
  2211. if (ptr != param)
  2212. free(ptr);
  2213. ptr = NULL;
  2214. return;
  2215. }
  2216. write_config(fcfg);
  2217. fclose(fcfg);
  2218. ptr = escape_string(param, isjson);
  2219. strcpy(io_buffer, message(MSG_SAVED, 0, ptr, isjson));
  2220. if (ptr != param)
  2221. free(ptr);
  2222. ptr = NULL;
  2223. }
  2224. static int itemstats(int i, char *id, struct cgminer_stats *stats, struct cgminer_pool_stats *pool_stats, struct api_data *extra, bool isjson)
  2225. {
  2226. struct api_data *root = NULL;
  2227. char buf[TMPBUFSIZ];
  2228. root = api_add_int(root, "STATS", &i, false);
  2229. root = api_add_string(root, "ID", id, false);
  2230. root = api_add_elapsed(root, "Elapsed", &(total_secs), false);
  2231. root = api_add_uint32(root, "Calls", &(stats->getwork_calls), false);
  2232. root = api_add_timeval(root, "Wait", &(stats->getwork_wait), false);
  2233. root = api_add_timeval(root, "Max", &(stats->getwork_wait_max), false);
  2234. root = api_add_timeval(root, "Min", &(stats->getwork_wait_min), false);
  2235. if (pool_stats) {
  2236. root = api_add_uint32(root, "Pool Calls", &(pool_stats->getwork_calls), false);
  2237. root = api_add_uint32(root, "Pool Attempts", &(pool_stats->getwork_attempts), false);
  2238. root = api_add_timeval(root, "Pool Wait", &(pool_stats->getwork_wait), false);
  2239. root = api_add_timeval(root, "Pool Max", &(pool_stats->getwork_wait_max), false);
  2240. root = api_add_timeval(root, "Pool Min", &(pool_stats->getwork_wait_min), false);
  2241. root = api_add_double(root, "Pool Av", &(pool_stats->getwork_wait_rolling), false);
  2242. root = api_add_bool(root, "Work Had Roll Time", &(pool_stats->hadrolltime), false);
  2243. root = api_add_bool(root, "Work Can Roll", &(pool_stats->canroll), false);
  2244. root = api_add_bool(root, "Work Had Expire", &(pool_stats->hadexpire), false);
  2245. root = api_add_uint32(root, "Work Roll Time", &(pool_stats->rolltime), false);
  2246. }
  2247. if (extra)
  2248. root = api_add_extra(root, extra);
  2249. if (isjson && (i > 0))
  2250. strcat(io_buffer, COMMA);
  2251. root = print_data(root, buf, isjson);
  2252. strcat(io_buffer, buf);
  2253. return ++i;
  2254. }
  2255. static void minerstats(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2256. {
  2257. struct api_data *extra;
  2258. char id[20];
  2259. int i, j;
  2260. strcpy(io_buffer, message(MSG_MINESTATS, 0, NULL, isjson));
  2261. if (isjson) {
  2262. strcat(io_buffer, COMMA);
  2263. strcat(io_buffer, JSON_MINESTATS);
  2264. }
  2265. i = 0;
  2266. for (j = 0; j < total_devices; j++) {
  2267. struct cgpu_info *cgpu = devices[j];
  2268. if (cgpu && cgpu->api) {
  2269. if (cgpu->api->get_api_stats)
  2270. extra = cgpu->api->get_api_stats(cgpu);
  2271. else
  2272. extra = NULL;
  2273. sprintf(id, "%s%d", cgpu->api->name, cgpu->device_id);
  2274. i = itemstats(i, id, &(cgpu->cgminer_stats), NULL, extra, isjson);
  2275. }
  2276. }
  2277. for (j = 0; j < total_pools; j++) {
  2278. struct pool *pool = pools[j];
  2279. sprintf(id, "POOL%d", j);
  2280. i = itemstats(i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, isjson);
  2281. }
  2282. if (isjson)
  2283. strcat(io_buffer, JSON_CLOSE);
  2284. }
  2285. static void failoveronly(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2286. {
  2287. if (param == NULL || *param == '\0') {
  2288. strcpy(io_buffer, message(MSG_MISBOOL, 0, NULL, isjson));
  2289. return;
  2290. }
  2291. *param = tolower(*param);
  2292. if (*param != 't' && *param != 'f') {
  2293. strcpy(io_buffer, message(MSG_INVBOOL, 0, NULL, isjson));
  2294. return;
  2295. }
  2296. bool tf = (*param == 't');
  2297. opt_fail_only = tf;
  2298. strcpy(io_buffer, message(MSG_FOO, tf, NULL, isjson));
  2299. }
  2300. static void minecoin(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
  2301. {
  2302. struct api_data *root = NULL;
  2303. char buf[TMPBUFSIZ];
  2304. sprintf(io_buffer, isjson
  2305. ? "%s," JSON_MINECOIN
  2306. : "%s" _MINECOIN ",",
  2307. message(MSG_MINECOIN, 0, NULL, isjson));
  2308. #ifdef USE_SCRYPT
  2309. if (opt_scrypt)
  2310. root = api_add_const(root, "Hash Method", SCRYPTSTR, false);
  2311. else
  2312. #endif
  2313. root = api_add_const(root, "Hash Method", SHA256STR, false);
  2314. mutex_lock(&ch_lock);
  2315. if (current_fullhash && *current_fullhash) {
  2316. root = api_add_timeval(root, "Current Block Time", &block_timeval, true);
  2317. root = api_add_string(root, "Current Block Hash", current_fullhash, true);
  2318. } else {
  2319. struct timeval t = {0,0};
  2320. root = api_add_timeval(root, "Current Block Time", &t, true);
  2321. root = api_add_const(root, "Current Block Hash", BLANK, false);
  2322. }
  2323. mutex_unlock(&ch_lock);
  2324. root = api_add_bool(root, "LP", &have_longpoll, false);
  2325. root = print_data(root, buf, isjson);
  2326. if (isjson)
  2327. strcat(buf, JSON_CLOSE);
  2328. strcat(io_buffer, buf);
  2329. }
  2330. static void debugstate(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2331. {
  2332. struct api_data *root = NULL;
  2333. char buf[TMPBUFSIZ];
  2334. if (param == NULL)
  2335. param = (char *)BLANK;
  2336. else
  2337. *param = tolower(*param);
  2338. switch(*param) {
  2339. case 's':
  2340. opt_realquiet = true;
  2341. break;
  2342. case 'q':
  2343. opt_quiet ^= true;
  2344. break;
  2345. case 'v':
  2346. opt_log_output ^= true;
  2347. if (opt_log_output)
  2348. opt_quiet = false;
  2349. break;
  2350. case 'd':
  2351. opt_debug ^= true;
  2352. opt_log_output = opt_debug;
  2353. if (opt_debug)
  2354. opt_quiet = false;
  2355. break;
  2356. case 'r':
  2357. opt_protocol ^= true;
  2358. if (opt_protocol)
  2359. opt_quiet = false;
  2360. break;
  2361. case 'p':
  2362. want_per_device_stats ^= true;
  2363. opt_log_output = want_per_device_stats;
  2364. break;
  2365. case 'n':
  2366. opt_log_output = false;
  2367. opt_debug = false;
  2368. opt_quiet = false;
  2369. opt_protocol = false;
  2370. want_per_device_stats = false;
  2371. break;
  2372. default:
  2373. // anything else just reports the settings
  2374. break;
  2375. }
  2376. sprintf(io_buffer, isjson
  2377. ? "%s," JSON_DEBUGSET
  2378. : "%s" _DEBUGSET ",",
  2379. message(MSG_DEBUGSET, 0, NULL, isjson));
  2380. root = api_add_bool(root, "Silent", &opt_realquiet, false);
  2381. root = api_add_bool(root, "Quiet", &opt_quiet, false);
  2382. root = api_add_bool(root, "Verbose", &opt_log_output, false);
  2383. root = api_add_bool(root, "Debug", &opt_debug, false);
  2384. root = api_add_bool(root, "RPCProto", &opt_protocol, false);
  2385. root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
  2386. root = print_data(root, buf, isjson);
  2387. if (isjson)
  2388. strcat(buf, JSON_CLOSE);
  2389. strcat(io_buffer, buf);
  2390. }
  2391. static void setconfig(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
  2392. {
  2393. char *comma;
  2394. int value;
  2395. if (param == NULL || *param == '\0') {
  2396. strcpy(io_buffer, message(MSG_CONPAR, 0, NULL, isjson));
  2397. return;
  2398. }
  2399. comma = strchr(param, ',');
  2400. if (!comma) {
  2401. strcpy(io_buffer, message(MSG_CONVAL, 0, param, isjson));
  2402. return;
  2403. }
  2404. *(comma++) = '\0';
  2405. value = atoi(comma);
  2406. if (value < 0 || value > 9999) {
  2407. strcpy(io_buffer, message(MSG_INVNUM, value, param, isjson));
  2408. return;
  2409. }
  2410. if (strcasecmp(param, "queue") == 0)
  2411. opt_queue = value;
  2412. else if (strcasecmp(param, "scantime") == 0)
  2413. opt_scantime = value;
  2414. else if (strcasecmp(param, "expiry") == 0)
  2415. opt_expiry = value;
  2416. else {
  2417. strcpy(io_buffer, message(MSG_UNKCON, 0, param, isjson));
  2418. return;
  2419. }
  2420. strcpy(io_buffer, message(MSG_SETCONFIG, value, param, isjson));
  2421. }
  2422. static void checkcommand(__maybe_unused SOCKETTYPE c, char *param, bool isjson, char group);
  2423. struct CMDS {
  2424. char *name;
  2425. void (*func)(SOCKETTYPE, char *, bool, char);
  2426. bool iswritemode;
  2427. } cmds[] = {
  2428. { "version", apiversion, false },
  2429. { "config", minerconfig, false },
  2430. { "devs", devstatus, false },
  2431. { "devdetail", devdetail, false },
  2432. { "pools", poolstatus, false },
  2433. { "summary", summary, false },
  2434. #ifdef HAVE_OPENCL
  2435. { "gpuenable", gpuenable, true },
  2436. { "gpudisable", gpudisable, true },
  2437. { "gpurestart", gpurestart, true },
  2438. { "gpu", gpudev, false },
  2439. #endif
  2440. #ifdef HAVE_AN_FPGA
  2441. { "pga", pgadev, false },
  2442. { "pgaenable", pgaenable, true },
  2443. { "pgadisable", pgadisable, true },
  2444. #endif
  2445. #ifdef WANT_CPUMINE
  2446. { "cpu", cpudev, false },
  2447. #endif
  2448. { "gpucount", gpucount, false },
  2449. { "pgacount", pgacount, false },
  2450. { "cpucount", cpucount, false },
  2451. { "switchpool", switchpool, true },
  2452. { "addpool", addpool, true },
  2453. { "poolpriority", poolpriority, true },
  2454. { "enablepool", enablepool, true },
  2455. { "disablepool", disablepool, true },
  2456. { "removepool", removepool, true },
  2457. #ifdef HAVE_OPENCL
  2458. { "gpuintensity", gpuintensity, true },
  2459. { "gpumem", gpumem, true },
  2460. { "gpuengine", gpuengine, true },
  2461. { "gpufan", gpufan, true },
  2462. { "gpuvddc", gpuvddc, true },
  2463. #endif
  2464. { "save", dosave, true },
  2465. { "quit", doquit, true },
  2466. { "privileged", privileged, true },
  2467. { "notify", notify, false },
  2468. { "devdetails", devdetails, false },
  2469. { "restart", dorestart, true },
  2470. { "stats", minerstats, false },
  2471. { "check", checkcommand, false },
  2472. { "failover-only", failoveronly, true },
  2473. { "coin", minecoin, false },
  2474. { "debug", debugstate, true },
  2475. { "setconfig", setconfig, true },
  2476. { NULL, NULL, false }
  2477. };
  2478. static void checkcommand(__maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
  2479. {
  2480. struct api_data *root = NULL;
  2481. char buf[TMPBUFSIZ];
  2482. char cmdbuf[100];
  2483. bool found, access;
  2484. int i;
  2485. if (param == NULL || *param == '\0') {
  2486. strcpy(io_buffer, message(MSG_MISCHK, 0, NULL, isjson));
  2487. return;
  2488. }
  2489. found = false;
  2490. access = false;
  2491. for (i = 0; cmds[i].name != NULL; i++) {
  2492. if (strcmp(cmds[i].name, param) == 0) {
  2493. found = true;
  2494. sprintf(cmdbuf, "|%s|", param);
  2495. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  2496. access = true;
  2497. break;
  2498. }
  2499. }
  2500. sprintf(io_buffer, isjson
  2501. ? "%s," JSON_CHECK
  2502. : "%s" _CHECK ",",
  2503. message(MSG_CHECK, 0, NULL, isjson));
  2504. root = api_add_const(root, "Exists", found ? YES : NO, false);
  2505. root = api_add_const(root, "Access", access ? YES : NO, false);
  2506. root = print_data(root, buf, isjson);
  2507. if (isjson)
  2508. strcat(buf, JSON_CLOSE);
  2509. strcat(io_buffer, buf);
  2510. }
  2511. static void send_result(SOCKETTYPE c, bool isjson)
  2512. {
  2513. int n;
  2514. int len;
  2515. if (isjson)
  2516. strcat(io_buffer, JSON_END);
  2517. len = strlen(io_buffer);
  2518. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : BLANK);
  2519. // ignore failure - it's closed immediately anyway
  2520. n = send(c, io_buffer, len+1, 0);
  2521. if (opt_debug) {
  2522. if (SOCKETFAIL(n))
  2523. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  2524. else
  2525. applog(LOG_DEBUG, "API: sent %d", n);
  2526. }
  2527. }
  2528. static void tidyup(__maybe_unused void *arg)
  2529. {
  2530. mutex_lock(&quit_restart_lock);
  2531. bye = true;
  2532. if (sock != INVSOCK) {
  2533. shutdown(sock, SHUT_RDWR);
  2534. CLOSESOCKET(sock);
  2535. sock = INVSOCK;
  2536. }
  2537. if (ipaccess != NULL) {
  2538. free(ipaccess);
  2539. ipaccess = NULL;
  2540. }
  2541. if (msg_buffer != NULL) {
  2542. free(msg_buffer);
  2543. msg_buffer = NULL;
  2544. }
  2545. if (io_buffer != NULL) {
  2546. free(io_buffer);
  2547. io_buffer = NULL;
  2548. }
  2549. mutex_unlock(&quit_restart_lock);
  2550. }
  2551. /*
  2552. * Interpret --api-groups G:cmd1:cmd2:cmd3,P:cmd4,*,...
  2553. */
  2554. static void setup_groups()
  2555. {
  2556. char *api_groups = opt_api_groups ? opt_api_groups : (char *)BLANK;
  2557. char *buf, *ptr, *next, *colon;
  2558. char group;
  2559. char commands[TMPBUFSIZ];
  2560. char cmdbuf[100];
  2561. char *cmd;
  2562. bool addstar, did;
  2563. int i;
  2564. buf = malloc(strlen(api_groups) + 1);
  2565. if (unlikely(!buf))
  2566. quit(1, "Failed to malloc ipgroups buf");
  2567. strcpy(buf, api_groups);
  2568. next = buf;
  2569. // for each group defined
  2570. while (next && *next) {
  2571. ptr = next;
  2572. next = strchr(ptr, ',');
  2573. if (next)
  2574. *(next++) = '\0';
  2575. // Validate the group
  2576. if (*(ptr+1) != ':') {
  2577. colon = strchr(ptr, ':');
  2578. if (colon)
  2579. *colon = '\0';
  2580. applog(LOG_WARNING, "API invalid group name '%s'", ptr);
  2581. quit(1, INVAPIGROUPS);
  2582. }
  2583. group = GROUP(*ptr);
  2584. if (!VALIDGROUP(group)) {
  2585. applog(LOG_WARNING, "API invalid group name '%c'", *ptr);
  2586. quit(1, INVAPIGROUPS);
  2587. }
  2588. if (group == PRIVGROUP) {
  2589. applog(LOG_WARNING, "API group name can't be '%c'", PRIVGROUP);
  2590. quit(1, INVAPIGROUPS);
  2591. }
  2592. if (group == NOPRIVGROUP) {
  2593. applog(LOG_WARNING, "API group name can't be '%c'", NOPRIVGROUP);
  2594. quit(1, INVAPIGROUPS);
  2595. }
  2596. if (apigroups[GROUPOFFSET(group)].commands != NULL) {
  2597. applog(LOG_WARNING, "API duplicate group name '%c'", *ptr);
  2598. quit(1, INVAPIGROUPS);
  2599. }
  2600. ptr += 2;
  2601. // Validate the command list (and handle '*')
  2602. cmd = &(commands[0]);
  2603. *(cmd++) = SEPARATOR;
  2604. *cmd = '\0';
  2605. addstar = false;
  2606. while (ptr && *ptr) {
  2607. colon = strchr(ptr, ':');
  2608. if (colon)
  2609. *(colon++) = '\0';
  2610. if (strcmp(ptr, "*") == 0)
  2611. addstar = true;
  2612. else {
  2613. did = false;
  2614. for (i = 0; cmds[i].name != NULL; i++) {
  2615. if (strcasecmp(ptr, cmds[i].name) == 0) {
  2616. did = true;
  2617. break;
  2618. }
  2619. }
  2620. if (did) {
  2621. // skip duplicates
  2622. sprintf(cmdbuf, "|%s|", cmds[i].name);
  2623. if (strstr(commands, cmdbuf) == NULL) {
  2624. strcpy(cmd, cmds[i].name);
  2625. cmd += strlen(cmds[i].name);
  2626. *(cmd++) = SEPARATOR;
  2627. *cmd = '\0';
  2628. }
  2629. } else {
  2630. applog(LOG_WARNING, "API unknown command '%s' in group '%c'", ptr, group);
  2631. quit(1, INVAPIGROUPS);
  2632. }
  2633. }
  2634. ptr = colon;
  2635. }
  2636. // * = allow all non-iswritemode commands
  2637. if (addstar) {
  2638. for (i = 0; cmds[i].name != NULL; i++) {
  2639. if (cmds[i].iswritemode == false) {
  2640. // skip duplicates
  2641. sprintf(cmdbuf, "|%s|", cmds[i].name);
  2642. if (strstr(commands, cmdbuf) == NULL) {
  2643. strcpy(cmd, cmds[i].name);
  2644. cmd += strlen(cmds[i].name);
  2645. *(cmd++) = SEPARATOR;
  2646. *cmd = '\0';
  2647. }
  2648. }
  2649. }
  2650. }
  2651. ptr = apigroups[GROUPOFFSET(group)].commands = malloc(strlen(commands) + 1);
  2652. if (unlikely(!ptr))
  2653. quit(1, "Failed to malloc group commands buf");
  2654. strcpy(ptr, commands);
  2655. }
  2656. // Now define R (NOPRIVGROUP) as all non-iswritemode commands
  2657. cmd = &(commands[0]);
  2658. *(cmd++) = SEPARATOR;
  2659. *cmd = '\0';
  2660. for (i = 0; cmds[i].name != NULL; i++) {
  2661. if (cmds[i].iswritemode == false) {
  2662. strcpy(cmd, cmds[i].name);
  2663. cmd += strlen(cmds[i].name);
  2664. *(cmd++) = SEPARATOR;
  2665. *cmd = '\0';
  2666. }
  2667. }
  2668. ptr = apigroups[GROUPOFFSET(NOPRIVGROUP)].commands = malloc(strlen(commands) + 1);
  2669. if (unlikely(!ptr))
  2670. quit(1, "Failed to malloc noprivgroup commands buf");
  2671. strcpy(ptr, commands);
  2672. // W (PRIVGROUP) is handled as a special case since it simply means all commands
  2673. free(buf);
  2674. return;
  2675. }
  2676. /*
  2677. * Interpret [W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  2678. * special case of 0/0 allows /0 (means all IP addresses)
  2679. */
  2680. #define ALLIP4 "0/0"
  2681. /*
  2682. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  2683. */
  2684. static void setup_ipaccess()
  2685. {
  2686. char *buf, *ptr, *comma, *slash, *dot;
  2687. int ipcount, mask, octet, i;
  2688. char group;
  2689. buf = malloc(strlen(opt_api_allow) + 1);
  2690. if (unlikely(!buf))
  2691. quit(1, "Failed to malloc ipaccess buf");
  2692. strcpy(buf, opt_api_allow);
  2693. ipcount = 1;
  2694. ptr = buf;
  2695. while (*ptr)
  2696. if (*(ptr++) == ',')
  2697. ipcount++;
  2698. // possibly more than needed, but never less
  2699. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  2700. if (unlikely(!ipaccess))
  2701. quit(1, "Failed to calloc ipaccess");
  2702. ips = 0;
  2703. ptr = buf;
  2704. while (ptr && *ptr) {
  2705. while (*ptr == ' ' || *ptr == '\t')
  2706. ptr++;
  2707. if (*ptr == ',') {
  2708. ptr++;
  2709. continue;
  2710. }
  2711. comma = strchr(ptr, ',');
  2712. if (comma)
  2713. *(comma++) = '\0';
  2714. group = NOPRIVGROUP;
  2715. if (isalpha(*ptr) && *(ptr+1) == ':') {
  2716. if (DEFINEDGROUP(*ptr))
  2717. group = GROUP(*ptr);
  2718. ptr += 2;
  2719. }
  2720. ipaccess[ips].group = group;
  2721. if (strcmp(ptr, ALLIP4) == 0)
  2722. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  2723. else {
  2724. slash = strchr(ptr, '/');
  2725. if (!slash)
  2726. ipaccess[ips].mask = 0xffffffff;
  2727. else {
  2728. *(slash++) = '\0';
  2729. mask = atoi(slash);
  2730. if (mask < 1 || mask > 32)
  2731. goto popipo; // skip invalid/zero
  2732. ipaccess[ips].mask = 0;
  2733. while (mask-- >= 0) {
  2734. octet = 1 << (mask % 8);
  2735. ipaccess[ips].mask |= (octet << (24 - (8 * (mask >> 3))));
  2736. }
  2737. }
  2738. ipaccess[ips].ip = 0; // missing default to '.0'
  2739. for (i = 0; ptr && (i < 4); i++) {
  2740. dot = strchr(ptr, '.');
  2741. if (dot)
  2742. *(dot++) = '\0';
  2743. octet = atoi(ptr);
  2744. if (octet < 0 || octet > 0xff)
  2745. goto popipo; // skip invalid
  2746. ipaccess[ips].ip |= (octet << (24 - (i * 8)));
  2747. ptr = dot;
  2748. }
  2749. ipaccess[ips].ip &= ipaccess[ips].mask;
  2750. }
  2751. ips++;
  2752. popipo:
  2753. ptr = comma;
  2754. }
  2755. free(buf);
  2756. }
  2757. static void *quit_thread(__maybe_unused void *userdata)
  2758. {
  2759. rename_thr("bfg-rpc-quit");
  2760. // allow thread creator to finish whatever it's doing
  2761. mutex_lock(&quit_restart_lock);
  2762. mutex_unlock(&quit_restart_lock);
  2763. if (opt_debug)
  2764. applog(LOG_DEBUG, "API: killing BFGMiner");
  2765. kill_work();
  2766. return NULL;
  2767. }
  2768. static void *restart_thread(__maybe_unused void *userdata)
  2769. {
  2770. rename_thr("bfg-rpc-restart");
  2771. // allow thread creator to finish whatever it's doing
  2772. mutex_lock(&quit_restart_lock);
  2773. mutex_unlock(&quit_restart_lock);
  2774. if (opt_debug)
  2775. applog(LOG_DEBUG, "API: restarting BFGMiner");
  2776. app_restart();
  2777. return NULL;
  2778. }
  2779. void api(int api_thr_id)
  2780. {
  2781. struct thr_info bye_thr;
  2782. char buf[TMPBUFSIZ];
  2783. char param_buf[TMPBUFSIZ];
  2784. const char *localaddr = "127.0.0.1";
  2785. SOCKETTYPE c;
  2786. int n, bound;
  2787. char *connectaddr;
  2788. char *binderror;
  2789. time_t bindstart;
  2790. short int port = opt_api_port;
  2791. struct sockaddr_in serv;
  2792. struct sockaddr_in cli;
  2793. socklen_t clisiz;
  2794. char cmdbuf[100];
  2795. char *cmd;
  2796. char *param;
  2797. bool addrok;
  2798. char group;
  2799. json_error_t json_err;
  2800. json_t *json_config;
  2801. json_t *json_val;
  2802. bool isjson;
  2803. bool did;
  2804. int i;
  2805. mutex_init(&quit_restart_lock);
  2806. pthread_cleanup_push(tidyup, NULL);
  2807. my_thr_id = api_thr_id;
  2808. if (!opt_api_listen) {
  2809. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  2810. return;
  2811. }
  2812. setup_groups();
  2813. if (opt_api_allow) {
  2814. setup_ipaccess();
  2815. if (ips == 0) {
  2816. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  2817. return;
  2818. }
  2819. }
  2820. /* This should be done before curl in needed
  2821. * to ensure curl has already called WSAStartup() in windows */
  2822. sleep(opt_log_interval);
  2823. sock = socket(AF_INET, SOCK_STREAM, 0);
  2824. if (sock == INVSOCK) {
  2825. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2826. return;
  2827. }
  2828. memset(&serv, 0, sizeof(serv));
  2829. serv.sin_family = AF_INET;
  2830. if (!opt_api_allow && !opt_api_network) {
  2831. serv.sin_addr.s_addr = inet_addr(localaddr);
  2832. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  2833. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2834. return;
  2835. }
  2836. }
  2837. serv.sin_port = htons(port);
  2838. #ifndef WIN32
  2839. // On linux with SO_REUSEADDR, bind will get the port if the previous
  2840. // socket is closed (even if it is still in TIME_WAIT) but fail if
  2841. // another program has it open - which is what we want
  2842. int optval = 1;
  2843. // If it doesn't work, we don't really care - just show a debug message
  2844. if (SOCKETFAIL(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval))))
  2845. applog(LOG_DEBUG, "API setsockopt SO_REUSEADDR failed (ignored): %s", SOCKERRMSG);
  2846. #else
  2847. // On windows a 2nd program can bind to a port>1024 already in use unless
  2848. // SO_EXCLUSIVEADDRUSE is used - however then the bind to a closed port
  2849. // in TIME_WAIT will fail until the timeout - so we leave the options alone
  2850. #endif
  2851. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  2852. bound = 0;
  2853. bindstart = time(NULL);
  2854. while (bound == 0) {
  2855. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  2856. binderror = SOCKERRMSG;
  2857. if ((time(NULL) - bindstart) > 61)
  2858. break;
  2859. else {
  2860. applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
  2861. sleep(30);
  2862. }
  2863. } else
  2864. bound = 1;
  2865. }
  2866. if (bound == 0) {
  2867. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  2868. return;
  2869. }
  2870. if (SOCKETFAIL(listen(sock, QUEUE))) {
  2871. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2872. CLOSESOCKET(sock);
  2873. return;
  2874. }
  2875. if (opt_api_allow)
  2876. applog(LOG_WARNING, "API running in IP access mode on port %d", port);
  2877. else {
  2878. if (opt_api_network)
  2879. applog(LOG_WARNING, "API running in UNRESTRICTED read access mode on port %d", port);
  2880. else
  2881. applog(LOG_WARNING, "API running in local read access mode on port %d", port);
  2882. }
  2883. io_buffer = malloc(MYBUFSIZ+1);
  2884. msg_buffer = malloc(MYBUFSIZ+1);
  2885. while (!bye) {
  2886. clisiz = sizeof(cli);
  2887. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  2888. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  2889. goto die;
  2890. }
  2891. connectaddr = inet_ntoa(cli.sin_addr);
  2892. addrok = false;
  2893. group = NOPRIVGROUP;
  2894. if (opt_api_allow) {
  2895. int client_ip = htonl(cli.sin_addr.s_addr);
  2896. for (i = 0; i < ips; i++) {
  2897. if ((client_ip & ipaccess[i].mask) == ipaccess[i].ip) {
  2898. addrok = true;
  2899. group = ipaccess[i].group;
  2900. break;
  2901. }
  2902. }
  2903. } else {
  2904. if (opt_api_network)
  2905. addrok = true;
  2906. else
  2907. addrok = (strcmp(connectaddr, localaddr) == 0);
  2908. }
  2909. if (opt_debug)
  2910. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  2911. if (addrok) {
  2912. n = recv(c, &buf[0], TMPBUFSIZ-1, 0);
  2913. if (SOCKETFAIL(n))
  2914. buf[0] = '\0';
  2915. else
  2916. buf[n] = '\0';
  2917. if (opt_debug) {
  2918. if (SOCKETFAIL(n))
  2919. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  2920. else
  2921. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  2922. }
  2923. if (!SOCKETFAIL(n)) {
  2924. // the time of the request in now
  2925. when = time(NULL);
  2926. did = false;
  2927. if (*buf != ISJSON) {
  2928. isjson = false;
  2929. param = strchr(buf, SEPARATOR);
  2930. if (param != NULL)
  2931. *(param++) = '\0';
  2932. cmd = buf;
  2933. }
  2934. else {
  2935. isjson = true;
  2936. param = NULL;
  2937. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  2938. json_config = json_loadb(buf, n, 0, &json_err);
  2939. #elif JANSSON_MAJOR_VERSION > 1
  2940. json_config = json_loads(buf, 0, &json_err);
  2941. #else
  2942. json_config = json_loads(buf, &json_err);
  2943. #endif
  2944. if (!json_is_object(json_config)) {
  2945. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  2946. send_result(c, isjson);
  2947. did = true;
  2948. }
  2949. else {
  2950. json_val = json_object_get(json_config, JSON_COMMAND);
  2951. if (json_val == NULL) {
  2952. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  2953. send_result(c, isjson);
  2954. did = true;
  2955. }
  2956. else {
  2957. if (!json_is_string(json_val)) {
  2958. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  2959. send_result(c, isjson);
  2960. did = true;
  2961. }
  2962. else {
  2963. cmd = (char *)json_string_value(json_val);
  2964. json_val = json_object_get(json_config, JSON_PARAMETER);
  2965. if (json_is_string(json_val))
  2966. param = (char *)json_string_value(json_val);
  2967. else if (json_is_integer(json_val)) {
  2968. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  2969. param = param_buf;
  2970. } else if (json_is_real(json_val)) {
  2971. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  2972. param = param_buf;
  2973. }
  2974. }
  2975. }
  2976. }
  2977. }
  2978. if (!did)
  2979. for (i = 0; cmds[i].name != NULL; i++) {
  2980. if (strcmp(cmd, cmds[i].name) == 0) {
  2981. sprintf(cmdbuf, "|%s|", cmd);
  2982. if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
  2983. (cmds[i].func)(c, param, isjson, group);
  2984. else {
  2985. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  2986. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  2987. }
  2988. send_result(c, isjson);
  2989. did = true;
  2990. break;
  2991. }
  2992. }
  2993. if (!did) {
  2994. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  2995. send_result(c, isjson);
  2996. }
  2997. }
  2998. }
  2999. CLOSESOCKET(c);
  3000. }
  3001. die:
  3002. /* Blank line fix for older compilers since pthread_cleanup_pop is a
  3003. * macro that gets confused by a label existing immediately before it
  3004. */
  3005. ;
  3006. pthread_cleanup_pop(true);
  3007. if (opt_debug)
  3008. applog(LOG_DEBUG, "API: terminating due to: %s",
  3009. do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
  3010. mutex_lock(&quit_restart_lock);
  3011. if (do_a_restart) {
  3012. if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
  3013. mutex_unlock(&quit_restart_lock);
  3014. quit(1, "API failed to initiate a restart - aborting");
  3015. }
  3016. pthread_detach(bye_thr.pth);
  3017. } else if (do_a_quit) {
  3018. if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
  3019. mutex_unlock(&quit_restart_lock);
  3020. quit(1, "API failed to initiate a clean quit - aborting");
  3021. }
  3022. pthread_detach(bye_thr.pth);
  3023. }
  3024. mutex_unlock(&quit_restart_lock);
  3025. }