api.c 59 KB

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