api.c 134 KB

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