api.c 92 KB

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