api.c 95 KB

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