api.c 135 KB

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