| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276 |
- /*
- * Copyright 2011-2013 Andrew Smith
- * Copyright 2011-2013 Con Kolivas
- * Copyright 2012-2013 Luke Dashjr
- *
- * 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 "deviceapi.h"
- #ifdef USE_LIBMICROHTTPD
- #include "httpsrv.h"
- #endif
- #include "miner.h"
- #include "util.h"
- #include "driver-cpu.h" /* for algo_names[], TODO: re-factor dependency */
- #define HAVE_AN_FPGA 1
- // 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
- static const char *UNAVAILABLE = " - API will not be available";
- static const char *MUNAVAILABLE = " - API multicast listener will not be available";
- static const char *BLANK = "";
- static const char *COMMA = ",";
- #define COMSTR ","
- static const char SEPARATOR = '|';
- #define SEPSTR "|"
- static const char GPUSEP = ',';
- static const char *APIVERSION = "2.1";
- static const char *DEAD = "Dead";
- static const char *SICK = "Sick";
- static const char *NOSTART = "NoStart";
- static const char *INIT = "Initialising";
- static const char *WAIT = "Waiting";
- 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 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 *OSINFO =
- #if defined(__linux)
- "Linux";
- #else
- #if defined(__APPLE__)
- "Apple";
- #else
- #if defined (__CYGWIN__)
- "Cygwin";
- #elif 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"
- 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_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
- #define MSG_ALRENAC 98
- #define MSG_ALRDISC 99
- #define MSG_CPUMRE 100
- #define MSG_CPUREN 101
- #define MSG_CPUDIS 102
- #define MSG_CPUREI 103
- #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
- #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_DEVSCAN 0x100
- #define MSG_INVNEG 121
- #define MSG_SETQUOTA 122
- enum code_severity {
- SEVERITY_ERR,
- SEVERITY_WARN,
- SEVERITY_INFO,
- SEVERITY_SUCC,
- SEVERITY_FAIL
- };
- enum code_parameters {
- PARAM_COUNT,
- 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_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" },
- { SEVERITY_INFO, MSG_ALRENAC, PARAM_CPU, "CPU %d already enabled" },
- { SEVERITY_INFO, MSG_ALRDISC, PARAM_CPU, "CPU %d already disabled" },
- { SEVERITY_WARN, MSG_CPUMRE, PARAM_CPU, "CPU %d must be restarted first" },
- { SEVERITY_INFO, MSG_CPUREN, PARAM_CPU, "CPU %d sent enable message" },
- { SEVERITY_INFO, MSG_CPUDIS, PARAM_CPU, "CPU %d set disable flag" },
- { SEVERITY_INFO, MSG_CPUREI, PARAM_CPU, "CPU %d restart attempted" },
- #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, "BFGMiner 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_SHA_INTENSITY_STR " - " MAX_SCRYPT_INTENSITY_STR },
- { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
- { SEVERITY_SUCC, MSG_MINECONFIG,PARAM_NONE, "BFGMiner 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, "BFGMiner 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, "BFGMiner 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_INVNEG, PARAM_BOTH, "Invalid negative number (%d) for '%s'" },
- { SEVERITY_SUCC, MSG_SETQUOTA,PARAM_SET, "Set pool '%s' to quota %d'" },
- { SEVERITY_ERR, MSG_CONPAR, PARAM_NONE, "Missing config parameters 'name,N'" },
- { SEVERITY_ERR, MSG_CONVAL, PARAM_STR, "Missing config value N for '%s,N'" },
- #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" },
- { SEVERITY_SUCC, MSG_DEVSCAN, PARAM_COUNT, "Added %d new device(s)" },
- { SEVERITY_FAIL, 0, 0, NULL }
- };
- static const char *localaddr = "127.0.0.1";
- static int my_thr_id = 0;
- static bool bye;
- // 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
- static bool per_proc;
- 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;
- #ifdef HAVE_OPENCL
- extern struct device_drv opencl_api;
- #endif
- #ifdef WANT_CPUMINE
- extern struct device_drv cpu_drv;
- #endif
- struct io_data {
- bytes_t data;
- SOCKETTYPE sock;
-
- // Whether to add various things
- bool close;
- };
- static struct io_data *rpc_io_data;
- static void io_reinit(struct io_data *io_data)
- {
- bytes_reset(&io_data->data);
- io_data->close = false;
- }
- static
- struct io_data *sock_io_new()
- {
- struct io_data *io_data = malloc(sizeof(struct io_data));
- bytes_init(&io_data->data);
- io_data->sock = INVSOCK;
- io_reinit(io_data);
- return io_data;
- }
- static
- size_t io_flush(struct io_data *io_data, bool complete)
- {
- size_t sent = 0, tosend = bytes_len(&io_data->data);
- ssize_t n;
- struct timeval timeout = {0, complete ? 50000: 0}, tv;
- fd_set wd;
- int count = 0;
-
- while (tosend)
- {
- FD_ZERO(&wd);
- FD_SET(io_data->sock, &wd);
- tv = timeout;
- if (select(io_data->sock + 1, NULL, &wd, NULL, &tv) < 1)
- break;
-
- n = send(io_data->sock, (void*)&bytes_buf(&io_data->data)[sent], tosend, 0);
- if (SOCKETFAIL(n))
- {
- if (!sock_blocks())
- applog(LOG_WARNING, "API: send (%lu) failed: %s", (unsigned long)tosend, SOCKERRMSG);
- break;
- }
- if (count <= 1)
- {
- if (n == tosend)
- applog(LOG_DEBUG, "API: sent all of %lu first go", (unsigned long)tosend);
- else
- applog(LOG_DEBUG, "API: sent %ld of %lu first go", (long)n, (unsigned long)tosend);
- }
- else
- {
- if (n == tosend)
- applog(LOG_DEBUG, "API: sent all of remaining %lu (count=%d)", (unsigned long)tosend, count);
- else
- applog(LOG_DEBUG, "API: sent %ld of remaining %lu (count=%d)", (long)n, (unsigned long)tosend, count);
- }
- sent += n;
- tosend -= n;
- }
-
- bytes_shift(&io_data->data, sent);
-
- return sent;
- }
- static bool io_add(struct io_data *io_data, char *buf)
- {
- size_t len = strlen(buf);
- if (bytes_len(&io_data->data) + len > SOCKBUFSIZ)
- io_flush(io_data, false);
- bytes_append(&io_data->data, buf, len);
- return true;
- }
- static bool io_put(struct io_data *io_data, char *buf)
- {
- bytes_reset(&io_data->data);
- return io_add(io_data, buf);
- }
- static void io_close(struct io_data *io_data)
- {
- io_data->close = true;
- }
- static void io_free()
- {
- bytes_free(&rpc_io_data->data);
- free(rpc_io_data);
- rpc_io_data = 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 = strdup(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;
- if (type == API_JSON)
- json_incref((json_t *)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:
- case API_PERCENT:
- 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;
- case API_JSON:
- api_data->data_was_malloc = false;
- api_data->data = (void *)json_deep_copy((json_t *)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, const 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);
- }
- struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_JSON, (void *)data, copy_data);
- }
- struct api_data *api_add_percent(struct api_data *root, char *name, double *data, bool copy_data)
- {
- return api_add_data_full(root, name, API_PERCENT, (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, "%.3f", *((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, "%"PRIu64".%06lu",
- (uint64_t)((struct timeval *)(root->data))->tv_sec,
- (unsigned long)((struct timeval *)(root->data))->tv_usec);
- break;
- case API_TEMP:
- sprintf(buf, "%.2f", *((float *)(root->data)));
- break;
- case API_JSON:
- escape = json_dumps((json_t *)(root->data), JSON_COMPACT);
- strcpy(buf, escape);
- free(escape);
- break;
- case API_PERCENT:
- sprintf(buf, "%.4f", *((double *)(root->data)) * 100.0);
- 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');
- free(root->name);
- if (root->type == API_JSON)
- json_decref((json_t *)root->data);
- 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 HAVE_OPENCL
- if (devices[i]->drv == &opencl_api)
- continue;
- #endif
- #ifdef WANT_CPUMINE
- if (devices[i]->drv == &cpu_drv)
- continue;
- #endif
- if (devices[i]->device != devices[i] && !per_proc)
- continue;
- ++count;
- }
- 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 HAVE_OPENCL
- if (devices[i]->drv == &opencl_api)
- continue;
- #endif
- #ifdef WANT_CPUMINE
- if (devices[i]->drv == &cpu_drv)
- continue;
- #endif
- if (devices[i]->device != devices[i] && !per_proc)
- continue;
- ++count;
- 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_COUNT:
- case PARAM_GPU:
- case PARAM_PGA:
- case PARAM_CPU:
- case PARAM_PID:
- 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;
- struct driver_registration *reg, *regtmp;
- 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);
-
- strcpy(buf, ""
- #ifdef USE_LIBMICROHTTPD
- " SGW"
- #endif
- #ifdef USE_LIBEVENT
- " SSM"
- #endif
- );
- BFG_FOREACH_DRIVER_BY_DNAME(reg, regtmp)
- {
- const struct device_drv * const drv = reg->drv;
- tailsprintf(buf, sizeof(buf), " %s", drv->name);
- }
- root = api_add_const(root, "Device Code", &buf[1], true);
-
- 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);
- #if BLKMAKER_VERSION > 0
- root = api_add_string(root, "Coinbase-Sig", opt_coinbase_sig, true);
- #endif
- root = print_data(root, buf, isjson, false);
- io_add(io_data, buf);
- if (isjson && io_open)
- io_close(io_data);
- }
- static const char*
- bool2str(bool b)
- {
- return b ? YES : NO;
- }
- static const char *status2str(enum alive status)
- {
- switch (status) {
- case LIFE_WELL:
- return ALIVE;
- case LIFE_SICK:
- return SICK;
- case LIFE_DEAD:
- case LIFE_DEAD2:
- return DEAD;
- case LIFE_NOSTART:
- return NOSTART;
- case LIFE_INIT:
- case LIFE_INIT2:
- return INIT;
- case LIFE_WAIT:
- return WAIT;
- case LIFE_MIXED:
- return "Mixed";
- default:
- return UNKNOWN;
- }
- }
- static
- struct api_data *api_add_device_identifier(struct api_data *root, struct cgpu_info *cgpu)
- {
- root = api_add_string(root, "Name", cgpu->drv->name, false);
- root = api_add_int(root, "ID", &(cgpu->device_id), false);
- if (per_proc)
- root = api_add_int(root, "ProcID", &(cgpu->proc_id), false);
- return root;
- }
- static
- int find_index_by_cgpu(struct cgpu_info *cgpu)
- {
- int n = 0, i;
-
- rd_lock(&devices_lock);
- for (i = 0; i < total_devices; ++i)
- {
- if (devices[i] == cgpu)
- break;
- if (devices[i]->device != devices[i] && !per_proc)
- continue;
- if (cgpu->devtype == devices[i]->devtype)
- ++n;
- }
- rd_unlock(&devices_lock);
- return n;
- }
- static void devdetail_an(struct io_data *io_data, struct cgpu_info *cgpu, bool isjson, bool precom)
- {
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- int n;
- cgpu_utility(cgpu);
- n = find_index_by_cgpu(cgpu);
- root = api_add_int(root, "DEVDETAILS", &n, true);
- root = api_add_device_identifier(root, cgpu);
- root = api_add_string(root, "Driver", cgpu->drv->dname, false);
- if (cgpu->kname)
- root = api_add_string(root, "Kernel", cgpu->kname, false);
- if (cgpu->name)
- root = api_add_string(root, "Model", cgpu->name, false);
- if (cgpu->device_path)
- root = api_add_string(root, "Device Path", cgpu->device_path, false);
- if (cgpu->drv->get_api_extra_device_detail)
- root = api_add_extra(root, cgpu->drv->get_api_extra_device_detail(cgpu));
- root = print_data(root, buf, isjson, precom);
- io_add(io_data, buf);
- }
- static
- void devstatus_an(struct io_data *io_data, struct cgpu_info *cgpu, bool isjson, bool precom)
- {
- struct cgpu_info *proc;
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- int n;
- n = find_index_by_cgpu(cgpu);
- double runtime = cgpu_runtime(cgpu);
- bool enabled = false;
- double total_mhashes = 0, rolling = 0, utility = 0;
- enum alive status = cgpu->status;
- float temp = -1;
- int accepted = 0, rejected = 0, stale = 0, hw_errors = 0;
- int diff1 = 0, bad_nonces = 0;
- double diff_accepted = 0, diff_rejected = 0, diff_stale = 0;
- int last_share_pool = -1;
- time_t last_share_pool_time = -1, last_device_valid_work = -1;
- double last_share_diff = -1;
- int procs = per_proc ? 1 : cgpu->procs, i;
- for (i = 0, proc = cgpu; i < procs; ++i, proc = proc->next_proc)
- {
- cgpu_utility(proc);
- if (proc->deven != DEV_DISABLED)
- enabled = true;
- total_mhashes += proc->total_mhashes;
- rolling += proc->rolling;
- utility += proc->utility;
- accepted += proc->accepted;
- rejected += proc->rejected;
- stale += proc->stale;
- hw_errors += proc->hw_errors;
- diff1 += proc->diff1;
- diff_accepted += proc->diff_accepted;
- diff_rejected += proc->diff_rejected;
- diff_stale += proc->diff_stale;
- bad_nonces += proc->bad_nonces;
- if (status != proc->status)
- status = LIFE_MIXED;
- if (proc->temp > temp)
- temp = proc->temp;
- if (proc->last_share_pool_time > last_share_pool_time)
- {
- last_share_pool_time = proc->last_share_pool_time;
- last_share_pool = proc->last_share_pool;
- last_share_diff = proc->last_share_diff;
- }
- if (proc->last_device_valid_work > last_device_valid_work)
- last_device_valid_work = proc->last_device_valid_work;
- if (per_proc)
- break;
- }
- root = api_add_int(root, (char*)cgpu->devtype, &n, true);
- root = api_add_device_identifier(root, cgpu);
- root = api_add_string(root, "Enabled", bool2str(enabled), false);
- root = api_add_string(root, "Status", status2str(status), false);
- if (temp > 0)
- root = api_add_temp(root, "Temperature", &temp, false);
-
- root = api_add_elapsed(root, "Device Elapsed", &runtime, false);
- double mhs = total_mhashes / runtime;
- 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, &rolling, false);
- root = api_add_int(root, "Accepted", &accepted, false);
- root = api_add_int(root, "Rejected", &rejected, false);
- root = api_add_int(root, "Hardware Errors", &hw_errors, false);
- root = api_add_utility(root, "Utility", &utility, false);
- root = api_add_int(root, "Stale", &stale, false);
- if (last_share_pool != -1)
- {
- root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
- root = api_add_time(root, "Last Share Time", &last_share_pool_time, false);
- }
- root = api_add_mhtotal(root, "Total MH", &total_mhashes, false);
- double work_utility = diff1 / runtime * 60;
- root = api_add_int(root, "Diff1 Work", &diff1, false);
- root = api_add_utility(root, "Work Utility", &work_utility, false);
- root = api_add_diff(root, "Difficulty Accepted", &diff_accepted, false);
- root = api_add_diff(root, "Difficulty Rejected", &diff_rejected, false);
- root = api_add_diff(root, "Difficulty Stale", &diff_stale, false);
- if (last_share_diff > 0)
- root = api_add_diff(root, "Last Share Difficulty", &last_share_diff, false);
- if (last_device_valid_work != -1)
- root = api_add_time(root, "Last Valid Work", &last_device_valid_work, false);
- double hwp = (bad_nonces + diff1) ?
- (double)(bad_nonces) / (double)(bad_nonces + diff1) : 0;
- root = api_add_percent(root, "Device Hardware%", &hwp, false);
- double rejp = diff1 ?
- (double)(diff_rejected) / (double)(diff1) : 0;
- root = api_add_percent(root, "Device Rejected%", &rejp, false);
- if ((per_proc || cgpu->procs <= 1) && cgpu->drv->get_api_extra_device_status)
- root = api_add_extra(root, cgpu->drv->get_api_extra_device_status(cgpu));
- root = print_data(root, buf, isjson, precom);
- io_add(io_data, buf);
- }
- #ifdef HAVE_OPENCL
- static void gpustatus(struct io_data *io_data, int gpu, bool isjson, bool precom)
- {
- if (gpu < 0 || gpu >= nDevs)
- return;
- devstatus_an(io_data, &gpus[gpu], isjson, precom);
- }
- #endif
- #ifdef HAVE_AN_FPGA
- static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom)
- {
- int dev = pgadevice(pga);
- if (dev < 0) // Should never happen
- return;
- devstatus_an(io_data, get_devices(dev), isjson, precom);
- }
- #endif
- #ifdef WANT_CPUMINE
- static void cpustatus(struct io_data *io_data, int cpu, bool isjson, bool precom)
- {
- if (opt_n_threads <= 0 || cpu < 0 || cpu >= num_processors)
- return;
- devstatus_an(io_data, &cpus[cpu], isjson, precom);
- }
- #endif
- static void
- devinfo_internal(void (*func)(struct io_data *, struct cgpu_info*, bool, bool), int msg, 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;
- int i;
- if (total_devices == 0) {
- message(io_data, MSG_NODEVS, 0, NULL, isjson);
- return;
- }
- message(io_data, msg, 0, NULL, isjson);
- if (isjson)
- io_open = io_add(io_data, COMSTR JSON_DEVS);
- for (i = 0; i < total_devices; ++i) {
- cgpu = get_devices(i);
- if (per_proc || cgpu->device == cgpu)
- func(io_data, cgpu, isjson, isjson && i > 0);
- }
- if (isjson && io_open)
- io_close(io_data);
- }
- static void devdetail(struct io_data *io_data, SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- return devinfo_internal(devdetail_an, MSG_DEVDETAILS, io_data, c, param, isjson, group);
- }
- static void devstatus(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- return devinfo_internal(devstatus_an, MSG_DEVS, io_data, c, param, isjson, group);
- }
- #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
- static void devscan(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
- {
- int n;
- bool io_open = false;
-
- applog(LOG_DEBUG, "RPC: request to scan %s for devices",
- param);
-
- if (param && !param[0])
- param = NULL;
-
- n = scan_serial(param);
-
- message(io_data, MSG_DEVSCAN, n, NULL, isjson);
-
- io_open = io_add(io_data, isjson ? COMSTR JSON_DEVS : _DEVS COMSTR);
- n = total_devices - n;
- for (int i = n; i < total_devices; ++i)
- devdetail_an(io_data, get_devices(i), isjson, i > n);
-
- if (isjson && io_open)
- io_close(io_data);
- }
- #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, *proc;
- int numpga = numpgas();
- int id;
- bool already;
- 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 %s id %d device %d %s",
- per_proc ? "proc" : "dev", id, dev, cgpu->proc_repr_ns);
- already = true;
- int procs = per_proc ? 1 : cgpu->procs, i;
- for (i = 0, proc = cgpu; i < procs; ++i, proc = proc->next_proc)
- {
- if (proc->deven == DEV_DISABLED)
- {
- proc_enable(proc);
- already = false;
- }
- }
-
- if (already)
- {
- 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
- 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, *proc;
- int numpga = numpgas();
- int id;
- bool already;
- 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 %s id %d device %d %s",
- per_proc ? "proc" : "dev", id, dev, cgpu->proc_repr_ns);
- already = true;
- int procs = per_proc ? 1 : cgpu->procs, i;
- for (i = 0, proc = cgpu; i < procs; ++i, proc = proc->next_proc)
- {
- if (proc->deven != DEV_DISABLED)
- {
- cgpu->deven = DEV_DISABLED;
- already = false;
- }
- }
-
- if (already)
- {
- message(io_data, MSG_PGALRDIS, id, NULL, isjson);
- return;
- }
- 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 && drv->identify_device(cgpu))
- message(io_data, MSG_PGAIDENT, id, NULL, isjson);
- else
- message(io_data, MSG_PGANOID, 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_int(root, "Quota", &pool->quota, 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_escape(root, "Proxy", pool->rpc_proxy, false);
- } else {
- 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_uint64(root, "Best Share", &(pool->best_diff), true);
- if (pool->admin_msg)
- root = api_add_escape(root, "Message", pool->admin_msg, true);
- double rejp = (pool->diff_accepted + pool->diff_rejected + pool->diff_stale) ?
- (double)(pool->diff_rejected) / (double)(pool->diff_accepted + pool->diff_rejected + pool->diff_stale) : 0;
- root = api_add_percent(root, "Pool Rejected%", &rejp, false);
- double stalep = (pool->diff_accepted + pool->diff_rejected + pool->diff_stale) ?
- (double)(pool->diff_stale) / (double)(pool->diff_accepted + pool->diff_rejected + pool->diff_stale) : 0;
- root = api_add_percent(root, "Pool Stale%", &stalep, false);
- 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
- if (opt_n_threads)
- root = api_add_string(root, "Algorithm", algo, false);
- #endif
- 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, &(total_rolling), 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_int(root, "Diff1 Work", &total_diff1, 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);
- double hwp = (total_bad_nonces + total_diff1) ?
- (double)(total_bad_nonces) / (double)(total_bad_nonces + total_diff1) : 0;
- root = api_add_percent(root, "Device Hardware%", &hwp, false);
- double rejp = total_diff1 ?
- (double)(total_diff_rejected) / (double)(total_diff1) : 0;
- root = api_add_percent(root, "Device Rejected%", &rejp, false);
- double prejp = (total_diff_accepted + total_diff_rejected + total_diff_stale) ?
- (double)(total_diff_rejected) / (double)(total_diff_accepted + total_diff_rejected + total_diff_stale) : 0;
- root = api_add_percent(root, "Pool Rejected%", &prejp, false);
- double stalep = (total_diff_accepted + total_diff_rejected + total_diff_stale) ?
- (double)(total_diff_stale) / (double)(total_diff_accepted + total_diff_rejected + total_diff_stale) : 0;
- root = api_add_percent(root, "Pool Stale%", &stalep, false);
- 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)
- {
- int id;
- if (!nDevs) {
- 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",
- id, gpus[id].proc_repr_ns);
- if (gpus[id].deven != DEV_DISABLED) {
- message(io_data, MSG_ALRENA, id, NULL, isjson);
- return;
- }
- if (gpus[id].status != LIFE_WELL)
- {
- message(io_data, MSG_GPUMRE, id, NULL, isjson);
- return;
- }
- proc_enable(&gpus[id]);
- 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",
- id, gpus[id].proc_repr_ns);
- 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);
- }
- #ifdef WANT_CPUMINE
- static void cpuenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- 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 >= opt_n_threads) {
- message(io_data, MSG_INVCPU, id, NULL, isjson);
- return;
- }
- applog(LOG_DEBUG, "API: request to cpuenable cpuid %d %s",
- id, cpus[id].proc_repr_ns);
- if (cpus[id].deven != DEV_DISABLED) {
- message(io_data, MSG_ALRENAC, id, NULL, isjson);
- return;
- }
- if (cpus[id].status != LIFE_WELL)
- {
- message(io_data, MSG_CPUMRE, id, NULL, isjson);
- return;
- }
- proc_enable(&cpus[id]);
- message(io_data, MSG_CPUREN, id, NULL, isjson);
- }
- static void cpudisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- 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 >= opt_n_threads) {
- message(io_data, MSG_INVCPU, id, NULL, isjson);
- return;
- }
- applog(LOG_DEBUG, "API: request to cpudisable cpuid %d %s",
- id, cpus[id].proc_repr_ns);
- if (cpus[id].deven == DEV_DISABLED) {
- message(io_data, MSG_ALRDISC, id, NULL, isjson);
- return;
- }
- cpus[id].deven = DEV_DISABLED;
- message(io_data, MSG_CPUDIS, id, NULL, isjson);
- }
- static void cpurestart(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- 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 >= opt_n_threads) {
- message(io_data, MSG_INVCPU, id, NULL, isjson);
- return;
- }
- reinit_device(&cpus[id]);
- message(io_data, MSG_CPUREI, id, NULL, isjson);
- }
- #endif
- 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);
- cg_rlock(&control_lock);
- if (id < 0 || id >= total_pools) {
- cg_runlock(&control_lock);
- message(io_data, MSG_INVPID, id, NULL, isjson);
- return;
- }
- pool = pools[id];
- pool->enabled = POOL_ENABLED;
- cg_runlock(&control_lock);
- 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)
- {
- int i;
- switch (prioritize_pools(param, &i)) {
- case MSG_NOPOOL:
- message(io_data, MSG_NOPOOL, 0, NULL, isjson);
- return;
- case MSG_MISPID:
- message(io_data, MSG_MISPID, 0, NULL, isjson);
- return;
- case MSG_INVPID:
- message(io_data, MSG_INVPID, i, NULL, isjson);
- return;
- case MSG_DUPPID:
- message(io_data, MSG_DUPPID, i, NULL, isjson);
- return;
- case MSG_POOLPRIO:
- default:
- message(io_data, MSG_POOLPRIO, 0, NULL, isjson);
- return;
- }
- }
- static void poolquota(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
- {
- struct pool *pool;
- int quota, id;
- char *comma;
- 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;
- }
- comma = strchr(param, ',');
- if (!comma) {
- message(io_data, MSG_CONVAL, 0, param, isjson);
- return;
- }
- *(comma++) = '\0';
- id = atoi(param);
- if (id < 0 || id >= total_pools) {
- message(io_data, MSG_INVPID, id, NULL, isjson);
- return;
- }
- pool = pools[id];
- quota = atoi(comma);
- if (quota < 0) {
- message(io_data, MSG_INVNEG, quota, pool->rpc_url, isjson);
- return;
- }
- pool->quota = quota;
- adjust_quota_gcd();
- message(io_data, MSG_SETQUOTA, quota, pool->rpc_url, 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 cgpu_info *proc;
- struct api_data *root = NULL;
- char buf[TMPBUFSIZ];
- char *reason;
-
- time_t last_not_well = 0;
- enum dev_reason uninitialised_var(enum_reason);
- int thread_fail_init_count = 0, thread_zero_hash_count = 0, thread_fail_queue_count = 0;
- int dev_sick_idle_60_count = 0, dev_dead_idle_600_count = 0;
- int dev_nostart_count = 0, dev_over_heat_count = 0, dev_thermal_cutoff_count = 0, dev_comms_error_count = 0, dev_throttle_count = 0;
- int procs = per_proc ? 1 : cgpu->procs, i;
- for (i = 0, proc = cgpu; i < procs; ++i, proc = proc->next_proc)
- {
- if (proc->device_last_not_well > last_not_well)
- {
- last_not_well = proc->device_last_not_well;
- enum_reason = proc->device_not_well_reason;
- thread_fail_init_count += proc->thread_fail_init_count;
- thread_zero_hash_count += proc->thread_zero_hash_count;
- thread_fail_queue_count += proc->thread_fail_queue_count;
- dev_sick_idle_60_count += proc->dev_sick_idle_60_count;
- dev_dead_idle_600_count += proc->dev_dead_idle_600_count;
- dev_nostart_count += proc->dev_nostart_count;
- dev_over_heat_count += proc->dev_over_heat_count;
- dev_thermal_cutoff_count += proc->dev_thermal_cutoff_count;
- dev_comms_error_count += proc->dev_comms_error_count;
- dev_throttle_count += proc->dev_throttle_count;
- }
- if (per_proc)
- break;
- }
-
- if (last_not_well == 0)
- reason = REASON_NONE;
- else
- switch (enum_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_device_identifier(root, cgpu);
- if (per_proc)
- root = api_add_time(root, "Last Well", &(cgpu->device_last_well), false);
- root = api_add_time(root, "Last Not Well", &last_not_well, false);
- root = api_add_string(root, "Reason Not Well", reason, false);
- root = api_add_int(root, "*Thread Fail Init", &thread_fail_init_count, false);
- root = api_add_int(root, "*Thread Zero Hash", &thread_zero_hash_count, false);
- root = api_add_int(root, "*Thread Fail Queue", &thread_fail_queue_count, false);
- root = api_add_int(root, "*Dev Sick Idle 60s", &dev_sick_idle_60_count, false);
- root = api_add_int(root, "*Dev Dead Idle 600s", &dev_dead_idle_600_count, false);
- root = api_add_int(root, "*Dev Nostart", &dev_nostart_count, false);
- root = api_add_int(root, "*Dev Over Heat", &dev_over_heat_count, false);
- root = api_add_int(root, "*Dev Thermal Cutoff", &dev_thermal_cutoff_count, false);
- root = api_add_int(root, "*Dev Comms Error", &dev_comms_error_count, false);
- root = api_add_int(root, "*Dev Throttle", &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, n = 0;
- 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);
- if (cgpu->device == cgpu || per_proc)
- notifystatus(io_data, n++, cgpu, isjson, group);
- }
- 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];
- double elapsed;
- root = api_add_int(root, "STATS", &i, false);
- root = api_add_string(root, "ID", id, false);
- elapsed = stats_elapsed(stats);
- root = api_add_elapsed(root, "Elapsed", &elapsed, 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;
- i = itemstats(io_data, i, cgpu->proc_repr_ns, &(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);
- cg_rlock(&ch_lock);
- if (current_fullhash && *current_fullhash) {
- root = api_add_time(root, "Current Block Time", &block_time, true);
- root = api_add_string(root, "Current Block Hash", current_fullhash, true);
- } else {
- time_t t = 0;
- root = api_add_time(root, "Current Block Time", &t, true);
- root = api_add_const(root, "Current Block Hash", BLANK, false);
- }
- cg_runlock(&ch_lock);
- root = api_add_bool(root, "LP", &have_longpoll, false);
- root = api_add_diff(root, "Network Difficulty", ¤t_diff, true);
- 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);
- }
- extern void stratumsrv_change_port();
- 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';
- #if BLKMAKER_VERSION > 0
- if (strcasecmp(param, "coinbase-sig") == 0) {
- free(opt_coinbase_sig);
- opt_coinbase_sig = strdup(comma);
- message(io_data, MSG_SETCONFIG, 1, param, isjson);
- return;
- }
- #endif
- 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;
- #ifdef USE_LIBMICROHTTPD
- else if (strcasecmp(param, "http-port") == 0)
- {
- httpsrv_stop();
- httpsrv_port = value;
- if (httpsrv_port != -1)
- httpsrv_start(httpsrv_port);
- }
- #endif
- #ifdef USE_LIBEVENT
- else if (strcasecmp(param, "stratum-port") == 0)
- {
- stratumsrv_port = value;
- stratumsrv_change_port();
- }
- #endif
- else {
- message(io_data, MSG_UNKCON, 0, param, isjson);
- return;
- }
- message(io_data, MSG_SETCONFIG, value, param, isjson);
- }
- #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 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 },
- { "devscan", devscan, true },
- { "devs", devstatus, false },
- { "procs", 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 },
- { "proc", pgadev, false },
- { "procenable", pgaenable, true },
- { "procdisable", pgadisable, true },
- { "procidentify", pgaidentify, true },
- #endif
- #ifdef WANT_CPUMINE
- { "cpuenable", cpuenable, true },
- { "cpudisable", cpudisable, true },
- { "cpurestart", cpurestart, true },
- { "cpu", cpudev, false },
- #endif
- { "gpucount", gpucount, false },
- { "pgacount", pgacount, false },
- { "proccount", pgacount, false },
- { "cpucount", cpucount, false },
- { "switchpool", switchpool, true },
- { "addpool", addpool, true },
- { "poolpriority", poolpriority, true },
- { "poolquota", poolquota, 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 },
- { "procnotify", notify, false },
- { "devdetails", devdetail, false },
- { "procdetails", devdetail, false },
- { "restart", dorestart, true },
- { "stats", minerstats, false },
- { "check", checkcommand, false },
- { "failover-only", failoveronly, true },
- { "coin", minecoin, false },
- { "debug", debugstate, true },
- { "setconfig", setconfig, true },
- #ifdef HAVE_AN_FPGA
- { "pgaset", pgaset, true },
- { "procset", pgaset, true },
- #endif
- { "zero", dozero, 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)
- {
- if (io_data->close)
- io_add(io_data, JSON_CLOSE);
-
- if (isjson)
- io_add(io_data, JSON_END);
-
- bytes_nullterminate(&io_data->data);
- applog(LOG_DEBUG, "API: send reply: (%ld) '%.10s%s'",
- (long)bytes_len(&io_data->data),
- bytes_buf(&io_data->data),
- bytes_len(&io_data->data) > 10 ? "..." : BLANK);
-
- io_flush(io_data, true);
-
- if (bytes_len(&io_data->data))
- applog(LOG_WARNING, "RPC: Timed out with %ld bytes left to send",
- (long)bytes_len(&io_data->data));
- }
- static void tidyup(__maybe_unused void *arg)
- {
- mutex_lock(&quit_restart_lock);
- SOCKETTYPE *apisock = (SOCKETTYPE *)arg;
- bye = true;
- if (*apisock != INVSOCK) {
- shutdown(*apisock, SHUT_RDWR);
- CLOSESOCKET(*apisock);
- *apisock = 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';
- quit(1, "API invalid group name '%s'", ptr);
- }
- group = GROUP(*ptr);
- if (!VALIDGROUP(group))
- quit(1, "API invalid group name '%c'", *ptr);
- if (group == PRIVGROUP)
- quit(1, "API group name can't be '%c'", PRIVGROUP);
- if (group == NOPRIVGROUP)
- quit(1, "API group name can't be '%c'", NOPRIVGROUP);
- if (apigroups[GROUPOFFSET(group)].commands != NULL)
- quit(1, "API duplicate group name '%c'", *ptr);
- 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 {
- quit(1, "API unknown command '%s' in group '%c'", ptr, group);
- }
- }
- 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 (VALIDGROUP(*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)
- {
- RenameThread("rpc_quit");
- // 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 BFGMiner");
- kill_work();
- return NULL;
- }
- static void *restart_thread(__maybe_unused void *userdata)
- {
- RenameThread("rpc_restart");
- // 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 BFGMiner");
- app_restart();
- return NULL;
- }
- static bool check_connect(struct sockaddr_in *cli, char **connectaddr, char *group)
- {
- bool addrok = false;
- int i;
- *connectaddr = inet_ntoa(cli->sin_addr);
- *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);
- }
- return addrok;
- }
- static void mcast()
- {
- struct sockaddr_in listen;
- struct ip_mreq grp;
- struct sockaddr_in came_from;
- time_t bindstart;
- const char *binderror;
- SOCKETTYPE mcast_sock;
- SOCKETTYPE reply_sock;
- socklen_t came_from_siz;
- char *connectaddr;
- ssize_t rep;
- int bound;
- int count;
- int reply_port;
- bool addrok;
- char group;
- char expect[] = "cgminer-"; // first 8 bytes constant
- char *expect_code;
- size_t expect_code_len;
- char buf[1024];
- char replybuf[1024];
- memset(&grp, 0, sizeof(grp));
- grp.imr_multiaddr.s_addr = inet_addr(opt_api_mcast_addr);
- if (grp.imr_multiaddr.s_addr == INADDR_NONE)
- quit(1, "Invalid Multicast Address");
- grp.imr_interface.s_addr = INADDR_ANY;
- mcast_sock = socket(AF_INET, SOCK_DGRAM, 0);
- int optval = 1;
- if (SOCKETFAIL(setsockopt(mcast_sock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval)))) {
- applog(LOG_ERR, "API mcast setsockopt SO_REUSEADDR failed (%s)%s", SOCKERRMSG, MUNAVAILABLE);
- goto die;
- }
- memset(&listen, 0, sizeof(listen));
- listen.sin_family = AF_INET;
- listen.sin_addr.s_addr = INADDR_ANY;
- listen.sin_port = htons(opt_api_mcast_port);
- // 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(mcast_sock, (struct sockaddr *)(&listen), sizeof(listen)))) {
- binderror = SOCKERRMSG;
- if ((time(NULL) - bindstart) > 61)
- break;
- else
- cgsleep_ms(30000);
- } else
- bound = 1;
- }
- if (bound == 0) {
- applog(LOG_ERR, "API mcast bind to port %d failed (%s)%s", opt_api_port, binderror, MUNAVAILABLE);
- goto die;
- }
- if (SOCKETFAIL(setsockopt(mcast_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)(&grp), sizeof(grp)))) {
- applog(LOG_ERR, "API mcast join failed (%s)%s", SOCKERRMSG, MUNAVAILABLE);
- goto die;
- }
- expect_code_len = sizeof(expect) + strlen(opt_api_mcast_code);
- expect_code = malloc(expect_code_len+1);
- if (!expect_code)
- quit(1, "Failed to malloc mcast expect_code");
- snprintf(expect_code, expect_code_len+1, "%s%s-", expect, opt_api_mcast_code);
- count = 0;
- while (80085) {
- cgsleep_ms(1000);
- count++;
- came_from_siz = sizeof(came_from);
- if (SOCKETFAIL(rep = recvfrom(mcast_sock, buf, sizeof(buf),
- 0, (struct sockaddr *)(&came_from), &came_from_siz))) {
- applog(LOG_DEBUG, "API mcast failed count=%d (%s) (%d)",
- count, SOCKERRMSG, (int)mcast_sock);
- continue;
- }
- addrok = check_connect(&came_from, &connectaddr, &group);
- applog(LOG_DEBUG, "API mcast from %s - %s",
- connectaddr, addrok ? "Accepted" : "Ignored");
- if (!addrok)
- continue;
- buf[rep] = '\0';
- if (rep > 0 && buf[rep-1] == '\n')
- buf[--rep] = '\0';
- applog(LOG_DEBUG, "API mcast request rep=%d (%s) from %s:%d",
- (int)rep, buf,
- inet_ntoa(came_from.sin_addr),
- ntohs(came_from.sin_port));
- if ((size_t)rep > expect_code_len && memcmp(buf, expect_code, expect_code_len) == 0) {
- reply_port = atoi(&buf[expect_code_len]);
- if (reply_port < 1 || reply_port > 65535) {
- applog(LOG_DEBUG, "API mcast request ignored - invalid port (%s)",
- &buf[expect_code_len]);
- } else {
- applog(LOG_DEBUG, "API mcast request OK port %s=%d",
- &buf[expect_code_len], reply_port);
- came_from.sin_port = htons(reply_port);
- reply_sock = socket(AF_INET, SOCK_DGRAM, 0);
- snprintf(replybuf, sizeof(replybuf),
- "cgm-%s-%d-%s",
- opt_api_mcast_code,
- opt_api_port, opt_api_mcast_des);
- rep = sendto(reply_sock, replybuf, strlen(replybuf)+1,
- 0, (struct sockaddr *)(&came_from),
- sizeof(came_from));
- if (SOCKETFAIL(rep)) {
- applog(LOG_DEBUG, "API mcast send reply failed (%s) (%d)",
- SOCKERRMSG, (int)reply_sock);
- } else {
- applog(LOG_DEBUG, "API mcast send reply (%s) succeeded (%d) (%d)",
- replybuf, (int)rep, (int)reply_sock);
- }
- CLOSESOCKET(reply_sock);
- }
- } else
- applog(LOG_DEBUG, "API mcast request was no good");
- }
- die:
- CLOSESOCKET(mcast_sock);
- }
- static void *mcast_thread(void *userdata)
- {
- pthread_detach(pthread_self());
- pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
- RenameThread("api_mcast");
- mcast();
- return NULL;
- }
- void mcast_init()
- {
- struct thr_info *thr;
- thr = calloc(1, sizeof(*thr));
- if (!thr)
- quit(1, "Failed to calloc mcast thr");
- if (thr_info_create(thr, NULL, mcast_thread, thr))
- quit(1, "API mcast thread create failed");
- }
- void api(int api_thr_id)
- {
- struct io_data *io_data;
- struct thr_info bye_thr;
- char buf[TMPBUFSIZ];
- char param_buf[TMPBUFSIZ];
- SOCKETTYPE c;
- int n, bound;
- char *connectaddr;
- const char *binderror;
- struct timeval 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;
- SOCKETTYPE *apisock;
- if (!opt_api_listen) {
- applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
- return;
- }
- apisock = malloc(sizeof(*apisock));
- *apisock = INVSOCK;
- rpc_io_data =
- io_data = sock_io_new();
- mutex_init(&quit_restart_lock);
- pthread_cleanup_push(tidyup, (void *)apisock);
- 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 */
- cgsleep_ms(opt_log_interval*1000);
- *apisock = socket(AF_INET, SOCK_STREAM, 0);
- if (*apisock == 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(*apisock, 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;
- cgtime(&bindstart);
- while (bound == 0) {
- if (SOCKETFAIL(bind(*apisock, (struct sockaddr *)(&serv), sizeof(serv)))) {
- binderror = SOCKERRMSG;
- if (timer_elapsed(&bindstart, NULL) > 61)
- break;
- else {
- applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
- cgsleep_ms(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(*apisock, QUEUE))) {
- applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
- CLOSESOCKET(*apisock);
- 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);
- }
- if (opt_api_mcast)
- mcast_init();
- while (!bye) {
- clisiz = sizeof(cli);
- if (SOCKETFAIL(c = accept(*apisock, (struct sockaddr *)(&cli), &clisiz))) {
- applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
- goto die;
- }
- addrok = check_connect(&cli, &connectaddr, &group);
- 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);
- io_data->sock = c;
- 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))
- {
- per_proc = !strncmp(cmds[i].name, "proc", 4);
- (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 (isjson)
- json_decref(json_config);
- 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);
- }
|