api.c 85 KB

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