api.c 99 KB

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