api.c 90 KB

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