| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079 |
- /*
- * Copyright 2011-2013 Andrew Smith
- * Copyright 2011-2012 Con Kolivas
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version. See COPYING for more details.
- *
- * Note: the code always includes GPU support even if there are no GPUs
- * this simplifies handling multiple other device code being included
- * depending on compile options
- */
- #define _MEMORY_DEBUG_MASTER 1
- #include "config.h"
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include "compat.h"
- #include "miner.h"
- #include "util.h"
- #include "driver-cpu.h" /* for algo_names[], TODO: re-factor dependency */
- #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX) || defined(USE_MODMINER)
- #define HAVE_AN_FPGA 1
- #endif
- // Big enough for largest API request
- // though a PC with 100s of PGAs/CPUs may exceed the size ...
- // data is truncated at the end of the last record that fits
- // but still closed correctly for JSON
- // Current code assumes it can socket send this size + JSON_CLOSE + JSON_END
- #define SOCKBUFSIZ 65432
- // BUFSIZ varies on Windows and Linux
- #define TMPBUFSIZ 8192
- // Number of requests to queue - normally would be small
- // However lots of PGA's may mean more
- #define QUEUE 100
- #if defined WIN32
- static char WSAbuf[1024];
- struct WSAERRORS {
- int id;
- char *code;
- } WSAErrors[] = {
- { 0, "No error" },
- { WSAEINTR, "Interrupted system call" },
- { WSAEBADF, "Bad file number" },
- { WSAEACCES, "Permission denied" },
- { WSAEFAULT, "Bad address" },
- { WSAEINVAL, "Invalid argument" },
- { WSAEMFILE, "Too many open sockets" },
- { WSAEWOULDBLOCK, "Operation would block" },
- { WSAEINPROGRESS, "Operation now in progress" },
- { WSAEALREADY, "Operation already in progress" },
- { WSAENOTSOCK, "Socket operation on non-socket" },
- { WSAEDESTADDRREQ, "Destination address required" },
- { WSAEMSGSIZE, "Message too long" },
- { WSAEPROTOTYPE, "Protocol wrong type for socket" },
- { WSAENOPROTOOPT, "Bad protocol option" },
- { WSAEPROTONOSUPPORT, "Protocol not supported" },
- { WSAESOCKTNOSUPPORT, "Socket type not supported" },
- { WSAEOPNOTSUPP, "Operation not supported on socket" },
- { WSAEPFNOSUPPORT, "Protocol family not supported" },
- { WSAEAFNOSUPPORT, "Address family not supported" },
- { WSAEADDRINUSE, "Address already in use" },
- { WSAEADDRNOTAVAIL, "Can't assign requested address" },
- { WSAENETDOWN, "Network is down" },
- { WSAENETUNREACH, "Network is unreachable" },
- { WSAENETRESET, "Net connection reset" },
- { WSAECONNABORTED, "Software caused connection abort" },
- { WSAECONNRESET, "Connection reset by peer" },
- { WSAENOBUFS, "No buffer space available" },
- { WSAEISCONN, "Socket is already connected" },
- { WSAENOTCONN, "Socket is not connected" },
- { WSAESHUTDOWN, "Can't send after socket shutdown" },
- { WSAETOOMANYREFS, "Too many references, can't splice" },
- { WSAETIMEDOUT, "Connection timed out" },
- { WSAECONNREFUSED, "Connection refused" },
- { WSAELOOP, "Too many levels of symbolic links" },
- { WSAENAMETOOLONG, "File name too long" },
- { WSAEHOSTDOWN, "Host is down" },
- { WSAEHOSTUNREACH, "No route to host" },
- { WSAENOTEMPTY, "Directory not empty" },
- { WSAEPROCLIM, "Too many processes" },
- { WSAEUSERS, "Too many users" },
- { WSAEDQUOT, "Disc quota exceeded" },
- { WSAESTALE, "Stale NFS file handle" },
- { WSAEREMOTE, "Too many levels of remote in path" },
- { WSASYSNOTREADY, "Network system is unavailable" },
- { WSAVERNOTSUPPORTED, "Winsock version out of range" },
- { WSANOTINITIALISED, "WSAStartup not yet called" },
- { WSAEDISCON, "Graceful shutdown in progress" },
- { WSAHOST_NOT_FOUND, "Host not found" },
- { WSANO_DATA, "No host data of that type was found" },
- { -1, "Unknown error code" }
- };
- char *WSAErrorMsg(void) {
- int i;
- int id = WSAGetLastError();
- /* Assume none of them are actually -1 */
- for (i = 0; WSAErrors[i].id != -1; i++)
- if (WSAErrors[i].id == id)
- break;
- sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
- return &(WSAbuf[0]);
- }
- #endif
- static SOCKETTYPE sock = INVSOCK;
- static const char *UNAVAILABLE = " - API will not be available";
- static const char *INVAPIGROUPS = "Invalid --api-groups parameter";
- static const char *BLANK = "";
- static const char *COMMA = ",";
- #define COMSTR ","
- static const char SEPARATOR = '|';
- #define SEPSTR "|"
- static const char GPUSEP = ',';
- static const char *APIVERSION = "1.25";
- static const char *DEAD = "Dead";
- #if defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA)
- static const char *SICK = "Sick";
- static const char *NOSTART = "NoStart";
- static const char *INIT = "Initialising";
- #endif
- static const char *DISABLED = "Disabled";
- static const char *ALIVE = "Alive";
- static const char *REJECTING = "Rejecting";
- static const char *UNKNOWN = "Unknown";
- #define _DYNAMIC "D"
- #ifdef HAVE_OPENCL
- static const char *DYNAMIC = _DYNAMIC;
- #endif
- static __maybe_unused const char *NONE = "None";
- static const char *YES = "Y";
- static const char *NO = "N";
- static const char *NULLSTR = "(null)";
- static const char *TRUESTR = "true";
- static const char *FALSESTR = "false";
- #ifdef USE_SCRYPT
- static const char *SCRYPTSTR = "scrypt";
- #endif
- static const char *SHA256STR = "sha256";
- static const char *DEVICECODE = ""
- #ifdef HAVE_OPENCL
- "GPU "
- #endif
- #ifdef USE_BITFORCE
- "BFL "
- #endif
- #ifdef USE_ICARUS
- "ICA "
- #endif
- #ifdef USE_ZTEX
- "ZTX "
- #endif
- #ifdef USE_MODMINER
- "MMQ "
- #endif
- #ifdef WANT_CPUMINE
- "CPU "
- #endif
- "";
- static const char *OSINFO =
- #if defined(__linux)
- "Linux";
- #else
- #if defined(__APPLE__)
- "Apple";
- #else
- #if defined (WIN32)
- "Windows";
- #else
- #if defined(unix)
- "Unix";
- #else
- "Unknown";
- #endif
- #endif
- #endif
- #endif
- #define _DEVS "DEVS"
- #define _POOLS "POOLS"
- #define _SUMMARY "SUMMARY"
- #define _STATUS "STATUS"
- #define _VERSION "VERSION"
- #define _MINECONFIG "CONFIG"
- #define _GPU "GPU"
- #ifdef HAVE_AN_FPGA
- #define _PGA "PGA"
- #endif
- #ifdef WANT_CPUMINE
- #define _CPU "CPU"
- #endif
- #define _GPUS "GPUS"
- #define _PGAS "PGAS"
- #define _CPUS "CPUS"
- #define _NOTIFY "NOTIFY"
- #define _DEVDETAILS "DEVDETAILS"
- #define _BYE "BYE"
- #define _RESTART "RESTART"
- #define _MINESTATS "STATS"
- #define _CHECK "CHECK"
- #define _MINECOIN "COIN"
- #define _DEBUGSET "DEBUG"
- #define _SETCONFIG "SETCONFIG"
- #define _USBSTATS "USBSTATS"
- static const char ISJSON = '{';
- #define JSON0 "{"
- #define JSON1 "\""
- #define JSON2 "\":["
- #define JSON3 "]"
- #define JSON4 ",\"id\":1"
- // If anyone cares, id=0 for truncated output
- #define JSON4_TRUNCATED ",\"id\":0"
- #define JSON5 "}"
- #define JSON_START JSON0
- #define JSON_DEVS JSON1 _DEVS JSON2
- #define JSON_POOLS JSON1 _POOLS JSON2
- #define JSON_SUMMARY JSON1 _SUMMARY JSON2
- #define JSON_STATUS JSON1 _STATUS JSON2
- #define JSON_VERSION JSON1 _VERSION JSON2
- #define JSON_MINECONFIG JSON1 _MINECONFIG JSON2
- #define JSON_GPU JSON1 _GPU JSON2
- #ifdef HAVE_AN_FPGA
- #define JSON_PGA JSON1 _PGA JSON2
- #endif
- #ifdef WANT_CPUMINE
- #define JSON_CPU JSON1 _CPU JSON2
- #endif
- #define JSON_GPUS JSON1 _GPUS JSON2
- #define JSON_PGAS JSON1 _PGAS JSON2
- #define JSON_CPUS JSON1 _CPUS JSON2
- #define JSON_NOTIFY JSON1 _NOTIFY JSON2
- #define JSON_DEVDETAILS JSON1 _DEVDETAILS JSON2
- #define JSON_BYE JSON1 _BYE JSON1
- #define JSON_RESTART JSON1 _RESTART JSON1
- #define JSON_CLOSE JSON3
- #define JSON_MINESTATS JSON1 _MINESTATS JSON2
- #define JSON_CHECK JSON1 _CHECK JSON2
- #define JSON_MINECOIN JSON1 _MINECOIN JSON2
- #define JSON_DEBUGSET JSON1 _DEBUGSET JSON2
- #define JSON_SETCONFIG JSON1 _SETCONFIG JSON2
- #define JSON_USBSTATS JSON1 _USBSTATS JSON2
- #define JSON_END JSON4 JSON5
- #define JSON_END_TRUNCATED JSON4_TRUNCATED JSON5
- static const char *JSON_COMMAND = "command";
- static const char *JSON_PARAMETER = "parameter";
- #define MSG_INVGPU 1
- #define MSG_ALRENA 2
- #define MSG_ALRDIS 3
- #define MSG_GPUMRE 4
- #define MSG_GPUREN 5
- #define MSG_GPUNON 6
- #define MSG_POOL 7
- #define MSG_NOPOOL 8
- #define MSG_DEVS 9
- #define MSG_NODEVS 10
- #define MSG_SUMM 11
- #define MSG_GPUDIS 12
- #define MSG_GPUREI 13
- #define MSG_INVCMD 14
- #define MSG_MISID 15
- #define MSG_GPUDEV 17
- #ifdef WANT_CPUMINE
- #define MSG_CPUNON 16
- #define MSG_CPUDEV 18
- #define MSG_INVCPU 19
- #endif
- #define MSG_NUMGPU 20
- #define MSG_NUMCPU 21
- #define MSG_VERSION 22
- #define MSG_INVJSON 23
- #define MSG_MISCMD 24
- #define MSG_MISPID 25
- #define MSG_INVPID 26
- #define MSG_SWITCHP 27
- #define MSG_MISVAL 28
- #define MSG_NOADL 29
- #define MSG_NOGPUADL 30
- #define MSG_INVINT 31
- #define MSG_GPUINT 32
- #define MSG_MINECONFIG 33
- #define MSG_GPUMERR 34
- #define MSG_GPUMEM 35
- #define MSG_GPUEERR 36
- #define MSG_GPUENG 37
- #define MSG_GPUVERR 38
- #define MSG_GPUVDDC 39
- #define MSG_GPUFERR 40
- #define MSG_GPUFAN 41
- #define MSG_MISFN 42
- #define MSG_BADFN 43
- #define MSG_SAVED 44
- #define MSG_ACCDENY 45
- #define MSG_ACCOK 46
- #define MSG_ENAPOOL 47
- #define MSG_DISPOOL 48
- #define MSG_ALRENAP 49
- #define MSG_ALRDISP 50
- #define MSG_DISLASTP 51
- #define MSG_MISPDP 52
- #define MSG_INVPDP 53
- #define MSG_TOOMANYP 54
- #define MSG_ADDPOOL 55
- #ifdef HAVE_AN_FPGA
- #define MSG_PGANON 56
- #define MSG_PGADEV 57
- #define MSG_INVPGA 58
- #endif
- #define MSG_NUMPGA 59
- #define MSG_NOTIFY 60
- #ifdef HAVE_AN_FPGA
- #define MSG_PGALRENA 61
- #define MSG_PGALRDIS 62
- #define MSG_PGAENA 63
- #define MSG_PGADIS 64
- #define MSG_PGAUNW 65
- #endif
- #define MSG_REMLASTP 66
- #define MSG_ACTPOOL 67
- #define MSG_REMPOOL 68
- #define MSG_DEVDETAILS 69
- #define MSG_MINESTATS 70
- #define MSG_MISCHK 71
- #define MSG_CHECK 72
- #define MSG_POOLPRIO 73
- #define MSG_DUPPID 74
- #define MSG_MISBOOL 75
- #define MSG_INVBOOL 76
- #define MSG_FOO 77
- #define MSG_MINECOIN 78
- #define MSG_DEBUGSET 79
- #define MSG_PGAIDENT 80
- #define MSG_PGANOID 81
- #define MSG_SETCONFIG 82
- #define MSG_UNKCON 83
- #define MSG_INVNUM 84
- #define MSG_CONPAR 85
- #define MSG_CONVAL 86
- #define MSG_USBSTA 87
- #define MSG_NOUSTA 88
- #ifdef HAVE_AN_FPGA
- #define MSG_MISPGAOPT 89
- #define MSG_PGANOSET 90
- #define MSG_PGAHELP 91
- #define MSG_PGASETOK 92
- #define MSG_PGASETERR 93
- #endif
- #define MSG_ZERMIS 94
- #define MSG_ZERINV 95
- #define MSG_ZERSUM 96
- #define MSG_ZERNOSUM 97
- #define MSG_USBNODEV 98
- #define MSG_INVHPLG 99
- #define MSG_HOTPLUG 100
- #define MSG_DISHPLG 101
- #define MSG_NOHPLG 102
- #define MSG_MISHPLG 103
- enum code_severity {
- SEVERITY_ERR,
- SEVERITY_WARN,
- SEVERITY_INFO,
- SEVERITY_SUCC,
- SEVERITY_FAIL
- };
- enum code_parameters {
- PARAM_GPU,
- PARAM_PGA,
- PARAM_CPU,
- PARAM_PID,
- PARAM_GPUMAX,
- PARAM_PGAMAX,
- PARAM_CPUMAX,
- PARAM_PMAX,
- PARAM_POOLMAX,
- // Single generic case: have the code resolve it - see below
- PARAM_DMAX,
- PARAM_CMD,
- PARAM_POOL,
- PARAM_STR,
- PARAM_BOTH,
- PARAM_BOOL,
- PARAM_SET,
- PARAM_INT,
- PARAM_NONE
- };
- struct CODES {
- const enum code_severity severity;
- const int code;
- const enum code_parameters params;
- const char *description;
- } codes[] = {
- #ifdef HAVE_OPENCL
- { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
- { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
- { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
- { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
- { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
- #endif
- { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
- { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
- { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
- { SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX,
- #ifdef HAVE_OPENCL
- "%d GPU(s)"
- #endif
- #if defined(HAVE_AN_FPGA) && defined(HAVE_OPENCL)
- " - "
- #endif
- #ifdef HAVE_AN_FPGA
- "%d PGA(s)"
- #endif
- #if defined(WANT_CPUMINE) && (defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA))
- " - "
- #endif
- #ifdef WANT_CPUMINE
- "%d CPU(s)"
- #endif
- },
- { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
- #ifdef HAVE_AN_FPGA
- "/PGAs"
- #endif
- #ifdef WANT_CPUMINE
- "/CPUs"
- #endif
- },
- { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
- #ifdef HAVE_OPENCL
- { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
- { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
- #endif
- { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
- { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
- #ifdef HAVE_OPENCL
- { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
- #endif
- #ifdef HAVE_AN_FPGA
- { SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
- { SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
- { SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
- { SEVERITY_INFO, MSG_PGALRENA,PARAM_PGA, "PGA %d already enabled" },
- { SEVERITY_INFO, MSG_PGALRDIS,PARAM_PGA, "PGA %d already disabled" },
- { SEVERITY_INFO, MSG_PGAENA, PARAM_PGA, "PGA %d sent enable message" },
- { SEVERITY_INFO, MSG_PGADIS, PARAM_PGA, "PGA %d set disable flag" },
- { SEVERITY_ERR, MSG_PGAUNW, PARAM_PGA, "PGA %d is not flagged WELL, cannot enable" },
- #endif
- #ifdef WANT_CPUMINE
- { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
- { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
- { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
- #endif
- { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
- { SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
- { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
- { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "CGMiner versions" },
- { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
- { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
- { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
- { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
- { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
- { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
- { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
- { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
- { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
- { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
- { SEVERITY_SUCC, MSG_MINECONFIG,PARAM_NONE, "CGMiner config" },
- #ifdef HAVE_OPENCL
- { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
- { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported success" },
- { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
- { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported success" },
- { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
- { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported success" },
- { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
- { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported success" },
- #endif
- { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
- { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
- { SEVERITY_SUCC, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
- { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
- { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
- { SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
- { SEVERITY_SUCC, MSG_POOLPRIO,PARAM_NONE, "Changed pool priorities" },
- { SEVERITY_ERR, MSG_DUPPID, PARAM_PID, "Duplicate pool specified %d" },
- { SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
- { SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
- { SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
- { SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
- { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
- { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
- { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
- { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
- { SEVERITY_ERR, MSG_REMLASTP,PARAM_POOL, "Cannot remove last pool %d:'%s'" },
- { SEVERITY_ERR, MSG_ACTPOOL, PARAM_POOL, "Cannot remove active pool %d:'%s'" },
- { SEVERITY_SUCC, MSG_REMPOOL, PARAM_BOTH, "Removed pool %d:'%s'" },
- { SEVERITY_SUCC, MSG_NOTIFY, PARAM_NONE, "Notify" },
- { SEVERITY_SUCC, MSG_DEVDETAILS,PARAM_NONE, "Device Details" },
- { SEVERITY_SUCC, MSG_MINESTATS,PARAM_NONE, "CGMiner stats" },
- { SEVERITY_ERR, MSG_MISCHK, PARAM_NONE, "Missing check cmd" },
- { SEVERITY_SUCC, MSG_CHECK, PARAM_NONE, "Check command" },
- { SEVERITY_ERR, MSG_MISBOOL, PARAM_NONE, "Missing parameter: true/false" },
- { SEVERITY_ERR, MSG_INVBOOL, PARAM_NONE, "Invalid parameter should be true or false" },
- { SEVERITY_SUCC, MSG_FOO, PARAM_BOOL, "Failover-Only set to %s" },
- { SEVERITY_SUCC, MSG_MINECOIN,PARAM_NONE, "CGMiner coin" },
- { SEVERITY_SUCC, MSG_DEBUGSET,PARAM_NONE, "Debug settings" },
- #ifdef HAVE_AN_FPGA
- { SEVERITY_SUCC, MSG_PGAIDENT,PARAM_PGA, "Identify command sent to PGA%d" },
- { SEVERITY_WARN, MSG_PGANOID, PARAM_PGA, "PGA%d does not support identify" },
- #endif
- { SEVERITY_SUCC, MSG_SETCONFIG,PARAM_SET, "Set config '%s' to %d" },
- { SEVERITY_ERR, MSG_UNKCON, PARAM_STR, "Unknown config '%s'" },
- { SEVERITY_ERR, MSG_INVNUM, PARAM_BOTH, "Invalid number (%d) for '%s' range is 0-9999" },
- { SEVERITY_ERR, MSG_CONPAR, PARAM_NONE, "Missing config parameters 'name,N'" },
- { SEVERITY_ERR, MSG_CONVAL, PARAM_STR, "Missing config value N for '%s,N'" },
- { SEVERITY_SUCC, MSG_USBSTA, PARAM_NONE, "USB Statistics" },
- { SEVERITY_INFO, MSG_NOUSTA, PARAM_NONE, "No USB Statistics" },
- #ifdef HAVE_AN_FPGA
- { SEVERITY_ERR, MSG_MISPGAOPT, PARAM_NONE, "Missing option after PGA number" },
- { SEVERITY_WARN, MSG_PGANOSET, PARAM_PGA, "PGA %d does not support pgaset" },
- { SEVERITY_INFO, MSG_PGAHELP, PARAM_BOTH, "PGA %d set help: %s" },
- { SEVERITY_SUCC, MSG_PGASETOK, PARAM_BOTH, "PGA %d set OK" },
- { SEVERITY_ERR, MSG_PGASETERR, PARAM_BOTH, "PGA %d set failed: %s" },
- #endif
- { SEVERITY_ERR, MSG_ZERMIS, PARAM_NONE, "Missing zero parameters" },
- { SEVERITY_ERR, MSG_ZERINV, PARAM_STR, "Invalid zero parameter '%s'" },
- { SEVERITY_SUCC, MSG_ZERSUM, PARAM_STR, "Zeroed %s stats with summary" },
- { SEVERITY_SUCC, MSG_ZERNOSUM, PARAM_STR, "Zeroed %s stats without summary" },
- #ifdef USE_USBUTILS
- { SEVERITY_ERR, MSG_USBNODEV, PARAM_PGA, "PGA%d has no device" },
- #endif
- { SEVERITY_ERR, MSG_INVHPLG, PARAM_STR, "Invalid value for hotplug (%s) must be 0..9999" },
- { SEVERITY_SUCC, MSG_HOTPLUG, PARAM_INT, "Hotplug check set to %ds" },
- { SEVERITY_SUCC, MSG_DISHPLG, PARAM_NONE, "Hotplug disabled" },
- { SEVERITY_WARN, MSG_NOHPLG, PARAM_NONE, "Hotplug is not available" },
- { SEVERITY_ERR, MSG_MISHPLG, PARAM_NONE, "Missing hotplug parameter" },
- { SEVERITY_FAIL, 0, 0, NULL }
- };
- static int my_thr_id = 0;
- static bool bye;
- #if defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA)
- static bool ping = true;
- #endif
- // Used to control quit restart access to shutdown variables
- static pthread_mutex_t quit_restart_lock;
- static bool do_a_quit;
- static bool do_a_restart;
- static time_t when = 0; // when the request occurred
- struct IP4ACCESS {
- in_addr_t ip;
- in_addr_t mask;
- char group;
- };
- #define GROUP(g) (toupper(g))
- #define PRIVGROUP GROUP('W')
- #define NOPRIVGROUP GROUP('R')
- #define ISPRIVGROUP(g) (GROUP(g) == PRIVGROUP)
- #define GROUPOFFSET(g) (GROUP(g) - GROUP('A'))
- #define VALIDGROUP(g) (GROUP(g) >= GROUP('A') && GROUP(g) <= GROUP('Z'))
- #define COMMANDS(g) (apigroups[GROUPOFFSET(g)].commands)
- #define DEFINEDGROUP(g) (ISPRIVGROUP(g) || COMMANDS(g) != NULL)
- struct APIGROUPS {
- // This becomes a string like: "|cmd1|cmd2|cmd3|" so it's quick to search
- char *commands;
- } apigroups['Z' - 'A' + 1]; // only A=0 to Z=25 (R: noprivs, W: allprivs)
- static struct IP4ACCESS *ipaccess = NULL;
- static int ips = 0;
- struct io_data {
- size_t siz;
- char *ptr;
- char *cur;
- bool sock;
- bool full;
- bool close;
- };
- struct io_list {
- struct io_data *io_data;
- struct io_list *prev;
- struct io_list *next;
- };
- static struct io_list *io_head = NULL;
- #define io_new(init) _io_new(init, false)
- #define sock_io_new() _io_new(SOCKBUFSIZ, true)
- static void io_reinit(struct io_data *io_data)
- {
- io_data->cur = io_data->ptr;
- *(io_data->ptr) = '\0';
- io_data->full = false;
- io_data->close = false;
- }
- static struct io_data *_io_new(size_t initial, bool socket_buf)
- {
- struct io_data *io_data;
- struct io_list *io_list;
- io_data = malloc(sizeof(*io_data));
- io_data->ptr = malloc(initial);
- io_data->siz = initial;
- io_data->sock = socket_buf;
- io_reinit(io_data);
- io_list = malloc(sizeof(*io_list));
- io_list->io_data = io_data;
- if (io_head) {
- io_list->next = io_head;
- io_list->prev = io_head->prev;
- io_list->next->prev = io_list;
- io_list->prev->next = io_list;
- } else {
- io_list->prev = io_list;
- io_list->next = io_list;
- io_head = io_list;
- }
- return io_data;
- }
- static bool io_add(struct io_data *io_data, char *buf)
- {
- size_t len, dif, tot;
- if (io_data->full)
- return false;
- len = strlen(buf);
- dif = io_data->cur - io_data->ptr;
- tot = len + 1 + dif;
- if (tot > io_data->siz) {
- size_t new = io_data->siz * 2;
- if (new < tot)
- new = tot * 2;
- if (io_data->sock) {
- if (new > SOCKBUFSIZ) {
- if (tot > SOCKBUFSIZ) {
- io_data->full = true;
- return false;
- }
- new = SOCKBUFSIZ;
- }
- }
- io_data->ptr = realloc(io_data->ptr, new);
- io_data->cur = io_data->ptr + dif;
- io_data->siz = new;
- }
- memcpy(io_data->cur, buf, len + 1);
- io_data->cur += len;
- return true;
- }
- static bool io_put(struct io_data *io_data, char *buf)
- {
- io_reinit(io_data);
- return io_add(io_data, buf);
- }
- static void io_close(struct io_data *io_data)
- {
- io_data->close = true;
- }
- static void io_free()
- {
- struct io_list *io_list, *io_next;
- if (io_head) {
- io_list = io_head;
- do {
- io_next = io_list->next;
- free(io_list->io_data);
- free(io_list);
- io_list = io_next;
- } while (io_list != io_head);
- io_head = NULL;
- }
- }
- // This is only called when expected to be needed (rarely)
- // i.e. strings outside of the codes control (input from the user)
- static char *escape_string(char *str, bool isjson)
- {
- char *buf, *ptr;
- int count;
- count = 0;
- for (ptr = str; *ptr; ptr++) {
- switch (*ptr) {
- case ',':
- case '|':
- case '=':
- if (!isjson)
- count++;
- break;
- case '"':
- if (isjson)
- count++;
- break;
- case '\\':
- count++;
- break;
- }
- }
- if (count == 0)
- return str;
- buf = malloc(strlen(str) + count + 1);
- if (unlikely(!buf))
- quit(1, "Failed to malloc escape buf");
- ptr = buf;
- while (*str)
- switch (*str) {
- case ',':
- case '|':
- case '=':
- if (!isjson)
- *(ptr++) = '\\';
- *(ptr++) = *(str++);
- break;
- case '"':
- if (isjson)
- *(ptr++) = '\\';
- *(ptr++) = *(str++);
- break;
- case '\\':
- *(ptr++) = '\\';
- *(ptr++) = *(str++);
- break;
- default:
- *(ptr++) = *(str++);
- break;
- }
- *ptr = '\0';
- return buf;
- }
- static struct api_data *api_add_extra(struct api_data *root, struct api_data *extra)
- {
- struct api_data *tmp;
- if (root) {
- if (extra) {
- // extra tail
- tmp = extra->prev;
- // extra prev = root tail
- extra->prev = root->prev;
- // root tail next = extra
- root->prev->next = extra;
- // extra tail next = root
- tmp->next = root;
- // root prev = extra tail
- root->prev = tmp;
- }
- } else
- root = extra;
- return root;
- }
- static struct api_data *api_add_data_full(struct api_data *root, char *name, enum api_data_type type, void *data, bool copy_data)
- {
- struct api_data *api_data;
- api_data = (struct api_data *)malloc(sizeof(struct api_data));
- api_data->name = name;
- api_data->type = type;
- if (root == NULL) {
- root = api_data;
- root->prev = root;
- root->next = root;
- }
- else {
- api_data->prev = root->prev;
- root->prev = api_data;
- api_data->next = root;
- api_data->prev->next = api_data;
- }
- api_data->data_was_malloc = copy_data;
- // Avoid crashing on bad data
- if (data == NULL) {
- api_data->type = type = API_CONST;
- data = (void *)NULLSTR;
- api_data->data_was_malloc = copy_data = false;
- }
- if (!copy_data)
- api_data->data = data;
- else
- switch(type) {
- case API_ESCAPE:
- case API_STRING:
- case API_CONST:
- api_data->data = (void *)malloc(strlen((char *)data) + 1);
- strcpy((char*)(api_data->data), (char *)data);
- break;
- case API_INT:
- api_data->data = (void *)malloc(sizeof(int));
- *((int *)(api_data->data)) = *((int *)data);
- break;
- case API_UINT:
- api_data->data = (void *)malloc(sizeof(unsigned int));
- *((unsigned int *)(api_data->data)) = *((unsigned int *)data);
- break;
- case API_UINT32:
- api_data->data = (void *)malloc(sizeof(uint32_t));
- *((uint32_t *)(api_data->data)) = *((uint32_t *)data);
- break;
- case API_UINT64:
- api_data->data = (void *)malloc(sizeof(uint64_t));
- *((uint64_t *)(api_data->data)) = *((uint64_t *)data);
- break;
- case API_DOUBLE:
- case API_ELAPSED:
- case API_MHS:
- case API_MHTOTAL:
- case API_UTILITY:
- case API_FREQ:
- case API_HS:
- case API_DIFF:
- api_data->data = (void *)malloc(sizeof(double));
- *((double *)(api_data->data)) = *((double *)data);
- break;
- case API_BOOL:
- api_data->data = (void *)malloc(sizeof(bool));
- *((bool *)(api_data->data)) = *((bool *)data);
- break;
- case API_TIMEVAL:
- api_data->data = (void *)malloc(sizeof(struct timeval));
- memcpy(api_data->data, data, sizeof(struct timeval));
- break;
- case API_TIME:
- api_data->data = (void *)malloc(sizeof(time_t));
- *(time_t *)(api_data->data) = *((time_t *)data);
- break;
- case API_VOLTS:
- case API_TEMP:
- api_data->data = (void *)malloc(sizeof(float));
- *((float *)(api_data->data)) = *((float *)data);
- break;
- default:
- applog(LOG_ERR, "API: unknown1 data type %d ignored", type);
- api_data->type = API_STRING;
- api_data->data_was_malloc = false;
- api_data->data = (void *)UNKNOWN;
- break;
- }
- return root;
- }
- struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_ESCAPE, (void *)data, copy_data);
- }
- struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_STRING, (void *)data, copy_data);
- }
- struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_CONST, (void *)data, copy_data);
- }
- struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_INT, (void *)data, copy_data);
- }
- struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_UINT, (void *)data, copy_data);
- }
- struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_UINT32, (void *)data, copy_data);
- }
- struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_UINT64, (void *)data, copy_data);
- }
- struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_DOUBLE, (void *)data, copy_data);
- }
- struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_ELAPSED, (void *)data, copy_data);
- }
- struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_BOOL, (void *)data, copy_data);
- }
- struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_TIMEVAL, (void *)data, copy_data);
- }
- struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_TIME, (void *)data, copy_data);
- }
- struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_MHS, (void *)data, copy_data);
- }
- struct api_data *api_add_mhtotal(struct api_data *root, char *name, double *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_MHTOTAL, (void *)data, copy_data);
- }
- struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_TEMP, (void *)data, copy_data);
- }
- struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_UTILITY, (void *)data, copy_data);
- }
- struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_FREQ, (void *)data, copy_data);
- }
- struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_VOLTS, (void *)data, copy_data);
- }
- struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_HS, (void *)data, copy_data);
- }
- struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_DIFF, (void *)data, copy_data);
- }
- static struct api_data *print_data(struct api_data *root, char *buf, bool isjson, bool precom)
- {
- struct api_data *tmp;
- bool first = true;
- char *original, *escape;
- char *quote;
- *buf = '\0';
- if (precom) {
- *(buf++) = *COMMA;
- *buf = '\0';
- }
- if (isjson) {
- strcpy(buf, JSON0);
- buf = strchr(buf, '\0');
- quote = JSON1;
- } else
- quote = (char *)BLANK;
- while (root) {
- if (!first)
- *(buf++) = *COMMA;
- else
- first = false;
- sprintf(buf, "%s%s%s%s", quote, root->name, quote, isjson ? ":" : "=");
- buf = strchr(buf, '\0');
- switch(root->type) {
- case API_STRING:
- case API_CONST:
- sprintf(buf, "%s%s%s", quote, (char *)(root->data), quote);
- break;
- case API_ESCAPE:
- original = (char *)(root->data);
- escape = escape_string((char *)(root->data), isjson);
- sprintf(buf, "%s%s%s", quote, escape, quote);
- if (escape != original)
- free(escape);
- break;
- case API_INT:
- sprintf(buf, "%d", *((int *)(root->data)));
- break;
- case API_UINT:
- sprintf(buf, "%u", *((unsigned int *)(root->data)));
- break;
- case API_UINT32:
- sprintf(buf, "%"PRIu32, *((uint32_t *)(root->data)));
- break;
- case API_UINT64:
- sprintf(buf, "%"PRIu64, *((uint64_t *)(root->data)));
- break;
- case API_TIME:
- sprintf(buf, "%lu", *((unsigned long *)(root->data)));
- break;
- case API_DOUBLE:
- sprintf(buf, "%f", *((double *)(root->data)));
- break;
- case API_ELAPSED:
- sprintf(buf, "%.0f", *((double *)(root->data)));
- break;
- case API_UTILITY:
- case API_FREQ:
- case API_MHS:
- sprintf(buf, "%.2f", *((double *)(root->data)));
- break;
- case API_VOLTS:
- sprintf(buf, "%.3f", *((float *)(root->data)));
- break;
- case API_MHTOTAL:
- sprintf(buf, "%.4f", *((double *)(root->data)));
- break;
- case API_HS:
- sprintf(buf, "%.15f", *((double *)(root->data)));
- break;
- case API_DIFF:
- sprintf(buf, "%.8f", *((double *)(root->data)));
- break;
- case API_BOOL:
- sprintf(buf, "%s", *((bool *)(root->data)) ? TRUESTR : FALSESTR);
- break;
- case API_TIMEVAL:
- sprintf(buf, "%ld.%06ld",
- ((struct timeval *)(root->data))->tv_sec,
- ((struct timeval *)(root->data))->tv_usec);
- break;
- case API_TEMP:
- sprintf(buf, "%.2f", *((float *)(root->data)));
- break;
- default:
- applog(LOG_ERR, "API: unknown2 data type %d ignored", root->type);
- sprintf(buf, "%s%s%s", quote, UNKNOWN, quote);
- break;
- }
- buf = strchr(buf, '\0');
- if (root->data_was_malloc)
- free(root->data);
- if (root->next == root) {
- free(root);
- root = NULL;
- } else {
- tmp = root;
- root = tmp->next;
- root->prev = tmp->prev;
- root->prev->next = root;
- free(tmp);
- }
- }
- strcpy(buf, isjson ? JSON5 : SEPSTR);
- return root;
- }
- #ifdef HAVE_AN_FPGA
- static int numpgas()
- {
- int count = 0;
- int i;
- rd_lock(&devices_lock);
- for (i = 0; i < total_devices; i++) {
- #ifdef USE_BITFORCE
- if (devices[i]->drv->drv_id == DRIVER_BITFORCE)
- count++;
- #endif
- #ifdef USE_ICARUS
- if (devices[i]->drv->drv_id == DRIVER_ICARUS)
- count++;
- #endif
- #ifdef USE_ZTEX
- if (devices[i]->drv->drv_id == DRIVER_ZTEX)
- count++;
- #endif
- #ifdef USE_MODMINER
- if (devices[i]->drv->drv_id == DRIVER_MODMINER)
- count++;
- #endif
- }
- rd_unlock(&devices_lock);
- return count;
- }
- static int pgadevice(int pgaid)
- {
- int count = 0;
- int i;
- rd_lock(&devices_lock);
- for (i = 0; i < total_devices; i++) {
- #ifdef USE_BITFORCE
- if (devices[i]->drv->drv_id == DRIVER_BITFORCE)
- count++;
- #endif
- #ifdef USE_ICARUS
- if (devices[i]->drv->drv_id == DRIVER_ICARUS)
- count++;
- #endif
- #ifdef USE_ZTEX
- if (devices[i]->drv->drv_id == DRIVER_ZTEX)
- count++;
- #endif
- #ifdef USE_MODMINER
- if (devices[i]->drv->drv_id == DRIVER_MODMINER)
- count++;
- #endif
- if (count == (pgaid + 1))
- goto foundit;
- }
- rd_unlock(&devices_lock);
- return -1;
- foundit:
- rd_unlock(&devices_lock);
- return i;
- }
- #endif
- // All replies (except BYE and RESTART) start with a message
- // thus for JSON, message() inserts JSON_START at the front
- // and send_result() adds JSON_END at the end
- static void message(struct io_data *io_data, int messageid, int paramid, char *param2, bool isjson)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- char buf2[TMPBUFSIZ];
- char severity[2];
- #ifdef HAVE_AN_FPGA
- int pga;
- #endif
- #ifdef WANT_CPUMINE
- int cpu;
- #endif
- int i;
- io_reinit(io_data);
- if (isjson)
- io_put(io_data, JSON_START JSON_STATUS);
- for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
- if (codes[i].code == messageid) {
- switch (codes[i].severity) {
- case SEVERITY_WARN:
- severity[0] = 'W';
- break;
- case SEVERITY_INFO:
- severity[0] = 'I';
- break;
- case SEVERITY_SUCC:
- severity[0] = 'S';
- break;
- case SEVERITY_ERR:
- default:
- severity[0] = 'E';
- break;
- }
- severity[1] = '\0';
- switch(codes[i].params) {
- case PARAM_GPU:
- case PARAM_PGA:
- case PARAM_CPU:
- case PARAM_PID:
- case PARAM_INT:
- sprintf(buf, codes[i].description, paramid);
- break;
- case PARAM_POOL:
- sprintf(buf, codes[i].description, paramid, pools[paramid]->rpc_url);
- break;
- #ifdef HAVE_OPENCL
- case PARAM_GPUMAX:
- sprintf(buf, codes[i].description, paramid, nDevs - 1);
- break;
- #endif
- #ifdef HAVE_AN_FPGA
- case PARAM_PGAMAX:
- pga = numpgas();
- sprintf(buf, codes[i].description, paramid, pga - 1);
- break;
- #endif
- #ifdef WANT_CPUMINE
- case PARAM_CPUMAX:
- if (opt_n_threads > 0)
- cpu = num_processors;
- else
- cpu = 0;
- sprintf(buf, codes[i].description, paramid, cpu - 1);
- break;
- #endif
- case PARAM_PMAX:
- sprintf(buf, codes[i].description, total_pools);
- break;
- case PARAM_POOLMAX:
- sprintf(buf, codes[i].description, paramid, total_pools - 1);
- break;
- case PARAM_DMAX:
- #ifdef HAVE_AN_FPGA
- pga = numpgas();
- #endif
- #ifdef WANT_CPUMINE
- if (opt_n_threads > 0)
- cpu = num_processors;
- else
- cpu = 0;
- #endif
- sprintf(buf, codes[i].description
- #ifdef HAVE_OPENCL
- , nDevs
- #endif
- #ifdef HAVE_AN_FPGA
- , pga
- #endif
- #ifdef WANT_CPUMINE
- , cpu
- #endif
- );
- break;
- case PARAM_CMD:
- sprintf(buf, codes[i].description, JSON_COMMAND);
- break;
- case PARAM_STR:
- sprintf(buf, codes[i].description, param2);
- break;
- case PARAM_BOTH:
- sprintf(buf, codes[i].description, paramid, param2);
- break;
- case PARAM_BOOL:
- sprintf(buf, codes[i].description, paramid ? TRUESTR : FALSESTR);
- break;
- case PARAM_SET:
- sprintf(buf, codes[i].description, param2, paramid);
- break;
- case PARAM_NONE:
- default:
- strcpy(buf, codes[i].description);
- }
- root = api_add_string(root, _STATUS, severity, false);
- root = api_add_time(root, "When", &when, false);
- root = api_add_int(root, "Code", &messageid, false);
- root = api_add_escape(root, "Msg", buf, false);
- root = api_add_escape(root, "Description", opt_api_description, false);
- root = print_data(root, buf2, isjson, false);
- io_add(io_data, buf2);
- if (isjson)
- io_add(io_data, JSON_CLOSE);
- return;
- }
- }
- root = api_add_string(root, _STATUS, "F", false);
- root = api_add_time(root, "When", &when, false);
- int id = -1;
- root = api_add_int(root, "Code", &id, false);
- sprintf(buf, "%d", messageid);
- root = api_add_escape(root, "Msg", buf, false);
- root = api_add_escape(root, "Description", opt_api_description, false);
- root = print_data(root, buf2, isjson, false);
- io_add(io_data, buf2);
- if (isjson)
- io_add(io_data, JSON_CLOSE);
- }
- static void apiversion(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- bool io_open;
- message(io_data, MSG_VERSION, 0, NULL, isjson);
- io_open = io_add(io_data, isjson ? COMSTR JSON_VERSION : _VERSION COMSTR);
- root = api_add_string(root, "CGMiner", VERSION, false);
- root = api_add_const(root, "API", APIVERSION, false);
- root = print_data(root, buf, isjson, false);
- io_add(io_data, buf);
- if (isjson && io_open)
- io_close(io_data);
- }
- static void minerconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- bool io_open;
- int gpucount = 0;
- int pgacount = 0;
- int cpucount = 0;
- char *adlinuse = (char *)NO;
- #ifdef HAVE_ADL
- const char *adl = YES;
- int i;
- for (i = 0; i < nDevs; i++) {
- if (gpus[i].has_adl) {
- adlinuse = (char *)YES;
- break;
- }
- }
- #else
- const char *adl = NO;
- #endif
- #ifdef HAVE_OPENCL
- gpucount = nDevs;
- #endif
- #ifdef HAVE_AN_FPGA
- pgacount = numpgas();
- #endif
- #ifdef WANT_CPUMINE
- cpucount = opt_n_threads > 0 ? num_processors : 0;
- #endif
- message(io_data, MSG_MINECONFIG, 0, NULL, isjson);
- io_open = io_add(io_data, isjson ? COMSTR JSON_MINECONFIG : _MINECONFIG COMSTR);
- root = api_add_int(root, "GPU Count", &gpucount, false);
- root = api_add_int(root, "PGA Count", &pgacount, false);
- root = api_add_int(root, "CPU Count", &cpucount, false);
- root = api_add_int(root, "Pool Count", &total_pools, false);
- root = api_add_const(root, "ADL", (char *)adl, false);
- root = api_add_string(root, "ADL in use", adlinuse, false);
- root = api_add_const(root, "Strategy", strategies[pool_strategy].s, false);
- root = api_add_int(root, "Log Interval", &opt_log_interval, false);
- root = api_add_const(root, "Device Code", DEVICECODE, false);
- root = api_add_const(root, "OS", OSINFO, false);
- root = api_add_bool(root, "Failover-Only", &opt_fail_only, false);
- root = api_add_int(root, "ScanTime", &opt_scantime, false);
- root = api_add_int(root, "Queue", &opt_queue, false);
- root = api_add_int(root, "Expiry", &opt_expiry, false);
- #ifdef USE_USBUTILS
- if (hotplug_time == 0)
- root = api_add_const(root, "Hotplug", DISABLED, false);
- else
- root = api_add_int(root, "Hotplug", &hotplug_time, false);
- #else
- root = api_add_const(root, "Hotplug", NONE, false);
- #endif
- root = print_data(root, buf, isjson, false);
- io_add(io_data, buf);
- if (isjson && io_open)
- io_close(io_data);
- }
- #if defined(HAVE_OPENCL) || defined(HAVE_AN_FPGA)
- static const char *status2str(enum alive status)
- {
- switch (status) {
- case LIFE_WELL:
- return ALIVE;
- case LIFE_SICK:
- return SICK;
- case LIFE_DEAD:
- return DEAD;
- case LIFE_NOSTART:
- return NOSTART;
- case LIFE_INIT:
- return INIT;
- default:
- return UNKNOWN;
- }
- }
- #endif
- #ifdef HAVE_OPENCL
- static void gpustatus(struct io_data *io_data, int gpu, bool isjson, bool precom)
- {
- struct api_data *root = NULL;
- char intensity[20];
- char buf[TMPBUFSIZ];
- char *enabled;
- char *status;
- float gt, gv;
- int ga, gf, gp, gc, gm, pt;
- if (gpu >= 0 && gpu < nDevs) {
- struct cgpu_info *cgpu = &gpus[gpu];
- cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
- #ifdef HAVE_ADL
- if (!gpu_stats(gpu, >, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
- #endif
- gt = gv = gm = gc = ga = gf = gp = pt = 0;
- if (cgpu->deven != DEV_DISABLED)
- enabled = (char *)YES;
- else
- enabled = (char *)NO;
- status = (char *)status2str(cgpu->status);
- if (cgpu->dynamic)
- strcpy(intensity, DYNAMIC);
- else
- sprintf(intensity, "%d", cgpu->intensity);
- root = api_add_int(root, "GPU", &gpu, false);
- root = api_add_string(root, "Enabled", enabled, false);
- root = api_add_string(root, "Status", status, false);
- root = api_add_temp(root, "Temperature", >, false);
- root = api_add_int(root, "Fan Speed", &gf, false);
- root = api_add_int(root, "Fan Percent", &gp, false);
- root = api_add_int(root, "GPU Clock", &gc, false);
- root = api_add_int(root, "Memory Clock", &gm, false);
- root = api_add_volts(root, "GPU Voltage", &gv, false);
- root = api_add_int(root, "GPU Activity", &ga, false);
- root = api_add_int(root, "Powertune", &pt, false);
- double mhs = cgpu->total_mhashes / total_secs;
- root = api_add_mhs(root, "MHS av", &mhs, false);
- char mhsname[27];
- sprintf(mhsname, "MHS %ds", opt_log_interval);
- root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
- root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
- root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
- root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
- root = api_add_utility(root, "Utility", &(cgpu->utility), false);
- root = api_add_string(root, "Intensity", intensity, false);
- int last_share_pool = cgpu->last_share_pool_time > 0 ?
- cgpu->last_share_pool : -1;
- root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
- root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
- root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
- root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
- root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
- root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
- root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
- root = api_add_time(root, "Last Valid Work", &(cgpu->last_device_valid_work), false);
- root = print_data(root, buf, isjson, precom);
- io_add(io_data, buf);
- }
- }
- #endif
- #ifdef HAVE_AN_FPGA
- static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- char *enabled;
- char *status;
- int numpga = numpgas();
- if (numpga > 0 && pga >= 0 && pga < numpga) {
- int dev = pgadevice(pga);
- if (dev < 0) // Should never happen
- return;
- struct cgpu_info *cgpu = get_devices(dev);
- double frequency = 0;
- float temp = cgpu->temp;
- #ifdef USE_ZTEX
- if (cgpu->drv->drv_id == DRIVER_ZTEX && cgpu->device_ztex)
- frequency = cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1);
- #endif
- #ifdef USE_MODMINER
- if (cgpu->drv->drv_id == DRIVER_MODMINER)
- frequency = cgpu->clock;
- #endif
- cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
- if (cgpu->deven != DEV_DISABLED)
- enabled = (char *)YES;
- else
- enabled = (char *)NO;
- status = (char *)status2str(cgpu->status);
- root = api_add_int(root, "PGA", &pga, false);
- root = api_add_string(root, "Name", cgpu->drv->name, false);
- root = api_add_int(root, "ID", &(cgpu->device_id), false);
- root = api_add_string(root, "Enabled", enabled, false);
- root = api_add_string(root, "Status", status, false);
- root = api_add_temp(root, "Temperature", &temp, false);
- double mhs = cgpu->total_mhashes / total_secs;
- root = api_add_mhs(root, "MHS av", &mhs, false);
- char mhsname[27];
- sprintf(mhsname, "MHS %ds", opt_log_interval);
- root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
- root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
- root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
- root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
- root = api_add_utility(root, "Utility", &(cgpu->utility), false);
- int last_share_pool = cgpu->last_share_pool_time > 0 ?
- cgpu->last_share_pool : -1;
- root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
- root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
- root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
- root = api_add_freq(root, "Frequency", &frequency, false);
- root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
- root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
- root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
- root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
- #ifdef USE_USBUTILS
- root = api_add_bool(root, "No Device", &(cgpu->usbinfo.nodev), false);
- #endif
- root = api_add_time(root, "Last Valid Work", &(cgpu->last_device_valid_work), false);
- root = print_data(root, buf, isjson, precom);
- io_add(io_data, buf);
- }
- }
- #endif
- #ifdef WANT_CPUMINE
- static void cpustatus(struct io_data *io_data, int cpu, bool isjson, bool precom)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
- struct cgpu_info *cgpu = &cpus[cpu];
- cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
- root = api_add_int(root, "CPU", &cpu, false);
- double mhs = cgpu->total_mhashes / total_secs;
- root = api_add_mhs(root, "MHS av", &mhs, false);
- char mhsname[27];
- sprintf(mhsname, "MHS %ds", opt_log_interval);
- root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
- root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
- root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
- root = api_add_utility(root, "Utility", &(cgpu->utility), false);
- int last_share_pool = cgpu->last_share_pool_time > 0 ?
- cgpu->last_share_pool : -1;
- root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
- root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
- root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
- root = api_add_int(root, "Diff1 Work", &(cgpu->diff1), false);
- root = api_add_diff(root, "Difficulty Accepted", &(cgpu->diff_accepted), false);
- root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
- root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
- root = api_add_time(root, "Last Valid Work", &(cgpu->last_device_valid_work), false);
- root = print_data(root, buf, isjson, precom);
- io_add(io_data, buf);
- }
- }
- #endif
- static void devstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- bool io_open = false;
- int devcount = 0;
- int numgpu = 0;
- int numpga = 0;
- int i;
- #ifdef HAVE_OPENCL
- numgpu = nDevs;
- #endif
- #ifdef HAVE_AN_FPGA
- numpga = numpgas();
- #endif
- if (numgpu == 0 && opt_n_threads == 0 && numpga == 0) {
- message(io_data, MSG_NODEVS, 0, NULL, isjson);
- return;
- }
- message(io_data, MSG_DEVS, 0, NULL, isjson);
- if (isjson)
- io_open = io_add(io_data, COMSTR JSON_DEVS);
- #ifdef HAVE_OPENCL
- for (i = 0; i < nDevs; i++) {
- gpustatus(io_data, i, isjson, isjson && devcount > 0);
- devcount++;
- }
- #endif
- #ifdef HAVE_AN_FPGA
- if (numpga > 0) {
- for (i = 0; i < numpga; i++) {
- pgastatus(io_data, i, isjson, isjson && devcount > 0);
- devcount++;
- }
- }
- #endif
- #ifdef WANT_CPUMINE
- if (opt_n_threads > 0) {
- for (i = 0; i < num_processors; i++) {
- cpustatus(io_data, i, isjson, isjson && devcount > 0);
- devcount++;
- }
- }
- #endif
- if (isjson && io_open)
- io_close(io_data);
- }
- #ifdef HAVE_OPENCL
- static void gpudev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- bool io_open = false;
- int id;
- if (nDevs == 0) {
- message(io_data, MSG_GPUNON, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= nDevs) {
- message(io_data, MSG_INVGPU, id, NULL, isjson);
- return;
- }
- message(io_data, MSG_GPUDEV, id, NULL, isjson);
- if (isjson)
- io_open = io_add(io_data, COMSTR JSON_GPU);
- gpustatus(io_data, id, isjson, false);
- if (isjson && io_open)
- io_close(io_data);
- }
- #endif
- #ifdef HAVE_AN_FPGA
- static void pgadev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- bool io_open = false;
- int numpga = numpgas();
- int id;
- if (numpga == 0) {
- message(io_data, MSG_PGANON, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= numpga) {
- message(io_data, MSG_INVPGA, id, NULL, isjson);
- return;
- }
- message(io_data, MSG_PGADEV, id, NULL, isjson);
- if (isjson)
- io_open = io_add(io_data, COMSTR JSON_PGA);
- pgastatus(io_data, id, isjson, false);
- if (isjson && io_open)
- io_close(io_data);
- }
- static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- struct cgpu_info *cgpu;
- int numpga = numpgas();
- struct thr_info *thr;
- int pga;
- int id;
- int i;
- if (numpga == 0) {
- message(io_data, MSG_PGANON, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= numpga) {
- message(io_data, MSG_INVPGA, id, NULL, isjson);
- return;
- }
- int dev = pgadevice(id);
- if (dev < 0) { // Should never happen
- message(io_data, MSG_INVPGA, id, NULL, isjson);
- return;
- }
- cgpu = get_devices(dev);
- applog(LOG_DEBUG, "API: request to pgaenable pgaid %d device %d %s%u",
- id, dev, cgpu->drv->name, cgpu->device_id);
- if (cgpu->deven != DEV_DISABLED) {
- message(io_data, MSG_PGALRENA, id, NULL, isjson);
- return;
- }
- #if 0 /* A DISABLED device wont change status FIXME: should disabling make it WELL? */
- if (cgpu->status != LIFE_WELL) {
- message(io_data, MSG_PGAUNW, id, NULL, isjson);
- return;
- }
- #endif
- #ifdef USE_USBUTILS
- if (cgpu->usbinfo.nodev) {
- message(io_data, MSG_USBNODEV, id, NULL, isjson);
- return;
- }
- #endif
- for (i = 0; i < mining_threads; i++) {
- thr = get_thread(i);
- pga = thr->cgpu->cgminer_id;
- if (pga == dev) {
- cgpu->deven = DEV_ENABLED;
- applog(LOG_DEBUG, "API: pushing ping (%d) to thread %d", ping, thr->id);
- tq_push(thr->q, &ping);
- }
- }
- message(io_data, MSG_PGAENA, id, NULL, isjson);
- }
- static void pgadisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- struct cgpu_info *cgpu;
- int numpga = numpgas();
- int id;
- if (numpga == 0) {
- message(io_data, MSG_PGANON, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= numpga) {
- message(io_data, MSG_INVPGA, id, NULL, isjson);
- return;
- }
- int dev = pgadevice(id);
- if (dev < 0) { // Should never happen
- message(io_data, MSG_INVPGA, id, NULL, isjson);
- return;
- }
- cgpu = get_devices(dev);
- applog(LOG_DEBUG, "API: request to pgadisable pgaid %d device %d %s%u",
- id, dev, cgpu->drv->name, cgpu->device_id);
- if (cgpu->deven == DEV_DISABLED) {
- message(io_data, MSG_PGALRDIS, id, NULL, isjson);
- return;
- }
- cgpu->deven = DEV_DISABLED;
- message(io_data, MSG_PGADIS, id, NULL, isjson);
- }
- static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- struct cgpu_info *cgpu;
- struct device_drv *drv;
- int numpga = numpgas();
- int id;
- if (numpga == 0) {
- message(io_data, MSG_PGANON, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= numpga) {
- message(io_data, MSG_INVPGA, id, NULL, isjson);
- return;
- }
- int dev = pgadevice(id);
- if (dev < 0) { // Should never happen
- message(io_data, MSG_INVPGA, id, NULL, isjson);
- return;
- }
- cgpu = get_devices(dev);
- drv = cgpu->drv;
- if (!drv->identify_device)
- message(io_data, MSG_PGANOID, id, NULL, isjson);
- else {
- drv->identify_device(cgpu);
- message(io_data, MSG_PGAIDENT, id, NULL, isjson);
- }
- }
- #endif
- #ifdef WANT_CPUMINE
- static void cpudev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- bool io_open = false;
- int id;
- if (opt_n_threads == 0) {
- message(io_data, MSG_CPUNON, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= num_processors) {
- message(io_data, MSG_INVCPU, id, NULL, isjson);
- return;
- }
- message(io_data, MSG_CPUDEV, id, NULL, isjson);
- if (isjson)
- io_open = io_add(io_data, COMSTR JSON_CPU);
- cpustatus(io_data, id, isjson, false);
- if (isjson && io_open)
- io_close(io_data);
- }
- #endif
- static void poolstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- bool io_open = false;
- char *status, *lp;
- int i;
- if (total_pools == 0) {
- message(io_data, MSG_NOPOOL, 0, NULL, isjson);
- return;
- }
- message(io_data, MSG_POOL, 0, NULL, isjson);
- if (isjson)
- io_open = io_add(io_data, COMSTR JSON_POOLS);
- for (i = 0; i < total_pools; i++) {
- struct pool *pool = pools[i];
- if (pool->removed)
- continue;
- switch (pool->enabled) {
- case POOL_DISABLED:
- status = (char *)DISABLED;
- break;
- case POOL_REJECTING:
- status = (char *)REJECTING;
- break;
- case POOL_ENABLED:
- if (pool->idle)
- status = (char *)DEAD;
- else
- status = (char *)ALIVE;
- break;
- default:
- status = (char *)UNKNOWN;
- break;
- }
- if (pool->hdr_path)
- lp = (char *)YES;
- else
- lp = (char *)NO;
- root = api_add_int(root, "POOL", &i, false);
- root = api_add_escape(root, "URL", pool->rpc_url, false);
- root = api_add_string(root, "Status", status, false);
- root = api_add_int(root, "Priority", &(pool->prio), false);
- root = api_add_string(root, "Long Poll", lp, false);
- root = api_add_uint(root, "Getworks", &(pool->getwork_requested), false);
- root = api_add_int(root, "Accepted", &(pool->accepted), false);
- root = api_add_int(root, "Rejected", &(pool->rejected), false);
- root = api_add_uint(root, "Discarded", &(pool->discarded_work), false);
- root = api_add_uint(root, "Stale", &(pool->stale_shares), false);
- root = api_add_uint(root, "Get Failures", &(pool->getfail_occasions), false);
- root = api_add_uint(root, "Remote Failures", &(pool->remotefail_occasions), false);
- root = api_add_escape(root, "User", pool->rpc_user, false);
- root = api_add_time(root, "Last Share Time", &(pool->last_share_time), false);
- root = api_add_int(root, "Diff1 Shares", &(pool->diff1), false);
- if (pool->rpc_proxy) {
- root = api_add_const(root, "Proxy Type", proxytype(pool->rpc_proxytype), false);
- root = api_add_escape(root, "Proxy", pool->rpc_proxy, false);
- } else {
- root = api_add_const(root, "Proxy Type", BLANK, false);
- root = api_add_const(root, "Proxy", BLANK, false);
- }
- root = api_add_diff(root, "Difficulty Accepted", &(pool->diff_accepted), false);
- root = api_add_diff(root, "Difficulty Rejected", &(pool->diff_rejected), false);
- root = api_add_diff(root, "Difficulty Stale", &(pool->diff_stale), false);
- root = api_add_diff(root, "Last Share Difficulty", &(pool->last_share_diff), false);
- root = api_add_bool(root, "Has Stratum", &(pool->has_stratum), false);
- root = api_add_bool(root, "Stratum Active", &(pool->stratum_active), false);
- if (pool->stratum_active)
- root = api_add_escape(root, "Stratum URL", pool->stratum_url, false);
- else
- root = api_add_const(root, "Stratum URL", BLANK, false);
- root = api_add_bool(root, "Has GBT", &(pool->has_gbt), false);
- root = api_add_uint64(root, "Best Share", &(pool->best_diff), true);
- root = print_data(root, buf, isjson, isjson && (i > 0));
- io_add(io_data, buf);
- }
- if (isjson && io_open)
- io_close(io_data);
- }
- static void summary(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- bool io_open;
- double utility, mhs, work_utility;
- #ifdef WANT_CPUMINE
- char *algo = (char *)(algo_names[opt_algo]);
- if (algo == NULL)
- algo = (char *)NULLSTR;
- #endif
- message(io_data, MSG_SUMM, 0, NULL, isjson);
- io_open = io_add(io_data, isjson ? COMSTR JSON_SUMMARY : _SUMMARY COMSTR);
- // stop hashmeter() changing some while copying
- mutex_lock(&hash_lock);
- utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
- mhs = total_mhashes_done / total_secs;
- work_utility = total_diff1 / ( total_secs ? total_secs : 1 ) * 60;
- root = api_add_elapsed(root, "Elapsed", &(total_secs), true);
- #ifdef WANT_CPUMINE
- root = api_add_string(root, "Algorithm", algo, false);
- #endif
- root = api_add_mhs(root, "MHS av", &(mhs), false);
- root = api_add_uint(root, "Found Blocks", &(found_blocks), true);
- root = api_add_int(root, "Getworks", &(total_getworks), true);
- root = api_add_int(root, "Accepted", &(total_accepted), true);
- root = api_add_int(root, "Rejected", &(total_rejected), true);
- root = api_add_int(root, "Hardware Errors", &(hw_errors), true);
- root = api_add_utility(root, "Utility", &(utility), false);
- root = api_add_int(root, "Discarded", &(total_discarded), true);
- root = api_add_int(root, "Stale", &(total_stale), true);
- root = api_add_uint(root, "Get Failures", &(total_go), true);
- root = api_add_uint(root, "Local Work", &(local_work), true);
- root = api_add_uint(root, "Remote Failures", &(total_ro), true);
- root = api_add_uint(root, "Network Blocks", &(new_blocks), true);
- root = api_add_mhtotal(root, "Total MH", &(total_mhashes_done), true);
- root = api_add_utility(root, "Work Utility", &(work_utility), false);
- root = api_add_diff(root, "Difficulty Accepted", &(total_diff_accepted), true);
- root = api_add_diff(root, "Difficulty Rejected", &(total_diff_rejected), true);
- root = api_add_diff(root, "Difficulty Stale", &(total_diff_stale), true);
- root = api_add_uint64(root, "Best Share", &(best_diff), true);
- mutex_unlock(&hash_lock);
- root = print_data(root, buf, isjson, false);
- io_add(io_data, buf);
- if (isjson && io_open)
- io_close(io_data);
- }
- #ifdef HAVE_OPENCL
- static void gpuenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- struct thr_info *thr;
- int gpu;
- int id;
- int i;
- if (gpu_threads == 0) {
- message(io_data, MSG_GPUNON, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= nDevs) {
- message(io_data, MSG_INVGPU, id, NULL, isjson);
- return;
- }
- applog(LOG_DEBUG, "API: request to gpuenable gpuid %d %s%u",
- id, gpus[id].drv->name, gpus[id].device_id);
- if (gpus[id].deven != DEV_DISABLED) {
- message(io_data, MSG_ALRENA, id, NULL, isjson);
- return;
- }
- for (i = 0; i < gpu_threads; i++) {
- thr = get_thread(i);
- gpu = thr->cgpu->device_id;
- if (gpu == id) {
- if (thr->cgpu->status != LIFE_WELL) {
- message(io_data, MSG_GPUMRE, id, NULL, isjson);
- return;
- }
- gpus[id].deven = DEV_ENABLED;
- applog(LOG_DEBUG, "API: pushing ping (%d) to thread %d", ping, thr->id);
- tq_push(thr->q, &ping);
- }
- }
- message(io_data, MSG_GPUREN, id, NULL, isjson);
- }
- static void gpudisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- int id;
- if (nDevs == 0) {
- message(io_data, MSG_GPUNON, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= nDevs) {
- message(io_data, MSG_INVGPU, id, NULL, isjson);
- return;
- }
- applog(LOG_DEBUG, "API: request to gpudisable gpuid %d %s%u",
- id, gpus[id].drv->name, gpus[id].device_id);
- if (gpus[id].deven == DEV_DISABLED) {
- message(io_data, MSG_ALRDIS, id, NULL, isjson);
- return;
- }
- gpus[id].deven = DEV_DISABLED;
- message(io_data, MSG_GPUDIS, id, NULL, isjson);
- }
- static void gpurestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- int id;
- if (nDevs == 0) {
- message(io_data, MSG_GPUNON, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= nDevs) {
- message(io_data, MSG_INVGPU, id, NULL, isjson);
- return;
- }
- reinit_device(&gpus[id]);
- message(io_data, MSG_GPUREI, id, NULL, isjson);
- }
- #endif
- static void gpucount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- bool io_open;
- int numgpu = 0;
- #ifdef HAVE_OPENCL
- numgpu = nDevs;
- #endif
- message(io_data, MSG_NUMGPU, 0, NULL, isjson);
- io_open = io_add(io_data, isjson ? COMSTR JSON_GPUS : _GPUS COMSTR);
- root = api_add_int(root, "Count", &numgpu, false);
- root = print_data(root, buf, isjson, false);
- io_add(io_data, buf);
- if (isjson && io_open)
- io_close(io_data);
- }
- static void pgacount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- bool io_open;
- int count = 0;
- #ifdef HAVE_AN_FPGA
- count = numpgas();
- #endif
- message(io_data, MSG_NUMPGA, 0, NULL, isjson);
- io_open = io_add(io_data, isjson ? COMSTR JSON_PGAS : _PGAS COMSTR);
- root = api_add_int(root, "Count", &count, false);
- root = print_data(root, buf, isjson, false);
- io_add(io_data, buf);
- if (isjson && io_open)
- io_close(io_data);
- }
- static void cpucount(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- bool io_open;
- int count = 0;
- #ifdef WANT_CPUMINE
- count = opt_n_threads > 0 ? num_processors : 0;
- #endif
- message(io_data, MSG_NUMCPU, 0, NULL, isjson);
- io_open = io_add(io_data, isjson ? COMSTR JSON_CPUS : _CPUS COMSTR);
- root = api_add_int(root, "Count", &count, false);
- root = print_data(root, buf, isjson, false);
- io_add(io_data, buf);
- if (isjson && io_open)
- io_close(io_data);
- }
- static void switchpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- struct pool *pool;
- int id;
- if (total_pools == 0) {
- message(io_data, MSG_NOPOOL, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISPID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= total_pools) {
- message(io_data, MSG_INVPID, id, NULL, isjson);
- return;
- }
- pool = pools[id];
- pool->enabled = POOL_ENABLED;
- switch_pools(pool);
- message(io_data, MSG_SWITCHP, id, NULL, isjson);
- }
- static void copyadvanceafter(char ch, char **param, char **buf)
- {
- #define src_p (*param)
- #define dst_b (*buf)
- while (*src_p && *src_p != ch) {
- if (*src_p == '\\' && *(src_p+1) != '\0')
- src_p++;
- *(dst_b++) = *(src_p++);
- }
- if (*src_p)
- src_p++;
- *(dst_b++) = '\0';
- }
- static bool pooldetails(char *param, char **url, char **user, char **pass)
- {
- char *ptr, *buf;
- ptr = buf = malloc(strlen(param)+1);
- if (unlikely(!buf))
- quit(1, "Failed to malloc pooldetails buf");
- *url = buf;
- // copy url
- copyadvanceafter(',', ¶m, &buf);
- if (!(*param)) // missing user
- goto exitsama;
- *user = buf;
- // copy user
- copyadvanceafter(',', ¶m, &buf);
- if (!*param) // missing pass
- goto exitsama;
- *pass = buf;
- // copy pass
- copyadvanceafter(',', ¶m, &buf);
- return true;
- exitsama:
- free(ptr);
- return false;
- }
- static void addpool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- char *url, *user, *pass;
- struct pool *pool;
- char *ptr;
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISPDP, 0, NULL, isjson);
- return;
- }
- if (!pooldetails(param, &url, &user, &pass)) {
- ptr = escape_string(param, isjson);
- message(io_data, MSG_INVPDP, 0, ptr, isjson);
- if (ptr != param)
- free(ptr);
- ptr = NULL;
- return;
- }
- pool = add_pool();
- detect_stratum(pool, url);
- add_pool_details(pool, true, url, user, pass);
- ptr = escape_string(url, isjson);
- message(io_data, MSG_ADDPOOL, 0, ptr, isjson);
- if (ptr != url)
- free(ptr);
- ptr = NULL;
- }
- static void enablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- struct pool *pool;
- int id;
- if (total_pools == 0) {
- message(io_data, MSG_NOPOOL, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISPID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= total_pools) {
- message(io_data, MSG_INVPID, id, NULL, isjson);
- return;
- }
- pool = pools[id];
- if (pool->enabled == POOL_ENABLED) {
- message(io_data, MSG_ALRENAP, id, NULL, isjson);
- return;
- }
- pool->enabled = POOL_ENABLED;
- if (pool->prio < current_pool()->prio)
- switch_pools(pool);
- message(io_data, MSG_ENAPOOL, id, NULL, isjson);
- }
- static void poolpriority(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- char *ptr, *next;
- int i, pr, prio = 0;
- // TODO: all cgminer code needs a mutex added everywhere for change
- // access to total_pools and also parts of the pools[] array,
- // just copying total_pools here wont solve that
- if (total_pools == 0) {
- message(io_data, MSG_NOPOOL, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISPID, 0, NULL, isjson);
- return;
- }
- bool pools_changed[total_pools];
- int new_prio[total_pools];
- for (i = 0; i < total_pools; ++i)
- pools_changed[i] = false;
- next = param;
- while (next && *next) {
- ptr = next;
- next = strchr(ptr, ',');
- if (next)
- *(next++) = '\0';
- i = atoi(ptr);
- if (i < 0 || i >= total_pools) {
- message(io_data, MSG_INVPID, i, NULL, isjson);
- return;
- }
- if (pools_changed[i]) {
- message(io_data, MSG_DUPPID, i, NULL, isjson);
- return;
- }
- pools_changed[i] = true;
- new_prio[i] = prio++;
- }
- // Only change them if no errors
- for (i = 0; i < total_pools; i++) {
- if (pools_changed[i])
- pools[i]->prio = new_prio[i];
- }
- // In priority order, cycle through the unchanged pools and append them
- for (pr = 0; pr < total_pools; pr++)
- for (i = 0; i < total_pools; i++) {
- if (!pools_changed[i] && pools[i]->prio == pr) {
- pools[i]->prio = prio++;
- pools_changed[i] = true;
- break;
- }
- }
- if (current_pool()->prio)
- switch_pools(NULL);
- message(io_data, MSG_POOLPRIO, 0, NULL, isjson);
- }
- static void disablepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- struct pool *pool;
- int id;
- if (total_pools == 0) {
- message(io_data, MSG_NOPOOL, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISPID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= total_pools) {
- message(io_data, MSG_INVPID, id, NULL, isjson);
- return;
- }
- pool = pools[id];
- if (pool->enabled == POOL_DISABLED) {
- message(io_data, MSG_ALRDISP, id, NULL, isjson);
- return;
- }
- if (enabled_pools <= 1) {
- message(io_data, MSG_DISLASTP, id, NULL, isjson);
- return;
- }
- pool->enabled = POOL_DISABLED;
- if (pool == current_pool())
- switch_pools(NULL);
- message(io_data, MSG_DISPOOL, id, NULL, isjson);
- }
- static void removepool(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- struct pool *pool;
- char *rpc_url;
- bool dofree = false;
- int id;
- if (total_pools == 0) {
- message(io_data, MSG_NOPOOL, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISPID, 0, NULL, isjson);
- return;
- }
- id = atoi(param);
- if (id < 0 || id >= total_pools) {
- message(io_data, MSG_INVPID, id, NULL, isjson);
- return;
- }
- if (total_pools <= 1) {
- message(io_data, MSG_REMLASTP, id, NULL, isjson);
- return;
- }
- pool = pools[id];
- if (pool == current_pool())
- switch_pools(NULL);
- if (pool == current_pool()) {
- message(io_data, MSG_ACTPOOL, id, NULL, isjson);
- return;
- }
- pool->enabled = POOL_DISABLED;
- rpc_url = escape_string(pool->rpc_url, isjson);
- if (rpc_url != pool->rpc_url)
- dofree = true;
- remove_pool(pool);
- message(io_data, MSG_REMPOOL, id, rpc_url, isjson);
- if (dofree)
- free(rpc_url);
- rpc_url = NULL;
- }
- #ifdef HAVE_OPENCL
- static bool splitgpuvalue(struct io_data *io_data, char *param, int *gpu, char **value, bool isjson)
- {
- int id;
- char *gpusep;
- if (nDevs == 0) {
- message(io_data, MSG_GPUNON, 0, NULL, isjson);
- return false;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISID, 0, NULL, isjson);
- return false;
- }
- gpusep = strchr(param, GPUSEP);
- if (gpusep == NULL) {
- message(io_data, MSG_MISVAL, 0, NULL, isjson);
- return false;
- }
- *(gpusep++) = '\0';
- id = atoi(param);
- if (id < 0 || id >= nDevs) {
- message(io_data, MSG_INVGPU, id, NULL, isjson);
- return false;
- }
- *gpu = id;
- *value = gpusep;
- return true;
- }
- static void gpuintensity(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- int id;
- char *value;
- int intensity;
- char intensitystr[7];
- if (!splitgpuvalue(io_data, param, &id, &value, isjson))
- return;
- if (!strncasecmp(value, DYNAMIC, 1)) {
- gpus[id].dynamic = true;
- strcpy(intensitystr, DYNAMIC);
- }
- else {
- intensity = atoi(value);
- if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
- message(io_data, MSG_INVINT, 0, value, isjson);
- return;
- }
- gpus[id].dynamic = false;
- gpus[id].intensity = intensity;
- sprintf(intensitystr, "%d", intensity);
- }
- message(io_data, MSG_GPUINT, id, intensitystr, isjson);
- }
- static void gpumem(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- #ifdef HAVE_ADL
- int id;
- char *value;
- int clock;
- if (!splitgpuvalue(io_data, param, &id, &value, isjson))
- return;
- clock = atoi(value);
- if (set_memoryclock(id, clock))
- message(io_data, MSG_GPUMERR, id, value, isjson);
- else
- message(io_data, MSG_GPUMEM, id, value, isjson);
- #else
- message(io_data, MSG_NOADL, 0, NULL, isjson);
- #endif
- }
- static void gpuengine(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- #ifdef HAVE_ADL
- int id;
- char *value;
- int clock;
- if (!splitgpuvalue(io_data, param, &id, &value, isjson))
- return;
- clock = atoi(value);
- if (set_engineclock(id, clock))
- message(io_data, MSG_GPUEERR, id, value, isjson);
- else
- message(io_data, MSG_GPUENG, id, value, isjson);
- #else
- message(io_data, MSG_NOADL, 0, NULL, isjson);
- #endif
- }
- static void gpufan(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- #ifdef HAVE_ADL
- int id;
- char *value;
- int fan;
- if (!splitgpuvalue(io_data, param, &id, &value, isjson))
- return;
- fan = atoi(value);
- if (set_fanspeed(id, fan))
- message(io_data, MSG_GPUFERR, id, value, isjson);
- else
- message(io_data, MSG_GPUFAN, id, value, isjson);
- #else
- message(io_data, MSG_NOADL, 0, NULL, isjson);
- #endif
- }
- static void gpuvddc(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- #ifdef HAVE_ADL
- int id;
- char *value;
- float vddc;
- if (!splitgpuvalue(io_data, param, &id, &value, isjson))
- return;
- vddc = atof(value);
- if (set_vddc(id, vddc))
- message(io_data, MSG_GPUVERR, id, value, isjson);
- else
- message(io_data, MSG_GPUVDDC, id, value, isjson);
- #else
- message(io_data, MSG_NOADL, 0, NULL, isjson);
- #endif
- }
- #endif
- void doquit(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- if (isjson)
- io_put(io_data, JSON_START JSON_BYE);
- else
- io_put(io_data, _BYE);
- bye = true;
- do_a_quit = true;
- }
- void dorestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- if (isjson)
- io_put(io_data, JSON_START JSON_RESTART);
- else
- io_put(io_data, _RESTART);
- bye = true;
- do_a_restart = true;
- }
- void privileged(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- message(io_data, MSG_ACCOK, 0, NULL, isjson);
- }
- void notifystatus(struct io_data *io_data, int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- char *reason;
- if (cgpu->device_last_not_well == 0)
- reason = REASON_NONE;
- else
- switch(cgpu->device_not_well_reason) {
- case REASON_THREAD_FAIL_INIT:
- reason = REASON_THREAD_FAIL_INIT_STR;
- break;
- case REASON_THREAD_ZERO_HASH:
- reason = REASON_THREAD_ZERO_HASH_STR;
- break;
- case REASON_THREAD_FAIL_QUEUE:
- reason = REASON_THREAD_FAIL_QUEUE_STR;
- break;
- case REASON_DEV_SICK_IDLE_60:
- reason = REASON_DEV_SICK_IDLE_60_STR;
- break;
- case REASON_DEV_DEAD_IDLE_600:
- reason = REASON_DEV_DEAD_IDLE_600_STR;
- break;
- case REASON_DEV_NOSTART:
- reason = REASON_DEV_NOSTART_STR;
- break;
- case REASON_DEV_OVER_HEAT:
- reason = REASON_DEV_OVER_HEAT_STR;
- break;
- case REASON_DEV_THERMAL_CUTOFF:
- reason = REASON_DEV_THERMAL_CUTOFF_STR;
- break;
- case REASON_DEV_COMMS_ERROR:
- reason = REASON_DEV_COMMS_ERROR_STR;
- break;
- default:
- reason = REASON_UNKNOWN_STR;
- break;
- }
- // ALL counters (and only counters) must start the name with a '*'
- // Simplifies future external support for identifying new counters
- root = api_add_int(root, "NOTIFY", &device, false);
- root = api_add_string(root, "Name", cgpu->drv->name, false);
- root = api_add_int(root, "ID", &(cgpu->device_id), false);
- root = api_add_time(root, "Last Well", &(cgpu->device_last_well), false);
- root = api_add_time(root, "Last Not Well", &(cgpu->device_last_not_well), false);
- root = api_add_string(root, "Reason Not Well", reason, false);
- root = api_add_int(root, "*Thread Fail Init", &(cgpu->thread_fail_init_count), false);
- root = api_add_int(root, "*Thread Zero Hash", &(cgpu->thread_zero_hash_count), false);
- root = api_add_int(root, "*Thread Fail Queue", &(cgpu->thread_fail_queue_count), false);
- root = api_add_int(root, "*Dev Sick Idle 60s", &(cgpu->dev_sick_idle_60_count), false);
- root = api_add_int(root, "*Dev Dead Idle 600s", &(cgpu->dev_dead_idle_600_count), false);
- root = api_add_int(root, "*Dev Nostart", &(cgpu->dev_nostart_count), false);
- root = api_add_int(root, "*Dev Over Heat", &(cgpu->dev_over_heat_count), false);
- root = api_add_int(root, "*Dev Thermal Cutoff", &(cgpu->dev_thermal_cutoff_count), false);
- root = api_add_int(root, "*Dev Comms Error", &(cgpu->dev_comms_error_count), false);
- root = api_add_int(root, "*Dev Throttle", &(cgpu->dev_throttle_count), false);
- root = print_data(root, buf, isjson, isjson && (device > 0));
- io_add(io_data, buf);
- }
- static void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, char group)
- {
- struct cgpu_info *cgpu;
- bool io_open = false;
- int i;
- if (total_devices == 0) {
- message(io_data, MSG_NODEVS, 0, NULL, isjson);
- return;
- }
- message(io_data, MSG_NOTIFY, 0, NULL, isjson);
- if (isjson)
- io_open = io_add(io_data, COMSTR JSON_NOTIFY);
- for (i = 0; i < total_devices; i++) {
- cgpu = get_devices(i);
- notifystatus(io_data, i, cgpu, isjson, group);
- }
- if (isjson && io_open)
- io_close(io_data);
- }
- static void devdetails(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- bool io_open = false;
- struct cgpu_info *cgpu;
- int i;
- if (total_devices == 0) {
- message(io_data, MSG_NODEVS, 0, NULL, isjson);
- return;
- }
- message(io_data, MSG_DEVDETAILS, 0, NULL, isjson);
- if (isjson)
- io_open = io_add(io_data, COMSTR JSON_DEVDETAILS);
- for (i = 0; i < total_devices; i++) {
- cgpu = get_devices(i);
- root = api_add_int(root, "DEVDETAILS", &i, false);
- root = api_add_string(root, "Name", cgpu->drv->name, false);
- root = api_add_int(root, "ID", &(cgpu->device_id), false);
- root = api_add_string(root, "Driver", cgpu->drv->dname, false);
- root = api_add_const(root, "Kernel", cgpu->kname ? : BLANK, false);
- root = api_add_const(root, "Model", cgpu->name ? : BLANK, false);
- root = api_add_const(root, "Device Path", cgpu->device_path ? : BLANK, false);
- root = print_data(root, buf, isjson, isjson && (i > 0));
- io_add(io_data, buf);
- }
- if (isjson && io_open)
- io_close(io_data);
- }
- void dosave(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- char filename[PATH_MAX];
- FILE *fcfg;
- char *ptr;
- if (param == NULL || *param == '\0') {
- default_save_file(filename);
- param = filename;
- }
- fcfg = fopen(param, "w");
- if (!fcfg) {
- ptr = escape_string(param, isjson);
- message(io_data, MSG_BADFN, 0, ptr, isjson);
- if (ptr != param)
- free(ptr);
- ptr = NULL;
- return;
- }
- write_config(fcfg);
- fclose(fcfg);
- ptr = escape_string(param, isjson);
- message(io_data, MSG_SAVED, 0, ptr, isjson);
- if (ptr != param)
- free(ptr);
- ptr = NULL;
- }
- static int itemstats(struct io_data *io_data, int i, char *id, struct cgminer_stats *stats, struct cgminer_pool_stats *pool_stats, struct api_data *extra, bool isjson)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- root = api_add_int(root, "STATS", &i, false);
- root = api_add_string(root, "ID", id, false);
- root = api_add_elapsed(root, "Elapsed", &(total_secs), false);
- root = api_add_uint32(root, "Calls", &(stats->getwork_calls), false);
- root = api_add_timeval(root, "Wait", &(stats->getwork_wait), false);
- root = api_add_timeval(root, "Max", &(stats->getwork_wait_max), false);
- root = api_add_timeval(root, "Min", &(stats->getwork_wait_min), false);
- if (pool_stats) {
- root = api_add_uint32(root, "Pool Calls", &(pool_stats->getwork_calls), false);
- root = api_add_uint32(root, "Pool Attempts", &(pool_stats->getwork_attempts), false);
- root = api_add_timeval(root, "Pool Wait", &(pool_stats->getwork_wait), false);
- root = api_add_timeval(root, "Pool Max", &(pool_stats->getwork_wait_max), false);
- root = api_add_timeval(root, "Pool Min", &(pool_stats->getwork_wait_min), false);
- root = api_add_double(root, "Pool Av", &(pool_stats->getwork_wait_rolling), false);
- root = api_add_bool(root, "Work Had Roll Time", &(pool_stats->hadrolltime), false);
- root = api_add_bool(root, "Work Can Roll", &(pool_stats->canroll), false);
- root = api_add_bool(root, "Work Had Expire", &(pool_stats->hadexpire), false);
- root = api_add_uint32(root, "Work Roll Time", &(pool_stats->rolltime), false);
- root = api_add_diff(root, "Work Diff", &(pool_stats->last_diff), false);
- root = api_add_diff(root, "Min Diff", &(pool_stats->min_diff), false);
- root = api_add_diff(root, "Max Diff", &(pool_stats->max_diff), false);
- root = api_add_uint32(root, "Min Diff Count", &(pool_stats->min_diff_count), false);
- root = api_add_uint32(root, "Max Diff Count", &(pool_stats->max_diff_count), false);
- root = api_add_uint64(root, "Times Sent", &(pool_stats->times_sent), false);
- root = api_add_uint64(root, "Bytes Sent", &(pool_stats->bytes_sent), false);
- root = api_add_uint64(root, "Times Recv", &(pool_stats->times_received), false);
- root = api_add_uint64(root, "Bytes Recv", &(pool_stats->bytes_received), false);
- root = api_add_uint64(root, "Net Bytes Sent", &(pool_stats->net_bytes_sent), false);
- root = api_add_uint64(root, "Net Bytes Recv", &(pool_stats->net_bytes_received), false);
- }
- if (extra)
- root = api_add_extra(root, extra);
- root = print_data(root, buf, isjson, isjson && (i > 0));
- io_add(io_data, buf);
- return ++i;
- }
- static void minerstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct cgpu_info *cgpu;
- bool io_open = false;
- struct api_data *extra;
- char id[20];
- int i, j;
- message(io_data, MSG_MINESTATS, 0, NULL, isjson);
- if (isjson)
- io_open = io_add(io_data, COMSTR JSON_MINESTATS);
- i = 0;
- for (j = 0; j < total_devices; j++) {
- cgpu = get_devices(j);
- if (cgpu && cgpu->drv) {
- if (cgpu->drv->get_api_stats)
- extra = cgpu->drv->get_api_stats(cgpu);
- else
- extra = NULL;
- sprintf(id, "%s%d", cgpu->drv->name, cgpu->device_id);
- i = itemstats(io_data, i, id, &(cgpu->cgminer_stats), NULL, extra, isjson);
- }
- }
- for (j = 0; j < total_pools; j++) {
- struct pool *pool = pools[j];
- sprintf(id, "POOL%d", j);
- i = itemstats(io_data, i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, isjson);
- }
- if (isjson && io_open)
- io_close(io_data);
- }
- static void failoveronly(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISBOOL, 0, NULL, isjson);
- return;
- }
- *param = tolower(*param);
- if (*param != 't' && *param != 'f') {
- message(io_data, MSG_INVBOOL, 0, NULL, isjson);
- return;
- }
- bool tf = (*param == 't');
- opt_fail_only = tf;
- message(io_data, MSG_FOO, tf, NULL, isjson);
- }
- static void minecoin(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- bool io_open;
- message(io_data, MSG_MINECOIN, 0, NULL, isjson);
- io_open = io_add(io_data, isjson ? COMSTR JSON_MINECOIN : _MINECOIN COMSTR);
- #ifdef USE_SCRYPT
- if (opt_scrypt)
- root = api_add_const(root, "Hash Method", SCRYPTSTR, false);
- else
- #endif
- root = api_add_const(root, "Hash Method", SHA256STR, false);
- mutex_lock(&ch_lock);
- if (current_fullhash && *current_fullhash) {
- root = api_add_timeval(root, "Current Block Time", &block_timeval, true);
- root = api_add_string(root, "Current Block Hash", current_fullhash, true);
- } else {
- struct timeval t = {0,0};
- root = api_add_timeval(root, "Current Block Time", &t, true);
- root = api_add_const(root, "Current Block Hash", BLANK, false);
- }
- mutex_unlock(&ch_lock);
- root = api_add_bool(root, "LP", &have_longpoll, false);
- root = print_data(root, buf, isjson, false);
- io_add(io_data, buf);
- if (isjson && io_open)
- io_close(io_data);
- }
- static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- bool io_open;
- if (param == NULL)
- param = (char *)BLANK;
- else
- *param = tolower(*param);
- switch(*param) {
- case 's':
- opt_realquiet = true;
- break;
- case 'q':
- opt_quiet ^= true;
- break;
- case 'v':
- opt_log_output ^= true;
- if (opt_log_output)
- opt_quiet = false;
- break;
- case 'd':
- opt_debug ^= true;
- opt_log_output = opt_debug;
- if (opt_debug)
- opt_quiet = false;
- break;
- case 'r':
- opt_protocol ^= true;
- if (opt_protocol)
- opt_quiet = false;
- break;
- case 'p':
- want_per_device_stats ^= true;
- opt_log_output = want_per_device_stats;
- break;
- case 'n':
- opt_log_output = false;
- opt_debug = false;
- opt_quiet = false;
- opt_protocol = false;
- want_per_device_stats = false;
- opt_worktime = false;
- break;
- case 'w':
- opt_worktime ^= true;
- break;
- #ifdef _MEMORY_DEBUG
- case 'y':
- cgmemspeedup();
- break;
- case 'z':
- cgmemrpt();
- break;
- #endif
- default:
- // anything else just reports the settings
- break;
- }
- message(io_data, MSG_DEBUGSET, 0, NULL, isjson);
- io_open = io_add(io_data, isjson ? COMSTR JSON_DEBUGSET : _DEBUGSET COMSTR);
- root = api_add_bool(root, "Silent", &opt_realquiet, false);
- root = api_add_bool(root, "Quiet", &opt_quiet, false);
- root = api_add_bool(root, "Verbose", &opt_log_output, false);
- root = api_add_bool(root, "Debug", &opt_debug, false);
- root = api_add_bool(root, "RPCProto", &opt_protocol, false);
- root = api_add_bool(root, "PerDevice", &want_per_device_stats, false);
- root = api_add_bool(root, "WorkTime", &opt_worktime, false);
- root = print_data(root, buf, isjson, false);
- io_add(io_data, buf);
- if (isjson && io_open)
- io_close(io_data);
- }
- static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- char *comma;
- int value;
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_CONPAR, 0, NULL, isjson);
- return;
- }
- comma = strchr(param, ',');
- if (!comma) {
- message(io_data, MSG_CONVAL, 0, param, isjson);
- return;
- }
- *(comma++) = '\0';
- value = atoi(comma);
- if (value < 0 || value > 9999) {
- message(io_data, MSG_INVNUM, value, param, isjson);
- return;
- }
- if (strcasecmp(param, "queue") == 0)
- opt_queue = value;
- else if (strcasecmp(param, "scantime") == 0)
- opt_scantime = value;
- else if (strcasecmp(param, "expiry") == 0)
- opt_expiry = value;
- else {
- message(io_data, MSG_UNKCON, 0, param, isjson);
- return;
- }
- message(io_data, MSG_SETCONFIG, value, param, isjson);
- }
- static void usbstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct api_data *root = NULL;
- #ifdef USE_USBUTILS
- char buf[TMPBUFSIZ];
- bool io_open = false;
- int count = 0;
- root = api_usb_stats(&count);
- #endif
- if (!root) {
- message(io_data, MSG_NOUSTA, 0, NULL, isjson);
- return;
- }
- #ifdef USE_USBUTILS
- message(io_data, MSG_USBSTA, 0, NULL, isjson);
- if (isjson)
- io_open = io_add(io_data, COMSTR JSON_USBSTATS);
- root = print_data(root, buf, isjson, false);
- io_add(io_data, buf);
- while (42) {
- root = api_usb_stats(&count);
- if (!root)
- break;
- root = print_data(root, buf, isjson, isjson);
- io_add(io_data, buf);
- }
- if (isjson && io_open)
- io_close(io_data);
- #endif
- }
- #ifdef HAVE_AN_FPGA
- static void pgaset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- struct cgpu_info *cgpu;
- struct device_drv *drv;
- char buf[TMPBUFSIZ];
- int numpga = numpgas();
- if (numpga == 0) {
- message(io_data, MSG_PGANON, 0, NULL, isjson);
- return;
- }
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISID, 0, NULL, isjson);
- return;
- }
- char *opt = strchr(param, ',');
- if (opt)
- *(opt++) = '\0';
- if (!opt || !*opt) {
- message(io_data, MSG_MISPGAOPT, 0, NULL, isjson);
- return;
- }
- int id = atoi(param);
- if (id < 0 || id >= numpga) {
- message(io_data, MSG_INVPGA, id, NULL, isjson);
- return;
- }
- int dev = pgadevice(id);
- if (dev < 0) { // Should never happen
- message(io_data, MSG_INVPGA, id, NULL, isjson);
- return;
- }
- cgpu = get_devices(dev);
- drv = cgpu->drv;
- char *set = strchr(opt, ',');
- if (set)
- *(set++) = '\0';
- if (!drv->set_device)
- message(io_data, MSG_PGANOSET, id, NULL, isjson);
- else {
- char *ret = drv->set_device(cgpu, opt, set, buf);
- if (ret) {
- if (strcasecmp(opt, "help") == 0)
- message(io_data, MSG_PGAHELP, id, ret, isjson);
- else
- message(io_data, MSG_PGASETERR, id, ret, isjson);
- } else
- message(io_data, MSG_PGASETOK, id, NULL, isjson);
- }
- }
- #endif
- static void dozero(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_ZERMIS, 0, NULL, isjson);
- return;
- }
- char *sum = strchr(param, ',');
- if (sum)
- *(sum++) = '\0';
- if (!sum || !*sum) {
- message(io_data, MSG_MISBOOL, 0, NULL, isjson);
- return;
- }
- bool all = false;
- bool bs = false;
- if (strcasecmp(param, "all") == 0)
- all = true;
- else if (strcasecmp(param, "bestshare") == 0)
- bs = true;
- if (all == false && bs == false) {
- message(io_data, MSG_ZERINV, 0, param, isjson);
- return;
- }
- *sum = tolower(*sum);
- if (*sum != 't' && *sum != 'f') {
- message(io_data, MSG_INVBOOL, 0, NULL, isjson);
- return;
- }
- bool dosum = (*sum == 't');
- if (dosum)
- print_summary();
- if (all)
- zero_stats();
- if (bs)
- zero_bestshare();
- if (dosum)
- message(io_data, MSG_ZERSUM, 0, all ? "All" : "BestShare", isjson);
- else
- message(io_data, MSG_ZERNOSUM, 0, all ? "All" : "BestShare", isjson);
- }
- static void dohotplug(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- #ifdef USE_USBUTILS
- int value;
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISHPLG, 0, NULL, isjson);
- return;
- }
- value = atoi(param);
- if (value < 0 || value > 9999) {
- message(io_data, MSG_INVHPLG, 0, param, isjson);
- return;
- }
- hotplug_time = value;
- if (value)
- message(io_data, MSG_HOTPLUG, value, NULL, isjson);
- else
- message(io_data, MSG_DISHPLG, 0, NULL, isjson);
- #else
- message(io_data, MSG_NOHPLG, 0, NULL, isjson);
- return;
- #endif
- }
- static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group);
- struct CMDS {
- char *name;
- void (*func)(struct io_data *, SOCKETTYPE, char *, bool, char);
- bool iswritemode;
- } cmds[] = {
- { "version", apiversion, false },
- { "config", minerconfig, false },
- { "devs", devstatus, false },
- { "pools", poolstatus, false },
- { "summary", summary, false },
- #ifdef HAVE_OPENCL
- { "gpuenable", gpuenable, true },
- { "gpudisable", gpudisable, true },
- { "gpurestart", gpurestart, true },
- { "gpu", gpudev, false },
- #endif
- #ifdef HAVE_AN_FPGA
- { "pga", pgadev, false },
- { "pgaenable", pgaenable, true },
- { "pgadisable", pgadisable, true },
- { "pgaidentify", pgaidentify, true },
- #endif
- #ifdef WANT_CPUMINE
- { "cpu", cpudev, false },
- #endif
- { "gpucount", gpucount, false },
- { "pgacount", pgacount, false },
- { "cpucount", cpucount, false },
- { "switchpool", switchpool, true },
- { "addpool", addpool, true },
- { "poolpriority", poolpriority, true },
- { "enablepool", enablepool, true },
- { "disablepool", disablepool, true },
- { "removepool", removepool, true },
- #ifdef HAVE_OPENCL
- { "gpuintensity", gpuintensity, true },
- { "gpumem", gpumem, true },
- { "gpuengine", gpuengine, true },
- { "gpufan", gpufan, true },
- { "gpuvddc", gpuvddc, true },
- #endif
- { "save", dosave, true },
- { "quit", doquit, true },
- { "privileged", privileged, true },
- { "notify", notify, false },
- { "devdetails", devdetails, false },
- { "restart", dorestart, true },
- { "stats", minerstats, false },
- { "check", checkcommand, false },
- { "failover-only", failoveronly, true },
- { "coin", minecoin, false },
- { "debug", debugstate, true },
- { "setconfig", setconfig, true },
- { "usbstats", usbstats, false },
- #ifdef HAVE_AN_FPGA
- { "pgaset", pgaset, true },
- #endif
- { "zero", dozero, true },
- { "hotplug", dohotplug, true },
- { NULL, NULL, false }
- };
- static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- bool io_open;
- char cmdbuf[100];
- bool found, access;
- int i;
- if (param == NULL || *param == '\0') {
- message(io_data, MSG_MISCHK, 0, NULL, isjson);
- return;
- }
- found = false;
- access = false;
- for (i = 0; cmds[i].name != NULL; i++) {
- if (strcmp(cmds[i].name, param) == 0) {
- found = true;
- sprintf(cmdbuf, "|%s|", param);
- if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
- access = true;
- break;
- }
- }
- message(io_data, MSG_CHECK, 0, NULL, isjson);
- io_open = io_add(io_data, isjson ? COMSTR JSON_CHECK : _CHECK COMSTR);
- root = api_add_const(root, "Exists", found ? YES : NO, false);
- root = api_add_const(root, "Access", access ? YES : NO, false);
- root = print_data(root, buf, isjson, false);
- io_add(io_data, buf);
- if (isjson && io_open)
- io_close(io_data);
- }
- static void send_result(struct io_data *io_data, SOCKETTYPE c, bool isjson)
- {
- char buf[SOCKBUFSIZ + sizeof(JSON_CLOSE) + sizeof(JSON_END)];
- int len;
- int n;
- strcpy(buf, io_data->ptr);
- if (io_data->close)
- strcat(buf, JSON_CLOSE);
- if (isjson) {
- if (io_data->full)
- strcat(buf, JSON_END_TRUNCATED);
- else
- strcat(buf, JSON_END);
- }
- len = strlen(buf);
- applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, buf, len > 10 ? "..." : BLANK);
- // ignore failure - it's closed immediately anyway
- n = send(c, buf, len+1, 0);
- if (SOCKETFAIL(n))
- applog(LOG_WARNING, "API: send failed: %s", SOCKERRMSG);
- else
- applog(LOG_DEBUG, "API: sent %d", n);
- }
- static void tidyup(__maybe_unused void *arg)
- {
- mutex_lock(&quit_restart_lock);
- bye = true;
- if (sock != INVSOCK) {
- shutdown(sock, SHUT_RDWR);
- CLOSESOCKET(sock);
- sock = INVSOCK;
- }
- if (ipaccess != NULL) {
- free(ipaccess);
- ipaccess = NULL;
- }
- io_free();
- mutex_unlock(&quit_restart_lock);
- }
- /*
- * Interpret --api-groups G:cmd1:cmd2:cmd3,P:cmd4,*,...
- */
- static void setup_groups()
- {
- char *api_groups = opt_api_groups ? opt_api_groups : (char *)BLANK;
- char *buf, *ptr, *next, *colon;
- char group;
- char commands[TMPBUFSIZ];
- char cmdbuf[100];
- char *cmd;
- bool addstar, did;
- int i;
- buf = malloc(strlen(api_groups) + 1);
- if (unlikely(!buf))
- quit(1, "Failed to malloc ipgroups buf");
- strcpy(buf, api_groups);
- next = buf;
- // for each group defined
- while (next && *next) {
- ptr = next;
- next = strchr(ptr, ',');
- if (next)
- *(next++) = '\0';
- // Validate the group
- if (*(ptr+1) != ':') {
- colon = strchr(ptr, ':');
- if (colon)
- *colon = '\0';
- applog(LOG_WARNING, "API invalid group name '%s'", ptr);
- quit(1, INVAPIGROUPS);
- }
- group = GROUP(*ptr);
- if (!VALIDGROUP(group)) {
- applog(LOG_WARNING, "API invalid group name '%c'", *ptr);
- quit(1, INVAPIGROUPS);
- }
- if (group == PRIVGROUP) {
- applog(LOG_WARNING, "API group name can't be '%c'", PRIVGROUP);
- quit(1, INVAPIGROUPS);
- }
- if (group == NOPRIVGROUP) {
- applog(LOG_WARNING, "API group name can't be '%c'", NOPRIVGROUP);
- quit(1, INVAPIGROUPS);
- }
- if (apigroups[GROUPOFFSET(group)].commands != NULL) {
- applog(LOG_WARNING, "API duplicate group name '%c'", *ptr);
- quit(1, INVAPIGROUPS);
- }
- ptr += 2;
- // Validate the command list (and handle '*')
- cmd = &(commands[0]);
- *(cmd++) = SEPARATOR;
- *cmd = '\0';
- addstar = false;
- while (ptr && *ptr) {
- colon = strchr(ptr, ':');
- if (colon)
- *(colon++) = '\0';
- if (strcmp(ptr, "*") == 0)
- addstar = true;
- else {
- did = false;
- for (i = 0; cmds[i].name != NULL; i++) {
- if (strcasecmp(ptr, cmds[i].name) == 0) {
- did = true;
- break;
- }
- }
- if (did) {
- // skip duplicates
- sprintf(cmdbuf, "|%s|", cmds[i].name);
- if (strstr(commands, cmdbuf) == NULL) {
- strcpy(cmd, cmds[i].name);
- cmd += strlen(cmds[i].name);
- *(cmd++) = SEPARATOR;
- *cmd = '\0';
- }
- } else {
- applog(LOG_WARNING, "API unknown command '%s' in group '%c'", ptr, group);
- quit(1, INVAPIGROUPS);
- }
- }
- ptr = colon;
- }
- // * = allow all non-iswritemode commands
- if (addstar) {
- for (i = 0; cmds[i].name != NULL; i++) {
- if (cmds[i].iswritemode == false) {
- // skip duplicates
- sprintf(cmdbuf, "|%s|", cmds[i].name);
- if (strstr(commands, cmdbuf) == NULL) {
- strcpy(cmd, cmds[i].name);
- cmd += strlen(cmds[i].name);
- *(cmd++) = SEPARATOR;
- *cmd = '\0';
- }
- }
- }
- }
- ptr = apigroups[GROUPOFFSET(group)].commands = malloc(strlen(commands) + 1);
- if (unlikely(!ptr))
- quit(1, "Failed to malloc group commands buf");
- strcpy(ptr, commands);
- }
- // Now define R (NOPRIVGROUP) as all non-iswritemode commands
- cmd = &(commands[0]);
- *(cmd++) = SEPARATOR;
- *cmd = '\0';
- for (i = 0; cmds[i].name != NULL; i++) {
- if (cmds[i].iswritemode == false) {
- strcpy(cmd, cmds[i].name);
- cmd += strlen(cmds[i].name);
- *(cmd++) = SEPARATOR;
- *cmd = '\0';
- }
- }
- ptr = apigroups[GROUPOFFSET(NOPRIVGROUP)].commands = malloc(strlen(commands) + 1);
- if (unlikely(!ptr))
- quit(1, "Failed to malloc noprivgroup commands buf");
- strcpy(ptr, commands);
- // W (PRIVGROUP) is handled as a special case since it simply means all commands
- free(buf);
- return;
- }
- /*
- * Interpret [W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
- * special case of 0/0 allows /0 (means all IP addresses)
- */
- #define ALLIP4 "0/0"
- /*
- * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
- */
- static void setup_ipaccess()
- {
- char *buf, *ptr, *comma, *slash, *dot;
- int ipcount, mask, octet, i;
- char group;
- buf = malloc(strlen(opt_api_allow) + 1);
- if (unlikely(!buf))
- quit(1, "Failed to malloc ipaccess buf");
- strcpy(buf, opt_api_allow);
- ipcount = 1;
- ptr = buf;
- while (*ptr)
- if (*(ptr++) == ',')
- ipcount++;
- // possibly more than needed, but never less
- ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
- if (unlikely(!ipaccess))
- quit(1, "Failed to calloc ipaccess");
- ips = 0;
- ptr = buf;
- while (ptr && *ptr) {
- while (*ptr == ' ' || *ptr == '\t')
- ptr++;
- if (*ptr == ',') {
- ptr++;
- continue;
- }
- comma = strchr(ptr, ',');
- if (comma)
- *(comma++) = '\0';
- group = NOPRIVGROUP;
- if (isalpha(*ptr) && *(ptr+1) == ':') {
- if (DEFINEDGROUP(*ptr))
- group = GROUP(*ptr);
- ptr += 2;
- }
- ipaccess[ips].group = group;
- if (strcmp(ptr, ALLIP4) == 0)
- ipaccess[ips].ip = ipaccess[ips].mask = 0;
- else {
- slash = strchr(ptr, '/');
- if (!slash)
- ipaccess[ips].mask = 0xffffffff;
- else {
- *(slash++) = '\0';
- mask = atoi(slash);
- if (mask < 1 || mask > 32)
- goto popipo; // skip invalid/zero
- ipaccess[ips].mask = 0;
- while (mask-- >= 0) {
- octet = 1 << (mask % 8);
- ipaccess[ips].mask |= (octet << (24 - (8 * (mask >> 3))));
- }
- }
- ipaccess[ips].ip = 0; // missing default to '.0'
- for (i = 0; ptr && (i < 4); i++) {
- dot = strchr(ptr, '.');
- if (dot)
- *(dot++) = '\0';
- octet = atoi(ptr);
- if (octet < 0 || octet > 0xff)
- goto popipo; // skip invalid
- ipaccess[ips].ip |= (octet << (24 - (i * 8)));
- ptr = dot;
- }
- ipaccess[ips].ip &= ipaccess[ips].mask;
- }
- ips++;
- popipo:
- ptr = comma;
- }
- free(buf);
- }
- static void *quit_thread(__maybe_unused void *userdata)
- {
- // allow thread creator to finish whatever it's doing
- mutex_lock(&quit_restart_lock);
- mutex_unlock(&quit_restart_lock);
- if (opt_debug)
- applog(LOG_DEBUG, "API: killing cgminer");
- kill_work();
- return NULL;
- }
- static void *restart_thread(__maybe_unused void *userdata)
- {
- // allow thread creator to finish whatever it's doing
- mutex_lock(&quit_restart_lock);
- mutex_unlock(&quit_restart_lock);
- if (opt_debug)
- applog(LOG_DEBUG, "API: restarting cgminer");
- app_restart();
- return NULL;
- }
- void api(int api_thr_id)
- {
- struct io_data *io_data;
- struct thr_info bye_thr;
- char buf[TMPBUFSIZ];
- char param_buf[TMPBUFSIZ];
- const char *localaddr = "127.0.0.1";
- SOCKETTYPE c;
- int n, bound;
- char *connectaddr;
- char *binderror;
- time_t bindstart;
- short int port = opt_api_port;
- struct sockaddr_in serv;
- struct sockaddr_in cli;
- socklen_t clisiz;
- char cmdbuf[100];
- char *cmd;
- char *param;
- bool addrok;
- char group;
- json_error_t json_err;
- json_t *json_config;
- json_t *json_val;
- bool isjson;
- bool did;
- int i;
- if (!opt_api_listen) {
- applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
- return;
- }
- io_data = sock_io_new();
- mutex_init(&quit_restart_lock);
- pthread_cleanup_push(tidyup, NULL);
- my_thr_id = api_thr_id;
- setup_groups();
- if (opt_api_allow) {
- setup_ipaccess();
- if (ips == 0) {
- applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
- return;
- }
- }
- /* This should be done before curl in needed
- * to ensure curl has already called WSAStartup() in windows */
- nmsleep(opt_log_interval*1000);
- sock = socket(AF_INET, SOCK_STREAM, 0);
- if (sock == INVSOCK) {
- applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
- return;
- }
- memset(&serv, 0, sizeof(serv));
- serv.sin_family = AF_INET;
- if (!opt_api_allow && !opt_api_network) {
- serv.sin_addr.s_addr = inet_addr(localaddr);
- if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
- applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
- return;
- }
- }
- serv.sin_port = htons(port);
- #ifndef WIN32
- // On linux with SO_REUSEADDR, bind will get the port if the previous
- // socket is closed (even if it is still in TIME_WAIT) but fail if
- // another program has it open - which is what we want
- int optval = 1;
- // If it doesn't work, we don't really care - just show a debug message
- if (SOCKETFAIL(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval))))
- applog(LOG_DEBUG, "API setsockopt SO_REUSEADDR failed (ignored): %s", SOCKERRMSG);
- #else
- // On windows a 2nd program can bind to a port>1024 already in use unless
- // SO_EXCLUSIVEADDRUSE is used - however then the bind to a closed port
- // in TIME_WAIT will fail until the timeout - so we leave the options alone
- #endif
- // try for more than 1 minute ... in case the old one hasn't completely gone yet
- bound = 0;
- bindstart = time(NULL);
- while (bound == 0) {
- if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
- binderror = SOCKERRMSG;
- if ((time(NULL) - bindstart) > 61)
- break;
- else {
- applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
- nmsleep(30000);
- }
- } else
- bound = 1;
- }
- if (bound == 0) {
- applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
- return;
- }
- if (SOCKETFAIL(listen(sock, QUEUE))) {
- applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
- CLOSESOCKET(sock);
- return;
- }
- if (opt_api_allow)
- applog(LOG_WARNING, "API running in IP access mode on port %d", port);
- else {
- if (opt_api_network)
- applog(LOG_WARNING, "API running in UNRESTRICTED read access mode on port %d", port);
- else
- applog(LOG_WARNING, "API running in local read access mode on port %d", port);
- }
- while (!bye) {
- clisiz = sizeof(cli);
- if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
- applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
- goto die;
- }
- connectaddr = inet_ntoa(cli.sin_addr);
- addrok = false;
- group = NOPRIVGROUP;
- if (opt_api_allow) {
- int client_ip = htonl(cli.sin_addr.s_addr);
- for (i = 0; i < ips; i++) {
- if ((client_ip & ipaccess[i].mask) == ipaccess[i].ip) {
- addrok = true;
- group = ipaccess[i].group;
- break;
- }
- }
- } else {
- if (opt_api_network)
- addrok = true;
- else
- addrok = (strcmp(connectaddr, localaddr) == 0);
- }
- if (opt_debug)
- applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
- if (addrok) {
- n = recv(c, &buf[0], TMPBUFSIZ-1, 0);
- if (SOCKETFAIL(n))
- buf[0] = '\0';
- else
- buf[n] = '\0';
- if (opt_debug) {
- if (SOCKETFAIL(n))
- applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
- else
- applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
- }
- if (!SOCKETFAIL(n)) {
- // the time of the request in now
- when = time(NULL);
- io_reinit(io_data);
- did = false;
- if (*buf != ISJSON) {
- isjson = false;
- param = strchr(buf, SEPARATOR);
- if (param != NULL)
- *(param++) = '\0';
- cmd = buf;
- }
- else {
- isjson = true;
- param = NULL;
- #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
- json_config = json_loadb(buf, n, 0, &json_err);
- #elif JANSSON_MAJOR_VERSION > 1
- json_config = json_loads(buf, 0, &json_err);
- #else
- json_config = json_loads(buf, &json_err);
- #endif
- if (!json_is_object(json_config)) {
- message(io_data, MSG_INVJSON, 0, NULL, isjson);
- send_result(io_data, c, isjson);
- did = true;
- }
- else {
- json_val = json_object_get(json_config, JSON_COMMAND);
- if (json_val == NULL) {
- message(io_data, MSG_MISCMD, 0, NULL, isjson);
- send_result(io_data, c, isjson);
- did = true;
- }
- else {
- if (!json_is_string(json_val)) {
- message(io_data, MSG_INVCMD, 0, NULL, isjson);
- send_result(io_data, c, isjson);
- did = true;
- }
- else {
- cmd = (char *)json_string_value(json_val);
- json_val = json_object_get(json_config, JSON_PARAMETER);
- if (json_is_string(json_val))
- param = (char *)json_string_value(json_val);
- else if (json_is_integer(json_val)) {
- sprintf(param_buf, "%d", (int)json_integer_value(json_val));
- param = param_buf;
- } else if (json_is_real(json_val)) {
- sprintf(param_buf, "%f", (double)json_real_value(json_val));
- param = param_buf;
- }
- }
- }
- }
- }
- if (!did)
- for (i = 0; cmds[i].name != NULL; i++) {
- if (strcmp(cmd, cmds[i].name) == 0) {
- sprintf(cmdbuf, "|%s|", cmd);
- if (ISPRIVGROUP(group) || strstr(COMMANDS(group), cmdbuf))
- (cmds[i].func)(io_data, c, param, isjson, group);
- else {
- message(io_data, MSG_ACCDENY, 0, cmds[i].name, isjson);
- applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
- }
- send_result(io_data, c, isjson);
- did = true;
- break;
- }
- }
- if (!did) {
- message(io_data, MSG_INVCMD, 0, NULL, isjson);
- send_result(io_data, c, isjson);
- }
- }
- }
- CLOSESOCKET(c);
- }
- die:
- /* Blank line fix for older compilers since pthread_cleanup_pop is a
- * macro that gets confused by a label existing immediately before it
- */
- ;
- pthread_cleanup_pop(true);
- if (opt_debug)
- applog(LOG_DEBUG, "API: terminating due to: %s",
- do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));
- mutex_lock(&quit_restart_lock);
- if (do_a_restart) {
- if (thr_info_create(&bye_thr, NULL, restart_thread, &bye_thr)) {
- mutex_unlock(&quit_restart_lock);
- quit(1, "API failed to initiate a restart - aborting");
- }
- pthread_detach(bye_thr.pth);
- } else if (do_a_quit) {
- if (thr_info_create(&bye_thr, NULL, quit_thread, &bye_thr)) {
- mutex_unlock(&quit_restart_lock);
- quit(1, "API failed to initiate a clean quit - aborting");
- }
- pthread_detach(bye_thr.pth);
- }
- mutex_unlock(&quit_restart_lock);
- }
|