api.c 99 KB

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