api.c 93 KB

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