api.c 100 KB

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