api.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  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(unix) || defined(__APPLE__)
  27. #include <errno.h>
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #define SOCKETTYPE int
  32. #define SOCKETFAIL(a) ((a) < 0)
  33. #define INVSOCK -1
  34. #define INVINETADDR -1
  35. #define CLOSESOCKET close
  36. #define SOCKERRMSG strerror(errno)
  37. #endif
  38. #ifdef WIN32
  39. #include <ws2tcpip.h>
  40. #include <winsock2.h>
  41. #define SOCKETTYPE SOCKET
  42. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  43. #define INVSOCK INVALID_SOCKET
  44. #define INVINETADDR INADDR_NONE
  45. #define CLOSESOCKET closesocket
  46. static char WSAbuf[1024];
  47. struct WSAERRORS {
  48. int id;
  49. char *code;
  50. } WSAErrors[] = {
  51. { 0, "No error" },
  52. { WSAEINTR, "Interrupted system call" },
  53. { WSAEBADF, "Bad file number" },
  54. { WSAEACCES, "Permission denied" },
  55. { WSAEFAULT, "Bad address" },
  56. { WSAEINVAL, "Invalid argument" },
  57. { WSAEMFILE, "Too many open sockets" },
  58. { WSAEWOULDBLOCK, "Operation would block" },
  59. { WSAEINPROGRESS, "Operation now in progress" },
  60. { WSAEALREADY, "Operation already in progress" },
  61. { WSAENOTSOCK, "Socket operation on non-socket" },
  62. { WSAEDESTADDRREQ, "Destination address required" },
  63. { WSAEMSGSIZE, "Message too long" },
  64. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  65. { WSAENOPROTOOPT, "Bad protocol option" },
  66. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  67. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  68. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  69. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  70. { WSAEAFNOSUPPORT, "Address family not supported" },
  71. { WSAEADDRINUSE, "Address already in use" },
  72. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  73. { WSAENETDOWN, "Network is down" },
  74. { WSAENETUNREACH, "Network is unreachable" },
  75. { WSAENETRESET, "Net connection reset" },
  76. { WSAECONNABORTED, "Software caused connection abort" },
  77. { WSAECONNRESET, "Connection reset by peer" },
  78. { WSAENOBUFS, "No buffer space available" },
  79. { WSAEISCONN, "Socket is already connected" },
  80. { WSAENOTCONN, "Socket is not connected" },
  81. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  82. { WSAETOOMANYREFS, "Too many references, can't splice" },
  83. { WSAETIMEDOUT, "Connection timed out" },
  84. { WSAECONNREFUSED, "Connection refused" },
  85. { WSAELOOP, "Too many levels of symbolic links" },
  86. { WSAENAMETOOLONG, "File name too long" },
  87. { WSAEHOSTDOWN, "Host is down" },
  88. { WSAEHOSTUNREACH, "No route to host" },
  89. { WSAENOTEMPTY, "Directory not empty" },
  90. { WSAEPROCLIM, "Too many processes" },
  91. { WSAEUSERS, "Too many users" },
  92. { WSAEDQUOT, "Disc quota exceeded" },
  93. { WSAESTALE, "Stale NFS file handle" },
  94. { WSAEREMOTE, "Too many levels of remote in path" },
  95. { WSASYSNOTREADY, "Network system is unavailable" },
  96. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  97. { WSANOTINITIALISED, "WSAStartup not yet called" },
  98. { WSAEDISCON, "Graceful shutdown in progress" },
  99. { WSAHOST_NOT_FOUND, "Host not found" },
  100. { WSANO_DATA, "No host data of that type was found" },
  101. { -1, "Unknown error code" }
  102. };
  103. static char *WSAErrorMsg()
  104. {
  105. int i;
  106. int id = WSAGetLastError();
  107. /* Assume none of them are actually -1 */
  108. for (i = 0; WSAErrors[i].id != -1; i++)
  109. if (WSAErrors[i].id == id)
  110. break;
  111. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  112. return &(WSAbuf[0]);
  113. }
  114. #define SOCKERRMSG WSAErrorMsg()
  115. #ifndef SHUT_RDWR
  116. #define SHUT_RDWR SD_BOTH
  117. #endif
  118. #ifndef in_addr_t
  119. #define in_addr_t uint32_t
  120. #endif
  121. #endif
  122. // Big enough for largest API request
  123. // though a PC with 100s of PGAs/CPUs may exceed the size ...
  124. // Current code assumes it can socket send this size also
  125. #define MYBUFSIZ 65432 // TODO: intercept before it's exceeded
  126. // Number of requests to queue - normally would be small
  127. // However lots of PGA's may mean more
  128. #define QUEUE 100
  129. static char *io_buffer = NULL;
  130. static char *msg_buffer = NULL;
  131. static SOCKETTYPE sock = INVSOCK;
  132. static const char *UNAVAILABLE = " - API will not be available";
  133. //static const char *BLANK = "";
  134. static const char *COMMA = ",";
  135. static const char SEPARATOR = '|';
  136. static const char GPUSEP = ',';
  137. static const char *APIVERSION = "1.7";
  138. static const char *DEAD = "Dead";
  139. static const char *SICK = "Sick";
  140. static const char *NOSTART = "NoStart";
  141. static const char *DISABLED = "Disabled";
  142. static const char *ALIVE = "Alive";
  143. #define _DYNAMIC "D"
  144. static const char *DYNAMIC = _DYNAMIC;
  145. static const char *YES = "Y";
  146. static const char *NO = "N";
  147. static const char *DEVICECODE = ""
  148. #ifdef HAVE_OPENCL
  149. "GPU "
  150. #endif
  151. #ifdef USE_BITFORCE
  152. "BFL "
  153. #endif
  154. #ifdef USE_ICARUS
  155. "ICA "
  156. #endif
  157. #ifdef WANT_CPUMINE
  158. "CPU "
  159. #endif
  160. "";
  161. static const char *OSINFO =
  162. #if defined(__linux)
  163. "Linux";
  164. #else
  165. #if defined(__APPLE__)
  166. "Apple";
  167. #else
  168. #if defined (WIN32)
  169. "Windows";
  170. #else
  171. #if defined(unix)
  172. "Unix";
  173. #else
  174. "Unknown";
  175. #endif
  176. #endif
  177. #endif
  178. #endif
  179. #define _DEVS "DEVS"
  180. #define _POOLS "POOLS"
  181. #define _SUMMARY "SUMMARY"
  182. #define _STATUS "STATUS"
  183. #define _VERSION "VERSION"
  184. #define _MINECON "CONFIG"
  185. #define _GPU "GPU"
  186. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  187. #define _PGA "PGA"
  188. #endif
  189. #ifdef WANT_CPUMINE
  190. #define _CPU "CPU"
  191. #endif
  192. #define _GPUS "GPUS"
  193. #define _PGAS "PGAS"
  194. #define _CPUS "CPUS"
  195. #define _NOTIFY "NOTIFY"
  196. #define _BYE "BYE"
  197. static const char ISJSON = '{';
  198. #define JSON0 "{"
  199. #define JSON1 "\""
  200. #define JSON2 "\":["
  201. #define JSON3 "]"
  202. #define JSON4 ",\"id\":1}"
  203. #define JSON_START JSON0
  204. #define JSON_DEVS JSON1 _DEVS JSON2
  205. #define JSON_POOLS JSON1 _POOLS JSON2
  206. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  207. #define JSON_STATUS JSON1 _STATUS JSON2
  208. #define JSON_VERSION JSON1 _VERSION JSON2
  209. #define JSON_MINECON JSON1 _MINECON JSON2
  210. #define JSON_GPU JSON1 _GPU JSON2
  211. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  212. #define JSON_PGA JSON1 _PGA JSON2
  213. #endif
  214. #ifdef WANT_CPUMINE
  215. #define JSON_CPU JSON1 _CPU JSON2
  216. #endif
  217. #define JSON_GPUS JSON1 _GPUS JSON2
  218. #define JSON_PGAS JSON1 _PGAS JSON2
  219. #define JSON_CPUS JSON1 _CPUS JSON2
  220. #define JSON_NOTIFY JSON1 _NOTIFY JSON2
  221. #define JSON_BYE JSON1 _BYE JSON1
  222. #define JSON_CLOSE JSON3
  223. #define JSON_END JSON4
  224. static const char *JSON_COMMAND = "command";
  225. static const char *JSON_PARAMETER = "parameter";
  226. #define MSG_INVGPU 1
  227. #define MSG_ALRENA 2
  228. #define MSG_ALRDIS 3
  229. #define MSG_GPUMRE 4
  230. #define MSG_GPUREN 5
  231. #define MSG_GPUNON 6
  232. #define MSG_POOL 7
  233. #define MSG_NOPOOL 8
  234. #define MSG_DEVS 9
  235. #define MSG_NODEVS 10
  236. #define MSG_SUMM 11
  237. #define MSG_GPUDIS 12
  238. #define MSG_GPUREI 13
  239. #define MSG_INVCMD 14
  240. #define MSG_MISID 15
  241. #define MSG_GPUDEV 17
  242. #ifdef WANT_CPUMINE
  243. #define MSG_CPUNON 16
  244. #define MSG_CPUDEV 18
  245. #define MSG_INVCPU 19
  246. #endif
  247. #define MSG_NUMGPU 20
  248. #define MSG_NUMCPU 21
  249. #define MSG_VERSION 22
  250. #define MSG_INVJSON 23
  251. #define MSG_MISCMD 24
  252. #define MSG_MISPID 25
  253. #define MSG_INVPID 26
  254. #define MSG_SWITCHP 27
  255. #define MSG_MISVAL 28
  256. #define MSG_NOADL 29
  257. #define MSG_NOGPUADL 30
  258. #define MSG_INVINT 31
  259. #define MSG_GPUINT 32
  260. #define MSG_MINECON 33
  261. #define MSG_GPUMERR 34
  262. #define MSG_GPUMEM 35
  263. #define MSG_GPUEERR 36
  264. #define MSG_GPUENG 37
  265. #define MSG_GPUVERR 38
  266. #define MSG_GPUVDDC 39
  267. #define MSG_GPUFERR 40
  268. #define MSG_GPUFAN 41
  269. #define MSG_MISFN 42
  270. #define MSG_BADFN 43
  271. #define MSG_SAVED 44
  272. #define MSG_ACCDENY 45
  273. #define MSG_ACCOK 46
  274. #define MSG_ENAPOOL 47
  275. #define MSG_DISPOOL 48
  276. #define MSG_ALRENAP 49
  277. #define MSG_ALRDISP 50
  278. #define MSG_DISLASTP 51
  279. #define MSG_MISPDP 52
  280. #define MSG_INVPDP 53
  281. #define MSG_TOOMANYP 54
  282. #define MSG_ADDPOOL 55
  283. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  284. #define MSG_PGANON 56
  285. #define MSG_PGADEV 57
  286. #define MSG_INVPGA 58
  287. #endif
  288. #define MSG_NUMPGA 59
  289. #define MSG_NOTIFY 60
  290. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  291. #define MSG_PGALRENA 61
  292. #define MSG_PGALRDIS 62
  293. #define MSG_PGAENA 63
  294. #define MSG_PGADIS 64
  295. #define MSG_PGAUNW 65
  296. #endif
  297. #define MSG_REMLASTP 66
  298. #define MSG_ACTPOOL 67
  299. #define MSG_REMPOOL 68
  300. enum code_severity {
  301. SEVERITY_ERR,
  302. SEVERITY_WARN,
  303. SEVERITY_INFO,
  304. SEVERITY_SUCC,
  305. SEVERITY_FAIL
  306. };
  307. enum code_parameters {
  308. PARAM_GPU,
  309. PARAM_PGA,
  310. PARAM_CPU,
  311. PARAM_GPUMAX,
  312. PARAM_PGAMAX,
  313. PARAM_CPUMAX,
  314. PARAM_PMAX,
  315. PARAM_POOLMAX,
  316. // Single generic case: have the code resolve it - see below
  317. PARAM_DMAX,
  318. PARAM_CMD,
  319. PARAM_POOL,
  320. PARAM_STR,
  321. PARAM_BOTH,
  322. PARAM_NONE
  323. };
  324. struct CODES {
  325. const enum code_severity severity;
  326. const int code;
  327. const enum code_parameters params;
  328. const char *description;
  329. } codes[] = {
  330. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  331. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  332. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  333. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  334. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  335. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  336. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  337. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  338. { SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX, "%d GPU(s)"
  339. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  340. " - %d PGA(s)"
  341. #endif
  342. #ifdef WANT_CPUMINE
  343. " - %d CPU(s)"
  344. #endif
  345. },
  346. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
  347. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  348. "/PGAs"
  349. #endif
  350. #ifdef WANT_CPUMINE
  351. "/CPUs"
  352. #endif
  353. },
  354. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  355. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  356. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  357. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  358. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  359. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  360. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  361. { SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
  362. { SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
  363. { SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
  364. { SEVERITY_INFO, MSG_PGALRENA,PARAM_PGA, "PGA %d already enabled" },
  365. { SEVERITY_INFO, MSG_PGALRDIS,PARAM_PGA, "PGA %d already disabled" },
  366. { SEVERITY_INFO, MSG_PGAENA, PARAM_PGA, "PGA %d sent enable message" },
  367. { SEVERITY_INFO, MSG_PGADIS, PARAM_PGA, "PGA %d set disable flag" },
  368. { SEVERITY_ERR, MSG_PGAUNW, PARAM_PGA, "PGA %d is not flagged WELL, cannot enable" },
  369. #endif
  370. #ifdef WANT_CPUMINE
  371. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  372. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  373. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  374. #endif
  375. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  376. { SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
  377. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  378. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "CGMiner versions" },
  379. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  380. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  381. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  382. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  383. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  384. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  385. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  386. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  387. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  388. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  389. { SEVERITY_SUCC, MSG_MINECON, PARAM_NONE, "CGMiner config" },
  390. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  391. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported succeess" },
  392. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  393. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported succeess" },
  394. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  395. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported succeess" },
  396. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  397. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported succeess" },
  398. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  399. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  400. { SEVERITY_ERR, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  401. { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
  402. { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
  403. { SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
  404. { SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
  405. { SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
  406. { SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
  407. { SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
  408. { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
  409. { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
  410. { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
  411. { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
  412. { SEVERITY_ERR, MSG_REMLASTP,PARAM_POOL, "Cannot remove last pool %d:'%s'" },
  413. { SEVERITY_ERR, MSG_ACTPOOL, PARAM_POOL, "Cannot remove active pool %d:'%s'" },
  414. { SEVERITY_SUCC, MSG_REMPOOL, PARAM_BOTH, "Removed pool %d:'%s'" },
  415. { SEVERITY_SUCC, MSG_NOTIFY, PARAM_NONE, "Notify" },
  416. { SEVERITY_FAIL, 0, 0, NULL }
  417. };
  418. static int my_thr_id = 0;
  419. static int bye = 0;
  420. static bool ping = true;
  421. static time_t when = 0; // when the request occurred
  422. struct IP4ACCESS {
  423. in_addr_t ip;
  424. in_addr_t mask;
  425. bool writemode;
  426. };
  427. static struct IP4ACCESS *ipaccess = NULL;
  428. static int ips = 0;
  429. #ifdef USE_BITFORCE
  430. extern struct device_api bitforce_api;
  431. #endif
  432. #ifdef USE_ICARUS
  433. extern struct device_api icarus_api;
  434. #endif
  435. // This is only called when expected to be needed (rarely)
  436. // i.e. strings outside of the codes control (input from the user)
  437. static char *escape_string(char *str, bool isjson)
  438. {
  439. char *buf, *ptr;
  440. int count;
  441. count = 0;
  442. for (ptr = str; *ptr; ptr++) {
  443. switch (*ptr) {
  444. case ',':
  445. case '|':
  446. case '=':
  447. if (!isjson)
  448. count++;
  449. break;
  450. case '"':
  451. if (isjson)
  452. count++;
  453. break;
  454. case '\\':
  455. count++;
  456. break;
  457. }
  458. }
  459. if (count == 0)
  460. return str;
  461. buf = malloc(strlen(str) + count + 1);
  462. if (unlikely(!buf))
  463. quit(1, "Failed to malloc escape buf");
  464. ptr = buf;
  465. while (*str)
  466. switch (*str) {
  467. case ',':
  468. case '|':
  469. case '=':
  470. if (!isjson)
  471. *(ptr++) = '\\';
  472. *(ptr++) = *(str++);
  473. break;
  474. case '"':
  475. if (isjson)
  476. *(ptr++) = '\\';
  477. *(ptr++) = *(str++);
  478. break;
  479. case '\\':
  480. *(ptr++) = '\\';
  481. *(ptr++) = *(str++);
  482. break;
  483. default:
  484. *(ptr++) = *(str++);
  485. break;
  486. }
  487. *ptr = '\0';
  488. return buf;
  489. }
  490. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  491. static int numpgas()
  492. {
  493. int count = 0;
  494. int i;
  495. for (i = 0; i < total_devices; i++) {
  496. #ifdef USE_BITFORCE
  497. if (devices[i]->api == &bitforce_api)
  498. count++;
  499. #endif
  500. #ifdef USE_ICARUS
  501. if (devices[i]->api == &icarus_api)
  502. count++;
  503. #endif
  504. }
  505. return count;
  506. }
  507. static int pgadevice(int pgaid)
  508. {
  509. int count = 0;
  510. int i;
  511. for (i = 0; i < total_devices; i++) {
  512. #ifdef USE_BITFORCE
  513. if (devices[i]->api == &bitforce_api)
  514. count++;
  515. #endif
  516. #ifdef USE_ICARUS
  517. if (devices[i]->api == &icarus_api)
  518. count++;
  519. #endif
  520. if (count == (pgaid + 1))
  521. return i;
  522. }
  523. return -1;
  524. }
  525. #endif
  526. // All replies (except BYE) start with a message
  527. // thus for JSON, message() inserts JSON_START at the front
  528. // and send_result() adds JSON_END at the end
  529. static char *message(int messageid, int paramid, char *param2, bool isjson)
  530. {
  531. char severity;
  532. char *ptr;
  533. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  534. int pga;
  535. #endif
  536. #ifdef WANT_CPUMINE
  537. int cpu;
  538. #endif
  539. int i;
  540. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  541. if (codes[i].code == messageid) {
  542. switch (codes[i].severity) {
  543. case SEVERITY_WARN:
  544. severity = 'W';
  545. break;
  546. case SEVERITY_INFO:
  547. severity = 'I';
  548. break;
  549. case SEVERITY_SUCC:
  550. severity = 'S';
  551. break;
  552. case SEVERITY_ERR:
  553. default:
  554. severity = 'E';
  555. break;
  556. }
  557. if (isjson)
  558. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"When\":%lu,\"Code\":%d,\"Msg\":\"", severity, (unsigned long)when, messageid);
  559. else
  560. sprintf(msg_buffer, _STATUS "=%c,When=%lu,Code=%d,Msg=", severity, (unsigned long)when, messageid);
  561. ptr = msg_buffer + strlen(msg_buffer);
  562. switch(codes[i].params) {
  563. case PARAM_GPU:
  564. case PARAM_PGA:
  565. case PARAM_CPU:
  566. sprintf(ptr, codes[i].description, paramid);
  567. break;
  568. case PARAM_POOL:
  569. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  570. break;
  571. case PARAM_GPUMAX:
  572. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  573. break;
  574. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  575. case PARAM_PGAMAX:
  576. pga = numpgas();
  577. sprintf(ptr, codes[i].description, paramid, pga - 1);
  578. break;
  579. #endif
  580. #ifdef WANT_CPUMINE
  581. case PARAM_CPUMAX:
  582. if (opt_n_threads > 0)
  583. cpu = num_processors;
  584. else
  585. cpu = 0;
  586. sprintf(ptr, codes[i].description, paramid, cpu - 1);
  587. break;
  588. #endif
  589. case PARAM_PMAX:
  590. sprintf(ptr, codes[i].description, total_pools);
  591. break;
  592. case PARAM_POOLMAX:
  593. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  594. break;
  595. case PARAM_DMAX:
  596. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  597. pga = numpgas();
  598. #endif
  599. #ifdef WANT_CPUMINE
  600. if (opt_n_threads > 0)
  601. cpu = num_processors;
  602. else
  603. cpu = 0;
  604. #endif
  605. sprintf(ptr, codes[i].description, nDevs
  606. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  607. , pga
  608. #endif
  609. #ifdef WANT_CPUMINE
  610. , cpu
  611. #endif
  612. );
  613. break;
  614. case PARAM_CMD:
  615. sprintf(ptr, codes[i].description, JSON_COMMAND);
  616. break;
  617. case PARAM_STR:
  618. sprintf(ptr, codes[i].description, param2);
  619. break;
  620. case PARAM_BOTH:
  621. sprintf(ptr, codes[i].description, paramid, param2);
  622. break;
  623. case PARAM_NONE:
  624. default:
  625. strcpy(ptr, codes[i].description);
  626. }
  627. ptr = msg_buffer + strlen(msg_buffer);
  628. if (isjson)
  629. sprintf(ptr, "\",\"Description\":\"%s\"}" JSON_CLOSE, opt_api_description);
  630. else
  631. sprintf(ptr, ",Description=%s%c", opt_api_description, SEPARATOR);
  632. return msg_buffer;
  633. }
  634. }
  635. if (isjson)
  636. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"When\":%lu,\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE,
  637. (unsigned long)when, messageid, opt_api_description);
  638. else
  639. sprintf(msg_buffer, _STATUS "=F,When=%lu,Code=-1,Msg=%d,Description=%s%c",
  640. (unsigned long)when, messageid, opt_api_description, SEPARATOR);
  641. return msg_buffer;
  642. }
  643. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  644. {
  645. if (isjson)
  646. sprintf(io_buffer, "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE,
  647. message(MSG_VERSION, 0, NULL, isjson),
  648. VERSION, APIVERSION);
  649. else
  650. sprintf(io_buffer, "%s" _VERSION ",CGMiner=%s,API=%s%c",
  651. message(MSG_VERSION, 0, NULL, isjson),
  652. VERSION, APIVERSION, SEPARATOR);
  653. }
  654. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  655. {
  656. char buf[BUFSIZ];
  657. int pgacount = 0;
  658. int cpucount = 0;
  659. char *adlinuse = (char *)NO;
  660. #ifdef HAVE_ADL
  661. const char *adl = YES;
  662. int i;
  663. for (i = 0; i < nDevs; i++) {
  664. if (gpus[i].has_adl) {
  665. adlinuse = (char *)YES;
  666. break;
  667. }
  668. }
  669. #else
  670. const char *adl = NO;
  671. #endif
  672. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  673. pgacount = numpgas();
  674. #endif
  675. #ifdef WANT_CPUMINE
  676. cpucount = opt_n_threads > 0 ? num_processors : 0;
  677. #endif
  678. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  679. if (isjson)
  680. sprintf(buf, "," JSON_MINECON "{\"GPU Count\":%d,\"PGA Count\":%d,\"CPU Count\":%d,\"Pool Count\":%d,\"ADL\":\"%s\",\"ADL in use\":\"%s\",\"Strategy\":\"%s\",\"Log Interval\":%d,\"Device Code\":\"%s\",\"OS\":\"%s\"}" JSON_CLOSE, nDevs, pgacount, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, opt_log_interval, DEVICECODE, OSINFO);
  681. else
  682. sprintf(buf, _MINECON ",GPU Count=%d,PGA Count=%d,CPU Count=%d,Pool Count=%d,ADL=%s,ADL in use=%s,Strategy=%s,Log Interval=%d,Device Code=%s,OS=%s%c", nDevs, pgacount, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, opt_log_interval, DEVICECODE, OSINFO, SEPARATOR);
  683. strcat(io_buffer, buf);
  684. }
  685. static void gpustatus(int gpu, bool isjson)
  686. {
  687. char intensity[20];
  688. char buf[BUFSIZ];
  689. char *enabled;
  690. char *status;
  691. float gt, gv;
  692. int ga, gf, gp, gc, gm, pt;
  693. if (gpu >= 0 && gpu < nDevs) {
  694. struct cgpu_info *cgpu = &gpus[gpu];
  695. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  696. #ifdef HAVE_ADL
  697. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  698. #endif
  699. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  700. if (cgpu->deven != DEV_DISABLED)
  701. enabled = (char *)YES;
  702. else
  703. enabled = (char *)NO;
  704. if (cgpu->status == LIFE_DEAD)
  705. status = (char *)DEAD;
  706. else if (cgpu->status == LIFE_SICK)
  707. status = (char *)SICK;
  708. else if (cgpu->status == LIFE_NOSTART)
  709. status = (char *)NOSTART;
  710. else
  711. status = (char *)ALIVE;
  712. if (cgpu->dynamic)
  713. strcpy(intensity, DYNAMIC);
  714. else
  715. sprintf(intensity, "%d", cgpu->intensity);
  716. if (isjson)
  717. sprintf(buf, "{\"GPU\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f,\"Fan Speed\":%d,\"Fan Percent\":%d,\"GPU Clock\":%d,\"Memory Clock\":%d,\"GPU Voltage\":%.3f,\"GPU Activity\":%d,\"Powertune\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Intensity\":\"%s\",\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f}",
  718. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  719. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  720. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  721. cgpu->utility, intensity,
  722. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  723. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  724. else
  725. sprintf(buf, "GPU=%d,Enabled=%s,Status=%s,Temperature=%.2f,Fan Speed=%d,Fan Percent=%d,GPU Clock=%d,Memory Clock=%d,GPU Voltage=%.3f,GPU Activity=%d,Powertune=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Intensity=%s,Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f%c",
  726. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  727. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  728. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  729. cgpu->utility, intensity,
  730. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  731. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, SEPARATOR);
  732. strcat(io_buffer, buf);
  733. }
  734. }
  735. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  736. static void pgastatus(int pga, bool isjson)
  737. {
  738. char buf[BUFSIZ];
  739. char *enabled;
  740. char *status;
  741. int numpga = numpgas();
  742. if (numpga > 0 && pga >= 0 && pga < numpga) {
  743. int dev = pgadevice(pga);
  744. if (dev < 0) // Should never happen
  745. return;
  746. struct cgpu_info *cgpu = devices[dev];
  747. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  748. if (cgpu->deven != DEV_DISABLED)
  749. enabled = (char *)YES;
  750. else
  751. enabled = (char *)NO;
  752. if (cgpu->status == LIFE_DEAD)
  753. status = (char *)DEAD;
  754. else if (cgpu->status == LIFE_SICK)
  755. status = (char *)SICK;
  756. else if (cgpu->status == LIFE_NOSTART)
  757. status = (char *)NOSTART;
  758. else
  759. status = (char *)ALIVE;
  760. if (isjson)
  761. sprintf(buf, "{\"PGA\":%d,\"Name\":\"%s\",\"ID\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f}",
  762. pga, cgpu->api->name, cgpu->device_id,
  763. enabled, status, cgpu->temp,
  764. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  765. cgpu->accepted, cgpu->rejected, cgpu->hw_errors, cgpu->utility,
  766. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  767. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  768. else
  769. sprintf(buf, "PGA=%d,Name=%s,ID=%d,Enabled=%s,Status=%s,Temperature=%.2f,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f%c",
  770. pga, cgpu->api->name, cgpu->device_id,
  771. enabled, status, cgpu->temp,
  772. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  773. cgpu->accepted, cgpu->rejected, cgpu->hw_errors, cgpu->utility,
  774. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  775. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, SEPARATOR);
  776. strcat(io_buffer, buf);
  777. }
  778. }
  779. #endif
  780. #ifdef WANT_CPUMINE
  781. static void cpustatus(int cpu, bool isjson)
  782. {
  783. char buf[BUFSIZ];
  784. if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
  785. struct cgpu_info *cgpu = &cpus[cpu];
  786. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  787. if (isjson)
  788. sprintf(buf, "{\"CPU\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Utility\":%.2f,\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f}",
  789. cpu, cgpu->total_mhashes / total_secs,
  790. opt_log_interval, cgpu->rolling,
  791. cgpu->accepted, cgpu->rejected,
  792. cgpu->utility,
  793. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  794. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  795. else
  796. sprintf(buf, "CPU=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Utility=%.2f,Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f%c",
  797. cpu, cgpu->total_mhashes / total_secs,
  798. opt_log_interval, cgpu->rolling,
  799. cgpu->accepted, cgpu->rejected,
  800. cgpu->utility,
  801. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  802. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, SEPARATOR);
  803. strcat(io_buffer, buf);
  804. }
  805. }
  806. #endif
  807. static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  808. {
  809. int devcount = 0;
  810. int i;
  811. if (nDevs == 0 && opt_n_threads == 0) {
  812. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  813. return;
  814. }
  815. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  816. if (isjson) {
  817. strcat(io_buffer, COMMA);
  818. strcat(io_buffer, JSON_DEVS);
  819. }
  820. for (i = 0; i < nDevs; i++) {
  821. if (isjson && devcount > 0)
  822. strcat(io_buffer, COMMA);
  823. gpustatus(i, isjson);
  824. devcount++;
  825. }
  826. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  827. int numpga = numpgas();
  828. if (numpga > 0)
  829. for (i = 0; i < numpga; i++) {
  830. if (isjson && devcount > 0)
  831. strcat(io_buffer, COMMA);
  832. pgastatus(i, isjson);
  833. devcount++;
  834. }
  835. #endif
  836. #ifdef WANT_CPUMINE
  837. if (opt_n_threads > 0)
  838. for (i = 0; i < num_processors; i++) {
  839. if (isjson && devcount > 0)
  840. strcat(io_buffer, COMMA);
  841. cpustatus(i, isjson);
  842. devcount++;
  843. }
  844. #endif
  845. if (isjson)
  846. strcat(io_buffer, JSON_CLOSE);
  847. }
  848. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  849. {
  850. int id;
  851. if (nDevs == 0) {
  852. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  853. return;
  854. }
  855. if (param == NULL || *param == '\0') {
  856. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  857. return;
  858. }
  859. id = atoi(param);
  860. if (id < 0 || id >= nDevs) {
  861. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  862. return;
  863. }
  864. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  865. if (isjson) {
  866. strcat(io_buffer, COMMA);
  867. strcat(io_buffer, JSON_GPU);
  868. }
  869. gpustatus(id, isjson);
  870. if (isjson)
  871. strcat(io_buffer, JSON_CLOSE);
  872. }
  873. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  874. static void pgadev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  875. {
  876. int numpga = numpgas();
  877. int id;
  878. if (numpga == 0) {
  879. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  880. return;
  881. }
  882. if (param == NULL || *param == '\0') {
  883. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  884. return;
  885. }
  886. id = atoi(param);
  887. if (id < 0 || id >= numpga) {
  888. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  889. return;
  890. }
  891. strcpy(io_buffer, message(MSG_PGADEV, id, NULL, isjson));
  892. if (isjson) {
  893. strcat(io_buffer, COMMA);
  894. strcat(io_buffer, JSON_PGA);
  895. }
  896. pgastatus(id, isjson);
  897. if (isjson)
  898. strcat(io_buffer, JSON_CLOSE);
  899. }
  900. static void pgaenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  901. {
  902. int numpga = numpgas();
  903. struct thr_info *thr;
  904. int pga;
  905. int id;
  906. int i;
  907. if (numpga == 0) {
  908. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  909. return;
  910. }
  911. if (param == NULL || *param == '\0') {
  912. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  913. return;
  914. }
  915. id = atoi(param);
  916. if (id < 0 || id >= numpga) {
  917. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  918. return;
  919. }
  920. int dev = pgadevice(id);
  921. if (dev < 0) { // Should never happen
  922. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  923. return;
  924. }
  925. struct cgpu_info *cgpu = devices[dev];
  926. if (cgpu->deven != DEV_DISABLED) {
  927. strcpy(io_buffer, message(MSG_PGALRENA, id, NULL, isjson));
  928. return;
  929. }
  930. if (cgpu->status != LIFE_WELL) {
  931. strcpy(io_buffer, message(MSG_PGAUNW, id, NULL, isjson));
  932. return;
  933. }
  934. for (i = 0; i < mining_threads; i++) {
  935. pga = thr_info[i].cgpu->device_id;
  936. if (pga == dev) {
  937. thr = &thr_info[i];
  938. cgpu->deven = DEV_ENABLED;
  939. tq_push(thr->q, &ping);
  940. }
  941. }
  942. strcpy(io_buffer, message(MSG_PGAENA, id, NULL, isjson));
  943. }
  944. static void pgadisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  945. {
  946. int numpga = numpgas();
  947. int id;
  948. if (numpga == 0) {
  949. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  950. return;
  951. }
  952. if (param == NULL || *param == '\0') {
  953. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  954. return;
  955. }
  956. id = atoi(param);
  957. if (id < 0 || id >= numpga) {
  958. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  959. return;
  960. }
  961. int dev = pgadevice(id);
  962. if (dev < 0) { // Should never happen
  963. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  964. return;
  965. }
  966. struct cgpu_info *cgpu = devices[dev];
  967. if (cgpu->deven == DEV_DISABLED) {
  968. strcpy(io_buffer, message(MSG_PGALRDIS, id, NULL, isjson));
  969. return;
  970. }
  971. cgpu->deven = DEV_DISABLED;
  972. strcpy(io_buffer, message(MSG_PGADIS, id, NULL, isjson));
  973. }
  974. #endif
  975. #ifdef WANT_CPUMINE
  976. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  977. {
  978. int id;
  979. if (opt_n_threads == 0) {
  980. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  981. return;
  982. }
  983. if (param == NULL || *param == '\0') {
  984. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  985. return;
  986. }
  987. id = atoi(param);
  988. if (id < 0 || id >= num_processors) {
  989. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  990. return;
  991. }
  992. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  993. if (isjson) {
  994. strcat(io_buffer, COMMA);
  995. strcat(io_buffer, JSON_CPU);
  996. }
  997. cpustatus(id, isjson);
  998. if (isjson)
  999. strcat(io_buffer, JSON_CLOSE);
  1000. }
  1001. #endif
  1002. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1003. {
  1004. char buf[BUFSIZ];
  1005. char *status, *lp;
  1006. char *rpc_url;
  1007. char *rpc_user;
  1008. int i;
  1009. if (total_pools == 0) {
  1010. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1011. return;
  1012. }
  1013. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  1014. if (isjson) {
  1015. strcat(io_buffer, COMMA);
  1016. strcat(io_buffer, JSON_POOLS);
  1017. }
  1018. for (i = 0; i < total_pools; i++) {
  1019. struct pool *pool = pools[i];
  1020. if (!pool->enabled)
  1021. status = (char *)DISABLED;
  1022. else {
  1023. if (pool->idle)
  1024. status = (char *)DEAD;
  1025. else
  1026. status = (char *)ALIVE;
  1027. }
  1028. if (pool->hdr_path)
  1029. lp = (char *)YES;
  1030. else
  1031. lp = (char *)NO;
  1032. rpc_url = escape_string(pool->rpc_url, isjson);
  1033. rpc_user = escape_string(pool->rpc_user, isjson);
  1034. if (isjson)
  1035. sprintf(buf, "%s{\"POOL\":%d,\"URL\":\"%s\",\"Status\":\"%s\",\"Priority\":%d,\"Long Poll\":\"%s\",\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Remote Failures\":%d,\"User\":\"%s\"}",
  1036. (i > 0) ? COMMA : "",
  1037. i, rpc_url, status, pool->prio, lp,
  1038. pool->getwork_requested,
  1039. pool->accepted, pool->rejected,
  1040. pool->discarded_work,
  1041. pool->stale_shares,
  1042. pool->getfail_occasions,
  1043. pool->remotefail_occasions,
  1044. rpc_user);
  1045. else
  1046. sprintf(buf, "POOL=%d,URL=%s,Status=%s,Priority=%d,Long Poll=%s,Getworks=%d,Accepted=%d,Rejected=%d,Discarded=%d,Stale=%d,Get Failures=%d,Remote Failures=%d,User=%s%c",
  1047. i, rpc_url, status, pool->prio, lp,
  1048. pool->getwork_requested,
  1049. pool->accepted, pool->rejected,
  1050. pool->discarded_work,
  1051. pool->stale_shares,
  1052. pool->getfail_occasions,
  1053. pool->remotefail_occasions,
  1054. rpc_user, SEPARATOR);
  1055. strcat(io_buffer, buf);
  1056. if (rpc_url != pool->rpc_url)
  1057. free(rpc_url);
  1058. rpc_url = NULL;
  1059. if (rpc_user != pool->rpc_user)
  1060. free(rpc_user);
  1061. rpc_user = NULL;
  1062. }
  1063. if (isjson)
  1064. strcat(io_buffer, JSON_CLOSE);
  1065. }
  1066. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1067. {
  1068. double utility, mhs;
  1069. #ifdef WANT_CPUMINE
  1070. char *algo = (char *)(algo_names[opt_algo]);
  1071. if (algo == NULL)
  1072. algo = "(null)";
  1073. #endif
  1074. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1075. mhs = total_mhashes_done / total_secs;
  1076. #ifdef WANT_CPUMINE
  1077. if (isjson)
  1078. sprintf(io_buffer, "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"Algorithm\":\"%s\",\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u,\"Total MH\":%.4f}" JSON_CLOSE,
  1079. message(MSG_SUMM, 0, NULL, isjson),
  1080. total_secs, algo, mhs, found_blocks,
  1081. total_getworks, total_accepted, total_rejected,
  1082. hw_errors, utility, total_discarded, total_stale,
  1083. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1084. else
  1085. sprintf(io_buffer, "%s" _SUMMARY ",Elapsed=%.0f,Algorithm=%s,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u,Total MH=%.4f%c",
  1086. message(MSG_SUMM, 0, NULL, isjson),
  1087. total_secs, algo, mhs, found_blocks,
  1088. total_getworks, total_accepted, total_rejected,
  1089. hw_errors, utility, total_discarded, total_stale,
  1090. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  1091. #else
  1092. if (isjson)
  1093. sprintf(io_buffer, "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u,\"Total MH\":%.4f}" JSON_CLOSE,
  1094. message(MSG_SUMM, 0, NULL, isjson),
  1095. total_secs, mhs, found_blocks,
  1096. total_getworks, total_accepted, total_rejected,
  1097. hw_errors, utility, total_discarded, total_stale,
  1098. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1099. else
  1100. sprintf(io_buffer, "%s" _SUMMARY ",Elapsed=%.0f,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u,Total MH=%.4f%c",
  1101. message(MSG_SUMM, 0, NULL, isjson),
  1102. total_secs, mhs, found_blocks,
  1103. total_getworks, total_accepted, total_rejected,
  1104. hw_errors, utility, total_discarded, total_stale,
  1105. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  1106. #endif
  1107. }
  1108. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1109. {
  1110. struct thr_info *thr;
  1111. int gpu;
  1112. int id;
  1113. int i;
  1114. if (gpu_threads == 0) {
  1115. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1116. return;
  1117. }
  1118. if (param == NULL || *param == '\0') {
  1119. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1120. return;
  1121. }
  1122. id = atoi(param);
  1123. if (id < 0 || id >= nDevs) {
  1124. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1125. return;
  1126. }
  1127. if (gpus[id].deven != DEV_DISABLED) {
  1128. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  1129. return;
  1130. }
  1131. for (i = 0; i < gpu_threads; i++) {
  1132. gpu = thr_info[i].cgpu->device_id;
  1133. if (gpu == id) {
  1134. thr = &thr_info[i];
  1135. if (thr->cgpu->status != LIFE_WELL) {
  1136. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  1137. return;
  1138. }
  1139. gpus[id].deven = DEV_ENABLED;
  1140. tq_push(thr->q, &ping);
  1141. }
  1142. }
  1143. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  1144. }
  1145. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1146. {
  1147. int id;
  1148. if (nDevs == 0) {
  1149. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1150. return;
  1151. }
  1152. if (param == NULL || *param == '\0') {
  1153. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1154. return;
  1155. }
  1156. id = atoi(param);
  1157. if (id < 0 || id >= nDevs) {
  1158. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1159. return;
  1160. }
  1161. if (gpus[id].deven == DEV_DISABLED) {
  1162. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  1163. return;
  1164. }
  1165. gpus[id].deven = DEV_DISABLED;
  1166. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  1167. }
  1168. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1169. {
  1170. int id;
  1171. if (nDevs == 0) {
  1172. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1173. return;
  1174. }
  1175. if (param == NULL || *param == '\0') {
  1176. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1177. return;
  1178. }
  1179. id = atoi(param);
  1180. if (id < 0 || id >= nDevs) {
  1181. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1182. return;
  1183. }
  1184. reinit_device(&gpus[id]);
  1185. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  1186. }
  1187. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1188. {
  1189. char buf[BUFSIZ];
  1190. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  1191. if (isjson)
  1192. sprintf(buf, "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE, nDevs);
  1193. else
  1194. sprintf(buf, _GPUS ",Count=%d%c", nDevs, SEPARATOR);
  1195. strcat(io_buffer, buf);
  1196. }
  1197. static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1198. {
  1199. char buf[BUFSIZ];
  1200. int count = 0;
  1201. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  1202. count = numpgas();
  1203. #endif
  1204. strcpy(io_buffer, message(MSG_NUMPGA, 0, NULL, isjson));
  1205. if (isjson)
  1206. sprintf(buf, "," JSON_PGAS "{\"Count\":%d}" JSON_CLOSE, count);
  1207. else
  1208. sprintf(buf, _PGAS ",Count=%d%c", count, SEPARATOR);
  1209. strcat(io_buffer, buf);
  1210. }
  1211. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1212. {
  1213. char buf[BUFSIZ];
  1214. int count = 0;
  1215. #ifdef WANT_CPUMINE
  1216. count = opt_n_threads > 0 ? num_processors : 0;
  1217. #endif
  1218. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  1219. if (isjson)
  1220. sprintf(buf, "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE, count);
  1221. else
  1222. sprintf(buf, _CPUS ",Count=%d%c", count, SEPARATOR);
  1223. strcat(io_buffer, buf);
  1224. }
  1225. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1226. {
  1227. struct pool *pool;
  1228. int id;
  1229. if (total_pools == 0) {
  1230. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1231. return;
  1232. }
  1233. if (param == NULL || *param == '\0') {
  1234. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1235. return;
  1236. }
  1237. id = atoi(param);
  1238. if (id < 0 || id >= total_pools) {
  1239. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1240. return;
  1241. }
  1242. pool = pools[id];
  1243. pool->enabled = true;
  1244. switch_pools(pool);
  1245. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  1246. }
  1247. static void copyadvanceafter(char ch, char **param, char **buf)
  1248. {
  1249. #define src_p (*param)
  1250. #define dst_b (*buf)
  1251. while (*src_p && *src_p != ch) {
  1252. if (*src_p == '\\' && *(src_p+1) != '\0')
  1253. src_p++;
  1254. *(dst_b++) = *(src_p++);
  1255. }
  1256. if (*src_p)
  1257. src_p++;
  1258. *(dst_b++) = '\0';
  1259. }
  1260. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1261. {
  1262. char *ptr, *buf;
  1263. ptr = buf = malloc(strlen(param)+1);
  1264. if (unlikely(!buf))
  1265. quit(1, "Failed to malloc pooldetails buf");
  1266. *url = buf;
  1267. // copy url
  1268. copyadvanceafter(',', &param, &buf);
  1269. if (!(*param)) // missing user
  1270. goto exitsama;
  1271. *user = buf;
  1272. // copy user
  1273. copyadvanceafter(',', &param, &buf);
  1274. if (!*param) // missing pass
  1275. goto exitsama;
  1276. *pass = buf;
  1277. // copy pass
  1278. copyadvanceafter(',', &param, &buf);
  1279. return true;
  1280. exitsama:
  1281. free(ptr);
  1282. return false;
  1283. }
  1284. static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1285. {
  1286. char *url, *user, *pass;
  1287. char *ptr;
  1288. if (param == NULL || *param == '\0') {
  1289. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  1290. return;
  1291. }
  1292. if (!pooldetails(param, &url, &user, &pass)) {
  1293. ptr = escape_string(param, isjson);
  1294. strcpy(io_buffer, message(MSG_INVPDP, 0, ptr, isjson));
  1295. if (ptr != param)
  1296. free(ptr);
  1297. ptr = NULL;
  1298. return;
  1299. }
  1300. if (add_pool_details(true, url, user, pass) == ADD_POOL_MAXIMUM) {
  1301. strcpy(io_buffer, message(MSG_TOOMANYP, MAX_POOLS, NULL, isjson));
  1302. return;
  1303. }
  1304. ptr = escape_string(url, isjson);
  1305. strcpy(io_buffer, message(MSG_ADDPOOL, 0, ptr, isjson));
  1306. if (ptr != url)
  1307. free(ptr);
  1308. ptr = NULL;
  1309. }
  1310. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1311. {
  1312. struct pool *pool;
  1313. int id;
  1314. if (total_pools == 0) {
  1315. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1316. return;
  1317. }
  1318. if (param == NULL || *param == '\0') {
  1319. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1320. return;
  1321. }
  1322. id = atoi(param);
  1323. if (id < 0 || id >= total_pools) {
  1324. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1325. return;
  1326. }
  1327. pool = pools[id];
  1328. if (pool->enabled == true) {
  1329. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  1330. return;
  1331. }
  1332. pool->enabled = true;
  1333. if (pool->prio < current_pool()->prio)
  1334. switch_pools(pool);
  1335. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  1336. }
  1337. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1338. {
  1339. struct pool *pool;
  1340. int id;
  1341. if (total_pools == 0) {
  1342. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1343. return;
  1344. }
  1345. if (param == NULL || *param == '\0') {
  1346. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1347. return;
  1348. }
  1349. id = atoi(param);
  1350. if (id < 0 || id >= total_pools) {
  1351. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1352. return;
  1353. }
  1354. pool = pools[id];
  1355. if (pool->enabled == false) {
  1356. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  1357. return;
  1358. }
  1359. if (active_pools() <= 1) {
  1360. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  1361. return;
  1362. }
  1363. pool->enabled = false;
  1364. if (pool == current_pool())
  1365. switch_pools(NULL);
  1366. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  1367. }
  1368. static void removepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1369. {
  1370. struct pool *pool;
  1371. char *rpc_url;
  1372. bool dofree = false;
  1373. int id;
  1374. if (total_pools == 0) {
  1375. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1376. return;
  1377. }
  1378. if (param == NULL || *param == '\0') {
  1379. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1380. return;
  1381. }
  1382. id = atoi(param);
  1383. if (id < 0 || id >= total_pools) {
  1384. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1385. return;
  1386. }
  1387. if (total_pools <= 1) {
  1388. strcpy(io_buffer, message(MSG_REMLASTP, id, NULL, isjson));
  1389. return;
  1390. }
  1391. pool = pools[id];
  1392. if (pool == current_pool())
  1393. switch_pools(NULL);
  1394. if (pool == current_pool()) {
  1395. strcpy(io_buffer, message(MSG_ACTPOOL, id, NULL, isjson));
  1396. return;
  1397. }
  1398. pool->enabled = false;
  1399. rpc_url = escape_string(pool->rpc_url, isjson);
  1400. if (rpc_url != pool->rpc_url)
  1401. dofree = true;
  1402. remove_pool(pool);
  1403. strcpy(io_buffer, message(MSG_REMPOOL, id, rpc_url, isjson));
  1404. if (dofree)
  1405. free(rpc_url);
  1406. rpc_url = NULL;
  1407. }
  1408. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  1409. {
  1410. int id;
  1411. char *gpusep;
  1412. if (nDevs == 0) {
  1413. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1414. return false;
  1415. }
  1416. if (param == NULL || *param == '\0') {
  1417. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1418. return false;
  1419. }
  1420. gpusep = strchr(param, GPUSEP);
  1421. if (gpusep == NULL) {
  1422. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  1423. return false;
  1424. }
  1425. *(gpusep++) = '\0';
  1426. id = atoi(param);
  1427. if (id < 0 || id >= nDevs) {
  1428. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1429. return false;
  1430. }
  1431. *gpu = id;
  1432. *value = gpusep;
  1433. return true;
  1434. }
  1435. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1436. {
  1437. int id;
  1438. char *value;
  1439. int intensity;
  1440. char intensitystr[7];
  1441. if (!splitgpuvalue(param, &id, &value, isjson))
  1442. return;
  1443. if (!strncasecmp(value, DYNAMIC, 1)) {
  1444. gpus[id].dynamic = true;
  1445. strcpy(intensitystr, DYNAMIC);
  1446. }
  1447. else {
  1448. intensity = atoi(value);
  1449. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1450. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1451. return;
  1452. }
  1453. gpus[id].dynamic = false;
  1454. gpus[id].intensity = intensity;
  1455. sprintf(intensitystr, "%d", intensity);
  1456. }
  1457. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1458. }
  1459. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1460. {
  1461. #ifdef HAVE_ADL
  1462. int id;
  1463. char *value;
  1464. int clock;
  1465. if (!splitgpuvalue(param, &id, &value, isjson))
  1466. return;
  1467. clock = atoi(value);
  1468. if (set_memoryclock(id, clock))
  1469. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  1470. else
  1471. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  1472. #else
  1473. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1474. #endif
  1475. }
  1476. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1477. {
  1478. #ifdef HAVE_ADL
  1479. int id;
  1480. char *value;
  1481. int clock;
  1482. if (!splitgpuvalue(param, &id, &value, isjson))
  1483. return;
  1484. clock = atoi(value);
  1485. if (set_engineclock(id, clock))
  1486. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  1487. else
  1488. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  1489. #else
  1490. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1491. #endif
  1492. }
  1493. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1494. {
  1495. #ifdef HAVE_ADL
  1496. int id;
  1497. char *value;
  1498. int fan;
  1499. if (!splitgpuvalue(param, &id, &value, isjson))
  1500. return;
  1501. fan = atoi(value);
  1502. if (set_fanspeed(id, fan))
  1503. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  1504. else
  1505. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  1506. #else
  1507. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1508. #endif
  1509. }
  1510. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1511. {
  1512. #ifdef HAVE_ADL
  1513. int id;
  1514. char *value;
  1515. float vddc;
  1516. if (!splitgpuvalue(param, &id, &value, isjson))
  1517. return;
  1518. vddc = atof(value);
  1519. if (set_vddc(id, vddc))
  1520. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  1521. else
  1522. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  1523. #else
  1524. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1525. #endif
  1526. }
  1527. static void send_result(SOCKETTYPE c, bool isjson);
  1528. void doquit(SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1529. {
  1530. if (isjson)
  1531. strcpy(io_buffer, JSON_START JSON_BYE);
  1532. else
  1533. strcpy(io_buffer, _BYE);
  1534. send_result(c, isjson);
  1535. *io_buffer = '\0';
  1536. bye = 1;
  1537. PTH(&thr_info[my_thr_id]) = 0L;
  1538. kill_work();
  1539. }
  1540. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1541. {
  1542. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  1543. }
  1544. void notifystatus(int device, struct cgpu_info *cgpu, bool isjson)
  1545. {
  1546. char buf[BUFSIZ];
  1547. char *reason;
  1548. if (cgpu->device_last_not_well == 0)
  1549. reason = REASON_NONE;
  1550. else
  1551. switch(cgpu->device_not_well_reason) {
  1552. case REASON_THREAD_FAIL_INIT:
  1553. reason = REASON_THREAD_FAIL_INIT_STR;
  1554. break;
  1555. case REASON_THREAD_ZERO_HASH:
  1556. reason = REASON_THREAD_ZERO_HASH_STR;
  1557. break;
  1558. case REASON_THREAD_FAIL_QUEUE:
  1559. reason = REASON_THREAD_FAIL_QUEUE_STR;
  1560. break;
  1561. case REASON_DEV_SICK_IDLE_60:
  1562. reason = REASON_DEV_SICK_IDLE_60_STR;
  1563. break;
  1564. case REASON_DEV_DEAD_IDLE_600:
  1565. reason = REASON_DEV_DEAD_IDLE_600_STR;
  1566. break;
  1567. case REASON_DEV_NOSTART:
  1568. reason = REASON_DEV_NOSTART_STR;
  1569. break;
  1570. case REASON_DEV_OVER_HEAT:
  1571. reason = REASON_DEV_OVER_HEAT_STR;
  1572. break;
  1573. case REASON_DEV_THERMAL_CUTOFF:
  1574. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  1575. break;
  1576. default:
  1577. reason = REASON_UNKNOWN_STR;
  1578. break;
  1579. }
  1580. // ALL counters (and only counters) must start the name with a '*'
  1581. // Simplifies future external support for adding new counters
  1582. if (isjson)
  1583. sprintf(buf, "%s{\"NOTIFY\":%d,\"Name\":\"%s\",\"ID\":%d,\"Last Well\":%lu,\"Last Not Well\":%lu,\"Reason Not Well\":\"%s\",\"*Thread Fail Init\":%d,\"*Thread Zero Hash\":%d,\"*Thread Fail Queue\":%d,\"*Dev Sick Idle 60s\":%d,\"*Dev Dead Idle 600s\":%d,\"*Dev Nostart\":%d,\"*Dev Over Heat\":%d,\"*Dev Thermal Cutoff\":%d}" JSON_CLOSE,
  1584. device > 0 ? "," : "", device, cgpu->api->name, cgpu->device_id,
  1585. cgpu->device_last_well, cgpu->device_last_not_well, reason,
  1586. cgpu->thread_fail_init_count, cgpu->thread_zero_hash_count,
  1587. cgpu->thread_fail_queue_count, cgpu->dev_sick_idle_60_count,
  1588. cgpu->dev_dead_idle_600_count, cgpu->dev_nostart_count,
  1589. cgpu->dev_over_heat_count, cgpu->dev_thermal_cutoff_count);
  1590. else
  1591. sprintf(buf, "NOTIFY=%d,Name=%s,ID=%d,Last Well=%lu,Last Not Well=%lu,Reason Not Well=%s,*Thread Fail Init=%d,*Thread Zero Hash=%d,*Thread Fail Queue=%d,*Dev Sick Idle 60s=%d,*Dev Dead Idle 600s=%d,*Dev Nostart=%d,*Dev Over Heat=%d,*Dev Thermal Cutoff=%d%c",
  1592. device, cgpu->api->name, cgpu->device_id,
  1593. cgpu->device_last_well, cgpu->device_last_not_well, reason,
  1594. cgpu->thread_fail_init_count, cgpu->thread_zero_hash_count,
  1595. cgpu->thread_fail_queue_count, cgpu->dev_sick_idle_60_count,
  1596. cgpu->dev_dead_idle_600_count, cgpu->dev_nostart_count,
  1597. cgpu->dev_over_heat_count, cgpu->dev_thermal_cutoff_count, SEPARATOR);
  1598. strcat(io_buffer, buf);
  1599. }
  1600. static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1601. {
  1602. int i;
  1603. if (total_devices == 0) {
  1604. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1605. return;
  1606. }
  1607. strcpy(io_buffer, message(MSG_NOTIFY, 0, NULL, isjson));
  1608. if (isjson) {
  1609. strcat(io_buffer, COMMA);
  1610. strcat(io_buffer, JSON_NOTIFY);
  1611. }
  1612. for (i = 0; i < total_devices; i++)
  1613. notifystatus(i, devices[i], isjson);
  1614. if (isjson)
  1615. strcat(io_buffer, JSON_CLOSE);
  1616. }
  1617. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1618. {
  1619. FILE *fcfg;
  1620. char *ptr;
  1621. if (param == NULL || *param == '\0') {
  1622. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  1623. return;
  1624. }
  1625. fcfg = fopen(param, "w");
  1626. if (!fcfg) {
  1627. ptr = escape_string(param, isjson);
  1628. strcpy(io_buffer, message(MSG_BADFN, 0, ptr, isjson));
  1629. if (ptr != param)
  1630. free(ptr);
  1631. ptr = NULL;
  1632. return;
  1633. }
  1634. write_config(fcfg);
  1635. fclose(fcfg);
  1636. ptr = escape_string(param, isjson);
  1637. strcpy(io_buffer, message(MSG_SAVED, 0, ptr, isjson));
  1638. if (ptr != param)
  1639. free(ptr);
  1640. ptr = NULL;
  1641. }
  1642. struct CMDS {
  1643. char *name;
  1644. void (*func)(SOCKETTYPE, char *, bool);
  1645. bool requires_writemode;
  1646. } cmds[] = {
  1647. { "version", apiversion, false },
  1648. { "config", minerconfig, false },
  1649. { "devs", devstatus, false },
  1650. { "pools", poolstatus, false },
  1651. { "summary", summary, false },
  1652. { "gpuenable", gpuenable, true },
  1653. { "gpudisable", gpudisable, true },
  1654. { "gpurestart", gpurestart, true },
  1655. { "gpu", gpudev, false },
  1656. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  1657. { "pga", pgadev, false },
  1658. { "pgaenable", pgaenable, true },
  1659. { "pgadisable", pgadisable, true },
  1660. #endif
  1661. #ifdef WANT_CPUMINE
  1662. { "cpu", cpudev, false },
  1663. #endif
  1664. { "gpucount", gpucount, false },
  1665. { "pgacount", pgacount, false },
  1666. { "cpucount", cpucount, false },
  1667. { "switchpool", switchpool, true },
  1668. { "addpool", addpool, true },
  1669. { "enablepool", enablepool, true },
  1670. { "disablepool", disablepool, true },
  1671. { "removepool", removepool, true },
  1672. { "gpuintensity", gpuintensity, true },
  1673. { "gpumem", gpumem, true },
  1674. { "gpuengine", gpuengine, true },
  1675. { "gpufan", gpufan, true },
  1676. { "gpuvddc", gpuvddc, true },
  1677. { "save", dosave, true },
  1678. { "quit", doquit, true },
  1679. { "privileged", privileged, true },
  1680. { "notify", notify, false },
  1681. { NULL, NULL, false }
  1682. };
  1683. static void send_result(SOCKETTYPE c, bool isjson)
  1684. {
  1685. int n;
  1686. int len;
  1687. if (isjson)
  1688. strcat(io_buffer, JSON_END);
  1689. len = strlen(io_buffer);
  1690. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : "");
  1691. // ignore failure - it's closed immediately anyway
  1692. n = send(c, io_buffer, len+1, 0);
  1693. if (opt_debug) {
  1694. if (SOCKETFAIL(n))
  1695. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  1696. else
  1697. applog(LOG_DEBUG, "API: sent %d", n);
  1698. }
  1699. }
  1700. static void tidyup(__maybe_unused void *arg)
  1701. {
  1702. bye = 1;
  1703. if (sock != INVSOCK) {
  1704. shutdown(sock, SHUT_RDWR);
  1705. CLOSESOCKET(sock);
  1706. sock = INVSOCK;
  1707. }
  1708. if (ipaccess != NULL) {
  1709. free(ipaccess);
  1710. ipaccess = NULL;
  1711. }
  1712. if (msg_buffer != NULL) {
  1713. free(msg_buffer);
  1714. msg_buffer = NULL;
  1715. }
  1716. if (io_buffer != NULL) {
  1717. free(io_buffer);
  1718. io_buffer = NULL;
  1719. }
  1720. }
  1721. /*
  1722. * Interpret [R|W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  1723. * special case of 0/0 allows /0 (means all IP addresses)
  1724. */
  1725. #define ALLIP4 "0/0"
  1726. /*
  1727. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  1728. */
  1729. static void setup_ipaccess()
  1730. {
  1731. char *buf, *ptr, *comma, *slash, *dot;
  1732. int ipcount, mask, octet, i;
  1733. bool writemode;
  1734. buf = malloc(strlen(opt_api_allow) + 1);
  1735. if (unlikely(!buf))
  1736. quit(1, "Failed to malloc ipaccess buf");
  1737. strcpy(buf, opt_api_allow);
  1738. ipcount = 1;
  1739. ptr = buf;
  1740. while (*ptr)
  1741. if (*(ptr++) == ',')
  1742. ipcount++;
  1743. // possibly more than needed, but never less
  1744. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  1745. if (unlikely(!ipaccess))
  1746. quit(1, "Failed to calloc ipaccess");
  1747. ips = 0;
  1748. ptr = buf;
  1749. while (ptr && *ptr) {
  1750. while (*ptr == ' ' || *ptr == '\t')
  1751. ptr++;
  1752. if (*ptr == ',') {
  1753. ptr++;
  1754. continue;
  1755. }
  1756. comma = strchr(ptr, ',');
  1757. if (comma)
  1758. *(comma++) = '\0';
  1759. writemode = false;
  1760. if (isalpha(*ptr) && *(ptr+1) == ':') {
  1761. if (tolower(*ptr) == 'w')
  1762. writemode = true;
  1763. ptr += 2;
  1764. }
  1765. ipaccess[ips].writemode = writemode;
  1766. if (strcmp(ptr, ALLIP4) == 0)
  1767. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  1768. else {
  1769. slash = strchr(ptr, '/');
  1770. if (!slash)
  1771. ipaccess[ips].mask = 0xffffffff;
  1772. else {
  1773. *(slash++) = '\0';
  1774. mask = atoi(slash);
  1775. if (mask < 1 || mask > 32)
  1776. goto popipo; // skip invalid/zero
  1777. ipaccess[ips].mask = 0;
  1778. while (mask-- >= 0) {
  1779. octet = 1 << (mask % 8);
  1780. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  1781. }
  1782. }
  1783. ipaccess[ips].ip = 0; // missing default to '.0'
  1784. for (i = 0; ptr && (i < 4); i++) {
  1785. dot = strchr(ptr, '.');
  1786. if (dot)
  1787. *(dot++) = '\0';
  1788. octet = atoi(ptr);
  1789. if (octet < 0 || octet > 0xff)
  1790. goto popipo; // skip invalid
  1791. ipaccess[ips].ip |= (octet << (i * 8));
  1792. ptr = dot;
  1793. }
  1794. ipaccess[ips].ip &= ipaccess[ips].mask;
  1795. }
  1796. ips++;
  1797. popipo:
  1798. ptr = comma;
  1799. }
  1800. free(buf);
  1801. }
  1802. void api(int api_thr_id)
  1803. {
  1804. char buf[BUFSIZ];
  1805. char param_buf[BUFSIZ];
  1806. const char *localaddr = "127.0.0.1";
  1807. SOCKETTYPE c;
  1808. int n, bound;
  1809. char *connectaddr;
  1810. char *binderror;
  1811. time_t bindstart;
  1812. short int port = opt_api_port;
  1813. struct sockaddr_in serv;
  1814. struct sockaddr_in cli;
  1815. socklen_t clisiz;
  1816. char *cmd;
  1817. char *param;
  1818. bool addrok;
  1819. bool writemode;
  1820. json_error_t json_err;
  1821. json_t *json_config;
  1822. json_t *json_val;
  1823. bool isjson;
  1824. bool did;
  1825. int i;
  1826. pthread_cleanup_push(tidyup, NULL);
  1827. my_thr_id = api_thr_id;
  1828. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1829. sleep(opt_log_interval);
  1830. if (!opt_api_listen) {
  1831. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1832. return;
  1833. }
  1834. if (opt_api_allow) {
  1835. setup_ipaccess();
  1836. if (ips == 0) {
  1837. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  1838. return;
  1839. }
  1840. }
  1841. sock = socket(AF_INET, SOCK_STREAM, 0);
  1842. if (sock == INVSOCK) {
  1843. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1844. return;
  1845. }
  1846. memset(&serv, 0, sizeof(serv));
  1847. serv.sin_family = AF_INET;
  1848. if (!opt_api_allow && !opt_api_network) {
  1849. serv.sin_addr.s_addr = inet_addr(localaddr);
  1850. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  1851. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1852. return;
  1853. }
  1854. }
  1855. serv.sin_port = htons(port);
  1856. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1857. bound = 0;
  1858. bindstart = time(NULL);
  1859. while (bound == 0) {
  1860. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1861. binderror = SOCKERRMSG;
  1862. if ((time(NULL) - bindstart) > 61)
  1863. break;
  1864. else {
  1865. applog(LOG_WARNING, "API bind to port %d failed - trying again in 15sec", port);
  1866. sleep(15);
  1867. }
  1868. }
  1869. else
  1870. bound = 1;
  1871. }
  1872. if (bound == 0) {
  1873. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1874. return;
  1875. }
  1876. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1877. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1878. CLOSESOCKET(sock);
  1879. return;
  1880. }
  1881. if (opt_api_allow)
  1882. applog(LOG_WARNING, "API running in IP access mode");
  1883. else {
  1884. if (opt_api_network)
  1885. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1886. else
  1887. applog(LOG_WARNING, "API running in local access mode");
  1888. }
  1889. io_buffer = malloc(MYBUFSIZ+1);
  1890. msg_buffer = malloc(MYBUFSIZ+1);
  1891. while (bye == 0) {
  1892. clisiz = sizeof(cli);
  1893. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1894. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1895. goto die;
  1896. }
  1897. connectaddr = inet_ntoa(cli.sin_addr);
  1898. addrok = false;
  1899. writemode = false;
  1900. if (opt_api_allow) {
  1901. for (i = 0; i < ips; i++) {
  1902. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  1903. addrok = true;
  1904. writemode = ipaccess[i].writemode;
  1905. break;
  1906. }
  1907. }
  1908. } else {
  1909. if (opt_api_network)
  1910. addrok = true;
  1911. else
  1912. addrok = (strcmp(connectaddr, localaddr) == 0);
  1913. }
  1914. if (opt_debug)
  1915. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1916. if (addrok) {
  1917. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1918. if (SOCKETFAIL(n))
  1919. buf[0] = '\0';
  1920. else
  1921. buf[n] = '\0';
  1922. if (opt_debug) {
  1923. if (SOCKETFAIL(n))
  1924. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  1925. else
  1926. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  1927. }
  1928. if (!SOCKETFAIL(n)) {
  1929. // the time of the request in now
  1930. when = time(NULL);
  1931. did = false;
  1932. if (*buf != ISJSON) {
  1933. isjson = false;
  1934. param = strchr(buf, SEPARATOR);
  1935. if (param != NULL)
  1936. *(param++) = '\0';
  1937. cmd = buf;
  1938. }
  1939. else {
  1940. isjson = true;
  1941. param = NULL;
  1942. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  1943. json_config = json_loadb(buf, n, 0, &json_err);
  1944. #elif JANSSON_MAJOR_VERSION > 1
  1945. json_config = json_loads(buf, 0, &json_err);
  1946. #else
  1947. json_config = json_loads(buf, &json_err);
  1948. #endif
  1949. if (!json_is_object(json_config)) {
  1950. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1951. send_result(c, isjson);
  1952. did = true;
  1953. }
  1954. else {
  1955. json_val = json_object_get(json_config, JSON_COMMAND);
  1956. if (json_val == NULL) {
  1957. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1958. send_result(c, isjson);
  1959. did = true;
  1960. }
  1961. else {
  1962. if (!json_is_string(json_val)) {
  1963. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1964. send_result(c, isjson);
  1965. did = true;
  1966. }
  1967. else {
  1968. cmd = (char *)json_string_value(json_val);
  1969. json_val = json_object_get(json_config, JSON_PARAMETER);
  1970. if (json_is_string(json_val))
  1971. param = (char *)json_string_value(json_val);
  1972. else if (json_is_integer(json_val)) {
  1973. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1974. param = param_buf;
  1975. } else if (json_is_real(json_val)) {
  1976. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1977. param = param_buf;
  1978. }
  1979. }
  1980. }
  1981. }
  1982. }
  1983. if (!did)
  1984. for (i = 0; cmds[i].name != NULL; i++) {
  1985. if (strcmp(cmd, cmds[i].name) == 0) {
  1986. if (cmds[i].requires_writemode && !writemode) {
  1987. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  1988. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  1989. }
  1990. else
  1991. (cmds[i].func)(c, param, isjson);
  1992. send_result(c, isjson);
  1993. did = true;
  1994. break;
  1995. }
  1996. }
  1997. if (!did) {
  1998. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1999. send_result(c, isjson);
  2000. }
  2001. }
  2002. }
  2003. CLOSESOCKET(c);
  2004. }
  2005. die:
  2006. pthread_cleanup_pop(true);
  2007. }