api.c 88 KB

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