api.c 135 KB

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