api.c 93 KB

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