api.c 78 KB

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