api.c 99 KB

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