api.c 88 KB

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