api.c 123 KB

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