api.c 99 KB

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