api.c 124 KB

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