api.c 101 KB

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