api.c 84 KB

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