api.c 135 KB

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