api.c 80 KB

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