api.c 97 KB

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