api.c 123 KB

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