api.c 91 KB

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