api.c 132 KB

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