api.c 106 KB

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