api.c 94 KB

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