api.c 139 KB

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