api.c 91 KB

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