api.c 101 KB

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